blob: 7ab04e15fcedaff5de9b82120e314ed5670261b6 [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()),
336 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100337 blocks_.reserve(kDefaultNumberOfBlocks);
338 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000339
David Brazdilbadd8262016-02-02 16:28:56 +0000340 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700341 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000342
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000343 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100344 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
345
David Brazdil69ba7b72015-06-23 18:27:30 +0100346 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000347 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100348
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000349 HBasicBlock* GetEntryBlock() const { return entry_block_; }
350 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100351 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000352
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000353 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
354 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000355
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000356 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100357
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100358 void ComputeDominanceInformation();
359 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000360 void ClearLoopInformation();
361 void FindBackEdges(ArenaBitVector* visited);
362 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100363 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100364 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000365
David Brazdil4833f5a2015-12-16 10:37:39 +0000366 // Analyze all natural loops in this graph. Returns a code specifying that it
367 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000368 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000369 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100370
David Brazdilffee3d32015-07-06 11:48:53 +0100371 // Iterate over blocks to compute try block membership. Needs reverse post
372 // order and loop information.
373 void ComputeTryBlockInformation();
374
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000375 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000376 // Returns the instruction to replace the invoke expression or null if the
377 // invoke is for a void method. Note that the caller is responsible for replacing
378 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000379 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000380
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000381 // Update the loop and try membership of `block`, which was spawned from `reference`.
382 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
383 // should be the new back edge.
384 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
385 HBasicBlock* reference,
386 bool replace_if_back_edge);
387
Mingyao Yang3584bce2015-05-19 16:01:59 -0700388 // Need to add a couple of blocks to test if the loop body is entered and
389 // put deoptimization instructions, etc.
390 void TransformLoopHeaderForBCE(HBasicBlock* header);
391
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000392 // Removes `block` from the graph. Assumes `block` has been disconnected from
393 // other blocks and has no instructions or phis.
394 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000395
David Brazdilfc6a86a2015-06-26 10:33:45 +0000396 // Splits the edge between `block` and `successor` while preserving the
397 // indices in the predecessor/successor lists. If there are multiple edges
398 // between the blocks, the lowest indices are used.
399 // Returns the new block which is empty and has the same dex pc as `successor`.
400 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
401
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100402 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
403 void SimplifyLoop(HBasicBlock* header);
404
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000405 int32_t GetNextInstructionId() {
406 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000407 return current_instruction_id_++;
408 }
409
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000410 int32_t GetCurrentInstructionId() const {
411 return current_instruction_id_;
412 }
413
414 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000415 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000416 current_instruction_id_ = id;
417 }
418
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100419 uint16_t GetMaximumNumberOfOutVRegs() const {
420 return maximum_number_of_out_vregs_;
421 }
422
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000423 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
424 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100425 }
426
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100427 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
428 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
429 }
430
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000431 void UpdateTemporariesVRegSlots(size_t slots) {
432 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100433 }
434
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000435 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100436 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000437 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100438 }
439
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100440 void SetNumberOfVRegs(uint16_t number_of_vregs) {
441 number_of_vregs_ = number_of_vregs;
442 }
443
444 uint16_t GetNumberOfVRegs() const {
445 return number_of_vregs_;
446 }
447
448 void SetNumberOfInVRegs(uint16_t value) {
449 number_of_in_vregs_ = value;
450 }
451
David Brazdildee58d62016-04-07 09:54:26 +0000452 uint16_t GetNumberOfInVRegs() const {
453 return number_of_in_vregs_;
454 }
455
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100456 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100457 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100458 return number_of_vregs_ - number_of_in_vregs_;
459 }
460
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100461 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100462 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100463 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100464
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100465 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
466 DCHECK(GetReversePostOrder()[0] == entry_block_);
467 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
468 }
469
470 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
471 return ReverseRange(GetReversePostOrder());
472 }
473
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100474 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100475 return linear_order_;
476 }
477
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100478 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
479 return ReverseRange(GetLinearOrder());
480 }
481
Mark Mendell1152c922015-04-24 17:06:35 -0400482 bool HasBoundsChecks() const {
483 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800484 }
485
Mark Mendell1152c922015-04-24 17:06:35 -0400486 void SetHasBoundsChecks(bool value) {
487 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800488 }
489
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100490 bool ShouldGenerateConstructorBarrier() const {
491 return should_generate_constructor_barrier_;
492 }
493
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000494 bool IsDebuggable() const { return debuggable_; }
495
David Brazdil8d5b8b22015-03-24 10:51:52 +0000496 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000497 // already, it is created and inserted into the graph. This method is only for
498 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600499 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000500
501 // TODO: This is problematic for the consistency of reference type propagation
502 // because it can be created anytime after the pass and thus it will be left
503 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600504 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000505
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600506 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
507 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000508 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600509 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
510 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000511 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600512 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
513 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000514 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600515 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
516 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000517 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000518
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100519 HCurrentMethod* GetCurrentMethod();
520
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100521 const DexFile& GetDexFile() const {
522 return dex_file_;
523 }
524
525 uint32_t GetMethodIdx() const {
526 return method_idx_;
527 }
528
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100529 InvokeType GetInvokeType() const {
530 return invoke_type_;
531 }
532
Mark Mendellc4701932015-04-10 13:18:51 -0400533 InstructionSet GetInstructionSet() const {
534 return instruction_set_;
535 }
536
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000537 bool IsCompilingOsr() const { return osr_; }
538
David Brazdil77a48ae2015-09-15 12:34:04 +0000539 bool HasTryCatch() const { return has_try_catch_; }
540 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100541
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000542 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
543 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
544
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100545 ArtMethod* GetArtMethod() const { return art_method_; }
546 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
547
Mark Mendellf6529172015-11-17 11:16:56 -0500548 // Returns an instruction with the opposite boolean value from 'cond'.
549 // The instruction has been inserted into the graph, either as a constant, or
550 // before cursor.
551 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
552
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000553 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
554
David Brazdil2d7352b2015-04-20 14:52:42 +0100555 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000556 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100557 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000558
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000559 template <class InstructionType, typename ValueType>
560 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600561 ArenaSafeMap<ValueType, InstructionType*>* cache,
562 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000563 // Try to find an existing constant of the given value.
564 InstructionType* constant = nullptr;
565 auto cached_constant = cache->find(value);
566 if (cached_constant != cache->end()) {
567 constant = cached_constant->second;
568 }
569
570 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100571 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000572 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600573 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000574 cache->Overwrite(value, constant);
575 InsertConstant(constant);
576 }
577 return constant;
578 }
579
David Brazdil8d5b8b22015-03-24 10:51:52 +0000580 void InsertConstant(HConstant* instruction);
581
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000582 // Cache a float constant into the graph. This method should only be
583 // called by the SsaBuilder when creating "equivalent" instructions.
584 void CacheFloatConstant(HFloatConstant* constant);
585
586 // See CacheFloatConstant comment.
587 void CacheDoubleConstant(HDoubleConstant* constant);
588
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000589 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000590
591 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100592 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000593
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100594 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100595 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000596
Aart Bik281c6812016-08-26 11:31:48 -0700597 // List of blocks to perform a linear order tree traversal. Unlike the reverse
598 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100599 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100600
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000601 HBasicBlock* entry_block_;
602 HBasicBlock* exit_block_;
603
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100604 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100605 uint16_t maximum_number_of_out_vregs_;
606
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100607 // The number of virtual registers in this method. Contains the parameters.
608 uint16_t number_of_vregs_;
609
610 // The number of virtual registers used by parameters of this method.
611 uint16_t number_of_in_vregs_;
612
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000613 // Number of vreg size slots that the temporaries use (used in baseline compiler).
614 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100615
Mark Mendell1152c922015-04-24 17:06:35 -0400616 // Has bounds checks. We can totally skip BCE if it's false.
617 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800618
David Brazdil77a48ae2015-09-15 12:34:04 +0000619 // Flag whether there are any try/catch blocks in the graph. We will skip
620 // try/catch-related passes if false.
621 bool has_try_catch_;
622
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000623 // Flag whether there are any irreducible loops in the graph.
624 bool has_irreducible_loops_;
625
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000626 // Indicates whether the graph should be compiled in a way that
627 // ensures full debuggability. If false, we can apply more
628 // aggressive optimizations that may limit the level of debugging.
629 const bool debuggable_;
630
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000631 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000632 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000633
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100634 // The dex file from which the method is from.
635 const DexFile& dex_file_;
636
637 // The method index in the dex file.
638 const uint32_t method_idx_;
639
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100640 // If inlined, this encodes how the callee is being invoked.
641 const InvokeType invoke_type_;
642
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100643 // Whether the graph has been transformed to SSA form. Only used
644 // in debug mode to ensure we are not using properties only valid
645 // for non-SSA form (like the number of temporaries).
646 bool in_ssa_form_;
647
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100648 const bool should_generate_constructor_barrier_;
649
Mathieu Chartiere401d142015-04-22 13:56:20 -0700650 const InstructionSet instruction_set_;
651
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000652 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000653 HNullConstant* cached_null_constant_;
654 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000655 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000656 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000657 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000658
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100659 HCurrentMethod* cached_current_method_;
660
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100661 // The ArtMethod this graph is for. Note that for AOT, it may be null,
662 // for example for methods whose declaring class could not be resolved
663 // (such as when the superclass could not be found).
664 ArtMethod* art_method_;
665
David Brazdil4833f5a2015-12-16 10:37:39 +0000666 // Keep the RTI of inexact Object to avoid having to pass stack handle
667 // collection pointer to passes which may create NullConstant.
668 ReferenceTypeInfo inexact_object_rti_;
669
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000670 // Whether we are compiling this graph for on stack replacement: this will
671 // make all loops seen as irreducible and emit special stack maps to mark
672 // compiled code entries which the interpreter can directly jump to.
673 const bool osr_;
674
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000675 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100676 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000677 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000678 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000679 DISALLOW_COPY_AND_ASSIGN(HGraph);
680};
681
Vladimir Markof9f64412015-09-02 14:05:49 +0100682class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000683 public:
684 HLoopInformation(HBasicBlock* header, HGraph* graph)
685 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100686 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000687 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100688 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100689 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100690 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000691 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100692 back_edges_.reserve(kDefaultNumberOfBackEdges);
693 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100694
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000695 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100696 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000697
698 void Dump(std::ostream& os);
699
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100700 HBasicBlock* GetHeader() const {
701 return header_;
702 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000703
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000704 void SetHeader(HBasicBlock* block) {
705 header_ = block;
706 }
707
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100708 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
709 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
710 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
711
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000712 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100713 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000714 }
715
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100716 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100717 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100718 }
719
David Brazdil46e2a392015-03-16 17:31:52 +0000720 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100721 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100722 }
723
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000724 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100725 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000726 }
727
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100728 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100729
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100730 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100731 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100732 }
733
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100734 // Returns the lifetime position of the back edge that has the
735 // greatest lifetime position.
736 size_t GetLifetimeEnd() const;
737
738 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100739 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100740 }
741
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000742 // Finds blocks that are part of this loop.
743 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100744
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100745 // Returns whether this loop information contains `block`.
746 // Note that this loop information *must* be populated before entering this function.
747 bool Contains(const HBasicBlock& block) const;
748
749 // Returns whether this loop information is an inner loop of `other`.
750 // Note that `other` *must* be populated before entering this function.
751 bool IsIn(const HLoopInformation& other) const;
752
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800753 // Returns true if instruction is not defined within this loop.
754 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700755
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100756 const ArenaBitVector& GetBlocks() const { return blocks_; }
757
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000758 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000759 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000760
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000761 void ClearAllBlocks() {
762 blocks_.ClearAllBits();
763 }
764
David Brazdil3f4a5222016-05-06 12:46:21 +0100765 bool HasBackEdgeNotDominatedByHeader() const;
766
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100767 bool IsPopulated() const {
768 return blocks_.GetHighestBitSet() != -1;
769 }
770
Anton Shaminf89381f2016-05-16 16:44:13 +0600771 bool DominatesAllBackEdges(HBasicBlock* block);
772
David Sehrc757dec2016-11-04 15:48:34 -0700773 bool HasExitEdge() const;
774
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000775 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100776 // Internal recursive implementation of `Populate`.
777 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100778 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100779
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000780 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100781 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000782 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100783 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100784 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100785 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000786
787 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
788};
789
David Brazdilec16f792015-08-19 15:04:01 +0100790// Stores try/catch information for basic blocks.
791// Note that HGraph is constructed so that catch blocks cannot simultaneously
792// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100793class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100794 public:
795 // Try block information constructor.
796 explicit TryCatchInformation(const HTryBoundary& try_entry)
797 : try_entry_(&try_entry),
798 catch_dex_file_(nullptr),
799 catch_type_index_(DexFile::kDexNoIndex16) {
800 DCHECK(try_entry_ != nullptr);
801 }
802
803 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800804 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100805 : try_entry_(nullptr),
806 catch_dex_file_(&dex_file),
807 catch_type_index_(catch_type_index) {}
808
809 bool IsTryBlock() const { return try_entry_ != nullptr; }
810
811 const HTryBoundary& GetTryEntry() const {
812 DCHECK(IsTryBlock());
813 return *try_entry_;
814 }
815
816 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
817
818 bool IsCatchAllTypeIndex() const {
819 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800820 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100821 }
822
Andreas Gampea5b09a62016-11-17 15:21:22 -0800823 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100824 DCHECK(IsCatchBlock());
825 return catch_type_index_;
826 }
827
828 const DexFile& GetCatchDexFile() const {
829 DCHECK(IsCatchBlock());
830 return *catch_dex_file_;
831 }
832
833 private:
834 // One of possibly several TryBoundary instructions entering the block's try.
835 // Only set for try blocks.
836 const HTryBoundary* try_entry_;
837
838 // Exception type information. Only set for catch blocks.
839 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800840 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100841};
842
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100843static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100844static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100845
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000846// A block in a method. Contains the list of instructions represented
847// as a double linked list. Each block knows its predecessors and
848// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100849
Vladimir Markof9f64412015-09-02 14:05:49 +0100850class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000851 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700852 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000853 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000854 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
855 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000856 loop_information_(nullptr),
857 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000858 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100859 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100860 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100861 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000862 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000863 try_catch_information_(nullptr) {
864 predecessors_.reserve(kDefaultNumberOfPredecessors);
865 successors_.reserve(kDefaultNumberOfSuccessors);
866 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
867 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000868
Vladimir Marko60584552015-09-03 13:35:12 +0000869 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100870 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000871 }
872
Vladimir Marko60584552015-09-03 13:35:12 +0000873 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100874 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000875 }
876
David Brazdild26a4112015-11-10 11:07:31 +0000877 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
878 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
879
Vladimir Marko60584552015-09-03 13:35:12 +0000880 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
881 return ContainsElement(successors_, block, start_from);
882 }
883
884 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100885 return dominated_blocks_;
886 }
887
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100888 bool IsEntryBlock() const {
889 return graph_->GetEntryBlock() == this;
890 }
891
892 bool IsExitBlock() const {
893 return graph_->GetExitBlock() == this;
894 }
895
David Brazdil46e2a392015-03-16 17:31:52 +0000896 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000897 bool IsSingleTryBoundary() const;
898
899 // Returns true if this block emits nothing but a jump.
900 bool IsSingleJump() const {
901 HLoopInformation* loop_info = GetLoopInformation();
902 return (IsSingleGoto() || IsSingleTryBoundary())
903 // Back edges generate a suspend check.
904 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
905 }
David Brazdil46e2a392015-03-16 17:31:52 +0000906
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000907 void AddBackEdge(HBasicBlock* back_edge) {
908 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000909 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000910 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100911 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000912 loop_information_->AddBackEdge(back_edge);
913 }
914
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000915 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000916 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000917
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100918 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000919 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600920 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000921
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000922 HBasicBlock* GetDominator() const { return dominator_; }
923 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000924 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
925
926 void RemoveDominatedBlock(HBasicBlock* block) {
927 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100928 }
Vladimir Marko60584552015-09-03 13:35:12 +0000929
930 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
931 ReplaceElement(dominated_blocks_, existing, new_block);
932 }
933
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100934 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000935
936 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100937 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000938 }
939
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100940 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
941 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100942 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100943 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100944 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
945 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000946
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000947 HInstruction* GetFirstInstructionDisregardMoves() const;
948
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000949 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000950 successors_.push_back(block);
951 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000952 }
953
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100954 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
955 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100956 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000957 new_block->predecessors_.push_back(this);
958 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000959 }
960
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000961 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
962 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000963 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000964 new_block->successors_.push_back(this);
965 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000966 }
967
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100968 // Insert `this` between `predecessor` and `successor. This method
969 // preserves the indicies, and will update the first edge found between
970 // `predecessor` and `successor`.
971 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
972 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100973 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000974 successor->predecessors_[predecessor_index] = this;
975 predecessor->successors_[successor_index] = this;
976 successors_.push_back(successor);
977 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100978 }
979
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100980 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000981 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100982 }
983
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000984 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000985 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000986 }
987
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100988 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000989 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100990 }
991
992 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000993 predecessors_.push_back(block);
994 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100995 }
996
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100997 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000998 DCHECK_EQ(predecessors_.size(), 2u);
999 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001000 }
1001
David Brazdil769c9e52015-04-27 13:54:09 +01001002 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001003 DCHECK_EQ(successors_.size(), 2u);
1004 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001005 }
1006
David Brazdilfc6a86a2015-06-26 10:33:45 +00001007 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001008 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001009 }
1010
David Brazdilfc6a86a2015-06-26 10:33:45 +00001011 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001012 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001013 }
1014
David Brazdilfc6a86a2015-06-26 10:33:45 +00001015 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001016 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001017 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001018 }
1019
1020 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001021 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001022 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001023 }
1024
1025 // Returns whether the first occurrence of `predecessor` in the list of
1026 // predecessors is at index `idx`.
1027 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001028 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001029 return GetPredecessorIndexOf(predecessor) == idx;
1030 }
1031
David Brazdild7558da2015-09-22 13:04:14 +01001032 // Create a new block between this block and its predecessors. The new block
1033 // is added to the graph, all predecessor edges are relinked to it and an edge
1034 // is created to `this`. Returns the new empty block. Reverse post order or
1035 // loop and try/catch information are not updated.
1036 HBasicBlock* CreateImmediateDominator();
1037
David Brazdilfc6a86a2015-06-26 10:33:45 +00001038 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001039 // created, latter block. Note that this method will add the block to the
1040 // graph, create a Goto at the end of the former block and will create an edge
1041 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001042 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001043 HBasicBlock* SplitBefore(HInstruction* cursor);
1044
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001045 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001046 // created block. Note that this method just updates raw block information,
1047 // like predecessors, successors, dominators, and instruction list. It does not
1048 // update the graph, reverse post order, loop information, nor make sure the
1049 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001050 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1051
1052 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1053 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001054
1055 // Merge `other` at the end of `this`. Successors and dominated blocks of
1056 // `other` are changed to be successors and dominated blocks of `this`. Note
1057 // that this method does not update the graph, reverse post order, loop
1058 // information, nor make sure the blocks are consistent (for example ending
1059 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001060 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001061
1062 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1063 // of `this` are moved to `other`.
1064 // Note that this method does not update the graph, reverse post order, loop
1065 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001066 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001067 void ReplaceWith(HBasicBlock* other);
1068
David Brazdil2d7352b2015-04-20 14:52:42 +01001069 // Merge `other` at the end of `this`. This method updates loops, reverse post
1070 // order, links to predecessors, successors, dominators and deletes the block
1071 // from the graph. The two blocks must be successive, i.e. `this` the only
1072 // predecessor of `other` and vice versa.
1073 void MergeWith(HBasicBlock* other);
1074
1075 // Disconnects `this` from all its predecessors, successors and dominator,
1076 // removes it from all loops it is included in and eventually from the graph.
1077 // The block must not dominate any other block. Predecessors and successors
1078 // are safely updated.
1079 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001080
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001081 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001082 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001083 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001084 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001085 // Replace instruction `initial` with `replacement` within this block.
1086 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1087 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001088 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001089 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001090 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1091 // instruction list. With 'ensure_safety' set to true, it verifies that the
1092 // instruction is not in use and removes it from the use lists of its inputs.
1093 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1094 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001095 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001096
1097 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001098 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001099 }
1100
Roland Levillain6b879dd2014-09-22 17:13:44 +01001101 bool IsLoopPreHeaderFirstPredecessor() const {
1102 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001103 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001104 }
1105
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001106 bool IsFirstPredecessorBackEdge() const {
1107 DCHECK(IsLoopHeader());
1108 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1109 }
1110
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001111 HLoopInformation* GetLoopInformation() const {
1112 return loop_information_;
1113 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001114
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001115 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001116 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001117 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001118 void SetInLoop(HLoopInformation* info) {
1119 if (IsLoopHeader()) {
1120 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001121 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001122 loop_information_ = info;
1123 } else if (loop_information_->Contains(*info->GetHeader())) {
1124 // Block is currently part of an outer loop. Make it part of this inner loop.
1125 // Note that a non loop header having a loop information means this loop information
1126 // has already been populated
1127 loop_information_ = info;
1128 } else {
1129 // Block is part of an inner loop. Do not update the loop information.
1130 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1131 // at this point, because this method is being called while populating `info`.
1132 }
1133 }
1134
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001135 // Raw update of the loop information.
1136 void SetLoopInformation(HLoopInformation* info) {
1137 loop_information_ = info;
1138 }
1139
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001140 bool IsInLoop() const { return loop_information_ != nullptr; }
1141
David Brazdilec16f792015-08-19 15:04:01 +01001142 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1143
1144 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1145 try_catch_information_ = try_catch_information;
1146 }
1147
1148 bool IsTryBlock() const {
1149 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1150 }
1151
1152 bool IsCatchBlock() const {
1153 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1154 }
David Brazdilffee3d32015-07-06 11:48:53 +01001155
1156 // Returns the try entry that this block's successors should have. They will
1157 // be in the same try, unless the block ends in a try boundary. In that case,
1158 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001159 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001160
David Brazdild7558da2015-09-22 13:04:14 +01001161 bool HasThrowingInstructions() const;
1162
David Brazdila4b8c212015-05-07 09:59:30 +01001163 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001164 bool Dominates(HBasicBlock* block) const;
1165
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001166 size_t GetLifetimeStart() const { return lifetime_start_; }
1167 size_t GetLifetimeEnd() const { return lifetime_end_; }
1168
1169 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1170 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1171
David Brazdil8d5b8b22015-03-24 10:51:52 +00001172 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001173 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001174 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001175 bool HasSinglePhi() const;
1176
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001177 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001178 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001179 ArenaVector<HBasicBlock*> predecessors_;
1180 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001181 HInstructionList instructions_;
1182 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001183 HLoopInformation* loop_information_;
1184 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001185 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001186 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001187 // The dex program counter of the first instruction of this block.
1188 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001189 size_t lifetime_start_;
1190 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001191 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001192
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001193 friend class HGraph;
1194 friend class HInstruction;
1195
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001196 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1197};
1198
David Brazdilb2bd1c52015-03-25 11:17:37 +00001199// Iterates over the LoopInformation of all loops which contain 'block'
1200// from the innermost to the outermost.
1201class HLoopInformationOutwardIterator : public ValueObject {
1202 public:
1203 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1204 : current_(block.GetLoopInformation()) {}
1205
1206 bool Done() const { return current_ == nullptr; }
1207
1208 void Advance() {
1209 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001210 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001211 }
1212
1213 HLoopInformation* Current() const {
1214 DCHECK(!Done());
1215 return current_;
1216 }
1217
1218 private:
1219 HLoopInformation* current_;
1220
1221 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1222};
1223
Alexandre Ramesef20f712015-06-09 10:29:30 +01001224#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001225 M(Above, Condition) \
1226 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001227 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001228 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001229 M(ArrayGet, Instruction) \
1230 M(ArrayLength, Instruction) \
1231 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001232 M(Below, Condition) \
1233 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001234 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001235 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001236 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001237 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001238 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001239 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001240 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001241 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001242 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001243 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001244 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001245 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001246 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001247 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001248 M(Exit, Instruction) \
1249 M(FloatConstant, Constant) \
1250 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001251 M(GreaterThan, Condition) \
1252 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001253 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001254 M(InstanceFieldGet, Instruction) \
1255 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001256 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001257 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001258 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001259 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001260 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001261 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001262 M(LessThan, Condition) \
1263 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001264 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001265 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001266 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001267 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001268 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001269 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001270 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001271 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001272 M(Neg, UnaryOperation) \
1273 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001274 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001275 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001276 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001277 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001278 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001279 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001280 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001281 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001282 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001283 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001284 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001285 M(Return, Instruction) \
1286 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001287 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001288 M(Shl, BinaryOperation) \
1289 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001290 M(StaticFieldGet, Instruction) \
1291 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001292 M(UnresolvedInstanceFieldGet, Instruction) \
1293 M(UnresolvedInstanceFieldSet, Instruction) \
1294 M(UnresolvedStaticFieldGet, Instruction) \
1295 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001296 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001297 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001298 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001299 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001300 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001301 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001302 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001303 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001304
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001305/*
1306 * Instructions, shared across several (not all) architectures.
1307 */
1308#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1309#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1310#else
1311#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001312 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001313 M(MultiplyAccumulate, Instruction) \
1314 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001315#endif
1316
Vladimir Markob4536b72015-11-24 13:45:23 +00001317#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001318#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001319#else
1320#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1321 M(ArmDexCacheArraysBase, Instruction)
1322#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001323
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001324#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001325#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001326#else
1327#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001328 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001329#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001330
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001331#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001332#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001333#else
1334#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1335 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001336 M(MipsDexCacheArraysBase, Instruction) \
1337 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001338#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001339
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001340#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1341
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001342#ifndef ART_ENABLE_CODEGEN_x86
1343#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1344#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001345#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1346 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001347 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001348 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001349 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001350#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001351
1352#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1353
1354#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1355 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001356 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001357 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1358 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001359 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001360 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001361 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1362 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1363
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001364#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1365 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001366 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001367 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001368 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001369 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001370
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001371#define FOR_EACH_INSTRUCTION(M) \
1372 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1373 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1374
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001375#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001376FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1377#undef FORWARD_DECLARATION
1378
Vladimir Marko372f10e2016-05-17 16:30:10 +01001379#define DECLARE_INSTRUCTION(type) \
1380 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1381 const char* DebugName() const OVERRIDE { return #type; } \
1382 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1383 return other->Is##type(); \
1384 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001385 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001386
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001387#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1388 bool Is##type() const { return As##type() != nullptr; } \
1389 const H##type* As##type() const { return this; } \
1390 H##type* As##type() { return this; }
1391
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001392template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001393class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001394 public:
David Brazdiled596192015-01-23 10:39:45 +00001395 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001396 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001397 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001398
Vladimir Marko46817b82016-03-29 12:21:58 +01001399 // Hook for the IntrusiveForwardList<>.
1400 // TODO: Hide this better.
1401 IntrusiveForwardListHook hook;
1402
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001403 private:
David Brazdiled596192015-01-23 10:39:45 +00001404 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001405 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001406
1407 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001408 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001409
Vladimir Marko46817b82016-03-29 12:21:58 +01001410 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001411
1412 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1413};
1414
David Brazdiled596192015-01-23 10:39:45 +00001415template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001416using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001417
David Brazdil1abb4192015-02-17 18:33:36 +00001418// This class is used by HEnvironment and HInstruction classes to record the
1419// instructions they use and pointers to the corresponding HUseListNodes kept
1420// by the used instructions.
1421template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001422class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001423 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001424 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1425 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001426
Vladimir Marko46817b82016-03-29 12:21:58 +01001427 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1428 : HUserRecord(old_record.instruction_, before_use_node) {}
1429 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1430 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001431 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001432 }
1433
1434 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001435 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1436 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001437
1438 private:
1439 // Instruction used by the user.
1440 HInstruction* instruction_;
1441
Vladimir Marko46817b82016-03-29 12:21:58 +01001442 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1443 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001444};
1445
Vladimir Markoe9004912016-06-16 16:50:52 +01001446// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1447// This is used for HInstruction::GetInputs() to return a container wrapper providing
1448// HInstruction* values even though the underlying container has HUserRecord<>s.
1449struct HInputExtractor {
1450 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1451 return record.GetInstruction();
1452 }
1453 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1454 return record.GetInstruction();
1455 }
1456};
1457
1458using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1459using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1460
Aart Bik854a02b2015-07-14 16:07:00 -07001461/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001462 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001463 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001464 * For write/read dependences on fields/arrays, the dependence analysis uses
1465 * type disambiguation (e.g. a float field write cannot modify the value of an
1466 * integer field read) and the access type (e.g. a reference array write cannot
1467 * modify the value of a reference field read [although it may modify the
1468 * reference fetch prior to reading the field, which is represented by its own
1469 * write/read dependence]). The analysis makes conservative points-to
1470 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1471 * the same, and any reference read depends on any reference read without
1472 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001473 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001474 * The internal representation uses 38-bit and is described in the table below.
1475 * The first line indicates the side effect, and for field/array accesses the
1476 * second line indicates the type of the access (in the order of the
1477 * Primitive::Type enum).
1478 * The two numbered lines below indicate the bit position in the bitfield (read
1479 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001480 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001481 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1482 * +-------------+---------+---------+--------------+---------+---------+
1483 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1484 * | 3 |333333322|222222221| 1 |111111110|000000000|
1485 * | 7 |654321098|765432109| 8 |765432109|876543210|
1486 *
1487 * Note that, to ease the implementation, 'changes' bits are least significant
1488 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001489 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001490class SideEffects : public ValueObject {
1491 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001492 SideEffects() : flags_(0) {}
1493
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001494 static SideEffects None() {
1495 return SideEffects(0);
1496 }
1497
1498 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001499 return SideEffects(kAllChangeBits | kAllDependOnBits);
1500 }
1501
1502 static SideEffects AllChanges() {
1503 return SideEffects(kAllChangeBits);
1504 }
1505
1506 static SideEffects AllDependencies() {
1507 return SideEffects(kAllDependOnBits);
1508 }
1509
1510 static SideEffects AllExceptGCDependency() {
1511 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1512 }
1513
1514 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001515 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001516 }
1517
Aart Bik34c3ba92015-07-20 14:08:59 -07001518 static SideEffects AllWrites() {
1519 return SideEffects(kAllWrites);
1520 }
1521
1522 static SideEffects AllReads() {
1523 return SideEffects(kAllReads);
1524 }
1525
1526 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1527 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001528 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001529 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001530 }
1531
Aart Bik854a02b2015-07-14 16:07:00 -07001532 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001533 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001534 }
1535
Aart Bik34c3ba92015-07-20 14:08:59 -07001536 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1537 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001538 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001539 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001540 }
1541
1542 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001543 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001544 }
1545
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001546 static SideEffects CanTriggerGC() {
1547 return SideEffects(1ULL << kCanTriggerGCBit);
1548 }
1549
1550 static SideEffects DependsOnGC() {
1551 return SideEffects(1ULL << kDependsOnGCBit);
1552 }
1553
Aart Bik854a02b2015-07-14 16:07:00 -07001554 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001555 SideEffects Union(SideEffects other) const {
1556 return SideEffects(flags_ | other.flags_);
1557 }
1558
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001559 SideEffects Exclusion(SideEffects other) const {
1560 return SideEffects(flags_ & ~other.flags_);
1561 }
1562
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001563 void Add(SideEffects other) {
1564 flags_ |= other.flags_;
1565 }
1566
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001567 bool Includes(SideEffects other) const {
1568 return (other.flags_ & flags_) == other.flags_;
1569 }
1570
1571 bool HasSideEffects() const {
1572 return (flags_ & kAllChangeBits);
1573 }
1574
1575 bool HasDependencies() const {
1576 return (flags_ & kAllDependOnBits);
1577 }
1578
1579 // Returns true if there are no side effects or dependencies.
1580 bool DoesNothing() const {
1581 return flags_ == 0;
1582 }
1583
Aart Bik854a02b2015-07-14 16:07:00 -07001584 // Returns true if something is written.
1585 bool DoesAnyWrite() const {
1586 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001587 }
1588
Aart Bik854a02b2015-07-14 16:07:00 -07001589 // Returns true if something is read.
1590 bool DoesAnyRead() const {
1591 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001592 }
1593
Aart Bik854a02b2015-07-14 16:07:00 -07001594 // Returns true if potentially everything is written and read
1595 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001596 bool DoesAllReadWrite() const {
1597 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1598 }
1599
Aart Bik854a02b2015-07-14 16:07:00 -07001600 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001601 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001602 }
1603
Roland Levillain0d5a2812015-11-13 10:07:31 +00001604 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001605 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001606 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1607 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001608 }
1609
1610 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001611 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001612 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001613 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001614 for (int s = kLastBit; s >= 0; s--) {
1615 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1616 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1617 // This is a bit for the GC side effect.
1618 if (current_bit_is_set) {
1619 flags += "GC";
1620 }
Aart Bik854a02b2015-07-14 16:07:00 -07001621 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001622 } else {
1623 // This is a bit for the array/field analysis.
1624 // The underscore character stands for the 'can trigger GC' bit.
1625 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1626 if (current_bit_is_set) {
1627 flags += kDebug[s];
1628 }
1629 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1630 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1631 flags += "|";
1632 }
1633 }
Aart Bik854a02b2015-07-14 16:07:00 -07001634 }
1635 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001636 }
1637
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001638 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001639
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001640 private:
1641 static constexpr int kFieldArrayAnalysisBits = 9;
1642
1643 static constexpr int kFieldWriteOffset = 0;
1644 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1645 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1646 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1647
1648 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1649
1650 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1651 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1652 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1653 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1654
1655 static constexpr int kLastBit = kDependsOnGCBit;
1656 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1657
1658 // Aliases.
1659
1660 static_assert(kChangeBits == kDependOnBits,
1661 "the 'change' bits should match the 'depend on' bits.");
1662
1663 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1664 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1665 static constexpr uint64_t kAllWrites =
1666 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1667 static constexpr uint64_t kAllReads =
1668 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001669
Aart Bik854a02b2015-07-14 16:07:00 -07001670 // Translates type to bit flag.
1671 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1672 CHECK_NE(type, Primitive::kPrimVoid);
1673 const uint64_t one = 1;
1674 const int shift = type; // 0-based consecutive enum
1675 DCHECK_LE(kFieldWriteOffset, shift);
1676 DCHECK_LT(shift, kArrayWriteOffset);
1677 return one << (type + offset);
1678 }
1679
1680 // Private constructor on direct flags value.
1681 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1682
1683 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001684};
1685
David Brazdiled596192015-01-23 10:39:45 +00001686// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001687class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001688 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001689 HEnvironment(ArenaAllocator* arena,
1690 size_t number_of_vregs,
1691 const DexFile& dex_file,
1692 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001693 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001694 InvokeType invoke_type,
1695 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001696 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1697 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001698 parent_(nullptr),
1699 dex_file_(dex_file),
1700 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001701 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001702 invoke_type_(invoke_type),
1703 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001704 }
1705
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001706 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001707 : HEnvironment(arena,
1708 to_copy.Size(),
1709 to_copy.GetDexFile(),
1710 to_copy.GetMethodIdx(),
1711 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001712 to_copy.GetInvokeType(),
1713 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001714
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001715 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001716 if (parent_ != nullptr) {
1717 parent_->SetAndCopyParentChain(allocator, parent);
1718 } else {
1719 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1720 parent_->CopyFrom(parent);
1721 if (parent->GetParent() != nullptr) {
1722 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1723 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001724 }
David Brazdiled596192015-01-23 10:39:45 +00001725 }
1726
Vladimir Marko71bf8092015-09-15 15:33:14 +01001727 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001728 void CopyFrom(HEnvironment* environment);
1729
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001730 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1731 // input to the loop phi instead. This is for inserting instructions that
1732 // require an environment (like HDeoptimization) in the loop pre-header.
1733 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001734
1735 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001736 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001737 }
1738
1739 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001740 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001741 }
1742
David Brazdil1abb4192015-02-17 18:33:36 +00001743 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001744
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001745 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001746
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001747 HEnvironment* GetParent() const { return parent_; }
1748
1749 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001750 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001751 }
1752
1753 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001754 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001755 }
1756
1757 uint32_t GetDexPc() const {
1758 return dex_pc_;
1759 }
1760
1761 uint32_t GetMethodIdx() const {
1762 return method_idx_;
1763 }
1764
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001765 InvokeType GetInvokeType() const {
1766 return invoke_type_;
1767 }
1768
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001769 const DexFile& GetDexFile() const {
1770 return dex_file_;
1771 }
1772
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001773 HInstruction* GetHolder() const {
1774 return holder_;
1775 }
1776
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001777
1778 bool IsFromInlinedInvoke() const {
1779 return GetParent() != nullptr;
1780 }
1781
David Brazdiled596192015-01-23 10:39:45 +00001782 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001783 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1784 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001785 HEnvironment* parent_;
1786 const DexFile& dex_file_;
1787 const uint32_t method_idx_;
1788 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001789 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001790
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001791 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001792 HInstruction* const holder_;
1793
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001794 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001795
1796 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1797};
1798
Vladimir Markof9f64412015-09-02 14:05:49 +01001799class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001800 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001801 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001802 : previous_(nullptr),
1803 next_(nullptr),
1804 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001805 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001806 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001807 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001808 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001809 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001810 locations_(nullptr),
1811 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001812 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001813 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001814 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1815 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1816 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001817
Dave Allison20dfc792014-06-16 20:44:29 -07001818 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001819
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001820#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001821 enum InstructionKind {
1822 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1823 };
1824#undef DECLARE_KIND
1825
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001826 HInstruction* GetNext() const { return next_; }
1827 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001828
Calin Juravle77520bc2015-01-12 18:45:46 +00001829 HInstruction* GetNextDisregardingMoves() const;
1830 HInstruction* GetPreviousDisregardingMoves() const;
1831
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001832 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001833 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001834 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001835 bool IsInBlock() const { return block_ != nullptr; }
1836 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001837 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1838 bool IsIrreducibleLoopHeaderPhi() const {
1839 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1840 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001841
Vladimir Marko372f10e2016-05-17 16:30:10 +01001842 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1843
1844 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1845 // One virtual method is enough, just const_cast<> and then re-add the const.
1846 return ArrayRef<const HUserRecord<HInstruction*>>(
1847 const_cast<HInstruction*>(this)->GetInputRecords());
1848 }
1849
Vladimir Markoe9004912016-06-16 16:50:52 +01001850 HInputsRef GetInputs() {
1851 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001852 }
1853
Vladimir Markoe9004912016-06-16 16:50:52 +01001854 HConstInputsRef GetInputs() const {
1855 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001856 }
1857
1858 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001859 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001860
Aart Bik2767f4b2016-10-28 15:03:53 -07001861 bool HasInput(HInstruction* input) const {
1862 for (const HInstruction* i : GetInputs()) {
1863 if (i == input) {
1864 return true;
1865 }
1866 }
1867 return false;
1868 }
1869
Vladimir Marko372f10e2016-05-17 16:30:10 +01001870 void SetRawInputAt(size_t index, HInstruction* input) {
1871 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1872 }
1873
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001874 virtual void Accept(HGraphVisitor* visitor) = 0;
1875 virtual const char* DebugName() const = 0;
1876
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001877 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1878
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001879 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001880
1881 uint32_t GetDexPc() const { return dex_pc_; }
1882
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001883 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001884
Roland Levillaine161a2a2014-10-03 12:45:18 +01001885 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001886 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001887
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001888 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001889 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001890
Calin Juravle10e244f2015-01-26 18:54:32 +00001891 // Does not apply for all instructions, but having this at top level greatly
1892 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001893 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001894 virtual bool CanBeNull() const {
1895 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1896 return true;
1897 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001898
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001899 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001900 return false;
1901 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001902
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001903 virtual bool IsActualObject() const {
1904 return GetType() == Primitive::kPrimNot;
1905 }
1906
Calin Juravle2e768302015-07-28 14:41:11 +00001907 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001908
Calin Juravle61d544b2015-02-23 16:46:57 +00001909 ReferenceTypeInfo GetReferenceTypeInfo() const {
1910 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001911 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001912 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001913 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001914
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001915 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001916 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001917 // Note: fixup_end remains valid across push_front().
1918 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1919 HUseListNode<HInstruction*>* new_node =
1920 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1921 uses_.push_front(*new_node);
1922 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001923 }
1924
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001925 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001926 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001927 // Note: env_fixup_end remains valid across push_front().
1928 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1929 HUseListNode<HEnvironment*>* new_node =
1930 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1931 env_uses_.push_front(*new_node);
1932 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001933 }
1934
David Brazdil1abb4192015-02-17 18:33:36 +00001935 void RemoveAsUserOfInput(size_t input) {
1936 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001937 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1938 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1939 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001940 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001941
Vladimir Marko372f10e2016-05-17 16:30:10 +01001942 void RemoveAsUserOfAllInputs() {
1943 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1944 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1945 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1946 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1947 }
1948 }
1949
David Brazdil1abb4192015-02-17 18:33:36 +00001950 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1951 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001952
Vladimir Marko46817b82016-03-29 12:21:58 +01001953 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1954 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1955 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001956 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001957 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001958 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001959
Aart Bikcc42be02016-10-20 16:14:16 -07001960 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07001961 return
Aart Bikff7d89c2016-11-07 08:49:28 -08001962 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07001963 !CanThrow() &&
1964 !IsSuspendCheck() &&
1965 !IsControlFlow() &&
1966 !IsNativeDebugInfo() &&
1967 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07001968 // If we added an explicit barrier then we should keep it.
1969 !IsMemoryBarrier();
1970 }
1971
Aart Bikcc42be02016-10-20 16:14:16 -07001972 bool IsDeadAndRemovable() const {
1973 return IsRemovable() && !HasUses();
1974 }
1975
Roland Levillain6c82d402014-10-13 16:10:27 +01001976 // Does this instruction strictly dominate `other_instruction`?
1977 // Returns false if this instruction and `other_instruction` are the same.
1978 // Aborts if this instruction and `other_instruction` are both phis.
1979 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001980
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001981 int GetId() const { return id_; }
1982 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001983
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001984 int GetSsaIndex() const { return ssa_index_; }
1985 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1986 bool HasSsaIndex() const { return ssa_index_ != -1; }
1987
1988 bool HasEnvironment() const { return environment_ != nullptr; }
1989 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001990 // Set the `environment_` field. Raw because this method does not
1991 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001992 void SetRawEnvironment(HEnvironment* environment) {
1993 DCHECK(environment_ == nullptr);
1994 DCHECK_EQ(environment->GetHolder(), this);
1995 environment_ = environment;
1996 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001997
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001998 void InsertRawEnvironment(HEnvironment* environment) {
1999 DCHECK(environment_ != nullptr);
2000 DCHECK_EQ(environment->GetHolder(), this);
2001 DCHECK(environment->GetParent() == nullptr);
2002 environment->parent_ = environment_;
2003 environment_ = environment;
2004 }
2005
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002006 void RemoveEnvironment();
2007
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002008 // Set the environment of this instruction, copying it from `environment`. While
2009 // copying, the uses lists are being updated.
2010 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002011 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002012 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002013 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002014 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002015 if (environment->GetParent() != nullptr) {
2016 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2017 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002018 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002019
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002020 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2021 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002022 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002023 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002024 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002025 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002026 if (environment->GetParent() != nullptr) {
2027 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2028 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002029 }
2030
Nicolas Geoffray39468442014-09-02 15:17:15 +01002031 // Returns the number of entries in the environment. Typically, that is the
2032 // number of dex registers in a method. It could be more in case of inlining.
2033 size_t EnvironmentSize() const;
2034
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002035 LocationSummary* GetLocations() const { return locations_; }
2036 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002037
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002038 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002039 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002040
Alexandre Rames188d4312015-04-09 18:30:21 +01002041 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2042 // uses of this instruction by `other` are *not* updated.
2043 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2044 ReplaceWith(other);
2045 other->ReplaceInput(this, use_index);
2046 }
2047
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002048 // Move `this` instruction before `cursor`.
2049 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002050
Vladimir Markofb337ea2015-11-25 15:25:10 +00002051 // Move `this` before its first user and out of any loops. If there is no
2052 // out-of-loop user that dominates all other users, move the instruction
2053 // to the end of the out-of-loop common dominator of the user's blocks.
2054 //
2055 // This can be used only on non-throwing instructions with no side effects that
2056 // have at least one use but no environment uses.
2057 void MoveBeforeFirstUserAndOutOfLoops();
2058
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002059#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002060 bool Is##type() const; \
2061 const H##type* As##type() const; \
2062 H##type* As##type();
2063
2064 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2065#undef INSTRUCTION_TYPE_CHECK
2066
2067#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002068 bool Is##type() const { return (As##type() != nullptr); } \
2069 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002070 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002071 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002072#undef INSTRUCTION_TYPE_CHECK
2073
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002074 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002075 // TODO: this method is used by LICM and GVN with possibly different
2076 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002077 virtual bool CanBeMoved() const { return false; }
2078
2079 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002080 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002081 return false;
2082 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002083
2084 // Returns whether any data encoded in the two instructions is equal.
2085 // This method does not look at the inputs. Both instructions must be
2086 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002087 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002088 return false;
2089 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002090
2091 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002092 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002093 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002094 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002095
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002096 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2097 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2098 // the virtual function because the __attribute__((__pure__)) doesn't really
2099 // apply the strong requirement for virtual functions, preventing optimizations.
2100 InstructionKind GetKind() const PURE;
2101 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002102
2103 virtual size_t ComputeHashCode() const {
2104 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002105 for (const HInstruction* input : GetInputs()) {
2106 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002107 }
2108 return result;
2109 }
2110
2111 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002112 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002113 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002114
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002115 size_t GetLifetimePosition() const { return lifetime_position_; }
2116 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2117 LiveInterval* GetLiveInterval() const { return live_interval_; }
2118 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2119 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2120
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002121 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2122
2123 // Returns whether the code generation of the instruction will require to have access
2124 // to the current method. Such instructions are:
2125 // (1): Instructions that require an environment, as calling the runtime requires
2126 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002127 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2128 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002129 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002130 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002131 }
2132
Vladimir Markodc151b22015-10-15 18:02:30 +01002133 // Returns whether the code generation of the instruction will require to have access
2134 // to the dex cache of the current method's declaring class via the current method.
2135 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002136
Mark Mendellc4701932015-04-10 13:18:51 -04002137 // Does this instruction have any use in an environment before
2138 // control flow hits 'other'?
2139 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2140
2141 // Remove all references to environment uses of this instruction.
2142 // The caller must ensure that this is safe to do.
2143 void RemoveEnvironmentUsers();
2144
Vladimir Markoa1de9182016-02-25 11:37:38 +00002145 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2146 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002147
David Brazdil1abb4192015-02-17 18:33:36 +00002148 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002149 // If set, the machine code for this instruction is assumed to be generated by
2150 // its users. Used by liveness analysis to compute use positions accordingly.
2151 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2152 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2153 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2154 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2155
Vladimir Marko372f10e2016-05-17 16:30:10 +01002156 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2157 return GetInputRecords()[i];
2158 }
2159
2160 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2161 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2162 input_records[index] = input;
2163 }
David Brazdil1abb4192015-02-17 18:33:36 +00002164
Vladimir Markoa1de9182016-02-25 11:37:38 +00002165 uint32_t GetPackedFields() const {
2166 return packed_fields_;
2167 }
2168
2169 template <size_t flag>
2170 bool GetPackedFlag() const {
2171 return (packed_fields_ & (1u << flag)) != 0u;
2172 }
2173
2174 template <size_t flag>
2175 void SetPackedFlag(bool value = true) {
2176 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2177 }
2178
2179 template <typename BitFieldType>
2180 typename BitFieldType::value_type GetPackedField() const {
2181 return BitFieldType::Decode(packed_fields_);
2182 }
2183
2184 template <typename BitFieldType>
2185 void SetPackedField(typename BitFieldType::value_type value) {
2186 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2187 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2188 }
2189
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002190 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002191 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2192 auto before_use_node = uses_.before_begin();
2193 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2194 HInstruction* user = use_node->GetUser();
2195 size_t input_index = use_node->GetIndex();
2196 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2197 before_use_node = use_node;
2198 }
2199 }
2200
2201 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2202 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2203 if (next != uses_.end()) {
2204 HInstruction* next_user = next->GetUser();
2205 size_t next_index = next->GetIndex();
2206 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2207 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2208 }
2209 }
2210
2211 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2212 auto before_env_use_node = env_uses_.before_begin();
2213 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2214 HEnvironment* user = env_use_node->GetUser();
2215 size_t input_index = env_use_node->GetIndex();
2216 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2217 before_env_use_node = env_use_node;
2218 }
2219 }
2220
2221 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2222 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2223 if (next != env_uses_.end()) {
2224 HEnvironment* next_user = next->GetUser();
2225 size_t next_index = next->GetIndex();
2226 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2227 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2228 }
2229 }
David Brazdil1abb4192015-02-17 18:33:36 +00002230
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002231 HInstruction* previous_;
2232 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002233 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002234 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002235
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002236 // An instruction gets an id when it is added to the graph.
2237 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002238 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002239 int id_;
2240
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002241 // When doing liveness analysis, instructions that have uses get an SSA index.
2242 int ssa_index_;
2243
Vladimir Markoa1de9182016-02-25 11:37:38 +00002244 // Packed fields.
2245 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002246
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002247 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002248 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002249
2250 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002251 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002252
Nicolas Geoffray39468442014-09-02 15:17:15 +01002253 // The environment associated with this instruction. Not null if the instruction
2254 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002255 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002256
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002257 // Set by the code generator.
2258 LocationSummary* locations_;
2259
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002260 // Set by the liveness analysis.
2261 LiveInterval* live_interval_;
2262
2263 // Set by the liveness analysis, this is the position in a linear
2264 // order of blocks where this instruction's live interval start.
2265 size_t lifetime_position_;
2266
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002267 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002268
Vladimir Markoa1de9182016-02-25 11:37:38 +00002269 // The reference handle part of the reference type info.
2270 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002271 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002272 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002273
David Brazdil1abb4192015-02-17 18:33:36 +00002274 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002275 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002276 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002277 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002278 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002279
2280 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2281};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002282std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002283
2284class HInstructionIterator : public ValueObject {
2285 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002286 explicit HInstructionIterator(const HInstructionList& instructions)
2287 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002288 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002289 }
2290
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002291 bool Done() const { return instruction_ == nullptr; }
2292 HInstruction* Current() const { return instruction_; }
2293 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002294 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002295 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002296 }
2297
2298 private:
2299 HInstruction* instruction_;
2300 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002301
2302 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002303};
2304
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002305class HBackwardInstructionIterator : public ValueObject {
2306 public:
2307 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2308 : instruction_(instructions.last_instruction_) {
2309 next_ = Done() ? nullptr : instruction_->GetPrevious();
2310 }
2311
2312 bool Done() const { return instruction_ == nullptr; }
2313 HInstruction* Current() const { return instruction_; }
2314 void Advance() {
2315 instruction_ = next_;
2316 next_ = Done() ? nullptr : instruction_->GetPrevious();
2317 }
2318
2319 private:
2320 HInstruction* instruction_;
2321 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002322
2323 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002324};
2325
Vladimir Markof9f64412015-09-02 14:05:49 +01002326template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002327class HTemplateInstruction: public HInstruction {
2328 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002329 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002330 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002331 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002332
Vladimir Marko372f10e2016-05-17 16:30:10 +01002333 using HInstruction::GetInputRecords; // Keep the const version visible.
2334 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2335 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002336 }
2337
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002338 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002339 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002340
2341 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002342};
2343
Vladimir Markof9f64412015-09-02 14:05:49 +01002344// HTemplateInstruction specialization for N=0.
2345template<>
2346class HTemplateInstruction<0>: public HInstruction {
2347 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002348 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002349 : HInstruction(side_effects, dex_pc) {}
2350
Vladimir Markof9f64412015-09-02 14:05:49 +01002351 virtual ~HTemplateInstruction() {}
2352
Vladimir Marko372f10e2016-05-17 16:30:10 +01002353 using HInstruction::GetInputRecords; // Keep the const version visible.
2354 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2355 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002356 }
2357
2358 private:
2359 friend class SsaBuilder;
2360};
2361
Dave Allison20dfc792014-06-16 20:44:29 -07002362template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002363class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002364 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002365 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002366 : HTemplateInstruction<N>(side_effects, dex_pc) {
2367 this->template SetPackedField<TypeField>(type);
2368 }
Dave Allison20dfc792014-06-16 20:44:29 -07002369 virtual ~HExpression() {}
2370
Vladimir Markoa1de9182016-02-25 11:37:38 +00002371 Primitive::Type GetType() const OVERRIDE {
2372 return TypeField::Decode(this->GetPackedFields());
2373 }
Dave Allison20dfc792014-06-16 20:44:29 -07002374
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002375 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002376 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2377 static constexpr size_t kFieldTypeSize =
2378 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2379 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2380 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2381 "Too many packed fields.");
2382 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002383};
2384
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002385// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2386// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002387class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002388 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002389 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2390 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002391
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002392 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002393
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002394 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002395
2396 private:
2397 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2398};
2399
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002400// Represents dex's RETURN opcodes. A HReturn is a control flow
2401// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002402class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002403 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002404 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2405 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002406 SetRawInputAt(0, value);
2407 }
2408
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002409 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002410
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002411 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002412
2413 private:
2414 DISALLOW_COPY_AND_ASSIGN(HReturn);
2415};
2416
Vladimir Markofcb503c2016-05-18 12:48:17 +01002417class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002418 public:
2419 HPhi(ArenaAllocator* arena,
2420 uint32_t reg_number,
2421 size_t number_of_inputs,
2422 Primitive::Type type,
2423 uint32_t dex_pc = kNoDexPc)
2424 : HInstruction(SideEffects::None(), dex_pc),
2425 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2426 reg_number_(reg_number) {
2427 SetPackedField<TypeField>(ToPhiType(type));
2428 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2429 // Phis are constructed live and marked dead if conflicting or unused.
2430 // Individual steps of SsaBuilder should assume that if a phi has been
2431 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2432 SetPackedFlag<kFlagIsLive>(true);
2433 SetPackedFlag<kFlagCanBeNull>(true);
2434 }
2435
2436 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2437 static Primitive::Type ToPhiType(Primitive::Type type) {
2438 return Primitive::PrimitiveKind(type);
2439 }
2440
2441 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2442
Vladimir Marko372f10e2016-05-17 16:30:10 +01002443 using HInstruction::GetInputRecords; // Keep the const version visible.
2444 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2445 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2446 }
David Brazdildee58d62016-04-07 09:54:26 +00002447
2448 void AddInput(HInstruction* input);
2449 void RemoveInputAt(size_t index);
2450
2451 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2452 void SetType(Primitive::Type new_type) {
2453 // Make sure that only valid type changes occur. The following are allowed:
2454 // (1) int -> float/ref (primitive type propagation),
2455 // (2) long -> double (primitive type propagation).
2456 DCHECK(GetType() == new_type ||
2457 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2458 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2459 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2460 SetPackedField<TypeField>(new_type);
2461 }
2462
2463 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2464 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2465
2466 uint32_t GetRegNumber() const { return reg_number_; }
2467
2468 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2469 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2470 bool IsDead() const { return !IsLive(); }
2471 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2472
Vladimir Markoe9004912016-06-16 16:50:52 +01002473 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002474 return other != nullptr
2475 && other->IsPhi()
2476 && other->AsPhi()->GetBlock() == GetBlock()
2477 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2478 }
2479
2480 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2481 // An equivalent phi is a phi having the same dex register and type.
2482 // It assumes that phis with the same dex register are adjacent.
2483 HPhi* GetNextEquivalentPhiWithSameType() {
2484 HInstruction* next = GetNext();
2485 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2486 if (next->GetType() == GetType()) {
2487 return next->AsPhi();
2488 }
2489 next = next->GetNext();
2490 }
2491 return nullptr;
2492 }
2493
2494 DECLARE_INSTRUCTION(Phi);
2495
David Brazdildee58d62016-04-07 09:54:26 +00002496 private:
2497 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2498 static constexpr size_t kFieldTypeSize =
2499 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2500 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2501 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2502 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2503 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2504 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2505
Vladimir Marko372f10e2016-05-17 16:30:10 +01002506 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002507 const uint32_t reg_number_;
2508
2509 DISALLOW_COPY_AND_ASSIGN(HPhi);
2510};
2511
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002512// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002513// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002514// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002515class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002516 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002517 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002518
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002519 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002520
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002521 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002522
2523 private:
2524 DISALLOW_COPY_AND_ASSIGN(HExit);
2525};
2526
2527// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002528class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002529 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002530 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002531
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002532 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002533
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002534 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002535 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002536 }
2537
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002538 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002539
2540 private:
2541 DISALLOW_COPY_AND_ASSIGN(HGoto);
2542};
2543
Roland Levillain9867bc72015-08-05 10:21:34 +01002544class HConstant : public HExpression<0> {
2545 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002546 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2547 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002548
2549 bool CanBeMoved() const OVERRIDE { return true; }
2550
Roland Levillain1a653882016-03-18 18:05:57 +00002551 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002552 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002553 // Is this constant 0 in the arithmetic sense?
2554 virtual bool IsArithmeticZero() const { return false; }
2555 // Is this constant a 0-bit pattern?
2556 virtual bool IsZeroBitPattern() const { return false; }
2557 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002558 virtual bool IsOne() const { return false; }
2559
David Brazdil77a48ae2015-09-15 12:34:04 +00002560 virtual uint64_t GetValueAsUint64() const = 0;
2561
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002562 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002563
2564 private:
2565 DISALLOW_COPY_AND_ASSIGN(HConstant);
2566};
2567
Vladimir Markofcb503c2016-05-18 12:48:17 +01002568class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002569 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002570 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002571 return true;
2572 }
2573
David Brazdil77a48ae2015-09-15 12:34:04 +00002574 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2575
Roland Levillain9867bc72015-08-05 10:21:34 +01002576 size_t ComputeHashCode() const OVERRIDE { return 0; }
2577
Roland Levillain1a653882016-03-18 18:05:57 +00002578 // The null constant representation is a 0-bit pattern.
2579 virtual bool IsZeroBitPattern() const { return true; }
2580
Roland Levillain9867bc72015-08-05 10:21:34 +01002581 DECLARE_INSTRUCTION(NullConstant);
2582
2583 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002584 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002585
2586 friend class HGraph;
2587 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2588};
2589
2590// Constants of the type int. Those can be from Dex instructions, or
2591// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002592class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002593 public:
2594 int32_t GetValue() const { return value_; }
2595
David Brazdil9f389d42015-10-01 14:32:56 +01002596 uint64_t GetValueAsUint64() const OVERRIDE {
2597 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2598 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002599
Vladimir Marko372f10e2016-05-17 16:30:10 +01002600 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002601 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002602 return other->AsIntConstant()->value_ == value_;
2603 }
2604
2605 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2606
2607 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002608 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2609 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002610 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2611
Roland Levillain1a653882016-03-18 18:05:57 +00002612 // Integer constants are used to encode Boolean values as well,
2613 // where 1 means true and 0 means false.
2614 bool IsTrue() const { return GetValue() == 1; }
2615 bool IsFalse() const { return GetValue() == 0; }
2616
Roland Levillain9867bc72015-08-05 10:21:34 +01002617 DECLARE_INSTRUCTION(IntConstant);
2618
2619 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002620 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2621 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2622 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2623 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002624
2625 const int32_t value_;
2626
2627 friend class HGraph;
2628 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2629 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2630 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2631};
2632
Vladimir Markofcb503c2016-05-18 12:48:17 +01002633class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002634 public:
2635 int64_t GetValue() const { return value_; }
2636
David Brazdil77a48ae2015-09-15 12:34:04 +00002637 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2638
Vladimir Marko372f10e2016-05-17 16:30:10 +01002639 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002640 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002641 return other->AsLongConstant()->value_ == value_;
2642 }
2643
2644 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2645
2646 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002647 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2648 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002649 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2650
2651 DECLARE_INSTRUCTION(LongConstant);
2652
2653 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002654 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2655 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002656
2657 const int64_t value_;
2658
2659 friend class HGraph;
2660 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2661};
Dave Allison20dfc792014-06-16 20:44:29 -07002662
Vladimir Markofcb503c2016-05-18 12:48:17 +01002663class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002664 public:
2665 float GetValue() const { return value_; }
2666
2667 uint64_t GetValueAsUint64() const OVERRIDE {
2668 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2669 }
2670
Vladimir Marko372f10e2016-05-17 16:30:10 +01002671 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002672 DCHECK(other->IsFloatConstant()) << other->DebugName();
2673 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2674 }
2675
2676 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2677
2678 bool IsMinusOne() const OVERRIDE {
2679 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2680 }
Roland Levillain1a653882016-03-18 18:05:57 +00002681 bool IsArithmeticZero() const OVERRIDE {
2682 return std::fpclassify(value_) == FP_ZERO;
2683 }
2684 bool IsArithmeticPositiveZero() const {
2685 return IsArithmeticZero() && !std::signbit(value_);
2686 }
2687 bool IsArithmeticNegativeZero() const {
2688 return IsArithmeticZero() && std::signbit(value_);
2689 }
2690 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002691 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002692 }
2693 bool IsOne() const OVERRIDE {
2694 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2695 }
2696 bool IsNaN() const {
2697 return std::isnan(value_);
2698 }
2699
2700 DECLARE_INSTRUCTION(FloatConstant);
2701
2702 private:
2703 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2704 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2705 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2706 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2707
2708 const float value_;
2709
2710 // Only the SsaBuilder and HGraph can create floating-point constants.
2711 friend class SsaBuilder;
2712 friend class HGraph;
2713 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2714};
2715
Vladimir Markofcb503c2016-05-18 12:48:17 +01002716class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002717 public:
2718 double GetValue() const { return value_; }
2719
2720 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2721
Vladimir Marko372f10e2016-05-17 16:30:10 +01002722 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002723 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2724 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2725 }
2726
2727 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2728
2729 bool IsMinusOne() const OVERRIDE {
2730 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2731 }
Roland Levillain1a653882016-03-18 18:05:57 +00002732 bool IsArithmeticZero() const OVERRIDE {
2733 return std::fpclassify(value_) == FP_ZERO;
2734 }
2735 bool IsArithmeticPositiveZero() const {
2736 return IsArithmeticZero() && !std::signbit(value_);
2737 }
2738 bool IsArithmeticNegativeZero() const {
2739 return IsArithmeticZero() && std::signbit(value_);
2740 }
2741 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002742 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002743 }
2744 bool IsOne() const OVERRIDE {
2745 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2746 }
2747 bool IsNaN() const {
2748 return std::isnan(value_);
2749 }
2750
2751 DECLARE_INSTRUCTION(DoubleConstant);
2752
2753 private:
2754 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2755 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2756 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2757 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2758
2759 const double value_;
2760
2761 // Only the SsaBuilder and HGraph can create floating-point constants.
2762 friend class SsaBuilder;
2763 friend class HGraph;
2764 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2765};
2766
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002767// Conditional branch. A block ending with an HIf instruction must have
2768// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002769class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002770 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002771 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2772 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002773 SetRawInputAt(0, input);
2774 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002775
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002776 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002777
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002778 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002779 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002780 }
2781
2782 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002783 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002784 }
2785
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002786 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002787
2788 private:
2789 DISALLOW_COPY_AND_ASSIGN(HIf);
2790};
2791
David Brazdilfc6a86a2015-06-26 10:33:45 +00002792
2793// Abstract instruction which marks the beginning and/or end of a try block and
2794// links it to the respective exception handlers. Behaves the same as a Goto in
2795// non-exceptional control flow.
2796// Normal-flow successor is stored at index zero, exception handlers under
2797// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002798class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002799 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002800 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002801 kEntry,
2802 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002803 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002804 };
2805
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002806 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002807 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2808 SetPackedField<BoundaryKindField>(kind);
2809 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002810
2811 bool IsControlFlow() const OVERRIDE { return true; }
2812
2813 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002814 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002815
David Brazdild26a4112015-11-10 11:07:31 +00002816 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2817 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2818 }
2819
David Brazdilfc6a86a2015-06-26 10:33:45 +00002820 // Returns whether `handler` is among its exception handlers (non-zero index
2821 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002822 bool HasExceptionHandler(const HBasicBlock& handler) const {
2823 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002824 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002825 }
2826
2827 // If not present already, adds `handler` to its block's list of exception
2828 // handlers.
2829 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002830 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002831 GetBlock()->AddSuccessor(handler);
2832 }
2833 }
2834
Vladimir Markoa1de9182016-02-25 11:37:38 +00002835 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2836 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002837
David Brazdilffee3d32015-07-06 11:48:53 +01002838 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2839
David Brazdilfc6a86a2015-06-26 10:33:45 +00002840 DECLARE_INSTRUCTION(TryBoundary);
2841
2842 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002843 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2844 static constexpr size_t kFieldBoundaryKindSize =
2845 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2846 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2847 kFieldBoundaryKind + kFieldBoundaryKindSize;
2848 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2849 "Too many packed fields.");
2850 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002851
2852 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2853};
2854
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002855// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002856class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002857 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002858 // We set CanTriggerGC to prevent any intermediate address to be live
2859 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002860 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002861 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002862 SetRawInputAt(0, cond);
2863 }
2864
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002865 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002866 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002867 return true;
2868 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002869 bool NeedsEnvironment() const OVERRIDE { return true; }
2870 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002871
2872 DECLARE_INSTRUCTION(Deoptimize);
2873
2874 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002875 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2876};
2877
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002878// Represents the ArtMethod that was passed as a first argument to
2879// the method. It is used by instructions that depend on it, like
2880// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002881class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002882 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002883 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2884 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002885
2886 DECLARE_INSTRUCTION(CurrentMethod);
2887
2888 private:
2889 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2890};
2891
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002892// Fetches an ArtMethod from the virtual table or the interface method table
2893// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002894class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002895 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002896 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002897 kVTable,
2898 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002899 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002900 };
2901 HClassTableGet(HInstruction* cls,
2902 Primitive::Type type,
2903 TableKind kind,
2904 size_t index,
2905 uint32_t dex_pc)
2906 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002907 index_(index) {
2908 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002909 SetRawInputAt(0, cls);
2910 }
2911
2912 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002913 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002914 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002915 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002916 }
2917
Vladimir Markoa1de9182016-02-25 11:37:38 +00002918 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002919 size_t GetIndex() const { return index_; }
2920
2921 DECLARE_INSTRUCTION(ClassTableGet);
2922
2923 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002924 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2925 static constexpr size_t kFieldTableKindSize =
2926 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2927 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2928 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2929 "Too many packed fields.");
2930 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2931
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002932 // The index of the ArtMethod in the table.
2933 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002934
2935 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2936};
2937
Mark Mendellfe57faa2015-09-18 09:26:15 -04002938// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2939// have one successor for each entry in the switch table, and the final successor
2940// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002941class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002942 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002943 HPackedSwitch(int32_t start_value,
2944 uint32_t num_entries,
2945 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002946 uint32_t dex_pc = kNoDexPc)
2947 : HTemplateInstruction(SideEffects::None(), dex_pc),
2948 start_value_(start_value),
2949 num_entries_(num_entries) {
2950 SetRawInputAt(0, input);
2951 }
2952
2953 bool IsControlFlow() const OVERRIDE { return true; }
2954
2955 int32_t GetStartValue() const { return start_value_; }
2956
Vladimir Marko211c2112015-09-24 16:52:33 +01002957 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002958
2959 HBasicBlock* GetDefaultBlock() const {
2960 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002961 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002962 }
2963 DECLARE_INSTRUCTION(PackedSwitch);
2964
2965 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002966 const int32_t start_value_;
2967 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002968
2969 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2970};
2971
Roland Levillain88cb1752014-10-20 16:36:47 +01002972class HUnaryOperation : public HExpression<1> {
2973 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002974 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2975 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002976 SetRawInputAt(0, input);
2977 }
2978
2979 HInstruction* GetInput() const { return InputAt(0); }
2980 Primitive::Type GetResultType() const { return GetType(); }
2981
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002982 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002983 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002984 return true;
2985 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002986
Roland Levillain31dd3d62016-02-16 12:21:02 +00002987 // Try to statically evaluate `this` and return a HConstant
2988 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002989 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002990 HConstant* TryStaticEvaluation() const;
2991
2992 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002993 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2994 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002995 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2996 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002997
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002998 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002999
3000 private:
3001 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3002};
3003
Dave Allison20dfc792014-06-16 20:44:29 -07003004class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003005 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003006 HBinaryOperation(Primitive::Type result_type,
3007 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003008 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003009 SideEffects side_effects = SideEffects::None(),
3010 uint32_t dex_pc = kNoDexPc)
3011 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003012 SetRawInputAt(0, left);
3013 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003014 }
3015
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003016 HInstruction* GetLeft() const { return InputAt(0); }
3017 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003018 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003019
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003020 virtual bool IsCommutative() const { return false; }
3021
3022 // Put constant on the right.
3023 // Returns whether order is changed.
3024 bool OrderInputsWithConstantOnTheRight() {
3025 HInstruction* left = InputAt(0);
3026 HInstruction* right = InputAt(1);
3027 if (left->IsConstant() && !right->IsConstant()) {
3028 ReplaceInput(right, 0);
3029 ReplaceInput(left, 1);
3030 return true;
3031 }
3032 return false;
3033 }
3034
3035 // Order inputs by instruction id, but favor constant on the right side.
3036 // This helps GVN for commutative ops.
3037 void OrderInputs() {
3038 DCHECK(IsCommutative());
3039 HInstruction* left = InputAt(0);
3040 HInstruction* right = InputAt(1);
3041 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3042 return;
3043 }
3044 if (OrderInputsWithConstantOnTheRight()) {
3045 return;
3046 }
3047 // Order according to instruction id.
3048 if (left->GetId() > right->GetId()) {
3049 ReplaceInput(right, 0);
3050 ReplaceInput(left, 1);
3051 }
3052 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003053
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003054 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003055 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003056 return true;
3057 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003058
Roland Levillain31dd3d62016-02-16 12:21:02 +00003059 // Try to statically evaluate `this` and return a HConstant
3060 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003061 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003062 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003063
3064 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003065 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3066 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003067 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3068 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003069 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003070 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3071 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003072 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3073 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003074 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3075 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003076 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003077 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3078 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003079
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003080 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003081 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003082 HConstant* GetConstantRight() const;
3083
3084 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003085 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003086 HInstruction* GetLeastConstantLeft() const;
3087
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003088 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003089
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003090 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003091 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3092};
3093
Mark Mendellc4701932015-04-10 13:18:51 -04003094// The comparison bias applies for floating point operations and indicates how NaN
3095// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003096enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003097 kNoBias, // bias is not applicable (i.e. for long operation)
3098 kGtBias, // return 1 for NaN comparisons
3099 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003100 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003101};
3102
Roland Levillain31dd3d62016-02-16 12:21:02 +00003103std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3104
Dave Allison20dfc792014-06-16 20:44:29 -07003105class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003106 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003107 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003108 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3109 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3110 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003111
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003112 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003113 // `instruction`, and disregard moves in between.
3114 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003115
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003116 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003117
3118 virtual IfCondition GetCondition() const = 0;
3119
Mark Mendellc4701932015-04-10 13:18:51 -04003120 virtual IfCondition GetOppositeCondition() const = 0;
3121
Vladimir Markoa1de9182016-02-25 11:37:38 +00003122 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003123 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3124
Vladimir Markoa1de9182016-02-25 11:37:38 +00003125 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3126 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003127
Vladimir Marko372f10e2016-05-17 16:30:10 +01003128 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003129 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003130 }
3131
Roland Levillain4fa13f62015-07-06 18:11:54 +01003132 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003133 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003134 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003135 if (if_cond == kCondNE) {
3136 return true;
3137 } else if (if_cond == kCondEQ) {
3138 return false;
3139 }
3140 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003141 }
3142
3143 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003144 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003145 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003146 if (if_cond == kCondEQ) {
3147 return true;
3148 } else if (if_cond == kCondNE) {
3149 return false;
3150 }
3151 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003152 }
3153
Roland Levillain31dd3d62016-02-16 12:21:02 +00003154 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003155 // Needed if we merge a HCompare into a HCondition.
3156 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3157 static constexpr size_t kFieldComparisonBiasSize =
3158 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3159 static constexpr size_t kNumberOfConditionPackedBits =
3160 kFieldComparisonBias + kFieldComparisonBiasSize;
3161 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3162 using ComparisonBiasField =
3163 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3164
Roland Levillain31dd3d62016-02-16 12:21:02 +00003165 template <typename T>
3166 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3167
3168 template <typename T>
3169 int32_t CompareFP(T x, T y) const {
3170 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3171 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3172 // Handle the bias.
3173 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3174 }
3175
3176 // Return an integer constant containing the result of a condition evaluated at compile time.
3177 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3178 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3179 }
3180
Dave Allison20dfc792014-06-16 20:44:29 -07003181 private:
3182 DISALLOW_COPY_AND_ASSIGN(HCondition);
3183};
3184
3185// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003186class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003187 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003188 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3189 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003190
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003191 bool IsCommutative() const OVERRIDE { return true; }
3192
Vladimir Marko9e23df52015-11-10 17:14:35 +00003193 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3194 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003195 return MakeConstantCondition(true, GetDexPc());
3196 }
3197 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3198 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3199 }
3200 // In the following Evaluate methods, a HCompare instruction has
3201 // been merged into this HEqual instruction; evaluate it as
3202 // `Compare(x, y) == 0`.
3203 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3204 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3205 GetDexPc());
3206 }
3207 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3208 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3209 }
3210 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3211 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003212 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003213
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003214 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003215
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003216 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003217 return kCondEQ;
3218 }
3219
Mark Mendellc4701932015-04-10 13:18:51 -04003220 IfCondition GetOppositeCondition() const OVERRIDE {
3221 return kCondNE;
3222 }
3223
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003224 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003225 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003226
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003227 DISALLOW_COPY_AND_ASSIGN(HEqual);
3228};
3229
Vladimir Markofcb503c2016-05-18 12:48:17 +01003230class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003231 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003232 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3233 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003234
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003235 bool IsCommutative() const OVERRIDE { return true; }
3236
Vladimir Marko9e23df52015-11-10 17:14:35 +00003237 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3238 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003239 return MakeConstantCondition(false, GetDexPc());
3240 }
3241 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3242 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3243 }
3244 // In the following Evaluate methods, a HCompare instruction has
3245 // been merged into this HNotEqual instruction; evaluate it as
3246 // `Compare(x, y) != 0`.
3247 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3248 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3249 }
3250 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3251 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3252 }
3253 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3254 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003255 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003256
Dave Allison20dfc792014-06-16 20:44:29 -07003257 DECLARE_INSTRUCTION(NotEqual);
3258
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003259 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003260 return kCondNE;
3261 }
3262
Mark Mendellc4701932015-04-10 13:18:51 -04003263 IfCondition GetOppositeCondition() const OVERRIDE {
3264 return kCondEQ;
3265 }
3266
Dave Allison20dfc792014-06-16 20:44:29 -07003267 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003268 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003269
Dave Allison20dfc792014-06-16 20:44:29 -07003270 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3271};
3272
Vladimir Markofcb503c2016-05-18 12:48:17 +01003273class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003274 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003275 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3276 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003277
Roland Levillain9867bc72015-08-05 10:21:34 +01003278 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003279 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003280 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003281 // In the following Evaluate methods, a HCompare instruction has
3282 // been merged into this HLessThan instruction; evaluate it as
3283 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003284 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003285 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3286 }
3287 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3288 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3289 }
3290 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3291 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003292 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003293
Dave Allison20dfc792014-06-16 20:44:29 -07003294 DECLARE_INSTRUCTION(LessThan);
3295
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003296 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003297 return kCondLT;
3298 }
3299
Mark Mendellc4701932015-04-10 13:18:51 -04003300 IfCondition GetOppositeCondition() const OVERRIDE {
3301 return kCondGE;
3302 }
3303
Dave Allison20dfc792014-06-16 20:44:29 -07003304 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003305 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003306
Dave Allison20dfc792014-06-16 20:44:29 -07003307 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3308};
3309
Vladimir Markofcb503c2016-05-18 12:48:17 +01003310class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003311 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003312 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3313 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003314
Roland Levillain9867bc72015-08-05 10:21:34 +01003315 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003316 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003317 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003318 // In the following Evaluate methods, a HCompare instruction has
3319 // been merged into this HLessThanOrEqual instruction; evaluate it as
3320 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003321 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003322 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3323 }
3324 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3325 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3326 }
3327 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3328 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003329 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003330
Dave Allison20dfc792014-06-16 20:44:29 -07003331 DECLARE_INSTRUCTION(LessThanOrEqual);
3332
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003333 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003334 return kCondLE;
3335 }
3336
Mark Mendellc4701932015-04-10 13:18:51 -04003337 IfCondition GetOppositeCondition() const OVERRIDE {
3338 return kCondGT;
3339 }
3340
Dave Allison20dfc792014-06-16 20:44:29 -07003341 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003342 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003343
Dave Allison20dfc792014-06-16 20:44:29 -07003344 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3345};
3346
Vladimir Markofcb503c2016-05-18 12:48:17 +01003347class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003348 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003349 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3350 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003351
Roland Levillain9867bc72015-08-05 10:21:34 +01003352 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003353 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003354 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003355 // In the following Evaluate methods, a HCompare instruction has
3356 // been merged into this HGreaterThan instruction; evaluate it as
3357 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003358 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003359 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3360 }
3361 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3362 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3363 }
3364 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3365 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003366 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003367
Dave Allison20dfc792014-06-16 20:44:29 -07003368 DECLARE_INSTRUCTION(GreaterThan);
3369
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003370 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003371 return kCondGT;
3372 }
3373
Mark Mendellc4701932015-04-10 13:18:51 -04003374 IfCondition GetOppositeCondition() const OVERRIDE {
3375 return kCondLE;
3376 }
3377
Dave Allison20dfc792014-06-16 20:44:29 -07003378 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003379 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003380
Dave Allison20dfc792014-06-16 20:44:29 -07003381 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3382};
3383
Vladimir Markofcb503c2016-05-18 12:48:17 +01003384class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003385 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003386 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3387 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003388
Roland Levillain9867bc72015-08-05 10:21:34 +01003389 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003390 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003391 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003392 // In the following Evaluate methods, a HCompare instruction has
3393 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3394 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003395 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003396 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3397 }
3398 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3399 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3400 }
3401 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3402 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003403 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003404
Dave Allison20dfc792014-06-16 20:44:29 -07003405 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3406
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003407 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003408 return kCondGE;
3409 }
3410
Mark Mendellc4701932015-04-10 13:18:51 -04003411 IfCondition GetOppositeCondition() const OVERRIDE {
3412 return kCondLT;
3413 }
3414
Dave Allison20dfc792014-06-16 20:44:29 -07003415 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003416 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003417
Dave Allison20dfc792014-06-16 20:44:29 -07003418 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3419};
3420
Vladimir Markofcb503c2016-05-18 12:48:17 +01003421class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003422 public:
3423 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3424 : HCondition(first, second, dex_pc) {}
3425
3426 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003427 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003428 }
3429 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003430 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3431 }
3432 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3433 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3434 LOG(FATAL) << DebugName() << " is not defined for float values";
3435 UNREACHABLE();
3436 }
3437 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3438 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3439 LOG(FATAL) << DebugName() << " is not defined for double values";
3440 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003441 }
3442
3443 DECLARE_INSTRUCTION(Below);
3444
3445 IfCondition GetCondition() const OVERRIDE {
3446 return kCondB;
3447 }
3448
3449 IfCondition GetOppositeCondition() const OVERRIDE {
3450 return kCondAE;
3451 }
3452
3453 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003454 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003455 return MakeUnsigned(x) < MakeUnsigned(y);
3456 }
Aart Bike9f37602015-10-09 11:15:55 -07003457
3458 DISALLOW_COPY_AND_ASSIGN(HBelow);
3459};
3460
Vladimir Markofcb503c2016-05-18 12:48:17 +01003461class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003462 public:
3463 HBelowOrEqual(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(BelowOrEqual);
3484
3485 IfCondition GetCondition() const OVERRIDE {
3486 return kCondBE;
3487 }
3488
3489 IfCondition GetOppositeCondition() const OVERRIDE {
3490 return kCondA;
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(HBelowOrEqual);
3499};
3500
Vladimir Markofcb503c2016-05-18 12:48:17 +01003501class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003502 public:
3503 HAbove(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(Above);
3524
3525 IfCondition GetCondition() const OVERRIDE {
3526 return kCondA;
3527 }
3528
3529 IfCondition GetOppositeCondition() const OVERRIDE {
3530 return kCondBE;
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(HAbove);
3539};
3540
Vladimir Markofcb503c2016-05-18 12:48:17 +01003541class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003542 public:
3543 HAboveOrEqual(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(AboveOrEqual);
3564
3565 IfCondition GetCondition() const OVERRIDE {
3566 return kCondAE;
3567 }
3568
3569 IfCondition GetOppositeCondition() const OVERRIDE {
3570 return kCondB;
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(HAboveOrEqual);
3579};
Dave Allison20dfc792014-06-16 20:44:29 -07003580
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003581// Instruction to check how two inputs compare to each other.
3582// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003583class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003584 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003585 // Note that `comparison_type` is the type of comparison performed
3586 // between the comparison's inputs, not the type of the instantiated
3587 // HCompare instruction (which is always Primitive::kPrimInt).
3588 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003589 HInstruction* first,
3590 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003591 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003592 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003593 : HBinaryOperation(Primitive::kPrimInt,
3594 first,
3595 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003596 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003597 dex_pc) {
3598 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003599 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3600 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003601 }
3602
Roland Levillain9867bc72015-08-05 10:21:34 +01003603 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003604 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3605
3606 template <typename T>
3607 int32_t ComputeFP(T x, T y) const {
3608 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3609 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3610 // Handle the bias.
3611 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3612 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003613
Roland Levillain9867bc72015-08-05 10:21:34 +01003614 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003615 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3616 // reach this code path when processing a freshly built HIR
3617 // graph. However HCompare integer instructions can be synthesized
3618 // by the instruction simplifier to implement IntegerCompare and
3619 // IntegerSignum intrinsics, so we have to handle this case.
3620 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003621 }
3622 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003623 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3624 }
3625 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3626 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3627 }
3628 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3629 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003630 }
3631
Vladimir Marko372f10e2016-05-17 16:30:10 +01003632 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003633 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003634 }
3635
Vladimir Markoa1de9182016-02-25 11:37:38 +00003636 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003637
Roland Levillain31dd3d62016-02-16 12:21:02 +00003638 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003639 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003640 bool IsGtBias() const {
3641 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003642 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003643 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003644
Roland Levillain1693a1f2016-03-15 14:57:31 +00003645 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3646 // Comparisons do not require a runtime call in any back end.
3647 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003648 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003649
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003650 DECLARE_INSTRUCTION(Compare);
3651
Roland Levillain31dd3d62016-02-16 12:21:02 +00003652 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003653 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3654 static constexpr size_t kFieldComparisonBiasSize =
3655 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3656 static constexpr size_t kNumberOfComparePackedBits =
3657 kFieldComparisonBias + kFieldComparisonBiasSize;
3658 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3659 using ComparisonBiasField =
3660 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3661
Roland Levillain31dd3d62016-02-16 12:21:02 +00003662 // Return an integer constant containing the result of a comparison evaluated at compile time.
3663 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3664 DCHECK(value == -1 || value == 0 || value == 1) << value;
3665 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3666 }
3667
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003668 private:
3669 DISALLOW_COPY_AND_ASSIGN(HCompare);
3670};
3671
Vladimir Markofcb503c2016-05-18 12:48:17 +01003672class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003673 public:
3674 HNewInstance(HInstruction* cls,
3675 HCurrentMethod* current_method,
3676 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003677 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003678 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003679 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003680 bool finalizable,
3681 QuickEntrypointEnum entrypoint)
3682 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3683 type_index_(type_index),
3684 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003685 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003686 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003687 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003688 SetRawInputAt(0, cls);
3689 SetRawInputAt(1, current_method);
3690 }
3691
Andreas Gampea5b09a62016-11-17 15:21:22 -08003692 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003693 const DexFile& GetDexFile() const { return dex_file_; }
3694
3695 // Calls runtime so needs an environment.
3696 bool NeedsEnvironment() const OVERRIDE { return true; }
3697
Mingyao Yang062157f2016-03-02 10:15:36 -08003698 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3699 bool CanThrow() const OVERRIDE { return true; }
3700
3701 // Needs to call into runtime to make sure it's instantiable/accessible.
3702 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003703
Vladimir Markoa1de9182016-02-25 11:37:38 +00003704 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003705
3706 bool CanBeNull() const OVERRIDE { return false; }
3707
3708 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3709
3710 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3711 entrypoint_ = entrypoint;
3712 }
3713
3714 bool IsStringAlloc() const;
3715
3716 DECLARE_INSTRUCTION(NewInstance);
3717
3718 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003719 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3720 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003721 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3722 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3723 "Too many packed fields.");
3724
Andreas Gampea5b09a62016-11-17 15:21:22 -08003725 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003726 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003727 QuickEntrypointEnum entrypoint_;
3728
3729 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3730};
3731
Agi Csaki05f20562015-08-19 14:58:14 -07003732enum IntrinsicNeedsEnvironmentOrCache {
3733 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3734 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003735};
3736
Aart Bik5d75afe2015-12-14 11:57:01 -08003737enum IntrinsicSideEffects {
3738 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3739 kReadSideEffects, // Intrinsic may read heap memory.
3740 kWriteSideEffects, // Intrinsic may write heap memory.
3741 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3742};
3743
3744enum IntrinsicExceptions {
3745 kNoThrow, // Intrinsic does not throw any exceptions.
3746 kCanThrow // Intrinsic may throw exceptions.
3747};
3748
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003749class HInvoke : public HInstruction {
3750 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003751 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003752
Vladimir Marko372f10e2016-05-17 16:30:10 +01003753 using HInstruction::GetInputRecords; // Keep the const version visible.
3754 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3755 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3756 }
3757
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003758 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003759 SetRawInputAt(index, argument);
3760 }
3761
Roland Levillain3e3d7332015-04-28 11:00:54 +01003762 // Return the number of arguments. This number can be lower than
3763 // the number of inputs returned by InputCount(), as some invoke
3764 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3765 // inputs at the end of their list of inputs.
3766 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3767
Vladimir Markoa1de9182016-02-25 11:37:38 +00003768 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003769
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003770 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003771 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003772
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003773 InvokeType GetInvokeType() const {
3774 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003775 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003776
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003777 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003778 return intrinsic_;
3779 }
3780
Aart Bik5d75afe2015-12-14 11:57:01 -08003781 void SetIntrinsic(Intrinsics intrinsic,
3782 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3783 IntrinsicSideEffects side_effects,
3784 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003785
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003786 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003787 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003788 }
3789
Aart Bikff7d89c2016-11-07 08:49:28 -08003790 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
3791
Vladimir Markoa1de9182016-02-25 11:37:38 +00003792 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003793
Aart Bik71bf7b42016-11-16 10:17:46 -08003794 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003795
Vladimir Marko372f10e2016-05-17 16:30:10 +01003796 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003797 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3798 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003799
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003800 uint32_t* GetIntrinsicOptimizations() {
3801 return &intrinsic_optimizations_;
3802 }
3803
3804 const uint32_t* GetIntrinsicOptimizations() const {
3805 return &intrinsic_optimizations_;
3806 }
3807
3808 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3809
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003810 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3811
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003812 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003813
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003814 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003815 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3816 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003817 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3818 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003819 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003820 static constexpr size_t kFieldReturnTypeSize =
3821 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3822 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3823 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3824 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003825 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003826 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3827
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003828 HInvoke(ArenaAllocator* arena,
3829 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003830 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003831 Primitive::Type return_type,
3832 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003833 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003834 ArtMethod* resolved_method,
3835 InvokeType invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003836 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003837 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003838 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003839 resolved_method_(resolved_method),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003840 inputs_(number_of_arguments + number_of_other_inputs,
3841 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003842 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003843 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003844 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003845 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003846 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003847 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003848 }
3849
Roland Levillain3e3d7332015-04-28 11:00:54 +01003850 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003851 ArtMethod* const resolved_method_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003852 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003853 const uint32_t dex_method_index_;
3854 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003855
3856 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3857 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003858
3859 private:
3860 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3861};
3862
Vladimir Markofcb503c2016-05-18 12:48:17 +01003863class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003864 public:
3865 HInvokeUnresolved(ArenaAllocator* arena,
3866 uint32_t number_of_arguments,
3867 Primitive::Type return_type,
3868 uint32_t dex_pc,
3869 uint32_t dex_method_index,
3870 InvokeType invoke_type)
3871 : HInvoke(arena,
3872 number_of_arguments,
3873 0u /* number_of_other_inputs */,
3874 return_type,
3875 dex_pc,
3876 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003877 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003878 invoke_type) {
3879 }
3880
3881 DECLARE_INSTRUCTION(InvokeUnresolved);
3882
3883 private:
3884 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3885};
3886
Vladimir Markofcb503c2016-05-18 12:48:17 +01003887class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003888 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003889 // Requirements of this method call regarding the class
3890 // initialization (clinit) check of its declaring class.
3891 enum class ClinitCheckRequirement {
3892 kNone, // Class already initialized.
3893 kExplicit, // Static call having explicit clinit check as last input.
3894 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003895 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003896 };
3897
Vladimir Marko58155012015-08-19 12:49:41 +00003898 // Determines how to load the target ArtMethod*.
3899 enum class MethodLoadKind {
3900 // Use a String init ArtMethod* loaded from Thread entrypoints.
3901 kStringInit,
3902
3903 // Use the method's own ArtMethod* loaded by the register allocator.
3904 kRecursive,
3905
3906 // Use ArtMethod* at a known address, embed the direct address in the code.
3907 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3908 kDirectAddress,
3909
3910 // Use ArtMethod* at an address that will be known at link time, embed the direct
3911 // address in the code. If the image is relocatable, emit .patch_oat entry.
3912 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3913 // the image relocatable or not.
3914 kDirectAddressWithFixup,
3915
Vladimir Markoa1de9182016-02-25 11:37:38 +00003916 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003917 // Used when we need to use the dex cache, for example for invoke-static that
3918 // may cause class initialization (the entry may point to a resolution method),
3919 // and we know that we can access the dex cache arrays using a PC-relative load.
3920 kDexCachePcRelative,
3921
3922 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3923 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3924 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3925 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3926 kDexCacheViaMethod,
3927 };
3928
3929 // Determines the location of the code pointer.
3930 enum class CodePtrLocation {
3931 // Recursive call, use local PC-relative call instruction.
3932 kCallSelf,
3933
3934 // Use PC-relative call instruction patched at link time.
3935 // Used for calls within an oat file, boot->boot or app->app.
3936 kCallPCRelative,
3937
3938 // Call to a known target address, embed the direct address in code.
3939 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3940 kCallDirect,
3941
3942 // Call to a target address that will be known at link time, embed the direct
3943 // address in code. If the image is relocatable, emit .patch_oat entry.
3944 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3945 // the image relocatable or not.
3946 kCallDirectWithFixup,
3947
3948 // Use code pointer from the ArtMethod*.
3949 // Used when we don't know the target code. This is also the last-resort-kind used when
3950 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3951 kCallArtMethod,
3952 };
3953
3954 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003955 MethodLoadKind method_load_kind;
3956 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003957 // The method load data holds
3958 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3959 // Note that there are multiple string init methods, each having its own offset.
3960 // - the method address for kDirectAddress
3961 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003962 uint64_t method_load_data;
3963 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003964 };
3965
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003966 HInvokeStaticOrDirect(ArenaAllocator* arena,
3967 uint32_t number_of_arguments,
3968 Primitive::Type return_type,
3969 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003970 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003971 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00003972 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003973 InvokeType invoke_type,
3974 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003975 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003976 : HInvoke(arena,
3977 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003978 // There is potentially one extra argument for the HCurrentMethod node, and
3979 // potentially one other if the clinit check is explicit, and potentially
3980 // one other if the method is a string factory.
3981 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003982 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003983 return_type,
3984 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003985 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003986 resolved_method,
3987 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003988 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003989 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003990 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3991 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003992
Vladimir Markodc151b22015-10-15 18:02:30 +01003993 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003994 bool had_current_method_input = HasCurrentMethodInput();
3995 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3996
3997 // Using the current method is the default and once we find a better
3998 // method load kind, we should not go back to using the current method.
3999 DCHECK(had_current_method_input || !needs_current_method_input);
4000
4001 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004002 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4003 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004004 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004005 dispatch_info_ = dispatch_info;
4006 }
4007
Vladimir Markoc53c0792015-11-19 15:48:33 +00004008 void AddSpecialInput(HInstruction* input) {
4009 // We allow only one special input.
4010 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4011 DCHECK(InputCount() == GetSpecialInputIndex() ||
4012 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4013 InsertInputAt(GetSpecialInputIndex(), input);
4014 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004015
Vladimir Marko372f10e2016-05-17 16:30:10 +01004016 using HInstruction::GetInputRecords; // Keep the const version visible.
4017 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4018 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4019 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4020 DCHECK(!input_records.empty());
4021 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4022 HInstruction* last_input = input_records.back().GetInstruction();
4023 // Note: `last_input` may be null during arguments setup.
4024 if (last_input != nullptr) {
4025 // `last_input` is the last input of a static invoke marked as having
4026 // an explicit clinit check. It must either be:
4027 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4028 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4029 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4030 }
4031 }
4032 return input_records;
4033 }
4034
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004035 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004036 // We access the method via the dex cache so we can't do an implicit null check.
4037 // TODO: for intrinsics we can generate implicit null checks.
4038 return false;
4039 }
4040
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004041 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004042 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004043 }
4044
Vladimir Markoc53c0792015-11-19 15:48:33 +00004045 // Get the index of the special input, if any.
4046 //
David Brazdil6de19382016-01-08 17:37:10 +00004047 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4048 // method pointer; otherwise there may be one platform-specific special input,
4049 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004050 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004051 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004052
Vladimir Marko58155012015-08-19 12:49:41 +00004053 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4054 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4055 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004056 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004057 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004058 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004059 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004060 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4061 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004062 bool HasCurrentMethodInput() const {
4063 // This function can be called only after the invoke has been fully initialized by the builder.
4064 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004065 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004066 return true;
4067 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004068 DCHECK(InputCount() == GetSpecialInputIndex() ||
4069 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004070 return false;
4071 }
4072 }
Vladimir Marko58155012015-08-19 12:49:41 +00004073 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
Vladimir Marko58155012015-08-19 12:49:41 +00004074
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004075 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004076 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004077 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004078 }
4079
4080 uint64_t GetMethodAddress() const {
4081 DCHECK(HasMethodAddress());
4082 return dispatch_info_.method_load_data;
4083 }
4084
4085 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004086 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004087 return dispatch_info_.method_load_data;
4088 }
4089
4090 uint64_t GetDirectCodePtr() const {
4091 DCHECK(HasDirectCodePtr());
4092 return dispatch_info_.direct_code_ptr;
4093 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004094
Vladimir Markoa1de9182016-02-25 11:37:38 +00004095 ClinitCheckRequirement GetClinitCheckRequirement() const {
4096 return GetPackedField<ClinitCheckRequirementField>();
4097 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004098
Roland Levillain4c0eb422015-04-24 16:43:49 +01004099 // Is this instruction a call to a static method?
4100 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004101 return GetInvokeType() == kStatic;
4102 }
4103
4104 MethodReference GetTargetMethod() const {
4105 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004106 }
4107
Vladimir Markofbb184a2015-11-13 14:47:00 +00004108 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4109 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4110 // instruction; only relevant for static calls with explicit clinit check.
4111 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004112 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004113 size_t last_input_index = inputs_.size() - 1u;
4114 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004115 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004116 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004117 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004118 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004119 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004120 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004121 }
4122
4123 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004124 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004125 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004126 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004127 }
4128
4129 // Is this a call to a static method whose declaring class has an
4130 // implicit intialization check requirement?
4131 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004132 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004133 }
4134
Vladimir Markob554b5a2015-11-06 12:57:55 +00004135 // Does this method load kind need the current method as an input?
4136 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4137 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4138 }
4139
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004140 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004141
Roland Levillain4c0eb422015-04-24 16:43:49 +01004142 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004143 void InsertInputAt(size_t index, HInstruction* input);
4144 void RemoveInputAt(size_t index);
4145
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004146 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004147 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004148 static constexpr size_t kFieldClinitCheckRequirementSize =
4149 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4150 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4151 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4152 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4153 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004154 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4155 kFieldClinitCheckRequirement,
4156 kFieldClinitCheckRequirementSize>;
4157
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004158 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004159 MethodReference target_method_;
4160 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004161
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004162 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004163};
Vladimir Markof64242a2015-12-01 14:58:23 +00004164std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004165std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004166
Vladimir Markofcb503c2016-05-18 12:48:17 +01004167class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004168 public:
4169 HInvokeVirtual(ArenaAllocator* arena,
4170 uint32_t number_of_arguments,
4171 Primitive::Type return_type,
4172 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004173 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004174 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004175 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004176 : HInvoke(arena,
4177 number_of_arguments,
4178 0u,
4179 return_type,
4180 dex_pc,
4181 dex_method_index,
4182 resolved_method,
4183 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004184 vtable_index_(vtable_index) {}
4185
Aart Bik71bf7b42016-11-16 10:17:46 -08004186 bool CanBeNull() const OVERRIDE {
4187 switch (GetIntrinsic()) {
4188 case Intrinsics::kThreadCurrentThread:
4189 case Intrinsics::kStringBufferAppend:
4190 case Intrinsics::kStringBufferToString:
4191 case Intrinsics::kStringBuilderAppend:
4192 case Intrinsics::kStringBuilderToString:
4193 return false;
4194 default:
4195 return HInvoke::CanBeNull();
4196 }
4197 }
4198
Calin Juravle641547a2015-04-21 22:08:51 +01004199 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004200 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004201 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004202 }
4203
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004204 uint32_t GetVTableIndex() const { return vtable_index_; }
4205
4206 DECLARE_INSTRUCTION(InvokeVirtual);
4207
4208 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004209 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004210 const uint32_t vtable_index_;
4211
4212 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4213};
4214
Vladimir Markofcb503c2016-05-18 12:48:17 +01004215class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004216 public:
4217 HInvokeInterface(ArenaAllocator* arena,
4218 uint32_t number_of_arguments,
4219 Primitive::Type return_type,
4220 uint32_t dex_pc,
4221 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004222 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004223 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004224 : HInvoke(arena,
4225 number_of_arguments,
4226 0u,
4227 return_type,
4228 dex_pc,
4229 dex_method_index,
4230 resolved_method,
4231 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004232 imt_index_(imt_index) {}
4233
Calin Juravle641547a2015-04-21 22:08:51 +01004234 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004235 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004236 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004237 }
4238
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004239 uint32_t GetImtIndex() const { return imt_index_; }
4240 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4241
4242 DECLARE_INSTRUCTION(InvokeInterface);
4243
4244 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004245 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004246 const uint32_t imt_index_;
4247
4248 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4249};
4250
Vladimir Markofcb503c2016-05-18 12:48:17 +01004251class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004252 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004253 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004254 : HUnaryOperation(result_type, input, dex_pc) {
4255 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4256 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004257
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004258 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004259
4260 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004261 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004262 }
4263 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004264 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004265 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004266 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4267 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4268 }
4269 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4270 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4271 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004272
Roland Levillain88cb1752014-10-20 16:36:47 +01004273 DECLARE_INSTRUCTION(Neg);
4274
4275 private:
4276 DISALLOW_COPY_AND_ASSIGN(HNeg);
4277};
4278
Vladimir Markofcb503c2016-05-18 12:48:17 +01004279class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004280 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004281 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004282 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004283 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004284 dex::TypeIndex type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004285 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004286 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004287 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004288 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004289 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004290 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004291 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004292 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004293 }
4294
Andreas Gampea5b09a62016-11-17 15:21:22 -08004295 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004296 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004297
4298 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004299 bool NeedsEnvironment() const OVERRIDE { return true; }
4300
Mingyao Yang0c365e62015-03-31 15:09:29 -07004301 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4302 bool CanThrow() const OVERRIDE { return true; }
4303
Calin Juravle10e244f2015-01-26 18:54:32 +00004304 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004305
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004306 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4307
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004308 DECLARE_INSTRUCTION(NewArray);
4309
4310 private:
Andreas Gampea5b09a62016-11-17 15:21:22 -08004311 const dex::TypeIndex type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004312 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004313 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004314
4315 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4316};
4317
Vladimir Markofcb503c2016-05-18 12:48:17 +01004318class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004319 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004320 HAdd(Primitive::Type result_type,
4321 HInstruction* left,
4322 HInstruction* right,
4323 uint32_t dex_pc = kNoDexPc)
4324 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004325
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004326 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004327
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004328 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004329
4330 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004331 return GetBlock()->GetGraph()->GetIntConstant(
4332 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004333 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004334 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004335 return GetBlock()->GetGraph()->GetLongConstant(
4336 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004337 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004338 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4339 return GetBlock()->GetGraph()->GetFloatConstant(
4340 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4341 }
4342 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4343 return GetBlock()->GetGraph()->GetDoubleConstant(
4344 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4345 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004346
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004347 DECLARE_INSTRUCTION(Add);
4348
4349 private:
4350 DISALLOW_COPY_AND_ASSIGN(HAdd);
4351};
4352
Vladimir Markofcb503c2016-05-18 12:48:17 +01004353class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004354 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004355 HSub(Primitive::Type result_type,
4356 HInstruction* left,
4357 HInstruction* right,
4358 uint32_t dex_pc = kNoDexPc)
4359 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004360
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004361 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004362
4363 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004364 return GetBlock()->GetGraph()->GetIntConstant(
4365 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004366 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004367 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004368 return GetBlock()->GetGraph()->GetLongConstant(
4369 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004370 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004371 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4372 return GetBlock()->GetGraph()->GetFloatConstant(
4373 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4374 }
4375 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4376 return GetBlock()->GetGraph()->GetDoubleConstant(
4377 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4378 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004379
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004380 DECLARE_INSTRUCTION(Sub);
4381
4382 private:
4383 DISALLOW_COPY_AND_ASSIGN(HSub);
4384};
4385
Vladimir Markofcb503c2016-05-18 12:48:17 +01004386class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004387 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004388 HMul(Primitive::Type result_type,
4389 HInstruction* left,
4390 HInstruction* right,
4391 uint32_t dex_pc = kNoDexPc)
4392 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004393
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004394 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004395
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004396 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004397
4398 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004399 return GetBlock()->GetGraph()->GetIntConstant(
4400 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004401 }
4402 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004403 return GetBlock()->GetGraph()->GetLongConstant(
4404 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004405 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004406 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4407 return GetBlock()->GetGraph()->GetFloatConstant(
4408 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4409 }
4410 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4411 return GetBlock()->GetGraph()->GetDoubleConstant(
4412 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4413 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004414
4415 DECLARE_INSTRUCTION(Mul);
4416
4417 private:
4418 DISALLOW_COPY_AND_ASSIGN(HMul);
4419};
4420
Vladimir Markofcb503c2016-05-18 12:48:17 +01004421class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004422 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004423 HDiv(Primitive::Type result_type,
4424 HInstruction* left,
4425 HInstruction* right,
4426 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004427 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004428
Roland Levillain9867bc72015-08-05 10:21:34 +01004429 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004430 T ComputeIntegral(T x, T y) const {
4431 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004432 // Our graph structure ensures we never have 0 for `y` during
4433 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004434 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004435 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004436 return (y == -1) ? -x : x / y;
4437 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004438
Roland Levillain31dd3d62016-02-16 12:21:02 +00004439 template <typename T>
4440 T ComputeFP(T x, T y) const {
4441 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4442 return x / y;
4443 }
4444
Roland Levillain9867bc72015-08-05 10:21:34 +01004445 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004446 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004447 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004448 }
4449 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004450 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004451 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4452 }
4453 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4454 return GetBlock()->GetGraph()->GetFloatConstant(
4455 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4456 }
4457 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4458 return GetBlock()->GetGraph()->GetDoubleConstant(
4459 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004460 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004461
4462 DECLARE_INSTRUCTION(Div);
4463
4464 private:
4465 DISALLOW_COPY_AND_ASSIGN(HDiv);
4466};
4467
Vladimir Markofcb503c2016-05-18 12:48:17 +01004468class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004469 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004470 HRem(Primitive::Type result_type,
4471 HInstruction* left,
4472 HInstruction* right,
4473 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004474 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004475
Roland Levillain9867bc72015-08-05 10:21:34 +01004476 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004477 T ComputeIntegral(T x, T y) const {
4478 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004479 // Our graph structure ensures we never have 0 for `y` during
4480 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004481 DCHECK_NE(y, 0);
4482 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4483 return (y == -1) ? 0 : x % y;
4484 }
4485
Roland Levillain31dd3d62016-02-16 12:21:02 +00004486 template <typename T>
4487 T ComputeFP(T x, T y) const {
4488 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4489 return std::fmod(x, y);
4490 }
4491
Roland Levillain9867bc72015-08-05 10:21:34 +01004492 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004493 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004494 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004495 }
4496 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004497 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004498 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004499 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004500 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4501 return GetBlock()->GetGraph()->GetFloatConstant(
4502 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4503 }
4504 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4505 return GetBlock()->GetGraph()->GetDoubleConstant(
4506 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4507 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004508
4509 DECLARE_INSTRUCTION(Rem);
4510
4511 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004512 DISALLOW_COPY_AND_ASSIGN(HRem);
4513};
4514
Vladimir Markofcb503c2016-05-18 12:48:17 +01004515class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004516 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004517 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4518 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004519 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004520 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004521 SetRawInputAt(0, value);
4522 }
4523
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004524 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4525
Calin Juravled0d48522014-11-04 16:40:20 +00004526 bool CanBeMoved() const OVERRIDE { return true; }
4527
Vladimir Marko372f10e2016-05-17 16:30:10 +01004528 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004529 return true;
4530 }
4531
4532 bool NeedsEnvironment() const OVERRIDE { return true; }
4533 bool CanThrow() const OVERRIDE { return true; }
4534
Calin Juravled0d48522014-11-04 16:40:20 +00004535 DECLARE_INSTRUCTION(DivZeroCheck);
4536
4537 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004538 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4539};
4540
Vladimir Markofcb503c2016-05-18 12:48:17 +01004541class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004542 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004543 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004544 HInstruction* value,
4545 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004546 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004547 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4548 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4549 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004550 }
4551
Roland Levillain5b5b9312016-03-22 14:57:31 +00004552 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004553 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004554 return value << (distance & max_shift_distance);
4555 }
4556
4557 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004558 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004559 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004560 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004561 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004562 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004563 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004564 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004565 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4566 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4567 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4568 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004569 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004570 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4571 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004572 LOG(FATAL) << DebugName() << " is not defined for float values";
4573 UNREACHABLE();
4574 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004575 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4576 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004577 LOG(FATAL) << DebugName() << " is not defined for double values";
4578 UNREACHABLE();
4579 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004580
4581 DECLARE_INSTRUCTION(Shl);
4582
4583 private:
4584 DISALLOW_COPY_AND_ASSIGN(HShl);
4585};
4586
Vladimir Markofcb503c2016-05-18 12:48:17 +01004587class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004588 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004589 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004590 HInstruction* value,
4591 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004592 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004593 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4594 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4595 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004596 }
4597
Roland Levillain5b5b9312016-03-22 14:57:31 +00004598 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004599 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004600 return value >> (distance & max_shift_distance);
4601 }
4602
4603 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004604 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004605 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004606 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004607 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004608 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004609 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004610 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004611 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4612 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4613 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4614 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004615 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004616 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4617 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004618 LOG(FATAL) << DebugName() << " is not defined for float values";
4619 UNREACHABLE();
4620 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004621 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4622 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004623 LOG(FATAL) << DebugName() << " is not defined for double values";
4624 UNREACHABLE();
4625 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004626
4627 DECLARE_INSTRUCTION(Shr);
4628
4629 private:
4630 DISALLOW_COPY_AND_ASSIGN(HShr);
4631};
4632
Vladimir Markofcb503c2016-05-18 12:48:17 +01004633class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004634 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004635 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004636 HInstruction* value,
4637 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004638 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004639 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4640 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4641 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004642 }
4643
Roland Levillain5b5b9312016-03-22 14:57:31 +00004644 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004645 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004646 typedef typename std::make_unsigned<T>::type V;
4647 V ux = static_cast<V>(value);
4648 return static_cast<T>(ux >> (distance & max_shift_distance));
4649 }
4650
4651 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004652 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004653 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004654 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004655 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004656 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004657 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004658 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004659 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4660 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4661 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4662 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004663 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004664 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4665 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004666 LOG(FATAL) << DebugName() << " is not defined for float values";
4667 UNREACHABLE();
4668 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004669 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4670 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004671 LOG(FATAL) << DebugName() << " is not defined for double values";
4672 UNREACHABLE();
4673 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004674
4675 DECLARE_INSTRUCTION(UShr);
4676
4677 private:
4678 DISALLOW_COPY_AND_ASSIGN(HUShr);
4679};
4680
Vladimir Markofcb503c2016-05-18 12:48:17 +01004681class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004682 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004683 HAnd(Primitive::Type result_type,
4684 HInstruction* left,
4685 HInstruction* right,
4686 uint32_t dex_pc = kNoDexPc)
4687 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004688
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004689 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004690
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004691 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004692
4693 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004694 return GetBlock()->GetGraph()->GetIntConstant(
4695 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004696 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004697 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004698 return GetBlock()->GetGraph()->GetLongConstant(
4699 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004700 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004701 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4702 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4703 LOG(FATAL) << DebugName() << " is not defined for float values";
4704 UNREACHABLE();
4705 }
4706 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4707 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4708 LOG(FATAL) << DebugName() << " is not defined for double values";
4709 UNREACHABLE();
4710 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004711
4712 DECLARE_INSTRUCTION(And);
4713
4714 private:
4715 DISALLOW_COPY_AND_ASSIGN(HAnd);
4716};
4717
Vladimir Markofcb503c2016-05-18 12:48:17 +01004718class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004719 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004720 HOr(Primitive::Type result_type,
4721 HInstruction* left,
4722 HInstruction* right,
4723 uint32_t dex_pc = kNoDexPc)
4724 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004725
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004726 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004727
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004728 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004729
4730 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004731 return GetBlock()->GetGraph()->GetIntConstant(
4732 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004733 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004734 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004735 return GetBlock()->GetGraph()->GetLongConstant(
4736 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004737 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004738 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4739 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4740 LOG(FATAL) << DebugName() << " is not defined for float values";
4741 UNREACHABLE();
4742 }
4743 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4744 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4745 LOG(FATAL) << DebugName() << " is not defined for double values";
4746 UNREACHABLE();
4747 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004748
4749 DECLARE_INSTRUCTION(Or);
4750
4751 private:
4752 DISALLOW_COPY_AND_ASSIGN(HOr);
4753};
4754
Vladimir Markofcb503c2016-05-18 12:48:17 +01004755class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004756 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004757 HXor(Primitive::Type result_type,
4758 HInstruction* left,
4759 HInstruction* right,
4760 uint32_t dex_pc = kNoDexPc)
4761 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004762
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004763 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004764
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004765 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004766
4767 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004768 return GetBlock()->GetGraph()->GetIntConstant(
4769 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004770 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004771 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004772 return GetBlock()->GetGraph()->GetLongConstant(
4773 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004774 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004775 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4776 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4777 LOG(FATAL) << DebugName() << " is not defined for float values";
4778 UNREACHABLE();
4779 }
4780 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4781 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4782 LOG(FATAL) << DebugName() << " is not defined for double values";
4783 UNREACHABLE();
4784 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004785
4786 DECLARE_INSTRUCTION(Xor);
4787
4788 private:
4789 DISALLOW_COPY_AND_ASSIGN(HXor);
4790};
4791
Vladimir Markofcb503c2016-05-18 12:48:17 +01004792class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004793 public:
4794 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004795 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004796 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4797 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004798 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004799
Roland Levillain5b5b9312016-03-22 14:57:31 +00004800 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004801 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004802 typedef typename std::make_unsigned<T>::type V;
4803 V ux = static_cast<V>(value);
4804 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004805 return static_cast<T>(ux);
4806 } else {
4807 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004808 return static_cast<T>(ux >> (distance & max_shift_value)) |
4809 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004810 }
4811 }
4812
Roland Levillain5b5b9312016-03-22 14:57:31 +00004813 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004814 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004815 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004816 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004817 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004818 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004819 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004820 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004821 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4822 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4823 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4824 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004825 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004826 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4827 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004828 LOG(FATAL) << DebugName() << " is not defined for float values";
4829 UNREACHABLE();
4830 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004831 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4832 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004833 LOG(FATAL) << DebugName() << " is not defined for double values";
4834 UNREACHABLE();
4835 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004836
4837 DECLARE_INSTRUCTION(Ror);
4838
4839 private:
4840 DISALLOW_COPY_AND_ASSIGN(HRor);
4841};
4842
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004843// The value of a parameter in this method. Its location depends on
4844// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004845class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004846 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004847 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004848 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004849 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004850 Primitive::Type parameter_type,
4851 bool is_this = false)
4852 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004853 dex_file_(dex_file),
4854 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004855 index_(index) {
4856 SetPackedFlag<kFlagIsThis>(is_this);
4857 SetPackedFlag<kFlagCanBeNull>(!is_this);
4858 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004859
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004860 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08004861 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004862 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004863 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004864
Vladimir Markoa1de9182016-02-25 11:37:38 +00004865 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4866 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004867
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004868 DECLARE_INSTRUCTION(ParameterValue);
4869
4870 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004871 // Whether or not the parameter value corresponds to 'this' argument.
4872 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4873 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4874 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4875 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4876 "Too many packed fields.");
4877
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004878 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004879 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004880 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004881 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004882 const uint8_t index_;
4883
4884 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4885};
4886
Vladimir Markofcb503c2016-05-18 12:48:17 +01004887class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004888 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004889 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4890 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004891
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004892 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004893 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004894 return true;
4895 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004896
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004897 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004898
4899 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004900 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004901 }
4902 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004903 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004904 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004905 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4906 LOG(FATAL) << DebugName() << " is not defined for float values";
4907 UNREACHABLE();
4908 }
4909 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4910 LOG(FATAL) << DebugName() << " is not defined for double values";
4911 UNREACHABLE();
4912 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004913
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004914 DECLARE_INSTRUCTION(Not);
4915
4916 private:
4917 DISALLOW_COPY_AND_ASSIGN(HNot);
4918};
4919
Vladimir Markofcb503c2016-05-18 12:48:17 +01004920class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004921 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004922 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4923 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004924
4925 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004926 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004927 return true;
4928 }
4929
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004930 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004931 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004932 return !x;
4933 }
4934
Roland Levillain9867bc72015-08-05 10:21:34 +01004935 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004936 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004937 }
4938 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4939 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004940 UNREACHABLE();
4941 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004942 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4943 LOG(FATAL) << DebugName() << " is not defined for float values";
4944 UNREACHABLE();
4945 }
4946 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4947 LOG(FATAL) << DebugName() << " is not defined for double values";
4948 UNREACHABLE();
4949 }
David Brazdil66d126e2015-04-03 16:02:44 +01004950
4951 DECLARE_INSTRUCTION(BooleanNot);
4952
4953 private:
4954 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4955};
4956
Vladimir Markofcb503c2016-05-18 12:48:17 +01004957class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004958 public:
4959 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004960 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004961 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004962 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004963 // Invariant: We should never generate a conversion to a Boolean value.
4964 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004965 }
4966
4967 HInstruction* GetInput() const { return InputAt(0); }
4968 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4969 Primitive::Type GetResultType() const { return GetType(); }
4970
4971 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004972 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4973 return true;
4974 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004975
Mark Mendelle82549b2015-05-06 10:55:34 -04004976 // Try to statically evaluate the conversion and return a HConstant
4977 // containing the result. If the input cannot be converted, return nullptr.
4978 HConstant* TryStaticEvaluation() const;
4979
Roland Levillaindff1f282014-11-05 14:15:05 +00004980 DECLARE_INSTRUCTION(TypeConversion);
4981
4982 private:
4983 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4984};
4985
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004986static constexpr uint32_t kNoRegNumber = -1;
4987
Vladimir Markofcb503c2016-05-18 12:48:17 +01004988class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004989 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004990 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4991 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004992 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004993 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004994 SetRawInputAt(0, value);
4995 }
4996
Calin Juravle10e244f2015-01-26 18:54:32 +00004997 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004998 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004999 return true;
5000 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005001
Calin Juravle10e244f2015-01-26 18:54:32 +00005002 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005003
Calin Juravle10e244f2015-01-26 18:54:32 +00005004 bool CanThrow() const OVERRIDE { return true; }
5005
5006 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005007
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005008
5009 DECLARE_INSTRUCTION(NullCheck);
5010
5011 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005012 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5013};
5014
5015class FieldInfo : public ValueObject {
5016 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005017 FieldInfo(MemberOffset field_offset,
5018 Primitive::Type field_type,
5019 bool is_volatile,
5020 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005021 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005022 const DexFile& dex_file,
5023 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005024 : field_offset_(field_offset),
5025 field_type_(field_type),
5026 is_volatile_(is_volatile),
5027 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005028 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005029 dex_file_(dex_file),
5030 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005031
5032 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005033 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005034 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005035 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005036 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005037 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005038 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005039
5040 private:
5041 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005042 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005043 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005044 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005045 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005046 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005047 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005048};
5049
Vladimir Markofcb503c2016-05-18 12:48:17 +01005050class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005051 public:
5052 HInstanceFieldGet(HInstruction* value,
5053 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005054 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005055 bool is_volatile,
5056 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005057 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005058 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005059 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005060 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005061 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005062 field_info_(field_offset,
5063 field_type,
5064 is_volatile,
5065 field_idx,
5066 declaring_class_def_index,
5067 dex_file,
5068 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005069 SetRawInputAt(0, value);
5070 }
5071
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005072 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005073
Vladimir Marko372f10e2016-05-17 16:30:10 +01005074 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5075 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005076 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005077 }
5078
Calin Juravle641547a2015-04-21 22:08:51 +01005079 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005080 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005081 }
5082
5083 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005084 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5085 }
5086
Calin Juravle52c48962014-12-16 17:02:57 +00005087 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005088 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005089 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005090 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005091
5092 DECLARE_INSTRUCTION(InstanceFieldGet);
5093
5094 private:
5095 const FieldInfo field_info_;
5096
5097 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5098};
5099
Vladimir Markofcb503c2016-05-18 12:48:17 +01005100class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005101 public:
5102 HInstanceFieldSet(HInstruction* object,
5103 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005104 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005105 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005106 bool is_volatile,
5107 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005108 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005109 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005110 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005111 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005112 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005113 field_info_(field_offset,
5114 field_type,
5115 is_volatile,
5116 field_idx,
5117 declaring_class_def_index,
5118 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005119 dex_cache) {
5120 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005121 SetRawInputAt(0, object);
5122 SetRawInputAt(1, value);
5123 }
5124
Calin Juravle641547a2015-04-21 22:08:51 +01005125 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005126 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005127 }
5128
Calin Juravle52c48962014-12-16 17:02:57 +00005129 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005130 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005131 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005132 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005133 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005134 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5135 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005136
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005137 DECLARE_INSTRUCTION(InstanceFieldSet);
5138
5139 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005140 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5141 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5142 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5143 "Too many packed fields.");
5144
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005145 const FieldInfo field_info_;
5146
5147 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5148};
5149
Vladimir Markofcb503c2016-05-18 12:48:17 +01005150class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005151 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005152 HArrayGet(HInstruction* array,
5153 HInstruction* index,
5154 Primitive::Type type,
5155 uint32_t dex_pc,
5156 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005157 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005158 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005159 SetRawInputAt(0, array);
5160 SetRawInputAt(1, index);
5161 }
5162
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005163 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005164 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005165 return true;
5166 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005167 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005168 // TODO: We can be smarter here.
5169 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5170 // which generates the implicit null check. There are cases when these can be removed
5171 // to produce better code. If we ever add optimizations to do so we should allow an
5172 // implicit check here (as long as the address falls in the first page).
5173 return false;
5174 }
5175
David Brazdil4833f5a2015-12-16 10:37:39 +00005176 bool IsEquivalentOf(HArrayGet* other) const {
5177 bool result = (GetDexPc() == other->GetDexPc());
5178 if (kIsDebugBuild && result) {
5179 DCHECK_EQ(GetBlock(), other->GetBlock());
5180 DCHECK_EQ(GetArray(), other->GetArray());
5181 DCHECK_EQ(GetIndex(), other->GetIndex());
5182 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005183 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005184 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005185 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5186 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005187 }
5188 }
5189 return result;
5190 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005191
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005192 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5193
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005194 HInstruction* GetArray() const { return InputAt(0); }
5195 HInstruction* GetIndex() const { return InputAt(1); }
5196
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005197 DECLARE_INSTRUCTION(ArrayGet);
5198
5199 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005200 // We treat a String as an array, creating the HArrayGet from String.charAt()
5201 // intrinsic in the instruction simplifier. We can always determine whether
5202 // a particular HArrayGet is actually a String.charAt() by looking at the type
5203 // of the input but that requires holding the mutator lock, so we prefer to use
5204 // a flag, so that code generators don't need to do the locking.
5205 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5206 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5207 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5208 "Too many packed fields.");
5209
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005210 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5211};
5212
Vladimir Markofcb503c2016-05-18 12:48:17 +01005213class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005214 public:
5215 HArraySet(HInstruction* array,
5216 HInstruction* index,
5217 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005218 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005219 uint32_t dex_pc)
5220 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005221 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5222 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5223 SetPackedFlag<kFlagValueCanBeNull>(true);
5224 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005225 SetRawInputAt(0, array);
5226 SetRawInputAt(1, index);
5227 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005228 // Make a best guess now, may be refined during SSA building.
5229 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005230 }
5231
Calin Juravle77520bc2015-01-12 18:45:46 +00005232 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005233 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005234 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005235 }
5236
Mingyao Yang81014cb2015-06-02 03:16:27 -07005237 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005238 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005239
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005240 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005241 // TODO: Same as for ArrayGet.
5242 return false;
5243 }
5244
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005245 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005246 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005247 }
5248
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005249 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005250 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005251 }
5252
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005253 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005254 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005255 }
5256
Vladimir Markoa1de9182016-02-25 11:37:38 +00005257 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5258 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5259 bool StaticTypeOfArrayIsObjectArray() const {
5260 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5261 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005262
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005263 HInstruction* GetArray() const { return InputAt(0); }
5264 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005265 HInstruction* GetValue() const { return InputAt(2); }
5266
5267 Primitive::Type GetComponentType() const {
5268 // The Dex format does not type floating point index operations. Since the
5269 // `expected_component_type_` is set during building and can therefore not
5270 // be correct, we also check what is the value type. If it is a floating
5271 // point type, we must use that type.
5272 Primitive::Type value_type = GetValue()->GetType();
5273 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5274 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005275 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005276 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005277
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005278 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005279 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005280 }
5281
Aart Bik18b36ab2016-04-13 16:41:35 -07005282 void ComputeSideEffects() {
5283 Primitive::Type type = GetComponentType();
5284 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5285 SideEffectsForArchRuntimeCalls(type)));
5286 }
5287
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005288 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5289 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5290 }
5291
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005292 DECLARE_INSTRUCTION(ArraySet);
5293
5294 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005295 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5296 static constexpr size_t kFieldExpectedComponentTypeSize =
5297 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5298 static constexpr size_t kFlagNeedsTypeCheck =
5299 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5300 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005301 // Cached information for the reference_type_info_ so that codegen
5302 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005303 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5304 static constexpr size_t kNumberOfArraySetPackedBits =
5305 kFlagStaticTypeOfArrayIsObjectArray + 1;
5306 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5307 using ExpectedComponentTypeField =
5308 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005309
5310 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5311};
5312
Vladimir Markofcb503c2016-05-18 12:48:17 +01005313class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005314 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005315 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005316 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005317 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005318 // Note that arrays do not change length, so the instruction does not
5319 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005320 SetRawInputAt(0, array);
5321 }
5322
Calin Juravle77520bc2015-01-12 18:45:46 +00005323 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005324 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005325 return true;
5326 }
Calin Juravle641547a2015-04-21 22:08:51 +01005327 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5328 return obj == InputAt(0);
5329 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005330
Vladimir Markodce016e2016-04-28 13:10:02 +01005331 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5332
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005333 DECLARE_INSTRUCTION(ArrayLength);
5334
5335 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005336 // We treat a String as an array, creating the HArrayLength from String.length()
5337 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5338 // determine whether a particular HArrayLength is actually a String.length() by
5339 // looking at the type of the input but that requires holding the mutator lock, so
5340 // we prefer to use a flag, so that code generators don't need to do the locking.
5341 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5342 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5343 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5344 "Too many packed fields.");
5345
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005346 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5347};
5348
Vladimir Markofcb503c2016-05-18 12:48:17 +01005349class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005350 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005351 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5352 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005353 HBoundsCheck(HInstruction* index,
5354 HInstruction* length,
5355 uint32_t dex_pc,
5356 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5357 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5358 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005359 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005360 SetRawInputAt(0, index);
5361 SetRawInputAt(1, length);
5362 }
5363
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005364 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005365 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005366 return true;
5367 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005368
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005369 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005370
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005371 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005372
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005373 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5374 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5375
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005376 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005377
5378 DECLARE_INSTRUCTION(BoundsCheck);
5379
5380 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005381 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5382 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5383 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5384 // so we need to create an HEnvironment which will be translated to an InlineInfo
5385 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5386 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5387 // from the invoke as we don't want to look again at the dex bytecode.
5388 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5389
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005390 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5391};
5392
Vladimir Markofcb503c2016-05-18 12:48:17 +01005393class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005394 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005395 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005396 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005397
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005398 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005399 return true;
5400 }
5401
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005402 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5403 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005404
5405 DECLARE_INSTRUCTION(SuspendCheck);
5406
5407 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005408 // Only used for code generation, in order to share the same slow path between back edges
5409 // of a same loop.
5410 SlowPathCode* slow_path_;
5411
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005412 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5413};
5414
David Srbecky0cf44932015-12-09 14:09:59 +00005415// Pseudo-instruction which provides the native debugger with mapping information.
5416// It ensures that we can generate line number and local variables at this point.
5417class HNativeDebugInfo : public HTemplateInstruction<0> {
5418 public:
5419 explicit HNativeDebugInfo(uint32_t dex_pc)
5420 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5421
5422 bool NeedsEnvironment() const OVERRIDE {
5423 return true;
5424 }
5425
5426 DECLARE_INSTRUCTION(NativeDebugInfo);
5427
5428 private:
5429 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5430};
5431
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005432/**
5433 * Instruction to load a Class object.
5434 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005435class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005436 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005437 // Determines how to load the Class.
5438 enum class LoadKind {
5439 // Use the Class* from the method's own ArtMethod*.
5440 kReferrersClass,
5441
5442 // Use boot image Class* address that will be known at link time.
5443 // Used for boot image classes referenced by boot image code in non-PIC mode.
5444 kBootImageLinkTimeAddress,
5445
5446 // Use PC-relative boot image Class* address that will be known at link time.
5447 // Used for boot image classes referenced by boot image code in PIC mode.
5448 kBootImageLinkTimePcRelative,
5449
5450 // Use a known boot image Class* address, embedded in the code by the codegen.
5451 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5452 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5453 // GetIncludePatchInformation().
5454 kBootImageAddress,
5455
5456 // Load from the resolved types array at an absolute address.
5457 // Used for classes outside the boot image referenced by JIT-compiled code.
5458 kDexCacheAddress,
5459
5460 // Load from resolved types array in the dex cache using a PC-relative load.
5461 // Used for classes outside boot image when we know that we can access
5462 // the dex cache arrays using a PC-relative load.
5463 kDexCachePcRelative,
5464
5465 // Load from resolved types array accessed through the class loaded from
5466 // the compiled method's own ArtMethod*. This is the default access type when
5467 // all other types are unavailable.
5468 kDexCacheViaMethod,
5469
5470 kLast = kDexCacheViaMethod
5471 };
5472
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005473 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005474 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005475 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005476 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005477 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005478 bool needs_access_check,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005479 bool is_in_dex_cache,
5480 bool is_in_boot_image)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005481 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5482 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005483 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005484 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005485 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005486 // Referrers class should not need access check. We never inline unverified
5487 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005488 DCHECK(!is_referrers_class || !needs_access_check);
5489
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005490 SetPackedField<LoadKindField>(
5491 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005492 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5493 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005494 SetPackedFlag<kFlagIsInBootImage>(is_in_boot_image);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005495 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005496 }
5497
5498 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5499 DCHECK(HasAddress(load_kind));
5500 load_data_.address = address;
5501 SetLoadKindInternal(load_kind);
5502 }
5503
5504 void SetLoadKindWithTypeReference(LoadKind load_kind,
5505 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005506 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005507 DCHECK(HasTypeReference(load_kind));
5508 DCHECK(IsSameDexFile(dex_file_, dex_file));
5509 DCHECK_EQ(type_index_, type_index);
5510 SetLoadKindInternal(load_kind);
5511 }
5512
5513 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5514 const DexFile& dex_file,
5515 uint32_t element_index) {
5516 DCHECK(HasDexCacheReference(load_kind));
5517 DCHECK(IsSameDexFile(dex_file_, dex_file));
5518 load_data_.dex_cache_element_index = element_index;
5519 SetLoadKindInternal(load_kind);
5520 }
5521
5522 LoadKind GetLoadKind() const {
5523 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005524 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005525
5526 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005527
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005528 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005529
Andreas Gampea5b09a62016-11-17 15:21:22 -08005530 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005531
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005532 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005533
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005534 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005535 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005536 }
5537
Calin Juravle0ba218d2015-05-19 18:46:01 +01005538 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005539 // The entrypoint the code generator is going to call does not do
5540 // clinit of the class.
5541 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005542 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005543 }
5544
5545 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005546 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005547 (!IsReferrersClass() && !IsInDexCache()) ||
5548 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005549 }
5550
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005551
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005552 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005553 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005554 }
5555
Calin Juravleacf735c2015-02-12 15:25:22 +00005556 ReferenceTypeInfo GetLoadedClassRTI() {
5557 return loaded_class_rti_;
5558 }
5559
5560 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5561 // Make sure we only set exact types (the loaded class should never be merged).
5562 DCHECK(rti.IsExact());
5563 loaded_class_rti_ = rti;
5564 }
5565
Andreas Gampea5b09a62016-11-17 15:21:22 -08005566 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005567 const DexFile& GetDexFile() const { return dex_file_; }
5568
5569 uint32_t GetDexCacheElementOffset() const;
5570
5571 uint64_t GetAddress() const {
5572 DCHECK(HasAddress(GetLoadKind()));
5573 return load_data_.address;
5574 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005575
Vladimir Markoa1de9182016-02-25 11:37:38 +00005576 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005577
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005578 static SideEffects SideEffectsForArchRuntimeCalls() {
5579 return SideEffects::CanTriggerGC();
5580 }
5581
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005582 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005583 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5584 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005585 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005586 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005587
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005588 void MarkInDexCache() {
5589 SetPackedFlag<kFlagIsInDexCache>(true);
5590 DCHECK(!NeedsEnvironment());
5591 RemoveEnvironment();
5592 SetSideEffects(SideEffects::None());
5593 }
5594
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005595 void MarkInBootImage() {
5596 SetPackedFlag<kFlagIsInBootImage>(true);
5597 }
5598
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005599 void AddSpecialInput(HInstruction* special_input);
5600
5601 using HInstruction::GetInputRecords; // Keep the const version visible.
5602 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5603 return ArrayRef<HUserRecord<HInstruction*>>(
5604 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5605 }
5606
5607 Primitive::Type GetType() const OVERRIDE {
5608 return Primitive::kPrimNot;
5609 }
5610
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005611 DECLARE_INSTRUCTION(LoadClass);
5612
5613 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005614 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005615 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005616 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005617 // Whether this instruction must generate the initialization check.
5618 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005619 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005620 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5621 static constexpr size_t kFieldLoadKindSize =
5622 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5623 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005624 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005625 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5626
5627 static bool HasTypeReference(LoadKind load_kind) {
5628 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5629 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5630 load_kind == LoadKind::kDexCacheViaMethod ||
5631 load_kind == LoadKind::kReferrersClass;
5632 }
5633
5634 static bool HasAddress(LoadKind load_kind) {
5635 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5636 }
5637
5638 static bool HasDexCacheReference(LoadKind load_kind) {
5639 return load_kind == LoadKind::kDexCachePcRelative;
5640 }
5641
5642 void SetLoadKindInternal(LoadKind load_kind);
5643
5644 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5645 // For other load kinds it's empty or possibly some architecture-specific instruction
5646 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5647 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005648
Andreas Gampea5b09a62016-11-17 15:21:22 -08005649 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005650 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005651
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005652 union {
5653 uint32_t dex_cache_element_index; // Only for dex cache reference.
5654 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5655 } load_data_;
5656
Calin Juravleacf735c2015-02-12 15:25:22 +00005657 ReferenceTypeInfo loaded_class_rti_;
5658
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005659 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5660};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005661std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5662
5663// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5664inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5665 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5666 return load_data_.dex_cache_element_index;
5667}
5668
5669// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5670inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005671 // The special input is used for PC-relative loads on some architectures,
5672 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005673 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005674 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5675 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5676 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005677 DCHECK(special_input_.GetInstruction() == nullptr);
5678 special_input_ = HUserRecord<HInstruction*>(special_input);
5679 special_input->AddUseAt(this, 0);
5680}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005681
Vladimir Marko372f10e2016-05-17 16:30:10 +01005682class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005683 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005684 // Determines how to load the String.
5685 enum class LoadKind {
5686 // Use boot image String* address that will be known at link time.
5687 // Used for boot image strings referenced by boot image code in non-PIC mode.
5688 kBootImageLinkTimeAddress,
5689
5690 // Use PC-relative boot image String* address that will be known at link time.
5691 // Used for boot image strings referenced by boot image code in PIC mode.
5692 kBootImageLinkTimePcRelative,
5693
5694 // Use a known boot image String* address, embedded in the code by the codegen.
5695 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5696 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5697 // GetIncludePatchInformation().
5698 kBootImageAddress,
5699
Vladimir Markoaad75c62016-10-03 08:46:48 +00005700 // Load from an entry in the .bss section using a PC-relative load.
5701 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5702 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005703
5704 // Load from resolved strings array accessed through the class loaded from
5705 // the compiled method's own ArtMethod*. This is the default access type when
5706 // all other types are unavailable.
5707 kDexCacheViaMethod,
5708
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005709 // Load from the root table associated with the JIT compiled method.
5710 kJitTableAddress,
5711
5712 kLast = kJitTableAddress,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005713 };
5714
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005715 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005716 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005717 const DexFile& dex_file,
5718 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005719 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5720 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005721 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005722 SetPackedFlag<kFlagIsInDexCache>(false);
5723 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005724 load_data_.dex_file_ = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005725 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005726
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005727 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5728 DCHECK(HasAddress(load_kind));
5729 load_data_.address = address;
5730 SetLoadKindInternal(load_kind);
5731 }
5732
5733 void SetLoadKindWithStringReference(LoadKind load_kind,
5734 const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005735 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005736 DCHECK(HasStringReference(load_kind));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005737 load_data_.dex_file_ = &dex_file;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005738 string_index_ = string_index;
5739 SetLoadKindInternal(load_kind);
5740 }
5741
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005742 LoadKind GetLoadKind() const {
5743 return GetPackedField<LoadKindField>();
5744 }
5745
5746 const DexFile& GetDexFile() const;
5747
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005748 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005749 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5750 return string_index_;
5751 }
5752
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005753 uint64_t GetAddress() const {
5754 DCHECK(HasAddress(GetLoadKind()));
5755 return load_data_.address;
5756 }
5757
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005758 bool CanBeMoved() const OVERRIDE { return true; }
5759
Vladimir Marko372f10e2016-05-17 16:30:10 +01005760 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005761
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005762 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005763
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005764 // Will call the runtime if we need to load the string through
5765 // the dex cache and the string is not guaranteed to be there yet.
5766 bool NeedsEnvironment() const OVERRIDE {
5767 LoadKind load_kind = GetLoadKind();
5768 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5769 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005770 load_kind == LoadKind::kBootImageAddress ||
5771 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005772 return false;
5773 }
5774 return !IsInDexCache();
5775 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005776
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005777 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5778 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5779 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005780
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005781 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005782 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005783
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005784 static SideEffects SideEffectsForArchRuntimeCalls() {
5785 return SideEffects::CanTriggerGC();
5786 }
5787
Vladimir Markoa1de9182016-02-25 11:37:38 +00005788 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5789
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005790 void MarkInDexCache() {
5791 SetPackedFlag<kFlagIsInDexCache>(true);
5792 DCHECK(!NeedsEnvironment());
5793 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005794 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005795 }
5796
Vladimir Marko372f10e2016-05-17 16:30:10 +01005797 void AddSpecialInput(HInstruction* special_input);
5798
5799 using HInstruction::GetInputRecords; // Keep the const version visible.
5800 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5801 return ArrayRef<HUserRecord<HInstruction*>>(
5802 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005803 }
5804
Vladimir Marko372f10e2016-05-17 16:30:10 +01005805 Primitive::Type GetType() const OVERRIDE {
5806 return Primitive::kPrimNot;
5807 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005808
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005809 DECLARE_INSTRUCTION(LoadString);
5810
5811 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005812 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005813 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5814 static constexpr size_t kFieldLoadKindSize =
5815 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5816 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005817 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005818 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005819
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005820 static bool HasStringReference(LoadKind load_kind) {
5821 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5822 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005823 load_kind == LoadKind::kBssEntry ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005824 load_kind == LoadKind::kDexCacheViaMethod ||
5825 load_kind == LoadKind::kJitTableAddress;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005826 }
5827
5828 static bool HasAddress(LoadKind load_kind) {
Vladimir Marko54d6a202016-11-09 12:46:38 +00005829 return load_kind == LoadKind::kBootImageAddress;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005830 }
5831
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005832 void SetLoadKindInternal(LoadKind load_kind);
5833
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005834 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5835 // For other load kinds it's empty or possibly some architecture-specific instruction
5836 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005837 HUserRecord<HInstruction*> special_input_;
5838
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005839 // String index serves also as the hash code and it's also needed for slow-paths,
5840 // so it must not be overwritten with other load data.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005841 dex::StringIndex string_index_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005842
5843 union {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005844 const DexFile* dex_file_; // For string reference.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005845 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5846 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005847
5848 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5849};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005850std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5851
5852// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5853inline const DexFile& HLoadString::GetDexFile() const {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005854 DCHECK(HasStringReference(GetLoadKind())) << GetLoadKind();
5855 return *load_data_.dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005856}
5857
5858// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5859inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005860 // The special input is used for PC-relative loads on some architectures,
5861 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005862 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005863 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005864 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5865 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005866 // HLoadString::GetInputRecords() returns an empty array at this point,
5867 // so use the GetInputRecords() from the base class to set the input record.
5868 DCHECK(special_input_.GetInstruction() == nullptr);
5869 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005870 special_input->AddUseAt(this, 0);
5871}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005872
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005873/**
5874 * Performs an initialization check on its Class object input.
5875 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005876class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005877 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005878 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005879 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005880 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005881 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5882 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005883 SetRawInputAt(0, constant);
5884 }
5885
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005886 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005887 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005888 return true;
5889 }
5890
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005891 bool NeedsEnvironment() const OVERRIDE {
5892 // May call runtime to initialize the class.
5893 return true;
5894 }
5895
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005896 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005897
5898 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5899
5900 DECLARE_INSTRUCTION(ClinitCheck);
5901
5902 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005903 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5904};
5905
Vladimir Markofcb503c2016-05-18 12:48:17 +01005906class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005907 public:
5908 HStaticFieldGet(HInstruction* cls,
5909 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005910 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005911 bool is_volatile,
5912 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005913 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005914 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005915 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005916 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005917 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005918 field_info_(field_offset,
5919 field_type,
5920 is_volatile,
5921 field_idx,
5922 declaring_class_def_index,
5923 dex_file,
5924 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005925 SetRawInputAt(0, cls);
5926 }
5927
Calin Juravle52c48962014-12-16 17:02:57 +00005928
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005929 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005930
Vladimir Marko372f10e2016-05-17 16:30:10 +01005931 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5932 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005933 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005934 }
5935
5936 size_t ComputeHashCode() const OVERRIDE {
5937 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5938 }
5939
Calin Juravle52c48962014-12-16 17:02:57 +00005940 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005941 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5942 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005943 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005944
5945 DECLARE_INSTRUCTION(StaticFieldGet);
5946
5947 private:
5948 const FieldInfo field_info_;
5949
5950 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5951};
5952
Vladimir Markofcb503c2016-05-18 12:48:17 +01005953class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005954 public:
5955 HStaticFieldSet(HInstruction* cls,
5956 HInstruction* value,
5957 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005958 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005959 bool is_volatile,
5960 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005961 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005962 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005963 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005964 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005965 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005966 field_info_(field_offset,
5967 field_type,
5968 is_volatile,
5969 field_idx,
5970 declaring_class_def_index,
5971 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005972 dex_cache) {
5973 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005974 SetRawInputAt(0, cls);
5975 SetRawInputAt(1, value);
5976 }
5977
Calin Juravle52c48962014-12-16 17:02:57 +00005978 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005979 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5980 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005981 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005982
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005983 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005984 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5985 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005986
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005987 DECLARE_INSTRUCTION(StaticFieldSet);
5988
5989 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005990 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5991 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5992 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5993 "Too many packed fields.");
5994
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005995 const FieldInfo field_info_;
5996
5997 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5998};
5999
Vladimir Markofcb503c2016-05-18 12:48:17 +01006000class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006001 public:
6002 HUnresolvedInstanceFieldGet(HInstruction* obj,
6003 Primitive::Type field_type,
6004 uint32_t field_index,
6005 uint32_t dex_pc)
6006 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6007 field_index_(field_index) {
6008 SetRawInputAt(0, obj);
6009 }
6010
6011 bool NeedsEnvironment() const OVERRIDE { return true; }
6012 bool CanThrow() const OVERRIDE { return true; }
6013
6014 Primitive::Type GetFieldType() const { return GetType(); }
6015 uint32_t GetFieldIndex() const { return field_index_; }
6016
6017 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6018
6019 private:
6020 const uint32_t field_index_;
6021
6022 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6023};
6024
Vladimir Markofcb503c2016-05-18 12:48:17 +01006025class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006026 public:
6027 HUnresolvedInstanceFieldSet(HInstruction* obj,
6028 HInstruction* value,
6029 Primitive::Type field_type,
6030 uint32_t field_index,
6031 uint32_t dex_pc)
6032 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006033 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006034 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006035 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006036 SetRawInputAt(0, obj);
6037 SetRawInputAt(1, value);
6038 }
6039
6040 bool NeedsEnvironment() const OVERRIDE { return true; }
6041 bool CanThrow() const OVERRIDE { return true; }
6042
Vladimir Markoa1de9182016-02-25 11:37:38 +00006043 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006044 uint32_t GetFieldIndex() const { return field_index_; }
6045
6046 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6047
6048 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006049 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6050 static constexpr size_t kFieldFieldTypeSize =
6051 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6052 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6053 kFieldFieldType + kFieldFieldTypeSize;
6054 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6055 "Too many packed fields.");
6056 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6057
Calin Juravlee460d1d2015-09-29 04:52:17 +01006058 const uint32_t field_index_;
6059
6060 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6061};
6062
Vladimir Markofcb503c2016-05-18 12:48:17 +01006063class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006064 public:
6065 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6066 uint32_t field_index,
6067 uint32_t dex_pc)
6068 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6069 field_index_(field_index) {
6070 }
6071
6072 bool NeedsEnvironment() const OVERRIDE { return true; }
6073 bool CanThrow() const OVERRIDE { return true; }
6074
6075 Primitive::Type GetFieldType() const { return GetType(); }
6076 uint32_t GetFieldIndex() const { return field_index_; }
6077
6078 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6079
6080 private:
6081 const uint32_t field_index_;
6082
6083 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6084};
6085
Vladimir Markofcb503c2016-05-18 12:48:17 +01006086class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006087 public:
6088 HUnresolvedStaticFieldSet(HInstruction* value,
6089 Primitive::Type field_type,
6090 uint32_t field_index,
6091 uint32_t dex_pc)
6092 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006093 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006094 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006095 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006096 SetRawInputAt(0, value);
6097 }
6098
6099 bool NeedsEnvironment() const OVERRIDE { return true; }
6100 bool CanThrow() const OVERRIDE { return true; }
6101
Vladimir Markoa1de9182016-02-25 11:37:38 +00006102 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006103 uint32_t GetFieldIndex() const { return field_index_; }
6104
6105 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6106
6107 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006108 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6109 static constexpr size_t kFieldFieldTypeSize =
6110 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6111 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6112 kFieldFieldType + kFieldFieldTypeSize;
6113 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6114 "Too many packed fields.");
6115 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6116
Calin Juravlee460d1d2015-09-29 04:52:17 +01006117 const uint32_t field_index_;
6118
6119 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6120};
6121
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006122// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006123class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006124 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006125 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6126 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006127
David Brazdilbbd733e2015-08-18 17:48:17 +01006128 bool CanBeNull() const OVERRIDE { return false; }
6129
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006130 DECLARE_INSTRUCTION(LoadException);
6131
6132 private:
6133 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6134};
6135
David Brazdilcb1c0552015-08-04 16:22:25 +01006136// Implicit part of move-exception which clears thread-local exception storage.
6137// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006138class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006139 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006140 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6141 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006142
6143 DECLARE_INSTRUCTION(ClearException);
6144
6145 private:
6146 DISALLOW_COPY_AND_ASSIGN(HClearException);
6147};
6148
Vladimir Markofcb503c2016-05-18 12:48:17 +01006149class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006150 public:
6151 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006152 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006153 SetRawInputAt(0, exception);
6154 }
6155
6156 bool IsControlFlow() const OVERRIDE { return true; }
6157
6158 bool NeedsEnvironment() const OVERRIDE { return true; }
6159
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006160 bool CanThrow() const OVERRIDE { return true; }
6161
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006162
6163 DECLARE_INSTRUCTION(Throw);
6164
6165 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006166 DISALLOW_COPY_AND_ASSIGN(HThrow);
6167};
6168
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006169/**
6170 * Implementation strategies for the code generator of a HInstanceOf
6171 * or `HCheckCast`.
6172 */
6173enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006174 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006175 kExactCheck, // Can do a single class compare.
6176 kClassHierarchyCheck, // Can just walk the super class chain.
6177 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6178 kInterfaceCheck, // No optimization yet when checking against an interface.
6179 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006180 kArrayCheck, // No optimization yet when checking against a generic array.
6181 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006182};
6183
Roland Levillain86503782016-02-11 19:07:30 +00006184std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6185
Vladimir Markofcb503c2016-05-18 12:48:17 +01006186class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006187 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006188 HInstanceOf(HInstruction* object,
6189 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006190 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006191 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006192 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006193 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006194 dex_pc) {
6195 SetPackedField<TypeCheckKindField>(check_kind);
6196 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006197 SetRawInputAt(0, object);
6198 SetRawInputAt(1, constant);
6199 }
6200
6201 bool CanBeMoved() const OVERRIDE { return true; }
6202
Vladimir Marko372f10e2016-05-17 16:30:10 +01006203 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006204 return true;
6205 }
6206
6207 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006208 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006209 }
6210
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006211 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006212 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6213 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6214 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6215 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006216
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006217 static bool CanCallRuntime(TypeCheckKind check_kind) {
6218 // Mips currently does runtime calls for any other checks.
6219 return check_kind != TypeCheckKind::kExactCheck;
6220 }
6221
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006222 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006223 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006224 }
6225
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006226 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006227
6228 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006229 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6230 static constexpr size_t kFieldTypeCheckKindSize =
6231 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6232 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6233 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6234 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6235 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006236
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006237 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6238};
6239
Vladimir Markofcb503c2016-05-18 12:48:17 +01006240class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006241 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006242 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006243 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006244 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6245 SetPackedFlag<kFlagUpperCanBeNull>(true);
6246 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006247 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006248 SetRawInputAt(0, input);
6249 }
6250
David Brazdilf5552582015-12-27 13:36:12 +00006251 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006252 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006253 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006254 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006255
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006256 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006257 DCHECK(GetUpperCanBeNull() || !can_be_null);
6258 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006259 }
6260
Vladimir Markoa1de9182016-02-25 11:37:38 +00006261 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006262
Calin Juravleb1498f62015-02-16 13:13:29 +00006263 DECLARE_INSTRUCTION(BoundType);
6264
6265 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006266 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6267 // is false then CanBeNull() cannot be true).
6268 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6269 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6270 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6271 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6272
Calin Juravleb1498f62015-02-16 13:13:29 +00006273 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006274 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6275 // It is used to bound the type in cases like:
6276 // if (x instanceof ClassX) {
6277 // // uper_bound_ will be ClassX
6278 // }
David Brazdilf5552582015-12-27 13:36:12 +00006279 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006280
6281 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6282};
6283
Vladimir Markofcb503c2016-05-18 12:48:17 +01006284class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006285 public:
6286 HCheckCast(HInstruction* object,
6287 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006288 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006289 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006290 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6291 SetPackedField<TypeCheckKindField>(check_kind);
6292 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006293 SetRawInputAt(0, object);
6294 SetRawInputAt(1, constant);
6295 }
6296
6297 bool CanBeMoved() const OVERRIDE { return true; }
6298
Vladimir Marko372f10e2016-05-17 16:30:10 +01006299 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006300 return true;
6301 }
6302
6303 bool NeedsEnvironment() const OVERRIDE {
6304 // Instruction may throw a CheckCastError.
6305 return true;
6306 }
6307
6308 bool CanThrow() const OVERRIDE { return true; }
6309
Vladimir Markoa1de9182016-02-25 11:37:38 +00006310 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6311 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6312 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6313 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006314
6315 DECLARE_INSTRUCTION(CheckCast);
6316
6317 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006318 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6319 static constexpr size_t kFieldTypeCheckKindSize =
6320 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6321 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6322 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6323 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6324 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006325
6326 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006327};
6328
Andreas Gampe26de38b2016-07-27 17:53:11 -07006329/**
6330 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6331 * @details We define the combined barrier types that are actually required
6332 * by the Java Memory Model, rather than using exactly the terminology from
6333 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6334 * primitives. Note that the JSR-133 cookbook generally does not deal with
6335 * store atomicity issues, and the recipes there are not always entirely sufficient.
6336 * The current recipe is as follows:
6337 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6338 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6339 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6340 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6341 * class has final fields.
6342 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6343 * store-to-memory instructions. Only generated together with non-temporal stores.
6344 */
6345enum MemBarrierKind {
6346 kAnyStore,
6347 kLoadAny,
6348 kStoreStore,
6349 kAnyAny,
6350 kNTStoreStore,
6351 kLastBarrierKind = kNTStoreStore
6352};
6353std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6354
Vladimir Markofcb503c2016-05-18 12:48:17 +01006355class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006356 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006357 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006358 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006359 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6360 SetPackedField<BarrierKindField>(barrier_kind);
6361 }
Calin Juravle27df7582015-04-17 19:12:31 +01006362
Vladimir Markoa1de9182016-02-25 11:37:38 +00006363 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006364
6365 DECLARE_INSTRUCTION(MemoryBarrier);
6366
6367 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006368 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6369 static constexpr size_t kFieldBarrierKindSize =
6370 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6371 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6372 kFieldBarrierKind + kFieldBarrierKindSize;
6373 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6374 "Too many packed fields.");
6375 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006376
6377 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6378};
6379
Vladimir Markofcb503c2016-05-18 12:48:17 +01006380class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006381 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006382 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006383 kEnter,
6384 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006385 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006386 };
6387
6388 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006389 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006390 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6391 dex_pc) {
6392 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006393 SetRawInputAt(0, object);
6394 }
6395
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006396 // Instruction may go into runtime, so we need an environment.
6397 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006398
6399 bool CanThrow() const OVERRIDE {
6400 // Verifier guarantees that monitor-exit cannot throw.
6401 // This is important because it allows the HGraphBuilder to remove
6402 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6403 return IsEnter();
6404 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006405
Vladimir Markoa1de9182016-02-25 11:37:38 +00006406 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6407 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006408
6409 DECLARE_INSTRUCTION(MonitorOperation);
6410
Calin Juravle52c48962014-12-16 17:02:57 +00006411 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006412 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6413 static constexpr size_t kFieldOperationKindSize =
6414 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6415 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6416 kFieldOperationKind + kFieldOperationKindSize;
6417 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6418 "Too many packed fields.");
6419 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006420
6421 private:
6422 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6423};
6424
Vladimir Markofcb503c2016-05-18 12:48:17 +01006425class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006426 public:
6427 HSelect(HInstruction* condition,
6428 HInstruction* true_value,
6429 HInstruction* false_value,
6430 uint32_t dex_pc)
6431 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6432 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6433
6434 // First input must be `true_value` or `false_value` to allow codegens to
6435 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6436 // that architectures which implement HSelect as a conditional move also
6437 // will not need to invert the condition.
6438 SetRawInputAt(0, false_value);
6439 SetRawInputAt(1, true_value);
6440 SetRawInputAt(2, condition);
6441 }
6442
6443 HInstruction* GetFalseValue() const { return InputAt(0); }
6444 HInstruction* GetTrueValue() const { return InputAt(1); }
6445 HInstruction* GetCondition() const { return InputAt(2); }
6446
6447 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006448 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6449 return true;
6450 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006451
6452 bool CanBeNull() const OVERRIDE {
6453 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6454 }
6455
6456 DECLARE_INSTRUCTION(Select);
6457
6458 private:
6459 DISALLOW_COPY_AND_ASSIGN(HSelect);
6460};
6461
Vladimir Markof9f64412015-09-02 14:05:49 +01006462class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006463 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006464 MoveOperands(Location source,
6465 Location destination,
6466 Primitive::Type type,
6467 HInstruction* instruction)
6468 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006469
6470 Location GetSource() const { return source_; }
6471 Location GetDestination() const { return destination_; }
6472
6473 void SetSource(Location value) { source_ = value; }
6474 void SetDestination(Location value) { destination_ = value; }
6475
6476 // The parallel move resolver marks moves as "in-progress" by clearing the
6477 // destination (but not the source).
6478 Location MarkPending() {
6479 DCHECK(!IsPending());
6480 Location dest = destination_;
6481 destination_ = Location::NoLocation();
6482 return dest;
6483 }
6484
6485 void ClearPending(Location dest) {
6486 DCHECK(IsPending());
6487 destination_ = dest;
6488 }
6489
6490 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006491 DCHECK(source_.IsValid() || destination_.IsInvalid());
6492 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006493 }
6494
6495 // True if this blocks a move from the given location.
6496 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006497 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006498 }
6499
6500 // A move is redundant if it's been eliminated, if its source and
6501 // destination are the same, or if its destination is unneeded.
6502 bool IsRedundant() const {
6503 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6504 }
6505
6506 // We clear both operands to indicate move that's been eliminated.
6507 void Eliminate() {
6508 source_ = destination_ = Location::NoLocation();
6509 }
6510
6511 bool IsEliminated() const {
6512 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6513 return source_.IsInvalid();
6514 }
6515
Alexey Frunze4dda3372015-06-01 18:31:49 -07006516 Primitive::Type GetType() const { return type_; }
6517
Nicolas Geoffray90218252015-04-15 11:56:51 +01006518 bool Is64BitMove() const {
6519 return Primitive::Is64BitType(type_);
6520 }
6521
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006522 HInstruction* GetInstruction() const { return instruction_; }
6523
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006524 private:
6525 Location source_;
6526 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006527 // The type this move is for.
6528 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006529 // The instruction this move is assocatied with. Null when this move is
6530 // for moving an input in the expected locations of user (including a phi user).
6531 // This is only used in debug mode, to ensure we do not connect interval siblings
6532 // in the same parallel move.
6533 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006534};
6535
Roland Levillainc9285912015-12-18 10:38:42 +00006536std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6537
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006538static constexpr size_t kDefaultNumberOfMoves = 4;
6539
Vladimir Markofcb503c2016-05-18 12:48:17 +01006540class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006541 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006542 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006543 : HTemplateInstruction(SideEffects::None(), dex_pc),
6544 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6545 moves_.reserve(kDefaultNumberOfMoves);
6546 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006547
Nicolas Geoffray90218252015-04-15 11:56:51 +01006548 void AddMove(Location source,
6549 Location destination,
6550 Primitive::Type type,
6551 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006552 DCHECK(source.IsValid());
6553 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006554 if (kIsDebugBuild) {
6555 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006556 for (const MoveOperands& move : moves_) {
6557 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006558 // Special case the situation where the move is for the spill slot
6559 // of the instruction.
6560 if ((GetPrevious() == instruction)
6561 || ((GetPrevious() == nullptr)
6562 && instruction->IsPhi()
6563 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006564 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006565 << "Doing parallel moves for the same instruction.";
6566 } else {
6567 DCHECK(false) << "Doing parallel moves for the same instruction.";
6568 }
6569 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006570 }
6571 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006572 for (const MoveOperands& move : moves_) {
6573 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006574 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006575 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006576 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006577 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006578 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006579 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006580 }
6581
Vladimir Marko225b6462015-09-28 12:17:40 +01006582 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006583 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006584 }
6585
Vladimir Marko225b6462015-09-28 12:17:40 +01006586 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006587
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006588 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006589
6590 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006591 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006592
6593 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6594};
6595
Mark Mendell0616ae02015-04-17 12:49:27 -04006596} // namespace art
6597
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006598#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6599#include "nodes_shared.h"
6600#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006601#ifdef ART_ENABLE_CODEGEN_arm
6602#include "nodes_arm.h"
6603#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006604#ifdef ART_ENABLE_CODEGEN_arm64
6605#include "nodes_arm64.h"
6606#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006607#ifdef ART_ENABLE_CODEGEN_mips
6608#include "nodes_mips.h"
6609#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006610#ifdef ART_ENABLE_CODEGEN_x86
6611#include "nodes_x86.h"
6612#endif
6613
6614namespace art {
6615
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006616class HGraphVisitor : public ValueObject {
6617 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006618 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6619 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006620
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006621 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006622 virtual void VisitBasicBlock(HBasicBlock* block);
6623
Roland Levillain633021e2014-10-01 14:12:25 +01006624 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006625 void VisitInsertionOrder();
6626
Roland Levillain633021e2014-10-01 14:12:25 +01006627 // Visit the graph following dominator tree reverse post-order.
6628 void VisitReversePostOrder();
6629
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006630 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006631
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006632 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006633#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006634 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6635
6636 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6637
6638#undef DECLARE_VISIT_INSTRUCTION
6639
6640 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006641 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006642
6643 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6644};
6645
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006646class HGraphDelegateVisitor : public HGraphVisitor {
6647 public:
6648 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6649 virtual ~HGraphDelegateVisitor() {}
6650
6651 // Visit functions that delegate to to super class.
6652#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006653 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006654
6655 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6656
6657#undef DECLARE_VISIT_INSTRUCTION
6658
6659 private:
6660 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6661};
6662
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006663// Iterator over the blocks that art part of the loop. Includes blocks part
6664// of an inner loop. The order in which the blocks are iterated is on their
6665// block id.
6666class HBlocksInLoopIterator : public ValueObject {
6667 public:
6668 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6669 : blocks_in_loop_(info.GetBlocks()),
6670 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6671 index_(0) {
6672 if (!blocks_in_loop_.IsBitSet(index_)) {
6673 Advance();
6674 }
6675 }
6676
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006677 bool Done() const { return index_ == blocks_.size(); }
6678 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006679 void Advance() {
6680 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006681 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006682 if (blocks_in_loop_.IsBitSet(index_)) {
6683 break;
6684 }
6685 }
6686 }
6687
6688 private:
6689 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006690 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006691 size_t index_;
6692
6693 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6694};
6695
Mingyao Yang3584bce2015-05-19 16:01:59 -07006696// Iterator over the blocks that art part of the loop. Includes blocks part
6697// of an inner loop. The order in which the blocks are iterated is reverse
6698// post order.
6699class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6700 public:
6701 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6702 : blocks_in_loop_(info.GetBlocks()),
6703 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6704 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006705 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006706 Advance();
6707 }
6708 }
6709
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006710 bool Done() const { return index_ == blocks_.size(); }
6711 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006712 void Advance() {
6713 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006714 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6715 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006716 break;
6717 }
6718 }
6719 }
6720
6721 private:
6722 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006723 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006724 size_t index_;
6725
6726 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6727};
6728
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006729inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006730 if (constant->IsIntConstant()) {
6731 return constant->AsIntConstant()->GetValue();
6732 } else if (constant->IsLongConstant()) {
6733 return constant->AsLongConstant()->GetValue();
6734 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006735 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006736 return 0;
6737 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006738}
6739
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006740#define INSTRUCTION_TYPE_CHECK(type, super) \
6741 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6742 inline const H##type* HInstruction::As##type() const { \
6743 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6744 } \
6745 inline H##type* HInstruction::As##type() { \
6746 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6747 }
6748
6749 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6750#undef INSTRUCTION_TYPE_CHECK
6751
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006752// Create space in `blocks` for adding `number_of_new_blocks` entries
6753// starting at location `at`. Blocks after `at` are moved accordingly.
6754inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6755 size_t number_of_new_blocks,
6756 size_t after) {
6757 DCHECK_LT(after, blocks->size());
6758 size_t old_size = blocks->size();
6759 size_t new_size = old_size + number_of_new_blocks;
6760 blocks->resize(new_size);
6761 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6762}
6763
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006764} // namespace art
6765
6766#endif // ART_COMPILER_OPTIMIZING_NODES_H_