blob: 064e119c38bdc5789769c99e812e5e015772d7b8 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
Vladimir Marko60584552015-09-03 13:35:12 +000029#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010030#include "base/transform_array_ref.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010031#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080032#include "dex_file_types.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000033#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000034#include "handle.h"
35#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000036#include "invoke_type.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010037#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010038#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000039#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000040#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010041#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070042#include "primitive.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010043#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044
45namespace art {
46
David Brazdil1abb4192015-02-17 18:33:36 +000047class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000048class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010049class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010051class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000053class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000055class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000056class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000057class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000058class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000059class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010060class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010061class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010062class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010063class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000064class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010065class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000066class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067
Mathieu Chartier736b5602015-09-02 14:54:11 -070068namespace mirror {
69class DexCache;
70} // namespace mirror
71
Nicolas Geoffray818f2102014-02-18 16:43:35 +000072static const int kDefaultNumberOfBlocks = 8;
73static const int kDefaultNumberOfSuccessors = 2;
74static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010075static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010076static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000077static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000078
Roland Levillain5b5b9312016-03-22 14:57:31 +000079// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
80static constexpr int32_t kMaxIntShiftDistance = 0x1f;
81// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
82static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000083
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010084static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070085static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010086
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010087static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
88
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060089static constexpr uint32_t kNoDexPc = -1;
90
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010091inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
92 // For the purposes of the compiler, the dex files must actually be the same object
93 // if we want to safely treat them as the same. This is especially important for JIT
94 // as custom class loaders can open the same underlying file (or memory) multiple
95 // times and provide different class resolution but no two class loaders should ever
96 // use the same DexFile object - doing so is an unsupported hack that can lead to
97 // all sorts of weird failures.
98 return &lhs == &rhs;
99}
100
Dave Allison20dfc792014-06-16 20:44:29 -0700101enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700102 // All types.
103 kCondEQ, // ==
104 kCondNE, // !=
105 // Signed integers and floating-point numbers.
106 kCondLT, // <
107 kCondLE, // <=
108 kCondGT, // >
109 kCondGE, // >=
110 // Unsigned integers.
111 kCondB, // <
112 kCondBE, // <=
113 kCondA, // >
114 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100115 // First and last aliases.
116 kCondFirst = kCondEQ,
117 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700118};
119
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000120enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000121 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000122 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000123 kAnalysisFailThrowCatchLoop,
124 kAnalysisFailAmbiguousArrayOp,
125 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000126};
127
Andreas Gampe9186ced2016-12-12 14:28:21 -0800128template <typename T>
129static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
130 return static_cast<typename std::make_unsigned<T>::type>(x);
131}
132
Vladimir Markof9f64412015-09-02 14:05:49 +0100133class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100134 public:
135 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
136
137 void AddInstruction(HInstruction* instruction);
138 void RemoveInstruction(HInstruction* instruction);
139
David Brazdilc3d743f2015-04-22 13:40:50 +0100140 // Insert `instruction` before/after an existing instruction `cursor`.
141 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
142 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
143
Roland Levillain6b469232014-09-25 10:10:38 +0100144 // Return true if this list contains `instruction`.
145 bool Contains(HInstruction* instruction) const;
146
Roland Levillainccc07a92014-09-16 14:48:16 +0100147 // Return true if `instruction1` is found before `instruction2` in
148 // this instruction list and false otherwise. Abort if none
149 // of these instructions is found.
150 bool FoundBefore(const HInstruction* instruction1,
151 const HInstruction* instruction2) const;
152
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000153 bool IsEmpty() const { return first_instruction_ == nullptr; }
154 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
155
156 // Update the block of all instructions to be `block`.
157 void SetBlockOfInstructions(HBasicBlock* block) const;
158
159 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000160 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000161 void Add(const HInstructionList& instruction_list);
162
David Brazdil2d7352b2015-04-20 14:52:42 +0100163 // Return the number of instructions in the list. This is an expensive operation.
164 size_t CountSize() const;
165
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100166 private:
167 HInstruction* first_instruction_;
168 HInstruction* last_instruction_;
169
170 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000171 friend class HGraph;
172 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100173 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000174 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100175 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100176
177 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
178};
179
David Brazdil4833f5a2015-12-16 10:37:39 +0000180class ReferenceTypeInfo : ValueObject {
181 public:
182 typedef Handle<mirror::Class> TypeHandle;
183
Vladimir Markoa1de9182016-02-25 11:37:38 +0000184 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
185
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700186 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100187 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
188 }
189
Vladimir Markoa1de9182016-02-25 11:37:38 +0000190 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000191 return ReferenceTypeInfo(type_handle, is_exact);
192 }
193
194 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
195
Vladimir Markof39745e2016-01-26 12:16:55 +0000196 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000197 return handle.GetReference() != nullptr;
198 }
199
Vladimir Marko456307a2016-04-19 14:12:13 +0000200 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000201 return IsValidHandle(type_handle_);
202 }
203
204 bool IsExact() const { return is_exact_; }
205
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700206 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000207 DCHECK(IsValid());
208 return GetTypeHandle()->IsObjectClass();
209 }
210
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700211 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000212 DCHECK(IsValid());
213 return GetTypeHandle()->IsStringClass();
214 }
215
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000217 DCHECK(IsValid());
218 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
219 }
220
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000222 DCHECK(IsValid());
223 return GetTypeHandle()->IsInterface();
224 }
225
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700226 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000227 DCHECK(IsValid());
228 return GetTypeHandle()->IsArrayClass();
229 }
230
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000232 DCHECK(IsValid());
233 return GetTypeHandle()->IsPrimitiveArray();
234 }
235
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000237 DCHECK(IsValid());
238 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
239 }
240
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700241 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 DCHECK(IsValid());
243 if (!IsExact()) return false;
244 if (!IsArrayClass()) return false;
245 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
246 }
247
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700248 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000249 DCHECK(IsValid());
250 if (!IsExact()) return false;
251 if (!IsArrayClass()) return false;
252 if (!rti.IsArrayClass()) return false;
253 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
254 rti.GetTypeHandle()->GetComponentType());
255 }
256
257 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
258
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700259 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000260 DCHECK(IsValid());
261 DCHECK(rti.IsValid());
262 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
263 }
264
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700265 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000266 DCHECK(IsValid());
267 DCHECK(rti.IsValid());
268 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
269 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
270 }
271
272 // Returns true if the type information provide the same amount of details.
273 // Note that it does not mean that the instructions have the same actual type
274 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700275 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000276 if (!IsValid() && !rti.IsValid()) {
277 // Invalid types are equal.
278 return true;
279 }
280 if (!IsValid() || !rti.IsValid()) {
281 // One is valid, the other not.
282 return false;
283 }
284 return IsExact() == rti.IsExact()
285 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
286 }
287
288 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000289 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
290 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
291 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000292
293 // The class of the object.
294 TypeHandle type_handle_;
295 // Whether or not the type is exact or a superclass of the actual type.
296 // Whether or not we have any information about this type.
297 bool is_exact_;
298};
299
300std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
301
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000302// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100303class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100305 HGraph(ArenaAllocator* arena,
306 const DexFile& dex_file,
307 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100308 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700309 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100310 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100311 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000312 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100313 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000314 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100315 blocks_(arena->Adapter(kArenaAllocBlockList)),
316 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
317 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700318 entry_block_(nullptr),
319 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100320 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100321 number_of_vregs_(0),
322 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000323 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400324 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000325 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000326 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000327 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000328 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100329 dex_file_(dex_file),
330 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100331 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100332 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100333 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800334 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700335 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000336 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100337 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
338 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
339 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
340 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000341 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000342 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700343 osr_(osr),
344 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100345 blocks_.reserve(kDefaultNumberOfBlocks);
346 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000347
David Brazdilbadd8262016-02-02 16:28:56 +0000348 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700349 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000350
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000351 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100352 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
353
David Brazdil69ba7b72015-06-23 18:27:30 +0100354 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000355 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100356
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000357 HBasicBlock* GetEntryBlock() const { return entry_block_; }
358 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100359 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000360
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000361 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
362 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000363
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000364 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100365
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100366 void ComputeDominanceInformation();
367 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000368 void ClearLoopInformation();
369 void FindBackEdges(ArenaBitVector* visited);
370 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100371 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100372 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373
David Brazdil4833f5a2015-12-16 10:37:39 +0000374 // Analyze all natural loops in this graph. Returns a code specifying that it
375 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000376 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000377 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100378
David Brazdilffee3d32015-07-06 11:48:53 +0100379 // Iterate over blocks to compute try block membership. Needs reverse post
380 // order and loop information.
381 void ComputeTryBlockInformation();
382
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000383 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000384 // Returns the instruction to replace the invoke expression or null if the
385 // invoke is for a void method. Note that the caller is responsible for replacing
386 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000387 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000388
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000389 // Update the loop and try membership of `block`, which was spawned from `reference`.
390 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
391 // should be the new back edge.
392 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
393 HBasicBlock* reference,
394 bool replace_if_back_edge);
395
Mingyao Yang3584bce2015-05-19 16:01:59 -0700396 // Need to add a couple of blocks to test if the loop body is entered and
397 // put deoptimization instructions, etc.
398 void TransformLoopHeaderForBCE(HBasicBlock* header);
399
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000400 // Removes `block` from the graph. Assumes `block` has been disconnected from
401 // other blocks and has no instructions or phis.
402 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000403
David Brazdilfc6a86a2015-06-26 10:33:45 +0000404 // Splits the edge between `block` and `successor` while preserving the
405 // indices in the predecessor/successor lists. If there are multiple edges
406 // between the blocks, the lowest indices are used.
407 // Returns the new block which is empty and has the same dex pc as `successor`.
408 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
409
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100410 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
411 void SimplifyLoop(HBasicBlock* header);
412
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000413 int32_t GetNextInstructionId() {
414 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000415 return current_instruction_id_++;
416 }
417
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000418 int32_t GetCurrentInstructionId() const {
419 return current_instruction_id_;
420 }
421
422 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000423 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000424 current_instruction_id_ = id;
425 }
426
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100427 uint16_t GetMaximumNumberOfOutVRegs() const {
428 return maximum_number_of_out_vregs_;
429 }
430
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000431 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
432 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100433 }
434
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100435 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
436 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
437 }
438
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000439 void UpdateTemporariesVRegSlots(size_t slots) {
440 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100441 }
442
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000443 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100444 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000445 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100446 }
447
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100448 void SetNumberOfVRegs(uint16_t number_of_vregs) {
449 number_of_vregs_ = number_of_vregs;
450 }
451
452 uint16_t GetNumberOfVRegs() const {
453 return number_of_vregs_;
454 }
455
456 void SetNumberOfInVRegs(uint16_t value) {
457 number_of_in_vregs_ = value;
458 }
459
David Brazdildee58d62016-04-07 09:54:26 +0000460 uint16_t GetNumberOfInVRegs() const {
461 return number_of_in_vregs_;
462 }
463
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100464 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100465 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100466 return number_of_vregs_ - number_of_in_vregs_;
467 }
468
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100469 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100470 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100471 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100472
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100473 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
474 DCHECK(GetReversePostOrder()[0] == entry_block_);
475 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
476 }
477
478 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
479 return ReverseRange(GetReversePostOrder());
480 }
481
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100482 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100483 return linear_order_;
484 }
485
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100486 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
487 return ReverseRange(GetLinearOrder());
488 }
489
Mark Mendell1152c922015-04-24 17:06:35 -0400490 bool HasBoundsChecks() const {
491 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800492 }
493
Mark Mendell1152c922015-04-24 17:06:35 -0400494 void SetHasBoundsChecks(bool value) {
495 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800496 }
497
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100498 bool ShouldGenerateConstructorBarrier() const {
499 return should_generate_constructor_barrier_;
500 }
501
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000502 bool IsDebuggable() const { return debuggable_; }
503
David Brazdil8d5b8b22015-03-24 10:51:52 +0000504 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000505 // already, it is created and inserted into the graph. This method is only for
506 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600507 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000508
509 // TODO: This is problematic for the consistency of reference type propagation
510 // because it can be created anytime after the pass and thus it will be left
511 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600512 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000513
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600514 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
515 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000516 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600517 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
518 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000519 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600520 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
521 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000522 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600523 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
524 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000525 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000526
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100527 HCurrentMethod* GetCurrentMethod();
528
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100529 const DexFile& GetDexFile() const {
530 return dex_file_;
531 }
532
533 uint32_t GetMethodIdx() const {
534 return method_idx_;
535 }
536
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100537 InvokeType GetInvokeType() const {
538 return invoke_type_;
539 }
540
Mark Mendellc4701932015-04-10 13:18:51 -0400541 InstructionSet GetInstructionSet() const {
542 return instruction_set_;
543 }
544
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000545 bool IsCompilingOsr() const { return osr_; }
546
Mingyao Yang063fc772016-08-02 11:02:54 -0700547 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
548 return cha_single_implementation_list_;
549 }
550
551 void AddCHASingleImplementationDependency(ArtMethod* method) {
552 cha_single_implementation_list_.insert(method);
553 }
554
555 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800556 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700557 }
558
David Brazdil77a48ae2015-09-15 12:34:04 +0000559 bool HasTryCatch() const { return has_try_catch_; }
560 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100561
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000562 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
563 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
564
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100565 ArtMethod* GetArtMethod() const { return art_method_; }
566 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
567
Mark Mendellf6529172015-11-17 11:16:56 -0500568 // Returns an instruction with the opposite boolean value from 'cond'.
569 // The instruction has been inserted into the graph, either as a constant, or
570 // before cursor.
571 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
572
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000573 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
574
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800575 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
576 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
577 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
578
David Brazdil2d7352b2015-04-20 14:52:42 +0100579 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000580 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100581 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000582
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000583 template <class InstructionType, typename ValueType>
584 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600585 ArenaSafeMap<ValueType, InstructionType*>* cache,
586 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000587 // Try to find an existing constant of the given value.
588 InstructionType* constant = nullptr;
589 auto cached_constant = cache->find(value);
590 if (cached_constant != cache->end()) {
591 constant = cached_constant->second;
592 }
593
594 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100595 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000596 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600597 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000598 cache->Overwrite(value, constant);
599 InsertConstant(constant);
600 }
601 return constant;
602 }
603
David Brazdil8d5b8b22015-03-24 10:51:52 +0000604 void InsertConstant(HConstant* instruction);
605
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000606 // Cache a float constant into the graph. This method should only be
607 // called by the SsaBuilder when creating "equivalent" instructions.
608 void CacheFloatConstant(HFloatConstant* constant);
609
610 // See CacheFloatConstant comment.
611 void CacheDoubleConstant(HDoubleConstant* constant);
612
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000613 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000614
615 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100616 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000617
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100618 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100619 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000620
Aart Bik281c6812016-08-26 11:31:48 -0700621 // List of blocks to perform a linear order tree traversal. Unlike the reverse
622 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100623 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100624
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000625 HBasicBlock* entry_block_;
626 HBasicBlock* exit_block_;
627
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100628 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100629 uint16_t maximum_number_of_out_vregs_;
630
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100631 // The number of virtual registers in this method. Contains the parameters.
632 uint16_t number_of_vregs_;
633
634 // The number of virtual registers used by parameters of this method.
635 uint16_t number_of_in_vregs_;
636
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000637 // Number of vreg size slots that the temporaries use (used in baseline compiler).
638 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100639
Mark Mendell1152c922015-04-24 17:06:35 -0400640 // Has bounds checks. We can totally skip BCE if it's false.
641 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800642
David Brazdil77a48ae2015-09-15 12:34:04 +0000643 // Flag whether there are any try/catch blocks in the graph. We will skip
644 // try/catch-related passes if false.
645 bool has_try_catch_;
646
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000647 // Flag whether there are any irreducible loops in the graph.
648 bool has_irreducible_loops_;
649
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000650 // Indicates whether the graph should be compiled in a way that
651 // ensures full debuggability. If false, we can apply more
652 // aggressive optimizations that may limit the level of debugging.
653 const bool debuggable_;
654
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000655 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000656 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000657
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100658 // The dex file from which the method is from.
659 const DexFile& dex_file_;
660
661 // The method index in the dex file.
662 const uint32_t method_idx_;
663
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100664 // If inlined, this encodes how the callee is being invoked.
665 const InvokeType invoke_type_;
666
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100667 // Whether the graph has been transformed to SSA form. Only used
668 // in debug mode to ensure we are not using properties only valid
669 // for non-SSA form (like the number of temporaries).
670 bool in_ssa_form_;
671
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100672 const bool should_generate_constructor_barrier_;
673
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800674 // Number of CHA guards in the graph. Used to short-circuit the
675 // CHA guard optimization pass when there is no CHA guard left.
676 uint32_t number_of_cha_guards_;
677
Mathieu Chartiere401d142015-04-22 13:56:20 -0700678 const InstructionSet instruction_set_;
679
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000680 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000681 HNullConstant* cached_null_constant_;
682 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000683 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000684 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000685 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000686
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100687 HCurrentMethod* cached_current_method_;
688
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100689 // The ArtMethod this graph is for. Note that for AOT, it may be null,
690 // for example for methods whose declaring class could not be resolved
691 // (such as when the superclass could not be found).
692 ArtMethod* art_method_;
693
David Brazdil4833f5a2015-12-16 10:37:39 +0000694 // Keep the RTI of inexact Object to avoid having to pass stack handle
695 // collection pointer to passes which may create NullConstant.
696 ReferenceTypeInfo inexact_object_rti_;
697
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000698 // Whether we are compiling this graph for on stack replacement: this will
699 // make all loops seen as irreducible and emit special stack maps to mark
700 // compiled code entries which the interpreter can directly jump to.
701 const bool osr_;
702
Mingyao Yang063fc772016-08-02 11:02:54 -0700703 // List of methods that are assumed to have single implementation.
704 ArenaSet<ArtMethod*> cha_single_implementation_list_;
705
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000706 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100707 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000708 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000709 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000710 DISALLOW_COPY_AND_ASSIGN(HGraph);
711};
712
Vladimir Markof9f64412015-09-02 14:05:49 +0100713class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000714 public:
715 HLoopInformation(HBasicBlock* header, HGraph* graph)
716 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100717 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000718 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100719 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100720 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100721 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000722 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100723 back_edges_.reserve(kDefaultNumberOfBackEdges);
724 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100725
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000726 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100727 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000728
729 void Dump(std::ostream& os);
730
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100731 HBasicBlock* GetHeader() const {
732 return header_;
733 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000734
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000735 void SetHeader(HBasicBlock* block) {
736 header_ = block;
737 }
738
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100739 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
740 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
741 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
742
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000743 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100744 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000745 }
746
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100747 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100748 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100749 }
750
David Brazdil46e2a392015-03-16 17:31:52 +0000751 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100752 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100753 }
754
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000755 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100756 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000757 }
758
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100759 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100760
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100761 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100762 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100763 }
764
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100765 // Returns the lifetime position of the back edge that has the
766 // greatest lifetime position.
767 size_t GetLifetimeEnd() const;
768
769 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100770 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100771 }
772
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000773 // Finds blocks that are part of this loop.
774 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100775
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100776 // Returns whether this loop information contains `block`.
777 // Note that this loop information *must* be populated before entering this function.
778 bool Contains(const HBasicBlock& block) const;
779
780 // Returns whether this loop information is an inner loop of `other`.
781 // Note that `other` *must* be populated before entering this function.
782 bool IsIn(const HLoopInformation& other) const;
783
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800784 // Returns true if instruction is not defined within this loop.
785 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700786
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100787 const ArenaBitVector& GetBlocks() const { return blocks_; }
788
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000789 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000790 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000791
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000792 void ClearAllBlocks() {
793 blocks_.ClearAllBits();
794 }
795
David Brazdil3f4a5222016-05-06 12:46:21 +0100796 bool HasBackEdgeNotDominatedByHeader() const;
797
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100798 bool IsPopulated() const {
799 return blocks_.GetHighestBitSet() != -1;
800 }
801
Anton Shaminf89381f2016-05-16 16:44:13 +0600802 bool DominatesAllBackEdges(HBasicBlock* block);
803
David Sehrc757dec2016-11-04 15:48:34 -0700804 bool HasExitEdge() const;
805
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000806 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100807 // Internal recursive implementation of `Populate`.
808 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100809 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100810
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000811 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100812 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000813 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100814 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100815 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100816 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000817
818 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
819};
820
David Brazdilec16f792015-08-19 15:04:01 +0100821// Stores try/catch information for basic blocks.
822// Note that HGraph is constructed so that catch blocks cannot simultaneously
823// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100824class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100825 public:
826 // Try block information constructor.
827 explicit TryCatchInformation(const HTryBoundary& try_entry)
828 : try_entry_(&try_entry),
829 catch_dex_file_(nullptr),
830 catch_type_index_(DexFile::kDexNoIndex16) {
831 DCHECK(try_entry_ != nullptr);
832 }
833
834 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800835 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100836 : try_entry_(nullptr),
837 catch_dex_file_(&dex_file),
838 catch_type_index_(catch_type_index) {}
839
840 bool IsTryBlock() const { return try_entry_ != nullptr; }
841
842 const HTryBoundary& GetTryEntry() const {
843 DCHECK(IsTryBlock());
844 return *try_entry_;
845 }
846
847 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
848
849 bool IsCatchAllTypeIndex() const {
850 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800851 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100852 }
853
Andreas Gampea5b09a62016-11-17 15:21:22 -0800854 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100855 DCHECK(IsCatchBlock());
856 return catch_type_index_;
857 }
858
859 const DexFile& GetCatchDexFile() const {
860 DCHECK(IsCatchBlock());
861 return *catch_dex_file_;
862 }
863
864 private:
865 // One of possibly several TryBoundary instructions entering the block's try.
866 // Only set for try blocks.
867 const HTryBoundary* try_entry_;
868
869 // Exception type information. Only set for catch blocks.
870 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800871 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100872};
873
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100874static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100875static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100876
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000877// A block in a method. Contains the list of instructions represented
878// as a double linked list. Each block knows its predecessors and
879// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100880
Vladimir Markof9f64412015-09-02 14:05:49 +0100881class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000882 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700883 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000884 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000885 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
886 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000887 loop_information_(nullptr),
888 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000889 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100890 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100891 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100892 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000893 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000894 try_catch_information_(nullptr) {
895 predecessors_.reserve(kDefaultNumberOfPredecessors);
896 successors_.reserve(kDefaultNumberOfSuccessors);
897 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
898 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000899
Vladimir Marko60584552015-09-03 13:35:12 +0000900 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100901 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000902 }
903
Vladimir Marko60584552015-09-03 13:35:12 +0000904 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100905 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000906 }
907
David Brazdild26a4112015-11-10 11:07:31 +0000908 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
909 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
910
Vladimir Marko60584552015-09-03 13:35:12 +0000911 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
912 return ContainsElement(successors_, block, start_from);
913 }
914
915 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100916 return dominated_blocks_;
917 }
918
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100919 bool IsEntryBlock() const {
920 return graph_->GetEntryBlock() == this;
921 }
922
923 bool IsExitBlock() const {
924 return graph_->GetExitBlock() == this;
925 }
926
David Brazdil46e2a392015-03-16 17:31:52 +0000927 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000928 bool IsSingleTryBoundary() const;
929
930 // Returns true if this block emits nothing but a jump.
931 bool IsSingleJump() const {
932 HLoopInformation* loop_info = GetLoopInformation();
933 return (IsSingleGoto() || IsSingleTryBoundary())
934 // Back edges generate a suspend check.
935 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
936 }
David Brazdil46e2a392015-03-16 17:31:52 +0000937
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000938 void AddBackEdge(HBasicBlock* back_edge) {
939 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000940 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000941 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100942 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000943 loop_information_->AddBackEdge(back_edge);
944 }
945
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000946 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000947 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000948
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100949 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000950 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600951 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000952
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000953 HBasicBlock* GetDominator() const { return dominator_; }
954 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000955 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
956
957 void RemoveDominatedBlock(HBasicBlock* block) {
958 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100959 }
Vladimir Marko60584552015-09-03 13:35:12 +0000960
961 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
962 ReplaceElement(dominated_blocks_, existing, new_block);
963 }
964
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100965 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000966
967 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100968 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000969 }
970
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100971 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
972 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100973 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100974 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100975 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
976 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000977
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000978 HInstruction* GetFirstInstructionDisregardMoves() const;
979
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000980 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000981 successors_.push_back(block);
982 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000983 }
984
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100985 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
986 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100987 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000988 new_block->predecessors_.push_back(this);
989 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000990 }
991
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000992 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
993 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000994 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000995 new_block->successors_.push_back(this);
996 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000997 }
998
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100999 // Insert `this` between `predecessor` and `successor. This method
1000 // preserves the indicies, and will update the first edge found between
1001 // `predecessor` and `successor`.
1002 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1003 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001004 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001005 successor->predecessors_[predecessor_index] = this;
1006 predecessor->successors_[successor_index] = this;
1007 successors_.push_back(successor);
1008 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001009 }
1010
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001011 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001012 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001013 }
1014
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001015 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001016 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001017 }
1018
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001019 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001020 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001021 }
1022
1023 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001024 predecessors_.push_back(block);
1025 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001026 }
1027
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001028 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001029 DCHECK_EQ(predecessors_.size(), 2u);
1030 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001031 }
1032
David Brazdil769c9e52015-04-27 13:54:09 +01001033 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001034 DCHECK_EQ(successors_.size(), 2u);
1035 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001036 }
1037
David Brazdilfc6a86a2015-06-26 10:33:45 +00001038 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001039 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001040 }
1041
David Brazdilfc6a86a2015-06-26 10:33:45 +00001042 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001043 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001044 }
1045
David Brazdilfc6a86a2015-06-26 10:33:45 +00001046 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001047 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001048 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001049 }
1050
1051 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001052 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001053 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001054 }
1055
1056 // Returns whether the first occurrence of `predecessor` in the list of
1057 // predecessors is at index `idx`.
1058 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001059 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001060 return GetPredecessorIndexOf(predecessor) == idx;
1061 }
1062
David Brazdild7558da2015-09-22 13:04:14 +01001063 // Create a new block between this block and its predecessors. The new block
1064 // is added to the graph, all predecessor edges are relinked to it and an edge
1065 // is created to `this`. Returns the new empty block. Reverse post order or
1066 // loop and try/catch information are not updated.
1067 HBasicBlock* CreateImmediateDominator();
1068
David Brazdilfc6a86a2015-06-26 10:33:45 +00001069 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001070 // created, latter block. Note that this method will add the block to the
1071 // graph, create a Goto at the end of the former block and will create an edge
1072 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001073 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001074 HBasicBlock* SplitBefore(HInstruction* cursor);
1075
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001076 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001077 // created block. Note that this method just updates raw block information,
1078 // like predecessors, successors, dominators, and instruction list. It does not
1079 // update the graph, reverse post order, loop information, nor make sure the
1080 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001081 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1082
1083 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1084 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001085
1086 // Merge `other` at the end of `this`. Successors and dominated blocks of
1087 // `other` are changed to be successors and dominated blocks of `this`. Note
1088 // that this method does not update the graph, reverse post order, loop
1089 // information, nor make sure the blocks are consistent (for example ending
1090 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001091 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001092
1093 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1094 // of `this` are moved to `other`.
1095 // Note that this method does not update the graph, reverse post order, loop
1096 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001097 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001098 void ReplaceWith(HBasicBlock* other);
1099
David Brazdil2d7352b2015-04-20 14:52:42 +01001100 // Merge `other` at the end of `this`. This method updates loops, reverse post
1101 // order, links to predecessors, successors, dominators and deletes the block
1102 // from the graph. The two blocks must be successive, i.e. `this` the only
1103 // predecessor of `other` and vice versa.
1104 void MergeWith(HBasicBlock* other);
1105
1106 // Disconnects `this` from all its predecessors, successors and dominator,
1107 // removes it from all loops it is included in and eventually from the graph.
1108 // The block must not dominate any other block. Predecessors and successors
1109 // are safely updated.
1110 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001111
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001112 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001113 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001114 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001115 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001116 // Replace instruction `initial` with `replacement` within this block.
1117 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1118 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001119 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001120 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001121 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1122 // instruction list. With 'ensure_safety' set to true, it verifies that the
1123 // instruction is not in use and removes it from the use lists of its inputs.
1124 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1125 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001126 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001127
1128 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001129 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001130 }
1131
Roland Levillain6b879dd2014-09-22 17:13:44 +01001132 bool IsLoopPreHeaderFirstPredecessor() const {
1133 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001134 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001135 }
1136
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001137 bool IsFirstPredecessorBackEdge() const {
1138 DCHECK(IsLoopHeader());
1139 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1140 }
1141
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001142 HLoopInformation* GetLoopInformation() const {
1143 return loop_information_;
1144 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001145
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001146 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001147 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001148 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001149 void SetInLoop(HLoopInformation* info) {
1150 if (IsLoopHeader()) {
1151 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001152 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001153 loop_information_ = info;
1154 } else if (loop_information_->Contains(*info->GetHeader())) {
1155 // Block is currently part of an outer loop. Make it part of this inner loop.
1156 // Note that a non loop header having a loop information means this loop information
1157 // has already been populated
1158 loop_information_ = info;
1159 } else {
1160 // Block is part of an inner loop. Do not update the loop information.
1161 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1162 // at this point, because this method is being called while populating `info`.
1163 }
1164 }
1165
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001166 // Raw update of the loop information.
1167 void SetLoopInformation(HLoopInformation* info) {
1168 loop_information_ = info;
1169 }
1170
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001171 bool IsInLoop() const { return loop_information_ != nullptr; }
1172
David Brazdilec16f792015-08-19 15:04:01 +01001173 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1174
1175 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1176 try_catch_information_ = try_catch_information;
1177 }
1178
1179 bool IsTryBlock() const {
1180 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1181 }
1182
1183 bool IsCatchBlock() const {
1184 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1185 }
David Brazdilffee3d32015-07-06 11:48:53 +01001186
1187 // Returns the try entry that this block's successors should have. They will
1188 // be in the same try, unless the block ends in a try boundary. In that case,
1189 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001190 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001191
David Brazdild7558da2015-09-22 13:04:14 +01001192 bool HasThrowingInstructions() const;
1193
David Brazdila4b8c212015-05-07 09:59:30 +01001194 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001195 bool Dominates(HBasicBlock* block) const;
1196
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001197 size_t GetLifetimeStart() const { return lifetime_start_; }
1198 size_t GetLifetimeEnd() const { return lifetime_end_; }
1199
1200 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1201 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1202
David Brazdil8d5b8b22015-03-24 10:51:52 +00001203 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001204 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001205 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001206 bool HasSinglePhi() const;
1207
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001208 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001209 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001210 ArenaVector<HBasicBlock*> predecessors_;
1211 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001212 HInstructionList instructions_;
1213 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001214 HLoopInformation* loop_information_;
1215 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001216 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001217 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001218 // The dex program counter of the first instruction of this block.
1219 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001220 size_t lifetime_start_;
1221 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001222 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001223
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001224 friend class HGraph;
1225 friend class HInstruction;
1226
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001227 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1228};
1229
David Brazdilb2bd1c52015-03-25 11:17:37 +00001230// Iterates over the LoopInformation of all loops which contain 'block'
1231// from the innermost to the outermost.
1232class HLoopInformationOutwardIterator : public ValueObject {
1233 public:
1234 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1235 : current_(block.GetLoopInformation()) {}
1236
1237 bool Done() const { return current_ == nullptr; }
1238
1239 void Advance() {
1240 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001241 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001242 }
1243
1244 HLoopInformation* Current() const {
1245 DCHECK(!Done());
1246 return current_;
1247 }
1248
1249 private:
1250 HLoopInformation* current_;
1251
1252 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1253};
1254
Alexandre Ramesef20f712015-06-09 10:29:30 +01001255#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001256 M(Above, Condition) \
1257 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001258 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001259 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001260 M(ArrayGet, Instruction) \
1261 M(ArrayLength, Instruction) \
1262 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001263 M(Below, Condition) \
1264 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001265 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001266 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001267 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001268 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001269 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001270 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001271 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001272 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001273 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001274 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001275 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001276 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001277 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001278 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001279 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001280 M(Exit, Instruction) \
1281 M(FloatConstant, Constant) \
1282 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001283 M(GreaterThan, Condition) \
1284 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001285 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001286 M(InstanceFieldGet, Instruction) \
1287 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001288 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001289 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001290 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001291 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001292 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001293 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001294 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001295 M(LessThan, Condition) \
1296 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001297 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001298 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001299 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001300 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001301 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001302 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001303 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001304 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001305 M(Neg, UnaryOperation) \
1306 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001307 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001308 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001309 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001310 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001311 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001312 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001313 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001314 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001315 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001316 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001317 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001318 M(Return, Instruction) \
1319 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001320 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001321 M(Shl, BinaryOperation) \
1322 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001323 M(StaticFieldGet, Instruction) \
1324 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001325 M(UnresolvedInstanceFieldGet, Instruction) \
1326 M(UnresolvedInstanceFieldSet, Instruction) \
1327 M(UnresolvedStaticFieldGet, Instruction) \
1328 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001329 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001330 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001331 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001332 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001333 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001334 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001335 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001336 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001337
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001338/*
1339 * Instructions, shared across several (not all) architectures.
1340 */
1341#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1342#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1343#else
1344#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001345 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001346 M(MultiplyAccumulate, Instruction) \
1347 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001348#endif
1349
Vladimir Markob4536b72015-11-24 13:45:23 +00001350#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001351#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001352#else
1353#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1354 M(ArmDexCacheArraysBase, Instruction)
1355#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001356
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001357#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001358#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001359#else
1360#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001361 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001362#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001363
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001364#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001365#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001366#else
1367#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1368 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001369 M(MipsDexCacheArraysBase, Instruction) \
1370 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001371#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001372
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001373#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1374
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001375#ifndef ART_ENABLE_CODEGEN_x86
1376#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1377#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001378#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1379 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001380 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001381 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001382 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001383#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001384
1385#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1386
1387#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1388 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001389 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001390 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1391 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001392 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001393 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001394 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1395 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1396
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001397#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1398 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001399 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001400 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001401 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001402 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001403
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001404#define FOR_EACH_INSTRUCTION(M) \
1405 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1406 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1407
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001408#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001409FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1410#undef FORWARD_DECLARATION
1411
Vladimir Marko372f10e2016-05-17 16:30:10 +01001412#define DECLARE_INSTRUCTION(type) \
1413 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1414 const char* DebugName() const OVERRIDE { return #type; } \
1415 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1416 return other->Is##type(); \
1417 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001418 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001419
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001420#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1421 bool Is##type() const { return As##type() != nullptr; } \
1422 const H##type* As##type() const { return this; } \
1423 H##type* As##type() { return this; }
1424
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001425template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001426class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001427 public:
David Brazdiled596192015-01-23 10:39:45 +00001428 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001429 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001430 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001431
Vladimir Marko46817b82016-03-29 12:21:58 +01001432 // Hook for the IntrusiveForwardList<>.
1433 // TODO: Hide this better.
1434 IntrusiveForwardListHook hook;
1435
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001436 private:
David Brazdiled596192015-01-23 10:39:45 +00001437 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001438 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001439
1440 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001441 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001442
Vladimir Marko46817b82016-03-29 12:21:58 +01001443 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001444
1445 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1446};
1447
David Brazdiled596192015-01-23 10:39:45 +00001448template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001449using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001450
David Brazdil1abb4192015-02-17 18:33:36 +00001451// This class is used by HEnvironment and HInstruction classes to record the
1452// instructions they use and pointers to the corresponding HUseListNodes kept
1453// by the used instructions.
1454template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001455class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001456 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001457 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1458 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001459
Vladimir Marko46817b82016-03-29 12:21:58 +01001460 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1461 : HUserRecord(old_record.instruction_, before_use_node) {}
1462 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1463 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001464 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001465 }
1466
1467 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001468 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1469 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001470
1471 private:
1472 // Instruction used by the user.
1473 HInstruction* instruction_;
1474
Vladimir Marko46817b82016-03-29 12:21:58 +01001475 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1476 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001477};
1478
Vladimir Markoe9004912016-06-16 16:50:52 +01001479// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1480// This is used for HInstruction::GetInputs() to return a container wrapper providing
1481// HInstruction* values even though the underlying container has HUserRecord<>s.
1482struct HInputExtractor {
1483 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1484 return record.GetInstruction();
1485 }
1486 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1487 return record.GetInstruction();
1488 }
1489};
1490
1491using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1492using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1493
Aart Bik854a02b2015-07-14 16:07:00 -07001494/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001495 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001496 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001497 * For write/read dependences on fields/arrays, the dependence analysis uses
1498 * type disambiguation (e.g. a float field write cannot modify the value of an
1499 * integer field read) and the access type (e.g. a reference array write cannot
1500 * modify the value of a reference field read [although it may modify the
1501 * reference fetch prior to reading the field, which is represented by its own
1502 * write/read dependence]). The analysis makes conservative points-to
1503 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1504 * the same, and any reference read depends on any reference read without
1505 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001506 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001507 * The internal representation uses 38-bit and is described in the table below.
1508 * The first line indicates the side effect, and for field/array accesses the
1509 * second line indicates the type of the access (in the order of the
1510 * Primitive::Type enum).
1511 * The two numbered lines below indicate the bit position in the bitfield (read
1512 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001513 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001514 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1515 * +-------------+---------+---------+--------------+---------+---------+
1516 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1517 * | 3 |333333322|222222221| 1 |111111110|000000000|
1518 * | 7 |654321098|765432109| 8 |765432109|876543210|
1519 *
1520 * Note that, to ease the implementation, 'changes' bits are least significant
1521 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001522 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001523class SideEffects : public ValueObject {
1524 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001525 SideEffects() : flags_(0) {}
1526
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001527 static SideEffects None() {
1528 return SideEffects(0);
1529 }
1530
1531 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001532 return SideEffects(kAllChangeBits | kAllDependOnBits);
1533 }
1534
1535 static SideEffects AllChanges() {
1536 return SideEffects(kAllChangeBits);
1537 }
1538
1539 static SideEffects AllDependencies() {
1540 return SideEffects(kAllDependOnBits);
1541 }
1542
1543 static SideEffects AllExceptGCDependency() {
1544 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1545 }
1546
1547 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001548 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001549 }
1550
Aart Bik34c3ba92015-07-20 14:08:59 -07001551 static SideEffects AllWrites() {
1552 return SideEffects(kAllWrites);
1553 }
1554
1555 static SideEffects AllReads() {
1556 return SideEffects(kAllReads);
1557 }
1558
1559 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1560 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001561 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001562 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001563 }
1564
Aart Bik854a02b2015-07-14 16:07:00 -07001565 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001566 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001567 }
1568
Aart Bik34c3ba92015-07-20 14:08:59 -07001569 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1570 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001571 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001572 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001573 }
1574
1575 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001576 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001577 }
1578
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001579 static SideEffects CanTriggerGC() {
1580 return SideEffects(1ULL << kCanTriggerGCBit);
1581 }
1582
1583 static SideEffects DependsOnGC() {
1584 return SideEffects(1ULL << kDependsOnGCBit);
1585 }
1586
Aart Bik854a02b2015-07-14 16:07:00 -07001587 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001588 SideEffects Union(SideEffects other) const {
1589 return SideEffects(flags_ | other.flags_);
1590 }
1591
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001592 SideEffects Exclusion(SideEffects other) const {
1593 return SideEffects(flags_ & ~other.flags_);
1594 }
1595
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001596 void Add(SideEffects other) {
1597 flags_ |= other.flags_;
1598 }
1599
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001600 bool Includes(SideEffects other) const {
1601 return (other.flags_ & flags_) == other.flags_;
1602 }
1603
1604 bool HasSideEffects() const {
1605 return (flags_ & kAllChangeBits);
1606 }
1607
1608 bool HasDependencies() const {
1609 return (flags_ & kAllDependOnBits);
1610 }
1611
1612 // Returns true if there are no side effects or dependencies.
1613 bool DoesNothing() const {
1614 return flags_ == 0;
1615 }
1616
Aart Bik854a02b2015-07-14 16:07:00 -07001617 // Returns true if something is written.
1618 bool DoesAnyWrite() const {
1619 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001620 }
1621
Aart Bik854a02b2015-07-14 16:07:00 -07001622 // Returns true if something is read.
1623 bool DoesAnyRead() const {
1624 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001625 }
1626
Aart Bik854a02b2015-07-14 16:07:00 -07001627 // Returns true if potentially everything is written and read
1628 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001629 bool DoesAllReadWrite() const {
1630 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1631 }
1632
Aart Bik854a02b2015-07-14 16:07:00 -07001633 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001634 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001635 }
1636
Roland Levillain0d5a2812015-11-13 10:07:31 +00001637 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001638 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001639 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1640 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001641 }
1642
1643 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001644 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001645 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001646 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001647 for (int s = kLastBit; s >= 0; s--) {
1648 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1649 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1650 // This is a bit for the GC side effect.
1651 if (current_bit_is_set) {
1652 flags += "GC";
1653 }
Aart Bik854a02b2015-07-14 16:07:00 -07001654 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001655 } else {
1656 // This is a bit for the array/field analysis.
1657 // The underscore character stands for the 'can trigger GC' bit.
1658 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1659 if (current_bit_is_set) {
1660 flags += kDebug[s];
1661 }
1662 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1663 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1664 flags += "|";
1665 }
1666 }
Aart Bik854a02b2015-07-14 16:07:00 -07001667 }
1668 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001669 }
1670
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001671 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001672
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001673 private:
1674 static constexpr int kFieldArrayAnalysisBits = 9;
1675
1676 static constexpr int kFieldWriteOffset = 0;
1677 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1678 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1679 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1680
1681 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1682
1683 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1684 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1685 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1686 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1687
1688 static constexpr int kLastBit = kDependsOnGCBit;
1689 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1690
1691 // Aliases.
1692
1693 static_assert(kChangeBits == kDependOnBits,
1694 "the 'change' bits should match the 'depend on' bits.");
1695
1696 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1697 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1698 static constexpr uint64_t kAllWrites =
1699 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1700 static constexpr uint64_t kAllReads =
1701 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001702
Aart Bik854a02b2015-07-14 16:07:00 -07001703 // Translates type to bit flag.
1704 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1705 CHECK_NE(type, Primitive::kPrimVoid);
1706 const uint64_t one = 1;
1707 const int shift = type; // 0-based consecutive enum
1708 DCHECK_LE(kFieldWriteOffset, shift);
1709 DCHECK_LT(shift, kArrayWriteOffset);
1710 return one << (type + offset);
1711 }
1712
1713 // Private constructor on direct flags value.
1714 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1715
1716 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001717};
1718
David Brazdiled596192015-01-23 10:39:45 +00001719// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001720class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001721 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001722 HEnvironment(ArenaAllocator* arena,
1723 size_t number_of_vregs,
1724 const DexFile& dex_file,
1725 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001726 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001727 InvokeType invoke_type,
1728 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001729 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1730 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001731 parent_(nullptr),
1732 dex_file_(dex_file),
1733 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001734 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001735 invoke_type_(invoke_type),
1736 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001737 }
1738
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001739 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001740 : HEnvironment(arena,
1741 to_copy.Size(),
1742 to_copy.GetDexFile(),
1743 to_copy.GetMethodIdx(),
1744 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001745 to_copy.GetInvokeType(),
1746 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001747
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001748 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001749 if (parent_ != nullptr) {
1750 parent_->SetAndCopyParentChain(allocator, parent);
1751 } else {
1752 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1753 parent_->CopyFrom(parent);
1754 if (parent->GetParent() != nullptr) {
1755 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1756 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001757 }
David Brazdiled596192015-01-23 10:39:45 +00001758 }
1759
Vladimir Marko71bf8092015-09-15 15:33:14 +01001760 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001761 void CopyFrom(HEnvironment* environment);
1762
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001763 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1764 // input to the loop phi instead. This is for inserting instructions that
1765 // require an environment (like HDeoptimization) in the loop pre-header.
1766 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001767
1768 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001769 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001770 }
1771
1772 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001773 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001774 }
1775
David Brazdil1abb4192015-02-17 18:33:36 +00001776 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001777
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001778 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001779
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001780 HEnvironment* GetParent() const { return parent_; }
1781
1782 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001783 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001784 }
1785
1786 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001787 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001788 }
1789
1790 uint32_t GetDexPc() const {
1791 return dex_pc_;
1792 }
1793
1794 uint32_t GetMethodIdx() const {
1795 return method_idx_;
1796 }
1797
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001798 InvokeType GetInvokeType() const {
1799 return invoke_type_;
1800 }
1801
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001802 const DexFile& GetDexFile() const {
1803 return dex_file_;
1804 }
1805
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001806 HInstruction* GetHolder() const {
1807 return holder_;
1808 }
1809
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001810
1811 bool IsFromInlinedInvoke() const {
1812 return GetParent() != nullptr;
1813 }
1814
David Brazdiled596192015-01-23 10:39:45 +00001815 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001816 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1817 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001818 HEnvironment* parent_;
1819 const DexFile& dex_file_;
1820 const uint32_t method_idx_;
1821 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001822 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001823
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001824 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001825 HInstruction* const holder_;
1826
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001827 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001828
1829 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1830};
1831
Vladimir Markof9f64412015-09-02 14:05:49 +01001832class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001833 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001834 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001835 : previous_(nullptr),
1836 next_(nullptr),
1837 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001838 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001839 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001840 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001841 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001842 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001843 locations_(nullptr),
1844 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001845 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001846 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001847 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1848 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1849 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001850
Dave Allison20dfc792014-06-16 20:44:29 -07001851 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001852
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001853#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001854 enum InstructionKind {
1855 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1856 };
1857#undef DECLARE_KIND
1858
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001859 HInstruction* GetNext() const { return next_; }
1860 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001861
Calin Juravle77520bc2015-01-12 18:45:46 +00001862 HInstruction* GetNextDisregardingMoves() const;
1863 HInstruction* GetPreviousDisregardingMoves() const;
1864
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001865 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001866 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001867 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001868 bool IsInBlock() const { return block_ != nullptr; }
1869 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001870 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1871 bool IsIrreducibleLoopHeaderPhi() const {
1872 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1873 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001874
Vladimir Marko372f10e2016-05-17 16:30:10 +01001875 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1876
1877 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1878 // One virtual method is enough, just const_cast<> and then re-add the const.
1879 return ArrayRef<const HUserRecord<HInstruction*>>(
1880 const_cast<HInstruction*>(this)->GetInputRecords());
1881 }
1882
Vladimir Markoe9004912016-06-16 16:50:52 +01001883 HInputsRef GetInputs() {
1884 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001885 }
1886
Vladimir Markoe9004912016-06-16 16:50:52 +01001887 HConstInputsRef GetInputs() const {
1888 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001889 }
1890
1891 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001892 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001893
Aart Bik2767f4b2016-10-28 15:03:53 -07001894 bool HasInput(HInstruction* input) const {
1895 for (const HInstruction* i : GetInputs()) {
1896 if (i == input) {
1897 return true;
1898 }
1899 }
1900 return false;
1901 }
1902
Vladimir Marko372f10e2016-05-17 16:30:10 +01001903 void SetRawInputAt(size_t index, HInstruction* input) {
1904 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1905 }
1906
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001907 virtual void Accept(HGraphVisitor* visitor) = 0;
1908 virtual const char* DebugName() const = 0;
1909
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001910 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1911
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001912 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001913
1914 uint32_t GetDexPc() const { return dex_pc_; }
1915
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001916 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001917
Roland Levillaine161a2a2014-10-03 12:45:18 +01001918 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001919 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001920
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001921 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001922 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001923
Calin Juravle10e244f2015-01-26 18:54:32 +00001924 // Does not apply for all instructions, but having this at top level greatly
1925 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001926 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001927 virtual bool CanBeNull() const {
1928 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1929 return true;
1930 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001931
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001932 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001933 return false;
1934 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001935
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001936 virtual bool IsActualObject() const {
1937 return GetType() == Primitive::kPrimNot;
1938 }
1939
Calin Juravle2e768302015-07-28 14:41:11 +00001940 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001941
Calin Juravle61d544b2015-02-23 16:46:57 +00001942 ReferenceTypeInfo GetReferenceTypeInfo() const {
1943 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001944 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001945 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001946 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001947
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001948 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001949 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001950 // Note: fixup_end remains valid across push_front().
1951 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1952 HUseListNode<HInstruction*>* new_node =
1953 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1954 uses_.push_front(*new_node);
1955 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001956 }
1957
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001958 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001959 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001960 // Note: env_fixup_end remains valid across push_front().
1961 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1962 HUseListNode<HEnvironment*>* new_node =
1963 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1964 env_uses_.push_front(*new_node);
1965 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001966 }
1967
David Brazdil1abb4192015-02-17 18:33:36 +00001968 void RemoveAsUserOfInput(size_t input) {
1969 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001970 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1971 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1972 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001973 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001974
Vladimir Marko372f10e2016-05-17 16:30:10 +01001975 void RemoveAsUserOfAllInputs() {
1976 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1977 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1978 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1979 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1980 }
1981 }
1982
David Brazdil1abb4192015-02-17 18:33:36 +00001983 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1984 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001985
Vladimir Marko46817b82016-03-29 12:21:58 +01001986 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1987 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1988 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001989 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001990 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001991 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001992
Aart Bikcc42be02016-10-20 16:14:16 -07001993 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07001994 return
Aart Bikff7d89c2016-11-07 08:49:28 -08001995 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07001996 !CanThrow() &&
1997 !IsSuspendCheck() &&
1998 !IsControlFlow() &&
1999 !IsNativeDebugInfo() &&
2000 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002001 // If we added an explicit barrier then we should keep it.
2002 !IsMemoryBarrier();
2003 }
2004
Aart Bikcc42be02016-10-20 16:14:16 -07002005 bool IsDeadAndRemovable() const {
2006 return IsRemovable() && !HasUses();
2007 }
2008
Roland Levillain6c82d402014-10-13 16:10:27 +01002009 // Does this instruction strictly dominate `other_instruction`?
2010 // Returns false if this instruction and `other_instruction` are the same.
2011 // Aborts if this instruction and `other_instruction` are both phis.
2012 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002013
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002014 int GetId() const { return id_; }
2015 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002016
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002017 int GetSsaIndex() const { return ssa_index_; }
2018 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2019 bool HasSsaIndex() const { return ssa_index_ != -1; }
2020
2021 bool HasEnvironment() const { return environment_ != nullptr; }
2022 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002023 // Set the `environment_` field. Raw because this method does not
2024 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002025 void SetRawEnvironment(HEnvironment* environment) {
2026 DCHECK(environment_ == nullptr);
2027 DCHECK_EQ(environment->GetHolder(), this);
2028 environment_ = environment;
2029 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002030
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002031 void InsertRawEnvironment(HEnvironment* environment) {
2032 DCHECK(environment_ != nullptr);
2033 DCHECK_EQ(environment->GetHolder(), this);
2034 DCHECK(environment->GetParent() == nullptr);
2035 environment->parent_ = environment_;
2036 environment_ = environment;
2037 }
2038
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002039 void RemoveEnvironment();
2040
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002041 // Set the environment of this instruction, copying it from `environment`. While
2042 // copying, the uses lists are being updated.
2043 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002044 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002045 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002046 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002047 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002048 if (environment->GetParent() != nullptr) {
2049 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2050 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002051 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002052
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002053 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2054 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002055 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002056 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002057 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002058 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002059 if (environment->GetParent() != nullptr) {
2060 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2061 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002062 }
2063
Nicolas Geoffray39468442014-09-02 15:17:15 +01002064 // Returns the number of entries in the environment. Typically, that is the
2065 // number of dex registers in a method. It could be more in case of inlining.
2066 size_t EnvironmentSize() const;
2067
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002068 LocationSummary* GetLocations() const { return locations_; }
2069 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002070
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002071 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002072 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002073
Alexandre Rames188d4312015-04-09 18:30:21 +01002074 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2075 // uses of this instruction by `other` are *not* updated.
2076 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2077 ReplaceWith(other);
2078 other->ReplaceInput(this, use_index);
2079 }
2080
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002081 // Move `this` instruction before `cursor`.
2082 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002083
Vladimir Markofb337ea2015-11-25 15:25:10 +00002084 // Move `this` before its first user and out of any loops. If there is no
2085 // out-of-loop user that dominates all other users, move the instruction
2086 // to the end of the out-of-loop common dominator of the user's blocks.
2087 //
2088 // This can be used only on non-throwing instructions with no side effects that
2089 // have at least one use but no environment uses.
2090 void MoveBeforeFirstUserAndOutOfLoops();
2091
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002092#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002093 bool Is##type() const; \
2094 const H##type* As##type() const; \
2095 H##type* As##type();
2096
2097 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2098#undef INSTRUCTION_TYPE_CHECK
2099
2100#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002101 bool Is##type() const { return (As##type() != nullptr); } \
2102 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002103 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002104 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002105#undef INSTRUCTION_TYPE_CHECK
2106
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002107 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002108 // TODO: this method is used by LICM and GVN with possibly different
2109 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002110 virtual bool CanBeMoved() const { return false; }
2111
2112 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002113 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002114 return false;
2115 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002116
2117 // Returns whether any data encoded in the two instructions is equal.
2118 // This method does not look at the inputs. Both instructions must be
2119 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002120 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002121 return false;
2122 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002123
2124 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002125 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002126 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002127 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002128
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002129 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2130 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2131 // the virtual function because the __attribute__((__pure__)) doesn't really
2132 // apply the strong requirement for virtual functions, preventing optimizations.
2133 InstructionKind GetKind() const PURE;
2134 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002135
2136 virtual size_t ComputeHashCode() const {
2137 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002138 for (const HInstruction* input : GetInputs()) {
2139 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002140 }
2141 return result;
2142 }
2143
2144 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002145 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002146 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002147
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002148 size_t GetLifetimePosition() const { return lifetime_position_; }
2149 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2150 LiveInterval* GetLiveInterval() const { return live_interval_; }
2151 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2152 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2153
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002154 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2155
2156 // Returns whether the code generation of the instruction will require to have access
2157 // to the current method. Such instructions are:
2158 // (1): Instructions that require an environment, as calling the runtime requires
2159 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002160 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2161 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002162 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002163 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002164 }
2165
Vladimir Markodc151b22015-10-15 18:02:30 +01002166 // Returns whether the code generation of the instruction will require to have access
2167 // to the dex cache of the current method's declaring class via the current method.
2168 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002169
Mark Mendellc4701932015-04-10 13:18:51 -04002170 // Does this instruction have any use in an environment before
2171 // control flow hits 'other'?
2172 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2173
2174 // Remove all references to environment uses of this instruction.
2175 // The caller must ensure that this is safe to do.
2176 void RemoveEnvironmentUsers();
2177
Vladimir Markoa1de9182016-02-25 11:37:38 +00002178 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2179 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002180
David Brazdil1abb4192015-02-17 18:33:36 +00002181 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002182 // If set, the machine code for this instruction is assumed to be generated by
2183 // its users. Used by liveness analysis to compute use positions accordingly.
2184 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2185 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2186 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2187 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2188
Vladimir Marko372f10e2016-05-17 16:30:10 +01002189 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2190 return GetInputRecords()[i];
2191 }
2192
2193 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2194 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2195 input_records[index] = input;
2196 }
David Brazdil1abb4192015-02-17 18:33:36 +00002197
Vladimir Markoa1de9182016-02-25 11:37:38 +00002198 uint32_t GetPackedFields() const {
2199 return packed_fields_;
2200 }
2201
2202 template <size_t flag>
2203 bool GetPackedFlag() const {
2204 return (packed_fields_ & (1u << flag)) != 0u;
2205 }
2206
2207 template <size_t flag>
2208 void SetPackedFlag(bool value = true) {
2209 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2210 }
2211
2212 template <typename BitFieldType>
2213 typename BitFieldType::value_type GetPackedField() const {
2214 return BitFieldType::Decode(packed_fields_);
2215 }
2216
2217 template <typename BitFieldType>
2218 void SetPackedField(typename BitFieldType::value_type value) {
2219 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2220 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2221 }
2222
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002223 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002224 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2225 auto before_use_node = uses_.before_begin();
2226 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2227 HInstruction* user = use_node->GetUser();
2228 size_t input_index = use_node->GetIndex();
2229 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2230 before_use_node = use_node;
2231 }
2232 }
2233
2234 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2235 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2236 if (next != uses_.end()) {
2237 HInstruction* next_user = next->GetUser();
2238 size_t next_index = next->GetIndex();
2239 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2240 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2241 }
2242 }
2243
2244 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2245 auto before_env_use_node = env_uses_.before_begin();
2246 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2247 HEnvironment* user = env_use_node->GetUser();
2248 size_t input_index = env_use_node->GetIndex();
2249 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2250 before_env_use_node = env_use_node;
2251 }
2252 }
2253
2254 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2255 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2256 if (next != env_uses_.end()) {
2257 HEnvironment* next_user = next->GetUser();
2258 size_t next_index = next->GetIndex();
2259 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2260 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2261 }
2262 }
David Brazdil1abb4192015-02-17 18:33:36 +00002263
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002264 HInstruction* previous_;
2265 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002266 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002267 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002268
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002269 // An instruction gets an id when it is added to the graph.
2270 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002271 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002272 int id_;
2273
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002274 // When doing liveness analysis, instructions that have uses get an SSA index.
2275 int ssa_index_;
2276
Vladimir Markoa1de9182016-02-25 11:37:38 +00002277 // Packed fields.
2278 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002279
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002280 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002281 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002282
2283 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002284 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002285
Nicolas Geoffray39468442014-09-02 15:17:15 +01002286 // The environment associated with this instruction. Not null if the instruction
2287 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002288 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002289
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002290 // Set by the code generator.
2291 LocationSummary* locations_;
2292
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002293 // Set by the liveness analysis.
2294 LiveInterval* live_interval_;
2295
2296 // Set by the liveness analysis, this is the position in a linear
2297 // order of blocks where this instruction's live interval start.
2298 size_t lifetime_position_;
2299
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002300 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002301
Vladimir Markoa1de9182016-02-25 11:37:38 +00002302 // The reference handle part of the reference type info.
2303 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002304 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002305 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002306
David Brazdil1abb4192015-02-17 18:33:36 +00002307 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002308 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002309 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002310 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002311 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002312
2313 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2314};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002315std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002316
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002317// Iterates over the instructions, while preserving the next instruction
2318// in case the current instruction gets removed from the list by the user
2319// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002320class HInstructionIterator : public ValueObject {
2321 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002322 explicit HInstructionIterator(const HInstructionList& instructions)
2323 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002324 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002325 }
2326
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002327 bool Done() const { return instruction_ == nullptr; }
2328 HInstruction* Current() const { return instruction_; }
2329 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002330 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002331 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002332 }
2333
2334 private:
2335 HInstruction* instruction_;
2336 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002337
2338 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002339};
2340
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002341// Iterates over the instructions without saving the next instruction,
2342// therefore handling changes in the graph potentially made by the user
2343// of this iterator.
2344class HInstructionIteratorHandleChanges : public ValueObject {
2345 public:
2346 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2347 : instruction_(instructions.first_instruction_) {
2348 }
2349
2350 bool Done() const { return instruction_ == nullptr; }
2351 HInstruction* Current() const { return instruction_; }
2352 void Advance() {
2353 instruction_ = instruction_->GetNext();
2354 }
2355
2356 private:
2357 HInstruction* instruction_;
2358
2359 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2360};
2361
2362
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002363class HBackwardInstructionIterator : public ValueObject {
2364 public:
2365 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2366 : instruction_(instructions.last_instruction_) {
2367 next_ = Done() ? nullptr : instruction_->GetPrevious();
2368 }
2369
2370 bool Done() const { return instruction_ == nullptr; }
2371 HInstruction* Current() const { return instruction_; }
2372 void Advance() {
2373 instruction_ = next_;
2374 next_ = Done() ? nullptr : instruction_->GetPrevious();
2375 }
2376
2377 private:
2378 HInstruction* instruction_;
2379 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002380
2381 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002382};
2383
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002384class HVariableInputSizeInstruction : public HInstruction {
2385 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002386 using HInstruction::GetInputRecords; // Keep the const version visible.
2387 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2388 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2389 }
2390
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002391 void AddInput(HInstruction* input);
2392 void InsertInputAt(size_t index, HInstruction* input);
2393 void RemoveInputAt(size_t index);
2394
2395 protected:
2396 HVariableInputSizeInstruction(SideEffects side_effects,
2397 uint32_t dex_pc,
2398 ArenaAllocator* arena,
2399 size_t number_of_inputs,
2400 ArenaAllocKind kind)
2401 : HInstruction(side_effects, dex_pc),
2402 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2403
2404 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2405
2406 private:
2407 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2408};
2409
Vladimir Markof9f64412015-09-02 14:05:49 +01002410template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002411class HTemplateInstruction: public HInstruction {
2412 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002413 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002414 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002415 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002416
Vladimir Marko372f10e2016-05-17 16:30:10 +01002417 using HInstruction::GetInputRecords; // Keep the const version visible.
2418 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2419 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002420 }
2421
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002422 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002423 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002424
2425 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002426};
2427
Vladimir Markof9f64412015-09-02 14:05:49 +01002428// HTemplateInstruction specialization for N=0.
2429template<>
2430class HTemplateInstruction<0>: public HInstruction {
2431 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002432 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002433 : HInstruction(side_effects, dex_pc) {}
2434
Vladimir Markof9f64412015-09-02 14:05:49 +01002435 virtual ~HTemplateInstruction() {}
2436
Vladimir Marko372f10e2016-05-17 16:30:10 +01002437 using HInstruction::GetInputRecords; // Keep the const version visible.
2438 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2439 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002440 }
2441
2442 private:
2443 friend class SsaBuilder;
2444};
2445
Dave Allison20dfc792014-06-16 20:44:29 -07002446template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002447class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002448 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002449 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002450 : HTemplateInstruction<N>(side_effects, dex_pc) {
2451 this->template SetPackedField<TypeField>(type);
2452 }
Dave Allison20dfc792014-06-16 20:44:29 -07002453 virtual ~HExpression() {}
2454
Vladimir Markoa1de9182016-02-25 11:37:38 +00002455 Primitive::Type GetType() const OVERRIDE {
2456 return TypeField::Decode(this->GetPackedFields());
2457 }
Dave Allison20dfc792014-06-16 20:44:29 -07002458
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002459 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002460 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2461 static constexpr size_t kFieldTypeSize =
2462 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2463 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2464 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2465 "Too many packed fields.");
2466 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002467};
2468
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002469// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2470// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002471class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002472 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002473 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2474 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002475
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002476 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002477
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002478 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002479
2480 private:
2481 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2482};
2483
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002484// Represents dex's RETURN opcodes. A HReturn is a control flow
2485// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002486class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002487 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002488 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2489 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002490 SetRawInputAt(0, value);
2491 }
2492
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002493 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002494
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002495 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002496
2497 private:
2498 DISALLOW_COPY_AND_ASSIGN(HReturn);
2499};
2500
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002501class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002502 public:
2503 HPhi(ArenaAllocator* arena,
2504 uint32_t reg_number,
2505 size_t number_of_inputs,
2506 Primitive::Type type,
2507 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002508 : HVariableInputSizeInstruction(
2509 SideEffects::None(),
2510 dex_pc,
2511 arena,
2512 number_of_inputs,
2513 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002514 reg_number_(reg_number) {
2515 SetPackedField<TypeField>(ToPhiType(type));
2516 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2517 // Phis are constructed live and marked dead if conflicting or unused.
2518 // Individual steps of SsaBuilder should assume that if a phi has been
2519 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2520 SetPackedFlag<kFlagIsLive>(true);
2521 SetPackedFlag<kFlagCanBeNull>(true);
2522 }
2523
2524 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2525 static Primitive::Type ToPhiType(Primitive::Type type) {
2526 return Primitive::PrimitiveKind(type);
2527 }
2528
2529 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2530
David Brazdildee58d62016-04-07 09:54:26 +00002531 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2532 void SetType(Primitive::Type new_type) {
2533 // Make sure that only valid type changes occur. The following are allowed:
2534 // (1) int -> float/ref (primitive type propagation),
2535 // (2) long -> double (primitive type propagation).
2536 DCHECK(GetType() == new_type ||
2537 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2538 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2539 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2540 SetPackedField<TypeField>(new_type);
2541 }
2542
2543 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2544 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2545
2546 uint32_t GetRegNumber() const { return reg_number_; }
2547
2548 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2549 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2550 bool IsDead() const { return !IsLive(); }
2551 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2552
Vladimir Markoe9004912016-06-16 16:50:52 +01002553 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002554 return other != nullptr
2555 && other->IsPhi()
2556 && other->AsPhi()->GetBlock() == GetBlock()
2557 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2558 }
2559
2560 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2561 // An equivalent phi is a phi having the same dex register and type.
2562 // It assumes that phis with the same dex register are adjacent.
2563 HPhi* GetNextEquivalentPhiWithSameType() {
2564 HInstruction* next = GetNext();
2565 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2566 if (next->GetType() == GetType()) {
2567 return next->AsPhi();
2568 }
2569 next = next->GetNext();
2570 }
2571 return nullptr;
2572 }
2573
2574 DECLARE_INSTRUCTION(Phi);
2575
David Brazdildee58d62016-04-07 09:54:26 +00002576 private:
2577 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2578 static constexpr size_t kFieldTypeSize =
2579 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2580 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2581 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2582 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2583 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2584 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2585
David Brazdildee58d62016-04-07 09:54:26 +00002586 const uint32_t reg_number_;
2587
2588 DISALLOW_COPY_AND_ASSIGN(HPhi);
2589};
2590
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002591// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002592// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002593// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002594class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002595 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002596 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002597
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002598 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002599
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002600 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002601
2602 private:
2603 DISALLOW_COPY_AND_ASSIGN(HExit);
2604};
2605
2606// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002607class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002608 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002609 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002610
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002611 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002612
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002613 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002614 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002615 }
2616
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002617 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002618
2619 private:
2620 DISALLOW_COPY_AND_ASSIGN(HGoto);
2621};
2622
Roland Levillain9867bc72015-08-05 10:21:34 +01002623class HConstant : public HExpression<0> {
2624 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002625 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2626 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002627
2628 bool CanBeMoved() const OVERRIDE { return true; }
2629
Roland Levillain1a653882016-03-18 18:05:57 +00002630 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002631 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002632 // Is this constant 0 in the arithmetic sense?
2633 virtual bool IsArithmeticZero() const { return false; }
2634 // Is this constant a 0-bit pattern?
2635 virtual bool IsZeroBitPattern() const { return false; }
2636 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002637 virtual bool IsOne() const { return false; }
2638
David Brazdil77a48ae2015-09-15 12:34:04 +00002639 virtual uint64_t GetValueAsUint64() const = 0;
2640
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002641 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002642
2643 private:
2644 DISALLOW_COPY_AND_ASSIGN(HConstant);
2645};
2646
Vladimir Markofcb503c2016-05-18 12:48:17 +01002647class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002648 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002649 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002650 return true;
2651 }
2652
David Brazdil77a48ae2015-09-15 12:34:04 +00002653 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2654
Roland Levillain9867bc72015-08-05 10:21:34 +01002655 size_t ComputeHashCode() const OVERRIDE { return 0; }
2656
Roland Levillain1a653882016-03-18 18:05:57 +00002657 // The null constant representation is a 0-bit pattern.
2658 virtual bool IsZeroBitPattern() const { return true; }
2659
Roland Levillain9867bc72015-08-05 10:21:34 +01002660 DECLARE_INSTRUCTION(NullConstant);
2661
2662 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002663 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002664
2665 friend class HGraph;
2666 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2667};
2668
2669// Constants of the type int. Those can be from Dex instructions, or
2670// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002671class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002672 public:
2673 int32_t GetValue() const { return value_; }
2674
David Brazdil9f389d42015-10-01 14:32:56 +01002675 uint64_t GetValueAsUint64() const OVERRIDE {
2676 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2677 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002678
Vladimir Marko372f10e2016-05-17 16:30:10 +01002679 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002680 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002681 return other->AsIntConstant()->value_ == value_;
2682 }
2683
2684 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2685
2686 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002687 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2688 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002689 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2690
Roland Levillain1a653882016-03-18 18:05:57 +00002691 // Integer constants are used to encode Boolean values as well,
2692 // where 1 means true and 0 means false.
2693 bool IsTrue() const { return GetValue() == 1; }
2694 bool IsFalse() const { return GetValue() == 0; }
2695
Roland Levillain9867bc72015-08-05 10:21:34 +01002696 DECLARE_INSTRUCTION(IntConstant);
2697
2698 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002699 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2700 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2701 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2702 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002703
2704 const int32_t value_;
2705
2706 friend class HGraph;
2707 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2708 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2709 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2710};
2711
Vladimir Markofcb503c2016-05-18 12:48:17 +01002712class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002713 public:
2714 int64_t GetValue() const { return value_; }
2715
David Brazdil77a48ae2015-09-15 12:34:04 +00002716 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2717
Vladimir Marko372f10e2016-05-17 16:30:10 +01002718 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002719 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002720 return other->AsLongConstant()->value_ == value_;
2721 }
2722
2723 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2724
2725 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002726 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2727 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002728 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2729
2730 DECLARE_INSTRUCTION(LongConstant);
2731
2732 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002733 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2734 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002735
2736 const int64_t value_;
2737
2738 friend class HGraph;
2739 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2740};
Dave Allison20dfc792014-06-16 20:44:29 -07002741
Vladimir Markofcb503c2016-05-18 12:48:17 +01002742class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002743 public:
2744 float GetValue() const { return value_; }
2745
2746 uint64_t GetValueAsUint64() const OVERRIDE {
2747 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2748 }
2749
Vladimir Marko372f10e2016-05-17 16:30:10 +01002750 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002751 DCHECK(other->IsFloatConstant()) << other->DebugName();
2752 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2753 }
2754
2755 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2756
2757 bool IsMinusOne() const OVERRIDE {
2758 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2759 }
Roland Levillain1a653882016-03-18 18:05:57 +00002760 bool IsArithmeticZero() const OVERRIDE {
2761 return std::fpclassify(value_) == FP_ZERO;
2762 }
2763 bool IsArithmeticPositiveZero() const {
2764 return IsArithmeticZero() && !std::signbit(value_);
2765 }
2766 bool IsArithmeticNegativeZero() const {
2767 return IsArithmeticZero() && std::signbit(value_);
2768 }
2769 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002770 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002771 }
2772 bool IsOne() const OVERRIDE {
2773 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2774 }
2775 bool IsNaN() const {
2776 return std::isnan(value_);
2777 }
2778
2779 DECLARE_INSTRUCTION(FloatConstant);
2780
2781 private:
2782 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2783 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2784 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2785 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2786
2787 const float value_;
2788
2789 // Only the SsaBuilder and HGraph can create floating-point constants.
2790 friend class SsaBuilder;
2791 friend class HGraph;
2792 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2793};
2794
Vladimir Markofcb503c2016-05-18 12:48:17 +01002795class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002796 public:
2797 double GetValue() const { return value_; }
2798
2799 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2800
Vladimir Marko372f10e2016-05-17 16:30:10 +01002801 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002802 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2803 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2804 }
2805
2806 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2807
2808 bool IsMinusOne() const OVERRIDE {
2809 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2810 }
Roland Levillain1a653882016-03-18 18:05:57 +00002811 bool IsArithmeticZero() const OVERRIDE {
2812 return std::fpclassify(value_) == FP_ZERO;
2813 }
2814 bool IsArithmeticPositiveZero() const {
2815 return IsArithmeticZero() && !std::signbit(value_);
2816 }
2817 bool IsArithmeticNegativeZero() const {
2818 return IsArithmeticZero() && std::signbit(value_);
2819 }
2820 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002821 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002822 }
2823 bool IsOne() const OVERRIDE {
2824 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2825 }
2826 bool IsNaN() const {
2827 return std::isnan(value_);
2828 }
2829
2830 DECLARE_INSTRUCTION(DoubleConstant);
2831
2832 private:
2833 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2834 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2835 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2836 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2837
2838 const double value_;
2839
2840 // Only the SsaBuilder and HGraph can create floating-point constants.
2841 friend class SsaBuilder;
2842 friend class HGraph;
2843 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2844};
2845
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002846// Conditional branch. A block ending with an HIf instruction must have
2847// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002848class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002849 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002850 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2851 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002852 SetRawInputAt(0, input);
2853 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002854
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002855 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002856
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002857 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002858 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002859 }
2860
2861 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002862 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002863 }
2864
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002865 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002866
2867 private:
2868 DISALLOW_COPY_AND_ASSIGN(HIf);
2869};
2870
David Brazdilfc6a86a2015-06-26 10:33:45 +00002871
2872// Abstract instruction which marks the beginning and/or end of a try block and
2873// links it to the respective exception handlers. Behaves the same as a Goto in
2874// non-exceptional control flow.
2875// Normal-flow successor is stored at index zero, exception handlers under
2876// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002877class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002878 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002879 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002880 kEntry,
2881 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002882 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002883 };
2884
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002885 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002886 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2887 SetPackedField<BoundaryKindField>(kind);
2888 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002889
2890 bool IsControlFlow() const OVERRIDE { return true; }
2891
2892 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002893 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002894
David Brazdild26a4112015-11-10 11:07:31 +00002895 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2896 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2897 }
2898
David Brazdilfc6a86a2015-06-26 10:33:45 +00002899 // Returns whether `handler` is among its exception handlers (non-zero index
2900 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002901 bool HasExceptionHandler(const HBasicBlock& handler) const {
2902 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002903 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002904 }
2905
2906 // If not present already, adds `handler` to its block's list of exception
2907 // handlers.
2908 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002909 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002910 GetBlock()->AddSuccessor(handler);
2911 }
2912 }
2913
Vladimir Markoa1de9182016-02-25 11:37:38 +00002914 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2915 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002916
David Brazdilffee3d32015-07-06 11:48:53 +01002917 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2918
David Brazdilfc6a86a2015-06-26 10:33:45 +00002919 DECLARE_INSTRUCTION(TryBoundary);
2920
2921 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002922 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2923 static constexpr size_t kFieldBoundaryKindSize =
2924 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2925 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2926 kFieldBoundaryKind + kFieldBoundaryKindSize;
2927 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2928 "Too many packed fields.");
2929 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002930
2931 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2932};
2933
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002934// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002935class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002936 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002937 // We set CanTriggerGC to prevent any intermediate address to be live
2938 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002939 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002940 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002941 SetRawInputAt(0, cond);
2942 }
2943
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002944 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002945 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002946 return true;
2947 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002948 bool NeedsEnvironment() const OVERRIDE { return true; }
2949 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002950
2951 DECLARE_INSTRUCTION(Deoptimize);
2952
2953 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002954 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2955};
2956
Mingyao Yang063fc772016-08-02 11:02:54 -07002957// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
2958// The compiled code checks this flag value in a guard before devirtualized call and
2959// if it's true, starts to do deoptimization.
2960// It has a 4-byte slot on stack.
2961// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002962class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07002963 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002964 // CHA guards are only optimized in a separate pass and it has no side effects
2965 // with regard to other passes.
2966 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
2967 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07002968 }
2969
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002970 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
2971
2972 // We do all CHA guard elimination/motion in a single pass, after which there is no
2973 // further guard elimination/motion since a guard might have been used for justification
2974 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
2975 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07002976 bool CanBeMoved() const OVERRIDE { return false; }
2977
2978 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
2979
2980 private:
2981 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
2982};
2983
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002984// Represents the ArtMethod that was passed as a first argument to
2985// the method. It is used by instructions that depend on it, like
2986// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002987class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002988 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002989 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2990 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002991
2992 DECLARE_INSTRUCTION(CurrentMethod);
2993
2994 private:
2995 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2996};
2997
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002998// Fetches an ArtMethod from the virtual table or the interface method table
2999// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003000class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003001 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003002 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003003 kVTable,
3004 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003005 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003006 };
3007 HClassTableGet(HInstruction* cls,
3008 Primitive::Type type,
3009 TableKind kind,
3010 size_t index,
3011 uint32_t dex_pc)
3012 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003013 index_(index) {
3014 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003015 SetRawInputAt(0, cls);
3016 }
3017
3018 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003019 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003020 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003021 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003022 }
3023
Vladimir Markoa1de9182016-02-25 11:37:38 +00003024 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003025 size_t GetIndex() const { return index_; }
3026
3027 DECLARE_INSTRUCTION(ClassTableGet);
3028
3029 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003030 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3031 static constexpr size_t kFieldTableKindSize =
3032 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3033 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3034 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3035 "Too many packed fields.");
3036 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3037
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003038 // The index of the ArtMethod in the table.
3039 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003040
3041 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3042};
3043
Mark Mendellfe57faa2015-09-18 09:26:15 -04003044// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3045// have one successor for each entry in the switch table, and the final successor
3046// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003047class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003048 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003049 HPackedSwitch(int32_t start_value,
3050 uint32_t num_entries,
3051 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003052 uint32_t dex_pc = kNoDexPc)
3053 : HTemplateInstruction(SideEffects::None(), dex_pc),
3054 start_value_(start_value),
3055 num_entries_(num_entries) {
3056 SetRawInputAt(0, input);
3057 }
3058
3059 bool IsControlFlow() const OVERRIDE { return true; }
3060
3061 int32_t GetStartValue() const { return start_value_; }
3062
Vladimir Marko211c2112015-09-24 16:52:33 +01003063 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003064
3065 HBasicBlock* GetDefaultBlock() const {
3066 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003067 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003068 }
3069 DECLARE_INSTRUCTION(PackedSwitch);
3070
3071 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003072 const int32_t start_value_;
3073 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003074
3075 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3076};
3077
Roland Levillain88cb1752014-10-20 16:36:47 +01003078class HUnaryOperation : public HExpression<1> {
3079 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003080 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3081 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003082 SetRawInputAt(0, input);
3083 }
3084
3085 HInstruction* GetInput() const { return InputAt(0); }
3086 Primitive::Type GetResultType() const { return GetType(); }
3087
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003088 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003089 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003090 return true;
3091 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003092
Roland Levillain31dd3d62016-02-16 12:21:02 +00003093 // Try to statically evaluate `this` and return a HConstant
3094 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003095 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003096 HConstant* TryStaticEvaluation() const;
3097
3098 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003099 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3100 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003101 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3102 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003103
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003104 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003105
3106 private:
3107 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3108};
3109
Dave Allison20dfc792014-06-16 20:44:29 -07003110class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003111 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003112 HBinaryOperation(Primitive::Type result_type,
3113 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003114 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003115 SideEffects side_effects = SideEffects::None(),
3116 uint32_t dex_pc = kNoDexPc)
3117 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003118 SetRawInputAt(0, left);
3119 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003120 }
3121
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003122 HInstruction* GetLeft() const { return InputAt(0); }
3123 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003124 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003125
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003126 virtual bool IsCommutative() const { return false; }
3127
3128 // Put constant on the right.
3129 // Returns whether order is changed.
3130 bool OrderInputsWithConstantOnTheRight() {
3131 HInstruction* left = InputAt(0);
3132 HInstruction* right = InputAt(1);
3133 if (left->IsConstant() && !right->IsConstant()) {
3134 ReplaceInput(right, 0);
3135 ReplaceInput(left, 1);
3136 return true;
3137 }
3138 return false;
3139 }
3140
3141 // Order inputs by instruction id, but favor constant on the right side.
3142 // This helps GVN for commutative ops.
3143 void OrderInputs() {
3144 DCHECK(IsCommutative());
3145 HInstruction* left = InputAt(0);
3146 HInstruction* right = InputAt(1);
3147 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3148 return;
3149 }
3150 if (OrderInputsWithConstantOnTheRight()) {
3151 return;
3152 }
3153 // Order according to instruction id.
3154 if (left->GetId() > right->GetId()) {
3155 ReplaceInput(right, 0);
3156 ReplaceInput(left, 1);
3157 }
3158 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003159
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003160 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003161 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003162 return true;
3163 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003164
Roland Levillain31dd3d62016-02-16 12:21:02 +00003165 // Try to statically evaluate `this` and return a HConstant
3166 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003167 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003168 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003169
3170 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003171 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3172 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003173 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3174 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003175 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003176 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3177 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003178 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3179 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003180 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3181 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003182 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003183 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3184 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003185
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003186 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003187 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003188 HConstant* GetConstantRight() const;
3189
3190 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003191 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003192 HInstruction* GetLeastConstantLeft() const;
3193
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003194 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003195
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003196 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003197 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3198};
3199
Mark Mendellc4701932015-04-10 13:18:51 -04003200// The comparison bias applies for floating point operations and indicates how NaN
3201// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003202enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003203 kNoBias, // bias is not applicable (i.e. for long operation)
3204 kGtBias, // return 1 for NaN comparisons
3205 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003206 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003207};
3208
Roland Levillain31dd3d62016-02-16 12:21:02 +00003209std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3210
Dave Allison20dfc792014-06-16 20:44:29 -07003211class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003212 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003213 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003214 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3215 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3216 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003217
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003218 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003219 // `instruction`, and disregard moves in between.
3220 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003221
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003222 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003223
3224 virtual IfCondition GetCondition() const = 0;
3225
Mark Mendellc4701932015-04-10 13:18:51 -04003226 virtual IfCondition GetOppositeCondition() const = 0;
3227
Vladimir Markoa1de9182016-02-25 11:37:38 +00003228 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003229 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3230
Vladimir Markoa1de9182016-02-25 11:37:38 +00003231 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3232 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003233
Vladimir Marko372f10e2016-05-17 16:30:10 +01003234 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003235 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003236 }
3237
Roland Levillain4fa13f62015-07-06 18:11:54 +01003238 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003239 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003240 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003241 if (if_cond == kCondNE) {
3242 return true;
3243 } else if (if_cond == kCondEQ) {
3244 return false;
3245 }
3246 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003247 }
3248
3249 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003250 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003251 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003252 if (if_cond == kCondEQ) {
3253 return true;
3254 } else if (if_cond == kCondNE) {
3255 return false;
3256 }
3257 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003258 }
3259
Roland Levillain31dd3d62016-02-16 12:21:02 +00003260 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003261 // Needed if we merge a HCompare into a HCondition.
3262 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3263 static constexpr size_t kFieldComparisonBiasSize =
3264 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3265 static constexpr size_t kNumberOfConditionPackedBits =
3266 kFieldComparisonBias + kFieldComparisonBiasSize;
3267 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3268 using ComparisonBiasField =
3269 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3270
Roland Levillain31dd3d62016-02-16 12:21:02 +00003271 template <typename T>
3272 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3273
3274 template <typename T>
3275 int32_t CompareFP(T x, T y) const {
3276 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3277 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3278 // Handle the bias.
3279 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3280 }
3281
3282 // Return an integer constant containing the result of a condition evaluated at compile time.
3283 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3284 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3285 }
3286
Dave Allison20dfc792014-06-16 20:44:29 -07003287 private:
3288 DISALLOW_COPY_AND_ASSIGN(HCondition);
3289};
3290
3291// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003292class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003293 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003294 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3295 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003296
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003297 bool IsCommutative() const OVERRIDE { return true; }
3298
Vladimir Marko9e23df52015-11-10 17:14:35 +00003299 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3300 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003301 return MakeConstantCondition(true, GetDexPc());
3302 }
3303 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3304 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3305 }
3306 // In the following Evaluate methods, a HCompare instruction has
3307 // been merged into this HEqual instruction; evaluate it as
3308 // `Compare(x, y) == 0`.
3309 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3310 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3311 GetDexPc());
3312 }
3313 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3314 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3315 }
3316 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3317 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003318 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003319
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003320 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003321
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003322 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003323 return kCondEQ;
3324 }
3325
Mark Mendellc4701932015-04-10 13:18:51 -04003326 IfCondition GetOppositeCondition() const OVERRIDE {
3327 return kCondNE;
3328 }
3329
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003330 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003331 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003332
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003333 DISALLOW_COPY_AND_ASSIGN(HEqual);
3334};
3335
Vladimir Markofcb503c2016-05-18 12:48:17 +01003336class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003337 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003338 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3339 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003340
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003341 bool IsCommutative() const OVERRIDE { return true; }
3342
Vladimir Marko9e23df52015-11-10 17:14:35 +00003343 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3344 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003345 return MakeConstantCondition(false, GetDexPc());
3346 }
3347 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3348 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3349 }
3350 // In the following Evaluate methods, a HCompare instruction has
3351 // been merged into this HNotEqual instruction; evaluate it as
3352 // `Compare(x, y) != 0`.
3353 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3354 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3355 }
3356 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3357 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3358 }
3359 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3360 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003361 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003362
Dave Allison20dfc792014-06-16 20:44:29 -07003363 DECLARE_INSTRUCTION(NotEqual);
3364
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003365 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003366 return kCondNE;
3367 }
3368
Mark Mendellc4701932015-04-10 13:18:51 -04003369 IfCondition GetOppositeCondition() const OVERRIDE {
3370 return kCondEQ;
3371 }
3372
Dave Allison20dfc792014-06-16 20:44:29 -07003373 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003374 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003375
Dave Allison20dfc792014-06-16 20:44:29 -07003376 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3377};
3378
Vladimir Markofcb503c2016-05-18 12:48:17 +01003379class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003380 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003381 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3382 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003383
Roland Levillain9867bc72015-08-05 10:21:34 +01003384 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003385 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003386 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003387 // In the following Evaluate methods, a HCompare instruction has
3388 // been merged into this HLessThan instruction; evaluate it as
3389 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003390 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003391 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3392 }
3393 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3394 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3395 }
3396 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3397 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003398 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003399
Dave Allison20dfc792014-06-16 20:44:29 -07003400 DECLARE_INSTRUCTION(LessThan);
3401
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003402 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003403 return kCondLT;
3404 }
3405
Mark Mendellc4701932015-04-10 13:18:51 -04003406 IfCondition GetOppositeCondition() const OVERRIDE {
3407 return kCondGE;
3408 }
3409
Dave Allison20dfc792014-06-16 20:44:29 -07003410 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003411 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003412
Dave Allison20dfc792014-06-16 20:44:29 -07003413 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3414};
3415
Vladimir Markofcb503c2016-05-18 12:48:17 +01003416class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003417 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003418 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3419 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003420
Roland Levillain9867bc72015-08-05 10:21:34 +01003421 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003422 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003423 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003424 // In the following Evaluate methods, a HCompare instruction has
3425 // been merged into this HLessThanOrEqual instruction; evaluate it as
3426 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003427 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003428 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3429 }
3430 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3431 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3432 }
3433 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3434 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003435 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003436
Dave Allison20dfc792014-06-16 20:44:29 -07003437 DECLARE_INSTRUCTION(LessThanOrEqual);
3438
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003439 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003440 return kCondLE;
3441 }
3442
Mark Mendellc4701932015-04-10 13:18:51 -04003443 IfCondition GetOppositeCondition() const OVERRIDE {
3444 return kCondGT;
3445 }
3446
Dave Allison20dfc792014-06-16 20:44:29 -07003447 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003448 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003449
Dave Allison20dfc792014-06-16 20:44:29 -07003450 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3451};
3452
Vladimir Markofcb503c2016-05-18 12:48:17 +01003453class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003454 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003455 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3456 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003457
Roland Levillain9867bc72015-08-05 10:21:34 +01003458 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003459 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003460 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003461 // In the following Evaluate methods, a HCompare instruction has
3462 // been merged into this HGreaterThan instruction; evaluate it as
3463 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003464 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003465 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3466 }
3467 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3468 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3469 }
3470 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3471 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003472 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003473
Dave Allison20dfc792014-06-16 20:44:29 -07003474 DECLARE_INSTRUCTION(GreaterThan);
3475
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003476 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003477 return kCondGT;
3478 }
3479
Mark Mendellc4701932015-04-10 13:18:51 -04003480 IfCondition GetOppositeCondition() const OVERRIDE {
3481 return kCondLE;
3482 }
3483
Dave Allison20dfc792014-06-16 20:44:29 -07003484 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003485 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003486
Dave Allison20dfc792014-06-16 20:44:29 -07003487 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3488};
3489
Vladimir Markofcb503c2016-05-18 12:48:17 +01003490class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003491 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003492 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3493 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003494
Roland Levillain9867bc72015-08-05 10:21:34 +01003495 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003496 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003497 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003498 // In the following Evaluate methods, a HCompare instruction has
3499 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3500 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003501 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003502 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3503 }
3504 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3505 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3506 }
3507 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3508 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003509 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003510
Dave Allison20dfc792014-06-16 20:44:29 -07003511 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3512
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003513 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003514 return kCondGE;
3515 }
3516
Mark Mendellc4701932015-04-10 13:18:51 -04003517 IfCondition GetOppositeCondition() const OVERRIDE {
3518 return kCondLT;
3519 }
3520
Dave Allison20dfc792014-06-16 20:44:29 -07003521 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003522 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003523
Dave Allison20dfc792014-06-16 20:44:29 -07003524 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3525};
3526
Vladimir Markofcb503c2016-05-18 12:48:17 +01003527class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003528 public:
3529 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3530 : HCondition(first, second, dex_pc) {}
3531
3532 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003533 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003534 }
3535 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003536 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3537 }
3538 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3539 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3540 LOG(FATAL) << DebugName() << " is not defined for float values";
3541 UNREACHABLE();
3542 }
3543 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3544 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3545 LOG(FATAL) << DebugName() << " is not defined for double values";
3546 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003547 }
3548
3549 DECLARE_INSTRUCTION(Below);
3550
3551 IfCondition GetCondition() const OVERRIDE {
3552 return kCondB;
3553 }
3554
3555 IfCondition GetOppositeCondition() const OVERRIDE {
3556 return kCondAE;
3557 }
3558
3559 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003560 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003561 return MakeUnsigned(x) < MakeUnsigned(y);
3562 }
Aart Bike9f37602015-10-09 11:15:55 -07003563
3564 DISALLOW_COPY_AND_ASSIGN(HBelow);
3565};
3566
Vladimir Markofcb503c2016-05-18 12:48:17 +01003567class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003568 public:
3569 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3570 : HCondition(first, second, dex_pc) {}
3571
3572 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003573 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003574 }
3575 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003576 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3577 }
3578 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3579 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3580 LOG(FATAL) << DebugName() << " is not defined for float values";
3581 UNREACHABLE();
3582 }
3583 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3584 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3585 LOG(FATAL) << DebugName() << " is not defined for double values";
3586 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003587 }
3588
3589 DECLARE_INSTRUCTION(BelowOrEqual);
3590
3591 IfCondition GetCondition() const OVERRIDE {
3592 return kCondBE;
3593 }
3594
3595 IfCondition GetOppositeCondition() const OVERRIDE {
3596 return kCondA;
3597 }
3598
3599 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003600 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003601 return MakeUnsigned(x) <= MakeUnsigned(y);
3602 }
Aart Bike9f37602015-10-09 11:15:55 -07003603
3604 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3605};
3606
Vladimir Markofcb503c2016-05-18 12:48:17 +01003607class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003608 public:
3609 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3610 : HCondition(first, second, dex_pc) {}
3611
3612 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003613 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003614 }
3615 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003616 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3617 }
3618 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3619 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3620 LOG(FATAL) << DebugName() << " is not defined for float values";
3621 UNREACHABLE();
3622 }
3623 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3624 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3625 LOG(FATAL) << DebugName() << " is not defined for double values";
3626 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003627 }
3628
3629 DECLARE_INSTRUCTION(Above);
3630
3631 IfCondition GetCondition() const OVERRIDE {
3632 return kCondA;
3633 }
3634
3635 IfCondition GetOppositeCondition() const OVERRIDE {
3636 return kCondBE;
3637 }
3638
3639 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003640 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003641 return MakeUnsigned(x) > MakeUnsigned(y);
3642 }
Aart Bike9f37602015-10-09 11:15:55 -07003643
3644 DISALLOW_COPY_AND_ASSIGN(HAbove);
3645};
3646
Vladimir Markofcb503c2016-05-18 12:48:17 +01003647class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003648 public:
3649 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3650 : HCondition(first, second, dex_pc) {}
3651
3652 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003653 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003654 }
3655 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003656 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3657 }
3658 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3659 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3660 LOG(FATAL) << DebugName() << " is not defined for float values";
3661 UNREACHABLE();
3662 }
3663 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3664 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3665 LOG(FATAL) << DebugName() << " is not defined for double values";
3666 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003667 }
3668
3669 DECLARE_INSTRUCTION(AboveOrEqual);
3670
3671 IfCondition GetCondition() const OVERRIDE {
3672 return kCondAE;
3673 }
3674
3675 IfCondition GetOppositeCondition() const OVERRIDE {
3676 return kCondB;
3677 }
3678
3679 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003680 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003681 return MakeUnsigned(x) >= MakeUnsigned(y);
3682 }
Aart Bike9f37602015-10-09 11:15:55 -07003683
3684 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3685};
Dave Allison20dfc792014-06-16 20:44:29 -07003686
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003687// Instruction to check how two inputs compare to each other.
3688// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003689class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003690 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003691 // Note that `comparison_type` is the type of comparison performed
3692 // between the comparison's inputs, not the type of the instantiated
3693 // HCompare instruction (which is always Primitive::kPrimInt).
3694 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003695 HInstruction* first,
3696 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003697 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003698 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003699 : HBinaryOperation(Primitive::kPrimInt,
3700 first,
3701 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003702 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003703 dex_pc) {
3704 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003705 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3706 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003707 }
3708
Roland Levillain9867bc72015-08-05 10:21:34 +01003709 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003710 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3711
3712 template <typename T>
3713 int32_t ComputeFP(T x, T y) const {
3714 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3715 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3716 // Handle the bias.
3717 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3718 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003719
Roland Levillain9867bc72015-08-05 10:21:34 +01003720 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003721 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3722 // reach this code path when processing a freshly built HIR
3723 // graph. However HCompare integer instructions can be synthesized
3724 // by the instruction simplifier to implement IntegerCompare and
3725 // IntegerSignum intrinsics, so we have to handle this case.
3726 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003727 }
3728 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003729 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3730 }
3731 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3732 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3733 }
3734 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3735 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003736 }
3737
Vladimir Marko372f10e2016-05-17 16:30:10 +01003738 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003739 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003740 }
3741
Vladimir Markoa1de9182016-02-25 11:37:38 +00003742 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003743
Roland Levillain31dd3d62016-02-16 12:21:02 +00003744 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003745 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003746 bool IsGtBias() const {
3747 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003748 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003749 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003750
Roland Levillain1693a1f2016-03-15 14:57:31 +00003751 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3752 // Comparisons do not require a runtime call in any back end.
3753 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003754 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003755
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003756 DECLARE_INSTRUCTION(Compare);
3757
Roland Levillain31dd3d62016-02-16 12:21:02 +00003758 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003759 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3760 static constexpr size_t kFieldComparisonBiasSize =
3761 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3762 static constexpr size_t kNumberOfComparePackedBits =
3763 kFieldComparisonBias + kFieldComparisonBiasSize;
3764 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3765 using ComparisonBiasField =
3766 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3767
Roland Levillain31dd3d62016-02-16 12:21:02 +00003768 // Return an integer constant containing the result of a comparison evaluated at compile time.
3769 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3770 DCHECK(value == -1 || value == 0 || value == 1) << value;
3771 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3772 }
3773
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003774 private:
3775 DISALLOW_COPY_AND_ASSIGN(HCompare);
3776};
3777
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00003778class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003779 public:
3780 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003781 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003782 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003783 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003784 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003785 bool finalizable,
3786 QuickEntrypointEnum entrypoint)
3787 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3788 type_index_(type_index),
3789 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003790 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003791 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003792 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003793 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003794 }
3795
Andreas Gampea5b09a62016-11-17 15:21:22 -08003796 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003797 const DexFile& GetDexFile() const { return dex_file_; }
3798
3799 // Calls runtime so needs an environment.
3800 bool NeedsEnvironment() const OVERRIDE { return true; }
3801
Mingyao Yang062157f2016-03-02 10:15:36 -08003802 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3803 bool CanThrow() const OVERRIDE { return true; }
3804
3805 // Needs to call into runtime to make sure it's instantiable/accessible.
3806 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003807
Vladimir Markoa1de9182016-02-25 11:37:38 +00003808 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003809
3810 bool CanBeNull() const OVERRIDE { return false; }
3811
3812 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3813
3814 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3815 entrypoint_ = entrypoint;
3816 }
3817
3818 bool IsStringAlloc() const;
3819
3820 DECLARE_INSTRUCTION(NewInstance);
3821
3822 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003823 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3824 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003825 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3826 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3827 "Too many packed fields.");
3828
Andreas Gampea5b09a62016-11-17 15:21:22 -08003829 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003830 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003831 QuickEntrypointEnum entrypoint_;
3832
3833 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3834};
3835
Agi Csaki05f20562015-08-19 14:58:14 -07003836enum IntrinsicNeedsEnvironmentOrCache {
3837 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3838 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003839};
3840
Aart Bik5d75afe2015-12-14 11:57:01 -08003841enum IntrinsicSideEffects {
3842 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3843 kReadSideEffects, // Intrinsic may read heap memory.
3844 kWriteSideEffects, // Intrinsic may write heap memory.
3845 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3846};
3847
3848enum IntrinsicExceptions {
3849 kNoThrow, // Intrinsic does not throw any exceptions.
3850 kCanThrow // Intrinsic may throw exceptions.
3851};
3852
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003853class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003854 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003855 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003856
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003857 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003858 SetRawInputAt(index, argument);
3859 }
3860
Roland Levillain3e3d7332015-04-28 11:00:54 +01003861 // Return the number of arguments. This number can be lower than
3862 // the number of inputs returned by InputCount(), as some invoke
3863 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3864 // inputs at the end of their list of inputs.
3865 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3866
Vladimir Markoa1de9182016-02-25 11:37:38 +00003867 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003868
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003869 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003870 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003871
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003872 InvokeType GetInvokeType() const {
3873 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003874 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003875
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003876 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003877 return intrinsic_;
3878 }
3879
Aart Bik5d75afe2015-12-14 11:57:01 -08003880 void SetIntrinsic(Intrinsics intrinsic,
3881 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3882 IntrinsicSideEffects side_effects,
3883 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003884
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003885 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003886 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003887 }
3888
Aart Bikff7d89c2016-11-07 08:49:28 -08003889 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
3890
Vladimir Markoa1de9182016-02-25 11:37:38 +00003891 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003892
Aart Bik71bf7b42016-11-16 10:17:46 -08003893 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003894
Vladimir Marko372f10e2016-05-17 16:30:10 +01003895 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003896 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3897 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003898
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003899 uint32_t* GetIntrinsicOptimizations() {
3900 return &intrinsic_optimizations_;
3901 }
3902
3903 const uint32_t* GetIntrinsicOptimizations() const {
3904 return &intrinsic_optimizations_;
3905 }
3906
3907 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3908
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003909 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3910
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003911 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003912
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003913 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003914 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3915 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003916 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3917 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003918 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003919 static constexpr size_t kFieldReturnTypeSize =
3920 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3921 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3922 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3923 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003924 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003925 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3926
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003927 HInvoke(ArenaAllocator* arena,
3928 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003929 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003930 Primitive::Type return_type,
3931 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003932 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003933 ArtMethod* resolved_method,
3934 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003935 : HVariableInputSizeInstruction(
3936 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
3937 dex_pc,
3938 arena,
3939 number_of_arguments + number_of_other_inputs,
3940 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003941 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003942 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003943 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003944 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003945 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003946 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003947 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003948 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003949 }
3950
Roland Levillain3e3d7332015-04-28 11:00:54 +01003951 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003952 ArtMethod* const resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003953 const uint32_t dex_method_index_;
3954 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003955
3956 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3957 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003958
3959 private:
3960 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3961};
3962
Vladimir Markofcb503c2016-05-18 12:48:17 +01003963class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003964 public:
3965 HInvokeUnresolved(ArenaAllocator* arena,
3966 uint32_t number_of_arguments,
3967 Primitive::Type return_type,
3968 uint32_t dex_pc,
3969 uint32_t dex_method_index,
3970 InvokeType invoke_type)
3971 : HInvoke(arena,
3972 number_of_arguments,
3973 0u /* number_of_other_inputs */,
3974 return_type,
3975 dex_pc,
3976 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003977 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003978 invoke_type) {
3979 }
3980
3981 DECLARE_INSTRUCTION(InvokeUnresolved);
3982
3983 private:
3984 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3985};
3986
Orion Hodsonac141392017-01-13 11:53:47 +00003987class HInvokePolymorphic FINAL : public HInvoke {
3988 public:
3989 HInvokePolymorphic(ArenaAllocator* arena,
3990 uint32_t number_of_arguments,
3991 Primitive::Type return_type,
3992 uint32_t dex_pc,
3993 uint32_t dex_method_index)
3994 : HInvoke(arena,
3995 number_of_arguments,
3996 0u /* number_of_other_inputs */,
3997 return_type,
3998 dex_pc,
3999 dex_method_index,
4000 nullptr,
4001 kVirtual) {}
4002
4003 DECLARE_INSTRUCTION(InvokePolymorphic);
4004
4005 private:
4006 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4007};
4008
Vladimir Markofcb503c2016-05-18 12:48:17 +01004009class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004010 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004011 // Requirements of this method call regarding the class
4012 // initialization (clinit) check of its declaring class.
4013 enum class ClinitCheckRequirement {
4014 kNone, // Class already initialized.
4015 kExplicit, // Static call having explicit clinit check as last input.
4016 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004017 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004018 };
4019
Vladimir Marko58155012015-08-19 12:49:41 +00004020 // Determines how to load the target ArtMethod*.
4021 enum class MethodLoadKind {
4022 // Use a String init ArtMethod* loaded from Thread entrypoints.
4023 kStringInit,
4024
4025 // Use the method's own ArtMethod* loaded by the register allocator.
4026 kRecursive,
4027
4028 // Use ArtMethod* at a known address, embed the direct address in the code.
4029 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4030 kDirectAddress,
4031
Vladimir Markoa1de9182016-02-25 11:37:38 +00004032 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004033 // Used when we need to use the dex cache, for example for invoke-static that
4034 // may cause class initialization (the entry may point to a resolution method),
4035 // and we know that we can access the dex cache arrays using a PC-relative load.
4036 kDexCachePcRelative,
4037
4038 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4039 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4040 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4041 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4042 kDexCacheViaMethod,
4043 };
4044
4045 // Determines the location of the code pointer.
4046 enum class CodePtrLocation {
4047 // Recursive call, use local PC-relative call instruction.
4048 kCallSelf,
4049
Vladimir Marko58155012015-08-19 12:49:41 +00004050 // Use code pointer from the ArtMethod*.
4051 // Used when we don't know the target code. This is also the last-resort-kind used when
4052 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4053 kCallArtMethod,
4054 };
4055
4056 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004057 MethodLoadKind method_load_kind;
4058 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004059 // The method load data holds
4060 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4061 // Note that there are multiple string init methods, each having its own offset.
4062 // - the method address for kDirectAddress
4063 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004064 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004065 };
4066
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004067 HInvokeStaticOrDirect(ArenaAllocator* arena,
4068 uint32_t number_of_arguments,
4069 Primitive::Type return_type,
4070 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004071 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004072 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004073 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004074 InvokeType invoke_type,
4075 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004076 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004077 : HInvoke(arena,
4078 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004079 // There is potentially one extra argument for the HCurrentMethod node, and
4080 // potentially one other if the clinit check is explicit, and potentially
4081 // one other if the method is a string factory.
4082 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004083 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004084 return_type,
4085 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004086 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004087 resolved_method,
4088 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004089 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004090 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004091 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4092 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004093
Vladimir Markodc151b22015-10-15 18:02:30 +01004094 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004095 bool had_current_method_input = HasCurrentMethodInput();
4096 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4097
4098 // Using the current method is the default and once we find a better
4099 // method load kind, we should not go back to using the current method.
4100 DCHECK(had_current_method_input || !needs_current_method_input);
4101
4102 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004103 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4104 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004105 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004106 dispatch_info_ = dispatch_info;
4107 }
4108
Vladimir Markoc53c0792015-11-19 15:48:33 +00004109 void AddSpecialInput(HInstruction* input) {
4110 // We allow only one special input.
4111 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4112 DCHECK(InputCount() == GetSpecialInputIndex() ||
4113 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4114 InsertInputAt(GetSpecialInputIndex(), input);
4115 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004116
Vladimir Marko372f10e2016-05-17 16:30:10 +01004117 using HInstruction::GetInputRecords; // Keep the const version visible.
4118 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4119 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4120 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4121 DCHECK(!input_records.empty());
4122 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4123 HInstruction* last_input = input_records.back().GetInstruction();
4124 // Note: `last_input` may be null during arguments setup.
4125 if (last_input != nullptr) {
4126 // `last_input` is the last input of a static invoke marked as having
4127 // an explicit clinit check. It must either be:
4128 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4129 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4130 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4131 }
4132 }
4133 return input_records;
4134 }
4135
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004136 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004137 // We access the method via the dex cache so we can't do an implicit null check.
4138 // TODO: for intrinsics we can generate implicit null checks.
4139 return false;
4140 }
4141
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004142 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004143 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004144 }
4145
Vladimir Markoc53c0792015-11-19 15:48:33 +00004146 // Get the index of the special input, if any.
4147 //
David Brazdil6de19382016-01-08 17:37:10 +00004148 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4149 // method pointer; otherwise there may be one platform-specific special input,
4150 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004151 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004152 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004153
Vladimir Marko58155012015-08-19 12:49:41 +00004154 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4155 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4156 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004157 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004158 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004159 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004160 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004161 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4162 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004163 bool HasCurrentMethodInput() const {
4164 // This function can be called only after the invoke has been fully initialized by the builder.
4165 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004166 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004167 return true;
4168 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004169 DCHECK(InputCount() == GetSpecialInputIndex() ||
4170 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004171 return false;
4172 }
4173 }
Vladimir Marko58155012015-08-19 12:49:41 +00004174
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004175 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004176 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004177 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004178 }
4179
4180 uint64_t GetMethodAddress() const {
4181 DCHECK(HasMethodAddress());
4182 return dispatch_info_.method_load_data;
4183 }
4184
4185 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004186 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004187 return dispatch_info_.method_load_data;
4188 }
4189
Vladimir Markoa1de9182016-02-25 11:37:38 +00004190 ClinitCheckRequirement GetClinitCheckRequirement() const {
4191 return GetPackedField<ClinitCheckRequirementField>();
4192 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004193
Roland Levillain4c0eb422015-04-24 16:43:49 +01004194 // Is this instruction a call to a static method?
4195 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004196 return GetInvokeType() == kStatic;
4197 }
4198
4199 MethodReference GetTargetMethod() const {
4200 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004201 }
4202
Vladimir Markofbb184a2015-11-13 14:47:00 +00004203 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4204 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4205 // instruction; only relevant for static calls with explicit clinit check.
4206 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004207 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004208 size_t last_input_index = inputs_.size() - 1u;
4209 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004210 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004211 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004212 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004213 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004214 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004215 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004216 }
4217
4218 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004219 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004220 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004221 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004222 }
4223
4224 // Is this a call to a static method whose declaring class has an
4225 // implicit intialization check requirement?
4226 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004227 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004228 }
4229
Vladimir Markob554b5a2015-11-06 12:57:55 +00004230 // Does this method load kind need the current method as an input?
4231 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4232 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4233 }
4234
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004235 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004236
4237 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004238 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004239 static constexpr size_t kFieldClinitCheckRequirementSize =
4240 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4241 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4242 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4243 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4244 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004245 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4246 kFieldClinitCheckRequirement,
4247 kFieldClinitCheckRequirementSize>;
4248
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004249 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004250 MethodReference target_method_;
4251 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004252
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004253 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004254};
Vladimir Markof64242a2015-12-01 14:58:23 +00004255std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004256std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004257
Vladimir Markofcb503c2016-05-18 12:48:17 +01004258class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004259 public:
4260 HInvokeVirtual(ArenaAllocator* arena,
4261 uint32_t number_of_arguments,
4262 Primitive::Type return_type,
4263 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004264 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004265 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004266 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004267 : HInvoke(arena,
4268 number_of_arguments,
4269 0u,
4270 return_type,
4271 dex_pc,
4272 dex_method_index,
4273 resolved_method,
4274 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004275 vtable_index_(vtable_index) {}
4276
Aart Bik71bf7b42016-11-16 10:17:46 -08004277 bool CanBeNull() const OVERRIDE {
4278 switch (GetIntrinsic()) {
4279 case Intrinsics::kThreadCurrentThread:
4280 case Intrinsics::kStringBufferAppend:
4281 case Intrinsics::kStringBufferToString:
4282 case Intrinsics::kStringBuilderAppend:
4283 case Intrinsics::kStringBuilderToString:
4284 return false;
4285 default:
4286 return HInvoke::CanBeNull();
4287 }
4288 }
4289
Calin Juravle641547a2015-04-21 22:08:51 +01004290 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004291 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004292 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004293 }
4294
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004295 uint32_t GetVTableIndex() const { return vtable_index_; }
4296
4297 DECLARE_INSTRUCTION(InvokeVirtual);
4298
4299 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004300 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004301 const uint32_t vtable_index_;
4302
4303 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4304};
4305
Vladimir Markofcb503c2016-05-18 12:48:17 +01004306class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004307 public:
4308 HInvokeInterface(ArenaAllocator* arena,
4309 uint32_t number_of_arguments,
4310 Primitive::Type return_type,
4311 uint32_t dex_pc,
4312 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004313 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004314 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004315 : HInvoke(arena,
4316 number_of_arguments,
4317 0u,
4318 return_type,
4319 dex_pc,
4320 dex_method_index,
4321 resolved_method,
4322 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004323 imt_index_(imt_index) {}
4324
Calin Juravle641547a2015-04-21 22:08:51 +01004325 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004326 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004327 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004328 }
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 Geoffraycb1b00a2015-01-28 14:50:01 +00004372 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004373 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004374 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004375 dex::TypeIndex type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004376 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004377 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004378 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004379 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004380 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004381 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004382 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004383 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004384 }
4385
Andreas Gampea5b09a62016-11-17 15:21:22 -08004386 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004387 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004388
4389 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004390 bool NeedsEnvironment() const OVERRIDE { return true; }
4391
Mingyao Yang0c365e62015-03-31 15:09:29 -07004392 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4393 bool CanThrow() const OVERRIDE { return true; }
4394
Calin Juravle10e244f2015-01-26 18:54:32 +00004395 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004396
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004397 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4398
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004399 DECLARE_INSTRUCTION(NewArray);
4400
4401 private:
Andreas Gampea5b09a62016-11-17 15:21:22 -08004402 const dex::TypeIndex type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004403 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004404 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004405
4406 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4407};
4408
Vladimir Markofcb503c2016-05-18 12:48:17 +01004409class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004410 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004411 HAdd(Primitive::Type result_type,
4412 HInstruction* left,
4413 HInstruction* right,
4414 uint32_t dex_pc = kNoDexPc)
4415 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004416
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004417 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004418
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004419 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004420
4421 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004422 return GetBlock()->GetGraph()->GetIntConstant(
4423 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004424 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004425 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004426 return GetBlock()->GetGraph()->GetLongConstant(
4427 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004428 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004429 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4430 return GetBlock()->GetGraph()->GetFloatConstant(
4431 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4432 }
4433 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4434 return GetBlock()->GetGraph()->GetDoubleConstant(
4435 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4436 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004437
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004438 DECLARE_INSTRUCTION(Add);
4439
4440 private:
4441 DISALLOW_COPY_AND_ASSIGN(HAdd);
4442};
4443
Vladimir Markofcb503c2016-05-18 12:48:17 +01004444class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004445 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004446 HSub(Primitive::Type result_type,
4447 HInstruction* left,
4448 HInstruction* right,
4449 uint32_t dex_pc = kNoDexPc)
4450 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004451
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004452 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004453
4454 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004455 return GetBlock()->GetGraph()->GetIntConstant(
4456 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004457 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004458 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004459 return GetBlock()->GetGraph()->GetLongConstant(
4460 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004461 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004462 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4463 return GetBlock()->GetGraph()->GetFloatConstant(
4464 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4465 }
4466 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4467 return GetBlock()->GetGraph()->GetDoubleConstant(
4468 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4469 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004470
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004471 DECLARE_INSTRUCTION(Sub);
4472
4473 private:
4474 DISALLOW_COPY_AND_ASSIGN(HSub);
4475};
4476
Vladimir Markofcb503c2016-05-18 12:48:17 +01004477class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004478 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004479 HMul(Primitive::Type result_type,
4480 HInstruction* left,
4481 HInstruction* right,
4482 uint32_t dex_pc = kNoDexPc)
4483 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004484
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004485 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004486
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004487 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004488
4489 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004490 return GetBlock()->GetGraph()->GetIntConstant(
4491 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004492 }
4493 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004494 return GetBlock()->GetGraph()->GetLongConstant(
4495 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004496 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004497 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4498 return GetBlock()->GetGraph()->GetFloatConstant(
4499 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4500 }
4501 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4502 return GetBlock()->GetGraph()->GetDoubleConstant(
4503 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4504 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004505
4506 DECLARE_INSTRUCTION(Mul);
4507
4508 private:
4509 DISALLOW_COPY_AND_ASSIGN(HMul);
4510};
4511
Vladimir Markofcb503c2016-05-18 12:48:17 +01004512class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004513 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004514 HDiv(Primitive::Type result_type,
4515 HInstruction* left,
4516 HInstruction* right,
4517 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004518 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004519
Roland Levillain9867bc72015-08-05 10:21:34 +01004520 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004521 T ComputeIntegral(T x, T y) const {
4522 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004523 // Our graph structure ensures we never have 0 for `y` during
4524 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004525 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004526 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004527 return (y == -1) ? -x : x / y;
4528 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004529
Roland Levillain31dd3d62016-02-16 12:21:02 +00004530 template <typename T>
4531 T ComputeFP(T x, T y) const {
4532 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4533 return x / y;
4534 }
4535
Roland Levillain9867bc72015-08-05 10:21:34 +01004536 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004537 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004538 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004539 }
4540 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004541 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004542 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4543 }
4544 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4545 return GetBlock()->GetGraph()->GetFloatConstant(
4546 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4547 }
4548 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4549 return GetBlock()->GetGraph()->GetDoubleConstant(
4550 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004551 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004552
4553 DECLARE_INSTRUCTION(Div);
4554
4555 private:
4556 DISALLOW_COPY_AND_ASSIGN(HDiv);
4557};
4558
Vladimir Markofcb503c2016-05-18 12:48:17 +01004559class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004560 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004561 HRem(Primitive::Type result_type,
4562 HInstruction* left,
4563 HInstruction* right,
4564 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004565 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004566
Roland Levillain9867bc72015-08-05 10:21:34 +01004567 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004568 T ComputeIntegral(T x, T y) const {
4569 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004570 // Our graph structure ensures we never have 0 for `y` during
4571 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004572 DCHECK_NE(y, 0);
4573 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4574 return (y == -1) ? 0 : x % y;
4575 }
4576
Roland Levillain31dd3d62016-02-16 12:21:02 +00004577 template <typename T>
4578 T ComputeFP(T x, T y) const {
4579 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4580 return std::fmod(x, y);
4581 }
4582
Roland Levillain9867bc72015-08-05 10:21:34 +01004583 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004584 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004585 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004586 }
4587 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004588 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004589 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004590 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004591 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4592 return GetBlock()->GetGraph()->GetFloatConstant(
4593 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4594 }
4595 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4596 return GetBlock()->GetGraph()->GetDoubleConstant(
4597 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4598 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004599
4600 DECLARE_INSTRUCTION(Rem);
4601
4602 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004603 DISALLOW_COPY_AND_ASSIGN(HRem);
4604};
4605
Vladimir Markofcb503c2016-05-18 12:48:17 +01004606class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004607 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004608 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4609 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004610 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004611 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004612 SetRawInputAt(0, value);
4613 }
4614
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004615 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4616
Calin Juravled0d48522014-11-04 16:40:20 +00004617 bool CanBeMoved() const OVERRIDE { return true; }
4618
Vladimir Marko372f10e2016-05-17 16:30:10 +01004619 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004620 return true;
4621 }
4622
4623 bool NeedsEnvironment() const OVERRIDE { return true; }
4624 bool CanThrow() const OVERRIDE { return true; }
4625
Calin Juravled0d48522014-11-04 16:40:20 +00004626 DECLARE_INSTRUCTION(DivZeroCheck);
4627
4628 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004629 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4630};
4631
Vladimir Markofcb503c2016-05-18 12:48:17 +01004632class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004633 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004634 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004635 HInstruction* value,
4636 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004637 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004638 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4639 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4640 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004641 }
4642
Roland Levillain5b5b9312016-03-22 14:57:31 +00004643 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004644 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004645 return value << (distance & max_shift_distance);
4646 }
4647
4648 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004649 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004650 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004651 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004652 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004653 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004654 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004655 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004656 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4657 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4658 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4659 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004660 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004661 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4662 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004663 LOG(FATAL) << DebugName() << " is not defined for float values";
4664 UNREACHABLE();
4665 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004666 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4667 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004668 LOG(FATAL) << DebugName() << " is not defined for double values";
4669 UNREACHABLE();
4670 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004671
4672 DECLARE_INSTRUCTION(Shl);
4673
4674 private:
4675 DISALLOW_COPY_AND_ASSIGN(HShl);
4676};
4677
Vladimir Markofcb503c2016-05-18 12:48:17 +01004678class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004679 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004680 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004681 HInstruction* value,
4682 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004683 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004684 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4685 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4686 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004687 }
4688
Roland Levillain5b5b9312016-03-22 14:57:31 +00004689 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004690 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004691 return value >> (distance & max_shift_distance);
4692 }
4693
4694 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004695 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004696 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004697 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004698 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004699 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004700 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004701 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004702 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4703 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4704 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4705 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004706 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004707 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4708 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004709 LOG(FATAL) << DebugName() << " is not defined for float values";
4710 UNREACHABLE();
4711 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004712 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4713 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004714 LOG(FATAL) << DebugName() << " is not defined for double values";
4715 UNREACHABLE();
4716 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004717
4718 DECLARE_INSTRUCTION(Shr);
4719
4720 private:
4721 DISALLOW_COPY_AND_ASSIGN(HShr);
4722};
4723
Vladimir Markofcb503c2016-05-18 12:48:17 +01004724class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004725 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004726 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004727 HInstruction* value,
4728 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004729 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004730 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4731 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4732 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004733 }
4734
Roland Levillain5b5b9312016-03-22 14:57:31 +00004735 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004736 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004737 typedef typename std::make_unsigned<T>::type V;
4738 V ux = static_cast<V>(value);
4739 return static_cast<T>(ux >> (distance & max_shift_distance));
4740 }
4741
4742 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004743 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004744 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004745 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004746 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004747 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004748 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004749 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004750 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4751 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4752 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4753 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004754 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004755 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4756 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004757 LOG(FATAL) << DebugName() << " is not defined for float values";
4758 UNREACHABLE();
4759 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004760 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4761 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004762 LOG(FATAL) << DebugName() << " is not defined for double values";
4763 UNREACHABLE();
4764 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004765
4766 DECLARE_INSTRUCTION(UShr);
4767
4768 private:
4769 DISALLOW_COPY_AND_ASSIGN(HUShr);
4770};
4771
Vladimir Markofcb503c2016-05-18 12:48:17 +01004772class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004773 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004774 HAnd(Primitive::Type result_type,
4775 HInstruction* left,
4776 HInstruction* right,
4777 uint32_t dex_pc = kNoDexPc)
4778 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004779
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004780 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004781
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004782 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004783
4784 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004785 return GetBlock()->GetGraph()->GetIntConstant(
4786 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004787 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004788 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004789 return GetBlock()->GetGraph()->GetLongConstant(
4790 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004791 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004792 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4793 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4794 LOG(FATAL) << DebugName() << " is not defined for float values";
4795 UNREACHABLE();
4796 }
4797 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4798 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4799 LOG(FATAL) << DebugName() << " is not defined for double values";
4800 UNREACHABLE();
4801 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004802
4803 DECLARE_INSTRUCTION(And);
4804
4805 private:
4806 DISALLOW_COPY_AND_ASSIGN(HAnd);
4807};
4808
Vladimir Markofcb503c2016-05-18 12:48:17 +01004809class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004810 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004811 HOr(Primitive::Type result_type,
4812 HInstruction* left,
4813 HInstruction* right,
4814 uint32_t dex_pc = kNoDexPc)
4815 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004816
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004817 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004818
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004819 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004820
4821 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004822 return GetBlock()->GetGraph()->GetIntConstant(
4823 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004824 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004825 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004826 return GetBlock()->GetGraph()->GetLongConstant(
4827 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004828 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004829 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4830 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4831 LOG(FATAL) << DebugName() << " is not defined for float values";
4832 UNREACHABLE();
4833 }
4834 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4835 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4836 LOG(FATAL) << DebugName() << " is not defined for double values";
4837 UNREACHABLE();
4838 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004839
4840 DECLARE_INSTRUCTION(Or);
4841
4842 private:
4843 DISALLOW_COPY_AND_ASSIGN(HOr);
4844};
4845
Vladimir Markofcb503c2016-05-18 12:48:17 +01004846class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004847 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004848 HXor(Primitive::Type result_type,
4849 HInstruction* left,
4850 HInstruction* right,
4851 uint32_t dex_pc = kNoDexPc)
4852 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004853
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004854 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004855
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004856 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004857
4858 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004859 return GetBlock()->GetGraph()->GetIntConstant(
4860 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004861 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004862 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004863 return GetBlock()->GetGraph()->GetLongConstant(
4864 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004865 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004866 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4867 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4868 LOG(FATAL) << DebugName() << " is not defined for float values";
4869 UNREACHABLE();
4870 }
4871 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4872 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4873 LOG(FATAL) << DebugName() << " is not defined for double values";
4874 UNREACHABLE();
4875 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004876
4877 DECLARE_INSTRUCTION(Xor);
4878
4879 private:
4880 DISALLOW_COPY_AND_ASSIGN(HXor);
4881};
4882
Vladimir Markofcb503c2016-05-18 12:48:17 +01004883class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004884 public:
4885 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004886 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004887 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4888 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004889 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004890
Roland Levillain5b5b9312016-03-22 14:57:31 +00004891 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004892 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004893 typedef typename std::make_unsigned<T>::type V;
4894 V ux = static_cast<V>(value);
4895 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004896 return static_cast<T>(ux);
4897 } else {
4898 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004899 return static_cast<T>(ux >> (distance & max_shift_value)) |
4900 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004901 }
4902 }
4903
Roland Levillain5b5b9312016-03-22 14:57:31 +00004904 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004905 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004906 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004907 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004908 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004909 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004910 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004911 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004912 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4913 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4914 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4915 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004916 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004917 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4918 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004919 LOG(FATAL) << DebugName() << " is not defined for float values";
4920 UNREACHABLE();
4921 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004922 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4923 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004924 LOG(FATAL) << DebugName() << " is not defined for double values";
4925 UNREACHABLE();
4926 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004927
4928 DECLARE_INSTRUCTION(Ror);
4929
4930 private:
4931 DISALLOW_COPY_AND_ASSIGN(HRor);
4932};
4933
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004934// The value of a parameter in this method. Its location depends on
4935// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004936class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004937 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004938 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004939 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004940 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004941 Primitive::Type parameter_type,
4942 bool is_this = false)
4943 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004944 dex_file_(dex_file),
4945 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004946 index_(index) {
4947 SetPackedFlag<kFlagIsThis>(is_this);
4948 SetPackedFlag<kFlagCanBeNull>(!is_this);
4949 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004950
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004951 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08004952 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004953 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004954 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004955
Vladimir Markoa1de9182016-02-25 11:37:38 +00004956 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4957 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004958
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004959 DECLARE_INSTRUCTION(ParameterValue);
4960
4961 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004962 // Whether or not the parameter value corresponds to 'this' argument.
4963 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4964 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4965 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4966 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4967 "Too many packed fields.");
4968
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004969 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004970 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004971 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004972 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004973 const uint8_t index_;
4974
4975 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4976};
4977
Vladimir Markofcb503c2016-05-18 12:48:17 +01004978class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004979 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004980 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4981 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004982
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004983 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004984 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004985 return true;
4986 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004987
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004988 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004989
4990 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004991 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004992 }
4993 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004994 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004995 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004996 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4997 LOG(FATAL) << DebugName() << " is not defined for float values";
4998 UNREACHABLE();
4999 }
5000 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5001 LOG(FATAL) << DebugName() << " is not defined for double values";
5002 UNREACHABLE();
5003 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005004
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005005 DECLARE_INSTRUCTION(Not);
5006
5007 private:
5008 DISALLOW_COPY_AND_ASSIGN(HNot);
5009};
5010
Vladimir Markofcb503c2016-05-18 12:48:17 +01005011class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005012 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005013 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5014 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005015
5016 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005017 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005018 return true;
5019 }
5020
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005021 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005022 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005023 return !x;
5024 }
5025
Roland Levillain9867bc72015-08-05 10:21:34 +01005026 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005027 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005028 }
5029 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5030 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005031 UNREACHABLE();
5032 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005033 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5034 LOG(FATAL) << DebugName() << " is not defined for float values";
5035 UNREACHABLE();
5036 }
5037 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5038 LOG(FATAL) << DebugName() << " is not defined for double values";
5039 UNREACHABLE();
5040 }
David Brazdil66d126e2015-04-03 16:02:44 +01005041
5042 DECLARE_INSTRUCTION(BooleanNot);
5043
5044 private:
5045 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5046};
5047
Vladimir Markofcb503c2016-05-18 12:48:17 +01005048class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005049 public:
5050 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005051 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005052 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005053 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005054 // Invariant: We should never generate a conversion to a Boolean value.
5055 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005056 }
5057
5058 HInstruction* GetInput() const { return InputAt(0); }
5059 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5060 Primitive::Type GetResultType() const { return GetType(); }
5061
5062 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005063 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5064 return true;
5065 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005066
Mark Mendelle82549b2015-05-06 10:55:34 -04005067 // Try to statically evaluate the conversion and return a HConstant
5068 // containing the result. If the input cannot be converted, return nullptr.
5069 HConstant* TryStaticEvaluation() const;
5070
Roland Levillaindff1f282014-11-05 14:15:05 +00005071 DECLARE_INSTRUCTION(TypeConversion);
5072
5073 private:
5074 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5075};
5076
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005077static constexpr uint32_t kNoRegNumber = -1;
5078
Vladimir Markofcb503c2016-05-18 12:48:17 +01005079class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005080 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005081 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5082 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005083 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005084 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005085 SetRawInputAt(0, value);
5086 }
5087
Calin Juravle10e244f2015-01-26 18:54:32 +00005088 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005089 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005090 return true;
5091 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005092
Calin Juravle10e244f2015-01-26 18:54:32 +00005093 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005094
Calin Juravle10e244f2015-01-26 18:54:32 +00005095 bool CanThrow() const OVERRIDE { return true; }
5096
5097 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005098
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005099
5100 DECLARE_INSTRUCTION(NullCheck);
5101
5102 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005103 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5104};
5105
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005106// Embeds an ArtField and all the information required by the compiler. We cache
5107// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005108class FieldInfo : public ValueObject {
5109 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005110 FieldInfo(ArtField* field,
5111 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005112 Primitive::Type field_type,
5113 bool is_volatile,
5114 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005115 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005116 const DexFile& dex_file)
5117 : field_(field),
5118 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005119 field_type_(field_type),
5120 is_volatile_(is_volatile),
5121 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005122 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005123 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005124
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005125 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005126 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005127 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005128 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005129 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005130 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005131 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005132
5133 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005134 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005135 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005136 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005137 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005138 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005139 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005140 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005141};
5142
Vladimir Markofcb503c2016-05-18 12:48:17 +01005143class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005144 public:
5145 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005146 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005147 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005148 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005149 bool is_volatile,
5150 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005151 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005152 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005153 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005154 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005155 field_info_(field,
5156 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005157 field_type,
5158 is_volatile,
5159 field_idx,
5160 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005161 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005162 SetRawInputAt(0, value);
5163 }
5164
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005165 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005166
Vladimir Marko372f10e2016-05-17 16:30:10 +01005167 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5168 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005169 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005170 }
5171
Calin Juravle641547a2015-04-21 22:08:51 +01005172 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005173 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005174 }
5175
5176 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005177 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5178 }
5179
Calin Juravle52c48962014-12-16 17:02:57 +00005180 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005181 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005182 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005183 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005184
5185 DECLARE_INSTRUCTION(InstanceFieldGet);
5186
5187 private:
5188 const FieldInfo field_info_;
5189
5190 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5191};
5192
Vladimir Markofcb503c2016-05-18 12:48:17 +01005193class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005194 public:
5195 HInstanceFieldSet(HInstruction* object,
5196 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005197 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005198 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005199 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005200 bool is_volatile,
5201 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005202 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005203 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005204 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005205 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005206 field_info_(field,
5207 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005208 field_type,
5209 is_volatile,
5210 field_idx,
5211 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005212 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005213 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005214 SetRawInputAt(0, object);
5215 SetRawInputAt(1, value);
5216 }
5217
Calin Juravle641547a2015-04-21 22:08:51 +01005218 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005219 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005220 }
5221
Calin Juravle52c48962014-12-16 17:02:57 +00005222 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005223 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005224 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005225 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005226 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005227 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5228 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005229
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005230 DECLARE_INSTRUCTION(InstanceFieldSet);
5231
5232 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005233 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5234 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5235 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5236 "Too many packed fields.");
5237
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005238 const FieldInfo field_info_;
5239
5240 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5241};
5242
Vladimir Markofcb503c2016-05-18 12:48:17 +01005243class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005244 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005245 HArrayGet(HInstruction* array,
5246 HInstruction* index,
5247 Primitive::Type type,
5248 uint32_t dex_pc,
5249 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005250 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005251 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005252 SetRawInputAt(0, array);
5253 SetRawInputAt(1, index);
5254 }
5255
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005256 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005257 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005258 return true;
5259 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005260 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005261 // TODO: We can be smarter here.
5262 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5263 // which generates the implicit null check. There are cases when these can be removed
5264 // to produce better code. If we ever add optimizations to do so we should allow an
5265 // implicit check here (as long as the address falls in the first page).
5266 return false;
5267 }
5268
David Brazdil4833f5a2015-12-16 10:37:39 +00005269 bool IsEquivalentOf(HArrayGet* other) const {
5270 bool result = (GetDexPc() == other->GetDexPc());
5271 if (kIsDebugBuild && result) {
5272 DCHECK_EQ(GetBlock(), other->GetBlock());
5273 DCHECK_EQ(GetArray(), other->GetArray());
5274 DCHECK_EQ(GetIndex(), other->GetIndex());
5275 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005276 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005277 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005278 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5279 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005280 }
5281 }
5282 return result;
5283 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005284
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005285 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5286
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005287 HInstruction* GetArray() const { return InputAt(0); }
5288 HInstruction* GetIndex() const { return InputAt(1); }
5289
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005290 DECLARE_INSTRUCTION(ArrayGet);
5291
5292 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005293 // We treat a String as an array, creating the HArrayGet from String.charAt()
5294 // intrinsic in the instruction simplifier. We can always determine whether
5295 // a particular HArrayGet is actually a String.charAt() by looking at the type
5296 // of the input but that requires holding the mutator lock, so we prefer to use
5297 // a flag, so that code generators don't need to do the locking.
5298 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5299 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5300 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5301 "Too many packed fields.");
5302
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005303 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5304};
5305
Vladimir Markofcb503c2016-05-18 12:48:17 +01005306class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005307 public:
5308 HArraySet(HInstruction* array,
5309 HInstruction* index,
5310 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005311 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005312 uint32_t dex_pc)
5313 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005314 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5315 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5316 SetPackedFlag<kFlagValueCanBeNull>(true);
5317 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005318 SetRawInputAt(0, array);
5319 SetRawInputAt(1, index);
5320 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005321 // Make a best guess now, may be refined during SSA building.
5322 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005323 }
5324
Calin Juravle77520bc2015-01-12 18:45:46 +00005325 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005326 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005327 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005328 }
5329
Mingyao Yang81014cb2015-06-02 03:16:27 -07005330 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005331 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005332
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005333 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005334 // TODO: Same as for ArrayGet.
5335 return false;
5336 }
5337
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005338 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005339 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005340 }
5341
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005342 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005343 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005344 }
5345
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005346 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005347 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005348 }
5349
Vladimir Markoa1de9182016-02-25 11:37:38 +00005350 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5351 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5352 bool StaticTypeOfArrayIsObjectArray() const {
5353 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5354 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005355
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005356 HInstruction* GetArray() const { return InputAt(0); }
5357 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005358 HInstruction* GetValue() const { return InputAt(2); }
5359
5360 Primitive::Type GetComponentType() const {
5361 // The Dex format does not type floating point index operations. Since the
5362 // `expected_component_type_` is set during building and can therefore not
5363 // be correct, we also check what is the value type. If it is a floating
5364 // point type, we must use that type.
5365 Primitive::Type value_type = GetValue()->GetType();
5366 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5367 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005368 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005369 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005370
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005371 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005372 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005373 }
5374
Aart Bik18b36ab2016-04-13 16:41:35 -07005375 void ComputeSideEffects() {
5376 Primitive::Type type = GetComponentType();
5377 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5378 SideEffectsForArchRuntimeCalls(type)));
5379 }
5380
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005381 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5382 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5383 }
5384
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005385 DECLARE_INSTRUCTION(ArraySet);
5386
5387 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005388 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5389 static constexpr size_t kFieldExpectedComponentTypeSize =
5390 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5391 static constexpr size_t kFlagNeedsTypeCheck =
5392 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5393 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005394 // Cached information for the reference_type_info_ so that codegen
5395 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005396 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5397 static constexpr size_t kNumberOfArraySetPackedBits =
5398 kFlagStaticTypeOfArrayIsObjectArray + 1;
5399 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5400 using ExpectedComponentTypeField =
5401 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005402
5403 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5404};
5405
Vladimir Markofcb503c2016-05-18 12:48:17 +01005406class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005407 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005408 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005409 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005410 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005411 // Note that arrays do not change length, so the instruction does not
5412 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005413 SetRawInputAt(0, array);
5414 }
5415
Calin Juravle77520bc2015-01-12 18:45:46 +00005416 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005417 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005418 return true;
5419 }
Calin Juravle641547a2015-04-21 22:08:51 +01005420 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5421 return obj == InputAt(0);
5422 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005423
Vladimir Markodce016e2016-04-28 13:10:02 +01005424 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5425
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005426 DECLARE_INSTRUCTION(ArrayLength);
5427
5428 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005429 // We treat a String as an array, creating the HArrayLength from String.length()
5430 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5431 // determine whether a particular HArrayLength is actually a String.length() by
5432 // looking at the type of the input but that requires holding the mutator lock, so
5433 // we prefer to use a flag, so that code generators don't need to do the locking.
5434 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5435 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5436 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5437 "Too many packed fields.");
5438
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005439 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5440};
5441
Vladimir Markofcb503c2016-05-18 12:48:17 +01005442class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005443 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005444 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5445 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005446 HBoundsCheck(HInstruction* index,
5447 HInstruction* length,
5448 uint32_t dex_pc,
5449 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5450 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5451 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005452 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005453 SetRawInputAt(0, index);
5454 SetRawInputAt(1, length);
5455 }
5456
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005457 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005458 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005459 return true;
5460 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005461
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005462 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005463
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005464 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005465
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005466 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5467 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5468
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005469 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005470
5471 DECLARE_INSTRUCTION(BoundsCheck);
5472
5473 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005474 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5475 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5476 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5477 // so we need to create an HEnvironment which will be translated to an InlineInfo
5478 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5479 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5480 // from the invoke as we don't want to look again at the dex bytecode.
5481 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5482
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005483 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5484};
5485
Vladimir Markofcb503c2016-05-18 12:48:17 +01005486class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005487 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005488 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005489 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005490
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005491 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005492 return true;
5493 }
5494
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005495 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5496 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005497
5498 DECLARE_INSTRUCTION(SuspendCheck);
5499
5500 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005501 // Only used for code generation, in order to share the same slow path between back edges
5502 // of a same loop.
5503 SlowPathCode* slow_path_;
5504
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005505 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5506};
5507
David Srbecky0cf44932015-12-09 14:09:59 +00005508// Pseudo-instruction which provides the native debugger with mapping information.
5509// It ensures that we can generate line number and local variables at this point.
5510class HNativeDebugInfo : public HTemplateInstruction<0> {
5511 public:
5512 explicit HNativeDebugInfo(uint32_t dex_pc)
5513 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5514
5515 bool NeedsEnvironment() const OVERRIDE {
5516 return true;
5517 }
5518
5519 DECLARE_INSTRUCTION(NativeDebugInfo);
5520
5521 private:
5522 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5523};
5524
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005525/**
5526 * Instruction to load a Class object.
5527 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005528class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005529 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005530 // Determines how to load the Class.
5531 enum class LoadKind {
5532 // Use the Class* from the method's own ArtMethod*.
5533 kReferrersClass,
5534
5535 // Use boot image Class* address that will be known at link time.
5536 // Used for boot image classes referenced by boot image code in non-PIC mode.
5537 kBootImageLinkTimeAddress,
5538
5539 // Use PC-relative boot image Class* address that will be known at link time.
5540 // Used for boot image classes referenced by boot image code in PIC mode.
5541 kBootImageLinkTimePcRelative,
5542
5543 // Use a known boot image Class* address, embedded in the code by the codegen.
5544 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5545 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5546 // GetIncludePatchInformation().
5547 kBootImageAddress,
5548
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005549 // Load from the root table associated with the JIT compiled method.
5550 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005551
5552 // Load from resolved types array in the dex cache using a PC-relative load.
5553 // Used for classes outside boot image when we know that we can access
5554 // the dex cache arrays using a PC-relative load.
5555 kDexCachePcRelative,
5556
5557 // Load from resolved types array accessed through the class loaded from
5558 // the compiled method's own ArtMethod*. This is the default access type when
5559 // all other types are unavailable.
5560 kDexCacheViaMethod,
5561
5562 kLast = kDexCacheViaMethod
5563 };
5564
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005565 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005566 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005567 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005568 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005569 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005570 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005571 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5572 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005573 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005574 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005575 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005576 // Referrers class should not need access check. We never inline unverified
5577 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005578 DCHECK(!is_referrers_class || !needs_access_check);
5579
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005580 SetPackedField<LoadKindField>(
5581 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005582 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005583 SetPackedFlag<kFlagIsInDexCache>(false);
5584 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005585 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005586 }
5587
5588 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5589 DCHECK(HasAddress(load_kind));
5590 load_data_.address = address;
5591 SetLoadKindInternal(load_kind);
5592 }
5593
5594 void SetLoadKindWithTypeReference(LoadKind load_kind,
5595 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005596 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005597 DCHECK(HasTypeReference(load_kind));
5598 DCHECK(IsSameDexFile(dex_file_, dex_file));
5599 DCHECK_EQ(type_index_, type_index);
5600 SetLoadKindInternal(load_kind);
5601 }
5602
5603 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5604 const DexFile& dex_file,
5605 uint32_t element_index) {
5606 DCHECK(HasDexCacheReference(load_kind));
5607 DCHECK(IsSameDexFile(dex_file_, dex_file));
5608 load_data_.dex_cache_element_index = element_index;
5609 SetLoadKindInternal(load_kind);
5610 }
5611
5612 LoadKind GetLoadKind() const {
5613 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005614 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005615
5616 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005617
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005618 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005619
Andreas Gampea5b09a62016-11-17 15:21:22 -08005620 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005621
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005622 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005623
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005624 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005625 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005626 }
5627
Calin Juravle0ba218d2015-05-19 18:46:01 +01005628 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005629 // The entrypoint the code generator is going to call does not do
5630 // clinit of the class.
5631 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005632 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005633 }
5634
5635 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005636 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005637 (!IsReferrersClass() && !IsInDexCache()) ||
5638 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005639 }
5640
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005641 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005642 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005643 }
5644
Calin Juravleacf735c2015-02-12 15:25:22 +00005645 ReferenceTypeInfo GetLoadedClassRTI() {
5646 return loaded_class_rti_;
5647 }
5648
5649 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5650 // Make sure we only set exact types (the loaded class should never be merged).
5651 DCHECK(rti.IsExact());
5652 loaded_class_rti_ = rti;
5653 }
5654
Andreas Gampea5b09a62016-11-17 15:21:22 -08005655 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005656 const DexFile& GetDexFile() const { return dex_file_; }
5657
5658 uint32_t GetDexCacheElementOffset() const;
5659
5660 uint64_t GetAddress() const {
5661 DCHECK(HasAddress(GetLoadKind()));
5662 return load_data_.address;
5663 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005664
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005665 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5666 return !IsReferrersClass();
5667 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005668
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005669 static SideEffects SideEffectsForArchRuntimeCalls() {
5670 return SideEffects::CanTriggerGC();
5671 }
5672
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005673 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005674 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5675 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005676 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005677 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005678
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005679 void MarkInDexCache() {
5680 SetPackedFlag<kFlagIsInDexCache>(true);
5681 DCHECK(!NeedsEnvironment());
5682 RemoveEnvironment();
5683 SetSideEffects(SideEffects::None());
5684 }
5685
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005686 void MarkInBootImage() {
5687 SetPackedFlag<kFlagIsInBootImage>(true);
5688 }
5689
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005690 void AddSpecialInput(HInstruction* special_input);
5691
5692 using HInstruction::GetInputRecords; // Keep the const version visible.
5693 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5694 return ArrayRef<HUserRecord<HInstruction*>>(
5695 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5696 }
5697
5698 Primitive::Type GetType() const OVERRIDE {
5699 return Primitive::kPrimNot;
5700 }
5701
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005702 DECLARE_INSTRUCTION(LoadClass);
5703
5704 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005705 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005706 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005707 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005708 // Whether this instruction must generate the initialization check.
5709 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005710 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005711 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5712 static constexpr size_t kFieldLoadKindSize =
5713 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5714 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005715 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005716 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5717
5718 static bool HasTypeReference(LoadKind load_kind) {
5719 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5720 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5721 load_kind == LoadKind::kDexCacheViaMethod ||
5722 load_kind == LoadKind::kReferrersClass;
5723 }
5724
5725 static bool HasAddress(LoadKind load_kind) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005726 return load_kind == LoadKind::kBootImageAddress ||
5727 load_kind == LoadKind::kJitTableAddress;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005728 }
5729
5730 static bool HasDexCacheReference(LoadKind load_kind) {
5731 return load_kind == LoadKind::kDexCachePcRelative;
5732 }
5733
5734 void SetLoadKindInternal(LoadKind load_kind);
5735
5736 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5737 // For other load kinds it's empty or possibly some architecture-specific instruction
5738 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5739 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005740
Andreas Gampea5b09a62016-11-17 15:21:22 -08005741 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005742 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005743
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005744 union {
5745 uint32_t dex_cache_element_index; // Only for dex cache reference.
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005746 uint64_t address; // Up to 64-bit, needed for kJitTableAddress on 64-bit targets.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005747 } load_data_;
5748
Calin Juravleacf735c2015-02-12 15:25:22 +00005749 ReferenceTypeInfo loaded_class_rti_;
5750
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005751 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5752};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005753std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5754
5755// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5756inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5757 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5758 return load_data_.dex_cache_element_index;
5759}
5760
5761// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5762inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005763 // The special input is used for PC-relative loads on some architectures,
5764 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005765 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005766 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5767 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5768 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005769 DCHECK(special_input_.GetInstruction() == nullptr);
5770 special_input_ = HUserRecord<HInstruction*>(special_input);
5771 special_input->AddUseAt(this, 0);
5772}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005773
Vladimir Marko372f10e2016-05-17 16:30:10 +01005774class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005775 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005776 // Determines how to load the String.
5777 enum class LoadKind {
5778 // Use boot image String* address that will be known at link time.
5779 // Used for boot image strings referenced by boot image code in non-PIC mode.
5780 kBootImageLinkTimeAddress,
5781
5782 // Use PC-relative boot image String* address that will be known at link time.
5783 // Used for boot image strings referenced by boot image code in PIC mode.
5784 kBootImageLinkTimePcRelative,
5785
5786 // Use a known boot image String* address, embedded in the code by the codegen.
5787 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5788 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5789 // GetIncludePatchInformation().
5790 kBootImageAddress,
5791
Vladimir Markoaad75c62016-10-03 08:46:48 +00005792 // Load from an entry in the .bss section using a PC-relative load.
5793 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5794 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005795
5796 // Load from resolved strings array accessed through the class loaded from
5797 // the compiled method's own ArtMethod*. This is the default access type when
5798 // all other types are unavailable.
5799 kDexCacheViaMethod,
5800
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005801 // Load from the root table associated with the JIT compiled method.
5802 kJitTableAddress,
5803
5804 kLast = kJitTableAddress,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005805 };
5806
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005807 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005808 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005809 const DexFile& dex_file,
5810 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005811 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5812 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005813 string_index_(string_index),
5814 dex_file_(dex_file) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005815 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005816 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005817
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005818 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005819
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005820 LoadKind GetLoadKind() const {
5821 return GetPackedField<LoadKindField>();
5822 }
5823
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005824 const DexFile& GetDexFile() const {
5825 return dex_file_;
5826 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005827
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005828 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005829 return string_index_;
5830 }
5831
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005832 Handle<mirror::String> GetString() const {
5833 return string_;
5834 }
5835
5836 void SetString(Handle<mirror::String> str) {
5837 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005838 }
5839
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005840 bool CanBeMoved() const OVERRIDE { return true; }
5841
Vladimir Marko372f10e2016-05-17 16:30:10 +01005842 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005843
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005844 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005845
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005846 // Will call the runtime if we need to load the string through
5847 // the dex cache and the string is not guaranteed to be there yet.
5848 bool NeedsEnvironment() const OVERRIDE {
5849 LoadKind load_kind = GetLoadKind();
5850 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5851 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005852 load_kind == LoadKind::kBootImageAddress ||
5853 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005854 return false;
5855 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005856 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005857 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005858
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005859 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5860 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5861 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005862
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005863 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005864 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005865
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005866 static SideEffects SideEffectsForArchRuntimeCalls() {
5867 return SideEffects::CanTriggerGC();
5868 }
5869
Vladimir Marko372f10e2016-05-17 16:30:10 +01005870 void AddSpecialInput(HInstruction* special_input);
5871
5872 using HInstruction::GetInputRecords; // Keep the const version visible.
5873 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5874 return ArrayRef<HUserRecord<HInstruction*>>(
5875 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005876 }
5877
Vladimir Marko372f10e2016-05-17 16:30:10 +01005878 Primitive::Type GetType() const OVERRIDE {
5879 return Primitive::kPrimNot;
5880 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005881
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005882 DECLARE_INSTRUCTION(LoadString);
5883
5884 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005885 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005886 static constexpr size_t kFieldLoadKindSize =
5887 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5888 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005889 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005890 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005891
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005892 void SetLoadKindInternal(LoadKind load_kind);
5893
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005894 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5895 // For other load kinds it's empty or possibly some architecture-specific instruction
5896 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005897 HUserRecord<HInstruction*> special_input_;
5898
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005899 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005900 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005901
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005902 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005903
5904 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5905};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005906std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5907
5908// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005909inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005910 // The special input is used for PC-relative loads on some architectures,
5911 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005912 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005913 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005914 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5915 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005916 // HLoadString::GetInputRecords() returns an empty array at this point,
5917 // so use the GetInputRecords() from the base class to set the input record.
5918 DCHECK(special_input_.GetInstruction() == nullptr);
5919 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005920 special_input->AddUseAt(this, 0);
5921}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005922
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005923/**
5924 * Performs an initialization check on its Class object input.
5925 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005926class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005927 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005928 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005929 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005930 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005931 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5932 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005933 SetRawInputAt(0, constant);
5934 }
5935
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005936 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005937 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005938 return true;
5939 }
5940
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005941 bool NeedsEnvironment() const OVERRIDE {
5942 // May call runtime to initialize the class.
5943 return true;
5944 }
5945
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005946 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005947
5948 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5949
5950 DECLARE_INSTRUCTION(ClinitCheck);
5951
5952 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005953 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5954};
5955
Vladimir Markofcb503c2016-05-18 12:48:17 +01005956class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005957 public:
5958 HStaticFieldGet(HInstruction* cls,
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 : HExpression(field_type, SideEffects::FieldReadOfType(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) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005975 SetRawInputAt(0, cls);
5976 }
5977
Calin Juravle52c48962014-12-16 17:02:57 +00005978
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005979 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005980
Vladimir Marko372f10e2016-05-17 16:30:10 +01005981 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5982 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005983 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005984 }
5985
5986 size_t ComputeHashCode() const OVERRIDE {
5987 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5988 }
5989
Calin Juravle52c48962014-12-16 17:02:57 +00005990 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005991 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5992 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005993 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005994
5995 DECLARE_INSTRUCTION(StaticFieldGet);
5996
5997 private:
5998 const FieldInfo field_info_;
5999
6000 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6001};
6002
Vladimir Markofcb503c2016-05-18 12:48:17 +01006003class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006004 public:
6005 HStaticFieldSet(HInstruction* cls,
6006 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006007 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006008 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006009 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006010 bool is_volatile,
6011 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006012 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006013 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006014 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006015 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006016 field_info_(field,
6017 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006018 field_type,
6019 is_volatile,
6020 field_idx,
6021 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006022 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006023 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006024 SetRawInputAt(0, cls);
6025 SetRawInputAt(1, value);
6026 }
6027
Calin Juravle52c48962014-12-16 17:02:57 +00006028 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006029 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6030 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006031 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006032
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006033 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006034 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6035 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006036
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006037 DECLARE_INSTRUCTION(StaticFieldSet);
6038
6039 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006040 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6041 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6042 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6043 "Too many packed fields.");
6044
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006045 const FieldInfo field_info_;
6046
6047 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6048};
6049
Vladimir Markofcb503c2016-05-18 12:48:17 +01006050class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006051 public:
6052 HUnresolvedInstanceFieldGet(HInstruction* obj,
6053 Primitive::Type field_type,
6054 uint32_t field_index,
6055 uint32_t dex_pc)
6056 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6057 field_index_(field_index) {
6058 SetRawInputAt(0, obj);
6059 }
6060
6061 bool NeedsEnvironment() const OVERRIDE { return true; }
6062 bool CanThrow() const OVERRIDE { return true; }
6063
6064 Primitive::Type GetFieldType() const { return GetType(); }
6065 uint32_t GetFieldIndex() const { return field_index_; }
6066
6067 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6068
6069 private:
6070 const uint32_t field_index_;
6071
6072 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6073};
6074
Vladimir Markofcb503c2016-05-18 12:48:17 +01006075class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006076 public:
6077 HUnresolvedInstanceFieldSet(HInstruction* obj,
6078 HInstruction* value,
6079 Primitive::Type field_type,
6080 uint32_t field_index,
6081 uint32_t dex_pc)
6082 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006083 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006084 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006085 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006086 SetRawInputAt(0, obj);
6087 SetRawInputAt(1, value);
6088 }
6089
6090 bool NeedsEnvironment() const OVERRIDE { return true; }
6091 bool CanThrow() const OVERRIDE { return true; }
6092
Vladimir Markoa1de9182016-02-25 11:37:38 +00006093 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006094 uint32_t GetFieldIndex() const { return field_index_; }
6095
6096 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6097
6098 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006099 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6100 static constexpr size_t kFieldFieldTypeSize =
6101 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6102 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6103 kFieldFieldType + kFieldFieldTypeSize;
6104 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6105 "Too many packed fields.");
6106 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6107
Calin Juravlee460d1d2015-09-29 04:52:17 +01006108 const uint32_t field_index_;
6109
6110 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6111};
6112
Vladimir Markofcb503c2016-05-18 12:48:17 +01006113class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006114 public:
6115 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6116 uint32_t field_index,
6117 uint32_t dex_pc)
6118 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6119 field_index_(field_index) {
6120 }
6121
6122 bool NeedsEnvironment() const OVERRIDE { return true; }
6123 bool CanThrow() const OVERRIDE { return true; }
6124
6125 Primitive::Type GetFieldType() const { return GetType(); }
6126 uint32_t GetFieldIndex() const { return field_index_; }
6127
6128 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6129
6130 private:
6131 const uint32_t field_index_;
6132
6133 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6134};
6135
Vladimir Markofcb503c2016-05-18 12:48:17 +01006136class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006137 public:
6138 HUnresolvedStaticFieldSet(HInstruction* value,
6139 Primitive::Type field_type,
6140 uint32_t field_index,
6141 uint32_t dex_pc)
6142 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006143 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006144 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006145 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006146 SetRawInputAt(0, value);
6147 }
6148
6149 bool NeedsEnvironment() const OVERRIDE { return true; }
6150 bool CanThrow() const OVERRIDE { return true; }
6151
Vladimir Markoa1de9182016-02-25 11:37:38 +00006152 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006153 uint32_t GetFieldIndex() const { return field_index_; }
6154
6155 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6156
6157 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006158 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6159 static constexpr size_t kFieldFieldTypeSize =
6160 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6161 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6162 kFieldFieldType + kFieldFieldTypeSize;
6163 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6164 "Too many packed fields.");
6165 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6166
Calin Juravlee460d1d2015-09-29 04:52:17 +01006167 const uint32_t field_index_;
6168
6169 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6170};
6171
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006172// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006173class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006174 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006175 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6176 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006177
David Brazdilbbd733e2015-08-18 17:48:17 +01006178 bool CanBeNull() const OVERRIDE { return false; }
6179
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006180 DECLARE_INSTRUCTION(LoadException);
6181
6182 private:
6183 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6184};
6185
David Brazdilcb1c0552015-08-04 16:22:25 +01006186// Implicit part of move-exception which clears thread-local exception storage.
6187// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006188class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006189 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006190 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6191 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006192
6193 DECLARE_INSTRUCTION(ClearException);
6194
6195 private:
6196 DISALLOW_COPY_AND_ASSIGN(HClearException);
6197};
6198
Vladimir Markofcb503c2016-05-18 12:48:17 +01006199class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006200 public:
6201 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006202 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006203 SetRawInputAt(0, exception);
6204 }
6205
6206 bool IsControlFlow() const OVERRIDE { return true; }
6207
6208 bool NeedsEnvironment() const OVERRIDE { return true; }
6209
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006210 bool CanThrow() const OVERRIDE { return true; }
6211
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006212
6213 DECLARE_INSTRUCTION(Throw);
6214
6215 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006216 DISALLOW_COPY_AND_ASSIGN(HThrow);
6217};
6218
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006219/**
6220 * Implementation strategies for the code generator of a HInstanceOf
6221 * or `HCheckCast`.
6222 */
6223enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006224 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006225 kExactCheck, // Can do a single class compare.
6226 kClassHierarchyCheck, // Can just walk the super class chain.
6227 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6228 kInterfaceCheck, // No optimization yet when checking against an interface.
6229 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006230 kArrayCheck, // No optimization yet when checking against a generic array.
6231 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006232};
6233
Roland Levillain86503782016-02-11 19:07:30 +00006234std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6235
Vladimir Markofcb503c2016-05-18 12:48:17 +01006236class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006237 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006238 HInstanceOf(HInstruction* object,
6239 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006240 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006241 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006242 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006243 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006244 dex_pc) {
6245 SetPackedField<TypeCheckKindField>(check_kind);
6246 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006247 SetRawInputAt(0, object);
6248 SetRawInputAt(1, constant);
6249 }
6250
6251 bool CanBeMoved() const OVERRIDE { return true; }
6252
Vladimir Marko372f10e2016-05-17 16:30:10 +01006253 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006254 return true;
6255 }
6256
6257 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006258 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006259 }
6260
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006261 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006262 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6263 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6264 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6265 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006266
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006267 static bool CanCallRuntime(TypeCheckKind check_kind) {
6268 // Mips currently does runtime calls for any other checks.
6269 return check_kind != TypeCheckKind::kExactCheck;
6270 }
6271
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006272 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006273 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006274 }
6275
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006276 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006277
6278 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006279 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6280 static constexpr size_t kFieldTypeCheckKindSize =
6281 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6282 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6283 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6284 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6285 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006286
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006287 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6288};
6289
Vladimir Markofcb503c2016-05-18 12:48:17 +01006290class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006291 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006292 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006293 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006294 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6295 SetPackedFlag<kFlagUpperCanBeNull>(true);
6296 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006297 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006298 SetRawInputAt(0, input);
6299 }
6300
David Brazdilf5552582015-12-27 13:36:12 +00006301 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006302 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006303 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006304 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006305
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006306 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006307 DCHECK(GetUpperCanBeNull() || !can_be_null);
6308 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006309 }
6310
Vladimir Markoa1de9182016-02-25 11:37:38 +00006311 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006312
Calin Juravleb1498f62015-02-16 13:13:29 +00006313 DECLARE_INSTRUCTION(BoundType);
6314
6315 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006316 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6317 // is false then CanBeNull() cannot be true).
6318 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6319 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6320 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6321 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6322
Calin Juravleb1498f62015-02-16 13:13:29 +00006323 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006324 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6325 // It is used to bound the type in cases like:
6326 // if (x instanceof ClassX) {
6327 // // uper_bound_ will be ClassX
6328 // }
David Brazdilf5552582015-12-27 13:36:12 +00006329 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006330
6331 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6332};
6333
Vladimir Markofcb503c2016-05-18 12:48:17 +01006334class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006335 public:
6336 HCheckCast(HInstruction* object,
6337 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006338 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006339 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006340 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6341 SetPackedField<TypeCheckKindField>(check_kind);
6342 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006343 SetRawInputAt(0, object);
6344 SetRawInputAt(1, constant);
6345 }
6346
6347 bool CanBeMoved() const OVERRIDE { return true; }
6348
Vladimir Marko372f10e2016-05-17 16:30:10 +01006349 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006350 return true;
6351 }
6352
6353 bool NeedsEnvironment() const OVERRIDE {
6354 // Instruction may throw a CheckCastError.
6355 return true;
6356 }
6357
6358 bool CanThrow() const OVERRIDE { return true; }
6359
Vladimir Markoa1de9182016-02-25 11:37:38 +00006360 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6361 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6362 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6363 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006364
6365 DECLARE_INSTRUCTION(CheckCast);
6366
6367 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006368 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6369 static constexpr size_t kFieldTypeCheckKindSize =
6370 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6371 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6372 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6373 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6374 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006375
6376 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006377};
6378
Andreas Gampe26de38b2016-07-27 17:53:11 -07006379/**
6380 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6381 * @details We define the combined barrier types that are actually required
6382 * by the Java Memory Model, rather than using exactly the terminology from
6383 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6384 * primitives. Note that the JSR-133 cookbook generally does not deal with
6385 * store atomicity issues, and the recipes there are not always entirely sufficient.
6386 * The current recipe is as follows:
6387 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6388 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6389 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6390 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6391 * class has final fields.
6392 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6393 * store-to-memory instructions. Only generated together with non-temporal stores.
6394 */
6395enum MemBarrierKind {
6396 kAnyStore,
6397 kLoadAny,
6398 kStoreStore,
6399 kAnyAny,
6400 kNTStoreStore,
6401 kLastBarrierKind = kNTStoreStore
6402};
6403std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6404
Vladimir Markofcb503c2016-05-18 12:48:17 +01006405class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006406 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006407 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006408 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006409 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6410 SetPackedField<BarrierKindField>(barrier_kind);
6411 }
Calin Juravle27df7582015-04-17 19:12:31 +01006412
Vladimir Markoa1de9182016-02-25 11:37:38 +00006413 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006414
6415 DECLARE_INSTRUCTION(MemoryBarrier);
6416
6417 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006418 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6419 static constexpr size_t kFieldBarrierKindSize =
6420 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6421 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6422 kFieldBarrierKind + kFieldBarrierKindSize;
6423 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6424 "Too many packed fields.");
6425 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006426
6427 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6428};
6429
Vladimir Markofcb503c2016-05-18 12:48:17 +01006430class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006431 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006432 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006433 kEnter,
6434 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006435 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006436 };
6437
6438 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006439 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006440 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6441 dex_pc) {
6442 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006443 SetRawInputAt(0, object);
6444 }
6445
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006446 // Instruction may go into runtime, so we need an environment.
6447 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006448
6449 bool CanThrow() const OVERRIDE {
6450 // Verifier guarantees that monitor-exit cannot throw.
6451 // This is important because it allows the HGraphBuilder to remove
6452 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6453 return IsEnter();
6454 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006455
Vladimir Markoa1de9182016-02-25 11:37:38 +00006456 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6457 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006458
6459 DECLARE_INSTRUCTION(MonitorOperation);
6460
Calin Juravle52c48962014-12-16 17:02:57 +00006461 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006462 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6463 static constexpr size_t kFieldOperationKindSize =
6464 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6465 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6466 kFieldOperationKind + kFieldOperationKindSize;
6467 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6468 "Too many packed fields.");
6469 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006470
6471 private:
6472 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6473};
6474
Vladimir Markofcb503c2016-05-18 12:48:17 +01006475class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006476 public:
6477 HSelect(HInstruction* condition,
6478 HInstruction* true_value,
6479 HInstruction* false_value,
6480 uint32_t dex_pc)
6481 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6482 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6483
6484 // First input must be `true_value` or `false_value` to allow codegens to
6485 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6486 // that architectures which implement HSelect as a conditional move also
6487 // will not need to invert the condition.
6488 SetRawInputAt(0, false_value);
6489 SetRawInputAt(1, true_value);
6490 SetRawInputAt(2, condition);
6491 }
6492
6493 HInstruction* GetFalseValue() const { return InputAt(0); }
6494 HInstruction* GetTrueValue() const { return InputAt(1); }
6495 HInstruction* GetCondition() const { return InputAt(2); }
6496
6497 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006498 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6499 return true;
6500 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006501
6502 bool CanBeNull() const OVERRIDE {
6503 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6504 }
6505
6506 DECLARE_INSTRUCTION(Select);
6507
6508 private:
6509 DISALLOW_COPY_AND_ASSIGN(HSelect);
6510};
6511
Vladimir Markof9f64412015-09-02 14:05:49 +01006512class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006513 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006514 MoveOperands(Location source,
6515 Location destination,
6516 Primitive::Type type,
6517 HInstruction* instruction)
6518 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006519
6520 Location GetSource() const { return source_; }
6521 Location GetDestination() const { return destination_; }
6522
6523 void SetSource(Location value) { source_ = value; }
6524 void SetDestination(Location value) { destination_ = value; }
6525
6526 // The parallel move resolver marks moves as "in-progress" by clearing the
6527 // destination (but not the source).
6528 Location MarkPending() {
6529 DCHECK(!IsPending());
6530 Location dest = destination_;
6531 destination_ = Location::NoLocation();
6532 return dest;
6533 }
6534
6535 void ClearPending(Location dest) {
6536 DCHECK(IsPending());
6537 destination_ = dest;
6538 }
6539
6540 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006541 DCHECK(source_.IsValid() || destination_.IsInvalid());
6542 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006543 }
6544
6545 // True if this blocks a move from the given location.
6546 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006547 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006548 }
6549
6550 // A move is redundant if it's been eliminated, if its source and
6551 // destination are the same, or if its destination is unneeded.
6552 bool IsRedundant() const {
6553 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6554 }
6555
6556 // We clear both operands to indicate move that's been eliminated.
6557 void Eliminate() {
6558 source_ = destination_ = Location::NoLocation();
6559 }
6560
6561 bool IsEliminated() const {
6562 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6563 return source_.IsInvalid();
6564 }
6565
Alexey Frunze4dda3372015-06-01 18:31:49 -07006566 Primitive::Type GetType() const { return type_; }
6567
Nicolas Geoffray90218252015-04-15 11:56:51 +01006568 bool Is64BitMove() const {
6569 return Primitive::Is64BitType(type_);
6570 }
6571
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006572 HInstruction* GetInstruction() const { return instruction_; }
6573
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006574 private:
6575 Location source_;
6576 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006577 // The type this move is for.
6578 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006579 // The instruction this move is assocatied with. Null when this move is
6580 // for moving an input in the expected locations of user (including a phi user).
6581 // This is only used in debug mode, to ensure we do not connect interval siblings
6582 // in the same parallel move.
6583 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006584};
6585
Roland Levillainc9285912015-12-18 10:38:42 +00006586std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6587
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006588static constexpr size_t kDefaultNumberOfMoves = 4;
6589
Vladimir Markofcb503c2016-05-18 12:48:17 +01006590class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006591 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006592 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006593 : HTemplateInstruction(SideEffects::None(), dex_pc),
6594 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6595 moves_.reserve(kDefaultNumberOfMoves);
6596 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006597
Nicolas Geoffray90218252015-04-15 11:56:51 +01006598 void AddMove(Location source,
6599 Location destination,
6600 Primitive::Type type,
6601 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006602 DCHECK(source.IsValid());
6603 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006604 if (kIsDebugBuild) {
6605 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006606 for (const MoveOperands& move : moves_) {
6607 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006608 // Special case the situation where the move is for the spill slot
6609 // of the instruction.
6610 if ((GetPrevious() == instruction)
6611 || ((GetPrevious() == nullptr)
6612 && instruction->IsPhi()
6613 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006614 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006615 << "Doing parallel moves for the same instruction.";
6616 } else {
6617 DCHECK(false) << "Doing parallel moves for the same instruction.";
6618 }
6619 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006620 }
6621 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006622 for (const MoveOperands& move : moves_) {
6623 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006624 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006625 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006626 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006627 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006628 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006629 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006630 }
6631
Vladimir Marko225b6462015-09-28 12:17:40 +01006632 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006633 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006634 }
6635
Vladimir Marko225b6462015-09-28 12:17:40 +01006636 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006637
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006638 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006639
6640 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006641 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006642
6643 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6644};
6645
Mark Mendell0616ae02015-04-17 12:49:27 -04006646} // namespace art
6647
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006648#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6649#include "nodes_shared.h"
6650#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006651#ifdef ART_ENABLE_CODEGEN_arm
6652#include "nodes_arm.h"
6653#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006654#ifdef ART_ENABLE_CODEGEN_arm64
6655#include "nodes_arm64.h"
6656#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006657#ifdef ART_ENABLE_CODEGEN_mips
6658#include "nodes_mips.h"
6659#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006660#ifdef ART_ENABLE_CODEGEN_x86
6661#include "nodes_x86.h"
6662#endif
6663
6664namespace art {
6665
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006666class HGraphVisitor : public ValueObject {
6667 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006668 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6669 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006670
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006671 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006672 virtual void VisitBasicBlock(HBasicBlock* block);
6673
Roland Levillain633021e2014-10-01 14:12:25 +01006674 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006675 void VisitInsertionOrder();
6676
Roland Levillain633021e2014-10-01 14:12:25 +01006677 // Visit the graph following dominator tree reverse post-order.
6678 void VisitReversePostOrder();
6679
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006680 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006681
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006682 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006683#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006684 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6685
6686 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6687
6688#undef DECLARE_VISIT_INSTRUCTION
6689
6690 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006691 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006692
6693 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6694};
6695
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006696class HGraphDelegateVisitor : public HGraphVisitor {
6697 public:
6698 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6699 virtual ~HGraphDelegateVisitor() {}
6700
6701 // Visit functions that delegate to to super class.
6702#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006703 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006704
6705 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6706
6707#undef DECLARE_VISIT_INSTRUCTION
6708
6709 private:
6710 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6711};
6712
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006713// Iterator over the blocks that art part of the loop. Includes blocks part
6714// of an inner loop. The order in which the blocks are iterated is on their
6715// block id.
6716class HBlocksInLoopIterator : public ValueObject {
6717 public:
6718 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6719 : blocks_in_loop_(info.GetBlocks()),
6720 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6721 index_(0) {
6722 if (!blocks_in_loop_.IsBitSet(index_)) {
6723 Advance();
6724 }
6725 }
6726
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006727 bool Done() const { return index_ == blocks_.size(); }
6728 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006729 void Advance() {
6730 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006731 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006732 if (blocks_in_loop_.IsBitSet(index_)) {
6733 break;
6734 }
6735 }
6736 }
6737
6738 private:
6739 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006740 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006741 size_t index_;
6742
6743 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6744};
6745
Mingyao Yang3584bce2015-05-19 16:01:59 -07006746// Iterator over the blocks that art part of the loop. Includes blocks part
6747// of an inner loop. The order in which the blocks are iterated is reverse
6748// post order.
6749class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6750 public:
6751 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6752 : blocks_in_loop_(info.GetBlocks()),
6753 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6754 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006755 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006756 Advance();
6757 }
6758 }
6759
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006760 bool Done() const { return index_ == blocks_.size(); }
6761 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006762 void Advance() {
6763 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006764 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6765 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006766 break;
6767 }
6768 }
6769 }
6770
6771 private:
6772 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006773 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006774 size_t index_;
6775
6776 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6777};
6778
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006779inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006780 if (constant->IsIntConstant()) {
6781 return constant->AsIntConstant()->GetValue();
6782 } else if (constant->IsLongConstant()) {
6783 return constant->AsLongConstant()->GetValue();
6784 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006785 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006786 return 0;
6787 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006788}
6789
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006790#define INSTRUCTION_TYPE_CHECK(type, super) \
6791 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6792 inline const H##type* HInstruction::As##type() const { \
6793 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6794 } \
6795 inline H##type* HInstruction::As##type() { \
6796 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6797 }
6798
6799 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6800#undef INSTRUCTION_TYPE_CHECK
6801
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006802// Create space in `blocks` for adding `number_of_new_blocks` entries
6803// starting at location `at`. Blocks after `at` are moved accordingly.
6804inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6805 size_t number_of_new_blocks,
6806 size_t after) {
6807 DCHECK_LT(after, blocks->size());
6808 size_t old_size = blocks->size();
6809 size_t new_size = old_size + number_of_new_blocks;
6810 blocks->resize(new_size);
6811 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6812}
6813
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006814} // namespace art
6815
6816#endif // ART_COMPILER_OPTIMIZING_NODES_H_