blob: 96f9abafbff70b962e10ddcd89b9459d3f707a69 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
Vladimir Marko60584552015-09-03 13:35:12 +000029#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010030#include "base/transform_array_ref.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010031#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080032#include "dex_file_types.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000033#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000034#include "handle.h"
35#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000036#include "invoke_type.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010037#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010038#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000039#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000040#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010041#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070042#include "primitive.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010043#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044
45namespace art {
46
David Brazdil1abb4192015-02-17 18:33:36 +000047class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000048class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010049class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010051class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000053class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000055class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000056class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000057class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000058class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000059class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010060class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010061class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010062class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010063class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000064class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010065class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000066class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067
Mathieu Chartier736b5602015-09-02 14:54:11 -070068namespace mirror {
69class DexCache;
70} // namespace mirror
71
Nicolas Geoffray818f2102014-02-18 16:43:35 +000072static const int kDefaultNumberOfBlocks = 8;
73static const int kDefaultNumberOfSuccessors = 2;
74static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010075static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010076static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000077static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000078
Roland Levillain5b5b9312016-03-22 14:57:31 +000079// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
80static constexpr int32_t kMaxIntShiftDistance = 0x1f;
81// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
82static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000083
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010084static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070085static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010086
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010087static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
88
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060089static constexpr uint32_t kNoDexPc = -1;
90
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010091inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
92 // For the purposes of the compiler, the dex files must actually be the same object
93 // if we want to safely treat them as the same. This is especially important for JIT
94 // as custom class loaders can open the same underlying file (or memory) multiple
95 // times and provide different class resolution but no two class loaders should ever
96 // use the same DexFile object - doing so is an unsupported hack that can lead to
97 // all sorts of weird failures.
98 return &lhs == &rhs;
99}
100
Dave Allison20dfc792014-06-16 20:44:29 -0700101enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700102 // All types.
103 kCondEQ, // ==
104 kCondNE, // !=
105 // Signed integers and floating-point numbers.
106 kCondLT, // <
107 kCondLE, // <=
108 kCondGT, // >
109 kCondGE, // >=
110 // Unsigned integers.
111 kCondB, // <
112 kCondBE, // <=
113 kCondA, // >
114 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100115 // First and last aliases.
116 kCondFirst = kCondEQ,
117 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700118};
119
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000120enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000121 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000122 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000123 kAnalysisFailThrowCatchLoop,
124 kAnalysisFailAmbiguousArrayOp,
125 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000126};
127
Andreas Gampe9186ced2016-12-12 14:28:21 -0800128template <typename T>
129static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
130 return static_cast<typename std::make_unsigned<T>::type>(x);
131}
132
Vladimir Markof9f64412015-09-02 14:05:49 +0100133class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100134 public:
135 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
136
137 void AddInstruction(HInstruction* instruction);
138 void RemoveInstruction(HInstruction* instruction);
139
David Brazdilc3d743f2015-04-22 13:40:50 +0100140 // Insert `instruction` before/after an existing instruction `cursor`.
141 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
142 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
143
Roland Levillain6b469232014-09-25 10:10:38 +0100144 // Return true if this list contains `instruction`.
145 bool Contains(HInstruction* instruction) const;
146
Roland Levillainccc07a92014-09-16 14:48:16 +0100147 // Return true if `instruction1` is found before `instruction2` in
148 // this instruction list and false otherwise. Abort if none
149 // of these instructions is found.
150 bool FoundBefore(const HInstruction* instruction1,
151 const HInstruction* instruction2) const;
152
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000153 bool IsEmpty() const { return first_instruction_ == nullptr; }
154 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
155
156 // Update the block of all instructions to be `block`.
157 void SetBlockOfInstructions(HBasicBlock* block) const;
158
159 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000160 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000161 void Add(const HInstructionList& instruction_list);
162
David Brazdil2d7352b2015-04-20 14:52:42 +0100163 // Return the number of instructions in the list. This is an expensive operation.
164 size_t CountSize() const;
165
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100166 private:
167 HInstruction* first_instruction_;
168 HInstruction* last_instruction_;
169
170 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000171 friend class HGraph;
172 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100173 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000174 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100175 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100176
177 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
178};
179
David Brazdil4833f5a2015-12-16 10:37:39 +0000180class ReferenceTypeInfo : ValueObject {
181 public:
182 typedef Handle<mirror::Class> TypeHandle;
183
Vladimir Markoa1de9182016-02-25 11:37:38 +0000184 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
185
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700186 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100187 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
188 }
189
Vladimir Markoa1de9182016-02-25 11:37:38 +0000190 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000191 return ReferenceTypeInfo(type_handle, is_exact);
192 }
193
194 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
195
Vladimir Markof39745e2016-01-26 12:16:55 +0000196 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000197 return handle.GetReference() != nullptr;
198 }
199
Vladimir Marko456307a2016-04-19 14:12:13 +0000200 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000201 return IsValidHandle(type_handle_);
202 }
203
204 bool IsExact() const { return is_exact_; }
205
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700206 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000207 DCHECK(IsValid());
208 return GetTypeHandle()->IsObjectClass();
209 }
210
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700211 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000212 DCHECK(IsValid());
213 return GetTypeHandle()->IsStringClass();
214 }
215
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000217 DCHECK(IsValid());
218 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
219 }
220
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000222 DCHECK(IsValid());
223 return GetTypeHandle()->IsInterface();
224 }
225
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700226 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000227 DCHECK(IsValid());
228 return GetTypeHandle()->IsArrayClass();
229 }
230
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000232 DCHECK(IsValid());
233 return GetTypeHandle()->IsPrimitiveArray();
234 }
235
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000237 DCHECK(IsValid());
238 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
239 }
240
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700241 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 DCHECK(IsValid());
243 if (!IsExact()) return false;
244 if (!IsArrayClass()) return false;
245 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
246 }
247
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700248 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000249 DCHECK(IsValid());
250 if (!IsExact()) return false;
251 if (!IsArrayClass()) return false;
252 if (!rti.IsArrayClass()) return false;
253 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
254 rti.GetTypeHandle()->GetComponentType());
255 }
256
257 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
258
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700259 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000260 DCHECK(IsValid());
261 DCHECK(rti.IsValid());
262 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
263 }
264
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700265 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000266 DCHECK(IsValid());
267 DCHECK(rti.IsValid());
268 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
269 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
270 }
271
272 // Returns true if the type information provide the same amount of details.
273 // Note that it does not mean that the instructions have the same actual type
274 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700275 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000276 if (!IsValid() && !rti.IsValid()) {
277 // Invalid types are equal.
278 return true;
279 }
280 if (!IsValid() || !rti.IsValid()) {
281 // One is valid, the other not.
282 return false;
283 }
284 return IsExact() == rti.IsExact()
285 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
286 }
287
288 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000289 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
290 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
291 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000292
293 // The class of the object.
294 TypeHandle type_handle_;
295 // Whether or not the type is exact or a superclass of the actual type.
296 // Whether or not we have any information about this type.
297 bool is_exact_;
298};
299
300std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
301
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000302// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100303class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100305 HGraph(ArenaAllocator* arena,
306 const DexFile& dex_file,
307 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100308 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700309 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100310 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100311 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000312 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100313 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000314 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100315 blocks_(arena->Adapter(kArenaAllocBlockList)),
316 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
317 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700318 entry_block_(nullptr),
319 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100320 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100321 number_of_vregs_(0),
322 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000323 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400324 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000325 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000326 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000327 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000328 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100329 dex_file_(dex_file),
330 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100331 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100332 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100333 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800334 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700335 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000336 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100337 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
338 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
339 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
340 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000341 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000342 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700343 osr_(osr),
344 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100345 blocks_.reserve(kDefaultNumberOfBlocks);
346 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000347
David Brazdilbadd8262016-02-02 16:28:56 +0000348 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700349 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000350
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000351 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100352 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
353
David Brazdil69ba7b72015-06-23 18:27:30 +0100354 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000355 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100356
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000357 HBasicBlock* GetEntryBlock() const { return entry_block_; }
358 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100359 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000360
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000361 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
362 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000363
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000364 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100365
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100366 void ComputeDominanceInformation();
367 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000368 void ClearLoopInformation();
369 void FindBackEdges(ArenaBitVector* visited);
370 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100371 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100372 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373
David Brazdil4833f5a2015-12-16 10:37:39 +0000374 // Analyze all natural loops in this graph. Returns a code specifying that it
375 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000376 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000377 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100378
David Brazdilffee3d32015-07-06 11:48:53 +0100379 // Iterate over blocks to compute try block membership. Needs reverse post
380 // order and loop information.
381 void ComputeTryBlockInformation();
382
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000383 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000384 // Returns the instruction to replace the invoke expression or null if the
385 // invoke is for a void method. Note that the caller is responsible for replacing
386 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000387 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000388
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000389 // Update the loop and try membership of `block`, which was spawned from `reference`.
390 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
391 // should be the new back edge.
392 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
393 HBasicBlock* reference,
394 bool replace_if_back_edge);
395
Mingyao Yang3584bce2015-05-19 16:01:59 -0700396 // Need to add a couple of blocks to test if the loop body is entered and
397 // put deoptimization instructions, etc.
398 void TransformLoopHeaderForBCE(HBasicBlock* header);
399
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000400 // Removes `block` from the graph. Assumes `block` has been disconnected from
401 // other blocks and has no instructions or phis.
402 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000403
David Brazdilfc6a86a2015-06-26 10:33:45 +0000404 // Splits the edge between `block` and `successor` while preserving the
405 // indices in the predecessor/successor lists. If there are multiple edges
406 // between the blocks, the lowest indices are used.
407 // Returns the new block which is empty and has the same dex pc as `successor`.
408 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
409
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100410 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
411 void SimplifyLoop(HBasicBlock* header);
412
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000413 int32_t GetNextInstructionId() {
414 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000415 return current_instruction_id_++;
416 }
417
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000418 int32_t GetCurrentInstructionId() const {
419 return current_instruction_id_;
420 }
421
422 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000423 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000424 current_instruction_id_ = id;
425 }
426
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100427 uint16_t GetMaximumNumberOfOutVRegs() const {
428 return maximum_number_of_out_vregs_;
429 }
430
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000431 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
432 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100433 }
434
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100435 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
436 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
437 }
438
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000439 void UpdateTemporariesVRegSlots(size_t slots) {
440 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100441 }
442
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000443 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100444 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000445 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100446 }
447
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100448 void SetNumberOfVRegs(uint16_t number_of_vregs) {
449 number_of_vregs_ = number_of_vregs;
450 }
451
452 uint16_t GetNumberOfVRegs() const {
453 return number_of_vregs_;
454 }
455
456 void SetNumberOfInVRegs(uint16_t value) {
457 number_of_in_vregs_ = value;
458 }
459
David Brazdildee58d62016-04-07 09:54:26 +0000460 uint16_t GetNumberOfInVRegs() const {
461 return number_of_in_vregs_;
462 }
463
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100464 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100465 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100466 return number_of_vregs_ - number_of_in_vregs_;
467 }
468
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100469 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100470 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100471 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100472
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100473 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
474 DCHECK(GetReversePostOrder()[0] == entry_block_);
475 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
476 }
477
478 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
479 return ReverseRange(GetReversePostOrder());
480 }
481
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100482 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100483 return linear_order_;
484 }
485
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100486 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
487 return ReverseRange(GetLinearOrder());
488 }
489
Mark Mendell1152c922015-04-24 17:06:35 -0400490 bool HasBoundsChecks() const {
491 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800492 }
493
Mark Mendell1152c922015-04-24 17:06:35 -0400494 void SetHasBoundsChecks(bool value) {
495 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800496 }
497
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100498 bool ShouldGenerateConstructorBarrier() const {
499 return should_generate_constructor_barrier_;
500 }
501
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000502 bool IsDebuggable() const { return debuggable_; }
503
David Brazdil8d5b8b22015-03-24 10:51:52 +0000504 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000505 // already, it is created and inserted into the graph. This method is only for
506 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600507 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000508
509 // TODO: This is problematic for the consistency of reference type propagation
510 // because it can be created anytime after the pass and thus it will be left
511 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600512 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000513
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600514 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
515 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000516 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600517 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
518 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000519 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600520 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
521 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000522 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600523 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
524 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000525 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000526
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100527 HCurrentMethod* GetCurrentMethod();
528
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100529 const DexFile& GetDexFile() const {
530 return dex_file_;
531 }
532
533 uint32_t GetMethodIdx() const {
534 return method_idx_;
535 }
536
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100537 InvokeType GetInvokeType() const {
538 return invoke_type_;
539 }
540
Mark Mendellc4701932015-04-10 13:18:51 -0400541 InstructionSet GetInstructionSet() const {
542 return instruction_set_;
543 }
544
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000545 bool IsCompilingOsr() const { return osr_; }
546
Mingyao Yang063fc772016-08-02 11:02:54 -0700547 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
548 return cha_single_implementation_list_;
549 }
550
551 void AddCHASingleImplementationDependency(ArtMethod* method) {
552 cha_single_implementation_list_.insert(method);
553 }
554
555 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800556 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700557 }
558
David Brazdil77a48ae2015-09-15 12:34:04 +0000559 bool HasTryCatch() const { return has_try_catch_; }
560 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100561
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000562 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
563 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
564
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100565 ArtMethod* GetArtMethod() const { return art_method_; }
566 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
567
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100568 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500569 // The instruction has been inserted into the graph, either as a constant, or
570 // before cursor.
571 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
572
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000573 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
574
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800575 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
576 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
577 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
578
David Brazdil2d7352b2015-04-20 14:52:42 +0100579 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000580 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100581 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000582
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000583 template <class InstructionType, typename ValueType>
584 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600585 ArenaSafeMap<ValueType, InstructionType*>* cache,
586 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000587 // Try to find an existing constant of the given value.
588 InstructionType* constant = nullptr;
589 auto cached_constant = cache->find(value);
590 if (cached_constant != cache->end()) {
591 constant = cached_constant->second;
592 }
593
594 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100595 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000596 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600597 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000598 cache->Overwrite(value, constant);
599 InsertConstant(constant);
600 }
601 return constant;
602 }
603
David Brazdil8d5b8b22015-03-24 10:51:52 +0000604 void InsertConstant(HConstant* instruction);
605
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000606 // Cache a float constant into the graph. This method should only be
607 // called by the SsaBuilder when creating "equivalent" instructions.
608 void CacheFloatConstant(HFloatConstant* constant);
609
610 // See CacheFloatConstant comment.
611 void CacheDoubleConstant(HDoubleConstant* constant);
612
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000613 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000614
615 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100616 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000617
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100618 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100619 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000620
Aart Bik281c6812016-08-26 11:31:48 -0700621 // List of blocks to perform a linear order tree traversal. Unlike the reverse
622 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100623 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100624
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000625 HBasicBlock* entry_block_;
626 HBasicBlock* exit_block_;
627
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100628 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100629 uint16_t maximum_number_of_out_vregs_;
630
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100631 // The number of virtual registers in this method. Contains the parameters.
632 uint16_t number_of_vregs_;
633
634 // The number of virtual registers used by parameters of this method.
635 uint16_t number_of_in_vregs_;
636
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000637 // Number of vreg size slots that the temporaries use (used in baseline compiler).
638 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100639
Mark Mendell1152c922015-04-24 17:06:35 -0400640 // Has bounds checks. We can totally skip BCE if it's false.
641 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800642
David Brazdil77a48ae2015-09-15 12:34:04 +0000643 // Flag whether there are any try/catch blocks in the graph. We will skip
644 // try/catch-related passes if false.
645 bool has_try_catch_;
646
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000647 // Flag whether there are any irreducible loops in the graph.
648 bool has_irreducible_loops_;
649
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000650 // Indicates whether the graph should be compiled in a way that
651 // ensures full debuggability. If false, we can apply more
652 // aggressive optimizations that may limit the level of debugging.
653 const bool debuggable_;
654
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000655 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000656 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000657
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100658 // The dex file from which the method is from.
659 const DexFile& dex_file_;
660
661 // The method index in the dex file.
662 const uint32_t method_idx_;
663
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100664 // If inlined, this encodes how the callee is being invoked.
665 const InvokeType invoke_type_;
666
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100667 // Whether the graph has been transformed to SSA form. Only used
668 // in debug mode to ensure we are not using properties only valid
669 // for non-SSA form (like the number of temporaries).
670 bool in_ssa_form_;
671
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100672 const bool should_generate_constructor_barrier_;
673
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800674 // Number of CHA guards in the graph. Used to short-circuit the
675 // CHA guard optimization pass when there is no CHA guard left.
676 uint32_t number_of_cha_guards_;
677
Mathieu Chartiere401d142015-04-22 13:56:20 -0700678 const InstructionSet instruction_set_;
679
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000680 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000681 HNullConstant* cached_null_constant_;
682 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000683 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000684 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000685 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000686
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100687 HCurrentMethod* cached_current_method_;
688
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100689 // The ArtMethod this graph is for. Note that for AOT, it may be null,
690 // for example for methods whose declaring class could not be resolved
691 // (such as when the superclass could not be found).
692 ArtMethod* art_method_;
693
David Brazdil4833f5a2015-12-16 10:37:39 +0000694 // Keep the RTI of inexact Object to avoid having to pass stack handle
695 // collection pointer to passes which may create NullConstant.
696 ReferenceTypeInfo inexact_object_rti_;
697
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000698 // Whether we are compiling this graph for on stack replacement: this will
699 // make all loops seen as irreducible and emit special stack maps to mark
700 // compiled code entries which the interpreter can directly jump to.
701 const bool osr_;
702
Mingyao Yang063fc772016-08-02 11:02:54 -0700703 // List of methods that are assumed to have single implementation.
704 ArenaSet<ArtMethod*> cha_single_implementation_list_;
705
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000706 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100707 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000708 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000709 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000710 DISALLOW_COPY_AND_ASSIGN(HGraph);
711};
712
Vladimir Markof9f64412015-09-02 14:05:49 +0100713class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000714 public:
715 HLoopInformation(HBasicBlock* header, HGraph* graph)
716 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100717 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000718 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100719 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100720 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100721 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000722 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100723 back_edges_.reserve(kDefaultNumberOfBackEdges);
724 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100725
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000726 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100727 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000728
729 void Dump(std::ostream& os);
730
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100731 HBasicBlock* GetHeader() const {
732 return header_;
733 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000734
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000735 void SetHeader(HBasicBlock* block) {
736 header_ = block;
737 }
738
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100739 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
740 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
741 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
742
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000743 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100744 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000745 }
746
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100747 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100748 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100749 }
750
David Brazdil46e2a392015-03-16 17:31:52 +0000751 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100752 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100753 }
754
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000755 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100756 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000757 }
758
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100759 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100760
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100761 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100762 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100763 }
764
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100765 // Returns the lifetime position of the back edge that has the
766 // greatest lifetime position.
767 size_t GetLifetimeEnd() const;
768
769 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100770 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100771 }
772
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000773 // Finds blocks that are part of this loop.
774 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100775
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100776 // Returns whether this loop information contains `block`.
777 // Note that this loop information *must* be populated before entering this function.
778 bool Contains(const HBasicBlock& block) const;
779
780 // Returns whether this loop information is an inner loop of `other`.
781 // Note that `other` *must* be populated before entering this function.
782 bool IsIn(const HLoopInformation& other) const;
783
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800784 // Returns true if instruction is not defined within this loop.
785 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700786
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100787 const ArenaBitVector& GetBlocks() const { return blocks_; }
788
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000789 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000790 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000791
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000792 void ClearAllBlocks() {
793 blocks_.ClearAllBits();
794 }
795
David Brazdil3f4a5222016-05-06 12:46:21 +0100796 bool HasBackEdgeNotDominatedByHeader() const;
797
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100798 bool IsPopulated() const {
799 return blocks_.GetHighestBitSet() != -1;
800 }
801
Anton Shaminf89381f2016-05-16 16:44:13 +0600802 bool DominatesAllBackEdges(HBasicBlock* block);
803
David Sehrc757dec2016-11-04 15:48:34 -0700804 bool HasExitEdge() const;
805
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000806 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100807 // Internal recursive implementation of `Populate`.
808 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100809 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100810
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000811 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100812 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000813 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100814 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100815 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100816 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000817
818 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
819};
820
David Brazdilec16f792015-08-19 15:04:01 +0100821// Stores try/catch information for basic blocks.
822// Note that HGraph is constructed so that catch blocks cannot simultaneously
823// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100824class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100825 public:
826 // Try block information constructor.
827 explicit TryCatchInformation(const HTryBoundary& try_entry)
828 : try_entry_(&try_entry),
829 catch_dex_file_(nullptr),
830 catch_type_index_(DexFile::kDexNoIndex16) {
831 DCHECK(try_entry_ != nullptr);
832 }
833
834 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800835 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100836 : try_entry_(nullptr),
837 catch_dex_file_(&dex_file),
838 catch_type_index_(catch_type_index) {}
839
840 bool IsTryBlock() const { return try_entry_ != nullptr; }
841
842 const HTryBoundary& GetTryEntry() const {
843 DCHECK(IsTryBlock());
844 return *try_entry_;
845 }
846
847 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
848
849 bool IsCatchAllTypeIndex() const {
850 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800851 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100852 }
853
Andreas Gampea5b09a62016-11-17 15:21:22 -0800854 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100855 DCHECK(IsCatchBlock());
856 return catch_type_index_;
857 }
858
859 const DexFile& GetCatchDexFile() const {
860 DCHECK(IsCatchBlock());
861 return *catch_dex_file_;
862 }
863
864 private:
865 // One of possibly several TryBoundary instructions entering the block's try.
866 // Only set for try blocks.
867 const HTryBoundary* try_entry_;
868
869 // Exception type information. Only set for catch blocks.
870 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800871 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100872};
873
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100874static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100875static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100876
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000877// A block in a method. Contains the list of instructions represented
878// as a double linked list. Each block knows its predecessors and
879// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100880
Vladimir Markof9f64412015-09-02 14:05:49 +0100881class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000882 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700883 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000884 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000885 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
886 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000887 loop_information_(nullptr),
888 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000889 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100890 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100891 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100892 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000893 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000894 try_catch_information_(nullptr) {
895 predecessors_.reserve(kDefaultNumberOfPredecessors);
896 successors_.reserve(kDefaultNumberOfSuccessors);
897 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
898 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000899
Vladimir Marko60584552015-09-03 13:35:12 +0000900 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100901 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000902 }
903
Vladimir Marko60584552015-09-03 13:35:12 +0000904 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100905 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000906 }
907
David Brazdild26a4112015-11-10 11:07:31 +0000908 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
909 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
910
Vladimir Marko60584552015-09-03 13:35:12 +0000911 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
912 return ContainsElement(successors_, block, start_from);
913 }
914
915 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100916 return dominated_blocks_;
917 }
918
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100919 bool IsEntryBlock() const {
920 return graph_->GetEntryBlock() == this;
921 }
922
923 bool IsExitBlock() const {
924 return graph_->GetExitBlock() == this;
925 }
926
David Brazdil46e2a392015-03-16 17:31:52 +0000927 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000928 bool IsSingleTryBoundary() const;
929
930 // Returns true if this block emits nothing but a jump.
931 bool IsSingleJump() const {
932 HLoopInformation* loop_info = GetLoopInformation();
933 return (IsSingleGoto() || IsSingleTryBoundary())
934 // Back edges generate a suspend check.
935 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
936 }
David Brazdil46e2a392015-03-16 17:31:52 +0000937
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000938 void AddBackEdge(HBasicBlock* back_edge) {
939 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000940 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000941 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100942 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000943 loop_information_->AddBackEdge(back_edge);
944 }
945
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000946 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000947 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000948
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100949 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000950 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600951 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000952
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000953 HBasicBlock* GetDominator() const { return dominator_; }
954 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000955 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
956
957 void RemoveDominatedBlock(HBasicBlock* block) {
958 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100959 }
Vladimir Marko60584552015-09-03 13:35:12 +0000960
961 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
962 ReplaceElement(dominated_blocks_, existing, new_block);
963 }
964
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100965 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000966
967 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100968 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000969 }
970
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100971 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
972 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100973 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100974 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100975 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
976 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000977
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000978 HInstruction* GetFirstInstructionDisregardMoves() const;
979
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000980 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000981 successors_.push_back(block);
982 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000983 }
984
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100985 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
986 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100987 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000988 new_block->predecessors_.push_back(this);
989 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000990 }
991
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000992 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
993 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000994 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000995 new_block->successors_.push_back(this);
996 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000997 }
998
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100999 // Insert `this` between `predecessor` and `successor. This method
1000 // preserves the indicies, and will update the first edge found between
1001 // `predecessor` and `successor`.
1002 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1003 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001004 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001005 successor->predecessors_[predecessor_index] = this;
1006 predecessor->successors_[successor_index] = this;
1007 successors_.push_back(successor);
1008 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001009 }
1010
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001011 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001012 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001013 }
1014
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001015 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001016 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001017 }
1018
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001019 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001020 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001021 }
1022
1023 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001024 predecessors_.push_back(block);
1025 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001026 }
1027
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001028 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001029 DCHECK_EQ(predecessors_.size(), 2u);
1030 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001031 }
1032
David Brazdil769c9e52015-04-27 13:54:09 +01001033 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001034 DCHECK_EQ(successors_.size(), 2u);
1035 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001036 }
1037
David Brazdilfc6a86a2015-06-26 10:33:45 +00001038 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001039 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001040 }
1041
David Brazdilfc6a86a2015-06-26 10:33:45 +00001042 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001043 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001044 }
1045
David Brazdilfc6a86a2015-06-26 10:33:45 +00001046 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001047 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001048 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001049 }
1050
1051 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001052 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001053 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001054 }
1055
1056 // Returns whether the first occurrence of `predecessor` in the list of
1057 // predecessors is at index `idx`.
1058 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001059 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001060 return GetPredecessorIndexOf(predecessor) == idx;
1061 }
1062
David Brazdild7558da2015-09-22 13:04:14 +01001063 // Create a new block between this block and its predecessors. The new block
1064 // is added to the graph, all predecessor edges are relinked to it and an edge
1065 // is created to `this`. Returns the new empty block. Reverse post order or
1066 // loop and try/catch information are not updated.
1067 HBasicBlock* CreateImmediateDominator();
1068
David Brazdilfc6a86a2015-06-26 10:33:45 +00001069 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001070 // created, latter block. Note that this method will add the block to the
1071 // graph, create a Goto at the end of the former block and will create an edge
1072 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001073 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001074 HBasicBlock* SplitBefore(HInstruction* cursor);
1075
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001076 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001077 // created block. Note that this method just updates raw block information,
1078 // like predecessors, successors, dominators, and instruction list. It does not
1079 // update the graph, reverse post order, loop information, nor make sure the
1080 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001081 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1082
1083 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1084 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001085
1086 // Merge `other` at the end of `this`. Successors and dominated blocks of
1087 // `other` are changed to be successors and dominated blocks of `this`. Note
1088 // that this method does not update the graph, reverse post order, loop
1089 // information, nor make sure the blocks are consistent (for example ending
1090 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001091 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001092
1093 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1094 // of `this` are moved to `other`.
1095 // Note that this method does not update the graph, reverse post order, loop
1096 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001097 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001098 void ReplaceWith(HBasicBlock* other);
1099
Aart Bik6b69e0a2017-01-11 10:20:43 -08001100 // Merges the instructions of `other` at the end of `this`.
1101 void MergeInstructionsWith(HBasicBlock* other);
1102
David Brazdil2d7352b2015-04-20 14:52:42 +01001103 // Merge `other` at the end of `this`. This method updates loops, reverse post
1104 // order, links to predecessors, successors, dominators and deletes the block
1105 // from the graph. The two blocks must be successive, i.e. `this` the only
1106 // predecessor of `other` and vice versa.
1107 void MergeWith(HBasicBlock* other);
1108
1109 // Disconnects `this` from all its predecessors, successors and dominator,
1110 // removes it from all loops it is included in and eventually from the graph.
1111 // The block must not dominate any other block. Predecessors and successors
1112 // are safely updated.
1113 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001114
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001115 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001116 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001117 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001118 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001119 // Replace instruction `initial` with `replacement` within this block.
1120 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1121 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001122 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001123 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001124 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1125 // instruction list. With 'ensure_safety' set to true, it verifies that the
1126 // instruction is not in use and removes it from the use lists of its inputs.
1127 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1128 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001129 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001130
1131 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001132 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001133 }
1134
Roland Levillain6b879dd2014-09-22 17:13:44 +01001135 bool IsLoopPreHeaderFirstPredecessor() const {
1136 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001137 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001138 }
1139
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001140 bool IsFirstPredecessorBackEdge() const {
1141 DCHECK(IsLoopHeader());
1142 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1143 }
1144
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001145 HLoopInformation* GetLoopInformation() const {
1146 return loop_information_;
1147 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001148
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001149 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001150 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001151 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001152 void SetInLoop(HLoopInformation* info) {
1153 if (IsLoopHeader()) {
1154 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001155 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001156 loop_information_ = info;
1157 } else if (loop_information_->Contains(*info->GetHeader())) {
1158 // Block is currently part of an outer loop. Make it part of this inner loop.
1159 // Note that a non loop header having a loop information means this loop information
1160 // has already been populated
1161 loop_information_ = info;
1162 } else {
1163 // Block is part of an inner loop. Do not update the loop information.
1164 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1165 // at this point, because this method is being called while populating `info`.
1166 }
1167 }
1168
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001169 // Raw update of the loop information.
1170 void SetLoopInformation(HLoopInformation* info) {
1171 loop_information_ = info;
1172 }
1173
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001174 bool IsInLoop() const { return loop_information_ != nullptr; }
1175
David Brazdilec16f792015-08-19 15:04:01 +01001176 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1177
1178 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1179 try_catch_information_ = try_catch_information;
1180 }
1181
1182 bool IsTryBlock() const {
1183 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1184 }
1185
1186 bool IsCatchBlock() const {
1187 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1188 }
David Brazdilffee3d32015-07-06 11:48:53 +01001189
1190 // Returns the try entry that this block's successors should have. They will
1191 // be in the same try, unless the block ends in a try boundary. In that case,
1192 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001193 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001194
David Brazdild7558da2015-09-22 13:04:14 +01001195 bool HasThrowingInstructions() const;
1196
David Brazdila4b8c212015-05-07 09:59:30 +01001197 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001198 bool Dominates(HBasicBlock* block) const;
1199
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001200 size_t GetLifetimeStart() const { return lifetime_start_; }
1201 size_t GetLifetimeEnd() const { return lifetime_end_; }
1202
1203 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1204 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1205
David Brazdil8d5b8b22015-03-24 10:51:52 +00001206 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001207 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001208 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001209 bool HasSinglePhi() const;
1210
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001211 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001212 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001213 ArenaVector<HBasicBlock*> predecessors_;
1214 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001215 HInstructionList instructions_;
1216 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001217 HLoopInformation* loop_information_;
1218 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001219 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001220 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001221 // The dex program counter of the first instruction of this block.
1222 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001223 size_t lifetime_start_;
1224 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001225 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001226
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001227 friend class HGraph;
1228 friend class HInstruction;
1229
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001230 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1231};
1232
David Brazdilb2bd1c52015-03-25 11:17:37 +00001233// Iterates over the LoopInformation of all loops which contain 'block'
1234// from the innermost to the outermost.
1235class HLoopInformationOutwardIterator : public ValueObject {
1236 public:
1237 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1238 : current_(block.GetLoopInformation()) {}
1239
1240 bool Done() const { return current_ == nullptr; }
1241
1242 void Advance() {
1243 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001244 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001245 }
1246
1247 HLoopInformation* Current() const {
1248 DCHECK(!Done());
1249 return current_;
1250 }
1251
1252 private:
1253 HLoopInformation* current_;
1254
1255 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1256};
1257
Alexandre Ramesef20f712015-06-09 10:29:30 +01001258#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001259 M(Above, Condition) \
1260 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001261 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001262 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001263 M(ArrayGet, Instruction) \
1264 M(ArrayLength, Instruction) \
1265 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001266 M(Below, Condition) \
1267 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001268 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001269 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001270 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001271 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001272 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001273 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001274 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001275 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001276 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001277 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001278 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001279 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001280 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001281 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001282 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001283 M(Exit, Instruction) \
1284 M(FloatConstant, Constant) \
1285 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001286 M(GreaterThan, Condition) \
1287 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001288 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001289 M(InstanceFieldGet, Instruction) \
1290 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001291 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001292 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001293 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001294 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001295 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001296 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001297 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001298 M(LessThan, Condition) \
1299 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001300 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001301 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001302 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001303 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001304 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001305 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001306 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001307 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001308 M(Neg, UnaryOperation) \
1309 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001310 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001311 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001312 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001313 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001314 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001315 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001316 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001317 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001318 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001319 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001320 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001321 M(Return, Instruction) \
1322 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001323 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001324 M(Shl, BinaryOperation) \
1325 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001326 M(StaticFieldGet, Instruction) \
1327 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001328 M(UnresolvedInstanceFieldGet, Instruction) \
1329 M(UnresolvedInstanceFieldSet, Instruction) \
1330 M(UnresolvedStaticFieldGet, Instruction) \
1331 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001332 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001333 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001334 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001335 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001336 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001337 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001338 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001339 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001340
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001341/*
1342 * Instructions, shared across several (not all) architectures.
1343 */
1344#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1345#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1346#else
1347#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001348 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001349 M(MultiplyAccumulate, Instruction) \
1350 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001351#endif
1352
Vladimir Markob4536b72015-11-24 13:45:23 +00001353#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001354#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001355#else
1356#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1357 M(ArmDexCacheArraysBase, Instruction)
1358#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001359
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001360#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001361#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001362#else
1363#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001364 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001365#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001366
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001367#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001368#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001369#else
1370#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1371 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001372 M(MipsDexCacheArraysBase, Instruction) \
1373 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001374#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001375
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001376#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1377
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001378#ifndef ART_ENABLE_CODEGEN_x86
1379#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1380#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001381#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1382 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001383 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001384 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001385 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001386#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001387
1388#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1389
1390#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1391 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001392 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001393 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1394 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001395 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001396 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001397 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1398 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1399
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001400#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1401 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001402 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001403 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001404 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001405 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001406
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001407#define FOR_EACH_INSTRUCTION(M) \
1408 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1409 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1410
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001411#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001412FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1413#undef FORWARD_DECLARATION
1414
Vladimir Marko372f10e2016-05-17 16:30:10 +01001415#define DECLARE_INSTRUCTION(type) \
1416 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1417 const char* DebugName() const OVERRIDE { return #type; } \
1418 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1419 return other->Is##type(); \
1420 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001421 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001422
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001423#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1424 bool Is##type() const { return As##type() != nullptr; } \
1425 const H##type* As##type() const { return this; } \
1426 H##type* As##type() { return this; }
1427
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001428template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001429class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001430 public:
David Brazdiled596192015-01-23 10:39:45 +00001431 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001432 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001433 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001434
Vladimir Marko46817b82016-03-29 12:21:58 +01001435 // Hook for the IntrusiveForwardList<>.
1436 // TODO: Hide this better.
1437 IntrusiveForwardListHook hook;
1438
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001439 private:
David Brazdiled596192015-01-23 10:39:45 +00001440 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001441 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001442
1443 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001444 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001445
Vladimir Marko46817b82016-03-29 12:21:58 +01001446 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001447
1448 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1449};
1450
David Brazdiled596192015-01-23 10:39:45 +00001451template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001452using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001453
David Brazdil1abb4192015-02-17 18:33:36 +00001454// This class is used by HEnvironment and HInstruction classes to record the
1455// instructions they use and pointers to the corresponding HUseListNodes kept
1456// by the used instructions.
1457template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001458class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001459 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001460 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1461 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001462
Vladimir Marko46817b82016-03-29 12:21:58 +01001463 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1464 : HUserRecord(old_record.instruction_, before_use_node) {}
1465 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1466 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001467 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001468 }
1469
1470 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001471 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1472 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001473
1474 private:
1475 // Instruction used by the user.
1476 HInstruction* instruction_;
1477
Vladimir Marko46817b82016-03-29 12:21:58 +01001478 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1479 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001480};
1481
Vladimir Markoe9004912016-06-16 16:50:52 +01001482// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1483// This is used for HInstruction::GetInputs() to return a container wrapper providing
1484// HInstruction* values even though the underlying container has HUserRecord<>s.
1485struct HInputExtractor {
1486 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1487 return record.GetInstruction();
1488 }
1489 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1490 return record.GetInstruction();
1491 }
1492};
1493
1494using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1495using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1496
Aart Bik854a02b2015-07-14 16:07:00 -07001497/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001498 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001499 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001500 * For write/read dependences on fields/arrays, the dependence analysis uses
1501 * type disambiguation (e.g. a float field write cannot modify the value of an
1502 * integer field read) and the access type (e.g. a reference array write cannot
1503 * modify the value of a reference field read [although it may modify the
1504 * reference fetch prior to reading the field, which is represented by its own
1505 * write/read dependence]). The analysis makes conservative points-to
1506 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1507 * the same, and any reference read depends on any reference read without
1508 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001509 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001510 * The internal representation uses 38-bit and is described in the table below.
1511 * The first line indicates the side effect, and for field/array accesses the
1512 * second line indicates the type of the access (in the order of the
1513 * Primitive::Type enum).
1514 * The two numbered lines below indicate the bit position in the bitfield (read
1515 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001516 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001517 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1518 * +-------------+---------+---------+--------------+---------+---------+
1519 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1520 * | 3 |333333322|222222221| 1 |111111110|000000000|
1521 * | 7 |654321098|765432109| 8 |765432109|876543210|
1522 *
1523 * Note that, to ease the implementation, 'changes' bits are least significant
1524 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001525 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001526class SideEffects : public ValueObject {
1527 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001528 SideEffects() : flags_(0) {}
1529
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001530 static SideEffects None() {
1531 return SideEffects(0);
1532 }
1533
1534 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001535 return SideEffects(kAllChangeBits | kAllDependOnBits);
1536 }
1537
1538 static SideEffects AllChanges() {
1539 return SideEffects(kAllChangeBits);
1540 }
1541
1542 static SideEffects AllDependencies() {
1543 return SideEffects(kAllDependOnBits);
1544 }
1545
1546 static SideEffects AllExceptGCDependency() {
1547 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1548 }
1549
1550 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001551 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001552 }
1553
Aart Bik34c3ba92015-07-20 14:08:59 -07001554 static SideEffects AllWrites() {
1555 return SideEffects(kAllWrites);
1556 }
1557
1558 static SideEffects AllReads() {
1559 return SideEffects(kAllReads);
1560 }
1561
1562 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1563 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001564 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001565 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001566 }
1567
Aart Bik854a02b2015-07-14 16:07:00 -07001568 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001569 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001570 }
1571
Aart Bik34c3ba92015-07-20 14:08:59 -07001572 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1573 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001574 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001575 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001576 }
1577
1578 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001579 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001580 }
1581
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001582 static SideEffects CanTriggerGC() {
1583 return SideEffects(1ULL << kCanTriggerGCBit);
1584 }
1585
1586 static SideEffects DependsOnGC() {
1587 return SideEffects(1ULL << kDependsOnGCBit);
1588 }
1589
Aart Bik854a02b2015-07-14 16:07:00 -07001590 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001591 SideEffects Union(SideEffects other) const {
1592 return SideEffects(flags_ | other.flags_);
1593 }
1594
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001595 SideEffects Exclusion(SideEffects other) const {
1596 return SideEffects(flags_ & ~other.flags_);
1597 }
1598
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001599 void Add(SideEffects other) {
1600 flags_ |= other.flags_;
1601 }
1602
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001603 bool Includes(SideEffects other) const {
1604 return (other.flags_ & flags_) == other.flags_;
1605 }
1606
1607 bool HasSideEffects() const {
1608 return (flags_ & kAllChangeBits);
1609 }
1610
1611 bool HasDependencies() const {
1612 return (flags_ & kAllDependOnBits);
1613 }
1614
1615 // Returns true if there are no side effects or dependencies.
1616 bool DoesNothing() const {
1617 return flags_ == 0;
1618 }
1619
Aart Bik854a02b2015-07-14 16:07:00 -07001620 // Returns true if something is written.
1621 bool DoesAnyWrite() const {
1622 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001623 }
1624
Aart Bik854a02b2015-07-14 16:07:00 -07001625 // Returns true if something is read.
1626 bool DoesAnyRead() const {
1627 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001628 }
1629
Aart Bik854a02b2015-07-14 16:07:00 -07001630 // Returns true if potentially everything is written and read
1631 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001632 bool DoesAllReadWrite() const {
1633 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1634 }
1635
Aart Bik854a02b2015-07-14 16:07:00 -07001636 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001637 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001638 }
1639
Roland Levillain0d5a2812015-11-13 10:07:31 +00001640 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001641 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001642 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1643 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001644 }
1645
1646 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001647 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001648 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001649 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001650 for (int s = kLastBit; s >= 0; s--) {
1651 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1652 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1653 // This is a bit for the GC side effect.
1654 if (current_bit_is_set) {
1655 flags += "GC";
1656 }
Aart Bik854a02b2015-07-14 16:07:00 -07001657 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001658 } else {
1659 // This is a bit for the array/field analysis.
1660 // The underscore character stands for the 'can trigger GC' bit.
1661 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1662 if (current_bit_is_set) {
1663 flags += kDebug[s];
1664 }
1665 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1666 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1667 flags += "|";
1668 }
1669 }
Aart Bik854a02b2015-07-14 16:07:00 -07001670 }
1671 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001672 }
1673
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001674 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001675
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001676 private:
1677 static constexpr int kFieldArrayAnalysisBits = 9;
1678
1679 static constexpr int kFieldWriteOffset = 0;
1680 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1681 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1682 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1683
1684 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1685
1686 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1687 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1688 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1689 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1690
1691 static constexpr int kLastBit = kDependsOnGCBit;
1692 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1693
1694 // Aliases.
1695
1696 static_assert(kChangeBits == kDependOnBits,
1697 "the 'change' bits should match the 'depend on' bits.");
1698
1699 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1700 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1701 static constexpr uint64_t kAllWrites =
1702 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1703 static constexpr uint64_t kAllReads =
1704 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001705
Aart Bik854a02b2015-07-14 16:07:00 -07001706 // Translates type to bit flag.
1707 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1708 CHECK_NE(type, Primitive::kPrimVoid);
1709 const uint64_t one = 1;
1710 const int shift = type; // 0-based consecutive enum
1711 DCHECK_LE(kFieldWriteOffset, shift);
1712 DCHECK_LT(shift, kArrayWriteOffset);
1713 return one << (type + offset);
1714 }
1715
1716 // Private constructor on direct flags value.
1717 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1718
1719 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001720};
1721
David Brazdiled596192015-01-23 10:39:45 +00001722// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001723class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001724 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001725 HEnvironment(ArenaAllocator* arena,
1726 size_t number_of_vregs,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001727 ArtMethod* method,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001728 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001729 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001730 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1731 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001732 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001733 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001734 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001735 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001736 }
1737
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001738 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001739 : HEnvironment(arena,
1740 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001741 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001742 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001743 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001744
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001745 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001746 if (parent_ != nullptr) {
1747 parent_->SetAndCopyParentChain(allocator, parent);
1748 } else {
1749 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1750 parent_->CopyFrom(parent);
1751 if (parent->GetParent() != nullptr) {
1752 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1753 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001754 }
David Brazdiled596192015-01-23 10:39:45 +00001755 }
1756
Vladimir Marko71bf8092015-09-15 15:33:14 +01001757 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001758 void CopyFrom(HEnvironment* environment);
1759
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001760 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1761 // input to the loop phi instead. This is for inserting instructions that
1762 // require an environment (like HDeoptimization) in the loop pre-header.
1763 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001764
1765 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001766 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001767 }
1768
1769 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001770 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001771 }
1772
David Brazdil1abb4192015-02-17 18:33:36 +00001773 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001774
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001775 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001776
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001777 HEnvironment* GetParent() const { return parent_; }
1778
1779 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001780 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001781 }
1782
1783 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001784 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001785 }
1786
1787 uint32_t GetDexPc() const {
1788 return dex_pc_;
1789 }
1790
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001791 ArtMethod* GetMethod() const {
1792 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001793 }
1794
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001795 HInstruction* GetHolder() const {
1796 return holder_;
1797 }
1798
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001799
1800 bool IsFromInlinedInvoke() const {
1801 return GetParent() != nullptr;
1802 }
1803
David Brazdiled596192015-01-23 10:39:45 +00001804 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001805 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1806 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001807 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001808 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001809 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001810
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001811 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001812 HInstruction* const holder_;
1813
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001814 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001815
1816 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1817};
1818
Vladimir Markof9f64412015-09-02 14:05:49 +01001819class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001820 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001821 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001822 : previous_(nullptr),
1823 next_(nullptr),
1824 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001825 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001826 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001827 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001828 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001829 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001830 locations_(nullptr),
1831 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001832 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001833 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001834 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1835 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1836 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001837
Dave Allison20dfc792014-06-16 20:44:29 -07001838 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001839
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001840#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001841 enum InstructionKind {
1842 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1843 };
1844#undef DECLARE_KIND
1845
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001846 HInstruction* GetNext() const { return next_; }
1847 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001848
Calin Juravle77520bc2015-01-12 18:45:46 +00001849 HInstruction* GetNextDisregardingMoves() const;
1850 HInstruction* GetPreviousDisregardingMoves() const;
1851
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001852 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001853 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001854 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001855 bool IsInBlock() const { return block_ != nullptr; }
1856 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001857 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1858 bool IsIrreducibleLoopHeaderPhi() const {
1859 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1860 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001861
Vladimir Marko372f10e2016-05-17 16:30:10 +01001862 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1863
1864 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1865 // One virtual method is enough, just const_cast<> and then re-add the const.
1866 return ArrayRef<const HUserRecord<HInstruction*>>(
1867 const_cast<HInstruction*>(this)->GetInputRecords());
1868 }
1869
Vladimir Markoe9004912016-06-16 16:50:52 +01001870 HInputsRef GetInputs() {
1871 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001872 }
1873
Vladimir Markoe9004912016-06-16 16:50:52 +01001874 HConstInputsRef GetInputs() const {
1875 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001876 }
1877
1878 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001879 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001880
Aart Bik2767f4b2016-10-28 15:03:53 -07001881 bool HasInput(HInstruction* input) const {
1882 for (const HInstruction* i : GetInputs()) {
1883 if (i == input) {
1884 return true;
1885 }
1886 }
1887 return false;
1888 }
1889
Vladimir Marko372f10e2016-05-17 16:30:10 +01001890 void SetRawInputAt(size_t index, HInstruction* input) {
1891 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1892 }
1893
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001894 virtual void Accept(HGraphVisitor* visitor) = 0;
1895 virtual const char* DebugName() const = 0;
1896
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001897 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1898
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001899 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001900
1901 uint32_t GetDexPc() const { return dex_pc_; }
1902
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001903 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001904
Roland Levillaine161a2a2014-10-03 12:45:18 +01001905 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001906 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001907
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001908 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001909 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001910
Calin Juravle10e244f2015-01-26 18:54:32 +00001911 // Does not apply for all instructions, but having this at top level greatly
1912 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001913 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001914 virtual bool CanBeNull() const {
1915 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1916 return true;
1917 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001918
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001919 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001920 return false;
1921 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001922
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001923 virtual bool IsActualObject() const {
1924 return GetType() == Primitive::kPrimNot;
1925 }
1926
Calin Juravle2e768302015-07-28 14:41:11 +00001927 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001928
Calin Juravle61d544b2015-02-23 16:46:57 +00001929 ReferenceTypeInfo GetReferenceTypeInfo() const {
1930 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001931 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001932 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001933 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001934
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001935 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001936 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001937 // Note: fixup_end remains valid across push_front().
1938 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1939 HUseListNode<HInstruction*>* new_node =
1940 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1941 uses_.push_front(*new_node);
1942 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001943 }
1944
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001945 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001946 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001947 // Note: env_fixup_end remains valid across push_front().
1948 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1949 HUseListNode<HEnvironment*>* new_node =
1950 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1951 env_uses_.push_front(*new_node);
1952 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001953 }
1954
David Brazdil1abb4192015-02-17 18:33:36 +00001955 void RemoveAsUserOfInput(size_t input) {
1956 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001957 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1958 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1959 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001960 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001961
Vladimir Marko372f10e2016-05-17 16:30:10 +01001962 void RemoveAsUserOfAllInputs() {
1963 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1964 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1965 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1966 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1967 }
1968 }
1969
David Brazdil1abb4192015-02-17 18:33:36 +00001970 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1971 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001972
Vladimir Marko46817b82016-03-29 12:21:58 +01001973 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1974 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1975 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001976 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001977 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001978 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001979
Aart Bikcc42be02016-10-20 16:14:16 -07001980 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07001981 return
Aart Bikff7d89c2016-11-07 08:49:28 -08001982 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07001983 !CanThrow() &&
1984 !IsSuspendCheck() &&
1985 !IsControlFlow() &&
1986 !IsNativeDebugInfo() &&
1987 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07001988 // If we added an explicit barrier then we should keep it.
1989 !IsMemoryBarrier();
1990 }
1991
Aart Bikcc42be02016-10-20 16:14:16 -07001992 bool IsDeadAndRemovable() const {
1993 return IsRemovable() && !HasUses();
1994 }
1995
Roland Levillain6c82d402014-10-13 16:10:27 +01001996 // Does this instruction strictly dominate `other_instruction`?
1997 // Returns false if this instruction and `other_instruction` are the same.
1998 // Aborts if this instruction and `other_instruction` are both phis.
1999 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002000
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002001 int GetId() const { return id_; }
2002 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002003
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002004 int GetSsaIndex() const { return ssa_index_; }
2005 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2006 bool HasSsaIndex() const { return ssa_index_ != -1; }
2007
2008 bool HasEnvironment() const { return environment_ != nullptr; }
2009 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002010 // Set the `environment_` field. Raw because this method does not
2011 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002012 void SetRawEnvironment(HEnvironment* environment) {
2013 DCHECK(environment_ == nullptr);
2014 DCHECK_EQ(environment->GetHolder(), this);
2015 environment_ = environment;
2016 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002017
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002018 void InsertRawEnvironment(HEnvironment* environment) {
2019 DCHECK(environment_ != nullptr);
2020 DCHECK_EQ(environment->GetHolder(), this);
2021 DCHECK(environment->GetParent() == nullptr);
2022 environment->parent_ = environment_;
2023 environment_ = environment;
2024 }
2025
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002026 void RemoveEnvironment();
2027
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002028 // Set the environment of this instruction, copying it from `environment`. While
2029 // copying, the uses lists are being updated.
2030 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002031 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002032 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002033 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002034 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002035 if (environment->GetParent() != nullptr) {
2036 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2037 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002038 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002039
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002040 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2041 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002042 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002043 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002044 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002045 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002046 if (environment->GetParent() != nullptr) {
2047 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2048 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002049 }
2050
Nicolas Geoffray39468442014-09-02 15:17:15 +01002051 // Returns the number of entries in the environment. Typically, that is the
2052 // number of dex registers in a method. It could be more in case of inlining.
2053 size_t EnvironmentSize() const;
2054
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002055 LocationSummary* GetLocations() const { return locations_; }
2056 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002057
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002058 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002059 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002060
Alexandre Rames188d4312015-04-09 18:30:21 +01002061 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2062 // uses of this instruction by `other` are *not* updated.
2063 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2064 ReplaceWith(other);
2065 other->ReplaceInput(this, use_index);
2066 }
2067
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002068 // Move `this` instruction before `cursor`
2069 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002070
Vladimir Markofb337ea2015-11-25 15:25:10 +00002071 // Move `this` before its first user and out of any loops. If there is no
2072 // out-of-loop user that dominates all other users, move the instruction
2073 // to the end of the out-of-loop common dominator of the user's blocks.
2074 //
2075 // This can be used only on non-throwing instructions with no side effects that
2076 // have at least one use but no environment uses.
2077 void MoveBeforeFirstUserAndOutOfLoops();
2078
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002079#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002080 bool Is##type() const; \
2081 const H##type* As##type() const; \
2082 H##type* As##type();
2083
2084 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2085#undef INSTRUCTION_TYPE_CHECK
2086
2087#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002088 bool Is##type() const { return (As##type() != nullptr); } \
2089 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002090 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002091 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002092#undef INSTRUCTION_TYPE_CHECK
2093
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002094 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002095 // TODO: this method is used by LICM and GVN with possibly different
2096 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002097 virtual bool CanBeMoved() const { return false; }
2098
2099 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002100 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002101 return false;
2102 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002103
2104 // Returns whether any data encoded in the two instructions is equal.
2105 // This method does not look at the inputs. Both instructions must be
2106 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002107 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002108 return false;
2109 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002110
2111 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002112 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002113 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002114 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002115
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002116 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2117 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2118 // the virtual function because the __attribute__((__pure__)) doesn't really
2119 // apply the strong requirement for virtual functions, preventing optimizations.
2120 InstructionKind GetKind() const PURE;
2121 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002122
2123 virtual size_t ComputeHashCode() const {
2124 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002125 for (const HInstruction* input : GetInputs()) {
2126 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002127 }
2128 return result;
2129 }
2130
2131 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002132 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002133 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002134
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002135 size_t GetLifetimePosition() const { return lifetime_position_; }
2136 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2137 LiveInterval* GetLiveInterval() const { return live_interval_; }
2138 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2139 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2140
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002141 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2142
2143 // Returns whether the code generation of the instruction will require to have access
2144 // to the current method. Such instructions are:
2145 // (1): Instructions that require an environment, as calling the runtime requires
2146 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002147 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2148 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002149 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002150 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002151 }
2152
Vladimir Markodc151b22015-10-15 18:02:30 +01002153 // Returns whether the code generation of the instruction will require to have access
2154 // to the dex cache of the current method's declaring class via the current method.
2155 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002156
Mark Mendellc4701932015-04-10 13:18:51 -04002157 // Does this instruction have any use in an environment before
2158 // control flow hits 'other'?
2159 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2160
2161 // Remove all references to environment uses of this instruction.
2162 // The caller must ensure that this is safe to do.
2163 void RemoveEnvironmentUsers();
2164
Vladimir Markoa1de9182016-02-25 11:37:38 +00002165 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2166 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002167
David Brazdil1abb4192015-02-17 18:33:36 +00002168 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002169 // If set, the machine code for this instruction is assumed to be generated by
2170 // its users. Used by liveness analysis to compute use positions accordingly.
2171 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2172 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2173 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2174 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2175
Vladimir Marko372f10e2016-05-17 16:30:10 +01002176 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2177 return GetInputRecords()[i];
2178 }
2179
2180 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2181 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2182 input_records[index] = input;
2183 }
David Brazdil1abb4192015-02-17 18:33:36 +00002184
Vladimir Markoa1de9182016-02-25 11:37:38 +00002185 uint32_t GetPackedFields() const {
2186 return packed_fields_;
2187 }
2188
2189 template <size_t flag>
2190 bool GetPackedFlag() const {
2191 return (packed_fields_ & (1u << flag)) != 0u;
2192 }
2193
2194 template <size_t flag>
2195 void SetPackedFlag(bool value = true) {
2196 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2197 }
2198
2199 template <typename BitFieldType>
2200 typename BitFieldType::value_type GetPackedField() const {
2201 return BitFieldType::Decode(packed_fields_);
2202 }
2203
2204 template <typename BitFieldType>
2205 void SetPackedField(typename BitFieldType::value_type value) {
2206 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2207 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2208 }
2209
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002210 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002211 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2212 auto before_use_node = uses_.before_begin();
2213 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2214 HInstruction* user = use_node->GetUser();
2215 size_t input_index = use_node->GetIndex();
2216 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2217 before_use_node = use_node;
2218 }
2219 }
2220
2221 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2222 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2223 if (next != uses_.end()) {
2224 HInstruction* next_user = next->GetUser();
2225 size_t next_index = next->GetIndex();
2226 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2227 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2228 }
2229 }
2230
2231 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2232 auto before_env_use_node = env_uses_.before_begin();
2233 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2234 HEnvironment* user = env_use_node->GetUser();
2235 size_t input_index = env_use_node->GetIndex();
2236 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2237 before_env_use_node = env_use_node;
2238 }
2239 }
2240
2241 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2242 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2243 if (next != env_uses_.end()) {
2244 HEnvironment* next_user = next->GetUser();
2245 size_t next_index = next->GetIndex();
2246 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2247 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2248 }
2249 }
David Brazdil1abb4192015-02-17 18:33:36 +00002250
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002251 HInstruction* previous_;
2252 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002253 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002254 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002255
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002256 // An instruction gets an id when it is added to the graph.
2257 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002258 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002259 int id_;
2260
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002261 // When doing liveness analysis, instructions that have uses get an SSA index.
2262 int ssa_index_;
2263
Vladimir Markoa1de9182016-02-25 11:37:38 +00002264 // Packed fields.
2265 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002266
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002267 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002268 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002269
2270 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002271 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002272
Nicolas Geoffray39468442014-09-02 15:17:15 +01002273 // The environment associated with this instruction. Not null if the instruction
2274 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002275 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002276
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002277 // Set by the code generator.
2278 LocationSummary* locations_;
2279
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002280 // Set by the liveness analysis.
2281 LiveInterval* live_interval_;
2282
2283 // Set by the liveness analysis, this is the position in a linear
2284 // order of blocks where this instruction's live interval start.
2285 size_t lifetime_position_;
2286
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002287 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002288
Vladimir Markoa1de9182016-02-25 11:37:38 +00002289 // The reference handle part of the reference type info.
2290 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002291 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002292 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002293
David Brazdil1abb4192015-02-17 18:33:36 +00002294 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002295 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002296 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002297 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002298 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002299
2300 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2301};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002302std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002303
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002304// Iterates over the instructions, while preserving the next instruction
2305// in case the current instruction gets removed from the list by the user
2306// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002307class HInstructionIterator : public ValueObject {
2308 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002309 explicit HInstructionIterator(const HInstructionList& instructions)
2310 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002311 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002312 }
2313
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002314 bool Done() const { return instruction_ == nullptr; }
2315 HInstruction* Current() const { return instruction_; }
2316 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002317 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002318 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002319 }
2320
2321 private:
2322 HInstruction* instruction_;
2323 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002324
2325 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002326};
2327
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002328// Iterates over the instructions without saving the next instruction,
2329// therefore handling changes in the graph potentially made by the user
2330// of this iterator.
2331class HInstructionIteratorHandleChanges : public ValueObject {
2332 public:
2333 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2334 : instruction_(instructions.first_instruction_) {
2335 }
2336
2337 bool Done() const { return instruction_ == nullptr; }
2338 HInstruction* Current() const { return instruction_; }
2339 void Advance() {
2340 instruction_ = instruction_->GetNext();
2341 }
2342
2343 private:
2344 HInstruction* instruction_;
2345
2346 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2347};
2348
2349
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002350class HBackwardInstructionIterator : public ValueObject {
2351 public:
2352 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2353 : instruction_(instructions.last_instruction_) {
2354 next_ = Done() ? nullptr : instruction_->GetPrevious();
2355 }
2356
2357 bool Done() const { return instruction_ == nullptr; }
2358 HInstruction* Current() const { return instruction_; }
2359 void Advance() {
2360 instruction_ = next_;
2361 next_ = Done() ? nullptr : instruction_->GetPrevious();
2362 }
2363
2364 private:
2365 HInstruction* instruction_;
2366 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002367
2368 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002369};
2370
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002371class HVariableInputSizeInstruction : public HInstruction {
2372 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002373 using HInstruction::GetInputRecords; // Keep the const version visible.
2374 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2375 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2376 }
2377
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002378 void AddInput(HInstruction* input);
2379 void InsertInputAt(size_t index, HInstruction* input);
2380 void RemoveInputAt(size_t index);
2381
2382 protected:
2383 HVariableInputSizeInstruction(SideEffects side_effects,
2384 uint32_t dex_pc,
2385 ArenaAllocator* arena,
2386 size_t number_of_inputs,
2387 ArenaAllocKind kind)
2388 : HInstruction(side_effects, dex_pc),
2389 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2390
2391 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2392
2393 private:
2394 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2395};
2396
Vladimir Markof9f64412015-09-02 14:05:49 +01002397template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002398class HTemplateInstruction: public HInstruction {
2399 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002400 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002401 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002402 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002403
Vladimir Marko372f10e2016-05-17 16:30:10 +01002404 using HInstruction::GetInputRecords; // Keep the const version visible.
2405 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2406 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002407 }
2408
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002409 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002410 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002411
2412 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002413};
2414
Vladimir Markof9f64412015-09-02 14:05:49 +01002415// HTemplateInstruction specialization for N=0.
2416template<>
2417class HTemplateInstruction<0>: public HInstruction {
2418 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002419 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002420 : HInstruction(side_effects, dex_pc) {}
2421
Vladimir Markof9f64412015-09-02 14:05:49 +01002422 virtual ~HTemplateInstruction() {}
2423
Vladimir Marko372f10e2016-05-17 16:30:10 +01002424 using HInstruction::GetInputRecords; // Keep the const version visible.
2425 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2426 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002427 }
2428
2429 private:
2430 friend class SsaBuilder;
2431};
2432
Dave Allison20dfc792014-06-16 20:44:29 -07002433template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002434class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002435 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002436 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002437 : HTemplateInstruction<N>(side_effects, dex_pc) {
2438 this->template SetPackedField<TypeField>(type);
2439 }
Dave Allison20dfc792014-06-16 20:44:29 -07002440 virtual ~HExpression() {}
2441
Vladimir Markoa1de9182016-02-25 11:37:38 +00002442 Primitive::Type GetType() const OVERRIDE {
2443 return TypeField::Decode(this->GetPackedFields());
2444 }
Dave Allison20dfc792014-06-16 20:44:29 -07002445
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002446 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002447 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2448 static constexpr size_t kFieldTypeSize =
2449 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2450 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2451 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2452 "Too many packed fields.");
2453 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002454};
2455
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002456// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2457// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002458class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002459 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002460 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2461 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002462
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002463 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002464
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002465 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002466
2467 private:
2468 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2469};
2470
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002471// Represents dex's RETURN opcodes. A HReturn is a control flow
2472// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002473class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002474 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002475 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2476 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002477 SetRawInputAt(0, value);
2478 }
2479
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002480 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002481
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002482 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002483
2484 private:
2485 DISALLOW_COPY_AND_ASSIGN(HReturn);
2486};
2487
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002488class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002489 public:
2490 HPhi(ArenaAllocator* arena,
2491 uint32_t reg_number,
2492 size_t number_of_inputs,
2493 Primitive::Type type,
2494 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002495 : HVariableInputSizeInstruction(
2496 SideEffects::None(),
2497 dex_pc,
2498 arena,
2499 number_of_inputs,
2500 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002501 reg_number_(reg_number) {
2502 SetPackedField<TypeField>(ToPhiType(type));
2503 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2504 // Phis are constructed live and marked dead if conflicting or unused.
2505 // Individual steps of SsaBuilder should assume that if a phi has been
2506 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2507 SetPackedFlag<kFlagIsLive>(true);
2508 SetPackedFlag<kFlagCanBeNull>(true);
2509 }
2510
2511 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2512 static Primitive::Type ToPhiType(Primitive::Type type) {
2513 return Primitive::PrimitiveKind(type);
2514 }
2515
2516 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2517
David Brazdildee58d62016-04-07 09:54:26 +00002518 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2519 void SetType(Primitive::Type new_type) {
2520 // Make sure that only valid type changes occur. The following are allowed:
2521 // (1) int -> float/ref (primitive type propagation),
2522 // (2) long -> double (primitive type propagation).
2523 DCHECK(GetType() == new_type ||
2524 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2525 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2526 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2527 SetPackedField<TypeField>(new_type);
2528 }
2529
2530 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2531 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2532
2533 uint32_t GetRegNumber() const { return reg_number_; }
2534
2535 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2536 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2537 bool IsDead() const { return !IsLive(); }
2538 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2539
Vladimir Markoe9004912016-06-16 16:50:52 +01002540 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002541 return other != nullptr
2542 && other->IsPhi()
2543 && other->AsPhi()->GetBlock() == GetBlock()
2544 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2545 }
2546
2547 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2548 // An equivalent phi is a phi having the same dex register and type.
2549 // It assumes that phis with the same dex register are adjacent.
2550 HPhi* GetNextEquivalentPhiWithSameType() {
2551 HInstruction* next = GetNext();
2552 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2553 if (next->GetType() == GetType()) {
2554 return next->AsPhi();
2555 }
2556 next = next->GetNext();
2557 }
2558 return nullptr;
2559 }
2560
2561 DECLARE_INSTRUCTION(Phi);
2562
David Brazdildee58d62016-04-07 09:54:26 +00002563 private:
2564 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2565 static constexpr size_t kFieldTypeSize =
2566 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2567 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2568 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2569 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2570 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2571 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2572
David Brazdildee58d62016-04-07 09:54:26 +00002573 const uint32_t reg_number_;
2574
2575 DISALLOW_COPY_AND_ASSIGN(HPhi);
2576};
2577
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002578// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002579// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002580// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002581class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002582 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002583 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002584
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002585 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002586
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002587 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002588
2589 private:
2590 DISALLOW_COPY_AND_ASSIGN(HExit);
2591};
2592
2593// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002594class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002595 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002596 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002597
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002598 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002599
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002600 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002601 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002602 }
2603
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002604 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002605
2606 private:
2607 DISALLOW_COPY_AND_ASSIGN(HGoto);
2608};
2609
Roland Levillain9867bc72015-08-05 10:21:34 +01002610class HConstant : public HExpression<0> {
2611 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002612 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2613 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002614
2615 bool CanBeMoved() const OVERRIDE { return true; }
2616
Roland Levillain1a653882016-03-18 18:05:57 +00002617 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002618 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002619 // Is this constant 0 in the arithmetic sense?
2620 virtual bool IsArithmeticZero() const { return false; }
2621 // Is this constant a 0-bit pattern?
2622 virtual bool IsZeroBitPattern() const { return false; }
2623 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002624 virtual bool IsOne() const { return false; }
2625
David Brazdil77a48ae2015-09-15 12:34:04 +00002626 virtual uint64_t GetValueAsUint64() const = 0;
2627
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002628 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002629
2630 private:
2631 DISALLOW_COPY_AND_ASSIGN(HConstant);
2632};
2633
Vladimir Markofcb503c2016-05-18 12:48:17 +01002634class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002635 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002636 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002637 return true;
2638 }
2639
David Brazdil77a48ae2015-09-15 12:34:04 +00002640 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2641
Roland Levillain9867bc72015-08-05 10:21:34 +01002642 size_t ComputeHashCode() const OVERRIDE { return 0; }
2643
Roland Levillain1a653882016-03-18 18:05:57 +00002644 // The null constant representation is a 0-bit pattern.
2645 virtual bool IsZeroBitPattern() const { return true; }
2646
Roland Levillain9867bc72015-08-05 10:21:34 +01002647 DECLARE_INSTRUCTION(NullConstant);
2648
2649 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002650 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002651
2652 friend class HGraph;
2653 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2654};
2655
2656// Constants of the type int. Those can be from Dex instructions, or
2657// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002658class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002659 public:
2660 int32_t GetValue() const { return value_; }
2661
David Brazdil9f389d42015-10-01 14:32:56 +01002662 uint64_t GetValueAsUint64() const OVERRIDE {
2663 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2664 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002665
Vladimir Marko372f10e2016-05-17 16:30:10 +01002666 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002667 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002668 return other->AsIntConstant()->value_ == value_;
2669 }
2670
2671 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2672
2673 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002674 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2675 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002676 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2677
Roland Levillain1a653882016-03-18 18:05:57 +00002678 // Integer constants are used to encode Boolean values as well,
2679 // where 1 means true and 0 means false.
2680 bool IsTrue() const { return GetValue() == 1; }
2681 bool IsFalse() const { return GetValue() == 0; }
2682
Roland Levillain9867bc72015-08-05 10:21:34 +01002683 DECLARE_INSTRUCTION(IntConstant);
2684
2685 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002686 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2687 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2688 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2689 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002690
2691 const int32_t value_;
2692
2693 friend class HGraph;
2694 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2695 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2696 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2697};
2698
Vladimir Markofcb503c2016-05-18 12:48:17 +01002699class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002700 public:
2701 int64_t GetValue() const { return value_; }
2702
David Brazdil77a48ae2015-09-15 12:34:04 +00002703 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2704
Vladimir Marko372f10e2016-05-17 16:30:10 +01002705 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002706 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002707 return other->AsLongConstant()->value_ == value_;
2708 }
2709
2710 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2711
2712 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002713 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2714 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002715 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2716
2717 DECLARE_INSTRUCTION(LongConstant);
2718
2719 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002720 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2721 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002722
2723 const int64_t value_;
2724
2725 friend class HGraph;
2726 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2727};
Dave Allison20dfc792014-06-16 20:44:29 -07002728
Vladimir Markofcb503c2016-05-18 12:48:17 +01002729class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002730 public:
2731 float GetValue() const { return value_; }
2732
2733 uint64_t GetValueAsUint64() const OVERRIDE {
2734 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2735 }
2736
Vladimir Marko372f10e2016-05-17 16:30:10 +01002737 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002738 DCHECK(other->IsFloatConstant()) << other->DebugName();
2739 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2740 }
2741
2742 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2743
2744 bool IsMinusOne() const OVERRIDE {
2745 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2746 }
Roland Levillain1a653882016-03-18 18:05:57 +00002747 bool IsArithmeticZero() const OVERRIDE {
2748 return std::fpclassify(value_) == FP_ZERO;
2749 }
2750 bool IsArithmeticPositiveZero() const {
2751 return IsArithmeticZero() && !std::signbit(value_);
2752 }
2753 bool IsArithmeticNegativeZero() const {
2754 return IsArithmeticZero() && std::signbit(value_);
2755 }
2756 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002757 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002758 }
2759 bool IsOne() const OVERRIDE {
2760 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2761 }
2762 bool IsNaN() const {
2763 return std::isnan(value_);
2764 }
2765
2766 DECLARE_INSTRUCTION(FloatConstant);
2767
2768 private:
2769 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2770 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2771 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2772 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2773
2774 const float value_;
2775
2776 // Only the SsaBuilder and HGraph can create floating-point constants.
2777 friend class SsaBuilder;
2778 friend class HGraph;
2779 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2780};
2781
Vladimir Markofcb503c2016-05-18 12:48:17 +01002782class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002783 public:
2784 double GetValue() const { return value_; }
2785
2786 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2787
Vladimir Marko372f10e2016-05-17 16:30:10 +01002788 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002789 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2790 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2791 }
2792
2793 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2794
2795 bool IsMinusOne() const OVERRIDE {
2796 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2797 }
Roland Levillain1a653882016-03-18 18:05:57 +00002798 bool IsArithmeticZero() const OVERRIDE {
2799 return std::fpclassify(value_) == FP_ZERO;
2800 }
2801 bool IsArithmeticPositiveZero() const {
2802 return IsArithmeticZero() && !std::signbit(value_);
2803 }
2804 bool IsArithmeticNegativeZero() const {
2805 return IsArithmeticZero() && std::signbit(value_);
2806 }
2807 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002808 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002809 }
2810 bool IsOne() const OVERRIDE {
2811 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2812 }
2813 bool IsNaN() const {
2814 return std::isnan(value_);
2815 }
2816
2817 DECLARE_INSTRUCTION(DoubleConstant);
2818
2819 private:
2820 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2821 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2822 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2823 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2824
2825 const double value_;
2826
2827 // Only the SsaBuilder and HGraph can create floating-point constants.
2828 friend class SsaBuilder;
2829 friend class HGraph;
2830 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2831};
2832
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002833// Conditional branch. A block ending with an HIf instruction must have
2834// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002835class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002836 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002837 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2838 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002839 SetRawInputAt(0, input);
2840 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002841
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002842 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002843
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002844 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002845 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002846 }
2847
2848 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002849 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002850 }
2851
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002852 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002853
2854 private:
2855 DISALLOW_COPY_AND_ASSIGN(HIf);
2856};
2857
David Brazdilfc6a86a2015-06-26 10:33:45 +00002858
2859// Abstract instruction which marks the beginning and/or end of a try block and
2860// links it to the respective exception handlers. Behaves the same as a Goto in
2861// non-exceptional control flow.
2862// Normal-flow successor is stored at index zero, exception handlers under
2863// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002864class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002865 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002866 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002867 kEntry,
2868 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002869 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002870 };
2871
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002872 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002873 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2874 SetPackedField<BoundaryKindField>(kind);
2875 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002876
2877 bool IsControlFlow() const OVERRIDE { return true; }
2878
2879 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002880 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002881
David Brazdild26a4112015-11-10 11:07:31 +00002882 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2883 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2884 }
2885
David Brazdilfc6a86a2015-06-26 10:33:45 +00002886 // Returns whether `handler` is among its exception handlers (non-zero index
2887 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002888 bool HasExceptionHandler(const HBasicBlock& handler) const {
2889 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002890 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002891 }
2892
2893 // If not present already, adds `handler` to its block's list of exception
2894 // handlers.
2895 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002896 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002897 GetBlock()->AddSuccessor(handler);
2898 }
2899 }
2900
Vladimir Markoa1de9182016-02-25 11:37:38 +00002901 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2902 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002903
David Brazdilffee3d32015-07-06 11:48:53 +01002904 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2905
David Brazdilfc6a86a2015-06-26 10:33:45 +00002906 DECLARE_INSTRUCTION(TryBoundary);
2907
2908 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002909 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2910 static constexpr size_t kFieldBoundaryKindSize =
2911 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2912 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2913 kFieldBoundaryKind + kFieldBoundaryKindSize;
2914 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2915 "Too many packed fields.");
2916 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002917
2918 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2919};
2920
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002921// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002922class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002923 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002924 // We set CanTriggerGC to prevent any intermediate address to be live
2925 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002926 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002927 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002928 SetRawInputAt(0, cond);
2929 }
2930
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002931 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002932 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002933 return true;
2934 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002935 bool NeedsEnvironment() const OVERRIDE { return true; }
2936 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002937
2938 DECLARE_INSTRUCTION(Deoptimize);
2939
2940 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002941 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2942};
2943
Mingyao Yang063fc772016-08-02 11:02:54 -07002944// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
2945// The compiled code checks this flag value in a guard before devirtualized call and
2946// if it's true, starts to do deoptimization.
2947// It has a 4-byte slot on stack.
2948// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002949class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07002950 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002951 // CHA guards are only optimized in a separate pass and it has no side effects
2952 // with regard to other passes.
2953 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
2954 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07002955 }
2956
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002957 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
2958
2959 // We do all CHA guard elimination/motion in a single pass, after which there is no
2960 // further guard elimination/motion since a guard might have been used for justification
2961 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
2962 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07002963 bool CanBeMoved() const OVERRIDE { return false; }
2964
2965 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
2966
2967 private:
2968 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
2969};
2970
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002971// Represents the ArtMethod that was passed as a first argument to
2972// the method. It is used by instructions that depend on it, like
2973// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002974class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002975 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002976 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2977 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002978
2979 DECLARE_INSTRUCTION(CurrentMethod);
2980
2981 private:
2982 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2983};
2984
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002985// Fetches an ArtMethod from the virtual table or the interface method table
2986// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002987class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002988 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002989 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002990 kVTable,
2991 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002992 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002993 };
2994 HClassTableGet(HInstruction* cls,
2995 Primitive::Type type,
2996 TableKind kind,
2997 size_t index,
2998 uint32_t dex_pc)
2999 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003000 index_(index) {
3001 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003002 SetRawInputAt(0, cls);
3003 }
3004
3005 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003006 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003007 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003008 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003009 }
3010
Vladimir Markoa1de9182016-02-25 11:37:38 +00003011 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003012 size_t GetIndex() const { return index_; }
3013
3014 DECLARE_INSTRUCTION(ClassTableGet);
3015
3016 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003017 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3018 static constexpr size_t kFieldTableKindSize =
3019 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3020 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3021 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3022 "Too many packed fields.");
3023 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3024
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003025 // The index of the ArtMethod in the table.
3026 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003027
3028 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3029};
3030
Mark Mendellfe57faa2015-09-18 09:26:15 -04003031// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3032// have one successor for each entry in the switch table, and the final successor
3033// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003034class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003035 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003036 HPackedSwitch(int32_t start_value,
3037 uint32_t num_entries,
3038 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003039 uint32_t dex_pc = kNoDexPc)
3040 : HTemplateInstruction(SideEffects::None(), dex_pc),
3041 start_value_(start_value),
3042 num_entries_(num_entries) {
3043 SetRawInputAt(0, input);
3044 }
3045
3046 bool IsControlFlow() const OVERRIDE { return true; }
3047
3048 int32_t GetStartValue() const { return start_value_; }
3049
Vladimir Marko211c2112015-09-24 16:52:33 +01003050 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003051
3052 HBasicBlock* GetDefaultBlock() const {
3053 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003054 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003055 }
3056 DECLARE_INSTRUCTION(PackedSwitch);
3057
3058 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003059 const int32_t start_value_;
3060 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003061
3062 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3063};
3064
Roland Levillain88cb1752014-10-20 16:36:47 +01003065class HUnaryOperation : public HExpression<1> {
3066 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003067 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3068 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003069 SetRawInputAt(0, input);
3070 }
3071
3072 HInstruction* GetInput() const { return InputAt(0); }
3073 Primitive::Type GetResultType() const { return GetType(); }
3074
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003075 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003076 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003077 return true;
3078 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003079
Roland Levillain31dd3d62016-02-16 12:21:02 +00003080 // Try to statically evaluate `this` and return a HConstant
3081 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003082 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003083 HConstant* TryStaticEvaluation() const;
3084
3085 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003086 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3087 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003088 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3089 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003090
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003091 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003092
3093 private:
3094 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3095};
3096
Dave Allison20dfc792014-06-16 20:44:29 -07003097class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003098 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003099 HBinaryOperation(Primitive::Type result_type,
3100 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003101 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003102 SideEffects side_effects = SideEffects::None(),
3103 uint32_t dex_pc = kNoDexPc)
3104 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003105 SetRawInputAt(0, left);
3106 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003107 }
3108
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003109 HInstruction* GetLeft() const { return InputAt(0); }
3110 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003111 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003112
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003113 virtual bool IsCommutative() const { return false; }
3114
3115 // Put constant on the right.
3116 // Returns whether order is changed.
3117 bool OrderInputsWithConstantOnTheRight() {
3118 HInstruction* left = InputAt(0);
3119 HInstruction* right = InputAt(1);
3120 if (left->IsConstant() && !right->IsConstant()) {
3121 ReplaceInput(right, 0);
3122 ReplaceInput(left, 1);
3123 return true;
3124 }
3125 return false;
3126 }
3127
3128 // Order inputs by instruction id, but favor constant on the right side.
3129 // This helps GVN for commutative ops.
3130 void OrderInputs() {
3131 DCHECK(IsCommutative());
3132 HInstruction* left = InputAt(0);
3133 HInstruction* right = InputAt(1);
3134 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3135 return;
3136 }
3137 if (OrderInputsWithConstantOnTheRight()) {
3138 return;
3139 }
3140 // Order according to instruction id.
3141 if (left->GetId() > right->GetId()) {
3142 ReplaceInput(right, 0);
3143 ReplaceInput(left, 1);
3144 }
3145 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003146
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003147 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003148 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003149 return true;
3150 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003151
Roland Levillain31dd3d62016-02-16 12:21:02 +00003152 // Try to statically evaluate `this` and return a HConstant
3153 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003154 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003155 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003156
3157 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003158 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3159 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003160 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3161 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003162 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003163 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3164 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003165 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3166 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003167 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3168 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003169 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003170 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3171 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003172
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003173 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003174 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003175 HConstant* GetConstantRight() const;
3176
3177 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003178 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003179 HInstruction* GetLeastConstantLeft() const;
3180
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003181 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003182
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003183 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003184 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3185};
3186
Mark Mendellc4701932015-04-10 13:18:51 -04003187// The comparison bias applies for floating point operations and indicates how NaN
3188// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003189enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003190 kNoBias, // bias is not applicable (i.e. for long operation)
3191 kGtBias, // return 1 for NaN comparisons
3192 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003193 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003194};
3195
Roland Levillain31dd3d62016-02-16 12:21:02 +00003196std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3197
Dave Allison20dfc792014-06-16 20:44:29 -07003198class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003199 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003200 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003201 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3202 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3203 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003204
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003205 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003206 // `instruction`, and disregard moves in between.
3207 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003208
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003209 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003210
3211 virtual IfCondition GetCondition() const = 0;
3212
Mark Mendellc4701932015-04-10 13:18:51 -04003213 virtual IfCondition GetOppositeCondition() const = 0;
3214
Vladimir Markoa1de9182016-02-25 11:37:38 +00003215 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003216 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3217
Vladimir Markoa1de9182016-02-25 11:37:38 +00003218 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3219 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003220
Vladimir Marko372f10e2016-05-17 16:30:10 +01003221 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003222 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003223 }
3224
Roland Levillain4fa13f62015-07-06 18:11:54 +01003225 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003226 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003227 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003228 if (if_cond == kCondNE) {
3229 return true;
3230 } else if (if_cond == kCondEQ) {
3231 return false;
3232 }
3233 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003234 }
3235
3236 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003237 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003238 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003239 if (if_cond == kCondEQ) {
3240 return true;
3241 } else if (if_cond == kCondNE) {
3242 return false;
3243 }
3244 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003245 }
3246
Roland Levillain31dd3d62016-02-16 12:21:02 +00003247 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003248 // Needed if we merge a HCompare into a HCondition.
3249 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3250 static constexpr size_t kFieldComparisonBiasSize =
3251 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3252 static constexpr size_t kNumberOfConditionPackedBits =
3253 kFieldComparisonBias + kFieldComparisonBiasSize;
3254 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3255 using ComparisonBiasField =
3256 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3257
Roland Levillain31dd3d62016-02-16 12:21:02 +00003258 template <typename T>
3259 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3260
3261 template <typename T>
3262 int32_t CompareFP(T x, T y) const {
3263 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3264 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3265 // Handle the bias.
3266 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3267 }
3268
3269 // Return an integer constant containing the result of a condition evaluated at compile time.
3270 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3271 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3272 }
3273
Dave Allison20dfc792014-06-16 20:44:29 -07003274 private:
3275 DISALLOW_COPY_AND_ASSIGN(HCondition);
3276};
3277
3278// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003279class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003280 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003281 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3282 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003283
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003284 bool IsCommutative() const OVERRIDE { return true; }
3285
Vladimir Marko9e23df52015-11-10 17:14:35 +00003286 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3287 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003288 return MakeConstantCondition(true, GetDexPc());
3289 }
3290 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3291 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3292 }
3293 // In the following Evaluate methods, a HCompare instruction has
3294 // been merged into this HEqual instruction; evaluate it as
3295 // `Compare(x, y) == 0`.
3296 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3297 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3298 GetDexPc());
3299 }
3300 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3301 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3302 }
3303 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3304 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003305 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003306
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003307 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003308
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003309 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003310 return kCondEQ;
3311 }
3312
Mark Mendellc4701932015-04-10 13:18:51 -04003313 IfCondition GetOppositeCondition() const OVERRIDE {
3314 return kCondNE;
3315 }
3316
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003317 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003318 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003319
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003320 DISALLOW_COPY_AND_ASSIGN(HEqual);
3321};
3322
Vladimir Markofcb503c2016-05-18 12:48:17 +01003323class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003324 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003325 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3326 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003327
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003328 bool IsCommutative() const OVERRIDE { return true; }
3329
Vladimir Marko9e23df52015-11-10 17:14:35 +00003330 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3331 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003332 return MakeConstantCondition(false, GetDexPc());
3333 }
3334 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3335 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3336 }
3337 // In the following Evaluate methods, a HCompare instruction has
3338 // been merged into this HNotEqual instruction; evaluate it as
3339 // `Compare(x, y) != 0`.
3340 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3341 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3342 }
3343 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3344 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3345 }
3346 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3347 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003348 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003349
Dave Allison20dfc792014-06-16 20:44:29 -07003350 DECLARE_INSTRUCTION(NotEqual);
3351
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003352 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003353 return kCondNE;
3354 }
3355
Mark Mendellc4701932015-04-10 13:18:51 -04003356 IfCondition GetOppositeCondition() const OVERRIDE {
3357 return kCondEQ;
3358 }
3359
Dave Allison20dfc792014-06-16 20:44:29 -07003360 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003361 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003362
Dave Allison20dfc792014-06-16 20:44:29 -07003363 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3364};
3365
Vladimir Markofcb503c2016-05-18 12:48:17 +01003366class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003367 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003368 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3369 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003370
Roland Levillain9867bc72015-08-05 10:21:34 +01003371 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003372 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003373 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003374 // In the following Evaluate methods, a HCompare instruction has
3375 // been merged into this HLessThan instruction; evaluate it as
3376 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003377 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003378 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3379 }
3380 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3381 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3382 }
3383 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3384 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003385 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003386
Dave Allison20dfc792014-06-16 20:44:29 -07003387 DECLARE_INSTRUCTION(LessThan);
3388
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003389 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003390 return kCondLT;
3391 }
3392
Mark Mendellc4701932015-04-10 13:18:51 -04003393 IfCondition GetOppositeCondition() const OVERRIDE {
3394 return kCondGE;
3395 }
3396
Dave Allison20dfc792014-06-16 20:44:29 -07003397 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003398 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003399
Dave Allison20dfc792014-06-16 20:44:29 -07003400 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3401};
3402
Vladimir Markofcb503c2016-05-18 12:48:17 +01003403class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003404 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003405 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3406 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003407
Roland Levillain9867bc72015-08-05 10:21:34 +01003408 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003409 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003410 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003411 // In the following Evaluate methods, a HCompare instruction has
3412 // been merged into this HLessThanOrEqual instruction; evaluate it as
3413 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003414 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003415 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3416 }
3417 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3418 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3419 }
3420 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3421 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003422 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003423
Dave Allison20dfc792014-06-16 20:44:29 -07003424 DECLARE_INSTRUCTION(LessThanOrEqual);
3425
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003426 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003427 return kCondLE;
3428 }
3429
Mark Mendellc4701932015-04-10 13:18:51 -04003430 IfCondition GetOppositeCondition() const OVERRIDE {
3431 return kCondGT;
3432 }
3433
Dave Allison20dfc792014-06-16 20:44:29 -07003434 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003435 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003436
Dave Allison20dfc792014-06-16 20:44:29 -07003437 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3438};
3439
Vladimir Markofcb503c2016-05-18 12:48:17 +01003440class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003441 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003442 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3443 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003444
Roland Levillain9867bc72015-08-05 10:21:34 +01003445 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003446 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003447 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003448 // In the following Evaluate methods, a HCompare instruction has
3449 // been merged into this HGreaterThan instruction; evaluate it as
3450 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003451 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003452 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3453 }
3454 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3455 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3456 }
3457 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3458 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003459 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003460
Dave Allison20dfc792014-06-16 20:44:29 -07003461 DECLARE_INSTRUCTION(GreaterThan);
3462
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003463 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003464 return kCondGT;
3465 }
3466
Mark Mendellc4701932015-04-10 13:18:51 -04003467 IfCondition GetOppositeCondition() const OVERRIDE {
3468 return kCondLE;
3469 }
3470
Dave Allison20dfc792014-06-16 20:44:29 -07003471 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003472 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003473
Dave Allison20dfc792014-06-16 20:44:29 -07003474 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3475};
3476
Vladimir Markofcb503c2016-05-18 12:48:17 +01003477class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003478 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003479 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3480 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003481
Roland Levillain9867bc72015-08-05 10:21:34 +01003482 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003483 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003484 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003485 // In the following Evaluate methods, a HCompare instruction has
3486 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3487 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003488 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003489 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3490 }
3491 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3492 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3493 }
3494 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3495 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003496 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003497
Dave Allison20dfc792014-06-16 20:44:29 -07003498 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3499
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003500 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003501 return kCondGE;
3502 }
3503
Mark Mendellc4701932015-04-10 13:18:51 -04003504 IfCondition GetOppositeCondition() const OVERRIDE {
3505 return kCondLT;
3506 }
3507
Dave Allison20dfc792014-06-16 20:44:29 -07003508 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003509 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003510
Dave Allison20dfc792014-06-16 20:44:29 -07003511 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3512};
3513
Vladimir Markofcb503c2016-05-18 12:48:17 +01003514class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003515 public:
3516 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3517 : HCondition(first, second, dex_pc) {}
3518
3519 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003520 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003521 }
3522 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003523 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3524 }
3525 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3526 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3527 LOG(FATAL) << DebugName() << " is not defined for float values";
3528 UNREACHABLE();
3529 }
3530 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3531 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3532 LOG(FATAL) << DebugName() << " is not defined for double values";
3533 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003534 }
3535
3536 DECLARE_INSTRUCTION(Below);
3537
3538 IfCondition GetCondition() const OVERRIDE {
3539 return kCondB;
3540 }
3541
3542 IfCondition GetOppositeCondition() const OVERRIDE {
3543 return kCondAE;
3544 }
3545
3546 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003547 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003548 return MakeUnsigned(x) < MakeUnsigned(y);
3549 }
Aart Bike9f37602015-10-09 11:15:55 -07003550
3551 DISALLOW_COPY_AND_ASSIGN(HBelow);
3552};
3553
Vladimir Markofcb503c2016-05-18 12:48:17 +01003554class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003555 public:
3556 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3557 : HCondition(first, second, dex_pc) {}
3558
3559 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003560 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003561 }
3562 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003563 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3564 }
3565 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3566 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3567 LOG(FATAL) << DebugName() << " is not defined for float values";
3568 UNREACHABLE();
3569 }
3570 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3571 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3572 LOG(FATAL) << DebugName() << " is not defined for double values";
3573 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003574 }
3575
3576 DECLARE_INSTRUCTION(BelowOrEqual);
3577
3578 IfCondition GetCondition() const OVERRIDE {
3579 return kCondBE;
3580 }
3581
3582 IfCondition GetOppositeCondition() const OVERRIDE {
3583 return kCondA;
3584 }
3585
3586 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003587 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003588 return MakeUnsigned(x) <= MakeUnsigned(y);
3589 }
Aart Bike9f37602015-10-09 11:15:55 -07003590
3591 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3592};
3593
Vladimir Markofcb503c2016-05-18 12:48:17 +01003594class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003595 public:
3596 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3597 : HCondition(first, second, dex_pc) {}
3598
3599 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003600 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003601 }
3602 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003603 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3604 }
3605 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3606 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3607 LOG(FATAL) << DebugName() << " is not defined for float values";
3608 UNREACHABLE();
3609 }
3610 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3611 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3612 LOG(FATAL) << DebugName() << " is not defined for double values";
3613 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003614 }
3615
3616 DECLARE_INSTRUCTION(Above);
3617
3618 IfCondition GetCondition() const OVERRIDE {
3619 return kCondA;
3620 }
3621
3622 IfCondition GetOppositeCondition() const OVERRIDE {
3623 return kCondBE;
3624 }
3625
3626 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003627 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003628 return MakeUnsigned(x) > MakeUnsigned(y);
3629 }
Aart Bike9f37602015-10-09 11:15:55 -07003630
3631 DISALLOW_COPY_AND_ASSIGN(HAbove);
3632};
3633
Vladimir Markofcb503c2016-05-18 12:48:17 +01003634class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003635 public:
3636 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3637 : HCondition(first, second, dex_pc) {}
3638
3639 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003640 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003641 }
3642 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003643 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3644 }
3645 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3646 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3647 LOG(FATAL) << DebugName() << " is not defined for float values";
3648 UNREACHABLE();
3649 }
3650 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3651 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3652 LOG(FATAL) << DebugName() << " is not defined for double values";
3653 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003654 }
3655
3656 DECLARE_INSTRUCTION(AboveOrEqual);
3657
3658 IfCondition GetCondition() const OVERRIDE {
3659 return kCondAE;
3660 }
3661
3662 IfCondition GetOppositeCondition() const OVERRIDE {
3663 return kCondB;
3664 }
3665
3666 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003667 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003668 return MakeUnsigned(x) >= MakeUnsigned(y);
3669 }
Aart Bike9f37602015-10-09 11:15:55 -07003670
3671 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3672};
Dave Allison20dfc792014-06-16 20:44:29 -07003673
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003674// Instruction to check how two inputs compare to each other.
3675// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003676class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003677 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003678 // Note that `comparison_type` is the type of comparison performed
3679 // between the comparison's inputs, not the type of the instantiated
3680 // HCompare instruction (which is always Primitive::kPrimInt).
3681 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003682 HInstruction* first,
3683 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003684 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003685 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003686 : HBinaryOperation(Primitive::kPrimInt,
3687 first,
3688 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003689 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003690 dex_pc) {
3691 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003692 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3693 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003694 }
3695
Roland Levillain9867bc72015-08-05 10:21:34 +01003696 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003697 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3698
3699 template <typename T>
3700 int32_t ComputeFP(T x, T y) const {
3701 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3702 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3703 // Handle the bias.
3704 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3705 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003706
Roland Levillain9867bc72015-08-05 10:21:34 +01003707 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003708 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3709 // reach this code path when processing a freshly built HIR
3710 // graph. However HCompare integer instructions can be synthesized
3711 // by the instruction simplifier to implement IntegerCompare and
3712 // IntegerSignum intrinsics, so we have to handle this case.
3713 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003714 }
3715 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003716 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3717 }
3718 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3719 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3720 }
3721 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3722 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003723 }
3724
Vladimir Marko372f10e2016-05-17 16:30:10 +01003725 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003726 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003727 }
3728
Vladimir Markoa1de9182016-02-25 11:37:38 +00003729 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003730
Roland Levillain31dd3d62016-02-16 12:21:02 +00003731 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003732 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003733 bool IsGtBias() const {
3734 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003735 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003736 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003737
Roland Levillain1693a1f2016-03-15 14:57:31 +00003738 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3739 // Comparisons do not require a runtime call in any back end.
3740 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003741 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003742
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003743 DECLARE_INSTRUCTION(Compare);
3744
Roland Levillain31dd3d62016-02-16 12:21:02 +00003745 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003746 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3747 static constexpr size_t kFieldComparisonBiasSize =
3748 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3749 static constexpr size_t kNumberOfComparePackedBits =
3750 kFieldComparisonBias + kFieldComparisonBiasSize;
3751 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3752 using ComparisonBiasField =
3753 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3754
Roland Levillain31dd3d62016-02-16 12:21:02 +00003755 // Return an integer constant containing the result of a comparison evaluated at compile time.
3756 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3757 DCHECK(value == -1 || value == 0 || value == 1) << value;
3758 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3759 }
3760
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003761 private:
3762 DISALLOW_COPY_AND_ASSIGN(HCompare);
3763};
3764
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003765class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003766 public:
3767 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003768 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003769 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003770 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003771 bool finalizable,
3772 QuickEntrypointEnum entrypoint)
3773 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3774 type_index_(type_index),
3775 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003776 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003777 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003778 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003779 }
3780
Andreas Gampea5b09a62016-11-17 15:21:22 -08003781 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003782 const DexFile& GetDexFile() const { return dex_file_; }
3783
3784 // Calls runtime so needs an environment.
3785 bool NeedsEnvironment() const OVERRIDE { return true; }
3786
Mingyao Yang062157f2016-03-02 10:15:36 -08003787 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3788 bool CanThrow() const OVERRIDE { return true; }
3789
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003790 bool NeedsChecks() const {
3791 return entrypoint_ == kQuickAllocObjectWithChecks;
3792 }
David Brazdil6de19382016-01-08 17:37:10 +00003793
Vladimir Markoa1de9182016-02-25 11:37:38 +00003794 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003795
3796 bool CanBeNull() const OVERRIDE { return false; }
3797
3798 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3799
3800 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3801 entrypoint_ = entrypoint;
3802 }
3803
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003804 HLoadClass* GetLoadClass() const {
3805 HInstruction* input = InputAt(0);
3806 if (input->IsClinitCheck()) {
3807 input = input->InputAt(0);
3808 }
3809 DCHECK(input->IsLoadClass());
3810 return input->AsLoadClass();
3811 }
3812
David Brazdil6de19382016-01-08 17:37:10 +00003813 bool IsStringAlloc() const;
3814
3815 DECLARE_INSTRUCTION(NewInstance);
3816
3817 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003818 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003819 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3820 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3821 "Too many packed fields.");
3822
Andreas Gampea5b09a62016-11-17 15:21:22 -08003823 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003824 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003825 QuickEntrypointEnum entrypoint_;
3826
3827 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3828};
3829
Agi Csaki05f20562015-08-19 14:58:14 -07003830enum IntrinsicNeedsEnvironmentOrCache {
3831 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3832 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003833};
3834
Aart Bik5d75afe2015-12-14 11:57:01 -08003835enum IntrinsicSideEffects {
3836 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3837 kReadSideEffects, // Intrinsic may read heap memory.
3838 kWriteSideEffects, // Intrinsic may write heap memory.
3839 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3840};
3841
3842enum IntrinsicExceptions {
3843 kNoThrow, // Intrinsic does not throw any exceptions.
3844 kCanThrow // Intrinsic may throw exceptions.
3845};
3846
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003847class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003848 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003849 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003850
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003851 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003852 SetRawInputAt(index, argument);
3853 }
3854
Roland Levillain3e3d7332015-04-28 11:00:54 +01003855 // Return the number of arguments. This number can be lower than
3856 // the number of inputs returned by InputCount(), as some invoke
3857 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3858 // inputs at the end of their list of inputs.
3859 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3860
Vladimir Markoa1de9182016-02-25 11:37:38 +00003861 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003862
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003863 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3864
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003865 InvokeType GetInvokeType() const {
3866 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003867 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003868
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003869 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003870 return intrinsic_;
3871 }
3872
Aart Bik5d75afe2015-12-14 11:57:01 -08003873 void SetIntrinsic(Intrinsics intrinsic,
3874 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3875 IntrinsicSideEffects side_effects,
3876 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003877
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003878 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003879 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003880 }
3881
Aart Bikff7d89c2016-11-07 08:49:28 -08003882 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
3883
Vladimir Markoa1de9182016-02-25 11:37:38 +00003884 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003885
Aart Bik71bf7b42016-11-16 10:17:46 -08003886 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003887
Vladimir Marko372f10e2016-05-17 16:30:10 +01003888 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003889 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3890 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003891
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003892 uint32_t* GetIntrinsicOptimizations() {
3893 return &intrinsic_optimizations_;
3894 }
3895
3896 const uint32_t* GetIntrinsicOptimizations() const {
3897 return &intrinsic_optimizations_;
3898 }
3899
3900 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3901
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003902 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3903
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003904 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003905
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003906 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003907 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3908 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003909 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3910 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003911 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003912 static constexpr size_t kFieldReturnTypeSize =
3913 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3914 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3915 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3916 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003917 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003918 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3919
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003920 HInvoke(ArenaAllocator* arena,
3921 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003922 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003923 Primitive::Type return_type,
3924 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003925 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003926 ArtMethod* resolved_method,
3927 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003928 : HVariableInputSizeInstruction(
3929 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
3930 dex_pc,
3931 arena,
3932 number_of_arguments + number_of_other_inputs,
3933 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003934 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003935 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003936 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003937 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003938 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003939 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003940 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003941 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003942 }
3943
Roland Levillain3e3d7332015-04-28 11:00:54 +01003944 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003945 ArtMethod* const resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003946 const uint32_t dex_method_index_;
3947 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003948
3949 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3950 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003951
3952 private:
3953 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3954};
3955
Vladimir Markofcb503c2016-05-18 12:48:17 +01003956class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003957 public:
3958 HInvokeUnresolved(ArenaAllocator* arena,
3959 uint32_t number_of_arguments,
3960 Primitive::Type return_type,
3961 uint32_t dex_pc,
3962 uint32_t dex_method_index,
3963 InvokeType invoke_type)
3964 : HInvoke(arena,
3965 number_of_arguments,
3966 0u /* number_of_other_inputs */,
3967 return_type,
3968 dex_pc,
3969 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003970 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003971 invoke_type) {
3972 }
3973
3974 DECLARE_INSTRUCTION(InvokeUnresolved);
3975
3976 private:
3977 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3978};
3979
Orion Hodsonac141392017-01-13 11:53:47 +00003980class HInvokePolymorphic FINAL : public HInvoke {
3981 public:
3982 HInvokePolymorphic(ArenaAllocator* arena,
3983 uint32_t number_of_arguments,
3984 Primitive::Type return_type,
3985 uint32_t dex_pc,
3986 uint32_t dex_method_index)
3987 : HInvoke(arena,
3988 number_of_arguments,
3989 0u /* number_of_other_inputs */,
3990 return_type,
3991 dex_pc,
3992 dex_method_index,
3993 nullptr,
3994 kVirtual) {}
3995
3996 DECLARE_INSTRUCTION(InvokePolymorphic);
3997
3998 private:
3999 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4000};
4001
Vladimir Markofcb503c2016-05-18 12:48:17 +01004002class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004003 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004004 // Requirements of this method call regarding the class
4005 // initialization (clinit) check of its declaring class.
4006 enum class ClinitCheckRequirement {
4007 kNone, // Class already initialized.
4008 kExplicit, // Static call having explicit clinit check as last input.
4009 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004010 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004011 };
4012
Vladimir Marko58155012015-08-19 12:49:41 +00004013 // Determines how to load the target ArtMethod*.
4014 enum class MethodLoadKind {
4015 // Use a String init ArtMethod* loaded from Thread entrypoints.
4016 kStringInit,
4017
4018 // Use the method's own ArtMethod* loaded by the register allocator.
4019 kRecursive,
4020
4021 // Use ArtMethod* at a known address, embed the direct address in the code.
4022 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4023 kDirectAddress,
4024
Vladimir Markoa1de9182016-02-25 11:37:38 +00004025 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004026 // Used when we need to use the dex cache, for example for invoke-static that
4027 // may cause class initialization (the entry may point to a resolution method),
4028 // and we know that we can access the dex cache arrays using a PC-relative load.
4029 kDexCachePcRelative,
4030
4031 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4032 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4033 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4034 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4035 kDexCacheViaMethod,
4036 };
4037
4038 // Determines the location of the code pointer.
4039 enum class CodePtrLocation {
4040 // Recursive call, use local PC-relative call instruction.
4041 kCallSelf,
4042
Vladimir Marko58155012015-08-19 12:49:41 +00004043 // Use code pointer from the ArtMethod*.
4044 // Used when we don't know the target code. This is also the last-resort-kind used when
4045 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4046 kCallArtMethod,
4047 };
4048
4049 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004050 MethodLoadKind method_load_kind;
4051 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004052 // The method load data holds
4053 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4054 // Note that there are multiple string init methods, each having its own offset.
4055 // - the method address for kDirectAddress
4056 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004057 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004058 };
4059
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004060 HInvokeStaticOrDirect(ArenaAllocator* arena,
4061 uint32_t number_of_arguments,
4062 Primitive::Type return_type,
4063 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004064 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004065 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004066 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004067 InvokeType invoke_type,
4068 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004069 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004070 : HInvoke(arena,
4071 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004072 // There is potentially one extra argument for the HCurrentMethod node, and
4073 // potentially one other if the clinit check is explicit, and potentially
4074 // one other if the method is a string factory.
4075 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004076 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004077 return_type,
4078 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004079 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004080 resolved_method,
4081 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004082 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004083 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004084 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4085 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004086
Vladimir Markodc151b22015-10-15 18:02:30 +01004087 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004088 bool had_current_method_input = HasCurrentMethodInput();
4089 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4090
4091 // Using the current method is the default and once we find a better
4092 // method load kind, we should not go back to using the current method.
4093 DCHECK(had_current_method_input || !needs_current_method_input);
4094
4095 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004096 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4097 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004098 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004099 dispatch_info_ = dispatch_info;
4100 }
4101
Vladimir Markoc53c0792015-11-19 15:48:33 +00004102 void AddSpecialInput(HInstruction* input) {
4103 // We allow only one special input.
4104 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4105 DCHECK(InputCount() == GetSpecialInputIndex() ||
4106 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4107 InsertInputAt(GetSpecialInputIndex(), input);
4108 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004109
Vladimir Marko372f10e2016-05-17 16:30:10 +01004110 using HInstruction::GetInputRecords; // Keep the const version visible.
4111 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4112 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4113 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4114 DCHECK(!input_records.empty());
4115 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4116 HInstruction* last_input = input_records.back().GetInstruction();
4117 // Note: `last_input` may be null during arguments setup.
4118 if (last_input != nullptr) {
4119 // `last_input` is the last input of a static invoke marked as having
4120 // an explicit clinit check. It must either be:
4121 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4122 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4123 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4124 }
4125 }
4126 return input_records;
4127 }
4128
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004129 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004130 // We access the method via the dex cache so we can't do an implicit null check.
4131 // TODO: for intrinsics we can generate implicit null checks.
4132 return false;
4133 }
4134
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004135 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004136 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004137 }
4138
Vladimir Markoc53c0792015-11-19 15:48:33 +00004139 // Get the index of the special input, if any.
4140 //
David Brazdil6de19382016-01-08 17:37:10 +00004141 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4142 // method pointer; otherwise there may be one platform-specific special input,
4143 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004144 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004145 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004146
Vladimir Marko58155012015-08-19 12:49:41 +00004147 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4148 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4149 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004150 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004151 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004152 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004153 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004154 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4155 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004156 bool HasCurrentMethodInput() const {
4157 // This function can be called only after the invoke has been fully initialized by the builder.
4158 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004159 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004160 return true;
4161 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004162 DCHECK(InputCount() == GetSpecialInputIndex() ||
4163 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004164 return false;
4165 }
4166 }
Vladimir Marko58155012015-08-19 12:49:41 +00004167
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004168 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004169 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004170 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004171 }
4172
4173 uint64_t GetMethodAddress() const {
4174 DCHECK(HasMethodAddress());
4175 return dispatch_info_.method_load_data;
4176 }
4177
4178 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004179 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004180 return dispatch_info_.method_load_data;
4181 }
4182
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004183 const DexFile& GetDexFileForPcRelativeDexCache() const;
4184
Vladimir Markoa1de9182016-02-25 11:37:38 +00004185 ClinitCheckRequirement GetClinitCheckRequirement() const {
4186 return GetPackedField<ClinitCheckRequirementField>();
4187 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004188
Roland Levillain4c0eb422015-04-24 16:43:49 +01004189 // Is this instruction a call to a static method?
4190 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004191 return GetInvokeType() == kStatic;
4192 }
4193
4194 MethodReference GetTargetMethod() const {
4195 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004196 }
4197
Vladimir Markofbb184a2015-11-13 14:47:00 +00004198 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4199 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4200 // instruction; only relevant for static calls with explicit clinit check.
4201 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004202 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004203 size_t last_input_index = inputs_.size() - 1u;
4204 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004205 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004206 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004207 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004208 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004209 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004210 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004211 }
4212
4213 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004214 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004215 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004216 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004217 }
4218
4219 // Is this a call to a static method whose declaring class has an
4220 // implicit intialization check requirement?
4221 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004222 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004223 }
4224
Vladimir Markob554b5a2015-11-06 12:57:55 +00004225 // Does this method load kind need the current method as an input?
4226 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4227 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4228 }
4229
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004230 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004231
4232 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004233 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004234 static constexpr size_t kFieldClinitCheckRequirementSize =
4235 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4236 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4237 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4238 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4239 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004240 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4241 kFieldClinitCheckRequirement,
4242 kFieldClinitCheckRequirementSize>;
4243
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004244 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004245 MethodReference target_method_;
4246 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004247
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004248 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004249};
Vladimir Markof64242a2015-12-01 14:58:23 +00004250std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004251std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004252
Vladimir Markofcb503c2016-05-18 12:48:17 +01004253class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004254 public:
4255 HInvokeVirtual(ArenaAllocator* arena,
4256 uint32_t number_of_arguments,
4257 Primitive::Type return_type,
4258 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004259 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004260 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004261 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004262 : HInvoke(arena,
4263 number_of_arguments,
4264 0u,
4265 return_type,
4266 dex_pc,
4267 dex_method_index,
4268 resolved_method,
4269 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004270 vtable_index_(vtable_index) {}
4271
Aart Bik71bf7b42016-11-16 10:17:46 -08004272 bool CanBeNull() const OVERRIDE {
4273 switch (GetIntrinsic()) {
4274 case Intrinsics::kThreadCurrentThread:
4275 case Intrinsics::kStringBufferAppend:
4276 case Intrinsics::kStringBufferToString:
4277 case Intrinsics::kStringBuilderAppend:
4278 case Intrinsics::kStringBuilderToString:
4279 return false;
4280 default:
4281 return HInvoke::CanBeNull();
4282 }
4283 }
4284
Calin Juravle641547a2015-04-21 22:08:51 +01004285 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004286 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004287 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004288 }
4289
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004290 uint32_t GetVTableIndex() const { return vtable_index_; }
4291
4292 DECLARE_INSTRUCTION(InvokeVirtual);
4293
4294 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004295 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004296 const uint32_t vtable_index_;
4297
4298 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4299};
4300
Vladimir Markofcb503c2016-05-18 12:48:17 +01004301class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004302 public:
4303 HInvokeInterface(ArenaAllocator* arena,
4304 uint32_t number_of_arguments,
4305 Primitive::Type return_type,
4306 uint32_t dex_pc,
4307 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004308 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004309 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004310 : HInvoke(arena,
4311 number_of_arguments,
4312 0u,
4313 return_type,
4314 dex_pc,
4315 dex_method_index,
4316 resolved_method,
4317 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004318 imt_index_(imt_index) {}
4319
Calin Juravle641547a2015-04-21 22:08:51 +01004320 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004321 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004322 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004323 }
4324
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004325 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4326 // The assembly stub currently needs it.
4327 return true;
4328 }
4329
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004330 uint32_t GetImtIndex() const { return imt_index_; }
4331 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4332
4333 DECLARE_INSTRUCTION(InvokeInterface);
4334
4335 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004336 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004337 const uint32_t imt_index_;
4338
4339 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4340};
4341
Vladimir Markofcb503c2016-05-18 12:48:17 +01004342class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004343 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004344 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004345 : HUnaryOperation(result_type, input, dex_pc) {
4346 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4347 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004348
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004349 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004350
4351 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004352 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004353 }
4354 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004355 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004356 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004357 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4358 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4359 }
4360 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4361 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4362 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004363
Roland Levillain88cb1752014-10-20 16:36:47 +01004364 DECLARE_INSTRUCTION(Neg);
4365
4366 private:
4367 DISALLOW_COPY_AND_ASSIGN(HNeg);
4368};
4369
Vladimir Markofcb503c2016-05-18 12:48:17 +01004370class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004371 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004372 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4373 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4374 SetRawInputAt(0, cls);
4375 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004376 }
4377
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004378 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004379 bool NeedsEnvironment() const OVERRIDE { return true; }
4380
Mingyao Yang0c365e62015-03-31 15:09:29 -07004381 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4382 bool CanThrow() const OVERRIDE { return true; }
4383
Calin Juravle10e244f2015-01-26 18:54:32 +00004384 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004385
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004386 HLoadClass* GetLoadClass() const {
4387 DCHECK(InputAt(0)->IsLoadClass());
4388 return InputAt(0)->AsLoadClass();
4389 }
4390
4391 HInstruction* GetLength() const {
4392 return InputAt(1);
4393 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004394
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004395 DECLARE_INSTRUCTION(NewArray);
4396
4397 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004398 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4399};
4400
Vladimir Markofcb503c2016-05-18 12:48:17 +01004401class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004402 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004403 HAdd(Primitive::Type result_type,
4404 HInstruction* left,
4405 HInstruction* right,
4406 uint32_t dex_pc = kNoDexPc)
4407 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004408
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004409 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004410
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004411 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004412
4413 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004414 return GetBlock()->GetGraph()->GetIntConstant(
4415 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004416 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004417 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004418 return GetBlock()->GetGraph()->GetLongConstant(
4419 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004420 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004421 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4422 return GetBlock()->GetGraph()->GetFloatConstant(
4423 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4424 }
4425 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4426 return GetBlock()->GetGraph()->GetDoubleConstant(
4427 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4428 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004429
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004430 DECLARE_INSTRUCTION(Add);
4431
4432 private:
4433 DISALLOW_COPY_AND_ASSIGN(HAdd);
4434};
4435
Vladimir Markofcb503c2016-05-18 12:48:17 +01004436class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004437 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004438 HSub(Primitive::Type result_type,
4439 HInstruction* left,
4440 HInstruction* right,
4441 uint32_t dex_pc = kNoDexPc)
4442 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004443
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004444 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004445
4446 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004447 return GetBlock()->GetGraph()->GetIntConstant(
4448 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004449 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004450 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004451 return GetBlock()->GetGraph()->GetLongConstant(
4452 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004453 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004454 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4455 return GetBlock()->GetGraph()->GetFloatConstant(
4456 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4457 }
4458 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4459 return GetBlock()->GetGraph()->GetDoubleConstant(
4460 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4461 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004462
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004463 DECLARE_INSTRUCTION(Sub);
4464
4465 private:
4466 DISALLOW_COPY_AND_ASSIGN(HSub);
4467};
4468
Vladimir Markofcb503c2016-05-18 12:48:17 +01004469class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004470 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004471 HMul(Primitive::Type result_type,
4472 HInstruction* left,
4473 HInstruction* right,
4474 uint32_t dex_pc = kNoDexPc)
4475 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004476
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004477 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004478
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004479 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004480
4481 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004482 return GetBlock()->GetGraph()->GetIntConstant(
4483 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004484 }
4485 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004486 return GetBlock()->GetGraph()->GetLongConstant(
4487 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004488 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004489 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4490 return GetBlock()->GetGraph()->GetFloatConstant(
4491 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4492 }
4493 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4494 return GetBlock()->GetGraph()->GetDoubleConstant(
4495 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4496 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004497
4498 DECLARE_INSTRUCTION(Mul);
4499
4500 private:
4501 DISALLOW_COPY_AND_ASSIGN(HMul);
4502};
4503
Vladimir Markofcb503c2016-05-18 12:48:17 +01004504class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004505 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004506 HDiv(Primitive::Type result_type,
4507 HInstruction* left,
4508 HInstruction* right,
4509 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004510 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004511
Roland Levillain9867bc72015-08-05 10:21:34 +01004512 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004513 T ComputeIntegral(T x, T y) const {
4514 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004515 // Our graph structure ensures we never have 0 for `y` during
4516 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004517 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004518 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004519 return (y == -1) ? -x : x / y;
4520 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004521
Roland Levillain31dd3d62016-02-16 12:21:02 +00004522 template <typename T>
4523 T ComputeFP(T x, T y) const {
4524 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4525 return x / y;
4526 }
4527
Roland Levillain9867bc72015-08-05 10:21:34 +01004528 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004529 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004530 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004531 }
4532 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004533 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004534 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4535 }
4536 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4537 return GetBlock()->GetGraph()->GetFloatConstant(
4538 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4539 }
4540 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4541 return GetBlock()->GetGraph()->GetDoubleConstant(
4542 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004543 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004544
4545 DECLARE_INSTRUCTION(Div);
4546
4547 private:
4548 DISALLOW_COPY_AND_ASSIGN(HDiv);
4549};
4550
Vladimir Markofcb503c2016-05-18 12:48:17 +01004551class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004552 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004553 HRem(Primitive::Type result_type,
4554 HInstruction* left,
4555 HInstruction* right,
4556 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004557 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004558
Roland Levillain9867bc72015-08-05 10:21:34 +01004559 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004560 T ComputeIntegral(T x, T y) const {
4561 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004562 // Our graph structure ensures we never have 0 for `y` during
4563 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004564 DCHECK_NE(y, 0);
4565 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4566 return (y == -1) ? 0 : x % y;
4567 }
4568
Roland Levillain31dd3d62016-02-16 12:21:02 +00004569 template <typename T>
4570 T ComputeFP(T x, T y) const {
4571 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4572 return std::fmod(x, y);
4573 }
4574
Roland Levillain9867bc72015-08-05 10:21:34 +01004575 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004576 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004577 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004578 }
4579 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004580 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004581 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004582 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004583 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4584 return GetBlock()->GetGraph()->GetFloatConstant(
4585 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4586 }
4587 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4588 return GetBlock()->GetGraph()->GetDoubleConstant(
4589 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4590 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004591
4592 DECLARE_INSTRUCTION(Rem);
4593
4594 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004595 DISALLOW_COPY_AND_ASSIGN(HRem);
4596};
4597
Vladimir Markofcb503c2016-05-18 12:48:17 +01004598class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004599 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004600 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4601 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004602 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004603 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004604 SetRawInputAt(0, value);
4605 }
4606
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004607 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4608
Calin Juravled0d48522014-11-04 16:40:20 +00004609 bool CanBeMoved() const OVERRIDE { return true; }
4610
Vladimir Marko372f10e2016-05-17 16:30:10 +01004611 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004612 return true;
4613 }
4614
4615 bool NeedsEnvironment() const OVERRIDE { return true; }
4616 bool CanThrow() const OVERRIDE { return true; }
4617
Calin Juravled0d48522014-11-04 16:40:20 +00004618 DECLARE_INSTRUCTION(DivZeroCheck);
4619
4620 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004621 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4622};
4623
Vladimir Markofcb503c2016-05-18 12:48:17 +01004624class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004625 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004626 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004627 HInstruction* value,
4628 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004629 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004630 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4631 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4632 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004633 }
4634
Roland Levillain5b5b9312016-03-22 14:57:31 +00004635 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004636 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004637 return value << (distance & max_shift_distance);
4638 }
4639
4640 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004641 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004642 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004643 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004644 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004645 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004646 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004647 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004648 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4649 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4650 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4651 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004652 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004653 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4654 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004655 LOG(FATAL) << DebugName() << " is not defined for float values";
4656 UNREACHABLE();
4657 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004658 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4659 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004660 LOG(FATAL) << DebugName() << " is not defined for double values";
4661 UNREACHABLE();
4662 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004663
4664 DECLARE_INSTRUCTION(Shl);
4665
4666 private:
4667 DISALLOW_COPY_AND_ASSIGN(HShl);
4668};
4669
Vladimir Markofcb503c2016-05-18 12:48:17 +01004670class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004671 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004672 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004673 HInstruction* value,
4674 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004675 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004676 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4677 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4678 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004679 }
4680
Roland Levillain5b5b9312016-03-22 14:57:31 +00004681 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004682 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004683 return value >> (distance & max_shift_distance);
4684 }
4685
4686 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004687 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004688 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004689 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004690 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004691 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004692 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004693 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004694 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4695 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4696 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4697 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004698 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004699 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4700 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004701 LOG(FATAL) << DebugName() << " is not defined for float values";
4702 UNREACHABLE();
4703 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004704 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4705 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004706 LOG(FATAL) << DebugName() << " is not defined for double values";
4707 UNREACHABLE();
4708 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004709
4710 DECLARE_INSTRUCTION(Shr);
4711
4712 private:
4713 DISALLOW_COPY_AND_ASSIGN(HShr);
4714};
4715
Vladimir Markofcb503c2016-05-18 12:48:17 +01004716class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004717 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004718 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004719 HInstruction* value,
4720 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004721 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004722 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4723 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4724 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004725 }
4726
Roland Levillain5b5b9312016-03-22 14:57:31 +00004727 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004728 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004729 typedef typename std::make_unsigned<T>::type V;
4730 V ux = static_cast<V>(value);
4731 return static_cast<T>(ux >> (distance & max_shift_distance));
4732 }
4733
4734 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004735 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004736 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004737 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004738 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004739 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004740 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004741 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004742 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4743 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4744 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4745 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004746 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004747 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4748 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004749 LOG(FATAL) << DebugName() << " is not defined for float values";
4750 UNREACHABLE();
4751 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004752 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4753 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004754 LOG(FATAL) << DebugName() << " is not defined for double values";
4755 UNREACHABLE();
4756 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004757
4758 DECLARE_INSTRUCTION(UShr);
4759
4760 private:
4761 DISALLOW_COPY_AND_ASSIGN(HUShr);
4762};
4763
Vladimir Markofcb503c2016-05-18 12:48:17 +01004764class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004765 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004766 HAnd(Primitive::Type result_type,
4767 HInstruction* left,
4768 HInstruction* right,
4769 uint32_t dex_pc = kNoDexPc)
4770 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004771
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004772 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004773
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004774 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004775
4776 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004777 return GetBlock()->GetGraph()->GetIntConstant(
4778 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004779 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004780 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004781 return GetBlock()->GetGraph()->GetLongConstant(
4782 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004783 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004784 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4785 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4786 LOG(FATAL) << DebugName() << " is not defined for float values";
4787 UNREACHABLE();
4788 }
4789 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4790 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4791 LOG(FATAL) << DebugName() << " is not defined for double values";
4792 UNREACHABLE();
4793 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004794
4795 DECLARE_INSTRUCTION(And);
4796
4797 private:
4798 DISALLOW_COPY_AND_ASSIGN(HAnd);
4799};
4800
Vladimir Markofcb503c2016-05-18 12:48:17 +01004801class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004802 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004803 HOr(Primitive::Type result_type,
4804 HInstruction* left,
4805 HInstruction* right,
4806 uint32_t dex_pc = kNoDexPc)
4807 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004808
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004809 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004810
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004811 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004812
4813 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004814 return GetBlock()->GetGraph()->GetIntConstant(
4815 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004816 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004817 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004818 return GetBlock()->GetGraph()->GetLongConstant(
4819 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004820 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004821 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4822 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4823 LOG(FATAL) << DebugName() << " is not defined for float values";
4824 UNREACHABLE();
4825 }
4826 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4827 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4828 LOG(FATAL) << DebugName() << " is not defined for double values";
4829 UNREACHABLE();
4830 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004831
4832 DECLARE_INSTRUCTION(Or);
4833
4834 private:
4835 DISALLOW_COPY_AND_ASSIGN(HOr);
4836};
4837
Vladimir Markofcb503c2016-05-18 12:48:17 +01004838class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004839 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004840 HXor(Primitive::Type result_type,
4841 HInstruction* left,
4842 HInstruction* right,
4843 uint32_t dex_pc = kNoDexPc)
4844 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004845
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004846 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004847
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004848 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004849
4850 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004851 return GetBlock()->GetGraph()->GetIntConstant(
4852 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004853 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004854 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004855 return GetBlock()->GetGraph()->GetLongConstant(
4856 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004857 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004858 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4859 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4860 LOG(FATAL) << DebugName() << " is not defined for float values";
4861 UNREACHABLE();
4862 }
4863 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4864 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4865 LOG(FATAL) << DebugName() << " is not defined for double values";
4866 UNREACHABLE();
4867 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004868
4869 DECLARE_INSTRUCTION(Xor);
4870
4871 private:
4872 DISALLOW_COPY_AND_ASSIGN(HXor);
4873};
4874
Vladimir Markofcb503c2016-05-18 12:48:17 +01004875class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004876 public:
4877 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004878 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004879 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4880 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004881 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004882
Roland Levillain5b5b9312016-03-22 14:57:31 +00004883 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004884 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004885 typedef typename std::make_unsigned<T>::type V;
4886 V ux = static_cast<V>(value);
4887 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004888 return static_cast<T>(ux);
4889 } else {
4890 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004891 return static_cast<T>(ux >> (distance & max_shift_value)) |
4892 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004893 }
4894 }
4895
Roland Levillain5b5b9312016-03-22 14:57:31 +00004896 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004897 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004898 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004899 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004900 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004901 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004902 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004903 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004904 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4905 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4906 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4907 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004908 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004909 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4910 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004911 LOG(FATAL) << DebugName() << " is not defined for float values";
4912 UNREACHABLE();
4913 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004914 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4915 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004916 LOG(FATAL) << DebugName() << " is not defined for double values";
4917 UNREACHABLE();
4918 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004919
4920 DECLARE_INSTRUCTION(Ror);
4921
4922 private:
4923 DISALLOW_COPY_AND_ASSIGN(HRor);
4924};
4925
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004926// The value of a parameter in this method. Its location depends on
4927// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004928class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004929 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004930 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004931 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004932 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004933 Primitive::Type parameter_type,
4934 bool is_this = false)
4935 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004936 dex_file_(dex_file),
4937 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004938 index_(index) {
4939 SetPackedFlag<kFlagIsThis>(is_this);
4940 SetPackedFlag<kFlagCanBeNull>(!is_this);
4941 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004942
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004943 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08004944 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004945 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004946 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004947
Vladimir Markoa1de9182016-02-25 11:37:38 +00004948 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4949 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004950
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004951 DECLARE_INSTRUCTION(ParameterValue);
4952
4953 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004954 // Whether or not the parameter value corresponds to 'this' argument.
4955 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4956 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4957 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4958 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4959 "Too many packed fields.");
4960
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004961 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004962 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004963 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004964 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004965 const uint8_t index_;
4966
4967 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4968};
4969
Vladimir Markofcb503c2016-05-18 12:48:17 +01004970class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004971 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004972 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4973 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004974
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004975 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004976 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004977 return true;
4978 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004979
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004980 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004981
4982 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004983 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004984 }
4985 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004986 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004987 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004988 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4989 LOG(FATAL) << DebugName() << " is not defined for float values";
4990 UNREACHABLE();
4991 }
4992 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4993 LOG(FATAL) << DebugName() << " is not defined for double values";
4994 UNREACHABLE();
4995 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004996
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004997 DECLARE_INSTRUCTION(Not);
4998
4999 private:
5000 DISALLOW_COPY_AND_ASSIGN(HNot);
5001};
5002
Vladimir Markofcb503c2016-05-18 12:48:17 +01005003class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005004 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005005 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5006 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005007
5008 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005009 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005010 return true;
5011 }
5012
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005013 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005014 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005015 return !x;
5016 }
5017
Roland Levillain9867bc72015-08-05 10:21:34 +01005018 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005019 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005020 }
5021 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5022 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005023 UNREACHABLE();
5024 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005025 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5026 LOG(FATAL) << DebugName() << " is not defined for float values";
5027 UNREACHABLE();
5028 }
5029 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5030 LOG(FATAL) << DebugName() << " is not defined for double values";
5031 UNREACHABLE();
5032 }
David Brazdil66d126e2015-04-03 16:02:44 +01005033
5034 DECLARE_INSTRUCTION(BooleanNot);
5035
5036 private:
5037 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5038};
5039
Vladimir Markofcb503c2016-05-18 12:48:17 +01005040class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005041 public:
5042 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005043 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005044 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005045 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005046 // Invariant: We should never generate a conversion to a Boolean value.
5047 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005048 }
5049
5050 HInstruction* GetInput() const { return InputAt(0); }
5051 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5052 Primitive::Type GetResultType() const { return GetType(); }
5053
5054 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005055 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5056 return true;
5057 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005058
Mark Mendelle82549b2015-05-06 10:55:34 -04005059 // Try to statically evaluate the conversion and return a HConstant
5060 // containing the result. If the input cannot be converted, return nullptr.
5061 HConstant* TryStaticEvaluation() const;
5062
Roland Levillaindff1f282014-11-05 14:15:05 +00005063 DECLARE_INSTRUCTION(TypeConversion);
5064
5065 private:
5066 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5067};
5068
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005069static constexpr uint32_t kNoRegNumber = -1;
5070
Vladimir Markofcb503c2016-05-18 12:48:17 +01005071class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005072 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005073 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5074 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005075 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005076 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005077 SetRawInputAt(0, value);
5078 }
5079
Calin Juravle10e244f2015-01-26 18:54:32 +00005080 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005081 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005082 return true;
5083 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005084
Calin Juravle10e244f2015-01-26 18:54:32 +00005085 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005086
Calin Juravle10e244f2015-01-26 18:54:32 +00005087 bool CanThrow() const OVERRIDE { return true; }
5088
5089 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005090
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005091
5092 DECLARE_INSTRUCTION(NullCheck);
5093
5094 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005095 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5096};
5097
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005098// Embeds an ArtField and all the information required by the compiler. We cache
5099// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005100class FieldInfo : public ValueObject {
5101 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005102 FieldInfo(ArtField* field,
5103 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005104 Primitive::Type field_type,
5105 bool is_volatile,
5106 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005107 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005108 const DexFile& dex_file)
5109 : field_(field),
5110 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005111 field_type_(field_type),
5112 is_volatile_(is_volatile),
5113 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005114 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005115 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005116
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005117 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005118 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005119 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005120 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005121 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005122 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005123 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005124
5125 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005126 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005127 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005128 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005129 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005130 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005131 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005132 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005133};
5134
Vladimir Markofcb503c2016-05-18 12:48:17 +01005135class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005136 public:
5137 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005138 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005139 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005140 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005141 bool is_volatile,
5142 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005143 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005144 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005145 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005146 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005147 field_info_(field,
5148 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005149 field_type,
5150 is_volatile,
5151 field_idx,
5152 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005153 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005154 SetRawInputAt(0, value);
5155 }
5156
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005157 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005158
Vladimir Marko372f10e2016-05-17 16:30:10 +01005159 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5160 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005161 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005162 }
5163
Calin Juravle641547a2015-04-21 22:08:51 +01005164 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005165 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005166 }
5167
5168 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005169 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5170 }
5171
Calin Juravle52c48962014-12-16 17:02:57 +00005172 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005173 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005174 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005175 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005176
5177 DECLARE_INSTRUCTION(InstanceFieldGet);
5178
5179 private:
5180 const FieldInfo field_info_;
5181
5182 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5183};
5184
Vladimir Markofcb503c2016-05-18 12:48:17 +01005185class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005186 public:
5187 HInstanceFieldSet(HInstruction* object,
5188 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005189 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005190 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005191 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005192 bool is_volatile,
5193 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005194 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005195 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005196 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005197 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005198 field_info_(field,
5199 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005200 field_type,
5201 is_volatile,
5202 field_idx,
5203 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005204 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005205 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005206 SetRawInputAt(0, object);
5207 SetRawInputAt(1, value);
5208 }
5209
Calin Juravle641547a2015-04-21 22:08:51 +01005210 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005211 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005212 }
5213
Calin Juravle52c48962014-12-16 17:02:57 +00005214 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005215 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005216 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005217 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005218 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005219 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5220 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005221
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005222 DECLARE_INSTRUCTION(InstanceFieldSet);
5223
5224 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005225 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5226 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5227 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5228 "Too many packed fields.");
5229
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005230 const FieldInfo field_info_;
5231
5232 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5233};
5234
Vladimir Markofcb503c2016-05-18 12:48:17 +01005235class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005236 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005237 HArrayGet(HInstruction* array,
5238 HInstruction* index,
5239 Primitive::Type type,
5240 uint32_t dex_pc,
5241 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005242 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005243 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005244 SetRawInputAt(0, array);
5245 SetRawInputAt(1, index);
5246 }
5247
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005248 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005249 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005250 return true;
5251 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005252 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005253 // TODO: We can be smarter here.
5254 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5255 // which generates the implicit null check. There are cases when these can be removed
5256 // to produce better code. If we ever add optimizations to do so we should allow an
5257 // implicit check here (as long as the address falls in the first page).
5258 return false;
5259 }
5260
David Brazdil4833f5a2015-12-16 10:37:39 +00005261 bool IsEquivalentOf(HArrayGet* other) const {
5262 bool result = (GetDexPc() == other->GetDexPc());
5263 if (kIsDebugBuild && result) {
5264 DCHECK_EQ(GetBlock(), other->GetBlock());
5265 DCHECK_EQ(GetArray(), other->GetArray());
5266 DCHECK_EQ(GetIndex(), other->GetIndex());
5267 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005268 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005269 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005270 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5271 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005272 }
5273 }
5274 return result;
5275 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005276
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005277 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5278
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005279 HInstruction* GetArray() const { return InputAt(0); }
5280 HInstruction* GetIndex() const { return InputAt(1); }
5281
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005282 DECLARE_INSTRUCTION(ArrayGet);
5283
5284 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005285 // We treat a String as an array, creating the HArrayGet from String.charAt()
5286 // intrinsic in the instruction simplifier. We can always determine whether
5287 // a particular HArrayGet is actually a String.charAt() by looking at the type
5288 // of the input but that requires holding the mutator lock, so we prefer to use
5289 // a flag, so that code generators don't need to do the locking.
5290 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5291 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5292 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5293 "Too many packed fields.");
5294
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005295 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5296};
5297
Vladimir Markofcb503c2016-05-18 12:48:17 +01005298class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005299 public:
5300 HArraySet(HInstruction* array,
5301 HInstruction* index,
5302 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005303 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005304 uint32_t dex_pc)
5305 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005306 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5307 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5308 SetPackedFlag<kFlagValueCanBeNull>(true);
5309 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005310 SetRawInputAt(0, array);
5311 SetRawInputAt(1, index);
5312 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005313 // Make a best guess now, may be refined during SSA building.
5314 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005315 }
5316
Calin Juravle77520bc2015-01-12 18:45:46 +00005317 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005318 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005319 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005320 }
5321
Mingyao Yang81014cb2015-06-02 03:16:27 -07005322 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005323 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005324
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005325 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005326 // TODO: Same as for ArrayGet.
5327 return false;
5328 }
5329
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005330 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005331 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005332 }
5333
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005334 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005335 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005336 }
5337
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005338 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005339 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005340 }
5341
Vladimir Markoa1de9182016-02-25 11:37:38 +00005342 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5343 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5344 bool StaticTypeOfArrayIsObjectArray() const {
5345 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5346 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005347
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005348 HInstruction* GetArray() const { return InputAt(0); }
5349 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005350 HInstruction* GetValue() const { return InputAt(2); }
5351
5352 Primitive::Type GetComponentType() const {
5353 // The Dex format does not type floating point index operations. Since the
5354 // `expected_component_type_` is set during building and can therefore not
5355 // be correct, we also check what is the value type. If it is a floating
5356 // point type, we must use that type.
5357 Primitive::Type value_type = GetValue()->GetType();
5358 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5359 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005360 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005361 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005362
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005363 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005364 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005365 }
5366
Aart Bik18b36ab2016-04-13 16:41:35 -07005367 void ComputeSideEffects() {
5368 Primitive::Type type = GetComponentType();
5369 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5370 SideEffectsForArchRuntimeCalls(type)));
5371 }
5372
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005373 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5374 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5375 }
5376
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005377 DECLARE_INSTRUCTION(ArraySet);
5378
5379 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005380 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5381 static constexpr size_t kFieldExpectedComponentTypeSize =
5382 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5383 static constexpr size_t kFlagNeedsTypeCheck =
5384 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5385 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005386 // Cached information for the reference_type_info_ so that codegen
5387 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005388 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5389 static constexpr size_t kNumberOfArraySetPackedBits =
5390 kFlagStaticTypeOfArrayIsObjectArray + 1;
5391 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5392 using ExpectedComponentTypeField =
5393 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005394
5395 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5396};
5397
Vladimir Markofcb503c2016-05-18 12:48:17 +01005398class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005399 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005400 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005401 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005402 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005403 // Note that arrays do not change length, so the instruction does not
5404 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005405 SetRawInputAt(0, array);
5406 }
5407
Calin Juravle77520bc2015-01-12 18:45:46 +00005408 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005409 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005410 return true;
5411 }
Calin Juravle641547a2015-04-21 22:08:51 +01005412 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5413 return obj == InputAt(0);
5414 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005415
Vladimir Markodce016e2016-04-28 13:10:02 +01005416 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5417
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005418 DECLARE_INSTRUCTION(ArrayLength);
5419
5420 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005421 // We treat a String as an array, creating the HArrayLength from String.length()
5422 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5423 // determine whether a particular HArrayLength is actually a String.length() by
5424 // looking at the type of the input but that requires holding the mutator lock, so
5425 // we prefer to use a flag, so that code generators don't need to do the locking.
5426 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5427 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5428 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5429 "Too many packed fields.");
5430
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005431 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5432};
5433
Vladimir Markofcb503c2016-05-18 12:48:17 +01005434class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005435 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005436 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5437 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005438 HBoundsCheck(HInstruction* index,
5439 HInstruction* length,
5440 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005441 bool string_char_at = false)
5442 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005443 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005444 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005445 SetRawInputAt(0, index);
5446 SetRawInputAt(1, length);
5447 }
5448
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005449 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005450 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005451 return true;
5452 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005453
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005454 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005455
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005456 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005457
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005458 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005459
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005460 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005461
5462 DECLARE_INSTRUCTION(BoundsCheck);
5463
5464 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005465 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005466
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005467 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5468};
5469
Vladimir Markofcb503c2016-05-18 12:48:17 +01005470class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005471 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005472 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005473 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005474
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005475 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005476 return true;
5477 }
5478
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005479 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5480 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005481
5482 DECLARE_INSTRUCTION(SuspendCheck);
5483
5484 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005485 // Only used for code generation, in order to share the same slow path between back edges
5486 // of a same loop.
5487 SlowPathCode* slow_path_;
5488
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005489 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5490};
5491
David Srbecky0cf44932015-12-09 14:09:59 +00005492// Pseudo-instruction which provides the native debugger with mapping information.
5493// It ensures that we can generate line number and local variables at this point.
5494class HNativeDebugInfo : public HTemplateInstruction<0> {
5495 public:
5496 explicit HNativeDebugInfo(uint32_t dex_pc)
5497 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5498
5499 bool NeedsEnvironment() const OVERRIDE {
5500 return true;
5501 }
5502
5503 DECLARE_INSTRUCTION(NativeDebugInfo);
5504
5505 private:
5506 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5507};
5508
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005509/**
5510 * Instruction to load a Class object.
5511 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005512class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005513 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005514 // Determines how to load the Class.
5515 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005516 // We cannot load this class. See HSharpening::SharpenLoadClass.
5517 kInvalid = -1,
5518
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005519 // Use the Class* from the method's own ArtMethod*.
5520 kReferrersClass,
5521
5522 // Use boot image Class* address that will be known at link time.
5523 // Used for boot image classes referenced by boot image code in non-PIC mode.
5524 kBootImageLinkTimeAddress,
5525
5526 // Use PC-relative boot image Class* address that will be known at link time.
5527 // Used for boot image classes referenced by boot image code in PIC mode.
5528 kBootImageLinkTimePcRelative,
5529
5530 // Use a known boot image Class* address, embedded in the code by the codegen.
5531 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5532 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5533 // GetIncludePatchInformation().
5534 kBootImageAddress,
5535
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005536 // Load from an entry in the .bss section using a PC-relative load.
5537 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5538 kBssEntry,
5539
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005540 // Load from the root table associated with the JIT compiled method.
5541 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005542
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005543 // Load from resolved types array accessed through the class loaded from
5544 // the compiled method's own ArtMethod*. This is the default access type when
5545 // all other types are unavailable.
5546 kDexCacheViaMethod,
5547
5548 kLast = kDexCacheViaMethod
5549 };
5550
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005551 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005552 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005553 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005554 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005555 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005556 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005557 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005558 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5559 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005560 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005561 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005562 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005563 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005564 // Referrers class should not need access check. We never inline unverified
5565 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005566 DCHECK(!is_referrers_class || !needs_access_check);
5567
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005568 SetPackedField<LoadKindField>(
5569 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005570 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005571 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005572 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005573 }
5574
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005575 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005576
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005577 LoadKind GetLoadKind() const {
5578 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005579 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005580
5581 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005582
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005583 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005584
Andreas Gampea5b09a62016-11-17 15:21:22 -08005585 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005586
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005587 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005588
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005589 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005590 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005591 }
5592
Calin Juravle0ba218d2015-05-19 18:46:01 +01005593 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005594 // The entrypoint the code generator is going to call does not do
5595 // clinit of the class.
5596 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005597 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005598 }
5599
5600 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005601 return NeedsAccessCheck() ||
5602 MustGenerateClinitCheck() ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005603 GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5604 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005605 }
5606
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005607 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005608 return NeedsAccessCheck() ||
5609 MustGenerateClinitCheck() ||
5610 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5611 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5612 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005613 ((GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5614 GetLoadKind() == LoadKind::kBssEntry) &&
5615 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005616 }
5617
Calin Juravleacf735c2015-02-12 15:25:22 +00005618 ReferenceTypeInfo GetLoadedClassRTI() {
5619 return loaded_class_rti_;
5620 }
5621
5622 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5623 // Make sure we only set exact types (the loaded class should never be merged).
5624 DCHECK(rti.IsExact());
5625 loaded_class_rti_ = rti;
5626 }
5627
Andreas Gampea5b09a62016-11-17 15:21:22 -08005628 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005629 const DexFile& GetDexFile() const { return dex_file_; }
5630
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005631 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005632 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005633 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005634
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005635 static SideEffects SideEffectsForArchRuntimeCalls() {
5636 return SideEffects::CanTriggerGC();
5637 }
5638
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005639 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005640 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005641 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005642 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005643
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005644 void MarkInBootImage() {
5645 SetPackedFlag<kFlagIsInBootImage>(true);
5646 }
5647
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005648 void AddSpecialInput(HInstruction* special_input);
5649
5650 using HInstruction::GetInputRecords; // Keep the const version visible.
5651 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5652 return ArrayRef<HUserRecord<HInstruction*>>(
5653 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5654 }
5655
5656 Primitive::Type GetType() const OVERRIDE {
5657 return Primitive::kPrimNot;
5658 }
5659
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005660 Handle<mirror::Class> GetClass() const {
5661 return klass_;
5662 }
5663
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005664 DECLARE_INSTRUCTION(LoadClass);
5665
5666 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005667 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005668 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005669 // Whether this instruction must generate the initialization check.
5670 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005671 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005672 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5673 static constexpr size_t kFieldLoadKindSize =
5674 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5675 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005676 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005677 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5678
5679 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005680 return load_kind == LoadKind::kReferrersClass ||
5681 load_kind == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005682 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005683 load_kind == LoadKind::kBssEntry ||
5684 load_kind == LoadKind::kDexCacheViaMethod;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005685 }
5686
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005687 void SetLoadKindInternal(LoadKind load_kind);
5688
5689 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5690 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005691 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005692 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005693
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005694 // A type index and dex file where the class can be accessed. The dex file can be:
5695 // - The compiling method's dex file if the class is defined there too.
5696 // - The compiling method's dex file if the class is referenced there.
5697 // - The dex file where the class is defined. When the load kind can only be
5698 // kBssEntry or kDexCacheViaMethod, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005699 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005700 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005701
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005702 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005703
Calin Juravleacf735c2015-02-12 15:25:22 +00005704 ReferenceTypeInfo loaded_class_rti_;
5705
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005706 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5707};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005708std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5709
5710// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005711inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005712 // The special input is used for PC-relative loads on some architectures,
5713 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005714 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005715 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005716 GetLoadKind() == LoadKind::kBootImageAddress ||
5717 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005718 DCHECK(special_input_.GetInstruction() == nullptr);
5719 special_input_ = HUserRecord<HInstruction*>(special_input);
5720 special_input->AddUseAt(this, 0);
5721}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005722
Vladimir Marko372f10e2016-05-17 16:30:10 +01005723class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005724 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005725 // Determines how to load the String.
5726 enum class LoadKind {
5727 // Use boot image String* address that will be known at link time.
5728 // Used for boot image strings referenced by boot image code in non-PIC mode.
5729 kBootImageLinkTimeAddress,
5730
5731 // Use PC-relative boot image String* address that will be known at link time.
5732 // Used for boot image strings referenced by boot image code in PIC mode.
5733 kBootImageLinkTimePcRelative,
5734
5735 // Use a known boot image String* address, embedded in the code by the codegen.
5736 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5737 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5738 // GetIncludePatchInformation().
5739 kBootImageAddress,
5740
Vladimir Markoaad75c62016-10-03 08:46:48 +00005741 // Load from an entry in the .bss section using a PC-relative load.
5742 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5743 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005744
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005745 // Load from the root table associated with the JIT compiled method.
5746 kJitTableAddress,
5747
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005748 // Load from resolved strings array accessed through the class loaded from
5749 // the compiled method's own ArtMethod*. This is the default access type when
5750 // all other types are unavailable.
5751 kDexCacheViaMethod,
5752
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005753 kLast = kDexCacheViaMethod,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005754 };
5755
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005756 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005757 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005758 const DexFile& dex_file,
5759 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005760 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5761 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005762 string_index_(string_index),
5763 dex_file_(dex_file) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005764 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005765 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005766
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005767 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005768
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005769 LoadKind GetLoadKind() const {
5770 return GetPackedField<LoadKindField>();
5771 }
5772
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005773 const DexFile& GetDexFile() const {
5774 return dex_file_;
5775 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005776
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005777 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005778 return string_index_;
5779 }
5780
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005781 Handle<mirror::String> GetString() const {
5782 return string_;
5783 }
5784
5785 void SetString(Handle<mirror::String> str) {
5786 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005787 }
5788
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005789 bool CanBeMoved() const OVERRIDE { return true; }
5790
Vladimir Marko372f10e2016-05-17 16:30:10 +01005791 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005792
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005793 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005794
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005795 // Will call the runtime if we need to load the string through
5796 // the dex cache and the string is not guaranteed to be there yet.
5797 bool NeedsEnvironment() const OVERRIDE {
5798 LoadKind load_kind = GetLoadKind();
5799 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5800 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005801 load_kind == LoadKind::kBootImageAddress ||
5802 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005803 return false;
5804 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005805 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005806 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005807
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005808 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5809 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5810 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005811
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005812 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005813 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005814
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005815 static SideEffects SideEffectsForArchRuntimeCalls() {
5816 return SideEffects::CanTriggerGC();
5817 }
5818
Vladimir Marko372f10e2016-05-17 16:30:10 +01005819 void AddSpecialInput(HInstruction* special_input);
5820
5821 using HInstruction::GetInputRecords; // Keep the const version visible.
5822 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5823 return ArrayRef<HUserRecord<HInstruction*>>(
5824 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005825 }
5826
Vladimir Marko372f10e2016-05-17 16:30:10 +01005827 Primitive::Type GetType() const OVERRIDE {
5828 return Primitive::kPrimNot;
5829 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005830
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005831 DECLARE_INSTRUCTION(LoadString);
5832
5833 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005834 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005835 static constexpr size_t kFieldLoadKindSize =
5836 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5837 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005838 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005839 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005840
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005841 void SetLoadKindInternal(LoadKind load_kind);
5842
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005843 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5844 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005845 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005846 HUserRecord<HInstruction*> special_input_;
5847
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005848 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005849 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005850
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005851 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005852
5853 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5854};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005855std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5856
5857// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005858inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005859 // The special input is used for PC-relative loads on some architectures,
5860 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005861 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005862 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005863 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5864 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005865 // HLoadString::GetInputRecords() returns an empty array at this point,
5866 // so use the GetInputRecords() from the base class to set the input record.
5867 DCHECK(special_input_.GetInstruction() == nullptr);
5868 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005869 special_input->AddUseAt(this, 0);
5870}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005871
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005872/**
5873 * Performs an initialization check on its Class object input.
5874 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005875class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005876 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005877 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005878 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005879 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005880 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5881 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005882 SetRawInputAt(0, constant);
5883 }
5884
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005885 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005886 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005887 return true;
5888 }
5889
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005890 bool NeedsEnvironment() const OVERRIDE {
5891 // May call runtime to initialize the class.
5892 return true;
5893 }
5894
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005895 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005896
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005897 HLoadClass* GetLoadClass() const {
5898 DCHECK(InputAt(0)->IsLoadClass());
5899 return InputAt(0)->AsLoadClass();
5900 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005901
5902 DECLARE_INSTRUCTION(ClinitCheck);
5903
5904 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005905 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5906};
5907
Vladimir Markofcb503c2016-05-18 12:48:17 +01005908class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005909 public:
5910 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005911 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005912 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005913 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005914 bool is_volatile,
5915 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005916 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005917 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005918 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005919 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005920 field_info_(field,
5921 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005922 field_type,
5923 is_volatile,
5924 field_idx,
5925 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005926 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005927 SetRawInputAt(0, cls);
5928 }
5929
Calin Juravle52c48962014-12-16 17:02:57 +00005930
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005931 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005932
Vladimir Marko372f10e2016-05-17 16:30:10 +01005933 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5934 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005935 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005936 }
5937
5938 size_t ComputeHashCode() const OVERRIDE {
5939 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5940 }
5941
Calin Juravle52c48962014-12-16 17:02:57 +00005942 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005943 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5944 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005945 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005946
5947 DECLARE_INSTRUCTION(StaticFieldGet);
5948
5949 private:
5950 const FieldInfo field_info_;
5951
5952 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5953};
5954
Vladimir Markofcb503c2016-05-18 12:48:17 +01005955class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005956 public:
5957 HStaticFieldSet(HInstruction* cls,
5958 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005959 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005960 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005961 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005962 bool is_volatile,
5963 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005964 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005965 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005966 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005967 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005968 field_info_(field,
5969 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005970 field_type,
5971 is_volatile,
5972 field_idx,
5973 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005974 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005975 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005976 SetRawInputAt(0, cls);
5977 SetRawInputAt(1, value);
5978 }
5979
Calin Juravle52c48962014-12-16 17:02:57 +00005980 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005981 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5982 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005983 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005984
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005985 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005986 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5987 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005988
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005989 DECLARE_INSTRUCTION(StaticFieldSet);
5990
5991 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005992 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5993 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5994 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5995 "Too many packed fields.");
5996
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005997 const FieldInfo field_info_;
5998
5999 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6000};
6001
Vladimir Markofcb503c2016-05-18 12:48:17 +01006002class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006003 public:
6004 HUnresolvedInstanceFieldGet(HInstruction* obj,
6005 Primitive::Type field_type,
6006 uint32_t field_index,
6007 uint32_t dex_pc)
6008 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6009 field_index_(field_index) {
6010 SetRawInputAt(0, obj);
6011 }
6012
6013 bool NeedsEnvironment() const OVERRIDE { return true; }
6014 bool CanThrow() const OVERRIDE { return true; }
6015
6016 Primitive::Type GetFieldType() const { return GetType(); }
6017 uint32_t GetFieldIndex() const { return field_index_; }
6018
6019 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6020
6021 private:
6022 const uint32_t field_index_;
6023
6024 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6025};
6026
Vladimir Markofcb503c2016-05-18 12:48:17 +01006027class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006028 public:
6029 HUnresolvedInstanceFieldSet(HInstruction* obj,
6030 HInstruction* value,
6031 Primitive::Type field_type,
6032 uint32_t field_index,
6033 uint32_t dex_pc)
6034 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006035 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006036 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006037 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006038 SetRawInputAt(0, obj);
6039 SetRawInputAt(1, value);
6040 }
6041
6042 bool NeedsEnvironment() const OVERRIDE { return true; }
6043 bool CanThrow() const OVERRIDE { return true; }
6044
Vladimir Markoa1de9182016-02-25 11:37:38 +00006045 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006046 uint32_t GetFieldIndex() const { return field_index_; }
6047
6048 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6049
6050 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006051 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6052 static constexpr size_t kFieldFieldTypeSize =
6053 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6054 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6055 kFieldFieldType + kFieldFieldTypeSize;
6056 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6057 "Too many packed fields.");
6058 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6059
Calin Juravlee460d1d2015-09-29 04:52:17 +01006060 const uint32_t field_index_;
6061
6062 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6063};
6064
Vladimir Markofcb503c2016-05-18 12:48:17 +01006065class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006066 public:
6067 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6068 uint32_t field_index,
6069 uint32_t dex_pc)
6070 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6071 field_index_(field_index) {
6072 }
6073
6074 bool NeedsEnvironment() const OVERRIDE { return true; }
6075 bool CanThrow() const OVERRIDE { return true; }
6076
6077 Primitive::Type GetFieldType() const { return GetType(); }
6078 uint32_t GetFieldIndex() const { return field_index_; }
6079
6080 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6081
6082 private:
6083 const uint32_t field_index_;
6084
6085 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6086};
6087
Vladimir Markofcb503c2016-05-18 12:48:17 +01006088class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006089 public:
6090 HUnresolvedStaticFieldSet(HInstruction* value,
6091 Primitive::Type field_type,
6092 uint32_t field_index,
6093 uint32_t dex_pc)
6094 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006095 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006096 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006097 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006098 SetRawInputAt(0, value);
6099 }
6100
6101 bool NeedsEnvironment() const OVERRIDE { return true; }
6102 bool CanThrow() const OVERRIDE { return true; }
6103
Vladimir Markoa1de9182016-02-25 11:37:38 +00006104 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006105 uint32_t GetFieldIndex() const { return field_index_; }
6106
6107 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6108
6109 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006110 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6111 static constexpr size_t kFieldFieldTypeSize =
6112 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6113 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6114 kFieldFieldType + kFieldFieldTypeSize;
6115 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6116 "Too many packed fields.");
6117 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6118
Calin Juravlee460d1d2015-09-29 04:52:17 +01006119 const uint32_t field_index_;
6120
6121 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6122};
6123
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006124// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006125class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006126 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006127 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6128 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006129
David Brazdilbbd733e2015-08-18 17:48:17 +01006130 bool CanBeNull() const OVERRIDE { return false; }
6131
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006132 DECLARE_INSTRUCTION(LoadException);
6133
6134 private:
6135 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6136};
6137
David Brazdilcb1c0552015-08-04 16:22:25 +01006138// Implicit part of move-exception which clears thread-local exception storage.
6139// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006140class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006141 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006142 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6143 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006144
6145 DECLARE_INSTRUCTION(ClearException);
6146
6147 private:
6148 DISALLOW_COPY_AND_ASSIGN(HClearException);
6149};
6150
Vladimir Markofcb503c2016-05-18 12:48:17 +01006151class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006152 public:
6153 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006154 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006155 SetRawInputAt(0, exception);
6156 }
6157
6158 bool IsControlFlow() const OVERRIDE { return true; }
6159
6160 bool NeedsEnvironment() const OVERRIDE { return true; }
6161
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006162 bool CanThrow() const OVERRIDE { return true; }
6163
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006164
6165 DECLARE_INSTRUCTION(Throw);
6166
6167 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006168 DISALLOW_COPY_AND_ASSIGN(HThrow);
6169};
6170
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006171/**
6172 * Implementation strategies for the code generator of a HInstanceOf
6173 * or `HCheckCast`.
6174 */
6175enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006176 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006177 kExactCheck, // Can do a single class compare.
6178 kClassHierarchyCheck, // Can just walk the super class chain.
6179 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6180 kInterfaceCheck, // No optimization yet when checking against an interface.
6181 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006182 kArrayCheck, // No optimization yet when checking against a generic array.
6183 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006184};
6185
Roland Levillain86503782016-02-11 19:07:30 +00006186std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6187
Vladimir Markofcb503c2016-05-18 12:48:17 +01006188class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006189 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006190 HInstanceOf(HInstruction* object,
6191 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006192 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006193 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006194 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006195 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006196 dex_pc) {
6197 SetPackedField<TypeCheckKindField>(check_kind);
6198 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006199 SetRawInputAt(0, object);
6200 SetRawInputAt(1, constant);
6201 }
6202
6203 bool CanBeMoved() const OVERRIDE { return true; }
6204
Vladimir Marko372f10e2016-05-17 16:30:10 +01006205 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006206 return true;
6207 }
6208
6209 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006210 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006211 }
6212
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006213 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006214 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6215 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6216 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6217 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006218
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006219 static bool CanCallRuntime(TypeCheckKind check_kind) {
6220 // Mips currently does runtime calls for any other checks.
6221 return check_kind != TypeCheckKind::kExactCheck;
6222 }
6223
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006224 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006225 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006226 }
6227
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006228 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006229
6230 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006231 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6232 static constexpr size_t kFieldTypeCheckKindSize =
6233 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6234 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6235 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6236 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6237 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006238
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006239 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6240};
6241
Vladimir Markofcb503c2016-05-18 12:48:17 +01006242class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006243 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006244 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006245 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006246 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6247 SetPackedFlag<kFlagUpperCanBeNull>(true);
6248 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006249 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006250 SetRawInputAt(0, input);
6251 }
6252
David Brazdilf5552582015-12-27 13:36:12 +00006253 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006254 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006255 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006256 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006257
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006258 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006259 DCHECK(GetUpperCanBeNull() || !can_be_null);
6260 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006261 }
6262
Vladimir Markoa1de9182016-02-25 11:37:38 +00006263 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006264
Calin Juravleb1498f62015-02-16 13:13:29 +00006265 DECLARE_INSTRUCTION(BoundType);
6266
6267 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006268 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6269 // is false then CanBeNull() cannot be true).
6270 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6271 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6272 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6273 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6274
Calin Juravleb1498f62015-02-16 13:13:29 +00006275 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006276 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6277 // It is used to bound the type in cases like:
6278 // if (x instanceof ClassX) {
6279 // // uper_bound_ will be ClassX
6280 // }
David Brazdilf5552582015-12-27 13:36:12 +00006281 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006282
6283 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6284};
6285
Vladimir Markofcb503c2016-05-18 12:48:17 +01006286class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006287 public:
6288 HCheckCast(HInstruction* object,
6289 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006290 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006291 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006292 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6293 SetPackedField<TypeCheckKindField>(check_kind);
6294 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006295 SetRawInputAt(0, object);
6296 SetRawInputAt(1, constant);
6297 }
6298
6299 bool CanBeMoved() const OVERRIDE { return true; }
6300
Vladimir Marko372f10e2016-05-17 16:30:10 +01006301 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006302 return true;
6303 }
6304
6305 bool NeedsEnvironment() const OVERRIDE {
6306 // Instruction may throw a CheckCastError.
6307 return true;
6308 }
6309
6310 bool CanThrow() const OVERRIDE { return true; }
6311
Vladimir Markoa1de9182016-02-25 11:37:38 +00006312 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6313 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6314 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6315 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006316
6317 DECLARE_INSTRUCTION(CheckCast);
6318
6319 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006320 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6321 static constexpr size_t kFieldTypeCheckKindSize =
6322 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6323 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6324 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6325 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6326 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006327
6328 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006329};
6330
Andreas Gampe26de38b2016-07-27 17:53:11 -07006331/**
6332 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6333 * @details We define the combined barrier types that are actually required
6334 * by the Java Memory Model, rather than using exactly the terminology from
6335 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6336 * primitives. Note that the JSR-133 cookbook generally does not deal with
6337 * store atomicity issues, and the recipes there are not always entirely sufficient.
6338 * The current recipe is as follows:
6339 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6340 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6341 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6342 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6343 * class has final fields.
6344 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6345 * store-to-memory instructions. Only generated together with non-temporal stores.
6346 */
6347enum MemBarrierKind {
6348 kAnyStore,
6349 kLoadAny,
6350 kStoreStore,
6351 kAnyAny,
6352 kNTStoreStore,
6353 kLastBarrierKind = kNTStoreStore
6354};
6355std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6356
Vladimir Markofcb503c2016-05-18 12:48:17 +01006357class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006358 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006359 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006360 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006361 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6362 SetPackedField<BarrierKindField>(barrier_kind);
6363 }
Calin Juravle27df7582015-04-17 19:12:31 +01006364
Vladimir Markoa1de9182016-02-25 11:37:38 +00006365 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006366
6367 DECLARE_INSTRUCTION(MemoryBarrier);
6368
6369 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006370 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6371 static constexpr size_t kFieldBarrierKindSize =
6372 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6373 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6374 kFieldBarrierKind + kFieldBarrierKindSize;
6375 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6376 "Too many packed fields.");
6377 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006378
6379 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6380};
6381
Vladimir Markofcb503c2016-05-18 12:48:17 +01006382class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006383 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006384 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006385 kEnter,
6386 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006387 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006388 };
6389
6390 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006391 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006392 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6393 dex_pc) {
6394 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006395 SetRawInputAt(0, object);
6396 }
6397
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006398 // Instruction may go into runtime, so we need an environment.
6399 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006400
6401 bool CanThrow() const OVERRIDE {
6402 // Verifier guarantees that monitor-exit cannot throw.
6403 // This is important because it allows the HGraphBuilder to remove
6404 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6405 return IsEnter();
6406 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006407
Vladimir Markoa1de9182016-02-25 11:37:38 +00006408 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6409 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006410
6411 DECLARE_INSTRUCTION(MonitorOperation);
6412
Calin Juravle52c48962014-12-16 17:02:57 +00006413 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006414 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6415 static constexpr size_t kFieldOperationKindSize =
6416 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6417 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6418 kFieldOperationKind + kFieldOperationKindSize;
6419 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6420 "Too many packed fields.");
6421 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006422
6423 private:
6424 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6425};
6426
Vladimir Markofcb503c2016-05-18 12:48:17 +01006427class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006428 public:
6429 HSelect(HInstruction* condition,
6430 HInstruction* true_value,
6431 HInstruction* false_value,
6432 uint32_t dex_pc)
6433 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6434 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6435
6436 // First input must be `true_value` or `false_value` to allow codegens to
6437 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6438 // that architectures which implement HSelect as a conditional move also
6439 // will not need to invert the condition.
6440 SetRawInputAt(0, false_value);
6441 SetRawInputAt(1, true_value);
6442 SetRawInputAt(2, condition);
6443 }
6444
6445 HInstruction* GetFalseValue() const { return InputAt(0); }
6446 HInstruction* GetTrueValue() const { return InputAt(1); }
6447 HInstruction* GetCondition() const { return InputAt(2); }
6448
6449 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006450 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6451 return true;
6452 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006453
6454 bool CanBeNull() const OVERRIDE {
6455 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6456 }
6457
6458 DECLARE_INSTRUCTION(Select);
6459
6460 private:
6461 DISALLOW_COPY_AND_ASSIGN(HSelect);
6462};
6463
Vladimir Markof9f64412015-09-02 14:05:49 +01006464class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006465 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006466 MoveOperands(Location source,
6467 Location destination,
6468 Primitive::Type type,
6469 HInstruction* instruction)
6470 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006471
6472 Location GetSource() const { return source_; }
6473 Location GetDestination() const { return destination_; }
6474
6475 void SetSource(Location value) { source_ = value; }
6476 void SetDestination(Location value) { destination_ = value; }
6477
6478 // The parallel move resolver marks moves as "in-progress" by clearing the
6479 // destination (but not the source).
6480 Location MarkPending() {
6481 DCHECK(!IsPending());
6482 Location dest = destination_;
6483 destination_ = Location::NoLocation();
6484 return dest;
6485 }
6486
6487 void ClearPending(Location dest) {
6488 DCHECK(IsPending());
6489 destination_ = dest;
6490 }
6491
6492 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006493 DCHECK(source_.IsValid() || destination_.IsInvalid());
6494 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006495 }
6496
6497 // True if this blocks a move from the given location.
6498 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006499 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006500 }
6501
6502 // A move is redundant if it's been eliminated, if its source and
6503 // destination are the same, or if its destination is unneeded.
6504 bool IsRedundant() const {
6505 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6506 }
6507
6508 // We clear both operands to indicate move that's been eliminated.
6509 void Eliminate() {
6510 source_ = destination_ = Location::NoLocation();
6511 }
6512
6513 bool IsEliminated() const {
6514 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6515 return source_.IsInvalid();
6516 }
6517
Alexey Frunze4dda3372015-06-01 18:31:49 -07006518 Primitive::Type GetType() const { return type_; }
6519
Nicolas Geoffray90218252015-04-15 11:56:51 +01006520 bool Is64BitMove() const {
6521 return Primitive::Is64BitType(type_);
6522 }
6523
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006524 HInstruction* GetInstruction() const { return instruction_; }
6525
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006526 private:
6527 Location source_;
6528 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006529 // The type this move is for.
6530 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006531 // The instruction this move is assocatied with. Null when this move is
6532 // for moving an input in the expected locations of user (including a phi user).
6533 // This is only used in debug mode, to ensure we do not connect interval siblings
6534 // in the same parallel move.
6535 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006536};
6537
Roland Levillainc9285912015-12-18 10:38:42 +00006538std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6539
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006540static constexpr size_t kDefaultNumberOfMoves = 4;
6541
Vladimir Markofcb503c2016-05-18 12:48:17 +01006542class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006543 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006544 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006545 : HTemplateInstruction(SideEffects::None(), dex_pc),
6546 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6547 moves_.reserve(kDefaultNumberOfMoves);
6548 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006549
Nicolas Geoffray90218252015-04-15 11:56:51 +01006550 void AddMove(Location source,
6551 Location destination,
6552 Primitive::Type type,
6553 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006554 DCHECK(source.IsValid());
6555 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006556 if (kIsDebugBuild) {
6557 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006558 for (const MoveOperands& move : moves_) {
6559 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006560 // Special case the situation where the move is for the spill slot
6561 // of the instruction.
6562 if ((GetPrevious() == instruction)
6563 || ((GetPrevious() == nullptr)
6564 && instruction->IsPhi()
6565 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006566 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006567 << "Doing parallel moves for the same instruction.";
6568 } else {
6569 DCHECK(false) << "Doing parallel moves for the same instruction.";
6570 }
6571 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006572 }
6573 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006574 for (const MoveOperands& move : moves_) {
6575 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006576 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006577 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006578 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006579 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006580 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006581 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006582 }
6583
Vladimir Marko225b6462015-09-28 12:17:40 +01006584 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006585 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006586 }
6587
Vladimir Marko225b6462015-09-28 12:17:40 +01006588 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006589
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006590 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006591
6592 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006593 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006594
6595 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6596};
6597
Mark Mendell0616ae02015-04-17 12:49:27 -04006598} // namespace art
6599
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006600#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6601#include "nodes_shared.h"
6602#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006603#ifdef ART_ENABLE_CODEGEN_arm
6604#include "nodes_arm.h"
6605#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006606#ifdef ART_ENABLE_CODEGEN_arm64
6607#include "nodes_arm64.h"
6608#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006609#ifdef ART_ENABLE_CODEGEN_mips
6610#include "nodes_mips.h"
6611#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006612#ifdef ART_ENABLE_CODEGEN_x86
6613#include "nodes_x86.h"
6614#endif
6615
6616namespace art {
6617
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006618class HGraphVisitor : public ValueObject {
6619 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006620 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6621 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006622
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006623 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006624 virtual void VisitBasicBlock(HBasicBlock* block);
6625
Roland Levillain633021e2014-10-01 14:12:25 +01006626 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006627 void VisitInsertionOrder();
6628
Roland Levillain633021e2014-10-01 14:12:25 +01006629 // Visit the graph following dominator tree reverse post-order.
6630 void VisitReversePostOrder();
6631
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006632 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006633
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006634 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006635#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006636 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6637
6638 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6639
6640#undef DECLARE_VISIT_INSTRUCTION
6641
6642 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006643 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006644
6645 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6646};
6647
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006648class HGraphDelegateVisitor : public HGraphVisitor {
6649 public:
6650 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6651 virtual ~HGraphDelegateVisitor() {}
6652
6653 // Visit functions that delegate to to super class.
6654#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006655 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006656
6657 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6658
6659#undef DECLARE_VISIT_INSTRUCTION
6660
6661 private:
6662 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6663};
6664
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006665// Iterator over the blocks that art part of the loop. Includes blocks part
6666// of an inner loop. The order in which the blocks are iterated is on their
6667// block id.
6668class HBlocksInLoopIterator : public ValueObject {
6669 public:
6670 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6671 : blocks_in_loop_(info.GetBlocks()),
6672 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6673 index_(0) {
6674 if (!blocks_in_loop_.IsBitSet(index_)) {
6675 Advance();
6676 }
6677 }
6678
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006679 bool Done() const { return index_ == blocks_.size(); }
6680 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006681 void Advance() {
6682 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006683 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006684 if (blocks_in_loop_.IsBitSet(index_)) {
6685 break;
6686 }
6687 }
6688 }
6689
6690 private:
6691 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006692 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006693 size_t index_;
6694
6695 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6696};
6697
Mingyao Yang3584bce2015-05-19 16:01:59 -07006698// Iterator over the blocks that art part of the loop. Includes blocks part
6699// of an inner loop. The order in which the blocks are iterated is reverse
6700// post order.
6701class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6702 public:
6703 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6704 : blocks_in_loop_(info.GetBlocks()),
6705 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6706 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006707 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006708 Advance();
6709 }
6710 }
6711
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006712 bool Done() const { return index_ == blocks_.size(); }
6713 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006714 void Advance() {
6715 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006716 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6717 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006718 break;
6719 }
6720 }
6721 }
6722
6723 private:
6724 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006725 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006726 size_t index_;
6727
6728 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6729};
6730
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006731inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006732 if (constant->IsIntConstant()) {
6733 return constant->AsIntConstant()->GetValue();
6734 } else if (constant->IsLongConstant()) {
6735 return constant->AsLongConstant()->GetValue();
6736 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006737 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006738 return 0;
6739 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006740}
6741
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006742#define INSTRUCTION_TYPE_CHECK(type, super) \
6743 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6744 inline const H##type* HInstruction::As##type() const { \
6745 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6746 } \
6747 inline H##type* HInstruction::As##type() { \
6748 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6749 }
6750
6751 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6752#undef INSTRUCTION_TYPE_CHECK
6753
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006754// Create space in `blocks` for adding `number_of_new_blocks` entries
6755// starting at location `at`. Blocks after `at` are moved accordingly.
6756inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6757 size_t number_of_new_blocks,
6758 size_t after) {
6759 DCHECK_LT(after, blocks->size());
6760 size_t old_size = blocks->size();
6761 size_t new_size = old_size + number_of_new_blocks;
6762 blocks->resize(new_size);
6763 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6764}
6765
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006766/*
6767 * Hunt "under the hood" of array lengths (leading to array references),
6768 * null checks (also leading to array references), and new arrays
6769 * (leading to the actual length). This makes it more likely related
6770 * instructions become actually comparable.
6771 */
6772inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
6773 while (instruction->IsArrayLength() ||
6774 instruction->IsNullCheck() ||
6775 instruction->IsNewArray()) {
6776 instruction = instruction->IsNewArray()
6777 ? instruction->AsNewArray()->GetLength()
6778 : instruction->InputAt(0);
6779 }
6780 return instruction;
6781}
6782
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006783} // namespace art
6784
6785#endif // ART_COMPILER_OPTIMIZING_NODES_H_