blob: ea9a94c420f22baffea8b9babfec6f4c2d07b8fa [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
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00003777class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003778 public:
3779 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003780 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003781 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003782 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003783 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003784 bool finalizable,
3785 QuickEntrypointEnum entrypoint)
3786 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3787 type_index_(type_index),
3788 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003789 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003790 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003791 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003792 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003793 }
3794
Andreas Gampea5b09a62016-11-17 15:21:22 -08003795 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003796 const DexFile& GetDexFile() const { return dex_file_; }
3797
3798 // Calls runtime so needs an environment.
3799 bool NeedsEnvironment() const OVERRIDE { return true; }
3800
Mingyao Yang062157f2016-03-02 10:15:36 -08003801 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3802 bool CanThrow() const OVERRIDE { return true; }
3803
3804 // Needs to call into runtime to make sure it's instantiable/accessible.
3805 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003806
Vladimir Markoa1de9182016-02-25 11:37:38 +00003807 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003808
3809 bool CanBeNull() const OVERRIDE { return false; }
3810
3811 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3812
3813 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3814 entrypoint_ = entrypoint;
3815 }
3816
3817 bool IsStringAlloc() const;
3818
3819 DECLARE_INSTRUCTION(NewInstance);
3820
3821 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003822 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3823 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003824 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3825 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3826 "Too many packed fields.");
3827
Andreas Gampea5b09a62016-11-17 15:21:22 -08003828 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003829 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003830 QuickEntrypointEnum entrypoint_;
3831
3832 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3833};
3834
Agi Csaki05f20562015-08-19 14:58:14 -07003835enum IntrinsicNeedsEnvironmentOrCache {
3836 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3837 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003838};
3839
Aart Bik5d75afe2015-12-14 11:57:01 -08003840enum IntrinsicSideEffects {
3841 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3842 kReadSideEffects, // Intrinsic may read heap memory.
3843 kWriteSideEffects, // Intrinsic may write heap memory.
3844 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3845};
3846
3847enum IntrinsicExceptions {
3848 kNoThrow, // Intrinsic does not throw any exceptions.
3849 kCanThrow // Intrinsic may throw exceptions.
3850};
3851
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003852class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003853 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003854 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003855
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003856 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003857 SetRawInputAt(index, argument);
3858 }
3859
Roland Levillain3e3d7332015-04-28 11:00:54 +01003860 // Return the number of arguments. This number can be lower than
3861 // the number of inputs returned by InputCount(), as some invoke
3862 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3863 // inputs at the end of their list of inputs.
3864 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3865
Vladimir Markoa1de9182016-02-25 11:37:38 +00003866 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003867
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003868 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003869 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003870
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003871 InvokeType GetInvokeType() const {
3872 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003873 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003874
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003875 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003876 return intrinsic_;
3877 }
3878
Aart Bik5d75afe2015-12-14 11:57:01 -08003879 void SetIntrinsic(Intrinsics intrinsic,
3880 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3881 IntrinsicSideEffects side_effects,
3882 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003883
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003884 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003885 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003886 }
3887
Aart Bikff7d89c2016-11-07 08:49:28 -08003888 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
3889
Vladimir Markoa1de9182016-02-25 11:37:38 +00003890 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003891
Aart Bik71bf7b42016-11-16 10:17:46 -08003892 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003893
Vladimir Marko372f10e2016-05-17 16:30:10 +01003894 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003895 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3896 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003897
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003898 uint32_t* GetIntrinsicOptimizations() {
3899 return &intrinsic_optimizations_;
3900 }
3901
3902 const uint32_t* GetIntrinsicOptimizations() const {
3903 return &intrinsic_optimizations_;
3904 }
3905
3906 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3907
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003908 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3909
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003910 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003911
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003912 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003913 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3914 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003915 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3916 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003917 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003918 static constexpr size_t kFieldReturnTypeSize =
3919 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3920 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3921 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3922 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003923 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003924 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3925
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003926 HInvoke(ArenaAllocator* arena,
3927 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003928 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003929 Primitive::Type return_type,
3930 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003931 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003932 ArtMethod* resolved_method,
3933 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003934 : HVariableInputSizeInstruction(
3935 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
3936 dex_pc,
3937 arena,
3938 number_of_arguments + number_of_other_inputs,
3939 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003940 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003941 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003942 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003943 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003944 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003945 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003946 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003947 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003948 }
3949
Roland Levillain3e3d7332015-04-28 11:00:54 +01003950 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003951 ArtMethod* const resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003952 const uint32_t dex_method_index_;
3953 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003954
3955 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3956 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003957
3958 private:
3959 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3960};
3961
Vladimir Markofcb503c2016-05-18 12:48:17 +01003962class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003963 public:
3964 HInvokeUnresolved(ArenaAllocator* arena,
3965 uint32_t number_of_arguments,
3966 Primitive::Type return_type,
3967 uint32_t dex_pc,
3968 uint32_t dex_method_index,
3969 InvokeType invoke_type)
3970 : HInvoke(arena,
3971 number_of_arguments,
3972 0u /* number_of_other_inputs */,
3973 return_type,
3974 dex_pc,
3975 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003976 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003977 invoke_type) {
3978 }
3979
3980 DECLARE_INSTRUCTION(InvokeUnresolved);
3981
3982 private:
3983 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3984};
3985
Vladimir Markofcb503c2016-05-18 12:48:17 +01003986class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003987 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003988 // Requirements of this method call regarding the class
3989 // initialization (clinit) check of its declaring class.
3990 enum class ClinitCheckRequirement {
3991 kNone, // Class already initialized.
3992 kExplicit, // Static call having explicit clinit check as last input.
3993 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003994 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003995 };
3996
Vladimir Marko58155012015-08-19 12:49:41 +00003997 // Determines how to load the target ArtMethod*.
3998 enum class MethodLoadKind {
3999 // Use a String init ArtMethod* loaded from Thread entrypoints.
4000 kStringInit,
4001
4002 // Use the method's own ArtMethod* loaded by the register allocator.
4003 kRecursive,
4004
4005 // Use ArtMethod* at a known address, embed the direct address in the code.
4006 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4007 kDirectAddress,
4008
Vladimir Markoa1de9182016-02-25 11:37:38 +00004009 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004010 // Used when we need to use the dex cache, for example for invoke-static that
4011 // may cause class initialization (the entry may point to a resolution method),
4012 // and we know that we can access the dex cache arrays using a PC-relative load.
4013 kDexCachePcRelative,
4014
4015 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4016 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4017 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4018 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4019 kDexCacheViaMethod,
4020 };
4021
4022 // Determines the location of the code pointer.
4023 enum class CodePtrLocation {
4024 // Recursive call, use local PC-relative call instruction.
4025 kCallSelf,
4026
Vladimir Marko58155012015-08-19 12:49:41 +00004027 // Use code pointer from the ArtMethod*.
4028 // Used when we don't know the target code. This is also the last-resort-kind used when
4029 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4030 kCallArtMethod,
4031 };
4032
4033 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004034 MethodLoadKind method_load_kind;
4035 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004036 // The method load data holds
4037 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4038 // Note that there are multiple string init methods, each having its own offset.
4039 // - the method address for kDirectAddress
4040 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004041 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004042 };
4043
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004044 HInvokeStaticOrDirect(ArenaAllocator* arena,
4045 uint32_t number_of_arguments,
4046 Primitive::Type return_type,
4047 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004048 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004049 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004050 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004051 InvokeType invoke_type,
4052 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004053 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004054 : HInvoke(arena,
4055 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004056 // There is potentially one extra argument for the HCurrentMethod node, and
4057 // potentially one other if the clinit check is explicit, and potentially
4058 // one other if the method is a string factory.
4059 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004060 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004061 return_type,
4062 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004063 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004064 resolved_method,
4065 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004066 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004067 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004068 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4069 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004070
Vladimir Markodc151b22015-10-15 18:02:30 +01004071 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004072 bool had_current_method_input = HasCurrentMethodInput();
4073 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4074
4075 // Using the current method is the default and once we find a better
4076 // method load kind, we should not go back to using the current method.
4077 DCHECK(had_current_method_input || !needs_current_method_input);
4078
4079 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004080 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4081 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004082 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004083 dispatch_info_ = dispatch_info;
4084 }
4085
Vladimir Markoc53c0792015-11-19 15:48:33 +00004086 void AddSpecialInput(HInstruction* input) {
4087 // We allow only one special input.
4088 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4089 DCHECK(InputCount() == GetSpecialInputIndex() ||
4090 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4091 InsertInputAt(GetSpecialInputIndex(), input);
4092 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004093
Vladimir Marko372f10e2016-05-17 16:30:10 +01004094 using HInstruction::GetInputRecords; // Keep the const version visible.
4095 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4096 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4097 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4098 DCHECK(!input_records.empty());
4099 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4100 HInstruction* last_input = input_records.back().GetInstruction();
4101 // Note: `last_input` may be null during arguments setup.
4102 if (last_input != nullptr) {
4103 // `last_input` is the last input of a static invoke marked as having
4104 // an explicit clinit check. It must either be:
4105 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4106 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4107 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4108 }
4109 }
4110 return input_records;
4111 }
4112
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004113 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004114 // We access the method via the dex cache so we can't do an implicit null check.
4115 // TODO: for intrinsics we can generate implicit null checks.
4116 return false;
4117 }
4118
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004119 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004120 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004121 }
4122
Vladimir Markoc53c0792015-11-19 15:48:33 +00004123 // Get the index of the special input, if any.
4124 //
David Brazdil6de19382016-01-08 17:37:10 +00004125 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4126 // method pointer; otherwise there may be one platform-specific special input,
4127 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004128 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004129 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004130
Vladimir Marko58155012015-08-19 12:49:41 +00004131 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4132 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4133 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004134 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004135 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004136 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004137 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004138 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4139 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004140 bool HasCurrentMethodInput() const {
4141 // This function can be called only after the invoke has been fully initialized by the builder.
4142 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004143 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004144 return true;
4145 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004146 DCHECK(InputCount() == GetSpecialInputIndex() ||
4147 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004148 return false;
4149 }
4150 }
Vladimir Marko58155012015-08-19 12:49:41 +00004151
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004152 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004153 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004154 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004155 }
4156
4157 uint64_t GetMethodAddress() const {
4158 DCHECK(HasMethodAddress());
4159 return dispatch_info_.method_load_data;
4160 }
4161
4162 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004163 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004164 return dispatch_info_.method_load_data;
4165 }
4166
Vladimir Markoa1de9182016-02-25 11:37:38 +00004167 ClinitCheckRequirement GetClinitCheckRequirement() const {
4168 return GetPackedField<ClinitCheckRequirementField>();
4169 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004170
Roland Levillain4c0eb422015-04-24 16:43:49 +01004171 // Is this instruction a call to a static method?
4172 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004173 return GetInvokeType() == kStatic;
4174 }
4175
4176 MethodReference GetTargetMethod() const {
4177 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004178 }
4179
Vladimir Markofbb184a2015-11-13 14:47:00 +00004180 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4181 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4182 // instruction; only relevant for static calls with explicit clinit check.
4183 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004184 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004185 size_t last_input_index = inputs_.size() - 1u;
4186 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004187 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004188 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004189 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004190 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004191 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004192 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004193 }
4194
4195 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004196 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004197 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004198 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004199 }
4200
4201 // Is this a call to a static method whose declaring class has an
4202 // implicit intialization check requirement?
4203 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004204 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004205 }
4206
Vladimir Markob554b5a2015-11-06 12:57:55 +00004207 // Does this method load kind need the current method as an input?
4208 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4209 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4210 }
4211
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004212 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004213
4214 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004215 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004216 static constexpr size_t kFieldClinitCheckRequirementSize =
4217 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4218 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4219 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4220 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4221 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004222 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4223 kFieldClinitCheckRequirement,
4224 kFieldClinitCheckRequirementSize>;
4225
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004226 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004227 MethodReference target_method_;
4228 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004229
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004230 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004231};
Vladimir Markof64242a2015-12-01 14:58:23 +00004232std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004233std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004234
Vladimir Markofcb503c2016-05-18 12:48:17 +01004235class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004236 public:
4237 HInvokeVirtual(ArenaAllocator* arena,
4238 uint32_t number_of_arguments,
4239 Primitive::Type return_type,
4240 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004241 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004242 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004243 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004244 : HInvoke(arena,
4245 number_of_arguments,
4246 0u,
4247 return_type,
4248 dex_pc,
4249 dex_method_index,
4250 resolved_method,
4251 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004252 vtable_index_(vtable_index) {}
4253
Aart Bik71bf7b42016-11-16 10:17:46 -08004254 bool CanBeNull() const OVERRIDE {
4255 switch (GetIntrinsic()) {
4256 case Intrinsics::kThreadCurrentThread:
4257 case Intrinsics::kStringBufferAppend:
4258 case Intrinsics::kStringBufferToString:
4259 case Intrinsics::kStringBuilderAppend:
4260 case Intrinsics::kStringBuilderToString:
4261 return false;
4262 default:
4263 return HInvoke::CanBeNull();
4264 }
4265 }
4266
Calin Juravle641547a2015-04-21 22:08:51 +01004267 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004268 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004269 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004270 }
4271
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004272 uint32_t GetVTableIndex() const { return vtable_index_; }
4273
4274 DECLARE_INSTRUCTION(InvokeVirtual);
4275
4276 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004277 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004278 const uint32_t vtable_index_;
4279
4280 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4281};
4282
Vladimir Markofcb503c2016-05-18 12:48:17 +01004283class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004284 public:
4285 HInvokeInterface(ArenaAllocator* arena,
4286 uint32_t number_of_arguments,
4287 Primitive::Type return_type,
4288 uint32_t dex_pc,
4289 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004290 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004291 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004292 : HInvoke(arena,
4293 number_of_arguments,
4294 0u,
4295 return_type,
4296 dex_pc,
4297 dex_method_index,
4298 resolved_method,
4299 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004300 imt_index_(imt_index) {}
4301
Calin Juravle641547a2015-04-21 22:08:51 +01004302 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004303 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004304 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004305 }
4306
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004307 uint32_t GetImtIndex() const { return imt_index_; }
4308 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4309
4310 DECLARE_INSTRUCTION(InvokeInterface);
4311
4312 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004313 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004314 const uint32_t imt_index_;
4315
4316 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4317};
4318
Vladimir Markofcb503c2016-05-18 12:48:17 +01004319class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004320 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004321 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004322 : HUnaryOperation(result_type, input, dex_pc) {
4323 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4324 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004325
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004326 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004327
4328 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004329 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004330 }
4331 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004332 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004333 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004334 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4335 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4336 }
4337 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4338 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4339 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004340
Roland Levillain88cb1752014-10-20 16:36:47 +01004341 DECLARE_INSTRUCTION(Neg);
4342
4343 private:
4344 DISALLOW_COPY_AND_ASSIGN(HNeg);
4345};
4346
Vladimir Markofcb503c2016-05-18 12:48:17 +01004347class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004348 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004349 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004350 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004351 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004352 dex::TypeIndex type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004353 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004354 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004355 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004356 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004357 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004358 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004359 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004360 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004361 }
4362
Andreas Gampea5b09a62016-11-17 15:21:22 -08004363 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004364 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004365
4366 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004367 bool NeedsEnvironment() const OVERRIDE { return true; }
4368
Mingyao Yang0c365e62015-03-31 15:09:29 -07004369 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4370 bool CanThrow() const OVERRIDE { return true; }
4371
Calin Juravle10e244f2015-01-26 18:54:32 +00004372 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004373
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004374 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4375
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004376 DECLARE_INSTRUCTION(NewArray);
4377
4378 private:
Andreas Gampea5b09a62016-11-17 15:21:22 -08004379 const dex::TypeIndex type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004380 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004381 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004382
4383 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4384};
4385
Vladimir Markofcb503c2016-05-18 12:48:17 +01004386class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004387 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004388 HAdd(Primitive::Type result_type,
4389 HInstruction* left,
4390 HInstruction* right,
4391 uint32_t dex_pc = kNoDexPc)
4392 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004393
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004394 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004395
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004396 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004397
4398 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004399 return GetBlock()->GetGraph()->GetIntConstant(
4400 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004401 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004402 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004403 return GetBlock()->GetGraph()->GetLongConstant(
4404 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004405 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004406 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4407 return GetBlock()->GetGraph()->GetFloatConstant(
4408 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4409 }
4410 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4411 return GetBlock()->GetGraph()->GetDoubleConstant(
4412 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4413 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004414
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004415 DECLARE_INSTRUCTION(Add);
4416
4417 private:
4418 DISALLOW_COPY_AND_ASSIGN(HAdd);
4419};
4420
Vladimir Markofcb503c2016-05-18 12:48:17 +01004421class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004422 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004423 HSub(Primitive::Type result_type,
4424 HInstruction* left,
4425 HInstruction* right,
4426 uint32_t dex_pc = kNoDexPc)
4427 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004428
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004429 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004430
4431 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004432 return GetBlock()->GetGraph()->GetIntConstant(
4433 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004434 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004435 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004436 return GetBlock()->GetGraph()->GetLongConstant(
4437 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004438 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004439 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4440 return GetBlock()->GetGraph()->GetFloatConstant(
4441 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4442 }
4443 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4444 return GetBlock()->GetGraph()->GetDoubleConstant(
4445 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4446 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004447
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004448 DECLARE_INSTRUCTION(Sub);
4449
4450 private:
4451 DISALLOW_COPY_AND_ASSIGN(HSub);
4452};
4453
Vladimir Markofcb503c2016-05-18 12:48:17 +01004454class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004455 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004456 HMul(Primitive::Type result_type,
4457 HInstruction* left,
4458 HInstruction* right,
4459 uint32_t dex_pc = kNoDexPc)
4460 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004461
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004462 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004463
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004464 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004465
4466 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004467 return GetBlock()->GetGraph()->GetIntConstant(
4468 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004469 }
4470 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004471 return GetBlock()->GetGraph()->GetLongConstant(
4472 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004473 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004474 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4475 return GetBlock()->GetGraph()->GetFloatConstant(
4476 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4477 }
4478 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4479 return GetBlock()->GetGraph()->GetDoubleConstant(
4480 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4481 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004482
4483 DECLARE_INSTRUCTION(Mul);
4484
4485 private:
4486 DISALLOW_COPY_AND_ASSIGN(HMul);
4487};
4488
Vladimir Markofcb503c2016-05-18 12:48:17 +01004489class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004490 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004491 HDiv(Primitive::Type result_type,
4492 HInstruction* left,
4493 HInstruction* right,
4494 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004495 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004496
Roland Levillain9867bc72015-08-05 10:21:34 +01004497 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004498 T ComputeIntegral(T x, T y) const {
4499 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004500 // Our graph structure ensures we never have 0 for `y` during
4501 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004502 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004503 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004504 return (y == -1) ? -x : x / y;
4505 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004506
Roland Levillain31dd3d62016-02-16 12:21:02 +00004507 template <typename T>
4508 T ComputeFP(T x, T y) const {
4509 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4510 return x / y;
4511 }
4512
Roland Levillain9867bc72015-08-05 10:21:34 +01004513 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004514 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004515 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004516 }
4517 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004518 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004519 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4520 }
4521 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4522 return GetBlock()->GetGraph()->GetFloatConstant(
4523 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4524 }
4525 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4526 return GetBlock()->GetGraph()->GetDoubleConstant(
4527 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004528 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004529
4530 DECLARE_INSTRUCTION(Div);
4531
4532 private:
4533 DISALLOW_COPY_AND_ASSIGN(HDiv);
4534};
4535
Vladimir Markofcb503c2016-05-18 12:48:17 +01004536class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004537 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004538 HRem(Primitive::Type result_type,
4539 HInstruction* left,
4540 HInstruction* right,
4541 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004542 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004543
Roland Levillain9867bc72015-08-05 10:21:34 +01004544 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004545 T ComputeIntegral(T x, T y) const {
4546 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004547 // Our graph structure ensures we never have 0 for `y` during
4548 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004549 DCHECK_NE(y, 0);
4550 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4551 return (y == -1) ? 0 : x % y;
4552 }
4553
Roland Levillain31dd3d62016-02-16 12:21:02 +00004554 template <typename T>
4555 T ComputeFP(T x, T y) const {
4556 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4557 return std::fmod(x, y);
4558 }
4559
Roland Levillain9867bc72015-08-05 10:21:34 +01004560 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004561 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004562 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004563 }
4564 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004565 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004566 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004567 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004568 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4569 return GetBlock()->GetGraph()->GetFloatConstant(
4570 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4571 }
4572 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4573 return GetBlock()->GetGraph()->GetDoubleConstant(
4574 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4575 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004576
4577 DECLARE_INSTRUCTION(Rem);
4578
4579 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004580 DISALLOW_COPY_AND_ASSIGN(HRem);
4581};
4582
Vladimir Markofcb503c2016-05-18 12:48:17 +01004583class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004584 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004585 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4586 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004587 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004588 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004589 SetRawInputAt(0, value);
4590 }
4591
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004592 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4593
Calin Juravled0d48522014-11-04 16:40:20 +00004594 bool CanBeMoved() const OVERRIDE { return true; }
4595
Vladimir Marko372f10e2016-05-17 16:30:10 +01004596 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004597 return true;
4598 }
4599
4600 bool NeedsEnvironment() const OVERRIDE { return true; }
4601 bool CanThrow() const OVERRIDE { return true; }
4602
Calin Juravled0d48522014-11-04 16:40:20 +00004603 DECLARE_INSTRUCTION(DivZeroCheck);
4604
4605 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004606 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4607};
4608
Vladimir Markofcb503c2016-05-18 12:48:17 +01004609class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004610 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004611 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004612 HInstruction* value,
4613 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004614 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004615 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4616 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4617 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004618 }
4619
Roland Levillain5b5b9312016-03-22 14:57:31 +00004620 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004621 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004622 return value << (distance & max_shift_distance);
4623 }
4624
4625 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004626 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004627 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004628 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004629 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004630 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004631 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004632 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004633 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4634 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4635 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4636 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004637 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004638 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4639 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004640 LOG(FATAL) << DebugName() << " is not defined for float values";
4641 UNREACHABLE();
4642 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004643 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4644 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004645 LOG(FATAL) << DebugName() << " is not defined for double values";
4646 UNREACHABLE();
4647 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004648
4649 DECLARE_INSTRUCTION(Shl);
4650
4651 private:
4652 DISALLOW_COPY_AND_ASSIGN(HShl);
4653};
4654
Vladimir Markofcb503c2016-05-18 12:48:17 +01004655class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004656 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004657 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004658 HInstruction* value,
4659 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004660 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004661 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4662 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4663 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004664 }
4665
Roland Levillain5b5b9312016-03-22 14:57:31 +00004666 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004667 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004668 return value >> (distance & max_shift_distance);
4669 }
4670
4671 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004672 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004673 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004674 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004675 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004676 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004677 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004678 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004679 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4680 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4681 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4682 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004683 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004684 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4685 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004686 LOG(FATAL) << DebugName() << " is not defined for float values";
4687 UNREACHABLE();
4688 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004689 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4690 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004691 LOG(FATAL) << DebugName() << " is not defined for double values";
4692 UNREACHABLE();
4693 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004694
4695 DECLARE_INSTRUCTION(Shr);
4696
4697 private:
4698 DISALLOW_COPY_AND_ASSIGN(HShr);
4699};
4700
Vladimir Markofcb503c2016-05-18 12:48:17 +01004701class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004702 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004703 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004704 HInstruction* value,
4705 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004706 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004707 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4708 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4709 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004710 }
4711
Roland Levillain5b5b9312016-03-22 14:57:31 +00004712 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004713 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004714 typedef typename std::make_unsigned<T>::type V;
4715 V ux = static_cast<V>(value);
4716 return static_cast<T>(ux >> (distance & max_shift_distance));
4717 }
4718
4719 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004720 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004721 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004722 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004723 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004724 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004725 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004726 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004727 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4728 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4729 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4730 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004731 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004732 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4733 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004734 LOG(FATAL) << DebugName() << " is not defined for float values";
4735 UNREACHABLE();
4736 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004737 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4738 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004739 LOG(FATAL) << DebugName() << " is not defined for double values";
4740 UNREACHABLE();
4741 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004742
4743 DECLARE_INSTRUCTION(UShr);
4744
4745 private:
4746 DISALLOW_COPY_AND_ASSIGN(HUShr);
4747};
4748
Vladimir Markofcb503c2016-05-18 12:48:17 +01004749class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004750 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004751 HAnd(Primitive::Type result_type,
4752 HInstruction* left,
4753 HInstruction* right,
4754 uint32_t dex_pc = kNoDexPc)
4755 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004756
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004757 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004758
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004759 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004760
4761 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004762 return GetBlock()->GetGraph()->GetIntConstant(
4763 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004764 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004765 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004766 return GetBlock()->GetGraph()->GetLongConstant(
4767 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004768 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004769 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4770 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4771 LOG(FATAL) << DebugName() << " is not defined for float values";
4772 UNREACHABLE();
4773 }
4774 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4775 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4776 LOG(FATAL) << DebugName() << " is not defined for double values";
4777 UNREACHABLE();
4778 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004779
4780 DECLARE_INSTRUCTION(And);
4781
4782 private:
4783 DISALLOW_COPY_AND_ASSIGN(HAnd);
4784};
4785
Vladimir Markofcb503c2016-05-18 12:48:17 +01004786class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004787 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004788 HOr(Primitive::Type result_type,
4789 HInstruction* left,
4790 HInstruction* right,
4791 uint32_t dex_pc = kNoDexPc)
4792 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004793
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004794 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004795
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004796 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004797
4798 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004799 return GetBlock()->GetGraph()->GetIntConstant(
4800 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004801 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004802 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004803 return GetBlock()->GetGraph()->GetLongConstant(
4804 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004805 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004806 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4807 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4808 LOG(FATAL) << DebugName() << " is not defined for float values";
4809 UNREACHABLE();
4810 }
4811 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4812 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4813 LOG(FATAL) << DebugName() << " is not defined for double values";
4814 UNREACHABLE();
4815 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004816
4817 DECLARE_INSTRUCTION(Or);
4818
4819 private:
4820 DISALLOW_COPY_AND_ASSIGN(HOr);
4821};
4822
Vladimir Markofcb503c2016-05-18 12:48:17 +01004823class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004824 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004825 HXor(Primitive::Type result_type,
4826 HInstruction* left,
4827 HInstruction* right,
4828 uint32_t dex_pc = kNoDexPc)
4829 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004830
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004831 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004832
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004833 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004834
4835 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004836 return GetBlock()->GetGraph()->GetIntConstant(
4837 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004838 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004839 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004840 return GetBlock()->GetGraph()->GetLongConstant(
4841 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004842 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004843 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4844 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4845 LOG(FATAL) << DebugName() << " is not defined for float values";
4846 UNREACHABLE();
4847 }
4848 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4849 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4850 LOG(FATAL) << DebugName() << " is not defined for double values";
4851 UNREACHABLE();
4852 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004853
4854 DECLARE_INSTRUCTION(Xor);
4855
4856 private:
4857 DISALLOW_COPY_AND_ASSIGN(HXor);
4858};
4859
Vladimir Markofcb503c2016-05-18 12:48:17 +01004860class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004861 public:
4862 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004863 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004864 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4865 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004866 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004867
Roland Levillain5b5b9312016-03-22 14:57:31 +00004868 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004869 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004870 typedef typename std::make_unsigned<T>::type V;
4871 V ux = static_cast<V>(value);
4872 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004873 return static_cast<T>(ux);
4874 } else {
4875 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004876 return static_cast<T>(ux >> (distance & max_shift_value)) |
4877 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004878 }
4879 }
4880
Roland Levillain5b5b9312016-03-22 14:57:31 +00004881 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004882 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004883 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004884 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004885 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004886 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004887 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004888 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004889 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4890 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4891 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4892 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004893 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004894 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4895 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004896 LOG(FATAL) << DebugName() << " is not defined for float values";
4897 UNREACHABLE();
4898 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004899 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4900 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004901 LOG(FATAL) << DebugName() << " is not defined for double values";
4902 UNREACHABLE();
4903 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004904
4905 DECLARE_INSTRUCTION(Ror);
4906
4907 private:
4908 DISALLOW_COPY_AND_ASSIGN(HRor);
4909};
4910
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004911// The value of a parameter in this method. Its location depends on
4912// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004913class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004914 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004915 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004916 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004917 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004918 Primitive::Type parameter_type,
4919 bool is_this = false)
4920 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004921 dex_file_(dex_file),
4922 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004923 index_(index) {
4924 SetPackedFlag<kFlagIsThis>(is_this);
4925 SetPackedFlag<kFlagCanBeNull>(!is_this);
4926 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004927
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004928 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08004929 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004930 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004931 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004932
Vladimir Markoa1de9182016-02-25 11:37:38 +00004933 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4934 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004935
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004936 DECLARE_INSTRUCTION(ParameterValue);
4937
4938 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004939 // Whether or not the parameter value corresponds to 'this' argument.
4940 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4941 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4942 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4943 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4944 "Too many packed fields.");
4945
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004946 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004947 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004948 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004949 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004950 const uint8_t index_;
4951
4952 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4953};
4954
Vladimir Markofcb503c2016-05-18 12:48:17 +01004955class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004956 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004957 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4958 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004959
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004960 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004961 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004962 return true;
4963 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004964
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004965 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004966
4967 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004968 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004969 }
4970 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004971 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004972 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004973 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4974 LOG(FATAL) << DebugName() << " is not defined for float values";
4975 UNREACHABLE();
4976 }
4977 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4978 LOG(FATAL) << DebugName() << " is not defined for double values";
4979 UNREACHABLE();
4980 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004981
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004982 DECLARE_INSTRUCTION(Not);
4983
4984 private:
4985 DISALLOW_COPY_AND_ASSIGN(HNot);
4986};
4987
Vladimir Markofcb503c2016-05-18 12:48:17 +01004988class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004989 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004990 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4991 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004992
4993 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004994 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004995 return true;
4996 }
4997
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004998 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004999 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005000 return !x;
5001 }
5002
Roland Levillain9867bc72015-08-05 10:21:34 +01005003 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005004 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005005 }
5006 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5007 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005008 UNREACHABLE();
5009 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005010 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5011 LOG(FATAL) << DebugName() << " is not defined for float values";
5012 UNREACHABLE();
5013 }
5014 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5015 LOG(FATAL) << DebugName() << " is not defined for double values";
5016 UNREACHABLE();
5017 }
David Brazdil66d126e2015-04-03 16:02:44 +01005018
5019 DECLARE_INSTRUCTION(BooleanNot);
5020
5021 private:
5022 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5023};
5024
Vladimir Markofcb503c2016-05-18 12:48:17 +01005025class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005026 public:
5027 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005028 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005029 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005030 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005031 // Invariant: We should never generate a conversion to a Boolean value.
5032 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005033 }
5034
5035 HInstruction* GetInput() const { return InputAt(0); }
5036 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5037 Primitive::Type GetResultType() const { return GetType(); }
5038
5039 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005040 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5041 return true;
5042 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005043
Mark Mendelle82549b2015-05-06 10:55:34 -04005044 // Try to statically evaluate the conversion and return a HConstant
5045 // containing the result. If the input cannot be converted, return nullptr.
5046 HConstant* TryStaticEvaluation() const;
5047
Roland Levillaindff1f282014-11-05 14:15:05 +00005048 DECLARE_INSTRUCTION(TypeConversion);
5049
5050 private:
5051 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5052};
5053
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005054static constexpr uint32_t kNoRegNumber = -1;
5055
Vladimir Markofcb503c2016-05-18 12:48:17 +01005056class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005057 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005058 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5059 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005060 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005061 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005062 SetRawInputAt(0, value);
5063 }
5064
Calin Juravle10e244f2015-01-26 18:54:32 +00005065 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005066 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005067 return true;
5068 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005069
Calin Juravle10e244f2015-01-26 18:54:32 +00005070 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005071
Calin Juravle10e244f2015-01-26 18:54:32 +00005072 bool CanThrow() const OVERRIDE { return true; }
5073
5074 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005075
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005076
5077 DECLARE_INSTRUCTION(NullCheck);
5078
5079 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005080 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5081};
5082
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005083// Embeds an ArtField and all the information required by the compiler. We cache
5084// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005085class FieldInfo : public ValueObject {
5086 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005087 FieldInfo(ArtField* field,
5088 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005089 Primitive::Type field_type,
5090 bool is_volatile,
5091 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005092 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005093 const DexFile& dex_file)
5094 : field_(field),
5095 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005096 field_type_(field_type),
5097 is_volatile_(is_volatile),
5098 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005099 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005100 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005101
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005102 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005103 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005104 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005105 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005106 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005107 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005108 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005109
5110 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005111 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005112 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005113 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005114 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005115 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005116 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005117 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005118};
5119
Vladimir Markofcb503c2016-05-18 12:48:17 +01005120class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005121 public:
5122 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005123 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005124 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005125 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005126 bool is_volatile,
5127 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005128 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005129 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005130 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005131 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005132 field_info_(field,
5133 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005134 field_type,
5135 is_volatile,
5136 field_idx,
5137 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005138 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005139 SetRawInputAt(0, value);
5140 }
5141
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005142 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005143
Vladimir Marko372f10e2016-05-17 16:30:10 +01005144 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5145 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005146 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005147 }
5148
Calin Juravle641547a2015-04-21 22:08:51 +01005149 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005150 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005151 }
5152
5153 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005154 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5155 }
5156
Calin Juravle52c48962014-12-16 17:02:57 +00005157 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005158 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005159 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005160 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005161
5162 DECLARE_INSTRUCTION(InstanceFieldGet);
5163
5164 private:
5165 const FieldInfo field_info_;
5166
5167 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5168};
5169
Vladimir Markofcb503c2016-05-18 12:48:17 +01005170class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005171 public:
5172 HInstanceFieldSet(HInstruction* object,
5173 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005174 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005175 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005176 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005177 bool is_volatile,
5178 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005179 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005180 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005181 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005182 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005183 field_info_(field,
5184 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005185 field_type,
5186 is_volatile,
5187 field_idx,
5188 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005189 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005190 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005191 SetRawInputAt(0, object);
5192 SetRawInputAt(1, value);
5193 }
5194
Calin Juravle641547a2015-04-21 22:08:51 +01005195 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005196 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005197 }
5198
Calin Juravle52c48962014-12-16 17:02:57 +00005199 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005200 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005201 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005202 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005203 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005204 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5205 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005206
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005207 DECLARE_INSTRUCTION(InstanceFieldSet);
5208
5209 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005210 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5211 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5212 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5213 "Too many packed fields.");
5214
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005215 const FieldInfo field_info_;
5216
5217 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5218};
5219
Vladimir Markofcb503c2016-05-18 12:48:17 +01005220class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005221 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005222 HArrayGet(HInstruction* array,
5223 HInstruction* index,
5224 Primitive::Type type,
5225 uint32_t dex_pc,
5226 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005227 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005228 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005229 SetRawInputAt(0, array);
5230 SetRawInputAt(1, index);
5231 }
5232
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005233 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005234 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005235 return true;
5236 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005237 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005238 // TODO: We can be smarter here.
5239 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5240 // which generates the implicit null check. There are cases when these can be removed
5241 // to produce better code. If we ever add optimizations to do so we should allow an
5242 // implicit check here (as long as the address falls in the first page).
5243 return false;
5244 }
5245
David Brazdil4833f5a2015-12-16 10:37:39 +00005246 bool IsEquivalentOf(HArrayGet* other) const {
5247 bool result = (GetDexPc() == other->GetDexPc());
5248 if (kIsDebugBuild && result) {
5249 DCHECK_EQ(GetBlock(), other->GetBlock());
5250 DCHECK_EQ(GetArray(), other->GetArray());
5251 DCHECK_EQ(GetIndex(), other->GetIndex());
5252 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005253 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005254 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005255 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5256 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005257 }
5258 }
5259 return result;
5260 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005261
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005262 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5263
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005264 HInstruction* GetArray() const { return InputAt(0); }
5265 HInstruction* GetIndex() const { return InputAt(1); }
5266
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005267 DECLARE_INSTRUCTION(ArrayGet);
5268
5269 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005270 // We treat a String as an array, creating the HArrayGet from String.charAt()
5271 // intrinsic in the instruction simplifier. We can always determine whether
5272 // a particular HArrayGet is actually a String.charAt() by looking at the type
5273 // of the input but that requires holding the mutator lock, so we prefer to use
5274 // a flag, so that code generators don't need to do the locking.
5275 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5276 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5277 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5278 "Too many packed fields.");
5279
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005280 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5281};
5282
Vladimir Markofcb503c2016-05-18 12:48:17 +01005283class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005284 public:
5285 HArraySet(HInstruction* array,
5286 HInstruction* index,
5287 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005288 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005289 uint32_t dex_pc)
5290 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005291 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5292 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5293 SetPackedFlag<kFlagValueCanBeNull>(true);
5294 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005295 SetRawInputAt(0, array);
5296 SetRawInputAt(1, index);
5297 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005298 // Make a best guess now, may be refined during SSA building.
5299 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005300 }
5301
Calin Juravle77520bc2015-01-12 18:45:46 +00005302 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005303 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005304 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005305 }
5306
Mingyao Yang81014cb2015-06-02 03:16:27 -07005307 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005308 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005309
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005310 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005311 // TODO: Same as for ArrayGet.
5312 return false;
5313 }
5314
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005315 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005316 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005317 }
5318
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005319 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005320 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005321 }
5322
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005323 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005324 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005325 }
5326
Vladimir Markoa1de9182016-02-25 11:37:38 +00005327 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5328 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5329 bool StaticTypeOfArrayIsObjectArray() const {
5330 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5331 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005332
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005333 HInstruction* GetArray() const { return InputAt(0); }
5334 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005335 HInstruction* GetValue() const { return InputAt(2); }
5336
5337 Primitive::Type GetComponentType() const {
5338 // The Dex format does not type floating point index operations. Since the
5339 // `expected_component_type_` is set during building and can therefore not
5340 // be correct, we also check what is the value type. If it is a floating
5341 // point type, we must use that type.
5342 Primitive::Type value_type = GetValue()->GetType();
5343 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5344 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005345 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005346 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005347
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005348 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005349 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005350 }
5351
Aart Bik18b36ab2016-04-13 16:41:35 -07005352 void ComputeSideEffects() {
5353 Primitive::Type type = GetComponentType();
5354 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5355 SideEffectsForArchRuntimeCalls(type)));
5356 }
5357
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005358 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5359 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5360 }
5361
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005362 DECLARE_INSTRUCTION(ArraySet);
5363
5364 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005365 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5366 static constexpr size_t kFieldExpectedComponentTypeSize =
5367 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5368 static constexpr size_t kFlagNeedsTypeCheck =
5369 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5370 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005371 // Cached information for the reference_type_info_ so that codegen
5372 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005373 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5374 static constexpr size_t kNumberOfArraySetPackedBits =
5375 kFlagStaticTypeOfArrayIsObjectArray + 1;
5376 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5377 using ExpectedComponentTypeField =
5378 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005379
5380 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5381};
5382
Vladimir Markofcb503c2016-05-18 12:48:17 +01005383class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005384 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005385 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005386 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005387 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005388 // Note that arrays do not change length, so the instruction does not
5389 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005390 SetRawInputAt(0, array);
5391 }
5392
Calin Juravle77520bc2015-01-12 18:45:46 +00005393 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005394 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005395 return true;
5396 }
Calin Juravle641547a2015-04-21 22:08:51 +01005397 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5398 return obj == InputAt(0);
5399 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005400
Vladimir Markodce016e2016-04-28 13:10:02 +01005401 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5402
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005403 DECLARE_INSTRUCTION(ArrayLength);
5404
5405 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005406 // We treat a String as an array, creating the HArrayLength from String.length()
5407 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5408 // determine whether a particular HArrayLength is actually a String.length() by
5409 // looking at the type of the input but that requires holding the mutator lock, so
5410 // we prefer to use a flag, so that code generators don't need to do the locking.
5411 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5412 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5413 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5414 "Too many packed fields.");
5415
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005416 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5417};
5418
Vladimir Markofcb503c2016-05-18 12:48:17 +01005419class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005420 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005421 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5422 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005423 HBoundsCheck(HInstruction* index,
5424 HInstruction* length,
5425 uint32_t dex_pc,
5426 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5427 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5428 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005429 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005430 SetRawInputAt(0, index);
5431 SetRawInputAt(1, length);
5432 }
5433
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005434 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005435 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005436 return true;
5437 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005438
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005439 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005440
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005441 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005442
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005443 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5444 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5445
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005446 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005447
5448 DECLARE_INSTRUCTION(BoundsCheck);
5449
5450 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005451 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5452 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5453 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5454 // so we need to create an HEnvironment which will be translated to an InlineInfo
5455 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5456 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5457 // from the invoke as we don't want to look again at the dex bytecode.
5458 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5459
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005460 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5461};
5462
Vladimir Markofcb503c2016-05-18 12:48:17 +01005463class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005464 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005465 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005466 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005467
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005468 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005469 return true;
5470 }
5471
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005472 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5473 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005474
5475 DECLARE_INSTRUCTION(SuspendCheck);
5476
5477 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005478 // Only used for code generation, in order to share the same slow path between back edges
5479 // of a same loop.
5480 SlowPathCode* slow_path_;
5481
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005482 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5483};
5484
David Srbecky0cf44932015-12-09 14:09:59 +00005485// Pseudo-instruction which provides the native debugger with mapping information.
5486// It ensures that we can generate line number and local variables at this point.
5487class HNativeDebugInfo : public HTemplateInstruction<0> {
5488 public:
5489 explicit HNativeDebugInfo(uint32_t dex_pc)
5490 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5491
5492 bool NeedsEnvironment() const OVERRIDE {
5493 return true;
5494 }
5495
5496 DECLARE_INSTRUCTION(NativeDebugInfo);
5497
5498 private:
5499 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5500};
5501
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005502/**
5503 * Instruction to load a Class object.
5504 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005505class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005506 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005507 // Determines how to load the Class.
5508 enum class LoadKind {
5509 // Use the Class* from the method's own ArtMethod*.
5510 kReferrersClass,
5511
5512 // Use boot image Class* address that will be known at link time.
5513 // Used for boot image classes referenced by boot image code in non-PIC mode.
5514 kBootImageLinkTimeAddress,
5515
5516 // Use PC-relative boot image Class* address that will be known at link time.
5517 // Used for boot image classes referenced by boot image code in PIC mode.
5518 kBootImageLinkTimePcRelative,
5519
5520 // Use a known boot image Class* address, embedded in the code by the codegen.
5521 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5522 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5523 // GetIncludePatchInformation().
5524 kBootImageAddress,
5525
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005526 // Load from the root table associated with the JIT compiled method.
5527 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005528
5529 // Load from resolved types array in the dex cache using a PC-relative load.
5530 // Used for classes outside boot image when we know that we can access
5531 // the dex cache arrays using a PC-relative load.
5532 kDexCachePcRelative,
5533
5534 // Load from resolved types array accessed through the class loaded from
5535 // the compiled method's own ArtMethod*. This is the default access type when
5536 // all other types are unavailable.
5537 kDexCacheViaMethod,
5538
5539 kLast = kDexCacheViaMethod
5540 };
5541
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005542 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005543 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005544 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005545 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005546 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005547 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005548 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5549 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005550 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005551 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005552 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005553 // Referrers class should not need access check. We never inline unverified
5554 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005555 DCHECK(!is_referrers_class || !needs_access_check);
5556
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005557 SetPackedField<LoadKindField>(
5558 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005559 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005560 SetPackedFlag<kFlagIsInDexCache>(false);
5561 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005562 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005563 }
5564
5565 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5566 DCHECK(HasAddress(load_kind));
5567 load_data_.address = address;
5568 SetLoadKindInternal(load_kind);
5569 }
5570
5571 void SetLoadKindWithTypeReference(LoadKind load_kind,
5572 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005573 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005574 DCHECK(HasTypeReference(load_kind));
5575 DCHECK(IsSameDexFile(dex_file_, dex_file));
5576 DCHECK_EQ(type_index_, type_index);
5577 SetLoadKindInternal(load_kind);
5578 }
5579
5580 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5581 const DexFile& dex_file,
5582 uint32_t element_index) {
5583 DCHECK(HasDexCacheReference(load_kind));
5584 DCHECK(IsSameDexFile(dex_file_, dex_file));
5585 load_data_.dex_cache_element_index = element_index;
5586 SetLoadKindInternal(load_kind);
5587 }
5588
5589 LoadKind GetLoadKind() const {
5590 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005591 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005592
5593 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005594
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005595 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005596
Andreas Gampea5b09a62016-11-17 15:21:22 -08005597 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005598
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005599 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005600
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005601 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005602 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005603 }
5604
Calin Juravle0ba218d2015-05-19 18:46:01 +01005605 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005606 // The entrypoint the code generator is going to call does not do
5607 // clinit of the class.
5608 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005609 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005610 }
5611
5612 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005613 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005614 (!IsReferrersClass() && !IsInDexCache()) ||
5615 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005616 }
5617
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005618 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005619 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005620 }
5621
Calin Juravleacf735c2015-02-12 15:25:22 +00005622 ReferenceTypeInfo GetLoadedClassRTI() {
5623 return loaded_class_rti_;
5624 }
5625
5626 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5627 // Make sure we only set exact types (the loaded class should never be merged).
5628 DCHECK(rti.IsExact());
5629 loaded_class_rti_ = rti;
5630 }
5631
Andreas Gampea5b09a62016-11-17 15:21:22 -08005632 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005633 const DexFile& GetDexFile() const { return dex_file_; }
5634
5635 uint32_t GetDexCacheElementOffset() const;
5636
5637 uint64_t GetAddress() const {
5638 DCHECK(HasAddress(GetLoadKind()));
5639 return load_data_.address;
5640 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005641
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005642 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5643 return !IsReferrersClass();
5644 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005645
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005646 static SideEffects SideEffectsForArchRuntimeCalls() {
5647 return SideEffects::CanTriggerGC();
5648 }
5649
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005650 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005651 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5652 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005653 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005654 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005655
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005656 void MarkInDexCache() {
5657 SetPackedFlag<kFlagIsInDexCache>(true);
5658 DCHECK(!NeedsEnvironment());
5659 RemoveEnvironment();
5660 SetSideEffects(SideEffects::None());
5661 }
5662
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005663 void MarkInBootImage() {
5664 SetPackedFlag<kFlagIsInBootImage>(true);
5665 }
5666
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005667 void AddSpecialInput(HInstruction* special_input);
5668
5669 using HInstruction::GetInputRecords; // Keep the const version visible.
5670 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5671 return ArrayRef<HUserRecord<HInstruction*>>(
5672 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5673 }
5674
5675 Primitive::Type GetType() const OVERRIDE {
5676 return Primitive::kPrimNot;
5677 }
5678
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005679 DECLARE_INSTRUCTION(LoadClass);
5680
5681 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005682 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005683 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005684 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005685 // Whether this instruction must generate the initialization check.
5686 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005687 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005688 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5689 static constexpr size_t kFieldLoadKindSize =
5690 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5691 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005692 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005693 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5694
5695 static bool HasTypeReference(LoadKind load_kind) {
5696 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5697 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5698 load_kind == LoadKind::kDexCacheViaMethod ||
5699 load_kind == LoadKind::kReferrersClass;
5700 }
5701
5702 static bool HasAddress(LoadKind load_kind) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005703 return load_kind == LoadKind::kBootImageAddress ||
5704 load_kind == LoadKind::kJitTableAddress;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005705 }
5706
5707 static bool HasDexCacheReference(LoadKind load_kind) {
5708 return load_kind == LoadKind::kDexCachePcRelative;
5709 }
5710
5711 void SetLoadKindInternal(LoadKind load_kind);
5712
5713 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5714 // For other load kinds it's empty or possibly some architecture-specific instruction
5715 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5716 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005717
Andreas Gampea5b09a62016-11-17 15:21:22 -08005718 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005719 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005720
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005721 union {
5722 uint32_t dex_cache_element_index; // Only for dex cache reference.
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005723 uint64_t address; // Up to 64-bit, needed for kJitTableAddress on 64-bit targets.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005724 } load_data_;
5725
Calin Juravleacf735c2015-02-12 15:25:22 +00005726 ReferenceTypeInfo loaded_class_rti_;
5727
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005728 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5729};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005730std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5731
5732// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5733inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5734 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5735 return load_data_.dex_cache_element_index;
5736}
5737
5738// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5739inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005740 // The special input is used for PC-relative loads on some architectures,
5741 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005742 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005743 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5744 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5745 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005746 DCHECK(special_input_.GetInstruction() == nullptr);
5747 special_input_ = HUserRecord<HInstruction*>(special_input);
5748 special_input->AddUseAt(this, 0);
5749}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005750
Vladimir Marko372f10e2016-05-17 16:30:10 +01005751class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005752 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005753 // Determines how to load the String.
5754 enum class LoadKind {
5755 // Use boot image String* address that will be known at link time.
5756 // Used for boot image strings referenced by boot image code in non-PIC mode.
5757 kBootImageLinkTimeAddress,
5758
5759 // Use PC-relative boot image String* address that will be known at link time.
5760 // Used for boot image strings referenced by boot image code in PIC mode.
5761 kBootImageLinkTimePcRelative,
5762
5763 // Use a known boot image String* address, embedded in the code by the codegen.
5764 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5765 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5766 // GetIncludePatchInformation().
5767 kBootImageAddress,
5768
Vladimir Markoaad75c62016-10-03 08:46:48 +00005769 // Load from an entry in the .bss section using a PC-relative load.
5770 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5771 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005772
5773 // Load from resolved strings array accessed through the class loaded from
5774 // the compiled method's own ArtMethod*. This is the default access type when
5775 // all other types are unavailable.
5776 kDexCacheViaMethod,
5777
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005778 // Load from the root table associated with the JIT compiled method.
5779 kJitTableAddress,
5780
5781 kLast = kJitTableAddress,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005782 };
5783
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005784 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005785 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005786 const DexFile& dex_file,
5787 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005788 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5789 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005790 string_index_(string_index),
5791 dex_file_(dex_file) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005792 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005793 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005794
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005795 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005796
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005797 LoadKind GetLoadKind() const {
5798 return GetPackedField<LoadKindField>();
5799 }
5800
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005801 const DexFile& GetDexFile() const {
5802 return dex_file_;
5803 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005804
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005805 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005806 return string_index_;
5807 }
5808
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005809 Handle<mirror::String> GetString() const {
5810 return string_;
5811 }
5812
5813 void SetString(Handle<mirror::String> str) {
5814 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005815 }
5816
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005817 bool CanBeMoved() const OVERRIDE { return true; }
5818
Vladimir Marko372f10e2016-05-17 16:30:10 +01005819 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005820
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005821 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005822
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005823 // Will call the runtime if we need to load the string through
5824 // the dex cache and the string is not guaranteed to be there yet.
5825 bool NeedsEnvironment() const OVERRIDE {
5826 LoadKind load_kind = GetLoadKind();
5827 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5828 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005829 load_kind == LoadKind::kBootImageAddress ||
5830 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005831 return false;
5832 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005833 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005834 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005835
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005836 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5837 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5838 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005839
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005840 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005841 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005842
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005843 static SideEffects SideEffectsForArchRuntimeCalls() {
5844 return SideEffects::CanTriggerGC();
5845 }
5846
Vladimir Marko372f10e2016-05-17 16:30:10 +01005847 void AddSpecialInput(HInstruction* special_input);
5848
5849 using HInstruction::GetInputRecords; // Keep the const version visible.
5850 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5851 return ArrayRef<HUserRecord<HInstruction*>>(
5852 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005853 }
5854
Vladimir Marko372f10e2016-05-17 16:30:10 +01005855 Primitive::Type GetType() const OVERRIDE {
5856 return Primitive::kPrimNot;
5857 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005858
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005859 DECLARE_INSTRUCTION(LoadString);
5860
5861 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005862 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005863 static constexpr size_t kFieldLoadKindSize =
5864 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5865 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005866 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005867 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005868
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005869 void SetLoadKindInternal(LoadKind load_kind);
5870
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005871 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5872 // For other load kinds it's empty or possibly some architecture-specific instruction
5873 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005874 HUserRecord<HInstruction*> special_input_;
5875
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005876 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005877 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005878
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005879 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005880
5881 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5882};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005883std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5884
5885// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005886inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005887 // The special input is used for PC-relative loads on some architectures,
5888 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005889 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005890 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005891 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5892 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005893 // HLoadString::GetInputRecords() returns an empty array at this point,
5894 // so use the GetInputRecords() from the base class to set the input record.
5895 DCHECK(special_input_.GetInstruction() == nullptr);
5896 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005897 special_input->AddUseAt(this, 0);
5898}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005899
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005900/**
5901 * Performs an initialization check on its Class object input.
5902 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005903class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005904 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005905 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005906 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005907 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005908 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5909 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005910 SetRawInputAt(0, constant);
5911 }
5912
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005913 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005914 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005915 return true;
5916 }
5917
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005918 bool NeedsEnvironment() const OVERRIDE {
5919 // May call runtime to initialize the class.
5920 return true;
5921 }
5922
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005923 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005924
5925 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5926
5927 DECLARE_INSTRUCTION(ClinitCheck);
5928
5929 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005930 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5931};
5932
Vladimir Markofcb503c2016-05-18 12:48:17 +01005933class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005934 public:
5935 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005936 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005937 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005938 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005939 bool is_volatile,
5940 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005941 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005942 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005943 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005944 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005945 field_info_(field,
5946 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005947 field_type,
5948 is_volatile,
5949 field_idx,
5950 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005951 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005952 SetRawInputAt(0, cls);
5953 }
5954
Calin Juravle52c48962014-12-16 17:02:57 +00005955
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005956 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005957
Vladimir Marko372f10e2016-05-17 16:30:10 +01005958 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5959 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005960 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005961 }
5962
5963 size_t ComputeHashCode() const OVERRIDE {
5964 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5965 }
5966
Calin Juravle52c48962014-12-16 17:02:57 +00005967 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005968 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5969 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005970 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005971
5972 DECLARE_INSTRUCTION(StaticFieldGet);
5973
5974 private:
5975 const FieldInfo field_info_;
5976
5977 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5978};
5979
Vladimir Markofcb503c2016-05-18 12:48:17 +01005980class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005981 public:
5982 HStaticFieldSet(HInstruction* cls,
5983 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005984 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005985 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005986 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005987 bool is_volatile,
5988 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005989 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005990 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005991 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005992 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005993 field_info_(field,
5994 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005995 field_type,
5996 is_volatile,
5997 field_idx,
5998 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005999 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006000 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006001 SetRawInputAt(0, cls);
6002 SetRawInputAt(1, value);
6003 }
6004
Calin Juravle52c48962014-12-16 17:02:57 +00006005 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006006 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6007 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006008 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006009
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006010 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006011 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6012 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006013
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006014 DECLARE_INSTRUCTION(StaticFieldSet);
6015
6016 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006017 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6018 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6019 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6020 "Too many packed fields.");
6021
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006022 const FieldInfo field_info_;
6023
6024 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6025};
6026
Vladimir Markofcb503c2016-05-18 12:48:17 +01006027class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006028 public:
6029 HUnresolvedInstanceFieldGet(HInstruction* obj,
6030 Primitive::Type field_type,
6031 uint32_t field_index,
6032 uint32_t dex_pc)
6033 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6034 field_index_(field_index) {
6035 SetRawInputAt(0, obj);
6036 }
6037
6038 bool NeedsEnvironment() const OVERRIDE { return true; }
6039 bool CanThrow() const OVERRIDE { return true; }
6040
6041 Primitive::Type GetFieldType() const { return GetType(); }
6042 uint32_t GetFieldIndex() const { return field_index_; }
6043
6044 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6045
6046 private:
6047 const uint32_t field_index_;
6048
6049 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6050};
6051
Vladimir Markofcb503c2016-05-18 12:48:17 +01006052class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006053 public:
6054 HUnresolvedInstanceFieldSet(HInstruction* obj,
6055 HInstruction* value,
6056 Primitive::Type field_type,
6057 uint32_t field_index,
6058 uint32_t dex_pc)
6059 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006060 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006061 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006062 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006063 SetRawInputAt(0, obj);
6064 SetRawInputAt(1, value);
6065 }
6066
6067 bool NeedsEnvironment() const OVERRIDE { return true; }
6068 bool CanThrow() const OVERRIDE { return true; }
6069
Vladimir Markoa1de9182016-02-25 11:37:38 +00006070 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006071 uint32_t GetFieldIndex() const { return field_index_; }
6072
6073 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6074
6075 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006076 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6077 static constexpr size_t kFieldFieldTypeSize =
6078 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6079 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6080 kFieldFieldType + kFieldFieldTypeSize;
6081 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6082 "Too many packed fields.");
6083 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6084
Calin Juravlee460d1d2015-09-29 04:52:17 +01006085 const uint32_t field_index_;
6086
6087 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6088};
6089
Vladimir Markofcb503c2016-05-18 12:48:17 +01006090class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006091 public:
6092 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6093 uint32_t field_index,
6094 uint32_t dex_pc)
6095 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6096 field_index_(field_index) {
6097 }
6098
6099 bool NeedsEnvironment() const OVERRIDE { return true; }
6100 bool CanThrow() const OVERRIDE { return true; }
6101
6102 Primitive::Type GetFieldType() const { return GetType(); }
6103 uint32_t GetFieldIndex() const { return field_index_; }
6104
6105 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6106
6107 private:
6108 const uint32_t field_index_;
6109
6110 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6111};
6112
Vladimir Markofcb503c2016-05-18 12:48:17 +01006113class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006114 public:
6115 HUnresolvedStaticFieldSet(HInstruction* value,
6116 Primitive::Type field_type,
6117 uint32_t field_index,
6118 uint32_t dex_pc)
6119 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006120 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006121 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006122 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006123 SetRawInputAt(0, value);
6124 }
6125
6126 bool NeedsEnvironment() const OVERRIDE { return true; }
6127 bool CanThrow() const OVERRIDE { return true; }
6128
Vladimir Markoa1de9182016-02-25 11:37:38 +00006129 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006130 uint32_t GetFieldIndex() const { return field_index_; }
6131
6132 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6133
6134 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006135 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6136 static constexpr size_t kFieldFieldTypeSize =
6137 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6138 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6139 kFieldFieldType + kFieldFieldTypeSize;
6140 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6141 "Too many packed fields.");
6142 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6143
Calin Juravlee460d1d2015-09-29 04:52:17 +01006144 const uint32_t field_index_;
6145
6146 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6147};
6148
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006149// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006150class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006151 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006152 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6153 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006154
David Brazdilbbd733e2015-08-18 17:48:17 +01006155 bool CanBeNull() const OVERRIDE { return false; }
6156
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006157 DECLARE_INSTRUCTION(LoadException);
6158
6159 private:
6160 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6161};
6162
David Brazdilcb1c0552015-08-04 16:22:25 +01006163// Implicit part of move-exception which clears thread-local exception storage.
6164// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006165class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006166 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006167 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6168 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006169
6170 DECLARE_INSTRUCTION(ClearException);
6171
6172 private:
6173 DISALLOW_COPY_AND_ASSIGN(HClearException);
6174};
6175
Vladimir Markofcb503c2016-05-18 12:48:17 +01006176class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006177 public:
6178 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006179 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006180 SetRawInputAt(0, exception);
6181 }
6182
6183 bool IsControlFlow() const OVERRIDE { return true; }
6184
6185 bool NeedsEnvironment() const OVERRIDE { return true; }
6186
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006187 bool CanThrow() const OVERRIDE { return true; }
6188
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006189
6190 DECLARE_INSTRUCTION(Throw);
6191
6192 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006193 DISALLOW_COPY_AND_ASSIGN(HThrow);
6194};
6195
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006196/**
6197 * Implementation strategies for the code generator of a HInstanceOf
6198 * or `HCheckCast`.
6199 */
6200enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006201 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006202 kExactCheck, // Can do a single class compare.
6203 kClassHierarchyCheck, // Can just walk the super class chain.
6204 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6205 kInterfaceCheck, // No optimization yet when checking against an interface.
6206 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006207 kArrayCheck, // No optimization yet when checking against a generic array.
6208 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006209};
6210
Roland Levillain86503782016-02-11 19:07:30 +00006211std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6212
Vladimir Markofcb503c2016-05-18 12:48:17 +01006213class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006214 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006215 HInstanceOf(HInstruction* object,
6216 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006217 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006218 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006219 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006220 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006221 dex_pc) {
6222 SetPackedField<TypeCheckKindField>(check_kind);
6223 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006224 SetRawInputAt(0, object);
6225 SetRawInputAt(1, constant);
6226 }
6227
6228 bool CanBeMoved() const OVERRIDE { return true; }
6229
Vladimir Marko372f10e2016-05-17 16:30:10 +01006230 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006231 return true;
6232 }
6233
6234 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006235 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006236 }
6237
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006238 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006239 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6240 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6241 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6242 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006243
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006244 static bool CanCallRuntime(TypeCheckKind check_kind) {
6245 // Mips currently does runtime calls for any other checks.
6246 return check_kind != TypeCheckKind::kExactCheck;
6247 }
6248
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006249 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006250 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006251 }
6252
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006253 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006254
6255 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006256 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6257 static constexpr size_t kFieldTypeCheckKindSize =
6258 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6259 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6260 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6261 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6262 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006263
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006264 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6265};
6266
Vladimir Markofcb503c2016-05-18 12:48:17 +01006267class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006268 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006269 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006270 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006271 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6272 SetPackedFlag<kFlagUpperCanBeNull>(true);
6273 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006274 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006275 SetRawInputAt(0, input);
6276 }
6277
David Brazdilf5552582015-12-27 13:36:12 +00006278 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006279 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006280 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006281 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006282
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006283 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006284 DCHECK(GetUpperCanBeNull() || !can_be_null);
6285 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006286 }
6287
Vladimir Markoa1de9182016-02-25 11:37:38 +00006288 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006289
Calin Juravleb1498f62015-02-16 13:13:29 +00006290 DECLARE_INSTRUCTION(BoundType);
6291
6292 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006293 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6294 // is false then CanBeNull() cannot be true).
6295 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6296 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6297 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6298 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6299
Calin Juravleb1498f62015-02-16 13:13:29 +00006300 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006301 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6302 // It is used to bound the type in cases like:
6303 // if (x instanceof ClassX) {
6304 // // uper_bound_ will be ClassX
6305 // }
David Brazdilf5552582015-12-27 13:36:12 +00006306 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006307
6308 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6309};
6310
Vladimir Markofcb503c2016-05-18 12:48:17 +01006311class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006312 public:
6313 HCheckCast(HInstruction* object,
6314 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006315 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006316 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006317 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6318 SetPackedField<TypeCheckKindField>(check_kind);
6319 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006320 SetRawInputAt(0, object);
6321 SetRawInputAt(1, constant);
6322 }
6323
6324 bool CanBeMoved() const OVERRIDE { return true; }
6325
Vladimir Marko372f10e2016-05-17 16:30:10 +01006326 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006327 return true;
6328 }
6329
6330 bool NeedsEnvironment() const OVERRIDE {
6331 // Instruction may throw a CheckCastError.
6332 return true;
6333 }
6334
6335 bool CanThrow() const OVERRIDE { return true; }
6336
Vladimir Markoa1de9182016-02-25 11:37:38 +00006337 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6338 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6339 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6340 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006341
6342 DECLARE_INSTRUCTION(CheckCast);
6343
6344 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006345 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6346 static constexpr size_t kFieldTypeCheckKindSize =
6347 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6348 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6349 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6350 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6351 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006352
6353 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006354};
6355
Andreas Gampe26de38b2016-07-27 17:53:11 -07006356/**
6357 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6358 * @details We define the combined barrier types that are actually required
6359 * by the Java Memory Model, rather than using exactly the terminology from
6360 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6361 * primitives. Note that the JSR-133 cookbook generally does not deal with
6362 * store atomicity issues, and the recipes there are not always entirely sufficient.
6363 * The current recipe is as follows:
6364 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6365 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6366 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6367 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6368 * class has final fields.
6369 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6370 * store-to-memory instructions. Only generated together with non-temporal stores.
6371 */
6372enum MemBarrierKind {
6373 kAnyStore,
6374 kLoadAny,
6375 kStoreStore,
6376 kAnyAny,
6377 kNTStoreStore,
6378 kLastBarrierKind = kNTStoreStore
6379};
6380std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6381
Vladimir Markofcb503c2016-05-18 12:48:17 +01006382class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006383 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006384 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006385 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006386 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6387 SetPackedField<BarrierKindField>(barrier_kind);
6388 }
Calin Juravle27df7582015-04-17 19:12:31 +01006389
Vladimir Markoa1de9182016-02-25 11:37:38 +00006390 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006391
6392 DECLARE_INSTRUCTION(MemoryBarrier);
6393
6394 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006395 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6396 static constexpr size_t kFieldBarrierKindSize =
6397 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6398 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6399 kFieldBarrierKind + kFieldBarrierKindSize;
6400 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6401 "Too many packed fields.");
6402 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006403
6404 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6405};
6406
Vladimir Markofcb503c2016-05-18 12:48:17 +01006407class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006408 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006409 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006410 kEnter,
6411 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006412 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006413 };
6414
6415 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006416 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006417 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6418 dex_pc) {
6419 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006420 SetRawInputAt(0, object);
6421 }
6422
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006423 // Instruction may go into runtime, so we need an environment.
6424 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006425
6426 bool CanThrow() const OVERRIDE {
6427 // Verifier guarantees that monitor-exit cannot throw.
6428 // This is important because it allows the HGraphBuilder to remove
6429 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6430 return IsEnter();
6431 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006432
Vladimir Markoa1de9182016-02-25 11:37:38 +00006433 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6434 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006435
6436 DECLARE_INSTRUCTION(MonitorOperation);
6437
Calin Juravle52c48962014-12-16 17:02:57 +00006438 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006439 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6440 static constexpr size_t kFieldOperationKindSize =
6441 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6442 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6443 kFieldOperationKind + kFieldOperationKindSize;
6444 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6445 "Too many packed fields.");
6446 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006447
6448 private:
6449 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6450};
6451
Vladimir Markofcb503c2016-05-18 12:48:17 +01006452class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006453 public:
6454 HSelect(HInstruction* condition,
6455 HInstruction* true_value,
6456 HInstruction* false_value,
6457 uint32_t dex_pc)
6458 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6459 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6460
6461 // First input must be `true_value` or `false_value` to allow codegens to
6462 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6463 // that architectures which implement HSelect as a conditional move also
6464 // will not need to invert the condition.
6465 SetRawInputAt(0, false_value);
6466 SetRawInputAt(1, true_value);
6467 SetRawInputAt(2, condition);
6468 }
6469
6470 HInstruction* GetFalseValue() const { return InputAt(0); }
6471 HInstruction* GetTrueValue() const { return InputAt(1); }
6472 HInstruction* GetCondition() const { return InputAt(2); }
6473
6474 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006475 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6476 return true;
6477 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006478
6479 bool CanBeNull() const OVERRIDE {
6480 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6481 }
6482
6483 DECLARE_INSTRUCTION(Select);
6484
6485 private:
6486 DISALLOW_COPY_AND_ASSIGN(HSelect);
6487};
6488
Vladimir Markof9f64412015-09-02 14:05:49 +01006489class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006490 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006491 MoveOperands(Location source,
6492 Location destination,
6493 Primitive::Type type,
6494 HInstruction* instruction)
6495 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006496
6497 Location GetSource() const { return source_; }
6498 Location GetDestination() const { return destination_; }
6499
6500 void SetSource(Location value) { source_ = value; }
6501 void SetDestination(Location value) { destination_ = value; }
6502
6503 // The parallel move resolver marks moves as "in-progress" by clearing the
6504 // destination (but not the source).
6505 Location MarkPending() {
6506 DCHECK(!IsPending());
6507 Location dest = destination_;
6508 destination_ = Location::NoLocation();
6509 return dest;
6510 }
6511
6512 void ClearPending(Location dest) {
6513 DCHECK(IsPending());
6514 destination_ = dest;
6515 }
6516
6517 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006518 DCHECK(source_.IsValid() || destination_.IsInvalid());
6519 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006520 }
6521
6522 // True if this blocks a move from the given location.
6523 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006524 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006525 }
6526
6527 // A move is redundant if it's been eliminated, if its source and
6528 // destination are the same, or if its destination is unneeded.
6529 bool IsRedundant() const {
6530 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6531 }
6532
6533 // We clear both operands to indicate move that's been eliminated.
6534 void Eliminate() {
6535 source_ = destination_ = Location::NoLocation();
6536 }
6537
6538 bool IsEliminated() const {
6539 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6540 return source_.IsInvalid();
6541 }
6542
Alexey Frunze4dda3372015-06-01 18:31:49 -07006543 Primitive::Type GetType() const { return type_; }
6544
Nicolas Geoffray90218252015-04-15 11:56:51 +01006545 bool Is64BitMove() const {
6546 return Primitive::Is64BitType(type_);
6547 }
6548
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006549 HInstruction* GetInstruction() const { return instruction_; }
6550
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006551 private:
6552 Location source_;
6553 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006554 // The type this move is for.
6555 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006556 // The instruction this move is assocatied with. Null when this move is
6557 // for moving an input in the expected locations of user (including a phi user).
6558 // This is only used in debug mode, to ensure we do not connect interval siblings
6559 // in the same parallel move.
6560 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006561};
6562
Roland Levillainc9285912015-12-18 10:38:42 +00006563std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6564
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006565static constexpr size_t kDefaultNumberOfMoves = 4;
6566
Vladimir Markofcb503c2016-05-18 12:48:17 +01006567class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006568 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006569 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006570 : HTemplateInstruction(SideEffects::None(), dex_pc),
6571 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6572 moves_.reserve(kDefaultNumberOfMoves);
6573 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006574
Nicolas Geoffray90218252015-04-15 11:56:51 +01006575 void AddMove(Location source,
6576 Location destination,
6577 Primitive::Type type,
6578 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006579 DCHECK(source.IsValid());
6580 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006581 if (kIsDebugBuild) {
6582 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006583 for (const MoveOperands& move : moves_) {
6584 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006585 // Special case the situation where the move is for the spill slot
6586 // of the instruction.
6587 if ((GetPrevious() == instruction)
6588 || ((GetPrevious() == nullptr)
6589 && instruction->IsPhi()
6590 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006591 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006592 << "Doing parallel moves for the same instruction.";
6593 } else {
6594 DCHECK(false) << "Doing parallel moves for the same instruction.";
6595 }
6596 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006597 }
6598 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006599 for (const MoveOperands& move : moves_) {
6600 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006601 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006602 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006603 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006604 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006605 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006606 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006607 }
6608
Vladimir Marko225b6462015-09-28 12:17:40 +01006609 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006610 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006611 }
6612
Vladimir Marko225b6462015-09-28 12:17:40 +01006613 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006614
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006615 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006616
6617 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006618 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006619
6620 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6621};
6622
Mark Mendell0616ae02015-04-17 12:49:27 -04006623} // namespace art
6624
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006625#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6626#include "nodes_shared.h"
6627#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006628#ifdef ART_ENABLE_CODEGEN_arm
6629#include "nodes_arm.h"
6630#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006631#ifdef ART_ENABLE_CODEGEN_arm64
6632#include "nodes_arm64.h"
6633#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006634#ifdef ART_ENABLE_CODEGEN_mips
6635#include "nodes_mips.h"
6636#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006637#ifdef ART_ENABLE_CODEGEN_x86
6638#include "nodes_x86.h"
6639#endif
6640
6641namespace art {
6642
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006643class HGraphVisitor : public ValueObject {
6644 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006645 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6646 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006647
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006648 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006649 virtual void VisitBasicBlock(HBasicBlock* block);
6650
Roland Levillain633021e2014-10-01 14:12:25 +01006651 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006652 void VisitInsertionOrder();
6653
Roland Levillain633021e2014-10-01 14:12:25 +01006654 // Visit the graph following dominator tree reverse post-order.
6655 void VisitReversePostOrder();
6656
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006657 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006658
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006659 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006660#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006661 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6662
6663 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6664
6665#undef DECLARE_VISIT_INSTRUCTION
6666
6667 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006668 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006669
6670 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6671};
6672
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006673class HGraphDelegateVisitor : public HGraphVisitor {
6674 public:
6675 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6676 virtual ~HGraphDelegateVisitor() {}
6677
6678 // Visit functions that delegate to to super class.
6679#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006680 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006681
6682 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6683
6684#undef DECLARE_VISIT_INSTRUCTION
6685
6686 private:
6687 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6688};
6689
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006690// Iterator over the blocks that art part of the loop. Includes blocks part
6691// of an inner loop. The order in which the blocks are iterated is on their
6692// block id.
6693class HBlocksInLoopIterator : public ValueObject {
6694 public:
6695 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6696 : blocks_in_loop_(info.GetBlocks()),
6697 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6698 index_(0) {
6699 if (!blocks_in_loop_.IsBitSet(index_)) {
6700 Advance();
6701 }
6702 }
6703
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006704 bool Done() const { return index_ == blocks_.size(); }
6705 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006706 void Advance() {
6707 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006708 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006709 if (blocks_in_loop_.IsBitSet(index_)) {
6710 break;
6711 }
6712 }
6713 }
6714
6715 private:
6716 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006717 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006718 size_t index_;
6719
6720 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6721};
6722
Mingyao Yang3584bce2015-05-19 16:01:59 -07006723// Iterator over the blocks that art part of the loop. Includes blocks part
6724// of an inner loop. The order in which the blocks are iterated is reverse
6725// post order.
6726class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6727 public:
6728 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6729 : blocks_in_loop_(info.GetBlocks()),
6730 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6731 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006732 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006733 Advance();
6734 }
6735 }
6736
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006737 bool Done() const { return index_ == blocks_.size(); }
6738 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006739 void Advance() {
6740 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006741 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6742 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006743 break;
6744 }
6745 }
6746 }
6747
6748 private:
6749 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006750 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006751 size_t index_;
6752
6753 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6754};
6755
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006756inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006757 if (constant->IsIntConstant()) {
6758 return constant->AsIntConstant()->GetValue();
6759 } else if (constant->IsLongConstant()) {
6760 return constant->AsLongConstant()->GetValue();
6761 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006762 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006763 return 0;
6764 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006765}
6766
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006767#define INSTRUCTION_TYPE_CHECK(type, super) \
6768 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6769 inline const H##type* HInstruction::As##type() const { \
6770 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6771 } \
6772 inline H##type* HInstruction::As##type() { \
6773 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6774 }
6775
6776 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6777#undef INSTRUCTION_TYPE_CHECK
6778
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006779// Create space in `blocks` for adding `number_of_new_blocks` entries
6780// starting at location `at`. Blocks after `at` are moved accordingly.
6781inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6782 size_t number_of_new_blocks,
6783 size_t after) {
6784 DCHECK_LT(after, blocks->size());
6785 size_t old_size = blocks->size();
6786 size_t new_size = old_size + number_of_new_blocks;
6787 blocks->resize(new_size);
6788 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6789}
6790
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006791} // namespace art
6792
6793#endif // ART_COMPILER_OPTIMIZING_NODES_H_