blob: e3f4d8f0352ea652e96421b3004da0d032f11c5c [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
Vladimir Markof9f64412015-09-02 14:05:49 +0100128class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100129 public:
130 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
131
132 void AddInstruction(HInstruction* instruction);
133 void RemoveInstruction(HInstruction* instruction);
134
David Brazdilc3d743f2015-04-22 13:40:50 +0100135 // Insert `instruction` before/after an existing instruction `cursor`.
136 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
137 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
138
Roland Levillain6b469232014-09-25 10:10:38 +0100139 // Return true if this list contains `instruction`.
140 bool Contains(HInstruction* instruction) const;
141
Roland Levillainccc07a92014-09-16 14:48:16 +0100142 // Return true if `instruction1` is found before `instruction2` in
143 // this instruction list and false otherwise. Abort if none
144 // of these instructions is found.
145 bool FoundBefore(const HInstruction* instruction1,
146 const HInstruction* instruction2) const;
147
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000148 bool IsEmpty() const { return first_instruction_ == nullptr; }
149 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
150
151 // Update the block of all instructions to be `block`.
152 void SetBlockOfInstructions(HBasicBlock* block) const;
153
154 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000155 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000156 void Add(const HInstructionList& instruction_list);
157
David Brazdil2d7352b2015-04-20 14:52:42 +0100158 // Return the number of instructions in the list. This is an expensive operation.
159 size_t CountSize() const;
160
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100161 private:
162 HInstruction* first_instruction_;
163 HInstruction* last_instruction_;
164
165 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000166 friend class HGraph;
167 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100168 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100169 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100170
171 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
172};
173
David Brazdil4833f5a2015-12-16 10:37:39 +0000174class ReferenceTypeInfo : ValueObject {
175 public:
176 typedef Handle<mirror::Class> TypeHandle;
177
Vladimir Markoa1de9182016-02-25 11:37:38 +0000178 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
179
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700180 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100181 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
182 }
183
Vladimir Markoa1de9182016-02-25 11:37:38 +0000184 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000185 return ReferenceTypeInfo(type_handle, is_exact);
186 }
187
188 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
189
Vladimir Markof39745e2016-01-26 12:16:55 +0000190 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000191 return handle.GetReference() != nullptr;
192 }
193
Vladimir Marko456307a2016-04-19 14:12:13 +0000194 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000195 return IsValidHandle(type_handle_);
196 }
197
198 bool IsExact() const { return is_exact_; }
199
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700200 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000201 DCHECK(IsValid());
202 return GetTypeHandle()->IsObjectClass();
203 }
204
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700205 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000206 DCHECK(IsValid());
207 return GetTypeHandle()->IsStringClass();
208 }
209
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700210 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000211 DCHECK(IsValid());
212 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
213 }
214
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000216 DCHECK(IsValid());
217 return GetTypeHandle()->IsInterface();
218 }
219
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700220 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000221 DCHECK(IsValid());
222 return GetTypeHandle()->IsArrayClass();
223 }
224
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000226 DCHECK(IsValid());
227 return GetTypeHandle()->IsPrimitiveArray();
228 }
229
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700230 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000231 DCHECK(IsValid());
232 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
233 }
234
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000236 DCHECK(IsValid());
237 if (!IsExact()) return false;
238 if (!IsArrayClass()) return false;
239 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
240 }
241
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700242 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000243 DCHECK(IsValid());
244 if (!IsExact()) return false;
245 if (!IsArrayClass()) return false;
246 if (!rti.IsArrayClass()) return false;
247 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
248 rti.GetTypeHandle()->GetComponentType());
249 }
250
251 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
252
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700253 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000254 DCHECK(IsValid());
255 DCHECK(rti.IsValid());
256 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
257 }
258
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700259 bool IsStrictSupertypeOf(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().Get() != rti.GetTypeHandle().Get() &&
263 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
264 }
265
266 // Returns true if the type information provide the same amount of details.
267 // Note that it does not mean that the instructions have the same actual type
268 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700269 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000270 if (!IsValid() && !rti.IsValid()) {
271 // Invalid types are equal.
272 return true;
273 }
274 if (!IsValid() || !rti.IsValid()) {
275 // One is valid, the other not.
276 return false;
277 }
278 return IsExact() == rti.IsExact()
279 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
280 }
281
282 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000283 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
284 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
285 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000286
287 // The class of the object.
288 TypeHandle type_handle_;
289 // Whether or not the type is exact or a superclass of the actual type.
290 // Whether or not we have any information about this type.
291 bool is_exact_;
292};
293
294std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
295
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000296// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100297class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000298 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100299 HGraph(ArenaAllocator* arena,
300 const DexFile& dex_file,
301 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100302 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700303 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100304 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100305 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000306 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100307 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100309 blocks_(arena->Adapter(kArenaAllocBlockList)),
310 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
311 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700312 entry_block_(nullptr),
313 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100314 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100315 number_of_vregs_(0),
316 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000317 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400318 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000319 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000320 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000321 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000322 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100323 dex_file_(dex_file),
324 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100325 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100326 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100327 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700328 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000329 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100330 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
331 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
332 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
333 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000334 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000335 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700336 osr_(osr),
337 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100338 blocks_.reserve(kDefaultNumberOfBlocks);
339 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000340
David Brazdilbadd8262016-02-02 16:28:56 +0000341 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700342 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000343
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000344 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100345 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
346
David Brazdil69ba7b72015-06-23 18:27:30 +0100347 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000348 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100349
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000350 HBasicBlock* GetEntryBlock() const { return entry_block_; }
351 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100352 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000353
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000354 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
355 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000356
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000357 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100358
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100359 void ComputeDominanceInformation();
360 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000361 void ClearLoopInformation();
362 void FindBackEdges(ArenaBitVector* visited);
363 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100364 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100365 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000366
David Brazdil4833f5a2015-12-16 10:37:39 +0000367 // Analyze all natural loops in this graph. Returns a code specifying that it
368 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000369 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000370 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100371
David Brazdilffee3d32015-07-06 11:48:53 +0100372 // Iterate over blocks to compute try block membership. Needs reverse post
373 // order and loop information.
374 void ComputeTryBlockInformation();
375
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000376 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000377 // Returns the instruction to replace the invoke expression or null if the
378 // invoke is for a void method. Note that the caller is responsible for replacing
379 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000380 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000381
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000382 // Update the loop and try membership of `block`, which was spawned from `reference`.
383 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
384 // should be the new back edge.
385 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
386 HBasicBlock* reference,
387 bool replace_if_back_edge);
388
Mingyao Yang3584bce2015-05-19 16:01:59 -0700389 // Need to add a couple of blocks to test if the loop body is entered and
390 // put deoptimization instructions, etc.
391 void TransformLoopHeaderForBCE(HBasicBlock* header);
392
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000393 // Removes `block` from the graph. Assumes `block` has been disconnected from
394 // other blocks and has no instructions or phis.
395 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000396
David Brazdilfc6a86a2015-06-26 10:33:45 +0000397 // Splits the edge between `block` and `successor` while preserving the
398 // indices in the predecessor/successor lists. If there are multiple edges
399 // between the blocks, the lowest indices are used.
400 // Returns the new block which is empty and has the same dex pc as `successor`.
401 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
402
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100403 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
404 void SimplifyLoop(HBasicBlock* header);
405
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000406 int32_t GetNextInstructionId() {
407 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000408 return current_instruction_id_++;
409 }
410
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000411 int32_t GetCurrentInstructionId() const {
412 return current_instruction_id_;
413 }
414
415 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000416 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000417 current_instruction_id_ = id;
418 }
419
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100420 uint16_t GetMaximumNumberOfOutVRegs() const {
421 return maximum_number_of_out_vregs_;
422 }
423
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000424 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
425 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100426 }
427
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100428 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
429 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
430 }
431
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000432 void UpdateTemporariesVRegSlots(size_t slots) {
433 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100434 }
435
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000436 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100437 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000438 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100439 }
440
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100441 void SetNumberOfVRegs(uint16_t number_of_vregs) {
442 number_of_vregs_ = number_of_vregs;
443 }
444
445 uint16_t GetNumberOfVRegs() const {
446 return number_of_vregs_;
447 }
448
449 void SetNumberOfInVRegs(uint16_t value) {
450 number_of_in_vregs_ = value;
451 }
452
David Brazdildee58d62016-04-07 09:54:26 +0000453 uint16_t GetNumberOfInVRegs() const {
454 return number_of_in_vregs_;
455 }
456
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100457 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100458 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100459 return number_of_vregs_ - number_of_in_vregs_;
460 }
461
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100462 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100463 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100464 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100465
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100466 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
467 DCHECK(GetReversePostOrder()[0] == entry_block_);
468 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
469 }
470
471 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
472 return ReverseRange(GetReversePostOrder());
473 }
474
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100475 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100476 return linear_order_;
477 }
478
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100479 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
480 return ReverseRange(GetLinearOrder());
481 }
482
Mark Mendell1152c922015-04-24 17:06:35 -0400483 bool HasBoundsChecks() const {
484 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800485 }
486
Mark Mendell1152c922015-04-24 17:06:35 -0400487 void SetHasBoundsChecks(bool value) {
488 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800489 }
490
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100491 bool ShouldGenerateConstructorBarrier() const {
492 return should_generate_constructor_barrier_;
493 }
494
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000495 bool IsDebuggable() const { return debuggable_; }
496
David Brazdil8d5b8b22015-03-24 10:51:52 +0000497 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000498 // already, it is created and inserted into the graph. This method is only for
499 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600500 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000501
502 // TODO: This is problematic for the consistency of reference type propagation
503 // because it can be created anytime after the pass and thus it will be left
504 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600505 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000506
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600507 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
508 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000509 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600510 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
511 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000512 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600513 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
514 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000515 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600516 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
517 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000518 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000519
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100520 HCurrentMethod* GetCurrentMethod();
521
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100522 const DexFile& GetDexFile() const {
523 return dex_file_;
524 }
525
526 uint32_t GetMethodIdx() const {
527 return method_idx_;
528 }
529
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100530 InvokeType GetInvokeType() const {
531 return invoke_type_;
532 }
533
Mark Mendellc4701932015-04-10 13:18:51 -0400534 InstructionSet GetInstructionSet() const {
535 return instruction_set_;
536 }
537
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000538 bool IsCompilingOsr() const { return osr_; }
539
Mingyao Yang063fc772016-08-02 11:02:54 -0700540 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
541 return cha_single_implementation_list_;
542 }
543
544 void AddCHASingleImplementationDependency(ArtMethod* method) {
545 cha_single_implementation_list_.insert(method);
546 }
547
548 bool HasShouldDeoptimizeFlag() const {
549 // TODO: if all CHA guards can be eliminated, there is no need for the flag
550 // even if cha_single_implementation_list_ is not empty.
551 return !cha_single_implementation_list_.empty();
552 }
553
David Brazdil77a48ae2015-09-15 12:34:04 +0000554 bool HasTryCatch() const { return has_try_catch_; }
555 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100556
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000557 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
558 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
559
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100560 ArtMethod* GetArtMethod() const { return art_method_; }
561 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
562
Mark Mendellf6529172015-11-17 11:16:56 -0500563 // Returns an instruction with the opposite boolean value from 'cond'.
564 // The instruction has been inserted into the graph, either as a constant, or
565 // before cursor.
566 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
567
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000568 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
569
David Brazdil2d7352b2015-04-20 14:52:42 +0100570 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000571 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100572 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000573
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000574 template <class InstructionType, typename ValueType>
575 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600576 ArenaSafeMap<ValueType, InstructionType*>* cache,
577 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000578 // Try to find an existing constant of the given value.
579 InstructionType* constant = nullptr;
580 auto cached_constant = cache->find(value);
581 if (cached_constant != cache->end()) {
582 constant = cached_constant->second;
583 }
584
585 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100586 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000587 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600588 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000589 cache->Overwrite(value, constant);
590 InsertConstant(constant);
591 }
592 return constant;
593 }
594
David Brazdil8d5b8b22015-03-24 10:51:52 +0000595 void InsertConstant(HConstant* instruction);
596
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000597 // Cache a float constant into the graph. This method should only be
598 // called by the SsaBuilder when creating "equivalent" instructions.
599 void CacheFloatConstant(HFloatConstant* constant);
600
601 // See CacheFloatConstant comment.
602 void CacheDoubleConstant(HDoubleConstant* constant);
603
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000604 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000605
606 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100607 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000608
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100609 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100610 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000611
Aart Bik281c6812016-08-26 11:31:48 -0700612 // List of blocks to perform a linear order tree traversal. Unlike the reverse
613 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100614 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100615
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000616 HBasicBlock* entry_block_;
617 HBasicBlock* exit_block_;
618
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100619 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100620 uint16_t maximum_number_of_out_vregs_;
621
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100622 // The number of virtual registers in this method. Contains the parameters.
623 uint16_t number_of_vregs_;
624
625 // The number of virtual registers used by parameters of this method.
626 uint16_t number_of_in_vregs_;
627
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000628 // Number of vreg size slots that the temporaries use (used in baseline compiler).
629 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100630
Mark Mendell1152c922015-04-24 17:06:35 -0400631 // Has bounds checks. We can totally skip BCE if it's false.
632 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800633
David Brazdil77a48ae2015-09-15 12:34:04 +0000634 // Flag whether there are any try/catch blocks in the graph. We will skip
635 // try/catch-related passes if false.
636 bool has_try_catch_;
637
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000638 // Flag whether there are any irreducible loops in the graph.
639 bool has_irreducible_loops_;
640
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000641 // Indicates whether the graph should be compiled in a way that
642 // ensures full debuggability. If false, we can apply more
643 // aggressive optimizations that may limit the level of debugging.
644 const bool debuggable_;
645
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000646 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000647 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000648
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100649 // The dex file from which the method is from.
650 const DexFile& dex_file_;
651
652 // The method index in the dex file.
653 const uint32_t method_idx_;
654
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100655 // If inlined, this encodes how the callee is being invoked.
656 const InvokeType invoke_type_;
657
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100658 // Whether the graph has been transformed to SSA form. Only used
659 // in debug mode to ensure we are not using properties only valid
660 // for non-SSA form (like the number of temporaries).
661 bool in_ssa_form_;
662
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100663 const bool should_generate_constructor_barrier_;
664
Mathieu Chartiere401d142015-04-22 13:56:20 -0700665 const InstructionSet instruction_set_;
666
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000667 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000668 HNullConstant* cached_null_constant_;
669 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000670 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000671 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000672 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000673
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100674 HCurrentMethod* cached_current_method_;
675
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100676 // The ArtMethod this graph is for. Note that for AOT, it may be null,
677 // for example for methods whose declaring class could not be resolved
678 // (such as when the superclass could not be found).
679 ArtMethod* art_method_;
680
David Brazdil4833f5a2015-12-16 10:37:39 +0000681 // Keep the RTI of inexact Object to avoid having to pass stack handle
682 // collection pointer to passes which may create NullConstant.
683 ReferenceTypeInfo inexact_object_rti_;
684
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000685 // Whether we are compiling this graph for on stack replacement: this will
686 // make all loops seen as irreducible and emit special stack maps to mark
687 // compiled code entries which the interpreter can directly jump to.
688 const bool osr_;
689
Mingyao Yang063fc772016-08-02 11:02:54 -0700690 // List of methods that are assumed to have single implementation.
691 ArenaSet<ArtMethod*> cha_single_implementation_list_;
692
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000693 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100694 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000695 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000696 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000697 DISALLOW_COPY_AND_ASSIGN(HGraph);
698};
699
Vladimir Markof9f64412015-09-02 14:05:49 +0100700class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000701 public:
702 HLoopInformation(HBasicBlock* header, HGraph* graph)
703 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100704 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000705 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100706 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100707 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100708 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000709 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100710 back_edges_.reserve(kDefaultNumberOfBackEdges);
711 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100712
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000713 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100714 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000715
716 void Dump(std::ostream& os);
717
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100718 HBasicBlock* GetHeader() const {
719 return header_;
720 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000721
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000722 void SetHeader(HBasicBlock* block) {
723 header_ = block;
724 }
725
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100726 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
727 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
728 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
729
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000730 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100731 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000732 }
733
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100734 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100735 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100736 }
737
David Brazdil46e2a392015-03-16 17:31:52 +0000738 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100739 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100740 }
741
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000742 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100743 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000744 }
745
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100746 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100747
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100748 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100749 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100750 }
751
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100752 // Returns the lifetime position of the back edge that has the
753 // greatest lifetime position.
754 size_t GetLifetimeEnd() const;
755
756 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100757 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100758 }
759
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000760 // Finds blocks that are part of this loop.
761 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100762
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100763 // Returns whether this loop information contains `block`.
764 // Note that this loop information *must* be populated before entering this function.
765 bool Contains(const HBasicBlock& block) const;
766
767 // Returns whether this loop information is an inner loop of `other`.
768 // Note that `other` *must* be populated before entering this function.
769 bool IsIn(const HLoopInformation& other) const;
770
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800771 // Returns true if instruction is not defined within this loop.
772 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700773
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100774 const ArenaBitVector& GetBlocks() const { return blocks_; }
775
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000776 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000777 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000778
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000779 void ClearAllBlocks() {
780 blocks_.ClearAllBits();
781 }
782
David Brazdil3f4a5222016-05-06 12:46:21 +0100783 bool HasBackEdgeNotDominatedByHeader() const;
784
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100785 bool IsPopulated() const {
786 return blocks_.GetHighestBitSet() != -1;
787 }
788
Anton Shaminf89381f2016-05-16 16:44:13 +0600789 bool DominatesAllBackEdges(HBasicBlock* block);
790
David Sehrc757dec2016-11-04 15:48:34 -0700791 bool HasExitEdge() const;
792
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000793 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100794 // Internal recursive implementation of `Populate`.
795 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100796 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100797
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000798 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100799 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000800 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100801 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100802 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100803 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000804
805 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
806};
807
David Brazdilec16f792015-08-19 15:04:01 +0100808// Stores try/catch information for basic blocks.
809// Note that HGraph is constructed so that catch blocks cannot simultaneously
810// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100811class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100812 public:
813 // Try block information constructor.
814 explicit TryCatchInformation(const HTryBoundary& try_entry)
815 : try_entry_(&try_entry),
816 catch_dex_file_(nullptr),
817 catch_type_index_(DexFile::kDexNoIndex16) {
818 DCHECK(try_entry_ != nullptr);
819 }
820
821 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800822 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100823 : try_entry_(nullptr),
824 catch_dex_file_(&dex_file),
825 catch_type_index_(catch_type_index) {}
826
827 bool IsTryBlock() const { return try_entry_ != nullptr; }
828
829 const HTryBoundary& GetTryEntry() const {
830 DCHECK(IsTryBlock());
831 return *try_entry_;
832 }
833
834 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
835
836 bool IsCatchAllTypeIndex() const {
837 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800838 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100839 }
840
Andreas Gampea5b09a62016-11-17 15:21:22 -0800841 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100842 DCHECK(IsCatchBlock());
843 return catch_type_index_;
844 }
845
846 const DexFile& GetCatchDexFile() const {
847 DCHECK(IsCatchBlock());
848 return *catch_dex_file_;
849 }
850
851 private:
852 // One of possibly several TryBoundary instructions entering the block's try.
853 // Only set for try blocks.
854 const HTryBoundary* try_entry_;
855
856 // Exception type information. Only set for catch blocks.
857 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800858 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100859};
860
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100861static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100862static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100863
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000864// A block in a method. Contains the list of instructions represented
865// as a double linked list. Each block knows its predecessors and
866// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100867
Vladimir Markof9f64412015-09-02 14:05:49 +0100868class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000869 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700870 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000871 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000872 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
873 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000874 loop_information_(nullptr),
875 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000876 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100877 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100878 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100879 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000880 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000881 try_catch_information_(nullptr) {
882 predecessors_.reserve(kDefaultNumberOfPredecessors);
883 successors_.reserve(kDefaultNumberOfSuccessors);
884 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
885 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000886
Vladimir Marko60584552015-09-03 13:35:12 +0000887 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100888 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000889 }
890
Vladimir Marko60584552015-09-03 13:35:12 +0000891 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100892 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000893 }
894
David Brazdild26a4112015-11-10 11:07:31 +0000895 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
896 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
897
Vladimir Marko60584552015-09-03 13:35:12 +0000898 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
899 return ContainsElement(successors_, block, start_from);
900 }
901
902 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100903 return dominated_blocks_;
904 }
905
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100906 bool IsEntryBlock() const {
907 return graph_->GetEntryBlock() == this;
908 }
909
910 bool IsExitBlock() const {
911 return graph_->GetExitBlock() == this;
912 }
913
David Brazdil46e2a392015-03-16 17:31:52 +0000914 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000915 bool IsSingleTryBoundary() const;
916
917 // Returns true if this block emits nothing but a jump.
918 bool IsSingleJump() const {
919 HLoopInformation* loop_info = GetLoopInformation();
920 return (IsSingleGoto() || IsSingleTryBoundary())
921 // Back edges generate a suspend check.
922 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
923 }
David Brazdil46e2a392015-03-16 17:31:52 +0000924
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000925 void AddBackEdge(HBasicBlock* back_edge) {
926 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000927 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000928 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100929 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000930 loop_information_->AddBackEdge(back_edge);
931 }
932
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000933 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000934 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000935
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100936 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000937 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600938 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000939
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000940 HBasicBlock* GetDominator() const { return dominator_; }
941 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000942 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
943
944 void RemoveDominatedBlock(HBasicBlock* block) {
945 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100946 }
Vladimir Marko60584552015-09-03 13:35:12 +0000947
948 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
949 ReplaceElement(dominated_blocks_, existing, new_block);
950 }
951
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100952 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000953
954 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100955 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000956 }
957
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100958 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
959 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100960 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100961 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100962 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
963 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000964
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000965 HInstruction* GetFirstInstructionDisregardMoves() const;
966
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000967 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000968 successors_.push_back(block);
969 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000970 }
971
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100972 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
973 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100974 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000975 new_block->predecessors_.push_back(this);
976 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000977 }
978
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000979 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
980 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000981 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000982 new_block->successors_.push_back(this);
983 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000984 }
985
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100986 // Insert `this` between `predecessor` and `successor. This method
987 // preserves the indicies, and will update the first edge found between
988 // `predecessor` and `successor`.
989 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
990 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100991 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000992 successor->predecessors_[predecessor_index] = this;
993 predecessor->successors_[successor_index] = this;
994 successors_.push_back(successor);
995 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100996 }
997
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100998 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000999 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001000 }
1001
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001002 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001003 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001004 }
1005
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001006 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001007 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001008 }
1009
1010 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001011 predecessors_.push_back(block);
1012 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001013 }
1014
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001015 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001016 DCHECK_EQ(predecessors_.size(), 2u);
1017 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001018 }
1019
David Brazdil769c9e52015-04-27 13:54:09 +01001020 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001021 DCHECK_EQ(successors_.size(), 2u);
1022 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001023 }
1024
David Brazdilfc6a86a2015-06-26 10:33:45 +00001025 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001026 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001027 }
1028
David Brazdilfc6a86a2015-06-26 10:33:45 +00001029 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001030 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001031 }
1032
David Brazdilfc6a86a2015-06-26 10:33:45 +00001033 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001034 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001035 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001036 }
1037
1038 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001039 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001040 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001041 }
1042
1043 // Returns whether the first occurrence of `predecessor` in the list of
1044 // predecessors is at index `idx`.
1045 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001046 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001047 return GetPredecessorIndexOf(predecessor) == idx;
1048 }
1049
David Brazdild7558da2015-09-22 13:04:14 +01001050 // Create a new block between this block and its predecessors. The new block
1051 // is added to the graph, all predecessor edges are relinked to it and an edge
1052 // is created to `this`. Returns the new empty block. Reverse post order or
1053 // loop and try/catch information are not updated.
1054 HBasicBlock* CreateImmediateDominator();
1055
David Brazdilfc6a86a2015-06-26 10:33:45 +00001056 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001057 // created, latter block. Note that this method will add the block to the
1058 // graph, create a Goto at the end of the former block and will create an edge
1059 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001060 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001061 HBasicBlock* SplitBefore(HInstruction* cursor);
1062
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001063 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001064 // created block. Note that this method just updates raw block information,
1065 // like predecessors, successors, dominators, and instruction list. It does not
1066 // update the graph, reverse post order, loop information, nor make sure the
1067 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001068 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1069
1070 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1071 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001072
1073 // Merge `other` at the end of `this`. Successors and dominated blocks of
1074 // `other` are changed to be successors and dominated blocks of `this`. Note
1075 // that this method does not update the graph, reverse post order, loop
1076 // information, nor make sure the blocks are consistent (for example ending
1077 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001078 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001079
1080 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1081 // of `this` are moved to `other`.
1082 // Note that this method does not update the graph, reverse post order, loop
1083 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001084 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001085 void ReplaceWith(HBasicBlock* other);
1086
David Brazdil2d7352b2015-04-20 14:52:42 +01001087 // Merge `other` at the end of `this`. This method updates loops, reverse post
1088 // order, links to predecessors, successors, dominators and deletes the block
1089 // from the graph. The two blocks must be successive, i.e. `this` the only
1090 // predecessor of `other` and vice versa.
1091 void MergeWith(HBasicBlock* other);
1092
1093 // Disconnects `this` from all its predecessors, successors and dominator,
1094 // removes it from all loops it is included in and eventually from the graph.
1095 // The block must not dominate any other block. Predecessors and successors
1096 // are safely updated.
1097 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001098
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001099 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001100 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001101 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001102 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001103 // Replace instruction `initial` with `replacement` within this block.
1104 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1105 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001106 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001107 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001108 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1109 // instruction list. With 'ensure_safety' set to true, it verifies that the
1110 // instruction is not in use and removes it from the use lists of its inputs.
1111 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1112 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001113 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001114
1115 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001116 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001117 }
1118
Roland Levillain6b879dd2014-09-22 17:13:44 +01001119 bool IsLoopPreHeaderFirstPredecessor() const {
1120 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001121 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001122 }
1123
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001124 bool IsFirstPredecessorBackEdge() const {
1125 DCHECK(IsLoopHeader());
1126 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1127 }
1128
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001129 HLoopInformation* GetLoopInformation() const {
1130 return loop_information_;
1131 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001132
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001133 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001134 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001135 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001136 void SetInLoop(HLoopInformation* info) {
1137 if (IsLoopHeader()) {
1138 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001139 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001140 loop_information_ = info;
1141 } else if (loop_information_->Contains(*info->GetHeader())) {
1142 // Block is currently part of an outer loop. Make it part of this inner loop.
1143 // Note that a non loop header having a loop information means this loop information
1144 // has already been populated
1145 loop_information_ = info;
1146 } else {
1147 // Block is part of an inner loop. Do not update the loop information.
1148 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1149 // at this point, because this method is being called while populating `info`.
1150 }
1151 }
1152
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001153 // Raw update of the loop information.
1154 void SetLoopInformation(HLoopInformation* info) {
1155 loop_information_ = info;
1156 }
1157
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001158 bool IsInLoop() const { return loop_information_ != nullptr; }
1159
David Brazdilec16f792015-08-19 15:04:01 +01001160 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1161
1162 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1163 try_catch_information_ = try_catch_information;
1164 }
1165
1166 bool IsTryBlock() const {
1167 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1168 }
1169
1170 bool IsCatchBlock() const {
1171 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1172 }
David Brazdilffee3d32015-07-06 11:48:53 +01001173
1174 // Returns the try entry that this block's successors should have. They will
1175 // be in the same try, unless the block ends in a try boundary. In that case,
1176 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001177 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001178
David Brazdild7558da2015-09-22 13:04:14 +01001179 bool HasThrowingInstructions() const;
1180
David Brazdila4b8c212015-05-07 09:59:30 +01001181 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001182 bool Dominates(HBasicBlock* block) const;
1183
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001184 size_t GetLifetimeStart() const { return lifetime_start_; }
1185 size_t GetLifetimeEnd() const { return lifetime_end_; }
1186
1187 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1188 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1189
David Brazdil8d5b8b22015-03-24 10:51:52 +00001190 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001191 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001192 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001193 bool HasSinglePhi() const;
1194
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001195 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001196 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001197 ArenaVector<HBasicBlock*> predecessors_;
1198 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001199 HInstructionList instructions_;
1200 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001201 HLoopInformation* loop_information_;
1202 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001203 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001204 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001205 // The dex program counter of the first instruction of this block.
1206 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001207 size_t lifetime_start_;
1208 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001209 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001210
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001211 friend class HGraph;
1212 friend class HInstruction;
1213
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001214 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1215};
1216
David Brazdilb2bd1c52015-03-25 11:17:37 +00001217// Iterates over the LoopInformation of all loops which contain 'block'
1218// from the innermost to the outermost.
1219class HLoopInformationOutwardIterator : public ValueObject {
1220 public:
1221 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1222 : current_(block.GetLoopInformation()) {}
1223
1224 bool Done() const { return current_ == nullptr; }
1225
1226 void Advance() {
1227 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001228 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001229 }
1230
1231 HLoopInformation* Current() const {
1232 DCHECK(!Done());
1233 return current_;
1234 }
1235
1236 private:
1237 HLoopInformation* current_;
1238
1239 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1240};
1241
Alexandre Ramesef20f712015-06-09 10:29:30 +01001242#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001243 M(Above, Condition) \
1244 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001245 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001246 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001247 M(ArrayGet, Instruction) \
1248 M(ArrayLength, Instruction) \
1249 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001250 M(Below, Condition) \
1251 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001252 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001253 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001254 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001255 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001256 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001257 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001258 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001259 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001260 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001261 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001262 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001263 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001264 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001265 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001266 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001267 M(Exit, Instruction) \
1268 M(FloatConstant, Constant) \
1269 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001270 M(GreaterThan, Condition) \
1271 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001272 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001273 M(InstanceFieldGet, Instruction) \
1274 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001275 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001276 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001277 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001278 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001279 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001280 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001281 M(LessThan, Condition) \
1282 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001283 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001284 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001285 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001286 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001287 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001288 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001289 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001290 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001291 M(Neg, UnaryOperation) \
1292 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001293 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001294 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001295 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001296 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001297 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001298 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001299 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001300 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001301 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001302 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001303 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001304 M(Return, Instruction) \
1305 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001306 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001307 M(Shl, BinaryOperation) \
1308 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001309 M(StaticFieldGet, Instruction) \
1310 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001311 M(UnresolvedInstanceFieldGet, Instruction) \
1312 M(UnresolvedInstanceFieldSet, Instruction) \
1313 M(UnresolvedStaticFieldGet, Instruction) \
1314 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001315 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001316 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001317 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001318 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001319 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001320 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001321 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001322 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001323
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001324/*
1325 * Instructions, shared across several (not all) architectures.
1326 */
1327#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1328#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1329#else
1330#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001331 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001332 M(MultiplyAccumulate, Instruction) \
1333 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001334#endif
1335
Vladimir Markob4536b72015-11-24 13:45:23 +00001336#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001337#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001338#else
1339#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1340 M(ArmDexCacheArraysBase, Instruction)
1341#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001342
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001343#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001344#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001345#else
1346#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001347 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001348#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001349
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001350#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001351#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001352#else
1353#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1354 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001355 M(MipsDexCacheArraysBase, Instruction) \
1356 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001357#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001358
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001359#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1360
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001361#ifndef ART_ENABLE_CODEGEN_x86
1362#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1363#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001364#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1365 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001366 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001367 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001368 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001369#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001370
1371#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1372
1373#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1374 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001375 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001376 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1377 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001378 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001379 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001380 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1381 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1382
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001383#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1384 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001385 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001386 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001387 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001388 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001389
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001390#define FOR_EACH_INSTRUCTION(M) \
1391 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1392 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1393
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001394#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001395FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1396#undef FORWARD_DECLARATION
1397
Vladimir Marko372f10e2016-05-17 16:30:10 +01001398#define DECLARE_INSTRUCTION(type) \
1399 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1400 const char* DebugName() const OVERRIDE { return #type; } \
1401 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1402 return other->Is##type(); \
1403 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001404 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001405
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001406#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1407 bool Is##type() const { return As##type() != nullptr; } \
1408 const H##type* As##type() const { return this; } \
1409 H##type* As##type() { return this; }
1410
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001411template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001412class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001413 public:
David Brazdiled596192015-01-23 10:39:45 +00001414 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001415 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001416 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001417
Vladimir Marko46817b82016-03-29 12:21:58 +01001418 // Hook for the IntrusiveForwardList<>.
1419 // TODO: Hide this better.
1420 IntrusiveForwardListHook hook;
1421
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001422 private:
David Brazdiled596192015-01-23 10:39:45 +00001423 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001424 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001425
1426 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001427 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001428
Vladimir Marko46817b82016-03-29 12:21:58 +01001429 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001430
1431 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1432};
1433
David Brazdiled596192015-01-23 10:39:45 +00001434template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001435using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001436
David Brazdil1abb4192015-02-17 18:33:36 +00001437// This class is used by HEnvironment and HInstruction classes to record the
1438// instructions they use and pointers to the corresponding HUseListNodes kept
1439// by the used instructions.
1440template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001441class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001442 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001443 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1444 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001445
Vladimir Marko46817b82016-03-29 12:21:58 +01001446 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1447 : HUserRecord(old_record.instruction_, before_use_node) {}
1448 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1449 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001450 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001451 }
1452
1453 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001454 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1455 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001456
1457 private:
1458 // Instruction used by the user.
1459 HInstruction* instruction_;
1460
Vladimir Marko46817b82016-03-29 12:21:58 +01001461 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1462 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001463};
1464
Vladimir Markoe9004912016-06-16 16:50:52 +01001465// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1466// This is used for HInstruction::GetInputs() to return a container wrapper providing
1467// HInstruction* values even though the underlying container has HUserRecord<>s.
1468struct HInputExtractor {
1469 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1470 return record.GetInstruction();
1471 }
1472 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1473 return record.GetInstruction();
1474 }
1475};
1476
1477using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1478using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1479
Aart Bik854a02b2015-07-14 16:07:00 -07001480/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001481 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001482 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001483 * For write/read dependences on fields/arrays, the dependence analysis uses
1484 * type disambiguation (e.g. a float field write cannot modify the value of an
1485 * integer field read) and the access type (e.g. a reference array write cannot
1486 * modify the value of a reference field read [although it may modify the
1487 * reference fetch prior to reading the field, which is represented by its own
1488 * write/read dependence]). The analysis makes conservative points-to
1489 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1490 * the same, and any reference read depends on any reference read without
1491 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001492 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001493 * The internal representation uses 38-bit and is described in the table below.
1494 * The first line indicates the side effect, and for field/array accesses the
1495 * second line indicates the type of the access (in the order of the
1496 * Primitive::Type enum).
1497 * The two numbered lines below indicate the bit position in the bitfield (read
1498 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001499 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001500 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1501 * +-------------+---------+---------+--------------+---------+---------+
1502 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1503 * | 3 |333333322|222222221| 1 |111111110|000000000|
1504 * | 7 |654321098|765432109| 8 |765432109|876543210|
1505 *
1506 * Note that, to ease the implementation, 'changes' bits are least significant
1507 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001508 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001509class SideEffects : public ValueObject {
1510 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001511 SideEffects() : flags_(0) {}
1512
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001513 static SideEffects None() {
1514 return SideEffects(0);
1515 }
1516
1517 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001518 return SideEffects(kAllChangeBits | kAllDependOnBits);
1519 }
1520
1521 static SideEffects AllChanges() {
1522 return SideEffects(kAllChangeBits);
1523 }
1524
1525 static SideEffects AllDependencies() {
1526 return SideEffects(kAllDependOnBits);
1527 }
1528
1529 static SideEffects AllExceptGCDependency() {
1530 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1531 }
1532
1533 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001534 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001535 }
1536
Aart Bik34c3ba92015-07-20 14:08:59 -07001537 static SideEffects AllWrites() {
1538 return SideEffects(kAllWrites);
1539 }
1540
1541 static SideEffects AllReads() {
1542 return SideEffects(kAllReads);
1543 }
1544
1545 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1546 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001547 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001548 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001549 }
1550
Aart Bik854a02b2015-07-14 16:07:00 -07001551 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001552 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001553 }
1554
Aart Bik34c3ba92015-07-20 14:08:59 -07001555 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1556 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001557 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001558 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001559 }
1560
1561 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001562 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001563 }
1564
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001565 static SideEffects CanTriggerGC() {
1566 return SideEffects(1ULL << kCanTriggerGCBit);
1567 }
1568
1569 static SideEffects DependsOnGC() {
1570 return SideEffects(1ULL << kDependsOnGCBit);
1571 }
1572
Aart Bik854a02b2015-07-14 16:07:00 -07001573 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001574 SideEffects Union(SideEffects other) const {
1575 return SideEffects(flags_ | other.flags_);
1576 }
1577
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001578 SideEffects Exclusion(SideEffects other) const {
1579 return SideEffects(flags_ & ~other.flags_);
1580 }
1581
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001582 void Add(SideEffects other) {
1583 flags_ |= other.flags_;
1584 }
1585
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001586 bool Includes(SideEffects other) const {
1587 return (other.flags_ & flags_) == other.flags_;
1588 }
1589
1590 bool HasSideEffects() const {
1591 return (flags_ & kAllChangeBits);
1592 }
1593
1594 bool HasDependencies() const {
1595 return (flags_ & kAllDependOnBits);
1596 }
1597
1598 // Returns true if there are no side effects or dependencies.
1599 bool DoesNothing() const {
1600 return flags_ == 0;
1601 }
1602
Aart Bik854a02b2015-07-14 16:07:00 -07001603 // Returns true if something is written.
1604 bool DoesAnyWrite() const {
1605 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001606 }
1607
Aart Bik854a02b2015-07-14 16:07:00 -07001608 // Returns true if something is read.
1609 bool DoesAnyRead() const {
1610 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001611 }
1612
Aart Bik854a02b2015-07-14 16:07:00 -07001613 // Returns true if potentially everything is written and read
1614 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001615 bool DoesAllReadWrite() const {
1616 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1617 }
1618
Aart Bik854a02b2015-07-14 16:07:00 -07001619 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001620 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001621 }
1622
Roland Levillain0d5a2812015-11-13 10:07:31 +00001623 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001624 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001625 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1626 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001627 }
1628
1629 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001630 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001631 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001632 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001633 for (int s = kLastBit; s >= 0; s--) {
1634 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1635 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1636 // This is a bit for the GC side effect.
1637 if (current_bit_is_set) {
1638 flags += "GC";
1639 }
Aart Bik854a02b2015-07-14 16:07:00 -07001640 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001641 } else {
1642 // This is a bit for the array/field analysis.
1643 // The underscore character stands for the 'can trigger GC' bit.
1644 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1645 if (current_bit_is_set) {
1646 flags += kDebug[s];
1647 }
1648 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1649 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1650 flags += "|";
1651 }
1652 }
Aart Bik854a02b2015-07-14 16:07:00 -07001653 }
1654 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001655 }
1656
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001657 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001658
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001659 private:
1660 static constexpr int kFieldArrayAnalysisBits = 9;
1661
1662 static constexpr int kFieldWriteOffset = 0;
1663 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1664 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1665 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1666
1667 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1668
1669 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1670 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1671 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1672 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1673
1674 static constexpr int kLastBit = kDependsOnGCBit;
1675 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1676
1677 // Aliases.
1678
1679 static_assert(kChangeBits == kDependOnBits,
1680 "the 'change' bits should match the 'depend on' bits.");
1681
1682 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1683 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1684 static constexpr uint64_t kAllWrites =
1685 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1686 static constexpr uint64_t kAllReads =
1687 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001688
Aart Bik854a02b2015-07-14 16:07:00 -07001689 // Translates type to bit flag.
1690 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1691 CHECK_NE(type, Primitive::kPrimVoid);
1692 const uint64_t one = 1;
1693 const int shift = type; // 0-based consecutive enum
1694 DCHECK_LE(kFieldWriteOffset, shift);
1695 DCHECK_LT(shift, kArrayWriteOffset);
1696 return one << (type + offset);
1697 }
1698
1699 // Private constructor on direct flags value.
1700 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1701
1702 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001703};
1704
David Brazdiled596192015-01-23 10:39:45 +00001705// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001706class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001707 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001708 HEnvironment(ArenaAllocator* arena,
1709 size_t number_of_vregs,
1710 const DexFile& dex_file,
1711 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001712 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001713 InvokeType invoke_type,
1714 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001715 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1716 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001717 parent_(nullptr),
1718 dex_file_(dex_file),
1719 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001720 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001721 invoke_type_(invoke_type),
1722 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001723 }
1724
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001725 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001726 : HEnvironment(arena,
1727 to_copy.Size(),
1728 to_copy.GetDexFile(),
1729 to_copy.GetMethodIdx(),
1730 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001731 to_copy.GetInvokeType(),
1732 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001733
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001734 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001735 if (parent_ != nullptr) {
1736 parent_->SetAndCopyParentChain(allocator, parent);
1737 } else {
1738 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1739 parent_->CopyFrom(parent);
1740 if (parent->GetParent() != nullptr) {
1741 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1742 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001743 }
David Brazdiled596192015-01-23 10:39:45 +00001744 }
1745
Vladimir Marko71bf8092015-09-15 15:33:14 +01001746 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001747 void CopyFrom(HEnvironment* environment);
1748
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001749 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1750 // input to the loop phi instead. This is for inserting instructions that
1751 // require an environment (like HDeoptimization) in the loop pre-header.
1752 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001753
1754 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001755 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001756 }
1757
1758 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001759 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001760 }
1761
David Brazdil1abb4192015-02-17 18:33:36 +00001762 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001763
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001764 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001765
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001766 HEnvironment* GetParent() const { return parent_; }
1767
1768 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001769 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001770 }
1771
1772 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001773 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001774 }
1775
1776 uint32_t GetDexPc() const {
1777 return dex_pc_;
1778 }
1779
1780 uint32_t GetMethodIdx() const {
1781 return method_idx_;
1782 }
1783
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001784 InvokeType GetInvokeType() const {
1785 return invoke_type_;
1786 }
1787
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001788 const DexFile& GetDexFile() const {
1789 return dex_file_;
1790 }
1791
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001792 HInstruction* GetHolder() const {
1793 return holder_;
1794 }
1795
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001796
1797 bool IsFromInlinedInvoke() const {
1798 return GetParent() != nullptr;
1799 }
1800
David Brazdiled596192015-01-23 10:39:45 +00001801 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001802 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1803 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001804 HEnvironment* parent_;
1805 const DexFile& dex_file_;
1806 const uint32_t method_idx_;
1807 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001808 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001809
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001810 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001811 HInstruction* const holder_;
1812
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001813 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001814
1815 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1816};
1817
Vladimir Markof9f64412015-09-02 14:05:49 +01001818class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001819 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001820 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001821 : previous_(nullptr),
1822 next_(nullptr),
1823 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001824 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001825 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001826 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001827 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001828 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001829 locations_(nullptr),
1830 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001831 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001832 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001833 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1834 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1835 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001836
Dave Allison20dfc792014-06-16 20:44:29 -07001837 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001838
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001839#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001840 enum InstructionKind {
1841 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1842 };
1843#undef DECLARE_KIND
1844
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001845 HInstruction* GetNext() const { return next_; }
1846 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001847
Calin Juravle77520bc2015-01-12 18:45:46 +00001848 HInstruction* GetNextDisregardingMoves() const;
1849 HInstruction* GetPreviousDisregardingMoves() const;
1850
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001851 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001852 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001853 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001854 bool IsInBlock() const { return block_ != nullptr; }
1855 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001856 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1857 bool IsIrreducibleLoopHeaderPhi() const {
1858 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1859 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001860
Vladimir Marko372f10e2016-05-17 16:30:10 +01001861 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1862
1863 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1864 // One virtual method is enough, just const_cast<> and then re-add the const.
1865 return ArrayRef<const HUserRecord<HInstruction*>>(
1866 const_cast<HInstruction*>(this)->GetInputRecords());
1867 }
1868
Vladimir Markoe9004912016-06-16 16:50:52 +01001869 HInputsRef GetInputs() {
1870 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001871 }
1872
Vladimir Markoe9004912016-06-16 16:50:52 +01001873 HConstInputsRef GetInputs() const {
1874 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001875 }
1876
1877 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001878 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001879
Aart Bik2767f4b2016-10-28 15:03:53 -07001880 bool HasInput(HInstruction* input) const {
1881 for (const HInstruction* i : GetInputs()) {
1882 if (i == input) {
1883 return true;
1884 }
1885 }
1886 return false;
1887 }
1888
Vladimir Marko372f10e2016-05-17 16:30:10 +01001889 void SetRawInputAt(size_t index, HInstruction* input) {
1890 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1891 }
1892
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001893 virtual void Accept(HGraphVisitor* visitor) = 0;
1894 virtual const char* DebugName() const = 0;
1895
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001896 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1897
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001898 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001899
1900 uint32_t GetDexPc() const { return dex_pc_; }
1901
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001902 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001903
Roland Levillaine161a2a2014-10-03 12:45:18 +01001904 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001905 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001906
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001907 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001908 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001909
Calin Juravle10e244f2015-01-26 18:54:32 +00001910 // Does not apply for all instructions, but having this at top level greatly
1911 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001912 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001913 virtual bool CanBeNull() const {
1914 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1915 return true;
1916 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001917
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001918 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001919 return false;
1920 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001921
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001922 virtual bool IsActualObject() const {
1923 return GetType() == Primitive::kPrimNot;
1924 }
1925
Calin Juravle2e768302015-07-28 14:41:11 +00001926 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001927
Calin Juravle61d544b2015-02-23 16:46:57 +00001928 ReferenceTypeInfo GetReferenceTypeInfo() const {
1929 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001930 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001931 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001932 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001933
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001934 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001935 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001936 // Note: fixup_end remains valid across push_front().
1937 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1938 HUseListNode<HInstruction*>* new_node =
1939 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1940 uses_.push_front(*new_node);
1941 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001942 }
1943
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001944 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001945 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001946 // Note: env_fixup_end remains valid across push_front().
1947 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1948 HUseListNode<HEnvironment*>* new_node =
1949 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1950 env_uses_.push_front(*new_node);
1951 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001952 }
1953
David Brazdil1abb4192015-02-17 18:33:36 +00001954 void RemoveAsUserOfInput(size_t input) {
1955 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001956 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1957 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1958 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001959 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001960
Vladimir Marko372f10e2016-05-17 16:30:10 +01001961 void RemoveAsUserOfAllInputs() {
1962 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1963 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1964 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1965 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1966 }
1967 }
1968
David Brazdil1abb4192015-02-17 18:33:36 +00001969 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1970 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001971
Vladimir Marko46817b82016-03-29 12:21:58 +01001972 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1973 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1974 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001975 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001976 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001977 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001978
Aart Bikcc42be02016-10-20 16:14:16 -07001979 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07001980 return
Aart Bikff7d89c2016-11-07 08:49:28 -08001981 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07001982 !CanThrow() &&
1983 !IsSuspendCheck() &&
1984 !IsControlFlow() &&
1985 !IsNativeDebugInfo() &&
1986 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07001987 // If we added an explicit barrier then we should keep it.
1988 !IsMemoryBarrier();
1989 }
1990
Aart Bikcc42be02016-10-20 16:14:16 -07001991 bool IsDeadAndRemovable() const {
1992 return IsRemovable() && !HasUses();
1993 }
1994
Roland Levillain6c82d402014-10-13 16:10:27 +01001995 // Does this instruction strictly dominate `other_instruction`?
1996 // Returns false if this instruction and `other_instruction` are the same.
1997 // Aborts if this instruction and `other_instruction` are both phis.
1998 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001999
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002000 int GetId() const { return id_; }
2001 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002002
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002003 int GetSsaIndex() const { return ssa_index_; }
2004 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2005 bool HasSsaIndex() const { return ssa_index_ != -1; }
2006
2007 bool HasEnvironment() const { return environment_ != nullptr; }
2008 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002009 // Set the `environment_` field. Raw because this method does not
2010 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002011 void SetRawEnvironment(HEnvironment* environment) {
2012 DCHECK(environment_ == nullptr);
2013 DCHECK_EQ(environment->GetHolder(), this);
2014 environment_ = environment;
2015 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002016
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002017 void InsertRawEnvironment(HEnvironment* environment) {
2018 DCHECK(environment_ != nullptr);
2019 DCHECK_EQ(environment->GetHolder(), this);
2020 DCHECK(environment->GetParent() == nullptr);
2021 environment->parent_ = environment_;
2022 environment_ = environment;
2023 }
2024
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002025 void RemoveEnvironment();
2026
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002027 // Set the environment of this instruction, copying it from `environment`. While
2028 // copying, the uses lists are being updated.
2029 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002030 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002031 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002032 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002033 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002034 if (environment->GetParent() != nullptr) {
2035 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2036 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002037 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002038
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002039 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2040 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002041 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002042 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002043 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002044 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002045 if (environment->GetParent() != nullptr) {
2046 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2047 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002048 }
2049
Nicolas Geoffray39468442014-09-02 15:17:15 +01002050 // Returns the number of entries in the environment. Typically, that is the
2051 // number of dex registers in a method. It could be more in case of inlining.
2052 size_t EnvironmentSize() const;
2053
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002054 LocationSummary* GetLocations() const { return locations_; }
2055 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002056
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002057 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002058 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002059
Alexandre Rames188d4312015-04-09 18:30:21 +01002060 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2061 // uses of this instruction by `other` are *not* updated.
2062 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2063 ReplaceWith(other);
2064 other->ReplaceInput(this, use_index);
2065 }
2066
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002067 // Move `this` instruction before `cursor`.
2068 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002069
Vladimir Markofb337ea2015-11-25 15:25:10 +00002070 // Move `this` before its first user and out of any loops. If there is no
2071 // out-of-loop user that dominates all other users, move the instruction
2072 // to the end of the out-of-loop common dominator of the user's blocks.
2073 //
2074 // This can be used only on non-throwing instructions with no side effects that
2075 // have at least one use but no environment uses.
2076 void MoveBeforeFirstUserAndOutOfLoops();
2077
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002078#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002079 bool Is##type() const; \
2080 const H##type* As##type() const; \
2081 H##type* As##type();
2082
2083 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2084#undef INSTRUCTION_TYPE_CHECK
2085
2086#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002087 bool Is##type() const { return (As##type() != nullptr); } \
2088 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002089 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002090 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002091#undef INSTRUCTION_TYPE_CHECK
2092
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002093 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002094 // TODO: this method is used by LICM and GVN with possibly different
2095 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002096 virtual bool CanBeMoved() const { return false; }
2097
2098 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002099 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002100 return false;
2101 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002102
2103 // Returns whether any data encoded in the two instructions is equal.
2104 // This method does not look at the inputs. Both instructions must be
2105 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002106 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002107 return false;
2108 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002109
2110 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002111 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002112 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002113 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002114
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002115 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2116 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2117 // the virtual function because the __attribute__((__pure__)) doesn't really
2118 // apply the strong requirement for virtual functions, preventing optimizations.
2119 InstructionKind GetKind() const PURE;
2120 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002121
2122 virtual size_t ComputeHashCode() const {
2123 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002124 for (const HInstruction* input : GetInputs()) {
2125 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002126 }
2127 return result;
2128 }
2129
2130 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002131 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002132 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002133
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002134 size_t GetLifetimePosition() const { return lifetime_position_; }
2135 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2136 LiveInterval* GetLiveInterval() const { return live_interval_; }
2137 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2138 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2139
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002140 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2141
2142 // Returns whether the code generation of the instruction will require to have access
2143 // to the current method. Such instructions are:
2144 // (1): Instructions that require an environment, as calling the runtime requires
2145 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002146 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2147 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002148 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002149 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002150 }
2151
Vladimir Markodc151b22015-10-15 18:02:30 +01002152 // Returns whether the code generation of the instruction will require to have access
2153 // to the dex cache of the current method's declaring class via the current method.
2154 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002155
Mark Mendellc4701932015-04-10 13:18:51 -04002156 // Does this instruction have any use in an environment before
2157 // control flow hits 'other'?
2158 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2159
2160 // Remove all references to environment uses of this instruction.
2161 // The caller must ensure that this is safe to do.
2162 void RemoveEnvironmentUsers();
2163
Vladimir Markoa1de9182016-02-25 11:37:38 +00002164 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2165 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002166
David Brazdil1abb4192015-02-17 18:33:36 +00002167 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002168 // If set, the machine code for this instruction is assumed to be generated by
2169 // its users. Used by liveness analysis to compute use positions accordingly.
2170 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2171 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2172 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2173 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2174
Vladimir Marko372f10e2016-05-17 16:30:10 +01002175 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2176 return GetInputRecords()[i];
2177 }
2178
2179 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2180 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2181 input_records[index] = input;
2182 }
David Brazdil1abb4192015-02-17 18:33:36 +00002183
Vladimir Markoa1de9182016-02-25 11:37:38 +00002184 uint32_t GetPackedFields() const {
2185 return packed_fields_;
2186 }
2187
2188 template <size_t flag>
2189 bool GetPackedFlag() const {
2190 return (packed_fields_ & (1u << flag)) != 0u;
2191 }
2192
2193 template <size_t flag>
2194 void SetPackedFlag(bool value = true) {
2195 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2196 }
2197
2198 template <typename BitFieldType>
2199 typename BitFieldType::value_type GetPackedField() const {
2200 return BitFieldType::Decode(packed_fields_);
2201 }
2202
2203 template <typename BitFieldType>
2204 void SetPackedField(typename BitFieldType::value_type value) {
2205 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2206 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2207 }
2208
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002209 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002210 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2211 auto before_use_node = uses_.before_begin();
2212 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2213 HInstruction* user = use_node->GetUser();
2214 size_t input_index = use_node->GetIndex();
2215 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2216 before_use_node = use_node;
2217 }
2218 }
2219
2220 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2221 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2222 if (next != uses_.end()) {
2223 HInstruction* next_user = next->GetUser();
2224 size_t next_index = next->GetIndex();
2225 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2226 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2227 }
2228 }
2229
2230 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2231 auto before_env_use_node = env_uses_.before_begin();
2232 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2233 HEnvironment* user = env_use_node->GetUser();
2234 size_t input_index = env_use_node->GetIndex();
2235 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2236 before_env_use_node = env_use_node;
2237 }
2238 }
2239
2240 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2241 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2242 if (next != env_uses_.end()) {
2243 HEnvironment* next_user = next->GetUser();
2244 size_t next_index = next->GetIndex();
2245 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2246 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2247 }
2248 }
David Brazdil1abb4192015-02-17 18:33:36 +00002249
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002250 HInstruction* previous_;
2251 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002252 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002253 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002254
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002255 // An instruction gets an id when it is added to the graph.
2256 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002257 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002258 int id_;
2259
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002260 // When doing liveness analysis, instructions that have uses get an SSA index.
2261 int ssa_index_;
2262
Vladimir Markoa1de9182016-02-25 11:37:38 +00002263 // Packed fields.
2264 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002265
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002266 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002267 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002268
2269 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002270 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002271
Nicolas Geoffray39468442014-09-02 15:17:15 +01002272 // The environment associated with this instruction. Not null if the instruction
2273 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002274 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002275
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002276 // Set by the code generator.
2277 LocationSummary* locations_;
2278
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002279 // Set by the liveness analysis.
2280 LiveInterval* live_interval_;
2281
2282 // Set by the liveness analysis, this is the position in a linear
2283 // order of blocks where this instruction's live interval start.
2284 size_t lifetime_position_;
2285
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002286 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002287
Vladimir Markoa1de9182016-02-25 11:37:38 +00002288 // The reference handle part of the reference type info.
2289 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002290 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002291 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002292
David Brazdil1abb4192015-02-17 18:33:36 +00002293 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002294 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002295 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002296 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002297 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002298
2299 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2300};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002301std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002302
2303class HInstructionIterator : public ValueObject {
2304 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002305 explicit HInstructionIterator(const HInstructionList& instructions)
2306 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002307 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002308 }
2309
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002310 bool Done() const { return instruction_ == nullptr; }
2311 HInstruction* Current() const { return instruction_; }
2312 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002313 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002314 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002315 }
2316
2317 private:
2318 HInstruction* instruction_;
2319 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002320
2321 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002322};
2323
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002324class HBackwardInstructionIterator : public ValueObject {
2325 public:
2326 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2327 : instruction_(instructions.last_instruction_) {
2328 next_ = Done() ? nullptr : instruction_->GetPrevious();
2329 }
2330
2331 bool Done() const { return instruction_ == nullptr; }
2332 HInstruction* Current() const { return instruction_; }
2333 void Advance() {
2334 instruction_ = next_;
2335 next_ = Done() ? nullptr : instruction_->GetPrevious();
2336 }
2337
2338 private:
2339 HInstruction* instruction_;
2340 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002341
2342 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002343};
2344
Vladimir Markof9f64412015-09-02 14:05:49 +01002345template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002346class HTemplateInstruction: public HInstruction {
2347 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002348 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002349 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002350 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002351
Vladimir Marko372f10e2016-05-17 16:30:10 +01002352 using HInstruction::GetInputRecords; // Keep the const version visible.
2353 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2354 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002355 }
2356
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002357 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002358 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002359
2360 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002361};
2362
Vladimir Markof9f64412015-09-02 14:05:49 +01002363// HTemplateInstruction specialization for N=0.
2364template<>
2365class HTemplateInstruction<0>: public HInstruction {
2366 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002367 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002368 : HInstruction(side_effects, dex_pc) {}
2369
Vladimir Markof9f64412015-09-02 14:05:49 +01002370 virtual ~HTemplateInstruction() {}
2371
Vladimir Marko372f10e2016-05-17 16:30:10 +01002372 using HInstruction::GetInputRecords; // Keep the const version visible.
2373 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2374 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002375 }
2376
2377 private:
2378 friend class SsaBuilder;
2379};
2380
Dave Allison20dfc792014-06-16 20:44:29 -07002381template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002382class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002383 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002384 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002385 : HTemplateInstruction<N>(side_effects, dex_pc) {
2386 this->template SetPackedField<TypeField>(type);
2387 }
Dave Allison20dfc792014-06-16 20:44:29 -07002388 virtual ~HExpression() {}
2389
Vladimir Markoa1de9182016-02-25 11:37:38 +00002390 Primitive::Type GetType() const OVERRIDE {
2391 return TypeField::Decode(this->GetPackedFields());
2392 }
Dave Allison20dfc792014-06-16 20:44:29 -07002393
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002394 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002395 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2396 static constexpr size_t kFieldTypeSize =
2397 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2398 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2399 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2400 "Too many packed fields.");
2401 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002402};
2403
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002404// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2405// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002406class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002407 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002408 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2409 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002410
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002411 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002412
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002413 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002414
2415 private:
2416 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2417};
2418
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002419// Represents dex's RETURN opcodes. A HReturn is a control flow
2420// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002421class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002422 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002423 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2424 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002425 SetRawInputAt(0, value);
2426 }
2427
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002428 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002429
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002430 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002431
2432 private:
2433 DISALLOW_COPY_AND_ASSIGN(HReturn);
2434};
2435
Vladimir Markofcb503c2016-05-18 12:48:17 +01002436class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002437 public:
2438 HPhi(ArenaAllocator* arena,
2439 uint32_t reg_number,
2440 size_t number_of_inputs,
2441 Primitive::Type type,
2442 uint32_t dex_pc = kNoDexPc)
2443 : HInstruction(SideEffects::None(), dex_pc),
2444 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2445 reg_number_(reg_number) {
2446 SetPackedField<TypeField>(ToPhiType(type));
2447 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2448 // Phis are constructed live and marked dead if conflicting or unused.
2449 // Individual steps of SsaBuilder should assume that if a phi has been
2450 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2451 SetPackedFlag<kFlagIsLive>(true);
2452 SetPackedFlag<kFlagCanBeNull>(true);
2453 }
2454
2455 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2456 static Primitive::Type ToPhiType(Primitive::Type type) {
2457 return Primitive::PrimitiveKind(type);
2458 }
2459
2460 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2461
Vladimir Marko372f10e2016-05-17 16:30:10 +01002462 using HInstruction::GetInputRecords; // Keep the const version visible.
2463 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2464 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2465 }
David Brazdildee58d62016-04-07 09:54:26 +00002466
2467 void AddInput(HInstruction* input);
2468 void RemoveInputAt(size_t index);
2469
2470 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2471 void SetType(Primitive::Type new_type) {
2472 // Make sure that only valid type changes occur. The following are allowed:
2473 // (1) int -> float/ref (primitive type propagation),
2474 // (2) long -> double (primitive type propagation).
2475 DCHECK(GetType() == new_type ||
2476 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2477 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2478 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2479 SetPackedField<TypeField>(new_type);
2480 }
2481
2482 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2483 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2484
2485 uint32_t GetRegNumber() const { return reg_number_; }
2486
2487 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2488 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2489 bool IsDead() const { return !IsLive(); }
2490 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2491
Vladimir Markoe9004912016-06-16 16:50:52 +01002492 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002493 return other != nullptr
2494 && other->IsPhi()
2495 && other->AsPhi()->GetBlock() == GetBlock()
2496 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2497 }
2498
2499 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2500 // An equivalent phi is a phi having the same dex register and type.
2501 // It assumes that phis with the same dex register are adjacent.
2502 HPhi* GetNextEquivalentPhiWithSameType() {
2503 HInstruction* next = GetNext();
2504 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2505 if (next->GetType() == GetType()) {
2506 return next->AsPhi();
2507 }
2508 next = next->GetNext();
2509 }
2510 return nullptr;
2511 }
2512
2513 DECLARE_INSTRUCTION(Phi);
2514
David Brazdildee58d62016-04-07 09:54:26 +00002515 private:
2516 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2517 static constexpr size_t kFieldTypeSize =
2518 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2519 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2520 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2521 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2522 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2523 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2524
Vladimir Marko372f10e2016-05-17 16:30:10 +01002525 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002526 const uint32_t reg_number_;
2527
2528 DISALLOW_COPY_AND_ASSIGN(HPhi);
2529};
2530
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002531// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002532// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002533// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002534class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002535 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002536 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002537
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002538 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002539
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002540 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002541
2542 private:
2543 DISALLOW_COPY_AND_ASSIGN(HExit);
2544};
2545
2546// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002547class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002548 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002549 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002550
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002551 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002552
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002553 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002554 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002555 }
2556
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002557 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002558
2559 private:
2560 DISALLOW_COPY_AND_ASSIGN(HGoto);
2561};
2562
Roland Levillain9867bc72015-08-05 10:21:34 +01002563class HConstant : public HExpression<0> {
2564 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002565 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2566 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002567
2568 bool CanBeMoved() const OVERRIDE { return true; }
2569
Roland Levillain1a653882016-03-18 18:05:57 +00002570 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002571 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002572 // Is this constant 0 in the arithmetic sense?
2573 virtual bool IsArithmeticZero() const { return false; }
2574 // Is this constant a 0-bit pattern?
2575 virtual bool IsZeroBitPattern() const { return false; }
2576 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002577 virtual bool IsOne() const { return false; }
2578
David Brazdil77a48ae2015-09-15 12:34:04 +00002579 virtual uint64_t GetValueAsUint64() const = 0;
2580
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002581 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002582
2583 private:
2584 DISALLOW_COPY_AND_ASSIGN(HConstant);
2585};
2586
Vladimir Markofcb503c2016-05-18 12:48:17 +01002587class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002588 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002589 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002590 return true;
2591 }
2592
David Brazdil77a48ae2015-09-15 12:34:04 +00002593 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2594
Roland Levillain9867bc72015-08-05 10:21:34 +01002595 size_t ComputeHashCode() const OVERRIDE { return 0; }
2596
Roland Levillain1a653882016-03-18 18:05:57 +00002597 // The null constant representation is a 0-bit pattern.
2598 virtual bool IsZeroBitPattern() const { return true; }
2599
Roland Levillain9867bc72015-08-05 10:21:34 +01002600 DECLARE_INSTRUCTION(NullConstant);
2601
2602 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002603 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002604
2605 friend class HGraph;
2606 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2607};
2608
2609// Constants of the type int. Those can be from Dex instructions, or
2610// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002611class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002612 public:
2613 int32_t GetValue() const { return value_; }
2614
David Brazdil9f389d42015-10-01 14:32:56 +01002615 uint64_t GetValueAsUint64() const OVERRIDE {
2616 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2617 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002618
Vladimir Marko372f10e2016-05-17 16:30:10 +01002619 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002620 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002621 return other->AsIntConstant()->value_ == value_;
2622 }
2623
2624 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2625
2626 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002627 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2628 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002629 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2630
Roland Levillain1a653882016-03-18 18:05:57 +00002631 // Integer constants are used to encode Boolean values as well,
2632 // where 1 means true and 0 means false.
2633 bool IsTrue() const { return GetValue() == 1; }
2634 bool IsFalse() const { return GetValue() == 0; }
2635
Roland Levillain9867bc72015-08-05 10:21:34 +01002636 DECLARE_INSTRUCTION(IntConstant);
2637
2638 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002639 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2640 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2641 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2642 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002643
2644 const int32_t value_;
2645
2646 friend class HGraph;
2647 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2648 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2649 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2650};
2651
Vladimir Markofcb503c2016-05-18 12:48:17 +01002652class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002653 public:
2654 int64_t GetValue() const { return value_; }
2655
David Brazdil77a48ae2015-09-15 12:34:04 +00002656 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2657
Vladimir Marko372f10e2016-05-17 16:30:10 +01002658 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002659 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002660 return other->AsLongConstant()->value_ == value_;
2661 }
2662
2663 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2664
2665 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002666 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2667 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002668 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2669
2670 DECLARE_INSTRUCTION(LongConstant);
2671
2672 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002673 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2674 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002675
2676 const int64_t value_;
2677
2678 friend class HGraph;
2679 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2680};
Dave Allison20dfc792014-06-16 20:44:29 -07002681
Vladimir Markofcb503c2016-05-18 12:48:17 +01002682class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002683 public:
2684 float GetValue() const { return value_; }
2685
2686 uint64_t GetValueAsUint64() const OVERRIDE {
2687 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2688 }
2689
Vladimir Marko372f10e2016-05-17 16:30:10 +01002690 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002691 DCHECK(other->IsFloatConstant()) << other->DebugName();
2692 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2693 }
2694
2695 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2696
2697 bool IsMinusOne() const OVERRIDE {
2698 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2699 }
Roland Levillain1a653882016-03-18 18:05:57 +00002700 bool IsArithmeticZero() const OVERRIDE {
2701 return std::fpclassify(value_) == FP_ZERO;
2702 }
2703 bool IsArithmeticPositiveZero() const {
2704 return IsArithmeticZero() && !std::signbit(value_);
2705 }
2706 bool IsArithmeticNegativeZero() const {
2707 return IsArithmeticZero() && std::signbit(value_);
2708 }
2709 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002710 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002711 }
2712 bool IsOne() const OVERRIDE {
2713 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2714 }
2715 bool IsNaN() const {
2716 return std::isnan(value_);
2717 }
2718
2719 DECLARE_INSTRUCTION(FloatConstant);
2720
2721 private:
2722 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2723 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2724 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2725 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2726
2727 const float value_;
2728
2729 // Only the SsaBuilder and HGraph can create floating-point constants.
2730 friend class SsaBuilder;
2731 friend class HGraph;
2732 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2733};
2734
Vladimir Markofcb503c2016-05-18 12:48:17 +01002735class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002736 public:
2737 double GetValue() const { return value_; }
2738
2739 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2740
Vladimir Marko372f10e2016-05-17 16:30:10 +01002741 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002742 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2743 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2744 }
2745
2746 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2747
2748 bool IsMinusOne() const OVERRIDE {
2749 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2750 }
Roland Levillain1a653882016-03-18 18:05:57 +00002751 bool IsArithmeticZero() const OVERRIDE {
2752 return std::fpclassify(value_) == FP_ZERO;
2753 }
2754 bool IsArithmeticPositiveZero() const {
2755 return IsArithmeticZero() && !std::signbit(value_);
2756 }
2757 bool IsArithmeticNegativeZero() const {
2758 return IsArithmeticZero() && std::signbit(value_);
2759 }
2760 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002761 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002762 }
2763 bool IsOne() const OVERRIDE {
2764 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2765 }
2766 bool IsNaN() const {
2767 return std::isnan(value_);
2768 }
2769
2770 DECLARE_INSTRUCTION(DoubleConstant);
2771
2772 private:
2773 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2774 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2775 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2776 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2777
2778 const double value_;
2779
2780 // Only the SsaBuilder and HGraph can create floating-point constants.
2781 friend class SsaBuilder;
2782 friend class HGraph;
2783 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2784};
2785
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002786// Conditional branch. A block ending with an HIf instruction must have
2787// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002788class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002789 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002790 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2791 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002792 SetRawInputAt(0, input);
2793 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002794
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002795 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002796
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002797 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002798 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002799 }
2800
2801 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002802 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002803 }
2804
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002805 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002806
2807 private:
2808 DISALLOW_COPY_AND_ASSIGN(HIf);
2809};
2810
David Brazdilfc6a86a2015-06-26 10:33:45 +00002811
2812// Abstract instruction which marks the beginning and/or end of a try block and
2813// links it to the respective exception handlers. Behaves the same as a Goto in
2814// non-exceptional control flow.
2815// Normal-flow successor is stored at index zero, exception handlers under
2816// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002817class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002818 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002819 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002820 kEntry,
2821 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002822 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002823 };
2824
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002825 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002826 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2827 SetPackedField<BoundaryKindField>(kind);
2828 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002829
2830 bool IsControlFlow() const OVERRIDE { return true; }
2831
2832 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002833 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002834
David Brazdild26a4112015-11-10 11:07:31 +00002835 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2836 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2837 }
2838
David Brazdilfc6a86a2015-06-26 10:33:45 +00002839 // Returns whether `handler` is among its exception handlers (non-zero index
2840 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002841 bool HasExceptionHandler(const HBasicBlock& handler) const {
2842 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002843 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002844 }
2845
2846 // If not present already, adds `handler` to its block's list of exception
2847 // handlers.
2848 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002849 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002850 GetBlock()->AddSuccessor(handler);
2851 }
2852 }
2853
Vladimir Markoa1de9182016-02-25 11:37:38 +00002854 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2855 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002856
David Brazdilffee3d32015-07-06 11:48:53 +01002857 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2858
David Brazdilfc6a86a2015-06-26 10:33:45 +00002859 DECLARE_INSTRUCTION(TryBoundary);
2860
2861 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002862 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2863 static constexpr size_t kFieldBoundaryKindSize =
2864 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2865 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2866 kFieldBoundaryKind + kFieldBoundaryKindSize;
2867 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2868 "Too many packed fields.");
2869 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002870
2871 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2872};
2873
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002874// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002875class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002876 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002877 // We set CanTriggerGC to prevent any intermediate address to be live
2878 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002879 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002880 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002881 SetRawInputAt(0, cond);
2882 }
2883
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002884 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002885 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002886 return true;
2887 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002888 bool NeedsEnvironment() const OVERRIDE { return true; }
2889 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002890
2891 DECLARE_INSTRUCTION(Deoptimize);
2892
2893 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002894 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2895};
2896
Mingyao Yang063fc772016-08-02 11:02:54 -07002897// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
2898// The compiled code checks this flag value in a guard before devirtualized call and
2899// if it's true, starts to do deoptimization.
2900// It has a 4-byte slot on stack.
2901// TODO: allocate a register for this flag.
2902class HShouldDeoptimizeFlag FINAL : public HExpression<0> {
2903 public:
2904 // TODO: use SideEffects to aid eliminating some CHA guards.
2905 explicit HShouldDeoptimizeFlag(uint32_t dex_pc)
2906 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
2907 }
2908
2909 // We don't eliminate CHA guards yet.
2910 bool CanBeMoved() const OVERRIDE { return false; }
2911
2912 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
2913
2914 private:
2915 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
2916};
2917
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002918// Represents the ArtMethod that was passed as a first argument to
2919// the method. It is used by instructions that depend on it, like
2920// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002921class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002922 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002923 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2924 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002925
2926 DECLARE_INSTRUCTION(CurrentMethod);
2927
2928 private:
2929 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2930};
2931
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002932// Fetches an ArtMethod from the virtual table or the interface method table
2933// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002934class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002935 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002936 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002937 kVTable,
2938 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002939 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002940 };
2941 HClassTableGet(HInstruction* cls,
2942 Primitive::Type type,
2943 TableKind kind,
2944 size_t index,
2945 uint32_t dex_pc)
2946 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002947 index_(index) {
2948 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002949 SetRawInputAt(0, cls);
2950 }
2951
2952 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002953 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002954 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002955 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002956 }
2957
Vladimir Markoa1de9182016-02-25 11:37:38 +00002958 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002959 size_t GetIndex() const { return index_; }
2960
2961 DECLARE_INSTRUCTION(ClassTableGet);
2962
2963 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002964 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2965 static constexpr size_t kFieldTableKindSize =
2966 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2967 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2968 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2969 "Too many packed fields.");
2970 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2971
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002972 // The index of the ArtMethod in the table.
2973 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002974
2975 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2976};
2977
Mark Mendellfe57faa2015-09-18 09:26:15 -04002978// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2979// have one successor for each entry in the switch table, and the final successor
2980// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002981class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002982 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002983 HPackedSwitch(int32_t start_value,
2984 uint32_t num_entries,
2985 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002986 uint32_t dex_pc = kNoDexPc)
2987 : HTemplateInstruction(SideEffects::None(), dex_pc),
2988 start_value_(start_value),
2989 num_entries_(num_entries) {
2990 SetRawInputAt(0, input);
2991 }
2992
2993 bool IsControlFlow() const OVERRIDE { return true; }
2994
2995 int32_t GetStartValue() const { return start_value_; }
2996
Vladimir Marko211c2112015-09-24 16:52:33 +01002997 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002998
2999 HBasicBlock* GetDefaultBlock() const {
3000 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003001 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003002 }
3003 DECLARE_INSTRUCTION(PackedSwitch);
3004
3005 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003006 const int32_t start_value_;
3007 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003008
3009 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3010};
3011
Roland Levillain88cb1752014-10-20 16:36:47 +01003012class HUnaryOperation : public HExpression<1> {
3013 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003014 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3015 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003016 SetRawInputAt(0, input);
3017 }
3018
3019 HInstruction* GetInput() const { return InputAt(0); }
3020 Primitive::Type GetResultType() const { return GetType(); }
3021
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003022 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003023 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003024 return true;
3025 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003026
Roland Levillain31dd3d62016-02-16 12:21:02 +00003027 // Try to statically evaluate `this` and return a HConstant
3028 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003029 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003030 HConstant* TryStaticEvaluation() const;
3031
3032 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003033 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3034 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003035 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3036 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003037
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003038 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003039
3040 private:
3041 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3042};
3043
Dave Allison20dfc792014-06-16 20:44:29 -07003044class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003045 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003046 HBinaryOperation(Primitive::Type result_type,
3047 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003048 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003049 SideEffects side_effects = SideEffects::None(),
3050 uint32_t dex_pc = kNoDexPc)
3051 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003052 SetRawInputAt(0, left);
3053 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003054 }
3055
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003056 HInstruction* GetLeft() const { return InputAt(0); }
3057 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003058 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003059
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003060 virtual bool IsCommutative() const { return false; }
3061
3062 // Put constant on the right.
3063 // Returns whether order is changed.
3064 bool OrderInputsWithConstantOnTheRight() {
3065 HInstruction* left = InputAt(0);
3066 HInstruction* right = InputAt(1);
3067 if (left->IsConstant() && !right->IsConstant()) {
3068 ReplaceInput(right, 0);
3069 ReplaceInput(left, 1);
3070 return true;
3071 }
3072 return false;
3073 }
3074
3075 // Order inputs by instruction id, but favor constant on the right side.
3076 // This helps GVN for commutative ops.
3077 void OrderInputs() {
3078 DCHECK(IsCommutative());
3079 HInstruction* left = InputAt(0);
3080 HInstruction* right = InputAt(1);
3081 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3082 return;
3083 }
3084 if (OrderInputsWithConstantOnTheRight()) {
3085 return;
3086 }
3087 // Order according to instruction id.
3088 if (left->GetId() > right->GetId()) {
3089 ReplaceInput(right, 0);
3090 ReplaceInput(left, 1);
3091 }
3092 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003093
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003094 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003095 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003096 return true;
3097 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003098
Roland Levillain31dd3d62016-02-16 12:21:02 +00003099 // Try to statically evaluate `this` and return a HConstant
3100 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003101 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003102 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003103
3104 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003105 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3106 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003107 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3108 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003109 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003110 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3111 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003112 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3113 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003114 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3115 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003116 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003117 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3118 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003119
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003120 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003121 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003122 HConstant* GetConstantRight() const;
3123
3124 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003125 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003126 HInstruction* GetLeastConstantLeft() const;
3127
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003128 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003129
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003130 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003131 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3132};
3133
Mark Mendellc4701932015-04-10 13:18:51 -04003134// The comparison bias applies for floating point operations and indicates how NaN
3135// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003136enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003137 kNoBias, // bias is not applicable (i.e. for long operation)
3138 kGtBias, // return 1 for NaN comparisons
3139 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003140 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003141};
3142
Roland Levillain31dd3d62016-02-16 12:21:02 +00003143std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3144
Dave Allison20dfc792014-06-16 20:44:29 -07003145class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003146 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003147 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003148 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3149 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3150 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003151
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003152 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003153 // `instruction`, and disregard moves in between.
3154 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003155
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003156 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003157
3158 virtual IfCondition GetCondition() const = 0;
3159
Mark Mendellc4701932015-04-10 13:18:51 -04003160 virtual IfCondition GetOppositeCondition() const = 0;
3161
Vladimir Markoa1de9182016-02-25 11:37:38 +00003162 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003163 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3164
Vladimir Markoa1de9182016-02-25 11:37:38 +00003165 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3166 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003167
Vladimir Marko372f10e2016-05-17 16:30:10 +01003168 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003169 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003170 }
3171
Roland Levillain4fa13f62015-07-06 18:11:54 +01003172 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003173 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003174 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003175 if (if_cond == kCondNE) {
3176 return true;
3177 } else if (if_cond == kCondEQ) {
3178 return false;
3179 }
3180 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003181 }
3182
3183 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003184 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003185 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003186 if (if_cond == kCondEQ) {
3187 return true;
3188 } else if (if_cond == kCondNE) {
3189 return false;
3190 }
3191 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003192 }
3193
Roland Levillain31dd3d62016-02-16 12:21:02 +00003194 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003195 // Needed if we merge a HCompare into a HCondition.
3196 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3197 static constexpr size_t kFieldComparisonBiasSize =
3198 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3199 static constexpr size_t kNumberOfConditionPackedBits =
3200 kFieldComparisonBias + kFieldComparisonBiasSize;
3201 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3202 using ComparisonBiasField =
3203 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3204
Roland Levillain31dd3d62016-02-16 12:21:02 +00003205 template <typename T>
3206 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3207
3208 template <typename T>
3209 int32_t CompareFP(T x, T y) const {
3210 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3211 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3212 // Handle the bias.
3213 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3214 }
3215
3216 // Return an integer constant containing the result of a condition evaluated at compile time.
3217 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3218 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3219 }
3220
Dave Allison20dfc792014-06-16 20:44:29 -07003221 private:
3222 DISALLOW_COPY_AND_ASSIGN(HCondition);
3223};
3224
3225// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003226class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003227 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003228 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3229 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003230
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003231 bool IsCommutative() const OVERRIDE { return true; }
3232
Vladimir Marko9e23df52015-11-10 17:14:35 +00003233 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3234 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003235 return MakeConstantCondition(true, GetDexPc());
3236 }
3237 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3238 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3239 }
3240 // In the following Evaluate methods, a HCompare instruction has
3241 // been merged into this HEqual instruction; evaluate it as
3242 // `Compare(x, y) == 0`.
3243 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3244 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3245 GetDexPc());
3246 }
3247 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3248 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3249 }
3250 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3251 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003252 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003253
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003254 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003255
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003256 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003257 return kCondEQ;
3258 }
3259
Mark Mendellc4701932015-04-10 13:18:51 -04003260 IfCondition GetOppositeCondition() const OVERRIDE {
3261 return kCondNE;
3262 }
3263
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003264 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003265 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003266
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003267 DISALLOW_COPY_AND_ASSIGN(HEqual);
3268};
3269
Vladimir Markofcb503c2016-05-18 12:48:17 +01003270class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003271 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003272 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3273 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003274
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003275 bool IsCommutative() const OVERRIDE { return true; }
3276
Vladimir Marko9e23df52015-11-10 17:14:35 +00003277 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3278 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003279 return MakeConstantCondition(false, GetDexPc());
3280 }
3281 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3282 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3283 }
3284 // In the following Evaluate methods, a HCompare instruction has
3285 // been merged into this HNotEqual instruction; evaluate it as
3286 // `Compare(x, y) != 0`.
3287 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3288 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3289 }
3290 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3291 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3292 }
3293 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3294 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003295 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003296
Dave Allison20dfc792014-06-16 20:44:29 -07003297 DECLARE_INSTRUCTION(NotEqual);
3298
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003299 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003300 return kCondNE;
3301 }
3302
Mark Mendellc4701932015-04-10 13:18:51 -04003303 IfCondition GetOppositeCondition() const OVERRIDE {
3304 return kCondEQ;
3305 }
3306
Dave Allison20dfc792014-06-16 20:44:29 -07003307 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003308 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003309
Dave Allison20dfc792014-06-16 20:44:29 -07003310 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3311};
3312
Vladimir Markofcb503c2016-05-18 12:48:17 +01003313class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003314 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003315 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3316 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003317
Roland Levillain9867bc72015-08-05 10:21:34 +01003318 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003319 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003320 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003321 // In the following Evaluate methods, a HCompare instruction has
3322 // been merged into this HLessThan instruction; evaluate it as
3323 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003324 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003325 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3326 }
3327 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3328 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3329 }
3330 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3331 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003332 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003333
Dave Allison20dfc792014-06-16 20:44:29 -07003334 DECLARE_INSTRUCTION(LessThan);
3335
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003336 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003337 return kCondLT;
3338 }
3339
Mark Mendellc4701932015-04-10 13:18:51 -04003340 IfCondition GetOppositeCondition() const OVERRIDE {
3341 return kCondGE;
3342 }
3343
Dave Allison20dfc792014-06-16 20:44:29 -07003344 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003345 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003346
Dave Allison20dfc792014-06-16 20:44:29 -07003347 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3348};
3349
Vladimir Markofcb503c2016-05-18 12:48:17 +01003350class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003351 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003352 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3353 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003354
Roland Levillain9867bc72015-08-05 10:21:34 +01003355 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003356 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003357 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003358 // In the following Evaluate methods, a HCompare instruction has
3359 // been merged into this HLessThanOrEqual instruction; evaluate it as
3360 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003361 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003362 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3363 }
3364 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3365 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3366 }
3367 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3368 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003369 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003370
Dave Allison20dfc792014-06-16 20:44:29 -07003371 DECLARE_INSTRUCTION(LessThanOrEqual);
3372
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003373 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003374 return kCondLE;
3375 }
3376
Mark Mendellc4701932015-04-10 13:18:51 -04003377 IfCondition GetOppositeCondition() const OVERRIDE {
3378 return kCondGT;
3379 }
3380
Dave Allison20dfc792014-06-16 20:44:29 -07003381 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003382 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003383
Dave Allison20dfc792014-06-16 20:44:29 -07003384 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3385};
3386
Vladimir Markofcb503c2016-05-18 12:48:17 +01003387class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003388 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003389 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3390 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003391
Roland Levillain9867bc72015-08-05 10:21:34 +01003392 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003393 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003394 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003395 // In the following Evaluate methods, a HCompare instruction has
3396 // been merged into this HGreaterThan instruction; evaluate it as
3397 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003398 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003399 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3400 }
3401 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3402 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3403 }
3404 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3405 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003406 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003407
Dave Allison20dfc792014-06-16 20:44:29 -07003408 DECLARE_INSTRUCTION(GreaterThan);
3409
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003410 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003411 return kCondGT;
3412 }
3413
Mark Mendellc4701932015-04-10 13:18:51 -04003414 IfCondition GetOppositeCondition() const OVERRIDE {
3415 return kCondLE;
3416 }
3417
Dave Allison20dfc792014-06-16 20:44:29 -07003418 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003419 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003420
Dave Allison20dfc792014-06-16 20:44:29 -07003421 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3422};
3423
Vladimir Markofcb503c2016-05-18 12:48:17 +01003424class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003425 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003426 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3427 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003428
Roland Levillain9867bc72015-08-05 10:21:34 +01003429 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003430 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003431 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003432 // In the following Evaluate methods, a HCompare instruction has
3433 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3434 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003435 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003436 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3437 }
3438 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3439 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3440 }
3441 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3442 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003443 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003444
Dave Allison20dfc792014-06-16 20:44:29 -07003445 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3446
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003447 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003448 return kCondGE;
3449 }
3450
Mark Mendellc4701932015-04-10 13:18:51 -04003451 IfCondition GetOppositeCondition() const OVERRIDE {
3452 return kCondLT;
3453 }
3454
Dave Allison20dfc792014-06-16 20:44:29 -07003455 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003456 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003457
Dave Allison20dfc792014-06-16 20:44:29 -07003458 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3459};
3460
Vladimir Markofcb503c2016-05-18 12:48:17 +01003461class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003462 public:
3463 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3464 : HCondition(first, second, dex_pc) {}
3465
3466 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003467 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003468 }
3469 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003470 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3471 }
3472 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3473 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3474 LOG(FATAL) << DebugName() << " is not defined for float values";
3475 UNREACHABLE();
3476 }
3477 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3478 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3479 LOG(FATAL) << DebugName() << " is not defined for double values";
3480 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003481 }
3482
3483 DECLARE_INSTRUCTION(Below);
3484
3485 IfCondition GetCondition() const OVERRIDE {
3486 return kCondB;
3487 }
3488
3489 IfCondition GetOppositeCondition() const OVERRIDE {
3490 return kCondAE;
3491 }
3492
3493 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003494 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003495 return MakeUnsigned(x) < MakeUnsigned(y);
3496 }
Aart Bike9f37602015-10-09 11:15:55 -07003497
3498 DISALLOW_COPY_AND_ASSIGN(HBelow);
3499};
3500
Vladimir Markofcb503c2016-05-18 12:48:17 +01003501class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003502 public:
3503 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3504 : HCondition(first, second, dex_pc) {}
3505
3506 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003507 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003508 }
3509 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003510 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3511 }
3512 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3513 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3514 LOG(FATAL) << DebugName() << " is not defined for float values";
3515 UNREACHABLE();
3516 }
3517 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3518 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3519 LOG(FATAL) << DebugName() << " is not defined for double values";
3520 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003521 }
3522
3523 DECLARE_INSTRUCTION(BelowOrEqual);
3524
3525 IfCondition GetCondition() const OVERRIDE {
3526 return kCondBE;
3527 }
3528
3529 IfCondition GetOppositeCondition() const OVERRIDE {
3530 return kCondA;
3531 }
3532
3533 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003534 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003535 return MakeUnsigned(x) <= MakeUnsigned(y);
3536 }
Aart Bike9f37602015-10-09 11:15:55 -07003537
3538 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3539};
3540
Vladimir Markofcb503c2016-05-18 12:48:17 +01003541class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003542 public:
3543 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3544 : HCondition(first, second, dex_pc) {}
3545
3546 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003547 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003548 }
3549 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003550 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3551 }
3552 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3553 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3554 LOG(FATAL) << DebugName() << " is not defined for float values";
3555 UNREACHABLE();
3556 }
3557 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3558 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3559 LOG(FATAL) << DebugName() << " is not defined for double values";
3560 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003561 }
3562
3563 DECLARE_INSTRUCTION(Above);
3564
3565 IfCondition GetCondition() const OVERRIDE {
3566 return kCondA;
3567 }
3568
3569 IfCondition GetOppositeCondition() const OVERRIDE {
3570 return kCondBE;
3571 }
3572
3573 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003574 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003575 return MakeUnsigned(x) > MakeUnsigned(y);
3576 }
Aart Bike9f37602015-10-09 11:15:55 -07003577
3578 DISALLOW_COPY_AND_ASSIGN(HAbove);
3579};
3580
Vladimir Markofcb503c2016-05-18 12:48:17 +01003581class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003582 public:
3583 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3584 : HCondition(first, second, dex_pc) {}
3585
3586 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003587 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003588 }
3589 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003590 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3591 }
3592 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3593 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3594 LOG(FATAL) << DebugName() << " is not defined for float values";
3595 UNREACHABLE();
3596 }
3597 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3598 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3599 LOG(FATAL) << DebugName() << " is not defined for double values";
3600 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003601 }
3602
3603 DECLARE_INSTRUCTION(AboveOrEqual);
3604
3605 IfCondition GetCondition() const OVERRIDE {
3606 return kCondAE;
3607 }
3608
3609 IfCondition GetOppositeCondition() const OVERRIDE {
3610 return kCondB;
3611 }
3612
3613 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003614 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003615 return MakeUnsigned(x) >= MakeUnsigned(y);
3616 }
Aart Bike9f37602015-10-09 11:15:55 -07003617
3618 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3619};
Dave Allison20dfc792014-06-16 20:44:29 -07003620
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003621// Instruction to check how two inputs compare to each other.
3622// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003623class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003624 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003625 // Note that `comparison_type` is the type of comparison performed
3626 // between the comparison's inputs, not the type of the instantiated
3627 // HCompare instruction (which is always Primitive::kPrimInt).
3628 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003629 HInstruction* first,
3630 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003631 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003632 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003633 : HBinaryOperation(Primitive::kPrimInt,
3634 first,
3635 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003636 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003637 dex_pc) {
3638 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003639 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3640 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003641 }
3642
Roland Levillain9867bc72015-08-05 10:21:34 +01003643 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003644 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3645
3646 template <typename T>
3647 int32_t ComputeFP(T x, T y) const {
3648 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3649 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3650 // Handle the bias.
3651 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3652 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003653
Roland Levillain9867bc72015-08-05 10:21:34 +01003654 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003655 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3656 // reach this code path when processing a freshly built HIR
3657 // graph. However HCompare integer instructions can be synthesized
3658 // by the instruction simplifier to implement IntegerCompare and
3659 // IntegerSignum intrinsics, so we have to handle this case.
3660 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003661 }
3662 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003663 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3664 }
3665 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3666 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3667 }
3668 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3669 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003670 }
3671
Vladimir Marko372f10e2016-05-17 16:30:10 +01003672 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003673 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003674 }
3675
Vladimir Markoa1de9182016-02-25 11:37:38 +00003676 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003677
Roland Levillain31dd3d62016-02-16 12:21:02 +00003678 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003679 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003680 bool IsGtBias() const {
3681 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003682 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003683 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003684
Roland Levillain1693a1f2016-03-15 14:57:31 +00003685 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3686 // Comparisons do not require a runtime call in any back end.
3687 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003688 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003689
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003690 DECLARE_INSTRUCTION(Compare);
3691
Roland Levillain31dd3d62016-02-16 12:21:02 +00003692 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003693 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3694 static constexpr size_t kFieldComparisonBiasSize =
3695 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3696 static constexpr size_t kNumberOfComparePackedBits =
3697 kFieldComparisonBias + kFieldComparisonBiasSize;
3698 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3699 using ComparisonBiasField =
3700 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3701
Roland Levillain31dd3d62016-02-16 12:21:02 +00003702 // Return an integer constant containing the result of a comparison evaluated at compile time.
3703 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3704 DCHECK(value == -1 || value == 0 || value == 1) << value;
3705 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3706 }
3707
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003708 private:
3709 DISALLOW_COPY_AND_ASSIGN(HCompare);
3710};
3711
Vladimir Markofcb503c2016-05-18 12:48:17 +01003712class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003713 public:
3714 HNewInstance(HInstruction* cls,
3715 HCurrentMethod* current_method,
3716 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003717 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003718 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003719 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003720 bool finalizable,
3721 QuickEntrypointEnum entrypoint)
3722 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3723 type_index_(type_index),
3724 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003725 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003726 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003727 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003728 SetRawInputAt(0, cls);
3729 SetRawInputAt(1, current_method);
3730 }
3731
Andreas Gampea5b09a62016-11-17 15:21:22 -08003732 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003733 const DexFile& GetDexFile() const { return dex_file_; }
3734
3735 // Calls runtime so needs an environment.
3736 bool NeedsEnvironment() const OVERRIDE { return true; }
3737
Mingyao Yang062157f2016-03-02 10:15:36 -08003738 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3739 bool CanThrow() const OVERRIDE { return true; }
3740
3741 // Needs to call into runtime to make sure it's instantiable/accessible.
3742 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003743
Vladimir Markoa1de9182016-02-25 11:37:38 +00003744 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003745
3746 bool CanBeNull() const OVERRIDE { return false; }
3747
3748 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3749
3750 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3751 entrypoint_ = entrypoint;
3752 }
3753
3754 bool IsStringAlloc() const;
3755
3756 DECLARE_INSTRUCTION(NewInstance);
3757
3758 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003759 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3760 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003761 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3762 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3763 "Too many packed fields.");
3764
Andreas Gampea5b09a62016-11-17 15:21:22 -08003765 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003766 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003767 QuickEntrypointEnum entrypoint_;
3768
3769 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3770};
3771
Agi Csaki05f20562015-08-19 14:58:14 -07003772enum IntrinsicNeedsEnvironmentOrCache {
3773 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3774 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003775};
3776
Aart Bik5d75afe2015-12-14 11:57:01 -08003777enum IntrinsicSideEffects {
3778 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3779 kReadSideEffects, // Intrinsic may read heap memory.
3780 kWriteSideEffects, // Intrinsic may write heap memory.
3781 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3782};
3783
3784enum IntrinsicExceptions {
3785 kNoThrow, // Intrinsic does not throw any exceptions.
3786 kCanThrow // Intrinsic may throw exceptions.
3787};
3788
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003789class HInvoke : public HInstruction {
3790 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003791 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003792
Vladimir Marko372f10e2016-05-17 16:30:10 +01003793 using HInstruction::GetInputRecords; // Keep the const version visible.
3794 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3795 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3796 }
3797
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003798 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003799 SetRawInputAt(index, argument);
3800 }
3801
Roland Levillain3e3d7332015-04-28 11:00:54 +01003802 // Return the number of arguments. This number can be lower than
3803 // the number of inputs returned by InputCount(), as some invoke
3804 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3805 // inputs at the end of their list of inputs.
3806 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3807
Vladimir Markoa1de9182016-02-25 11:37:38 +00003808 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003809
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003810 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003811 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003812
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003813 InvokeType GetInvokeType() const {
3814 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003815 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003816
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003817 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003818 return intrinsic_;
3819 }
3820
Aart Bik5d75afe2015-12-14 11:57:01 -08003821 void SetIntrinsic(Intrinsics intrinsic,
3822 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3823 IntrinsicSideEffects side_effects,
3824 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003825
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003826 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003827 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003828 }
3829
Aart Bikff7d89c2016-11-07 08:49:28 -08003830 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
3831
Vladimir Markoa1de9182016-02-25 11:37:38 +00003832 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003833
Aart Bik71bf7b42016-11-16 10:17:46 -08003834 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003835
Vladimir Marko372f10e2016-05-17 16:30:10 +01003836 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003837 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3838 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003839
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003840 uint32_t* GetIntrinsicOptimizations() {
3841 return &intrinsic_optimizations_;
3842 }
3843
3844 const uint32_t* GetIntrinsicOptimizations() const {
3845 return &intrinsic_optimizations_;
3846 }
3847
3848 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3849
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003850 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3851
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003852 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003853
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003854 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003855 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3856 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003857 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3858 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003859 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003860 static constexpr size_t kFieldReturnTypeSize =
3861 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3862 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3863 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3864 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003865 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003866 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3867
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003868 HInvoke(ArenaAllocator* arena,
3869 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003870 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003871 Primitive::Type return_type,
3872 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003873 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003874 ArtMethod* resolved_method,
3875 InvokeType invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003876 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003877 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003878 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003879 resolved_method_(resolved_method),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003880 inputs_(number_of_arguments + number_of_other_inputs,
3881 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003882 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003883 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003884 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003885 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003886 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003887 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003888 }
3889
Roland Levillain3e3d7332015-04-28 11:00:54 +01003890 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003891 ArtMethod* const resolved_method_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003892 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003893 const uint32_t dex_method_index_;
3894 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003895
3896 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3897 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003898
3899 private:
3900 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3901};
3902
Vladimir Markofcb503c2016-05-18 12:48:17 +01003903class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003904 public:
3905 HInvokeUnresolved(ArenaAllocator* arena,
3906 uint32_t number_of_arguments,
3907 Primitive::Type return_type,
3908 uint32_t dex_pc,
3909 uint32_t dex_method_index,
3910 InvokeType invoke_type)
3911 : HInvoke(arena,
3912 number_of_arguments,
3913 0u /* number_of_other_inputs */,
3914 return_type,
3915 dex_pc,
3916 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003917 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003918 invoke_type) {
3919 }
3920
3921 DECLARE_INSTRUCTION(InvokeUnresolved);
3922
3923 private:
3924 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3925};
3926
Vladimir Markofcb503c2016-05-18 12:48:17 +01003927class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003928 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003929 // Requirements of this method call regarding the class
3930 // initialization (clinit) check of its declaring class.
3931 enum class ClinitCheckRequirement {
3932 kNone, // Class already initialized.
3933 kExplicit, // Static call having explicit clinit check as last input.
3934 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003935 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003936 };
3937
Vladimir Marko58155012015-08-19 12:49:41 +00003938 // Determines how to load the target ArtMethod*.
3939 enum class MethodLoadKind {
3940 // Use a String init ArtMethod* loaded from Thread entrypoints.
3941 kStringInit,
3942
3943 // Use the method's own ArtMethod* loaded by the register allocator.
3944 kRecursive,
3945
3946 // Use ArtMethod* at a known address, embed the direct address in the code.
3947 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3948 kDirectAddress,
3949
3950 // Use ArtMethod* at an address that will be known at link time, embed the direct
3951 // address in the code. If the image is relocatable, emit .patch_oat entry.
3952 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3953 // the image relocatable or not.
3954 kDirectAddressWithFixup,
3955
Vladimir Markoa1de9182016-02-25 11:37:38 +00003956 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003957 // Used when we need to use the dex cache, for example for invoke-static that
3958 // may cause class initialization (the entry may point to a resolution method),
3959 // and we know that we can access the dex cache arrays using a PC-relative load.
3960 kDexCachePcRelative,
3961
3962 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3963 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3964 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3965 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3966 kDexCacheViaMethod,
3967 };
3968
3969 // Determines the location of the code pointer.
3970 enum class CodePtrLocation {
3971 // Recursive call, use local PC-relative call instruction.
3972 kCallSelf,
3973
3974 // Use PC-relative call instruction patched at link time.
3975 // Used for calls within an oat file, boot->boot or app->app.
3976 kCallPCRelative,
3977
3978 // Call to a known target address, embed the direct address in code.
3979 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3980 kCallDirect,
3981
3982 // Call to a target address that will be known at link time, embed the direct
3983 // address in code. If the image is relocatable, emit .patch_oat entry.
3984 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3985 // the image relocatable or not.
3986 kCallDirectWithFixup,
3987
3988 // Use code pointer from the ArtMethod*.
3989 // Used when we don't know the target code. This is also the last-resort-kind used when
3990 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3991 kCallArtMethod,
3992 };
3993
3994 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003995 MethodLoadKind method_load_kind;
3996 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003997 // The method load data holds
3998 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3999 // Note that there are multiple string init methods, each having its own offset.
4000 // - the method address for kDirectAddress
4001 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004002 uint64_t method_load_data;
4003 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00004004 };
4005
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004006 HInvokeStaticOrDirect(ArenaAllocator* arena,
4007 uint32_t number_of_arguments,
4008 Primitive::Type return_type,
4009 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004010 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004011 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004012 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004013 InvokeType invoke_type,
4014 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004015 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004016 : HInvoke(arena,
4017 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004018 // There is potentially one extra argument for the HCurrentMethod node, and
4019 // potentially one other if the clinit check is explicit, and potentially
4020 // one other if the method is a string factory.
4021 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004022 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004023 return_type,
4024 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004025 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004026 resolved_method,
4027 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004028 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004029 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004030 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4031 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004032
Vladimir Markodc151b22015-10-15 18:02:30 +01004033 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004034 bool had_current_method_input = HasCurrentMethodInput();
4035 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4036
4037 // Using the current method is the default and once we find a better
4038 // method load kind, we should not go back to using the current method.
4039 DCHECK(had_current_method_input || !needs_current_method_input);
4040
4041 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004042 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4043 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004044 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004045 dispatch_info_ = dispatch_info;
4046 }
4047
Vladimir Markoc53c0792015-11-19 15:48:33 +00004048 void AddSpecialInput(HInstruction* input) {
4049 // We allow only one special input.
4050 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4051 DCHECK(InputCount() == GetSpecialInputIndex() ||
4052 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4053 InsertInputAt(GetSpecialInputIndex(), input);
4054 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004055
Vladimir Marko372f10e2016-05-17 16:30:10 +01004056 using HInstruction::GetInputRecords; // Keep the const version visible.
4057 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4058 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4059 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4060 DCHECK(!input_records.empty());
4061 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4062 HInstruction* last_input = input_records.back().GetInstruction();
4063 // Note: `last_input` may be null during arguments setup.
4064 if (last_input != nullptr) {
4065 // `last_input` is the last input of a static invoke marked as having
4066 // an explicit clinit check. It must either be:
4067 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4068 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4069 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4070 }
4071 }
4072 return input_records;
4073 }
4074
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004075 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004076 // We access the method via the dex cache so we can't do an implicit null check.
4077 // TODO: for intrinsics we can generate implicit null checks.
4078 return false;
4079 }
4080
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004081 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004082 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004083 }
4084
Vladimir Markoc53c0792015-11-19 15:48:33 +00004085 // Get the index of the special input, if any.
4086 //
David Brazdil6de19382016-01-08 17:37:10 +00004087 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4088 // method pointer; otherwise there may be one platform-specific special input,
4089 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004090 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004091 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004092
Vladimir Marko58155012015-08-19 12:49:41 +00004093 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4094 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4095 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004096 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004097 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004098 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004099 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004100 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4101 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004102 bool HasCurrentMethodInput() const {
4103 // This function can be called only after the invoke has been fully initialized by the builder.
4104 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004105 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004106 return true;
4107 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004108 DCHECK(InputCount() == GetSpecialInputIndex() ||
4109 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004110 return false;
4111 }
4112 }
Vladimir Marko58155012015-08-19 12:49:41 +00004113 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
Vladimir Marko58155012015-08-19 12:49:41 +00004114
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004115 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004116 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004117 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004118 }
4119
4120 uint64_t GetMethodAddress() const {
4121 DCHECK(HasMethodAddress());
4122 return dispatch_info_.method_load_data;
4123 }
4124
4125 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004126 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004127 return dispatch_info_.method_load_data;
4128 }
4129
4130 uint64_t GetDirectCodePtr() const {
4131 DCHECK(HasDirectCodePtr());
4132 return dispatch_info_.direct_code_ptr;
4133 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004134
Vladimir Markoa1de9182016-02-25 11:37:38 +00004135 ClinitCheckRequirement GetClinitCheckRequirement() const {
4136 return GetPackedField<ClinitCheckRequirementField>();
4137 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004138
Roland Levillain4c0eb422015-04-24 16:43:49 +01004139 // Is this instruction a call to a static method?
4140 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004141 return GetInvokeType() == kStatic;
4142 }
4143
4144 MethodReference GetTargetMethod() const {
4145 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004146 }
4147
Vladimir Markofbb184a2015-11-13 14:47:00 +00004148 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4149 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4150 // instruction; only relevant for static calls with explicit clinit check.
4151 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004152 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004153 size_t last_input_index = inputs_.size() - 1u;
4154 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004155 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004156 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004157 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004158 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004159 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004160 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004161 }
4162
4163 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004164 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004165 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004166 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004167 }
4168
4169 // Is this a call to a static method whose declaring class has an
4170 // implicit intialization check requirement?
4171 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004172 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004173 }
4174
Vladimir Markob554b5a2015-11-06 12:57:55 +00004175 // Does this method load kind need the current method as an input?
4176 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4177 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4178 }
4179
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004180 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004181
Roland Levillain4c0eb422015-04-24 16:43:49 +01004182 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004183 void InsertInputAt(size_t index, HInstruction* input);
4184 void RemoveInputAt(size_t index);
4185
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004186 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004187 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004188 static constexpr size_t kFieldClinitCheckRequirementSize =
4189 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4190 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4191 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4192 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4193 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004194 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4195 kFieldClinitCheckRequirement,
4196 kFieldClinitCheckRequirementSize>;
4197
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004198 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004199 MethodReference target_method_;
4200 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004201
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004202 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004203};
Vladimir Markof64242a2015-12-01 14:58:23 +00004204std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004205std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004206
Vladimir Markofcb503c2016-05-18 12:48:17 +01004207class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004208 public:
4209 HInvokeVirtual(ArenaAllocator* arena,
4210 uint32_t number_of_arguments,
4211 Primitive::Type return_type,
4212 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004213 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004214 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004215 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004216 : HInvoke(arena,
4217 number_of_arguments,
4218 0u,
4219 return_type,
4220 dex_pc,
4221 dex_method_index,
4222 resolved_method,
4223 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004224 vtable_index_(vtable_index) {}
4225
Aart Bik71bf7b42016-11-16 10:17:46 -08004226 bool CanBeNull() const OVERRIDE {
4227 switch (GetIntrinsic()) {
4228 case Intrinsics::kThreadCurrentThread:
4229 case Intrinsics::kStringBufferAppend:
4230 case Intrinsics::kStringBufferToString:
4231 case Intrinsics::kStringBuilderAppend:
4232 case Intrinsics::kStringBuilderToString:
4233 return false;
4234 default:
4235 return HInvoke::CanBeNull();
4236 }
4237 }
4238
Calin Juravle641547a2015-04-21 22:08:51 +01004239 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004240 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004241 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004242 }
4243
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004244 uint32_t GetVTableIndex() const { return vtable_index_; }
4245
4246 DECLARE_INSTRUCTION(InvokeVirtual);
4247
4248 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004249 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004250 const uint32_t vtable_index_;
4251
4252 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4253};
4254
Vladimir Markofcb503c2016-05-18 12:48:17 +01004255class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004256 public:
4257 HInvokeInterface(ArenaAllocator* arena,
4258 uint32_t number_of_arguments,
4259 Primitive::Type return_type,
4260 uint32_t dex_pc,
4261 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004262 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004263 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004264 : HInvoke(arena,
4265 number_of_arguments,
4266 0u,
4267 return_type,
4268 dex_pc,
4269 dex_method_index,
4270 resolved_method,
4271 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004272 imt_index_(imt_index) {}
4273
Calin Juravle641547a2015-04-21 22:08:51 +01004274 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004275 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004276 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004277 }
4278
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004279 uint32_t GetImtIndex() const { return imt_index_; }
4280 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4281
4282 DECLARE_INSTRUCTION(InvokeInterface);
4283
4284 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004285 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004286 const uint32_t imt_index_;
4287
4288 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4289};
4290
Vladimir Markofcb503c2016-05-18 12:48:17 +01004291class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004292 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004293 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004294 : HUnaryOperation(result_type, input, dex_pc) {
4295 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4296 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004297
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004298 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004299
4300 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004301 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004302 }
4303 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004304 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004305 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004306 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4307 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4308 }
4309 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4310 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4311 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004312
Roland Levillain88cb1752014-10-20 16:36:47 +01004313 DECLARE_INSTRUCTION(Neg);
4314
4315 private:
4316 DISALLOW_COPY_AND_ASSIGN(HNeg);
4317};
4318
Vladimir Markofcb503c2016-05-18 12:48:17 +01004319class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004320 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004321 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004322 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004323 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004324 dex::TypeIndex type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004325 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004326 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004327 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004328 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004329 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004330 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004331 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004332 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004333 }
4334
Andreas Gampea5b09a62016-11-17 15:21:22 -08004335 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004336 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004337
4338 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004339 bool NeedsEnvironment() const OVERRIDE { return true; }
4340
Mingyao Yang0c365e62015-03-31 15:09:29 -07004341 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4342 bool CanThrow() const OVERRIDE { return true; }
4343
Calin Juravle10e244f2015-01-26 18:54:32 +00004344 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004345
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004346 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4347
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004348 DECLARE_INSTRUCTION(NewArray);
4349
4350 private:
Andreas Gampea5b09a62016-11-17 15:21:22 -08004351 const dex::TypeIndex type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004352 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004353 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004354
4355 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4356};
4357
Vladimir Markofcb503c2016-05-18 12:48:17 +01004358class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004359 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004360 HAdd(Primitive::Type result_type,
4361 HInstruction* left,
4362 HInstruction* right,
4363 uint32_t dex_pc = kNoDexPc)
4364 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004365
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004366 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004367
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004368 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004369
4370 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004371 return GetBlock()->GetGraph()->GetIntConstant(
4372 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004373 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004374 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004375 return GetBlock()->GetGraph()->GetLongConstant(
4376 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004377 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004378 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4379 return GetBlock()->GetGraph()->GetFloatConstant(
4380 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4381 }
4382 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4383 return GetBlock()->GetGraph()->GetDoubleConstant(
4384 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4385 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004386
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004387 DECLARE_INSTRUCTION(Add);
4388
4389 private:
4390 DISALLOW_COPY_AND_ASSIGN(HAdd);
4391};
4392
Vladimir Markofcb503c2016-05-18 12:48:17 +01004393class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004394 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004395 HSub(Primitive::Type result_type,
4396 HInstruction* left,
4397 HInstruction* right,
4398 uint32_t dex_pc = kNoDexPc)
4399 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004400
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004401 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004402
4403 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004404 return GetBlock()->GetGraph()->GetIntConstant(
4405 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004406 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004407 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004408 return GetBlock()->GetGraph()->GetLongConstant(
4409 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004410 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004411 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4412 return GetBlock()->GetGraph()->GetFloatConstant(
4413 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4414 }
4415 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4416 return GetBlock()->GetGraph()->GetDoubleConstant(
4417 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4418 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004419
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004420 DECLARE_INSTRUCTION(Sub);
4421
4422 private:
4423 DISALLOW_COPY_AND_ASSIGN(HSub);
4424};
4425
Vladimir Markofcb503c2016-05-18 12:48:17 +01004426class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004427 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004428 HMul(Primitive::Type result_type,
4429 HInstruction* left,
4430 HInstruction* right,
4431 uint32_t dex_pc = kNoDexPc)
4432 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004433
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004434 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004435
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004436 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004437
4438 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004439 return GetBlock()->GetGraph()->GetIntConstant(
4440 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004441 }
4442 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004443 return GetBlock()->GetGraph()->GetLongConstant(
4444 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004445 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004446 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4447 return GetBlock()->GetGraph()->GetFloatConstant(
4448 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4449 }
4450 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4451 return GetBlock()->GetGraph()->GetDoubleConstant(
4452 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4453 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004454
4455 DECLARE_INSTRUCTION(Mul);
4456
4457 private:
4458 DISALLOW_COPY_AND_ASSIGN(HMul);
4459};
4460
Vladimir Markofcb503c2016-05-18 12:48:17 +01004461class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004462 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004463 HDiv(Primitive::Type result_type,
4464 HInstruction* left,
4465 HInstruction* right,
4466 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004467 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004468
Roland Levillain9867bc72015-08-05 10:21:34 +01004469 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004470 T ComputeIntegral(T x, T y) const {
4471 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004472 // Our graph structure ensures we never have 0 for `y` during
4473 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004474 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004475 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004476 return (y == -1) ? -x : x / y;
4477 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004478
Roland Levillain31dd3d62016-02-16 12:21:02 +00004479 template <typename T>
4480 T ComputeFP(T x, T y) const {
4481 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4482 return x / y;
4483 }
4484
Roland Levillain9867bc72015-08-05 10:21:34 +01004485 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004486 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004487 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004488 }
4489 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004490 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004491 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4492 }
4493 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4494 return GetBlock()->GetGraph()->GetFloatConstant(
4495 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4496 }
4497 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4498 return GetBlock()->GetGraph()->GetDoubleConstant(
4499 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004500 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004501
4502 DECLARE_INSTRUCTION(Div);
4503
4504 private:
4505 DISALLOW_COPY_AND_ASSIGN(HDiv);
4506};
4507
Vladimir Markofcb503c2016-05-18 12:48:17 +01004508class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004509 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004510 HRem(Primitive::Type result_type,
4511 HInstruction* left,
4512 HInstruction* right,
4513 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004514 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004515
Roland Levillain9867bc72015-08-05 10:21:34 +01004516 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004517 T ComputeIntegral(T x, T y) const {
4518 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004519 // Our graph structure ensures we never have 0 for `y` during
4520 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004521 DCHECK_NE(y, 0);
4522 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4523 return (y == -1) ? 0 : x % y;
4524 }
4525
Roland Levillain31dd3d62016-02-16 12:21:02 +00004526 template <typename T>
4527 T ComputeFP(T x, T y) const {
4528 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4529 return std::fmod(x, y);
4530 }
4531
Roland Levillain9867bc72015-08-05 10:21:34 +01004532 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004533 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004534 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004535 }
4536 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004537 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004538 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004539 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004540 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4541 return GetBlock()->GetGraph()->GetFloatConstant(
4542 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4543 }
4544 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4545 return GetBlock()->GetGraph()->GetDoubleConstant(
4546 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4547 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004548
4549 DECLARE_INSTRUCTION(Rem);
4550
4551 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004552 DISALLOW_COPY_AND_ASSIGN(HRem);
4553};
4554
Vladimir Markofcb503c2016-05-18 12:48:17 +01004555class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004556 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004557 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4558 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004559 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004560 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004561 SetRawInputAt(0, value);
4562 }
4563
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004564 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4565
Calin Juravled0d48522014-11-04 16:40:20 +00004566 bool CanBeMoved() const OVERRIDE { return true; }
4567
Vladimir Marko372f10e2016-05-17 16:30:10 +01004568 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004569 return true;
4570 }
4571
4572 bool NeedsEnvironment() const OVERRIDE { return true; }
4573 bool CanThrow() const OVERRIDE { return true; }
4574
Calin Juravled0d48522014-11-04 16:40:20 +00004575 DECLARE_INSTRUCTION(DivZeroCheck);
4576
4577 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004578 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4579};
4580
Vladimir Markofcb503c2016-05-18 12:48:17 +01004581class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004582 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004583 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004584 HInstruction* value,
4585 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004586 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004587 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4588 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4589 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004590 }
4591
Roland Levillain5b5b9312016-03-22 14:57:31 +00004592 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004593 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004594 return value << (distance & max_shift_distance);
4595 }
4596
4597 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004598 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004599 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004600 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004601 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004602 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004603 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004604 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004605 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4606 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4607 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4608 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004609 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004610 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4611 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004612 LOG(FATAL) << DebugName() << " is not defined for float values";
4613 UNREACHABLE();
4614 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004615 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4616 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004617 LOG(FATAL) << DebugName() << " is not defined for double values";
4618 UNREACHABLE();
4619 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004620
4621 DECLARE_INSTRUCTION(Shl);
4622
4623 private:
4624 DISALLOW_COPY_AND_ASSIGN(HShl);
4625};
4626
Vladimir Markofcb503c2016-05-18 12:48:17 +01004627class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004628 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004629 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004630 HInstruction* value,
4631 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004632 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004633 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4634 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4635 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004636 }
4637
Roland Levillain5b5b9312016-03-22 14:57:31 +00004638 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004639 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004640 return value >> (distance & max_shift_distance);
4641 }
4642
4643 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004644 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004645 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004646 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004647 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004648 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004649 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004650 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004651 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4652 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4653 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4654 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004655 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004656 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4657 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004658 LOG(FATAL) << DebugName() << " is not defined for float values";
4659 UNREACHABLE();
4660 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004661 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4662 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004663 LOG(FATAL) << DebugName() << " is not defined for double values";
4664 UNREACHABLE();
4665 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004666
4667 DECLARE_INSTRUCTION(Shr);
4668
4669 private:
4670 DISALLOW_COPY_AND_ASSIGN(HShr);
4671};
4672
Vladimir Markofcb503c2016-05-18 12:48:17 +01004673class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004674 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004675 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004676 HInstruction* value,
4677 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004678 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004679 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4680 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4681 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004682 }
4683
Roland Levillain5b5b9312016-03-22 14:57:31 +00004684 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004685 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004686 typedef typename std::make_unsigned<T>::type V;
4687 V ux = static_cast<V>(value);
4688 return static_cast<T>(ux >> (distance & max_shift_distance));
4689 }
4690
4691 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004692 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004693 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004694 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004695 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004696 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004697 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004698 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004699 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4700 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4701 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4702 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004703 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004704 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4705 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004706 LOG(FATAL) << DebugName() << " is not defined for float values";
4707 UNREACHABLE();
4708 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004709 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4710 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004711 LOG(FATAL) << DebugName() << " is not defined for double values";
4712 UNREACHABLE();
4713 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004714
4715 DECLARE_INSTRUCTION(UShr);
4716
4717 private:
4718 DISALLOW_COPY_AND_ASSIGN(HUShr);
4719};
4720
Vladimir Markofcb503c2016-05-18 12:48:17 +01004721class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004722 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004723 HAnd(Primitive::Type result_type,
4724 HInstruction* left,
4725 HInstruction* right,
4726 uint32_t dex_pc = kNoDexPc)
4727 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004728
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004729 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004730
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004731 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004732
4733 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004734 return GetBlock()->GetGraph()->GetIntConstant(
4735 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004736 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004737 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004738 return GetBlock()->GetGraph()->GetLongConstant(
4739 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004740 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004741 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4742 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4743 LOG(FATAL) << DebugName() << " is not defined for float values";
4744 UNREACHABLE();
4745 }
4746 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4747 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4748 LOG(FATAL) << DebugName() << " is not defined for double values";
4749 UNREACHABLE();
4750 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004751
4752 DECLARE_INSTRUCTION(And);
4753
4754 private:
4755 DISALLOW_COPY_AND_ASSIGN(HAnd);
4756};
4757
Vladimir Markofcb503c2016-05-18 12:48:17 +01004758class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004759 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004760 HOr(Primitive::Type result_type,
4761 HInstruction* left,
4762 HInstruction* right,
4763 uint32_t dex_pc = kNoDexPc)
4764 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004765
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004766 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004767
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004768 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004769
4770 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004771 return GetBlock()->GetGraph()->GetIntConstant(
4772 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004773 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004774 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004775 return GetBlock()->GetGraph()->GetLongConstant(
4776 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004777 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004778 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4779 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4780 LOG(FATAL) << DebugName() << " is not defined for float values";
4781 UNREACHABLE();
4782 }
4783 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4784 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4785 LOG(FATAL) << DebugName() << " is not defined for double values";
4786 UNREACHABLE();
4787 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004788
4789 DECLARE_INSTRUCTION(Or);
4790
4791 private:
4792 DISALLOW_COPY_AND_ASSIGN(HOr);
4793};
4794
Vladimir Markofcb503c2016-05-18 12:48:17 +01004795class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004796 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004797 HXor(Primitive::Type result_type,
4798 HInstruction* left,
4799 HInstruction* right,
4800 uint32_t dex_pc = kNoDexPc)
4801 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004802
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004803 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004804
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004805 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004806
4807 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004808 return GetBlock()->GetGraph()->GetIntConstant(
4809 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004810 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004811 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004812 return GetBlock()->GetGraph()->GetLongConstant(
4813 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004814 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004815 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4816 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4817 LOG(FATAL) << DebugName() << " is not defined for float values";
4818 UNREACHABLE();
4819 }
4820 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4821 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4822 LOG(FATAL) << DebugName() << " is not defined for double values";
4823 UNREACHABLE();
4824 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004825
4826 DECLARE_INSTRUCTION(Xor);
4827
4828 private:
4829 DISALLOW_COPY_AND_ASSIGN(HXor);
4830};
4831
Vladimir Markofcb503c2016-05-18 12:48:17 +01004832class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004833 public:
4834 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004835 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004836 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4837 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004838 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004839
Roland Levillain5b5b9312016-03-22 14:57:31 +00004840 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004841 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004842 typedef typename std::make_unsigned<T>::type V;
4843 V ux = static_cast<V>(value);
4844 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004845 return static_cast<T>(ux);
4846 } else {
4847 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004848 return static_cast<T>(ux >> (distance & max_shift_value)) |
4849 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004850 }
4851 }
4852
Roland Levillain5b5b9312016-03-22 14:57:31 +00004853 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004854 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004855 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004856 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004857 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004858 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004859 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004860 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004861 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4862 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4863 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4864 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004865 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004866 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4867 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004868 LOG(FATAL) << DebugName() << " is not defined for float values";
4869 UNREACHABLE();
4870 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004871 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4872 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004873 LOG(FATAL) << DebugName() << " is not defined for double values";
4874 UNREACHABLE();
4875 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004876
4877 DECLARE_INSTRUCTION(Ror);
4878
4879 private:
4880 DISALLOW_COPY_AND_ASSIGN(HRor);
4881};
4882
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004883// The value of a parameter in this method. Its location depends on
4884// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004885class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004886 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004887 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004888 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004889 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004890 Primitive::Type parameter_type,
4891 bool is_this = false)
4892 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004893 dex_file_(dex_file),
4894 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004895 index_(index) {
4896 SetPackedFlag<kFlagIsThis>(is_this);
4897 SetPackedFlag<kFlagCanBeNull>(!is_this);
4898 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004899
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004900 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08004901 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004902 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004903 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004904
Vladimir Markoa1de9182016-02-25 11:37:38 +00004905 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4906 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004907
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004908 DECLARE_INSTRUCTION(ParameterValue);
4909
4910 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004911 // Whether or not the parameter value corresponds to 'this' argument.
4912 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4913 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4914 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4915 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4916 "Too many packed fields.");
4917
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004918 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004919 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004920 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004921 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004922 const uint8_t index_;
4923
4924 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4925};
4926
Vladimir Markofcb503c2016-05-18 12:48:17 +01004927class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004928 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004929 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4930 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004931
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004932 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004933 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004934 return true;
4935 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004936
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004937 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004938
4939 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004940 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004941 }
4942 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004943 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004944 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004945 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4946 LOG(FATAL) << DebugName() << " is not defined for float values";
4947 UNREACHABLE();
4948 }
4949 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4950 LOG(FATAL) << DebugName() << " is not defined for double values";
4951 UNREACHABLE();
4952 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004953
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004954 DECLARE_INSTRUCTION(Not);
4955
4956 private:
4957 DISALLOW_COPY_AND_ASSIGN(HNot);
4958};
4959
Vladimir Markofcb503c2016-05-18 12:48:17 +01004960class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004961 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004962 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4963 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004964
4965 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004966 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004967 return true;
4968 }
4969
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004970 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004971 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004972 return !x;
4973 }
4974
Roland Levillain9867bc72015-08-05 10:21:34 +01004975 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004976 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004977 }
4978 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4979 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004980 UNREACHABLE();
4981 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004982 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4983 LOG(FATAL) << DebugName() << " is not defined for float values";
4984 UNREACHABLE();
4985 }
4986 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4987 LOG(FATAL) << DebugName() << " is not defined for double values";
4988 UNREACHABLE();
4989 }
David Brazdil66d126e2015-04-03 16:02:44 +01004990
4991 DECLARE_INSTRUCTION(BooleanNot);
4992
4993 private:
4994 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4995};
4996
Vladimir Markofcb503c2016-05-18 12:48:17 +01004997class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004998 public:
4999 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005000 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005001 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005002 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005003 // Invariant: We should never generate a conversion to a Boolean value.
5004 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005005 }
5006
5007 HInstruction* GetInput() const { return InputAt(0); }
5008 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5009 Primitive::Type GetResultType() const { return GetType(); }
5010
5011 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005012 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5013 return true;
5014 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005015
Mark Mendelle82549b2015-05-06 10:55:34 -04005016 // Try to statically evaluate the conversion and return a HConstant
5017 // containing the result. If the input cannot be converted, return nullptr.
5018 HConstant* TryStaticEvaluation() const;
5019
Roland Levillaindff1f282014-11-05 14:15:05 +00005020 DECLARE_INSTRUCTION(TypeConversion);
5021
5022 private:
5023 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5024};
5025
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005026static constexpr uint32_t kNoRegNumber = -1;
5027
Vladimir Markofcb503c2016-05-18 12:48:17 +01005028class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005029 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005030 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5031 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005032 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005033 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005034 SetRawInputAt(0, value);
5035 }
5036
Calin Juravle10e244f2015-01-26 18:54:32 +00005037 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005038 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005039 return true;
5040 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005041
Calin Juravle10e244f2015-01-26 18:54:32 +00005042 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005043
Calin Juravle10e244f2015-01-26 18:54:32 +00005044 bool CanThrow() const OVERRIDE { return true; }
5045
5046 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005047
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005048
5049 DECLARE_INSTRUCTION(NullCheck);
5050
5051 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005052 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5053};
5054
5055class FieldInfo : public ValueObject {
5056 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005057 FieldInfo(MemberOffset field_offset,
5058 Primitive::Type field_type,
5059 bool is_volatile,
5060 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005061 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005062 const DexFile& dex_file,
5063 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005064 : field_offset_(field_offset),
5065 field_type_(field_type),
5066 is_volatile_(is_volatile),
5067 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005068 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005069 dex_file_(dex_file),
5070 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005071
5072 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005073 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005074 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005075 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005076 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005077 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005078 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005079
5080 private:
5081 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005082 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005083 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005084 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005085 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005086 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005087 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005088};
5089
Vladimir Markofcb503c2016-05-18 12:48:17 +01005090class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005091 public:
5092 HInstanceFieldGet(HInstruction* value,
5093 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005094 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005095 bool is_volatile,
5096 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005097 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005098 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005099 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005100 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005101 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005102 field_info_(field_offset,
5103 field_type,
5104 is_volatile,
5105 field_idx,
5106 declaring_class_def_index,
5107 dex_file,
5108 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005109 SetRawInputAt(0, value);
5110 }
5111
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005112 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005113
Vladimir Marko372f10e2016-05-17 16:30:10 +01005114 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5115 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005116 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005117 }
5118
Calin Juravle641547a2015-04-21 22:08:51 +01005119 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005120 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005121 }
5122
5123 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005124 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5125 }
5126
Calin Juravle52c48962014-12-16 17:02:57 +00005127 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005128 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005129 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005130 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005131
5132 DECLARE_INSTRUCTION(InstanceFieldGet);
5133
5134 private:
5135 const FieldInfo field_info_;
5136
5137 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5138};
5139
Vladimir Markofcb503c2016-05-18 12:48:17 +01005140class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005141 public:
5142 HInstanceFieldSet(HInstruction* object,
5143 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005144 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005145 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005146 bool is_volatile,
5147 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005148 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005149 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005150 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005151 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005152 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005153 field_info_(field_offset,
5154 field_type,
5155 is_volatile,
5156 field_idx,
5157 declaring_class_def_index,
5158 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005159 dex_cache) {
5160 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005161 SetRawInputAt(0, object);
5162 SetRawInputAt(1, value);
5163 }
5164
Calin Juravle641547a2015-04-21 22:08:51 +01005165 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005166 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005167 }
5168
Calin Juravle52c48962014-12-16 17:02:57 +00005169 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005170 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005171 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005172 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005173 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005174 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5175 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005176
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005177 DECLARE_INSTRUCTION(InstanceFieldSet);
5178
5179 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005180 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5181 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5182 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5183 "Too many packed fields.");
5184
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005185 const FieldInfo field_info_;
5186
5187 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5188};
5189
Vladimir Markofcb503c2016-05-18 12:48:17 +01005190class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005191 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005192 HArrayGet(HInstruction* array,
5193 HInstruction* index,
5194 Primitive::Type type,
5195 uint32_t dex_pc,
5196 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005197 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005198 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005199 SetRawInputAt(0, array);
5200 SetRawInputAt(1, index);
5201 }
5202
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005203 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005204 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005205 return true;
5206 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005207 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005208 // TODO: We can be smarter here.
5209 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5210 // which generates the implicit null check. There are cases when these can be removed
5211 // to produce better code. If we ever add optimizations to do so we should allow an
5212 // implicit check here (as long as the address falls in the first page).
5213 return false;
5214 }
5215
David Brazdil4833f5a2015-12-16 10:37:39 +00005216 bool IsEquivalentOf(HArrayGet* other) const {
5217 bool result = (GetDexPc() == other->GetDexPc());
5218 if (kIsDebugBuild && result) {
5219 DCHECK_EQ(GetBlock(), other->GetBlock());
5220 DCHECK_EQ(GetArray(), other->GetArray());
5221 DCHECK_EQ(GetIndex(), other->GetIndex());
5222 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005223 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005224 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005225 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5226 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005227 }
5228 }
5229 return result;
5230 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005231
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005232 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5233
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005234 HInstruction* GetArray() const { return InputAt(0); }
5235 HInstruction* GetIndex() const { return InputAt(1); }
5236
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005237 DECLARE_INSTRUCTION(ArrayGet);
5238
5239 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005240 // We treat a String as an array, creating the HArrayGet from String.charAt()
5241 // intrinsic in the instruction simplifier. We can always determine whether
5242 // a particular HArrayGet is actually a String.charAt() by looking at the type
5243 // of the input but that requires holding the mutator lock, so we prefer to use
5244 // a flag, so that code generators don't need to do the locking.
5245 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5246 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5247 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5248 "Too many packed fields.");
5249
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005250 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5251};
5252
Vladimir Markofcb503c2016-05-18 12:48:17 +01005253class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254 public:
5255 HArraySet(HInstruction* array,
5256 HInstruction* index,
5257 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005258 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005259 uint32_t dex_pc)
5260 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005261 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5262 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5263 SetPackedFlag<kFlagValueCanBeNull>(true);
5264 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005265 SetRawInputAt(0, array);
5266 SetRawInputAt(1, index);
5267 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005268 // Make a best guess now, may be refined during SSA building.
5269 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005270 }
5271
Calin Juravle77520bc2015-01-12 18:45:46 +00005272 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005273 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005274 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005275 }
5276
Mingyao Yang81014cb2015-06-02 03:16:27 -07005277 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005278 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005279
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005280 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005281 // TODO: Same as for ArrayGet.
5282 return false;
5283 }
5284
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005285 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005286 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005287 }
5288
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005289 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005290 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005291 }
5292
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005293 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005294 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005295 }
5296
Vladimir Markoa1de9182016-02-25 11:37:38 +00005297 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5298 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5299 bool StaticTypeOfArrayIsObjectArray() const {
5300 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5301 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005302
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005303 HInstruction* GetArray() const { return InputAt(0); }
5304 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005305 HInstruction* GetValue() const { return InputAt(2); }
5306
5307 Primitive::Type GetComponentType() const {
5308 // The Dex format does not type floating point index operations. Since the
5309 // `expected_component_type_` is set during building and can therefore not
5310 // be correct, we also check what is the value type. If it is a floating
5311 // point type, we must use that type.
5312 Primitive::Type value_type = GetValue()->GetType();
5313 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5314 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005315 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005316 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005317
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005318 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005319 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005320 }
5321
Aart Bik18b36ab2016-04-13 16:41:35 -07005322 void ComputeSideEffects() {
5323 Primitive::Type type = GetComponentType();
5324 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5325 SideEffectsForArchRuntimeCalls(type)));
5326 }
5327
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005328 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5329 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5330 }
5331
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005332 DECLARE_INSTRUCTION(ArraySet);
5333
5334 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005335 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5336 static constexpr size_t kFieldExpectedComponentTypeSize =
5337 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5338 static constexpr size_t kFlagNeedsTypeCheck =
5339 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5340 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005341 // Cached information for the reference_type_info_ so that codegen
5342 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005343 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5344 static constexpr size_t kNumberOfArraySetPackedBits =
5345 kFlagStaticTypeOfArrayIsObjectArray + 1;
5346 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5347 using ExpectedComponentTypeField =
5348 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005349
5350 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5351};
5352
Vladimir Markofcb503c2016-05-18 12:48:17 +01005353class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005354 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005355 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005356 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005357 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005358 // Note that arrays do not change length, so the instruction does not
5359 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005360 SetRawInputAt(0, array);
5361 }
5362
Calin Juravle77520bc2015-01-12 18:45:46 +00005363 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005364 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005365 return true;
5366 }
Calin Juravle641547a2015-04-21 22:08:51 +01005367 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5368 return obj == InputAt(0);
5369 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005370
Vladimir Markodce016e2016-04-28 13:10:02 +01005371 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5372
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005373 DECLARE_INSTRUCTION(ArrayLength);
5374
5375 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005376 // We treat a String as an array, creating the HArrayLength from String.length()
5377 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5378 // determine whether a particular HArrayLength is actually a String.length() by
5379 // looking at the type of the input but that requires holding the mutator lock, so
5380 // we prefer to use a flag, so that code generators don't need to do the locking.
5381 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5382 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5383 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5384 "Too many packed fields.");
5385
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005386 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5387};
5388
Vladimir Markofcb503c2016-05-18 12:48:17 +01005389class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005390 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005391 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5392 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005393 HBoundsCheck(HInstruction* index,
5394 HInstruction* length,
5395 uint32_t dex_pc,
5396 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5397 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5398 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005399 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005400 SetRawInputAt(0, index);
5401 SetRawInputAt(1, length);
5402 }
5403
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005404 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005405 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005406 return true;
5407 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005408
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005409 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005410
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005411 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005412
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005413 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5414 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5415
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005416 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005417
5418 DECLARE_INSTRUCTION(BoundsCheck);
5419
5420 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005421 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5422 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5423 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5424 // so we need to create an HEnvironment which will be translated to an InlineInfo
5425 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5426 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5427 // from the invoke as we don't want to look again at the dex bytecode.
5428 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5429
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005430 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5431};
5432
Vladimir Markofcb503c2016-05-18 12:48:17 +01005433class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005434 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005435 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005436 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005437
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005438 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005439 return true;
5440 }
5441
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005442 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5443 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005444
5445 DECLARE_INSTRUCTION(SuspendCheck);
5446
5447 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005448 // Only used for code generation, in order to share the same slow path between back edges
5449 // of a same loop.
5450 SlowPathCode* slow_path_;
5451
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005452 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5453};
5454
David Srbecky0cf44932015-12-09 14:09:59 +00005455// Pseudo-instruction which provides the native debugger with mapping information.
5456// It ensures that we can generate line number and local variables at this point.
5457class HNativeDebugInfo : public HTemplateInstruction<0> {
5458 public:
5459 explicit HNativeDebugInfo(uint32_t dex_pc)
5460 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5461
5462 bool NeedsEnvironment() const OVERRIDE {
5463 return true;
5464 }
5465
5466 DECLARE_INSTRUCTION(NativeDebugInfo);
5467
5468 private:
5469 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5470};
5471
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005472/**
5473 * Instruction to load a Class object.
5474 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005475class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005476 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005477 // Determines how to load the Class.
5478 enum class LoadKind {
5479 // Use the Class* from the method's own ArtMethod*.
5480 kReferrersClass,
5481
5482 // Use boot image Class* address that will be known at link time.
5483 // Used for boot image classes referenced by boot image code in non-PIC mode.
5484 kBootImageLinkTimeAddress,
5485
5486 // Use PC-relative boot image Class* address that will be known at link time.
5487 // Used for boot image classes referenced by boot image code in PIC mode.
5488 kBootImageLinkTimePcRelative,
5489
5490 // Use a known boot image Class* address, embedded in the code by the codegen.
5491 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5492 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5493 // GetIncludePatchInformation().
5494 kBootImageAddress,
5495
5496 // Load from the resolved types array at an absolute address.
5497 // Used for classes outside the boot image referenced by JIT-compiled code.
5498 kDexCacheAddress,
5499
5500 // Load from resolved types array in the dex cache using a PC-relative load.
5501 // Used for classes outside boot image when we know that we can access
5502 // the dex cache arrays using a PC-relative load.
5503 kDexCachePcRelative,
5504
5505 // Load from resolved types array accessed through the class loaded from
5506 // the compiled method's own ArtMethod*. This is the default access type when
5507 // all other types are unavailable.
5508 kDexCacheViaMethod,
5509
5510 kLast = kDexCacheViaMethod
5511 };
5512
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005513 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005514 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005515 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005516 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005517 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005518 bool needs_access_check,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005519 bool is_in_dex_cache,
5520 bool is_in_boot_image)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005521 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5522 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005523 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005524 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005525 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005526 // Referrers class should not need access check. We never inline unverified
5527 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005528 DCHECK(!is_referrers_class || !needs_access_check);
5529
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005530 SetPackedField<LoadKindField>(
5531 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005532 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5533 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005534 SetPackedFlag<kFlagIsInBootImage>(is_in_boot_image);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005535 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005536 }
5537
5538 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5539 DCHECK(HasAddress(load_kind));
5540 load_data_.address = address;
5541 SetLoadKindInternal(load_kind);
5542 }
5543
5544 void SetLoadKindWithTypeReference(LoadKind load_kind,
5545 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005546 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005547 DCHECK(HasTypeReference(load_kind));
5548 DCHECK(IsSameDexFile(dex_file_, dex_file));
5549 DCHECK_EQ(type_index_, type_index);
5550 SetLoadKindInternal(load_kind);
5551 }
5552
5553 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5554 const DexFile& dex_file,
5555 uint32_t element_index) {
5556 DCHECK(HasDexCacheReference(load_kind));
5557 DCHECK(IsSameDexFile(dex_file_, dex_file));
5558 load_data_.dex_cache_element_index = element_index;
5559 SetLoadKindInternal(load_kind);
5560 }
5561
5562 LoadKind GetLoadKind() const {
5563 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005564 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005565
5566 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005567
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005568 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005569
Andreas Gampea5b09a62016-11-17 15:21:22 -08005570 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005571
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005572 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005573
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005574 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005575 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005576 }
5577
Calin Juravle0ba218d2015-05-19 18:46:01 +01005578 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005579 // The entrypoint the code generator is going to call does not do
5580 // clinit of the class.
5581 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005582 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005583 }
5584
5585 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005586 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005587 (!IsReferrersClass() && !IsInDexCache()) ||
5588 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005589 }
5590
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005591
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005592 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005593 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005594 }
5595
Calin Juravleacf735c2015-02-12 15:25:22 +00005596 ReferenceTypeInfo GetLoadedClassRTI() {
5597 return loaded_class_rti_;
5598 }
5599
5600 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5601 // Make sure we only set exact types (the loaded class should never be merged).
5602 DCHECK(rti.IsExact());
5603 loaded_class_rti_ = rti;
5604 }
5605
Andreas Gampea5b09a62016-11-17 15:21:22 -08005606 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005607 const DexFile& GetDexFile() const { return dex_file_; }
5608
5609 uint32_t GetDexCacheElementOffset() const;
5610
5611 uint64_t GetAddress() const {
5612 DCHECK(HasAddress(GetLoadKind()));
5613 return load_data_.address;
5614 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005615
Vladimir Markoa1de9182016-02-25 11:37:38 +00005616 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005617
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005618 static SideEffects SideEffectsForArchRuntimeCalls() {
5619 return SideEffects::CanTriggerGC();
5620 }
5621
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005622 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005623 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5624 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005625 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005626 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005627
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005628 void MarkInDexCache() {
5629 SetPackedFlag<kFlagIsInDexCache>(true);
5630 DCHECK(!NeedsEnvironment());
5631 RemoveEnvironment();
5632 SetSideEffects(SideEffects::None());
5633 }
5634
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005635 void MarkInBootImage() {
5636 SetPackedFlag<kFlagIsInBootImage>(true);
5637 }
5638
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005639 void AddSpecialInput(HInstruction* special_input);
5640
5641 using HInstruction::GetInputRecords; // Keep the const version visible.
5642 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5643 return ArrayRef<HUserRecord<HInstruction*>>(
5644 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5645 }
5646
5647 Primitive::Type GetType() const OVERRIDE {
5648 return Primitive::kPrimNot;
5649 }
5650
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005651 DECLARE_INSTRUCTION(LoadClass);
5652
5653 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005654 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005655 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005656 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005657 // Whether this instruction must generate the initialization check.
5658 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005659 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005660 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5661 static constexpr size_t kFieldLoadKindSize =
5662 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5663 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005664 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005665 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5666
5667 static bool HasTypeReference(LoadKind load_kind) {
5668 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5669 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5670 load_kind == LoadKind::kDexCacheViaMethod ||
5671 load_kind == LoadKind::kReferrersClass;
5672 }
5673
5674 static bool HasAddress(LoadKind load_kind) {
5675 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5676 }
5677
5678 static bool HasDexCacheReference(LoadKind load_kind) {
5679 return load_kind == LoadKind::kDexCachePcRelative;
5680 }
5681
5682 void SetLoadKindInternal(LoadKind load_kind);
5683
5684 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5685 // For other load kinds it's empty or possibly some architecture-specific instruction
5686 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5687 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005688
Andreas Gampea5b09a62016-11-17 15:21:22 -08005689 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005690 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005691
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005692 union {
5693 uint32_t dex_cache_element_index; // Only for dex cache reference.
5694 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5695 } load_data_;
5696
Calin Juravleacf735c2015-02-12 15:25:22 +00005697 ReferenceTypeInfo loaded_class_rti_;
5698
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005699 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5700};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005701std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5702
5703// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5704inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5705 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5706 return load_data_.dex_cache_element_index;
5707}
5708
5709// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5710inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005711 // The special input is used for PC-relative loads on some architectures,
5712 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005713 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005714 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5715 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5716 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005717 DCHECK(special_input_.GetInstruction() == nullptr);
5718 special_input_ = HUserRecord<HInstruction*>(special_input);
5719 special_input->AddUseAt(this, 0);
5720}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005721
Vladimir Marko372f10e2016-05-17 16:30:10 +01005722class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005723 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005724 // Determines how to load the String.
5725 enum class LoadKind {
5726 // Use boot image String* address that will be known at link time.
5727 // Used for boot image strings referenced by boot image code in non-PIC mode.
5728 kBootImageLinkTimeAddress,
5729
5730 // Use PC-relative boot image String* address that will be known at link time.
5731 // Used for boot image strings referenced by boot image code in PIC mode.
5732 kBootImageLinkTimePcRelative,
5733
5734 // Use a known boot image String* address, embedded in the code by the codegen.
5735 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5736 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5737 // GetIncludePatchInformation().
5738 kBootImageAddress,
5739
Vladimir Markoaad75c62016-10-03 08:46:48 +00005740 // Load from an entry in the .bss section using a PC-relative load.
5741 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5742 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005743
5744 // Load from resolved strings array accessed through the class loaded from
5745 // the compiled method's own ArtMethod*. This is the default access type when
5746 // all other types are unavailable.
5747 kDexCacheViaMethod,
5748
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005749 // Load from the root table associated with the JIT compiled method.
5750 kJitTableAddress,
5751
5752 kLast = kJitTableAddress,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005753 };
5754
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005755 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005756 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005757 const DexFile& dex_file,
5758 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005759 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5760 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005761 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005762 SetPackedFlag<kFlagIsInDexCache>(false);
5763 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005764 load_data_.dex_file_ = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005765 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005766
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005767 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5768 DCHECK(HasAddress(load_kind));
5769 load_data_.address = address;
5770 SetLoadKindInternal(load_kind);
5771 }
5772
5773 void SetLoadKindWithStringReference(LoadKind load_kind,
5774 const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005775 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005776 DCHECK(HasStringReference(load_kind));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005777 load_data_.dex_file_ = &dex_file;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005778 string_index_ = string_index;
5779 SetLoadKindInternal(load_kind);
5780 }
5781
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005782 LoadKind GetLoadKind() const {
5783 return GetPackedField<LoadKindField>();
5784 }
5785
5786 const DexFile& GetDexFile() const;
5787
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005788 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005789 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5790 return string_index_;
5791 }
5792
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005793 uint64_t GetAddress() const {
5794 DCHECK(HasAddress(GetLoadKind()));
5795 return load_data_.address;
5796 }
5797
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005798 bool CanBeMoved() const OVERRIDE { return true; }
5799
Vladimir Marko372f10e2016-05-17 16:30:10 +01005800 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005801
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005802 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005803
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005804 // Will call the runtime if we need to load the string through
5805 // the dex cache and the string is not guaranteed to be there yet.
5806 bool NeedsEnvironment() const OVERRIDE {
5807 LoadKind load_kind = GetLoadKind();
5808 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5809 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005810 load_kind == LoadKind::kBootImageAddress ||
5811 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005812 return false;
5813 }
5814 return !IsInDexCache();
5815 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005816
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005817 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5818 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5819 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005820
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005821 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005822 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005823
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005824 static SideEffects SideEffectsForArchRuntimeCalls() {
5825 return SideEffects::CanTriggerGC();
5826 }
5827
Vladimir Markoa1de9182016-02-25 11:37:38 +00005828 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5829
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005830 void MarkInDexCache() {
5831 SetPackedFlag<kFlagIsInDexCache>(true);
5832 DCHECK(!NeedsEnvironment());
5833 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005834 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005835 }
5836
Vladimir Marko372f10e2016-05-17 16:30:10 +01005837 void AddSpecialInput(HInstruction* special_input);
5838
5839 using HInstruction::GetInputRecords; // Keep the const version visible.
5840 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5841 return ArrayRef<HUserRecord<HInstruction*>>(
5842 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005843 }
5844
Vladimir Marko372f10e2016-05-17 16:30:10 +01005845 Primitive::Type GetType() const OVERRIDE {
5846 return Primitive::kPrimNot;
5847 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005848
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005849 DECLARE_INSTRUCTION(LoadString);
5850
5851 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005852 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005853 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5854 static constexpr size_t kFieldLoadKindSize =
5855 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5856 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005857 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005858 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005859
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005860 static bool HasStringReference(LoadKind load_kind) {
5861 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5862 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005863 load_kind == LoadKind::kBssEntry ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005864 load_kind == LoadKind::kDexCacheViaMethod ||
5865 load_kind == LoadKind::kJitTableAddress;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005866 }
5867
5868 static bool HasAddress(LoadKind load_kind) {
Vladimir Marko54d6a202016-11-09 12:46:38 +00005869 return load_kind == LoadKind::kBootImageAddress;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005870 }
5871
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005872 void SetLoadKindInternal(LoadKind load_kind);
5873
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005874 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5875 // For other load kinds it's empty or possibly some architecture-specific instruction
5876 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005877 HUserRecord<HInstruction*> special_input_;
5878
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005879 // String index serves also as the hash code and it's also needed for slow-paths,
5880 // so it must not be overwritten with other load data.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005881 dex::StringIndex string_index_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005882
5883 union {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005884 const DexFile* dex_file_; // For string reference.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005885 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5886 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005887
5888 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5889};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005890std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5891
5892// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5893inline const DexFile& HLoadString::GetDexFile() const {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005894 DCHECK(HasStringReference(GetLoadKind())) << GetLoadKind();
5895 return *load_data_.dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005896}
5897
5898// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5899inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005900 // The special input is used for PC-relative loads on some architectures,
5901 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005902 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005903 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005904 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5905 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005906 // HLoadString::GetInputRecords() returns an empty array at this point,
5907 // so use the GetInputRecords() from the base class to set the input record.
5908 DCHECK(special_input_.GetInstruction() == nullptr);
5909 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005910 special_input->AddUseAt(this, 0);
5911}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005912
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005913/**
5914 * Performs an initialization check on its Class object input.
5915 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005916class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005917 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005918 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005919 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005920 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005921 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5922 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005923 SetRawInputAt(0, constant);
5924 }
5925
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005926 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005927 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005928 return true;
5929 }
5930
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005931 bool NeedsEnvironment() const OVERRIDE {
5932 // May call runtime to initialize the class.
5933 return true;
5934 }
5935
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005936 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005937
5938 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5939
5940 DECLARE_INSTRUCTION(ClinitCheck);
5941
5942 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005943 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5944};
5945
Vladimir Markofcb503c2016-05-18 12:48:17 +01005946class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005947 public:
5948 HStaticFieldGet(HInstruction* cls,
5949 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005950 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005951 bool is_volatile,
5952 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005953 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005954 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005955 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005956 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005957 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005958 field_info_(field_offset,
5959 field_type,
5960 is_volatile,
5961 field_idx,
5962 declaring_class_def_index,
5963 dex_file,
5964 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005965 SetRawInputAt(0, cls);
5966 }
5967
Calin Juravle52c48962014-12-16 17:02:57 +00005968
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005969 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005970
Vladimir Marko372f10e2016-05-17 16:30:10 +01005971 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5972 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005973 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005974 }
5975
5976 size_t ComputeHashCode() const OVERRIDE {
5977 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5978 }
5979
Calin Juravle52c48962014-12-16 17:02:57 +00005980 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005981 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5982 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005983 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005984
5985 DECLARE_INSTRUCTION(StaticFieldGet);
5986
5987 private:
5988 const FieldInfo field_info_;
5989
5990 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5991};
5992
Vladimir Markofcb503c2016-05-18 12:48:17 +01005993class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005994 public:
5995 HStaticFieldSet(HInstruction* cls,
5996 HInstruction* value,
5997 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005998 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005999 bool is_volatile,
6000 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006001 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006002 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006003 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01006004 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006005 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07006006 field_info_(field_offset,
6007 field_type,
6008 is_volatile,
6009 field_idx,
6010 declaring_class_def_index,
6011 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006012 dex_cache) {
6013 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006014 SetRawInputAt(0, cls);
6015 SetRawInputAt(1, value);
6016 }
6017
Calin Juravle52c48962014-12-16 17:02:57 +00006018 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006019 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6020 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006021 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006022
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006023 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006024 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6025 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006026
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006027 DECLARE_INSTRUCTION(StaticFieldSet);
6028
6029 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006030 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6031 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6032 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6033 "Too many packed fields.");
6034
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006035 const FieldInfo field_info_;
6036
6037 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6038};
6039
Vladimir Markofcb503c2016-05-18 12:48:17 +01006040class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006041 public:
6042 HUnresolvedInstanceFieldGet(HInstruction* obj,
6043 Primitive::Type field_type,
6044 uint32_t field_index,
6045 uint32_t dex_pc)
6046 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6047 field_index_(field_index) {
6048 SetRawInputAt(0, obj);
6049 }
6050
6051 bool NeedsEnvironment() const OVERRIDE { return true; }
6052 bool CanThrow() const OVERRIDE { return true; }
6053
6054 Primitive::Type GetFieldType() const { return GetType(); }
6055 uint32_t GetFieldIndex() const { return field_index_; }
6056
6057 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6058
6059 private:
6060 const uint32_t field_index_;
6061
6062 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6063};
6064
Vladimir Markofcb503c2016-05-18 12:48:17 +01006065class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006066 public:
6067 HUnresolvedInstanceFieldSet(HInstruction* obj,
6068 HInstruction* value,
6069 Primitive::Type field_type,
6070 uint32_t field_index,
6071 uint32_t dex_pc)
6072 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006073 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006074 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006075 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006076 SetRawInputAt(0, obj);
6077 SetRawInputAt(1, value);
6078 }
6079
6080 bool NeedsEnvironment() const OVERRIDE { return true; }
6081 bool CanThrow() const OVERRIDE { return true; }
6082
Vladimir Markoa1de9182016-02-25 11:37:38 +00006083 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006084 uint32_t GetFieldIndex() const { return field_index_; }
6085
6086 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6087
6088 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006089 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6090 static constexpr size_t kFieldFieldTypeSize =
6091 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6092 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6093 kFieldFieldType + kFieldFieldTypeSize;
6094 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6095 "Too many packed fields.");
6096 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6097
Calin Juravlee460d1d2015-09-29 04:52:17 +01006098 const uint32_t field_index_;
6099
6100 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6101};
6102
Vladimir Markofcb503c2016-05-18 12:48:17 +01006103class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006104 public:
6105 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6106 uint32_t field_index,
6107 uint32_t dex_pc)
6108 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6109 field_index_(field_index) {
6110 }
6111
6112 bool NeedsEnvironment() const OVERRIDE { return true; }
6113 bool CanThrow() const OVERRIDE { return true; }
6114
6115 Primitive::Type GetFieldType() const { return GetType(); }
6116 uint32_t GetFieldIndex() const { return field_index_; }
6117
6118 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6119
6120 private:
6121 const uint32_t field_index_;
6122
6123 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6124};
6125
Vladimir Markofcb503c2016-05-18 12:48:17 +01006126class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006127 public:
6128 HUnresolvedStaticFieldSet(HInstruction* value,
6129 Primitive::Type field_type,
6130 uint32_t field_index,
6131 uint32_t dex_pc)
6132 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006133 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006134 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006135 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006136 SetRawInputAt(0, value);
6137 }
6138
6139 bool NeedsEnvironment() const OVERRIDE { return true; }
6140 bool CanThrow() const OVERRIDE { return true; }
6141
Vladimir Markoa1de9182016-02-25 11:37:38 +00006142 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006143 uint32_t GetFieldIndex() const { return field_index_; }
6144
6145 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6146
6147 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006148 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6149 static constexpr size_t kFieldFieldTypeSize =
6150 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6151 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6152 kFieldFieldType + kFieldFieldTypeSize;
6153 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6154 "Too many packed fields.");
6155 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6156
Calin Juravlee460d1d2015-09-29 04:52:17 +01006157 const uint32_t field_index_;
6158
6159 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6160};
6161
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006162// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006163class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006164 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006165 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6166 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006167
David Brazdilbbd733e2015-08-18 17:48:17 +01006168 bool CanBeNull() const OVERRIDE { return false; }
6169
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006170 DECLARE_INSTRUCTION(LoadException);
6171
6172 private:
6173 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6174};
6175
David Brazdilcb1c0552015-08-04 16:22:25 +01006176// Implicit part of move-exception which clears thread-local exception storage.
6177// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006178class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006179 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006180 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6181 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006182
6183 DECLARE_INSTRUCTION(ClearException);
6184
6185 private:
6186 DISALLOW_COPY_AND_ASSIGN(HClearException);
6187};
6188
Vladimir Markofcb503c2016-05-18 12:48:17 +01006189class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006190 public:
6191 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006192 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006193 SetRawInputAt(0, exception);
6194 }
6195
6196 bool IsControlFlow() const OVERRIDE { return true; }
6197
6198 bool NeedsEnvironment() const OVERRIDE { return true; }
6199
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006200 bool CanThrow() const OVERRIDE { return true; }
6201
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006202
6203 DECLARE_INSTRUCTION(Throw);
6204
6205 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006206 DISALLOW_COPY_AND_ASSIGN(HThrow);
6207};
6208
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006209/**
6210 * Implementation strategies for the code generator of a HInstanceOf
6211 * or `HCheckCast`.
6212 */
6213enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006214 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006215 kExactCheck, // Can do a single class compare.
6216 kClassHierarchyCheck, // Can just walk the super class chain.
6217 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6218 kInterfaceCheck, // No optimization yet when checking against an interface.
6219 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006220 kArrayCheck, // No optimization yet when checking against a generic array.
6221 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006222};
6223
Roland Levillain86503782016-02-11 19:07:30 +00006224std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6225
Vladimir Markofcb503c2016-05-18 12:48:17 +01006226class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006227 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006228 HInstanceOf(HInstruction* object,
6229 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006230 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006231 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006232 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006233 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006234 dex_pc) {
6235 SetPackedField<TypeCheckKindField>(check_kind);
6236 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006237 SetRawInputAt(0, object);
6238 SetRawInputAt(1, constant);
6239 }
6240
6241 bool CanBeMoved() const OVERRIDE { return true; }
6242
Vladimir Marko372f10e2016-05-17 16:30:10 +01006243 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006244 return true;
6245 }
6246
6247 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006248 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006249 }
6250
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006251 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006252 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6253 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6254 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6255 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006256
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006257 static bool CanCallRuntime(TypeCheckKind check_kind) {
6258 // Mips currently does runtime calls for any other checks.
6259 return check_kind != TypeCheckKind::kExactCheck;
6260 }
6261
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006262 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006263 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006264 }
6265
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006266 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006267
6268 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006269 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6270 static constexpr size_t kFieldTypeCheckKindSize =
6271 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6272 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6273 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6274 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6275 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006276
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006277 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6278};
6279
Vladimir Markofcb503c2016-05-18 12:48:17 +01006280class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006281 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006282 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006283 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006284 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6285 SetPackedFlag<kFlagUpperCanBeNull>(true);
6286 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006287 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006288 SetRawInputAt(0, input);
6289 }
6290
David Brazdilf5552582015-12-27 13:36:12 +00006291 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006292 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006293 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006294 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006295
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006296 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006297 DCHECK(GetUpperCanBeNull() || !can_be_null);
6298 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006299 }
6300
Vladimir Markoa1de9182016-02-25 11:37:38 +00006301 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006302
Calin Juravleb1498f62015-02-16 13:13:29 +00006303 DECLARE_INSTRUCTION(BoundType);
6304
6305 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006306 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6307 // is false then CanBeNull() cannot be true).
6308 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6309 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6310 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6311 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6312
Calin Juravleb1498f62015-02-16 13:13:29 +00006313 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006314 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6315 // It is used to bound the type in cases like:
6316 // if (x instanceof ClassX) {
6317 // // uper_bound_ will be ClassX
6318 // }
David Brazdilf5552582015-12-27 13:36:12 +00006319 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006320
6321 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6322};
6323
Vladimir Markofcb503c2016-05-18 12:48:17 +01006324class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006325 public:
6326 HCheckCast(HInstruction* object,
6327 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006328 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006329 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006330 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6331 SetPackedField<TypeCheckKindField>(check_kind);
6332 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006333 SetRawInputAt(0, object);
6334 SetRawInputAt(1, constant);
6335 }
6336
6337 bool CanBeMoved() const OVERRIDE { return true; }
6338
Vladimir Marko372f10e2016-05-17 16:30:10 +01006339 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006340 return true;
6341 }
6342
6343 bool NeedsEnvironment() const OVERRIDE {
6344 // Instruction may throw a CheckCastError.
6345 return true;
6346 }
6347
6348 bool CanThrow() const OVERRIDE { return true; }
6349
Vladimir Markoa1de9182016-02-25 11:37:38 +00006350 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6351 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6352 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6353 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006354
6355 DECLARE_INSTRUCTION(CheckCast);
6356
6357 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006358 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6359 static constexpr size_t kFieldTypeCheckKindSize =
6360 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6361 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6362 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6363 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6364 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006365
6366 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006367};
6368
Andreas Gampe26de38b2016-07-27 17:53:11 -07006369/**
6370 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6371 * @details We define the combined barrier types that are actually required
6372 * by the Java Memory Model, rather than using exactly the terminology from
6373 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6374 * primitives. Note that the JSR-133 cookbook generally does not deal with
6375 * store atomicity issues, and the recipes there are not always entirely sufficient.
6376 * The current recipe is as follows:
6377 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6378 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6379 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6380 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6381 * class has final fields.
6382 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6383 * store-to-memory instructions. Only generated together with non-temporal stores.
6384 */
6385enum MemBarrierKind {
6386 kAnyStore,
6387 kLoadAny,
6388 kStoreStore,
6389 kAnyAny,
6390 kNTStoreStore,
6391 kLastBarrierKind = kNTStoreStore
6392};
6393std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6394
Vladimir Markofcb503c2016-05-18 12:48:17 +01006395class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006396 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006397 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006398 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006399 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6400 SetPackedField<BarrierKindField>(barrier_kind);
6401 }
Calin Juravle27df7582015-04-17 19:12:31 +01006402
Vladimir Markoa1de9182016-02-25 11:37:38 +00006403 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006404
6405 DECLARE_INSTRUCTION(MemoryBarrier);
6406
6407 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006408 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6409 static constexpr size_t kFieldBarrierKindSize =
6410 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6411 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6412 kFieldBarrierKind + kFieldBarrierKindSize;
6413 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6414 "Too many packed fields.");
6415 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006416
6417 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6418};
6419
Vladimir Markofcb503c2016-05-18 12:48:17 +01006420class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006421 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006422 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006423 kEnter,
6424 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006425 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006426 };
6427
6428 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006429 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006430 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6431 dex_pc) {
6432 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006433 SetRawInputAt(0, object);
6434 }
6435
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006436 // Instruction may go into runtime, so we need an environment.
6437 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006438
6439 bool CanThrow() const OVERRIDE {
6440 // Verifier guarantees that monitor-exit cannot throw.
6441 // This is important because it allows the HGraphBuilder to remove
6442 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6443 return IsEnter();
6444 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006445
Vladimir Markoa1de9182016-02-25 11:37:38 +00006446 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6447 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006448
6449 DECLARE_INSTRUCTION(MonitorOperation);
6450
Calin Juravle52c48962014-12-16 17:02:57 +00006451 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006452 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6453 static constexpr size_t kFieldOperationKindSize =
6454 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6455 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6456 kFieldOperationKind + kFieldOperationKindSize;
6457 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6458 "Too many packed fields.");
6459 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006460
6461 private:
6462 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6463};
6464
Vladimir Markofcb503c2016-05-18 12:48:17 +01006465class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006466 public:
6467 HSelect(HInstruction* condition,
6468 HInstruction* true_value,
6469 HInstruction* false_value,
6470 uint32_t dex_pc)
6471 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6472 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6473
6474 // First input must be `true_value` or `false_value` to allow codegens to
6475 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6476 // that architectures which implement HSelect as a conditional move also
6477 // will not need to invert the condition.
6478 SetRawInputAt(0, false_value);
6479 SetRawInputAt(1, true_value);
6480 SetRawInputAt(2, condition);
6481 }
6482
6483 HInstruction* GetFalseValue() const { return InputAt(0); }
6484 HInstruction* GetTrueValue() const { return InputAt(1); }
6485 HInstruction* GetCondition() const { return InputAt(2); }
6486
6487 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006488 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6489 return true;
6490 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006491
6492 bool CanBeNull() const OVERRIDE {
6493 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6494 }
6495
6496 DECLARE_INSTRUCTION(Select);
6497
6498 private:
6499 DISALLOW_COPY_AND_ASSIGN(HSelect);
6500};
6501
Vladimir Markof9f64412015-09-02 14:05:49 +01006502class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006503 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006504 MoveOperands(Location source,
6505 Location destination,
6506 Primitive::Type type,
6507 HInstruction* instruction)
6508 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006509
6510 Location GetSource() const { return source_; }
6511 Location GetDestination() const { return destination_; }
6512
6513 void SetSource(Location value) { source_ = value; }
6514 void SetDestination(Location value) { destination_ = value; }
6515
6516 // The parallel move resolver marks moves as "in-progress" by clearing the
6517 // destination (but not the source).
6518 Location MarkPending() {
6519 DCHECK(!IsPending());
6520 Location dest = destination_;
6521 destination_ = Location::NoLocation();
6522 return dest;
6523 }
6524
6525 void ClearPending(Location dest) {
6526 DCHECK(IsPending());
6527 destination_ = dest;
6528 }
6529
6530 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006531 DCHECK(source_.IsValid() || destination_.IsInvalid());
6532 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006533 }
6534
6535 // True if this blocks a move from the given location.
6536 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006537 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006538 }
6539
6540 // A move is redundant if it's been eliminated, if its source and
6541 // destination are the same, or if its destination is unneeded.
6542 bool IsRedundant() const {
6543 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6544 }
6545
6546 // We clear both operands to indicate move that's been eliminated.
6547 void Eliminate() {
6548 source_ = destination_ = Location::NoLocation();
6549 }
6550
6551 bool IsEliminated() const {
6552 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6553 return source_.IsInvalid();
6554 }
6555
Alexey Frunze4dda3372015-06-01 18:31:49 -07006556 Primitive::Type GetType() const { return type_; }
6557
Nicolas Geoffray90218252015-04-15 11:56:51 +01006558 bool Is64BitMove() const {
6559 return Primitive::Is64BitType(type_);
6560 }
6561
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006562 HInstruction* GetInstruction() const { return instruction_; }
6563
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006564 private:
6565 Location source_;
6566 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006567 // The type this move is for.
6568 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006569 // The instruction this move is assocatied with. Null when this move is
6570 // for moving an input in the expected locations of user (including a phi user).
6571 // This is only used in debug mode, to ensure we do not connect interval siblings
6572 // in the same parallel move.
6573 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006574};
6575
Roland Levillainc9285912015-12-18 10:38:42 +00006576std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6577
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006578static constexpr size_t kDefaultNumberOfMoves = 4;
6579
Vladimir Markofcb503c2016-05-18 12:48:17 +01006580class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006581 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006582 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006583 : HTemplateInstruction(SideEffects::None(), dex_pc),
6584 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6585 moves_.reserve(kDefaultNumberOfMoves);
6586 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006587
Nicolas Geoffray90218252015-04-15 11:56:51 +01006588 void AddMove(Location source,
6589 Location destination,
6590 Primitive::Type type,
6591 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006592 DCHECK(source.IsValid());
6593 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006594 if (kIsDebugBuild) {
6595 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006596 for (const MoveOperands& move : moves_) {
6597 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006598 // Special case the situation where the move is for the spill slot
6599 // of the instruction.
6600 if ((GetPrevious() == instruction)
6601 || ((GetPrevious() == nullptr)
6602 && instruction->IsPhi()
6603 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006604 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006605 << "Doing parallel moves for the same instruction.";
6606 } else {
6607 DCHECK(false) << "Doing parallel moves for the same instruction.";
6608 }
6609 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006610 }
6611 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006612 for (const MoveOperands& move : moves_) {
6613 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006614 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006615 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006616 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006617 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006618 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006619 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006620 }
6621
Vladimir Marko225b6462015-09-28 12:17:40 +01006622 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006623 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006624 }
6625
Vladimir Marko225b6462015-09-28 12:17:40 +01006626 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006627
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006628 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006629
6630 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006631 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006632
6633 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6634};
6635
Mark Mendell0616ae02015-04-17 12:49:27 -04006636} // namespace art
6637
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006638#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6639#include "nodes_shared.h"
6640#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006641#ifdef ART_ENABLE_CODEGEN_arm
6642#include "nodes_arm.h"
6643#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006644#ifdef ART_ENABLE_CODEGEN_arm64
6645#include "nodes_arm64.h"
6646#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006647#ifdef ART_ENABLE_CODEGEN_mips
6648#include "nodes_mips.h"
6649#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006650#ifdef ART_ENABLE_CODEGEN_x86
6651#include "nodes_x86.h"
6652#endif
6653
6654namespace art {
6655
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006656class HGraphVisitor : public ValueObject {
6657 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006658 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6659 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006660
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006661 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006662 virtual void VisitBasicBlock(HBasicBlock* block);
6663
Roland Levillain633021e2014-10-01 14:12:25 +01006664 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006665 void VisitInsertionOrder();
6666
Roland Levillain633021e2014-10-01 14:12:25 +01006667 // Visit the graph following dominator tree reverse post-order.
6668 void VisitReversePostOrder();
6669
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006670 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006671
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006672 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006673#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006674 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6675
6676 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6677
6678#undef DECLARE_VISIT_INSTRUCTION
6679
6680 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006681 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006682
6683 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6684};
6685
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006686class HGraphDelegateVisitor : public HGraphVisitor {
6687 public:
6688 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6689 virtual ~HGraphDelegateVisitor() {}
6690
6691 // Visit functions that delegate to to super class.
6692#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006693 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006694
6695 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6696
6697#undef DECLARE_VISIT_INSTRUCTION
6698
6699 private:
6700 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6701};
6702
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006703// Iterator over the blocks that art part of the loop. Includes blocks part
6704// of an inner loop. The order in which the blocks are iterated is on their
6705// block id.
6706class HBlocksInLoopIterator : public ValueObject {
6707 public:
6708 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6709 : blocks_in_loop_(info.GetBlocks()),
6710 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6711 index_(0) {
6712 if (!blocks_in_loop_.IsBitSet(index_)) {
6713 Advance();
6714 }
6715 }
6716
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006717 bool Done() const { return index_ == blocks_.size(); }
6718 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006719 void Advance() {
6720 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006721 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006722 if (blocks_in_loop_.IsBitSet(index_)) {
6723 break;
6724 }
6725 }
6726 }
6727
6728 private:
6729 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006730 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006731 size_t index_;
6732
6733 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6734};
6735
Mingyao Yang3584bce2015-05-19 16:01:59 -07006736// Iterator over the blocks that art part of the loop. Includes blocks part
6737// of an inner loop. The order in which the blocks are iterated is reverse
6738// post order.
6739class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6740 public:
6741 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6742 : blocks_in_loop_(info.GetBlocks()),
6743 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6744 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006745 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006746 Advance();
6747 }
6748 }
6749
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006750 bool Done() const { return index_ == blocks_.size(); }
6751 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006752 void Advance() {
6753 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006754 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6755 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006756 break;
6757 }
6758 }
6759 }
6760
6761 private:
6762 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006763 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006764 size_t index_;
6765
6766 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6767};
6768
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006769inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006770 if (constant->IsIntConstant()) {
6771 return constant->AsIntConstant()->GetValue();
6772 } else if (constant->IsLongConstant()) {
6773 return constant->AsLongConstant()->GetValue();
6774 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006775 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006776 return 0;
6777 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006778}
6779
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006780#define INSTRUCTION_TYPE_CHECK(type, super) \
6781 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6782 inline const H##type* HInstruction::As##type() const { \
6783 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6784 } \
6785 inline H##type* HInstruction::As##type() { \
6786 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6787 }
6788
6789 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6790#undef INSTRUCTION_TYPE_CHECK
6791
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006792// Create space in `blocks` for adding `number_of_new_blocks` entries
6793// starting at location `at`. Blocks after `at` are moved accordingly.
6794inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6795 size_t number_of_new_blocks,
6796 size_t after) {
6797 DCHECK_LT(after, blocks->size());
6798 size_t old_size = blocks->size();
6799 size_t new_size = old_size + number_of_new_blocks;
6800 blocks->resize(new_size);
6801 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6802}
6803
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006804} // namespace art
6805
6806#endif // ART_COMPILER_OPTIMIZING_NODES_H_