blob: eebc49c991d022ca28e3c81d2658bb055bd93714 [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.
2075 virtual bool CanBeMoved() const { return false; }
2076
2077 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002078 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002079 return false;
2080 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002081
2082 // Returns whether any data encoded in the two instructions is equal.
2083 // This method does not look at the inputs. Both instructions must be
2084 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002085 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002086 return false;
2087 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002088
2089 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002090 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002091 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002092 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002093
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002094 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2095 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2096 // the virtual function because the __attribute__((__pure__)) doesn't really
2097 // apply the strong requirement for virtual functions, preventing optimizations.
2098 InstructionKind GetKind() const PURE;
2099 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002100
2101 virtual size_t ComputeHashCode() const {
2102 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002103 for (const HInstruction* input : GetInputs()) {
2104 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002105 }
2106 return result;
2107 }
2108
2109 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002110 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002111 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002112
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002113 size_t GetLifetimePosition() const { return lifetime_position_; }
2114 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2115 LiveInterval* GetLiveInterval() const { return live_interval_; }
2116 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2117 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2118
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002119 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2120
2121 // Returns whether the code generation of the instruction will require to have access
2122 // to the current method. Such instructions are:
2123 // (1): Instructions that require an environment, as calling the runtime requires
2124 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002125 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2126 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002127 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002128 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002129 }
2130
Vladimir Markodc151b22015-10-15 18:02:30 +01002131 // Returns whether the code generation of the instruction will require to have access
2132 // to the dex cache of the current method's declaring class via the current method.
2133 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002134
Mark Mendellc4701932015-04-10 13:18:51 -04002135 // Does this instruction have any use in an environment before
2136 // control flow hits 'other'?
2137 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2138
2139 // Remove all references to environment uses of this instruction.
2140 // The caller must ensure that this is safe to do.
2141 void RemoveEnvironmentUsers();
2142
Vladimir Markoa1de9182016-02-25 11:37:38 +00002143 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2144 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002145
David Brazdil1abb4192015-02-17 18:33:36 +00002146 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002147 // If set, the machine code for this instruction is assumed to be generated by
2148 // its users. Used by liveness analysis to compute use positions accordingly.
2149 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2150 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2151 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2152 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2153
Vladimir Marko372f10e2016-05-17 16:30:10 +01002154 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2155 return GetInputRecords()[i];
2156 }
2157
2158 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2159 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2160 input_records[index] = input;
2161 }
David Brazdil1abb4192015-02-17 18:33:36 +00002162
Vladimir Markoa1de9182016-02-25 11:37:38 +00002163 uint32_t GetPackedFields() const {
2164 return packed_fields_;
2165 }
2166
2167 template <size_t flag>
2168 bool GetPackedFlag() const {
2169 return (packed_fields_ & (1u << flag)) != 0u;
2170 }
2171
2172 template <size_t flag>
2173 void SetPackedFlag(bool value = true) {
2174 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2175 }
2176
2177 template <typename BitFieldType>
2178 typename BitFieldType::value_type GetPackedField() const {
2179 return BitFieldType::Decode(packed_fields_);
2180 }
2181
2182 template <typename BitFieldType>
2183 void SetPackedField(typename BitFieldType::value_type value) {
2184 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2185 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2186 }
2187
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002188 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002189 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2190 auto before_use_node = uses_.before_begin();
2191 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2192 HInstruction* user = use_node->GetUser();
2193 size_t input_index = use_node->GetIndex();
2194 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2195 before_use_node = use_node;
2196 }
2197 }
2198
2199 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2200 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2201 if (next != uses_.end()) {
2202 HInstruction* next_user = next->GetUser();
2203 size_t next_index = next->GetIndex();
2204 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2205 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2206 }
2207 }
2208
2209 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2210 auto before_env_use_node = env_uses_.before_begin();
2211 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2212 HEnvironment* user = env_use_node->GetUser();
2213 size_t input_index = env_use_node->GetIndex();
2214 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2215 before_env_use_node = env_use_node;
2216 }
2217 }
2218
2219 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2220 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2221 if (next != env_uses_.end()) {
2222 HEnvironment* next_user = next->GetUser();
2223 size_t next_index = next->GetIndex();
2224 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2225 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2226 }
2227 }
David Brazdil1abb4192015-02-17 18:33:36 +00002228
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002229 HInstruction* previous_;
2230 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002231 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002232 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002233
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002234 // An instruction gets an id when it is added to the graph.
2235 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002236 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002237 int id_;
2238
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002239 // When doing liveness analysis, instructions that have uses get an SSA index.
2240 int ssa_index_;
2241
Vladimir Markoa1de9182016-02-25 11:37:38 +00002242 // Packed fields.
2243 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002244
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002245 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002246 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002247
2248 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002249 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002250
Nicolas Geoffray39468442014-09-02 15:17:15 +01002251 // The environment associated with this instruction. Not null if the instruction
2252 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002253 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002254
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002255 // Set by the code generator.
2256 LocationSummary* locations_;
2257
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002258 // Set by the liveness analysis.
2259 LiveInterval* live_interval_;
2260
2261 // Set by the liveness analysis, this is the position in a linear
2262 // order of blocks where this instruction's live interval start.
2263 size_t lifetime_position_;
2264
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002265 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002266
Vladimir Markoa1de9182016-02-25 11:37:38 +00002267 // The reference handle part of the reference type info.
2268 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002269 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002270 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002271
David Brazdil1abb4192015-02-17 18:33:36 +00002272 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002273 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002274 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002275 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002276 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002277
2278 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2279};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002280std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002281
2282class HInstructionIterator : public ValueObject {
2283 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002284 explicit HInstructionIterator(const HInstructionList& instructions)
2285 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002286 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002287 }
2288
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002289 bool Done() const { return instruction_ == nullptr; }
2290 HInstruction* Current() const { return instruction_; }
2291 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002292 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002293 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002294 }
2295
2296 private:
2297 HInstruction* instruction_;
2298 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002299
2300 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002301};
2302
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002303class HBackwardInstructionIterator : public ValueObject {
2304 public:
2305 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2306 : instruction_(instructions.last_instruction_) {
2307 next_ = Done() ? nullptr : instruction_->GetPrevious();
2308 }
2309
2310 bool Done() const { return instruction_ == nullptr; }
2311 HInstruction* Current() const { return instruction_; }
2312 void Advance() {
2313 instruction_ = next_;
2314 next_ = Done() ? nullptr : instruction_->GetPrevious();
2315 }
2316
2317 private:
2318 HInstruction* instruction_;
2319 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002320
2321 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002322};
2323
Vladimir Markof9f64412015-09-02 14:05:49 +01002324template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002325class HTemplateInstruction: public HInstruction {
2326 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002327 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002328 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002329 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002330
Vladimir Marko372f10e2016-05-17 16:30:10 +01002331 using HInstruction::GetInputRecords; // Keep the const version visible.
2332 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2333 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002334 }
2335
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002336 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002337 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002338
2339 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002340};
2341
Vladimir Markof9f64412015-09-02 14:05:49 +01002342// HTemplateInstruction specialization for N=0.
2343template<>
2344class HTemplateInstruction<0>: public HInstruction {
2345 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002346 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002347 : HInstruction(side_effects, dex_pc) {}
2348
Vladimir Markof9f64412015-09-02 14:05:49 +01002349 virtual ~HTemplateInstruction() {}
2350
Vladimir Marko372f10e2016-05-17 16:30:10 +01002351 using HInstruction::GetInputRecords; // Keep the const version visible.
2352 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2353 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002354 }
2355
2356 private:
2357 friend class SsaBuilder;
2358};
2359
Dave Allison20dfc792014-06-16 20:44:29 -07002360template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002361class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002362 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002363 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002364 : HTemplateInstruction<N>(side_effects, dex_pc) {
2365 this->template SetPackedField<TypeField>(type);
2366 }
Dave Allison20dfc792014-06-16 20:44:29 -07002367 virtual ~HExpression() {}
2368
Vladimir Markoa1de9182016-02-25 11:37:38 +00002369 Primitive::Type GetType() const OVERRIDE {
2370 return TypeField::Decode(this->GetPackedFields());
2371 }
Dave Allison20dfc792014-06-16 20:44:29 -07002372
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002373 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002374 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2375 static constexpr size_t kFieldTypeSize =
2376 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2377 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2378 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2379 "Too many packed fields.");
2380 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002381};
2382
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002383// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2384// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002385class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002386 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002387 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2388 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002389
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002390 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002391
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002392 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002393
2394 private:
2395 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2396};
2397
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002398// Represents dex's RETURN opcodes. A HReturn is a control flow
2399// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002400class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002401 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002402 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2403 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002404 SetRawInputAt(0, value);
2405 }
2406
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002407 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002408
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002409 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002410
2411 private:
2412 DISALLOW_COPY_AND_ASSIGN(HReturn);
2413};
2414
Vladimir Markofcb503c2016-05-18 12:48:17 +01002415class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002416 public:
2417 HPhi(ArenaAllocator* arena,
2418 uint32_t reg_number,
2419 size_t number_of_inputs,
2420 Primitive::Type type,
2421 uint32_t dex_pc = kNoDexPc)
2422 : HInstruction(SideEffects::None(), dex_pc),
2423 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2424 reg_number_(reg_number) {
2425 SetPackedField<TypeField>(ToPhiType(type));
2426 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2427 // Phis are constructed live and marked dead if conflicting or unused.
2428 // Individual steps of SsaBuilder should assume that if a phi has been
2429 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2430 SetPackedFlag<kFlagIsLive>(true);
2431 SetPackedFlag<kFlagCanBeNull>(true);
2432 }
2433
2434 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2435 static Primitive::Type ToPhiType(Primitive::Type type) {
2436 return Primitive::PrimitiveKind(type);
2437 }
2438
2439 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2440
Vladimir Marko372f10e2016-05-17 16:30:10 +01002441 using HInstruction::GetInputRecords; // Keep the const version visible.
2442 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2443 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2444 }
David Brazdildee58d62016-04-07 09:54:26 +00002445
2446 void AddInput(HInstruction* input);
2447 void RemoveInputAt(size_t index);
2448
2449 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2450 void SetType(Primitive::Type new_type) {
2451 // Make sure that only valid type changes occur. The following are allowed:
2452 // (1) int -> float/ref (primitive type propagation),
2453 // (2) long -> double (primitive type propagation).
2454 DCHECK(GetType() == new_type ||
2455 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2456 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2457 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2458 SetPackedField<TypeField>(new_type);
2459 }
2460
2461 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2462 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2463
2464 uint32_t GetRegNumber() const { return reg_number_; }
2465
2466 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2467 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2468 bool IsDead() const { return !IsLive(); }
2469 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2470
Vladimir Markoe9004912016-06-16 16:50:52 +01002471 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002472 return other != nullptr
2473 && other->IsPhi()
2474 && other->AsPhi()->GetBlock() == GetBlock()
2475 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2476 }
2477
2478 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2479 // An equivalent phi is a phi having the same dex register and type.
2480 // It assumes that phis with the same dex register are adjacent.
2481 HPhi* GetNextEquivalentPhiWithSameType() {
2482 HInstruction* next = GetNext();
2483 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2484 if (next->GetType() == GetType()) {
2485 return next->AsPhi();
2486 }
2487 next = next->GetNext();
2488 }
2489 return nullptr;
2490 }
2491
2492 DECLARE_INSTRUCTION(Phi);
2493
David Brazdildee58d62016-04-07 09:54:26 +00002494 private:
2495 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2496 static constexpr size_t kFieldTypeSize =
2497 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2498 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2499 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2500 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2501 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2502 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2503
Vladimir Marko372f10e2016-05-17 16:30:10 +01002504 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002505 const uint32_t reg_number_;
2506
2507 DISALLOW_COPY_AND_ASSIGN(HPhi);
2508};
2509
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002510// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002511// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002512// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002513class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002514 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002515 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002516
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002517 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002518
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002519 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002520
2521 private:
2522 DISALLOW_COPY_AND_ASSIGN(HExit);
2523};
2524
2525// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002526class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002527 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002528 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002529
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002530 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002531
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002532 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002533 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002534 }
2535
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002536 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002537
2538 private:
2539 DISALLOW_COPY_AND_ASSIGN(HGoto);
2540};
2541
Roland Levillain9867bc72015-08-05 10:21:34 +01002542class HConstant : public HExpression<0> {
2543 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002544 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2545 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002546
2547 bool CanBeMoved() const OVERRIDE { return true; }
2548
Roland Levillain1a653882016-03-18 18:05:57 +00002549 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002550 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002551 // Is this constant 0 in the arithmetic sense?
2552 virtual bool IsArithmeticZero() const { return false; }
2553 // Is this constant a 0-bit pattern?
2554 virtual bool IsZeroBitPattern() const { return false; }
2555 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002556 virtual bool IsOne() const { return false; }
2557
David Brazdil77a48ae2015-09-15 12:34:04 +00002558 virtual uint64_t GetValueAsUint64() const = 0;
2559
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002560 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002561
2562 private:
2563 DISALLOW_COPY_AND_ASSIGN(HConstant);
2564};
2565
Vladimir Markofcb503c2016-05-18 12:48:17 +01002566class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002567 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002568 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002569 return true;
2570 }
2571
David Brazdil77a48ae2015-09-15 12:34:04 +00002572 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2573
Roland Levillain9867bc72015-08-05 10:21:34 +01002574 size_t ComputeHashCode() const OVERRIDE { return 0; }
2575
Roland Levillain1a653882016-03-18 18:05:57 +00002576 // The null constant representation is a 0-bit pattern.
2577 virtual bool IsZeroBitPattern() const { return true; }
2578
Roland Levillain9867bc72015-08-05 10:21:34 +01002579 DECLARE_INSTRUCTION(NullConstant);
2580
2581 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002582 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002583
2584 friend class HGraph;
2585 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2586};
2587
2588// Constants of the type int. Those can be from Dex instructions, or
2589// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002590class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002591 public:
2592 int32_t GetValue() const { return value_; }
2593
David Brazdil9f389d42015-10-01 14:32:56 +01002594 uint64_t GetValueAsUint64() const OVERRIDE {
2595 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2596 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002597
Vladimir Marko372f10e2016-05-17 16:30:10 +01002598 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002599 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002600 return other->AsIntConstant()->value_ == value_;
2601 }
2602
2603 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2604
2605 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002606 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2607 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002608 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2609
Roland Levillain1a653882016-03-18 18:05:57 +00002610 // Integer constants are used to encode Boolean values as well,
2611 // where 1 means true and 0 means false.
2612 bool IsTrue() const { return GetValue() == 1; }
2613 bool IsFalse() const { return GetValue() == 0; }
2614
Roland Levillain9867bc72015-08-05 10:21:34 +01002615 DECLARE_INSTRUCTION(IntConstant);
2616
2617 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002618 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2619 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2620 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2621 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002622
2623 const int32_t value_;
2624
2625 friend class HGraph;
2626 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2627 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2628 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2629};
2630
Vladimir Markofcb503c2016-05-18 12:48:17 +01002631class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002632 public:
2633 int64_t GetValue() const { return value_; }
2634
David Brazdil77a48ae2015-09-15 12:34:04 +00002635 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2636
Vladimir Marko372f10e2016-05-17 16:30:10 +01002637 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002638 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002639 return other->AsLongConstant()->value_ == value_;
2640 }
2641
2642 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2643
2644 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002645 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2646 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002647 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2648
2649 DECLARE_INSTRUCTION(LongConstant);
2650
2651 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002652 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2653 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002654
2655 const int64_t value_;
2656
2657 friend class HGraph;
2658 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2659};
Dave Allison20dfc792014-06-16 20:44:29 -07002660
Vladimir Markofcb503c2016-05-18 12:48:17 +01002661class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002662 public:
2663 float GetValue() const { return value_; }
2664
2665 uint64_t GetValueAsUint64() const OVERRIDE {
2666 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2667 }
2668
Vladimir Marko372f10e2016-05-17 16:30:10 +01002669 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002670 DCHECK(other->IsFloatConstant()) << other->DebugName();
2671 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2672 }
2673
2674 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2675
2676 bool IsMinusOne() const OVERRIDE {
2677 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2678 }
Roland Levillain1a653882016-03-18 18:05:57 +00002679 bool IsArithmeticZero() const OVERRIDE {
2680 return std::fpclassify(value_) == FP_ZERO;
2681 }
2682 bool IsArithmeticPositiveZero() const {
2683 return IsArithmeticZero() && !std::signbit(value_);
2684 }
2685 bool IsArithmeticNegativeZero() const {
2686 return IsArithmeticZero() && std::signbit(value_);
2687 }
2688 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002689 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002690 }
2691 bool IsOne() const OVERRIDE {
2692 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2693 }
2694 bool IsNaN() const {
2695 return std::isnan(value_);
2696 }
2697
2698 DECLARE_INSTRUCTION(FloatConstant);
2699
2700 private:
2701 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2702 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2703 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2704 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2705
2706 const float value_;
2707
2708 // Only the SsaBuilder and HGraph can create floating-point constants.
2709 friend class SsaBuilder;
2710 friend class HGraph;
2711 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2712};
2713
Vladimir Markofcb503c2016-05-18 12:48:17 +01002714class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002715 public:
2716 double GetValue() const { return value_; }
2717
2718 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2719
Vladimir Marko372f10e2016-05-17 16:30:10 +01002720 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002721 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2722 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2723 }
2724
2725 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2726
2727 bool IsMinusOne() const OVERRIDE {
2728 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2729 }
Roland Levillain1a653882016-03-18 18:05:57 +00002730 bool IsArithmeticZero() const OVERRIDE {
2731 return std::fpclassify(value_) == FP_ZERO;
2732 }
2733 bool IsArithmeticPositiveZero() const {
2734 return IsArithmeticZero() && !std::signbit(value_);
2735 }
2736 bool IsArithmeticNegativeZero() const {
2737 return IsArithmeticZero() && std::signbit(value_);
2738 }
2739 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002740 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002741 }
2742 bool IsOne() const OVERRIDE {
2743 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2744 }
2745 bool IsNaN() const {
2746 return std::isnan(value_);
2747 }
2748
2749 DECLARE_INSTRUCTION(DoubleConstant);
2750
2751 private:
2752 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2753 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2754 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2755 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2756
2757 const double value_;
2758
2759 // Only the SsaBuilder and HGraph can create floating-point constants.
2760 friend class SsaBuilder;
2761 friend class HGraph;
2762 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2763};
2764
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002765// Conditional branch. A block ending with an HIf instruction must have
2766// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002767class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002768 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002769 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2770 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002771 SetRawInputAt(0, input);
2772 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002773
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002774 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002775
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002776 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002777 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002778 }
2779
2780 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002781 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002782 }
2783
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002784 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002785
2786 private:
2787 DISALLOW_COPY_AND_ASSIGN(HIf);
2788};
2789
David Brazdilfc6a86a2015-06-26 10:33:45 +00002790
2791// Abstract instruction which marks the beginning and/or end of a try block and
2792// links it to the respective exception handlers. Behaves the same as a Goto in
2793// non-exceptional control flow.
2794// Normal-flow successor is stored at index zero, exception handlers under
2795// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002796class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002797 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002798 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002799 kEntry,
2800 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002801 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002802 };
2803
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002804 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002805 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2806 SetPackedField<BoundaryKindField>(kind);
2807 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002808
2809 bool IsControlFlow() const OVERRIDE { return true; }
2810
2811 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002812 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002813
David Brazdild26a4112015-11-10 11:07:31 +00002814 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2815 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2816 }
2817
David Brazdilfc6a86a2015-06-26 10:33:45 +00002818 // Returns whether `handler` is among its exception handlers (non-zero index
2819 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002820 bool HasExceptionHandler(const HBasicBlock& handler) const {
2821 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002822 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002823 }
2824
2825 // If not present already, adds `handler` to its block's list of exception
2826 // handlers.
2827 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002828 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002829 GetBlock()->AddSuccessor(handler);
2830 }
2831 }
2832
Vladimir Markoa1de9182016-02-25 11:37:38 +00002833 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2834 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002835
David Brazdilffee3d32015-07-06 11:48:53 +01002836 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2837
David Brazdilfc6a86a2015-06-26 10:33:45 +00002838 DECLARE_INSTRUCTION(TryBoundary);
2839
2840 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002841 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2842 static constexpr size_t kFieldBoundaryKindSize =
2843 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2844 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2845 kFieldBoundaryKind + kFieldBoundaryKindSize;
2846 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2847 "Too many packed fields.");
2848 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002849
2850 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2851};
2852
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002853// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002854class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002855 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002856 // We set CanTriggerGC to prevent any intermediate address to be live
2857 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002858 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002859 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002860 SetRawInputAt(0, cond);
2861 }
2862
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002863 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002864 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002865 return true;
2866 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002867 bool NeedsEnvironment() const OVERRIDE { return true; }
2868 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002869
2870 DECLARE_INSTRUCTION(Deoptimize);
2871
2872 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002873 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2874};
2875
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002876// Represents the ArtMethod that was passed as a first argument to
2877// the method. It is used by instructions that depend on it, like
2878// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002879class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002880 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002881 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2882 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002883
2884 DECLARE_INSTRUCTION(CurrentMethod);
2885
2886 private:
2887 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2888};
2889
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002890// Fetches an ArtMethod from the virtual table or the interface method table
2891// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002892class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002893 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002894 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002895 kVTable,
2896 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002897 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002898 };
2899 HClassTableGet(HInstruction* cls,
2900 Primitive::Type type,
2901 TableKind kind,
2902 size_t index,
2903 uint32_t dex_pc)
2904 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002905 index_(index) {
2906 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002907 SetRawInputAt(0, cls);
2908 }
2909
2910 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002911 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002912 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002913 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002914 }
2915
Vladimir Markoa1de9182016-02-25 11:37:38 +00002916 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002917 size_t GetIndex() const { return index_; }
2918
2919 DECLARE_INSTRUCTION(ClassTableGet);
2920
2921 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002922 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2923 static constexpr size_t kFieldTableKindSize =
2924 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2925 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2926 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2927 "Too many packed fields.");
2928 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2929
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002930 // The index of the ArtMethod in the table.
2931 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002932
2933 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2934};
2935
Mark Mendellfe57faa2015-09-18 09:26:15 -04002936// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2937// have one successor for each entry in the switch table, and the final successor
2938// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002939class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002940 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002941 HPackedSwitch(int32_t start_value,
2942 uint32_t num_entries,
2943 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002944 uint32_t dex_pc = kNoDexPc)
2945 : HTemplateInstruction(SideEffects::None(), dex_pc),
2946 start_value_(start_value),
2947 num_entries_(num_entries) {
2948 SetRawInputAt(0, input);
2949 }
2950
2951 bool IsControlFlow() const OVERRIDE { return true; }
2952
2953 int32_t GetStartValue() const { return start_value_; }
2954
Vladimir Marko211c2112015-09-24 16:52:33 +01002955 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002956
2957 HBasicBlock* GetDefaultBlock() const {
2958 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002959 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002960 }
2961 DECLARE_INSTRUCTION(PackedSwitch);
2962
2963 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002964 const int32_t start_value_;
2965 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002966
2967 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2968};
2969
Roland Levillain88cb1752014-10-20 16:36:47 +01002970class HUnaryOperation : public HExpression<1> {
2971 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002972 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2973 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002974 SetRawInputAt(0, input);
2975 }
2976
2977 HInstruction* GetInput() const { return InputAt(0); }
2978 Primitive::Type GetResultType() const { return GetType(); }
2979
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002980 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002981 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002982 return true;
2983 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002984
Roland Levillain31dd3d62016-02-16 12:21:02 +00002985 // Try to statically evaluate `this` and return a HConstant
2986 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002987 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002988 HConstant* TryStaticEvaluation() const;
2989
2990 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002991 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2992 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002993 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2994 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002995
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002996 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002997
2998 private:
2999 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3000};
3001
Dave Allison20dfc792014-06-16 20:44:29 -07003002class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003003 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003004 HBinaryOperation(Primitive::Type result_type,
3005 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003006 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003007 SideEffects side_effects = SideEffects::None(),
3008 uint32_t dex_pc = kNoDexPc)
3009 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003010 SetRawInputAt(0, left);
3011 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003012 }
3013
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003014 HInstruction* GetLeft() const { return InputAt(0); }
3015 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003016 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003017
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003018 virtual bool IsCommutative() const { return false; }
3019
3020 // Put constant on the right.
3021 // Returns whether order is changed.
3022 bool OrderInputsWithConstantOnTheRight() {
3023 HInstruction* left = InputAt(0);
3024 HInstruction* right = InputAt(1);
3025 if (left->IsConstant() && !right->IsConstant()) {
3026 ReplaceInput(right, 0);
3027 ReplaceInput(left, 1);
3028 return true;
3029 }
3030 return false;
3031 }
3032
3033 // Order inputs by instruction id, but favor constant on the right side.
3034 // This helps GVN for commutative ops.
3035 void OrderInputs() {
3036 DCHECK(IsCommutative());
3037 HInstruction* left = InputAt(0);
3038 HInstruction* right = InputAt(1);
3039 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3040 return;
3041 }
3042 if (OrderInputsWithConstantOnTheRight()) {
3043 return;
3044 }
3045 // Order according to instruction id.
3046 if (left->GetId() > right->GetId()) {
3047 ReplaceInput(right, 0);
3048 ReplaceInput(left, 1);
3049 }
3050 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003051
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003052 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003053 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003054 return true;
3055 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003056
Roland Levillain31dd3d62016-02-16 12:21:02 +00003057 // Try to statically evaluate `this` and return a HConstant
3058 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003059 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003060 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003061
3062 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003063 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3064 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003065 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3066 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003067 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003068 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3069 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003070 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3071 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003072 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3073 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003074 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003075 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3076 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003077
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003078 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003079 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003080 HConstant* GetConstantRight() const;
3081
3082 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003083 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003084 HInstruction* GetLeastConstantLeft() const;
3085
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003086 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003087
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003088 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003089 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3090};
3091
Mark Mendellc4701932015-04-10 13:18:51 -04003092// The comparison bias applies for floating point operations and indicates how NaN
3093// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003094enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003095 kNoBias, // bias is not applicable (i.e. for long operation)
3096 kGtBias, // return 1 for NaN comparisons
3097 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003098 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003099};
3100
Roland Levillain31dd3d62016-02-16 12:21:02 +00003101std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3102
Dave Allison20dfc792014-06-16 20:44:29 -07003103class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003104 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003105 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003106 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3107 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3108 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003109
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003110 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003111 // `instruction`, and disregard moves in between.
3112 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003113
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003114 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003115
3116 virtual IfCondition GetCondition() const = 0;
3117
Mark Mendellc4701932015-04-10 13:18:51 -04003118 virtual IfCondition GetOppositeCondition() const = 0;
3119
Vladimir Markoa1de9182016-02-25 11:37:38 +00003120 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003121 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3122
Vladimir Markoa1de9182016-02-25 11:37:38 +00003123 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3124 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003125
Vladimir Marko372f10e2016-05-17 16:30:10 +01003126 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003127 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003128 }
3129
Roland Levillain4fa13f62015-07-06 18:11:54 +01003130 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003131 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003132 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003133 if (if_cond == kCondNE) {
3134 return true;
3135 } else if (if_cond == kCondEQ) {
3136 return false;
3137 }
3138 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003139 }
3140
3141 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003142 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003143 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003144 if (if_cond == kCondEQ) {
3145 return true;
3146 } else if (if_cond == kCondNE) {
3147 return false;
3148 }
3149 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003150 }
3151
Roland Levillain31dd3d62016-02-16 12:21:02 +00003152 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003153 // Needed if we merge a HCompare into a HCondition.
3154 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3155 static constexpr size_t kFieldComparisonBiasSize =
3156 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3157 static constexpr size_t kNumberOfConditionPackedBits =
3158 kFieldComparisonBias + kFieldComparisonBiasSize;
3159 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3160 using ComparisonBiasField =
3161 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3162
Roland Levillain31dd3d62016-02-16 12:21:02 +00003163 template <typename T>
3164 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3165
3166 template <typename T>
3167 int32_t CompareFP(T x, T y) const {
3168 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3169 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3170 // Handle the bias.
3171 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3172 }
3173
3174 // Return an integer constant containing the result of a condition evaluated at compile time.
3175 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3176 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3177 }
3178
Dave Allison20dfc792014-06-16 20:44:29 -07003179 private:
3180 DISALLOW_COPY_AND_ASSIGN(HCondition);
3181};
3182
3183// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003184class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003185 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003186 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3187 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003188
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003189 bool IsCommutative() const OVERRIDE { return true; }
3190
Vladimir Marko9e23df52015-11-10 17:14:35 +00003191 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3192 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003193 return MakeConstantCondition(true, GetDexPc());
3194 }
3195 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3196 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3197 }
3198 // In the following Evaluate methods, a HCompare instruction has
3199 // been merged into this HEqual instruction; evaluate it as
3200 // `Compare(x, y) == 0`.
3201 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3202 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3203 GetDexPc());
3204 }
3205 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3206 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3207 }
3208 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3209 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003210 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003211
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003212 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003213
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003214 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003215 return kCondEQ;
3216 }
3217
Mark Mendellc4701932015-04-10 13:18:51 -04003218 IfCondition GetOppositeCondition() const OVERRIDE {
3219 return kCondNE;
3220 }
3221
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003222 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003223 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003224
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003225 DISALLOW_COPY_AND_ASSIGN(HEqual);
3226};
3227
Vladimir Markofcb503c2016-05-18 12:48:17 +01003228class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003229 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003230 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3231 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003232
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003233 bool IsCommutative() const OVERRIDE { return true; }
3234
Vladimir Marko9e23df52015-11-10 17:14:35 +00003235 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3236 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003237 return MakeConstantCondition(false, GetDexPc());
3238 }
3239 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3240 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3241 }
3242 // In the following Evaluate methods, a HCompare instruction has
3243 // been merged into this HNotEqual instruction; evaluate it as
3244 // `Compare(x, y) != 0`.
3245 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3246 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3247 }
3248 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3249 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3250 }
3251 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3252 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003253 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003254
Dave Allison20dfc792014-06-16 20:44:29 -07003255 DECLARE_INSTRUCTION(NotEqual);
3256
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003257 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003258 return kCondNE;
3259 }
3260
Mark Mendellc4701932015-04-10 13:18:51 -04003261 IfCondition GetOppositeCondition() const OVERRIDE {
3262 return kCondEQ;
3263 }
3264
Dave Allison20dfc792014-06-16 20:44:29 -07003265 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003266 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003267
Dave Allison20dfc792014-06-16 20:44:29 -07003268 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3269};
3270
Vladimir Markofcb503c2016-05-18 12:48:17 +01003271class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003272 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003273 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3274 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003275
Roland Levillain9867bc72015-08-05 10:21:34 +01003276 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003277 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003278 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003279 // In the following Evaluate methods, a HCompare instruction has
3280 // been merged into this HLessThan instruction; evaluate it as
3281 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003282 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003283 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3284 }
3285 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3286 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3287 }
3288 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3289 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003290 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003291
Dave Allison20dfc792014-06-16 20:44:29 -07003292 DECLARE_INSTRUCTION(LessThan);
3293
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003294 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003295 return kCondLT;
3296 }
3297
Mark Mendellc4701932015-04-10 13:18:51 -04003298 IfCondition GetOppositeCondition() const OVERRIDE {
3299 return kCondGE;
3300 }
3301
Dave Allison20dfc792014-06-16 20:44:29 -07003302 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003303 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003304
Dave Allison20dfc792014-06-16 20:44:29 -07003305 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3306};
3307
Vladimir Markofcb503c2016-05-18 12:48:17 +01003308class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003309 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003310 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3311 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003312
Roland Levillain9867bc72015-08-05 10:21:34 +01003313 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003314 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003315 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003316 // In the following Evaluate methods, a HCompare instruction has
3317 // been merged into this HLessThanOrEqual instruction; evaluate it as
3318 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003319 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003320 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3321 }
3322 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3323 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3324 }
3325 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3326 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003327 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003328
Dave Allison20dfc792014-06-16 20:44:29 -07003329 DECLARE_INSTRUCTION(LessThanOrEqual);
3330
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003331 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003332 return kCondLE;
3333 }
3334
Mark Mendellc4701932015-04-10 13:18:51 -04003335 IfCondition GetOppositeCondition() const OVERRIDE {
3336 return kCondGT;
3337 }
3338
Dave Allison20dfc792014-06-16 20:44:29 -07003339 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003340 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003341
Dave Allison20dfc792014-06-16 20:44:29 -07003342 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3343};
3344
Vladimir Markofcb503c2016-05-18 12:48:17 +01003345class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003346 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003347 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3348 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003349
Roland Levillain9867bc72015-08-05 10:21:34 +01003350 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003351 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003352 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003353 // In the following Evaluate methods, a HCompare instruction has
3354 // been merged into this HGreaterThan instruction; evaluate it as
3355 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003356 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003357 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3358 }
3359 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3360 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3361 }
3362 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3363 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003364 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003365
Dave Allison20dfc792014-06-16 20:44:29 -07003366 DECLARE_INSTRUCTION(GreaterThan);
3367
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003368 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003369 return kCondGT;
3370 }
3371
Mark Mendellc4701932015-04-10 13:18:51 -04003372 IfCondition GetOppositeCondition() const OVERRIDE {
3373 return kCondLE;
3374 }
3375
Dave Allison20dfc792014-06-16 20:44:29 -07003376 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003377 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003378
Dave Allison20dfc792014-06-16 20:44:29 -07003379 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3380};
3381
Vladimir Markofcb503c2016-05-18 12:48:17 +01003382class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003383 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003384 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3385 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003386
Roland Levillain9867bc72015-08-05 10:21:34 +01003387 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003388 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003389 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003390 // In the following Evaluate methods, a HCompare instruction has
3391 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3392 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003393 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003394 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3395 }
3396 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3397 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3398 }
3399 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3400 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003401 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003402
Dave Allison20dfc792014-06-16 20:44:29 -07003403 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3404
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003405 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003406 return kCondGE;
3407 }
3408
Mark Mendellc4701932015-04-10 13:18:51 -04003409 IfCondition GetOppositeCondition() const OVERRIDE {
3410 return kCondLT;
3411 }
3412
Dave Allison20dfc792014-06-16 20:44:29 -07003413 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003414 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003415
Dave Allison20dfc792014-06-16 20:44:29 -07003416 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3417};
3418
Vladimir Markofcb503c2016-05-18 12:48:17 +01003419class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003420 public:
3421 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3422 : HCondition(first, second, dex_pc) {}
3423
3424 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003425 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003426 }
3427 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003428 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3429 }
3430 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3431 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3432 LOG(FATAL) << DebugName() << " is not defined for float values";
3433 UNREACHABLE();
3434 }
3435 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3436 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3437 LOG(FATAL) << DebugName() << " is not defined for double values";
3438 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003439 }
3440
3441 DECLARE_INSTRUCTION(Below);
3442
3443 IfCondition GetCondition() const OVERRIDE {
3444 return kCondB;
3445 }
3446
3447 IfCondition GetOppositeCondition() const OVERRIDE {
3448 return kCondAE;
3449 }
3450
3451 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003452 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003453 return MakeUnsigned(x) < MakeUnsigned(y);
3454 }
Aart Bike9f37602015-10-09 11:15:55 -07003455
3456 DISALLOW_COPY_AND_ASSIGN(HBelow);
3457};
3458
Vladimir Markofcb503c2016-05-18 12:48:17 +01003459class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003460 public:
3461 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3462 : HCondition(first, second, dex_pc) {}
3463
3464 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003465 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003466 }
3467 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003468 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3469 }
3470 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3471 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3472 LOG(FATAL) << DebugName() << " is not defined for float values";
3473 UNREACHABLE();
3474 }
3475 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3476 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3477 LOG(FATAL) << DebugName() << " is not defined for double values";
3478 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003479 }
3480
3481 DECLARE_INSTRUCTION(BelowOrEqual);
3482
3483 IfCondition GetCondition() const OVERRIDE {
3484 return kCondBE;
3485 }
3486
3487 IfCondition GetOppositeCondition() const OVERRIDE {
3488 return kCondA;
3489 }
3490
3491 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003492 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003493 return MakeUnsigned(x) <= MakeUnsigned(y);
3494 }
Aart Bike9f37602015-10-09 11:15:55 -07003495
3496 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3497};
3498
Vladimir Markofcb503c2016-05-18 12:48:17 +01003499class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003500 public:
3501 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3502 : HCondition(first, second, dex_pc) {}
3503
3504 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003505 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003506 }
3507 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003508 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3509 }
3510 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3511 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3512 LOG(FATAL) << DebugName() << " is not defined for float values";
3513 UNREACHABLE();
3514 }
3515 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3516 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3517 LOG(FATAL) << DebugName() << " is not defined for double values";
3518 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003519 }
3520
3521 DECLARE_INSTRUCTION(Above);
3522
3523 IfCondition GetCondition() const OVERRIDE {
3524 return kCondA;
3525 }
3526
3527 IfCondition GetOppositeCondition() const OVERRIDE {
3528 return kCondBE;
3529 }
3530
3531 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003532 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003533 return MakeUnsigned(x) > MakeUnsigned(y);
3534 }
Aart Bike9f37602015-10-09 11:15:55 -07003535
3536 DISALLOW_COPY_AND_ASSIGN(HAbove);
3537};
3538
Vladimir Markofcb503c2016-05-18 12:48:17 +01003539class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003540 public:
3541 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3542 : HCondition(first, second, dex_pc) {}
3543
3544 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003545 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003546 }
3547 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003548 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3549 }
3550 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3551 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3552 LOG(FATAL) << DebugName() << " is not defined for float values";
3553 UNREACHABLE();
3554 }
3555 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3556 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3557 LOG(FATAL) << DebugName() << " is not defined for double values";
3558 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003559 }
3560
3561 DECLARE_INSTRUCTION(AboveOrEqual);
3562
3563 IfCondition GetCondition() const OVERRIDE {
3564 return kCondAE;
3565 }
3566
3567 IfCondition GetOppositeCondition() const OVERRIDE {
3568 return kCondB;
3569 }
3570
3571 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003572 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003573 return MakeUnsigned(x) >= MakeUnsigned(y);
3574 }
Aart Bike9f37602015-10-09 11:15:55 -07003575
3576 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3577};
Dave Allison20dfc792014-06-16 20:44:29 -07003578
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003579// Instruction to check how two inputs compare to each other.
3580// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003581class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003582 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003583 // Note that `comparison_type` is the type of comparison performed
3584 // between the comparison's inputs, not the type of the instantiated
3585 // HCompare instruction (which is always Primitive::kPrimInt).
3586 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003587 HInstruction* first,
3588 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003589 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003590 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003591 : HBinaryOperation(Primitive::kPrimInt,
3592 first,
3593 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003594 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003595 dex_pc) {
3596 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003597 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3598 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003599 }
3600
Roland Levillain9867bc72015-08-05 10:21:34 +01003601 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003602 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3603
3604 template <typename T>
3605 int32_t ComputeFP(T x, T y) const {
3606 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3607 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3608 // Handle the bias.
3609 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3610 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003611
Roland Levillain9867bc72015-08-05 10:21:34 +01003612 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003613 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3614 // reach this code path when processing a freshly built HIR
3615 // graph. However HCompare integer instructions can be synthesized
3616 // by the instruction simplifier to implement IntegerCompare and
3617 // IntegerSignum intrinsics, so we have to handle this case.
3618 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003619 }
3620 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003621 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3622 }
3623 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3624 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3625 }
3626 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3627 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003628 }
3629
Vladimir Marko372f10e2016-05-17 16:30:10 +01003630 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003631 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003632 }
3633
Vladimir Markoa1de9182016-02-25 11:37:38 +00003634 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003635
Roland Levillain31dd3d62016-02-16 12:21:02 +00003636 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003637 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003638 bool IsGtBias() const {
3639 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003640 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003641 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003642
Roland Levillain1693a1f2016-03-15 14:57:31 +00003643 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3644 // Comparisons do not require a runtime call in any back end.
3645 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003646 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003647
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003648 DECLARE_INSTRUCTION(Compare);
3649
Roland Levillain31dd3d62016-02-16 12:21:02 +00003650 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003651 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3652 static constexpr size_t kFieldComparisonBiasSize =
3653 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3654 static constexpr size_t kNumberOfComparePackedBits =
3655 kFieldComparisonBias + kFieldComparisonBiasSize;
3656 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3657 using ComparisonBiasField =
3658 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3659
Roland Levillain31dd3d62016-02-16 12:21:02 +00003660 // Return an integer constant containing the result of a comparison evaluated at compile time.
3661 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3662 DCHECK(value == -1 || value == 0 || value == 1) << value;
3663 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3664 }
3665
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003666 private:
3667 DISALLOW_COPY_AND_ASSIGN(HCompare);
3668};
3669
Vladimir Markofcb503c2016-05-18 12:48:17 +01003670class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003671 public:
3672 HNewInstance(HInstruction* cls,
3673 HCurrentMethod* current_method,
3674 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003675 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003676 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003677 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003678 bool finalizable,
3679 QuickEntrypointEnum entrypoint)
3680 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3681 type_index_(type_index),
3682 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003683 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003684 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003685 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003686 SetRawInputAt(0, cls);
3687 SetRawInputAt(1, current_method);
3688 }
3689
Andreas Gampea5b09a62016-11-17 15:21:22 -08003690 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003691 const DexFile& GetDexFile() const { return dex_file_; }
3692
3693 // Calls runtime so needs an environment.
3694 bool NeedsEnvironment() const OVERRIDE { return true; }
3695
Mingyao Yang062157f2016-03-02 10:15:36 -08003696 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3697 bool CanThrow() const OVERRIDE { return true; }
3698
3699 // Needs to call into runtime to make sure it's instantiable/accessible.
3700 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003701
Vladimir Markoa1de9182016-02-25 11:37:38 +00003702 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003703
3704 bool CanBeNull() const OVERRIDE { return false; }
3705
3706 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3707
3708 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3709 entrypoint_ = entrypoint;
3710 }
3711
3712 bool IsStringAlloc() const;
3713
3714 DECLARE_INSTRUCTION(NewInstance);
3715
3716 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003717 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3718 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003719 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3720 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3721 "Too many packed fields.");
3722
Andreas Gampea5b09a62016-11-17 15:21:22 -08003723 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003724 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003725 QuickEntrypointEnum entrypoint_;
3726
3727 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3728};
3729
Agi Csaki05f20562015-08-19 14:58:14 -07003730enum IntrinsicNeedsEnvironmentOrCache {
3731 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3732 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003733};
3734
Aart Bik5d75afe2015-12-14 11:57:01 -08003735enum IntrinsicSideEffects {
3736 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3737 kReadSideEffects, // Intrinsic may read heap memory.
3738 kWriteSideEffects, // Intrinsic may write heap memory.
3739 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3740};
3741
3742enum IntrinsicExceptions {
3743 kNoThrow, // Intrinsic does not throw any exceptions.
3744 kCanThrow // Intrinsic may throw exceptions.
3745};
3746
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003747class HInvoke : public HInstruction {
3748 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003749 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003750
Vladimir Marko372f10e2016-05-17 16:30:10 +01003751 using HInstruction::GetInputRecords; // Keep the const version visible.
3752 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3753 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3754 }
3755
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003756 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003757 SetRawInputAt(index, argument);
3758 }
3759
Roland Levillain3e3d7332015-04-28 11:00:54 +01003760 // Return the number of arguments. This number can be lower than
3761 // the number of inputs returned by InputCount(), as some invoke
3762 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3763 // inputs at the end of their list of inputs.
3764 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3765
Vladimir Markoa1de9182016-02-25 11:37:38 +00003766 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003767
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003768 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003769 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003770
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003771 InvokeType GetInvokeType() const {
3772 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003773 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003774
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003775 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003776 return intrinsic_;
3777 }
3778
Aart Bik5d75afe2015-12-14 11:57:01 -08003779 void SetIntrinsic(Intrinsics intrinsic,
3780 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3781 IntrinsicSideEffects side_effects,
3782 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003783
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003784 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003785 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003786 }
3787
Aart Bikff7d89c2016-11-07 08:49:28 -08003788 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
3789
Vladimir Markoa1de9182016-02-25 11:37:38 +00003790 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003791
3792 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3793
Vladimir Marko372f10e2016-05-17 16:30:10 +01003794 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003795 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3796 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003797
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003798 uint32_t* GetIntrinsicOptimizations() {
3799 return &intrinsic_optimizations_;
3800 }
3801
3802 const uint32_t* GetIntrinsicOptimizations() const {
3803 return &intrinsic_optimizations_;
3804 }
3805
3806 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3807
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003808 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3809
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003810 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003811
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003812 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003813 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3814 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003815 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3816 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003817 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003818 static constexpr size_t kFieldReturnTypeSize =
3819 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3820 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3821 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3822 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003823 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003824 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3825
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003826 HInvoke(ArenaAllocator* arena,
3827 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003828 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003829 Primitive::Type return_type,
3830 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003831 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003832 ArtMethod* resolved_method,
3833 InvokeType invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003834 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003835 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003836 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003837 resolved_method_(resolved_method),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003838 inputs_(number_of_arguments + number_of_other_inputs,
3839 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003840 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003841 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003842 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003843 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003844 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003845 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003846 }
3847
Roland Levillain3e3d7332015-04-28 11:00:54 +01003848 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003849 ArtMethod* const resolved_method_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003850 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003851 const uint32_t dex_method_index_;
3852 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003853
3854 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3855 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003856
3857 private:
3858 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3859};
3860
Vladimir Markofcb503c2016-05-18 12:48:17 +01003861class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003862 public:
3863 HInvokeUnresolved(ArenaAllocator* arena,
3864 uint32_t number_of_arguments,
3865 Primitive::Type return_type,
3866 uint32_t dex_pc,
3867 uint32_t dex_method_index,
3868 InvokeType invoke_type)
3869 : HInvoke(arena,
3870 number_of_arguments,
3871 0u /* number_of_other_inputs */,
3872 return_type,
3873 dex_pc,
3874 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003875 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003876 invoke_type) {
3877 }
3878
3879 DECLARE_INSTRUCTION(InvokeUnresolved);
3880
3881 private:
3882 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3883};
3884
Vladimir Markofcb503c2016-05-18 12:48:17 +01003885class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003886 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003887 // Requirements of this method call regarding the class
3888 // initialization (clinit) check of its declaring class.
3889 enum class ClinitCheckRequirement {
3890 kNone, // Class already initialized.
3891 kExplicit, // Static call having explicit clinit check as last input.
3892 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003893 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003894 };
3895
Vladimir Marko58155012015-08-19 12:49:41 +00003896 // Determines how to load the target ArtMethod*.
3897 enum class MethodLoadKind {
3898 // Use a String init ArtMethod* loaded from Thread entrypoints.
3899 kStringInit,
3900
3901 // Use the method's own ArtMethod* loaded by the register allocator.
3902 kRecursive,
3903
3904 // Use ArtMethod* at a known address, embed the direct address in the code.
3905 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3906 kDirectAddress,
3907
3908 // Use ArtMethod* at an address that will be known at link time, embed the direct
3909 // address in the code. If the image is relocatable, emit .patch_oat entry.
3910 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3911 // the image relocatable or not.
3912 kDirectAddressWithFixup,
3913
Vladimir Markoa1de9182016-02-25 11:37:38 +00003914 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003915 // Used when we need to use the dex cache, for example for invoke-static that
3916 // may cause class initialization (the entry may point to a resolution method),
3917 // and we know that we can access the dex cache arrays using a PC-relative load.
3918 kDexCachePcRelative,
3919
3920 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3921 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3922 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3923 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3924 kDexCacheViaMethod,
3925 };
3926
3927 // Determines the location of the code pointer.
3928 enum class CodePtrLocation {
3929 // Recursive call, use local PC-relative call instruction.
3930 kCallSelf,
3931
3932 // Use PC-relative call instruction patched at link time.
3933 // Used for calls within an oat file, boot->boot or app->app.
3934 kCallPCRelative,
3935
3936 // Call to a known target address, embed the direct address in code.
3937 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3938 kCallDirect,
3939
3940 // Call to a target address that will be known at link time, embed the direct
3941 // address in code. If the image is relocatable, emit .patch_oat entry.
3942 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3943 // the image relocatable or not.
3944 kCallDirectWithFixup,
3945
3946 // Use code pointer from the ArtMethod*.
3947 // Used when we don't know the target code. This is also the last-resort-kind used when
3948 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3949 kCallArtMethod,
3950 };
3951
3952 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003953 MethodLoadKind method_load_kind;
3954 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003955 // The method load data holds
3956 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3957 // Note that there are multiple string init methods, each having its own offset.
3958 // - the method address for kDirectAddress
3959 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003960 uint64_t method_load_data;
3961 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003962 };
3963
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003964 HInvokeStaticOrDirect(ArenaAllocator* arena,
3965 uint32_t number_of_arguments,
3966 Primitive::Type return_type,
3967 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003968 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003969 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00003970 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003971 InvokeType invoke_type,
3972 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003973 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003974 : HInvoke(arena,
3975 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003976 // There is potentially one extra argument for the HCurrentMethod node, and
3977 // potentially one other if the clinit check is explicit, and potentially
3978 // one other if the method is a string factory.
3979 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003980 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003981 return_type,
3982 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003983 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003984 resolved_method,
3985 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003986 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003987 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003988 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3989 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003990
Vladimir Markodc151b22015-10-15 18:02:30 +01003991 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003992 bool had_current_method_input = HasCurrentMethodInput();
3993 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3994
3995 // Using the current method is the default and once we find a better
3996 // method load kind, we should not go back to using the current method.
3997 DCHECK(had_current_method_input || !needs_current_method_input);
3998
3999 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004000 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4001 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004002 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004003 dispatch_info_ = dispatch_info;
4004 }
4005
Vladimir Markoc53c0792015-11-19 15:48:33 +00004006 void AddSpecialInput(HInstruction* input) {
4007 // We allow only one special input.
4008 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4009 DCHECK(InputCount() == GetSpecialInputIndex() ||
4010 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4011 InsertInputAt(GetSpecialInputIndex(), input);
4012 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004013
Vladimir Marko372f10e2016-05-17 16:30:10 +01004014 using HInstruction::GetInputRecords; // Keep the const version visible.
4015 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4016 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4017 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4018 DCHECK(!input_records.empty());
4019 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4020 HInstruction* last_input = input_records.back().GetInstruction();
4021 // Note: `last_input` may be null during arguments setup.
4022 if (last_input != nullptr) {
4023 // `last_input` is the last input of a static invoke marked as having
4024 // an explicit clinit check. It must either be:
4025 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4026 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4027 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4028 }
4029 }
4030 return input_records;
4031 }
4032
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004033 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004034 // We access the method via the dex cache so we can't do an implicit null check.
4035 // TODO: for intrinsics we can generate implicit null checks.
4036 return false;
4037 }
4038
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004039 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004040 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004041 }
4042
Vladimir Markoc53c0792015-11-19 15:48:33 +00004043 // Get the index of the special input, if any.
4044 //
David Brazdil6de19382016-01-08 17:37:10 +00004045 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4046 // method pointer; otherwise there may be one platform-specific special input,
4047 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004048 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004049 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004050
Vladimir Marko58155012015-08-19 12:49:41 +00004051 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4052 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4053 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004054 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004055 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004056 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004057 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004058 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4059 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004060 bool HasCurrentMethodInput() const {
4061 // This function can be called only after the invoke has been fully initialized by the builder.
4062 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004063 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004064 return true;
4065 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004066 DCHECK(InputCount() == GetSpecialInputIndex() ||
4067 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004068 return false;
4069 }
4070 }
Vladimir Marko58155012015-08-19 12:49:41 +00004071 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
Vladimir Marko58155012015-08-19 12:49:41 +00004072
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004073 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004074 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004075 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004076 }
4077
4078 uint64_t GetMethodAddress() const {
4079 DCHECK(HasMethodAddress());
4080 return dispatch_info_.method_load_data;
4081 }
4082
4083 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004084 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004085 return dispatch_info_.method_load_data;
4086 }
4087
4088 uint64_t GetDirectCodePtr() const {
4089 DCHECK(HasDirectCodePtr());
4090 return dispatch_info_.direct_code_ptr;
4091 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004092
Vladimir Markoa1de9182016-02-25 11:37:38 +00004093 ClinitCheckRequirement GetClinitCheckRequirement() const {
4094 return GetPackedField<ClinitCheckRequirementField>();
4095 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004096
Roland Levillain4c0eb422015-04-24 16:43:49 +01004097 // Is this instruction a call to a static method?
4098 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004099 return GetInvokeType() == kStatic;
4100 }
4101
4102 MethodReference GetTargetMethod() const {
4103 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004104 }
4105
Vladimir Markofbb184a2015-11-13 14:47:00 +00004106 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4107 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4108 // instruction; only relevant for static calls with explicit clinit check.
4109 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004110 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004111 size_t last_input_index = inputs_.size() - 1u;
4112 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004113 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004114 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004115 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004116 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004117 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004118 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004119 }
4120
4121 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004122 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004123 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004124 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004125 }
4126
4127 // Is this a call to a static method whose declaring class has an
4128 // implicit intialization check requirement?
4129 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004130 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004131 }
4132
Vladimir Markob554b5a2015-11-06 12:57:55 +00004133 // Does this method load kind need the current method as an input?
4134 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4135 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4136 }
4137
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004138 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004139
Roland Levillain4c0eb422015-04-24 16:43:49 +01004140 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004141 void InsertInputAt(size_t index, HInstruction* input);
4142 void RemoveInputAt(size_t index);
4143
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004144 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004145 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004146 static constexpr size_t kFieldClinitCheckRequirementSize =
4147 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4148 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4149 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4150 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4151 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004152 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4153 kFieldClinitCheckRequirement,
4154 kFieldClinitCheckRequirementSize>;
4155
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004156 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004157 MethodReference target_method_;
4158 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004159
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004160 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004161};
Vladimir Markof64242a2015-12-01 14:58:23 +00004162std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004163std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004164
Vladimir Markofcb503c2016-05-18 12:48:17 +01004165class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004166 public:
4167 HInvokeVirtual(ArenaAllocator* arena,
4168 uint32_t number_of_arguments,
4169 Primitive::Type return_type,
4170 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004171 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004172 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004173 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004174 : HInvoke(arena,
4175 number_of_arguments,
4176 0u,
4177 return_type,
4178 dex_pc,
4179 dex_method_index,
4180 resolved_method,
4181 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004182 vtable_index_(vtable_index) {}
4183
Calin Juravle641547a2015-04-21 22:08:51 +01004184 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004185 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004186 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004187 }
4188
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004189 uint32_t GetVTableIndex() const { return vtable_index_; }
4190
4191 DECLARE_INSTRUCTION(InvokeVirtual);
4192
4193 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004194 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004195 const uint32_t vtable_index_;
4196
4197 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4198};
4199
Vladimir Markofcb503c2016-05-18 12:48:17 +01004200class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004201 public:
4202 HInvokeInterface(ArenaAllocator* arena,
4203 uint32_t number_of_arguments,
4204 Primitive::Type return_type,
4205 uint32_t dex_pc,
4206 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004207 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004208 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004209 : HInvoke(arena,
4210 number_of_arguments,
4211 0u,
4212 return_type,
4213 dex_pc,
4214 dex_method_index,
4215 resolved_method,
4216 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004217 imt_index_(imt_index) {}
4218
Calin Juravle641547a2015-04-21 22:08:51 +01004219 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004220 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004221 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004222 }
4223
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004224 uint32_t GetImtIndex() const { return imt_index_; }
4225 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4226
4227 DECLARE_INSTRUCTION(InvokeInterface);
4228
4229 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004230 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004231 const uint32_t imt_index_;
4232
4233 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4234};
4235
Vladimir Markofcb503c2016-05-18 12:48:17 +01004236class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004237 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004238 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004239 : HUnaryOperation(result_type, input, dex_pc) {
4240 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4241 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004242
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004243 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004244
4245 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004246 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004247 }
4248 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004249 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004250 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004251 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4252 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4253 }
4254 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4255 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4256 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004257
Roland Levillain88cb1752014-10-20 16:36:47 +01004258 DECLARE_INSTRUCTION(Neg);
4259
4260 private:
4261 DISALLOW_COPY_AND_ASSIGN(HNeg);
4262};
4263
Vladimir Markofcb503c2016-05-18 12:48:17 +01004264class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004265 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004266 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004267 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004268 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004269 dex::TypeIndex type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004270 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004271 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004272 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004273 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004274 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004275 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004276 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004277 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004278 }
4279
Andreas Gampea5b09a62016-11-17 15:21:22 -08004280 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004281 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004282
4283 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004284 bool NeedsEnvironment() const OVERRIDE { return true; }
4285
Mingyao Yang0c365e62015-03-31 15:09:29 -07004286 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4287 bool CanThrow() const OVERRIDE { return true; }
4288
Calin Juravle10e244f2015-01-26 18:54:32 +00004289 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004290
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004291 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4292
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004293 DECLARE_INSTRUCTION(NewArray);
4294
4295 private:
Andreas Gampea5b09a62016-11-17 15:21:22 -08004296 const dex::TypeIndex type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004297 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004298 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004299
4300 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4301};
4302
Vladimir Markofcb503c2016-05-18 12:48:17 +01004303class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004304 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004305 HAdd(Primitive::Type result_type,
4306 HInstruction* left,
4307 HInstruction* right,
4308 uint32_t dex_pc = kNoDexPc)
4309 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004310
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004311 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004312
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004313 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004314
4315 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004316 return GetBlock()->GetGraph()->GetIntConstant(
4317 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004318 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004319 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004320 return GetBlock()->GetGraph()->GetLongConstant(
4321 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004322 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004323 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4324 return GetBlock()->GetGraph()->GetFloatConstant(
4325 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4326 }
4327 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4328 return GetBlock()->GetGraph()->GetDoubleConstant(
4329 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4330 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004331
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004332 DECLARE_INSTRUCTION(Add);
4333
4334 private:
4335 DISALLOW_COPY_AND_ASSIGN(HAdd);
4336};
4337
Vladimir Markofcb503c2016-05-18 12:48:17 +01004338class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004339 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004340 HSub(Primitive::Type result_type,
4341 HInstruction* left,
4342 HInstruction* right,
4343 uint32_t dex_pc = kNoDexPc)
4344 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004345
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004346 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004347
4348 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004349 return GetBlock()->GetGraph()->GetIntConstant(
4350 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004351 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004352 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004353 return GetBlock()->GetGraph()->GetLongConstant(
4354 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004355 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004356 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4357 return GetBlock()->GetGraph()->GetFloatConstant(
4358 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4359 }
4360 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4361 return GetBlock()->GetGraph()->GetDoubleConstant(
4362 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4363 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004364
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004365 DECLARE_INSTRUCTION(Sub);
4366
4367 private:
4368 DISALLOW_COPY_AND_ASSIGN(HSub);
4369};
4370
Vladimir Markofcb503c2016-05-18 12:48:17 +01004371class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004372 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004373 HMul(Primitive::Type result_type,
4374 HInstruction* left,
4375 HInstruction* right,
4376 uint32_t dex_pc = kNoDexPc)
4377 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004378
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004379 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004380
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004381 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004382
4383 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004384 return GetBlock()->GetGraph()->GetIntConstant(
4385 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004386 }
4387 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004388 return GetBlock()->GetGraph()->GetLongConstant(
4389 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004390 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004391 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4392 return GetBlock()->GetGraph()->GetFloatConstant(
4393 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4394 }
4395 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4396 return GetBlock()->GetGraph()->GetDoubleConstant(
4397 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4398 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004399
4400 DECLARE_INSTRUCTION(Mul);
4401
4402 private:
4403 DISALLOW_COPY_AND_ASSIGN(HMul);
4404};
4405
Vladimir Markofcb503c2016-05-18 12:48:17 +01004406class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004407 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004408 HDiv(Primitive::Type result_type,
4409 HInstruction* left,
4410 HInstruction* right,
4411 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004412 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004413
Roland Levillain9867bc72015-08-05 10:21:34 +01004414 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004415 T ComputeIntegral(T x, T y) const {
4416 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004417 // Our graph structure ensures we never have 0 for `y` during
4418 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004419 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004420 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004421 return (y == -1) ? -x : x / y;
4422 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004423
Roland Levillain31dd3d62016-02-16 12:21:02 +00004424 template <typename T>
4425 T ComputeFP(T x, T y) const {
4426 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4427 return x / y;
4428 }
4429
Roland Levillain9867bc72015-08-05 10:21:34 +01004430 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004431 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004432 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004433 }
4434 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004435 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004436 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4437 }
4438 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4439 return GetBlock()->GetGraph()->GetFloatConstant(
4440 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4441 }
4442 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4443 return GetBlock()->GetGraph()->GetDoubleConstant(
4444 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004445 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004446
4447 DECLARE_INSTRUCTION(Div);
4448
4449 private:
4450 DISALLOW_COPY_AND_ASSIGN(HDiv);
4451};
4452
Vladimir Markofcb503c2016-05-18 12:48:17 +01004453class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004454 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004455 HRem(Primitive::Type result_type,
4456 HInstruction* left,
4457 HInstruction* right,
4458 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004459 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004460
Roland Levillain9867bc72015-08-05 10:21:34 +01004461 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004462 T ComputeIntegral(T x, T y) const {
4463 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004464 // Our graph structure ensures we never have 0 for `y` during
4465 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004466 DCHECK_NE(y, 0);
4467 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4468 return (y == -1) ? 0 : x % y;
4469 }
4470
Roland Levillain31dd3d62016-02-16 12:21:02 +00004471 template <typename T>
4472 T ComputeFP(T x, T y) const {
4473 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4474 return std::fmod(x, y);
4475 }
4476
Roland Levillain9867bc72015-08-05 10:21:34 +01004477 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004478 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004479 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004480 }
4481 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004482 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004483 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004484 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004485 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4486 return GetBlock()->GetGraph()->GetFloatConstant(
4487 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4488 }
4489 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4490 return GetBlock()->GetGraph()->GetDoubleConstant(
4491 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4492 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004493
4494 DECLARE_INSTRUCTION(Rem);
4495
4496 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004497 DISALLOW_COPY_AND_ASSIGN(HRem);
4498};
4499
Vladimir Markofcb503c2016-05-18 12:48:17 +01004500class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004501 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004502 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4503 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004504 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004505 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004506 SetRawInputAt(0, value);
4507 }
4508
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004509 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4510
Calin Juravled0d48522014-11-04 16:40:20 +00004511 bool CanBeMoved() const OVERRIDE { return true; }
4512
Vladimir Marko372f10e2016-05-17 16:30:10 +01004513 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004514 return true;
4515 }
4516
4517 bool NeedsEnvironment() const OVERRIDE { return true; }
4518 bool CanThrow() const OVERRIDE { return true; }
4519
Calin Juravled0d48522014-11-04 16:40:20 +00004520 DECLARE_INSTRUCTION(DivZeroCheck);
4521
4522 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004523 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4524};
4525
Vladimir Markofcb503c2016-05-18 12:48:17 +01004526class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004527 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004528 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004529 HInstruction* value,
4530 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004531 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004532 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4533 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4534 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004535 }
4536
Roland Levillain5b5b9312016-03-22 14:57:31 +00004537 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004538 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004539 return value << (distance & max_shift_distance);
4540 }
4541
4542 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004543 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004544 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004545 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004546 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004547 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004548 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004549 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004550 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4551 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4552 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4553 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004554 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004555 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4556 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004557 LOG(FATAL) << DebugName() << " is not defined for float values";
4558 UNREACHABLE();
4559 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004560 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4561 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004562 LOG(FATAL) << DebugName() << " is not defined for double values";
4563 UNREACHABLE();
4564 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004565
4566 DECLARE_INSTRUCTION(Shl);
4567
4568 private:
4569 DISALLOW_COPY_AND_ASSIGN(HShl);
4570};
4571
Vladimir Markofcb503c2016-05-18 12:48:17 +01004572class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004573 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004574 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004575 HInstruction* value,
4576 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004577 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004578 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4579 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4580 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004581 }
4582
Roland Levillain5b5b9312016-03-22 14:57:31 +00004583 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004584 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004585 return value >> (distance & max_shift_distance);
4586 }
4587
4588 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004589 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004590 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004591 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004592 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004593 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004594 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004595 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004596 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4597 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4598 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4599 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004600 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004601 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4602 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004603 LOG(FATAL) << DebugName() << " is not defined for float values";
4604 UNREACHABLE();
4605 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004606 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4607 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004608 LOG(FATAL) << DebugName() << " is not defined for double values";
4609 UNREACHABLE();
4610 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004611
4612 DECLARE_INSTRUCTION(Shr);
4613
4614 private:
4615 DISALLOW_COPY_AND_ASSIGN(HShr);
4616};
4617
Vladimir Markofcb503c2016-05-18 12:48:17 +01004618class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004619 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004620 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004621 HInstruction* value,
4622 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004623 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004624 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4625 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4626 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004627 }
4628
Roland Levillain5b5b9312016-03-22 14:57:31 +00004629 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004630 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004631 typedef typename std::make_unsigned<T>::type V;
4632 V ux = static_cast<V>(value);
4633 return static_cast<T>(ux >> (distance & max_shift_distance));
4634 }
4635
4636 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004637 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004638 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004639 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004640 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004641 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004642 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004643 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004644 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4645 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4646 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4647 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004648 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004649 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4650 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004651 LOG(FATAL) << DebugName() << " is not defined for float values";
4652 UNREACHABLE();
4653 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004654 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4655 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004656 LOG(FATAL) << DebugName() << " is not defined for double values";
4657 UNREACHABLE();
4658 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004659
4660 DECLARE_INSTRUCTION(UShr);
4661
4662 private:
4663 DISALLOW_COPY_AND_ASSIGN(HUShr);
4664};
4665
Vladimir Markofcb503c2016-05-18 12:48:17 +01004666class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004667 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004668 HAnd(Primitive::Type result_type,
4669 HInstruction* left,
4670 HInstruction* right,
4671 uint32_t dex_pc = kNoDexPc)
4672 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004673
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004674 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004675
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004676 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004677
4678 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004679 return GetBlock()->GetGraph()->GetIntConstant(
4680 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004681 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004682 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004683 return GetBlock()->GetGraph()->GetLongConstant(
4684 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004685 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004686 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4687 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4688 LOG(FATAL) << DebugName() << " is not defined for float values";
4689 UNREACHABLE();
4690 }
4691 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4692 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4693 LOG(FATAL) << DebugName() << " is not defined for double values";
4694 UNREACHABLE();
4695 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004696
4697 DECLARE_INSTRUCTION(And);
4698
4699 private:
4700 DISALLOW_COPY_AND_ASSIGN(HAnd);
4701};
4702
Vladimir Markofcb503c2016-05-18 12:48:17 +01004703class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004704 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004705 HOr(Primitive::Type result_type,
4706 HInstruction* left,
4707 HInstruction* right,
4708 uint32_t dex_pc = kNoDexPc)
4709 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004710
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004711 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004712
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004713 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004714
4715 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004716 return GetBlock()->GetGraph()->GetIntConstant(
4717 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004718 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004719 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004720 return GetBlock()->GetGraph()->GetLongConstant(
4721 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004722 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004723 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4724 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4725 LOG(FATAL) << DebugName() << " is not defined for float values";
4726 UNREACHABLE();
4727 }
4728 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4729 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4730 LOG(FATAL) << DebugName() << " is not defined for double values";
4731 UNREACHABLE();
4732 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004733
4734 DECLARE_INSTRUCTION(Or);
4735
4736 private:
4737 DISALLOW_COPY_AND_ASSIGN(HOr);
4738};
4739
Vladimir Markofcb503c2016-05-18 12:48:17 +01004740class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004741 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004742 HXor(Primitive::Type result_type,
4743 HInstruction* left,
4744 HInstruction* right,
4745 uint32_t dex_pc = kNoDexPc)
4746 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004747
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004748 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004749
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004750 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004751
4752 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004753 return GetBlock()->GetGraph()->GetIntConstant(
4754 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004755 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004756 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004757 return GetBlock()->GetGraph()->GetLongConstant(
4758 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004759 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004760 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4761 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4762 LOG(FATAL) << DebugName() << " is not defined for float values";
4763 UNREACHABLE();
4764 }
4765 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4766 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4767 LOG(FATAL) << DebugName() << " is not defined for double values";
4768 UNREACHABLE();
4769 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004770
4771 DECLARE_INSTRUCTION(Xor);
4772
4773 private:
4774 DISALLOW_COPY_AND_ASSIGN(HXor);
4775};
4776
Vladimir Markofcb503c2016-05-18 12:48:17 +01004777class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004778 public:
4779 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004780 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004781 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4782 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004783 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004784
Roland Levillain5b5b9312016-03-22 14:57:31 +00004785 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004786 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004787 typedef typename std::make_unsigned<T>::type V;
4788 V ux = static_cast<V>(value);
4789 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004790 return static_cast<T>(ux);
4791 } else {
4792 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004793 return static_cast<T>(ux >> (distance & max_shift_value)) |
4794 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004795 }
4796 }
4797
Roland Levillain5b5b9312016-03-22 14:57:31 +00004798 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004799 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004800 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004801 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004802 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004803 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004804 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004805 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004806 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4807 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4808 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4809 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004810 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004811 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4812 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004813 LOG(FATAL) << DebugName() << " is not defined for float values";
4814 UNREACHABLE();
4815 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004816 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4817 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004818 LOG(FATAL) << DebugName() << " is not defined for double values";
4819 UNREACHABLE();
4820 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004821
4822 DECLARE_INSTRUCTION(Ror);
4823
4824 private:
4825 DISALLOW_COPY_AND_ASSIGN(HRor);
4826};
4827
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004828// The value of a parameter in this method. Its location depends on
4829// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004830class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004831 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004832 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004833 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004834 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004835 Primitive::Type parameter_type,
4836 bool is_this = false)
4837 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004838 dex_file_(dex_file),
4839 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004840 index_(index) {
4841 SetPackedFlag<kFlagIsThis>(is_this);
4842 SetPackedFlag<kFlagCanBeNull>(!is_this);
4843 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004844
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004845 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08004846 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004847 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004848 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004849
Vladimir Markoa1de9182016-02-25 11:37:38 +00004850 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4851 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004852
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004853 DECLARE_INSTRUCTION(ParameterValue);
4854
4855 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004856 // Whether or not the parameter value corresponds to 'this' argument.
4857 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4858 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4859 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4860 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4861 "Too many packed fields.");
4862
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004863 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004864 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004865 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004866 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004867 const uint8_t index_;
4868
4869 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4870};
4871
Vladimir Markofcb503c2016-05-18 12:48:17 +01004872class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004873 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004874 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4875 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004876
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004877 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004878 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004879 return true;
4880 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004881
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004882 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004883
4884 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004885 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004886 }
4887 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004888 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004889 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004890 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4891 LOG(FATAL) << DebugName() << " is not defined for float values";
4892 UNREACHABLE();
4893 }
4894 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4895 LOG(FATAL) << DebugName() << " is not defined for double values";
4896 UNREACHABLE();
4897 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004898
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004899 DECLARE_INSTRUCTION(Not);
4900
4901 private:
4902 DISALLOW_COPY_AND_ASSIGN(HNot);
4903};
4904
Vladimir Markofcb503c2016-05-18 12:48:17 +01004905class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004906 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004907 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4908 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004909
4910 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004911 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004912 return true;
4913 }
4914
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004915 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004916 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004917 return !x;
4918 }
4919
Roland Levillain9867bc72015-08-05 10:21:34 +01004920 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004921 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004922 }
4923 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4924 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004925 UNREACHABLE();
4926 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004927 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4928 LOG(FATAL) << DebugName() << " is not defined for float values";
4929 UNREACHABLE();
4930 }
4931 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4932 LOG(FATAL) << DebugName() << " is not defined for double values";
4933 UNREACHABLE();
4934 }
David Brazdil66d126e2015-04-03 16:02:44 +01004935
4936 DECLARE_INSTRUCTION(BooleanNot);
4937
4938 private:
4939 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4940};
4941
Vladimir Markofcb503c2016-05-18 12:48:17 +01004942class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004943 public:
4944 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004945 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004946 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004947 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004948 // Invariant: We should never generate a conversion to a Boolean value.
4949 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004950 }
4951
4952 HInstruction* GetInput() const { return InputAt(0); }
4953 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4954 Primitive::Type GetResultType() const { return GetType(); }
4955
4956 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004957 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4958 return true;
4959 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004960
Mark Mendelle82549b2015-05-06 10:55:34 -04004961 // Try to statically evaluate the conversion and return a HConstant
4962 // containing the result. If the input cannot be converted, return nullptr.
4963 HConstant* TryStaticEvaluation() const;
4964
Roland Levillaindff1f282014-11-05 14:15:05 +00004965 DECLARE_INSTRUCTION(TypeConversion);
4966
4967 private:
4968 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4969};
4970
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004971static constexpr uint32_t kNoRegNumber = -1;
4972
Vladimir Markofcb503c2016-05-18 12:48:17 +01004973class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004974 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004975 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4976 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004977 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004978 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004979 SetRawInputAt(0, value);
4980 }
4981
Calin Juravle10e244f2015-01-26 18:54:32 +00004982 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004983 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004984 return true;
4985 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004986
Calin Juravle10e244f2015-01-26 18:54:32 +00004987 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004988
Calin Juravle10e244f2015-01-26 18:54:32 +00004989 bool CanThrow() const OVERRIDE { return true; }
4990
4991 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004992
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004993
4994 DECLARE_INSTRUCTION(NullCheck);
4995
4996 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004997 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4998};
4999
5000class FieldInfo : public ValueObject {
5001 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005002 FieldInfo(MemberOffset field_offset,
5003 Primitive::Type field_type,
5004 bool is_volatile,
5005 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005006 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005007 const DexFile& dex_file,
5008 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005009 : field_offset_(field_offset),
5010 field_type_(field_type),
5011 is_volatile_(is_volatile),
5012 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005013 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005014 dex_file_(dex_file),
5015 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005016
5017 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005018 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005019 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005020 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005021 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005022 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005023 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005024
5025 private:
5026 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005027 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005028 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005029 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005030 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005031 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005032 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005033};
5034
Vladimir Markofcb503c2016-05-18 12:48:17 +01005035class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005036 public:
5037 HInstanceFieldGet(HInstruction* value,
5038 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005039 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005040 bool is_volatile,
5041 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005042 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005043 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005044 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005045 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005046 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005047 field_info_(field_offset,
5048 field_type,
5049 is_volatile,
5050 field_idx,
5051 declaring_class_def_index,
5052 dex_file,
5053 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005054 SetRawInputAt(0, value);
5055 }
5056
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005057 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005058
Vladimir Marko372f10e2016-05-17 16:30:10 +01005059 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5060 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005061 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005062 }
5063
Calin Juravle641547a2015-04-21 22:08:51 +01005064 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005065 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005066 }
5067
5068 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005069 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5070 }
5071
Calin Juravle52c48962014-12-16 17:02:57 +00005072 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005073 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005074 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005075 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005076
5077 DECLARE_INSTRUCTION(InstanceFieldGet);
5078
5079 private:
5080 const FieldInfo field_info_;
5081
5082 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5083};
5084
Vladimir Markofcb503c2016-05-18 12:48:17 +01005085class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005086 public:
5087 HInstanceFieldSet(HInstruction* object,
5088 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005089 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005090 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005091 bool is_volatile,
5092 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005093 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005094 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005095 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005096 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005097 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005098 field_info_(field_offset,
5099 field_type,
5100 is_volatile,
5101 field_idx,
5102 declaring_class_def_index,
5103 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005104 dex_cache) {
5105 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005106 SetRawInputAt(0, object);
5107 SetRawInputAt(1, value);
5108 }
5109
Calin Juravle641547a2015-04-21 22:08:51 +01005110 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005111 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005112 }
5113
Calin Juravle52c48962014-12-16 17:02:57 +00005114 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005115 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005116 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005117 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005118 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005119 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5120 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005121
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005122 DECLARE_INSTRUCTION(InstanceFieldSet);
5123
5124 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005125 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5126 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5127 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5128 "Too many packed fields.");
5129
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005130 const FieldInfo field_info_;
5131
5132 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5133};
5134
Vladimir Markofcb503c2016-05-18 12:48:17 +01005135class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005136 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005137 HArrayGet(HInstruction* array,
5138 HInstruction* index,
5139 Primitive::Type type,
5140 uint32_t dex_pc,
5141 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005142 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005143 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005144 SetRawInputAt(0, array);
5145 SetRawInputAt(1, index);
5146 }
5147
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005148 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005149 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005150 return true;
5151 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005152 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005153 // TODO: We can be smarter here.
5154 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5155 // which generates the implicit null check. There are cases when these can be removed
5156 // to produce better code. If we ever add optimizations to do so we should allow an
5157 // implicit check here (as long as the address falls in the first page).
5158 return false;
5159 }
5160
David Brazdil4833f5a2015-12-16 10:37:39 +00005161 bool IsEquivalentOf(HArrayGet* other) const {
5162 bool result = (GetDexPc() == other->GetDexPc());
5163 if (kIsDebugBuild && result) {
5164 DCHECK_EQ(GetBlock(), other->GetBlock());
5165 DCHECK_EQ(GetArray(), other->GetArray());
5166 DCHECK_EQ(GetIndex(), other->GetIndex());
5167 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005168 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005169 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005170 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5171 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005172 }
5173 }
5174 return result;
5175 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005176
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005177 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5178
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005179 HInstruction* GetArray() const { return InputAt(0); }
5180 HInstruction* GetIndex() const { return InputAt(1); }
5181
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005182 DECLARE_INSTRUCTION(ArrayGet);
5183
5184 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005185 // We treat a String as an array, creating the HArrayGet from String.charAt()
5186 // intrinsic in the instruction simplifier. We can always determine whether
5187 // a particular HArrayGet is actually a String.charAt() by looking at the type
5188 // of the input but that requires holding the mutator lock, so we prefer to use
5189 // a flag, so that code generators don't need to do the locking.
5190 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5191 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5192 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5193 "Too many packed fields.");
5194
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005195 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5196};
5197
Vladimir Markofcb503c2016-05-18 12:48:17 +01005198class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005199 public:
5200 HArraySet(HInstruction* array,
5201 HInstruction* index,
5202 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005203 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005204 uint32_t dex_pc)
5205 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005206 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5207 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5208 SetPackedFlag<kFlagValueCanBeNull>(true);
5209 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005210 SetRawInputAt(0, array);
5211 SetRawInputAt(1, index);
5212 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005213 // Make a best guess now, may be refined during SSA building.
5214 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005215 }
5216
Calin Juravle77520bc2015-01-12 18:45:46 +00005217 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005218 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005219 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005220 }
5221
Mingyao Yang81014cb2015-06-02 03:16:27 -07005222 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005223 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005224
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005225 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005226 // TODO: Same as for ArrayGet.
5227 return false;
5228 }
5229
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005230 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005231 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005232 }
5233
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005234 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005235 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005236 }
5237
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005238 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005239 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005240 }
5241
Vladimir Markoa1de9182016-02-25 11:37:38 +00005242 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5243 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5244 bool StaticTypeOfArrayIsObjectArray() const {
5245 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5246 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005247
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005248 HInstruction* GetArray() const { return InputAt(0); }
5249 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005250 HInstruction* GetValue() const { return InputAt(2); }
5251
5252 Primitive::Type GetComponentType() const {
5253 // The Dex format does not type floating point index operations. Since the
5254 // `expected_component_type_` is set during building and can therefore not
5255 // be correct, we also check what is the value type. If it is a floating
5256 // point type, we must use that type.
5257 Primitive::Type value_type = GetValue()->GetType();
5258 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5259 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005260 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005261 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005262
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005263 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005264 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005265 }
5266
Aart Bik18b36ab2016-04-13 16:41:35 -07005267 void ComputeSideEffects() {
5268 Primitive::Type type = GetComponentType();
5269 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5270 SideEffectsForArchRuntimeCalls(type)));
5271 }
5272
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005273 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5274 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5275 }
5276
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005277 DECLARE_INSTRUCTION(ArraySet);
5278
5279 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005280 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5281 static constexpr size_t kFieldExpectedComponentTypeSize =
5282 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5283 static constexpr size_t kFlagNeedsTypeCheck =
5284 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5285 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005286 // Cached information for the reference_type_info_ so that codegen
5287 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005288 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5289 static constexpr size_t kNumberOfArraySetPackedBits =
5290 kFlagStaticTypeOfArrayIsObjectArray + 1;
5291 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5292 using ExpectedComponentTypeField =
5293 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005294
5295 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5296};
5297
Vladimir Markofcb503c2016-05-18 12:48:17 +01005298class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005299 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005300 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005301 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005302 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005303 // Note that arrays do not change length, so the instruction does not
5304 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005305 SetRawInputAt(0, array);
5306 }
5307
Calin Juravle77520bc2015-01-12 18:45:46 +00005308 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005309 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005310 return true;
5311 }
Calin Juravle641547a2015-04-21 22:08:51 +01005312 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5313 return obj == InputAt(0);
5314 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005315
Vladimir Markodce016e2016-04-28 13:10:02 +01005316 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5317
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005318 DECLARE_INSTRUCTION(ArrayLength);
5319
5320 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005321 // We treat a String as an array, creating the HArrayLength from String.length()
5322 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5323 // determine whether a particular HArrayLength is actually a String.length() by
5324 // looking at the type of the input but that requires holding the mutator lock, so
5325 // we prefer to use a flag, so that code generators don't need to do the locking.
5326 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5327 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5328 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5329 "Too many packed fields.");
5330
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005331 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5332};
5333
Vladimir Markofcb503c2016-05-18 12:48:17 +01005334class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005335 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005336 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5337 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005338 HBoundsCheck(HInstruction* index,
5339 HInstruction* length,
5340 uint32_t dex_pc,
5341 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5342 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5343 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005344 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005345 SetRawInputAt(0, index);
5346 SetRawInputAt(1, length);
5347 }
5348
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005349 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005350 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005351 return true;
5352 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005353
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005354 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005355
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005356 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005357
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005358 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5359 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5360
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005361 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005362
5363 DECLARE_INSTRUCTION(BoundsCheck);
5364
5365 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005366 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5367 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5368 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5369 // so we need to create an HEnvironment which will be translated to an InlineInfo
5370 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5371 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5372 // from the invoke as we don't want to look again at the dex bytecode.
5373 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5374
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005375 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5376};
5377
Vladimir Markofcb503c2016-05-18 12:48:17 +01005378class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005379 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005380 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005381 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005382
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005383 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005384 return true;
5385 }
5386
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005387 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5388 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005389
5390 DECLARE_INSTRUCTION(SuspendCheck);
5391
5392 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005393 // Only used for code generation, in order to share the same slow path between back edges
5394 // of a same loop.
5395 SlowPathCode* slow_path_;
5396
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005397 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5398};
5399
David Srbecky0cf44932015-12-09 14:09:59 +00005400// Pseudo-instruction which provides the native debugger with mapping information.
5401// It ensures that we can generate line number and local variables at this point.
5402class HNativeDebugInfo : public HTemplateInstruction<0> {
5403 public:
5404 explicit HNativeDebugInfo(uint32_t dex_pc)
5405 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5406
5407 bool NeedsEnvironment() const OVERRIDE {
5408 return true;
5409 }
5410
5411 DECLARE_INSTRUCTION(NativeDebugInfo);
5412
5413 private:
5414 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5415};
5416
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005417/**
5418 * Instruction to load a Class object.
5419 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005420class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005421 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005422 // Determines how to load the Class.
5423 enum class LoadKind {
5424 // Use the Class* from the method's own ArtMethod*.
5425 kReferrersClass,
5426
5427 // Use boot image Class* address that will be known at link time.
5428 // Used for boot image classes referenced by boot image code in non-PIC mode.
5429 kBootImageLinkTimeAddress,
5430
5431 // Use PC-relative boot image Class* address that will be known at link time.
5432 // Used for boot image classes referenced by boot image code in PIC mode.
5433 kBootImageLinkTimePcRelative,
5434
5435 // Use a known boot image Class* address, embedded in the code by the codegen.
5436 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5437 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5438 // GetIncludePatchInformation().
5439 kBootImageAddress,
5440
5441 // Load from the resolved types array at an absolute address.
5442 // Used for classes outside the boot image referenced by JIT-compiled code.
5443 kDexCacheAddress,
5444
5445 // Load from resolved types array in the dex cache using a PC-relative load.
5446 // Used for classes outside boot image when we know that we can access
5447 // the dex cache arrays using a PC-relative load.
5448 kDexCachePcRelative,
5449
5450 // Load from resolved types array accessed through the class loaded from
5451 // the compiled method's own ArtMethod*. This is the default access type when
5452 // all other types are unavailable.
5453 kDexCacheViaMethod,
5454
5455 kLast = kDexCacheViaMethod
5456 };
5457
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005458 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005459 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005460 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005461 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005462 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005463 bool needs_access_check,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005464 bool is_in_dex_cache,
5465 bool is_in_boot_image)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005466 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5467 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005468 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005469 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005470 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005471 // Referrers class should not need access check. We never inline unverified
5472 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005473 DCHECK(!is_referrers_class || !needs_access_check);
5474
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005475 SetPackedField<LoadKindField>(
5476 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005477 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5478 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005479 SetPackedFlag<kFlagIsInBootImage>(is_in_boot_image);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005480 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005481 }
5482
5483 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5484 DCHECK(HasAddress(load_kind));
5485 load_data_.address = address;
5486 SetLoadKindInternal(load_kind);
5487 }
5488
5489 void SetLoadKindWithTypeReference(LoadKind load_kind,
5490 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005491 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005492 DCHECK(HasTypeReference(load_kind));
5493 DCHECK(IsSameDexFile(dex_file_, dex_file));
5494 DCHECK_EQ(type_index_, type_index);
5495 SetLoadKindInternal(load_kind);
5496 }
5497
5498 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5499 const DexFile& dex_file,
5500 uint32_t element_index) {
5501 DCHECK(HasDexCacheReference(load_kind));
5502 DCHECK(IsSameDexFile(dex_file_, dex_file));
5503 load_data_.dex_cache_element_index = element_index;
5504 SetLoadKindInternal(load_kind);
5505 }
5506
5507 LoadKind GetLoadKind() const {
5508 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005509 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005510
5511 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005512
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005513 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005514
Andreas Gampea5b09a62016-11-17 15:21:22 -08005515 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005516
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005517 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005518
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005519 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005520 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005521 }
5522
Calin Juravle0ba218d2015-05-19 18:46:01 +01005523 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005524 // The entrypoint the code generator is going to call does not do
5525 // clinit of the class.
5526 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005527 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005528 }
5529
5530 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005531 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005532 (!IsReferrersClass() && !IsInDexCache()) ||
5533 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005534 }
5535
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005536
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005537 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005538 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005539 }
5540
Calin Juravleacf735c2015-02-12 15:25:22 +00005541 ReferenceTypeInfo GetLoadedClassRTI() {
5542 return loaded_class_rti_;
5543 }
5544
5545 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5546 // Make sure we only set exact types (the loaded class should never be merged).
5547 DCHECK(rti.IsExact());
5548 loaded_class_rti_ = rti;
5549 }
5550
Andreas Gampea5b09a62016-11-17 15:21:22 -08005551 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005552 const DexFile& GetDexFile() const { return dex_file_; }
5553
5554 uint32_t GetDexCacheElementOffset() const;
5555
5556 uint64_t GetAddress() const {
5557 DCHECK(HasAddress(GetLoadKind()));
5558 return load_data_.address;
5559 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005560
Vladimir Markoa1de9182016-02-25 11:37:38 +00005561 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005562
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005563 static SideEffects SideEffectsForArchRuntimeCalls() {
5564 return SideEffects::CanTriggerGC();
5565 }
5566
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005567 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005568 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5569 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005570 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005571 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005572
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005573 void MarkInDexCache() {
5574 SetPackedFlag<kFlagIsInDexCache>(true);
5575 DCHECK(!NeedsEnvironment());
5576 RemoveEnvironment();
5577 SetSideEffects(SideEffects::None());
5578 }
5579
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005580 void MarkInBootImage() {
5581 SetPackedFlag<kFlagIsInBootImage>(true);
5582 }
5583
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005584 void AddSpecialInput(HInstruction* special_input);
5585
5586 using HInstruction::GetInputRecords; // Keep the const version visible.
5587 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5588 return ArrayRef<HUserRecord<HInstruction*>>(
5589 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5590 }
5591
5592 Primitive::Type GetType() const OVERRIDE {
5593 return Primitive::kPrimNot;
5594 }
5595
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005596 DECLARE_INSTRUCTION(LoadClass);
5597
5598 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005599 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005600 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005601 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005602 // Whether this instruction must generate the initialization check.
5603 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005604 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005605 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5606 static constexpr size_t kFieldLoadKindSize =
5607 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5608 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005609 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005610 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5611
5612 static bool HasTypeReference(LoadKind load_kind) {
5613 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5614 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5615 load_kind == LoadKind::kDexCacheViaMethod ||
5616 load_kind == LoadKind::kReferrersClass;
5617 }
5618
5619 static bool HasAddress(LoadKind load_kind) {
5620 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5621 }
5622
5623 static bool HasDexCacheReference(LoadKind load_kind) {
5624 return load_kind == LoadKind::kDexCachePcRelative;
5625 }
5626
5627 void SetLoadKindInternal(LoadKind load_kind);
5628
5629 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5630 // For other load kinds it's empty or possibly some architecture-specific instruction
5631 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5632 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005633
Andreas Gampea5b09a62016-11-17 15:21:22 -08005634 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005635 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005636
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005637 union {
5638 uint32_t dex_cache_element_index; // Only for dex cache reference.
5639 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5640 } load_data_;
5641
Calin Juravleacf735c2015-02-12 15:25:22 +00005642 ReferenceTypeInfo loaded_class_rti_;
5643
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005644 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5645};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005646std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5647
5648// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5649inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5650 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5651 return load_data_.dex_cache_element_index;
5652}
5653
5654// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5655inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005656 // The special input is used for PC-relative loads on some architectures,
5657 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005658 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005659 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5660 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5661 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005662 DCHECK(special_input_.GetInstruction() == nullptr);
5663 special_input_ = HUserRecord<HInstruction*>(special_input);
5664 special_input->AddUseAt(this, 0);
5665}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005666
Vladimir Marko372f10e2016-05-17 16:30:10 +01005667class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005668 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005669 // Determines how to load the String.
5670 enum class LoadKind {
5671 // Use boot image String* address that will be known at link time.
5672 // Used for boot image strings referenced by boot image code in non-PIC mode.
5673 kBootImageLinkTimeAddress,
5674
5675 // Use PC-relative boot image String* address that will be known at link time.
5676 // Used for boot image strings referenced by boot image code in PIC mode.
5677 kBootImageLinkTimePcRelative,
5678
5679 // Use a known boot image String* address, embedded in the code by the codegen.
5680 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5681 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5682 // GetIncludePatchInformation().
5683 kBootImageAddress,
5684
Vladimir Markoaad75c62016-10-03 08:46:48 +00005685 // Load from an entry in the .bss section using a PC-relative load.
5686 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5687 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005688
5689 // Load from resolved strings array accessed through the class loaded from
5690 // the compiled method's own ArtMethod*. This is the default access type when
5691 // all other types are unavailable.
5692 kDexCacheViaMethod,
5693
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005694 // Load from the root table associated with the JIT compiled method.
5695 kJitTableAddress,
5696
5697 kLast = kJitTableAddress,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005698 };
5699
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005700 HLoadString(HCurrentMethod* current_method,
5701 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005702 const DexFile& dex_file,
5703 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005704 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5705 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005706 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005707 SetPackedFlag<kFlagIsInDexCache>(false);
5708 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005709 load_data_.dex_file_ = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005710 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005711
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005712 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5713 DCHECK(HasAddress(load_kind));
5714 load_data_.address = address;
5715 SetLoadKindInternal(load_kind);
5716 }
5717
5718 void SetLoadKindWithStringReference(LoadKind load_kind,
5719 const DexFile& dex_file,
5720 uint32_t string_index) {
5721 DCHECK(HasStringReference(load_kind));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005722 load_data_.dex_file_ = &dex_file;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005723 string_index_ = string_index;
5724 SetLoadKindInternal(load_kind);
5725 }
5726
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005727 LoadKind GetLoadKind() const {
5728 return GetPackedField<LoadKindField>();
5729 }
5730
5731 const DexFile& GetDexFile() const;
5732
5733 uint32_t GetStringIndex() const {
5734 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5735 return string_index_;
5736 }
5737
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005738 uint64_t GetAddress() const {
5739 DCHECK(HasAddress(GetLoadKind()));
5740 return load_data_.address;
5741 }
5742
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005743 bool CanBeMoved() const OVERRIDE { return true; }
5744
Vladimir Marko372f10e2016-05-17 16:30:10 +01005745 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005746
5747 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5748
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005749 // Will call the runtime if we need to load the string through
5750 // the dex cache and the string is not guaranteed to be there yet.
5751 bool NeedsEnvironment() const OVERRIDE {
5752 LoadKind load_kind = GetLoadKind();
5753 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5754 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005755 load_kind == LoadKind::kBootImageAddress ||
5756 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005757 return false;
5758 }
5759 return !IsInDexCache();
5760 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005761
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005762 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5763 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5764 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005765
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005766 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005767 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005768
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005769 static SideEffects SideEffectsForArchRuntimeCalls() {
5770 return SideEffects::CanTriggerGC();
5771 }
5772
Vladimir Markoa1de9182016-02-25 11:37:38 +00005773 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5774
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005775 void MarkInDexCache() {
5776 SetPackedFlag<kFlagIsInDexCache>(true);
5777 DCHECK(!NeedsEnvironment());
5778 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005779 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005780 }
5781
Vladimir Marko372f10e2016-05-17 16:30:10 +01005782 void AddSpecialInput(HInstruction* special_input);
5783
5784 using HInstruction::GetInputRecords; // Keep the const version visible.
5785 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5786 return ArrayRef<HUserRecord<HInstruction*>>(
5787 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005788 }
5789
Vladimir Marko372f10e2016-05-17 16:30:10 +01005790 Primitive::Type GetType() const OVERRIDE {
5791 return Primitive::kPrimNot;
5792 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005793
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005794 DECLARE_INSTRUCTION(LoadString);
5795
5796 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005797 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005798 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5799 static constexpr size_t kFieldLoadKindSize =
5800 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5801 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005802 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005803 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005804
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005805 static bool HasStringReference(LoadKind load_kind) {
5806 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5807 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005808 load_kind == LoadKind::kBssEntry ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005809 load_kind == LoadKind::kDexCacheViaMethod ||
5810 load_kind == LoadKind::kJitTableAddress;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005811 }
5812
5813 static bool HasAddress(LoadKind load_kind) {
Vladimir Marko54d6a202016-11-09 12:46:38 +00005814 return load_kind == LoadKind::kBootImageAddress;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005815 }
5816
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005817 void SetLoadKindInternal(LoadKind load_kind);
5818
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005819 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5820 // For other load kinds it's empty or possibly some architecture-specific instruction
5821 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005822 HUserRecord<HInstruction*> special_input_;
5823
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005824 // String index serves also as the hash code and it's also needed for slow-paths,
5825 // so it must not be overwritten with other load data.
5826 uint32_t string_index_;
5827
5828 union {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005829 const DexFile* dex_file_; // For string reference.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005830 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5831 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005832
5833 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5834};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005835std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5836
5837// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5838inline const DexFile& HLoadString::GetDexFile() const {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005839 DCHECK(HasStringReference(GetLoadKind())) << GetLoadKind();
5840 return *load_data_.dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005841}
5842
5843// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5844inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005845 // The special input is used for PC-relative loads on some architectures,
5846 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005847 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005848 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005849 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5850 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005851 // HLoadString::GetInputRecords() returns an empty array at this point,
5852 // so use the GetInputRecords() from the base class to set the input record.
5853 DCHECK(special_input_.GetInstruction() == nullptr);
5854 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005855 special_input->AddUseAt(this, 0);
5856}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005857
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005858/**
5859 * Performs an initialization check on its Class object input.
5860 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005861class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005862 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005863 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005864 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005865 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005866 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5867 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005868 SetRawInputAt(0, constant);
5869 }
5870
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005871 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005872 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005873 return true;
5874 }
5875
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005876 bool NeedsEnvironment() const OVERRIDE {
5877 // May call runtime to initialize the class.
5878 return true;
5879 }
5880
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005881 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005882
5883 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5884
5885 DECLARE_INSTRUCTION(ClinitCheck);
5886
5887 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005888 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5889};
5890
Vladimir Markofcb503c2016-05-18 12:48:17 +01005891class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005892 public:
5893 HStaticFieldGet(HInstruction* cls,
5894 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005895 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005896 bool is_volatile,
5897 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005898 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005899 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005900 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005901 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005902 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005903 field_info_(field_offset,
5904 field_type,
5905 is_volatile,
5906 field_idx,
5907 declaring_class_def_index,
5908 dex_file,
5909 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005910 SetRawInputAt(0, cls);
5911 }
5912
Calin Juravle52c48962014-12-16 17:02:57 +00005913
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005914 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005915
Vladimir Marko372f10e2016-05-17 16:30:10 +01005916 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5917 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005918 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005919 }
5920
5921 size_t ComputeHashCode() const OVERRIDE {
5922 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5923 }
5924
Calin Juravle52c48962014-12-16 17:02:57 +00005925 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005926 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5927 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005928 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005929
5930 DECLARE_INSTRUCTION(StaticFieldGet);
5931
5932 private:
5933 const FieldInfo field_info_;
5934
5935 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5936};
5937
Vladimir Markofcb503c2016-05-18 12:48:17 +01005938class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005939 public:
5940 HStaticFieldSet(HInstruction* cls,
5941 HInstruction* value,
5942 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005943 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005944 bool is_volatile,
5945 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005946 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005947 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005948 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005949 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005950 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005951 field_info_(field_offset,
5952 field_type,
5953 is_volatile,
5954 field_idx,
5955 declaring_class_def_index,
5956 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005957 dex_cache) {
5958 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005959 SetRawInputAt(0, cls);
5960 SetRawInputAt(1, value);
5961 }
5962
Calin Juravle52c48962014-12-16 17:02:57 +00005963 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005964 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5965 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005966 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005967
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005968 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005969 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5970 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005971
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005972 DECLARE_INSTRUCTION(StaticFieldSet);
5973
5974 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005975 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5976 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5977 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5978 "Too many packed fields.");
5979
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005980 const FieldInfo field_info_;
5981
5982 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5983};
5984
Vladimir Markofcb503c2016-05-18 12:48:17 +01005985class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005986 public:
5987 HUnresolvedInstanceFieldGet(HInstruction* obj,
5988 Primitive::Type field_type,
5989 uint32_t field_index,
5990 uint32_t dex_pc)
5991 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5992 field_index_(field_index) {
5993 SetRawInputAt(0, obj);
5994 }
5995
5996 bool NeedsEnvironment() const OVERRIDE { return true; }
5997 bool CanThrow() const OVERRIDE { return true; }
5998
5999 Primitive::Type GetFieldType() const { return GetType(); }
6000 uint32_t GetFieldIndex() const { return field_index_; }
6001
6002 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6003
6004 private:
6005 const uint32_t field_index_;
6006
6007 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6008};
6009
Vladimir Markofcb503c2016-05-18 12:48:17 +01006010class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006011 public:
6012 HUnresolvedInstanceFieldSet(HInstruction* obj,
6013 HInstruction* value,
6014 Primitive::Type field_type,
6015 uint32_t field_index,
6016 uint32_t dex_pc)
6017 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006018 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006019 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006020 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006021 SetRawInputAt(0, obj);
6022 SetRawInputAt(1, value);
6023 }
6024
6025 bool NeedsEnvironment() const OVERRIDE { return true; }
6026 bool CanThrow() const OVERRIDE { return true; }
6027
Vladimir Markoa1de9182016-02-25 11:37:38 +00006028 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006029 uint32_t GetFieldIndex() const { return field_index_; }
6030
6031 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6032
6033 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006034 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6035 static constexpr size_t kFieldFieldTypeSize =
6036 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6037 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6038 kFieldFieldType + kFieldFieldTypeSize;
6039 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6040 "Too many packed fields.");
6041 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6042
Calin Juravlee460d1d2015-09-29 04:52:17 +01006043 const uint32_t field_index_;
6044
6045 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6046};
6047
Vladimir Markofcb503c2016-05-18 12:48:17 +01006048class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006049 public:
6050 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6051 uint32_t field_index,
6052 uint32_t dex_pc)
6053 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6054 field_index_(field_index) {
6055 }
6056
6057 bool NeedsEnvironment() const OVERRIDE { return true; }
6058 bool CanThrow() const OVERRIDE { return true; }
6059
6060 Primitive::Type GetFieldType() const { return GetType(); }
6061 uint32_t GetFieldIndex() const { return field_index_; }
6062
6063 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6064
6065 private:
6066 const uint32_t field_index_;
6067
6068 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6069};
6070
Vladimir Markofcb503c2016-05-18 12:48:17 +01006071class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006072 public:
6073 HUnresolvedStaticFieldSet(HInstruction* value,
6074 Primitive::Type field_type,
6075 uint32_t field_index,
6076 uint32_t dex_pc)
6077 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006078 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006079 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006080 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006081 SetRawInputAt(0, value);
6082 }
6083
6084 bool NeedsEnvironment() const OVERRIDE { return true; }
6085 bool CanThrow() const OVERRIDE { return true; }
6086
Vladimir Markoa1de9182016-02-25 11:37:38 +00006087 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006088 uint32_t GetFieldIndex() const { return field_index_; }
6089
6090 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6091
6092 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006093 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6094 static constexpr size_t kFieldFieldTypeSize =
6095 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6096 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6097 kFieldFieldType + kFieldFieldTypeSize;
6098 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6099 "Too many packed fields.");
6100 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6101
Calin Juravlee460d1d2015-09-29 04:52:17 +01006102 const uint32_t field_index_;
6103
6104 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6105};
6106
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006107// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006108class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006109 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006110 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6111 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006112
David Brazdilbbd733e2015-08-18 17:48:17 +01006113 bool CanBeNull() const OVERRIDE { return false; }
6114
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006115 DECLARE_INSTRUCTION(LoadException);
6116
6117 private:
6118 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6119};
6120
David Brazdilcb1c0552015-08-04 16:22:25 +01006121// Implicit part of move-exception which clears thread-local exception storage.
6122// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006123class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006124 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006125 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6126 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006127
6128 DECLARE_INSTRUCTION(ClearException);
6129
6130 private:
6131 DISALLOW_COPY_AND_ASSIGN(HClearException);
6132};
6133
Vladimir Markofcb503c2016-05-18 12:48:17 +01006134class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006135 public:
6136 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006137 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006138 SetRawInputAt(0, exception);
6139 }
6140
6141 bool IsControlFlow() const OVERRIDE { return true; }
6142
6143 bool NeedsEnvironment() const OVERRIDE { return true; }
6144
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006145 bool CanThrow() const OVERRIDE { return true; }
6146
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006147
6148 DECLARE_INSTRUCTION(Throw);
6149
6150 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006151 DISALLOW_COPY_AND_ASSIGN(HThrow);
6152};
6153
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006154/**
6155 * Implementation strategies for the code generator of a HInstanceOf
6156 * or `HCheckCast`.
6157 */
6158enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006159 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006160 kExactCheck, // Can do a single class compare.
6161 kClassHierarchyCheck, // Can just walk the super class chain.
6162 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6163 kInterfaceCheck, // No optimization yet when checking against an interface.
6164 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006165 kArrayCheck, // No optimization yet when checking against a generic array.
6166 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006167};
6168
Roland Levillain86503782016-02-11 19:07:30 +00006169std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6170
Vladimir Markofcb503c2016-05-18 12:48:17 +01006171class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006172 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006173 HInstanceOf(HInstruction* object,
6174 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006175 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006176 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006177 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006178 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006179 dex_pc) {
6180 SetPackedField<TypeCheckKindField>(check_kind);
6181 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006182 SetRawInputAt(0, object);
6183 SetRawInputAt(1, constant);
6184 }
6185
6186 bool CanBeMoved() const OVERRIDE { return true; }
6187
Vladimir Marko372f10e2016-05-17 16:30:10 +01006188 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006189 return true;
6190 }
6191
6192 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006193 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006194 }
6195
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006196 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006197 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6198 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6199 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6200 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006201
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006202 static bool CanCallRuntime(TypeCheckKind check_kind) {
6203 // Mips currently does runtime calls for any other checks.
6204 return check_kind != TypeCheckKind::kExactCheck;
6205 }
6206
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006207 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006208 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006209 }
6210
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006211 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006212
6213 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006214 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6215 static constexpr size_t kFieldTypeCheckKindSize =
6216 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6217 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6218 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6219 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6220 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006221
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006222 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6223};
6224
Vladimir Markofcb503c2016-05-18 12:48:17 +01006225class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006226 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006227 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006228 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006229 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6230 SetPackedFlag<kFlagUpperCanBeNull>(true);
6231 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006232 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006233 SetRawInputAt(0, input);
6234 }
6235
David Brazdilf5552582015-12-27 13:36:12 +00006236 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006237 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006238 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006239 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006240
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006241 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006242 DCHECK(GetUpperCanBeNull() || !can_be_null);
6243 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006244 }
6245
Vladimir Markoa1de9182016-02-25 11:37:38 +00006246 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006247
Calin Juravleb1498f62015-02-16 13:13:29 +00006248 DECLARE_INSTRUCTION(BoundType);
6249
6250 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006251 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6252 // is false then CanBeNull() cannot be true).
6253 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6254 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6255 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6256 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6257
Calin Juravleb1498f62015-02-16 13:13:29 +00006258 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006259 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6260 // It is used to bound the type in cases like:
6261 // if (x instanceof ClassX) {
6262 // // uper_bound_ will be ClassX
6263 // }
David Brazdilf5552582015-12-27 13:36:12 +00006264 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006265
6266 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6267};
6268
Vladimir Markofcb503c2016-05-18 12:48:17 +01006269class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006270 public:
6271 HCheckCast(HInstruction* object,
6272 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006273 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006274 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006275 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6276 SetPackedField<TypeCheckKindField>(check_kind);
6277 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006278 SetRawInputAt(0, object);
6279 SetRawInputAt(1, constant);
6280 }
6281
6282 bool CanBeMoved() const OVERRIDE { return true; }
6283
Vladimir Marko372f10e2016-05-17 16:30:10 +01006284 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006285 return true;
6286 }
6287
6288 bool NeedsEnvironment() const OVERRIDE {
6289 // Instruction may throw a CheckCastError.
6290 return true;
6291 }
6292
6293 bool CanThrow() const OVERRIDE { return true; }
6294
Vladimir Markoa1de9182016-02-25 11:37:38 +00006295 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6296 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6297 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6298 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006299
6300 DECLARE_INSTRUCTION(CheckCast);
6301
6302 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006303 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6304 static constexpr size_t kFieldTypeCheckKindSize =
6305 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6306 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6307 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6308 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6309 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006310
6311 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006312};
6313
Andreas Gampe26de38b2016-07-27 17:53:11 -07006314/**
6315 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6316 * @details We define the combined barrier types that are actually required
6317 * by the Java Memory Model, rather than using exactly the terminology from
6318 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6319 * primitives. Note that the JSR-133 cookbook generally does not deal with
6320 * store atomicity issues, and the recipes there are not always entirely sufficient.
6321 * The current recipe is as follows:
6322 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6323 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6324 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6325 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6326 * class has final fields.
6327 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6328 * store-to-memory instructions. Only generated together with non-temporal stores.
6329 */
6330enum MemBarrierKind {
6331 kAnyStore,
6332 kLoadAny,
6333 kStoreStore,
6334 kAnyAny,
6335 kNTStoreStore,
6336 kLastBarrierKind = kNTStoreStore
6337};
6338std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6339
Vladimir Markofcb503c2016-05-18 12:48:17 +01006340class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006341 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006342 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006343 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006344 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6345 SetPackedField<BarrierKindField>(barrier_kind);
6346 }
Calin Juravle27df7582015-04-17 19:12:31 +01006347
Vladimir Markoa1de9182016-02-25 11:37:38 +00006348 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006349
6350 DECLARE_INSTRUCTION(MemoryBarrier);
6351
6352 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006353 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6354 static constexpr size_t kFieldBarrierKindSize =
6355 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6356 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6357 kFieldBarrierKind + kFieldBarrierKindSize;
6358 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6359 "Too many packed fields.");
6360 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006361
6362 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6363};
6364
Vladimir Markofcb503c2016-05-18 12:48:17 +01006365class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006366 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006367 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006368 kEnter,
6369 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006370 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006371 };
6372
6373 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006374 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006375 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6376 dex_pc) {
6377 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006378 SetRawInputAt(0, object);
6379 }
6380
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006381 // Instruction may go into runtime, so we need an environment.
6382 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006383
6384 bool CanThrow() const OVERRIDE {
6385 // Verifier guarantees that monitor-exit cannot throw.
6386 // This is important because it allows the HGraphBuilder to remove
6387 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6388 return IsEnter();
6389 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006390
Vladimir Markoa1de9182016-02-25 11:37:38 +00006391 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6392 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006393
6394 DECLARE_INSTRUCTION(MonitorOperation);
6395
Calin Juravle52c48962014-12-16 17:02:57 +00006396 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006397 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6398 static constexpr size_t kFieldOperationKindSize =
6399 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6400 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6401 kFieldOperationKind + kFieldOperationKindSize;
6402 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6403 "Too many packed fields.");
6404 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006405
6406 private:
6407 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6408};
6409
Vladimir Markofcb503c2016-05-18 12:48:17 +01006410class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006411 public:
6412 HSelect(HInstruction* condition,
6413 HInstruction* true_value,
6414 HInstruction* false_value,
6415 uint32_t dex_pc)
6416 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6417 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6418
6419 // First input must be `true_value` or `false_value` to allow codegens to
6420 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6421 // that architectures which implement HSelect as a conditional move also
6422 // will not need to invert the condition.
6423 SetRawInputAt(0, false_value);
6424 SetRawInputAt(1, true_value);
6425 SetRawInputAt(2, condition);
6426 }
6427
6428 HInstruction* GetFalseValue() const { return InputAt(0); }
6429 HInstruction* GetTrueValue() const { return InputAt(1); }
6430 HInstruction* GetCondition() const { return InputAt(2); }
6431
6432 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006433 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6434 return true;
6435 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006436
6437 bool CanBeNull() const OVERRIDE {
6438 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6439 }
6440
6441 DECLARE_INSTRUCTION(Select);
6442
6443 private:
6444 DISALLOW_COPY_AND_ASSIGN(HSelect);
6445};
6446
Vladimir Markof9f64412015-09-02 14:05:49 +01006447class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006448 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006449 MoveOperands(Location source,
6450 Location destination,
6451 Primitive::Type type,
6452 HInstruction* instruction)
6453 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006454
6455 Location GetSource() const { return source_; }
6456 Location GetDestination() const { return destination_; }
6457
6458 void SetSource(Location value) { source_ = value; }
6459 void SetDestination(Location value) { destination_ = value; }
6460
6461 // The parallel move resolver marks moves as "in-progress" by clearing the
6462 // destination (but not the source).
6463 Location MarkPending() {
6464 DCHECK(!IsPending());
6465 Location dest = destination_;
6466 destination_ = Location::NoLocation();
6467 return dest;
6468 }
6469
6470 void ClearPending(Location dest) {
6471 DCHECK(IsPending());
6472 destination_ = dest;
6473 }
6474
6475 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006476 DCHECK(source_.IsValid() || destination_.IsInvalid());
6477 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006478 }
6479
6480 // True if this blocks a move from the given location.
6481 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006482 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006483 }
6484
6485 // A move is redundant if it's been eliminated, if its source and
6486 // destination are the same, or if its destination is unneeded.
6487 bool IsRedundant() const {
6488 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6489 }
6490
6491 // We clear both operands to indicate move that's been eliminated.
6492 void Eliminate() {
6493 source_ = destination_ = Location::NoLocation();
6494 }
6495
6496 bool IsEliminated() const {
6497 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6498 return source_.IsInvalid();
6499 }
6500
Alexey Frunze4dda3372015-06-01 18:31:49 -07006501 Primitive::Type GetType() const { return type_; }
6502
Nicolas Geoffray90218252015-04-15 11:56:51 +01006503 bool Is64BitMove() const {
6504 return Primitive::Is64BitType(type_);
6505 }
6506
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006507 HInstruction* GetInstruction() const { return instruction_; }
6508
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006509 private:
6510 Location source_;
6511 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006512 // The type this move is for.
6513 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006514 // The instruction this move is assocatied with. Null when this move is
6515 // for moving an input in the expected locations of user (including a phi user).
6516 // This is only used in debug mode, to ensure we do not connect interval siblings
6517 // in the same parallel move.
6518 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006519};
6520
Roland Levillainc9285912015-12-18 10:38:42 +00006521std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6522
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006523static constexpr size_t kDefaultNumberOfMoves = 4;
6524
Vladimir Markofcb503c2016-05-18 12:48:17 +01006525class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006526 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006527 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006528 : HTemplateInstruction(SideEffects::None(), dex_pc),
6529 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6530 moves_.reserve(kDefaultNumberOfMoves);
6531 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006532
Nicolas Geoffray90218252015-04-15 11:56:51 +01006533 void AddMove(Location source,
6534 Location destination,
6535 Primitive::Type type,
6536 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006537 DCHECK(source.IsValid());
6538 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006539 if (kIsDebugBuild) {
6540 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006541 for (const MoveOperands& move : moves_) {
6542 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006543 // Special case the situation where the move is for the spill slot
6544 // of the instruction.
6545 if ((GetPrevious() == instruction)
6546 || ((GetPrevious() == nullptr)
6547 && instruction->IsPhi()
6548 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006549 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006550 << "Doing parallel moves for the same instruction.";
6551 } else {
6552 DCHECK(false) << "Doing parallel moves for the same instruction.";
6553 }
6554 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006555 }
6556 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006557 for (const MoveOperands& move : moves_) {
6558 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006559 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006560 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006561 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006562 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006563 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006564 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006565 }
6566
Vladimir Marko225b6462015-09-28 12:17:40 +01006567 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006568 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006569 }
6570
Vladimir Marko225b6462015-09-28 12:17:40 +01006571 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006572
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006573 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006574
6575 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006576 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006577
6578 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6579};
6580
Mark Mendell0616ae02015-04-17 12:49:27 -04006581} // namespace art
6582
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006583#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6584#include "nodes_shared.h"
6585#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006586#ifdef ART_ENABLE_CODEGEN_arm
6587#include "nodes_arm.h"
6588#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006589#ifdef ART_ENABLE_CODEGEN_arm64
6590#include "nodes_arm64.h"
6591#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006592#ifdef ART_ENABLE_CODEGEN_mips
6593#include "nodes_mips.h"
6594#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006595#ifdef ART_ENABLE_CODEGEN_x86
6596#include "nodes_x86.h"
6597#endif
6598
6599namespace art {
6600
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006601class HGraphVisitor : public ValueObject {
6602 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006603 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6604 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006605
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006606 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006607 virtual void VisitBasicBlock(HBasicBlock* block);
6608
Roland Levillain633021e2014-10-01 14:12:25 +01006609 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006610 void VisitInsertionOrder();
6611
Roland Levillain633021e2014-10-01 14:12:25 +01006612 // Visit the graph following dominator tree reverse post-order.
6613 void VisitReversePostOrder();
6614
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006615 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006616
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006617 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006618#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006619 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6620
6621 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6622
6623#undef DECLARE_VISIT_INSTRUCTION
6624
6625 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006626 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006627
6628 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6629};
6630
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006631class HGraphDelegateVisitor : public HGraphVisitor {
6632 public:
6633 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6634 virtual ~HGraphDelegateVisitor() {}
6635
6636 // Visit functions that delegate to to super class.
6637#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006638 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006639
6640 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6641
6642#undef DECLARE_VISIT_INSTRUCTION
6643
6644 private:
6645 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6646};
6647
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006648// Iterator over the blocks that art part of the loop. Includes blocks part
6649// of an inner loop. The order in which the blocks are iterated is on their
6650// block id.
6651class HBlocksInLoopIterator : public ValueObject {
6652 public:
6653 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6654 : blocks_in_loop_(info.GetBlocks()),
6655 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6656 index_(0) {
6657 if (!blocks_in_loop_.IsBitSet(index_)) {
6658 Advance();
6659 }
6660 }
6661
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006662 bool Done() const { return index_ == blocks_.size(); }
6663 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006664 void Advance() {
6665 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006666 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006667 if (blocks_in_loop_.IsBitSet(index_)) {
6668 break;
6669 }
6670 }
6671 }
6672
6673 private:
6674 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006675 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006676 size_t index_;
6677
6678 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6679};
6680
Mingyao Yang3584bce2015-05-19 16:01:59 -07006681// Iterator over the blocks that art part of the loop. Includes blocks part
6682// of an inner loop. The order in which the blocks are iterated is reverse
6683// post order.
6684class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6685 public:
6686 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6687 : blocks_in_loop_(info.GetBlocks()),
6688 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6689 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006690 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006691 Advance();
6692 }
6693 }
6694
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006695 bool Done() const { return index_ == blocks_.size(); }
6696 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006697 void Advance() {
6698 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006699 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6700 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006701 break;
6702 }
6703 }
6704 }
6705
6706 private:
6707 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006708 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006709 size_t index_;
6710
6711 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6712};
6713
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006714inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006715 if (constant->IsIntConstant()) {
6716 return constant->AsIntConstant()->GetValue();
6717 } else if (constant->IsLongConstant()) {
6718 return constant->AsLongConstant()->GetValue();
6719 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006720 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006721 return 0;
6722 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006723}
6724
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006725#define INSTRUCTION_TYPE_CHECK(type, super) \
6726 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6727 inline const H##type* HInstruction::As##type() const { \
6728 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6729 } \
6730 inline H##type* HInstruction::As##type() { \
6731 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6732 }
6733
6734 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6735#undef INSTRUCTION_TYPE_CHECK
6736
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006737// Create space in `blocks` for adding `number_of_new_blocks` entries
6738// starting at location `at`. Blocks after `at` are moved accordingly.
6739inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6740 size_t number_of_new_blocks,
6741 size_t after) {
6742 DCHECK_LT(after, blocks->size());
6743 size_t old_size = blocks->size();
6744 size_t new_size = old_size + number_of_new_blocks;
6745 blocks->resize(new_size);
6746 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6747}
6748
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006749} // namespace art
6750
6751#endif // ART_COMPILER_OPTIMIZING_NODES_H_