blob: 6c4f3dd517c0a6e5fcf67211c70f70556fb4bc26 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
Vladimir Marko60584552015-09-03 13:35:12 +000029#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010030#include "base/transform_array_ref.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010031#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080032#include "dex_file_types.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000033#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000034#include "handle.h"
35#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000036#include "invoke_type.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010037#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010038#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000039#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000040#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010041#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070042#include "primitive.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010043#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044
45namespace art {
46
David Brazdil1abb4192015-02-17 18:33:36 +000047class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000048class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010049class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010051class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000053class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000055class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000056class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000057class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000058class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000059class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010060class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010061class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010062class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010063class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000064class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010065class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000066class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067
Mathieu Chartier736b5602015-09-02 14:54:11 -070068namespace mirror {
69class DexCache;
70} // namespace mirror
71
Nicolas Geoffray818f2102014-02-18 16:43:35 +000072static const int kDefaultNumberOfBlocks = 8;
73static const int kDefaultNumberOfSuccessors = 2;
74static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010075static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010076static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000077static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000078
Roland Levillain5b5b9312016-03-22 14:57:31 +000079// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
80static constexpr int32_t kMaxIntShiftDistance = 0x1f;
81// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
82static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000083
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010084static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070085static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010086
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010087static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
88
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060089static constexpr uint32_t kNoDexPc = -1;
90
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010091inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
92 // For the purposes of the compiler, the dex files must actually be the same object
93 // if we want to safely treat them as the same. This is especially important for JIT
94 // as custom class loaders can open the same underlying file (or memory) multiple
95 // times and provide different class resolution but no two class loaders should ever
96 // use the same DexFile object - doing so is an unsupported hack that can lead to
97 // all sorts of weird failures.
98 return &lhs == &rhs;
99}
100
Dave Allison20dfc792014-06-16 20:44:29 -0700101enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700102 // All types.
103 kCondEQ, // ==
104 kCondNE, // !=
105 // Signed integers and floating-point numbers.
106 kCondLT, // <
107 kCondLE, // <=
108 kCondGT, // >
109 kCondGE, // >=
110 // Unsigned integers.
111 kCondB, // <
112 kCondBE, // <=
113 kCondA, // >
114 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100115 // First and last aliases.
116 kCondFirst = kCondEQ,
117 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700118};
119
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000120enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000121 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000122 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000123 kAnalysisFailThrowCatchLoop,
124 kAnalysisFailAmbiguousArrayOp,
125 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000126};
127
Andreas Gampe9186ced2016-12-12 14:28:21 -0800128template <typename T>
129static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
130 return static_cast<typename std::make_unsigned<T>::type>(x);
131}
132
Vladimir Markof9f64412015-09-02 14:05:49 +0100133class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100134 public:
135 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
136
137 void AddInstruction(HInstruction* instruction);
138 void RemoveInstruction(HInstruction* instruction);
139
David Brazdilc3d743f2015-04-22 13:40:50 +0100140 // Insert `instruction` before/after an existing instruction `cursor`.
141 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
142 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
143
Roland Levillain6b469232014-09-25 10:10:38 +0100144 // Return true if this list contains `instruction`.
145 bool Contains(HInstruction* instruction) const;
146
Roland Levillainccc07a92014-09-16 14:48:16 +0100147 // Return true if `instruction1` is found before `instruction2` in
148 // this instruction list and false otherwise. Abort if none
149 // of these instructions is found.
150 bool FoundBefore(const HInstruction* instruction1,
151 const HInstruction* instruction2) const;
152
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000153 bool IsEmpty() const { return first_instruction_ == nullptr; }
154 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
155
156 // Update the block of all instructions to be `block`.
157 void SetBlockOfInstructions(HBasicBlock* block) const;
158
159 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000160 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000161 void Add(const HInstructionList& instruction_list);
162
David Brazdil2d7352b2015-04-20 14:52:42 +0100163 // Return the number of instructions in the list. This is an expensive operation.
164 size_t CountSize() const;
165
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100166 private:
167 HInstruction* first_instruction_;
168 HInstruction* last_instruction_;
169
170 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000171 friend class HGraph;
172 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100173 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000174 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100175 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100176
177 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
178};
179
David Brazdil4833f5a2015-12-16 10:37:39 +0000180class ReferenceTypeInfo : ValueObject {
181 public:
182 typedef Handle<mirror::Class> TypeHandle;
183
Vladimir Markoa1de9182016-02-25 11:37:38 +0000184 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
185
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700186 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100187 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
188 }
189
Vladimir Markoa1de9182016-02-25 11:37:38 +0000190 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000191 return ReferenceTypeInfo(type_handle, is_exact);
192 }
193
194 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
195
Vladimir Markof39745e2016-01-26 12:16:55 +0000196 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000197 return handle.GetReference() != nullptr;
198 }
199
Vladimir Marko456307a2016-04-19 14:12:13 +0000200 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000201 return IsValidHandle(type_handle_);
202 }
203
204 bool IsExact() const { return is_exact_; }
205
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700206 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000207 DCHECK(IsValid());
208 return GetTypeHandle()->IsObjectClass();
209 }
210
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700211 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000212 DCHECK(IsValid());
213 return GetTypeHandle()->IsStringClass();
214 }
215
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000217 DCHECK(IsValid());
218 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
219 }
220
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000222 DCHECK(IsValid());
223 return GetTypeHandle()->IsInterface();
224 }
225
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700226 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000227 DCHECK(IsValid());
228 return GetTypeHandle()->IsArrayClass();
229 }
230
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000232 DCHECK(IsValid());
233 return GetTypeHandle()->IsPrimitiveArray();
234 }
235
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000237 DCHECK(IsValid());
238 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
239 }
240
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700241 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 DCHECK(IsValid());
243 if (!IsExact()) return false;
244 if (!IsArrayClass()) return false;
245 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
246 }
247
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700248 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000249 DCHECK(IsValid());
250 if (!IsExact()) return false;
251 if (!IsArrayClass()) return false;
252 if (!rti.IsArrayClass()) return false;
253 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
254 rti.GetTypeHandle()->GetComponentType());
255 }
256
257 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
258
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700259 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000260 DCHECK(IsValid());
261 DCHECK(rti.IsValid());
262 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
263 }
264
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700265 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000266 DCHECK(IsValid());
267 DCHECK(rti.IsValid());
268 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
269 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
270 }
271
272 // Returns true if the type information provide the same amount of details.
273 // Note that it does not mean that the instructions have the same actual type
274 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700275 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000276 if (!IsValid() && !rti.IsValid()) {
277 // Invalid types are equal.
278 return true;
279 }
280 if (!IsValid() || !rti.IsValid()) {
281 // One is valid, the other not.
282 return false;
283 }
284 return IsExact() == rti.IsExact()
285 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
286 }
287
288 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000289 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
290 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
291 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000292
293 // The class of the object.
294 TypeHandle type_handle_;
295 // Whether or not the type is exact or a superclass of the actual type.
296 // Whether or not we have any information about this type.
297 bool is_exact_;
298};
299
300std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
301
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000302// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100303class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100305 HGraph(ArenaAllocator* arena,
306 const DexFile& dex_file,
307 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100308 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700309 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100310 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100311 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000312 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100313 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000314 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100315 blocks_(arena->Adapter(kArenaAllocBlockList)),
316 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
317 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700318 entry_block_(nullptr),
319 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100320 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100321 number_of_vregs_(0),
322 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000323 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400324 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000325 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000326 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000327 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000328 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100329 dex_file_(dex_file),
330 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100331 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100332 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100333 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800334 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700335 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000336 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100337 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
338 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
339 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
340 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000341 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000342 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700343 osr_(osr),
344 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100345 blocks_.reserve(kDefaultNumberOfBlocks);
346 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000347
David Brazdilbadd8262016-02-02 16:28:56 +0000348 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700349 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000350
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000351 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100352 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
353
David Brazdil69ba7b72015-06-23 18:27:30 +0100354 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000355 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100356
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000357 HBasicBlock* GetEntryBlock() const { return entry_block_; }
358 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100359 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000360
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000361 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
362 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000363
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000364 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100365
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100366 void ComputeDominanceInformation();
367 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000368 void ClearLoopInformation();
369 void FindBackEdges(ArenaBitVector* visited);
370 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100371 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100372 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373
David Brazdil4833f5a2015-12-16 10:37:39 +0000374 // Analyze all natural loops in this graph. Returns a code specifying that it
375 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000376 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000377 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100378
David Brazdilffee3d32015-07-06 11:48:53 +0100379 // Iterate over blocks to compute try block membership. Needs reverse post
380 // order and loop information.
381 void ComputeTryBlockInformation();
382
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000383 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000384 // Returns the instruction to replace the invoke expression or null if the
385 // invoke is for a void method. Note that the caller is responsible for replacing
386 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000387 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000388
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000389 // Update the loop and try membership of `block`, which was spawned from `reference`.
390 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
391 // should be the new back edge.
392 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
393 HBasicBlock* reference,
394 bool replace_if_back_edge);
395
Mingyao Yang3584bce2015-05-19 16:01:59 -0700396 // Need to add a couple of blocks to test if the loop body is entered and
397 // put deoptimization instructions, etc.
398 void TransformLoopHeaderForBCE(HBasicBlock* header);
399
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000400 // Removes `block` from the graph. Assumes `block` has been disconnected from
401 // other blocks and has no instructions or phis.
402 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000403
David Brazdilfc6a86a2015-06-26 10:33:45 +0000404 // Splits the edge between `block` and `successor` while preserving the
405 // indices in the predecessor/successor lists. If there are multiple edges
406 // between the blocks, the lowest indices are used.
407 // Returns the new block which is empty and has the same dex pc as `successor`.
408 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
409
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100410 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
411 void SimplifyLoop(HBasicBlock* header);
412
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000413 int32_t GetNextInstructionId() {
414 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000415 return current_instruction_id_++;
416 }
417
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000418 int32_t GetCurrentInstructionId() const {
419 return current_instruction_id_;
420 }
421
422 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000423 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000424 current_instruction_id_ = id;
425 }
426
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100427 uint16_t GetMaximumNumberOfOutVRegs() const {
428 return maximum_number_of_out_vregs_;
429 }
430
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000431 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
432 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100433 }
434
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100435 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
436 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
437 }
438
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000439 void UpdateTemporariesVRegSlots(size_t slots) {
440 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100441 }
442
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000443 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100444 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000445 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100446 }
447
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100448 void SetNumberOfVRegs(uint16_t number_of_vregs) {
449 number_of_vregs_ = number_of_vregs;
450 }
451
452 uint16_t GetNumberOfVRegs() const {
453 return number_of_vregs_;
454 }
455
456 void SetNumberOfInVRegs(uint16_t value) {
457 number_of_in_vregs_ = value;
458 }
459
David Brazdildee58d62016-04-07 09:54:26 +0000460 uint16_t GetNumberOfInVRegs() const {
461 return number_of_in_vregs_;
462 }
463
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100464 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100465 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100466 return number_of_vregs_ - number_of_in_vregs_;
467 }
468
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100469 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100470 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100471 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100472
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100473 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
474 DCHECK(GetReversePostOrder()[0] == entry_block_);
475 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
476 }
477
478 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
479 return ReverseRange(GetReversePostOrder());
480 }
481
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100482 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100483 return linear_order_;
484 }
485
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100486 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
487 return ReverseRange(GetLinearOrder());
488 }
489
Mark Mendell1152c922015-04-24 17:06:35 -0400490 bool HasBoundsChecks() const {
491 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800492 }
493
Mark Mendell1152c922015-04-24 17:06:35 -0400494 void SetHasBoundsChecks(bool value) {
495 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800496 }
497
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100498 bool ShouldGenerateConstructorBarrier() const {
499 return should_generate_constructor_barrier_;
500 }
501
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000502 bool IsDebuggable() const { return debuggable_; }
503
David Brazdil8d5b8b22015-03-24 10:51:52 +0000504 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000505 // already, it is created and inserted into the graph. This method is only for
506 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600507 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000508
509 // TODO: This is problematic for the consistency of reference type propagation
510 // because it can be created anytime after the pass and thus it will be left
511 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600512 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000513
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600514 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
515 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000516 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600517 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
518 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000519 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600520 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
521 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000522 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600523 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
524 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000525 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000526
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100527 HCurrentMethod* GetCurrentMethod();
528
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100529 const DexFile& GetDexFile() const {
530 return dex_file_;
531 }
532
533 uint32_t GetMethodIdx() const {
534 return method_idx_;
535 }
536
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100537 InvokeType GetInvokeType() const {
538 return invoke_type_;
539 }
540
Mark Mendellc4701932015-04-10 13:18:51 -0400541 InstructionSet GetInstructionSet() const {
542 return instruction_set_;
543 }
544
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000545 bool IsCompilingOsr() const { return osr_; }
546
Mingyao Yang063fc772016-08-02 11:02:54 -0700547 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
548 return cha_single_implementation_list_;
549 }
550
551 void AddCHASingleImplementationDependency(ArtMethod* method) {
552 cha_single_implementation_list_.insert(method);
553 }
554
555 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800556 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700557 }
558
David Brazdil77a48ae2015-09-15 12:34:04 +0000559 bool HasTryCatch() const { return has_try_catch_; }
560 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100561
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000562 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
563 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
564
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100565 ArtMethod* GetArtMethod() const { return art_method_; }
566 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
567
Mark Mendellf6529172015-11-17 11:16:56 -0500568 // Returns an instruction with the opposite boolean value from 'cond'.
569 // The instruction has been inserted into the graph, either as a constant, or
570 // before cursor.
571 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
572
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000573 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
574
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800575 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
576 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
577 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
578
David Brazdil2d7352b2015-04-20 14:52:42 +0100579 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000580 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100581 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000582
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000583 template <class InstructionType, typename ValueType>
584 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600585 ArenaSafeMap<ValueType, InstructionType*>* cache,
586 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000587 // Try to find an existing constant of the given value.
588 InstructionType* constant = nullptr;
589 auto cached_constant = cache->find(value);
590 if (cached_constant != cache->end()) {
591 constant = cached_constant->second;
592 }
593
594 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100595 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000596 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600597 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000598 cache->Overwrite(value, constant);
599 InsertConstant(constant);
600 }
601 return constant;
602 }
603
David Brazdil8d5b8b22015-03-24 10:51:52 +0000604 void InsertConstant(HConstant* instruction);
605
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000606 // Cache a float constant into the graph. This method should only be
607 // called by the SsaBuilder when creating "equivalent" instructions.
608 void CacheFloatConstant(HFloatConstant* constant);
609
610 // See CacheFloatConstant comment.
611 void CacheDoubleConstant(HDoubleConstant* constant);
612
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000613 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000614
615 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100616 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000617
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100618 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100619 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000620
Aart Bik281c6812016-08-26 11:31:48 -0700621 // List of blocks to perform a linear order tree traversal. Unlike the reverse
622 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100623 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100624
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000625 HBasicBlock* entry_block_;
626 HBasicBlock* exit_block_;
627
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100628 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100629 uint16_t maximum_number_of_out_vregs_;
630
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100631 // The number of virtual registers in this method. Contains the parameters.
632 uint16_t number_of_vregs_;
633
634 // The number of virtual registers used by parameters of this method.
635 uint16_t number_of_in_vregs_;
636
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000637 // Number of vreg size slots that the temporaries use (used in baseline compiler).
638 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100639
Mark Mendell1152c922015-04-24 17:06:35 -0400640 // Has bounds checks. We can totally skip BCE if it's false.
641 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800642
David Brazdil77a48ae2015-09-15 12:34:04 +0000643 // Flag whether there are any try/catch blocks in the graph. We will skip
644 // try/catch-related passes if false.
645 bool has_try_catch_;
646
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000647 // Flag whether there are any irreducible loops in the graph.
648 bool has_irreducible_loops_;
649
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000650 // Indicates whether the graph should be compiled in a way that
651 // ensures full debuggability. If false, we can apply more
652 // aggressive optimizations that may limit the level of debugging.
653 const bool debuggable_;
654
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000655 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000656 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000657
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100658 // The dex file from which the method is from.
659 const DexFile& dex_file_;
660
661 // The method index in the dex file.
662 const uint32_t method_idx_;
663
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100664 // If inlined, this encodes how the callee is being invoked.
665 const InvokeType invoke_type_;
666
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100667 // Whether the graph has been transformed to SSA form. Only used
668 // in debug mode to ensure we are not using properties only valid
669 // for non-SSA form (like the number of temporaries).
670 bool in_ssa_form_;
671
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100672 const bool should_generate_constructor_barrier_;
673
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800674 // Number of CHA guards in the graph. Used to short-circuit the
675 // CHA guard optimization pass when there is no CHA guard left.
676 uint32_t number_of_cha_guards_;
677
Mathieu Chartiere401d142015-04-22 13:56:20 -0700678 const InstructionSet instruction_set_;
679
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000680 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000681 HNullConstant* cached_null_constant_;
682 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000683 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000684 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000685 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000686
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100687 HCurrentMethod* cached_current_method_;
688
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100689 // The ArtMethod this graph is for. Note that for AOT, it may be null,
690 // for example for methods whose declaring class could not be resolved
691 // (such as when the superclass could not be found).
692 ArtMethod* art_method_;
693
David Brazdil4833f5a2015-12-16 10:37:39 +0000694 // Keep the RTI of inexact Object to avoid having to pass stack handle
695 // collection pointer to passes which may create NullConstant.
696 ReferenceTypeInfo inexact_object_rti_;
697
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000698 // Whether we are compiling this graph for on stack replacement: this will
699 // make all loops seen as irreducible and emit special stack maps to mark
700 // compiled code entries which the interpreter can directly jump to.
701 const bool osr_;
702
Mingyao Yang063fc772016-08-02 11:02:54 -0700703 // List of methods that are assumed to have single implementation.
704 ArenaSet<ArtMethod*> cha_single_implementation_list_;
705
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000706 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100707 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000708 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000709 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000710 DISALLOW_COPY_AND_ASSIGN(HGraph);
711};
712
Vladimir Markof9f64412015-09-02 14:05:49 +0100713class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000714 public:
715 HLoopInformation(HBasicBlock* header, HGraph* graph)
716 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100717 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000718 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100719 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100720 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100721 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000722 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100723 back_edges_.reserve(kDefaultNumberOfBackEdges);
724 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100725
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000726 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100727 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000728
729 void Dump(std::ostream& os);
730
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100731 HBasicBlock* GetHeader() const {
732 return header_;
733 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000734
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000735 void SetHeader(HBasicBlock* block) {
736 header_ = block;
737 }
738
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100739 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
740 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
741 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
742
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000743 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100744 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000745 }
746
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100747 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100748 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100749 }
750
David Brazdil46e2a392015-03-16 17:31:52 +0000751 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100752 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100753 }
754
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000755 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100756 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000757 }
758
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100759 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100760
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100761 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100762 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100763 }
764
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100765 // Returns the lifetime position of the back edge that has the
766 // greatest lifetime position.
767 size_t GetLifetimeEnd() const;
768
769 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100770 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100771 }
772
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000773 // Finds blocks that are part of this loop.
774 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100775
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100776 // Returns whether this loop information contains `block`.
777 // Note that this loop information *must* be populated before entering this function.
778 bool Contains(const HBasicBlock& block) const;
779
780 // Returns whether this loop information is an inner loop of `other`.
781 // Note that `other` *must* be populated before entering this function.
782 bool IsIn(const HLoopInformation& other) const;
783
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800784 // Returns true if instruction is not defined within this loop.
785 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700786
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100787 const ArenaBitVector& GetBlocks() const { return blocks_; }
788
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000789 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000790 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000791
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000792 void ClearAllBlocks() {
793 blocks_.ClearAllBits();
794 }
795
David Brazdil3f4a5222016-05-06 12:46:21 +0100796 bool HasBackEdgeNotDominatedByHeader() const;
797
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100798 bool IsPopulated() const {
799 return blocks_.GetHighestBitSet() != -1;
800 }
801
Anton Shaminf89381f2016-05-16 16:44:13 +0600802 bool DominatesAllBackEdges(HBasicBlock* block);
803
David Sehrc757dec2016-11-04 15:48:34 -0700804 bool HasExitEdge() const;
805
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000806 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100807 // Internal recursive implementation of `Populate`.
808 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100809 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100810
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000811 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100812 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000813 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100814 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100815 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100816 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000817
818 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
819};
820
David Brazdilec16f792015-08-19 15:04:01 +0100821// Stores try/catch information for basic blocks.
822// Note that HGraph is constructed so that catch blocks cannot simultaneously
823// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100824class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100825 public:
826 // Try block information constructor.
827 explicit TryCatchInformation(const HTryBoundary& try_entry)
828 : try_entry_(&try_entry),
829 catch_dex_file_(nullptr),
830 catch_type_index_(DexFile::kDexNoIndex16) {
831 DCHECK(try_entry_ != nullptr);
832 }
833
834 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800835 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100836 : try_entry_(nullptr),
837 catch_dex_file_(&dex_file),
838 catch_type_index_(catch_type_index) {}
839
840 bool IsTryBlock() const { return try_entry_ != nullptr; }
841
842 const HTryBoundary& GetTryEntry() const {
843 DCHECK(IsTryBlock());
844 return *try_entry_;
845 }
846
847 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
848
849 bool IsCatchAllTypeIndex() const {
850 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800851 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100852 }
853
Andreas Gampea5b09a62016-11-17 15:21:22 -0800854 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100855 DCHECK(IsCatchBlock());
856 return catch_type_index_;
857 }
858
859 const DexFile& GetCatchDexFile() const {
860 DCHECK(IsCatchBlock());
861 return *catch_dex_file_;
862 }
863
864 private:
865 // One of possibly several TryBoundary instructions entering the block's try.
866 // Only set for try blocks.
867 const HTryBoundary* try_entry_;
868
869 // Exception type information. Only set for catch blocks.
870 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800871 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100872};
873
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100874static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100875static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100876
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000877// A block in a method. Contains the list of instructions represented
878// as a double linked list. Each block knows its predecessors and
879// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100880
Vladimir Markof9f64412015-09-02 14:05:49 +0100881class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000882 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700883 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000884 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000885 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
886 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000887 loop_information_(nullptr),
888 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000889 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100890 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100891 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100892 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000893 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000894 try_catch_information_(nullptr) {
895 predecessors_.reserve(kDefaultNumberOfPredecessors);
896 successors_.reserve(kDefaultNumberOfSuccessors);
897 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
898 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000899
Vladimir Marko60584552015-09-03 13:35:12 +0000900 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100901 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000902 }
903
Vladimir Marko60584552015-09-03 13:35:12 +0000904 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100905 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000906 }
907
David Brazdild26a4112015-11-10 11:07:31 +0000908 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
909 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
910
Vladimir Marko60584552015-09-03 13:35:12 +0000911 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
912 return ContainsElement(successors_, block, start_from);
913 }
914
915 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100916 return dominated_blocks_;
917 }
918
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100919 bool IsEntryBlock() const {
920 return graph_->GetEntryBlock() == this;
921 }
922
923 bool IsExitBlock() const {
924 return graph_->GetExitBlock() == this;
925 }
926
David Brazdil46e2a392015-03-16 17:31:52 +0000927 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000928 bool IsSingleTryBoundary() const;
929
930 // Returns true if this block emits nothing but a jump.
931 bool IsSingleJump() const {
932 HLoopInformation* loop_info = GetLoopInformation();
933 return (IsSingleGoto() || IsSingleTryBoundary())
934 // Back edges generate a suspend check.
935 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
936 }
David Brazdil46e2a392015-03-16 17:31:52 +0000937
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000938 void AddBackEdge(HBasicBlock* back_edge) {
939 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000940 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000941 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100942 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000943 loop_information_->AddBackEdge(back_edge);
944 }
945
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000946 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000947 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000948
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100949 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000950 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600951 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000952
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000953 HBasicBlock* GetDominator() const { return dominator_; }
954 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000955 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
956
957 void RemoveDominatedBlock(HBasicBlock* block) {
958 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100959 }
Vladimir Marko60584552015-09-03 13:35:12 +0000960
961 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
962 ReplaceElement(dominated_blocks_, existing, new_block);
963 }
964
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100965 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000966
967 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100968 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000969 }
970
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100971 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
972 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100973 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100974 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100975 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
976 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000977
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000978 HInstruction* GetFirstInstructionDisregardMoves() const;
979
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000980 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000981 successors_.push_back(block);
982 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000983 }
984
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100985 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
986 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100987 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000988 new_block->predecessors_.push_back(this);
989 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000990 }
991
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000992 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
993 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000994 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000995 new_block->successors_.push_back(this);
996 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000997 }
998
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100999 // Insert `this` between `predecessor` and `successor. This method
1000 // preserves the indicies, and will update the first edge found between
1001 // `predecessor` and `successor`.
1002 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1003 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001004 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001005 successor->predecessors_[predecessor_index] = this;
1006 predecessor->successors_[successor_index] = this;
1007 successors_.push_back(successor);
1008 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001009 }
1010
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001011 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001012 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001013 }
1014
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001015 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001016 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001017 }
1018
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001019 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001020 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001021 }
1022
1023 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001024 predecessors_.push_back(block);
1025 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001026 }
1027
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001028 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001029 DCHECK_EQ(predecessors_.size(), 2u);
1030 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001031 }
1032
David Brazdil769c9e52015-04-27 13:54:09 +01001033 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001034 DCHECK_EQ(successors_.size(), 2u);
1035 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001036 }
1037
David Brazdilfc6a86a2015-06-26 10:33:45 +00001038 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001039 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001040 }
1041
David Brazdilfc6a86a2015-06-26 10:33:45 +00001042 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001043 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001044 }
1045
David Brazdilfc6a86a2015-06-26 10:33:45 +00001046 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001047 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001048 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001049 }
1050
1051 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001052 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001053 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001054 }
1055
1056 // Returns whether the first occurrence of `predecessor` in the list of
1057 // predecessors is at index `idx`.
1058 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001059 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001060 return GetPredecessorIndexOf(predecessor) == idx;
1061 }
1062
David Brazdild7558da2015-09-22 13:04:14 +01001063 // Create a new block between this block and its predecessors. The new block
1064 // is added to the graph, all predecessor edges are relinked to it and an edge
1065 // is created to `this`. Returns the new empty block. Reverse post order or
1066 // loop and try/catch information are not updated.
1067 HBasicBlock* CreateImmediateDominator();
1068
David Brazdilfc6a86a2015-06-26 10:33:45 +00001069 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001070 // created, latter block. Note that this method will add the block to the
1071 // graph, create a Goto at the end of the former block and will create an edge
1072 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001073 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001074 HBasicBlock* SplitBefore(HInstruction* cursor);
1075
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001076 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001077 // created block. Note that this method just updates raw block information,
1078 // like predecessors, successors, dominators, and instruction list. It does not
1079 // update the graph, reverse post order, loop information, nor make sure the
1080 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001081 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1082
1083 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1084 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001085
1086 // Merge `other` at the end of `this`. Successors and dominated blocks of
1087 // `other` are changed to be successors and dominated blocks of `this`. Note
1088 // that this method does not update the graph, reverse post order, loop
1089 // information, nor make sure the blocks are consistent (for example ending
1090 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001091 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001092
1093 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1094 // of `this` are moved to `other`.
1095 // Note that this method does not update the graph, reverse post order, loop
1096 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001097 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001098 void ReplaceWith(HBasicBlock* other);
1099
David Brazdil2d7352b2015-04-20 14:52:42 +01001100 // Merge `other` at the end of `this`. This method updates loops, reverse post
1101 // order, links to predecessors, successors, dominators and deletes the block
1102 // from the graph. The two blocks must be successive, i.e. `this` the only
1103 // predecessor of `other` and vice versa.
1104 void MergeWith(HBasicBlock* other);
1105
1106 // Disconnects `this` from all its predecessors, successors and dominator,
1107 // removes it from all loops it is included in and eventually from the graph.
1108 // The block must not dominate any other block. Predecessors and successors
1109 // are safely updated.
1110 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001111
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001112 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001113 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001114 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001115 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001116 // Replace instruction `initial` with `replacement` within this block.
1117 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1118 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001119 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001120 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001121 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1122 // instruction list. With 'ensure_safety' set to true, it verifies that the
1123 // instruction is not in use and removes it from the use lists of its inputs.
1124 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1125 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001126 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001127
1128 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001129 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001130 }
1131
Roland Levillain6b879dd2014-09-22 17:13:44 +01001132 bool IsLoopPreHeaderFirstPredecessor() const {
1133 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001134 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001135 }
1136
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001137 bool IsFirstPredecessorBackEdge() const {
1138 DCHECK(IsLoopHeader());
1139 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1140 }
1141
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001142 HLoopInformation* GetLoopInformation() const {
1143 return loop_information_;
1144 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001145
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001146 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001147 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001148 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001149 void SetInLoop(HLoopInformation* info) {
1150 if (IsLoopHeader()) {
1151 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001152 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001153 loop_information_ = info;
1154 } else if (loop_information_->Contains(*info->GetHeader())) {
1155 // Block is currently part of an outer loop. Make it part of this inner loop.
1156 // Note that a non loop header having a loop information means this loop information
1157 // has already been populated
1158 loop_information_ = info;
1159 } else {
1160 // Block is part of an inner loop. Do not update the loop information.
1161 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1162 // at this point, because this method is being called while populating `info`.
1163 }
1164 }
1165
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001166 // Raw update of the loop information.
1167 void SetLoopInformation(HLoopInformation* info) {
1168 loop_information_ = info;
1169 }
1170
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001171 bool IsInLoop() const { return loop_information_ != nullptr; }
1172
David Brazdilec16f792015-08-19 15:04:01 +01001173 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1174
1175 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1176 try_catch_information_ = try_catch_information;
1177 }
1178
1179 bool IsTryBlock() const {
1180 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1181 }
1182
1183 bool IsCatchBlock() const {
1184 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1185 }
David Brazdilffee3d32015-07-06 11:48:53 +01001186
1187 // Returns the try entry that this block's successors should have. They will
1188 // be in the same try, unless the block ends in a try boundary. In that case,
1189 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001190 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001191
David Brazdild7558da2015-09-22 13:04:14 +01001192 bool HasThrowingInstructions() const;
1193
David Brazdila4b8c212015-05-07 09:59:30 +01001194 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001195 bool Dominates(HBasicBlock* block) const;
1196
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001197 size_t GetLifetimeStart() const { return lifetime_start_; }
1198 size_t GetLifetimeEnd() const { return lifetime_end_; }
1199
1200 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1201 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1202
David Brazdil8d5b8b22015-03-24 10:51:52 +00001203 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001204 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001205 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001206 bool HasSinglePhi() const;
1207
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001208 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001209 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001210 ArenaVector<HBasicBlock*> predecessors_;
1211 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001212 HInstructionList instructions_;
1213 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001214 HLoopInformation* loop_information_;
1215 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001216 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001217 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001218 // The dex program counter of the first instruction of this block.
1219 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001220 size_t lifetime_start_;
1221 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001222 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001223
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001224 friend class HGraph;
1225 friend class HInstruction;
1226
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001227 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1228};
1229
David Brazdilb2bd1c52015-03-25 11:17:37 +00001230// Iterates over the LoopInformation of all loops which contain 'block'
1231// from the innermost to the outermost.
1232class HLoopInformationOutwardIterator : public ValueObject {
1233 public:
1234 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1235 : current_(block.GetLoopInformation()) {}
1236
1237 bool Done() const { return current_ == nullptr; }
1238
1239 void Advance() {
1240 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001241 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001242 }
1243
1244 HLoopInformation* Current() const {
1245 DCHECK(!Done());
1246 return current_;
1247 }
1248
1249 private:
1250 HLoopInformation* current_;
1251
1252 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1253};
1254
Alexandre Ramesef20f712015-06-09 10:29:30 +01001255#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001256 M(Above, Condition) \
1257 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001258 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001259 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001260 M(ArrayGet, Instruction) \
1261 M(ArrayLength, Instruction) \
1262 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001263 M(Below, Condition) \
1264 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001265 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001266 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001267 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001268 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001269 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001270 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001271 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001272 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001273 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001274 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001275 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001276 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001277 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001278 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001279 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001280 M(Exit, Instruction) \
1281 M(FloatConstant, Constant) \
1282 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001283 M(GreaterThan, Condition) \
1284 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001285 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001286 M(InstanceFieldGet, Instruction) \
1287 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001288 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001289 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001290 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001291 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001292 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001293 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001294 M(LessThan, Condition) \
1295 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001296 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001297 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001298 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001299 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001300 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001301 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001302 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001303 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001304 M(Neg, UnaryOperation) \
1305 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001306 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001307 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001308 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001309 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001310 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001311 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001312 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001313 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001314 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001315 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001316 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001317 M(Return, Instruction) \
1318 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001319 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001320 M(Shl, BinaryOperation) \
1321 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001322 M(StaticFieldGet, Instruction) \
1323 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001324 M(UnresolvedInstanceFieldGet, Instruction) \
1325 M(UnresolvedInstanceFieldSet, Instruction) \
1326 M(UnresolvedStaticFieldGet, Instruction) \
1327 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001328 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001329 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001330 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001331 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001332 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001333 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001334 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001335 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001336
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001337/*
1338 * Instructions, shared across several (not all) architectures.
1339 */
1340#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1341#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1342#else
1343#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001344 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001345 M(MultiplyAccumulate, Instruction) \
1346 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001347#endif
1348
Vladimir Markob4536b72015-11-24 13:45:23 +00001349#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001350#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001351#else
1352#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1353 M(ArmDexCacheArraysBase, Instruction)
1354#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001355
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001356#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001357#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001358#else
1359#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001360 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001361#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001362
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001363#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001364#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001365#else
1366#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1367 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001368 M(MipsDexCacheArraysBase, Instruction) \
1369 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001370#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001371
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001372#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1373
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001374#ifndef ART_ENABLE_CODEGEN_x86
1375#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1376#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001377#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1378 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001379 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001380 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001381 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001382#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001383
1384#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1385
1386#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1387 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001388 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001389 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1390 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001391 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001392 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001393 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1394 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1395
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001396#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1397 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001398 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001399 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001400 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001401 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001402
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001403#define FOR_EACH_INSTRUCTION(M) \
1404 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1405 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1406
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001407#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001408FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1409#undef FORWARD_DECLARATION
1410
Vladimir Marko372f10e2016-05-17 16:30:10 +01001411#define DECLARE_INSTRUCTION(type) \
1412 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1413 const char* DebugName() const OVERRIDE { return #type; } \
1414 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1415 return other->Is##type(); \
1416 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001417 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001418
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001419#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1420 bool Is##type() const { return As##type() != nullptr; } \
1421 const H##type* As##type() const { return this; } \
1422 H##type* As##type() { return this; }
1423
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001424template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001425class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001426 public:
David Brazdiled596192015-01-23 10:39:45 +00001427 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001428 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001429 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001430
Vladimir Marko46817b82016-03-29 12:21:58 +01001431 // Hook for the IntrusiveForwardList<>.
1432 // TODO: Hide this better.
1433 IntrusiveForwardListHook hook;
1434
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001435 private:
David Brazdiled596192015-01-23 10:39:45 +00001436 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001437 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001438
1439 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001440 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001441
Vladimir Marko46817b82016-03-29 12:21:58 +01001442 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001443
1444 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1445};
1446
David Brazdiled596192015-01-23 10:39:45 +00001447template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001448using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001449
David Brazdil1abb4192015-02-17 18:33:36 +00001450// This class is used by HEnvironment and HInstruction classes to record the
1451// instructions they use and pointers to the corresponding HUseListNodes kept
1452// by the used instructions.
1453template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001454class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001455 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001456 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1457 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001458
Vladimir Marko46817b82016-03-29 12:21:58 +01001459 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1460 : HUserRecord(old_record.instruction_, before_use_node) {}
1461 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1462 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001463 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001464 }
1465
1466 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001467 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1468 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001469
1470 private:
1471 // Instruction used by the user.
1472 HInstruction* instruction_;
1473
Vladimir Marko46817b82016-03-29 12:21:58 +01001474 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1475 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001476};
1477
Vladimir Markoe9004912016-06-16 16:50:52 +01001478// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1479// This is used for HInstruction::GetInputs() to return a container wrapper providing
1480// HInstruction* values even though the underlying container has HUserRecord<>s.
1481struct HInputExtractor {
1482 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1483 return record.GetInstruction();
1484 }
1485 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1486 return record.GetInstruction();
1487 }
1488};
1489
1490using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1491using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1492
Aart Bik854a02b2015-07-14 16:07:00 -07001493/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001494 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001495 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001496 * For write/read dependences on fields/arrays, the dependence analysis uses
1497 * type disambiguation (e.g. a float field write cannot modify the value of an
1498 * integer field read) and the access type (e.g. a reference array write cannot
1499 * modify the value of a reference field read [although it may modify the
1500 * reference fetch prior to reading the field, which is represented by its own
1501 * write/read dependence]). The analysis makes conservative points-to
1502 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1503 * the same, and any reference read depends on any reference read without
1504 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001505 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001506 * The internal representation uses 38-bit and is described in the table below.
1507 * The first line indicates the side effect, and for field/array accesses the
1508 * second line indicates the type of the access (in the order of the
1509 * Primitive::Type enum).
1510 * The two numbered lines below indicate the bit position in the bitfield (read
1511 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001512 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001513 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1514 * +-------------+---------+---------+--------------+---------+---------+
1515 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1516 * | 3 |333333322|222222221| 1 |111111110|000000000|
1517 * | 7 |654321098|765432109| 8 |765432109|876543210|
1518 *
1519 * Note that, to ease the implementation, 'changes' bits are least significant
1520 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001521 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001522class SideEffects : public ValueObject {
1523 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001524 SideEffects() : flags_(0) {}
1525
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001526 static SideEffects None() {
1527 return SideEffects(0);
1528 }
1529
1530 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001531 return SideEffects(kAllChangeBits | kAllDependOnBits);
1532 }
1533
1534 static SideEffects AllChanges() {
1535 return SideEffects(kAllChangeBits);
1536 }
1537
1538 static SideEffects AllDependencies() {
1539 return SideEffects(kAllDependOnBits);
1540 }
1541
1542 static SideEffects AllExceptGCDependency() {
1543 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1544 }
1545
1546 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001547 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001548 }
1549
Aart Bik34c3ba92015-07-20 14:08:59 -07001550 static SideEffects AllWrites() {
1551 return SideEffects(kAllWrites);
1552 }
1553
1554 static SideEffects AllReads() {
1555 return SideEffects(kAllReads);
1556 }
1557
1558 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1559 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001560 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001561 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001562 }
1563
Aart Bik854a02b2015-07-14 16:07:00 -07001564 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001565 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001566 }
1567
Aart Bik34c3ba92015-07-20 14:08:59 -07001568 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1569 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001570 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001571 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001572 }
1573
1574 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001575 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001576 }
1577
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001578 static SideEffects CanTriggerGC() {
1579 return SideEffects(1ULL << kCanTriggerGCBit);
1580 }
1581
1582 static SideEffects DependsOnGC() {
1583 return SideEffects(1ULL << kDependsOnGCBit);
1584 }
1585
Aart Bik854a02b2015-07-14 16:07:00 -07001586 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001587 SideEffects Union(SideEffects other) const {
1588 return SideEffects(flags_ | other.flags_);
1589 }
1590
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001591 SideEffects Exclusion(SideEffects other) const {
1592 return SideEffects(flags_ & ~other.flags_);
1593 }
1594
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001595 void Add(SideEffects other) {
1596 flags_ |= other.flags_;
1597 }
1598
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001599 bool Includes(SideEffects other) const {
1600 return (other.flags_ & flags_) == other.flags_;
1601 }
1602
1603 bool HasSideEffects() const {
1604 return (flags_ & kAllChangeBits);
1605 }
1606
1607 bool HasDependencies() const {
1608 return (flags_ & kAllDependOnBits);
1609 }
1610
1611 // Returns true if there are no side effects or dependencies.
1612 bool DoesNothing() const {
1613 return flags_ == 0;
1614 }
1615
Aart Bik854a02b2015-07-14 16:07:00 -07001616 // Returns true if something is written.
1617 bool DoesAnyWrite() const {
1618 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001619 }
1620
Aart Bik854a02b2015-07-14 16:07:00 -07001621 // Returns true if something is read.
1622 bool DoesAnyRead() const {
1623 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001624 }
1625
Aart Bik854a02b2015-07-14 16:07:00 -07001626 // Returns true if potentially everything is written and read
1627 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001628 bool DoesAllReadWrite() const {
1629 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1630 }
1631
Aart Bik854a02b2015-07-14 16:07:00 -07001632 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001633 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001634 }
1635
Roland Levillain0d5a2812015-11-13 10:07:31 +00001636 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001637 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001638 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1639 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001640 }
1641
1642 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001643 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001644 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001645 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001646 for (int s = kLastBit; s >= 0; s--) {
1647 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1648 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1649 // This is a bit for the GC side effect.
1650 if (current_bit_is_set) {
1651 flags += "GC";
1652 }
Aart Bik854a02b2015-07-14 16:07:00 -07001653 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001654 } else {
1655 // This is a bit for the array/field analysis.
1656 // The underscore character stands for the 'can trigger GC' bit.
1657 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1658 if (current_bit_is_set) {
1659 flags += kDebug[s];
1660 }
1661 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1662 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1663 flags += "|";
1664 }
1665 }
Aart Bik854a02b2015-07-14 16:07:00 -07001666 }
1667 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001668 }
1669
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001670 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001671
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001672 private:
1673 static constexpr int kFieldArrayAnalysisBits = 9;
1674
1675 static constexpr int kFieldWriteOffset = 0;
1676 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1677 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1678 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1679
1680 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1681
1682 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1683 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1684 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1685 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1686
1687 static constexpr int kLastBit = kDependsOnGCBit;
1688 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1689
1690 // Aliases.
1691
1692 static_assert(kChangeBits == kDependOnBits,
1693 "the 'change' bits should match the 'depend on' bits.");
1694
1695 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1696 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1697 static constexpr uint64_t kAllWrites =
1698 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1699 static constexpr uint64_t kAllReads =
1700 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001701
Aart Bik854a02b2015-07-14 16:07:00 -07001702 // Translates type to bit flag.
1703 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1704 CHECK_NE(type, Primitive::kPrimVoid);
1705 const uint64_t one = 1;
1706 const int shift = type; // 0-based consecutive enum
1707 DCHECK_LE(kFieldWriteOffset, shift);
1708 DCHECK_LT(shift, kArrayWriteOffset);
1709 return one << (type + offset);
1710 }
1711
1712 // Private constructor on direct flags value.
1713 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1714
1715 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001716};
1717
David Brazdiled596192015-01-23 10:39:45 +00001718// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001719class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001720 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001721 HEnvironment(ArenaAllocator* arena,
1722 size_t number_of_vregs,
1723 const DexFile& dex_file,
1724 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001725 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001726 InvokeType invoke_type,
1727 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001728 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1729 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001730 parent_(nullptr),
1731 dex_file_(dex_file),
1732 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001733 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001734 invoke_type_(invoke_type),
1735 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001736 }
1737
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001738 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001739 : HEnvironment(arena,
1740 to_copy.Size(),
1741 to_copy.GetDexFile(),
1742 to_copy.GetMethodIdx(),
1743 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001744 to_copy.GetInvokeType(),
1745 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001746
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001747 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001748 if (parent_ != nullptr) {
1749 parent_->SetAndCopyParentChain(allocator, parent);
1750 } else {
1751 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1752 parent_->CopyFrom(parent);
1753 if (parent->GetParent() != nullptr) {
1754 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1755 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001756 }
David Brazdiled596192015-01-23 10:39:45 +00001757 }
1758
Vladimir Marko71bf8092015-09-15 15:33:14 +01001759 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001760 void CopyFrom(HEnvironment* environment);
1761
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001762 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1763 // input to the loop phi instead. This is for inserting instructions that
1764 // require an environment (like HDeoptimization) in the loop pre-header.
1765 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001766
1767 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001768 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001769 }
1770
1771 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001772 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001773 }
1774
David Brazdil1abb4192015-02-17 18:33:36 +00001775 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001776
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001777 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001778
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001779 HEnvironment* GetParent() const { return parent_; }
1780
1781 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001782 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001783 }
1784
1785 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001786 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001787 }
1788
1789 uint32_t GetDexPc() const {
1790 return dex_pc_;
1791 }
1792
1793 uint32_t GetMethodIdx() const {
1794 return method_idx_;
1795 }
1796
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001797 InvokeType GetInvokeType() const {
1798 return invoke_type_;
1799 }
1800
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001801 const DexFile& GetDexFile() const {
1802 return dex_file_;
1803 }
1804
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001805 HInstruction* GetHolder() const {
1806 return holder_;
1807 }
1808
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001809
1810 bool IsFromInlinedInvoke() const {
1811 return GetParent() != nullptr;
1812 }
1813
David Brazdiled596192015-01-23 10:39:45 +00001814 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001815 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1816 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001817 HEnvironment* parent_;
1818 const DexFile& dex_file_;
1819 const uint32_t method_idx_;
1820 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001821 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001822
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001823 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001824 HInstruction* const holder_;
1825
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001826 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001827
1828 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1829};
1830
Vladimir Markof9f64412015-09-02 14:05:49 +01001831class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001832 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001833 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001834 : previous_(nullptr),
1835 next_(nullptr),
1836 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001837 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001838 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001839 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001840 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001841 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001842 locations_(nullptr),
1843 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001844 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001845 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001846 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1847 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1848 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001849
Dave Allison20dfc792014-06-16 20:44:29 -07001850 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001851
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001852#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001853 enum InstructionKind {
1854 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1855 };
1856#undef DECLARE_KIND
1857
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001858 HInstruction* GetNext() const { return next_; }
1859 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001860
Calin Juravle77520bc2015-01-12 18:45:46 +00001861 HInstruction* GetNextDisregardingMoves() const;
1862 HInstruction* GetPreviousDisregardingMoves() const;
1863
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001864 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001865 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001866 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001867 bool IsInBlock() const { return block_ != nullptr; }
1868 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001869 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1870 bool IsIrreducibleLoopHeaderPhi() const {
1871 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1872 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001873
Vladimir Marko372f10e2016-05-17 16:30:10 +01001874 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1875
1876 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1877 // One virtual method is enough, just const_cast<> and then re-add the const.
1878 return ArrayRef<const HUserRecord<HInstruction*>>(
1879 const_cast<HInstruction*>(this)->GetInputRecords());
1880 }
1881
Vladimir Markoe9004912016-06-16 16:50:52 +01001882 HInputsRef GetInputs() {
1883 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001884 }
1885
Vladimir Markoe9004912016-06-16 16:50:52 +01001886 HConstInputsRef GetInputs() const {
1887 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001888 }
1889
1890 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001891 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001892
Aart Bik2767f4b2016-10-28 15:03:53 -07001893 bool HasInput(HInstruction* input) const {
1894 for (const HInstruction* i : GetInputs()) {
1895 if (i == input) {
1896 return true;
1897 }
1898 }
1899 return false;
1900 }
1901
Vladimir Marko372f10e2016-05-17 16:30:10 +01001902 void SetRawInputAt(size_t index, HInstruction* input) {
1903 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1904 }
1905
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001906 virtual void Accept(HGraphVisitor* visitor) = 0;
1907 virtual const char* DebugName() const = 0;
1908
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001909 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1910
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001911 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001912
1913 uint32_t GetDexPc() const { return dex_pc_; }
1914
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001915 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001916
Roland Levillaine161a2a2014-10-03 12:45:18 +01001917 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001918 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001919
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001920 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001921 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001922
Calin Juravle10e244f2015-01-26 18:54:32 +00001923 // Does not apply for all instructions, but having this at top level greatly
1924 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001925 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001926 virtual bool CanBeNull() const {
1927 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1928 return true;
1929 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001930
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001931 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001932 return false;
1933 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001934
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001935 virtual bool IsActualObject() const {
1936 return GetType() == Primitive::kPrimNot;
1937 }
1938
Calin Juravle2e768302015-07-28 14:41:11 +00001939 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001940
Calin Juravle61d544b2015-02-23 16:46:57 +00001941 ReferenceTypeInfo GetReferenceTypeInfo() const {
1942 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001943 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001944 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001945 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001946
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001947 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001948 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001949 // Note: fixup_end remains valid across push_front().
1950 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1951 HUseListNode<HInstruction*>* new_node =
1952 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1953 uses_.push_front(*new_node);
1954 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001955 }
1956
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001957 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001958 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001959 // Note: env_fixup_end remains valid across push_front().
1960 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1961 HUseListNode<HEnvironment*>* new_node =
1962 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1963 env_uses_.push_front(*new_node);
1964 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001965 }
1966
David Brazdil1abb4192015-02-17 18:33:36 +00001967 void RemoveAsUserOfInput(size_t input) {
1968 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001969 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1970 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1971 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001972 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001973
Vladimir Marko372f10e2016-05-17 16:30:10 +01001974 void RemoveAsUserOfAllInputs() {
1975 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1976 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1977 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1978 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1979 }
1980 }
1981
David Brazdil1abb4192015-02-17 18:33:36 +00001982 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1983 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001984
Vladimir Marko46817b82016-03-29 12:21:58 +01001985 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1986 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1987 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001988 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001989 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001990 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001991
Aart Bikcc42be02016-10-20 16:14:16 -07001992 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07001993 return
Aart Bikff7d89c2016-11-07 08:49:28 -08001994 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07001995 !CanThrow() &&
1996 !IsSuspendCheck() &&
1997 !IsControlFlow() &&
1998 !IsNativeDebugInfo() &&
1999 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002000 // If we added an explicit barrier then we should keep it.
2001 !IsMemoryBarrier();
2002 }
2003
Aart Bikcc42be02016-10-20 16:14:16 -07002004 bool IsDeadAndRemovable() const {
2005 return IsRemovable() && !HasUses();
2006 }
2007
Roland Levillain6c82d402014-10-13 16:10:27 +01002008 // Does this instruction strictly dominate `other_instruction`?
2009 // Returns false if this instruction and `other_instruction` are the same.
2010 // Aborts if this instruction and `other_instruction` are both phis.
2011 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002012
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002013 int GetId() const { return id_; }
2014 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002015
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002016 int GetSsaIndex() const { return ssa_index_; }
2017 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2018 bool HasSsaIndex() const { return ssa_index_ != -1; }
2019
2020 bool HasEnvironment() const { return environment_ != nullptr; }
2021 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002022 // Set the `environment_` field. Raw because this method does not
2023 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002024 void SetRawEnvironment(HEnvironment* environment) {
2025 DCHECK(environment_ == nullptr);
2026 DCHECK_EQ(environment->GetHolder(), this);
2027 environment_ = environment;
2028 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002029
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002030 void InsertRawEnvironment(HEnvironment* environment) {
2031 DCHECK(environment_ != nullptr);
2032 DCHECK_EQ(environment->GetHolder(), this);
2033 DCHECK(environment->GetParent() == nullptr);
2034 environment->parent_ = environment_;
2035 environment_ = environment;
2036 }
2037
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002038 void RemoveEnvironment();
2039
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002040 // Set the environment of this instruction, copying it from `environment`. While
2041 // copying, the uses lists are being updated.
2042 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002043 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002044 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002045 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002046 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002047 if (environment->GetParent() != nullptr) {
2048 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2049 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002050 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002051
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002052 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2053 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002054 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002055 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002056 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002057 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002058 if (environment->GetParent() != nullptr) {
2059 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2060 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002061 }
2062
Nicolas Geoffray39468442014-09-02 15:17:15 +01002063 // Returns the number of entries in the environment. Typically, that is the
2064 // number of dex registers in a method. It could be more in case of inlining.
2065 size_t EnvironmentSize() const;
2066
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002067 LocationSummary* GetLocations() const { return locations_; }
2068 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002069
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002070 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002071 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002072
Alexandre Rames188d4312015-04-09 18:30:21 +01002073 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2074 // uses of this instruction by `other` are *not* updated.
2075 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2076 ReplaceWith(other);
2077 other->ReplaceInput(this, use_index);
2078 }
2079
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002080 // Move `this` instruction before `cursor`.
2081 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002082
Vladimir Markofb337ea2015-11-25 15:25:10 +00002083 // Move `this` before its first user and out of any loops. If there is no
2084 // out-of-loop user that dominates all other users, move the instruction
2085 // to the end of the out-of-loop common dominator of the user's blocks.
2086 //
2087 // This can be used only on non-throwing instructions with no side effects that
2088 // have at least one use but no environment uses.
2089 void MoveBeforeFirstUserAndOutOfLoops();
2090
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002091#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002092 bool Is##type() const; \
2093 const H##type* As##type() const; \
2094 H##type* As##type();
2095
2096 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2097#undef INSTRUCTION_TYPE_CHECK
2098
2099#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002100 bool Is##type() const { return (As##type() != nullptr); } \
2101 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002102 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002103 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002104#undef INSTRUCTION_TYPE_CHECK
2105
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002106 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002107 // TODO: this method is used by LICM and GVN with possibly different
2108 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002109 virtual bool CanBeMoved() const { return false; }
2110
2111 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002112 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002113 return false;
2114 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002115
2116 // Returns whether any data encoded in the two instructions is equal.
2117 // This method does not look at the inputs. Both instructions must be
2118 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002119 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002120 return false;
2121 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002122
2123 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002124 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002125 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002126 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002127
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002128 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2129 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2130 // the virtual function because the __attribute__((__pure__)) doesn't really
2131 // apply the strong requirement for virtual functions, preventing optimizations.
2132 InstructionKind GetKind() const PURE;
2133 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002134
2135 virtual size_t ComputeHashCode() const {
2136 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002137 for (const HInstruction* input : GetInputs()) {
2138 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002139 }
2140 return result;
2141 }
2142
2143 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002144 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002145 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002146
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002147 size_t GetLifetimePosition() const { return lifetime_position_; }
2148 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2149 LiveInterval* GetLiveInterval() const { return live_interval_; }
2150 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2151 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2152
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002153 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2154
2155 // Returns whether the code generation of the instruction will require to have access
2156 // to the current method. Such instructions are:
2157 // (1): Instructions that require an environment, as calling the runtime requires
2158 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002159 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2160 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002161 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002162 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002163 }
2164
Vladimir Markodc151b22015-10-15 18:02:30 +01002165 // Returns whether the code generation of the instruction will require to have access
2166 // to the dex cache of the current method's declaring class via the current method.
2167 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002168
Mark Mendellc4701932015-04-10 13:18:51 -04002169 // Does this instruction have any use in an environment before
2170 // control flow hits 'other'?
2171 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2172
2173 // Remove all references to environment uses of this instruction.
2174 // The caller must ensure that this is safe to do.
2175 void RemoveEnvironmentUsers();
2176
Vladimir Markoa1de9182016-02-25 11:37:38 +00002177 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2178 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002179
David Brazdil1abb4192015-02-17 18:33:36 +00002180 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002181 // If set, the machine code for this instruction is assumed to be generated by
2182 // its users. Used by liveness analysis to compute use positions accordingly.
2183 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2184 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2185 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2186 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2187
Vladimir Marko372f10e2016-05-17 16:30:10 +01002188 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2189 return GetInputRecords()[i];
2190 }
2191
2192 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2193 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2194 input_records[index] = input;
2195 }
David Brazdil1abb4192015-02-17 18:33:36 +00002196
Vladimir Markoa1de9182016-02-25 11:37:38 +00002197 uint32_t GetPackedFields() const {
2198 return packed_fields_;
2199 }
2200
2201 template <size_t flag>
2202 bool GetPackedFlag() const {
2203 return (packed_fields_ & (1u << flag)) != 0u;
2204 }
2205
2206 template <size_t flag>
2207 void SetPackedFlag(bool value = true) {
2208 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2209 }
2210
2211 template <typename BitFieldType>
2212 typename BitFieldType::value_type GetPackedField() const {
2213 return BitFieldType::Decode(packed_fields_);
2214 }
2215
2216 template <typename BitFieldType>
2217 void SetPackedField(typename BitFieldType::value_type value) {
2218 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2219 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2220 }
2221
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002222 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002223 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2224 auto before_use_node = uses_.before_begin();
2225 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2226 HInstruction* user = use_node->GetUser();
2227 size_t input_index = use_node->GetIndex();
2228 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2229 before_use_node = use_node;
2230 }
2231 }
2232
2233 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2234 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2235 if (next != uses_.end()) {
2236 HInstruction* next_user = next->GetUser();
2237 size_t next_index = next->GetIndex();
2238 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2239 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2240 }
2241 }
2242
2243 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2244 auto before_env_use_node = env_uses_.before_begin();
2245 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2246 HEnvironment* user = env_use_node->GetUser();
2247 size_t input_index = env_use_node->GetIndex();
2248 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2249 before_env_use_node = env_use_node;
2250 }
2251 }
2252
2253 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2254 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2255 if (next != env_uses_.end()) {
2256 HEnvironment* next_user = next->GetUser();
2257 size_t next_index = next->GetIndex();
2258 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2259 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2260 }
2261 }
David Brazdil1abb4192015-02-17 18:33:36 +00002262
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002263 HInstruction* previous_;
2264 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002265 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002266 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002267
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002268 // An instruction gets an id when it is added to the graph.
2269 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002270 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002271 int id_;
2272
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002273 // When doing liveness analysis, instructions that have uses get an SSA index.
2274 int ssa_index_;
2275
Vladimir Markoa1de9182016-02-25 11:37:38 +00002276 // Packed fields.
2277 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002278
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002279 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002280 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002281
2282 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002283 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002284
Nicolas Geoffray39468442014-09-02 15:17:15 +01002285 // The environment associated with this instruction. Not null if the instruction
2286 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002287 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002288
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002289 // Set by the code generator.
2290 LocationSummary* locations_;
2291
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002292 // Set by the liveness analysis.
2293 LiveInterval* live_interval_;
2294
2295 // Set by the liveness analysis, this is the position in a linear
2296 // order of blocks where this instruction's live interval start.
2297 size_t lifetime_position_;
2298
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002299 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002300
Vladimir Markoa1de9182016-02-25 11:37:38 +00002301 // The reference handle part of the reference type info.
2302 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002303 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002304 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002305
David Brazdil1abb4192015-02-17 18:33:36 +00002306 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002307 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002308 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002309 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002310 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002311
2312 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2313};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002314std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002315
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002316// Iterates over the instructions, while preserving the next instruction
2317// in case the current instruction gets removed from the list by the user
2318// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002319class HInstructionIterator : public ValueObject {
2320 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002321 explicit HInstructionIterator(const HInstructionList& instructions)
2322 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002323 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002324 }
2325
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002326 bool Done() const { return instruction_ == nullptr; }
2327 HInstruction* Current() const { return instruction_; }
2328 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002329 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002330 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002331 }
2332
2333 private:
2334 HInstruction* instruction_;
2335 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002336
2337 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002338};
2339
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002340// Iterates over the instructions without saving the next instruction,
2341// therefore handling changes in the graph potentially made by the user
2342// of this iterator.
2343class HInstructionIteratorHandleChanges : public ValueObject {
2344 public:
2345 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2346 : instruction_(instructions.first_instruction_) {
2347 }
2348
2349 bool Done() const { return instruction_ == nullptr; }
2350 HInstruction* Current() const { return instruction_; }
2351 void Advance() {
2352 instruction_ = instruction_->GetNext();
2353 }
2354
2355 private:
2356 HInstruction* instruction_;
2357
2358 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2359};
2360
2361
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002362class HBackwardInstructionIterator : public ValueObject {
2363 public:
2364 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2365 : instruction_(instructions.last_instruction_) {
2366 next_ = Done() ? nullptr : instruction_->GetPrevious();
2367 }
2368
2369 bool Done() const { return instruction_ == nullptr; }
2370 HInstruction* Current() const { return instruction_; }
2371 void Advance() {
2372 instruction_ = next_;
2373 next_ = Done() ? nullptr : instruction_->GetPrevious();
2374 }
2375
2376 private:
2377 HInstruction* instruction_;
2378 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002379
2380 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002381};
2382
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002383class HVariableInputSizeInstruction : public HInstruction {
2384 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002385 using HInstruction::GetInputRecords; // Keep the const version visible.
2386 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2387 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2388 }
2389
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002390 void AddInput(HInstruction* input);
2391 void InsertInputAt(size_t index, HInstruction* input);
2392 void RemoveInputAt(size_t index);
2393
2394 protected:
2395 HVariableInputSizeInstruction(SideEffects side_effects,
2396 uint32_t dex_pc,
2397 ArenaAllocator* arena,
2398 size_t number_of_inputs,
2399 ArenaAllocKind kind)
2400 : HInstruction(side_effects, dex_pc),
2401 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2402
2403 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2404
2405 private:
2406 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2407};
2408
Vladimir Markof9f64412015-09-02 14:05:49 +01002409template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002410class HTemplateInstruction: public HInstruction {
2411 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002412 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002413 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002414 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002415
Vladimir Marko372f10e2016-05-17 16:30:10 +01002416 using HInstruction::GetInputRecords; // Keep the const version visible.
2417 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2418 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002419 }
2420
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002421 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002422 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002423
2424 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002425};
2426
Vladimir Markof9f64412015-09-02 14:05:49 +01002427// HTemplateInstruction specialization for N=0.
2428template<>
2429class HTemplateInstruction<0>: public HInstruction {
2430 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002431 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002432 : HInstruction(side_effects, dex_pc) {}
2433
Vladimir Markof9f64412015-09-02 14:05:49 +01002434 virtual ~HTemplateInstruction() {}
2435
Vladimir Marko372f10e2016-05-17 16:30:10 +01002436 using HInstruction::GetInputRecords; // Keep the const version visible.
2437 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2438 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002439 }
2440
2441 private:
2442 friend class SsaBuilder;
2443};
2444
Dave Allison20dfc792014-06-16 20:44:29 -07002445template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002446class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002447 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002448 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002449 : HTemplateInstruction<N>(side_effects, dex_pc) {
2450 this->template SetPackedField<TypeField>(type);
2451 }
Dave Allison20dfc792014-06-16 20:44:29 -07002452 virtual ~HExpression() {}
2453
Vladimir Markoa1de9182016-02-25 11:37:38 +00002454 Primitive::Type GetType() const OVERRIDE {
2455 return TypeField::Decode(this->GetPackedFields());
2456 }
Dave Allison20dfc792014-06-16 20:44:29 -07002457
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002458 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002459 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2460 static constexpr size_t kFieldTypeSize =
2461 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2462 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2463 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2464 "Too many packed fields.");
2465 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002466};
2467
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002468// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2469// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002470class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002471 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002472 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2473 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002474
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002475 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002476
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002477 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002478
2479 private:
2480 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2481};
2482
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002483// Represents dex's RETURN opcodes. A HReturn is a control flow
2484// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002485class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002486 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002487 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2488 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002489 SetRawInputAt(0, value);
2490 }
2491
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002492 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002493
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002494 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002495
2496 private:
2497 DISALLOW_COPY_AND_ASSIGN(HReturn);
2498};
2499
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002500class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002501 public:
2502 HPhi(ArenaAllocator* arena,
2503 uint32_t reg_number,
2504 size_t number_of_inputs,
2505 Primitive::Type type,
2506 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002507 : HVariableInputSizeInstruction(
2508 SideEffects::None(),
2509 dex_pc,
2510 arena,
2511 number_of_inputs,
2512 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002513 reg_number_(reg_number) {
2514 SetPackedField<TypeField>(ToPhiType(type));
2515 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2516 // Phis are constructed live and marked dead if conflicting or unused.
2517 // Individual steps of SsaBuilder should assume that if a phi has been
2518 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2519 SetPackedFlag<kFlagIsLive>(true);
2520 SetPackedFlag<kFlagCanBeNull>(true);
2521 }
2522
2523 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2524 static Primitive::Type ToPhiType(Primitive::Type type) {
2525 return Primitive::PrimitiveKind(type);
2526 }
2527
2528 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2529
David Brazdildee58d62016-04-07 09:54:26 +00002530 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2531 void SetType(Primitive::Type new_type) {
2532 // Make sure that only valid type changes occur. The following are allowed:
2533 // (1) int -> float/ref (primitive type propagation),
2534 // (2) long -> double (primitive type propagation).
2535 DCHECK(GetType() == new_type ||
2536 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2537 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2538 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2539 SetPackedField<TypeField>(new_type);
2540 }
2541
2542 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2543 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2544
2545 uint32_t GetRegNumber() const { return reg_number_; }
2546
2547 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2548 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2549 bool IsDead() const { return !IsLive(); }
2550 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2551
Vladimir Markoe9004912016-06-16 16:50:52 +01002552 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002553 return other != nullptr
2554 && other->IsPhi()
2555 && other->AsPhi()->GetBlock() == GetBlock()
2556 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2557 }
2558
2559 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2560 // An equivalent phi is a phi having the same dex register and type.
2561 // It assumes that phis with the same dex register are adjacent.
2562 HPhi* GetNextEquivalentPhiWithSameType() {
2563 HInstruction* next = GetNext();
2564 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2565 if (next->GetType() == GetType()) {
2566 return next->AsPhi();
2567 }
2568 next = next->GetNext();
2569 }
2570 return nullptr;
2571 }
2572
2573 DECLARE_INSTRUCTION(Phi);
2574
David Brazdildee58d62016-04-07 09:54:26 +00002575 private:
2576 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2577 static constexpr size_t kFieldTypeSize =
2578 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2579 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2580 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2581 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2582 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2583 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2584
David Brazdildee58d62016-04-07 09:54:26 +00002585 const uint32_t reg_number_;
2586
2587 DISALLOW_COPY_AND_ASSIGN(HPhi);
2588};
2589
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002590// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002591// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002592// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002593class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002594 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002595 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002596
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002597 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002598
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002599 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002600
2601 private:
2602 DISALLOW_COPY_AND_ASSIGN(HExit);
2603};
2604
2605// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002606class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002607 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002608 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002609
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002610 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002611
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002612 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002613 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002614 }
2615
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002616 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002617
2618 private:
2619 DISALLOW_COPY_AND_ASSIGN(HGoto);
2620};
2621
Roland Levillain9867bc72015-08-05 10:21:34 +01002622class HConstant : public HExpression<0> {
2623 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002624 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2625 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002626
2627 bool CanBeMoved() const OVERRIDE { return true; }
2628
Roland Levillain1a653882016-03-18 18:05:57 +00002629 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002630 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002631 // Is this constant 0 in the arithmetic sense?
2632 virtual bool IsArithmeticZero() const { return false; }
2633 // Is this constant a 0-bit pattern?
2634 virtual bool IsZeroBitPattern() const { return false; }
2635 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002636 virtual bool IsOne() const { return false; }
2637
David Brazdil77a48ae2015-09-15 12:34:04 +00002638 virtual uint64_t GetValueAsUint64() const = 0;
2639
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002640 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002641
2642 private:
2643 DISALLOW_COPY_AND_ASSIGN(HConstant);
2644};
2645
Vladimir Markofcb503c2016-05-18 12:48:17 +01002646class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002647 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002648 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002649 return true;
2650 }
2651
David Brazdil77a48ae2015-09-15 12:34:04 +00002652 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2653
Roland Levillain9867bc72015-08-05 10:21:34 +01002654 size_t ComputeHashCode() const OVERRIDE { return 0; }
2655
Roland Levillain1a653882016-03-18 18:05:57 +00002656 // The null constant representation is a 0-bit pattern.
2657 virtual bool IsZeroBitPattern() const { return true; }
2658
Roland Levillain9867bc72015-08-05 10:21:34 +01002659 DECLARE_INSTRUCTION(NullConstant);
2660
2661 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002662 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002663
2664 friend class HGraph;
2665 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2666};
2667
2668// Constants of the type int. Those can be from Dex instructions, or
2669// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002670class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002671 public:
2672 int32_t GetValue() const { return value_; }
2673
David Brazdil9f389d42015-10-01 14:32:56 +01002674 uint64_t GetValueAsUint64() const OVERRIDE {
2675 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2676 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002677
Vladimir Marko372f10e2016-05-17 16:30:10 +01002678 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002679 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002680 return other->AsIntConstant()->value_ == value_;
2681 }
2682
2683 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2684
2685 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002686 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2687 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002688 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2689
Roland Levillain1a653882016-03-18 18:05:57 +00002690 // Integer constants are used to encode Boolean values as well,
2691 // where 1 means true and 0 means false.
2692 bool IsTrue() const { return GetValue() == 1; }
2693 bool IsFalse() const { return GetValue() == 0; }
2694
Roland Levillain9867bc72015-08-05 10:21:34 +01002695 DECLARE_INSTRUCTION(IntConstant);
2696
2697 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002698 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2699 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2700 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2701 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002702
2703 const int32_t value_;
2704
2705 friend class HGraph;
2706 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2707 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2708 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2709};
2710
Vladimir Markofcb503c2016-05-18 12:48:17 +01002711class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002712 public:
2713 int64_t GetValue() const { return value_; }
2714
David Brazdil77a48ae2015-09-15 12:34:04 +00002715 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2716
Vladimir Marko372f10e2016-05-17 16:30:10 +01002717 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002718 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002719 return other->AsLongConstant()->value_ == value_;
2720 }
2721
2722 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2723
2724 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002725 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2726 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002727 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2728
2729 DECLARE_INSTRUCTION(LongConstant);
2730
2731 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002732 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2733 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002734
2735 const int64_t value_;
2736
2737 friend class HGraph;
2738 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2739};
Dave Allison20dfc792014-06-16 20:44:29 -07002740
Vladimir Markofcb503c2016-05-18 12:48:17 +01002741class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002742 public:
2743 float GetValue() const { return value_; }
2744
2745 uint64_t GetValueAsUint64() const OVERRIDE {
2746 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2747 }
2748
Vladimir Marko372f10e2016-05-17 16:30:10 +01002749 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002750 DCHECK(other->IsFloatConstant()) << other->DebugName();
2751 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2752 }
2753
2754 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2755
2756 bool IsMinusOne() const OVERRIDE {
2757 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2758 }
Roland Levillain1a653882016-03-18 18:05:57 +00002759 bool IsArithmeticZero() const OVERRIDE {
2760 return std::fpclassify(value_) == FP_ZERO;
2761 }
2762 bool IsArithmeticPositiveZero() const {
2763 return IsArithmeticZero() && !std::signbit(value_);
2764 }
2765 bool IsArithmeticNegativeZero() const {
2766 return IsArithmeticZero() && std::signbit(value_);
2767 }
2768 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002769 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002770 }
2771 bool IsOne() const OVERRIDE {
2772 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2773 }
2774 bool IsNaN() const {
2775 return std::isnan(value_);
2776 }
2777
2778 DECLARE_INSTRUCTION(FloatConstant);
2779
2780 private:
2781 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2782 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2783 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2784 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2785
2786 const float value_;
2787
2788 // Only the SsaBuilder and HGraph can create floating-point constants.
2789 friend class SsaBuilder;
2790 friend class HGraph;
2791 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2792};
2793
Vladimir Markofcb503c2016-05-18 12:48:17 +01002794class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002795 public:
2796 double GetValue() const { return value_; }
2797
2798 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2799
Vladimir Marko372f10e2016-05-17 16:30:10 +01002800 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002801 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2802 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2803 }
2804
2805 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2806
2807 bool IsMinusOne() const OVERRIDE {
2808 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2809 }
Roland Levillain1a653882016-03-18 18:05:57 +00002810 bool IsArithmeticZero() const OVERRIDE {
2811 return std::fpclassify(value_) == FP_ZERO;
2812 }
2813 bool IsArithmeticPositiveZero() const {
2814 return IsArithmeticZero() && !std::signbit(value_);
2815 }
2816 bool IsArithmeticNegativeZero() const {
2817 return IsArithmeticZero() && std::signbit(value_);
2818 }
2819 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002820 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002821 }
2822 bool IsOne() const OVERRIDE {
2823 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2824 }
2825 bool IsNaN() const {
2826 return std::isnan(value_);
2827 }
2828
2829 DECLARE_INSTRUCTION(DoubleConstant);
2830
2831 private:
2832 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2833 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2834 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2835 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2836
2837 const double value_;
2838
2839 // Only the SsaBuilder and HGraph can create floating-point constants.
2840 friend class SsaBuilder;
2841 friend class HGraph;
2842 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2843};
2844
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002845// Conditional branch. A block ending with an HIf instruction must have
2846// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002847class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002848 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002849 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2850 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002851 SetRawInputAt(0, input);
2852 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002853
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002854 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002855
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002856 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002857 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002858 }
2859
2860 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002861 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002862 }
2863
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002864 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002865
2866 private:
2867 DISALLOW_COPY_AND_ASSIGN(HIf);
2868};
2869
David Brazdilfc6a86a2015-06-26 10:33:45 +00002870
2871// Abstract instruction which marks the beginning and/or end of a try block and
2872// links it to the respective exception handlers. Behaves the same as a Goto in
2873// non-exceptional control flow.
2874// Normal-flow successor is stored at index zero, exception handlers under
2875// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002876class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002877 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002878 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002879 kEntry,
2880 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002881 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002882 };
2883
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002884 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002885 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2886 SetPackedField<BoundaryKindField>(kind);
2887 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002888
2889 bool IsControlFlow() const OVERRIDE { return true; }
2890
2891 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002892 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002893
David Brazdild26a4112015-11-10 11:07:31 +00002894 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2895 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2896 }
2897
David Brazdilfc6a86a2015-06-26 10:33:45 +00002898 // Returns whether `handler` is among its exception handlers (non-zero index
2899 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002900 bool HasExceptionHandler(const HBasicBlock& handler) const {
2901 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002902 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002903 }
2904
2905 // If not present already, adds `handler` to its block's list of exception
2906 // handlers.
2907 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002908 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002909 GetBlock()->AddSuccessor(handler);
2910 }
2911 }
2912
Vladimir Markoa1de9182016-02-25 11:37:38 +00002913 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2914 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002915
David Brazdilffee3d32015-07-06 11:48:53 +01002916 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2917
David Brazdilfc6a86a2015-06-26 10:33:45 +00002918 DECLARE_INSTRUCTION(TryBoundary);
2919
2920 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002921 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2922 static constexpr size_t kFieldBoundaryKindSize =
2923 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2924 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2925 kFieldBoundaryKind + kFieldBoundaryKindSize;
2926 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2927 "Too many packed fields.");
2928 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002929
2930 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2931};
2932
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002933// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002934class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002935 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002936 // We set CanTriggerGC to prevent any intermediate address to be live
2937 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002938 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002939 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002940 SetRawInputAt(0, cond);
2941 }
2942
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002943 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002944 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002945 return true;
2946 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002947 bool NeedsEnvironment() const OVERRIDE { return true; }
2948 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002949
2950 DECLARE_INSTRUCTION(Deoptimize);
2951
2952 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002953 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2954};
2955
Mingyao Yang063fc772016-08-02 11:02:54 -07002956// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
2957// The compiled code checks this flag value in a guard before devirtualized call and
2958// if it's true, starts to do deoptimization.
2959// It has a 4-byte slot on stack.
2960// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002961class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07002962 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002963 // CHA guards are only optimized in a separate pass and it has no side effects
2964 // with regard to other passes.
2965 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
2966 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07002967 }
2968
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002969 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
2970
2971 // We do all CHA guard elimination/motion in a single pass, after which there is no
2972 // further guard elimination/motion since a guard might have been used for justification
2973 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
2974 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07002975 bool CanBeMoved() const OVERRIDE { return false; }
2976
2977 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
2978
2979 private:
2980 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
2981};
2982
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002983// Represents the ArtMethod that was passed as a first argument to
2984// the method. It is used by instructions that depend on it, like
2985// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002986class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002987 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002988 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2989 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002990
2991 DECLARE_INSTRUCTION(CurrentMethod);
2992
2993 private:
2994 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2995};
2996
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002997// Fetches an ArtMethod from the virtual table or the interface method table
2998// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002999class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003000 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003001 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003002 kVTable,
3003 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003004 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003005 };
3006 HClassTableGet(HInstruction* cls,
3007 Primitive::Type type,
3008 TableKind kind,
3009 size_t index,
3010 uint32_t dex_pc)
3011 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003012 index_(index) {
3013 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003014 SetRawInputAt(0, cls);
3015 }
3016
3017 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003018 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003019 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003020 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003021 }
3022
Vladimir Markoa1de9182016-02-25 11:37:38 +00003023 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003024 size_t GetIndex() const { return index_; }
3025
3026 DECLARE_INSTRUCTION(ClassTableGet);
3027
3028 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003029 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3030 static constexpr size_t kFieldTableKindSize =
3031 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3032 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3033 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3034 "Too many packed fields.");
3035 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3036
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003037 // The index of the ArtMethod in the table.
3038 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003039
3040 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3041};
3042
Mark Mendellfe57faa2015-09-18 09:26:15 -04003043// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3044// have one successor for each entry in the switch table, and the final successor
3045// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003046class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003047 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003048 HPackedSwitch(int32_t start_value,
3049 uint32_t num_entries,
3050 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003051 uint32_t dex_pc = kNoDexPc)
3052 : HTemplateInstruction(SideEffects::None(), dex_pc),
3053 start_value_(start_value),
3054 num_entries_(num_entries) {
3055 SetRawInputAt(0, input);
3056 }
3057
3058 bool IsControlFlow() const OVERRIDE { return true; }
3059
3060 int32_t GetStartValue() const { return start_value_; }
3061
Vladimir Marko211c2112015-09-24 16:52:33 +01003062 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003063
3064 HBasicBlock* GetDefaultBlock() const {
3065 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003066 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003067 }
3068 DECLARE_INSTRUCTION(PackedSwitch);
3069
3070 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003071 const int32_t start_value_;
3072 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003073
3074 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3075};
3076
Roland Levillain88cb1752014-10-20 16:36:47 +01003077class HUnaryOperation : public HExpression<1> {
3078 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003079 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3080 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003081 SetRawInputAt(0, input);
3082 }
3083
3084 HInstruction* GetInput() const { return InputAt(0); }
3085 Primitive::Type GetResultType() const { return GetType(); }
3086
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003087 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003088 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003089 return true;
3090 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003091
Roland Levillain31dd3d62016-02-16 12:21:02 +00003092 // Try to statically evaluate `this` and return a HConstant
3093 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003094 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003095 HConstant* TryStaticEvaluation() const;
3096
3097 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003098 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3099 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003100 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3101 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003102
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003103 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003104
3105 private:
3106 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3107};
3108
Dave Allison20dfc792014-06-16 20:44:29 -07003109class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003110 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003111 HBinaryOperation(Primitive::Type result_type,
3112 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003113 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003114 SideEffects side_effects = SideEffects::None(),
3115 uint32_t dex_pc = kNoDexPc)
3116 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003117 SetRawInputAt(0, left);
3118 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003119 }
3120
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003121 HInstruction* GetLeft() const { return InputAt(0); }
3122 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003123 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003124
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003125 virtual bool IsCommutative() const { return false; }
3126
3127 // Put constant on the right.
3128 // Returns whether order is changed.
3129 bool OrderInputsWithConstantOnTheRight() {
3130 HInstruction* left = InputAt(0);
3131 HInstruction* right = InputAt(1);
3132 if (left->IsConstant() && !right->IsConstant()) {
3133 ReplaceInput(right, 0);
3134 ReplaceInput(left, 1);
3135 return true;
3136 }
3137 return false;
3138 }
3139
3140 // Order inputs by instruction id, but favor constant on the right side.
3141 // This helps GVN for commutative ops.
3142 void OrderInputs() {
3143 DCHECK(IsCommutative());
3144 HInstruction* left = InputAt(0);
3145 HInstruction* right = InputAt(1);
3146 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3147 return;
3148 }
3149 if (OrderInputsWithConstantOnTheRight()) {
3150 return;
3151 }
3152 // Order according to instruction id.
3153 if (left->GetId() > right->GetId()) {
3154 ReplaceInput(right, 0);
3155 ReplaceInput(left, 1);
3156 }
3157 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003158
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003159 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003160 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003161 return true;
3162 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003163
Roland Levillain31dd3d62016-02-16 12:21:02 +00003164 // Try to statically evaluate `this` and return a HConstant
3165 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003166 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003167 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003168
3169 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003170 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3171 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003172 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3173 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003174 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003175 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3176 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003177 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3178 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003179 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3180 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003181 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003182 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3183 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003184
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003185 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003186 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003187 HConstant* GetConstantRight() const;
3188
3189 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003190 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003191 HInstruction* GetLeastConstantLeft() const;
3192
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003193 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003194
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003195 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003196 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3197};
3198
Mark Mendellc4701932015-04-10 13:18:51 -04003199// The comparison bias applies for floating point operations and indicates how NaN
3200// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003201enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003202 kNoBias, // bias is not applicable (i.e. for long operation)
3203 kGtBias, // return 1 for NaN comparisons
3204 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003205 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003206};
3207
Roland Levillain31dd3d62016-02-16 12:21:02 +00003208std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3209
Dave Allison20dfc792014-06-16 20:44:29 -07003210class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003211 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003212 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003213 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3214 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3215 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003216
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003217 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003218 // `instruction`, and disregard moves in between.
3219 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003220
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003221 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003222
3223 virtual IfCondition GetCondition() const = 0;
3224
Mark Mendellc4701932015-04-10 13:18:51 -04003225 virtual IfCondition GetOppositeCondition() const = 0;
3226
Vladimir Markoa1de9182016-02-25 11:37:38 +00003227 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003228 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3229
Vladimir Markoa1de9182016-02-25 11:37:38 +00003230 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3231 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003232
Vladimir Marko372f10e2016-05-17 16:30:10 +01003233 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003234 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003235 }
3236
Roland Levillain4fa13f62015-07-06 18:11:54 +01003237 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003238 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003239 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003240 if (if_cond == kCondNE) {
3241 return true;
3242 } else if (if_cond == kCondEQ) {
3243 return false;
3244 }
3245 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003246 }
3247
3248 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003249 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003250 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003251 if (if_cond == kCondEQ) {
3252 return true;
3253 } else if (if_cond == kCondNE) {
3254 return false;
3255 }
3256 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003257 }
3258
Roland Levillain31dd3d62016-02-16 12:21:02 +00003259 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003260 // Needed if we merge a HCompare into a HCondition.
3261 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3262 static constexpr size_t kFieldComparisonBiasSize =
3263 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3264 static constexpr size_t kNumberOfConditionPackedBits =
3265 kFieldComparisonBias + kFieldComparisonBiasSize;
3266 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3267 using ComparisonBiasField =
3268 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3269
Roland Levillain31dd3d62016-02-16 12:21:02 +00003270 template <typename T>
3271 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3272
3273 template <typename T>
3274 int32_t CompareFP(T x, T y) const {
3275 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3276 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3277 // Handle the bias.
3278 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3279 }
3280
3281 // Return an integer constant containing the result of a condition evaluated at compile time.
3282 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3283 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3284 }
3285
Dave Allison20dfc792014-06-16 20:44:29 -07003286 private:
3287 DISALLOW_COPY_AND_ASSIGN(HCondition);
3288};
3289
3290// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003291class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003292 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003293 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3294 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003295
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003296 bool IsCommutative() const OVERRIDE { return true; }
3297
Vladimir Marko9e23df52015-11-10 17:14:35 +00003298 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3299 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003300 return MakeConstantCondition(true, GetDexPc());
3301 }
3302 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3303 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3304 }
3305 // In the following Evaluate methods, a HCompare instruction has
3306 // been merged into this HEqual instruction; evaluate it as
3307 // `Compare(x, y) == 0`.
3308 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3309 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3310 GetDexPc());
3311 }
3312 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3313 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3314 }
3315 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3316 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003317 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003318
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003319 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003320
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003321 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003322 return kCondEQ;
3323 }
3324
Mark Mendellc4701932015-04-10 13:18:51 -04003325 IfCondition GetOppositeCondition() const OVERRIDE {
3326 return kCondNE;
3327 }
3328
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003329 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003330 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003331
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003332 DISALLOW_COPY_AND_ASSIGN(HEqual);
3333};
3334
Vladimir Markofcb503c2016-05-18 12:48:17 +01003335class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003336 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003337 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3338 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003339
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003340 bool IsCommutative() const OVERRIDE { return true; }
3341
Vladimir Marko9e23df52015-11-10 17:14:35 +00003342 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3343 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003344 return MakeConstantCondition(false, GetDexPc());
3345 }
3346 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3347 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3348 }
3349 // In the following Evaluate methods, a HCompare instruction has
3350 // been merged into this HNotEqual instruction; evaluate it as
3351 // `Compare(x, y) != 0`.
3352 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3353 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3354 }
3355 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3356 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3357 }
3358 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3359 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003360 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003361
Dave Allison20dfc792014-06-16 20:44:29 -07003362 DECLARE_INSTRUCTION(NotEqual);
3363
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003364 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003365 return kCondNE;
3366 }
3367
Mark Mendellc4701932015-04-10 13:18:51 -04003368 IfCondition GetOppositeCondition() const OVERRIDE {
3369 return kCondEQ;
3370 }
3371
Dave Allison20dfc792014-06-16 20:44:29 -07003372 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003373 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003374
Dave Allison20dfc792014-06-16 20:44:29 -07003375 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3376};
3377
Vladimir Markofcb503c2016-05-18 12:48:17 +01003378class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003379 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003380 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3381 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003382
Roland Levillain9867bc72015-08-05 10:21:34 +01003383 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003384 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003385 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003386 // In the following Evaluate methods, a HCompare instruction has
3387 // been merged into this HLessThan instruction; evaluate it as
3388 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003389 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003390 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3391 }
3392 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3393 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3394 }
3395 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3396 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003397 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003398
Dave Allison20dfc792014-06-16 20:44:29 -07003399 DECLARE_INSTRUCTION(LessThan);
3400
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003401 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003402 return kCondLT;
3403 }
3404
Mark Mendellc4701932015-04-10 13:18:51 -04003405 IfCondition GetOppositeCondition() const OVERRIDE {
3406 return kCondGE;
3407 }
3408
Dave Allison20dfc792014-06-16 20:44:29 -07003409 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003410 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003411
Dave Allison20dfc792014-06-16 20:44:29 -07003412 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3413};
3414
Vladimir Markofcb503c2016-05-18 12:48:17 +01003415class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003416 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003417 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3418 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003419
Roland Levillain9867bc72015-08-05 10:21:34 +01003420 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003421 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003422 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003423 // In the following Evaluate methods, a HCompare instruction has
3424 // been merged into this HLessThanOrEqual instruction; evaluate it as
3425 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003426 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003427 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3428 }
3429 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3430 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3431 }
3432 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3433 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003434 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003435
Dave Allison20dfc792014-06-16 20:44:29 -07003436 DECLARE_INSTRUCTION(LessThanOrEqual);
3437
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003438 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003439 return kCondLE;
3440 }
3441
Mark Mendellc4701932015-04-10 13:18:51 -04003442 IfCondition GetOppositeCondition() const OVERRIDE {
3443 return kCondGT;
3444 }
3445
Dave Allison20dfc792014-06-16 20:44:29 -07003446 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003447 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003448
Dave Allison20dfc792014-06-16 20:44:29 -07003449 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3450};
3451
Vladimir Markofcb503c2016-05-18 12:48:17 +01003452class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003453 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003454 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3455 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003456
Roland Levillain9867bc72015-08-05 10:21:34 +01003457 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003458 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003459 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003460 // In the following Evaluate methods, a HCompare instruction has
3461 // been merged into this HGreaterThan instruction; evaluate it as
3462 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003463 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003464 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3465 }
3466 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3467 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3468 }
3469 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3470 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003471 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003472
Dave Allison20dfc792014-06-16 20:44:29 -07003473 DECLARE_INSTRUCTION(GreaterThan);
3474
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003475 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003476 return kCondGT;
3477 }
3478
Mark Mendellc4701932015-04-10 13:18:51 -04003479 IfCondition GetOppositeCondition() const OVERRIDE {
3480 return kCondLE;
3481 }
3482
Dave Allison20dfc792014-06-16 20:44:29 -07003483 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003484 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003485
Dave Allison20dfc792014-06-16 20:44:29 -07003486 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3487};
3488
Vladimir Markofcb503c2016-05-18 12:48:17 +01003489class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003490 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003491 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3492 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003493
Roland Levillain9867bc72015-08-05 10:21:34 +01003494 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003495 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003496 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003497 // In the following Evaluate methods, a HCompare instruction has
3498 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3499 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003500 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003501 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3502 }
3503 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3504 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3505 }
3506 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3507 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003508 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003509
Dave Allison20dfc792014-06-16 20:44:29 -07003510 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3511
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003512 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003513 return kCondGE;
3514 }
3515
Mark Mendellc4701932015-04-10 13:18:51 -04003516 IfCondition GetOppositeCondition() const OVERRIDE {
3517 return kCondLT;
3518 }
3519
Dave Allison20dfc792014-06-16 20:44:29 -07003520 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003521 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003522
Dave Allison20dfc792014-06-16 20:44:29 -07003523 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3524};
3525
Vladimir Markofcb503c2016-05-18 12:48:17 +01003526class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003527 public:
3528 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3529 : HCondition(first, second, dex_pc) {}
3530
3531 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003532 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003533 }
3534 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003535 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3536 }
3537 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3538 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3539 LOG(FATAL) << DebugName() << " is not defined for float values";
3540 UNREACHABLE();
3541 }
3542 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3543 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3544 LOG(FATAL) << DebugName() << " is not defined for double values";
3545 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003546 }
3547
3548 DECLARE_INSTRUCTION(Below);
3549
3550 IfCondition GetCondition() const OVERRIDE {
3551 return kCondB;
3552 }
3553
3554 IfCondition GetOppositeCondition() const OVERRIDE {
3555 return kCondAE;
3556 }
3557
3558 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003559 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003560 return MakeUnsigned(x) < MakeUnsigned(y);
3561 }
Aart Bike9f37602015-10-09 11:15:55 -07003562
3563 DISALLOW_COPY_AND_ASSIGN(HBelow);
3564};
3565
Vladimir Markofcb503c2016-05-18 12:48:17 +01003566class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003567 public:
3568 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3569 : HCondition(first, second, dex_pc) {}
3570
3571 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003572 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003573 }
3574 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003575 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3576 }
3577 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3578 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3579 LOG(FATAL) << DebugName() << " is not defined for float values";
3580 UNREACHABLE();
3581 }
3582 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3583 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3584 LOG(FATAL) << DebugName() << " is not defined for double values";
3585 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003586 }
3587
3588 DECLARE_INSTRUCTION(BelowOrEqual);
3589
3590 IfCondition GetCondition() const OVERRIDE {
3591 return kCondBE;
3592 }
3593
3594 IfCondition GetOppositeCondition() const OVERRIDE {
3595 return kCondA;
3596 }
3597
3598 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003599 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003600 return MakeUnsigned(x) <= MakeUnsigned(y);
3601 }
Aart Bike9f37602015-10-09 11:15:55 -07003602
3603 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3604};
3605
Vladimir Markofcb503c2016-05-18 12:48:17 +01003606class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003607 public:
3608 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3609 : HCondition(first, second, dex_pc) {}
3610
3611 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003612 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003613 }
3614 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003615 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3616 }
3617 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3618 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3619 LOG(FATAL) << DebugName() << " is not defined for float values";
3620 UNREACHABLE();
3621 }
3622 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3623 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3624 LOG(FATAL) << DebugName() << " is not defined for double values";
3625 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003626 }
3627
3628 DECLARE_INSTRUCTION(Above);
3629
3630 IfCondition GetCondition() const OVERRIDE {
3631 return kCondA;
3632 }
3633
3634 IfCondition GetOppositeCondition() const OVERRIDE {
3635 return kCondBE;
3636 }
3637
3638 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003639 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003640 return MakeUnsigned(x) > MakeUnsigned(y);
3641 }
Aart Bike9f37602015-10-09 11:15:55 -07003642
3643 DISALLOW_COPY_AND_ASSIGN(HAbove);
3644};
3645
Vladimir Markofcb503c2016-05-18 12:48:17 +01003646class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003647 public:
3648 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3649 : HCondition(first, second, dex_pc) {}
3650
3651 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003652 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003653 }
3654 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003655 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3656 }
3657 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3658 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3659 LOG(FATAL) << DebugName() << " is not defined for float values";
3660 UNREACHABLE();
3661 }
3662 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3663 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3664 LOG(FATAL) << DebugName() << " is not defined for double values";
3665 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003666 }
3667
3668 DECLARE_INSTRUCTION(AboveOrEqual);
3669
3670 IfCondition GetCondition() const OVERRIDE {
3671 return kCondAE;
3672 }
3673
3674 IfCondition GetOppositeCondition() const OVERRIDE {
3675 return kCondB;
3676 }
3677
3678 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003679 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003680 return MakeUnsigned(x) >= MakeUnsigned(y);
3681 }
Aart Bike9f37602015-10-09 11:15:55 -07003682
3683 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3684};
Dave Allison20dfc792014-06-16 20:44:29 -07003685
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003686// Instruction to check how two inputs compare to each other.
3687// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003688class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003689 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003690 // Note that `comparison_type` is the type of comparison performed
3691 // between the comparison's inputs, not the type of the instantiated
3692 // HCompare instruction (which is always Primitive::kPrimInt).
3693 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003694 HInstruction* first,
3695 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003696 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003697 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003698 : HBinaryOperation(Primitive::kPrimInt,
3699 first,
3700 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003701 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003702 dex_pc) {
3703 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003704 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3705 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003706 }
3707
Roland Levillain9867bc72015-08-05 10:21:34 +01003708 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003709 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3710
3711 template <typename T>
3712 int32_t ComputeFP(T x, T y) const {
3713 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3714 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3715 // Handle the bias.
3716 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3717 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003718
Roland Levillain9867bc72015-08-05 10:21:34 +01003719 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003720 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3721 // reach this code path when processing a freshly built HIR
3722 // graph. However HCompare integer instructions can be synthesized
3723 // by the instruction simplifier to implement IntegerCompare and
3724 // IntegerSignum intrinsics, so we have to handle this case.
3725 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003726 }
3727 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003728 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3729 }
3730 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3731 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3732 }
3733 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3734 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003735 }
3736
Vladimir Marko372f10e2016-05-17 16:30:10 +01003737 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003738 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003739 }
3740
Vladimir Markoa1de9182016-02-25 11:37:38 +00003741 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003742
Roland Levillain31dd3d62016-02-16 12:21:02 +00003743 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003744 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003745 bool IsGtBias() const {
3746 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003747 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003748 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003749
Roland Levillain1693a1f2016-03-15 14:57:31 +00003750 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3751 // Comparisons do not require a runtime call in any back end.
3752 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003753 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003754
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003755 DECLARE_INSTRUCTION(Compare);
3756
Roland Levillain31dd3d62016-02-16 12:21:02 +00003757 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003758 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3759 static constexpr size_t kFieldComparisonBiasSize =
3760 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3761 static constexpr size_t kNumberOfComparePackedBits =
3762 kFieldComparisonBias + kFieldComparisonBiasSize;
3763 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3764 using ComparisonBiasField =
3765 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3766
Roland Levillain31dd3d62016-02-16 12:21:02 +00003767 // Return an integer constant containing the result of a comparison evaluated at compile time.
3768 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3769 DCHECK(value == -1 || value == 0 || value == 1) << value;
3770 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3771 }
3772
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003773 private:
3774 DISALLOW_COPY_AND_ASSIGN(HCompare);
3775};
3776
Vladimir Markofcb503c2016-05-18 12:48:17 +01003777class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003778 public:
3779 HNewInstance(HInstruction* cls,
3780 HCurrentMethod* current_method,
3781 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003782 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003783 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003784 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003785 bool finalizable,
3786 QuickEntrypointEnum entrypoint)
3787 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3788 type_index_(type_index),
3789 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003790 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003791 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003792 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003793 SetRawInputAt(0, cls);
3794 SetRawInputAt(1, current_method);
3795 }
3796
Andreas Gampea5b09a62016-11-17 15:21:22 -08003797 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003798 const DexFile& GetDexFile() const { return dex_file_; }
3799
3800 // Calls runtime so needs an environment.
3801 bool NeedsEnvironment() const OVERRIDE { return true; }
3802
Mingyao Yang062157f2016-03-02 10:15:36 -08003803 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3804 bool CanThrow() const OVERRIDE { return true; }
3805
3806 // Needs to call into runtime to make sure it's instantiable/accessible.
3807 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003808
Vladimir Markoa1de9182016-02-25 11:37:38 +00003809 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003810
3811 bool CanBeNull() const OVERRIDE { return false; }
3812
3813 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3814
3815 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3816 entrypoint_ = entrypoint;
3817 }
3818
3819 bool IsStringAlloc() const;
3820
3821 DECLARE_INSTRUCTION(NewInstance);
3822
3823 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003824 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3825 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003826 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3827 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3828 "Too many packed fields.");
3829
Andreas Gampea5b09a62016-11-17 15:21:22 -08003830 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003831 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003832 QuickEntrypointEnum entrypoint_;
3833
3834 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3835};
3836
Agi Csaki05f20562015-08-19 14:58:14 -07003837enum IntrinsicNeedsEnvironmentOrCache {
3838 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3839 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003840};
3841
Aart Bik5d75afe2015-12-14 11:57:01 -08003842enum IntrinsicSideEffects {
3843 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3844 kReadSideEffects, // Intrinsic may read heap memory.
3845 kWriteSideEffects, // Intrinsic may write heap memory.
3846 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3847};
3848
3849enum IntrinsicExceptions {
3850 kNoThrow, // Intrinsic does not throw any exceptions.
3851 kCanThrow // Intrinsic may throw exceptions.
3852};
3853
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003854class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003855 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003856 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003857
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003858 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003859 SetRawInputAt(index, argument);
3860 }
3861
Roland Levillain3e3d7332015-04-28 11:00:54 +01003862 // Return the number of arguments. This number can be lower than
3863 // the number of inputs returned by InputCount(), as some invoke
3864 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3865 // inputs at the end of their list of inputs.
3866 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3867
Vladimir Markoa1de9182016-02-25 11:37:38 +00003868 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003869
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003870 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003871 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003872
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003873 InvokeType GetInvokeType() const {
3874 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003875 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003876
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003877 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003878 return intrinsic_;
3879 }
3880
Aart Bik5d75afe2015-12-14 11:57:01 -08003881 void SetIntrinsic(Intrinsics intrinsic,
3882 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3883 IntrinsicSideEffects side_effects,
3884 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003885
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003886 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003887 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003888 }
3889
Aart Bikff7d89c2016-11-07 08:49:28 -08003890 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
3891
Vladimir Markoa1de9182016-02-25 11:37:38 +00003892 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003893
Aart Bik71bf7b42016-11-16 10:17:46 -08003894 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003895
Vladimir Marko372f10e2016-05-17 16:30:10 +01003896 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003897 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3898 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003899
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003900 uint32_t* GetIntrinsicOptimizations() {
3901 return &intrinsic_optimizations_;
3902 }
3903
3904 const uint32_t* GetIntrinsicOptimizations() const {
3905 return &intrinsic_optimizations_;
3906 }
3907
3908 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3909
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003910 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3911
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003912 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003913
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003914 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003915 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3916 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003917 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3918 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003919 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003920 static constexpr size_t kFieldReturnTypeSize =
3921 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3922 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3923 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3924 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003925 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003926 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3927
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003928 HInvoke(ArenaAllocator* arena,
3929 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003930 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003931 Primitive::Type return_type,
3932 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003933 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003934 ArtMethod* resolved_method,
3935 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003936 : HVariableInputSizeInstruction(
3937 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
3938 dex_pc,
3939 arena,
3940 number_of_arguments + number_of_other_inputs,
3941 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003942 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003943 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003944 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003945 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003946 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003947 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003948 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003949 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003950 }
3951
Roland Levillain3e3d7332015-04-28 11:00:54 +01003952 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003953 ArtMethod* const resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003954 const uint32_t dex_method_index_;
3955 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003956
3957 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3958 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003959
3960 private:
3961 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3962};
3963
Vladimir Markofcb503c2016-05-18 12:48:17 +01003964class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003965 public:
3966 HInvokeUnresolved(ArenaAllocator* arena,
3967 uint32_t number_of_arguments,
3968 Primitive::Type return_type,
3969 uint32_t dex_pc,
3970 uint32_t dex_method_index,
3971 InvokeType invoke_type)
3972 : HInvoke(arena,
3973 number_of_arguments,
3974 0u /* number_of_other_inputs */,
3975 return_type,
3976 dex_pc,
3977 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003978 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003979 invoke_type) {
3980 }
3981
3982 DECLARE_INSTRUCTION(InvokeUnresolved);
3983
3984 private:
3985 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3986};
3987
Vladimir Markofcb503c2016-05-18 12:48:17 +01003988class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003989 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003990 // Requirements of this method call regarding the class
3991 // initialization (clinit) check of its declaring class.
3992 enum class ClinitCheckRequirement {
3993 kNone, // Class already initialized.
3994 kExplicit, // Static call having explicit clinit check as last input.
3995 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003996 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003997 };
3998
Vladimir Marko58155012015-08-19 12:49:41 +00003999 // Determines how to load the target ArtMethod*.
4000 enum class MethodLoadKind {
4001 // Use a String init ArtMethod* loaded from Thread entrypoints.
4002 kStringInit,
4003
4004 // Use the method's own ArtMethod* loaded by the register allocator.
4005 kRecursive,
4006
4007 // Use ArtMethod* at a known address, embed the direct address in the code.
4008 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4009 kDirectAddress,
4010
Vladimir Markoa1de9182016-02-25 11:37:38 +00004011 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004012 // Used when we need to use the dex cache, for example for invoke-static that
4013 // may cause class initialization (the entry may point to a resolution method),
4014 // and we know that we can access the dex cache arrays using a PC-relative load.
4015 kDexCachePcRelative,
4016
4017 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4018 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4019 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4020 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4021 kDexCacheViaMethod,
4022 };
4023
4024 // Determines the location of the code pointer.
4025 enum class CodePtrLocation {
4026 // Recursive call, use local PC-relative call instruction.
4027 kCallSelf,
4028
Vladimir Marko58155012015-08-19 12:49:41 +00004029 // Use code pointer from the ArtMethod*.
4030 // Used when we don't know the target code. This is also the last-resort-kind used when
4031 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4032 kCallArtMethod,
4033 };
4034
4035 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004036 MethodLoadKind method_load_kind;
4037 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004038 // The method load data holds
4039 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4040 // Note that there are multiple string init methods, each having its own offset.
4041 // - the method address for kDirectAddress
4042 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004043 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004044 };
4045
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004046 HInvokeStaticOrDirect(ArenaAllocator* arena,
4047 uint32_t number_of_arguments,
4048 Primitive::Type return_type,
4049 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004050 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004051 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004052 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004053 InvokeType invoke_type,
4054 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004055 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004056 : HInvoke(arena,
4057 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004058 // There is potentially one extra argument for the HCurrentMethod node, and
4059 // potentially one other if the clinit check is explicit, and potentially
4060 // one other if the method is a string factory.
4061 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004062 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004063 return_type,
4064 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004065 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004066 resolved_method,
4067 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004068 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004069 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004070 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4071 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004072
Vladimir Markodc151b22015-10-15 18:02:30 +01004073 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004074 bool had_current_method_input = HasCurrentMethodInput();
4075 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4076
4077 // Using the current method is the default and once we find a better
4078 // method load kind, we should not go back to using the current method.
4079 DCHECK(had_current_method_input || !needs_current_method_input);
4080
4081 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004082 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4083 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004084 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004085 dispatch_info_ = dispatch_info;
4086 }
4087
Vladimir Markoc53c0792015-11-19 15:48:33 +00004088 void AddSpecialInput(HInstruction* input) {
4089 // We allow only one special input.
4090 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4091 DCHECK(InputCount() == GetSpecialInputIndex() ||
4092 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4093 InsertInputAt(GetSpecialInputIndex(), input);
4094 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004095
Vladimir Marko372f10e2016-05-17 16:30:10 +01004096 using HInstruction::GetInputRecords; // Keep the const version visible.
4097 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4098 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4099 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4100 DCHECK(!input_records.empty());
4101 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4102 HInstruction* last_input = input_records.back().GetInstruction();
4103 // Note: `last_input` may be null during arguments setup.
4104 if (last_input != nullptr) {
4105 // `last_input` is the last input of a static invoke marked as having
4106 // an explicit clinit check. It must either be:
4107 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4108 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4109 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4110 }
4111 }
4112 return input_records;
4113 }
4114
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004115 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004116 // We access the method via the dex cache so we can't do an implicit null check.
4117 // TODO: for intrinsics we can generate implicit null checks.
4118 return false;
4119 }
4120
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004121 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004122 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004123 }
4124
Vladimir Markoc53c0792015-11-19 15:48:33 +00004125 // Get the index of the special input, if any.
4126 //
David Brazdil6de19382016-01-08 17:37:10 +00004127 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4128 // method pointer; otherwise there may be one platform-specific special input,
4129 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004130 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004131 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004132
Vladimir Marko58155012015-08-19 12:49:41 +00004133 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4134 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4135 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004136 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004137 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004138 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004139 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004140 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4141 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004142 bool HasCurrentMethodInput() const {
4143 // This function can be called only after the invoke has been fully initialized by the builder.
4144 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004145 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004146 return true;
4147 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004148 DCHECK(InputCount() == GetSpecialInputIndex() ||
4149 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004150 return false;
4151 }
4152 }
Vladimir Marko58155012015-08-19 12:49:41 +00004153
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004154 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004155 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004156 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004157 }
4158
4159 uint64_t GetMethodAddress() const {
4160 DCHECK(HasMethodAddress());
4161 return dispatch_info_.method_load_data;
4162 }
4163
4164 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004165 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004166 return dispatch_info_.method_load_data;
4167 }
4168
Vladimir Markoa1de9182016-02-25 11:37:38 +00004169 ClinitCheckRequirement GetClinitCheckRequirement() const {
4170 return GetPackedField<ClinitCheckRequirementField>();
4171 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004172
Roland Levillain4c0eb422015-04-24 16:43:49 +01004173 // Is this instruction a call to a static method?
4174 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004175 return GetInvokeType() == kStatic;
4176 }
4177
4178 MethodReference GetTargetMethod() const {
4179 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004180 }
4181
Vladimir Markofbb184a2015-11-13 14:47:00 +00004182 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4183 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4184 // instruction; only relevant for static calls with explicit clinit check.
4185 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004186 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004187 size_t last_input_index = inputs_.size() - 1u;
4188 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004189 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004190 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004191 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004192 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004193 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004194 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004195 }
4196
4197 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004198 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004199 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004200 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004201 }
4202
4203 // Is this a call to a static method whose declaring class has an
4204 // implicit intialization check requirement?
4205 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004206 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004207 }
4208
Vladimir Markob554b5a2015-11-06 12:57:55 +00004209 // Does this method load kind need the current method as an input?
4210 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4211 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4212 }
4213
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004214 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004215
4216 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004217 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004218 static constexpr size_t kFieldClinitCheckRequirementSize =
4219 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4220 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4221 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4222 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4223 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004224 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4225 kFieldClinitCheckRequirement,
4226 kFieldClinitCheckRequirementSize>;
4227
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004228 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004229 MethodReference target_method_;
4230 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004231
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004232 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004233};
Vladimir Markof64242a2015-12-01 14:58:23 +00004234std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004235std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004236
Vladimir Markofcb503c2016-05-18 12:48:17 +01004237class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004238 public:
4239 HInvokeVirtual(ArenaAllocator* arena,
4240 uint32_t number_of_arguments,
4241 Primitive::Type return_type,
4242 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004243 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004244 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004245 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004246 : HInvoke(arena,
4247 number_of_arguments,
4248 0u,
4249 return_type,
4250 dex_pc,
4251 dex_method_index,
4252 resolved_method,
4253 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004254 vtable_index_(vtable_index) {}
4255
Aart Bik71bf7b42016-11-16 10:17:46 -08004256 bool CanBeNull() const OVERRIDE {
4257 switch (GetIntrinsic()) {
4258 case Intrinsics::kThreadCurrentThread:
4259 case Intrinsics::kStringBufferAppend:
4260 case Intrinsics::kStringBufferToString:
4261 case Intrinsics::kStringBuilderAppend:
4262 case Intrinsics::kStringBuilderToString:
4263 return false;
4264 default:
4265 return HInvoke::CanBeNull();
4266 }
4267 }
4268
Calin Juravle641547a2015-04-21 22:08:51 +01004269 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004270 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004271 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004272 }
4273
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004274 uint32_t GetVTableIndex() const { return vtable_index_; }
4275
4276 DECLARE_INSTRUCTION(InvokeVirtual);
4277
4278 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004279 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004280 const uint32_t vtable_index_;
4281
4282 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4283};
4284
Vladimir Markofcb503c2016-05-18 12:48:17 +01004285class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004286 public:
4287 HInvokeInterface(ArenaAllocator* arena,
4288 uint32_t number_of_arguments,
4289 Primitive::Type return_type,
4290 uint32_t dex_pc,
4291 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004292 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004293 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004294 : HInvoke(arena,
4295 number_of_arguments,
4296 0u,
4297 return_type,
4298 dex_pc,
4299 dex_method_index,
4300 resolved_method,
4301 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004302 imt_index_(imt_index) {}
4303
Calin Juravle641547a2015-04-21 22:08:51 +01004304 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004305 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004306 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004307 }
4308
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004309 uint32_t GetImtIndex() const { return imt_index_; }
4310 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4311
4312 DECLARE_INSTRUCTION(InvokeInterface);
4313
4314 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004315 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004316 const uint32_t imt_index_;
4317
4318 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4319};
4320
Vladimir Markofcb503c2016-05-18 12:48:17 +01004321class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004322 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004323 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004324 : HUnaryOperation(result_type, input, dex_pc) {
4325 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4326 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004327
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004328 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004329
4330 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004331 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004332 }
4333 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004334 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004335 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004336 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4337 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4338 }
4339 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4340 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4341 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004342
Roland Levillain88cb1752014-10-20 16:36:47 +01004343 DECLARE_INSTRUCTION(Neg);
4344
4345 private:
4346 DISALLOW_COPY_AND_ASSIGN(HNeg);
4347};
4348
Vladimir Markofcb503c2016-05-18 12:48:17 +01004349class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004350 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004351 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004352 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004353 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004354 dex::TypeIndex type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004355 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004356 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004357 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004358 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004359 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004360 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004361 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004362 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004363 }
4364
Andreas Gampea5b09a62016-11-17 15:21:22 -08004365 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004366 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004367
4368 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004369 bool NeedsEnvironment() const OVERRIDE { return true; }
4370
Mingyao Yang0c365e62015-03-31 15:09:29 -07004371 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4372 bool CanThrow() const OVERRIDE { return true; }
4373
Calin Juravle10e244f2015-01-26 18:54:32 +00004374 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004375
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004376 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4377
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004378 DECLARE_INSTRUCTION(NewArray);
4379
4380 private:
Andreas Gampea5b09a62016-11-17 15:21:22 -08004381 const dex::TypeIndex type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004382 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004383 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004384
4385 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4386};
4387
Vladimir Markofcb503c2016-05-18 12:48:17 +01004388class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004389 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004390 HAdd(Primitive::Type result_type,
4391 HInstruction* left,
4392 HInstruction* right,
4393 uint32_t dex_pc = kNoDexPc)
4394 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004395
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004396 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004397
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004398 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004399
4400 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004401 return GetBlock()->GetGraph()->GetIntConstant(
4402 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004403 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004404 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004405 return GetBlock()->GetGraph()->GetLongConstant(
4406 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004407 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004408 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4409 return GetBlock()->GetGraph()->GetFloatConstant(
4410 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4411 }
4412 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4413 return GetBlock()->GetGraph()->GetDoubleConstant(
4414 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4415 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004416
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004417 DECLARE_INSTRUCTION(Add);
4418
4419 private:
4420 DISALLOW_COPY_AND_ASSIGN(HAdd);
4421};
4422
Vladimir Markofcb503c2016-05-18 12:48:17 +01004423class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004424 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004425 HSub(Primitive::Type result_type,
4426 HInstruction* left,
4427 HInstruction* right,
4428 uint32_t dex_pc = kNoDexPc)
4429 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004430
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004431 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004432
4433 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004434 return GetBlock()->GetGraph()->GetIntConstant(
4435 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004436 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004437 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004438 return GetBlock()->GetGraph()->GetLongConstant(
4439 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004440 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004441 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4442 return GetBlock()->GetGraph()->GetFloatConstant(
4443 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4444 }
4445 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4446 return GetBlock()->GetGraph()->GetDoubleConstant(
4447 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4448 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004449
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004450 DECLARE_INSTRUCTION(Sub);
4451
4452 private:
4453 DISALLOW_COPY_AND_ASSIGN(HSub);
4454};
4455
Vladimir Markofcb503c2016-05-18 12:48:17 +01004456class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004457 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004458 HMul(Primitive::Type result_type,
4459 HInstruction* left,
4460 HInstruction* right,
4461 uint32_t dex_pc = kNoDexPc)
4462 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004463
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004464 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004465
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004466 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004467
4468 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004469 return GetBlock()->GetGraph()->GetIntConstant(
4470 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004471 }
4472 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004473 return GetBlock()->GetGraph()->GetLongConstant(
4474 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004475 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004476 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4477 return GetBlock()->GetGraph()->GetFloatConstant(
4478 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4479 }
4480 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4481 return GetBlock()->GetGraph()->GetDoubleConstant(
4482 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4483 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004484
4485 DECLARE_INSTRUCTION(Mul);
4486
4487 private:
4488 DISALLOW_COPY_AND_ASSIGN(HMul);
4489};
4490
Vladimir Markofcb503c2016-05-18 12:48:17 +01004491class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004492 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004493 HDiv(Primitive::Type result_type,
4494 HInstruction* left,
4495 HInstruction* right,
4496 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004497 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004498
Roland Levillain9867bc72015-08-05 10:21:34 +01004499 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004500 T ComputeIntegral(T x, T y) const {
4501 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004502 // Our graph structure ensures we never have 0 for `y` during
4503 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004504 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004505 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004506 return (y == -1) ? -x : x / y;
4507 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004508
Roland Levillain31dd3d62016-02-16 12:21:02 +00004509 template <typename T>
4510 T ComputeFP(T x, T y) const {
4511 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4512 return x / y;
4513 }
4514
Roland Levillain9867bc72015-08-05 10:21:34 +01004515 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004516 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004517 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004518 }
4519 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004520 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004521 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4522 }
4523 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4524 return GetBlock()->GetGraph()->GetFloatConstant(
4525 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4526 }
4527 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4528 return GetBlock()->GetGraph()->GetDoubleConstant(
4529 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004530 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004531
4532 DECLARE_INSTRUCTION(Div);
4533
4534 private:
4535 DISALLOW_COPY_AND_ASSIGN(HDiv);
4536};
4537
Vladimir Markofcb503c2016-05-18 12:48:17 +01004538class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004539 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004540 HRem(Primitive::Type result_type,
4541 HInstruction* left,
4542 HInstruction* right,
4543 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004544 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004545
Roland Levillain9867bc72015-08-05 10:21:34 +01004546 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004547 T ComputeIntegral(T x, T y) const {
4548 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004549 // Our graph structure ensures we never have 0 for `y` during
4550 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004551 DCHECK_NE(y, 0);
4552 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4553 return (y == -1) ? 0 : x % y;
4554 }
4555
Roland Levillain31dd3d62016-02-16 12:21:02 +00004556 template <typename T>
4557 T ComputeFP(T x, T y) const {
4558 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4559 return std::fmod(x, y);
4560 }
4561
Roland Levillain9867bc72015-08-05 10:21:34 +01004562 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004563 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004564 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004565 }
4566 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004567 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004568 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004569 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004570 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4571 return GetBlock()->GetGraph()->GetFloatConstant(
4572 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4573 }
4574 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4575 return GetBlock()->GetGraph()->GetDoubleConstant(
4576 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4577 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004578
4579 DECLARE_INSTRUCTION(Rem);
4580
4581 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004582 DISALLOW_COPY_AND_ASSIGN(HRem);
4583};
4584
Vladimir Markofcb503c2016-05-18 12:48:17 +01004585class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004586 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004587 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4588 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004589 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004590 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004591 SetRawInputAt(0, value);
4592 }
4593
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004594 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4595
Calin Juravled0d48522014-11-04 16:40:20 +00004596 bool CanBeMoved() const OVERRIDE { return true; }
4597
Vladimir Marko372f10e2016-05-17 16:30:10 +01004598 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004599 return true;
4600 }
4601
4602 bool NeedsEnvironment() const OVERRIDE { return true; }
4603 bool CanThrow() const OVERRIDE { return true; }
4604
Calin Juravled0d48522014-11-04 16:40:20 +00004605 DECLARE_INSTRUCTION(DivZeroCheck);
4606
4607 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004608 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4609};
4610
Vladimir Markofcb503c2016-05-18 12:48:17 +01004611class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004612 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004613 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004614 HInstruction* value,
4615 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004616 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004617 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4618 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4619 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004620 }
4621
Roland Levillain5b5b9312016-03-22 14:57:31 +00004622 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004623 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004624 return value << (distance & max_shift_distance);
4625 }
4626
4627 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004628 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004629 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004630 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004631 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004632 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004633 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004634 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004635 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4636 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4637 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4638 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004639 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004640 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4641 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004642 LOG(FATAL) << DebugName() << " is not defined for float values";
4643 UNREACHABLE();
4644 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004645 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4646 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004647 LOG(FATAL) << DebugName() << " is not defined for double values";
4648 UNREACHABLE();
4649 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004650
4651 DECLARE_INSTRUCTION(Shl);
4652
4653 private:
4654 DISALLOW_COPY_AND_ASSIGN(HShl);
4655};
4656
Vladimir Markofcb503c2016-05-18 12:48:17 +01004657class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004658 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004659 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004660 HInstruction* value,
4661 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004662 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004663 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4664 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4665 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004666 }
4667
Roland Levillain5b5b9312016-03-22 14:57:31 +00004668 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004669 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004670 return value >> (distance & max_shift_distance);
4671 }
4672
4673 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004674 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004675 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004676 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004677 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004678 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004679 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004680 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004681 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4682 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4683 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4684 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004685 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004686 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4687 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004688 LOG(FATAL) << DebugName() << " is not defined for float values";
4689 UNREACHABLE();
4690 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004691 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4692 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004693 LOG(FATAL) << DebugName() << " is not defined for double values";
4694 UNREACHABLE();
4695 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004696
4697 DECLARE_INSTRUCTION(Shr);
4698
4699 private:
4700 DISALLOW_COPY_AND_ASSIGN(HShr);
4701};
4702
Vladimir Markofcb503c2016-05-18 12:48:17 +01004703class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004704 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004705 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004706 HInstruction* value,
4707 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004708 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004709 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4710 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4711 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004712 }
4713
Roland Levillain5b5b9312016-03-22 14:57:31 +00004714 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004715 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004716 typedef typename std::make_unsigned<T>::type V;
4717 V ux = static_cast<V>(value);
4718 return static_cast<T>(ux >> (distance & max_shift_distance));
4719 }
4720
4721 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004722 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004723 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004724 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004725 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004726 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004727 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004728 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004729 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4730 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4731 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4732 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004733 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004734 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4735 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004736 LOG(FATAL) << DebugName() << " is not defined for float values";
4737 UNREACHABLE();
4738 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004739 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4740 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004741 LOG(FATAL) << DebugName() << " is not defined for double values";
4742 UNREACHABLE();
4743 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004744
4745 DECLARE_INSTRUCTION(UShr);
4746
4747 private:
4748 DISALLOW_COPY_AND_ASSIGN(HUShr);
4749};
4750
Vladimir Markofcb503c2016-05-18 12:48:17 +01004751class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004752 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004753 HAnd(Primitive::Type result_type,
4754 HInstruction* left,
4755 HInstruction* right,
4756 uint32_t dex_pc = kNoDexPc)
4757 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004758
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004759 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004760
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004761 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004762
4763 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004764 return GetBlock()->GetGraph()->GetIntConstant(
4765 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004766 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004767 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004768 return GetBlock()->GetGraph()->GetLongConstant(
4769 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004770 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004771 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4772 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4773 LOG(FATAL) << DebugName() << " is not defined for float values";
4774 UNREACHABLE();
4775 }
4776 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4777 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4778 LOG(FATAL) << DebugName() << " is not defined for double values";
4779 UNREACHABLE();
4780 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004781
4782 DECLARE_INSTRUCTION(And);
4783
4784 private:
4785 DISALLOW_COPY_AND_ASSIGN(HAnd);
4786};
4787
Vladimir Markofcb503c2016-05-18 12:48:17 +01004788class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004789 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004790 HOr(Primitive::Type result_type,
4791 HInstruction* left,
4792 HInstruction* right,
4793 uint32_t dex_pc = kNoDexPc)
4794 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004795
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004796 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004797
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004798 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004799
4800 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004801 return GetBlock()->GetGraph()->GetIntConstant(
4802 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004803 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004804 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004805 return GetBlock()->GetGraph()->GetLongConstant(
4806 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004807 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004808 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4809 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4810 LOG(FATAL) << DebugName() << " is not defined for float values";
4811 UNREACHABLE();
4812 }
4813 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4814 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4815 LOG(FATAL) << DebugName() << " is not defined for double values";
4816 UNREACHABLE();
4817 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004818
4819 DECLARE_INSTRUCTION(Or);
4820
4821 private:
4822 DISALLOW_COPY_AND_ASSIGN(HOr);
4823};
4824
Vladimir Markofcb503c2016-05-18 12:48:17 +01004825class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004826 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004827 HXor(Primitive::Type result_type,
4828 HInstruction* left,
4829 HInstruction* right,
4830 uint32_t dex_pc = kNoDexPc)
4831 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004832
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004833 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004834
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004835 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004836
4837 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004838 return GetBlock()->GetGraph()->GetIntConstant(
4839 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004840 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004841 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004842 return GetBlock()->GetGraph()->GetLongConstant(
4843 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004844 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004845 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4846 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4847 LOG(FATAL) << DebugName() << " is not defined for float values";
4848 UNREACHABLE();
4849 }
4850 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4851 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4852 LOG(FATAL) << DebugName() << " is not defined for double values";
4853 UNREACHABLE();
4854 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004855
4856 DECLARE_INSTRUCTION(Xor);
4857
4858 private:
4859 DISALLOW_COPY_AND_ASSIGN(HXor);
4860};
4861
Vladimir Markofcb503c2016-05-18 12:48:17 +01004862class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004863 public:
4864 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004865 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004866 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4867 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004868 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004869
Roland Levillain5b5b9312016-03-22 14:57:31 +00004870 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004871 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004872 typedef typename std::make_unsigned<T>::type V;
4873 V ux = static_cast<V>(value);
4874 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004875 return static_cast<T>(ux);
4876 } else {
4877 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004878 return static_cast<T>(ux >> (distance & max_shift_value)) |
4879 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004880 }
4881 }
4882
Roland Levillain5b5b9312016-03-22 14:57:31 +00004883 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004884 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004885 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004886 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004887 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004888 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004889 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004890 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004891 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4892 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4893 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4894 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004895 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004896 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4897 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004898 LOG(FATAL) << DebugName() << " is not defined for float values";
4899 UNREACHABLE();
4900 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004901 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4902 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004903 LOG(FATAL) << DebugName() << " is not defined for double values";
4904 UNREACHABLE();
4905 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004906
4907 DECLARE_INSTRUCTION(Ror);
4908
4909 private:
4910 DISALLOW_COPY_AND_ASSIGN(HRor);
4911};
4912
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004913// The value of a parameter in this method. Its location depends on
4914// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004915class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004916 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004917 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004918 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004919 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004920 Primitive::Type parameter_type,
4921 bool is_this = false)
4922 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004923 dex_file_(dex_file),
4924 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004925 index_(index) {
4926 SetPackedFlag<kFlagIsThis>(is_this);
4927 SetPackedFlag<kFlagCanBeNull>(!is_this);
4928 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004929
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004930 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08004931 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004932 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004933 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004934
Vladimir Markoa1de9182016-02-25 11:37:38 +00004935 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4936 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004937
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004938 DECLARE_INSTRUCTION(ParameterValue);
4939
4940 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004941 // Whether or not the parameter value corresponds to 'this' argument.
4942 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4943 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4944 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4945 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4946 "Too many packed fields.");
4947
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004948 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004949 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004950 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004951 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004952 const uint8_t index_;
4953
4954 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4955};
4956
Vladimir Markofcb503c2016-05-18 12:48:17 +01004957class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004958 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004959 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4960 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004961
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004962 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004963 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004964 return true;
4965 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004966
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004967 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004968
4969 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004970 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004971 }
4972 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004973 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004974 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004975 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4976 LOG(FATAL) << DebugName() << " is not defined for float values";
4977 UNREACHABLE();
4978 }
4979 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4980 LOG(FATAL) << DebugName() << " is not defined for double values";
4981 UNREACHABLE();
4982 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004983
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004984 DECLARE_INSTRUCTION(Not);
4985
4986 private:
4987 DISALLOW_COPY_AND_ASSIGN(HNot);
4988};
4989
Vladimir Markofcb503c2016-05-18 12:48:17 +01004990class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004991 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004992 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4993 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004994
4995 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004996 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004997 return true;
4998 }
4999
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005000 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005001 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005002 return !x;
5003 }
5004
Roland Levillain9867bc72015-08-05 10:21:34 +01005005 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005006 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005007 }
5008 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5009 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005010 UNREACHABLE();
5011 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005012 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5013 LOG(FATAL) << DebugName() << " is not defined for float values";
5014 UNREACHABLE();
5015 }
5016 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5017 LOG(FATAL) << DebugName() << " is not defined for double values";
5018 UNREACHABLE();
5019 }
David Brazdil66d126e2015-04-03 16:02:44 +01005020
5021 DECLARE_INSTRUCTION(BooleanNot);
5022
5023 private:
5024 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5025};
5026
Vladimir Markofcb503c2016-05-18 12:48:17 +01005027class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005028 public:
5029 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005030 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005031 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005032 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005033 // Invariant: We should never generate a conversion to a Boolean value.
5034 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005035 }
5036
5037 HInstruction* GetInput() const { return InputAt(0); }
5038 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5039 Primitive::Type GetResultType() const { return GetType(); }
5040
5041 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005042 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5043 return true;
5044 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005045
Mark Mendelle82549b2015-05-06 10:55:34 -04005046 // Try to statically evaluate the conversion and return a HConstant
5047 // containing the result. If the input cannot be converted, return nullptr.
5048 HConstant* TryStaticEvaluation() const;
5049
Roland Levillaindff1f282014-11-05 14:15:05 +00005050 DECLARE_INSTRUCTION(TypeConversion);
5051
5052 private:
5053 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5054};
5055
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005056static constexpr uint32_t kNoRegNumber = -1;
5057
Vladimir Markofcb503c2016-05-18 12:48:17 +01005058class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005059 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005060 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5061 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005062 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005063 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005064 SetRawInputAt(0, value);
5065 }
5066
Calin Juravle10e244f2015-01-26 18:54:32 +00005067 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005068 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005069 return true;
5070 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005071
Calin Juravle10e244f2015-01-26 18:54:32 +00005072 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005073
Calin Juravle10e244f2015-01-26 18:54:32 +00005074 bool CanThrow() const OVERRIDE { return true; }
5075
5076 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005077
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005078
5079 DECLARE_INSTRUCTION(NullCheck);
5080
5081 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005082 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5083};
5084
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005085// Embeds an ArtField and all the information required by the compiler. We cache
5086// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005087class FieldInfo : public ValueObject {
5088 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005089 FieldInfo(ArtField* field,
5090 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005091 Primitive::Type field_type,
5092 bool is_volatile,
5093 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005094 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005095 const DexFile& dex_file)
5096 : field_(field),
5097 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005098 field_type_(field_type),
5099 is_volatile_(is_volatile),
5100 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005101 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005102 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005103
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005104 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005105 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005106 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005107 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005108 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005109 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005110 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005111
5112 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005113 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005114 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005115 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005116 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005117 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005118 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005119 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005120};
5121
Vladimir Markofcb503c2016-05-18 12:48:17 +01005122class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005123 public:
5124 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005125 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005126 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005127 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005128 bool is_volatile,
5129 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005130 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005131 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005132 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005133 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005134 field_info_(field,
5135 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005136 field_type,
5137 is_volatile,
5138 field_idx,
5139 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005140 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005141 SetRawInputAt(0, value);
5142 }
5143
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005144 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005145
Vladimir Marko372f10e2016-05-17 16:30:10 +01005146 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5147 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005148 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005149 }
5150
Calin Juravle641547a2015-04-21 22:08:51 +01005151 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005152 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005153 }
5154
5155 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005156 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5157 }
5158
Calin Juravle52c48962014-12-16 17:02:57 +00005159 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005160 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005161 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005162 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005163
5164 DECLARE_INSTRUCTION(InstanceFieldGet);
5165
5166 private:
5167 const FieldInfo field_info_;
5168
5169 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5170};
5171
Vladimir Markofcb503c2016-05-18 12:48:17 +01005172class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005173 public:
5174 HInstanceFieldSet(HInstruction* object,
5175 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005176 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005177 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005178 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005179 bool is_volatile,
5180 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005181 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005182 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005183 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005184 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005185 field_info_(field,
5186 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005187 field_type,
5188 is_volatile,
5189 field_idx,
5190 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005191 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005192 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005193 SetRawInputAt(0, object);
5194 SetRawInputAt(1, value);
5195 }
5196
Calin Juravle641547a2015-04-21 22:08:51 +01005197 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005198 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005199 }
5200
Calin Juravle52c48962014-12-16 17:02:57 +00005201 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005202 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005203 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005204 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005205 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005206 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5207 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005208
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005209 DECLARE_INSTRUCTION(InstanceFieldSet);
5210
5211 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005212 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5213 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5214 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5215 "Too many packed fields.");
5216
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005217 const FieldInfo field_info_;
5218
5219 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5220};
5221
Vladimir Markofcb503c2016-05-18 12:48:17 +01005222class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005223 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005224 HArrayGet(HInstruction* array,
5225 HInstruction* index,
5226 Primitive::Type type,
5227 uint32_t dex_pc,
5228 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005229 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005230 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005231 SetRawInputAt(0, array);
5232 SetRawInputAt(1, index);
5233 }
5234
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005235 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005236 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005237 return true;
5238 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005239 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005240 // TODO: We can be smarter here.
5241 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5242 // which generates the implicit null check. There are cases when these can be removed
5243 // to produce better code. If we ever add optimizations to do so we should allow an
5244 // implicit check here (as long as the address falls in the first page).
5245 return false;
5246 }
5247
David Brazdil4833f5a2015-12-16 10:37:39 +00005248 bool IsEquivalentOf(HArrayGet* other) const {
5249 bool result = (GetDexPc() == other->GetDexPc());
5250 if (kIsDebugBuild && result) {
5251 DCHECK_EQ(GetBlock(), other->GetBlock());
5252 DCHECK_EQ(GetArray(), other->GetArray());
5253 DCHECK_EQ(GetIndex(), other->GetIndex());
5254 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005255 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005256 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005257 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5258 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005259 }
5260 }
5261 return result;
5262 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005263
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005264 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5265
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005266 HInstruction* GetArray() const { return InputAt(0); }
5267 HInstruction* GetIndex() const { return InputAt(1); }
5268
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005269 DECLARE_INSTRUCTION(ArrayGet);
5270
5271 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005272 // We treat a String as an array, creating the HArrayGet from String.charAt()
5273 // intrinsic in the instruction simplifier. We can always determine whether
5274 // a particular HArrayGet is actually a String.charAt() by looking at the type
5275 // of the input but that requires holding the mutator lock, so we prefer to use
5276 // a flag, so that code generators don't need to do the locking.
5277 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5278 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5279 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5280 "Too many packed fields.");
5281
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005282 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5283};
5284
Vladimir Markofcb503c2016-05-18 12:48:17 +01005285class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005286 public:
5287 HArraySet(HInstruction* array,
5288 HInstruction* index,
5289 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005290 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005291 uint32_t dex_pc)
5292 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005293 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5294 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5295 SetPackedFlag<kFlagValueCanBeNull>(true);
5296 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005297 SetRawInputAt(0, array);
5298 SetRawInputAt(1, index);
5299 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005300 // Make a best guess now, may be refined during SSA building.
5301 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005302 }
5303
Calin Juravle77520bc2015-01-12 18:45:46 +00005304 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005305 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005306 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005307 }
5308
Mingyao Yang81014cb2015-06-02 03:16:27 -07005309 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005310 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005311
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005312 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005313 // TODO: Same as for ArrayGet.
5314 return false;
5315 }
5316
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005317 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005318 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005319 }
5320
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005321 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005322 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005323 }
5324
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005325 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005326 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005327 }
5328
Vladimir Markoa1de9182016-02-25 11:37:38 +00005329 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5330 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5331 bool StaticTypeOfArrayIsObjectArray() const {
5332 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5333 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005334
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005335 HInstruction* GetArray() const { return InputAt(0); }
5336 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005337 HInstruction* GetValue() const { return InputAt(2); }
5338
5339 Primitive::Type GetComponentType() const {
5340 // The Dex format does not type floating point index operations. Since the
5341 // `expected_component_type_` is set during building and can therefore not
5342 // be correct, we also check what is the value type. If it is a floating
5343 // point type, we must use that type.
5344 Primitive::Type value_type = GetValue()->GetType();
5345 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5346 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005347 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005348 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005349
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005350 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005351 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005352 }
5353
Aart Bik18b36ab2016-04-13 16:41:35 -07005354 void ComputeSideEffects() {
5355 Primitive::Type type = GetComponentType();
5356 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5357 SideEffectsForArchRuntimeCalls(type)));
5358 }
5359
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005360 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5361 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5362 }
5363
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005364 DECLARE_INSTRUCTION(ArraySet);
5365
5366 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005367 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5368 static constexpr size_t kFieldExpectedComponentTypeSize =
5369 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5370 static constexpr size_t kFlagNeedsTypeCheck =
5371 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5372 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005373 // Cached information for the reference_type_info_ so that codegen
5374 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005375 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5376 static constexpr size_t kNumberOfArraySetPackedBits =
5377 kFlagStaticTypeOfArrayIsObjectArray + 1;
5378 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5379 using ExpectedComponentTypeField =
5380 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005381
5382 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5383};
5384
Vladimir Markofcb503c2016-05-18 12:48:17 +01005385class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005386 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005387 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005388 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005389 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005390 // Note that arrays do not change length, so the instruction does not
5391 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005392 SetRawInputAt(0, array);
5393 }
5394
Calin Juravle77520bc2015-01-12 18:45:46 +00005395 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005396 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005397 return true;
5398 }
Calin Juravle641547a2015-04-21 22:08:51 +01005399 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5400 return obj == InputAt(0);
5401 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005402
Vladimir Markodce016e2016-04-28 13:10:02 +01005403 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5404
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005405 DECLARE_INSTRUCTION(ArrayLength);
5406
5407 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005408 // We treat a String as an array, creating the HArrayLength from String.length()
5409 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5410 // determine whether a particular HArrayLength is actually a String.length() by
5411 // looking at the type of the input but that requires holding the mutator lock, so
5412 // we prefer to use a flag, so that code generators don't need to do the locking.
5413 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5414 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5415 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5416 "Too many packed fields.");
5417
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005418 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5419};
5420
Vladimir Markofcb503c2016-05-18 12:48:17 +01005421class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005422 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005423 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5424 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005425 HBoundsCheck(HInstruction* index,
5426 HInstruction* length,
5427 uint32_t dex_pc,
5428 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5429 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5430 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005431 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005432 SetRawInputAt(0, index);
5433 SetRawInputAt(1, length);
5434 }
5435
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005436 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005437 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005438 return true;
5439 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005440
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005441 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005442
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005443 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005444
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005445 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5446 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5447
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005448 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005449
5450 DECLARE_INSTRUCTION(BoundsCheck);
5451
5452 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005453 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5454 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5455 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5456 // so we need to create an HEnvironment which will be translated to an InlineInfo
5457 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5458 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5459 // from the invoke as we don't want to look again at the dex bytecode.
5460 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5461
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005462 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5463};
5464
Vladimir Markofcb503c2016-05-18 12:48:17 +01005465class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005466 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005467 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005468 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005469
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005470 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005471 return true;
5472 }
5473
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005474 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5475 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005476
5477 DECLARE_INSTRUCTION(SuspendCheck);
5478
5479 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005480 // Only used for code generation, in order to share the same slow path between back edges
5481 // of a same loop.
5482 SlowPathCode* slow_path_;
5483
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005484 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5485};
5486
David Srbecky0cf44932015-12-09 14:09:59 +00005487// Pseudo-instruction which provides the native debugger with mapping information.
5488// It ensures that we can generate line number and local variables at this point.
5489class HNativeDebugInfo : public HTemplateInstruction<0> {
5490 public:
5491 explicit HNativeDebugInfo(uint32_t dex_pc)
5492 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5493
5494 bool NeedsEnvironment() const OVERRIDE {
5495 return true;
5496 }
5497
5498 DECLARE_INSTRUCTION(NativeDebugInfo);
5499
5500 private:
5501 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5502};
5503
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005504/**
5505 * Instruction to load a Class object.
5506 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005507class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005508 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005509 // Determines how to load the Class.
5510 enum class LoadKind {
5511 // Use the Class* from the method's own ArtMethod*.
5512 kReferrersClass,
5513
5514 // Use boot image Class* address that will be known at link time.
5515 // Used for boot image classes referenced by boot image code in non-PIC mode.
5516 kBootImageLinkTimeAddress,
5517
5518 // Use PC-relative boot image Class* address that will be known at link time.
5519 // Used for boot image classes referenced by boot image code in PIC mode.
5520 kBootImageLinkTimePcRelative,
5521
5522 // Use a known boot image Class* address, embedded in the code by the codegen.
5523 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5524 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5525 // GetIncludePatchInformation().
5526 kBootImageAddress,
5527
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005528 // Load from the root table associated with the JIT compiled method.
5529 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005530
5531 // Load from resolved types array in the dex cache using a PC-relative load.
5532 // Used for classes outside boot image when we know that we can access
5533 // the dex cache arrays using a PC-relative load.
5534 kDexCachePcRelative,
5535
5536 // Load from resolved types array accessed through the class loaded from
5537 // the compiled method's own ArtMethod*. This is the default access type when
5538 // all other types are unavailable.
5539 kDexCacheViaMethod,
5540
5541 kLast = kDexCacheViaMethod
5542 };
5543
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005544 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005545 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005546 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005547 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005548 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005549 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005550 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5551 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005552 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005553 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005554 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005555 // Referrers class should not need access check. We never inline unverified
5556 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005557 DCHECK(!is_referrers_class || !needs_access_check);
5558
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005559 SetPackedField<LoadKindField>(
5560 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005561 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005562 SetPackedFlag<kFlagIsInDexCache>(false);
5563 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005564 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005565 }
5566
5567 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5568 DCHECK(HasAddress(load_kind));
5569 load_data_.address = address;
5570 SetLoadKindInternal(load_kind);
5571 }
5572
5573 void SetLoadKindWithTypeReference(LoadKind load_kind,
5574 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005575 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005576 DCHECK(HasTypeReference(load_kind));
5577 DCHECK(IsSameDexFile(dex_file_, dex_file));
5578 DCHECK_EQ(type_index_, type_index);
5579 SetLoadKindInternal(load_kind);
5580 }
5581
5582 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5583 const DexFile& dex_file,
5584 uint32_t element_index) {
5585 DCHECK(HasDexCacheReference(load_kind));
5586 DCHECK(IsSameDexFile(dex_file_, dex_file));
5587 load_data_.dex_cache_element_index = element_index;
5588 SetLoadKindInternal(load_kind);
5589 }
5590
5591 LoadKind GetLoadKind() const {
5592 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005593 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005594
5595 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005596
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005597 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005598
Andreas Gampea5b09a62016-11-17 15:21:22 -08005599 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005600
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005601 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005602
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005603 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005604 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005605 }
5606
Calin Juravle0ba218d2015-05-19 18:46:01 +01005607 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005608 // The entrypoint the code generator is going to call does not do
5609 // clinit of the class.
5610 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005611 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005612 }
5613
5614 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005615 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005616 (!IsReferrersClass() && !IsInDexCache()) ||
5617 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005618 }
5619
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005620 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005621 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005622 }
5623
Calin Juravleacf735c2015-02-12 15:25:22 +00005624 ReferenceTypeInfo GetLoadedClassRTI() {
5625 return loaded_class_rti_;
5626 }
5627
5628 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5629 // Make sure we only set exact types (the loaded class should never be merged).
5630 DCHECK(rti.IsExact());
5631 loaded_class_rti_ = rti;
5632 }
5633
Andreas Gampea5b09a62016-11-17 15:21:22 -08005634 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005635 const DexFile& GetDexFile() const { return dex_file_; }
5636
5637 uint32_t GetDexCacheElementOffset() const;
5638
5639 uint64_t GetAddress() const {
5640 DCHECK(HasAddress(GetLoadKind()));
5641 return load_data_.address;
5642 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005643
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005644 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5645 return !IsReferrersClass();
5646 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005647
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005648 static SideEffects SideEffectsForArchRuntimeCalls() {
5649 return SideEffects::CanTriggerGC();
5650 }
5651
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005652 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005653 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5654 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005655 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005656 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005657
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005658 void MarkInDexCache() {
5659 SetPackedFlag<kFlagIsInDexCache>(true);
5660 DCHECK(!NeedsEnvironment());
5661 RemoveEnvironment();
5662 SetSideEffects(SideEffects::None());
5663 }
5664
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005665 void MarkInBootImage() {
5666 SetPackedFlag<kFlagIsInBootImage>(true);
5667 }
5668
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005669 void AddSpecialInput(HInstruction* special_input);
5670
5671 using HInstruction::GetInputRecords; // Keep the const version visible.
5672 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5673 return ArrayRef<HUserRecord<HInstruction*>>(
5674 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5675 }
5676
5677 Primitive::Type GetType() const OVERRIDE {
5678 return Primitive::kPrimNot;
5679 }
5680
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005681 DECLARE_INSTRUCTION(LoadClass);
5682
5683 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005684 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005685 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005686 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005687 // Whether this instruction must generate the initialization check.
5688 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005689 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005690 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5691 static constexpr size_t kFieldLoadKindSize =
5692 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5693 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005694 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005695 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5696
5697 static bool HasTypeReference(LoadKind load_kind) {
5698 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5699 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5700 load_kind == LoadKind::kDexCacheViaMethod ||
5701 load_kind == LoadKind::kReferrersClass;
5702 }
5703
5704 static bool HasAddress(LoadKind load_kind) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005705 return load_kind == LoadKind::kBootImageAddress ||
5706 load_kind == LoadKind::kJitTableAddress;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005707 }
5708
5709 static bool HasDexCacheReference(LoadKind load_kind) {
5710 return load_kind == LoadKind::kDexCachePcRelative;
5711 }
5712
5713 void SetLoadKindInternal(LoadKind load_kind);
5714
5715 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5716 // For other load kinds it's empty or possibly some architecture-specific instruction
5717 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5718 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005719
Andreas Gampea5b09a62016-11-17 15:21:22 -08005720 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005721 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005722
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005723 union {
5724 uint32_t dex_cache_element_index; // Only for dex cache reference.
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005725 uint64_t address; // Up to 64-bit, needed for kJitTableAddress on 64-bit targets.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005726 } load_data_;
5727
Calin Juravleacf735c2015-02-12 15:25:22 +00005728 ReferenceTypeInfo loaded_class_rti_;
5729
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005730 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5731};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005732std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5733
5734// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5735inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5736 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5737 return load_data_.dex_cache_element_index;
5738}
5739
5740// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5741inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005742 // The special input is used for PC-relative loads on some architectures,
5743 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005744 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005745 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5746 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5747 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005748 DCHECK(special_input_.GetInstruction() == nullptr);
5749 special_input_ = HUserRecord<HInstruction*>(special_input);
5750 special_input->AddUseAt(this, 0);
5751}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005752
Vladimir Marko372f10e2016-05-17 16:30:10 +01005753class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005754 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005755 // Determines how to load the String.
5756 enum class LoadKind {
5757 // Use boot image String* address that will be known at link time.
5758 // Used for boot image strings referenced by boot image code in non-PIC mode.
5759 kBootImageLinkTimeAddress,
5760
5761 // Use PC-relative boot image String* address that will be known at link time.
5762 // Used for boot image strings referenced by boot image code in PIC mode.
5763 kBootImageLinkTimePcRelative,
5764
5765 // Use a known boot image String* address, embedded in the code by the codegen.
5766 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5767 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5768 // GetIncludePatchInformation().
5769 kBootImageAddress,
5770
Vladimir Markoaad75c62016-10-03 08:46:48 +00005771 // Load from an entry in the .bss section using a PC-relative load.
5772 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5773 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005774
5775 // Load from resolved strings array accessed through the class loaded from
5776 // the compiled method's own ArtMethod*. This is the default access type when
5777 // all other types are unavailable.
5778 kDexCacheViaMethod,
5779
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005780 // Load from the root table associated with the JIT compiled method.
5781 kJitTableAddress,
5782
5783 kLast = kJitTableAddress,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005784 };
5785
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005786 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005787 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005788 const DexFile& dex_file,
5789 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005790 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5791 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005792 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005793 SetPackedFlag<kFlagIsInDexCache>(false);
5794 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005795 load_data_.dex_file_ = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005796 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005797
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005798 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5799 DCHECK(HasAddress(load_kind));
5800 load_data_.address = address;
5801 SetLoadKindInternal(load_kind);
5802 }
5803
5804 void SetLoadKindWithStringReference(LoadKind load_kind,
5805 const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005806 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005807 DCHECK(HasStringReference(load_kind));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005808 load_data_.dex_file_ = &dex_file;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005809 string_index_ = string_index;
5810 SetLoadKindInternal(load_kind);
5811 }
5812
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005813 LoadKind GetLoadKind() const {
5814 return GetPackedField<LoadKindField>();
5815 }
5816
5817 const DexFile& GetDexFile() const;
5818
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005819 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005820 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5821 return string_index_;
5822 }
5823
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005824 uint64_t GetAddress() const {
5825 DCHECK(HasAddress(GetLoadKind()));
5826 return load_data_.address;
5827 }
5828
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005829 bool CanBeMoved() const OVERRIDE { return true; }
5830
Vladimir Marko372f10e2016-05-17 16:30:10 +01005831 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005832
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005833 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005834
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005835 // Will call the runtime if we need to load the string through
5836 // the dex cache and the string is not guaranteed to be there yet.
5837 bool NeedsEnvironment() const OVERRIDE {
5838 LoadKind load_kind = GetLoadKind();
5839 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5840 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005841 load_kind == LoadKind::kBootImageAddress ||
5842 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005843 return false;
5844 }
5845 return !IsInDexCache();
5846 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005847
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005848 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5849 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5850 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005851
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005852 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005853 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005854
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005855 static SideEffects SideEffectsForArchRuntimeCalls() {
5856 return SideEffects::CanTriggerGC();
5857 }
5858
Vladimir Markoa1de9182016-02-25 11:37:38 +00005859 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5860
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005861 void MarkInDexCache() {
5862 SetPackedFlag<kFlagIsInDexCache>(true);
5863 DCHECK(!NeedsEnvironment());
5864 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005865 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005866 }
5867
Vladimir Marko372f10e2016-05-17 16:30:10 +01005868 void AddSpecialInput(HInstruction* special_input);
5869
5870 using HInstruction::GetInputRecords; // Keep the const version visible.
5871 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5872 return ArrayRef<HUserRecord<HInstruction*>>(
5873 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005874 }
5875
Vladimir Marko372f10e2016-05-17 16:30:10 +01005876 Primitive::Type GetType() const OVERRIDE {
5877 return Primitive::kPrimNot;
5878 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005879
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005880 DECLARE_INSTRUCTION(LoadString);
5881
5882 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005883 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005884 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5885 static constexpr size_t kFieldLoadKindSize =
5886 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5887 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005888 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005889 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005890
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005891 static bool HasStringReference(LoadKind load_kind) {
5892 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5893 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005894 load_kind == LoadKind::kBssEntry ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005895 load_kind == LoadKind::kDexCacheViaMethod ||
5896 load_kind == LoadKind::kJitTableAddress;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005897 }
5898
5899 static bool HasAddress(LoadKind load_kind) {
Vladimir Marko54d6a202016-11-09 12:46:38 +00005900 return load_kind == LoadKind::kBootImageAddress;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005901 }
5902
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005903 void SetLoadKindInternal(LoadKind load_kind);
5904
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005905 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5906 // For other load kinds it's empty or possibly some architecture-specific instruction
5907 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005908 HUserRecord<HInstruction*> special_input_;
5909
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005910 // String index serves also as the hash code and it's also needed for slow-paths,
5911 // so it must not be overwritten with other load data.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005912 dex::StringIndex string_index_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005913
5914 union {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005915 const DexFile* dex_file_; // For string reference.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005916 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5917 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005918
5919 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5920};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005921std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5922
5923// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5924inline const DexFile& HLoadString::GetDexFile() const {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005925 DCHECK(HasStringReference(GetLoadKind())) << GetLoadKind();
5926 return *load_data_.dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005927}
5928
5929// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5930inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005931 // The special input is used for PC-relative loads on some architectures,
5932 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005933 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005934 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005935 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5936 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005937 // HLoadString::GetInputRecords() returns an empty array at this point,
5938 // so use the GetInputRecords() from the base class to set the input record.
5939 DCHECK(special_input_.GetInstruction() == nullptr);
5940 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005941 special_input->AddUseAt(this, 0);
5942}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005943
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005944/**
5945 * Performs an initialization check on its Class object input.
5946 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005947class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005948 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005949 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005950 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005951 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005952 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5953 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005954 SetRawInputAt(0, constant);
5955 }
5956
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005957 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005958 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005959 return true;
5960 }
5961
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005962 bool NeedsEnvironment() const OVERRIDE {
5963 // May call runtime to initialize the class.
5964 return true;
5965 }
5966
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005967 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005968
5969 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5970
5971 DECLARE_INSTRUCTION(ClinitCheck);
5972
5973 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005974 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5975};
5976
Vladimir Markofcb503c2016-05-18 12:48:17 +01005977class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005978 public:
5979 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005980 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005981 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005982 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005983 bool is_volatile,
5984 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005985 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005986 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005987 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005988 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005989 field_info_(field,
5990 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005991 field_type,
5992 is_volatile,
5993 field_idx,
5994 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005995 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005996 SetRawInputAt(0, cls);
5997 }
5998
Calin Juravle52c48962014-12-16 17:02:57 +00005999
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006000 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006001
Vladimir Marko372f10e2016-05-17 16:30:10 +01006002 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6003 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006004 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006005 }
6006
6007 size_t ComputeHashCode() const OVERRIDE {
6008 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6009 }
6010
Calin Juravle52c48962014-12-16 17:02:57 +00006011 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006012 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6013 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006014 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006015
6016 DECLARE_INSTRUCTION(StaticFieldGet);
6017
6018 private:
6019 const FieldInfo field_info_;
6020
6021 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6022};
6023
Vladimir Markofcb503c2016-05-18 12:48:17 +01006024class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006025 public:
6026 HStaticFieldSet(HInstruction* cls,
6027 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006028 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006029 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006030 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006031 bool is_volatile,
6032 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006033 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006034 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006035 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006036 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006037 field_info_(field,
6038 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006039 field_type,
6040 is_volatile,
6041 field_idx,
6042 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006043 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006044 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006045 SetRawInputAt(0, cls);
6046 SetRawInputAt(1, value);
6047 }
6048
Calin Juravle52c48962014-12-16 17:02:57 +00006049 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006050 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6051 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006052 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006053
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006054 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006055 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6056 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006057
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006058 DECLARE_INSTRUCTION(StaticFieldSet);
6059
6060 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006061 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6062 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6063 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6064 "Too many packed fields.");
6065
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006066 const FieldInfo field_info_;
6067
6068 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6069};
6070
Vladimir Markofcb503c2016-05-18 12:48:17 +01006071class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006072 public:
6073 HUnresolvedInstanceFieldGet(HInstruction* obj,
6074 Primitive::Type field_type,
6075 uint32_t field_index,
6076 uint32_t dex_pc)
6077 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6078 field_index_(field_index) {
6079 SetRawInputAt(0, obj);
6080 }
6081
6082 bool NeedsEnvironment() const OVERRIDE { return true; }
6083 bool CanThrow() const OVERRIDE { return true; }
6084
6085 Primitive::Type GetFieldType() const { return GetType(); }
6086 uint32_t GetFieldIndex() const { return field_index_; }
6087
6088 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6089
6090 private:
6091 const uint32_t field_index_;
6092
6093 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6094};
6095
Vladimir Markofcb503c2016-05-18 12:48:17 +01006096class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006097 public:
6098 HUnresolvedInstanceFieldSet(HInstruction* obj,
6099 HInstruction* value,
6100 Primitive::Type field_type,
6101 uint32_t field_index,
6102 uint32_t dex_pc)
6103 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006104 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006105 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006106 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006107 SetRawInputAt(0, obj);
6108 SetRawInputAt(1, value);
6109 }
6110
6111 bool NeedsEnvironment() const OVERRIDE { return true; }
6112 bool CanThrow() const OVERRIDE { return true; }
6113
Vladimir Markoa1de9182016-02-25 11:37:38 +00006114 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006115 uint32_t GetFieldIndex() const { return field_index_; }
6116
6117 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6118
6119 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006120 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6121 static constexpr size_t kFieldFieldTypeSize =
6122 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6123 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6124 kFieldFieldType + kFieldFieldTypeSize;
6125 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6126 "Too many packed fields.");
6127 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6128
Calin Juravlee460d1d2015-09-29 04:52:17 +01006129 const uint32_t field_index_;
6130
6131 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6132};
6133
Vladimir Markofcb503c2016-05-18 12:48:17 +01006134class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006135 public:
6136 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6137 uint32_t field_index,
6138 uint32_t dex_pc)
6139 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6140 field_index_(field_index) {
6141 }
6142
6143 bool NeedsEnvironment() const OVERRIDE { return true; }
6144 bool CanThrow() const OVERRIDE { return true; }
6145
6146 Primitive::Type GetFieldType() const { return GetType(); }
6147 uint32_t GetFieldIndex() const { return field_index_; }
6148
6149 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6150
6151 private:
6152 const uint32_t field_index_;
6153
6154 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6155};
6156
Vladimir Markofcb503c2016-05-18 12:48:17 +01006157class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006158 public:
6159 HUnresolvedStaticFieldSet(HInstruction* value,
6160 Primitive::Type field_type,
6161 uint32_t field_index,
6162 uint32_t dex_pc)
6163 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006164 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006165 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006166 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006167 SetRawInputAt(0, value);
6168 }
6169
6170 bool NeedsEnvironment() const OVERRIDE { return true; }
6171 bool CanThrow() const OVERRIDE { return true; }
6172
Vladimir Markoa1de9182016-02-25 11:37:38 +00006173 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006174 uint32_t GetFieldIndex() const { return field_index_; }
6175
6176 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6177
6178 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006179 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6180 static constexpr size_t kFieldFieldTypeSize =
6181 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6182 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6183 kFieldFieldType + kFieldFieldTypeSize;
6184 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6185 "Too many packed fields.");
6186 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6187
Calin Juravlee460d1d2015-09-29 04:52:17 +01006188 const uint32_t field_index_;
6189
6190 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6191};
6192
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006193// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006194class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006195 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006196 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6197 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006198
David Brazdilbbd733e2015-08-18 17:48:17 +01006199 bool CanBeNull() const OVERRIDE { return false; }
6200
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006201 DECLARE_INSTRUCTION(LoadException);
6202
6203 private:
6204 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6205};
6206
David Brazdilcb1c0552015-08-04 16:22:25 +01006207// Implicit part of move-exception which clears thread-local exception storage.
6208// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006209class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006210 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006211 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6212 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006213
6214 DECLARE_INSTRUCTION(ClearException);
6215
6216 private:
6217 DISALLOW_COPY_AND_ASSIGN(HClearException);
6218};
6219
Vladimir Markofcb503c2016-05-18 12:48:17 +01006220class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006221 public:
6222 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006223 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006224 SetRawInputAt(0, exception);
6225 }
6226
6227 bool IsControlFlow() const OVERRIDE { return true; }
6228
6229 bool NeedsEnvironment() const OVERRIDE { return true; }
6230
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006231 bool CanThrow() const OVERRIDE { return true; }
6232
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006233
6234 DECLARE_INSTRUCTION(Throw);
6235
6236 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006237 DISALLOW_COPY_AND_ASSIGN(HThrow);
6238};
6239
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006240/**
6241 * Implementation strategies for the code generator of a HInstanceOf
6242 * or `HCheckCast`.
6243 */
6244enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006245 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006246 kExactCheck, // Can do a single class compare.
6247 kClassHierarchyCheck, // Can just walk the super class chain.
6248 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6249 kInterfaceCheck, // No optimization yet when checking against an interface.
6250 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006251 kArrayCheck, // No optimization yet when checking against a generic array.
6252 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006253};
6254
Roland Levillain86503782016-02-11 19:07:30 +00006255std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6256
Vladimir Markofcb503c2016-05-18 12:48:17 +01006257class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006258 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006259 HInstanceOf(HInstruction* object,
6260 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006261 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006262 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006263 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006264 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006265 dex_pc) {
6266 SetPackedField<TypeCheckKindField>(check_kind);
6267 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006268 SetRawInputAt(0, object);
6269 SetRawInputAt(1, constant);
6270 }
6271
6272 bool CanBeMoved() const OVERRIDE { return true; }
6273
Vladimir Marko372f10e2016-05-17 16:30:10 +01006274 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006275 return true;
6276 }
6277
6278 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006279 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006280 }
6281
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006282 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006283 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6284 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6285 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6286 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006287
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006288 static bool CanCallRuntime(TypeCheckKind check_kind) {
6289 // Mips currently does runtime calls for any other checks.
6290 return check_kind != TypeCheckKind::kExactCheck;
6291 }
6292
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006293 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006294 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006295 }
6296
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006297 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006298
6299 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006300 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6301 static constexpr size_t kFieldTypeCheckKindSize =
6302 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6303 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6304 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6305 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6306 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006307
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006308 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6309};
6310
Vladimir Markofcb503c2016-05-18 12:48:17 +01006311class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006312 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006313 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006314 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006315 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6316 SetPackedFlag<kFlagUpperCanBeNull>(true);
6317 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006318 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006319 SetRawInputAt(0, input);
6320 }
6321
David Brazdilf5552582015-12-27 13:36:12 +00006322 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006323 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006324 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006325 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006326
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006327 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006328 DCHECK(GetUpperCanBeNull() || !can_be_null);
6329 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006330 }
6331
Vladimir Markoa1de9182016-02-25 11:37:38 +00006332 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006333
Calin Juravleb1498f62015-02-16 13:13:29 +00006334 DECLARE_INSTRUCTION(BoundType);
6335
6336 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006337 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6338 // is false then CanBeNull() cannot be true).
6339 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6340 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6341 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6342 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6343
Calin Juravleb1498f62015-02-16 13:13:29 +00006344 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006345 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6346 // It is used to bound the type in cases like:
6347 // if (x instanceof ClassX) {
6348 // // uper_bound_ will be ClassX
6349 // }
David Brazdilf5552582015-12-27 13:36:12 +00006350 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006351
6352 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6353};
6354
Vladimir Markofcb503c2016-05-18 12:48:17 +01006355class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006356 public:
6357 HCheckCast(HInstruction* object,
6358 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006359 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006360 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006361 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6362 SetPackedField<TypeCheckKindField>(check_kind);
6363 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006364 SetRawInputAt(0, object);
6365 SetRawInputAt(1, constant);
6366 }
6367
6368 bool CanBeMoved() const OVERRIDE { return true; }
6369
Vladimir Marko372f10e2016-05-17 16:30:10 +01006370 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006371 return true;
6372 }
6373
6374 bool NeedsEnvironment() const OVERRIDE {
6375 // Instruction may throw a CheckCastError.
6376 return true;
6377 }
6378
6379 bool CanThrow() const OVERRIDE { return true; }
6380
Vladimir Markoa1de9182016-02-25 11:37:38 +00006381 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6382 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6383 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6384 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006385
6386 DECLARE_INSTRUCTION(CheckCast);
6387
6388 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006389 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6390 static constexpr size_t kFieldTypeCheckKindSize =
6391 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6392 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6393 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6394 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6395 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006396
6397 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006398};
6399
Andreas Gampe26de38b2016-07-27 17:53:11 -07006400/**
6401 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6402 * @details We define the combined barrier types that are actually required
6403 * by the Java Memory Model, rather than using exactly the terminology from
6404 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6405 * primitives. Note that the JSR-133 cookbook generally does not deal with
6406 * store atomicity issues, and the recipes there are not always entirely sufficient.
6407 * The current recipe is as follows:
6408 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6409 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6410 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6411 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6412 * class has final fields.
6413 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6414 * store-to-memory instructions. Only generated together with non-temporal stores.
6415 */
6416enum MemBarrierKind {
6417 kAnyStore,
6418 kLoadAny,
6419 kStoreStore,
6420 kAnyAny,
6421 kNTStoreStore,
6422 kLastBarrierKind = kNTStoreStore
6423};
6424std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6425
Vladimir Markofcb503c2016-05-18 12:48:17 +01006426class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006427 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006428 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006429 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006430 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6431 SetPackedField<BarrierKindField>(barrier_kind);
6432 }
Calin Juravle27df7582015-04-17 19:12:31 +01006433
Vladimir Markoa1de9182016-02-25 11:37:38 +00006434 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006435
6436 DECLARE_INSTRUCTION(MemoryBarrier);
6437
6438 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006439 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6440 static constexpr size_t kFieldBarrierKindSize =
6441 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6442 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6443 kFieldBarrierKind + kFieldBarrierKindSize;
6444 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6445 "Too many packed fields.");
6446 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006447
6448 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6449};
6450
Vladimir Markofcb503c2016-05-18 12:48:17 +01006451class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006452 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006453 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006454 kEnter,
6455 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006456 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006457 };
6458
6459 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006460 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006461 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6462 dex_pc) {
6463 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006464 SetRawInputAt(0, object);
6465 }
6466
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006467 // Instruction may go into runtime, so we need an environment.
6468 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006469
6470 bool CanThrow() const OVERRIDE {
6471 // Verifier guarantees that monitor-exit cannot throw.
6472 // This is important because it allows the HGraphBuilder to remove
6473 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6474 return IsEnter();
6475 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006476
Vladimir Markoa1de9182016-02-25 11:37:38 +00006477 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6478 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006479
6480 DECLARE_INSTRUCTION(MonitorOperation);
6481
Calin Juravle52c48962014-12-16 17:02:57 +00006482 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006483 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6484 static constexpr size_t kFieldOperationKindSize =
6485 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6486 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6487 kFieldOperationKind + kFieldOperationKindSize;
6488 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6489 "Too many packed fields.");
6490 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006491
6492 private:
6493 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6494};
6495
Vladimir Markofcb503c2016-05-18 12:48:17 +01006496class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006497 public:
6498 HSelect(HInstruction* condition,
6499 HInstruction* true_value,
6500 HInstruction* false_value,
6501 uint32_t dex_pc)
6502 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6503 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6504
6505 // First input must be `true_value` or `false_value` to allow codegens to
6506 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6507 // that architectures which implement HSelect as a conditional move also
6508 // will not need to invert the condition.
6509 SetRawInputAt(0, false_value);
6510 SetRawInputAt(1, true_value);
6511 SetRawInputAt(2, condition);
6512 }
6513
6514 HInstruction* GetFalseValue() const { return InputAt(0); }
6515 HInstruction* GetTrueValue() const { return InputAt(1); }
6516 HInstruction* GetCondition() const { return InputAt(2); }
6517
6518 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006519 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6520 return true;
6521 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006522
6523 bool CanBeNull() const OVERRIDE {
6524 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6525 }
6526
6527 DECLARE_INSTRUCTION(Select);
6528
6529 private:
6530 DISALLOW_COPY_AND_ASSIGN(HSelect);
6531};
6532
Vladimir Markof9f64412015-09-02 14:05:49 +01006533class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006534 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006535 MoveOperands(Location source,
6536 Location destination,
6537 Primitive::Type type,
6538 HInstruction* instruction)
6539 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006540
6541 Location GetSource() const { return source_; }
6542 Location GetDestination() const { return destination_; }
6543
6544 void SetSource(Location value) { source_ = value; }
6545 void SetDestination(Location value) { destination_ = value; }
6546
6547 // The parallel move resolver marks moves as "in-progress" by clearing the
6548 // destination (but not the source).
6549 Location MarkPending() {
6550 DCHECK(!IsPending());
6551 Location dest = destination_;
6552 destination_ = Location::NoLocation();
6553 return dest;
6554 }
6555
6556 void ClearPending(Location dest) {
6557 DCHECK(IsPending());
6558 destination_ = dest;
6559 }
6560
6561 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006562 DCHECK(source_.IsValid() || destination_.IsInvalid());
6563 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006564 }
6565
6566 // True if this blocks a move from the given location.
6567 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006568 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006569 }
6570
6571 // A move is redundant if it's been eliminated, if its source and
6572 // destination are the same, or if its destination is unneeded.
6573 bool IsRedundant() const {
6574 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6575 }
6576
6577 // We clear both operands to indicate move that's been eliminated.
6578 void Eliminate() {
6579 source_ = destination_ = Location::NoLocation();
6580 }
6581
6582 bool IsEliminated() const {
6583 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6584 return source_.IsInvalid();
6585 }
6586
Alexey Frunze4dda3372015-06-01 18:31:49 -07006587 Primitive::Type GetType() const { return type_; }
6588
Nicolas Geoffray90218252015-04-15 11:56:51 +01006589 bool Is64BitMove() const {
6590 return Primitive::Is64BitType(type_);
6591 }
6592
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006593 HInstruction* GetInstruction() const { return instruction_; }
6594
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006595 private:
6596 Location source_;
6597 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006598 // The type this move is for.
6599 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006600 // The instruction this move is assocatied with. Null when this move is
6601 // for moving an input in the expected locations of user (including a phi user).
6602 // This is only used in debug mode, to ensure we do not connect interval siblings
6603 // in the same parallel move.
6604 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006605};
6606
Roland Levillainc9285912015-12-18 10:38:42 +00006607std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6608
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006609static constexpr size_t kDefaultNumberOfMoves = 4;
6610
Vladimir Markofcb503c2016-05-18 12:48:17 +01006611class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006612 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006613 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006614 : HTemplateInstruction(SideEffects::None(), dex_pc),
6615 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6616 moves_.reserve(kDefaultNumberOfMoves);
6617 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006618
Nicolas Geoffray90218252015-04-15 11:56:51 +01006619 void AddMove(Location source,
6620 Location destination,
6621 Primitive::Type type,
6622 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006623 DCHECK(source.IsValid());
6624 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006625 if (kIsDebugBuild) {
6626 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006627 for (const MoveOperands& move : moves_) {
6628 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006629 // Special case the situation where the move is for the spill slot
6630 // of the instruction.
6631 if ((GetPrevious() == instruction)
6632 || ((GetPrevious() == nullptr)
6633 && instruction->IsPhi()
6634 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006635 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006636 << "Doing parallel moves for the same instruction.";
6637 } else {
6638 DCHECK(false) << "Doing parallel moves for the same instruction.";
6639 }
6640 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006641 }
6642 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006643 for (const MoveOperands& move : moves_) {
6644 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006645 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006646 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006647 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006648 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006649 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006650 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006651 }
6652
Vladimir Marko225b6462015-09-28 12:17:40 +01006653 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006654 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006655 }
6656
Vladimir Marko225b6462015-09-28 12:17:40 +01006657 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006658
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006659 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006660
6661 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006662 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006663
6664 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6665};
6666
Mark Mendell0616ae02015-04-17 12:49:27 -04006667} // namespace art
6668
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006669#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6670#include "nodes_shared.h"
6671#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006672#ifdef ART_ENABLE_CODEGEN_arm
6673#include "nodes_arm.h"
6674#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006675#ifdef ART_ENABLE_CODEGEN_arm64
6676#include "nodes_arm64.h"
6677#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006678#ifdef ART_ENABLE_CODEGEN_mips
6679#include "nodes_mips.h"
6680#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006681#ifdef ART_ENABLE_CODEGEN_x86
6682#include "nodes_x86.h"
6683#endif
6684
6685namespace art {
6686
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006687class HGraphVisitor : public ValueObject {
6688 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006689 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6690 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006691
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006692 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006693 virtual void VisitBasicBlock(HBasicBlock* block);
6694
Roland Levillain633021e2014-10-01 14:12:25 +01006695 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006696 void VisitInsertionOrder();
6697
Roland Levillain633021e2014-10-01 14:12:25 +01006698 // Visit the graph following dominator tree reverse post-order.
6699 void VisitReversePostOrder();
6700
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006701 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006702
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006703 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006704#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006705 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6706
6707 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6708
6709#undef DECLARE_VISIT_INSTRUCTION
6710
6711 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006712 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006713
6714 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6715};
6716
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006717class HGraphDelegateVisitor : public HGraphVisitor {
6718 public:
6719 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6720 virtual ~HGraphDelegateVisitor() {}
6721
6722 // Visit functions that delegate to to super class.
6723#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006724 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006725
6726 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6727
6728#undef DECLARE_VISIT_INSTRUCTION
6729
6730 private:
6731 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6732};
6733
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006734// Iterator over the blocks that art part of the loop. Includes blocks part
6735// of an inner loop. The order in which the blocks are iterated is on their
6736// block id.
6737class HBlocksInLoopIterator : public ValueObject {
6738 public:
6739 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6740 : blocks_in_loop_(info.GetBlocks()),
6741 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6742 index_(0) {
6743 if (!blocks_in_loop_.IsBitSet(index_)) {
6744 Advance();
6745 }
6746 }
6747
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006748 bool Done() const { return index_ == blocks_.size(); }
6749 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006750 void Advance() {
6751 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006752 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006753 if (blocks_in_loop_.IsBitSet(index_)) {
6754 break;
6755 }
6756 }
6757 }
6758
6759 private:
6760 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006761 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006762 size_t index_;
6763
6764 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6765};
6766
Mingyao Yang3584bce2015-05-19 16:01:59 -07006767// Iterator over the blocks that art part of the loop. Includes blocks part
6768// of an inner loop. The order in which the blocks are iterated is reverse
6769// post order.
6770class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6771 public:
6772 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6773 : blocks_in_loop_(info.GetBlocks()),
6774 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6775 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006776 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006777 Advance();
6778 }
6779 }
6780
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006781 bool Done() const { return index_ == blocks_.size(); }
6782 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006783 void Advance() {
6784 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006785 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6786 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006787 break;
6788 }
6789 }
6790 }
6791
6792 private:
6793 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006794 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006795 size_t index_;
6796
6797 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6798};
6799
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006800inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006801 if (constant->IsIntConstant()) {
6802 return constant->AsIntConstant()->GetValue();
6803 } else if (constant->IsLongConstant()) {
6804 return constant->AsLongConstant()->GetValue();
6805 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006806 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006807 return 0;
6808 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006809}
6810
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006811#define INSTRUCTION_TYPE_CHECK(type, super) \
6812 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6813 inline const H##type* HInstruction::As##type() const { \
6814 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6815 } \
6816 inline H##type* HInstruction::As##type() { \
6817 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6818 }
6819
6820 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6821#undef INSTRUCTION_TYPE_CHECK
6822
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006823// Create space in `blocks` for adding `number_of_new_blocks` entries
6824// starting at location `at`. Blocks after `at` are moved accordingly.
6825inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6826 size_t number_of_new_blocks,
6827 size_t after) {
6828 DCHECK_LT(after, blocks->size());
6829 size_t old_size = blocks->size();
6830 size_t new_size = old_size + number_of_new_blocks;
6831 blocks->resize(new_size);
6832 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6833}
6834
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006835} // namespace art
6836
6837#endif // ART_COMPILER_OPTIMIZING_NODES_H_