blob: 99d767346779d94f71bafd0c54a888a5d1f68fde [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"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010028#include "dex_file.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000038#include "utils/array_ref.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010039#include "utils/intrusive_forward_list.h"
Vladimir Marko372f10e2016-05-17 16:30:10 +010040#include "utils/transform_array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000041
42namespace art {
43
David Brazdil1abb4192015-02-17 18:33:36 +000044class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010046class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010048class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000050class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000051class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000053class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000054class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000055class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000056class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010057class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010058class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010059class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010060class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000061class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010062class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000063class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000064
Mathieu Chartier736b5602015-09-02 14:54:11 -070065namespace mirror {
66class DexCache;
67} // namespace mirror
68
Nicolas Geoffray818f2102014-02-18 16:43:35 +000069static const int kDefaultNumberOfBlocks = 8;
70static const int kDefaultNumberOfSuccessors = 2;
71static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010072static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010073static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000074static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000075
Roland Levillain5b5b9312016-03-22 14:57:31 +000076// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
77static constexpr int32_t kMaxIntShiftDistance = 0x1f;
78// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
79static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000080
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010081static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070082static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010083
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010084static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
85
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060086static constexpr uint32_t kNoDexPc = -1;
87
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010088inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
89 // For the purposes of the compiler, the dex files must actually be the same object
90 // if we want to safely treat them as the same. This is especially important for JIT
91 // as custom class loaders can open the same underlying file (or memory) multiple
92 // times and provide different class resolution but no two class loaders should ever
93 // use the same DexFile object - doing so is an unsupported hack that can lead to
94 // all sorts of weird failures.
95 return &lhs == &rhs;
96}
97
Dave Allison20dfc792014-06-16 20:44:29 -070098enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070099 // All types.
100 kCondEQ, // ==
101 kCondNE, // !=
102 // Signed integers and floating-point numbers.
103 kCondLT, // <
104 kCondLE, // <=
105 kCondGT, // >
106 kCondGE, // >=
107 // Unsigned integers.
108 kCondB, // <
109 kCondBE, // <=
110 kCondA, // >
111 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100112 // First and last aliases.
113 kCondFirst = kCondEQ,
114 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700115};
116
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000117enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000118 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000119 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000120 kAnalysisFailThrowCatchLoop,
121 kAnalysisFailAmbiguousArrayOp,
122 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000123};
124
Vladimir Markof9f64412015-09-02 14:05:49 +0100125class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100126 public:
127 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
128
129 void AddInstruction(HInstruction* instruction);
130 void RemoveInstruction(HInstruction* instruction);
131
David Brazdilc3d743f2015-04-22 13:40:50 +0100132 // Insert `instruction` before/after an existing instruction `cursor`.
133 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
134 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
135
Roland Levillain6b469232014-09-25 10:10:38 +0100136 // Return true if this list contains `instruction`.
137 bool Contains(HInstruction* instruction) const;
138
Roland Levillainccc07a92014-09-16 14:48:16 +0100139 // Return true if `instruction1` is found before `instruction2` in
140 // this instruction list and false otherwise. Abort if none
141 // of these instructions is found.
142 bool FoundBefore(const HInstruction* instruction1,
143 const HInstruction* instruction2) const;
144
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000145 bool IsEmpty() const { return first_instruction_ == nullptr; }
146 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
147
148 // Update the block of all instructions to be `block`.
149 void SetBlockOfInstructions(HBasicBlock* block) const;
150
151 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000152 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000153 void Add(const HInstructionList& instruction_list);
154
David Brazdil2d7352b2015-04-20 14:52:42 +0100155 // Return the number of instructions in the list. This is an expensive operation.
156 size_t CountSize() const;
157
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100158 private:
159 HInstruction* first_instruction_;
160 HInstruction* last_instruction_;
161
162 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000163 friend class HGraph;
164 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100165 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100166 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100167
168 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
169};
170
David Brazdil4833f5a2015-12-16 10:37:39 +0000171class ReferenceTypeInfo : ValueObject {
172 public:
173 typedef Handle<mirror::Class> TypeHandle;
174
Vladimir Markoa1de9182016-02-25 11:37:38 +0000175 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
176
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700177 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100178 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
179 }
180
Vladimir Markoa1de9182016-02-25 11:37:38 +0000181 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000182 return ReferenceTypeInfo(type_handle, is_exact);
183 }
184
185 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
186
Vladimir Markof39745e2016-01-26 12:16:55 +0000187 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000188 return handle.GetReference() != nullptr;
189 }
190
Vladimir Marko456307a2016-04-19 14:12:13 +0000191 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000192 return IsValidHandle(type_handle_);
193 }
194
195 bool IsExact() const { return is_exact_; }
196
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700197 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000198 DCHECK(IsValid());
199 return GetTypeHandle()->IsObjectClass();
200 }
201
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700202 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000203 DCHECK(IsValid());
204 return GetTypeHandle()->IsStringClass();
205 }
206
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700207 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000208 DCHECK(IsValid());
209 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
210 }
211
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700212 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000213 DCHECK(IsValid());
214 return GetTypeHandle()->IsInterface();
215 }
216
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700217 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000218 DCHECK(IsValid());
219 return GetTypeHandle()->IsArrayClass();
220 }
221
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700222 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000223 DCHECK(IsValid());
224 return GetTypeHandle()->IsPrimitiveArray();
225 }
226
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700227 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000228 DCHECK(IsValid());
229 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
230 }
231
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700232 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000233 DCHECK(IsValid());
234 if (!IsExact()) return false;
235 if (!IsArrayClass()) return false;
236 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
237 }
238
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700239 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000240 DCHECK(IsValid());
241 if (!IsExact()) return false;
242 if (!IsArrayClass()) return false;
243 if (!rti.IsArrayClass()) return false;
244 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
245 rti.GetTypeHandle()->GetComponentType());
246 }
247
248 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
249
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700250 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000251 DCHECK(IsValid());
252 DCHECK(rti.IsValid());
253 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
254 }
255
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700256 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000257 DCHECK(IsValid());
258 DCHECK(rti.IsValid());
259 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
260 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
261 }
262
263 // Returns true if the type information provide the same amount of details.
264 // Note that it does not mean that the instructions have the same actual type
265 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700266 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000267 if (!IsValid() && !rti.IsValid()) {
268 // Invalid types are equal.
269 return true;
270 }
271 if (!IsValid() || !rti.IsValid()) {
272 // One is valid, the other not.
273 return false;
274 }
275 return IsExact() == rti.IsExact()
276 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
277 }
278
279 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000280 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
281 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
282 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000283
284 // The class of the object.
285 TypeHandle type_handle_;
286 // Whether or not the type is exact or a superclass of the actual type.
287 // Whether or not we have any information about this type.
288 bool is_exact_;
289};
290
291std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
292
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000293// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100294class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000295 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100296 HGraph(ArenaAllocator* arena,
297 const DexFile& dex_file,
298 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100299 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700300 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100301 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100302 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000303 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100304 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000305 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100306 blocks_(arena->Adapter(kArenaAllocBlockList)),
307 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
308 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700309 entry_block_(nullptr),
310 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100311 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100312 number_of_vregs_(0),
313 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000314 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400315 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000316 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000317 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000318 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000319 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100320 dex_file_(dex_file),
321 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100322 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100323 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100324 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700325 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000326 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100327 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
328 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
329 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
330 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000331 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000332 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
333 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100334 blocks_.reserve(kDefaultNumberOfBlocks);
335 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000336
David Brazdilbadd8262016-02-02 16:28:56 +0000337 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
338 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
339
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000340 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100341 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
342
David Brazdil69ba7b72015-06-23 18:27:30 +0100343 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000344 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100345
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000346 HBasicBlock* GetEntryBlock() const { return entry_block_; }
347 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100348 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000349
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000350 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
351 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000352
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000353 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100354
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100355 void ComputeDominanceInformation();
356 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000357 void ClearLoopInformation();
358 void FindBackEdges(ArenaBitVector* visited);
359 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100360 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100361 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000362
David Brazdil4833f5a2015-12-16 10:37:39 +0000363 // Analyze all natural loops in this graph. Returns a code specifying that it
364 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000365 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000366 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100367
David Brazdilffee3d32015-07-06 11:48:53 +0100368 // Iterate over blocks to compute try block membership. Needs reverse post
369 // order and loop information.
370 void ComputeTryBlockInformation();
371
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000372 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000373 // Returns the instruction to replace the invoke expression or null if the
374 // invoke is for a void method. Note that the caller is responsible for replacing
375 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000376 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000377
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000378 // Update the loop and try membership of `block`, which was spawned from `reference`.
379 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
380 // should be the new back edge.
381 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
382 HBasicBlock* reference,
383 bool replace_if_back_edge);
384
Mingyao Yang3584bce2015-05-19 16:01:59 -0700385 // Need to add a couple of blocks to test if the loop body is entered and
386 // put deoptimization instructions, etc.
387 void TransformLoopHeaderForBCE(HBasicBlock* header);
388
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000389 // Removes `block` from the graph. Assumes `block` has been disconnected from
390 // other blocks and has no instructions or phis.
391 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000392
David Brazdilfc6a86a2015-06-26 10:33:45 +0000393 // Splits the edge between `block` and `successor` while preserving the
394 // indices in the predecessor/successor lists. If there are multiple edges
395 // between the blocks, the lowest indices are used.
396 // Returns the new block which is empty and has the same dex pc as `successor`.
397 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
398
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100399 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
400 void SimplifyLoop(HBasicBlock* header);
401
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000402 int32_t GetNextInstructionId() {
403 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000404 return current_instruction_id_++;
405 }
406
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000407 int32_t GetCurrentInstructionId() const {
408 return current_instruction_id_;
409 }
410
411 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000412 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000413 current_instruction_id_ = id;
414 }
415
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100416 uint16_t GetMaximumNumberOfOutVRegs() const {
417 return maximum_number_of_out_vregs_;
418 }
419
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000420 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
421 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100422 }
423
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100424 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
425 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
426 }
427
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000428 void UpdateTemporariesVRegSlots(size_t slots) {
429 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100430 }
431
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000432 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100433 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000434 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100435 }
436
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100437 void SetNumberOfVRegs(uint16_t number_of_vregs) {
438 number_of_vregs_ = number_of_vregs;
439 }
440
441 uint16_t GetNumberOfVRegs() const {
442 return number_of_vregs_;
443 }
444
445 void SetNumberOfInVRegs(uint16_t value) {
446 number_of_in_vregs_ = value;
447 }
448
David Brazdildee58d62016-04-07 09:54:26 +0000449 uint16_t GetNumberOfInVRegs() const {
450 return number_of_in_vregs_;
451 }
452
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100453 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100454 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100455 return number_of_vregs_ - number_of_in_vregs_;
456 }
457
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100458 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100459 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100460 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100461
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100462 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100463 return linear_order_;
464 }
465
Mark Mendell1152c922015-04-24 17:06:35 -0400466 bool HasBoundsChecks() const {
467 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800468 }
469
Mark Mendell1152c922015-04-24 17:06:35 -0400470 void SetHasBoundsChecks(bool value) {
471 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800472 }
473
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100474 bool ShouldGenerateConstructorBarrier() const {
475 return should_generate_constructor_barrier_;
476 }
477
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000478 bool IsDebuggable() const { return debuggable_; }
479
David Brazdil8d5b8b22015-03-24 10:51:52 +0000480 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000481 // already, it is created and inserted into the graph. This method is only for
482 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600483 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000484
485 // TODO: This is problematic for the consistency of reference type propagation
486 // because it can be created anytime after the pass and thus it will be left
487 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600488 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000489
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600490 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
491 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000492 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600493 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
494 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000495 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600496 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
497 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000498 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600499 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
500 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000501 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000502
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100503 HCurrentMethod* GetCurrentMethod();
504
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100505 const DexFile& GetDexFile() const {
506 return dex_file_;
507 }
508
509 uint32_t GetMethodIdx() const {
510 return method_idx_;
511 }
512
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100513 InvokeType GetInvokeType() const {
514 return invoke_type_;
515 }
516
Mark Mendellc4701932015-04-10 13:18:51 -0400517 InstructionSet GetInstructionSet() const {
518 return instruction_set_;
519 }
520
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000521 bool IsCompilingOsr() const { return osr_; }
522
David Brazdil77a48ae2015-09-15 12:34:04 +0000523 bool HasTryCatch() const { return has_try_catch_; }
524 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100525
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000526 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
527 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
528
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100529 ArtMethod* GetArtMethod() const { return art_method_; }
530 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
531
Mark Mendellf6529172015-11-17 11:16:56 -0500532 // Returns an instruction with the opposite boolean value from 'cond'.
533 // The instruction has been inserted into the graph, either as a constant, or
534 // before cursor.
535 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
536
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000537 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
538
David Brazdil2d7352b2015-04-20 14:52:42 +0100539 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000540 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100541 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000542
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000543 template <class InstructionType, typename ValueType>
544 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600545 ArenaSafeMap<ValueType, InstructionType*>* cache,
546 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000547 // Try to find an existing constant of the given value.
548 InstructionType* constant = nullptr;
549 auto cached_constant = cache->find(value);
550 if (cached_constant != cache->end()) {
551 constant = cached_constant->second;
552 }
553
554 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100555 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000556 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600557 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000558 cache->Overwrite(value, constant);
559 InsertConstant(constant);
560 }
561 return constant;
562 }
563
David Brazdil8d5b8b22015-03-24 10:51:52 +0000564 void InsertConstant(HConstant* instruction);
565
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000566 // Cache a float constant into the graph. This method should only be
567 // called by the SsaBuilder when creating "equivalent" instructions.
568 void CacheFloatConstant(HFloatConstant* constant);
569
570 // See CacheFloatConstant comment.
571 void CacheDoubleConstant(HDoubleConstant* constant);
572
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000573 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000574
575 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100576 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000577
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100578 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100579 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000580
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100581 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100582 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100583
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000584 HBasicBlock* entry_block_;
585 HBasicBlock* exit_block_;
586
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100587 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100588 uint16_t maximum_number_of_out_vregs_;
589
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100590 // The number of virtual registers in this method. Contains the parameters.
591 uint16_t number_of_vregs_;
592
593 // The number of virtual registers used by parameters of this method.
594 uint16_t number_of_in_vregs_;
595
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000596 // Number of vreg size slots that the temporaries use (used in baseline compiler).
597 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100598
Mark Mendell1152c922015-04-24 17:06:35 -0400599 // Has bounds checks. We can totally skip BCE if it's false.
600 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800601
David Brazdil77a48ae2015-09-15 12:34:04 +0000602 // Flag whether there are any try/catch blocks in the graph. We will skip
603 // try/catch-related passes if false.
604 bool has_try_catch_;
605
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000606 // Flag whether there are any irreducible loops in the graph.
607 bool has_irreducible_loops_;
608
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000609 // Indicates whether the graph should be compiled in a way that
610 // ensures full debuggability. If false, we can apply more
611 // aggressive optimizations that may limit the level of debugging.
612 const bool debuggable_;
613
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000614 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000615 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000616
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100617 // The dex file from which the method is from.
618 const DexFile& dex_file_;
619
620 // The method index in the dex file.
621 const uint32_t method_idx_;
622
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100623 // If inlined, this encodes how the callee is being invoked.
624 const InvokeType invoke_type_;
625
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100626 // Whether the graph has been transformed to SSA form. Only used
627 // in debug mode to ensure we are not using properties only valid
628 // for non-SSA form (like the number of temporaries).
629 bool in_ssa_form_;
630
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100631 const bool should_generate_constructor_barrier_;
632
Mathieu Chartiere401d142015-04-22 13:56:20 -0700633 const InstructionSet instruction_set_;
634
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000635 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000636 HNullConstant* cached_null_constant_;
637 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000638 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000639 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000640 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000641
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100642 HCurrentMethod* cached_current_method_;
643
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100644 // The ArtMethod this graph is for. Note that for AOT, it may be null,
645 // for example for methods whose declaring class could not be resolved
646 // (such as when the superclass could not be found).
647 ArtMethod* art_method_;
648
David Brazdil4833f5a2015-12-16 10:37:39 +0000649 // Keep the RTI of inexact Object to avoid having to pass stack handle
650 // collection pointer to passes which may create NullConstant.
651 ReferenceTypeInfo inexact_object_rti_;
652
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000653 // Whether we are compiling this graph for on stack replacement: this will
654 // make all loops seen as irreducible and emit special stack maps to mark
655 // compiled code entries which the interpreter can directly jump to.
656 const bool osr_;
657
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000658 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100659 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000660 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000661 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000662 DISALLOW_COPY_AND_ASSIGN(HGraph);
663};
664
Vladimir Markof9f64412015-09-02 14:05:49 +0100665class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000666 public:
667 HLoopInformation(HBasicBlock* header, HGraph* graph)
668 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100669 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000670 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100671 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100672 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100673 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000674 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100675 back_edges_.reserve(kDefaultNumberOfBackEdges);
676 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100677
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000678 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100679 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000680
681 void Dump(std::ostream& os);
682
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100683 HBasicBlock* GetHeader() const {
684 return header_;
685 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000686
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000687 void SetHeader(HBasicBlock* block) {
688 header_ = block;
689 }
690
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100691 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
692 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
693 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
694
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000695 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100696 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000697 }
698
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100699 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100700 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100701 }
702
David Brazdil46e2a392015-03-16 17:31:52 +0000703 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100704 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100705 }
706
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000707 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100708 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000709 }
710
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100711 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100712
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100713 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100714 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100715 }
716
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100717 // Returns the lifetime position of the back edge that has the
718 // greatest lifetime position.
719 size_t GetLifetimeEnd() const;
720
721 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100722 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100723 }
724
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000725 // Finds blocks that are part of this loop.
726 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100727
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100728 // Returns whether this loop information contains `block`.
729 // Note that this loop information *must* be populated before entering this function.
730 bool Contains(const HBasicBlock& block) const;
731
732 // Returns whether this loop information is an inner loop of `other`.
733 // Note that `other` *must* be populated before entering this function.
734 bool IsIn(const HLoopInformation& other) const;
735
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800736 // Returns true if instruction is not defined within this loop.
737 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700738
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100739 const ArenaBitVector& GetBlocks() const { return blocks_; }
740
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000741 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000742 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000743
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000744 void ClearAllBlocks() {
745 blocks_.ClearAllBits();
746 }
747
David Brazdil3f4a5222016-05-06 12:46:21 +0100748 bool HasBackEdgeNotDominatedByHeader() const;
749
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100750 bool IsPopulated() const {
751 return blocks_.GetHighestBitSet() != -1;
752 }
753
Anton Shaminf89381f2016-05-16 16:44:13 +0600754 bool DominatesAllBackEdges(HBasicBlock* block);
755
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000756 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100757 // Internal recursive implementation of `Populate`.
758 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100759 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100760
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000761 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100762 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000763 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100764 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100765 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100766 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000767
768 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
769};
770
David Brazdilec16f792015-08-19 15:04:01 +0100771// Stores try/catch information for basic blocks.
772// Note that HGraph is constructed so that catch blocks cannot simultaneously
773// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100774class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100775 public:
776 // Try block information constructor.
777 explicit TryCatchInformation(const HTryBoundary& try_entry)
778 : try_entry_(&try_entry),
779 catch_dex_file_(nullptr),
780 catch_type_index_(DexFile::kDexNoIndex16) {
781 DCHECK(try_entry_ != nullptr);
782 }
783
784 // Catch block information constructor.
785 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
786 : try_entry_(nullptr),
787 catch_dex_file_(&dex_file),
788 catch_type_index_(catch_type_index) {}
789
790 bool IsTryBlock() const { return try_entry_ != nullptr; }
791
792 const HTryBoundary& GetTryEntry() const {
793 DCHECK(IsTryBlock());
794 return *try_entry_;
795 }
796
797 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
798
799 bool IsCatchAllTypeIndex() const {
800 DCHECK(IsCatchBlock());
801 return catch_type_index_ == DexFile::kDexNoIndex16;
802 }
803
804 uint16_t GetCatchTypeIndex() const {
805 DCHECK(IsCatchBlock());
806 return catch_type_index_;
807 }
808
809 const DexFile& GetCatchDexFile() const {
810 DCHECK(IsCatchBlock());
811 return *catch_dex_file_;
812 }
813
814 private:
815 // One of possibly several TryBoundary instructions entering the block's try.
816 // Only set for try blocks.
817 const HTryBoundary* try_entry_;
818
819 // Exception type information. Only set for catch blocks.
820 const DexFile* catch_dex_file_;
821 const uint16_t catch_type_index_;
822};
823
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100824static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100825static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100826
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000827// A block in a method. Contains the list of instructions represented
828// as a double linked list. Each block knows its predecessors and
829// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100830
Vladimir Markof9f64412015-09-02 14:05:49 +0100831class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000832 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600833 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000834 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000835 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
836 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000837 loop_information_(nullptr),
838 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000839 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100840 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100841 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100842 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000843 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000844 try_catch_information_(nullptr) {
845 predecessors_.reserve(kDefaultNumberOfPredecessors);
846 successors_.reserve(kDefaultNumberOfSuccessors);
847 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
848 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000849
Vladimir Marko60584552015-09-03 13:35:12 +0000850 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100851 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000852 }
853
Vladimir Marko60584552015-09-03 13:35:12 +0000854 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100855 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000856 }
857
David Brazdild26a4112015-11-10 11:07:31 +0000858 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
859 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
860
Vladimir Marko60584552015-09-03 13:35:12 +0000861 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
862 return ContainsElement(successors_, block, start_from);
863 }
864
865 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100866 return dominated_blocks_;
867 }
868
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100869 bool IsEntryBlock() const {
870 return graph_->GetEntryBlock() == this;
871 }
872
873 bool IsExitBlock() const {
874 return graph_->GetExitBlock() == this;
875 }
876
David Brazdil46e2a392015-03-16 17:31:52 +0000877 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000878 bool IsSingleTryBoundary() const;
879
880 // Returns true if this block emits nothing but a jump.
881 bool IsSingleJump() const {
882 HLoopInformation* loop_info = GetLoopInformation();
883 return (IsSingleGoto() || IsSingleTryBoundary())
884 // Back edges generate a suspend check.
885 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
886 }
David Brazdil46e2a392015-03-16 17:31:52 +0000887
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000888 void AddBackEdge(HBasicBlock* back_edge) {
889 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000890 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000891 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100892 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000893 loop_information_->AddBackEdge(back_edge);
894 }
895
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000896 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000897 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000898
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100899 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000900 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600901 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000902
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000903 HBasicBlock* GetDominator() const { return dominator_; }
904 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000905 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
906
907 void RemoveDominatedBlock(HBasicBlock* block) {
908 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100909 }
Vladimir Marko60584552015-09-03 13:35:12 +0000910
911 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
912 ReplaceElement(dominated_blocks_, existing, new_block);
913 }
914
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100915 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000916
917 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100918 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000919 }
920
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100921 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
922 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100923 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100924 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100925 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
926 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000927
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000928 HInstruction* GetFirstInstructionDisregardMoves() const;
929
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000930 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000931 successors_.push_back(block);
932 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000933 }
934
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100935 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
936 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100937 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000938 new_block->predecessors_.push_back(this);
939 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000940 }
941
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000942 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
943 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000944 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000945 new_block->successors_.push_back(this);
946 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000947 }
948
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100949 // Insert `this` between `predecessor` and `successor. This method
950 // preserves the indicies, and will update the first edge found between
951 // `predecessor` and `successor`.
952 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
953 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100954 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000955 successor->predecessors_[predecessor_index] = this;
956 predecessor->successors_[successor_index] = this;
957 successors_.push_back(successor);
958 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100959 }
960
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100961 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000962 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100963 }
964
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000965 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000966 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000967 }
968
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100969 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000970 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100971 }
972
973 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000974 predecessors_.push_back(block);
975 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100976 }
977
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100978 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000979 DCHECK_EQ(predecessors_.size(), 2u);
980 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100981 }
982
David Brazdil769c9e52015-04-27 13:54:09 +0100983 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000984 DCHECK_EQ(successors_.size(), 2u);
985 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100986 }
987
David Brazdilfc6a86a2015-06-26 10:33:45 +0000988 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000989 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100990 }
991
David Brazdilfc6a86a2015-06-26 10:33:45 +0000992 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000993 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100994 }
995
David Brazdilfc6a86a2015-06-26 10:33:45 +0000996 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000997 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100998 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000999 }
1000
1001 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001002 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001003 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001004 }
1005
1006 // Returns whether the first occurrence of `predecessor` in the list of
1007 // predecessors is at index `idx`.
1008 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001009 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001010 return GetPredecessorIndexOf(predecessor) == idx;
1011 }
1012
David Brazdild7558da2015-09-22 13:04:14 +01001013 // Create a new block between this block and its predecessors. The new block
1014 // is added to the graph, all predecessor edges are relinked to it and an edge
1015 // is created to `this`. Returns the new empty block. Reverse post order or
1016 // loop and try/catch information are not updated.
1017 HBasicBlock* CreateImmediateDominator();
1018
David Brazdilfc6a86a2015-06-26 10:33:45 +00001019 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001020 // created, latter block. Note that this method will add the block to the
1021 // graph, create a Goto at the end of the former block and will create an edge
1022 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001023 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001024 HBasicBlock* SplitBefore(HInstruction* cursor);
1025
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001026 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001027 // created block. Note that this method just updates raw block information,
1028 // like predecessors, successors, dominators, and instruction list. It does not
1029 // update the graph, reverse post order, loop information, nor make sure the
1030 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001031 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1032
1033 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1034 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001035
1036 // Merge `other` at the end of `this`. Successors and dominated blocks of
1037 // `other` are changed to be successors and dominated blocks of `this`. Note
1038 // that this method does not update the graph, reverse post order, loop
1039 // information, nor make sure the blocks are consistent (for example ending
1040 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001041 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001042
1043 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1044 // of `this` are moved to `other`.
1045 // Note that this method does not update the graph, reverse post order, loop
1046 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001047 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001048 void ReplaceWith(HBasicBlock* other);
1049
David Brazdil2d7352b2015-04-20 14:52:42 +01001050 // Merge `other` at the end of `this`. This method updates loops, reverse post
1051 // order, links to predecessors, successors, dominators and deletes the block
1052 // from the graph. The two blocks must be successive, i.e. `this` the only
1053 // predecessor of `other` and vice versa.
1054 void MergeWith(HBasicBlock* other);
1055
1056 // Disconnects `this` from all its predecessors, successors and dominator,
1057 // removes it from all loops it is included in and eventually from the graph.
1058 // The block must not dominate any other block. Predecessors and successors
1059 // are safely updated.
1060 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001061
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001062 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001063 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001064 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001065 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001066 // Replace instruction `initial` with `replacement` within this block.
1067 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1068 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001069 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001070 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001071 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1072 // instruction list. With 'ensure_safety' set to true, it verifies that the
1073 // instruction is not in use and removes it from the use lists of its inputs.
1074 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1075 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001076 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001077
1078 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001079 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001080 }
1081
Roland Levillain6b879dd2014-09-22 17:13:44 +01001082 bool IsLoopPreHeaderFirstPredecessor() const {
1083 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001084 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001085 }
1086
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001087 bool IsFirstPredecessorBackEdge() const {
1088 DCHECK(IsLoopHeader());
1089 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1090 }
1091
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001092 HLoopInformation* GetLoopInformation() const {
1093 return loop_information_;
1094 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001095
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001096 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001097 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001098 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001099 void SetInLoop(HLoopInformation* info) {
1100 if (IsLoopHeader()) {
1101 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001102 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001103 loop_information_ = info;
1104 } else if (loop_information_->Contains(*info->GetHeader())) {
1105 // Block is currently part of an outer loop. Make it part of this inner loop.
1106 // Note that a non loop header having a loop information means this loop information
1107 // has already been populated
1108 loop_information_ = info;
1109 } else {
1110 // Block is part of an inner loop. Do not update the loop information.
1111 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1112 // at this point, because this method is being called while populating `info`.
1113 }
1114 }
1115
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001116 // Raw update of the loop information.
1117 void SetLoopInformation(HLoopInformation* info) {
1118 loop_information_ = info;
1119 }
1120
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001121 bool IsInLoop() const { return loop_information_ != nullptr; }
1122
David Brazdilec16f792015-08-19 15:04:01 +01001123 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1124
1125 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1126 try_catch_information_ = try_catch_information;
1127 }
1128
1129 bool IsTryBlock() const {
1130 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1131 }
1132
1133 bool IsCatchBlock() const {
1134 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1135 }
David Brazdilffee3d32015-07-06 11:48:53 +01001136
1137 // Returns the try entry that this block's successors should have. They will
1138 // be in the same try, unless the block ends in a try boundary. In that case,
1139 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001140 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001141
David Brazdild7558da2015-09-22 13:04:14 +01001142 bool HasThrowingInstructions() const;
1143
David Brazdila4b8c212015-05-07 09:59:30 +01001144 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001145 bool Dominates(HBasicBlock* block) const;
1146
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001147 size_t GetLifetimeStart() const { return lifetime_start_; }
1148 size_t GetLifetimeEnd() const { return lifetime_end_; }
1149
1150 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1151 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1152
David Brazdil8d5b8b22015-03-24 10:51:52 +00001153 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001154 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001155 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001156 bool HasSinglePhi() const;
1157
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001158 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001159 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001160 ArenaVector<HBasicBlock*> predecessors_;
1161 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001162 HInstructionList instructions_;
1163 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001164 HLoopInformation* loop_information_;
1165 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001166 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001167 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001168 // The dex program counter of the first instruction of this block.
1169 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001170 size_t lifetime_start_;
1171 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001172 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001173
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001174 friend class HGraph;
1175 friend class HInstruction;
1176
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001177 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1178};
1179
David Brazdilb2bd1c52015-03-25 11:17:37 +00001180// Iterates over the LoopInformation of all loops which contain 'block'
1181// from the innermost to the outermost.
1182class HLoopInformationOutwardIterator : public ValueObject {
1183 public:
1184 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1185 : current_(block.GetLoopInformation()) {}
1186
1187 bool Done() const { return current_ == nullptr; }
1188
1189 void Advance() {
1190 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001191 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001192 }
1193
1194 HLoopInformation* Current() const {
1195 DCHECK(!Done());
1196 return current_;
1197 }
1198
1199 private:
1200 HLoopInformation* current_;
1201
1202 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1203};
1204
Alexandre Ramesef20f712015-06-09 10:29:30 +01001205#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001206 M(Above, Condition) \
1207 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001208 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001209 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001210 M(ArrayGet, Instruction) \
1211 M(ArrayLength, Instruction) \
1212 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001213 M(Below, Condition) \
1214 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001215 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001216 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001217 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001218 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001219 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001220 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001221 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001222 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001223 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001224 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001225 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001226 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001227 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001228 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001229 M(Exit, Instruction) \
1230 M(FloatConstant, Constant) \
1231 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001232 M(GreaterThan, Condition) \
1233 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001234 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001235 M(InstanceFieldGet, Instruction) \
1236 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001237 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001238 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001239 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001240 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001241 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001242 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001243 M(LessThan, Condition) \
1244 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001245 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001246 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001247 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001248 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001249 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001250 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001251 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001252 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001253 M(Neg, UnaryOperation) \
1254 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001255 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001256 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001257 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001258 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001259 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001260 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001261 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001262 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001263 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001264 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001265 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001266 M(Return, Instruction) \
1267 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001268 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001269 M(Shl, BinaryOperation) \
1270 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001271 M(StaticFieldGet, Instruction) \
1272 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001273 M(UnresolvedInstanceFieldGet, Instruction) \
1274 M(UnresolvedInstanceFieldSet, Instruction) \
1275 M(UnresolvedStaticFieldGet, Instruction) \
1276 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001277 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001278 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001279 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001280 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001281 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001282 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001283 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001284 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001285
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001286/*
1287 * Instructions, shared across several (not all) architectures.
1288 */
1289#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1290#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1291#else
1292#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001293 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001294 M(MultiplyAccumulate, Instruction) \
1295 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001296#endif
1297
Vladimir Markob4536b72015-11-24 13:45:23 +00001298#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001299#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001300#else
1301#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1302 M(ArmDexCacheArraysBase, Instruction)
1303#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001304
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001305#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001306#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001307#else
1308#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001309 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001310#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001311
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001312#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001313#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001314#else
1315#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1316 M(MipsComputeBaseMethodAddress, Instruction) \
1317 M(MipsDexCacheArraysBase, Instruction)
1318#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001319
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001320#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1321
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001322#ifndef ART_ENABLE_CODEGEN_x86
1323#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1324#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001325#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1326 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001327 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001328 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001329 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001330#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001331
1332#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1333
1334#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1335 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001336 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001337 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1338 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001339 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001340 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001341 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1342 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1343
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001344#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1345 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001346 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001347 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001348 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001349 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001350
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001351#define FOR_EACH_INSTRUCTION(M) \
1352 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1353 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1354
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001355#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001356FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1357#undef FORWARD_DECLARATION
1358
Vladimir Marko372f10e2016-05-17 16:30:10 +01001359#define DECLARE_INSTRUCTION(type) \
1360 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1361 const char* DebugName() const OVERRIDE { return #type; } \
1362 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1363 return other->Is##type(); \
1364 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001365 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001366
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001367#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1368 bool Is##type() const { return As##type() != nullptr; } \
1369 const H##type* As##type() const { return this; } \
1370 H##type* As##type() { return this; }
1371
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001372template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001373class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001374 public:
David Brazdiled596192015-01-23 10:39:45 +00001375 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001376 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001377 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001378
Vladimir Marko46817b82016-03-29 12:21:58 +01001379 // Hook for the IntrusiveForwardList<>.
1380 // TODO: Hide this better.
1381 IntrusiveForwardListHook hook;
1382
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001383 private:
David Brazdiled596192015-01-23 10:39:45 +00001384 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001385 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001386
1387 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001388 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001389
Vladimir Marko46817b82016-03-29 12:21:58 +01001390 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001391
1392 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1393};
1394
David Brazdiled596192015-01-23 10:39:45 +00001395template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001396using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001397
David Brazdil1abb4192015-02-17 18:33:36 +00001398// This class is used by HEnvironment and HInstruction classes to record the
1399// instructions they use and pointers to the corresponding HUseListNodes kept
1400// by the used instructions.
1401template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001402class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001403 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001404 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1405 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001406
Vladimir Marko46817b82016-03-29 12:21:58 +01001407 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1408 : HUserRecord(old_record.instruction_, before_use_node) {}
1409 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1410 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001411 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001412 }
1413
1414 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001415 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1416 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001417
1418 private:
1419 // Instruction used by the user.
1420 HInstruction* instruction_;
1421
Vladimir Marko46817b82016-03-29 12:21:58 +01001422 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1423 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001424};
1425
Vladimir Markoe9004912016-06-16 16:50:52 +01001426// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1427// This is used for HInstruction::GetInputs() to return a container wrapper providing
1428// HInstruction* values even though the underlying container has HUserRecord<>s.
1429struct HInputExtractor {
1430 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1431 return record.GetInstruction();
1432 }
1433 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1434 return record.GetInstruction();
1435 }
1436};
1437
1438using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1439using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1440
Aart Bik854a02b2015-07-14 16:07:00 -07001441/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001442 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001443 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001444 * For write/read dependences on fields/arrays, the dependence analysis uses
1445 * type disambiguation (e.g. a float field write cannot modify the value of an
1446 * integer field read) and the access type (e.g. a reference array write cannot
1447 * modify the value of a reference field read [although it may modify the
1448 * reference fetch prior to reading the field, which is represented by its own
1449 * write/read dependence]). The analysis makes conservative points-to
1450 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1451 * the same, and any reference read depends on any reference read without
1452 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001453 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001454 * The internal representation uses 38-bit and is described in the table below.
1455 * The first line indicates the side effect, and for field/array accesses the
1456 * second line indicates the type of the access (in the order of the
1457 * Primitive::Type enum).
1458 * The two numbered lines below indicate the bit position in the bitfield (read
1459 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001460 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001461 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1462 * +-------------+---------+---------+--------------+---------+---------+
1463 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1464 * | 3 |333333322|222222221| 1 |111111110|000000000|
1465 * | 7 |654321098|765432109| 8 |765432109|876543210|
1466 *
1467 * Note that, to ease the implementation, 'changes' bits are least significant
1468 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001469 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001470class SideEffects : public ValueObject {
1471 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001472 SideEffects() : flags_(0) {}
1473
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001474 static SideEffects None() {
1475 return SideEffects(0);
1476 }
1477
1478 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001479 return SideEffects(kAllChangeBits | kAllDependOnBits);
1480 }
1481
1482 static SideEffects AllChanges() {
1483 return SideEffects(kAllChangeBits);
1484 }
1485
1486 static SideEffects AllDependencies() {
1487 return SideEffects(kAllDependOnBits);
1488 }
1489
1490 static SideEffects AllExceptGCDependency() {
1491 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1492 }
1493
1494 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001495 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001496 }
1497
Aart Bik34c3ba92015-07-20 14:08:59 -07001498 static SideEffects AllWrites() {
1499 return SideEffects(kAllWrites);
1500 }
1501
1502 static SideEffects AllReads() {
1503 return SideEffects(kAllReads);
1504 }
1505
1506 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1507 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001508 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001509 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001510 }
1511
Aart Bik854a02b2015-07-14 16:07:00 -07001512 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001513 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001514 }
1515
Aart Bik34c3ba92015-07-20 14:08:59 -07001516 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1517 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001518 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001519 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001520 }
1521
1522 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001523 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001524 }
1525
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001526 static SideEffects CanTriggerGC() {
1527 return SideEffects(1ULL << kCanTriggerGCBit);
1528 }
1529
1530 static SideEffects DependsOnGC() {
1531 return SideEffects(1ULL << kDependsOnGCBit);
1532 }
1533
Aart Bik854a02b2015-07-14 16:07:00 -07001534 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001535 SideEffects Union(SideEffects other) const {
1536 return SideEffects(flags_ | other.flags_);
1537 }
1538
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001539 SideEffects Exclusion(SideEffects other) const {
1540 return SideEffects(flags_ & ~other.flags_);
1541 }
1542
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001543 void Add(SideEffects other) {
1544 flags_ |= other.flags_;
1545 }
1546
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001547 bool Includes(SideEffects other) const {
1548 return (other.flags_ & flags_) == other.flags_;
1549 }
1550
1551 bool HasSideEffects() const {
1552 return (flags_ & kAllChangeBits);
1553 }
1554
1555 bool HasDependencies() const {
1556 return (flags_ & kAllDependOnBits);
1557 }
1558
1559 // Returns true if there are no side effects or dependencies.
1560 bool DoesNothing() const {
1561 return flags_ == 0;
1562 }
1563
Aart Bik854a02b2015-07-14 16:07:00 -07001564 // Returns true if something is written.
1565 bool DoesAnyWrite() const {
1566 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001567 }
1568
Aart Bik854a02b2015-07-14 16:07:00 -07001569 // Returns true if something is read.
1570 bool DoesAnyRead() const {
1571 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001572 }
1573
Aart Bik854a02b2015-07-14 16:07:00 -07001574 // Returns true if potentially everything is written and read
1575 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001576 bool DoesAllReadWrite() const {
1577 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1578 }
1579
Aart Bik854a02b2015-07-14 16:07:00 -07001580 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001581 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001582 }
1583
Roland Levillain0d5a2812015-11-13 10:07:31 +00001584 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001585 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001586 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1587 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001588 }
1589
1590 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001591 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001592 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001593 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001594 for (int s = kLastBit; s >= 0; s--) {
1595 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1596 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1597 // This is a bit for the GC side effect.
1598 if (current_bit_is_set) {
1599 flags += "GC";
1600 }
Aart Bik854a02b2015-07-14 16:07:00 -07001601 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001602 } else {
1603 // This is a bit for the array/field analysis.
1604 // The underscore character stands for the 'can trigger GC' bit.
1605 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1606 if (current_bit_is_set) {
1607 flags += kDebug[s];
1608 }
1609 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1610 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1611 flags += "|";
1612 }
1613 }
Aart Bik854a02b2015-07-14 16:07:00 -07001614 }
1615 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001616 }
1617
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001618 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001619
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001620 private:
1621 static constexpr int kFieldArrayAnalysisBits = 9;
1622
1623 static constexpr int kFieldWriteOffset = 0;
1624 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1625 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1626 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1627
1628 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1629
1630 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1631 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1632 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1633 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1634
1635 static constexpr int kLastBit = kDependsOnGCBit;
1636 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1637
1638 // Aliases.
1639
1640 static_assert(kChangeBits == kDependOnBits,
1641 "the 'change' bits should match the 'depend on' bits.");
1642
1643 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1644 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1645 static constexpr uint64_t kAllWrites =
1646 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1647 static constexpr uint64_t kAllReads =
1648 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001649
Aart Bik854a02b2015-07-14 16:07:00 -07001650 // Translates type to bit flag.
1651 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1652 CHECK_NE(type, Primitive::kPrimVoid);
1653 const uint64_t one = 1;
1654 const int shift = type; // 0-based consecutive enum
1655 DCHECK_LE(kFieldWriteOffset, shift);
1656 DCHECK_LT(shift, kArrayWriteOffset);
1657 return one << (type + offset);
1658 }
1659
1660 // Private constructor on direct flags value.
1661 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1662
1663 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001664};
1665
David Brazdiled596192015-01-23 10:39:45 +00001666// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001667class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001668 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001669 HEnvironment(ArenaAllocator* arena,
1670 size_t number_of_vregs,
1671 const DexFile& dex_file,
1672 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001673 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001674 InvokeType invoke_type,
1675 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001676 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1677 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001678 parent_(nullptr),
1679 dex_file_(dex_file),
1680 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001681 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001682 invoke_type_(invoke_type),
1683 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001684 }
1685
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001686 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001687 : HEnvironment(arena,
1688 to_copy.Size(),
1689 to_copy.GetDexFile(),
1690 to_copy.GetMethodIdx(),
1691 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001692 to_copy.GetInvokeType(),
1693 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001694
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001695 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001696 if (parent_ != nullptr) {
1697 parent_->SetAndCopyParentChain(allocator, parent);
1698 } else {
1699 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1700 parent_->CopyFrom(parent);
1701 if (parent->GetParent() != nullptr) {
1702 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1703 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001704 }
David Brazdiled596192015-01-23 10:39:45 +00001705 }
1706
Vladimir Marko71bf8092015-09-15 15:33:14 +01001707 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001708 void CopyFrom(HEnvironment* environment);
1709
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001710 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1711 // input to the loop phi instead. This is for inserting instructions that
1712 // require an environment (like HDeoptimization) in the loop pre-header.
1713 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001714
1715 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001716 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001717 }
1718
1719 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001720 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001721 }
1722
David Brazdil1abb4192015-02-17 18:33:36 +00001723 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001724
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001725 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001726
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001727 HEnvironment* GetParent() const { return parent_; }
1728
1729 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001730 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001731 }
1732
1733 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001734 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001735 }
1736
1737 uint32_t GetDexPc() const {
1738 return dex_pc_;
1739 }
1740
1741 uint32_t GetMethodIdx() const {
1742 return method_idx_;
1743 }
1744
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001745 InvokeType GetInvokeType() const {
1746 return invoke_type_;
1747 }
1748
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001749 const DexFile& GetDexFile() const {
1750 return dex_file_;
1751 }
1752
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001753 HInstruction* GetHolder() const {
1754 return holder_;
1755 }
1756
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001757
1758 bool IsFromInlinedInvoke() const {
1759 return GetParent() != nullptr;
1760 }
1761
David Brazdiled596192015-01-23 10:39:45 +00001762 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001763 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1764 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001765 HEnvironment* parent_;
1766 const DexFile& dex_file_;
1767 const uint32_t method_idx_;
1768 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001769 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001770
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001771 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001772 HInstruction* const holder_;
1773
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001774 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001775
1776 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1777};
1778
Vladimir Markof9f64412015-09-02 14:05:49 +01001779class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001780 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001781 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001782 : previous_(nullptr),
1783 next_(nullptr),
1784 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001785 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001786 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001787 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001788 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001789 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001790 locations_(nullptr),
1791 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001792 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001793 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001794 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1795 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1796 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001797
Dave Allison20dfc792014-06-16 20:44:29 -07001798 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001799
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001800#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001801 enum InstructionKind {
1802 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1803 };
1804#undef DECLARE_KIND
1805
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001806 HInstruction* GetNext() const { return next_; }
1807 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001808
Calin Juravle77520bc2015-01-12 18:45:46 +00001809 HInstruction* GetNextDisregardingMoves() const;
1810 HInstruction* GetPreviousDisregardingMoves() const;
1811
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001812 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001813 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001814 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001815 bool IsInBlock() const { return block_ != nullptr; }
1816 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001817 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1818 bool IsIrreducibleLoopHeaderPhi() const {
1819 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1820 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001821
Vladimir Marko372f10e2016-05-17 16:30:10 +01001822 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1823
1824 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1825 // One virtual method is enough, just const_cast<> and then re-add the const.
1826 return ArrayRef<const HUserRecord<HInstruction*>>(
1827 const_cast<HInstruction*>(this)->GetInputRecords());
1828 }
1829
Vladimir Markoe9004912016-06-16 16:50:52 +01001830 HInputsRef GetInputs() {
1831 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001832 }
1833
Vladimir Markoe9004912016-06-16 16:50:52 +01001834 HConstInputsRef GetInputs() const {
1835 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001836 }
1837
1838 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001839 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001840
Vladimir Marko372f10e2016-05-17 16:30:10 +01001841 void SetRawInputAt(size_t index, HInstruction* input) {
1842 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1843 }
1844
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001845 virtual void Accept(HGraphVisitor* visitor) = 0;
1846 virtual const char* DebugName() const = 0;
1847
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001848 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1849
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001850 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001851
1852 uint32_t GetDexPc() const { return dex_pc_; }
1853
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001854 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001855
Roland Levillaine161a2a2014-10-03 12:45:18 +01001856 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001857 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001858
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001859 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001860 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001861
Calin Juravle10e244f2015-01-26 18:54:32 +00001862 // Does not apply for all instructions, but having this at top level greatly
1863 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001864 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001865 virtual bool CanBeNull() const {
1866 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1867 return true;
1868 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001869
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001870 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001871 return false;
1872 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001873
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001874 virtual bool IsActualObject() const {
1875 return GetType() == Primitive::kPrimNot;
1876 }
1877
Calin Juravle2e768302015-07-28 14:41:11 +00001878 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001879
Calin Juravle61d544b2015-02-23 16:46:57 +00001880 ReferenceTypeInfo GetReferenceTypeInfo() const {
1881 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001882 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001883 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001884 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001885
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001886 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001887 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001888 // Note: fixup_end remains valid across push_front().
1889 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1890 HUseListNode<HInstruction*>* new_node =
1891 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1892 uses_.push_front(*new_node);
1893 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001894 }
1895
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001896 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001897 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001898 // Note: env_fixup_end remains valid across push_front().
1899 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1900 HUseListNode<HEnvironment*>* new_node =
1901 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1902 env_uses_.push_front(*new_node);
1903 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001904 }
1905
David Brazdil1abb4192015-02-17 18:33:36 +00001906 void RemoveAsUserOfInput(size_t input) {
1907 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001908 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1909 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1910 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001911 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001912
Vladimir Marko372f10e2016-05-17 16:30:10 +01001913 void RemoveAsUserOfAllInputs() {
1914 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1915 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1916 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1917 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1918 }
1919 }
1920
David Brazdil1abb4192015-02-17 18:33:36 +00001921 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1922 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001923
Vladimir Marko46817b82016-03-29 12:21:58 +01001924 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1925 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1926 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001927 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001928 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001929 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001930
Roland Levillain6c82d402014-10-13 16:10:27 +01001931 // Does this instruction strictly dominate `other_instruction`?
1932 // Returns false if this instruction and `other_instruction` are the same.
1933 // Aborts if this instruction and `other_instruction` are both phis.
1934 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001935
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001936 int GetId() const { return id_; }
1937 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001938
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001939 int GetSsaIndex() const { return ssa_index_; }
1940 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1941 bool HasSsaIndex() const { return ssa_index_ != -1; }
1942
1943 bool HasEnvironment() const { return environment_ != nullptr; }
1944 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001945 // Set the `environment_` field. Raw because this method does not
1946 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001947 void SetRawEnvironment(HEnvironment* environment) {
1948 DCHECK(environment_ == nullptr);
1949 DCHECK_EQ(environment->GetHolder(), this);
1950 environment_ = environment;
1951 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001952
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001953 void InsertRawEnvironment(HEnvironment* environment) {
1954 DCHECK(environment_ != nullptr);
1955 DCHECK_EQ(environment->GetHolder(), this);
1956 DCHECK(environment->GetParent() == nullptr);
1957 environment->parent_ = environment_;
1958 environment_ = environment;
1959 }
1960
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001961 void RemoveEnvironment();
1962
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001963 // Set the environment of this instruction, copying it from `environment`. While
1964 // copying, the uses lists are being updated.
1965 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001966 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001967 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001968 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001969 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001970 if (environment->GetParent() != nullptr) {
1971 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1972 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001973 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001974
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001975 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1976 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001977 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001978 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001979 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001980 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001981 if (environment->GetParent() != nullptr) {
1982 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1983 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001984 }
1985
Nicolas Geoffray39468442014-09-02 15:17:15 +01001986 // Returns the number of entries in the environment. Typically, that is the
1987 // number of dex registers in a method. It could be more in case of inlining.
1988 size_t EnvironmentSize() const;
1989
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001990 LocationSummary* GetLocations() const { return locations_; }
1991 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001992
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001993 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001994 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001995
Alexandre Rames188d4312015-04-09 18:30:21 +01001996 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1997 // uses of this instruction by `other` are *not* updated.
1998 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1999 ReplaceWith(other);
2000 other->ReplaceInput(this, use_index);
2001 }
2002
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002003 // Move `this` instruction before `cursor`.
2004 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002005
Vladimir Markofb337ea2015-11-25 15:25:10 +00002006 // Move `this` before its first user and out of any loops. If there is no
2007 // out-of-loop user that dominates all other users, move the instruction
2008 // to the end of the out-of-loop common dominator of the user's blocks.
2009 //
2010 // This can be used only on non-throwing instructions with no side effects that
2011 // have at least one use but no environment uses.
2012 void MoveBeforeFirstUserAndOutOfLoops();
2013
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002014#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002015 bool Is##type() const; \
2016 const H##type* As##type() const; \
2017 H##type* As##type();
2018
2019 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2020#undef INSTRUCTION_TYPE_CHECK
2021
2022#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002023 bool Is##type() const { return (As##type() != nullptr); } \
2024 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002025 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002026 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002027#undef INSTRUCTION_TYPE_CHECK
2028
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002029 // Returns whether the instruction can be moved within the graph.
2030 virtual bool CanBeMoved() const { return false; }
2031
2032 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002033 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002034 return false;
2035 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002036
2037 // Returns whether any data encoded in the two instructions is equal.
2038 // This method does not look at the inputs. Both instructions must be
2039 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002040 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002041 return false;
2042 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002043
2044 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002045 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002046 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002047 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002048
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002049 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2050 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2051 // the virtual function because the __attribute__((__pure__)) doesn't really
2052 // apply the strong requirement for virtual functions, preventing optimizations.
2053 InstructionKind GetKind() const PURE;
2054 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002055
2056 virtual size_t ComputeHashCode() const {
2057 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002058 for (const HInstruction* input : GetInputs()) {
2059 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002060 }
2061 return result;
2062 }
2063
2064 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002065 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002066 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002067
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002068 size_t GetLifetimePosition() const { return lifetime_position_; }
2069 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2070 LiveInterval* GetLiveInterval() const { return live_interval_; }
2071 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2072 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2073
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002074 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2075
2076 // Returns whether the code generation of the instruction will require to have access
2077 // to the current method. Such instructions are:
2078 // (1): Instructions that require an environment, as calling the runtime requires
2079 // to walk the stack and have the current method stored at a specific stack address.
2080 // (2): Object literals like classes and strings, that are loaded from the dex cache
2081 // fields of the current method.
2082 bool NeedsCurrentMethod() const {
2083 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2084 }
2085
Vladimir Markodc151b22015-10-15 18:02:30 +01002086 // Returns whether the code generation of the instruction will require to have access
2087 // to the dex cache of the current method's declaring class via the current method.
2088 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002089
Mark Mendellc4701932015-04-10 13:18:51 -04002090 // Does this instruction have any use in an environment before
2091 // control flow hits 'other'?
2092 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2093
2094 // Remove all references to environment uses of this instruction.
2095 // The caller must ensure that this is safe to do.
2096 void RemoveEnvironmentUsers();
2097
Vladimir Markoa1de9182016-02-25 11:37:38 +00002098 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2099 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002100
David Brazdil1abb4192015-02-17 18:33:36 +00002101 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002102 // If set, the machine code for this instruction is assumed to be generated by
2103 // its users. Used by liveness analysis to compute use positions accordingly.
2104 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2105 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2106 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2107 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2108
Vladimir Marko372f10e2016-05-17 16:30:10 +01002109 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2110 return GetInputRecords()[i];
2111 }
2112
2113 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2114 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2115 input_records[index] = input;
2116 }
David Brazdil1abb4192015-02-17 18:33:36 +00002117
Vladimir Markoa1de9182016-02-25 11:37:38 +00002118 uint32_t GetPackedFields() const {
2119 return packed_fields_;
2120 }
2121
2122 template <size_t flag>
2123 bool GetPackedFlag() const {
2124 return (packed_fields_ & (1u << flag)) != 0u;
2125 }
2126
2127 template <size_t flag>
2128 void SetPackedFlag(bool value = true) {
2129 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2130 }
2131
2132 template <typename BitFieldType>
2133 typename BitFieldType::value_type GetPackedField() const {
2134 return BitFieldType::Decode(packed_fields_);
2135 }
2136
2137 template <typename BitFieldType>
2138 void SetPackedField(typename BitFieldType::value_type value) {
2139 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2140 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2141 }
2142
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002143 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002144 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2145 auto before_use_node = uses_.before_begin();
2146 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2147 HInstruction* user = use_node->GetUser();
2148 size_t input_index = use_node->GetIndex();
2149 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2150 before_use_node = use_node;
2151 }
2152 }
2153
2154 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2155 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2156 if (next != uses_.end()) {
2157 HInstruction* next_user = next->GetUser();
2158 size_t next_index = next->GetIndex();
2159 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2160 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2161 }
2162 }
2163
2164 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2165 auto before_env_use_node = env_uses_.before_begin();
2166 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2167 HEnvironment* user = env_use_node->GetUser();
2168 size_t input_index = env_use_node->GetIndex();
2169 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2170 before_env_use_node = env_use_node;
2171 }
2172 }
2173
2174 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2175 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2176 if (next != env_uses_.end()) {
2177 HEnvironment* next_user = next->GetUser();
2178 size_t next_index = next->GetIndex();
2179 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2180 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2181 }
2182 }
David Brazdil1abb4192015-02-17 18:33:36 +00002183
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002184 HInstruction* previous_;
2185 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002186 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002187 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002188
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002189 // An instruction gets an id when it is added to the graph.
2190 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002191 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002192 int id_;
2193
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002194 // When doing liveness analysis, instructions that have uses get an SSA index.
2195 int ssa_index_;
2196
Vladimir Markoa1de9182016-02-25 11:37:38 +00002197 // Packed fields.
2198 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002199
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002200 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002201 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002202
2203 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002204 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002205
Nicolas Geoffray39468442014-09-02 15:17:15 +01002206 // The environment associated with this instruction. Not null if the instruction
2207 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002208 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002209
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002210 // Set by the code generator.
2211 LocationSummary* locations_;
2212
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002213 // Set by the liveness analysis.
2214 LiveInterval* live_interval_;
2215
2216 // Set by the liveness analysis, this is the position in a linear
2217 // order of blocks where this instruction's live interval start.
2218 size_t lifetime_position_;
2219
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002220 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002221
Vladimir Markoa1de9182016-02-25 11:37:38 +00002222 // The reference handle part of the reference type info.
2223 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002224 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002225 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002226
David Brazdil1abb4192015-02-17 18:33:36 +00002227 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002228 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002229 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002230 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002231 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002232
2233 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2234};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002235std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002236
2237class HInstructionIterator : public ValueObject {
2238 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002239 explicit HInstructionIterator(const HInstructionList& instructions)
2240 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002241 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002242 }
2243
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002244 bool Done() const { return instruction_ == nullptr; }
2245 HInstruction* Current() const { return instruction_; }
2246 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002247 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002248 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002249 }
2250
2251 private:
2252 HInstruction* instruction_;
2253 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002254
2255 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002256};
2257
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002258class HBackwardInstructionIterator : public ValueObject {
2259 public:
2260 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2261 : instruction_(instructions.last_instruction_) {
2262 next_ = Done() ? nullptr : instruction_->GetPrevious();
2263 }
2264
2265 bool Done() const { return instruction_ == nullptr; }
2266 HInstruction* Current() const { return instruction_; }
2267 void Advance() {
2268 instruction_ = next_;
2269 next_ = Done() ? nullptr : instruction_->GetPrevious();
2270 }
2271
2272 private:
2273 HInstruction* instruction_;
2274 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002275
2276 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002277};
2278
Vladimir Markof9f64412015-09-02 14:05:49 +01002279template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002280class HTemplateInstruction: public HInstruction {
2281 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002282 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002283 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002284 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002285
Vladimir Marko372f10e2016-05-17 16:30:10 +01002286 using HInstruction::GetInputRecords; // Keep the const version visible.
2287 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2288 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002289 }
2290
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002291 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002292 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002293
2294 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002295};
2296
Vladimir Markof9f64412015-09-02 14:05:49 +01002297// HTemplateInstruction specialization for N=0.
2298template<>
2299class HTemplateInstruction<0>: public HInstruction {
2300 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002301 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002302 : HInstruction(side_effects, dex_pc) {}
2303
Vladimir Markof9f64412015-09-02 14:05:49 +01002304 virtual ~HTemplateInstruction() {}
2305
Vladimir Marko372f10e2016-05-17 16:30:10 +01002306 using HInstruction::GetInputRecords; // Keep the const version visible.
2307 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2308 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002309 }
2310
2311 private:
2312 friend class SsaBuilder;
2313};
2314
Dave Allison20dfc792014-06-16 20:44:29 -07002315template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002316class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002317 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002318 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002319 : HTemplateInstruction<N>(side_effects, dex_pc) {
2320 this->template SetPackedField<TypeField>(type);
2321 }
Dave Allison20dfc792014-06-16 20:44:29 -07002322 virtual ~HExpression() {}
2323
Vladimir Markoa1de9182016-02-25 11:37:38 +00002324 Primitive::Type GetType() const OVERRIDE {
2325 return TypeField::Decode(this->GetPackedFields());
2326 }
Dave Allison20dfc792014-06-16 20:44:29 -07002327
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002328 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002329 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2330 static constexpr size_t kFieldTypeSize =
2331 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2332 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2333 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2334 "Too many packed fields.");
2335 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002336};
2337
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002338// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2339// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002340class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002341 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002342 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2343 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002344
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002345 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002346
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002347 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002348
2349 private:
2350 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2351};
2352
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002353// Represents dex's RETURN opcodes. A HReturn is a control flow
2354// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002355class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002356 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002357 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2358 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002359 SetRawInputAt(0, value);
2360 }
2361
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002362 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002363
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002364 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002365
2366 private:
2367 DISALLOW_COPY_AND_ASSIGN(HReturn);
2368};
2369
Vladimir Markofcb503c2016-05-18 12:48:17 +01002370class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002371 public:
2372 HPhi(ArenaAllocator* arena,
2373 uint32_t reg_number,
2374 size_t number_of_inputs,
2375 Primitive::Type type,
2376 uint32_t dex_pc = kNoDexPc)
2377 : HInstruction(SideEffects::None(), dex_pc),
2378 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2379 reg_number_(reg_number) {
2380 SetPackedField<TypeField>(ToPhiType(type));
2381 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2382 // Phis are constructed live and marked dead if conflicting or unused.
2383 // Individual steps of SsaBuilder should assume that if a phi has been
2384 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2385 SetPackedFlag<kFlagIsLive>(true);
2386 SetPackedFlag<kFlagCanBeNull>(true);
2387 }
2388
2389 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2390 static Primitive::Type ToPhiType(Primitive::Type type) {
2391 return Primitive::PrimitiveKind(type);
2392 }
2393
2394 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2395
Vladimir Marko372f10e2016-05-17 16:30:10 +01002396 using HInstruction::GetInputRecords; // Keep the const version visible.
2397 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2398 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2399 }
David Brazdildee58d62016-04-07 09:54:26 +00002400
2401 void AddInput(HInstruction* input);
2402 void RemoveInputAt(size_t index);
2403
2404 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2405 void SetType(Primitive::Type new_type) {
2406 // Make sure that only valid type changes occur. The following are allowed:
2407 // (1) int -> float/ref (primitive type propagation),
2408 // (2) long -> double (primitive type propagation).
2409 DCHECK(GetType() == new_type ||
2410 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2411 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2412 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2413 SetPackedField<TypeField>(new_type);
2414 }
2415
2416 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2417 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2418
2419 uint32_t GetRegNumber() const { return reg_number_; }
2420
2421 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2422 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2423 bool IsDead() const { return !IsLive(); }
2424 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2425
Vladimir Markoe9004912016-06-16 16:50:52 +01002426 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002427 return other != nullptr
2428 && other->IsPhi()
2429 && other->AsPhi()->GetBlock() == GetBlock()
2430 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2431 }
2432
2433 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2434 // An equivalent phi is a phi having the same dex register and type.
2435 // It assumes that phis with the same dex register are adjacent.
2436 HPhi* GetNextEquivalentPhiWithSameType() {
2437 HInstruction* next = GetNext();
2438 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2439 if (next->GetType() == GetType()) {
2440 return next->AsPhi();
2441 }
2442 next = next->GetNext();
2443 }
2444 return nullptr;
2445 }
2446
2447 DECLARE_INSTRUCTION(Phi);
2448
David Brazdildee58d62016-04-07 09:54:26 +00002449 private:
2450 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2451 static constexpr size_t kFieldTypeSize =
2452 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2453 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2454 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2455 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2456 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2457 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2458
Vladimir Marko372f10e2016-05-17 16:30:10 +01002459 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002460 const uint32_t reg_number_;
2461
2462 DISALLOW_COPY_AND_ASSIGN(HPhi);
2463};
2464
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002465// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002466// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002467// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002468class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002469 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002470 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002471
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002472 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002473
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002474 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002475
2476 private:
2477 DISALLOW_COPY_AND_ASSIGN(HExit);
2478};
2479
2480// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002481class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002482 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002483 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002484
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002485 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002486
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002487 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002488 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002489 }
2490
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002491 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002492
2493 private:
2494 DISALLOW_COPY_AND_ASSIGN(HGoto);
2495};
2496
Roland Levillain9867bc72015-08-05 10:21:34 +01002497class HConstant : public HExpression<0> {
2498 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002499 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2500 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002501
2502 bool CanBeMoved() const OVERRIDE { return true; }
2503
Roland Levillain1a653882016-03-18 18:05:57 +00002504 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002505 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002506 // Is this constant 0 in the arithmetic sense?
2507 virtual bool IsArithmeticZero() const { return false; }
2508 // Is this constant a 0-bit pattern?
2509 virtual bool IsZeroBitPattern() const { return false; }
2510 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002511 virtual bool IsOne() const { return false; }
2512
David Brazdil77a48ae2015-09-15 12:34:04 +00002513 virtual uint64_t GetValueAsUint64() const = 0;
2514
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002515 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002516
2517 private:
2518 DISALLOW_COPY_AND_ASSIGN(HConstant);
2519};
2520
Vladimir Markofcb503c2016-05-18 12:48:17 +01002521class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002522 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002523 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002524 return true;
2525 }
2526
David Brazdil77a48ae2015-09-15 12:34:04 +00002527 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2528
Roland Levillain9867bc72015-08-05 10:21:34 +01002529 size_t ComputeHashCode() const OVERRIDE { return 0; }
2530
Roland Levillain1a653882016-03-18 18:05:57 +00002531 // The null constant representation is a 0-bit pattern.
2532 virtual bool IsZeroBitPattern() const { return true; }
2533
Roland Levillain9867bc72015-08-05 10:21:34 +01002534 DECLARE_INSTRUCTION(NullConstant);
2535
2536 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002537 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002538
2539 friend class HGraph;
2540 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2541};
2542
2543// Constants of the type int. Those can be from Dex instructions, or
2544// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002545class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002546 public:
2547 int32_t GetValue() const { return value_; }
2548
David Brazdil9f389d42015-10-01 14:32:56 +01002549 uint64_t GetValueAsUint64() const OVERRIDE {
2550 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2551 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002552
Vladimir Marko372f10e2016-05-17 16:30:10 +01002553 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002554 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002555 return other->AsIntConstant()->value_ == value_;
2556 }
2557
2558 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2559
2560 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002561 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2562 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002563 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2564
Roland Levillain1a653882016-03-18 18:05:57 +00002565 // Integer constants are used to encode Boolean values as well,
2566 // where 1 means true and 0 means false.
2567 bool IsTrue() const { return GetValue() == 1; }
2568 bool IsFalse() const { return GetValue() == 0; }
2569
Roland Levillain9867bc72015-08-05 10:21:34 +01002570 DECLARE_INSTRUCTION(IntConstant);
2571
2572 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002573 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2574 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2575 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2576 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002577
2578 const int32_t value_;
2579
2580 friend class HGraph;
2581 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2582 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2583 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2584};
2585
Vladimir Markofcb503c2016-05-18 12:48:17 +01002586class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002587 public:
2588 int64_t GetValue() const { return value_; }
2589
David Brazdil77a48ae2015-09-15 12:34:04 +00002590 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2591
Vladimir Marko372f10e2016-05-17 16:30:10 +01002592 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002593 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002594 return other->AsLongConstant()->value_ == value_;
2595 }
2596
2597 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2598
2599 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002600 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2601 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002602 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2603
2604 DECLARE_INSTRUCTION(LongConstant);
2605
2606 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002607 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2608 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002609
2610 const int64_t value_;
2611
2612 friend class HGraph;
2613 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2614};
Dave Allison20dfc792014-06-16 20:44:29 -07002615
Vladimir Markofcb503c2016-05-18 12:48:17 +01002616class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002617 public:
2618 float GetValue() const { return value_; }
2619
2620 uint64_t GetValueAsUint64() const OVERRIDE {
2621 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2622 }
2623
Vladimir Marko372f10e2016-05-17 16:30:10 +01002624 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002625 DCHECK(other->IsFloatConstant()) << other->DebugName();
2626 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2627 }
2628
2629 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2630
2631 bool IsMinusOne() const OVERRIDE {
2632 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2633 }
Roland Levillain1a653882016-03-18 18:05:57 +00002634 bool IsArithmeticZero() const OVERRIDE {
2635 return std::fpclassify(value_) == FP_ZERO;
2636 }
2637 bool IsArithmeticPositiveZero() const {
2638 return IsArithmeticZero() && !std::signbit(value_);
2639 }
2640 bool IsArithmeticNegativeZero() const {
2641 return IsArithmeticZero() && std::signbit(value_);
2642 }
2643 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002644 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002645 }
2646 bool IsOne() const OVERRIDE {
2647 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2648 }
2649 bool IsNaN() const {
2650 return std::isnan(value_);
2651 }
2652
2653 DECLARE_INSTRUCTION(FloatConstant);
2654
2655 private:
2656 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2657 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2658 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2659 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2660
2661 const float value_;
2662
2663 // Only the SsaBuilder and HGraph can create floating-point constants.
2664 friend class SsaBuilder;
2665 friend class HGraph;
2666 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2667};
2668
Vladimir Markofcb503c2016-05-18 12:48:17 +01002669class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002670 public:
2671 double GetValue() const { return value_; }
2672
2673 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2674
Vladimir Marko372f10e2016-05-17 16:30:10 +01002675 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002676 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2677 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2678 }
2679
2680 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2681
2682 bool IsMinusOne() const OVERRIDE {
2683 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2684 }
Roland Levillain1a653882016-03-18 18:05:57 +00002685 bool IsArithmeticZero() const OVERRIDE {
2686 return std::fpclassify(value_) == FP_ZERO;
2687 }
2688 bool IsArithmeticPositiveZero() const {
2689 return IsArithmeticZero() && !std::signbit(value_);
2690 }
2691 bool IsArithmeticNegativeZero() const {
2692 return IsArithmeticZero() && std::signbit(value_);
2693 }
2694 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002695 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002696 }
2697 bool IsOne() const OVERRIDE {
2698 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2699 }
2700 bool IsNaN() const {
2701 return std::isnan(value_);
2702 }
2703
2704 DECLARE_INSTRUCTION(DoubleConstant);
2705
2706 private:
2707 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2708 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2709 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2710 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2711
2712 const double value_;
2713
2714 // Only the SsaBuilder and HGraph can create floating-point constants.
2715 friend class SsaBuilder;
2716 friend class HGraph;
2717 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2718};
2719
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002720// Conditional branch. A block ending with an HIf instruction must have
2721// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002722class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002723 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002724 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2725 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002726 SetRawInputAt(0, input);
2727 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002728
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002729 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002730
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002731 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002732 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002733 }
2734
2735 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002736 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002737 }
2738
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002739 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002740
2741 private:
2742 DISALLOW_COPY_AND_ASSIGN(HIf);
2743};
2744
David Brazdilfc6a86a2015-06-26 10:33:45 +00002745
2746// Abstract instruction which marks the beginning and/or end of a try block and
2747// links it to the respective exception handlers. Behaves the same as a Goto in
2748// non-exceptional control flow.
2749// Normal-flow successor is stored at index zero, exception handlers under
2750// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002751class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002752 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002753 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002754 kEntry,
2755 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002756 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002757 };
2758
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002759 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002760 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2761 SetPackedField<BoundaryKindField>(kind);
2762 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002763
2764 bool IsControlFlow() const OVERRIDE { return true; }
2765
2766 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002767 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002768
David Brazdild26a4112015-11-10 11:07:31 +00002769 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2770 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2771 }
2772
David Brazdilfc6a86a2015-06-26 10:33:45 +00002773 // Returns whether `handler` is among its exception handlers (non-zero index
2774 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002775 bool HasExceptionHandler(const HBasicBlock& handler) const {
2776 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002777 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002778 }
2779
2780 // If not present already, adds `handler` to its block's list of exception
2781 // handlers.
2782 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002783 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002784 GetBlock()->AddSuccessor(handler);
2785 }
2786 }
2787
Vladimir Markoa1de9182016-02-25 11:37:38 +00002788 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2789 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002790
David Brazdilffee3d32015-07-06 11:48:53 +01002791 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2792
David Brazdilfc6a86a2015-06-26 10:33:45 +00002793 DECLARE_INSTRUCTION(TryBoundary);
2794
2795 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002796 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2797 static constexpr size_t kFieldBoundaryKindSize =
2798 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2799 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2800 kFieldBoundaryKind + kFieldBoundaryKindSize;
2801 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2802 "Too many packed fields.");
2803 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002804
2805 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2806};
2807
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002808// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002809class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002810 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002811 // We set CanTriggerGC to prevent any intermediate address to be live
2812 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002813 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002814 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002815 SetRawInputAt(0, cond);
2816 }
2817
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002818 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002819 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002820 return true;
2821 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002822 bool NeedsEnvironment() const OVERRIDE { return true; }
2823 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002824
2825 DECLARE_INSTRUCTION(Deoptimize);
2826
2827 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002828 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2829};
2830
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002831// Represents the ArtMethod that was passed as a first argument to
2832// the method. It is used by instructions that depend on it, like
2833// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002834class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002835 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002836 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2837 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002838
2839 DECLARE_INSTRUCTION(CurrentMethod);
2840
2841 private:
2842 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2843};
2844
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002845// Fetches an ArtMethod from the virtual table or the interface method table
2846// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002847class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002848 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002849 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002850 kVTable,
2851 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002852 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002853 };
2854 HClassTableGet(HInstruction* cls,
2855 Primitive::Type type,
2856 TableKind kind,
2857 size_t index,
2858 uint32_t dex_pc)
2859 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002860 index_(index) {
2861 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002862 SetRawInputAt(0, cls);
2863 }
2864
2865 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002866 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002867 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002868 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002869 }
2870
Vladimir Markoa1de9182016-02-25 11:37:38 +00002871 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002872 size_t GetIndex() const { return index_; }
2873
2874 DECLARE_INSTRUCTION(ClassTableGet);
2875
2876 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002877 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2878 static constexpr size_t kFieldTableKindSize =
2879 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2880 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2881 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2882 "Too many packed fields.");
2883 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2884
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002885 // The index of the ArtMethod in the table.
2886 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002887
2888 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2889};
2890
Mark Mendellfe57faa2015-09-18 09:26:15 -04002891// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2892// have one successor for each entry in the switch table, and the final successor
2893// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002894class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002895 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002896 HPackedSwitch(int32_t start_value,
2897 uint32_t num_entries,
2898 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002899 uint32_t dex_pc = kNoDexPc)
2900 : HTemplateInstruction(SideEffects::None(), dex_pc),
2901 start_value_(start_value),
2902 num_entries_(num_entries) {
2903 SetRawInputAt(0, input);
2904 }
2905
2906 bool IsControlFlow() const OVERRIDE { return true; }
2907
2908 int32_t GetStartValue() const { return start_value_; }
2909
Vladimir Marko211c2112015-09-24 16:52:33 +01002910 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002911
2912 HBasicBlock* GetDefaultBlock() const {
2913 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002914 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002915 }
2916 DECLARE_INSTRUCTION(PackedSwitch);
2917
2918 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002919 const int32_t start_value_;
2920 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002921
2922 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2923};
2924
Roland Levillain88cb1752014-10-20 16:36:47 +01002925class HUnaryOperation : public HExpression<1> {
2926 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002927 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2928 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002929 SetRawInputAt(0, input);
2930 }
2931
2932 HInstruction* GetInput() const { return InputAt(0); }
2933 Primitive::Type GetResultType() const { return GetType(); }
2934
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002935 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002936 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002937 return true;
2938 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002939
Roland Levillain31dd3d62016-02-16 12:21:02 +00002940 // Try to statically evaluate `this` and return a HConstant
2941 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002942 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002943 HConstant* TryStaticEvaluation() const;
2944
2945 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002946 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2947 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002948 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2949 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002950
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002951 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002952
2953 private:
2954 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2955};
2956
Dave Allison20dfc792014-06-16 20:44:29 -07002957class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002958 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002959 HBinaryOperation(Primitive::Type result_type,
2960 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002961 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002962 SideEffects side_effects = SideEffects::None(),
2963 uint32_t dex_pc = kNoDexPc)
2964 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002965 SetRawInputAt(0, left);
2966 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002967 }
2968
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002969 HInstruction* GetLeft() const { return InputAt(0); }
2970 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002971 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002972
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002973 virtual bool IsCommutative() const { return false; }
2974
2975 // Put constant on the right.
2976 // Returns whether order is changed.
2977 bool OrderInputsWithConstantOnTheRight() {
2978 HInstruction* left = InputAt(0);
2979 HInstruction* right = InputAt(1);
2980 if (left->IsConstant() && !right->IsConstant()) {
2981 ReplaceInput(right, 0);
2982 ReplaceInput(left, 1);
2983 return true;
2984 }
2985 return false;
2986 }
2987
2988 // Order inputs by instruction id, but favor constant on the right side.
2989 // This helps GVN for commutative ops.
2990 void OrderInputs() {
2991 DCHECK(IsCommutative());
2992 HInstruction* left = InputAt(0);
2993 HInstruction* right = InputAt(1);
2994 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2995 return;
2996 }
2997 if (OrderInputsWithConstantOnTheRight()) {
2998 return;
2999 }
3000 // Order according to instruction id.
3001 if (left->GetId() > right->GetId()) {
3002 ReplaceInput(right, 0);
3003 ReplaceInput(left, 1);
3004 }
3005 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003006
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003007 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003008 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003009 return true;
3010 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003011
Roland Levillain31dd3d62016-02-16 12:21:02 +00003012 // Try to statically evaluate `this` and return a HConstant
3013 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003014 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003015 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003016
3017 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003018 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3019 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003020 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3021 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003022 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003023 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3024 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003025 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3026 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003027 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3028 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003029 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003030 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3031 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003032
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003033 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003034 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003035 HConstant* GetConstantRight() const;
3036
3037 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003038 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003039 HInstruction* GetLeastConstantLeft() const;
3040
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003041 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003042
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003043 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003044 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3045};
3046
Mark Mendellc4701932015-04-10 13:18:51 -04003047// The comparison bias applies for floating point operations and indicates how NaN
3048// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003049enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003050 kNoBias, // bias is not applicable (i.e. for long operation)
3051 kGtBias, // return 1 for NaN comparisons
3052 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003053 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003054};
3055
Roland Levillain31dd3d62016-02-16 12:21:02 +00003056std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3057
Dave Allison20dfc792014-06-16 20:44:29 -07003058class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003059 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003060 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003061 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3062 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3063 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003064
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003065 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003066 // `instruction`, and disregard moves in between.
3067 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003068
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003069 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003070
3071 virtual IfCondition GetCondition() const = 0;
3072
Mark Mendellc4701932015-04-10 13:18:51 -04003073 virtual IfCondition GetOppositeCondition() const = 0;
3074
Vladimir Markoa1de9182016-02-25 11:37:38 +00003075 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003076 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3077
Vladimir Markoa1de9182016-02-25 11:37:38 +00003078 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3079 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003080
Vladimir Marko372f10e2016-05-17 16:30:10 +01003081 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003082 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003083 }
3084
Roland Levillain4fa13f62015-07-06 18:11:54 +01003085 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003086 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003087 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003088 if (if_cond == kCondNE) {
3089 return true;
3090 } else if (if_cond == kCondEQ) {
3091 return false;
3092 }
3093 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003094 }
3095
3096 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003097 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003098 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003099 if (if_cond == kCondEQ) {
3100 return true;
3101 } else if (if_cond == kCondNE) {
3102 return false;
3103 }
3104 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003105 }
3106
Roland Levillain31dd3d62016-02-16 12:21:02 +00003107 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003108 // Needed if we merge a HCompare into a HCondition.
3109 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3110 static constexpr size_t kFieldComparisonBiasSize =
3111 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3112 static constexpr size_t kNumberOfConditionPackedBits =
3113 kFieldComparisonBias + kFieldComparisonBiasSize;
3114 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3115 using ComparisonBiasField =
3116 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3117
Roland Levillain31dd3d62016-02-16 12:21:02 +00003118 template <typename T>
3119 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3120
3121 template <typename T>
3122 int32_t CompareFP(T x, T y) const {
3123 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3124 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3125 // Handle the bias.
3126 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3127 }
3128
3129 // Return an integer constant containing the result of a condition evaluated at compile time.
3130 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3131 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3132 }
3133
Dave Allison20dfc792014-06-16 20:44:29 -07003134 private:
3135 DISALLOW_COPY_AND_ASSIGN(HCondition);
3136};
3137
3138// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003139class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003140 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003141 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3142 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003143
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003144 bool IsCommutative() const OVERRIDE { return true; }
3145
Vladimir Marko9e23df52015-11-10 17:14:35 +00003146 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3147 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003148 return MakeConstantCondition(true, GetDexPc());
3149 }
3150 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3151 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3152 }
3153 // In the following Evaluate methods, a HCompare instruction has
3154 // been merged into this HEqual instruction; evaluate it as
3155 // `Compare(x, y) == 0`.
3156 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3157 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3158 GetDexPc());
3159 }
3160 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3161 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3162 }
3163 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3164 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003165 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003166
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003167 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003168
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003169 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003170 return kCondEQ;
3171 }
3172
Mark Mendellc4701932015-04-10 13:18:51 -04003173 IfCondition GetOppositeCondition() const OVERRIDE {
3174 return kCondNE;
3175 }
3176
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003177 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003178 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003179
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003180 DISALLOW_COPY_AND_ASSIGN(HEqual);
3181};
3182
Vladimir Markofcb503c2016-05-18 12:48:17 +01003183class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003184 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003185 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3186 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003187
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003188 bool IsCommutative() const OVERRIDE { return true; }
3189
Vladimir Marko9e23df52015-11-10 17:14:35 +00003190 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3191 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003192 return MakeConstantCondition(false, GetDexPc());
3193 }
3194 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3195 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3196 }
3197 // In the following Evaluate methods, a HCompare instruction has
3198 // been merged into this HNotEqual instruction; evaluate it as
3199 // `Compare(x, y) != 0`.
3200 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3201 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3202 }
3203 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3204 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3205 }
3206 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3207 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003208 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003209
Dave Allison20dfc792014-06-16 20:44:29 -07003210 DECLARE_INSTRUCTION(NotEqual);
3211
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003212 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003213 return kCondNE;
3214 }
3215
Mark Mendellc4701932015-04-10 13:18:51 -04003216 IfCondition GetOppositeCondition() const OVERRIDE {
3217 return kCondEQ;
3218 }
3219
Dave Allison20dfc792014-06-16 20:44:29 -07003220 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003221 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003222
Dave Allison20dfc792014-06-16 20:44:29 -07003223 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3224};
3225
Vladimir Markofcb503c2016-05-18 12:48:17 +01003226class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003227 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003228 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3229 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003230
Roland Levillain9867bc72015-08-05 10:21:34 +01003231 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003232 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003233 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003234 // In the following Evaluate methods, a HCompare instruction has
3235 // been merged into this HLessThan instruction; evaluate it as
3236 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003237 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003238 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3239 }
3240 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3241 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3242 }
3243 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3244 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003245 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003246
Dave Allison20dfc792014-06-16 20:44:29 -07003247 DECLARE_INSTRUCTION(LessThan);
3248
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003249 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003250 return kCondLT;
3251 }
3252
Mark Mendellc4701932015-04-10 13:18:51 -04003253 IfCondition GetOppositeCondition() const OVERRIDE {
3254 return kCondGE;
3255 }
3256
Dave Allison20dfc792014-06-16 20:44:29 -07003257 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003258 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003259
Dave Allison20dfc792014-06-16 20:44:29 -07003260 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3261};
3262
Vladimir Markofcb503c2016-05-18 12:48:17 +01003263class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003264 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003265 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3266 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003267
Roland Levillain9867bc72015-08-05 10:21:34 +01003268 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003269 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003270 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003271 // In the following Evaluate methods, a HCompare instruction has
3272 // been merged into this HLessThanOrEqual instruction; evaluate it as
3273 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003274 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003275 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3276 }
3277 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3278 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3279 }
3280 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3281 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003282 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003283
Dave Allison20dfc792014-06-16 20:44:29 -07003284 DECLARE_INSTRUCTION(LessThanOrEqual);
3285
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003286 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003287 return kCondLE;
3288 }
3289
Mark Mendellc4701932015-04-10 13:18:51 -04003290 IfCondition GetOppositeCondition() const OVERRIDE {
3291 return kCondGT;
3292 }
3293
Dave Allison20dfc792014-06-16 20:44:29 -07003294 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003295 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003296
Dave Allison20dfc792014-06-16 20:44:29 -07003297 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3298};
3299
Vladimir Markofcb503c2016-05-18 12:48:17 +01003300class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003301 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003302 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3303 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003304
Roland Levillain9867bc72015-08-05 10:21:34 +01003305 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003306 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003307 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003308 // In the following Evaluate methods, a HCompare instruction has
3309 // been merged into this HGreaterThan instruction; evaluate it as
3310 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003311 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003312 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3313 }
3314 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3315 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3316 }
3317 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3318 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003319 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003320
Dave Allison20dfc792014-06-16 20:44:29 -07003321 DECLARE_INSTRUCTION(GreaterThan);
3322
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003323 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003324 return kCondGT;
3325 }
3326
Mark Mendellc4701932015-04-10 13:18:51 -04003327 IfCondition GetOppositeCondition() const OVERRIDE {
3328 return kCondLE;
3329 }
3330
Dave Allison20dfc792014-06-16 20:44:29 -07003331 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003332 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003333
Dave Allison20dfc792014-06-16 20:44:29 -07003334 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3335};
3336
Vladimir Markofcb503c2016-05-18 12:48:17 +01003337class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003338 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003339 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3340 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003341
Roland Levillain9867bc72015-08-05 10:21:34 +01003342 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003343 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003344 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003345 // In the following Evaluate methods, a HCompare instruction has
3346 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3347 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003348 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003349 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3350 }
3351 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3352 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3353 }
3354 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3355 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003356 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003357
Dave Allison20dfc792014-06-16 20:44:29 -07003358 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3359
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003360 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003361 return kCondGE;
3362 }
3363
Mark Mendellc4701932015-04-10 13:18:51 -04003364 IfCondition GetOppositeCondition() const OVERRIDE {
3365 return kCondLT;
3366 }
3367
Dave Allison20dfc792014-06-16 20:44:29 -07003368 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003369 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003370
Dave Allison20dfc792014-06-16 20:44:29 -07003371 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3372};
3373
Vladimir Markofcb503c2016-05-18 12:48:17 +01003374class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003375 public:
3376 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3377 : HCondition(first, second, dex_pc) {}
3378
3379 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003380 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003381 }
3382 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003383 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3384 }
3385 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3386 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3387 LOG(FATAL) << DebugName() << " is not defined for float values";
3388 UNREACHABLE();
3389 }
3390 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3391 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3392 LOG(FATAL) << DebugName() << " is not defined for double values";
3393 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003394 }
3395
3396 DECLARE_INSTRUCTION(Below);
3397
3398 IfCondition GetCondition() const OVERRIDE {
3399 return kCondB;
3400 }
3401
3402 IfCondition GetOppositeCondition() const OVERRIDE {
3403 return kCondAE;
3404 }
3405
3406 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003407 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003408 return MakeUnsigned(x) < MakeUnsigned(y);
3409 }
Aart Bike9f37602015-10-09 11:15:55 -07003410
3411 DISALLOW_COPY_AND_ASSIGN(HBelow);
3412};
3413
Vladimir Markofcb503c2016-05-18 12:48:17 +01003414class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003415 public:
3416 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3417 : HCondition(first, second, dex_pc) {}
3418
3419 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003420 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003421 }
3422 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003423 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3424 }
3425 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3426 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3427 LOG(FATAL) << DebugName() << " is not defined for float values";
3428 UNREACHABLE();
3429 }
3430 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3431 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3432 LOG(FATAL) << DebugName() << " is not defined for double values";
3433 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003434 }
3435
3436 DECLARE_INSTRUCTION(BelowOrEqual);
3437
3438 IfCondition GetCondition() const OVERRIDE {
3439 return kCondBE;
3440 }
3441
3442 IfCondition GetOppositeCondition() const OVERRIDE {
3443 return kCondA;
3444 }
3445
3446 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003447 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003448 return MakeUnsigned(x) <= MakeUnsigned(y);
3449 }
Aart Bike9f37602015-10-09 11:15:55 -07003450
3451 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3452};
3453
Vladimir Markofcb503c2016-05-18 12:48:17 +01003454class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003455 public:
3456 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3457 : HCondition(first, second, dex_pc) {}
3458
3459 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003460 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003461 }
3462 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003463 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3464 }
3465 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3466 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3467 LOG(FATAL) << DebugName() << " is not defined for float values";
3468 UNREACHABLE();
3469 }
3470 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3471 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3472 LOG(FATAL) << DebugName() << " is not defined for double values";
3473 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003474 }
3475
3476 DECLARE_INSTRUCTION(Above);
3477
3478 IfCondition GetCondition() const OVERRIDE {
3479 return kCondA;
3480 }
3481
3482 IfCondition GetOppositeCondition() const OVERRIDE {
3483 return kCondBE;
3484 }
3485
3486 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003487 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003488 return MakeUnsigned(x) > MakeUnsigned(y);
3489 }
Aart Bike9f37602015-10-09 11:15:55 -07003490
3491 DISALLOW_COPY_AND_ASSIGN(HAbove);
3492};
3493
Vladimir Markofcb503c2016-05-18 12:48:17 +01003494class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003495 public:
3496 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3497 : HCondition(first, second, dex_pc) {}
3498
3499 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003500 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003501 }
3502 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003503 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3504 }
3505 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3506 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3507 LOG(FATAL) << DebugName() << " is not defined for float values";
3508 UNREACHABLE();
3509 }
3510 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3511 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3512 LOG(FATAL) << DebugName() << " is not defined for double values";
3513 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003514 }
3515
3516 DECLARE_INSTRUCTION(AboveOrEqual);
3517
3518 IfCondition GetCondition() const OVERRIDE {
3519 return kCondAE;
3520 }
3521
3522 IfCondition GetOppositeCondition() const OVERRIDE {
3523 return kCondB;
3524 }
3525
3526 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003527 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003528 return MakeUnsigned(x) >= MakeUnsigned(y);
3529 }
Aart Bike9f37602015-10-09 11:15:55 -07003530
3531 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3532};
Dave Allison20dfc792014-06-16 20:44:29 -07003533
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003534// Instruction to check how two inputs compare to each other.
3535// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003536class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003537 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003538 // Note that `comparison_type` is the type of comparison performed
3539 // between the comparison's inputs, not the type of the instantiated
3540 // HCompare instruction (which is always Primitive::kPrimInt).
3541 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003542 HInstruction* first,
3543 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003544 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003545 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003546 : HBinaryOperation(Primitive::kPrimInt,
3547 first,
3548 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003549 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003550 dex_pc) {
3551 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003552 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3553 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003554 }
3555
Roland Levillain9867bc72015-08-05 10:21:34 +01003556 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003557 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3558
3559 template <typename T>
3560 int32_t ComputeFP(T x, T y) const {
3561 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3562 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3563 // Handle the bias.
3564 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3565 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003566
Roland Levillain9867bc72015-08-05 10:21:34 +01003567 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003568 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3569 // reach this code path when processing a freshly built HIR
3570 // graph. However HCompare integer instructions can be synthesized
3571 // by the instruction simplifier to implement IntegerCompare and
3572 // IntegerSignum intrinsics, so we have to handle this case.
3573 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003574 }
3575 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003576 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3577 }
3578 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3579 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3580 }
3581 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3582 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003583 }
3584
Vladimir Marko372f10e2016-05-17 16:30:10 +01003585 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003586 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003587 }
3588
Vladimir Markoa1de9182016-02-25 11:37:38 +00003589 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003590
Roland Levillain31dd3d62016-02-16 12:21:02 +00003591 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003592 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003593 bool IsGtBias() const {
3594 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003595 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003596 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003597
Roland Levillain1693a1f2016-03-15 14:57:31 +00003598 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3599 // Comparisons do not require a runtime call in any back end.
3600 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003601 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003602
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003603 DECLARE_INSTRUCTION(Compare);
3604
Roland Levillain31dd3d62016-02-16 12:21:02 +00003605 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003606 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3607 static constexpr size_t kFieldComparisonBiasSize =
3608 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3609 static constexpr size_t kNumberOfComparePackedBits =
3610 kFieldComparisonBias + kFieldComparisonBiasSize;
3611 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3612 using ComparisonBiasField =
3613 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3614
Roland Levillain31dd3d62016-02-16 12:21:02 +00003615 // Return an integer constant containing the result of a comparison evaluated at compile time.
3616 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3617 DCHECK(value == -1 || value == 0 || value == 1) << value;
3618 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3619 }
3620
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003621 private:
3622 DISALLOW_COPY_AND_ASSIGN(HCompare);
3623};
3624
Vladimir Markofcb503c2016-05-18 12:48:17 +01003625class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003626 public:
3627 HNewInstance(HInstruction* cls,
3628 HCurrentMethod* current_method,
3629 uint32_t dex_pc,
3630 uint16_t type_index,
3631 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003632 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003633 bool finalizable,
3634 QuickEntrypointEnum entrypoint)
3635 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3636 type_index_(type_index),
3637 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003638 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003639 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003640 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003641 SetRawInputAt(0, cls);
3642 SetRawInputAt(1, current_method);
3643 }
3644
3645 uint16_t GetTypeIndex() const { return type_index_; }
3646 const DexFile& GetDexFile() const { return dex_file_; }
3647
3648 // Calls runtime so needs an environment.
3649 bool NeedsEnvironment() const OVERRIDE { return true; }
3650
Mingyao Yang062157f2016-03-02 10:15:36 -08003651 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3652 bool CanThrow() const OVERRIDE { return true; }
3653
3654 // Needs to call into runtime to make sure it's instantiable/accessible.
3655 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003656
Vladimir Markoa1de9182016-02-25 11:37:38 +00003657 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003658
3659 bool CanBeNull() const OVERRIDE { return false; }
3660
3661 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3662
3663 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3664 entrypoint_ = entrypoint;
3665 }
3666
3667 bool IsStringAlloc() const;
3668
3669 DECLARE_INSTRUCTION(NewInstance);
3670
3671 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003672 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3673 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003674 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3675 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3676 "Too many packed fields.");
3677
David Brazdil6de19382016-01-08 17:37:10 +00003678 const uint16_t type_index_;
3679 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003680 QuickEntrypointEnum entrypoint_;
3681
3682 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3683};
3684
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003685enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003686#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3687 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003688#include "intrinsics_list.h"
3689 kNone,
3690 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3691#undef INTRINSICS_LIST
3692#undef OPTIMIZING_INTRINSICS
3693};
3694std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3695
Agi Csaki05f20562015-08-19 14:58:14 -07003696enum IntrinsicNeedsEnvironmentOrCache {
3697 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3698 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003699};
3700
Aart Bik5d75afe2015-12-14 11:57:01 -08003701enum IntrinsicSideEffects {
3702 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3703 kReadSideEffects, // Intrinsic may read heap memory.
3704 kWriteSideEffects, // Intrinsic may write heap memory.
3705 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3706};
3707
3708enum IntrinsicExceptions {
3709 kNoThrow, // Intrinsic does not throw any exceptions.
3710 kCanThrow // Intrinsic may throw exceptions.
3711};
3712
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003713class HInvoke : public HInstruction {
3714 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003715 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003716
Vladimir Marko372f10e2016-05-17 16:30:10 +01003717 using HInstruction::GetInputRecords; // Keep the const version visible.
3718 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3719 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3720 }
3721
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003722 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003723 SetRawInputAt(index, argument);
3724 }
3725
Roland Levillain3e3d7332015-04-28 11:00:54 +01003726 // Return the number of arguments. This number can be lower than
3727 // the number of inputs returned by InputCount(), as some invoke
3728 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3729 // inputs at the end of their list of inputs.
3730 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3731
Vladimir Markoa1de9182016-02-25 11:37:38 +00003732 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003733
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003734 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003735 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003736
Vladimir Markoa1de9182016-02-25 11:37:38 +00003737 InvokeType GetOriginalInvokeType() const {
3738 return GetPackedField<OriginalInvokeTypeField>();
3739 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003740
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003741 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003742 return intrinsic_;
3743 }
3744
Aart Bik5d75afe2015-12-14 11:57:01 -08003745 void SetIntrinsic(Intrinsics intrinsic,
3746 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3747 IntrinsicSideEffects side_effects,
3748 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003749
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003750 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003751 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003752 }
3753
Vladimir Markoa1de9182016-02-25 11:37:38 +00003754 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003755
3756 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3757
Vladimir Marko372f10e2016-05-17 16:30:10 +01003758 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003759 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3760 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003761
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003762 uint32_t* GetIntrinsicOptimizations() {
3763 return &intrinsic_optimizations_;
3764 }
3765
3766 const uint32_t* GetIntrinsicOptimizations() const {
3767 return &intrinsic_optimizations_;
3768 }
3769
3770 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3771
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003772 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003773
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003774 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003775 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3776 static constexpr size_t kFieldOriginalInvokeTypeSize =
3777 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3778 static constexpr size_t kFieldReturnType =
3779 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3780 static constexpr size_t kFieldReturnTypeSize =
3781 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3782 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3783 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3784 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3785 using OriginalInvokeTypeField =
3786 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3787 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3788
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003789 HInvoke(ArenaAllocator* arena,
3790 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003791 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003792 Primitive::Type return_type,
3793 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003794 uint32_t dex_method_index,
3795 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003796 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003797 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003798 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003799 inputs_(number_of_arguments + number_of_other_inputs,
3800 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003801 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003802 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003803 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003804 SetPackedField<ReturnTypeField>(return_type);
3805 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3806 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003807 }
3808
Vladimir Markoa1de9182016-02-25 11:37:38 +00003809 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003810
Roland Levillain3e3d7332015-04-28 11:00:54 +01003811 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003812 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003813 const uint32_t dex_method_index_;
3814 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003815
3816 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3817 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003818
3819 private:
3820 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3821};
3822
Vladimir Markofcb503c2016-05-18 12:48:17 +01003823class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003824 public:
3825 HInvokeUnresolved(ArenaAllocator* arena,
3826 uint32_t number_of_arguments,
3827 Primitive::Type return_type,
3828 uint32_t dex_pc,
3829 uint32_t dex_method_index,
3830 InvokeType invoke_type)
3831 : HInvoke(arena,
3832 number_of_arguments,
3833 0u /* number_of_other_inputs */,
3834 return_type,
3835 dex_pc,
3836 dex_method_index,
3837 invoke_type) {
3838 }
3839
3840 DECLARE_INSTRUCTION(InvokeUnresolved);
3841
3842 private:
3843 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3844};
3845
Vladimir Markofcb503c2016-05-18 12:48:17 +01003846class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003847 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003848 // Requirements of this method call regarding the class
3849 // initialization (clinit) check of its declaring class.
3850 enum class ClinitCheckRequirement {
3851 kNone, // Class already initialized.
3852 kExplicit, // Static call having explicit clinit check as last input.
3853 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003854 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003855 };
3856
Vladimir Marko58155012015-08-19 12:49:41 +00003857 // Determines how to load the target ArtMethod*.
3858 enum class MethodLoadKind {
3859 // Use a String init ArtMethod* loaded from Thread entrypoints.
3860 kStringInit,
3861
3862 // Use the method's own ArtMethod* loaded by the register allocator.
3863 kRecursive,
3864
3865 // Use ArtMethod* at a known address, embed the direct address in the code.
3866 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3867 kDirectAddress,
3868
3869 // Use ArtMethod* at an address that will be known at link time, embed the direct
3870 // address in the code. If the image is relocatable, emit .patch_oat entry.
3871 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3872 // the image relocatable or not.
3873 kDirectAddressWithFixup,
3874
Vladimir Markoa1de9182016-02-25 11:37:38 +00003875 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003876 // Used when we need to use the dex cache, for example for invoke-static that
3877 // may cause class initialization (the entry may point to a resolution method),
3878 // and we know that we can access the dex cache arrays using a PC-relative load.
3879 kDexCachePcRelative,
3880
3881 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3882 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3883 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3884 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3885 kDexCacheViaMethod,
3886 };
3887
3888 // Determines the location of the code pointer.
3889 enum class CodePtrLocation {
3890 // Recursive call, use local PC-relative call instruction.
3891 kCallSelf,
3892
3893 // Use PC-relative call instruction patched at link time.
3894 // Used for calls within an oat file, boot->boot or app->app.
3895 kCallPCRelative,
3896
3897 // Call to a known target address, embed the direct address in code.
3898 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3899 kCallDirect,
3900
3901 // Call to a target address that will be known at link time, embed the direct
3902 // address in code. If the image is relocatable, emit .patch_oat entry.
3903 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3904 // the image relocatable or not.
3905 kCallDirectWithFixup,
3906
3907 // Use code pointer from the ArtMethod*.
3908 // Used when we don't know the target code. This is also the last-resort-kind used when
3909 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3910 kCallArtMethod,
3911 };
3912
3913 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003914 MethodLoadKind method_load_kind;
3915 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003916 // The method load data holds
3917 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3918 // Note that there are multiple string init methods, each having its own offset.
3919 // - the method address for kDirectAddress
3920 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003921 uint64_t method_load_data;
3922 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003923 };
3924
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003925 HInvokeStaticOrDirect(ArenaAllocator* arena,
3926 uint32_t number_of_arguments,
3927 Primitive::Type return_type,
3928 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003929 uint32_t method_index,
3930 MethodReference target_method,
3931 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003932 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003933 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003934 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003935 : HInvoke(arena,
3936 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003937 // There is potentially one extra argument for the HCurrentMethod node, and
3938 // potentially one other if the clinit check is explicit, and potentially
3939 // one other if the method is a string factory.
3940 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003941 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003942 return_type,
3943 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003944 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003945 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003946 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003947 dispatch_info_(dispatch_info) {
3948 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3949 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3950 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003951
Vladimir Markodc151b22015-10-15 18:02:30 +01003952 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003953 bool had_current_method_input = HasCurrentMethodInput();
3954 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3955
3956 // Using the current method is the default and once we find a better
3957 // method load kind, we should not go back to using the current method.
3958 DCHECK(had_current_method_input || !needs_current_method_input);
3959
3960 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003961 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3962 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003963 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003964 dispatch_info_ = dispatch_info;
3965 }
3966
Vladimir Markoc53c0792015-11-19 15:48:33 +00003967 void AddSpecialInput(HInstruction* input) {
3968 // We allow only one special input.
3969 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3970 DCHECK(InputCount() == GetSpecialInputIndex() ||
3971 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3972 InsertInputAt(GetSpecialInputIndex(), input);
3973 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003974
Vladimir Marko372f10e2016-05-17 16:30:10 +01003975 using HInstruction::GetInputRecords; // Keep the const version visible.
3976 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3977 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
3978 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
3979 DCHECK(!input_records.empty());
3980 DCHECK_GT(input_records.size(), GetNumberOfArguments());
3981 HInstruction* last_input = input_records.back().GetInstruction();
3982 // Note: `last_input` may be null during arguments setup.
3983 if (last_input != nullptr) {
3984 // `last_input` is the last input of a static invoke marked as having
3985 // an explicit clinit check. It must either be:
3986 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3987 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3988 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
3989 }
3990 }
3991 return input_records;
3992 }
3993
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003994 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003995 // We access the method via the dex cache so we can't do an implicit null check.
3996 // TODO: for intrinsics we can generate implicit null checks.
3997 return false;
3998 }
3999
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004000 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004001 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004002 }
4003
Vladimir Markoc53c0792015-11-19 15:48:33 +00004004 // Get the index of the special input, if any.
4005 //
David Brazdil6de19382016-01-08 17:37:10 +00004006 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4007 // method pointer; otherwise there may be one platform-specific special input,
4008 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004009 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004010 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004011
Vladimir Markoa1de9182016-02-25 11:37:38 +00004012 InvokeType GetOptimizedInvokeType() const {
4013 return GetPackedField<OptimizedInvokeTypeField>();
4014 }
4015
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004016 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004017 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004018 }
4019
Vladimir Marko58155012015-08-19 12:49:41 +00004020 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4021 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4022 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004023 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004024 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004025 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004026 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004027 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4028 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004029 bool HasCurrentMethodInput() const {
4030 // This function can be called only after the invoke has been fully initialized by the builder.
4031 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004032 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004033 return true;
4034 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004035 DCHECK(InputCount() == GetSpecialInputIndex() ||
4036 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004037 return false;
4038 }
4039 }
Vladimir Marko58155012015-08-19 12:49:41 +00004040 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4041 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004042 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004043
4044 int32_t GetStringInitOffset() const {
4045 DCHECK(IsStringInit());
4046 return dispatch_info_.method_load_data;
4047 }
4048
4049 uint64_t GetMethodAddress() const {
4050 DCHECK(HasMethodAddress());
4051 return dispatch_info_.method_load_data;
4052 }
4053
4054 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004055 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004056 return dispatch_info_.method_load_data;
4057 }
4058
4059 uint64_t GetDirectCodePtr() const {
4060 DCHECK(HasDirectCodePtr());
4061 return dispatch_info_.direct_code_ptr;
4062 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004063
Vladimir Markoa1de9182016-02-25 11:37:38 +00004064 ClinitCheckRequirement GetClinitCheckRequirement() const {
4065 return GetPackedField<ClinitCheckRequirementField>();
4066 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004067
Roland Levillain4c0eb422015-04-24 16:43:49 +01004068 // Is this instruction a call to a static method?
4069 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004070 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004071 }
4072
Vladimir Markofbb184a2015-11-13 14:47:00 +00004073 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4074 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4075 // instruction; only relevant for static calls with explicit clinit check.
4076 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004077 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004078 size_t last_input_index = inputs_.size() - 1u;
4079 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004080 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004081 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004082 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004083 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004084 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004085 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004086 }
4087
4088 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004089 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004090 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004091 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004092 }
4093
4094 // Is this a call to a static method whose declaring class has an
4095 // implicit intialization check requirement?
4096 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004097 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004098 }
4099
Vladimir Markob554b5a2015-11-06 12:57:55 +00004100 // Does this method load kind need the current method as an input?
4101 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4102 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4103 }
4104
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004105 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004106
Roland Levillain4c0eb422015-04-24 16:43:49 +01004107 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004108 void InsertInputAt(size_t index, HInstruction* input);
4109 void RemoveInputAt(size_t index);
4110
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004111 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004112 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4113 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4114 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4115 static constexpr size_t kFieldClinitCheckRequirement =
4116 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4117 static constexpr size_t kFieldClinitCheckRequirementSize =
4118 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4119 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4120 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4121 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4122 "Too many packed fields.");
4123 using OptimizedInvokeTypeField =
4124 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4125 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4126 kFieldClinitCheckRequirement,
4127 kFieldClinitCheckRequirementSize>;
4128
Vladimir Marko58155012015-08-19 12:49:41 +00004129 // The target method may refer to different dex file or method index than the original
4130 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4131 // in derived class to increase visibility.
4132 MethodReference target_method_;
4133 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004134
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004135 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004136};
Vladimir Markof64242a2015-12-01 14:58:23 +00004137std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004138std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004139
Vladimir Markofcb503c2016-05-18 12:48:17 +01004140class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004141 public:
4142 HInvokeVirtual(ArenaAllocator* arena,
4143 uint32_t number_of_arguments,
4144 Primitive::Type return_type,
4145 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004146 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004147 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004148 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004149 vtable_index_(vtable_index) {}
4150
Calin Juravle641547a2015-04-21 22:08:51 +01004151 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004152 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004153 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004154 }
4155
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004156 uint32_t GetVTableIndex() const { return vtable_index_; }
4157
4158 DECLARE_INSTRUCTION(InvokeVirtual);
4159
4160 private:
4161 const uint32_t vtable_index_;
4162
4163 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4164};
4165
Vladimir Markofcb503c2016-05-18 12:48:17 +01004166class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004167 public:
4168 HInvokeInterface(ArenaAllocator* arena,
4169 uint32_t number_of_arguments,
4170 Primitive::Type return_type,
4171 uint32_t dex_pc,
4172 uint32_t dex_method_index,
4173 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004174 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004175 imt_index_(imt_index) {}
4176
Calin Juravle641547a2015-04-21 22:08:51 +01004177 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004178 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004179 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004180 }
4181
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004182 uint32_t GetImtIndex() const { return imt_index_; }
4183 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4184
4185 DECLARE_INSTRUCTION(InvokeInterface);
4186
4187 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004188 const uint32_t imt_index_;
4189
4190 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4191};
4192
Vladimir Markofcb503c2016-05-18 12:48:17 +01004193class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004194 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004195 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004196 : HUnaryOperation(result_type, input, dex_pc) {
4197 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4198 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004199
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004200 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004201
4202 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004203 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004204 }
4205 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004206 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004207 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004208 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4209 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4210 }
4211 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4212 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4213 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004214
Roland Levillain88cb1752014-10-20 16:36:47 +01004215 DECLARE_INSTRUCTION(Neg);
4216
4217 private:
4218 DISALLOW_COPY_AND_ASSIGN(HNeg);
4219};
4220
Vladimir Markofcb503c2016-05-18 12:48:17 +01004221class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004222 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004223 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004224 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004225 uint32_t dex_pc,
4226 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004227 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004228 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004229 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004230 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004231 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004232 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004233 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004234 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004235 }
4236
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004237 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004238 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004239
4240 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004241 bool NeedsEnvironment() const OVERRIDE { return true; }
4242
Mingyao Yang0c365e62015-03-31 15:09:29 -07004243 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4244 bool CanThrow() const OVERRIDE { return true; }
4245
Calin Juravle10e244f2015-01-26 18:54:32 +00004246 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004247
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004248 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4249
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004250 DECLARE_INSTRUCTION(NewArray);
4251
4252 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004253 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004254 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004255 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004256
4257 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4258};
4259
Vladimir Markofcb503c2016-05-18 12:48:17 +01004260class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004261 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004262 HAdd(Primitive::Type result_type,
4263 HInstruction* left,
4264 HInstruction* right,
4265 uint32_t dex_pc = kNoDexPc)
4266 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004267
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004268 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004269
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004270 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004271
4272 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004273 return GetBlock()->GetGraph()->GetIntConstant(
4274 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004275 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004276 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004277 return GetBlock()->GetGraph()->GetLongConstant(
4278 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004279 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004280 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4281 return GetBlock()->GetGraph()->GetFloatConstant(
4282 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4283 }
4284 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4285 return GetBlock()->GetGraph()->GetDoubleConstant(
4286 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4287 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004288
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004289 DECLARE_INSTRUCTION(Add);
4290
4291 private:
4292 DISALLOW_COPY_AND_ASSIGN(HAdd);
4293};
4294
Vladimir Markofcb503c2016-05-18 12:48:17 +01004295class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004296 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004297 HSub(Primitive::Type result_type,
4298 HInstruction* left,
4299 HInstruction* right,
4300 uint32_t dex_pc = kNoDexPc)
4301 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004302
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004303 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004304
4305 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004306 return GetBlock()->GetGraph()->GetIntConstant(
4307 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004308 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004309 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004310 return GetBlock()->GetGraph()->GetLongConstant(
4311 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004312 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004313 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4314 return GetBlock()->GetGraph()->GetFloatConstant(
4315 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4316 }
4317 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4318 return GetBlock()->GetGraph()->GetDoubleConstant(
4319 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4320 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004321
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004322 DECLARE_INSTRUCTION(Sub);
4323
4324 private:
4325 DISALLOW_COPY_AND_ASSIGN(HSub);
4326};
4327
Vladimir Markofcb503c2016-05-18 12:48:17 +01004328class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004329 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004330 HMul(Primitive::Type result_type,
4331 HInstruction* left,
4332 HInstruction* right,
4333 uint32_t dex_pc = kNoDexPc)
4334 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004335
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004336 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004337
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004338 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004339
4340 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004341 return GetBlock()->GetGraph()->GetIntConstant(
4342 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004343 }
4344 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004345 return GetBlock()->GetGraph()->GetLongConstant(
4346 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004347 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004348 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4349 return GetBlock()->GetGraph()->GetFloatConstant(
4350 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4351 }
4352 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4353 return GetBlock()->GetGraph()->GetDoubleConstant(
4354 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4355 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004356
4357 DECLARE_INSTRUCTION(Mul);
4358
4359 private:
4360 DISALLOW_COPY_AND_ASSIGN(HMul);
4361};
4362
Vladimir Markofcb503c2016-05-18 12:48:17 +01004363class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004364 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004365 HDiv(Primitive::Type result_type,
4366 HInstruction* left,
4367 HInstruction* right,
4368 uint32_t dex_pc)
4369 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004370
Roland Levillain9867bc72015-08-05 10:21:34 +01004371 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004372 T ComputeIntegral(T x, T y) const {
4373 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004374 // Our graph structure ensures we never have 0 for `y` during
4375 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004376 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004377 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004378 return (y == -1) ? -x : x / y;
4379 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004380
Roland Levillain31dd3d62016-02-16 12:21:02 +00004381 template <typename T>
4382 T ComputeFP(T x, T y) const {
4383 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4384 return x / y;
4385 }
4386
Roland Levillain9867bc72015-08-05 10:21:34 +01004387 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004388 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004389 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004390 }
4391 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004392 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004393 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4394 }
4395 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4396 return GetBlock()->GetGraph()->GetFloatConstant(
4397 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4398 }
4399 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4400 return GetBlock()->GetGraph()->GetDoubleConstant(
4401 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004402 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004403
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004404 static SideEffects SideEffectsForArchRuntimeCalls() {
4405 // The generated code can use a runtime call.
4406 return SideEffects::CanTriggerGC();
4407 }
4408
Calin Juravle7c4954d2014-10-28 16:57:40 +00004409 DECLARE_INSTRUCTION(Div);
4410
4411 private:
4412 DISALLOW_COPY_AND_ASSIGN(HDiv);
4413};
4414
Vladimir Markofcb503c2016-05-18 12:48:17 +01004415class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004416 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004417 HRem(Primitive::Type result_type,
4418 HInstruction* left,
4419 HInstruction* right,
4420 uint32_t dex_pc)
4421 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004422
Roland Levillain9867bc72015-08-05 10:21:34 +01004423 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004424 T ComputeIntegral(T x, T y) const {
4425 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004426 // Our graph structure ensures we never have 0 for `y` during
4427 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004428 DCHECK_NE(y, 0);
4429 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4430 return (y == -1) ? 0 : x % y;
4431 }
4432
Roland Levillain31dd3d62016-02-16 12:21:02 +00004433 template <typename T>
4434 T ComputeFP(T x, T y) const {
4435 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4436 return std::fmod(x, y);
4437 }
4438
Roland Levillain9867bc72015-08-05 10:21:34 +01004439 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004440 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004441 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004442 }
4443 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004444 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004445 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004446 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004447 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4448 return GetBlock()->GetGraph()->GetFloatConstant(
4449 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4450 }
4451 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4452 return GetBlock()->GetGraph()->GetDoubleConstant(
4453 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4454 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004455
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004456 static SideEffects SideEffectsForArchRuntimeCalls() {
4457 return SideEffects::CanTriggerGC();
4458 }
4459
Calin Juravlebacfec32014-11-14 15:54:36 +00004460 DECLARE_INSTRUCTION(Rem);
4461
4462 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004463 DISALLOW_COPY_AND_ASSIGN(HRem);
4464};
4465
Vladimir Markofcb503c2016-05-18 12:48:17 +01004466class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004467 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004468 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4469 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004470 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004471 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004472 SetRawInputAt(0, value);
4473 }
4474
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004475 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4476
Calin Juravled0d48522014-11-04 16:40:20 +00004477 bool CanBeMoved() const OVERRIDE { return true; }
4478
Vladimir Marko372f10e2016-05-17 16:30:10 +01004479 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004480 return true;
4481 }
4482
4483 bool NeedsEnvironment() const OVERRIDE { return true; }
4484 bool CanThrow() const OVERRIDE { return true; }
4485
Calin Juravled0d48522014-11-04 16:40:20 +00004486 DECLARE_INSTRUCTION(DivZeroCheck);
4487
4488 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004489 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4490};
4491
Vladimir Markofcb503c2016-05-18 12:48:17 +01004492class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004493 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004494 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004495 HInstruction* value,
4496 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004497 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004498 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4499 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4500 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004501 }
4502
Roland Levillain5b5b9312016-03-22 14:57:31 +00004503 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004504 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004505 return value << (distance & max_shift_distance);
4506 }
4507
4508 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004509 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004510 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004511 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004512 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004513 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004514 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004515 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004516 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4517 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4518 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4519 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004520 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004521 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4522 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004523 LOG(FATAL) << DebugName() << " is not defined for float values";
4524 UNREACHABLE();
4525 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004526 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4527 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004528 LOG(FATAL) << DebugName() << " is not defined for double values";
4529 UNREACHABLE();
4530 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004531
4532 DECLARE_INSTRUCTION(Shl);
4533
4534 private:
4535 DISALLOW_COPY_AND_ASSIGN(HShl);
4536};
4537
Vladimir Markofcb503c2016-05-18 12:48:17 +01004538class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004539 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004540 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004541 HInstruction* value,
4542 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004543 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004544 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4545 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4546 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004547 }
4548
Roland Levillain5b5b9312016-03-22 14:57:31 +00004549 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004550 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004551 return value >> (distance & max_shift_distance);
4552 }
4553
4554 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004555 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004556 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004557 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004558 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004559 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004560 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004561 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004562 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4563 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4564 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4565 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004566 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004567 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4568 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004569 LOG(FATAL) << DebugName() << " is not defined for float values";
4570 UNREACHABLE();
4571 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004572 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4573 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004574 LOG(FATAL) << DebugName() << " is not defined for double values";
4575 UNREACHABLE();
4576 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004577
4578 DECLARE_INSTRUCTION(Shr);
4579
4580 private:
4581 DISALLOW_COPY_AND_ASSIGN(HShr);
4582};
4583
Vladimir Markofcb503c2016-05-18 12:48:17 +01004584class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004585 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004586 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004587 HInstruction* value,
4588 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004589 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004590 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4591 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4592 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004593 }
4594
Roland Levillain5b5b9312016-03-22 14:57:31 +00004595 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004596 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004597 typedef typename std::make_unsigned<T>::type V;
4598 V ux = static_cast<V>(value);
4599 return static_cast<T>(ux >> (distance & max_shift_distance));
4600 }
4601
4602 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004603 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004604 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004605 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004606 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004607 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004608 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004609 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004610 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4611 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4612 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4613 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004614 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004615 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4616 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004617 LOG(FATAL) << DebugName() << " is not defined for float values";
4618 UNREACHABLE();
4619 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004620 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4621 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004622 LOG(FATAL) << DebugName() << " is not defined for double values";
4623 UNREACHABLE();
4624 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004625
4626 DECLARE_INSTRUCTION(UShr);
4627
4628 private:
4629 DISALLOW_COPY_AND_ASSIGN(HUShr);
4630};
4631
Vladimir Markofcb503c2016-05-18 12:48:17 +01004632class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004633 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004634 HAnd(Primitive::Type result_type,
4635 HInstruction* left,
4636 HInstruction* right,
4637 uint32_t dex_pc = kNoDexPc)
4638 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004639
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004640 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004641
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004642 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004643
4644 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004645 return GetBlock()->GetGraph()->GetIntConstant(
4646 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004647 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004648 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004649 return GetBlock()->GetGraph()->GetLongConstant(
4650 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004651 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004652 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4653 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4654 LOG(FATAL) << DebugName() << " is not defined for float values";
4655 UNREACHABLE();
4656 }
4657 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4658 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4659 LOG(FATAL) << DebugName() << " is not defined for double values";
4660 UNREACHABLE();
4661 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004662
4663 DECLARE_INSTRUCTION(And);
4664
4665 private:
4666 DISALLOW_COPY_AND_ASSIGN(HAnd);
4667};
4668
Vladimir Markofcb503c2016-05-18 12:48:17 +01004669class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004670 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004671 HOr(Primitive::Type result_type,
4672 HInstruction* left,
4673 HInstruction* right,
4674 uint32_t dex_pc = kNoDexPc)
4675 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004676
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004677 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004678
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004679 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004680
4681 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004682 return GetBlock()->GetGraph()->GetIntConstant(
4683 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004684 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004685 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004686 return GetBlock()->GetGraph()->GetLongConstant(
4687 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004688 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004689 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4690 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4691 LOG(FATAL) << DebugName() << " is not defined for float values";
4692 UNREACHABLE();
4693 }
4694 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4695 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4696 LOG(FATAL) << DebugName() << " is not defined for double values";
4697 UNREACHABLE();
4698 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004699
4700 DECLARE_INSTRUCTION(Or);
4701
4702 private:
4703 DISALLOW_COPY_AND_ASSIGN(HOr);
4704};
4705
Vladimir Markofcb503c2016-05-18 12:48:17 +01004706class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004707 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004708 HXor(Primitive::Type result_type,
4709 HInstruction* left,
4710 HInstruction* right,
4711 uint32_t dex_pc = kNoDexPc)
4712 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004713
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004714 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004715
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004716 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004717
4718 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004719 return GetBlock()->GetGraph()->GetIntConstant(
4720 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004721 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004722 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004723 return GetBlock()->GetGraph()->GetLongConstant(
4724 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004725 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004726 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4727 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4728 LOG(FATAL) << DebugName() << " is not defined for float values";
4729 UNREACHABLE();
4730 }
4731 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4732 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4733 LOG(FATAL) << DebugName() << " is not defined for double values";
4734 UNREACHABLE();
4735 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004736
4737 DECLARE_INSTRUCTION(Xor);
4738
4739 private:
4740 DISALLOW_COPY_AND_ASSIGN(HXor);
4741};
4742
Vladimir Markofcb503c2016-05-18 12:48:17 +01004743class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004744 public:
4745 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004746 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004747 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4748 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004749 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004750
Roland Levillain5b5b9312016-03-22 14:57:31 +00004751 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004752 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004753 typedef typename std::make_unsigned<T>::type V;
4754 V ux = static_cast<V>(value);
4755 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004756 return static_cast<T>(ux);
4757 } else {
4758 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004759 return static_cast<T>(ux >> (distance & max_shift_value)) |
4760 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004761 }
4762 }
4763
Roland Levillain5b5b9312016-03-22 14:57:31 +00004764 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004765 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004766 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004767 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004768 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004769 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004770 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004771 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004772 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4773 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4774 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4775 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004776 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004777 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4778 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004779 LOG(FATAL) << DebugName() << " is not defined for float values";
4780 UNREACHABLE();
4781 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004782 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4783 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004784 LOG(FATAL) << DebugName() << " is not defined for double values";
4785 UNREACHABLE();
4786 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004787
4788 DECLARE_INSTRUCTION(Ror);
4789
4790 private:
4791 DISALLOW_COPY_AND_ASSIGN(HRor);
4792};
4793
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004794// The value of a parameter in this method. Its location depends on
4795// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004796class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004797 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004798 HParameterValue(const DexFile& dex_file,
4799 uint16_t type_index,
4800 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004801 Primitive::Type parameter_type,
4802 bool is_this = false)
4803 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004804 dex_file_(dex_file),
4805 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004806 index_(index) {
4807 SetPackedFlag<kFlagIsThis>(is_this);
4808 SetPackedFlag<kFlagCanBeNull>(!is_this);
4809 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004810
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004811 const DexFile& GetDexFile() const { return dex_file_; }
4812 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004813 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004814 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004815
Vladimir Markoa1de9182016-02-25 11:37:38 +00004816 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4817 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004818
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004819 DECLARE_INSTRUCTION(ParameterValue);
4820
4821 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004822 // Whether or not the parameter value corresponds to 'this' argument.
4823 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4824 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4825 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4826 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4827 "Too many packed fields.");
4828
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004829 const DexFile& dex_file_;
4830 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004831 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004832 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004833 const uint8_t index_;
4834
4835 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4836};
4837
Vladimir Markofcb503c2016-05-18 12:48:17 +01004838class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004839 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004840 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4841 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004842
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004843 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004844 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004845 return true;
4846 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004847
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004848 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004849
4850 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004851 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004852 }
4853 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004854 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004855 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004856 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4857 LOG(FATAL) << DebugName() << " is not defined for float values";
4858 UNREACHABLE();
4859 }
4860 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4861 LOG(FATAL) << DebugName() << " is not defined for double values";
4862 UNREACHABLE();
4863 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004864
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004865 DECLARE_INSTRUCTION(Not);
4866
4867 private:
4868 DISALLOW_COPY_AND_ASSIGN(HNot);
4869};
4870
Vladimir Markofcb503c2016-05-18 12:48:17 +01004871class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004872 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004873 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4874 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004875
4876 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004877 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004878 return true;
4879 }
4880
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004881 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004882 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004883 return !x;
4884 }
4885
Roland Levillain9867bc72015-08-05 10:21:34 +01004886 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004887 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004888 }
4889 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4890 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004891 UNREACHABLE();
4892 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004893 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4894 LOG(FATAL) << DebugName() << " is not defined for float values";
4895 UNREACHABLE();
4896 }
4897 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4898 LOG(FATAL) << DebugName() << " is not defined for double values";
4899 UNREACHABLE();
4900 }
David Brazdil66d126e2015-04-03 16:02:44 +01004901
4902 DECLARE_INSTRUCTION(BooleanNot);
4903
4904 private:
4905 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4906};
4907
Vladimir Markofcb503c2016-05-18 12:48:17 +01004908class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004909 public:
4910 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004911 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004912 : HExpression(result_type,
4913 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4914 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004915 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004916 // Invariant: We should never generate a conversion to a Boolean value.
4917 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004918 }
4919
4920 HInstruction* GetInput() const { return InputAt(0); }
4921 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4922 Primitive::Type GetResultType() const { return GetType(); }
4923
4924 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004925 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4926 return true;
4927 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004928
Mark Mendelle82549b2015-05-06 10:55:34 -04004929 // Try to statically evaluate the conversion and return a HConstant
4930 // containing the result. If the input cannot be converted, return nullptr.
4931 HConstant* TryStaticEvaluation() const;
4932
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004933 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4934 Primitive::Type result_type) {
4935 // Some architectures may not require the 'GC' side effects, but at this point
4936 // in the compilation process we do not know what architecture we will
4937 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004938 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4939 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004940 return SideEffects::CanTriggerGC();
4941 }
4942 return SideEffects::None();
4943 }
4944
Roland Levillaindff1f282014-11-05 14:15:05 +00004945 DECLARE_INSTRUCTION(TypeConversion);
4946
4947 private:
4948 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4949};
4950
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004951static constexpr uint32_t kNoRegNumber = -1;
4952
Vladimir Markofcb503c2016-05-18 12:48:17 +01004953class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004954 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004955 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4956 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004957 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004958 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004959 SetRawInputAt(0, value);
4960 }
4961
Calin Juravle10e244f2015-01-26 18:54:32 +00004962 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004963 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004964 return true;
4965 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004966
Calin Juravle10e244f2015-01-26 18:54:32 +00004967 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004968
Calin Juravle10e244f2015-01-26 18:54:32 +00004969 bool CanThrow() const OVERRIDE { return true; }
4970
4971 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004972
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004973
4974 DECLARE_INSTRUCTION(NullCheck);
4975
4976 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004977 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4978};
4979
4980class FieldInfo : public ValueObject {
4981 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004982 FieldInfo(MemberOffset field_offset,
4983 Primitive::Type field_type,
4984 bool is_volatile,
4985 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004986 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004987 const DexFile& dex_file,
4988 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004989 : field_offset_(field_offset),
4990 field_type_(field_type),
4991 is_volatile_(is_volatile),
4992 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004993 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004994 dex_file_(dex_file),
4995 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004996
4997 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004998 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004999 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005000 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005001 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005002 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005003 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005004
5005 private:
5006 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005007 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005008 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005009 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005010 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005011 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005012 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005013};
5014
Vladimir Markofcb503c2016-05-18 12:48:17 +01005015class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005016 public:
5017 HInstanceFieldGet(HInstruction* value,
5018 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005019 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005020 bool is_volatile,
5021 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005022 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005023 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005024 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005025 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005026 : HExpression(field_type,
Serban Constantinescufca16662016-07-14 09:21:59 +01005027 SideEffectsForArchRuntimeCalls(field_type, is_volatile),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005028 dex_pc),
5029 field_info_(field_offset,
5030 field_type,
5031 is_volatile,
5032 field_idx,
5033 declaring_class_def_index,
5034 dex_file,
5035 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005036 SetRawInputAt(0, value);
5037 }
5038
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005039 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005040
Vladimir Marko372f10e2016-05-17 16:30:10 +01005041 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5042 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005043 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005044 }
5045
Calin Juravle641547a2015-04-21 22:08:51 +01005046 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005047 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005048 }
5049
5050 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005051 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5052 }
5053
Calin Juravle52c48962014-12-16 17:02:57 +00005054 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005055 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005056 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005057 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005058
Serban Constantinescufca16662016-07-14 09:21:59 +01005059 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type field_type, bool is_volatile) {
5060 SideEffects side_effects = SideEffects::FieldReadOfType(field_type, is_volatile);
5061
5062 // MIPS delegates volatile kPrimLong and kPrimDouble loads to a runtime helper.
5063 if (Primitive::Is64BitType(field_type)) {
5064 side_effects.Add(SideEffects::CanTriggerGC());
5065 }
5066 return side_effects;
5067 }
5068
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005069 DECLARE_INSTRUCTION(InstanceFieldGet);
5070
5071 private:
5072 const FieldInfo field_info_;
5073
5074 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5075};
5076
Vladimir Markofcb503c2016-05-18 12:48:17 +01005077class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005078 public:
5079 HInstanceFieldSet(HInstruction* object,
5080 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005081 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005082 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005083 bool is_volatile,
5084 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005085 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005086 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005087 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005088 uint32_t dex_pc)
Serban Constantinescufca16662016-07-14 09:21:59 +01005089 : HTemplateInstruction(SideEffectsForArchRuntimeCalls(field_type, is_volatile),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005090 dex_pc),
5091 field_info_(field_offset,
5092 field_type,
5093 is_volatile,
5094 field_idx,
5095 declaring_class_def_index,
5096 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005097 dex_cache) {
5098 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005099 SetRawInputAt(0, object);
5100 SetRawInputAt(1, value);
5101 }
5102
Calin Juravle641547a2015-04-21 22:08:51 +01005103 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005104 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005105 }
5106
Calin Juravle52c48962014-12-16 17:02:57 +00005107 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005108 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005109 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005110 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005111 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005112 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5113 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005114
Serban Constantinescufca16662016-07-14 09:21:59 +01005115 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type field_type, bool is_volatile) {
5116 SideEffects side_effects = SideEffects::FieldWriteOfType(field_type, is_volatile);
5117
5118 // MIPS delegates volatile kPrimLong and kPrimDouble stores to a runtime helper.
5119 if (Primitive::Is64BitType(field_type)) {
5120 side_effects.Add(SideEffects::CanTriggerGC());
5121 }
5122 return side_effects;
5123 }
5124
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005125 DECLARE_INSTRUCTION(InstanceFieldSet);
5126
5127 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005128 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5129 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5130 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5131 "Too many packed fields.");
5132
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005133 const FieldInfo field_info_;
5134
5135 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5136};
5137
Vladimir Markofcb503c2016-05-18 12:48:17 +01005138class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005139 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005140 HArrayGet(HInstruction* array,
5141 HInstruction* index,
5142 Primitive::Type type,
5143 uint32_t dex_pc,
5144 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005145 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005146 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005147 SetRawInputAt(0, array);
5148 SetRawInputAt(1, index);
5149 }
5150
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005151 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005152 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005153 return true;
5154 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005155 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005156 // TODO: We can be smarter here.
5157 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5158 // which generates the implicit null check. There are cases when these can be removed
5159 // to produce better code. If we ever add optimizations to do so we should allow an
5160 // implicit check here (as long as the address falls in the first page).
5161 return false;
5162 }
5163
David Brazdil4833f5a2015-12-16 10:37:39 +00005164 bool IsEquivalentOf(HArrayGet* other) const {
5165 bool result = (GetDexPc() == other->GetDexPc());
5166 if (kIsDebugBuild && result) {
5167 DCHECK_EQ(GetBlock(), other->GetBlock());
5168 DCHECK_EQ(GetArray(), other->GetArray());
5169 DCHECK_EQ(GetIndex(), other->GetIndex());
5170 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005171 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005172 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005173 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5174 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005175 }
5176 }
5177 return result;
5178 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005179
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005180 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5181
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005182 HInstruction* GetArray() const { return InputAt(0); }
5183 HInstruction* GetIndex() const { return InputAt(1); }
5184
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005185 DECLARE_INSTRUCTION(ArrayGet);
5186
5187 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005188 // We treat a String as an array, creating the HArrayGet from String.charAt()
5189 // intrinsic in the instruction simplifier. We can always determine whether
5190 // a particular HArrayGet is actually a String.charAt() by looking at the type
5191 // of the input but that requires holding the mutator lock, so we prefer to use
5192 // a flag, so that code generators don't need to do the locking.
5193 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5194 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5195 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5196 "Too many packed fields.");
5197
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005198 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5199};
5200
Vladimir Markofcb503c2016-05-18 12:48:17 +01005201class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005202 public:
5203 HArraySet(HInstruction* array,
5204 HInstruction* index,
5205 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005206 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005207 uint32_t dex_pc)
5208 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005209 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5210 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5211 SetPackedFlag<kFlagValueCanBeNull>(true);
5212 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005213 SetRawInputAt(0, array);
5214 SetRawInputAt(1, index);
5215 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005216 // Make a best guess now, may be refined during SSA building.
5217 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005218 }
5219
Calin Juravle77520bc2015-01-12 18:45:46 +00005220 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005221 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005222 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005223 }
5224
Mingyao Yang81014cb2015-06-02 03:16:27 -07005225 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005226 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005227
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005228 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005229 // TODO: Same as for ArrayGet.
5230 return false;
5231 }
5232
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005233 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005234 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005235 }
5236
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005237 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005238 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005239 }
5240
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005241 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005242 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005243 }
5244
Vladimir Markoa1de9182016-02-25 11:37:38 +00005245 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5246 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5247 bool StaticTypeOfArrayIsObjectArray() const {
5248 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5249 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005250
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005251 HInstruction* GetArray() const { return InputAt(0); }
5252 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005253 HInstruction* GetValue() const { return InputAt(2); }
5254
5255 Primitive::Type GetComponentType() const {
5256 // The Dex format does not type floating point index operations. Since the
5257 // `expected_component_type_` is set during building and can therefore not
5258 // be correct, we also check what is the value type. If it is a floating
5259 // point type, we must use that type.
5260 Primitive::Type value_type = GetValue()->GetType();
5261 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5262 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005263 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005264 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005265
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005266 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005267 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005268 }
5269
Aart Bik18b36ab2016-04-13 16:41:35 -07005270 void ComputeSideEffects() {
5271 Primitive::Type type = GetComponentType();
5272 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5273 SideEffectsForArchRuntimeCalls(type)));
5274 }
5275
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005276 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5277 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5278 }
5279
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005280 DECLARE_INSTRUCTION(ArraySet);
5281
5282 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005283 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5284 static constexpr size_t kFieldExpectedComponentTypeSize =
5285 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5286 static constexpr size_t kFlagNeedsTypeCheck =
5287 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5288 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005289 // Cached information for the reference_type_info_ so that codegen
5290 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005291 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5292 static constexpr size_t kNumberOfArraySetPackedBits =
5293 kFlagStaticTypeOfArrayIsObjectArray + 1;
5294 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5295 using ExpectedComponentTypeField =
5296 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005297
5298 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5299};
5300
Vladimir Markofcb503c2016-05-18 12:48:17 +01005301class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005302 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005303 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005304 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005305 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005306 // Note that arrays do not change length, so the instruction does not
5307 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005308 SetRawInputAt(0, array);
5309 }
5310
Calin Juravle77520bc2015-01-12 18:45:46 +00005311 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005312 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005313 return true;
5314 }
Calin Juravle641547a2015-04-21 22:08:51 +01005315 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5316 return obj == InputAt(0);
5317 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005318
Vladimir Markodce016e2016-04-28 13:10:02 +01005319 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5320
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005321 DECLARE_INSTRUCTION(ArrayLength);
5322
5323 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005324 // We treat a String as an array, creating the HArrayLength from String.length()
5325 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5326 // determine whether a particular HArrayLength is actually a String.length() by
5327 // looking at the type of the input but that requires holding the mutator lock, so
5328 // we prefer to use a flag, so that code generators don't need to do the locking.
5329 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5330 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5331 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5332 "Too many packed fields.");
5333
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005334 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5335};
5336
Vladimir Markofcb503c2016-05-18 12:48:17 +01005337class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005338 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005339 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5340 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005341 HBoundsCheck(HInstruction* index,
5342 HInstruction* length,
5343 uint32_t dex_pc,
5344 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5345 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5346 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005347 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005348 SetRawInputAt(0, index);
5349 SetRawInputAt(1, length);
5350 }
5351
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005352 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005353 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005354 return true;
5355 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005356
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005357 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005358
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005359 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005360
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005361 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5362 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5363
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005364 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005365
5366 DECLARE_INSTRUCTION(BoundsCheck);
5367
5368 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005369 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5370 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5371 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5372 // so we need to create an HEnvironment which will be translated to an InlineInfo
5373 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5374 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5375 // from the invoke as we don't want to look again at the dex bytecode.
5376 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5377
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005378 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5379};
5380
Vladimir Markofcb503c2016-05-18 12:48:17 +01005381class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005382 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005383 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005384 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005385
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005386 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005387 return true;
5388 }
5389
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005390 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5391 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005392
5393 DECLARE_INSTRUCTION(SuspendCheck);
5394
5395 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005396 // Only used for code generation, in order to share the same slow path between back edges
5397 // of a same loop.
5398 SlowPathCode* slow_path_;
5399
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005400 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5401};
5402
David Srbecky0cf44932015-12-09 14:09:59 +00005403// Pseudo-instruction which provides the native debugger with mapping information.
5404// It ensures that we can generate line number and local variables at this point.
5405class HNativeDebugInfo : public HTemplateInstruction<0> {
5406 public:
5407 explicit HNativeDebugInfo(uint32_t dex_pc)
5408 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5409
5410 bool NeedsEnvironment() const OVERRIDE {
5411 return true;
5412 }
5413
5414 DECLARE_INSTRUCTION(NativeDebugInfo);
5415
5416 private:
5417 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5418};
5419
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005420/**
5421 * Instruction to load a Class object.
5422 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005423class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005424 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005425 // Determines how to load the Class.
5426 enum class LoadKind {
5427 // Use the Class* from the method's own ArtMethod*.
5428 kReferrersClass,
5429
5430 // Use boot image Class* address that will be known at link time.
5431 // Used for boot image classes referenced by boot image code in non-PIC mode.
5432 kBootImageLinkTimeAddress,
5433
5434 // Use PC-relative boot image Class* address that will be known at link time.
5435 // Used for boot image classes referenced by boot image code in PIC mode.
5436 kBootImageLinkTimePcRelative,
5437
5438 // Use a known boot image Class* address, embedded in the code by the codegen.
5439 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5440 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5441 // GetIncludePatchInformation().
5442 kBootImageAddress,
5443
5444 // Load from the resolved types array at an absolute address.
5445 // Used for classes outside the boot image referenced by JIT-compiled code.
5446 kDexCacheAddress,
5447
5448 // Load from resolved types array in the dex cache using a PC-relative load.
5449 // Used for classes outside boot image when we know that we can access
5450 // the dex cache arrays using a PC-relative load.
5451 kDexCachePcRelative,
5452
5453 // Load from resolved types array accessed through the class loaded from
5454 // the compiled method's own ArtMethod*. This is the default access type when
5455 // all other types are unavailable.
5456 kDexCacheViaMethod,
5457
5458 kLast = kDexCacheViaMethod
5459 };
5460
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005461 HLoadClass(HCurrentMethod* current_method,
5462 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005463 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005464 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005465 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005466 bool needs_access_check,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005467 bool is_in_dex_cache,
5468 bool is_in_boot_image)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005469 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5470 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005471 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005472 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005473 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005474 // Referrers class should not need access check. We never inline unverified
5475 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005476 DCHECK(!is_referrers_class || !needs_access_check);
5477
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005478 SetPackedField<LoadKindField>(
5479 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005480 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5481 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005482 SetPackedFlag<kFlagIsInBootImage>(is_in_boot_image);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005483 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005484 }
5485
5486 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5487 DCHECK(HasAddress(load_kind));
5488 load_data_.address = address;
5489 SetLoadKindInternal(load_kind);
5490 }
5491
5492 void SetLoadKindWithTypeReference(LoadKind load_kind,
5493 const DexFile& dex_file,
5494 uint32_t type_index) {
5495 DCHECK(HasTypeReference(load_kind));
5496 DCHECK(IsSameDexFile(dex_file_, dex_file));
5497 DCHECK_EQ(type_index_, type_index);
5498 SetLoadKindInternal(load_kind);
5499 }
5500
5501 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5502 const DexFile& dex_file,
5503 uint32_t element_index) {
5504 DCHECK(HasDexCacheReference(load_kind));
5505 DCHECK(IsSameDexFile(dex_file_, dex_file));
5506 load_data_.dex_cache_element_index = element_index;
5507 SetLoadKindInternal(load_kind);
5508 }
5509
5510 LoadKind GetLoadKind() const {
5511 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005512 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005513
5514 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005515
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005516 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005517
5518 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5519
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005520 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005521
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005522 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005523 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005524 }
5525
Calin Juravle0ba218d2015-05-19 18:46:01 +01005526 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005527 // The entrypoint the code generator is going to call does not do
5528 // clinit of the class.
5529 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005530 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005531 }
5532
5533 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005534 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005535 (!IsReferrersClass() && !IsInDexCache()) ||
5536 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005537 }
5538
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005539
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005540 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005541 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005542 }
5543
Calin Juravleacf735c2015-02-12 15:25:22 +00005544 ReferenceTypeInfo GetLoadedClassRTI() {
5545 return loaded_class_rti_;
5546 }
5547
5548 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5549 // Make sure we only set exact types (the loaded class should never be merged).
5550 DCHECK(rti.IsExact());
5551 loaded_class_rti_ = rti;
5552 }
5553
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005554 uint32_t GetTypeIndex() const { return type_index_; }
5555 const DexFile& GetDexFile() const { return dex_file_; }
5556
5557 uint32_t GetDexCacheElementOffset() const;
5558
5559 uint64_t GetAddress() const {
5560 DCHECK(HasAddress(GetLoadKind()));
5561 return load_data_.address;
5562 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005563
Vladimir Markoa1de9182016-02-25 11:37:38 +00005564 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005565
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005566 static SideEffects SideEffectsForArchRuntimeCalls() {
5567 return SideEffects::CanTriggerGC();
5568 }
5569
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005570 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005571 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5572 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005573 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005574 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005575
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005576 void MarkInDexCache() {
5577 SetPackedFlag<kFlagIsInDexCache>(true);
5578 DCHECK(!NeedsEnvironment());
5579 RemoveEnvironment();
5580 SetSideEffects(SideEffects::None());
5581 }
5582
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005583 void MarkInBootImage() {
5584 SetPackedFlag<kFlagIsInBootImage>(true);
5585 }
5586
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005587 void AddSpecialInput(HInstruction* special_input);
5588
5589 using HInstruction::GetInputRecords; // Keep the const version visible.
5590 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5591 return ArrayRef<HUserRecord<HInstruction*>>(
5592 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5593 }
5594
5595 Primitive::Type GetType() const OVERRIDE {
5596 return Primitive::kPrimNot;
5597 }
5598
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005599 DECLARE_INSTRUCTION(LoadClass);
5600
5601 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005602 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005603 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005604 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005605 // Whether this instruction must generate the initialization check.
5606 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005607 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005608 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5609 static constexpr size_t kFieldLoadKindSize =
5610 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5611 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005612 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005613 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5614
5615 static bool HasTypeReference(LoadKind load_kind) {
5616 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5617 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5618 load_kind == LoadKind::kDexCacheViaMethod ||
5619 load_kind == LoadKind::kReferrersClass;
5620 }
5621
5622 static bool HasAddress(LoadKind load_kind) {
5623 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5624 }
5625
5626 static bool HasDexCacheReference(LoadKind load_kind) {
5627 return load_kind == LoadKind::kDexCachePcRelative;
5628 }
5629
5630 void SetLoadKindInternal(LoadKind load_kind);
5631
5632 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5633 // For other load kinds it's empty or possibly some architecture-specific instruction
5634 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5635 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005636
5637 const uint16_t type_index_;
5638 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005639
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005640 union {
5641 uint32_t dex_cache_element_index; // Only for dex cache reference.
5642 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5643 } load_data_;
5644
Calin Juravleacf735c2015-02-12 15:25:22 +00005645 ReferenceTypeInfo loaded_class_rti_;
5646
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005647 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5648};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005649std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5650
5651// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5652inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5653 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5654 return load_data_.dex_cache_element_index;
5655}
5656
5657// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5658inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005659 // The special input is used for PC-relative loads on some architectures,
5660 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005661 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005662 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5663 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5664 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005665 DCHECK(special_input_.GetInstruction() == nullptr);
5666 special_input_ = HUserRecord<HInstruction*>(special_input);
5667 special_input->AddUseAt(this, 0);
5668}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005669
Vladimir Marko372f10e2016-05-17 16:30:10 +01005670class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005671 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005672 // Determines how to load the String.
5673 enum class LoadKind {
5674 // Use boot image String* address that will be known at link time.
5675 // Used for boot image strings referenced by boot image code in non-PIC mode.
5676 kBootImageLinkTimeAddress,
5677
5678 // Use PC-relative boot image String* address that will be known at link time.
5679 // Used for boot image strings referenced by boot image code in PIC mode.
5680 kBootImageLinkTimePcRelative,
5681
5682 // Use a known boot image String* address, embedded in the code by the codegen.
5683 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5684 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5685 // GetIncludePatchInformation().
5686 kBootImageAddress,
5687
5688 // Load from the resolved strings array at an absolute address.
5689 // Used for strings outside the boot image referenced by JIT-compiled code.
5690 kDexCacheAddress,
5691
5692 // Load from resolved strings array in the dex cache using a PC-relative load.
5693 // Used for strings outside boot image when we know that we can access
5694 // the dex cache arrays using a PC-relative load.
5695 kDexCachePcRelative,
5696
5697 // Load from resolved strings array accessed through the class loaded from
5698 // the compiled method's own ArtMethod*. This is the default access type when
5699 // all other types are unavailable.
5700 kDexCacheViaMethod,
5701
5702 kLast = kDexCacheViaMethod
5703 };
5704
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005705 HLoadString(HCurrentMethod* current_method,
5706 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005707 const DexFile& dex_file,
5708 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005709 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5710 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005711 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005712 SetPackedFlag<kFlagIsInDexCache>(false);
5713 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5714 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005715 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005716
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005717 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5718 DCHECK(HasAddress(load_kind));
5719 load_data_.address = address;
5720 SetLoadKindInternal(load_kind);
5721 }
5722
5723 void SetLoadKindWithStringReference(LoadKind load_kind,
5724 const DexFile& dex_file,
5725 uint32_t string_index) {
5726 DCHECK(HasStringReference(load_kind));
5727 load_data_.ref.dex_file = &dex_file;
5728 string_index_ = string_index;
5729 SetLoadKindInternal(load_kind);
5730 }
5731
5732 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5733 const DexFile& dex_file,
5734 uint32_t element_index) {
5735 DCHECK(HasDexCacheReference(load_kind));
5736 load_data_.ref.dex_file = &dex_file;
5737 load_data_.ref.dex_cache_element_index = element_index;
5738 SetLoadKindInternal(load_kind);
5739 }
5740
5741 LoadKind GetLoadKind() const {
5742 return GetPackedField<LoadKindField>();
5743 }
5744
5745 const DexFile& GetDexFile() const;
5746
5747 uint32_t GetStringIndex() const {
5748 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5749 return string_index_;
5750 }
5751
5752 uint32_t GetDexCacheElementOffset() const;
5753
5754 uint64_t GetAddress() const {
5755 DCHECK(HasAddress(GetLoadKind()));
5756 return load_data_.address;
5757 }
5758
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005759 bool CanBeMoved() const OVERRIDE { return true; }
5760
Vladimir Marko372f10e2016-05-17 16:30:10 +01005761 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005762
5763 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5764
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005765 // Will call the runtime if we need to load the string through
5766 // the dex cache and the string is not guaranteed to be there yet.
5767 bool NeedsEnvironment() const OVERRIDE {
5768 LoadKind load_kind = GetLoadKind();
5769 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5770 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5771 load_kind == LoadKind::kBootImageAddress) {
5772 return false;
5773 }
5774 return !IsInDexCache();
5775 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005776
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005777 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5778 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5779 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005780
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005781 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005782 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005783
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005784 static SideEffects SideEffectsForArchRuntimeCalls() {
5785 return SideEffects::CanTriggerGC();
5786 }
5787
Vladimir Markoa1de9182016-02-25 11:37:38 +00005788 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5789
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005790 void MarkInDexCache() {
5791 SetPackedFlag<kFlagIsInDexCache>(true);
5792 DCHECK(!NeedsEnvironment());
5793 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005794 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005795 }
5796
Vladimir Marko372f10e2016-05-17 16:30:10 +01005797 void AddSpecialInput(HInstruction* special_input);
5798
5799 using HInstruction::GetInputRecords; // Keep the const version visible.
5800 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5801 return ArrayRef<HUserRecord<HInstruction*>>(
5802 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005803 }
5804
Vladimir Marko372f10e2016-05-17 16:30:10 +01005805 Primitive::Type GetType() const OVERRIDE {
5806 return Primitive::kPrimNot;
5807 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005808
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005809 DECLARE_INSTRUCTION(LoadString);
5810
5811 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005812 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005813 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5814 static constexpr size_t kFieldLoadKindSize =
5815 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5816 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005817 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005818 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005819
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005820 static bool HasStringReference(LoadKind load_kind) {
5821 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5822 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5823 load_kind == LoadKind::kDexCacheViaMethod;
5824 }
5825
5826 static bool HasAddress(LoadKind load_kind) {
5827 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5828 }
5829
5830 static bool HasDexCacheReference(LoadKind load_kind) {
5831 return load_kind == LoadKind::kDexCachePcRelative;
5832 }
5833
5834 void SetLoadKindInternal(LoadKind load_kind);
5835
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005836 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5837 // For other load kinds it's empty or possibly some architecture-specific instruction
5838 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005839 HUserRecord<HInstruction*> special_input_;
5840
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005841 // String index serves also as the hash code and it's also needed for slow-paths,
5842 // so it must not be overwritten with other load data.
5843 uint32_t string_index_;
5844
5845 union {
5846 struct {
5847 const DexFile* dex_file; // For string reference and dex cache reference.
5848 uint32_t dex_cache_element_index; // Only for dex cache reference.
5849 } ref;
5850 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5851 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005852
5853 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5854};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005855std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5856
5857// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5858inline const DexFile& HLoadString::GetDexFile() const {
5859 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5860 << GetLoadKind();
5861 return *load_data_.ref.dex_file;
5862}
5863
5864// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5865inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5866 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5867 return load_data_.ref.dex_cache_element_index;
5868}
5869
5870// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5871inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005872 // The special input is used for PC-relative loads on some architectures,
5873 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005874 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005875 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5876 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5877 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005878 // HLoadString::GetInputRecords() returns an empty array at this point,
5879 // so use the GetInputRecords() from the base class to set the input record.
5880 DCHECK(special_input_.GetInstruction() == nullptr);
5881 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005882 special_input->AddUseAt(this, 0);
5883}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005884
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005885/**
5886 * Performs an initialization check on its Class object input.
5887 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005888class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005889 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005890 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005891 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005892 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005893 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5894 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005895 SetRawInputAt(0, constant);
5896 }
5897
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005898 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005899 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005900 return true;
5901 }
5902
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005903 bool NeedsEnvironment() const OVERRIDE {
5904 // May call runtime to initialize the class.
5905 return true;
5906 }
5907
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005908 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005909
5910 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5911
5912 DECLARE_INSTRUCTION(ClinitCheck);
5913
5914 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005915 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5916};
5917
Vladimir Markofcb503c2016-05-18 12:48:17 +01005918class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005919 public:
5920 HStaticFieldGet(HInstruction* cls,
5921 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005922 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005923 bool is_volatile,
5924 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005925 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005926 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005927 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005928 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005929 : HExpression(field_type,
Serban Constantinescufca16662016-07-14 09:21:59 +01005930 SideEffectsForArchRuntimeCalls(field_type, is_volatile),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005931 dex_pc),
5932 field_info_(field_offset,
5933 field_type,
5934 is_volatile,
5935 field_idx,
5936 declaring_class_def_index,
5937 dex_file,
5938 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005939 SetRawInputAt(0, cls);
5940 }
5941
Calin Juravle52c48962014-12-16 17:02:57 +00005942
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005943 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005944
Vladimir Marko372f10e2016-05-17 16:30:10 +01005945 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5946 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005947 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005948 }
5949
5950 size_t ComputeHashCode() const OVERRIDE {
5951 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5952 }
5953
Calin Juravle52c48962014-12-16 17:02:57 +00005954 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005955 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5956 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005957 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005958
Serban Constantinescufca16662016-07-14 09:21:59 +01005959 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type field_type, bool is_volatile) {
5960 SideEffects side_effects = SideEffects::FieldReadOfType(field_type, is_volatile);
5961
5962 // MIPS delegates volatile kPrimLong and kPrimDouble loads to a runtime helper.
5963 if (Primitive::Is64BitType(field_type)) {
5964 side_effects.Add(SideEffects::CanTriggerGC());
5965 }
5966 return side_effects;
5967 }
5968
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005969 DECLARE_INSTRUCTION(StaticFieldGet);
5970
5971 private:
5972 const FieldInfo field_info_;
5973
5974 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5975};
5976
Vladimir Markofcb503c2016-05-18 12:48:17 +01005977class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005978 public:
5979 HStaticFieldSet(HInstruction* cls,
5980 HInstruction* value,
5981 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005982 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005983 bool is_volatile,
5984 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005985 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005986 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005987 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005988 uint32_t dex_pc)
Serban Constantinescufca16662016-07-14 09:21:59 +01005989 : HTemplateInstruction(SideEffectsForArchRuntimeCalls(field_type, is_volatile),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005990 dex_pc),
5991 field_info_(field_offset,
5992 field_type,
5993 is_volatile,
5994 field_idx,
5995 declaring_class_def_index,
5996 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005997 dex_cache) {
5998 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005999 SetRawInputAt(0, cls);
6000 SetRawInputAt(1, value);
6001 }
6002
Calin Juravle52c48962014-12-16 17:02:57 +00006003 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006004 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6005 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006006 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006007
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006008 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006009 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6010 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006011
Serban Constantinescufca16662016-07-14 09:21:59 +01006012 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type field_type, bool is_volatile) {
6013 SideEffects side_effects = SideEffects::FieldWriteOfType(field_type, is_volatile);
6014
6015 // MIPS delegates volatile kPrimLong and kPrimDouble stores to a runtime helper.
6016 if (Primitive::Is64BitType(field_type)) {
6017 side_effects.Add(SideEffects::CanTriggerGC());
6018 }
6019 return side_effects;
6020 }
6021
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006022 DECLARE_INSTRUCTION(StaticFieldSet);
6023
6024 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006025 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6026 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6027 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6028 "Too many packed fields.");
6029
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006030 const FieldInfo field_info_;
6031
6032 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6033};
6034
Vladimir Markofcb503c2016-05-18 12:48:17 +01006035class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006036 public:
6037 HUnresolvedInstanceFieldGet(HInstruction* obj,
6038 Primitive::Type field_type,
6039 uint32_t field_index,
6040 uint32_t dex_pc)
6041 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6042 field_index_(field_index) {
6043 SetRawInputAt(0, obj);
6044 }
6045
6046 bool NeedsEnvironment() const OVERRIDE { return true; }
6047 bool CanThrow() const OVERRIDE { return true; }
6048
6049 Primitive::Type GetFieldType() const { return GetType(); }
6050 uint32_t GetFieldIndex() const { return field_index_; }
6051
6052 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6053
6054 private:
6055 const uint32_t field_index_;
6056
6057 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6058};
6059
Vladimir Markofcb503c2016-05-18 12:48:17 +01006060class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006061 public:
6062 HUnresolvedInstanceFieldSet(HInstruction* obj,
6063 HInstruction* value,
6064 Primitive::Type field_type,
6065 uint32_t field_index,
6066 uint32_t dex_pc)
6067 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006068 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006069 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006070 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006071 SetRawInputAt(0, obj);
6072 SetRawInputAt(1, value);
6073 }
6074
6075 bool NeedsEnvironment() const OVERRIDE { return true; }
6076 bool CanThrow() const OVERRIDE { return true; }
6077
Vladimir Markoa1de9182016-02-25 11:37:38 +00006078 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006079 uint32_t GetFieldIndex() const { return field_index_; }
6080
6081 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6082
6083 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006084 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6085 static constexpr size_t kFieldFieldTypeSize =
6086 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6087 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6088 kFieldFieldType + kFieldFieldTypeSize;
6089 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6090 "Too many packed fields.");
6091 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6092
Calin Juravlee460d1d2015-09-29 04:52:17 +01006093 const uint32_t field_index_;
6094
6095 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6096};
6097
Vladimir Markofcb503c2016-05-18 12:48:17 +01006098class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006099 public:
6100 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6101 uint32_t field_index,
6102 uint32_t dex_pc)
6103 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6104 field_index_(field_index) {
6105 }
6106
6107 bool NeedsEnvironment() const OVERRIDE { return true; }
6108 bool CanThrow() const OVERRIDE { return true; }
6109
6110 Primitive::Type GetFieldType() const { return GetType(); }
6111 uint32_t GetFieldIndex() const { return field_index_; }
6112
6113 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6114
6115 private:
6116 const uint32_t field_index_;
6117
6118 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6119};
6120
Vladimir Markofcb503c2016-05-18 12:48:17 +01006121class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006122 public:
6123 HUnresolvedStaticFieldSet(HInstruction* value,
6124 Primitive::Type field_type,
6125 uint32_t field_index,
6126 uint32_t dex_pc)
6127 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006128 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006129 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006130 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006131 SetRawInputAt(0, value);
6132 }
6133
6134 bool NeedsEnvironment() const OVERRIDE { return true; }
6135 bool CanThrow() const OVERRIDE { return true; }
6136
Vladimir Markoa1de9182016-02-25 11:37:38 +00006137 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006138 uint32_t GetFieldIndex() const { return field_index_; }
6139
6140 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6141
6142 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006143 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6144 static constexpr size_t kFieldFieldTypeSize =
6145 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6146 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6147 kFieldFieldType + kFieldFieldTypeSize;
6148 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6149 "Too many packed fields.");
6150 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6151
Calin Juravlee460d1d2015-09-29 04:52:17 +01006152 const uint32_t field_index_;
6153
6154 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6155};
6156
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006157// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006158class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006159 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006160 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6161 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006162
David Brazdilbbd733e2015-08-18 17:48:17 +01006163 bool CanBeNull() const OVERRIDE { return false; }
6164
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006165 DECLARE_INSTRUCTION(LoadException);
6166
6167 private:
6168 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6169};
6170
David Brazdilcb1c0552015-08-04 16:22:25 +01006171// Implicit part of move-exception which clears thread-local exception storage.
6172// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006173class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006174 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006175 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6176 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006177
6178 DECLARE_INSTRUCTION(ClearException);
6179
6180 private:
6181 DISALLOW_COPY_AND_ASSIGN(HClearException);
6182};
6183
Vladimir Markofcb503c2016-05-18 12:48:17 +01006184class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006185 public:
6186 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006187 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006188 SetRawInputAt(0, exception);
6189 }
6190
6191 bool IsControlFlow() const OVERRIDE { return true; }
6192
6193 bool NeedsEnvironment() const OVERRIDE { return true; }
6194
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006195 bool CanThrow() const OVERRIDE { return true; }
6196
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006197
6198 DECLARE_INSTRUCTION(Throw);
6199
6200 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006201 DISALLOW_COPY_AND_ASSIGN(HThrow);
6202};
6203
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006204/**
6205 * Implementation strategies for the code generator of a HInstanceOf
6206 * or `HCheckCast`.
6207 */
6208enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006209 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006210 kExactCheck, // Can do a single class compare.
6211 kClassHierarchyCheck, // Can just walk the super class chain.
6212 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6213 kInterfaceCheck, // No optimization yet when checking against an interface.
6214 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006215 kArrayCheck, // No optimization yet when checking against a generic array.
6216 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006217};
6218
Roland Levillain86503782016-02-11 19:07:30 +00006219std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6220
Vladimir Markofcb503c2016-05-18 12:48:17 +01006221class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006222 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006223 HInstanceOf(HInstruction* object,
6224 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006225 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006226 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006227 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006228 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006229 dex_pc) {
6230 SetPackedField<TypeCheckKindField>(check_kind);
6231 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006232 SetRawInputAt(0, object);
6233 SetRawInputAt(1, constant);
6234 }
6235
6236 bool CanBeMoved() const OVERRIDE { return true; }
6237
Vladimir Marko372f10e2016-05-17 16:30:10 +01006238 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006239 return true;
6240 }
6241
6242 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006243 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006244 }
6245
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006246 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006247 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6248 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6249 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6250 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006251
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006252 static bool CanCallRuntime(TypeCheckKind check_kind) {
6253 // Mips currently does runtime calls for any other checks.
6254 return check_kind != TypeCheckKind::kExactCheck;
6255 }
6256
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006257 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006258 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006259 }
6260
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006261 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006262
6263 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006264 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6265 static constexpr size_t kFieldTypeCheckKindSize =
6266 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6267 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6268 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6269 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6270 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006271
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006272 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6273};
6274
Vladimir Markofcb503c2016-05-18 12:48:17 +01006275class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006276 public:
David Brazdilf5552582015-12-27 13:36:12 +00006277 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006278 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006279 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6280 SetPackedFlag<kFlagUpperCanBeNull>(true);
6281 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006282 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006283 SetRawInputAt(0, input);
6284 }
6285
David Brazdilf5552582015-12-27 13:36:12 +00006286 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006287 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006288 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006289 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006290
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006291 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006292 DCHECK(GetUpperCanBeNull() || !can_be_null);
6293 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006294 }
6295
Vladimir Markoa1de9182016-02-25 11:37:38 +00006296 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006297
Calin Juravleb1498f62015-02-16 13:13:29 +00006298 DECLARE_INSTRUCTION(BoundType);
6299
6300 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006301 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6302 // is false then CanBeNull() cannot be true).
6303 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6304 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6305 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6306 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6307
Calin Juravleb1498f62015-02-16 13:13:29 +00006308 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006309 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6310 // It is used to bound the type in cases like:
6311 // if (x instanceof ClassX) {
6312 // // uper_bound_ will be ClassX
6313 // }
David Brazdilf5552582015-12-27 13:36:12 +00006314 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006315
6316 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6317};
6318
Vladimir Markofcb503c2016-05-18 12:48:17 +01006319class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006320 public:
6321 HCheckCast(HInstruction* object,
6322 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006323 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006324 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006325 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6326 SetPackedField<TypeCheckKindField>(check_kind);
6327 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006328 SetRawInputAt(0, object);
6329 SetRawInputAt(1, constant);
6330 }
6331
6332 bool CanBeMoved() const OVERRIDE { return true; }
6333
Vladimir Marko372f10e2016-05-17 16:30:10 +01006334 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006335 return true;
6336 }
6337
6338 bool NeedsEnvironment() const OVERRIDE {
6339 // Instruction may throw a CheckCastError.
6340 return true;
6341 }
6342
6343 bool CanThrow() const OVERRIDE { return true; }
6344
Vladimir Markoa1de9182016-02-25 11:37:38 +00006345 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6346 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6347 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6348 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006349
6350 DECLARE_INSTRUCTION(CheckCast);
6351
6352 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006353 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6354 static constexpr size_t kFieldTypeCheckKindSize =
6355 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6356 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6357 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6358 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6359 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006360
6361 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006362};
6363
Andreas Gampe26de38b2016-07-27 17:53:11 -07006364/**
6365 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6366 * @details We define the combined barrier types that are actually required
6367 * by the Java Memory Model, rather than using exactly the terminology from
6368 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6369 * primitives. Note that the JSR-133 cookbook generally does not deal with
6370 * store atomicity issues, and the recipes there are not always entirely sufficient.
6371 * The current recipe is as follows:
6372 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6373 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6374 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6375 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6376 * class has final fields.
6377 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6378 * store-to-memory instructions. Only generated together with non-temporal stores.
6379 */
6380enum MemBarrierKind {
6381 kAnyStore,
6382 kLoadAny,
6383 kStoreStore,
6384 kAnyAny,
6385 kNTStoreStore,
6386 kLastBarrierKind = kNTStoreStore
6387};
6388std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6389
Vladimir Markofcb503c2016-05-18 12:48:17 +01006390class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006391 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006392 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006393 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006394 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6395 SetPackedField<BarrierKindField>(barrier_kind);
6396 }
Calin Juravle27df7582015-04-17 19:12:31 +01006397
Vladimir Markoa1de9182016-02-25 11:37:38 +00006398 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006399
6400 DECLARE_INSTRUCTION(MemoryBarrier);
6401
6402 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006403 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6404 static constexpr size_t kFieldBarrierKindSize =
6405 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6406 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6407 kFieldBarrierKind + kFieldBarrierKindSize;
6408 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6409 "Too many packed fields.");
6410 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006411
6412 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6413};
6414
Vladimir Markofcb503c2016-05-18 12:48:17 +01006415class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006416 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006417 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006418 kEnter,
6419 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006420 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006421 };
6422
6423 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006424 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006425 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6426 dex_pc) {
6427 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006428 SetRawInputAt(0, object);
6429 }
6430
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006431 // Instruction may go into runtime, so we need an environment.
6432 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006433
6434 bool CanThrow() const OVERRIDE {
6435 // Verifier guarantees that monitor-exit cannot throw.
6436 // This is important because it allows the HGraphBuilder to remove
6437 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6438 return IsEnter();
6439 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006440
Vladimir Markoa1de9182016-02-25 11:37:38 +00006441 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6442 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006443
6444 DECLARE_INSTRUCTION(MonitorOperation);
6445
Calin Juravle52c48962014-12-16 17:02:57 +00006446 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006447 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6448 static constexpr size_t kFieldOperationKindSize =
6449 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6450 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6451 kFieldOperationKind + kFieldOperationKindSize;
6452 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6453 "Too many packed fields.");
6454 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006455
6456 private:
6457 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6458};
6459
Vladimir Markofcb503c2016-05-18 12:48:17 +01006460class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006461 public:
6462 HSelect(HInstruction* condition,
6463 HInstruction* true_value,
6464 HInstruction* false_value,
6465 uint32_t dex_pc)
6466 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6467 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6468
6469 // First input must be `true_value` or `false_value` to allow codegens to
6470 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6471 // that architectures which implement HSelect as a conditional move also
6472 // will not need to invert the condition.
6473 SetRawInputAt(0, false_value);
6474 SetRawInputAt(1, true_value);
6475 SetRawInputAt(2, condition);
6476 }
6477
6478 HInstruction* GetFalseValue() const { return InputAt(0); }
6479 HInstruction* GetTrueValue() const { return InputAt(1); }
6480 HInstruction* GetCondition() const { return InputAt(2); }
6481
6482 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006483 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6484 return true;
6485 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006486
6487 bool CanBeNull() const OVERRIDE {
6488 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6489 }
6490
6491 DECLARE_INSTRUCTION(Select);
6492
6493 private:
6494 DISALLOW_COPY_AND_ASSIGN(HSelect);
6495};
6496
Vladimir Markof9f64412015-09-02 14:05:49 +01006497class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006498 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006499 MoveOperands(Location source,
6500 Location destination,
6501 Primitive::Type type,
6502 HInstruction* instruction)
6503 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006504
6505 Location GetSource() const { return source_; }
6506 Location GetDestination() const { return destination_; }
6507
6508 void SetSource(Location value) { source_ = value; }
6509 void SetDestination(Location value) { destination_ = value; }
6510
6511 // The parallel move resolver marks moves as "in-progress" by clearing the
6512 // destination (but not the source).
6513 Location MarkPending() {
6514 DCHECK(!IsPending());
6515 Location dest = destination_;
6516 destination_ = Location::NoLocation();
6517 return dest;
6518 }
6519
6520 void ClearPending(Location dest) {
6521 DCHECK(IsPending());
6522 destination_ = dest;
6523 }
6524
6525 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006526 DCHECK(source_.IsValid() || destination_.IsInvalid());
6527 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006528 }
6529
6530 // True if this blocks a move from the given location.
6531 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006532 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006533 }
6534
6535 // A move is redundant if it's been eliminated, if its source and
6536 // destination are the same, or if its destination is unneeded.
6537 bool IsRedundant() const {
6538 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6539 }
6540
6541 // We clear both operands to indicate move that's been eliminated.
6542 void Eliminate() {
6543 source_ = destination_ = Location::NoLocation();
6544 }
6545
6546 bool IsEliminated() const {
6547 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6548 return source_.IsInvalid();
6549 }
6550
Alexey Frunze4dda3372015-06-01 18:31:49 -07006551 Primitive::Type GetType() const { return type_; }
6552
Nicolas Geoffray90218252015-04-15 11:56:51 +01006553 bool Is64BitMove() const {
6554 return Primitive::Is64BitType(type_);
6555 }
6556
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006557 HInstruction* GetInstruction() const { return instruction_; }
6558
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006559 private:
6560 Location source_;
6561 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006562 // The type this move is for.
6563 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006564 // The instruction this move is assocatied with. Null when this move is
6565 // for moving an input in the expected locations of user (including a phi user).
6566 // This is only used in debug mode, to ensure we do not connect interval siblings
6567 // in the same parallel move.
6568 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006569};
6570
Roland Levillainc9285912015-12-18 10:38:42 +00006571std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6572
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006573static constexpr size_t kDefaultNumberOfMoves = 4;
6574
Vladimir Markofcb503c2016-05-18 12:48:17 +01006575class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006576 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006577 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006578 : HTemplateInstruction(SideEffects::None(), dex_pc),
6579 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6580 moves_.reserve(kDefaultNumberOfMoves);
6581 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006582
Nicolas Geoffray90218252015-04-15 11:56:51 +01006583 void AddMove(Location source,
6584 Location destination,
6585 Primitive::Type type,
6586 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006587 DCHECK(source.IsValid());
6588 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006589 if (kIsDebugBuild) {
6590 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006591 for (const MoveOperands& move : moves_) {
6592 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006593 // Special case the situation where the move is for the spill slot
6594 // of the instruction.
6595 if ((GetPrevious() == instruction)
6596 || ((GetPrevious() == nullptr)
6597 && instruction->IsPhi()
6598 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006599 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006600 << "Doing parallel moves for the same instruction.";
6601 } else {
6602 DCHECK(false) << "Doing parallel moves for the same instruction.";
6603 }
6604 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006605 }
6606 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006607 for (const MoveOperands& move : moves_) {
6608 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006609 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006610 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006611 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006612 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006613 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006614 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006615 }
6616
Vladimir Marko225b6462015-09-28 12:17:40 +01006617 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006618 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006619 }
6620
Vladimir Marko225b6462015-09-28 12:17:40 +01006621 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006622
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006623 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006624
6625 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006626 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006627
6628 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6629};
6630
Mark Mendell0616ae02015-04-17 12:49:27 -04006631} // namespace art
6632
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006633#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6634#include "nodes_shared.h"
6635#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006636#ifdef ART_ENABLE_CODEGEN_arm
6637#include "nodes_arm.h"
6638#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006639#ifdef ART_ENABLE_CODEGEN_arm64
6640#include "nodes_arm64.h"
6641#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006642#ifdef ART_ENABLE_CODEGEN_mips
6643#include "nodes_mips.h"
6644#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006645#ifdef ART_ENABLE_CODEGEN_x86
6646#include "nodes_x86.h"
6647#endif
6648
6649namespace art {
6650
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006651class HGraphVisitor : public ValueObject {
6652 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006653 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6654 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006655
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006656 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006657 virtual void VisitBasicBlock(HBasicBlock* block);
6658
Roland Levillain633021e2014-10-01 14:12:25 +01006659 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006660 void VisitInsertionOrder();
6661
Roland Levillain633021e2014-10-01 14:12:25 +01006662 // Visit the graph following dominator tree reverse post-order.
6663 void VisitReversePostOrder();
6664
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006665 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006666
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006667 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006668#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006669 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6670
6671 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6672
6673#undef DECLARE_VISIT_INSTRUCTION
6674
6675 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006676 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006677
6678 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6679};
6680
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006681class HGraphDelegateVisitor : public HGraphVisitor {
6682 public:
6683 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6684 virtual ~HGraphDelegateVisitor() {}
6685
6686 // Visit functions that delegate to to super class.
6687#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006688 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006689
6690 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6691
6692#undef DECLARE_VISIT_INSTRUCTION
6693
6694 private:
6695 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6696};
6697
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006698class HInsertionOrderIterator : public ValueObject {
6699 public:
6700 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6701
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006702 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006703 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006704 void Advance() { ++index_; }
6705
6706 private:
6707 const HGraph& graph_;
6708 size_t index_;
6709
6710 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6711};
6712
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006713class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006714 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006715 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6716 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006717 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006718 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006719
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006720 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6721 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006722 void Advance() { ++index_; }
6723
6724 private:
6725 const HGraph& graph_;
6726 size_t index_;
6727
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006728 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006729};
6730
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006731class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006732 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006733 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006734 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006735 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006736 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006737 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006738
6739 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006740 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006741 void Advance() { --index_; }
6742
6743 private:
6744 const HGraph& graph_;
6745 size_t index_;
6746
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006747 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006748};
6749
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006750class HLinearPostOrderIterator : public ValueObject {
6751 public:
6752 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006753 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006754
6755 bool Done() const { return index_ == 0; }
6756
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006757 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006758
6759 void Advance() {
6760 --index_;
6761 DCHECK_GE(index_, 0U);
6762 }
6763
6764 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006765 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006766 size_t index_;
6767
6768 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6769};
6770
6771class HLinearOrderIterator : public ValueObject {
6772 public:
6773 explicit HLinearOrderIterator(const HGraph& graph)
6774 : order_(graph.GetLinearOrder()), index_(0) {}
6775
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006776 bool Done() const { return index_ == order_.size(); }
6777 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006778 void Advance() { ++index_; }
6779
6780 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006781 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006782 size_t index_;
6783
6784 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6785};
6786
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006787// Iterator over the blocks that art part of the loop. Includes blocks part
6788// of an inner loop. The order in which the blocks are iterated is on their
6789// block id.
6790class HBlocksInLoopIterator : public ValueObject {
6791 public:
6792 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6793 : blocks_in_loop_(info.GetBlocks()),
6794 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6795 index_(0) {
6796 if (!blocks_in_loop_.IsBitSet(index_)) {
6797 Advance();
6798 }
6799 }
6800
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006801 bool Done() const { return index_ == blocks_.size(); }
6802 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006803 void Advance() {
6804 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006805 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006806 if (blocks_in_loop_.IsBitSet(index_)) {
6807 break;
6808 }
6809 }
6810 }
6811
6812 private:
6813 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006814 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006815 size_t index_;
6816
6817 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6818};
6819
Mingyao Yang3584bce2015-05-19 16:01:59 -07006820// Iterator over the blocks that art part of the loop. Includes blocks part
6821// of an inner loop. The order in which the blocks are iterated is reverse
6822// post order.
6823class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6824 public:
6825 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6826 : blocks_in_loop_(info.GetBlocks()),
6827 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6828 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006829 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006830 Advance();
6831 }
6832 }
6833
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006834 bool Done() const { return index_ == blocks_.size(); }
6835 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006836 void Advance() {
6837 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006838 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6839 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006840 break;
6841 }
6842 }
6843 }
6844
6845 private:
6846 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006847 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006848 size_t index_;
6849
6850 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6851};
6852
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006853inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006854 if (constant->IsIntConstant()) {
6855 return constant->AsIntConstant()->GetValue();
6856 } else if (constant->IsLongConstant()) {
6857 return constant->AsLongConstant()->GetValue();
6858 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006859 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006860 return 0;
6861 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006862}
6863
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006864#define INSTRUCTION_TYPE_CHECK(type, super) \
6865 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6866 inline const H##type* HInstruction::As##type() const { \
6867 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6868 } \
6869 inline H##type* HInstruction::As##type() { \
6870 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6871 }
6872
6873 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6874#undef INSTRUCTION_TYPE_CHECK
6875
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006876// Create space in `blocks` for adding `number_of_new_blocks` entries
6877// starting at location `at`. Blocks after `at` are moved accordingly.
6878inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6879 size_t number_of_new_blocks,
6880 size_t after) {
6881 DCHECK_LT(after, blocks->size());
6882 size_t old_size = blocks->size();
6883 size_t new_size = old_size + number_of_new_blocks;
6884 blocks->resize(new_size);
6885 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6886}
6887
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006888} // namespace art
6889
6890#endif // ART_COMPILER_OPTIMIZING_NODES_H_