blob: bf13702749f70cfd8fc725c245ea10f19a72a55a [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
Vladimir Marko60584552015-09-03 13:35:12 +000029#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010030#include "base/transform_array_ref.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010031#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080032#include "dex_file_types.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000033#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000034#include "handle.h"
35#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000036#include "invoke_type.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010037#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010038#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000039#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000040#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010041#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070042#include "primitive.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010043#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044
45namespace art {
46
David Brazdil1abb4192015-02-17 18:33:36 +000047class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000048class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010049class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010051class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000053class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000055class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000056class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000057class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000058class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000059class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010060class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010061class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010062class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010063class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000064class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010065class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000066class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067
Mathieu Chartier736b5602015-09-02 14:54:11 -070068namespace mirror {
69class DexCache;
70} // namespace mirror
71
Nicolas Geoffray818f2102014-02-18 16:43:35 +000072static const int kDefaultNumberOfBlocks = 8;
73static const int kDefaultNumberOfSuccessors = 2;
74static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010075static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010076static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000077static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000078
Roland Levillain5b5b9312016-03-22 14:57:31 +000079// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
80static constexpr int32_t kMaxIntShiftDistance = 0x1f;
81// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
82static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000083
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010084static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070085static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010086
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010087static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
88
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060089static constexpr uint32_t kNoDexPc = -1;
90
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010091inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
92 // For the purposes of the compiler, the dex files must actually be the same object
93 // if we want to safely treat them as the same. This is especially important for JIT
94 // as custom class loaders can open the same underlying file (or memory) multiple
95 // times and provide different class resolution but no two class loaders should ever
96 // use the same DexFile object - doing so is an unsupported hack that can lead to
97 // all sorts of weird failures.
98 return &lhs == &rhs;
99}
100
Dave Allison20dfc792014-06-16 20:44:29 -0700101enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700102 // All types.
103 kCondEQ, // ==
104 kCondNE, // !=
105 // Signed integers and floating-point numbers.
106 kCondLT, // <
107 kCondLE, // <=
108 kCondGT, // >
109 kCondGE, // >=
110 // Unsigned integers.
111 kCondB, // <
112 kCondBE, // <=
113 kCondA, // >
114 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100115 // First and last aliases.
116 kCondFirst = kCondEQ,
117 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700118};
119
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000120enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000121 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000122 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000123 kAnalysisFailThrowCatchLoop,
124 kAnalysisFailAmbiguousArrayOp,
125 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000126};
127
Andreas Gampe9186ced2016-12-12 14:28:21 -0800128template <typename T>
129static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
130 return static_cast<typename std::make_unsigned<T>::type>(x);
131}
132
Vladimir Markof9f64412015-09-02 14:05:49 +0100133class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100134 public:
135 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
136
137 void AddInstruction(HInstruction* instruction);
138 void RemoveInstruction(HInstruction* instruction);
139
David Brazdilc3d743f2015-04-22 13:40:50 +0100140 // Insert `instruction` before/after an existing instruction `cursor`.
141 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
142 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
143
Roland Levillain6b469232014-09-25 10:10:38 +0100144 // Return true if this list contains `instruction`.
145 bool Contains(HInstruction* instruction) const;
146
Roland Levillainccc07a92014-09-16 14:48:16 +0100147 // Return true if `instruction1` is found before `instruction2` in
148 // this instruction list and false otherwise. Abort if none
149 // of these instructions is found.
150 bool FoundBefore(const HInstruction* instruction1,
151 const HInstruction* instruction2) const;
152
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000153 bool IsEmpty() const { return first_instruction_ == nullptr; }
154 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
155
156 // Update the block of all instructions to be `block`.
157 void SetBlockOfInstructions(HBasicBlock* block) const;
158
159 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000160 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000161 void Add(const HInstructionList& instruction_list);
162
David Brazdil2d7352b2015-04-20 14:52:42 +0100163 // Return the number of instructions in the list. This is an expensive operation.
164 size_t CountSize() const;
165
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100166 private:
167 HInstruction* first_instruction_;
168 HInstruction* last_instruction_;
169
170 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000171 friend class HGraph;
172 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100173 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000174 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100175 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100176
177 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
178};
179
David Brazdil4833f5a2015-12-16 10:37:39 +0000180class ReferenceTypeInfo : ValueObject {
181 public:
182 typedef Handle<mirror::Class> TypeHandle;
183
Vladimir Markoa1de9182016-02-25 11:37:38 +0000184 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
185
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700186 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100187 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
188 }
189
Vladimir Markoa1de9182016-02-25 11:37:38 +0000190 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000191 return ReferenceTypeInfo(type_handle, is_exact);
192 }
193
194 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
195
Vladimir Markof39745e2016-01-26 12:16:55 +0000196 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000197 return handle.GetReference() != nullptr;
198 }
199
Vladimir Marko456307a2016-04-19 14:12:13 +0000200 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000201 return IsValidHandle(type_handle_);
202 }
203
204 bool IsExact() const { return is_exact_; }
205
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700206 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000207 DCHECK(IsValid());
208 return GetTypeHandle()->IsObjectClass();
209 }
210
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700211 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000212 DCHECK(IsValid());
213 return GetTypeHandle()->IsStringClass();
214 }
215
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000217 DCHECK(IsValid());
218 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
219 }
220
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000222 DCHECK(IsValid());
223 return GetTypeHandle()->IsInterface();
224 }
225
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700226 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000227 DCHECK(IsValid());
228 return GetTypeHandle()->IsArrayClass();
229 }
230
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000232 DCHECK(IsValid());
233 return GetTypeHandle()->IsPrimitiveArray();
234 }
235
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000237 DCHECK(IsValid());
238 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
239 }
240
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700241 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 DCHECK(IsValid());
243 if (!IsExact()) return false;
244 if (!IsArrayClass()) return false;
245 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
246 }
247
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700248 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000249 DCHECK(IsValid());
250 if (!IsExact()) return false;
251 if (!IsArrayClass()) return false;
252 if (!rti.IsArrayClass()) return false;
253 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
254 rti.GetTypeHandle()->GetComponentType());
255 }
256
257 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
258
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700259 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000260 DCHECK(IsValid());
261 DCHECK(rti.IsValid());
262 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
263 }
264
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700265 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000266 DCHECK(IsValid());
267 DCHECK(rti.IsValid());
268 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
269 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
270 }
271
272 // Returns true if the type information provide the same amount of details.
273 // Note that it does not mean that the instructions have the same actual type
274 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700275 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000276 if (!IsValid() && !rti.IsValid()) {
277 // Invalid types are equal.
278 return true;
279 }
280 if (!IsValid() || !rti.IsValid()) {
281 // One is valid, the other not.
282 return false;
283 }
284 return IsExact() == rti.IsExact()
285 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
286 }
287
288 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000289 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
290 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
291 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000292
293 // The class of the object.
294 TypeHandle type_handle_;
295 // Whether or not the type is exact or a superclass of the actual type.
296 // Whether or not we have any information about this type.
297 bool is_exact_;
298};
299
300std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
301
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000302// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100303class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100305 HGraph(ArenaAllocator* arena,
306 const DexFile& dex_file,
307 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100308 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700309 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100310 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100311 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000312 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100313 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000314 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100315 blocks_(arena->Adapter(kArenaAllocBlockList)),
316 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
317 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700318 entry_block_(nullptr),
319 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100320 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100321 number_of_vregs_(0),
322 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000323 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400324 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000325 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000326 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000327 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000328 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100329 dex_file_(dex_file),
330 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100331 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100332 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100333 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800334 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700335 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000336 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100337 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
338 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
339 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
340 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000341 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000342 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700343 osr_(osr),
344 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100345 blocks_.reserve(kDefaultNumberOfBlocks);
346 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000347
David Brazdilbadd8262016-02-02 16:28:56 +0000348 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700349 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000350
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000351 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100352 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
353
David Brazdil69ba7b72015-06-23 18:27:30 +0100354 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000355 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100356
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000357 HBasicBlock* GetEntryBlock() const { return entry_block_; }
358 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100359 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000360
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000361 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
362 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000363
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000364 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100365
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100366 void ComputeDominanceInformation();
367 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000368 void ClearLoopInformation();
369 void FindBackEdges(ArenaBitVector* visited);
370 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100371 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100372 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373
David Brazdil4833f5a2015-12-16 10:37:39 +0000374 // Analyze all natural loops in this graph. Returns a code specifying that it
375 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000376 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000377 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100378
David Brazdilffee3d32015-07-06 11:48:53 +0100379 // Iterate over blocks to compute try block membership. Needs reverse post
380 // order and loop information.
381 void ComputeTryBlockInformation();
382
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000383 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000384 // Returns the instruction to replace the invoke expression or null if the
385 // invoke is for a void method. Note that the caller is responsible for replacing
386 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000387 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000388
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000389 // Update the loop and try membership of `block`, which was spawned from `reference`.
390 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
391 // should be the new back edge.
392 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
393 HBasicBlock* reference,
394 bool replace_if_back_edge);
395
Mingyao Yang3584bce2015-05-19 16:01:59 -0700396 // Need to add a couple of blocks to test if the loop body is entered and
397 // put deoptimization instructions, etc.
398 void TransformLoopHeaderForBCE(HBasicBlock* header);
399
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000400 // Removes `block` from the graph. Assumes `block` has been disconnected from
401 // other blocks and has no instructions or phis.
402 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000403
David Brazdilfc6a86a2015-06-26 10:33:45 +0000404 // Splits the edge between `block` and `successor` while preserving the
405 // indices in the predecessor/successor lists. If there are multiple edges
406 // between the blocks, the lowest indices are used.
407 // Returns the new block which is empty and has the same dex pc as `successor`.
408 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
409
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100410 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
411 void SimplifyLoop(HBasicBlock* header);
412
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000413 int32_t GetNextInstructionId() {
414 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000415 return current_instruction_id_++;
416 }
417
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000418 int32_t GetCurrentInstructionId() const {
419 return current_instruction_id_;
420 }
421
422 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000423 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000424 current_instruction_id_ = id;
425 }
426
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100427 uint16_t GetMaximumNumberOfOutVRegs() const {
428 return maximum_number_of_out_vregs_;
429 }
430
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000431 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
432 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100433 }
434
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100435 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
436 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
437 }
438
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000439 void UpdateTemporariesVRegSlots(size_t slots) {
440 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100441 }
442
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000443 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100444 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000445 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100446 }
447
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100448 void SetNumberOfVRegs(uint16_t number_of_vregs) {
449 number_of_vregs_ = number_of_vregs;
450 }
451
452 uint16_t GetNumberOfVRegs() const {
453 return number_of_vregs_;
454 }
455
456 void SetNumberOfInVRegs(uint16_t value) {
457 number_of_in_vregs_ = value;
458 }
459
David Brazdildee58d62016-04-07 09:54:26 +0000460 uint16_t GetNumberOfInVRegs() const {
461 return number_of_in_vregs_;
462 }
463
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100464 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100465 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100466 return number_of_vregs_ - number_of_in_vregs_;
467 }
468
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100469 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100470 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100471 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100472
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100473 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
474 DCHECK(GetReversePostOrder()[0] == entry_block_);
475 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
476 }
477
478 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
479 return ReverseRange(GetReversePostOrder());
480 }
481
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100482 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100483 return linear_order_;
484 }
485
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100486 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
487 return ReverseRange(GetLinearOrder());
488 }
489
Mark Mendell1152c922015-04-24 17:06:35 -0400490 bool HasBoundsChecks() const {
491 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800492 }
493
Mark Mendell1152c922015-04-24 17:06:35 -0400494 void SetHasBoundsChecks(bool value) {
495 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800496 }
497
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100498 bool ShouldGenerateConstructorBarrier() const {
499 return should_generate_constructor_barrier_;
500 }
501
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000502 bool IsDebuggable() const { return debuggable_; }
503
David Brazdil8d5b8b22015-03-24 10:51:52 +0000504 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000505 // already, it is created and inserted into the graph. This method is only for
506 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600507 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000508
509 // TODO: This is problematic for the consistency of reference type propagation
510 // because it can be created anytime after the pass and thus it will be left
511 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600512 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000513
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600514 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
515 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000516 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600517 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
518 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000519 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600520 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
521 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000522 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600523 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
524 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000525 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000526
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100527 HCurrentMethod* GetCurrentMethod();
528
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100529 const DexFile& GetDexFile() const {
530 return dex_file_;
531 }
532
533 uint32_t GetMethodIdx() const {
534 return method_idx_;
535 }
536
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100537 InvokeType GetInvokeType() const {
538 return invoke_type_;
539 }
540
Mark Mendellc4701932015-04-10 13:18:51 -0400541 InstructionSet GetInstructionSet() const {
542 return instruction_set_;
543 }
544
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000545 bool IsCompilingOsr() const { return osr_; }
546
Mingyao Yang063fc772016-08-02 11:02:54 -0700547 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
548 return cha_single_implementation_list_;
549 }
550
551 void AddCHASingleImplementationDependency(ArtMethod* method) {
552 cha_single_implementation_list_.insert(method);
553 }
554
555 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800556 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700557 }
558
David Brazdil77a48ae2015-09-15 12:34:04 +0000559 bool HasTryCatch() const { return has_try_catch_; }
560 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100561
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000562 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
563 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
564
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100565 ArtMethod* GetArtMethod() const { return art_method_; }
566 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
567
Mark Mendellf6529172015-11-17 11:16:56 -0500568 // Returns an instruction with the opposite boolean value from 'cond'.
569 // The instruction has been inserted into the graph, either as a constant, or
570 // before cursor.
571 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
572
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000573 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
574
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800575 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
576 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
577 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
578
David Brazdil2d7352b2015-04-20 14:52:42 +0100579 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000580 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100581 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000582
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000583 template <class InstructionType, typename ValueType>
584 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600585 ArenaSafeMap<ValueType, InstructionType*>* cache,
586 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000587 // Try to find an existing constant of the given value.
588 InstructionType* constant = nullptr;
589 auto cached_constant = cache->find(value);
590 if (cached_constant != cache->end()) {
591 constant = cached_constant->second;
592 }
593
594 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100595 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000596 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600597 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000598 cache->Overwrite(value, constant);
599 InsertConstant(constant);
600 }
601 return constant;
602 }
603
David Brazdil8d5b8b22015-03-24 10:51:52 +0000604 void InsertConstant(HConstant* instruction);
605
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000606 // Cache a float constant into the graph. This method should only be
607 // called by the SsaBuilder when creating "equivalent" instructions.
608 void CacheFloatConstant(HFloatConstant* constant);
609
610 // See CacheFloatConstant comment.
611 void CacheDoubleConstant(HDoubleConstant* constant);
612
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000613 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000614
615 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100616 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000617
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100618 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100619 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000620
Aart Bik281c6812016-08-26 11:31:48 -0700621 // List of blocks to perform a linear order tree traversal. Unlike the reverse
622 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100623 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100624
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000625 HBasicBlock* entry_block_;
626 HBasicBlock* exit_block_;
627
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100628 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100629 uint16_t maximum_number_of_out_vregs_;
630
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100631 // The number of virtual registers in this method. Contains the parameters.
632 uint16_t number_of_vregs_;
633
634 // The number of virtual registers used by parameters of this method.
635 uint16_t number_of_in_vregs_;
636
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000637 // Number of vreg size slots that the temporaries use (used in baseline compiler).
638 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100639
Mark Mendell1152c922015-04-24 17:06:35 -0400640 // Has bounds checks. We can totally skip BCE if it's false.
641 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800642
David Brazdil77a48ae2015-09-15 12:34:04 +0000643 // Flag whether there are any try/catch blocks in the graph. We will skip
644 // try/catch-related passes if false.
645 bool has_try_catch_;
646
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000647 // Flag whether there are any irreducible loops in the graph.
648 bool has_irreducible_loops_;
649
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000650 // Indicates whether the graph should be compiled in a way that
651 // ensures full debuggability. If false, we can apply more
652 // aggressive optimizations that may limit the level of debugging.
653 const bool debuggable_;
654
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000655 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000656 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000657
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100658 // The dex file from which the method is from.
659 const DexFile& dex_file_;
660
661 // The method index in the dex file.
662 const uint32_t method_idx_;
663
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100664 // If inlined, this encodes how the callee is being invoked.
665 const InvokeType invoke_type_;
666
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100667 // Whether the graph has been transformed to SSA form. Only used
668 // in debug mode to ensure we are not using properties only valid
669 // for non-SSA form (like the number of temporaries).
670 bool in_ssa_form_;
671
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100672 const bool should_generate_constructor_barrier_;
673
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800674 // Number of CHA guards in the graph. Used to short-circuit the
675 // CHA guard optimization pass when there is no CHA guard left.
676 uint32_t number_of_cha_guards_;
677
Mathieu Chartiere401d142015-04-22 13:56:20 -0700678 const InstructionSet instruction_set_;
679
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000680 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000681 HNullConstant* cached_null_constant_;
682 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000683 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000684 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000685 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000686
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100687 HCurrentMethod* cached_current_method_;
688
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100689 // The ArtMethod this graph is for. Note that for AOT, it may be null,
690 // for example for methods whose declaring class could not be resolved
691 // (such as when the superclass could not be found).
692 ArtMethod* art_method_;
693
David Brazdil4833f5a2015-12-16 10:37:39 +0000694 // Keep the RTI of inexact Object to avoid having to pass stack handle
695 // collection pointer to passes which may create NullConstant.
696 ReferenceTypeInfo inexact_object_rti_;
697
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000698 // Whether we are compiling this graph for on stack replacement: this will
699 // make all loops seen as irreducible and emit special stack maps to mark
700 // compiled code entries which the interpreter can directly jump to.
701 const bool osr_;
702
Mingyao Yang063fc772016-08-02 11:02:54 -0700703 // List of methods that are assumed to have single implementation.
704 ArenaSet<ArtMethod*> cha_single_implementation_list_;
705
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000706 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100707 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000708 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000709 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000710 DISALLOW_COPY_AND_ASSIGN(HGraph);
711};
712
Vladimir Markof9f64412015-09-02 14:05:49 +0100713class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000714 public:
715 HLoopInformation(HBasicBlock* header, HGraph* graph)
716 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100717 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000718 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100719 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100720 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100721 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000722 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100723 back_edges_.reserve(kDefaultNumberOfBackEdges);
724 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100725
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000726 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100727 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000728
729 void Dump(std::ostream& os);
730
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100731 HBasicBlock* GetHeader() const {
732 return header_;
733 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000734
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000735 void SetHeader(HBasicBlock* block) {
736 header_ = block;
737 }
738
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100739 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
740 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
741 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
742
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000743 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100744 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000745 }
746
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100747 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100748 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100749 }
750
David Brazdil46e2a392015-03-16 17:31:52 +0000751 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100752 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100753 }
754
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000755 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100756 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000757 }
758
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100759 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100760
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100761 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100762 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100763 }
764
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100765 // Returns the lifetime position of the back edge that has the
766 // greatest lifetime position.
767 size_t GetLifetimeEnd() const;
768
769 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100770 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100771 }
772
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000773 // Finds blocks that are part of this loop.
774 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100775
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100776 // Returns whether this loop information contains `block`.
777 // Note that this loop information *must* be populated before entering this function.
778 bool Contains(const HBasicBlock& block) const;
779
780 // Returns whether this loop information is an inner loop of `other`.
781 // Note that `other` *must* be populated before entering this function.
782 bool IsIn(const HLoopInformation& other) const;
783
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800784 // Returns true if instruction is not defined within this loop.
785 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700786
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100787 const ArenaBitVector& GetBlocks() const { return blocks_; }
788
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000789 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000790 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000791
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000792 void ClearAllBlocks() {
793 blocks_.ClearAllBits();
794 }
795
David Brazdil3f4a5222016-05-06 12:46:21 +0100796 bool HasBackEdgeNotDominatedByHeader() const;
797
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100798 bool IsPopulated() const {
799 return blocks_.GetHighestBitSet() != -1;
800 }
801
Anton Shaminf89381f2016-05-16 16:44:13 +0600802 bool DominatesAllBackEdges(HBasicBlock* block);
803
David Sehrc757dec2016-11-04 15:48:34 -0700804 bool HasExitEdge() const;
805
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000806 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100807 // Internal recursive implementation of `Populate`.
808 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100809 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100810
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000811 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100812 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000813 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100814 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100815 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100816 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000817
818 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
819};
820
David Brazdilec16f792015-08-19 15:04:01 +0100821// Stores try/catch information for basic blocks.
822// Note that HGraph is constructed so that catch blocks cannot simultaneously
823// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100824class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100825 public:
826 // Try block information constructor.
827 explicit TryCatchInformation(const HTryBoundary& try_entry)
828 : try_entry_(&try_entry),
829 catch_dex_file_(nullptr),
830 catch_type_index_(DexFile::kDexNoIndex16) {
831 DCHECK(try_entry_ != nullptr);
832 }
833
834 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800835 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100836 : try_entry_(nullptr),
837 catch_dex_file_(&dex_file),
838 catch_type_index_(catch_type_index) {}
839
840 bool IsTryBlock() const { return try_entry_ != nullptr; }
841
842 const HTryBoundary& GetTryEntry() const {
843 DCHECK(IsTryBlock());
844 return *try_entry_;
845 }
846
847 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
848
849 bool IsCatchAllTypeIndex() const {
850 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800851 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100852 }
853
Andreas Gampea5b09a62016-11-17 15:21:22 -0800854 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100855 DCHECK(IsCatchBlock());
856 return catch_type_index_;
857 }
858
859 const DexFile& GetCatchDexFile() const {
860 DCHECK(IsCatchBlock());
861 return *catch_dex_file_;
862 }
863
864 private:
865 // One of possibly several TryBoundary instructions entering the block's try.
866 // Only set for try blocks.
867 const HTryBoundary* try_entry_;
868
869 // Exception type information. Only set for catch blocks.
870 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800871 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100872};
873
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100874static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100875static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100876
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000877// A block in a method. Contains the list of instructions represented
878// as a double linked list. Each block knows its predecessors and
879// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100880
Vladimir Markof9f64412015-09-02 14:05:49 +0100881class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000882 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700883 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000884 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000885 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
886 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000887 loop_information_(nullptr),
888 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000889 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100890 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100891 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100892 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000893 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000894 try_catch_information_(nullptr) {
895 predecessors_.reserve(kDefaultNumberOfPredecessors);
896 successors_.reserve(kDefaultNumberOfSuccessors);
897 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
898 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000899
Vladimir Marko60584552015-09-03 13:35:12 +0000900 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100901 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000902 }
903
Vladimir Marko60584552015-09-03 13:35:12 +0000904 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100905 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000906 }
907
David Brazdild26a4112015-11-10 11:07:31 +0000908 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
909 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
910
Vladimir Marko60584552015-09-03 13:35:12 +0000911 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
912 return ContainsElement(successors_, block, start_from);
913 }
914
915 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100916 return dominated_blocks_;
917 }
918
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100919 bool IsEntryBlock() const {
920 return graph_->GetEntryBlock() == this;
921 }
922
923 bool IsExitBlock() const {
924 return graph_->GetExitBlock() == this;
925 }
926
David Brazdil46e2a392015-03-16 17:31:52 +0000927 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000928 bool IsSingleTryBoundary() const;
929
930 // Returns true if this block emits nothing but a jump.
931 bool IsSingleJump() const {
932 HLoopInformation* loop_info = GetLoopInformation();
933 return (IsSingleGoto() || IsSingleTryBoundary())
934 // Back edges generate a suspend check.
935 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
936 }
David Brazdil46e2a392015-03-16 17:31:52 +0000937
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000938 void AddBackEdge(HBasicBlock* back_edge) {
939 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000940 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000941 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100942 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000943 loop_information_->AddBackEdge(back_edge);
944 }
945
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000946 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000947 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000948
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100949 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000950 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600951 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000952
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000953 HBasicBlock* GetDominator() const { return dominator_; }
954 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000955 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
956
957 void RemoveDominatedBlock(HBasicBlock* block) {
958 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100959 }
Vladimir Marko60584552015-09-03 13:35:12 +0000960
961 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
962 ReplaceElement(dominated_blocks_, existing, new_block);
963 }
964
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100965 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000966
967 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100968 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000969 }
970
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100971 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
972 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100973 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100974 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100975 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
976 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000977
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000978 HInstruction* GetFirstInstructionDisregardMoves() const;
979
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000980 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000981 successors_.push_back(block);
982 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000983 }
984
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100985 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
986 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100987 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000988 new_block->predecessors_.push_back(this);
989 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000990 }
991
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000992 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
993 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000994 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000995 new_block->successors_.push_back(this);
996 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000997 }
998
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100999 // Insert `this` between `predecessor` and `successor. This method
1000 // preserves the indicies, and will update the first edge found between
1001 // `predecessor` and `successor`.
1002 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1003 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001004 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001005 successor->predecessors_[predecessor_index] = this;
1006 predecessor->successors_[successor_index] = this;
1007 successors_.push_back(successor);
1008 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001009 }
1010
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001011 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001012 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001013 }
1014
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001015 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001016 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001017 }
1018
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001019 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001020 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001021 }
1022
1023 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001024 predecessors_.push_back(block);
1025 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001026 }
1027
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001028 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001029 DCHECK_EQ(predecessors_.size(), 2u);
1030 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001031 }
1032
David Brazdil769c9e52015-04-27 13:54:09 +01001033 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001034 DCHECK_EQ(successors_.size(), 2u);
1035 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001036 }
1037
David Brazdilfc6a86a2015-06-26 10:33:45 +00001038 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001039 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001040 }
1041
David Brazdilfc6a86a2015-06-26 10:33:45 +00001042 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001043 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001044 }
1045
David Brazdilfc6a86a2015-06-26 10:33:45 +00001046 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001047 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001048 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001049 }
1050
1051 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001052 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001053 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001054 }
1055
1056 // Returns whether the first occurrence of `predecessor` in the list of
1057 // predecessors is at index `idx`.
1058 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001059 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001060 return GetPredecessorIndexOf(predecessor) == idx;
1061 }
1062
David Brazdild7558da2015-09-22 13:04:14 +01001063 // Create a new block between this block and its predecessors. The new block
1064 // is added to the graph, all predecessor edges are relinked to it and an edge
1065 // is created to `this`. Returns the new empty block. Reverse post order or
1066 // loop and try/catch information are not updated.
1067 HBasicBlock* CreateImmediateDominator();
1068
David Brazdilfc6a86a2015-06-26 10:33:45 +00001069 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001070 // created, latter block. Note that this method will add the block to the
1071 // graph, create a Goto at the end of the former block and will create an edge
1072 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001073 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001074 HBasicBlock* SplitBefore(HInstruction* cursor);
1075
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001076 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001077 // created block. Note that this method just updates raw block information,
1078 // like predecessors, successors, dominators, and instruction list. It does not
1079 // update the graph, reverse post order, loop information, nor make sure the
1080 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001081 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1082
1083 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1084 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001085
1086 // Merge `other` at the end of `this`. Successors and dominated blocks of
1087 // `other` are changed to be successors and dominated blocks of `this`. Note
1088 // that this method does not update the graph, reverse post order, loop
1089 // information, nor make sure the blocks are consistent (for example ending
1090 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001091 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001092
1093 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1094 // of `this` are moved to `other`.
1095 // Note that this method does not update the graph, reverse post order, loop
1096 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001097 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001098 void ReplaceWith(HBasicBlock* other);
1099
Aart Bik6b69e0a2017-01-11 10:20:43 -08001100 // Merges the instructions of `other` at the end of `this`.
1101 void MergeInstructionsWith(HBasicBlock* other);
1102
David Brazdil2d7352b2015-04-20 14:52:42 +01001103 // Merge `other` at the end of `this`. This method updates loops, reverse post
1104 // order, links to predecessors, successors, dominators and deletes the block
1105 // from the graph. The two blocks must be successive, i.e. `this` the only
1106 // predecessor of `other` and vice versa.
1107 void MergeWith(HBasicBlock* other);
1108
1109 // Disconnects `this` from all its predecessors, successors and dominator,
1110 // removes it from all loops it is included in and eventually from the graph.
1111 // The block must not dominate any other block. Predecessors and successors
1112 // are safely updated.
1113 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001114
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001115 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001116 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001117 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001118 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001119 // Replace instruction `initial` with `replacement` within this block.
1120 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1121 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001122 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001123 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001124 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1125 // instruction list. With 'ensure_safety' set to true, it verifies that the
1126 // instruction is not in use and removes it from the use lists of its inputs.
1127 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1128 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001129 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001130
1131 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001132 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001133 }
1134
Roland Levillain6b879dd2014-09-22 17:13:44 +01001135 bool IsLoopPreHeaderFirstPredecessor() const {
1136 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001137 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001138 }
1139
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001140 bool IsFirstPredecessorBackEdge() const {
1141 DCHECK(IsLoopHeader());
1142 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1143 }
1144
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001145 HLoopInformation* GetLoopInformation() const {
1146 return loop_information_;
1147 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001148
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001149 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001150 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001151 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001152 void SetInLoop(HLoopInformation* info) {
1153 if (IsLoopHeader()) {
1154 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001155 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001156 loop_information_ = info;
1157 } else if (loop_information_->Contains(*info->GetHeader())) {
1158 // Block is currently part of an outer loop. Make it part of this inner loop.
1159 // Note that a non loop header having a loop information means this loop information
1160 // has already been populated
1161 loop_information_ = info;
1162 } else {
1163 // Block is part of an inner loop. Do not update the loop information.
1164 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1165 // at this point, because this method is being called while populating `info`.
1166 }
1167 }
1168
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001169 // Raw update of the loop information.
1170 void SetLoopInformation(HLoopInformation* info) {
1171 loop_information_ = info;
1172 }
1173
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001174 bool IsInLoop() const { return loop_information_ != nullptr; }
1175
David Brazdilec16f792015-08-19 15:04:01 +01001176 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1177
1178 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1179 try_catch_information_ = try_catch_information;
1180 }
1181
1182 bool IsTryBlock() const {
1183 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1184 }
1185
1186 bool IsCatchBlock() const {
1187 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1188 }
David Brazdilffee3d32015-07-06 11:48:53 +01001189
1190 // Returns the try entry that this block's successors should have. They will
1191 // be in the same try, unless the block ends in a try boundary. In that case,
1192 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001193 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001194
David Brazdild7558da2015-09-22 13:04:14 +01001195 bool HasThrowingInstructions() const;
1196
David Brazdila4b8c212015-05-07 09:59:30 +01001197 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001198 bool Dominates(HBasicBlock* block) const;
1199
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001200 size_t GetLifetimeStart() const { return lifetime_start_; }
1201 size_t GetLifetimeEnd() const { return lifetime_end_; }
1202
1203 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1204 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1205
David Brazdil8d5b8b22015-03-24 10:51:52 +00001206 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001207 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001208 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001209 bool HasSinglePhi() const;
1210
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001211 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001212 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001213 ArenaVector<HBasicBlock*> predecessors_;
1214 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001215 HInstructionList instructions_;
1216 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001217 HLoopInformation* loop_information_;
1218 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001219 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001220 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001221 // The dex program counter of the first instruction of this block.
1222 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001223 size_t lifetime_start_;
1224 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001225 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001226
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001227 friend class HGraph;
1228 friend class HInstruction;
1229
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001230 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1231};
1232
David Brazdilb2bd1c52015-03-25 11:17:37 +00001233// Iterates over the LoopInformation of all loops which contain 'block'
1234// from the innermost to the outermost.
1235class HLoopInformationOutwardIterator : public ValueObject {
1236 public:
1237 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1238 : current_(block.GetLoopInformation()) {}
1239
1240 bool Done() const { return current_ == nullptr; }
1241
1242 void Advance() {
1243 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001244 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001245 }
1246
1247 HLoopInformation* Current() const {
1248 DCHECK(!Done());
1249 return current_;
1250 }
1251
1252 private:
1253 HLoopInformation* current_;
1254
1255 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1256};
1257
Alexandre Ramesef20f712015-06-09 10:29:30 +01001258#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001259 M(Above, Condition) \
1260 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001261 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001262 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001263 M(ArrayGet, Instruction) \
1264 M(ArrayLength, Instruction) \
1265 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001266 M(Below, Condition) \
1267 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001268 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001269 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001270 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001271 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001272 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001273 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001274 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001275 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001276 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001277 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001278 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001279 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001280 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001281 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001282 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001283 M(Exit, Instruction) \
1284 M(FloatConstant, Constant) \
1285 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001286 M(GreaterThan, Condition) \
1287 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001288 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001289 M(InstanceFieldGet, Instruction) \
1290 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001291 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001292 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001293 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001294 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001295 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001296 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001297 M(LessThan, Condition) \
1298 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001299 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001300 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001301 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001302 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001303 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001304 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001305 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001306 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001307 M(Neg, UnaryOperation) \
1308 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001309 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001310 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001311 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001312 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001313 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001314 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001315 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001316 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001317 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001318 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001319 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001320 M(Return, Instruction) \
1321 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001322 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001323 M(Shl, BinaryOperation) \
1324 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001325 M(StaticFieldGet, Instruction) \
1326 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001327 M(UnresolvedInstanceFieldGet, Instruction) \
1328 M(UnresolvedInstanceFieldSet, Instruction) \
1329 M(UnresolvedStaticFieldGet, Instruction) \
1330 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001331 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001332 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001333 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001334 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001335 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001336 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001337 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001338 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001339
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001340/*
1341 * Instructions, shared across several (not all) architectures.
1342 */
1343#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1344#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1345#else
1346#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001347 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001348 M(MultiplyAccumulate, Instruction) \
1349 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001350#endif
1351
Vladimir Markob4536b72015-11-24 13:45:23 +00001352#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001353#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001354#else
1355#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1356 M(ArmDexCacheArraysBase, Instruction)
1357#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001358
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001359#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001360#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001361#else
1362#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001363 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001364#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001365
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001366#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001367#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001368#else
1369#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1370 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001371 M(MipsDexCacheArraysBase, Instruction) \
1372 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001373#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001374
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001375#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1376
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001377#ifndef ART_ENABLE_CODEGEN_x86
1378#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1379#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001380#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1381 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001382 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001383 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001384 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001385#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001386
1387#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1388
1389#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1390 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001391 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001392 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1393 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001394 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001395 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001396 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1397 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1398
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001399#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1400 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001401 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001402 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001403 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001404 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001405
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001406#define FOR_EACH_INSTRUCTION(M) \
1407 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1408 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1409
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001410#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001411FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1412#undef FORWARD_DECLARATION
1413
Vladimir Marko372f10e2016-05-17 16:30:10 +01001414#define DECLARE_INSTRUCTION(type) \
1415 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1416 const char* DebugName() const OVERRIDE { return #type; } \
1417 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1418 return other->Is##type(); \
1419 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001420 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001421
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001422#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1423 bool Is##type() const { return As##type() != nullptr; } \
1424 const H##type* As##type() const { return this; } \
1425 H##type* As##type() { return this; }
1426
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001427template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001428class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001429 public:
David Brazdiled596192015-01-23 10:39:45 +00001430 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001431 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001432 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001433
Vladimir Marko46817b82016-03-29 12:21:58 +01001434 // Hook for the IntrusiveForwardList<>.
1435 // TODO: Hide this better.
1436 IntrusiveForwardListHook hook;
1437
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001438 private:
David Brazdiled596192015-01-23 10:39:45 +00001439 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001440 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001441
1442 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001443 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001444
Vladimir Marko46817b82016-03-29 12:21:58 +01001445 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001446
1447 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1448};
1449
David Brazdiled596192015-01-23 10:39:45 +00001450template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001451using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001452
David Brazdil1abb4192015-02-17 18:33:36 +00001453// This class is used by HEnvironment and HInstruction classes to record the
1454// instructions they use and pointers to the corresponding HUseListNodes kept
1455// by the used instructions.
1456template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001457class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001458 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001459 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1460 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001461
Vladimir Marko46817b82016-03-29 12:21:58 +01001462 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1463 : HUserRecord(old_record.instruction_, before_use_node) {}
1464 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1465 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001466 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001467 }
1468
1469 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001470 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1471 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001472
1473 private:
1474 // Instruction used by the user.
1475 HInstruction* instruction_;
1476
Vladimir Marko46817b82016-03-29 12:21:58 +01001477 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1478 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001479};
1480
Vladimir Markoe9004912016-06-16 16:50:52 +01001481// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1482// This is used for HInstruction::GetInputs() to return a container wrapper providing
1483// HInstruction* values even though the underlying container has HUserRecord<>s.
1484struct HInputExtractor {
1485 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1486 return record.GetInstruction();
1487 }
1488 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1489 return record.GetInstruction();
1490 }
1491};
1492
1493using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1494using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1495
Aart Bik854a02b2015-07-14 16:07:00 -07001496/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001497 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001498 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001499 * For write/read dependences on fields/arrays, the dependence analysis uses
1500 * type disambiguation (e.g. a float field write cannot modify the value of an
1501 * integer field read) and the access type (e.g. a reference array write cannot
1502 * modify the value of a reference field read [although it may modify the
1503 * reference fetch prior to reading the field, which is represented by its own
1504 * write/read dependence]). The analysis makes conservative points-to
1505 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1506 * the same, and any reference read depends on any reference read without
1507 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001508 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001509 * The internal representation uses 38-bit and is described in the table below.
1510 * The first line indicates the side effect, and for field/array accesses the
1511 * second line indicates the type of the access (in the order of the
1512 * Primitive::Type enum).
1513 * The two numbered lines below indicate the bit position in the bitfield (read
1514 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001515 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001516 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1517 * +-------------+---------+---------+--------------+---------+---------+
1518 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1519 * | 3 |333333322|222222221| 1 |111111110|000000000|
1520 * | 7 |654321098|765432109| 8 |765432109|876543210|
1521 *
1522 * Note that, to ease the implementation, 'changes' bits are least significant
1523 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001524 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001525class SideEffects : public ValueObject {
1526 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001527 SideEffects() : flags_(0) {}
1528
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001529 static SideEffects None() {
1530 return SideEffects(0);
1531 }
1532
1533 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001534 return SideEffects(kAllChangeBits | kAllDependOnBits);
1535 }
1536
1537 static SideEffects AllChanges() {
1538 return SideEffects(kAllChangeBits);
1539 }
1540
1541 static SideEffects AllDependencies() {
1542 return SideEffects(kAllDependOnBits);
1543 }
1544
1545 static SideEffects AllExceptGCDependency() {
1546 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1547 }
1548
1549 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001550 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001551 }
1552
Aart Bik34c3ba92015-07-20 14:08:59 -07001553 static SideEffects AllWrites() {
1554 return SideEffects(kAllWrites);
1555 }
1556
1557 static SideEffects AllReads() {
1558 return SideEffects(kAllReads);
1559 }
1560
1561 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1562 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001563 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001564 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001565 }
1566
Aart Bik854a02b2015-07-14 16:07:00 -07001567 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001568 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001569 }
1570
Aart Bik34c3ba92015-07-20 14:08:59 -07001571 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1572 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001573 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001574 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001575 }
1576
1577 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001578 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001579 }
1580
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001581 static SideEffects CanTriggerGC() {
1582 return SideEffects(1ULL << kCanTriggerGCBit);
1583 }
1584
1585 static SideEffects DependsOnGC() {
1586 return SideEffects(1ULL << kDependsOnGCBit);
1587 }
1588
Aart Bik854a02b2015-07-14 16:07:00 -07001589 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001590 SideEffects Union(SideEffects other) const {
1591 return SideEffects(flags_ | other.flags_);
1592 }
1593
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001594 SideEffects Exclusion(SideEffects other) const {
1595 return SideEffects(flags_ & ~other.flags_);
1596 }
1597
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001598 void Add(SideEffects other) {
1599 flags_ |= other.flags_;
1600 }
1601
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001602 bool Includes(SideEffects other) const {
1603 return (other.flags_ & flags_) == other.flags_;
1604 }
1605
1606 bool HasSideEffects() const {
1607 return (flags_ & kAllChangeBits);
1608 }
1609
1610 bool HasDependencies() const {
1611 return (flags_ & kAllDependOnBits);
1612 }
1613
1614 // Returns true if there are no side effects or dependencies.
1615 bool DoesNothing() const {
1616 return flags_ == 0;
1617 }
1618
Aart Bik854a02b2015-07-14 16:07:00 -07001619 // Returns true if something is written.
1620 bool DoesAnyWrite() const {
1621 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001622 }
1623
Aart Bik854a02b2015-07-14 16:07:00 -07001624 // Returns true if something is read.
1625 bool DoesAnyRead() const {
1626 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001627 }
1628
Aart Bik854a02b2015-07-14 16:07:00 -07001629 // Returns true if potentially everything is written and read
1630 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001631 bool DoesAllReadWrite() const {
1632 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1633 }
1634
Aart Bik854a02b2015-07-14 16:07:00 -07001635 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001636 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001637 }
1638
Roland Levillain0d5a2812015-11-13 10:07:31 +00001639 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001640 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001641 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1642 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001643 }
1644
1645 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001646 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001647 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001648 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001649 for (int s = kLastBit; s >= 0; s--) {
1650 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1651 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1652 // This is a bit for the GC side effect.
1653 if (current_bit_is_set) {
1654 flags += "GC";
1655 }
Aart Bik854a02b2015-07-14 16:07:00 -07001656 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001657 } else {
1658 // This is a bit for the array/field analysis.
1659 // The underscore character stands for the 'can trigger GC' bit.
1660 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1661 if (current_bit_is_set) {
1662 flags += kDebug[s];
1663 }
1664 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1665 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1666 flags += "|";
1667 }
1668 }
Aart Bik854a02b2015-07-14 16:07:00 -07001669 }
1670 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001671 }
1672
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001673 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001674
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001675 private:
1676 static constexpr int kFieldArrayAnalysisBits = 9;
1677
1678 static constexpr int kFieldWriteOffset = 0;
1679 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1680 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1681 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1682
1683 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1684
1685 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1686 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1687 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1688 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1689
1690 static constexpr int kLastBit = kDependsOnGCBit;
1691 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1692
1693 // Aliases.
1694
1695 static_assert(kChangeBits == kDependOnBits,
1696 "the 'change' bits should match the 'depend on' bits.");
1697
1698 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1699 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1700 static constexpr uint64_t kAllWrites =
1701 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1702 static constexpr uint64_t kAllReads =
1703 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001704
Aart Bik854a02b2015-07-14 16:07:00 -07001705 // Translates type to bit flag.
1706 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1707 CHECK_NE(type, Primitive::kPrimVoid);
1708 const uint64_t one = 1;
1709 const int shift = type; // 0-based consecutive enum
1710 DCHECK_LE(kFieldWriteOffset, shift);
1711 DCHECK_LT(shift, kArrayWriteOffset);
1712 return one << (type + offset);
1713 }
1714
1715 // Private constructor on direct flags value.
1716 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1717
1718 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001719};
1720
David Brazdiled596192015-01-23 10:39:45 +00001721// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001722class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001723 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001724 HEnvironment(ArenaAllocator* arena,
1725 size_t number_of_vregs,
1726 const DexFile& dex_file,
1727 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001728 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001729 InvokeType invoke_type,
1730 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001731 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1732 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001733 parent_(nullptr),
1734 dex_file_(dex_file),
1735 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001736 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001737 invoke_type_(invoke_type),
1738 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001739 }
1740
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001741 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001742 : HEnvironment(arena,
1743 to_copy.Size(),
1744 to_copy.GetDexFile(),
1745 to_copy.GetMethodIdx(),
1746 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001747 to_copy.GetInvokeType(),
1748 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001749
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001750 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001751 if (parent_ != nullptr) {
1752 parent_->SetAndCopyParentChain(allocator, parent);
1753 } else {
1754 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1755 parent_->CopyFrom(parent);
1756 if (parent->GetParent() != nullptr) {
1757 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1758 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001759 }
David Brazdiled596192015-01-23 10:39:45 +00001760 }
1761
Vladimir Marko71bf8092015-09-15 15:33:14 +01001762 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001763 void CopyFrom(HEnvironment* environment);
1764
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001765 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1766 // input to the loop phi instead. This is for inserting instructions that
1767 // require an environment (like HDeoptimization) in the loop pre-header.
1768 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001769
1770 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001771 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001772 }
1773
1774 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001775 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001776 }
1777
David Brazdil1abb4192015-02-17 18:33:36 +00001778 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001779
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001780 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001781
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001782 HEnvironment* GetParent() const { return parent_; }
1783
1784 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001785 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001786 }
1787
1788 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001789 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001790 }
1791
1792 uint32_t GetDexPc() const {
1793 return dex_pc_;
1794 }
1795
1796 uint32_t GetMethodIdx() const {
1797 return method_idx_;
1798 }
1799
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001800 InvokeType GetInvokeType() const {
1801 return invoke_type_;
1802 }
1803
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001804 const DexFile& GetDexFile() const {
1805 return dex_file_;
1806 }
1807
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001808 HInstruction* GetHolder() const {
1809 return holder_;
1810 }
1811
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001812
1813 bool IsFromInlinedInvoke() const {
1814 return GetParent() != nullptr;
1815 }
1816
David Brazdiled596192015-01-23 10:39:45 +00001817 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001818 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1819 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001820 HEnvironment* parent_;
1821 const DexFile& dex_file_;
1822 const uint32_t method_idx_;
1823 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001824 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001825
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001826 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001827 HInstruction* const holder_;
1828
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001829 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001830
1831 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1832};
1833
Vladimir Markof9f64412015-09-02 14:05:49 +01001834class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001835 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001836 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001837 : previous_(nullptr),
1838 next_(nullptr),
1839 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001840 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001841 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001842 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001843 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001844 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001845 locations_(nullptr),
1846 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001847 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001848 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001849 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1850 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1851 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001852
Dave Allison20dfc792014-06-16 20:44:29 -07001853 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001854
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001855#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001856 enum InstructionKind {
1857 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1858 };
1859#undef DECLARE_KIND
1860
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001861 HInstruction* GetNext() const { return next_; }
1862 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001863
Calin Juravle77520bc2015-01-12 18:45:46 +00001864 HInstruction* GetNextDisregardingMoves() const;
1865 HInstruction* GetPreviousDisregardingMoves() const;
1866
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001867 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001868 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001869 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001870 bool IsInBlock() const { return block_ != nullptr; }
1871 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001872 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1873 bool IsIrreducibleLoopHeaderPhi() const {
1874 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1875 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001876
Vladimir Marko372f10e2016-05-17 16:30:10 +01001877 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1878
1879 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1880 // One virtual method is enough, just const_cast<> and then re-add the const.
1881 return ArrayRef<const HUserRecord<HInstruction*>>(
1882 const_cast<HInstruction*>(this)->GetInputRecords());
1883 }
1884
Vladimir Markoe9004912016-06-16 16:50:52 +01001885 HInputsRef GetInputs() {
1886 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001887 }
1888
Vladimir Markoe9004912016-06-16 16:50:52 +01001889 HConstInputsRef GetInputs() const {
1890 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001891 }
1892
1893 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001894 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001895
Aart Bik2767f4b2016-10-28 15:03:53 -07001896 bool HasInput(HInstruction* input) const {
1897 for (const HInstruction* i : GetInputs()) {
1898 if (i == input) {
1899 return true;
1900 }
1901 }
1902 return false;
1903 }
1904
Vladimir Marko372f10e2016-05-17 16:30:10 +01001905 void SetRawInputAt(size_t index, HInstruction* input) {
1906 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1907 }
1908
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001909 virtual void Accept(HGraphVisitor* visitor) = 0;
1910 virtual const char* DebugName() const = 0;
1911
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001912 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1913
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001914 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001915
1916 uint32_t GetDexPc() const { return dex_pc_; }
1917
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001918 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001919
Roland Levillaine161a2a2014-10-03 12:45:18 +01001920 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001921 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001922
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001923 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001924 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001925
Calin Juravle10e244f2015-01-26 18:54:32 +00001926 // Does not apply for all instructions, but having this at top level greatly
1927 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001928 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001929 virtual bool CanBeNull() const {
1930 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1931 return true;
1932 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001933
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001934 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001935 return false;
1936 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001937
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001938 virtual bool IsActualObject() const {
1939 return GetType() == Primitive::kPrimNot;
1940 }
1941
Calin Juravle2e768302015-07-28 14:41:11 +00001942 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001943
Calin Juravle61d544b2015-02-23 16:46:57 +00001944 ReferenceTypeInfo GetReferenceTypeInfo() const {
1945 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001946 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001947 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001948 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001949
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001950 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001951 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001952 // Note: fixup_end remains valid across push_front().
1953 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1954 HUseListNode<HInstruction*>* new_node =
1955 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1956 uses_.push_front(*new_node);
1957 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001958 }
1959
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001960 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001961 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001962 // Note: env_fixup_end remains valid across push_front().
1963 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1964 HUseListNode<HEnvironment*>* new_node =
1965 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1966 env_uses_.push_front(*new_node);
1967 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001968 }
1969
David Brazdil1abb4192015-02-17 18:33:36 +00001970 void RemoveAsUserOfInput(size_t input) {
1971 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001972 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1973 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1974 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001975 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001976
Vladimir Marko372f10e2016-05-17 16:30:10 +01001977 void RemoveAsUserOfAllInputs() {
1978 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1979 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1980 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1981 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1982 }
1983 }
1984
David Brazdil1abb4192015-02-17 18:33:36 +00001985 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1986 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001987
Vladimir Marko46817b82016-03-29 12:21:58 +01001988 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1989 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1990 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001991 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001992 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001993 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001994
Aart Bikcc42be02016-10-20 16:14:16 -07001995 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07001996 return
Aart Bikff7d89c2016-11-07 08:49:28 -08001997 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07001998 !CanThrow() &&
1999 !IsSuspendCheck() &&
2000 !IsControlFlow() &&
2001 !IsNativeDebugInfo() &&
2002 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002003 // If we added an explicit barrier then we should keep it.
2004 !IsMemoryBarrier();
2005 }
2006
Aart Bikcc42be02016-10-20 16:14:16 -07002007 bool IsDeadAndRemovable() const {
2008 return IsRemovable() && !HasUses();
2009 }
2010
Roland Levillain6c82d402014-10-13 16:10:27 +01002011 // Does this instruction strictly dominate `other_instruction`?
2012 // Returns false if this instruction and `other_instruction` are the same.
2013 // Aborts if this instruction and `other_instruction` are both phis.
2014 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002015
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002016 int GetId() const { return id_; }
2017 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002018
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002019 int GetSsaIndex() const { return ssa_index_; }
2020 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2021 bool HasSsaIndex() const { return ssa_index_ != -1; }
2022
2023 bool HasEnvironment() const { return environment_ != nullptr; }
2024 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002025 // Set the `environment_` field. Raw because this method does not
2026 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002027 void SetRawEnvironment(HEnvironment* environment) {
2028 DCHECK(environment_ == nullptr);
2029 DCHECK_EQ(environment->GetHolder(), this);
2030 environment_ = environment;
2031 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002032
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002033 void InsertRawEnvironment(HEnvironment* environment) {
2034 DCHECK(environment_ != nullptr);
2035 DCHECK_EQ(environment->GetHolder(), this);
2036 DCHECK(environment->GetParent() == nullptr);
2037 environment->parent_ = environment_;
2038 environment_ = environment;
2039 }
2040
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002041 void RemoveEnvironment();
2042
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002043 // Set the environment of this instruction, copying it from `environment`. While
2044 // copying, the uses lists are being updated.
2045 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002046 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002047 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002048 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002049 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002050 if (environment->GetParent() != nullptr) {
2051 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2052 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002053 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002054
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002055 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2056 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002057 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002058 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002059 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002060 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002061 if (environment->GetParent() != nullptr) {
2062 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2063 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002064 }
2065
Nicolas Geoffray39468442014-09-02 15:17:15 +01002066 // Returns the number of entries in the environment. Typically, that is the
2067 // number of dex registers in a method. It could be more in case of inlining.
2068 size_t EnvironmentSize() const;
2069
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002070 LocationSummary* GetLocations() const { return locations_; }
2071 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002072
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002073 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002074 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002075
Alexandre Rames188d4312015-04-09 18:30:21 +01002076 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2077 // uses of this instruction by `other` are *not* updated.
2078 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2079 ReplaceWith(other);
2080 other->ReplaceInput(this, use_index);
2081 }
2082
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002083 // Move `this` instruction before `cursor`.
2084 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002085
Vladimir Markofb337ea2015-11-25 15:25:10 +00002086 // Move `this` before its first user and out of any loops. If there is no
2087 // out-of-loop user that dominates all other users, move the instruction
2088 // to the end of the out-of-loop common dominator of the user's blocks.
2089 //
2090 // This can be used only on non-throwing instructions with no side effects that
2091 // have at least one use but no environment uses.
2092 void MoveBeforeFirstUserAndOutOfLoops();
2093
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002094#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002095 bool Is##type() const; \
2096 const H##type* As##type() const; \
2097 H##type* As##type();
2098
2099 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2100#undef INSTRUCTION_TYPE_CHECK
2101
2102#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002103 bool Is##type() const { return (As##type() != nullptr); } \
2104 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002105 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002106 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002107#undef INSTRUCTION_TYPE_CHECK
2108
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002109 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002110 // TODO: this method is used by LICM and GVN with possibly different
2111 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002112 virtual bool CanBeMoved() const { return false; }
2113
2114 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002115 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002116 return false;
2117 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002118
2119 // Returns whether any data encoded in the two instructions is equal.
2120 // This method does not look at the inputs. Both instructions must be
2121 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002122 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002123 return false;
2124 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002125
2126 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002127 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002128 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002129 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002130
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002131 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2132 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2133 // the virtual function because the __attribute__((__pure__)) doesn't really
2134 // apply the strong requirement for virtual functions, preventing optimizations.
2135 InstructionKind GetKind() const PURE;
2136 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002137
2138 virtual size_t ComputeHashCode() const {
2139 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002140 for (const HInstruction* input : GetInputs()) {
2141 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002142 }
2143 return result;
2144 }
2145
2146 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002147 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002148 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002149
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002150 size_t GetLifetimePosition() const { return lifetime_position_; }
2151 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2152 LiveInterval* GetLiveInterval() const { return live_interval_; }
2153 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2154 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2155
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002156 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2157
2158 // Returns whether the code generation of the instruction will require to have access
2159 // to the current method. Such instructions are:
2160 // (1): Instructions that require an environment, as calling the runtime requires
2161 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002162 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2163 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002164 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002165 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002166 }
2167
Vladimir Markodc151b22015-10-15 18:02:30 +01002168 // Returns whether the code generation of the instruction will require to have access
2169 // to the dex cache of the current method's declaring class via the current method.
2170 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002171
Mark Mendellc4701932015-04-10 13:18:51 -04002172 // Does this instruction have any use in an environment before
2173 // control flow hits 'other'?
2174 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2175
2176 // Remove all references to environment uses of this instruction.
2177 // The caller must ensure that this is safe to do.
2178 void RemoveEnvironmentUsers();
2179
Vladimir Markoa1de9182016-02-25 11:37:38 +00002180 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2181 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002182
David Brazdil1abb4192015-02-17 18:33:36 +00002183 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002184 // If set, the machine code for this instruction is assumed to be generated by
2185 // its users. Used by liveness analysis to compute use positions accordingly.
2186 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2187 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2188 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2189 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2190
Vladimir Marko372f10e2016-05-17 16:30:10 +01002191 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2192 return GetInputRecords()[i];
2193 }
2194
2195 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2196 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2197 input_records[index] = input;
2198 }
David Brazdil1abb4192015-02-17 18:33:36 +00002199
Vladimir Markoa1de9182016-02-25 11:37:38 +00002200 uint32_t GetPackedFields() const {
2201 return packed_fields_;
2202 }
2203
2204 template <size_t flag>
2205 bool GetPackedFlag() const {
2206 return (packed_fields_ & (1u << flag)) != 0u;
2207 }
2208
2209 template <size_t flag>
2210 void SetPackedFlag(bool value = true) {
2211 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2212 }
2213
2214 template <typename BitFieldType>
2215 typename BitFieldType::value_type GetPackedField() const {
2216 return BitFieldType::Decode(packed_fields_);
2217 }
2218
2219 template <typename BitFieldType>
2220 void SetPackedField(typename BitFieldType::value_type value) {
2221 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2222 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2223 }
2224
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002225 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002226 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2227 auto before_use_node = uses_.before_begin();
2228 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2229 HInstruction* user = use_node->GetUser();
2230 size_t input_index = use_node->GetIndex();
2231 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2232 before_use_node = use_node;
2233 }
2234 }
2235
2236 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2237 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2238 if (next != uses_.end()) {
2239 HInstruction* next_user = next->GetUser();
2240 size_t next_index = next->GetIndex();
2241 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2242 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2243 }
2244 }
2245
2246 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2247 auto before_env_use_node = env_uses_.before_begin();
2248 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2249 HEnvironment* user = env_use_node->GetUser();
2250 size_t input_index = env_use_node->GetIndex();
2251 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2252 before_env_use_node = env_use_node;
2253 }
2254 }
2255
2256 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2257 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2258 if (next != env_uses_.end()) {
2259 HEnvironment* next_user = next->GetUser();
2260 size_t next_index = next->GetIndex();
2261 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2262 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2263 }
2264 }
David Brazdil1abb4192015-02-17 18:33:36 +00002265
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002266 HInstruction* previous_;
2267 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002268 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002269 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002270
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002271 // An instruction gets an id when it is added to the graph.
2272 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002273 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002274 int id_;
2275
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002276 // When doing liveness analysis, instructions that have uses get an SSA index.
2277 int ssa_index_;
2278
Vladimir Markoa1de9182016-02-25 11:37:38 +00002279 // Packed fields.
2280 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002281
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002282 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002283 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002284
2285 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002286 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002287
Nicolas Geoffray39468442014-09-02 15:17:15 +01002288 // The environment associated with this instruction. Not null if the instruction
2289 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002290 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002291
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002292 // Set by the code generator.
2293 LocationSummary* locations_;
2294
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002295 // Set by the liveness analysis.
2296 LiveInterval* live_interval_;
2297
2298 // Set by the liveness analysis, this is the position in a linear
2299 // order of blocks where this instruction's live interval start.
2300 size_t lifetime_position_;
2301
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002302 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002303
Vladimir Markoa1de9182016-02-25 11:37:38 +00002304 // The reference handle part of the reference type info.
2305 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002306 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002307 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002308
David Brazdil1abb4192015-02-17 18:33:36 +00002309 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002310 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002311 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002312 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002313 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002314
2315 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2316};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002317std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002318
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002319// Iterates over the instructions, while preserving the next instruction
2320// in case the current instruction gets removed from the list by the user
2321// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002322class HInstructionIterator : public ValueObject {
2323 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002324 explicit HInstructionIterator(const HInstructionList& instructions)
2325 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002326 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002327 }
2328
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002329 bool Done() const { return instruction_ == nullptr; }
2330 HInstruction* Current() const { return instruction_; }
2331 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002332 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002333 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002334 }
2335
2336 private:
2337 HInstruction* instruction_;
2338 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002339
2340 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002341};
2342
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002343// Iterates over the instructions without saving the next instruction,
2344// therefore handling changes in the graph potentially made by the user
2345// of this iterator.
2346class HInstructionIteratorHandleChanges : public ValueObject {
2347 public:
2348 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2349 : instruction_(instructions.first_instruction_) {
2350 }
2351
2352 bool Done() const { return instruction_ == nullptr; }
2353 HInstruction* Current() const { return instruction_; }
2354 void Advance() {
2355 instruction_ = instruction_->GetNext();
2356 }
2357
2358 private:
2359 HInstruction* instruction_;
2360
2361 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2362};
2363
2364
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002365class HBackwardInstructionIterator : public ValueObject {
2366 public:
2367 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2368 : instruction_(instructions.last_instruction_) {
2369 next_ = Done() ? nullptr : instruction_->GetPrevious();
2370 }
2371
2372 bool Done() const { return instruction_ == nullptr; }
2373 HInstruction* Current() const { return instruction_; }
2374 void Advance() {
2375 instruction_ = next_;
2376 next_ = Done() ? nullptr : instruction_->GetPrevious();
2377 }
2378
2379 private:
2380 HInstruction* instruction_;
2381 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002382
2383 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002384};
2385
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002386class HVariableInputSizeInstruction : public HInstruction {
2387 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002388 using HInstruction::GetInputRecords; // Keep the const version visible.
2389 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2390 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2391 }
2392
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002393 void AddInput(HInstruction* input);
2394 void InsertInputAt(size_t index, HInstruction* input);
2395 void RemoveInputAt(size_t index);
2396
2397 protected:
2398 HVariableInputSizeInstruction(SideEffects side_effects,
2399 uint32_t dex_pc,
2400 ArenaAllocator* arena,
2401 size_t number_of_inputs,
2402 ArenaAllocKind kind)
2403 : HInstruction(side_effects, dex_pc),
2404 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2405
2406 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2407
2408 private:
2409 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2410};
2411
Vladimir Markof9f64412015-09-02 14:05:49 +01002412template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002413class HTemplateInstruction: public HInstruction {
2414 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002415 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002416 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002417 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002418
Vladimir Marko372f10e2016-05-17 16:30:10 +01002419 using HInstruction::GetInputRecords; // Keep the const version visible.
2420 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2421 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002422 }
2423
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002424 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002425 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002426
2427 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002428};
2429
Vladimir Markof9f64412015-09-02 14:05:49 +01002430// HTemplateInstruction specialization for N=0.
2431template<>
2432class HTemplateInstruction<0>: public HInstruction {
2433 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002434 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002435 : HInstruction(side_effects, dex_pc) {}
2436
Vladimir Markof9f64412015-09-02 14:05:49 +01002437 virtual ~HTemplateInstruction() {}
2438
Vladimir Marko372f10e2016-05-17 16:30:10 +01002439 using HInstruction::GetInputRecords; // Keep the const version visible.
2440 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2441 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002442 }
2443
2444 private:
2445 friend class SsaBuilder;
2446};
2447
Dave Allison20dfc792014-06-16 20:44:29 -07002448template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002449class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002450 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002451 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002452 : HTemplateInstruction<N>(side_effects, dex_pc) {
2453 this->template SetPackedField<TypeField>(type);
2454 }
Dave Allison20dfc792014-06-16 20:44:29 -07002455 virtual ~HExpression() {}
2456
Vladimir Markoa1de9182016-02-25 11:37:38 +00002457 Primitive::Type GetType() const OVERRIDE {
2458 return TypeField::Decode(this->GetPackedFields());
2459 }
Dave Allison20dfc792014-06-16 20:44:29 -07002460
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002461 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002462 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2463 static constexpr size_t kFieldTypeSize =
2464 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2465 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2466 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2467 "Too many packed fields.");
2468 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002469};
2470
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002471// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2472// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002473class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002474 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002475 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2476 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002477
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002478 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002479
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002480 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002481
2482 private:
2483 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2484};
2485
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002486// Represents dex's RETURN opcodes. A HReturn is a control flow
2487// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002488class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002489 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002490 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2491 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002492 SetRawInputAt(0, value);
2493 }
2494
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002495 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002496
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002497 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002498
2499 private:
2500 DISALLOW_COPY_AND_ASSIGN(HReturn);
2501};
2502
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002503class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002504 public:
2505 HPhi(ArenaAllocator* arena,
2506 uint32_t reg_number,
2507 size_t number_of_inputs,
2508 Primitive::Type type,
2509 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002510 : HVariableInputSizeInstruction(
2511 SideEffects::None(),
2512 dex_pc,
2513 arena,
2514 number_of_inputs,
2515 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002516 reg_number_(reg_number) {
2517 SetPackedField<TypeField>(ToPhiType(type));
2518 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2519 // Phis are constructed live and marked dead if conflicting or unused.
2520 // Individual steps of SsaBuilder should assume that if a phi has been
2521 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2522 SetPackedFlag<kFlagIsLive>(true);
2523 SetPackedFlag<kFlagCanBeNull>(true);
2524 }
2525
2526 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2527 static Primitive::Type ToPhiType(Primitive::Type type) {
2528 return Primitive::PrimitiveKind(type);
2529 }
2530
2531 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2532
David Brazdildee58d62016-04-07 09:54:26 +00002533 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2534 void SetType(Primitive::Type new_type) {
2535 // Make sure that only valid type changes occur. The following are allowed:
2536 // (1) int -> float/ref (primitive type propagation),
2537 // (2) long -> double (primitive type propagation).
2538 DCHECK(GetType() == new_type ||
2539 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2540 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2541 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2542 SetPackedField<TypeField>(new_type);
2543 }
2544
2545 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2546 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2547
2548 uint32_t GetRegNumber() const { return reg_number_; }
2549
2550 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2551 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2552 bool IsDead() const { return !IsLive(); }
2553 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2554
Vladimir Markoe9004912016-06-16 16:50:52 +01002555 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002556 return other != nullptr
2557 && other->IsPhi()
2558 && other->AsPhi()->GetBlock() == GetBlock()
2559 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2560 }
2561
2562 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2563 // An equivalent phi is a phi having the same dex register and type.
2564 // It assumes that phis with the same dex register are adjacent.
2565 HPhi* GetNextEquivalentPhiWithSameType() {
2566 HInstruction* next = GetNext();
2567 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2568 if (next->GetType() == GetType()) {
2569 return next->AsPhi();
2570 }
2571 next = next->GetNext();
2572 }
2573 return nullptr;
2574 }
2575
2576 DECLARE_INSTRUCTION(Phi);
2577
David Brazdildee58d62016-04-07 09:54:26 +00002578 private:
2579 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2580 static constexpr size_t kFieldTypeSize =
2581 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2582 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2583 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2584 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2585 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2586 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2587
David Brazdildee58d62016-04-07 09:54:26 +00002588 const uint32_t reg_number_;
2589
2590 DISALLOW_COPY_AND_ASSIGN(HPhi);
2591};
2592
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002593// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002594// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002595// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002596class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002597 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002598 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002599
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002600 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002601
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002602 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002603
2604 private:
2605 DISALLOW_COPY_AND_ASSIGN(HExit);
2606};
2607
2608// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002609class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002610 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002611 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002612
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002613 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002614
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002615 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002616 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002617 }
2618
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002619 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002620
2621 private:
2622 DISALLOW_COPY_AND_ASSIGN(HGoto);
2623};
2624
Roland Levillain9867bc72015-08-05 10:21:34 +01002625class HConstant : public HExpression<0> {
2626 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002627 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2628 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002629
2630 bool CanBeMoved() const OVERRIDE { return true; }
2631
Roland Levillain1a653882016-03-18 18:05:57 +00002632 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002633 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002634 // Is this constant 0 in the arithmetic sense?
2635 virtual bool IsArithmeticZero() const { return false; }
2636 // Is this constant a 0-bit pattern?
2637 virtual bool IsZeroBitPattern() const { return false; }
2638 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002639 virtual bool IsOne() const { return false; }
2640
David Brazdil77a48ae2015-09-15 12:34:04 +00002641 virtual uint64_t GetValueAsUint64() const = 0;
2642
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002643 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002644
2645 private:
2646 DISALLOW_COPY_AND_ASSIGN(HConstant);
2647};
2648
Vladimir Markofcb503c2016-05-18 12:48:17 +01002649class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002650 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002651 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002652 return true;
2653 }
2654
David Brazdil77a48ae2015-09-15 12:34:04 +00002655 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2656
Roland Levillain9867bc72015-08-05 10:21:34 +01002657 size_t ComputeHashCode() const OVERRIDE { return 0; }
2658
Roland Levillain1a653882016-03-18 18:05:57 +00002659 // The null constant representation is a 0-bit pattern.
2660 virtual bool IsZeroBitPattern() const { return true; }
2661
Roland Levillain9867bc72015-08-05 10:21:34 +01002662 DECLARE_INSTRUCTION(NullConstant);
2663
2664 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002665 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002666
2667 friend class HGraph;
2668 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2669};
2670
2671// Constants of the type int. Those can be from Dex instructions, or
2672// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002673class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002674 public:
2675 int32_t GetValue() const { return value_; }
2676
David Brazdil9f389d42015-10-01 14:32:56 +01002677 uint64_t GetValueAsUint64() const OVERRIDE {
2678 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2679 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002680
Vladimir Marko372f10e2016-05-17 16:30:10 +01002681 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002682 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002683 return other->AsIntConstant()->value_ == value_;
2684 }
2685
2686 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2687
2688 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002689 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2690 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002691 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2692
Roland Levillain1a653882016-03-18 18:05:57 +00002693 // Integer constants are used to encode Boolean values as well,
2694 // where 1 means true and 0 means false.
2695 bool IsTrue() const { return GetValue() == 1; }
2696 bool IsFalse() const { return GetValue() == 0; }
2697
Roland Levillain9867bc72015-08-05 10:21:34 +01002698 DECLARE_INSTRUCTION(IntConstant);
2699
2700 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002701 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2702 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2703 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2704 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002705
2706 const int32_t value_;
2707
2708 friend class HGraph;
2709 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2710 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2711 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2712};
2713
Vladimir Markofcb503c2016-05-18 12:48:17 +01002714class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002715 public:
2716 int64_t GetValue() const { return value_; }
2717
David Brazdil77a48ae2015-09-15 12:34:04 +00002718 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2719
Vladimir Marko372f10e2016-05-17 16:30:10 +01002720 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002721 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002722 return other->AsLongConstant()->value_ == value_;
2723 }
2724
2725 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2726
2727 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002728 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2729 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002730 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2731
2732 DECLARE_INSTRUCTION(LongConstant);
2733
2734 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002735 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2736 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002737
2738 const int64_t value_;
2739
2740 friend class HGraph;
2741 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2742};
Dave Allison20dfc792014-06-16 20:44:29 -07002743
Vladimir Markofcb503c2016-05-18 12:48:17 +01002744class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002745 public:
2746 float GetValue() const { return value_; }
2747
2748 uint64_t GetValueAsUint64() const OVERRIDE {
2749 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2750 }
2751
Vladimir Marko372f10e2016-05-17 16:30:10 +01002752 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002753 DCHECK(other->IsFloatConstant()) << other->DebugName();
2754 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2755 }
2756
2757 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2758
2759 bool IsMinusOne() const OVERRIDE {
2760 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2761 }
Roland Levillain1a653882016-03-18 18:05:57 +00002762 bool IsArithmeticZero() const OVERRIDE {
2763 return std::fpclassify(value_) == FP_ZERO;
2764 }
2765 bool IsArithmeticPositiveZero() const {
2766 return IsArithmeticZero() && !std::signbit(value_);
2767 }
2768 bool IsArithmeticNegativeZero() const {
2769 return IsArithmeticZero() && std::signbit(value_);
2770 }
2771 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002772 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002773 }
2774 bool IsOne() const OVERRIDE {
2775 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2776 }
2777 bool IsNaN() const {
2778 return std::isnan(value_);
2779 }
2780
2781 DECLARE_INSTRUCTION(FloatConstant);
2782
2783 private:
2784 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2785 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2786 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2787 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2788
2789 const float value_;
2790
2791 // Only the SsaBuilder and HGraph can create floating-point constants.
2792 friend class SsaBuilder;
2793 friend class HGraph;
2794 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2795};
2796
Vladimir Markofcb503c2016-05-18 12:48:17 +01002797class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002798 public:
2799 double GetValue() const { return value_; }
2800
2801 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2802
Vladimir Marko372f10e2016-05-17 16:30:10 +01002803 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002804 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2805 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2806 }
2807
2808 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2809
2810 bool IsMinusOne() const OVERRIDE {
2811 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2812 }
Roland Levillain1a653882016-03-18 18:05:57 +00002813 bool IsArithmeticZero() const OVERRIDE {
2814 return std::fpclassify(value_) == FP_ZERO;
2815 }
2816 bool IsArithmeticPositiveZero() const {
2817 return IsArithmeticZero() && !std::signbit(value_);
2818 }
2819 bool IsArithmeticNegativeZero() const {
2820 return IsArithmeticZero() && std::signbit(value_);
2821 }
2822 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002823 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002824 }
2825 bool IsOne() const OVERRIDE {
2826 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2827 }
2828 bool IsNaN() const {
2829 return std::isnan(value_);
2830 }
2831
2832 DECLARE_INSTRUCTION(DoubleConstant);
2833
2834 private:
2835 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2836 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2837 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2838 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2839
2840 const double value_;
2841
2842 // Only the SsaBuilder and HGraph can create floating-point constants.
2843 friend class SsaBuilder;
2844 friend class HGraph;
2845 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2846};
2847
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002848// Conditional branch. A block ending with an HIf instruction must have
2849// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002850class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002851 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002852 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2853 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002854 SetRawInputAt(0, input);
2855 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002856
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002857 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002858
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002859 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002860 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002861 }
2862
2863 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002864 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002865 }
2866
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002867 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002868
2869 private:
2870 DISALLOW_COPY_AND_ASSIGN(HIf);
2871};
2872
David Brazdilfc6a86a2015-06-26 10:33:45 +00002873
2874// Abstract instruction which marks the beginning and/or end of a try block and
2875// links it to the respective exception handlers. Behaves the same as a Goto in
2876// non-exceptional control flow.
2877// Normal-flow successor is stored at index zero, exception handlers under
2878// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002879class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002880 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002881 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002882 kEntry,
2883 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002884 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002885 };
2886
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002887 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002888 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2889 SetPackedField<BoundaryKindField>(kind);
2890 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002891
2892 bool IsControlFlow() const OVERRIDE { return true; }
2893
2894 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002895 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002896
David Brazdild26a4112015-11-10 11:07:31 +00002897 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2898 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2899 }
2900
David Brazdilfc6a86a2015-06-26 10:33:45 +00002901 // Returns whether `handler` is among its exception handlers (non-zero index
2902 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002903 bool HasExceptionHandler(const HBasicBlock& handler) const {
2904 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002905 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002906 }
2907
2908 // If not present already, adds `handler` to its block's list of exception
2909 // handlers.
2910 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002911 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002912 GetBlock()->AddSuccessor(handler);
2913 }
2914 }
2915
Vladimir Markoa1de9182016-02-25 11:37:38 +00002916 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2917 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002918
David Brazdilffee3d32015-07-06 11:48:53 +01002919 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2920
David Brazdilfc6a86a2015-06-26 10:33:45 +00002921 DECLARE_INSTRUCTION(TryBoundary);
2922
2923 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002924 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2925 static constexpr size_t kFieldBoundaryKindSize =
2926 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2927 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2928 kFieldBoundaryKind + kFieldBoundaryKindSize;
2929 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2930 "Too many packed fields.");
2931 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002932
2933 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2934};
2935
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002936// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002937class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002938 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002939 // We set CanTriggerGC to prevent any intermediate address to be live
2940 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002941 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002942 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002943 SetRawInputAt(0, cond);
2944 }
2945
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002946 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002947 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002948 return true;
2949 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002950 bool NeedsEnvironment() const OVERRIDE { return true; }
2951 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002952
2953 DECLARE_INSTRUCTION(Deoptimize);
2954
2955 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002956 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2957};
2958
Mingyao Yang063fc772016-08-02 11:02:54 -07002959// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
2960// The compiled code checks this flag value in a guard before devirtualized call and
2961// if it's true, starts to do deoptimization.
2962// It has a 4-byte slot on stack.
2963// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002964class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07002965 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002966 // CHA guards are only optimized in a separate pass and it has no side effects
2967 // with regard to other passes.
2968 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
2969 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07002970 }
2971
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002972 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
2973
2974 // We do all CHA guard elimination/motion in a single pass, after which there is no
2975 // further guard elimination/motion since a guard might have been used for justification
2976 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
2977 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07002978 bool CanBeMoved() const OVERRIDE { return false; }
2979
2980 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
2981
2982 private:
2983 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
2984};
2985
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002986// Represents the ArtMethod that was passed as a first argument to
2987// the method. It is used by instructions that depend on it, like
2988// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002989class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002990 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002991 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2992 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002993
2994 DECLARE_INSTRUCTION(CurrentMethod);
2995
2996 private:
2997 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2998};
2999
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003000// Fetches an ArtMethod from the virtual table or the interface method table
3001// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003002class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003003 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003004 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003005 kVTable,
3006 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003007 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003008 };
3009 HClassTableGet(HInstruction* cls,
3010 Primitive::Type type,
3011 TableKind kind,
3012 size_t index,
3013 uint32_t dex_pc)
3014 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003015 index_(index) {
3016 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003017 SetRawInputAt(0, cls);
3018 }
3019
3020 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003021 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003022 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003023 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003024 }
3025
Vladimir Markoa1de9182016-02-25 11:37:38 +00003026 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003027 size_t GetIndex() const { return index_; }
3028
3029 DECLARE_INSTRUCTION(ClassTableGet);
3030
3031 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003032 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3033 static constexpr size_t kFieldTableKindSize =
3034 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3035 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3036 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3037 "Too many packed fields.");
3038 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3039
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003040 // The index of the ArtMethod in the table.
3041 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003042
3043 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3044};
3045
Mark Mendellfe57faa2015-09-18 09:26:15 -04003046// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3047// have one successor for each entry in the switch table, and the final successor
3048// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003049class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003050 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003051 HPackedSwitch(int32_t start_value,
3052 uint32_t num_entries,
3053 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003054 uint32_t dex_pc = kNoDexPc)
3055 : HTemplateInstruction(SideEffects::None(), dex_pc),
3056 start_value_(start_value),
3057 num_entries_(num_entries) {
3058 SetRawInputAt(0, input);
3059 }
3060
3061 bool IsControlFlow() const OVERRIDE { return true; }
3062
3063 int32_t GetStartValue() const { return start_value_; }
3064
Vladimir Marko211c2112015-09-24 16:52:33 +01003065 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003066
3067 HBasicBlock* GetDefaultBlock() const {
3068 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003069 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003070 }
3071 DECLARE_INSTRUCTION(PackedSwitch);
3072
3073 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003074 const int32_t start_value_;
3075 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003076
3077 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3078};
3079
Roland Levillain88cb1752014-10-20 16:36:47 +01003080class HUnaryOperation : public HExpression<1> {
3081 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003082 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3083 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003084 SetRawInputAt(0, input);
3085 }
3086
3087 HInstruction* GetInput() const { return InputAt(0); }
3088 Primitive::Type GetResultType() const { return GetType(); }
3089
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003090 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003091 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003092 return true;
3093 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003094
Roland Levillain31dd3d62016-02-16 12:21:02 +00003095 // Try to statically evaluate `this` and return a HConstant
3096 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003097 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003098 HConstant* TryStaticEvaluation() const;
3099
3100 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003101 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3102 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003103 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3104 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003105
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003106 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003107
3108 private:
3109 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3110};
3111
Dave Allison20dfc792014-06-16 20:44:29 -07003112class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003113 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003114 HBinaryOperation(Primitive::Type result_type,
3115 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003116 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003117 SideEffects side_effects = SideEffects::None(),
3118 uint32_t dex_pc = kNoDexPc)
3119 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003120 SetRawInputAt(0, left);
3121 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003122 }
3123
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003124 HInstruction* GetLeft() const { return InputAt(0); }
3125 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003126 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003127
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003128 virtual bool IsCommutative() const { return false; }
3129
3130 // Put constant on the right.
3131 // Returns whether order is changed.
3132 bool OrderInputsWithConstantOnTheRight() {
3133 HInstruction* left = InputAt(0);
3134 HInstruction* right = InputAt(1);
3135 if (left->IsConstant() && !right->IsConstant()) {
3136 ReplaceInput(right, 0);
3137 ReplaceInput(left, 1);
3138 return true;
3139 }
3140 return false;
3141 }
3142
3143 // Order inputs by instruction id, but favor constant on the right side.
3144 // This helps GVN for commutative ops.
3145 void OrderInputs() {
3146 DCHECK(IsCommutative());
3147 HInstruction* left = InputAt(0);
3148 HInstruction* right = InputAt(1);
3149 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3150 return;
3151 }
3152 if (OrderInputsWithConstantOnTheRight()) {
3153 return;
3154 }
3155 // Order according to instruction id.
3156 if (left->GetId() > right->GetId()) {
3157 ReplaceInput(right, 0);
3158 ReplaceInput(left, 1);
3159 }
3160 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003161
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003162 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003163 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003164 return true;
3165 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003166
Roland Levillain31dd3d62016-02-16 12:21:02 +00003167 // Try to statically evaluate `this` and return a HConstant
3168 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003169 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003170 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003171
3172 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003173 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3174 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003175 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3176 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003177 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003178 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3179 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003180 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3181 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003182 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3183 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003184 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003185 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3186 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003187
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003188 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003189 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003190 HConstant* GetConstantRight() const;
3191
3192 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003193 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003194 HInstruction* GetLeastConstantLeft() const;
3195
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003196 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003197
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003198 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003199 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3200};
3201
Mark Mendellc4701932015-04-10 13:18:51 -04003202// The comparison bias applies for floating point operations and indicates how NaN
3203// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003204enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003205 kNoBias, // bias is not applicable (i.e. for long operation)
3206 kGtBias, // return 1 for NaN comparisons
3207 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003208 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003209};
3210
Roland Levillain31dd3d62016-02-16 12:21:02 +00003211std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3212
Dave Allison20dfc792014-06-16 20:44:29 -07003213class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003214 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003215 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003216 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3217 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3218 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003219
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003220 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003221 // `instruction`, and disregard moves in between.
3222 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003223
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003224 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003225
3226 virtual IfCondition GetCondition() const = 0;
3227
Mark Mendellc4701932015-04-10 13:18:51 -04003228 virtual IfCondition GetOppositeCondition() const = 0;
3229
Vladimir Markoa1de9182016-02-25 11:37:38 +00003230 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003231 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3232
Vladimir Markoa1de9182016-02-25 11:37:38 +00003233 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3234 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003235
Vladimir Marko372f10e2016-05-17 16:30:10 +01003236 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003237 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003238 }
3239
Roland Levillain4fa13f62015-07-06 18:11:54 +01003240 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003241 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003242 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003243 if (if_cond == kCondNE) {
3244 return true;
3245 } else if (if_cond == kCondEQ) {
3246 return false;
3247 }
3248 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003249 }
3250
3251 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003252 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003253 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003254 if (if_cond == kCondEQ) {
3255 return true;
3256 } else if (if_cond == kCondNE) {
3257 return false;
3258 }
3259 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003260 }
3261
Roland Levillain31dd3d62016-02-16 12:21:02 +00003262 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003263 // Needed if we merge a HCompare into a HCondition.
3264 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3265 static constexpr size_t kFieldComparisonBiasSize =
3266 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3267 static constexpr size_t kNumberOfConditionPackedBits =
3268 kFieldComparisonBias + kFieldComparisonBiasSize;
3269 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3270 using ComparisonBiasField =
3271 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3272
Roland Levillain31dd3d62016-02-16 12:21:02 +00003273 template <typename T>
3274 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3275
3276 template <typename T>
3277 int32_t CompareFP(T x, T y) const {
3278 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3279 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3280 // Handle the bias.
3281 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3282 }
3283
3284 // Return an integer constant containing the result of a condition evaluated at compile time.
3285 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3286 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3287 }
3288
Dave Allison20dfc792014-06-16 20:44:29 -07003289 private:
3290 DISALLOW_COPY_AND_ASSIGN(HCondition);
3291};
3292
3293// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003294class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003295 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003296 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3297 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003298
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003299 bool IsCommutative() const OVERRIDE { return true; }
3300
Vladimir Marko9e23df52015-11-10 17:14:35 +00003301 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3302 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003303 return MakeConstantCondition(true, GetDexPc());
3304 }
3305 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3306 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3307 }
3308 // In the following Evaluate methods, a HCompare instruction has
3309 // been merged into this HEqual instruction; evaluate it as
3310 // `Compare(x, y) == 0`.
3311 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3312 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3313 GetDexPc());
3314 }
3315 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3316 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3317 }
3318 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3319 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003320 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003321
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003322 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003323
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003324 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003325 return kCondEQ;
3326 }
3327
Mark Mendellc4701932015-04-10 13:18:51 -04003328 IfCondition GetOppositeCondition() const OVERRIDE {
3329 return kCondNE;
3330 }
3331
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003332 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003333 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003334
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003335 DISALLOW_COPY_AND_ASSIGN(HEqual);
3336};
3337
Vladimir Markofcb503c2016-05-18 12:48:17 +01003338class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003339 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003340 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3341 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003342
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003343 bool IsCommutative() const OVERRIDE { return true; }
3344
Vladimir Marko9e23df52015-11-10 17:14:35 +00003345 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3346 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003347 return MakeConstantCondition(false, GetDexPc());
3348 }
3349 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3350 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3351 }
3352 // In the following Evaluate methods, a HCompare instruction has
3353 // been merged into this HNotEqual instruction; evaluate it as
3354 // `Compare(x, y) != 0`.
3355 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3356 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3357 }
3358 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3359 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3360 }
3361 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3362 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003363 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003364
Dave Allison20dfc792014-06-16 20:44:29 -07003365 DECLARE_INSTRUCTION(NotEqual);
3366
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003367 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003368 return kCondNE;
3369 }
3370
Mark Mendellc4701932015-04-10 13:18:51 -04003371 IfCondition GetOppositeCondition() const OVERRIDE {
3372 return kCondEQ;
3373 }
3374
Dave Allison20dfc792014-06-16 20:44:29 -07003375 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003376 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003377
Dave Allison20dfc792014-06-16 20:44:29 -07003378 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3379};
3380
Vladimir Markofcb503c2016-05-18 12:48:17 +01003381class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003382 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003383 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3384 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003385
Roland Levillain9867bc72015-08-05 10:21:34 +01003386 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003387 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003388 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003389 // In the following Evaluate methods, a HCompare instruction has
3390 // been merged into this HLessThan instruction; evaluate it as
3391 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003392 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003393 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3394 }
3395 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3396 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3397 }
3398 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3399 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003400 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003401
Dave Allison20dfc792014-06-16 20:44:29 -07003402 DECLARE_INSTRUCTION(LessThan);
3403
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003404 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003405 return kCondLT;
3406 }
3407
Mark Mendellc4701932015-04-10 13:18:51 -04003408 IfCondition GetOppositeCondition() const OVERRIDE {
3409 return kCondGE;
3410 }
3411
Dave Allison20dfc792014-06-16 20:44:29 -07003412 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003413 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003414
Dave Allison20dfc792014-06-16 20:44:29 -07003415 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3416};
3417
Vladimir Markofcb503c2016-05-18 12:48:17 +01003418class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003419 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003420 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3421 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003422
Roland Levillain9867bc72015-08-05 10:21:34 +01003423 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003424 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003425 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003426 // In the following Evaluate methods, a HCompare instruction has
3427 // been merged into this HLessThanOrEqual instruction; evaluate it as
3428 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003429 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003430 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3431 }
3432 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3433 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3434 }
3435 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3436 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003437 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003438
Dave Allison20dfc792014-06-16 20:44:29 -07003439 DECLARE_INSTRUCTION(LessThanOrEqual);
3440
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003441 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003442 return kCondLE;
3443 }
3444
Mark Mendellc4701932015-04-10 13:18:51 -04003445 IfCondition GetOppositeCondition() const OVERRIDE {
3446 return kCondGT;
3447 }
3448
Dave Allison20dfc792014-06-16 20:44:29 -07003449 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003450 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003451
Dave Allison20dfc792014-06-16 20:44:29 -07003452 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3453};
3454
Vladimir Markofcb503c2016-05-18 12:48:17 +01003455class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003456 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003457 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3458 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003459
Roland Levillain9867bc72015-08-05 10:21:34 +01003460 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003461 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003462 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003463 // In the following Evaluate methods, a HCompare instruction has
3464 // been merged into this HGreaterThan instruction; evaluate it as
3465 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003466 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003467 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3468 }
3469 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3470 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3471 }
3472 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3473 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003474 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003475
Dave Allison20dfc792014-06-16 20:44:29 -07003476 DECLARE_INSTRUCTION(GreaterThan);
3477
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003478 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003479 return kCondGT;
3480 }
3481
Mark Mendellc4701932015-04-10 13:18:51 -04003482 IfCondition GetOppositeCondition() const OVERRIDE {
3483 return kCondLE;
3484 }
3485
Dave Allison20dfc792014-06-16 20:44:29 -07003486 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003487 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003488
Dave Allison20dfc792014-06-16 20:44:29 -07003489 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3490};
3491
Vladimir Markofcb503c2016-05-18 12:48:17 +01003492class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003493 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003494 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3495 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003496
Roland Levillain9867bc72015-08-05 10:21:34 +01003497 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003498 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003499 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003500 // In the following Evaluate methods, a HCompare instruction has
3501 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3502 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003503 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003504 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3505 }
3506 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3507 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3508 }
3509 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3510 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003511 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003512
Dave Allison20dfc792014-06-16 20:44:29 -07003513 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3514
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003515 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003516 return kCondGE;
3517 }
3518
Mark Mendellc4701932015-04-10 13:18:51 -04003519 IfCondition GetOppositeCondition() const OVERRIDE {
3520 return kCondLT;
3521 }
3522
Dave Allison20dfc792014-06-16 20:44:29 -07003523 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003524 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003525
Dave Allison20dfc792014-06-16 20:44:29 -07003526 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3527};
3528
Vladimir Markofcb503c2016-05-18 12:48:17 +01003529class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003530 public:
3531 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3532 : HCondition(first, second, dex_pc) {}
3533
3534 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003535 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003536 }
3537 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003538 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3539 }
3540 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3541 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3542 LOG(FATAL) << DebugName() << " is not defined for float values";
3543 UNREACHABLE();
3544 }
3545 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3546 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3547 LOG(FATAL) << DebugName() << " is not defined for double values";
3548 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003549 }
3550
3551 DECLARE_INSTRUCTION(Below);
3552
3553 IfCondition GetCondition() const OVERRIDE {
3554 return kCondB;
3555 }
3556
3557 IfCondition GetOppositeCondition() const OVERRIDE {
3558 return kCondAE;
3559 }
3560
3561 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003562 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003563 return MakeUnsigned(x) < MakeUnsigned(y);
3564 }
Aart Bike9f37602015-10-09 11:15:55 -07003565
3566 DISALLOW_COPY_AND_ASSIGN(HBelow);
3567};
3568
Vladimir Markofcb503c2016-05-18 12:48:17 +01003569class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003570 public:
3571 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3572 : HCondition(first, second, dex_pc) {}
3573
3574 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003575 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003576 }
3577 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003578 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3579 }
3580 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3581 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3582 LOG(FATAL) << DebugName() << " is not defined for float values";
3583 UNREACHABLE();
3584 }
3585 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3586 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3587 LOG(FATAL) << DebugName() << " is not defined for double values";
3588 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003589 }
3590
3591 DECLARE_INSTRUCTION(BelowOrEqual);
3592
3593 IfCondition GetCondition() const OVERRIDE {
3594 return kCondBE;
3595 }
3596
3597 IfCondition GetOppositeCondition() const OVERRIDE {
3598 return kCondA;
3599 }
3600
3601 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003602 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003603 return MakeUnsigned(x) <= MakeUnsigned(y);
3604 }
Aart Bike9f37602015-10-09 11:15:55 -07003605
3606 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3607};
3608
Vladimir Markofcb503c2016-05-18 12:48:17 +01003609class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003610 public:
3611 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3612 : HCondition(first, second, dex_pc) {}
3613
3614 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003615 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003616 }
3617 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003618 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3619 }
3620 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3621 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3622 LOG(FATAL) << DebugName() << " is not defined for float values";
3623 UNREACHABLE();
3624 }
3625 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3626 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3627 LOG(FATAL) << DebugName() << " is not defined for double values";
3628 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003629 }
3630
3631 DECLARE_INSTRUCTION(Above);
3632
3633 IfCondition GetCondition() const OVERRIDE {
3634 return kCondA;
3635 }
3636
3637 IfCondition GetOppositeCondition() const OVERRIDE {
3638 return kCondBE;
3639 }
3640
3641 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003642 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003643 return MakeUnsigned(x) > MakeUnsigned(y);
3644 }
Aart Bike9f37602015-10-09 11:15:55 -07003645
3646 DISALLOW_COPY_AND_ASSIGN(HAbove);
3647};
3648
Vladimir Markofcb503c2016-05-18 12:48:17 +01003649class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003650 public:
3651 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3652 : HCondition(first, second, dex_pc) {}
3653
3654 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003655 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003656 }
3657 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003658 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3659 }
3660 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3661 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3662 LOG(FATAL) << DebugName() << " is not defined for float values";
3663 UNREACHABLE();
3664 }
3665 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3666 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3667 LOG(FATAL) << DebugName() << " is not defined for double values";
3668 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003669 }
3670
3671 DECLARE_INSTRUCTION(AboveOrEqual);
3672
3673 IfCondition GetCondition() const OVERRIDE {
3674 return kCondAE;
3675 }
3676
3677 IfCondition GetOppositeCondition() const OVERRIDE {
3678 return kCondB;
3679 }
3680
3681 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003682 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003683 return MakeUnsigned(x) >= MakeUnsigned(y);
3684 }
Aart Bike9f37602015-10-09 11:15:55 -07003685
3686 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3687};
Dave Allison20dfc792014-06-16 20:44:29 -07003688
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003689// Instruction to check how two inputs compare to each other.
3690// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003691class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003692 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003693 // Note that `comparison_type` is the type of comparison performed
3694 // between the comparison's inputs, not the type of the instantiated
3695 // HCompare instruction (which is always Primitive::kPrimInt).
3696 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003697 HInstruction* first,
3698 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003699 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003700 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003701 : HBinaryOperation(Primitive::kPrimInt,
3702 first,
3703 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003704 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003705 dex_pc) {
3706 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003707 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3708 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003709 }
3710
Roland Levillain9867bc72015-08-05 10:21:34 +01003711 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003712 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3713
3714 template <typename T>
3715 int32_t ComputeFP(T x, T y) const {
3716 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3717 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3718 // Handle the bias.
3719 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3720 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003721
Roland Levillain9867bc72015-08-05 10:21:34 +01003722 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003723 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3724 // reach this code path when processing a freshly built HIR
3725 // graph. However HCompare integer instructions can be synthesized
3726 // by the instruction simplifier to implement IntegerCompare and
3727 // IntegerSignum intrinsics, so we have to handle this case.
3728 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003729 }
3730 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003731 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3732 }
3733 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3734 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3735 }
3736 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3737 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003738 }
3739
Vladimir Marko372f10e2016-05-17 16:30:10 +01003740 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003741 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003742 }
3743
Vladimir Markoa1de9182016-02-25 11:37:38 +00003744 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003745
Roland Levillain31dd3d62016-02-16 12:21:02 +00003746 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003747 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003748 bool IsGtBias() const {
3749 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003750 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003751 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003752
Roland Levillain1693a1f2016-03-15 14:57:31 +00003753 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3754 // Comparisons do not require a runtime call in any back end.
3755 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003756 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003757
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003758 DECLARE_INSTRUCTION(Compare);
3759
Roland Levillain31dd3d62016-02-16 12:21:02 +00003760 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003761 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3762 static constexpr size_t kFieldComparisonBiasSize =
3763 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3764 static constexpr size_t kNumberOfComparePackedBits =
3765 kFieldComparisonBias + kFieldComparisonBiasSize;
3766 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3767 using ComparisonBiasField =
3768 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3769
Roland Levillain31dd3d62016-02-16 12:21:02 +00003770 // Return an integer constant containing the result of a comparison evaluated at compile time.
3771 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3772 DCHECK(value == -1 || value == 0 || value == 1) << value;
3773 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3774 }
3775
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003776 private:
3777 DISALLOW_COPY_AND_ASSIGN(HCompare);
3778};
3779
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003780class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003781 public:
3782 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003783 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003784 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003785 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003786 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003787 bool finalizable,
3788 QuickEntrypointEnum entrypoint)
3789 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3790 type_index_(type_index),
3791 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003792 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003793 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003794 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003795 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003796 }
3797
Andreas Gampea5b09a62016-11-17 15:21:22 -08003798 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003799 const DexFile& GetDexFile() const { return dex_file_; }
3800
3801 // Calls runtime so needs an environment.
3802 bool NeedsEnvironment() const OVERRIDE { return true; }
3803
Mingyao Yang062157f2016-03-02 10:15:36 -08003804 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3805 bool CanThrow() const OVERRIDE { return true; }
3806
3807 // Needs to call into runtime to make sure it's instantiable/accessible.
3808 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003809
Vladimir Markoa1de9182016-02-25 11:37:38 +00003810 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003811
3812 bool CanBeNull() const OVERRIDE { return false; }
3813
3814 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3815
3816 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3817 entrypoint_ = entrypoint;
3818 }
3819
3820 bool IsStringAlloc() const;
3821
3822 DECLARE_INSTRUCTION(NewInstance);
3823
3824 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003825 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3826 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003827 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3828 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3829 "Too many packed fields.");
3830
Andreas Gampea5b09a62016-11-17 15:21:22 -08003831 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003832 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003833 QuickEntrypointEnum entrypoint_;
3834
3835 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3836};
3837
Agi Csaki05f20562015-08-19 14:58:14 -07003838enum IntrinsicNeedsEnvironmentOrCache {
3839 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3840 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003841};
3842
Aart Bik5d75afe2015-12-14 11:57:01 -08003843enum IntrinsicSideEffects {
3844 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3845 kReadSideEffects, // Intrinsic may read heap memory.
3846 kWriteSideEffects, // Intrinsic may write heap memory.
3847 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3848};
3849
3850enum IntrinsicExceptions {
3851 kNoThrow, // Intrinsic does not throw any exceptions.
3852 kCanThrow // Intrinsic may throw exceptions.
3853};
3854
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003855class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003856 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003857 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003858
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003859 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003860 SetRawInputAt(index, argument);
3861 }
3862
Roland Levillain3e3d7332015-04-28 11:00:54 +01003863 // Return the number of arguments. This number can be lower than
3864 // the number of inputs returned by InputCount(), as some invoke
3865 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3866 // inputs at the end of their list of inputs.
3867 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3868
Vladimir Markoa1de9182016-02-25 11:37:38 +00003869 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003870
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003871 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003872 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003873
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003874 InvokeType GetInvokeType() const {
3875 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003876 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003877
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003878 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003879 return intrinsic_;
3880 }
3881
Aart Bik5d75afe2015-12-14 11:57:01 -08003882 void SetIntrinsic(Intrinsics intrinsic,
3883 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3884 IntrinsicSideEffects side_effects,
3885 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003886
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003887 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003888 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003889 }
3890
Aart Bikff7d89c2016-11-07 08:49:28 -08003891 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
3892
Vladimir Markoa1de9182016-02-25 11:37:38 +00003893 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003894
Aart Bik71bf7b42016-11-16 10:17:46 -08003895 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003896
Vladimir Marko372f10e2016-05-17 16:30:10 +01003897 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003898 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3899 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003900
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003901 uint32_t* GetIntrinsicOptimizations() {
3902 return &intrinsic_optimizations_;
3903 }
3904
3905 const uint32_t* GetIntrinsicOptimizations() const {
3906 return &intrinsic_optimizations_;
3907 }
3908
3909 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3910
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003911 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3912
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003913 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003914
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003915 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003916 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3917 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003918 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3919 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003920 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003921 static constexpr size_t kFieldReturnTypeSize =
3922 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3923 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3924 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3925 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003926 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003927 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3928
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003929 HInvoke(ArenaAllocator* arena,
3930 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003931 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003932 Primitive::Type return_type,
3933 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003934 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003935 ArtMethod* resolved_method,
3936 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003937 : HVariableInputSizeInstruction(
3938 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
3939 dex_pc,
3940 arena,
3941 number_of_arguments + number_of_other_inputs,
3942 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003943 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003944 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003945 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003946 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003947 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003948 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003949 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003950 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003951 }
3952
Roland Levillain3e3d7332015-04-28 11:00:54 +01003953 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003954 ArtMethod* const resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003955 const uint32_t dex_method_index_;
3956 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003957
3958 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3959 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003960
3961 private:
3962 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3963};
3964
Vladimir Markofcb503c2016-05-18 12:48:17 +01003965class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003966 public:
3967 HInvokeUnresolved(ArenaAllocator* arena,
3968 uint32_t number_of_arguments,
3969 Primitive::Type return_type,
3970 uint32_t dex_pc,
3971 uint32_t dex_method_index,
3972 InvokeType invoke_type)
3973 : HInvoke(arena,
3974 number_of_arguments,
3975 0u /* number_of_other_inputs */,
3976 return_type,
3977 dex_pc,
3978 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003979 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003980 invoke_type) {
3981 }
3982
3983 DECLARE_INSTRUCTION(InvokeUnresolved);
3984
3985 private:
3986 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3987};
3988
Vladimir Markofcb503c2016-05-18 12:48:17 +01003989class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003990 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003991 // Requirements of this method call regarding the class
3992 // initialization (clinit) check of its declaring class.
3993 enum class ClinitCheckRequirement {
3994 kNone, // Class already initialized.
3995 kExplicit, // Static call having explicit clinit check as last input.
3996 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003997 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003998 };
3999
Vladimir Marko58155012015-08-19 12:49:41 +00004000 // Determines how to load the target ArtMethod*.
4001 enum class MethodLoadKind {
4002 // Use a String init ArtMethod* loaded from Thread entrypoints.
4003 kStringInit,
4004
4005 // Use the method's own ArtMethod* loaded by the register allocator.
4006 kRecursive,
4007
4008 // Use ArtMethod* at a known address, embed the direct address in the code.
4009 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4010 kDirectAddress,
4011
Vladimir Markoa1de9182016-02-25 11:37:38 +00004012 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004013 // Used when we need to use the dex cache, for example for invoke-static that
4014 // may cause class initialization (the entry may point to a resolution method),
4015 // and we know that we can access the dex cache arrays using a PC-relative load.
4016 kDexCachePcRelative,
4017
4018 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4019 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4020 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4021 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4022 kDexCacheViaMethod,
4023 };
4024
4025 // Determines the location of the code pointer.
4026 enum class CodePtrLocation {
4027 // Recursive call, use local PC-relative call instruction.
4028 kCallSelf,
4029
Vladimir Marko58155012015-08-19 12:49:41 +00004030 // Use code pointer from the ArtMethod*.
4031 // Used when we don't know the target code. This is also the last-resort-kind used when
4032 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4033 kCallArtMethod,
4034 };
4035
4036 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004037 MethodLoadKind method_load_kind;
4038 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004039 // The method load data holds
4040 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4041 // Note that there are multiple string init methods, each having its own offset.
4042 // - the method address for kDirectAddress
4043 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004044 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004045 };
4046
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004047 HInvokeStaticOrDirect(ArenaAllocator* arena,
4048 uint32_t number_of_arguments,
4049 Primitive::Type return_type,
4050 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004051 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004052 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004053 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004054 InvokeType invoke_type,
4055 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004056 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004057 : HInvoke(arena,
4058 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004059 // There is potentially one extra argument for the HCurrentMethod node, and
4060 // potentially one other if the clinit check is explicit, and potentially
4061 // one other if the method is a string factory.
4062 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004063 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004064 return_type,
4065 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004066 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004067 resolved_method,
4068 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004069 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004070 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004071 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4072 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004073
Vladimir Markodc151b22015-10-15 18:02:30 +01004074 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004075 bool had_current_method_input = HasCurrentMethodInput();
4076 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4077
4078 // Using the current method is the default and once we find a better
4079 // method load kind, we should not go back to using the current method.
4080 DCHECK(had_current_method_input || !needs_current_method_input);
4081
4082 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004083 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4084 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004085 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004086 dispatch_info_ = dispatch_info;
4087 }
4088
Vladimir Markoc53c0792015-11-19 15:48:33 +00004089 void AddSpecialInput(HInstruction* input) {
4090 // We allow only one special input.
4091 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4092 DCHECK(InputCount() == GetSpecialInputIndex() ||
4093 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4094 InsertInputAt(GetSpecialInputIndex(), input);
4095 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004096
Vladimir Marko372f10e2016-05-17 16:30:10 +01004097 using HInstruction::GetInputRecords; // Keep the const version visible.
4098 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4099 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4100 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4101 DCHECK(!input_records.empty());
4102 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4103 HInstruction* last_input = input_records.back().GetInstruction();
4104 // Note: `last_input` may be null during arguments setup.
4105 if (last_input != nullptr) {
4106 // `last_input` is the last input of a static invoke marked as having
4107 // an explicit clinit check. It must either be:
4108 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4109 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4110 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4111 }
4112 }
4113 return input_records;
4114 }
4115
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004116 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004117 // We access the method via the dex cache so we can't do an implicit null check.
4118 // TODO: for intrinsics we can generate implicit null checks.
4119 return false;
4120 }
4121
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004122 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004123 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004124 }
4125
Vladimir Markoc53c0792015-11-19 15:48:33 +00004126 // Get the index of the special input, if any.
4127 //
David Brazdil6de19382016-01-08 17:37:10 +00004128 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4129 // method pointer; otherwise there may be one platform-specific special input,
4130 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004131 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004132 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004133
Vladimir Marko58155012015-08-19 12:49:41 +00004134 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4135 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4136 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004137 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004138 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004139 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004140 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004141 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4142 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004143 bool HasCurrentMethodInput() const {
4144 // This function can be called only after the invoke has been fully initialized by the builder.
4145 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004146 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004147 return true;
4148 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004149 DCHECK(InputCount() == GetSpecialInputIndex() ||
4150 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004151 return false;
4152 }
4153 }
Vladimir Marko58155012015-08-19 12:49:41 +00004154
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004155 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004156 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004157 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004158 }
4159
4160 uint64_t GetMethodAddress() const {
4161 DCHECK(HasMethodAddress());
4162 return dispatch_info_.method_load_data;
4163 }
4164
4165 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004166 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004167 return dispatch_info_.method_load_data;
4168 }
4169
Vladimir Markoa1de9182016-02-25 11:37:38 +00004170 ClinitCheckRequirement GetClinitCheckRequirement() const {
4171 return GetPackedField<ClinitCheckRequirementField>();
4172 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004173
Roland Levillain4c0eb422015-04-24 16:43:49 +01004174 // Is this instruction a call to a static method?
4175 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004176 return GetInvokeType() == kStatic;
4177 }
4178
4179 MethodReference GetTargetMethod() const {
4180 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004181 }
4182
Vladimir Markofbb184a2015-11-13 14:47:00 +00004183 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4184 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4185 // instruction; only relevant for static calls with explicit clinit check.
4186 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004187 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004188 size_t last_input_index = inputs_.size() - 1u;
4189 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004190 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004191 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004192 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004193 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004194 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004195 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004196 }
4197
4198 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004199 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004200 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004201 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004202 }
4203
4204 // Is this a call to a static method whose declaring class has an
4205 // implicit intialization check requirement?
4206 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004207 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004208 }
4209
Vladimir Markob554b5a2015-11-06 12:57:55 +00004210 // Does this method load kind need the current method as an input?
4211 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4212 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4213 }
4214
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004215 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004216
4217 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004218 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004219 static constexpr size_t kFieldClinitCheckRequirementSize =
4220 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4221 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4222 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4223 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4224 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004225 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4226 kFieldClinitCheckRequirement,
4227 kFieldClinitCheckRequirementSize>;
4228
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004229 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004230 MethodReference target_method_;
4231 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004232
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004233 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004234};
Vladimir Markof64242a2015-12-01 14:58:23 +00004235std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004236std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004237
Vladimir Markofcb503c2016-05-18 12:48:17 +01004238class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004239 public:
4240 HInvokeVirtual(ArenaAllocator* arena,
4241 uint32_t number_of_arguments,
4242 Primitive::Type return_type,
4243 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004244 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004245 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004246 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004247 : HInvoke(arena,
4248 number_of_arguments,
4249 0u,
4250 return_type,
4251 dex_pc,
4252 dex_method_index,
4253 resolved_method,
4254 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004255 vtable_index_(vtable_index) {}
4256
Aart Bik71bf7b42016-11-16 10:17:46 -08004257 bool CanBeNull() const OVERRIDE {
4258 switch (GetIntrinsic()) {
4259 case Intrinsics::kThreadCurrentThread:
4260 case Intrinsics::kStringBufferAppend:
4261 case Intrinsics::kStringBufferToString:
4262 case Intrinsics::kStringBuilderAppend:
4263 case Intrinsics::kStringBuilderToString:
4264 return false;
4265 default:
4266 return HInvoke::CanBeNull();
4267 }
4268 }
4269
Calin Juravle641547a2015-04-21 22:08:51 +01004270 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004271 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004272 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004273 }
4274
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004275 uint32_t GetVTableIndex() const { return vtable_index_; }
4276
4277 DECLARE_INSTRUCTION(InvokeVirtual);
4278
4279 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004280 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004281 const uint32_t vtable_index_;
4282
4283 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4284};
4285
Vladimir Markofcb503c2016-05-18 12:48:17 +01004286class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004287 public:
4288 HInvokeInterface(ArenaAllocator* arena,
4289 uint32_t number_of_arguments,
4290 Primitive::Type return_type,
4291 uint32_t dex_pc,
4292 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004293 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004294 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004295 : HInvoke(arena,
4296 number_of_arguments,
4297 0u,
4298 return_type,
4299 dex_pc,
4300 dex_method_index,
4301 resolved_method,
4302 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004303 imt_index_(imt_index) {}
4304
Calin Juravle641547a2015-04-21 22:08:51 +01004305 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004306 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004307 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004308 }
4309
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004310 uint32_t GetImtIndex() const { return imt_index_; }
4311 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4312
4313 DECLARE_INSTRUCTION(InvokeInterface);
4314
4315 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004316 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004317 const uint32_t imt_index_;
4318
4319 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4320};
4321
Vladimir Markofcb503c2016-05-18 12:48:17 +01004322class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004323 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004324 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004325 : HUnaryOperation(result_type, input, dex_pc) {
4326 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4327 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004328
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004329 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004330
4331 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004332 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004333 }
4334 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004335 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004336 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004337 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4338 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4339 }
4340 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4341 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4342 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004343
Roland Levillain88cb1752014-10-20 16:36:47 +01004344 DECLARE_INSTRUCTION(Neg);
4345
4346 private:
4347 DISALLOW_COPY_AND_ASSIGN(HNeg);
4348};
4349
Vladimir Markofcb503c2016-05-18 12:48:17 +01004350class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004351 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004352 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004353 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004354 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004355 dex::TypeIndex type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004356 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004357 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004358 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004359 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004360 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004361 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004362 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004363 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004364 }
4365
Andreas Gampea5b09a62016-11-17 15:21:22 -08004366 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004367 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004368
4369 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004370 bool NeedsEnvironment() const OVERRIDE { return true; }
4371
Mingyao Yang0c365e62015-03-31 15:09:29 -07004372 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4373 bool CanThrow() const OVERRIDE { return true; }
4374
Calin Juravle10e244f2015-01-26 18:54:32 +00004375 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004376
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004377 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4378
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004379 DECLARE_INSTRUCTION(NewArray);
4380
4381 private:
Andreas Gampea5b09a62016-11-17 15:21:22 -08004382 const dex::TypeIndex type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004383 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004384 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004385
4386 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4387};
4388
Vladimir Markofcb503c2016-05-18 12:48:17 +01004389class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004390 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004391 HAdd(Primitive::Type result_type,
4392 HInstruction* left,
4393 HInstruction* right,
4394 uint32_t dex_pc = kNoDexPc)
4395 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004396
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004397 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004398
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004399 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004400
4401 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004402 return GetBlock()->GetGraph()->GetIntConstant(
4403 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004404 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004405 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004406 return GetBlock()->GetGraph()->GetLongConstant(
4407 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004408 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004409 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4410 return GetBlock()->GetGraph()->GetFloatConstant(
4411 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4412 }
4413 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4414 return GetBlock()->GetGraph()->GetDoubleConstant(
4415 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4416 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004417
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004418 DECLARE_INSTRUCTION(Add);
4419
4420 private:
4421 DISALLOW_COPY_AND_ASSIGN(HAdd);
4422};
4423
Vladimir Markofcb503c2016-05-18 12:48:17 +01004424class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004425 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004426 HSub(Primitive::Type result_type,
4427 HInstruction* left,
4428 HInstruction* right,
4429 uint32_t dex_pc = kNoDexPc)
4430 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004431
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004432 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004433
4434 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004435 return GetBlock()->GetGraph()->GetIntConstant(
4436 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004437 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004438 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004439 return GetBlock()->GetGraph()->GetLongConstant(
4440 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004441 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004442 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4443 return GetBlock()->GetGraph()->GetFloatConstant(
4444 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4445 }
4446 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4447 return GetBlock()->GetGraph()->GetDoubleConstant(
4448 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4449 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004450
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004451 DECLARE_INSTRUCTION(Sub);
4452
4453 private:
4454 DISALLOW_COPY_AND_ASSIGN(HSub);
4455};
4456
Vladimir Markofcb503c2016-05-18 12:48:17 +01004457class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004458 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004459 HMul(Primitive::Type result_type,
4460 HInstruction* left,
4461 HInstruction* right,
4462 uint32_t dex_pc = kNoDexPc)
4463 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004464
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004465 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004466
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004467 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004468
4469 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004470 return GetBlock()->GetGraph()->GetIntConstant(
4471 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004472 }
4473 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004474 return GetBlock()->GetGraph()->GetLongConstant(
4475 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004476 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004477 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4478 return GetBlock()->GetGraph()->GetFloatConstant(
4479 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4480 }
4481 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4482 return GetBlock()->GetGraph()->GetDoubleConstant(
4483 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4484 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004485
4486 DECLARE_INSTRUCTION(Mul);
4487
4488 private:
4489 DISALLOW_COPY_AND_ASSIGN(HMul);
4490};
4491
Vladimir Markofcb503c2016-05-18 12:48:17 +01004492class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004493 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004494 HDiv(Primitive::Type result_type,
4495 HInstruction* left,
4496 HInstruction* right,
4497 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004498 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004499
Roland Levillain9867bc72015-08-05 10:21:34 +01004500 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004501 T ComputeIntegral(T x, T y) const {
4502 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004503 // Our graph structure ensures we never have 0 for `y` during
4504 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004505 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004506 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004507 return (y == -1) ? -x : x / y;
4508 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004509
Roland Levillain31dd3d62016-02-16 12:21:02 +00004510 template <typename T>
4511 T ComputeFP(T x, T y) const {
4512 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4513 return x / y;
4514 }
4515
Roland Levillain9867bc72015-08-05 10:21:34 +01004516 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004517 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004518 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004519 }
4520 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004521 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004522 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4523 }
4524 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4525 return GetBlock()->GetGraph()->GetFloatConstant(
4526 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4527 }
4528 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4529 return GetBlock()->GetGraph()->GetDoubleConstant(
4530 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004531 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004532
4533 DECLARE_INSTRUCTION(Div);
4534
4535 private:
4536 DISALLOW_COPY_AND_ASSIGN(HDiv);
4537};
4538
Vladimir Markofcb503c2016-05-18 12:48:17 +01004539class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004540 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004541 HRem(Primitive::Type result_type,
4542 HInstruction* left,
4543 HInstruction* right,
4544 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004545 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004546
Roland Levillain9867bc72015-08-05 10:21:34 +01004547 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004548 T ComputeIntegral(T x, T y) const {
4549 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004550 // Our graph structure ensures we never have 0 for `y` during
4551 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004552 DCHECK_NE(y, 0);
4553 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4554 return (y == -1) ? 0 : x % y;
4555 }
4556
Roland Levillain31dd3d62016-02-16 12:21:02 +00004557 template <typename T>
4558 T ComputeFP(T x, T y) const {
4559 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4560 return std::fmod(x, y);
4561 }
4562
Roland Levillain9867bc72015-08-05 10:21:34 +01004563 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004564 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004565 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004566 }
4567 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004568 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004569 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004570 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004571 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4572 return GetBlock()->GetGraph()->GetFloatConstant(
4573 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4574 }
4575 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4576 return GetBlock()->GetGraph()->GetDoubleConstant(
4577 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4578 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004579
4580 DECLARE_INSTRUCTION(Rem);
4581
4582 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004583 DISALLOW_COPY_AND_ASSIGN(HRem);
4584};
4585
Vladimir Markofcb503c2016-05-18 12:48:17 +01004586class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004587 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004588 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4589 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004590 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004591 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004592 SetRawInputAt(0, value);
4593 }
4594
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004595 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4596
Calin Juravled0d48522014-11-04 16:40:20 +00004597 bool CanBeMoved() const OVERRIDE { return true; }
4598
Vladimir Marko372f10e2016-05-17 16:30:10 +01004599 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004600 return true;
4601 }
4602
4603 bool NeedsEnvironment() const OVERRIDE { return true; }
4604 bool CanThrow() const OVERRIDE { return true; }
4605
Calin Juravled0d48522014-11-04 16:40:20 +00004606 DECLARE_INSTRUCTION(DivZeroCheck);
4607
4608 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004609 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4610};
4611
Vladimir Markofcb503c2016-05-18 12:48:17 +01004612class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004613 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004614 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004615 HInstruction* value,
4616 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004617 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004618 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4619 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4620 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004621 }
4622
Roland Levillain5b5b9312016-03-22 14:57:31 +00004623 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004624 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004625 return value << (distance & max_shift_distance);
4626 }
4627
4628 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004629 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004630 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004631 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004632 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004633 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004634 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004635 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004636 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4637 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4638 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4639 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004640 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004641 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4642 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004643 LOG(FATAL) << DebugName() << " is not defined for float values";
4644 UNREACHABLE();
4645 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004646 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4647 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004648 LOG(FATAL) << DebugName() << " is not defined for double values";
4649 UNREACHABLE();
4650 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004651
4652 DECLARE_INSTRUCTION(Shl);
4653
4654 private:
4655 DISALLOW_COPY_AND_ASSIGN(HShl);
4656};
4657
Vladimir Markofcb503c2016-05-18 12:48:17 +01004658class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004659 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004660 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004661 HInstruction* value,
4662 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004663 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004664 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4665 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4666 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004667 }
4668
Roland Levillain5b5b9312016-03-22 14:57:31 +00004669 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004670 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004671 return value >> (distance & max_shift_distance);
4672 }
4673
4674 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004675 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004676 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004677 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004678 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004679 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004680 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004681 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004682 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4683 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4684 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4685 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004686 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004687 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4688 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004689 LOG(FATAL) << DebugName() << " is not defined for float values";
4690 UNREACHABLE();
4691 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004692 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4693 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004694 LOG(FATAL) << DebugName() << " is not defined for double values";
4695 UNREACHABLE();
4696 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004697
4698 DECLARE_INSTRUCTION(Shr);
4699
4700 private:
4701 DISALLOW_COPY_AND_ASSIGN(HShr);
4702};
4703
Vladimir Markofcb503c2016-05-18 12:48:17 +01004704class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004705 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004706 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004707 HInstruction* value,
4708 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004709 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004710 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4711 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4712 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004713 }
4714
Roland Levillain5b5b9312016-03-22 14:57:31 +00004715 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004716 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004717 typedef typename std::make_unsigned<T>::type V;
4718 V ux = static_cast<V>(value);
4719 return static_cast<T>(ux >> (distance & max_shift_distance));
4720 }
4721
4722 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004723 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004724 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004725 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004726 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004727 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004728 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004729 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004730 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4731 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4732 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4733 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004734 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004735 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4736 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004737 LOG(FATAL) << DebugName() << " is not defined for float values";
4738 UNREACHABLE();
4739 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004740 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4741 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004742 LOG(FATAL) << DebugName() << " is not defined for double values";
4743 UNREACHABLE();
4744 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004745
4746 DECLARE_INSTRUCTION(UShr);
4747
4748 private:
4749 DISALLOW_COPY_AND_ASSIGN(HUShr);
4750};
4751
Vladimir Markofcb503c2016-05-18 12:48:17 +01004752class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004753 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004754 HAnd(Primitive::Type result_type,
4755 HInstruction* left,
4756 HInstruction* right,
4757 uint32_t dex_pc = kNoDexPc)
4758 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004759
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004760 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004761
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004762 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004763
4764 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004765 return GetBlock()->GetGraph()->GetIntConstant(
4766 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004767 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004768 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004769 return GetBlock()->GetGraph()->GetLongConstant(
4770 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004771 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004772 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4773 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4774 LOG(FATAL) << DebugName() << " is not defined for float values";
4775 UNREACHABLE();
4776 }
4777 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4778 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4779 LOG(FATAL) << DebugName() << " is not defined for double values";
4780 UNREACHABLE();
4781 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004782
4783 DECLARE_INSTRUCTION(And);
4784
4785 private:
4786 DISALLOW_COPY_AND_ASSIGN(HAnd);
4787};
4788
Vladimir Markofcb503c2016-05-18 12:48:17 +01004789class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004790 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004791 HOr(Primitive::Type result_type,
4792 HInstruction* left,
4793 HInstruction* right,
4794 uint32_t dex_pc = kNoDexPc)
4795 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004796
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004797 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004798
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004799 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004800
4801 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004802 return GetBlock()->GetGraph()->GetIntConstant(
4803 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004804 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004805 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004806 return GetBlock()->GetGraph()->GetLongConstant(
4807 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004808 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004809 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4810 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4811 LOG(FATAL) << DebugName() << " is not defined for float values";
4812 UNREACHABLE();
4813 }
4814 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4815 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4816 LOG(FATAL) << DebugName() << " is not defined for double values";
4817 UNREACHABLE();
4818 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004819
4820 DECLARE_INSTRUCTION(Or);
4821
4822 private:
4823 DISALLOW_COPY_AND_ASSIGN(HOr);
4824};
4825
Vladimir Markofcb503c2016-05-18 12:48:17 +01004826class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004827 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004828 HXor(Primitive::Type result_type,
4829 HInstruction* left,
4830 HInstruction* right,
4831 uint32_t dex_pc = kNoDexPc)
4832 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004833
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004834 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004835
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004836 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004837
4838 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004839 return GetBlock()->GetGraph()->GetIntConstant(
4840 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004841 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004842 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004843 return GetBlock()->GetGraph()->GetLongConstant(
4844 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004845 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004846 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4847 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4848 LOG(FATAL) << DebugName() << " is not defined for float values";
4849 UNREACHABLE();
4850 }
4851 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4852 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4853 LOG(FATAL) << DebugName() << " is not defined for double values";
4854 UNREACHABLE();
4855 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004856
4857 DECLARE_INSTRUCTION(Xor);
4858
4859 private:
4860 DISALLOW_COPY_AND_ASSIGN(HXor);
4861};
4862
Vladimir Markofcb503c2016-05-18 12:48:17 +01004863class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004864 public:
4865 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004866 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004867 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4868 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004869 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004870
Roland Levillain5b5b9312016-03-22 14:57:31 +00004871 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004872 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004873 typedef typename std::make_unsigned<T>::type V;
4874 V ux = static_cast<V>(value);
4875 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004876 return static_cast<T>(ux);
4877 } else {
4878 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004879 return static_cast<T>(ux >> (distance & max_shift_value)) |
4880 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004881 }
4882 }
4883
Roland Levillain5b5b9312016-03-22 14:57:31 +00004884 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004885 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004886 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004887 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004888 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004889 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004890 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004891 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004892 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4893 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4894 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4895 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004896 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004897 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4898 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004899 LOG(FATAL) << DebugName() << " is not defined for float values";
4900 UNREACHABLE();
4901 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004902 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4903 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004904 LOG(FATAL) << DebugName() << " is not defined for double values";
4905 UNREACHABLE();
4906 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004907
4908 DECLARE_INSTRUCTION(Ror);
4909
4910 private:
4911 DISALLOW_COPY_AND_ASSIGN(HRor);
4912};
4913
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004914// The value of a parameter in this method. Its location depends on
4915// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004916class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004917 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004918 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004919 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004920 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004921 Primitive::Type parameter_type,
4922 bool is_this = false)
4923 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004924 dex_file_(dex_file),
4925 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004926 index_(index) {
4927 SetPackedFlag<kFlagIsThis>(is_this);
4928 SetPackedFlag<kFlagCanBeNull>(!is_this);
4929 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004930
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004931 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08004932 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004933 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004934 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004935
Vladimir Markoa1de9182016-02-25 11:37:38 +00004936 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4937 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004938
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004939 DECLARE_INSTRUCTION(ParameterValue);
4940
4941 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004942 // Whether or not the parameter value corresponds to 'this' argument.
4943 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4944 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4945 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4946 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4947 "Too many packed fields.");
4948
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004949 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004950 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004951 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004952 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004953 const uint8_t index_;
4954
4955 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4956};
4957
Vladimir Markofcb503c2016-05-18 12:48:17 +01004958class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004959 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004960 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4961 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004962
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004963 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004964 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004965 return true;
4966 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004967
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004968 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004969
4970 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004971 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004972 }
4973 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004974 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004975 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004976 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4977 LOG(FATAL) << DebugName() << " is not defined for float values";
4978 UNREACHABLE();
4979 }
4980 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4981 LOG(FATAL) << DebugName() << " is not defined for double values";
4982 UNREACHABLE();
4983 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004984
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004985 DECLARE_INSTRUCTION(Not);
4986
4987 private:
4988 DISALLOW_COPY_AND_ASSIGN(HNot);
4989};
4990
Vladimir Markofcb503c2016-05-18 12:48:17 +01004991class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004992 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004993 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4994 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004995
4996 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004997 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004998 return true;
4999 }
5000
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005001 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005002 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005003 return !x;
5004 }
5005
Roland Levillain9867bc72015-08-05 10:21:34 +01005006 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005007 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005008 }
5009 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5010 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005011 UNREACHABLE();
5012 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005013 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5014 LOG(FATAL) << DebugName() << " is not defined for float values";
5015 UNREACHABLE();
5016 }
5017 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5018 LOG(FATAL) << DebugName() << " is not defined for double values";
5019 UNREACHABLE();
5020 }
David Brazdil66d126e2015-04-03 16:02:44 +01005021
5022 DECLARE_INSTRUCTION(BooleanNot);
5023
5024 private:
5025 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5026};
5027
Vladimir Markofcb503c2016-05-18 12:48:17 +01005028class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005029 public:
5030 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005031 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005032 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005033 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005034 // Invariant: We should never generate a conversion to a Boolean value.
5035 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005036 }
5037
5038 HInstruction* GetInput() const { return InputAt(0); }
5039 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5040 Primitive::Type GetResultType() const { return GetType(); }
5041
5042 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005043 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5044 return true;
5045 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005046
Mark Mendelle82549b2015-05-06 10:55:34 -04005047 // Try to statically evaluate the conversion and return a HConstant
5048 // containing the result. If the input cannot be converted, return nullptr.
5049 HConstant* TryStaticEvaluation() const;
5050
Roland Levillaindff1f282014-11-05 14:15:05 +00005051 DECLARE_INSTRUCTION(TypeConversion);
5052
5053 private:
5054 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5055};
5056
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005057static constexpr uint32_t kNoRegNumber = -1;
5058
Vladimir Markofcb503c2016-05-18 12:48:17 +01005059class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005060 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005061 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5062 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005063 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005064 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005065 SetRawInputAt(0, value);
5066 }
5067
Calin Juravle10e244f2015-01-26 18:54:32 +00005068 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005069 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005070 return true;
5071 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005072
Calin Juravle10e244f2015-01-26 18:54:32 +00005073 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005074
Calin Juravle10e244f2015-01-26 18:54:32 +00005075 bool CanThrow() const OVERRIDE { return true; }
5076
5077 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005078
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005079
5080 DECLARE_INSTRUCTION(NullCheck);
5081
5082 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005083 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5084};
5085
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005086// Embeds an ArtField and all the information required by the compiler. We cache
5087// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005088class FieldInfo : public ValueObject {
5089 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005090 FieldInfo(ArtField* field,
5091 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005092 Primitive::Type field_type,
5093 bool is_volatile,
5094 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005095 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005096 const DexFile& dex_file)
5097 : field_(field),
5098 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005099 field_type_(field_type),
5100 is_volatile_(is_volatile),
5101 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005102 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005103 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005104
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005105 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005106 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005107 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005108 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005109 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005110 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005111 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005112
5113 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005114 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005115 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005116 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005117 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005118 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005119 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005120 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005121};
5122
Vladimir Markofcb503c2016-05-18 12:48:17 +01005123class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005124 public:
5125 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005126 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005127 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005128 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005129 bool is_volatile,
5130 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005131 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005132 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005133 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005134 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005135 field_info_(field,
5136 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005137 field_type,
5138 is_volatile,
5139 field_idx,
5140 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005141 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005142 SetRawInputAt(0, value);
5143 }
5144
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005145 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005146
Vladimir Marko372f10e2016-05-17 16:30:10 +01005147 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5148 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005149 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005150 }
5151
Calin Juravle641547a2015-04-21 22:08:51 +01005152 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005153 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005154 }
5155
5156 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005157 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5158 }
5159
Calin Juravle52c48962014-12-16 17:02:57 +00005160 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005161 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005162 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005163 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005164
5165 DECLARE_INSTRUCTION(InstanceFieldGet);
5166
5167 private:
5168 const FieldInfo field_info_;
5169
5170 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5171};
5172
Vladimir Markofcb503c2016-05-18 12:48:17 +01005173class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005174 public:
5175 HInstanceFieldSet(HInstruction* object,
5176 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005177 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005178 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005179 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005180 bool is_volatile,
5181 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005182 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005183 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005184 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005185 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005186 field_info_(field,
5187 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005188 field_type,
5189 is_volatile,
5190 field_idx,
5191 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005192 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005193 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005194 SetRawInputAt(0, object);
5195 SetRawInputAt(1, value);
5196 }
5197
Calin Juravle641547a2015-04-21 22:08:51 +01005198 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005199 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005200 }
5201
Calin Juravle52c48962014-12-16 17:02:57 +00005202 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005203 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005204 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005205 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005206 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005207 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5208 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005209
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005210 DECLARE_INSTRUCTION(InstanceFieldSet);
5211
5212 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005213 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5214 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5215 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5216 "Too many packed fields.");
5217
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005218 const FieldInfo field_info_;
5219
5220 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5221};
5222
Vladimir Markofcb503c2016-05-18 12:48:17 +01005223class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005224 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005225 HArrayGet(HInstruction* array,
5226 HInstruction* index,
5227 Primitive::Type type,
5228 uint32_t dex_pc,
5229 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005230 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005231 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005232 SetRawInputAt(0, array);
5233 SetRawInputAt(1, index);
5234 }
5235
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005236 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005237 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005238 return true;
5239 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005240 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005241 // TODO: We can be smarter here.
5242 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5243 // which generates the implicit null check. There are cases when these can be removed
5244 // to produce better code. If we ever add optimizations to do so we should allow an
5245 // implicit check here (as long as the address falls in the first page).
5246 return false;
5247 }
5248
David Brazdil4833f5a2015-12-16 10:37:39 +00005249 bool IsEquivalentOf(HArrayGet* other) const {
5250 bool result = (GetDexPc() == other->GetDexPc());
5251 if (kIsDebugBuild && result) {
5252 DCHECK_EQ(GetBlock(), other->GetBlock());
5253 DCHECK_EQ(GetArray(), other->GetArray());
5254 DCHECK_EQ(GetIndex(), other->GetIndex());
5255 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005256 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005257 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005258 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5259 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005260 }
5261 }
5262 return result;
5263 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005264
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005265 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5266
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005267 HInstruction* GetArray() const { return InputAt(0); }
5268 HInstruction* GetIndex() const { return InputAt(1); }
5269
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005270 DECLARE_INSTRUCTION(ArrayGet);
5271
5272 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005273 // We treat a String as an array, creating the HArrayGet from String.charAt()
5274 // intrinsic in the instruction simplifier. We can always determine whether
5275 // a particular HArrayGet is actually a String.charAt() by looking at the type
5276 // of the input but that requires holding the mutator lock, so we prefer to use
5277 // a flag, so that code generators don't need to do the locking.
5278 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5279 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5280 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5281 "Too many packed fields.");
5282
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005283 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5284};
5285
Vladimir Markofcb503c2016-05-18 12:48:17 +01005286class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005287 public:
5288 HArraySet(HInstruction* array,
5289 HInstruction* index,
5290 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005291 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005292 uint32_t dex_pc)
5293 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005294 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5295 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5296 SetPackedFlag<kFlagValueCanBeNull>(true);
5297 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005298 SetRawInputAt(0, array);
5299 SetRawInputAt(1, index);
5300 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005301 // Make a best guess now, may be refined during SSA building.
5302 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005303 }
5304
Calin Juravle77520bc2015-01-12 18:45:46 +00005305 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005306 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005307 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005308 }
5309
Mingyao Yang81014cb2015-06-02 03:16:27 -07005310 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005311 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005312
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005313 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005314 // TODO: Same as for ArrayGet.
5315 return false;
5316 }
5317
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005318 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005319 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005320 }
5321
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005322 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005323 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005324 }
5325
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005326 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005327 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005328 }
5329
Vladimir Markoa1de9182016-02-25 11:37:38 +00005330 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5331 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5332 bool StaticTypeOfArrayIsObjectArray() const {
5333 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5334 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005335
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005336 HInstruction* GetArray() const { return InputAt(0); }
5337 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005338 HInstruction* GetValue() const { return InputAt(2); }
5339
5340 Primitive::Type GetComponentType() const {
5341 // The Dex format does not type floating point index operations. Since the
5342 // `expected_component_type_` is set during building and can therefore not
5343 // be correct, we also check what is the value type. If it is a floating
5344 // point type, we must use that type.
5345 Primitive::Type value_type = GetValue()->GetType();
5346 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5347 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005348 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005349 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005350
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005351 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005352 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005353 }
5354
Aart Bik18b36ab2016-04-13 16:41:35 -07005355 void ComputeSideEffects() {
5356 Primitive::Type type = GetComponentType();
5357 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5358 SideEffectsForArchRuntimeCalls(type)));
5359 }
5360
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005361 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5362 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5363 }
5364
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005365 DECLARE_INSTRUCTION(ArraySet);
5366
5367 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005368 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5369 static constexpr size_t kFieldExpectedComponentTypeSize =
5370 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5371 static constexpr size_t kFlagNeedsTypeCheck =
5372 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5373 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005374 // Cached information for the reference_type_info_ so that codegen
5375 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005376 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5377 static constexpr size_t kNumberOfArraySetPackedBits =
5378 kFlagStaticTypeOfArrayIsObjectArray + 1;
5379 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5380 using ExpectedComponentTypeField =
5381 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005382
5383 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5384};
5385
Vladimir Markofcb503c2016-05-18 12:48:17 +01005386class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005387 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005388 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005389 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005390 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005391 // Note that arrays do not change length, so the instruction does not
5392 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005393 SetRawInputAt(0, array);
5394 }
5395
Calin Juravle77520bc2015-01-12 18:45:46 +00005396 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005397 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005398 return true;
5399 }
Calin Juravle641547a2015-04-21 22:08:51 +01005400 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5401 return obj == InputAt(0);
5402 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005403
Vladimir Markodce016e2016-04-28 13:10:02 +01005404 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5405
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005406 DECLARE_INSTRUCTION(ArrayLength);
5407
5408 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005409 // We treat a String as an array, creating the HArrayLength from String.length()
5410 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5411 // determine whether a particular HArrayLength is actually a String.length() by
5412 // looking at the type of the input but that requires holding the mutator lock, so
5413 // we prefer to use a flag, so that code generators don't need to do the locking.
5414 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5415 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5416 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5417 "Too many packed fields.");
5418
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005419 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5420};
5421
Vladimir Markofcb503c2016-05-18 12:48:17 +01005422class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005423 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005424 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5425 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005426 HBoundsCheck(HInstruction* index,
5427 HInstruction* length,
5428 uint32_t dex_pc,
5429 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5430 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5431 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005432 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005433 SetRawInputAt(0, index);
5434 SetRawInputAt(1, length);
5435 }
5436
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005437 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005438 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005439 return true;
5440 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005441
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005442 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005443
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005444 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005445
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005446 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5447 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5448
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005449 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005450
5451 DECLARE_INSTRUCTION(BoundsCheck);
5452
5453 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005454 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5455 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5456 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5457 // so we need to create an HEnvironment which will be translated to an InlineInfo
5458 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5459 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5460 // from the invoke as we don't want to look again at the dex bytecode.
5461 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5462
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005463 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5464};
5465
Vladimir Markofcb503c2016-05-18 12:48:17 +01005466class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005467 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005468 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005469 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005470
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005471 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005472 return true;
5473 }
5474
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005475 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5476 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005477
5478 DECLARE_INSTRUCTION(SuspendCheck);
5479
5480 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005481 // Only used for code generation, in order to share the same slow path between back edges
5482 // of a same loop.
5483 SlowPathCode* slow_path_;
5484
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005485 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5486};
5487
David Srbecky0cf44932015-12-09 14:09:59 +00005488// Pseudo-instruction which provides the native debugger with mapping information.
5489// It ensures that we can generate line number and local variables at this point.
5490class HNativeDebugInfo : public HTemplateInstruction<0> {
5491 public:
5492 explicit HNativeDebugInfo(uint32_t dex_pc)
5493 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5494
5495 bool NeedsEnvironment() const OVERRIDE {
5496 return true;
5497 }
5498
5499 DECLARE_INSTRUCTION(NativeDebugInfo);
5500
5501 private:
5502 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5503};
5504
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005505/**
5506 * Instruction to load a Class object.
5507 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005508class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005509 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005510 // Determines how to load the Class.
5511 enum class LoadKind {
5512 // Use the Class* from the method's own ArtMethod*.
5513 kReferrersClass,
5514
5515 // Use boot image Class* address that will be known at link time.
5516 // Used for boot image classes referenced by boot image code in non-PIC mode.
5517 kBootImageLinkTimeAddress,
5518
5519 // Use PC-relative boot image Class* address that will be known at link time.
5520 // Used for boot image classes referenced by boot image code in PIC mode.
5521 kBootImageLinkTimePcRelative,
5522
5523 // Use a known boot image Class* address, embedded in the code by the codegen.
5524 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5525 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5526 // GetIncludePatchInformation().
5527 kBootImageAddress,
5528
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005529 // Load from the root table associated with the JIT compiled method.
5530 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005531
5532 // Load from resolved types array in the dex cache using a PC-relative load.
5533 // Used for classes outside boot image when we know that we can access
5534 // the dex cache arrays using a PC-relative load.
5535 kDexCachePcRelative,
5536
5537 // Load from resolved types array accessed through the class loaded from
5538 // the compiled method's own ArtMethod*. This is the default access type when
5539 // all other types are unavailable.
5540 kDexCacheViaMethod,
5541
5542 kLast = kDexCacheViaMethod
5543 };
5544
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005545 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005546 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005547 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005548 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005549 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005550 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005551 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5552 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005553 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005554 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005555 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005556 // Referrers class should not need access check. We never inline unverified
5557 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005558 DCHECK(!is_referrers_class || !needs_access_check);
5559
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005560 SetPackedField<LoadKindField>(
5561 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005562 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005563 SetPackedFlag<kFlagIsInDexCache>(false);
5564 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005565 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005566 }
5567
5568 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5569 DCHECK(HasAddress(load_kind));
5570 load_data_.address = address;
5571 SetLoadKindInternal(load_kind);
5572 }
5573
5574 void SetLoadKindWithTypeReference(LoadKind load_kind,
5575 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005576 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005577 DCHECK(HasTypeReference(load_kind));
5578 DCHECK(IsSameDexFile(dex_file_, dex_file));
5579 DCHECK_EQ(type_index_, type_index);
5580 SetLoadKindInternal(load_kind);
5581 }
5582
5583 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5584 const DexFile& dex_file,
5585 uint32_t element_index) {
5586 DCHECK(HasDexCacheReference(load_kind));
5587 DCHECK(IsSameDexFile(dex_file_, dex_file));
5588 load_data_.dex_cache_element_index = element_index;
5589 SetLoadKindInternal(load_kind);
5590 }
5591
5592 LoadKind GetLoadKind() const {
5593 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005594 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005595
5596 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005597
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005598 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005599
Andreas Gampea5b09a62016-11-17 15:21:22 -08005600 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005601
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005602 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005603
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005604 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005605 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005606 }
5607
Calin Juravle0ba218d2015-05-19 18:46:01 +01005608 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005609 // The entrypoint the code generator is going to call does not do
5610 // clinit of the class.
5611 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005612 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005613 }
5614
5615 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005616 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005617 (!IsReferrersClass() && !IsInDexCache()) ||
5618 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005619 }
5620
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005621 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005622 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005623 }
5624
Calin Juravleacf735c2015-02-12 15:25:22 +00005625 ReferenceTypeInfo GetLoadedClassRTI() {
5626 return loaded_class_rti_;
5627 }
5628
5629 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5630 // Make sure we only set exact types (the loaded class should never be merged).
5631 DCHECK(rti.IsExact());
5632 loaded_class_rti_ = rti;
5633 }
5634
Andreas Gampea5b09a62016-11-17 15:21:22 -08005635 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005636 const DexFile& GetDexFile() const { return dex_file_; }
5637
5638 uint32_t GetDexCacheElementOffset() const;
5639
5640 uint64_t GetAddress() const {
5641 DCHECK(HasAddress(GetLoadKind()));
5642 return load_data_.address;
5643 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005644
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005645 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5646 return !IsReferrersClass();
5647 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005648
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005649 static SideEffects SideEffectsForArchRuntimeCalls() {
5650 return SideEffects::CanTriggerGC();
5651 }
5652
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005653 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005654 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5655 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005656 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005657 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005658
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005659 void MarkInDexCache() {
5660 SetPackedFlag<kFlagIsInDexCache>(true);
5661 DCHECK(!NeedsEnvironment());
5662 RemoveEnvironment();
5663 SetSideEffects(SideEffects::None());
5664 }
5665
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005666 void MarkInBootImage() {
5667 SetPackedFlag<kFlagIsInBootImage>(true);
5668 }
5669
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005670 void AddSpecialInput(HInstruction* special_input);
5671
5672 using HInstruction::GetInputRecords; // Keep the const version visible.
5673 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5674 return ArrayRef<HUserRecord<HInstruction*>>(
5675 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5676 }
5677
5678 Primitive::Type GetType() const OVERRIDE {
5679 return Primitive::kPrimNot;
5680 }
5681
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005682 DECLARE_INSTRUCTION(LoadClass);
5683
5684 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005685 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005686 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005687 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005688 // Whether this instruction must generate the initialization check.
5689 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005690 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005691 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5692 static constexpr size_t kFieldLoadKindSize =
5693 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5694 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005695 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005696 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5697
5698 static bool HasTypeReference(LoadKind load_kind) {
5699 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5700 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5701 load_kind == LoadKind::kDexCacheViaMethod ||
5702 load_kind == LoadKind::kReferrersClass;
5703 }
5704
5705 static bool HasAddress(LoadKind load_kind) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005706 return load_kind == LoadKind::kBootImageAddress ||
5707 load_kind == LoadKind::kJitTableAddress;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005708 }
5709
5710 static bool HasDexCacheReference(LoadKind load_kind) {
5711 return load_kind == LoadKind::kDexCachePcRelative;
5712 }
5713
5714 void SetLoadKindInternal(LoadKind load_kind);
5715
5716 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5717 // For other load kinds it's empty or possibly some architecture-specific instruction
5718 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5719 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005720
Andreas Gampea5b09a62016-11-17 15:21:22 -08005721 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005722 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005723
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005724 union {
5725 uint32_t dex_cache_element_index; // Only for dex cache reference.
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005726 uint64_t address; // Up to 64-bit, needed for kJitTableAddress on 64-bit targets.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005727 } load_data_;
5728
Calin Juravleacf735c2015-02-12 15:25:22 +00005729 ReferenceTypeInfo loaded_class_rti_;
5730
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005731 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5732};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005733std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5734
5735// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5736inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5737 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5738 return load_data_.dex_cache_element_index;
5739}
5740
5741// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5742inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005743 // The special input is used for PC-relative loads on some architectures,
5744 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005745 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005746 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5747 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5748 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005749 DCHECK(special_input_.GetInstruction() == nullptr);
5750 special_input_ = HUserRecord<HInstruction*>(special_input);
5751 special_input->AddUseAt(this, 0);
5752}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005753
Vladimir Marko372f10e2016-05-17 16:30:10 +01005754class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005755 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005756 // Determines how to load the String.
5757 enum class LoadKind {
5758 // Use boot image String* address that will be known at link time.
5759 // Used for boot image strings referenced by boot image code in non-PIC mode.
5760 kBootImageLinkTimeAddress,
5761
5762 // Use PC-relative boot image String* address that will be known at link time.
5763 // Used for boot image strings referenced by boot image code in PIC mode.
5764 kBootImageLinkTimePcRelative,
5765
5766 // Use a known boot image String* address, embedded in the code by the codegen.
5767 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5768 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5769 // GetIncludePatchInformation().
5770 kBootImageAddress,
5771
Vladimir Markoaad75c62016-10-03 08:46:48 +00005772 // Load from an entry in the .bss section using a PC-relative load.
5773 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5774 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005775
5776 // Load from resolved strings array accessed through the class loaded from
5777 // the compiled method's own ArtMethod*. This is the default access type when
5778 // all other types are unavailable.
5779 kDexCacheViaMethod,
5780
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005781 // Load from the root table associated with the JIT compiled method.
5782 kJitTableAddress,
5783
5784 kLast = kJitTableAddress,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005785 };
5786
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005787 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005788 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005789 const DexFile& dex_file,
5790 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005791 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5792 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005793 string_index_(string_index),
5794 dex_file_(dex_file) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005795 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005796 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005797
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005798 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005799
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005800 LoadKind GetLoadKind() const {
5801 return GetPackedField<LoadKindField>();
5802 }
5803
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005804 const DexFile& GetDexFile() const {
5805 return dex_file_;
5806 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005807
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005808 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005809 return string_index_;
5810 }
5811
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005812 Handle<mirror::String> GetString() const {
5813 return string_;
5814 }
5815
5816 void SetString(Handle<mirror::String> str) {
5817 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005818 }
5819
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005820 bool CanBeMoved() const OVERRIDE { return true; }
5821
Vladimir Marko372f10e2016-05-17 16:30:10 +01005822 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005823
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005824 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005825
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005826 // Will call the runtime if we need to load the string through
5827 // the dex cache and the string is not guaranteed to be there yet.
5828 bool NeedsEnvironment() const OVERRIDE {
5829 LoadKind load_kind = GetLoadKind();
5830 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5831 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005832 load_kind == LoadKind::kBootImageAddress ||
5833 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005834 return false;
5835 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005836 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005837 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005838
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005839 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5840 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5841 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005842
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005843 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005844 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005845
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005846 static SideEffects SideEffectsForArchRuntimeCalls() {
5847 return SideEffects::CanTriggerGC();
5848 }
5849
Vladimir Marko372f10e2016-05-17 16:30:10 +01005850 void AddSpecialInput(HInstruction* special_input);
5851
5852 using HInstruction::GetInputRecords; // Keep the const version visible.
5853 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5854 return ArrayRef<HUserRecord<HInstruction*>>(
5855 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005856 }
5857
Vladimir Marko372f10e2016-05-17 16:30:10 +01005858 Primitive::Type GetType() const OVERRIDE {
5859 return Primitive::kPrimNot;
5860 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005861
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005862 DECLARE_INSTRUCTION(LoadString);
5863
5864 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005865 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005866 static constexpr size_t kFieldLoadKindSize =
5867 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5868 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005869 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005870 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005871
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005872 void SetLoadKindInternal(LoadKind load_kind);
5873
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005874 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5875 // For other load kinds it's empty or possibly some architecture-specific instruction
5876 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005877 HUserRecord<HInstruction*> special_input_;
5878
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005879 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005880 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005881
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005882 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005883
5884 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5885};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005886std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5887
5888// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005889inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005890 // The special input is used for PC-relative loads on some architectures,
5891 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005892 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005893 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005894 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5895 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005896 // HLoadString::GetInputRecords() returns an empty array at this point,
5897 // so use the GetInputRecords() from the base class to set the input record.
5898 DCHECK(special_input_.GetInstruction() == nullptr);
5899 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005900 special_input->AddUseAt(this, 0);
5901}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005902
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005903/**
5904 * Performs an initialization check on its Class object input.
5905 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005906class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005907 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005908 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005909 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005910 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005911 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5912 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005913 SetRawInputAt(0, constant);
5914 }
5915
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005916 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005917 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005918 return true;
5919 }
5920
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005921 bool NeedsEnvironment() const OVERRIDE {
5922 // May call runtime to initialize the class.
5923 return true;
5924 }
5925
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005926 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005927
5928 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5929
5930 DECLARE_INSTRUCTION(ClinitCheck);
5931
5932 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005933 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5934};
5935
Vladimir Markofcb503c2016-05-18 12:48:17 +01005936class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005937 public:
5938 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005939 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005940 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005941 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005942 bool is_volatile,
5943 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005944 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005945 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005946 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005947 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005948 field_info_(field,
5949 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005950 field_type,
5951 is_volatile,
5952 field_idx,
5953 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005954 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005955 SetRawInputAt(0, cls);
5956 }
5957
Calin Juravle52c48962014-12-16 17:02:57 +00005958
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005959 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005960
Vladimir Marko372f10e2016-05-17 16:30:10 +01005961 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5962 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005963 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005964 }
5965
5966 size_t ComputeHashCode() const OVERRIDE {
5967 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5968 }
5969
Calin Juravle52c48962014-12-16 17:02:57 +00005970 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005971 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5972 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005973 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005974
5975 DECLARE_INSTRUCTION(StaticFieldGet);
5976
5977 private:
5978 const FieldInfo field_info_;
5979
5980 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5981};
5982
Vladimir Markofcb503c2016-05-18 12:48:17 +01005983class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005984 public:
5985 HStaticFieldSet(HInstruction* cls,
5986 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005987 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005988 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005989 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005990 bool is_volatile,
5991 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005992 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005993 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005994 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005995 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005996 field_info_(field,
5997 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005998 field_type,
5999 is_volatile,
6000 field_idx,
6001 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006002 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006003 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006004 SetRawInputAt(0, cls);
6005 SetRawInputAt(1, value);
6006 }
6007
Calin Juravle52c48962014-12-16 17:02:57 +00006008 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006009 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6010 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006011 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006012
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006013 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006014 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6015 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006016
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006017 DECLARE_INSTRUCTION(StaticFieldSet);
6018
6019 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006020 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6021 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6022 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6023 "Too many packed fields.");
6024
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006025 const FieldInfo field_info_;
6026
6027 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6028};
6029
Vladimir Markofcb503c2016-05-18 12:48:17 +01006030class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006031 public:
6032 HUnresolvedInstanceFieldGet(HInstruction* obj,
6033 Primitive::Type field_type,
6034 uint32_t field_index,
6035 uint32_t dex_pc)
6036 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6037 field_index_(field_index) {
6038 SetRawInputAt(0, obj);
6039 }
6040
6041 bool NeedsEnvironment() const OVERRIDE { return true; }
6042 bool CanThrow() const OVERRIDE { return true; }
6043
6044 Primitive::Type GetFieldType() const { return GetType(); }
6045 uint32_t GetFieldIndex() const { return field_index_; }
6046
6047 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6048
6049 private:
6050 const uint32_t field_index_;
6051
6052 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6053};
6054
Vladimir Markofcb503c2016-05-18 12:48:17 +01006055class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006056 public:
6057 HUnresolvedInstanceFieldSet(HInstruction* obj,
6058 HInstruction* value,
6059 Primitive::Type field_type,
6060 uint32_t field_index,
6061 uint32_t dex_pc)
6062 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006063 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006064 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006065 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006066 SetRawInputAt(0, obj);
6067 SetRawInputAt(1, value);
6068 }
6069
6070 bool NeedsEnvironment() const OVERRIDE { return true; }
6071 bool CanThrow() const OVERRIDE { return true; }
6072
Vladimir Markoa1de9182016-02-25 11:37:38 +00006073 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006074 uint32_t GetFieldIndex() const { return field_index_; }
6075
6076 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6077
6078 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006079 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6080 static constexpr size_t kFieldFieldTypeSize =
6081 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6082 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6083 kFieldFieldType + kFieldFieldTypeSize;
6084 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6085 "Too many packed fields.");
6086 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6087
Calin Juravlee460d1d2015-09-29 04:52:17 +01006088 const uint32_t field_index_;
6089
6090 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6091};
6092
Vladimir Markofcb503c2016-05-18 12:48:17 +01006093class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006094 public:
6095 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6096 uint32_t field_index,
6097 uint32_t dex_pc)
6098 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6099 field_index_(field_index) {
6100 }
6101
6102 bool NeedsEnvironment() const OVERRIDE { return true; }
6103 bool CanThrow() const OVERRIDE { return true; }
6104
6105 Primitive::Type GetFieldType() const { return GetType(); }
6106 uint32_t GetFieldIndex() const { return field_index_; }
6107
6108 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6109
6110 private:
6111 const uint32_t field_index_;
6112
6113 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6114};
6115
Vladimir Markofcb503c2016-05-18 12:48:17 +01006116class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006117 public:
6118 HUnresolvedStaticFieldSet(HInstruction* value,
6119 Primitive::Type field_type,
6120 uint32_t field_index,
6121 uint32_t dex_pc)
6122 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006123 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006124 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006125 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006126 SetRawInputAt(0, value);
6127 }
6128
6129 bool NeedsEnvironment() const OVERRIDE { return true; }
6130 bool CanThrow() const OVERRIDE { return true; }
6131
Vladimir Markoa1de9182016-02-25 11:37:38 +00006132 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006133 uint32_t GetFieldIndex() const { return field_index_; }
6134
6135 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6136
6137 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006138 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6139 static constexpr size_t kFieldFieldTypeSize =
6140 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6141 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6142 kFieldFieldType + kFieldFieldTypeSize;
6143 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6144 "Too many packed fields.");
6145 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6146
Calin Juravlee460d1d2015-09-29 04:52:17 +01006147 const uint32_t field_index_;
6148
6149 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6150};
6151
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006152// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006153class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006154 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006155 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6156 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006157
David Brazdilbbd733e2015-08-18 17:48:17 +01006158 bool CanBeNull() const OVERRIDE { return false; }
6159
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006160 DECLARE_INSTRUCTION(LoadException);
6161
6162 private:
6163 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6164};
6165
David Brazdilcb1c0552015-08-04 16:22:25 +01006166// Implicit part of move-exception which clears thread-local exception storage.
6167// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006168class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006169 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006170 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6171 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006172
6173 DECLARE_INSTRUCTION(ClearException);
6174
6175 private:
6176 DISALLOW_COPY_AND_ASSIGN(HClearException);
6177};
6178
Vladimir Markofcb503c2016-05-18 12:48:17 +01006179class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006180 public:
6181 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006182 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006183 SetRawInputAt(0, exception);
6184 }
6185
6186 bool IsControlFlow() const OVERRIDE { return true; }
6187
6188 bool NeedsEnvironment() const OVERRIDE { return true; }
6189
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006190 bool CanThrow() const OVERRIDE { return true; }
6191
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006192
6193 DECLARE_INSTRUCTION(Throw);
6194
6195 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006196 DISALLOW_COPY_AND_ASSIGN(HThrow);
6197};
6198
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006199/**
6200 * Implementation strategies for the code generator of a HInstanceOf
6201 * or `HCheckCast`.
6202 */
6203enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006204 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006205 kExactCheck, // Can do a single class compare.
6206 kClassHierarchyCheck, // Can just walk the super class chain.
6207 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6208 kInterfaceCheck, // No optimization yet when checking against an interface.
6209 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006210 kArrayCheck, // No optimization yet when checking against a generic array.
6211 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006212};
6213
Roland Levillain86503782016-02-11 19:07:30 +00006214std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6215
Vladimir Markofcb503c2016-05-18 12:48:17 +01006216class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006217 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006218 HInstanceOf(HInstruction* object,
6219 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006220 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006221 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006222 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006223 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006224 dex_pc) {
6225 SetPackedField<TypeCheckKindField>(check_kind);
6226 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006227 SetRawInputAt(0, object);
6228 SetRawInputAt(1, constant);
6229 }
6230
6231 bool CanBeMoved() const OVERRIDE { return true; }
6232
Vladimir Marko372f10e2016-05-17 16:30:10 +01006233 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006234 return true;
6235 }
6236
6237 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006238 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006239 }
6240
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006241 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006242 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6243 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6244 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6245 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006246
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006247 static bool CanCallRuntime(TypeCheckKind check_kind) {
6248 // Mips currently does runtime calls for any other checks.
6249 return check_kind != TypeCheckKind::kExactCheck;
6250 }
6251
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006252 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006253 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006254 }
6255
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006256 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006257
6258 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006259 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6260 static constexpr size_t kFieldTypeCheckKindSize =
6261 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6262 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6263 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6264 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6265 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006266
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006267 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6268};
6269
Vladimir Markofcb503c2016-05-18 12:48:17 +01006270class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006271 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006272 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006273 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006274 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6275 SetPackedFlag<kFlagUpperCanBeNull>(true);
6276 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006277 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006278 SetRawInputAt(0, input);
6279 }
6280
David Brazdilf5552582015-12-27 13:36:12 +00006281 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006282 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006283 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006284 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006285
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006286 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006287 DCHECK(GetUpperCanBeNull() || !can_be_null);
6288 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006289 }
6290
Vladimir Markoa1de9182016-02-25 11:37:38 +00006291 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006292
Calin Juravleb1498f62015-02-16 13:13:29 +00006293 DECLARE_INSTRUCTION(BoundType);
6294
6295 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006296 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6297 // is false then CanBeNull() cannot be true).
6298 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6299 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6300 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6301 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6302
Calin Juravleb1498f62015-02-16 13:13:29 +00006303 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006304 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6305 // It is used to bound the type in cases like:
6306 // if (x instanceof ClassX) {
6307 // // uper_bound_ will be ClassX
6308 // }
David Brazdilf5552582015-12-27 13:36:12 +00006309 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006310
6311 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6312};
6313
Vladimir Markofcb503c2016-05-18 12:48:17 +01006314class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006315 public:
6316 HCheckCast(HInstruction* object,
6317 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006318 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006319 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006320 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6321 SetPackedField<TypeCheckKindField>(check_kind);
6322 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006323 SetRawInputAt(0, object);
6324 SetRawInputAt(1, constant);
6325 }
6326
6327 bool CanBeMoved() const OVERRIDE { return true; }
6328
Vladimir Marko372f10e2016-05-17 16:30:10 +01006329 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006330 return true;
6331 }
6332
6333 bool NeedsEnvironment() const OVERRIDE {
6334 // Instruction may throw a CheckCastError.
6335 return true;
6336 }
6337
6338 bool CanThrow() const OVERRIDE { return true; }
6339
Vladimir Markoa1de9182016-02-25 11:37:38 +00006340 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6341 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6342 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6343 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006344
6345 DECLARE_INSTRUCTION(CheckCast);
6346
6347 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006348 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6349 static constexpr size_t kFieldTypeCheckKindSize =
6350 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6351 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6352 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6353 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6354 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006355
6356 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006357};
6358
Andreas Gampe26de38b2016-07-27 17:53:11 -07006359/**
6360 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6361 * @details We define the combined barrier types that are actually required
6362 * by the Java Memory Model, rather than using exactly the terminology from
6363 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6364 * primitives. Note that the JSR-133 cookbook generally does not deal with
6365 * store atomicity issues, and the recipes there are not always entirely sufficient.
6366 * The current recipe is as follows:
6367 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6368 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6369 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6370 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6371 * class has final fields.
6372 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6373 * store-to-memory instructions. Only generated together with non-temporal stores.
6374 */
6375enum MemBarrierKind {
6376 kAnyStore,
6377 kLoadAny,
6378 kStoreStore,
6379 kAnyAny,
6380 kNTStoreStore,
6381 kLastBarrierKind = kNTStoreStore
6382};
6383std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6384
Vladimir Markofcb503c2016-05-18 12:48:17 +01006385class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006386 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006387 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006388 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006389 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6390 SetPackedField<BarrierKindField>(barrier_kind);
6391 }
Calin Juravle27df7582015-04-17 19:12:31 +01006392
Vladimir Markoa1de9182016-02-25 11:37:38 +00006393 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006394
6395 DECLARE_INSTRUCTION(MemoryBarrier);
6396
6397 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006398 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6399 static constexpr size_t kFieldBarrierKindSize =
6400 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6401 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6402 kFieldBarrierKind + kFieldBarrierKindSize;
6403 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6404 "Too many packed fields.");
6405 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006406
6407 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6408};
6409
Vladimir Markofcb503c2016-05-18 12:48:17 +01006410class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006411 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006412 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006413 kEnter,
6414 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006415 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006416 };
6417
6418 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006419 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006420 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6421 dex_pc) {
6422 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006423 SetRawInputAt(0, object);
6424 }
6425
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006426 // Instruction may go into runtime, so we need an environment.
6427 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006428
6429 bool CanThrow() const OVERRIDE {
6430 // Verifier guarantees that monitor-exit cannot throw.
6431 // This is important because it allows the HGraphBuilder to remove
6432 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6433 return IsEnter();
6434 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006435
Vladimir Markoa1de9182016-02-25 11:37:38 +00006436 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6437 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006438
6439 DECLARE_INSTRUCTION(MonitorOperation);
6440
Calin Juravle52c48962014-12-16 17:02:57 +00006441 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006442 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6443 static constexpr size_t kFieldOperationKindSize =
6444 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6445 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6446 kFieldOperationKind + kFieldOperationKindSize;
6447 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6448 "Too many packed fields.");
6449 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006450
6451 private:
6452 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6453};
6454
Vladimir Markofcb503c2016-05-18 12:48:17 +01006455class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006456 public:
6457 HSelect(HInstruction* condition,
6458 HInstruction* true_value,
6459 HInstruction* false_value,
6460 uint32_t dex_pc)
6461 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6462 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6463
6464 // First input must be `true_value` or `false_value` to allow codegens to
6465 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6466 // that architectures which implement HSelect as a conditional move also
6467 // will not need to invert the condition.
6468 SetRawInputAt(0, false_value);
6469 SetRawInputAt(1, true_value);
6470 SetRawInputAt(2, condition);
6471 }
6472
6473 HInstruction* GetFalseValue() const { return InputAt(0); }
6474 HInstruction* GetTrueValue() const { return InputAt(1); }
6475 HInstruction* GetCondition() const { return InputAt(2); }
6476
6477 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006478 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6479 return true;
6480 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006481
6482 bool CanBeNull() const OVERRIDE {
6483 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6484 }
6485
6486 DECLARE_INSTRUCTION(Select);
6487
6488 private:
6489 DISALLOW_COPY_AND_ASSIGN(HSelect);
6490};
6491
Vladimir Markof9f64412015-09-02 14:05:49 +01006492class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006493 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006494 MoveOperands(Location source,
6495 Location destination,
6496 Primitive::Type type,
6497 HInstruction* instruction)
6498 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006499
6500 Location GetSource() const { return source_; }
6501 Location GetDestination() const { return destination_; }
6502
6503 void SetSource(Location value) { source_ = value; }
6504 void SetDestination(Location value) { destination_ = value; }
6505
6506 // The parallel move resolver marks moves as "in-progress" by clearing the
6507 // destination (but not the source).
6508 Location MarkPending() {
6509 DCHECK(!IsPending());
6510 Location dest = destination_;
6511 destination_ = Location::NoLocation();
6512 return dest;
6513 }
6514
6515 void ClearPending(Location dest) {
6516 DCHECK(IsPending());
6517 destination_ = dest;
6518 }
6519
6520 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006521 DCHECK(source_.IsValid() || destination_.IsInvalid());
6522 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006523 }
6524
6525 // True if this blocks a move from the given location.
6526 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006527 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006528 }
6529
6530 // A move is redundant if it's been eliminated, if its source and
6531 // destination are the same, or if its destination is unneeded.
6532 bool IsRedundant() const {
6533 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6534 }
6535
6536 // We clear both operands to indicate move that's been eliminated.
6537 void Eliminate() {
6538 source_ = destination_ = Location::NoLocation();
6539 }
6540
6541 bool IsEliminated() const {
6542 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6543 return source_.IsInvalid();
6544 }
6545
Alexey Frunze4dda3372015-06-01 18:31:49 -07006546 Primitive::Type GetType() const { return type_; }
6547
Nicolas Geoffray90218252015-04-15 11:56:51 +01006548 bool Is64BitMove() const {
6549 return Primitive::Is64BitType(type_);
6550 }
6551
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006552 HInstruction* GetInstruction() const { return instruction_; }
6553
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006554 private:
6555 Location source_;
6556 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006557 // The type this move is for.
6558 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006559 // The instruction this move is assocatied with. Null when this move is
6560 // for moving an input in the expected locations of user (including a phi user).
6561 // This is only used in debug mode, to ensure we do not connect interval siblings
6562 // in the same parallel move.
6563 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006564};
6565
Roland Levillainc9285912015-12-18 10:38:42 +00006566std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6567
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006568static constexpr size_t kDefaultNumberOfMoves = 4;
6569
Vladimir Markofcb503c2016-05-18 12:48:17 +01006570class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006571 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006572 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006573 : HTemplateInstruction(SideEffects::None(), dex_pc),
6574 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6575 moves_.reserve(kDefaultNumberOfMoves);
6576 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006577
Nicolas Geoffray90218252015-04-15 11:56:51 +01006578 void AddMove(Location source,
6579 Location destination,
6580 Primitive::Type type,
6581 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006582 DCHECK(source.IsValid());
6583 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006584 if (kIsDebugBuild) {
6585 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006586 for (const MoveOperands& move : moves_) {
6587 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006588 // Special case the situation where the move is for the spill slot
6589 // of the instruction.
6590 if ((GetPrevious() == instruction)
6591 || ((GetPrevious() == nullptr)
6592 && instruction->IsPhi()
6593 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006594 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006595 << "Doing parallel moves for the same instruction.";
6596 } else {
6597 DCHECK(false) << "Doing parallel moves for the same instruction.";
6598 }
6599 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006600 }
6601 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006602 for (const MoveOperands& move : moves_) {
6603 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006604 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006605 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006606 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006607 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006608 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006609 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006610 }
6611
Vladimir Marko225b6462015-09-28 12:17:40 +01006612 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006613 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006614 }
6615
Vladimir Marko225b6462015-09-28 12:17:40 +01006616 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006617
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006618 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006619
6620 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006621 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006622
6623 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6624};
6625
Mark Mendell0616ae02015-04-17 12:49:27 -04006626} // namespace art
6627
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006628#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6629#include "nodes_shared.h"
6630#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006631#ifdef ART_ENABLE_CODEGEN_arm
6632#include "nodes_arm.h"
6633#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006634#ifdef ART_ENABLE_CODEGEN_arm64
6635#include "nodes_arm64.h"
6636#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006637#ifdef ART_ENABLE_CODEGEN_mips
6638#include "nodes_mips.h"
6639#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006640#ifdef ART_ENABLE_CODEGEN_x86
6641#include "nodes_x86.h"
6642#endif
6643
6644namespace art {
6645
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006646class HGraphVisitor : public ValueObject {
6647 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006648 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6649 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006650
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006651 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006652 virtual void VisitBasicBlock(HBasicBlock* block);
6653
Roland Levillain633021e2014-10-01 14:12:25 +01006654 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006655 void VisitInsertionOrder();
6656
Roland Levillain633021e2014-10-01 14:12:25 +01006657 // Visit the graph following dominator tree reverse post-order.
6658 void VisitReversePostOrder();
6659
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006660 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006661
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006662 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006663#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006664 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6665
6666 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6667
6668#undef DECLARE_VISIT_INSTRUCTION
6669
6670 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006671 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006672
6673 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6674};
6675
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006676class HGraphDelegateVisitor : public HGraphVisitor {
6677 public:
6678 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6679 virtual ~HGraphDelegateVisitor() {}
6680
6681 // Visit functions that delegate to to super class.
6682#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006683 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006684
6685 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6686
6687#undef DECLARE_VISIT_INSTRUCTION
6688
6689 private:
6690 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6691};
6692
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006693// Iterator over the blocks that art part of the loop. Includes blocks part
6694// of an inner loop. The order in which the blocks are iterated is on their
6695// block id.
6696class HBlocksInLoopIterator : public ValueObject {
6697 public:
6698 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6699 : blocks_in_loop_(info.GetBlocks()),
6700 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6701 index_(0) {
6702 if (!blocks_in_loop_.IsBitSet(index_)) {
6703 Advance();
6704 }
6705 }
6706
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006707 bool Done() const { return index_ == blocks_.size(); }
6708 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006709 void Advance() {
6710 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006711 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006712 if (blocks_in_loop_.IsBitSet(index_)) {
6713 break;
6714 }
6715 }
6716 }
6717
6718 private:
6719 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006720 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006721 size_t index_;
6722
6723 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6724};
6725
Mingyao Yang3584bce2015-05-19 16:01:59 -07006726// Iterator over the blocks that art part of the loop. Includes blocks part
6727// of an inner loop. The order in which the blocks are iterated is reverse
6728// post order.
6729class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6730 public:
6731 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6732 : blocks_in_loop_(info.GetBlocks()),
6733 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6734 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006735 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006736 Advance();
6737 }
6738 }
6739
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006740 bool Done() const { return index_ == blocks_.size(); }
6741 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006742 void Advance() {
6743 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006744 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6745 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006746 break;
6747 }
6748 }
6749 }
6750
6751 private:
6752 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006753 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006754 size_t index_;
6755
6756 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6757};
6758
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006759inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006760 if (constant->IsIntConstant()) {
6761 return constant->AsIntConstant()->GetValue();
6762 } else if (constant->IsLongConstant()) {
6763 return constant->AsLongConstant()->GetValue();
6764 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006765 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006766 return 0;
6767 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006768}
6769
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006770#define INSTRUCTION_TYPE_CHECK(type, super) \
6771 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6772 inline const H##type* HInstruction::As##type() const { \
6773 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6774 } \
6775 inline H##type* HInstruction::As##type() { \
6776 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6777 }
6778
6779 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6780#undef INSTRUCTION_TYPE_CHECK
6781
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006782// Create space in `blocks` for adding `number_of_new_blocks` entries
6783// starting at location `at`. Blocks after `at` are moved accordingly.
6784inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6785 size_t number_of_new_blocks,
6786 size_t after) {
6787 DCHECK_LT(after, blocks->size());
6788 size_t old_size = blocks->size();
6789 size_t new_size = old_size + number_of_new_blocks;
6790 blocks->resize(new_size);
6791 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6792}
6793
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006794} // namespace art
6795
6796#endif // ART_COMPILER_OPTIMIZING_NODES_H_