blob: 119b62a16b431a5b5b0808985341b80b623d8d5e [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) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001317 M(MipsDexCacheArraysBase, Instruction) \
1318 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001319#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001320
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001321#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1322
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001323#ifndef ART_ENABLE_CODEGEN_x86
1324#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1325#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001326#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1327 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001328 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001329 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001330 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001331#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001332
1333#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1334
1335#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1336 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001337 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001338 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1339 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001340 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001341 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001342 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1343 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1344
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001345#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1346 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001347 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001348 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001349 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001350 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001351
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001352#define FOR_EACH_INSTRUCTION(M) \
1353 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1354 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1355
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001356#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001357FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1358#undef FORWARD_DECLARATION
1359
Vladimir Marko372f10e2016-05-17 16:30:10 +01001360#define DECLARE_INSTRUCTION(type) \
1361 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1362 const char* DebugName() const OVERRIDE { return #type; } \
1363 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1364 return other->Is##type(); \
1365 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001366 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001367
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001368#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1369 bool Is##type() const { return As##type() != nullptr; } \
1370 const H##type* As##type() const { return this; } \
1371 H##type* As##type() { return this; }
1372
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001373template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001374class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001375 public:
David Brazdiled596192015-01-23 10:39:45 +00001376 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001377 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001378 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001379
Vladimir Marko46817b82016-03-29 12:21:58 +01001380 // Hook for the IntrusiveForwardList<>.
1381 // TODO: Hide this better.
1382 IntrusiveForwardListHook hook;
1383
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001384 private:
David Brazdiled596192015-01-23 10:39:45 +00001385 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001386 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001387
1388 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001389 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001390
Vladimir Marko46817b82016-03-29 12:21:58 +01001391 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001392
1393 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1394};
1395
David Brazdiled596192015-01-23 10:39:45 +00001396template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001397using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001398
David Brazdil1abb4192015-02-17 18:33:36 +00001399// This class is used by HEnvironment and HInstruction classes to record the
1400// instructions they use and pointers to the corresponding HUseListNodes kept
1401// by the used instructions.
1402template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001403class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001404 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001405 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1406 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001407
Vladimir Marko46817b82016-03-29 12:21:58 +01001408 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1409 : HUserRecord(old_record.instruction_, before_use_node) {}
1410 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1411 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001412 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001413 }
1414
1415 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001416 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1417 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001418
1419 private:
1420 // Instruction used by the user.
1421 HInstruction* instruction_;
1422
Vladimir Marko46817b82016-03-29 12:21:58 +01001423 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1424 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001425};
1426
Vladimir Markoe9004912016-06-16 16:50:52 +01001427// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1428// This is used for HInstruction::GetInputs() to return a container wrapper providing
1429// HInstruction* values even though the underlying container has HUserRecord<>s.
1430struct HInputExtractor {
1431 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1432 return record.GetInstruction();
1433 }
1434 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1435 return record.GetInstruction();
1436 }
1437};
1438
1439using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1440using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1441
Aart Bik854a02b2015-07-14 16:07:00 -07001442/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001443 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001444 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001445 * For write/read dependences on fields/arrays, the dependence analysis uses
1446 * type disambiguation (e.g. a float field write cannot modify the value of an
1447 * integer field read) and the access type (e.g. a reference array write cannot
1448 * modify the value of a reference field read [although it may modify the
1449 * reference fetch prior to reading the field, which is represented by its own
1450 * write/read dependence]). The analysis makes conservative points-to
1451 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1452 * the same, and any reference read depends on any reference read without
1453 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001454 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001455 * The internal representation uses 38-bit and is described in the table below.
1456 * The first line indicates the side effect, and for field/array accesses the
1457 * second line indicates the type of the access (in the order of the
1458 * Primitive::Type enum).
1459 * The two numbered lines below indicate the bit position in the bitfield (read
1460 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001461 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001462 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1463 * +-------------+---------+---------+--------------+---------+---------+
1464 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1465 * | 3 |333333322|222222221| 1 |111111110|000000000|
1466 * | 7 |654321098|765432109| 8 |765432109|876543210|
1467 *
1468 * Note that, to ease the implementation, 'changes' bits are least significant
1469 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001470 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001471class SideEffects : public ValueObject {
1472 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001473 SideEffects() : flags_(0) {}
1474
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001475 static SideEffects None() {
1476 return SideEffects(0);
1477 }
1478
1479 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001480 return SideEffects(kAllChangeBits | kAllDependOnBits);
1481 }
1482
1483 static SideEffects AllChanges() {
1484 return SideEffects(kAllChangeBits);
1485 }
1486
1487 static SideEffects AllDependencies() {
1488 return SideEffects(kAllDependOnBits);
1489 }
1490
1491 static SideEffects AllExceptGCDependency() {
1492 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1493 }
1494
1495 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001496 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001497 }
1498
Aart Bik34c3ba92015-07-20 14:08:59 -07001499 static SideEffects AllWrites() {
1500 return SideEffects(kAllWrites);
1501 }
1502
1503 static SideEffects AllReads() {
1504 return SideEffects(kAllReads);
1505 }
1506
1507 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1508 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001509 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001510 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001511 }
1512
Aart Bik854a02b2015-07-14 16:07:00 -07001513 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001514 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001515 }
1516
Aart Bik34c3ba92015-07-20 14:08:59 -07001517 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1518 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001519 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001520 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001521 }
1522
1523 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001524 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001525 }
1526
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001527 static SideEffects CanTriggerGC() {
1528 return SideEffects(1ULL << kCanTriggerGCBit);
1529 }
1530
1531 static SideEffects DependsOnGC() {
1532 return SideEffects(1ULL << kDependsOnGCBit);
1533 }
1534
Aart Bik854a02b2015-07-14 16:07:00 -07001535 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001536 SideEffects Union(SideEffects other) const {
1537 return SideEffects(flags_ | other.flags_);
1538 }
1539
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001540 SideEffects Exclusion(SideEffects other) const {
1541 return SideEffects(flags_ & ~other.flags_);
1542 }
1543
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001544 void Add(SideEffects other) {
1545 flags_ |= other.flags_;
1546 }
1547
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001548 bool Includes(SideEffects other) const {
1549 return (other.flags_ & flags_) == other.flags_;
1550 }
1551
1552 bool HasSideEffects() const {
1553 return (flags_ & kAllChangeBits);
1554 }
1555
1556 bool HasDependencies() const {
1557 return (flags_ & kAllDependOnBits);
1558 }
1559
1560 // Returns true if there are no side effects or dependencies.
1561 bool DoesNothing() const {
1562 return flags_ == 0;
1563 }
1564
Aart Bik854a02b2015-07-14 16:07:00 -07001565 // Returns true if something is written.
1566 bool DoesAnyWrite() const {
1567 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001568 }
1569
Aart Bik854a02b2015-07-14 16:07:00 -07001570 // Returns true if something is read.
1571 bool DoesAnyRead() const {
1572 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001573 }
1574
Aart Bik854a02b2015-07-14 16:07:00 -07001575 // Returns true if potentially everything is written and read
1576 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001577 bool DoesAllReadWrite() const {
1578 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1579 }
1580
Aart Bik854a02b2015-07-14 16:07:00 -07001581 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001582 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001583 }
1584
Roland Levillain0d5a2812015-11-13 10:07:31 +00001585 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001586 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001587 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1588 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001589 }
1590
1591 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001592 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001593 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001594 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001595 for (int s = kLastBit; s >= 0; s--) {
1596 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1597 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1598 // This is a bit for the GC side effect.
1599 if (current_bit_is_set) {
1600 flags += "GC";
1601 }
Aart Bik854a02b2015-07-14 16:07:00 -07001602 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001603 } else {
1604 // This is a bit for the array/field analysis.
1605 // The underscore character stands for the 'can trigger GC' bit.
1606 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1607 if (current_bit_is_set) {
1608 flags += kDebug[s];
1609 }
1610 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1611 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1612 flags += "|";
1613 }
1614 }
Aart Bik854a02b2015-07-14 16:07:00 -07001615 }
1616 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001617 }
1618
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001619 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001620
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001621 private:
1622 static constexpr int kFieldArrayAnalysisBits = 9;
1623
1624 static constexpr int kFieldWriteOffset = 0;
1625 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1626 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1627 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1628
1629 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1630
1631 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1632 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1633 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1634 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1635
1636 static constexpr int kLastBit = kDependsOnGCBit;
1637 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1638
1639 // Aliases.
1640
1641 static_assert(kChangeBits == kDependOnBits,
1642 "the 'change' bits should match the 'depend on' bits.");
1643
1644 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1645 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1646 static constexpr uint64_t kAllWrites =
1647 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1648 static constexpr uint64_t kAllReads =
1649 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001650
Aart Bik854a02b2015-07-14 16:07:00 -07001651 // Translates type to bit flag.
1652 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1653 CHECK_NE(type, Primitive::kPrimVoid);
1654 const uint64_t one = 1;
1655 const int shift = type; // 0-based consecutive enum
1656 DCHECK_LE(kFieldWriteOffset, shift);
1657 DCHECK_LT(shift, kArrayWriteOffset);
1658 return one << (type + offset);
1659 }
1660
1661 // Private constructor on direct flags value.
1662 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1663
1664 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001665};
1666
David Brazdiled596192015-01-23 10:39:45 +00001667// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001668class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001669 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001670 HEnvironment(ArenaAllocator* arena,
1671 size_t number_of_vregs,
1672 const DexFile& dex_file,
1673 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001674 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001675 InvokeType invoke_type,
1676 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001677 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1678 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001679 parent_(nullptr),
1680 dex_file_(dex_file),
1681 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001682 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001683 invoke_type_(invoke_type),
1684 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001685 }
1686
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001687 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001688 : HEnvironment(arena,
1689 to_copy.Size(),
1690 to_copy.GetDexFile(),
1691 to_copy.GetMethodIdx(),
1692 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001693 to_copy.GetInvokeType(),
1694 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001695
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001696 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001697 if (parent_ != nullptr) {
1698 parent_->SetAndCopyParentChain(allocator, parent);
1699 } else {
1700 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1701 parent_->CopyFrom(parent);
1702 if (parent->GetParent() != nullptr) {
1703 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1704 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001705 }
David Brazdiled596192015-01-23 10:39:45 +00001706 }
1707
Vladimir Marko71bf8092015-09-15 15:33:14 +01001708 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001709 void CopyFrom(HEnvironment* environment);
1710
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001711 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1712 // input to the loop phi instead. This is for inserting instructions that
1713 // require an environment (like HDeoptimization) in the loop pre-header.
1714 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001715
1716 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001717 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001718 }
1719
1720 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001721 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001722 }
1723
David Brazdil1abb4192015-02-17 18:33:36 +00001724 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001725
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001726 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001727
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001728 HEnvironment* GetParent() const { return parent_; }
1729
1730 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001731 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001732 }
1733
1734 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001735 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001736 }
1737
1738 uint32_t GetDexPc() const {
1739 return dex_pc_;
1740 }
1741
1742 uint32_t GetMethodIdx() const {
1743 return method_idx_;
1744 }
1745
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001746 InvokeType GetInvokeType() const {
1747 return invoke_type_;
1748 }
1749
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001750 const DexFile& GetDexFile() const {
1751 return dex_file_;
1752 }
1753
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001754 HInstruction* GetHolder() const {
1755 return holder_;
1756 }
1757
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001758
1759 bool IsFromInlinedInvoke() const {
1760 return GetParent() != nullptr;
1761 }
1762
David Brazdiled596192015-01-23 10:39:45 +00001763 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001764 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1765 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001766 HEnvironment* parent_;
1767 const DexFile& dex_file_;
1768 const uint32_t method_idx_;
1769 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001770 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001771
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001772 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001773 HInstruction* const holder_;
1774
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001775 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001776
1777 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1778};
1779
Vladimir Markof9f64412015-09-02 14:05:49 +01001780class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001781 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001782 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001783 : previous_(nullptr),
1784 next_(nullptr),
1785 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001786 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001787 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001788 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001789 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001790 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001791 locations_(nullptr),
1792 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001793 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001794 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001795 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1796 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1797 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001798
Dave Allison20dfc792014-06-16 20:44:29 -07001799 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001800
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001801#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001802 enum InstructionKind {
1803 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1804 };
1805#undef DECLARE_KIND
1806
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001807 HInstruction* GetNext() const { return next_; }
1808 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001809
Calin Juravle77520bc2015-01-12 18:45:46 +00001810 HInstruction* GetNextDisregardingMoves() const;
1811 HInstruction* GetPreviousDisregardingMoves() const;
1812
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001813 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001814 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001815 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001816 bool IsInBlock() const { return block_ != nullptr; }
1817 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001818 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1819 bool IsIrreducibleLoopHeaderPhi() const {
1820 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1821 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001822
Vladimir Marko372f10e2016-05-17 16:30:10 +01001823 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1824
1825 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1826 // One virtual method is enough, just const_cast<> and then re-add the const.
1827 return ArrayRef<const HUserRecord<HInstruction*>>(
1828 const_cast<HInstruction*>(this)->GetInputRecords());
1829 }
1830
Vladimir Markoe9004912016-06-16 16:50:52 +01001831 HInputsRef GetInputs() {
1832 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001833 }
1834
Vladimir Markoe9004912016-06-16 16:50:52 +01001835 HConstInputsRef GetInputs() const {
1836 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001837 }
1838
1839 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001840 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001841
Vladimir Marko372f10e2016-05-17 16:30:10 +01001842 void SetRawInputAt(size_t index, HInstruction* input) {
1843 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1844 }
1845
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001846 virtual void Accept(HGraphVisitor* visitor) = 0;
1847 virtual const char* DebugName() const = 0;
1848
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001849 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1850
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001851 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001852
1853 uint32_t GetDexPc() const { return dex_pc_; }
1854
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001855 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001856
Roland Levillaine161a2a2014-10-03 12:45:18 +01001857 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001858 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001859
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001860 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001861 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001862
Calin Juravle10e244f2015-01-26 18:54:32 +00001863 // Does not apply for all instructions, but having this at top level greatly
1864 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001865 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001866 virtual bool CanBeNull() const {
1867 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1868 return true;
1869 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001870
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001871 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001872 return false;
1873 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001874
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001875 virtual bool IsActualObject() const {
1876 return GetType() == Primitive::kPrimNot;
1877 }
1878
Calin Juravle2e768302015-07-28 14:41:11 +00001879 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001880
Calin Juravle61d544b2015-02-23 16:46:57 +00001881 ReferenceTypeInfo GetReferenceTypeInfo() const {
1882 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001883 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001884 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001885 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001886
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001887 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001888 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001889 // Note: fixup_end remains valid across push_front().
1890 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1891 HUseListNode<HInstruction*>* new_node =
1892 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1893 uses_.push_front(*new_node);
1894 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001895 }
1896
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001897 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001898 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001899 // Note: env_fixup_end remains valid across push_front().
1900 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1901 HUseListNode<HEnvironment*>* new_node =
1902 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1903 env_uses_.push_front(*new_node);
1904 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001905 }
1906
David Brazdil1abb4192015-02-17 18:33:36 +00001907 void RemoveAsUserOfInput(size_t input) {
1908 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001909 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1910 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1911 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001912 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001913
Vladimir Marko372f10e2016-05-17 16:30:10 +01001914 void RemoveAsUserOfAllInputs() {
1915 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1916 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1917 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1918 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1919 }
1920 }
1921
David Brazdil1abb4192015-02-17 18:33:36 +00001922 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1923 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001924
Vladimir Marko46817b82016-03-29 12:21:58 +01001925 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1926 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1927 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001928 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001929 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001930 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001931
Roland Levillain6c82d402014-10-13 16:10:27 +01001932 // Does this instruction strictly dominate `other_instruction`?
1933 // Returns false if this instruction and `other_instruction` are the same.
1934 // Aborts if this instruction and `other_instruction` are both phis.
1935 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001936
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001937 int GetId() const { return id_; }
1938 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001939
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001940 int GetSsaIndex() const { return ssa_index_; }
1941 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1942 bool HasSsaIndex() const { return ssa_index_ != -1; }
1943
1944 bool HasEnvironment() const { return environment_ != nullptr; }
1945 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001946 // Set the `environment_` field. Raw because this method does not
1947 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001948 void SetRawEnvironment(HEnvironment* environment) {
1949 DCHECK(environment_ == nullptr);
1950 DCHECK_EQ(environment->GetHolder(), this);
1951 environment_ = environment;
1952 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001953
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001954 void InsertRawEnvironment(HEnvironment* environment) {
1955 DCHECK(environment_ != nullptr);
1956 DCHECK_EQ(environment->GetHolder(), this);
1957 DCHECK(environment->GetParent() == nullptr);
1958 environment->parent_ = environment_;
1959 environment_ = environment;
1960 }
1961
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001962 void RemoveEnvironment();
1963
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001964 // Set the environment of this instruction, copying it from `environment`. While
1965 // copying, the uses lists are being updated.
1966 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001967 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001968 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001969 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001970 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001971 if (environment->GetParent() != nullptr) {
1972 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1973 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001974 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001975
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001976 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1977 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001978 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001979 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001980 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001981 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001982 if (environment->GetParent() != nullptr) {
1983 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1984 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001985 }
1986
Nicolas Geoffray39468442014-09-02 15:17:15 +01001987 // Returns the number of entries in the environment. Typically, that is the
1988 // number of dex registers in a method. It could be more in case of inlining.
1989 size_t EnvironmentSize() const;
1990
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001991 LocationSummary* GetLocations() const { return locations_; }
1992 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001993
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001994 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001995 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001996
Alexandre Rames188d4312015-04-09 18:30:21 +01001997 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1998 // uses of this instruction by `other` are *not* updated.
1999 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2000 ReplaceWith(other);
2001 other->ReplaceInput(this, use_index);
2002 }
2003
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002004 // Move `this` instruction before `cursor`.
2005 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002006
Vladimir Markofb337ea2015-11-25 15:25:10 +00002007 // Move `this` before its first user and out of any loops. If there is no
2008 // out-of-loop user that dominates all other users, move the instruction
2009 // to the end of the out-of-loop common dominator of the user's blocks.
2010 //
2011 // This can be used only on non-throwing instructions with no side effects that
2012 // have at least one use but no environment uses.
2013 void MoveBeforeFirstUserAndOutOfLoops();
2014
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002015#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002016 bool Is##type() const; \
2017 const H##type* As##type() const; \
2018 H##type* As##type();
2019
2020 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2021#undef INSTRUCTION_TYPE_CHECK
2022
2023#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002024 bool Is##type() const { return (As##type() != nullptr); } \
2025 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002026 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002027 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002028#undef INSTRUCTION_TYPE_CHECK
2029
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002030 // Returns whether the instruction can be moved within the graph.
2031 virtual bool CanBeMoved() const { return false; }
2032
2033 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002034 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002035 return false;
2036 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002037
2038 // Returns whether any data encoded in the two instructions is equal.
2039 // This method does not look at the inputs. Both instructions must be
2040 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002041 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002042 return false;
2043 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002044
2045 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002046 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002047 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002048 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002049
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002050 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2051 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2052 // the virtual function because the __attribute__((__pure__)) doesn't really
2053 // apply the strong requirement for virtual functions, preventing optimizations.
2054 InstructionKind GetKind() const PURE;
2055 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002056
2057 virtual size_t ComputeHashCode() const {
2058 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002059 for (const HInstruction* input : GetInputs()) {
2060 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002061 }
2062 return result;
2063 }
2064
2065 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002066 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002067 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002068
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002069 size_t GetLifetimePosition() const { return lifetime_position_; }
2070 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2071 LiveInterval* GetLiveInterval() const { return live_interval_; }
2072 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2073 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2074
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002075 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2076
2077 // Returns whether the code generation of the instruction will require to have access
2078 // to the current method. Such instructions are:
2079 // (1): Instructions that require an environment, as calling the runtime requires
2080 // to walk the stack and have the current method stored at a specific stack address.
2081 // (2): Object literals like classes and strings, that are loaded from the dex cache
2082 // fields of the current method.
2083 bool NeedsCurrentMethod() const {
2084 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2085 }
2086
Vladimir Markodc151b22015-10-15 18:02:30 +01002087 // Returns whether the code generation of the instruction will require to have access
2088 // to the dex cache of the current method's declaring class via the current method.
2089 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002090
Mark Mendellc4701932015-04-10 13:18:51 -04002091 // Does this instruction have any use in an environment before
2092 // control flow hits 'other'?
2093 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2094
2095 // Remove all references to environment uses of this instruction.
2096 // The caller must ensure that this is safe to do.
2097 void RemoveEnvironmentUsers();
2098
Vladimir Markoa1de9182016-02-25 11:37:38 +00002099 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2100 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002101
David Brazdil1abb4192015-02-17 18:33:36 +00002102 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002103 // If set, the machine code for this instruction is assumed to be generated by
2104 // its users. Used by liveness analysis to compute use positions accordingly.
2105 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2106 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2107 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2108 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2109
Vladimir Marko372f10e2016-05-17 16:30:10 +01002110 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2111 return GetInputRecords()[i];
2112 }
2113
2114 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2115 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2116 input_records[index] = input;
2117 }
David Brazdil1abb4192015-02-17 18:33:36 +00002118
Vladimir Markoa1de9182016-02-25 11:37:38 +00002119 uint32_t GetPackedFields() const {
2120 return packed_fields_;
2121 }
2122
2123 template <size_t flag>
2124 bool GetPackedFlag() const {
2125 return (packed_fields_ & (1u << flag)) != 0u;
2126 }
2127
2128 template <size_t flag>
2129 void SetPackedFlag(bool value = true) {
2130 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2131 }
2132
2133 template <typename BitFieldType>
2134 typename BitFieldType::value_type GetPackedField() const {
2135 return BitFieldType::Decode(packed_fields_);
2136 }
2137
2138 template <typename BitFieldType>
2139 void SetPackedField(typename BitFieldType::value_type value) {
2140 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2141 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2142 }
2143
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002144 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002145 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2146 auto before_use_node = uses_.before_begin();
2147 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2148 HInstruction* user = use_node->GetUser();
2149 size_t input_index = use_node->GetIndex();
2150 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2151 before_use_node = use_node;
2152 }
2153 }
2154
2155 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2156 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2157 if (next != uses_.end()) {
2158 HInstruction* next_user = next->GetUser();
2159 size_t next_index = next->GetIndex();
2160 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2161 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2162 }
2163 }
2164
2165 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2166 auto before_env_use_node = env_uses_.before_begin();
2167 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2168 HEnvironment* user = env_use_node->GetUser();
2169 size_t input_index = env_use_node->GetIndex();
2170 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2171 before_env_use_node = env_use_node;
2172 }
2173 }
2174
2175 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2176 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2177 if (next != env_uses_.end()) {
2178 HEnvironment* next_user = next->GetUser();
2179 size_t next_index = next->GetIndex();
2180 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2181 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2182 }
2183 }
David Brazdil1abb4192015-02-17 18:33:36 +00002184
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002185 HInstruction* previous_;
2186 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002187 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002188 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002189
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002190 // An instruction gets an id when it is added to the graph.
2191 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002192 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002193 int id_;
2194
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002195 // When doing liveness analysis, instructions that have uses get an SSA index.
2196 int ssa_index_;
2197
Vladimir Markoa1de9182016-02-25 11:37:38 +00002198 // Packed fields.
2199 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002200
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002201 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002202 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002203
2204 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002205 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002206
Nicolas Geoffray39468442014-09-02 15:17:15 +01002207 // The environment associated with this instruction. Not null if the instruction
2208 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002209 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002210
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002211 // Set by the code generator.
2212 LocationSummary* locations_;
2213
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002214 // Set by the liveness analysis.
2215 LiveInterval* live_interval_;
2216
2217 // Set by the liveness analysis, this is the position in a linear
2218 // order of blocks where this instruction's live interval start.
2219 size_t lifetime_position_;
2220
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002221 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002222
Vladimir Markoa1de9182016-02-25 11:37:38 +00002223 // The reference handle part of the reference type info.
2224 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002225 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002226 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002227
David Brazdil1abb4192015-02-17 18:33:36 +00002228 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002229 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002230 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002231 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002232 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002233
2234 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2235};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002236std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002237
2238class HInstructionIterator : public ValueObject {
2239 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002240 explicit HInstructionIterator(const HInstructionList& instructions)
2241 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002242 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002243 }
2244
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002245 bool Done() const { return instruction_ == nullptr; }
2246 HInstruction* Current() const { return instruction_; }
2247 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002248 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002249 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002250 }
2251
2252 private:
2253 HInstruction* instruction_;
2254 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002255
2256 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002257};
2258
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002259class HBackwardInstructionIterator : public ValueObject {
2260 public:
2261 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2262 : instruction_(instructions.last_instruction_) {
2263 next_ = Done() ? nullptr : instruction_->GetPrevious();
2264 }
2265
2266 bool Done() const { return instruction_ == nullptr; }
2267 HInstruction* Current() const { return instruction_; }
2268 void Advance() {
2269 instruction_ = next_;
2270 next_ = Done() ? nullptr : instruction_->GetPrevious();
2271 }
2272
2273 private:
2274 HInstruction* instruction_;
2275 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002276
2277 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002278};
2279
Vladimir Markof9f64412015-09-02 14:05:49 +01002280template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002281class HTemplateInstruction: public HInstruction {
2282 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002283 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002284 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002285 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002286
Vladimir Marko372f10e2016-05-17 16:30:10 +01002287 using HInstruction::GetInputRecords; // Keep the const version visible.
2288 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2289 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002290 }
2291
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002292 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002293 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002294
2295 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002296};
2297
Vladimir Markof9f64412015-09-02 14:05:49 +01002298// HTemplateInstruction specialization for N=0.
2299template<>
2300class HTemplateInstruction<0>: public HInstruction {
2301 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002302 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002303 : HInstruction(side_effects, dex_pc) {}
2304
Vladimir Markof9f64412015-09-02 14:05:49 +01002305 virtual ~HTemplateInstruction() {}
2306
Vladimir Marko372f10e2016-05-17 16:30:10 +01002307 using HInstruction::GetInputRecords; // Keep the const version visible.
2308 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2309 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002310 }
2311
2312 private:
2313 friend class SsaBuilder;
2314};
2315
Dave Allison20dfc792014-06-16 20:44:29 -07002316template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002317class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002318 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002319 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002320 : HTemplateInstruction<N>(side_effects, dex_pc) {
2321 this->template SetPackedField<TypeField>(type);
2322 }
Dave Allison20dfc792014-06-16 20:44:29 -07002323 virtual ~HExpression() {}
2324
Vladimir Markoa1de9182016-02-25 11:37:38 +00002325 Primitive::Type GetType() const OVERRIDE {
2326 return TypeField::Decode(this->GetPackedFields());
2327 }
Dave Allison20dfc792014-06-16 20:44:29 -07002328
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002329 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002330 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2331 static constexpr size_t kFieldTypeSize =
2332 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2333 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2334 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2335 "Too many packed fields.");
2336 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002337};
2338
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002339// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2340// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002341class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002342 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002343 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2344 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002345
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002346 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002347
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002348 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002349
2350 private:
2351 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2352};
2353
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002354// Represents dex's RETURN opcodes. A HReturn is a control flow
2355// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002356class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002357 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002358 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2359 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002360 SetRawInputAt(0, value);
2361 }
2362
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002363 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002364
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002365 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002366
2367 private:
2368 DISALLOW_COPY_AND_ASSIGN(HReturn);
2369};
2370
Vladimir Markofcb503c2016-05-18 12:48:17 +01002371class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002372 public:
2373 HPhi(ArenaAllocator* arena,
2374 uint32_t reg_number,
2375 size_t number_of_inputs,
2376 Primitive::Type type,
2377 uint32_t dex_pc = kNoDexPc)
2378 : HInstruction(SideEffects::None(), dex_pc),
2379 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2380 reg_number_(reg_number) {
2381 SetPackedField<TypeField>(ToPhiType(type));
2382 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2383 // Phis are constructed live and marked dead if conflicting or unused.
2384 // Individual steps of SsaBuilder should assume that if a phi has been
2385 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2386 SetPackedFlag<kFlagIsLive>(true);
2387 SetPackedFlag<kFlagCanBeNull>(true);
2388 }
2389
2390 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2391 static Primitive::Type ToPhiType(Primitive::Type type) {
2392 return Primitive::PrimitiveKind(type);
2393 }
2394
2395 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2396
Vladimir Marko372f10e2016-05-17 16:30:10 +01002397 using HInstruction::GetInputRecords; // Keep the const version visible.
2398 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2399 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2400 }
David Brazdildee58d62016-04-07 09:54:26 +00002401
2402 void AddInput(HInstruction* input);
2403 void RemoveInputAt(size_t index);
2404
2405 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2406 void SetType(Primitive::Type new_type) {
2407 // Make sure that only valid type changes occur. The following are allowed:
2408 // (1) int -> float/ref (primitive type propagation),
2409 // (2) long -> double (primitive type propagation).
2410 DCHECK(GetType() == new_type ||
2411 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2412 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2413 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2414 SetPackedField<TypeField>(new_type);
2415 }
2416
2417 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2418 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2419
2420 uint32_t GetRegNumber() const { return reg_number_; }
2421
2422 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2423 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2424 bool IsDead() const { return !IsLive(); }
2425 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2426
Vladimir Markoe9004912016-06-16 16:50:52 +01002427 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002428 return other != nullptr
2429 && other->IsPhi()
2430 && other->AsPhi()->GetBlock() == GetBlock()
2431 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2432 }
2433
2434 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2435 // An equivalent phi is a phi having the same dex register and type.
2436 // It assumes that phis with the same dex register are adjacent.
2437 HPhi* GetNextEquivalentPhiWithSameType() {
2438 HInstruction* next = GetNext();
2439 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2440 if (next->GetType() == GetType()) {
2441 return next->AsPhi();
2442 }
2443 next = next->GetNext();
2444 }
2445 return nullptr;
2446 }
2447
2448 DECLARE_INSTRUCTION(Phi);
2449
David Brazdildee58d62016-04-07 09:54:26 +00002450 private:
2451 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2452 static constexpr size_t kFieldTypeSize =
2453 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2454 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2455 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2456 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2457 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2458 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2459
Vladimir Marko372f10e2016-05-17 16:30:10 +01002460 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002461 const uint32_t reg_number_;
2462
2463 DISALLOW_COPY_AND_ASSIGN(HPhi);
2464};
2465
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002466// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002467// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002468// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002469class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002470 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002471 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002472
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002473 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002474
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002475 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002476
2477 private:
2478 DISALLOW_COPY_AND_ASSIGN(HExit);
2479};
2480
2481// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002482class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002483 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002484 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002485
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002486 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002487
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002488 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002489 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002490 }
2491
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002492 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002493
2494 private:
2495 DISALLOW_COPY_AND_ASSIGN(HGoto);
2496};
2497
Roland Levillain9867bc72015-08-05 10:21:34 +01002498class HConstant : public HExpression<0> {
2499 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002500 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2501 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002502
2503 bool CanBeMoved() const OVERRIDE { return true; }
2504
Roland Levillain1a653882016-03-18 18:05:57 +00002505 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002506 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002507 // Is this constant 0 in the arithmetic sense?
2508 virtual bool IsArithmeticZero() const { return false; }
2509 // Is this constant a 0-bit pattern?
2510 virtual bool IsZeroBitPattern() const { return false; }
2511 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002512 virtual bool IsOne() const { return false; }
2513
David Brazdil77a48ae2015-09-15 12:34:04 +00002514 virtual uint64_t GetValueAsUint64() const = 0;
2515
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002516 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002517
2518 private:
2519 DISALLOW_COPY_AND_ASSIGN(HConstant);
2520};
2521
Vladimir Markofcb503c2016-05-18 12:48:17 +01002522class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002523 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002524 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002525 return true;
2526 }
2527
David Brazdil77a48ae2015-09-15 12:34:04 +00002528 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2529
Roland Levillain9867bc72015-08-05 10:21:34 +01002530 size_t ComputeHashCode() const OVERRIDE { return 0; }
2531
Roland Levillain1a653882016-03-18 18:05:57 +00002532 // The null constant representation is a 0-bit pattern.
2533 virtual bool IsZeroBitPattern() const { return true; }
2534
Roland Levillain9867bc72015-08-05 10:21:34 +01002535 DECLARE_INSTRUCTION(NullConstant);
2536
2537 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002538 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002539
2540 friend class HGraph;
2541 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2542};
2543
2544// Constants of the type int. Those can be from Dex instructions, or
2545// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002546class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002547 public:
2548 int32_t GetValue() const { return value_; }
2549
David Brazdil9f389d42015-10-01 14:32:56 +01002550 uint64_t GetValueAsUint64() const OVERRIDE {
2551 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2552 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002553
Vladimir Marko372f10e2016-05-17 16:30:10 +01002554 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002555 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002556 return other->AsIntConstant()->value_ == value_;
2557 }
2558
2559 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2560
2561 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002562 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2563 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002564 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2565
Roland Levillain1a653882016-03-18 18:05:57 +00002566 // Integer constants are used to encode Boolean values as well,
2567 // where 1 means true and 0 means false.
2568 bool IsTrue() const { return GetValue() == 1; }
2569 bool IsFalse() const { return GetValue() == 0; }
2570
Roland Levillain9867bc72015-08-05 10:21:34 +01002571 DECLARE_INSTRUCTION(IntConstant);
2572
2573 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002574 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2575 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2576 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2577 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002578
2579 const int32_t value_;
2580
2581 friend class HGraph;
2582 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2583 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2584 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2585};
2586
Vladimir Markofcb503c2016-05-18 12:48:17 +01002587class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002588 public:
2589 int64_t GetValue() const { return value_; }
2590
David Brazdil77a48ae2015-09-15 12:34:04 +00002591 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2592
Vladimir Marko372f10e2016-05-17 16:30:10 +01002593 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002594 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002595 return other->AsLongConstant()->value_ == value_;
2596 }
2597
2598 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2599
2600 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002601 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2602 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002603 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2604
2605 DECLARE_INSTRUCTION(LongConstant);
2606
2607 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002608 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2609 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002610
2611 const int64_t value_;
2612
2613 friend class HGraph;
2614 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2615};
Dave Allison20dfc792014-06-16 20:44:29 -07002616
Vladimir Markofcb503c2016-05-18 12:48:17 +01002617class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002618 public:
2619 float GetValue() const { return value_; }
2620
2621 uint64_t GetValueAsUint64() const OVERRIDE {
2622 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2623 }
2624
Vladimir Marko372f10e2016-05-17 16:30:10 +01002625 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002626 DCHECK(other->IsFloatConstant()) << other->DebugName();
2627 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2628 }
2629
2630 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2631
2632 bool IsMinusOne() const OVERRIDE {
2633 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2634 }
Roland Levillain1a653882016-03-18 18:05:57 +00002635 bool IsArithmeticZero() const OVERRIDE {
2636 return std::fpclassify(value_) == FP_ZERO;
2637 }
2638 bool IsArithmeticPositiveZero() const {
2639 return IsArithmeticZero() && !std::signbit(value_);
2640 }
2641 bool IsArithmeticNegativeZero() const {
2642 return IsArithmeticZero() && std::signbit(value_);
2643 }
2644 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002645 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002646 }
2647 bool IsOne() const OVERRIDE {
2648 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2649 }
2650 bool IsNaN() const {
2651 return std::isnan(value_);
2652 }
2653
2654 DECLARE_INSTRUCTION(FloatConstant);
2655
2656 private:
2657 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2658 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2659 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2660 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2661
2662 const float value_;
2663
2664 // Only the SsaBuilder and HGraph can create floating-point constants.
2665 friend class SsaBuilder;
2666 friend class HGraph;
2667 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2668};
2669
Vladimir Markofcb503c2016-05-18 12:48:17 +01002670class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002671 public:
2672 double GetValue() const { return value_; }
2673
2674 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2675
Vladimir Marko372f10e2016-05-17 16:30:10 +01002676 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002677 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2678 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2679 }
2680
2681 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2682
2683 bool IsMinusOne() const OVERRIDE {
2684 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2685 }
Roland Levillain1a653882016-03-18 18:05:57 +00002686 bool IsArithmeticZero() const OVERRIDE {
2687 return std::fpclassify(value_) == FP_ZERO;
2688 }
2689 bool IsArithmeticPositiveZero() const {
2690 return IsArithmeticZero() && !std::signbit(value_);
2691 }
2692 bool IsArithmeticNegativeZero() const {
2693 return IsArithmeticZero() && std::signbit(value_);
2694 }
2695 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002696 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002697 }
2698 bool IsOne() const OVERRIDE {
2699 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2700 }
2701 bool IsNaN() const {
2702 return std::isnan(value_);
2703 }
2704
2705 DECLARE_INSTRUCTION(DoubleConstant);
2706
2707 private:
2708 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2709 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2710 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2711 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2712
2713 const double value_;
2714
2715 // Only the SsaBuilder and HGraph can create floating-point constants.
2716 friend class SsaBuilder;
2717 friend class HGraph;
2718 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2719};
2720
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002721// Conditional branch. A block ending with an HIf instruction must have
2722// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002723class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002724 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002725 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2726 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002727 SetRawInputAt(0, input);
2728 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002729
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002730 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002731
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002732 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002733 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002734 }
2735
2736 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002737 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002738 }
2739
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002740 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002741
2742 private:
2743 DISALLOW_COPY_AND_ASSIGN(HIf);
2744};
2745
David Brazdilfc6a86a2015-06-26 10:33:45 +00002746
2747// Abstract instruction which marks the beginning and/or end of a try block and
2748// links it to the respective exception handlers. Behaves the same as a Goto in
2749// non-exceptional control flow.
2750// Normal-flow successor is stored at index zero, exception handlers under
2751// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002752class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002753 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002754 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002755 kEntry,
2756 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002757 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002758 };
2759
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002760 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002761 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2762 SetPackedField<BoundaryKindField>(kind);
2763 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002764
2765 bool IsControlFlow() const OVERRIDE { return true; }
2766
2767 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002768 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002769
David Brazdild26a4112015-11-10 11:07:31 +00002770 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2771 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2772 }
2773
David Brazdilfc6a86a2015-06-26 10:33:45 +00002774 // Returns whether `handler` is among its exception handlers (non-zero index
2775 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002776 bool HasExceptionHandler(const HBasicBlock& handler) const {
2777 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002778 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002779 }
2780
2781 // If not present already, adds `handler` to its block's list of exception
2782 // handlers.
2783 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002784 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002785 GetBlock()->AddSuccessor(handler);
2786 }
2787 }
2788
Vladimir Markoa1de9182016-02-25 11:37:38 +00002789 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2790 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002791
David Brazdilffee3d32015-07-06 11:48:53 +01002792 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2793
David Brazdilfc6a86a2015-06-26 10:33:45 +00002794 DECLARE_INSTRUCTION(TryBoundary);
2795
2796 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002797 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2798 static constexpr size_t kFieldBoundaryKindSize =
2799 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2800 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2801 kFieldBoundaryKind + kFieldBoundaryKindSize;
2802 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2803 "Too many packed fields.");
2804 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002805
2806 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2807};
2808
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002809// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002810class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002811 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002812 // We set CanTriggerGC to prevent any intermediate address to be live
2813 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002814 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002815 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002816 SetRawInputAt(0, cond);
2817 }
2818
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002819 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002820 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002821 return true;
2822 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002823 bool NeedsEnvironment() const OVERRIDE { return true; }
2824 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002825
2826 DECLARE_INSTRUCTION(Deoptimize);
2827
2828 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002829 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2830};
2831
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002832// Represents the ArtMethod that was passed as a first argument to
2833// the method. It is used by instructions that depend on it, like
2834// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002835class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002836 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002837 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2838 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002839
2840 DECLARE_INSTRUCTION(CurrentMethod);
2841
2842 private:
2843 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2844};
2845
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002846// Fetches an ArtMethod from the virtual table or the interface method table
2847// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002848class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002849 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002850 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002851 kVTable,
2852 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002853 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002854 };
2855 HClassTableGet(HInstruction* cls,
2856 Primitive::Type type,
2857 TableKind kind,
2858 size_t index,
2859 uint32_t dex_pc)
2860 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002861 index_(index) {
2862 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002863 SetRawInputAt(0, cls);
2864 }
2865
2866 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002867 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002868 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002869 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002870 }
2871
Vladimir Markoa1de9182016-02-25 11:37:38 +00002872 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002873 size_t GetIndex() const { return index_; }
2874
2875 DECLARE_INSTRUCTION(ClassTableGet);
2876
2877 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002878 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2879 static constexpr size_t kFieldTableKindSize =
2880 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2881 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2882 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2883 "Too many packed fields.");
2884 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2885
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002886 // The index of the ArtMethod in the table.
2887 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002888
2889 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2890};
2891
Mark Mendellfe57faa2015-09-18 09:26:15 -04002892// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2893// have one successor for each entry in the switch table, and the final successor
2894// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002895class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002896 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002897 HPackedSwitch(int32_t start_value,
2898 uint32_t num_entries,
2899 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002900 uint32_t dex_pc = kNoDexPc)
2901 : HTemplateInstruction(SideEffects::None(), dex_pc),
2902 start_value_(start_value),
2903 num_entries_(num_entries) {
2904 SetRawInputAt(0, input);
2905 }
2906
2907 bool IsControlFlow() const OVERRIDE { return true; }
2908
2909 int32_t GetStartValue() const { return start_value_; }
2910
Vladimir Marko211c2112015-09-24 16:52:33 +01002911 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002912
2913 HBasicBlock* GetDefaultBlock() const {
2914 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002915 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002916 }
2917 DECLARE_INSTRUCTION(PackedSwitch);
2918
2919 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002920 const int32_t start_value_;
2921 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002922
2923 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2924};
2925
Roland Levillain88cb1752014-10-20 16:36:47 +01002926class HUnaryOperation : public HExpression<1> {
2927 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002928 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2929 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002930 SetRawInputAt(0, input);
2931 }
2932
2933 HInstruction* GetInput() const { return InputAt(0); }
2934 Primitive::Type GetResultType() const { return GetType(); }
2935
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002936 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002937 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002938 return true;
2939 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002940
Roland Levillain31dd3d62016-02-16 12:21:02 +00002941 // Try to statically evaluate `this` and return a HConstant
2942 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002943 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002944 HConstant* TryStaticEvaluation() const;
2945
2946 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002947 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2948 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002949 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2950 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002951
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002952 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002953
2954 private:
2955 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2956};
2957
Dave Allison20dfc792014-06-16 20:44:29 -07002958class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002959 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002960 HBinaryOperation(Primitive::Type result_type,
2961 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002962 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002963 SideEffects side_effects = SideEffects::None(),
2964 uint32_t dex_pc = kNoDexPc)
2965 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002966 SetRawInputAt(0, left);
2967 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002968 }
2969
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002970 HInstruction* GetLeft() const { return InputAt(0); }
2971 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002972 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002973
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002974 virtual bool IsCommutative() const { return false; }
2975
2976 // Put constant on the right.
2977 // Returns whether order is changed.
2978 bool OrderInputsWithConstantOnTheRight() {
2979 HInstruction* left = InputAt(0);
2980 HInstruction* right = InputAt(1);
2981 if (left->IsConstant() && !right->IsConstant()) {
2982 ReplaceInput(right, 0);
2983 ReplaceInput(left, 1);
2984 return true;
2985 }
2986 return false;
2987 }
2988
2989 // Order inputs by instruction id, but favor constant on the right side.
2990 // This helps GVN for commutative ops.
2991 void OrderInputs() {
2992 DCHECK(IsCommutative());
2993 HInstruction* left = InputAt(0);
2994 HInstruction* right = InputAt(1);
2995 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2996 return;
2997 }
2998 if (OrderInputsWithConstantOnTheRight()) {
2999 return;
3000 }
3001 // Order according to instruction id.
3002 if (left->GetId() > right->GetId()) {
3003 ReplaceInput(right, 0);
3004 ReplaceInput(left, 1);
3005 }
3006 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003007
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003008 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003009 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003010 return true;
3011 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003012
Roland Levillain31dd3d62016-02-16 12:21:02 +00003013 // Try to statically evaluate `this` and return a HConstant
3014 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003015 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003016 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003017
3018 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003019 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3020 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003021 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3022 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003023 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003024 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3025 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003026 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3027 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003028 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3029 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003030 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003031 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3032 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003033
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003034 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003035 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003036 HConstant* GetConstantRight() const;
3037
3038 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003039 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003040 HInstruction* GetLeastConstantLeft() const;
3041
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003042 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003043
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003044 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003045 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3046};
3047
Mark Mendellc4701932015-04-10 13:18:51 -04003048// The comparison bias applies for floating point operations and indicates how NaN
3049// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003050enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003051 kNoBias, // bias is not applicable (i.e. for long operation)
3052 kGtBias, // return 1 for NaN comparisons
3053 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003054 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003055};
3056
Roland Levillain31dd3d62016-02-16 12:21:02 +00003057std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3058
Dave Allison20dfc792014-06-16 20:44:29 -07003059class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003060 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003061 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003062 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3063 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3064 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003065
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003066 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003067 // `instruction`, and disregard moves in between.
3068 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003069
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003070 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003071
3072 virtual IfCondition GetCondition() const = 0;
3073
Mark Mendellc4701932015-04-10 13:18:51 -04003074 virtual IfCondition GetOppositeCondition() const = 0;
3075
Vladimir Markoa1de9182016-02-25 11:37:38 +00003076 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003077 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3078
Vladimir Markoa1de9182016-02-25 11:37:38 +00003079 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3080 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003081
Vladimir Marko372f10e2016-05-17 16:30:10 +01003082 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003083 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003084 }
3085
Roland Levillain4fa13f62015-07-06 18:11:54 +01003086 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003087 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003088 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003089 if (if_cond == kCondNE) {
3090 return true;
3091 } else if (if_cond == kCondEQ) {
3092 return false;
3093 }
3094 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003095 }
3096
3097 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003098 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003099 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003100 if (if_cond == kCondEQ) {
3101 return true;
3102 } else if (if_cond == kCondNE) {
3103 return false;
3104 }
3105 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003106 }
3107
Roland Levillain31dd3d62016-02-16 12:21:02 +00003108 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003109 // Needed if we merge a HCompare into a HCondition.
3110 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3111 static constexpr size_t kFieldComparisonBiasSize =
3112 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3113 static constexpr size_t kNumberOfConditionPackedBits =
3114 kFieldComparisonBias + kFieldComparisonBiasSize;
3115 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3116 using ComparisonBiasField =
3117 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3118
Roland Levillain31dd3d62016-02-16 12:21:02 +00003119 template <typename T>
3120 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3121
3122 template <typename T>
3123 int32_t CompareFP(T x, T y) const {
3124 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3125 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3126 // Handle the bias.
3127 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3128 }
3129
3130 // Return an integer constant containing the result of a condition evaluated at compile time.
3131 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3132 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3133 }
3134
Dave Allison20dfc792014-06-16 20:44:29 -07003135 private:
3136 DISALLOW_COPY_AND_ASSIGN(HCondition);
3137};
3138
3139// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003140class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003141 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003142 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3143 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003144
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003145 bool IsCommutative() const OVERRIDE { return true; }
3146
Vladimir Marko9e23df52015-11-10 17:14:35 +00003147 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3148 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003149 return MakeConstantCondition(true, GetDexPc());
3150 }
3151 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3152 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3153 }
3154 // In the following Evaluate methods, a HCompare instruction has
3155 // been merged into this HEqual instruction; evaluate it as
3156 // `Compare(x, y) == 0`.
3157 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3158 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3159 GetDexPc());
3160 }
3161 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3162 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3163 }
3164 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3165 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003166 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003167
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003168 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003169
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003170 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003171 return kCondEQ;
3172 }
3173
Mark Mendellc4701932015-04-10 13:18:51 -04003174 IfCondition GetOppositeCondition() const OVERRIDE {
3175 return kCondNE;
3176 }
3177
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003178 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003179 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003180
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003181 DISALLOW_COPY_AND_ASSIGN(HEqual);
3182};
3183
Vladimir Markofcb503c2016-05-18 12:48:17 +01003184class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003185 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003186 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3187 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003188
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003189 bool IsCommutative() const OVERRIDE { return true; }
3190
Vladimir Marko9e23df52015-11-10 17:14:35 +00003191 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3192 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003193 return MakeConstantCondition(false, GetDexPc());
3194 }
3195 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3196 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3197 }
3198 // In the following Evaluate methods, a HCompare instruction has
3199 // been merged into this HNotEqual instruction; evaluate it as
3200 // `Compare(x, y) != 0`.
3201 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3202 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3203 }
3204 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3205 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3206 }
3207 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3208 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003209 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003210
Dave Allison20dfc792014-06-16 20:44:29 -07003211 DECLARE_INSTRUCTION(NotEqual);
3212
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003213 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003214 return kCondNE;
3215 }
3216
Mark Mendellc4701932015-04-10 13:18:51 -04003217 IfCondition GetOppositeCondition() const OVERRIDE {
3218 return kCondEQ;
3219 }
3220
Dave Allison20dfc792014-06-16 20:44:29 -07003221 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003222 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003223
Dave Allison20dfc792014-06-16 20:44:29 -07003224 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3225};
3226
Vladimir Markofcb503c2016-05-18 12:48:17 +01003227class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003228 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003229 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3230 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003231
Roland Levillain9867bc72015-08-05 10:21:34 +01003232 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003233 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003234 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003235 // In the following Evaluate methods, a HCompare instruction has
3236 // been merged into this HLessThan instruction; evaluate it as
3237 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003238 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003239 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3240 }
3241 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3242 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3243 }
3244 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3245 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003246 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003247
Dave Allison20dfc792014-06-16 20:44:29 -07003248 DECLARE_INSTRUCTION(LessThan);
3249
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003250 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003251 return kCondLT;
3252 }
3253
Mark Mendellc4701932015-04-10 13:18:51 -04003254 IfCondition GetOppositeCondition() const OVERRIDE {
3255 return kCondGE;
3256 }
3257
Dave Allison20dfc792014-06-16 20:44:29 -07003258 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003259 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003260
Dave Allison20dfc792014-06-16 20:44:29 -07003261 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3262};
3263
Vladimir Markofcb503c2016-05-18 12:48:17 +01003264class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003265 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003266 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3267 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003268
Roland Levillain9867bc72015-08-05 10:21:34 +01003269 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003270 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003271 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003272 // In the following Evaluate methods, a HCompare instruction has
3273 // been merged into this HLessThanOrEqual instruction; evaluate it as
3274 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003275 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003276 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3277 }
3278 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3279 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3280 }
3281 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3282 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003283 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003284
Dave Allison20dfc792014-06-16 20:44:29 -07003285 DECLARE_INSTRUCTION(LessThanOrEqual);
3286
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003287 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003288 return kCondLE;
3289 }
3290
Mark Mendellc4701932015-04-10 13:18:51 -04003291 IfCondition GetOppositeCondition() const OVERRIDE {
3292 return kCondGT;
3293 }
3294
Dave Allison20dfc792014-06-16 20:44:29 -07003295 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003296 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003297
Dave Allison20dfc792014-06-16 20:44:29 -07003298 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3299};
3300
Vladimir Markofcb503c2016-05-18 12:48:17 +01003301class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003302 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003303 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3304 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003305
Roland Levillain9867bc72015-08-05 10:21:34 +01003306 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003307 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003308 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003309 // In the following Evaluate methods, a HCompare instruction has
3310 // been merged into this HGreaterThan instruction; evaluate it as
3311 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003312 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003313 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3314 }
3315 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3316 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3317 }
3318 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3319 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003320 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003321
Dave Allison20dfc792014-06-16 20:44:29 -07003322 DECLARE_INSTRUCTION(GreaterThan);
3323
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003324 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003325 return kCondGT;
3326 }
3327
Mark Mendellc4701932015-04-10 13:18:51 -04003328 IfCondition GetOppositeCondition() const OVERRIDE {
3329 return kCondLE;
3330 }
3331
Dave Allison20dfc792014-06-16 20:44:29 -07003332 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003333 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003334
Dave Allison20dfc792014-06-16 20:44:29 -07003335 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3336};
3337
Vladimir Markofcb503c2016-05-18 12:48:17 +01003338class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003339 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003340 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3341 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003342
Roland Levillain9867bc72015-08-05 10:21:34 +01003343 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003344 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003345 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003346 // In the following Evaluate methods, a HCompare instruction has
3347 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3348 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003349 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003350 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3351 }
3352 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3353 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3354 }
3355 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3356 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003357 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003358
Dave Allison20dfc792014-06-16 20:44:29 -07003359 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3360
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003361 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003362 return kCondGE;
3363 }
3364
Mark Mendellc4701932015-04-10 13:18:51 -04003365 IfCondition GetOppositeCondition() const OVERRIDE {
3366 return kCondLT;
3367 }
3368
Dave Allison20dfc792014-06-16 20:44:29 -07003369 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003370 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003371
Dave Allison20dfc792014-06-16 20:44:29 -07003372 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3373};
3374
Vladimir Markofcb503c2016-05-18 12:48:17 +01003375class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003376 public:
3377 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3378 : HCondition(first, second, dex_pc) {}
3379
3380 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003381 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003382 }
3383 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003384 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3385 }
3386 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3387 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3388 LOG(FATAL) << DebugName() << " is not defined for float values";
3389 UNREACHABLE();
3390 }
3391 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3392 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3393 LOG(FATAL) << DebugName() << " is not defined for double values";
3394 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003395 }
3396
3397 DECLARE_INSTRUCTION(Below);
3398
3399 IfCondition GetCondition() const OVERRIDE {
3400 return kCondB;
3401 }
3402
3403 IfCondition GetOppositeCondition() const OVERRIDE {
3404 return kCondAE;
3405 }
3406
3407 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003408 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003409 return MakeUnsigned(x) < MakeUnsigned(y);
3410 }
Aart Bike9f37602015-10-09 11:15:55 -07003411
3412 DISALLOW_COPY_AND_ASSIGN(HBelow);
3413};
3414
Vladimir Markofcb503c2016-05-18 12:48:17 +01003415class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003416 public:
3417 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3418 : HCondition(first, second, dex_pc) {}
3419
3420 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003421 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003422 }
3423 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003424 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3425 }
3426 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3427 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3428 LOG(FATAL) << DebugName() << " is not defined for float values";
3429 UNREACHABLE();
3430 }
3431 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3432 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3433 LOG(FATAL) << DebugName() << " is not defined for double values";
3434 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003435 }
3436
3437 DECLARE_INSTRUCTION(BelowOrEqual);
3438
3439 IfCondition GetCondition() const OVERRIDE {
3440 return kCondBE;
3441 }
3442
3443 IfCondition GetOppositeCondition() const OVERRIDE {
3444 return kCondA;
3445 }
3446
3447 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003448 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003449 return MakeUnsigned(x) <= MakeUnsigned(y);
3450 }
Aart Bike9f37602015-10-09 11:15:55 -07003451
3452 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3453};
3454
Vladimir Markofcb503c2016-05-18 12:48:17 +01003455class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003456 public:
3457 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3458 : HCondition(first, second, dex_pc) {}
3459
3460 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003461 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003462 }
3463 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003464 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3465 }
3466 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3467 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3468 LOG(FATAL) << DebugName() << " is not defined for float values";
3469 UNREACHABLE();
3470 }
3471 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3472 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3473 LOG(FATAL) << DebugName() << " is not defined for double values";
3474 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003475 }
3476
3477 DECLARE_INSTRUCTION(Above);
3478
3479 IfCondition GetCondition() const OVERRIDE {
3480 return kCondA;
3481 }
3482
3483 IfCondition GetOppositeCondition() const OVERRIDE {
3484 return kCondBE;
3485 }
3486
3487 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003488 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003489 return MakeUnsigned(x) > MakeUnsigned(y);
3490 }
Aart Bike9f37602015-10-09 11:15:55 -07003491
3492 DISALLOW_COPY_AND_ASSIGN(HAbove);
3493};
3494
Vladimir Markofcb503c2016-05-18 12:48:17 +01003495class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003496 public:
3497 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3498 : HCondition(first, second, dex_pc) {}
3499
3500 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003501 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003502 }
3503 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003504 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3505 }
3506 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3507 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3508 LOG(FATAL) << DebugName() << " is not defined for float values";
3509 UNREACHABLE();
3510 }
3511 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3512 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3513 LOG(FATAL) << DebugName() << " is not defined for double values";
3514 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003515 }
3516
3517 DECLARE_INSTRUCTION(AboveOrEqual);
3518
3519 IfCondition GetCondition() const OVERRIDE {
3520 return kCondAE;
3521 }
3522
3523 IfCondition GetOppositeCondition() const OVERRIDE {
3524 return kCondB;
3525 }
3526
3527 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003528 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003529 return MakeUnsigned(x) >= MakeUnsigned(y);
3530 }
Aart Bike9f37602015-10-09 11:15:55 -07003531
3532 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3533};
Dave Allison20dfc792014-06-16 20:44:29 -07003534
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003535// Instruction to check how two inputs compare to each other.
3536// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003537class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003538 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003539 // Note that `comparison_type` is the type of comparison performed
3540 // between the comparison's inputs, not the type of the instantiated
3541 // HCompare instruction (which is always Primitive::kPrimInt).
3542 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003543 HInstruction* first,
3544 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003545 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003546 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003547 : HBinaryOperation(Primitive::kPrimInt,
3548 first,
3549 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003550 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003551 dex_pc) {
3552 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003553 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3554 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003555 }
3556
Roland Levillain9867bc72015-08-05 10:21:34 +01003557 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003558 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3559
3560 template <typename T>
3561 int32_t ComputeFP(T x, T y) const {
3562 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3563 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3564 // Handle the bias.
3565 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3566 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003567
Roland Levillain9867bc72015-08-05 10:21:34 +01003568 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003569 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3570 // reach this code path when processing a freshly built HIR
3571 // graph. However HCompare integer instructions can be synthesized
3572 // by the instruction simplifier to implement IntegerCompare and
3573 // IntegerSignum intrinsics, so we have to handle this case.
3574 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003575 }
3576 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003577 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3578 }
3579 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3580 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3581 }
3582 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3583 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003584 }
3585
Vladimir Marko372f10e2016-05-17 16:30:10 +01003586 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003587 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003588 }
3589
Vladimir Markoa1de9182016-02-25 11:37:38 +00003590 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003591
Roland Levillain31dd3d62016-02-16 12:21:02 +00003592 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003593 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003594 bool IsGtBias() const {
3595 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003596 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003597 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003598
Roland Levillain1693a1f2016-03-15 14:57:31 +00003599 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3600 // Comparisons do not require a runtime call in any back end.
3601 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003602 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003603
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003604 DECLARE_INSTRUCTION(Compare);
3605
Roland Levillain31dd3d62016-02-16 12:21:02 +00003606 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003607 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3608 static constexpr size_t kFieldComparisonBiasSize =
3609 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3610 static constexpr size_t kNumberOfComparePackedBits =
3611 kFieldComparisonBias + kFieldComparisonBiasSize;
3612 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3613 using ComparisonBiasField =
3614 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3615
Roland Levillain31dd3d62016-02-16 12:21:02 +00003616 // Return an integer constant containing the result of a comparison evaluated at compile time.
3617 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3618 DCHECK(value == -1 || value == 0 || value == 1) << value;
3619 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3620 }
3621
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003622 private:
3623 DISALLOW_COPY_AND_ASSIGN(HCompare);
3624};
3625
Vladimir Markofcb503c2016-05-18 12:48:17 +01003626class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003627 public:
3628 HNewInstance(HInstruction* cls,
3629 HCurrentMethod* current_method,
3630 uint32_t dex_pc,
3631 uint16_t type_index,
3632 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003633 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003634 bool finalizable,
3635 QuickEntrypointEnum entrypoint)
3636 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3637 type_index_(type_index),
3638 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003639 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003640 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003641 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003642 SetRawInputAt(0, cls);
3643 SetRawInputAt(1, current_method);
3644 }
3645
3646 uint16_t GetTypeIndex() const { return type_index_; }
3647 const DexFile& GetDexFile() const { return dex_file_; }
3648
3649 // Calls runtime so needs an environment.
3650 bool NeedsEnvironment() const OVERRIDE { return true; }
3651
Mingyao Yang062157f2016-03-02 10:15:36 -08003652 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3653 bool CanThrow() const OVERRIDE { return true; }
3654
3655 // Needs to call into runtime to make sure it's instantiable/accessible.
3656 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003657
Vladimir Markoa1de9182016-02-25 11:37:38 +00003658 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003659
3660 bool CanBeNull() const OVERRIDE { return false; }
3661
3662 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3663
3664 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3665 entrypoint_ = entrypoint;
3666 }
3667
3668 bool IsStringAlloc() const;
3669
3670 DECLARE_INSTRUCTION(NewInstance);
3671
3672 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003673 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3674 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003675 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3676 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3677 "Too many packed fields.");
3678
David Brazdil6de19382016-01-08 17:37:10 +00003679 const uint16_t type_index_;
3680 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003681 QuickEntrypointEnum entrypoint_;
3682
3683 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3684};
3685
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003686enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003687#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3688 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003689#include "intrinsics_list.h"
3690 kNone,
3691 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3692#undef INTRINSICS_LIST
3693#undef OPTIMIZING_INTRINSICS
3694};
3695std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3696
Agi Csaki05f20562015-08-19 14:58:14 -07003697enum IntrinsicNeedsEnvironmentOrCache {
3698 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3699 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003700};
3701
Aart Bik5d75afe2015-12-14 11:57:01 -08003702enum IntrinsicSideEffects {
3703 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3704 kReadSideEffects, // Intrinsic may read heap memory.
3705 kWriteSideEffects, // Intrinsic may write heap memory.
3706 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3707};
3708
3709enum IntrinsicExceptions {
3710 kNoThrow, // Intrinsic does not throw any exceptions.
3711 kCanThrow // Intrinsic may throw exceptions.
3712};
3713
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003714class HInvoke : public HInstruction {
3715 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003716 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003717
Vladimir Marko372f10e2016-05-17 16:30:10 +01003718 using HInstruction::GetInputRecords; // Keep the const version visible.
3719 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3720 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3721 }
3722
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003723 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003724 SetRawInputAt(index, argument);
3725 }
3726
Roland Levillain3e3d7332015-04-28 11:00:54 +01003727 // Return the number of arguments. This number can be lower than
3728 // the number of inputs returned by InputCount(), as some invoke
3729 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3730 // inputs at the end of their list of inputs.
3731 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3732
Vladimir Markoa1de9182016-02-25 11:37:38 +00003733 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003734
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003735 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003736 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003737
Vladimir Markoa1de9182016-02-25 11:37:38 +00003738 InvokeType GetOriginalInvokeType() const {
3739 return GetPackedField<OriginalInvokeTypeField>();
3740 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003741
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003742 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003743 return intrinsic_;
3744 }
3745
Aart Bik5d75afe2015-12-14 11:57:01 -08003746 void SetIntrinsic(Intrinsics intrinsic,
3747 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3748 IntrinsicSideEffects side_effects,
3749 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003750
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003751 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003752 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003753 }
3754
Vladimir Markoa1de9182016-02-25 11:37:38 +00003755 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003756
3757 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3758
Vladimir Marko372f10e2016-05-17 16:30:10 +01003759 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003760 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3761 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003762
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003763 uint32_t* GetIntrinsicOptimizations() {
3764 return &intrinsic_optimizations_;
3765 }
3766
3767 const uint32_t* GetIntrinsicOptimizations() const {
3768 return &intrinsic_optimizations_;
3769 }
3770
3771 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3772
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003773 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003774
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003775 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003776 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3777 static constexpr size_t kFieldOriginalInvokeTypeSize =
3778 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3779 static constexpr size_t kFieldReturnType =
3780 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3781 static constexpr size_t kFieldReturnTypeSize =
3782 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3783 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3784 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3785 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3786 using OriginalInvokeTypeField =
3787 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3788 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3789
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003790 HInvoke(ArenaAllocator* arena,
3791 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003792 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003793 Primitive::Type return_type,
3794 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003795 uint32_t dex_method_index,
3796 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003797 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003798 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003799 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003800 inputs_(number_of_arguments + number_of_other_inputs,
3801 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003802 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003803 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003804 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003805 SetPackedField<ReturnTypeField>(return_type);
3806 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3807 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003808 }
3809
Vladimir Markoa1de9182016-02-25 11:37:38 +00003810 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003811
Roland Levillain3e3d7332015-04-28 11:00:54 +01003812 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003813 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003814 const uint32_t dex_method_index_;
3815 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003816
3817 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3818 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003819
3820 private:
3821 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3822};
3823
Vladimir Markofcb503c2016-05-18 12:48:17 +01003824class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003825 public:
3826 HInvokeUnresolved(ArenaAllocator* arena,
3827 uint32_t number_of_arguments,
3828 Primitive::Type return_type,
3829 uint32_t dex_pc,
3830 uint32_t dex_method_index,
3831 InvokeType invoke_type)
3832 : HInvoke(arena,
3833 number_of_arguments,
3834 0u /* number_of_other_inputs */,
3835 return_type,
3836 dex_pc,
3837 dex_method_index,
3838 invoke_type) {
3839 }
3840
3841 DECLARE_INSTRUCTION(InvokeUnresolved);
3842
3843 private:
3844 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3845};
3846
Vladimir Markofcb503c2016-05-18 12:48:17 +01003847class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003848 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003849 // Requirements of this method call regarding the class
3850 // initialization (clinit) check of its declaring class.
3851 enum class ClinitCheckRequirement {
3852 kNone, // Class already initialized.
3853 kExplicit, // Static call having explicit clinit check as last input.
3854 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003855 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003856 };
3857
Vladimir Marko58155012015-08-19 12:49:41 +00003858 // Determines how to load the target ArtMethod*.
3859 enum class MethodLoadKind {
3860 // Use a String init ArtMethod* loaded from Thread entrypoints.
3861 kStringInit,
3862
3863 // Use the method's own ArtMethod* loaded by the register allocator.
3864 kRecursive,
3865
3866 // Use ArtMethod* at a known address, embed the direct address in the code.
3867 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3868 kDirectAddress,
3869
3870 // Use ArtMethod* at an address that will be known at link time, embed the direct
3871 // address in the code. If the image is relocatable, emit .patch_oat entry.
3872 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3873 // the image relocatable or not.
3874 kDirectAddressWithFixup,
3875
Vladimir Markoa1de9182016-02-25 11:37:38 +00003876 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003877 // Used when we need to use the dex cache, for example for invoke-static that
3878 // may cause class initialization (the entry may point to a resolution method),
3879 // and we know that we can access the dex cache arrays using a PC-relative load.
3880 kDexCachePcRelative,
3881
3882 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3883 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3884 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3885 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3886 kDexCacheViaMethod,
3887 };
3888
3889 // Determines the location of the code pointer.
3890 enum class CodePtrLocation {
3891 // Recursive call, use local PC-relative call instruction.
3892 kCallSelf,
3893
3894 // Use PC-relative call instruction patched at link time.
3895 // Used for calls within an oat file, boot->boot or app->app.
3896 kCallPCRelative,
3897
3898 // Call to a known target address, embed the direct address in code.
3899 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3900 kCallDirect,
3901
3902 // Call to a target address that will be known at link time, embed the direct
3903 // address in code. If the image is relocatable, emit .patch_oat entry.
3904 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3905 // the image relocatable or not.
3906 kCallDirectWithFixup,
3907
3908 // Use code pointer from the ArtMethod*.
3909 // Used when we don't know the target code. This is also the last-resort-kind used when
3910 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3911 kCallArtMethod,
3912 };
3913
3914 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003915 MethodLoadKind method_load_kind;
3916 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003917 // The method load data holds
3918 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3919 // Note that there are multiple string init methods, each having its own offset.
3920 // - the method address for kDirectAddress
3921 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003922 uint64_t method_load_data;
3923 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003924 };
3925
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003926 HInvokeStaticOrDirect(ArenaAllocator* arena,
3927 uint32_t number_of_arguments,
3928 Primitive::Type return_type,
3929 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003930 uint32_t method_index,
3931 MethodReference target_method,
3932 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003933 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003934 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003935 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003936 : HInvoke(arena,
3937 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003938 // There is potentially one extra argument for the HCurrentMethod node, and
3939 // potentially one other if the clinit check is explicit, and potentially
3940 // one other if the method is a string factory.
3941 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003942 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003943 return_type,
3944 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003945 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003946 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003947 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003948 dispatch_info_(dispatch_info) {
3949 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3950 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3951 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003952
Vladimir Markodc151b22015-10-15 18:02:30 +01003953 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003954 bool had_current_method_input = HasCurrentMethodInput();
3955 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3956
3957 // Using the current method is the default and once we find a better
3958 // method load kind, we should not go back to using the current method.
3959 DCHECK(had_current_method_input || !needs_current_method_input);
3960
3961 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003962 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3963 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003964 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003965 dispatch_info_ = dispatch_info;
3966 }
3967
Vladimir Markoc53c0792015-11-19 15:48:33 +00003968 void AddSpecialInput(HInstruction* input) {
3969 // We allow only one special input.
3970 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3971 DCHECK(InputCount() == GetSpecialInputIndex() ||
3972 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3973 InsertInputAt(GetSpecialInputIndex(), input);
3974 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003975
Vladimir Marko372f10e2016-05-17 16:30:10 +01003976 using HInstruction::GetInputRecords; // Keep the const version visible.
3977 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3978 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
3979 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
3980 DCHECK(!input_records.empty());
3981 DCHECK_GT(input_records.size(), GetNumberOfArguments());
3982 HInstruction* last_input = input_records.back().GetInstruction();
3983 // Note: `last_input` may be null during arguments setup.
3984 if (last_input != nullptr) {
3985 // `last_input` is the last input of a static invoke marked as having
3986 // an explicit clinit check. It must either be:
3987 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3988 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3989 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
3990 }
3991 }
3992 return input_records;
3993 }
3994
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003995 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003996 // We access the method via the dex cache so we can't do an implicit null check.
3997 // TODO: for intrinsics we can generate implicit null checks.
3998 return false;
3999 }
4000
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004001 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004002 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004003 }
4004
Vladimir Markoc53c0792015-11-19 15:48:33 +00004005 // Get the index of the special input, if any.
4006 //
David Brazdil6de19382016-01-08 17:37:10 +00004007 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4008 // method pointer; otherwise there may be one platform-specific special input,
4009 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004010 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004011 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004012
Vladimir Markoa1de9182016-02-25 11:37:38 +00004013 InvokeType GetOptimizedInvokeType() const {
4014 return GetPackedField<OptimizedInvokeTypeField>();
4015 }
4016
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004017 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004018 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004019 }
4020
Vladimir Marko58155012015-08-19 12:49:41 +00004021 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4022 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4023 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004024 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004025 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004026 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004027 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004028 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4029 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004030 bool HasCurrentMethodInput() const {
4031 // This function can be called only after the invoke has been fully initialized by the builder.
4032 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004033 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004034 return true;
4035 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004036 DCHECK(InputCount() == GetSpecialInputIndex() ||
4037 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004038 return false;
4039 }
4040 }
Vladimir Marko58155012015-08-19 12:49:41 +00004041 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4042 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004043 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004044
4045 int32_t GetStringInitOffset() const {
4046 DCHECK(IsStringInit());
4047 return dispatch_info_.method_load_data;
4048 }
4049
4050 uint64_t GetMethodAddress() const {
4051 DCHECK(HasMethodAddress());
4052 return dispatch_info_.method_load_data;
4053 }
4054
4055 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004056 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004057 return dispatch_info_.method_load_data;
4058 }
4059
4060 uint64_t GetDirectCodePtr() const {
4061 DCHECK(HasDirectCodePtr());
4062 return dispatch_info_.direct_code_ptr;
4063 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004064
Vladimir Markoa1de9182016-02-25 11:37:38 +00004065 ClinitCheckRequirement GetClinitCheckRequirement() const {
4066 return GetPackedField<ClinitCheckRequirementField>();
4067 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004068
Roland Levillain4c0eb422015-04-24 16:43:49 +01004069 // Is this instruction a call to a static method?
4070 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004071 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004072 }
4073
Vladimir Markofbb184a2015-11-13 14:47:00 +00004074 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4075 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4076 // instruction; only relevant for static calls with explicit clinit check.
4077 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004078 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004079 size_t last_input_index = inputs_.size() - 1u;
4080 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004081 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004082 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004083 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004084 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004085 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004086 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004087 }
4088
4089 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004090 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004091 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004092 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004093 }
4094
4095 // Is this a call to a static method whose declaring class has an
4096 // implicit intialization check requirement?
4097 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004098 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004099 }
4100
Vladimir Markob554b5a2015-11-06 12:57:55 +00004101 // Does this method load kind need the current method as an input?
4102 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4103 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4104 }
4105
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004106 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004107
Roland Levillain4c0eb422015-04-24 16:43:49 +01004108 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004109 void InsertInputAt(size_t index, HInstruction* input);
4110 void RemoveInputAt(size_t index);
4111
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004112 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004113 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4114 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4115 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4116 static constexpr size_t kFieldClinitCheckRequirement =
4117 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4118 static constexpr size_t kFieldClinitCheckRequirementSize =
4119 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4120 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4121 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4122 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4123 "Too many packed fields.");
4124 using OptimizedInvokeTypeField =
4125 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4126 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4127 kFieldClinitCheckRequirement,
4128 kFieldClinitCheckRequirementSize>;
4129
Vladimir Marko58155012015-08-19 12:49:41 +00004130 // The target method may refer to different dex file or method index than the original
4131 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4132 // in derived class to increase visibility.
4133 MethodReference target_method_;
4134 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004135
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004136 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004137};
Vladimir Markof64242a2015-12-01 14:58:23 +00004138std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004139std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004140
Vladimir Markofcb503c2016-05-18 12:48:17 +01004141class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004142 public:
4143 HInvokeVirtual(ArenaAllocator* arena,
4144 uint32_t number_of_arguments,
4145 Primitive::Type return_type,
4146 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004147 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004148 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004149 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004150 vtable_index_(vtable_index) {}
4151
Calin Juravle641547a2015-04-21 22:08:51 +01004152 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004153 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004154 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004155 }
4156
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004157 uint32_t GetVTableIndex() const { return vtable_index_; }
4158
4159 DECLARE_INSTRUCTION(InvokeVirtual);
4160
4161 private:
4162 const uint32_t vtable_index_;
4163
4164 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4165};
4166
Vladimir Markofcb503c2016-05-18 12:48:17 +01004167class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004168 public:
4169 HInvokeInterface(ArenaAllocator* arena,
4170 uint32_t number_of_arguments,
4171 Primitive::Type return_type,
4172 uint32_t dex_pc,
4173 uint32_t dex_method_index,
4174 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004175 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004176 imt_index_(imt_index) {}
4177
Calin Juravle641547a2015-04-21 22:08:51 +01004178 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004179 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004180 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004181 }
4182
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004183 uint32_t GetImtIndex() const { return imt_index_; }
4184 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4185
4186 DECLARE_INSTRUCTION(InvokeInterface);
4187
4188 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004189 const uint32_t imt_index_;
4190
4191 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4192};
4193
Vladimir Markofcb503c2016-05-18 12:48:17 +01004194class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004195 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004196 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004197 : HUnaryOperation(result_type, input, dex_pc) {
4198 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4199 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004200
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004201 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004202
4203 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004204 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004205 }
4206 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004207 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004208 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004209 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4210 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4211 }
4212 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4213 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4214 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004215
Roland Levillain88cb1752014-10-20 16:36:47 +01004216 DECLARE_INSTRUCTION(Neg);
4217
4218 private:
4219 DISALLOW_COPY_AND_ASSIGN(HNeg);
4220};
4221
Vladimir Markofcb503c2016-05-18 12:48:17 +01004222class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004223 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004224 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004225 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004226 uint32_t dex_pc,
4227 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004228 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004229 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004230 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004231 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004232 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004233 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004234 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004235 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004236 }
4237
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004238 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004239 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004240
4241 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004242 bool NeedsEnvironment() const OVERRIDE { return true; }
4243
Mingyao Yang0c365e62015-03-31 15:09:29 -07004244 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4245 bool CanThrow() const OVERRIDE { return true; }
4246
Calin Juravle10e244f2015-01-26 18:54:32 +00004247 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004248
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004249 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4250
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004251 DECLARE_INSTRUCTION(NewArray);
4252
4253 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004254 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004255 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004256 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004257
4258 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4259};
4260
Vladimir Markofcb503c2016-05-18 12:48:17 +01004261class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004262 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004263 HAdd(Primitive::Type result_type,
4264 HInstruction* left,
4265 HInstruction* right,
4266 uint32_t dex_pc = kNoDexPc)
4267 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004268
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004269 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004270
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004271 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004272
4273 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004274 return GetBlock()->GetGraph()->GetIntConstant(
4275 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004276 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004277 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004278 return GetBlock()->GetGraph()->GetLongConstant(
4279 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004280 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004281 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4282 return GetBlock()->GetGraph()->GetFloatConstant(
4283 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4284 }
4285 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4286 return GetBlock()->GetGraph()->GetDoubleConstant(
4287 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4288 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004289
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004290 DECLARE_INSTRUCTION(Add);
4291
4292 private:
4293 DISALLOW_COPY_AND_ASSIGN(HAdd);
4294};
4295
Vladimir Markofcb503c2016-05-18 12:48:17 +01004296class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004297 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004298 HSub(Primitive::Type result_type,
4299 HInstruction* left,
4300 HInstruction* right,
4301 uint32_t dex_pc = kNoDexPc)
4302 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004303
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004304 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004305
4306 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004307 return GetBlock()->GetGraph()->GetIntConstant(
4308 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004309 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004310 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004311 return GetBlock()->GetGraph()->GetLongConstant(
4312 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004313 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004314 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4315 return GetBlock()->GetGraph()->GetFloatConstant(
4316 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4317 }
4318 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4319 return GetBlock()->GetGraph()->GetDoubleConstant(
4320 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4321 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004322
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004323 DECLARE_INSTRUCTION(Sub);
4324
4325 private:
4326 DISALLOW_COPY_AND_ASSIGN(HSub);
4327};
4328
Vladimir Markofcb503c2016-05-18 12:48:17 +01004329class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004330 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004331 HMul(Primitive::Type result_type,
4332 HInstruction* left,
4333 HInstruction* right,
4334 uint32_t dex_pc = kNoDexPc)
4335 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004336
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004337 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004338
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004339 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004340
4341 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004342 return GetBlock()->GetGraph()->GetIntConstant(
4343 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004344 }
4345 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004346 return GetBlock()->GetGraph()->GetLongConstant(
4347 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004348 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004349 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4350 return GetBlock()->GetGraph()->GetFloatConstant(
4351 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4352 }
4353 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4354 return GetBlock()->GetGraph()->GetDoubleConstant(
4355 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4356 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004357
4358 DECLARE_INSTRUCTION(Mul);
4359
4360 private:
4361 DISALLOW_COPY_AND_ASSIGN(HMul);
4362};
4363
Vladimir Markofcb503c2016-05-18 12:48:17 +01004364class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004365 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004366 HDiv(Primitive::Type result_type,
4367 HInstruction* left,
4368 HInstruction* right,
4369 uint32_t dex_pc)
4370 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004371
Roland Levillain9867bc72015-08-05 10:21:34 +01004372 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004373 T ComputeIntegral(T x, T y) const {
4374 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004375 // Our graph structure ensures we never have 0 for `y` during
4376 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004377 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004378 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004379 return (y == -1) ? -x : x / y;
4380 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004381
Roland Levillain31dd3d62016-02-16 12:21:02 +00004382 template <typename T>
4383 T ComputeFP(T x, T y) const {
4384 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4385 return x / y;
4386 }
4387
Roland Levillain9867bc72015-08-05 10:21:34 +01004388 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004389 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004390 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004391 }
4392 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004393 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004394 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4395 }
4396 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4397 return GetBlock()->GetGraph()->GetFloatConstant(
4398 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4399 }
4400 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4401 return GetBlock()->GetGraph()->GetDoubleConstant(
4402 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004403 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004404
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004405 static SideEffects SideEffectsForArchRuntimeCalls() {
4406 // The generated code can use a runtime call.
4407 return SideEffects::CanTriggerGC();
4408 }
4409
Calin Juravle7c4954d2014-10-28 16:57:40 +00004410 DECLARE_INSTRUCTION(Div);
4411
4412 private:
4413 DISALLOW_COPY_AND_ASSIGN(HDiv);
4414};
4415
Vladimir Markofcb503c2016-05-18 12:48:17 +01004416class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004417 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004418 HRem(Primitive::Type result_type,
4419 HInstruction* left,
4420 HInstruction* right,
4421 uint32_t dex_pc)
4422 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004423
Roland Levillain9867bc72015-08-05 10:21:34 +01004424 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004425 T ComputeIntegral(T x, T y) const {
4426 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004427 // Our graph structure ensures we never have 0 for `y` during
4428 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004429 DCHECK_NE(y, 0);
4430 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4431 return (y == -1) ? 0 : x % y;
4432 }
4433
Roland Levillain31dd3d62016-02-16 12:21:02 +00004434 template <typename T>
4435 T ComputeFP(T x, T y) const {
4436 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4437 return std::fmod(x, y);
4438 }
4439
Roland Levillain9867bc72015-08-05 10:21:34 +01004440 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004441 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004442 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004443 }
4444 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004445 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004446 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004447 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004448 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4449 return GetBlock()->GetGraph()->GetFloatConstant(
4450 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4451 }
4452 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4453 return GetBlock()->GetGraph()->GetDoubleConstant(
4454 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4455 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004456
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004457 static SideEffects SideEffectsForArchRuntimeCalls() {
4458 return SideEffects::CanTriggerGC();
4459 }
4460
Calin Juravlebacfec32014-11-14 15:54:36 +00004461 DECLARE_INSTRUCTION(Rem);
4462
4463 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004464 DISALLOW_COPY_AND_ASSIGN(HRem);
4465};
4466
Vladimir Markofcb503c2016-05-18 12:48:17 +01004467class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004468 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004469 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4470 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004471 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004472 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004473 SetRawInputAt(0, value);
4474 }
4475
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004476 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4477
Calin Juravled0d48522014-11-04 16:40:20 +00004478 bool CanBeMoved() const OVERRIDE { return true; }
4479
Vladimir Marko372f10e2016-05-17 16:30:10 +01004480 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004481 return true;
4482 }
4483
4484 bool NeedsEnvironment() const OVERRIDE { return true; }
4485 bool CanThrow() const OVERRIDE { return true; }
4486
Calin Juravled0d48522014-11-04 16:40:20 +00004487 DECLARE_INSTRUCTION(DivZeroCheck);
4488
4489 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004490 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4491};
4492
Vladimir Markofcb503c2016-05-18 12:48:17 +01004493class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004494 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004495 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004496 HInstruction* value,
4497 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004498 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004499 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4500 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4501 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004502 }
4503
Roland Levillain5b5b9312016-03-22 14:57:31 +00004504 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004505 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004506 return value << (distance & max_shift_distance);
4507 }
4508
4509 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004510 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004511 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004512 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004513 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004514 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004515 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004516 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004517 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4518 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4519 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4520 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004521 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004522 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4523 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004524 LOG(FATAL) << DebugName() << " is not defined for float values";
4525 UNREACHABLE();
4526 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004527 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4528 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004529 LOG(FATAL) << DebugName() << " is not defined for double values";
4530 UNREACHABLE();
4531 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004532
4533 DECLARE_INSTRUCTION(Shl);
4534
4535 private:
4536 DISALLOW_COPY_AND_ASSIGN(HShl);
4537};
4538
Vladimir Markofcb503c2016-05-18 12:48:17 +01004539class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004540 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004541 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004542 HInstruction* value,
4543 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004544 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004545 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4546 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4547 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004548 }
4549
Roland Levillain5b5b9312016-03-22 14:57:31 +00004550 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004551 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004552 return value >> (distance & max_shift_distance);
4553 }
4554
4555 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004556 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004557 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004558 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004559 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004560 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004561 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004562 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004563 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4564 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4565 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4566 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004567 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004568 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4569 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004570 LOG(FATAL) << DebugName() << " is not defined for float values";
4571 UNREACHABLE();
4572 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004573 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4574 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004575 LOG(FATAL) << DebugName() << " is not defined for double values";
4576 UNREACHABLE();
4577 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004578
4579 DECLARE_INSTRUCTION(Shr);
4580
4581 private:
4582 DISALLOW_COPY_AND_ASSIGN(HShr);
4583};
4584
Vladimir Markofcb503c2016-05-18 12:48:17 +01004585class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004586 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004587 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004588 HInstruction* value,
4589 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004590 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004591 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4592 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4593 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004594 }
4595
Roland Levillain5b5b9312016-03-22 14:57:31 +00004596 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004597 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004598 typedef typename std::make_unsigned<T>::type V;
4599 V ux = static_cast<V>(value);
4600 return static_cast<T>(ux >> (distance & max_shift_distance));
4601 }
4602
4603 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004604 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004605 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004606 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004607 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004608 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004609 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004610 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004611 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4612 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4613 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4614 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004615 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004616 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4617 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004618 LOG(FATAL) << DebugName() << " is not defined for float values";
4619 UNREACHABLE();
4620 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004621 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4622 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004623 LOG(FATAL) << DebugName() << " is not defined for double values";
4624 UNREACHABLE();
4625 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004626
4627 DECLARE_INSTRUCTION(UShr);
4628
4629 private:
4630 DISALLOW_COPY_AND_ASSIGN(HUShr);
4631};
4632
Vladimir Markofcb503c2016-05-18 12:48:17 +01004633class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004634 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004635 HAnd(Primitive::Type result_type,
4636 HInstruction* left,
4637 HInstruction* right,
4638 uint32_t dex_pc = kNoDexPc)
4639 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004640
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004641 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004642
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004643 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004644
4645 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004646 return GetBlock()->GetGraph()->GetIntConstant(
4647 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004648 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004649 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004650 return GetBlock()->GetGraph()->GetLongConstant(
4651 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004652 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004653 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4654 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4655 LOG(FATAL) << DebugName() << " is not defined for float values";
4656 UNREACHABLE();
4657 }
4658 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4659 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4660 LOG(FATAL) << DebugName() << " is not defined for double values";
4661 UNREACHABLE();
4662 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004663
4664 DECLARE_INSTRUCTION(And);
4665
4666 private:
4667 DISALLOW_COPY_AND_ASSIGN(HAnd);
4668};
4669
Vladimir Markofcb503c2016-05-18 12:48:17 +01004670class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004671 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004672 HOr(Primitive::Type result_type,
4673 HInstruction* left,
4674 HInstruction* right,
4675 uint32_t dex_pc = kNoDexPc)
4676 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004677
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004678 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004679
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004680 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004681
4682 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004683 return GetBlock()->GetGraph()->GetIntConstant(
4684 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004685 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004686 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004687 return GetBlock()->GetGraph()->GetLongConstant(
4688 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004689 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004690 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4691 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4692 LOG(FATAL) << DebugName() << " is not defined for float values";
4693 UNREACHABLE();
4694 }
4695 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4696 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4697 LOG(FATAL) << DebugName() << " is not defined for double values";
4698 UNREACHABLE();
4699 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004700
4701 DECLARE_INSTRUCTION(Or);
4702
4703 private:
4704 DISALLOW_COPY_AND_ASSIGN(HOr);
4705};
4706
Vladimir Markofcb503c2016-05-18 12:48:17 +01004707class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004708 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004709 HXor(Primitive::Type result_type,
4710 HInstruction* left,
4711 HInstruction* right,
4712 uint32_t dex_pc = kNoDexPc)
4713 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004714
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004715 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004716
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004717 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004718
4719 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004720 return GetBlock()->GetGraph()->GetIntConstant(
4721 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004722 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004723 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004724 return GetBlock()->GetGraph()->GetLongConstant(
4725 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004726 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004727 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4728 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4729 LOG(FATAL) << DebugName() << " is not defined for float values";
4730 UNREACHABLE();
4731 }
4732 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4733 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4734 LOG(FATAL) << DebugName() << " is not defined for double values";
4735 UNREACHABLE();
4736 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004737
4738 DECLARE_INSTRUCTION(Xor);
4739
4740 private:
4741 DISALLOW_COPY_AND_ASSIGN(HXor);
4742};
4743
Vladimir Markofcb503c2016-05-18 12:48:17 +01004744class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004745 public:
4746 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004747 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004748 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4749 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004750 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004751
Roland Levillain5b5b9312016-03-22 14:57:31 +00004752 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004753 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004754 typedef typename std::make_unsigned<T>::type V;
4755 V ux = static_cast<V>(value);
4756 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004757 return static_cast<T>(ux);
4758 } else {
4759 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004760 return static_cast<T>(ux >> (distance & max_shift_value)) |
4761 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004762 }
4763 }
4764
Roland Levillain5b5b9312016-03-22 14:57:31 +00004765 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004766 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004767 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004768 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004769 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004770 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004771 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004772 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004773 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4774 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4775 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4776 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004777 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004778 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4779 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004780 LOG(FATAL) << DebugName() << " is not defined for float values";
4781 UNREACHABLE();
4782 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004783 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4784 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004785 LOG(FATAL) << DebugName() << " is not defined for double values";
4786 UNREACHABLE();
4787 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004788
4789 DECLARE_INSTRUCTION(Ror);
4790
4791 private:
4792 DISALLOW_COPY_AND_ASSIGN(HRor);
4793};
4794
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004795// The value of a parameter in this method. Its location depends on
4796// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004797class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004798 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004799 HParameterValue(const DexFile& dex_file,
4800 uint16_t type_index,
4801 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004802 Primitive::Type parameter_type,
4803 bool is_this = false)
4804 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004805 dex_file_(dex_file),
4806 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004807 index_(index) {
4808 SetPackedFlag<kFlagIsThis>(is_this);
4809 SetPackedFlag<kFlagCanBeNull>(!is_this);
4810 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004811
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004812 const DexFile& GetDexFile() const { return dex_file_; }
4813 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004814 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004815 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004816
Vladimir Markoa1de9182016-02-25 11:37:38 +00004817 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4818 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004819
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004820 DECLARE_INSTRUCTION(ParameterValue);
4821
4822 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004823 // Whether or not the parameter value corresponds to 'this' argument.
4824 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4825 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4826 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4827 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4828 "Too many packed fields.");
4829
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004830 const DexFile& dex_file_;
4831 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004832 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004833 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004834 const uint8_t index_;
4835
4836 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4837};
4838
Vladimir Markofcb503c2016-05-18 12:48:17 +01004839class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004840 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004841 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4842 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004843
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004844 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004845 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004846 return true;
4847 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004848
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004849 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004850
4851 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004852 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004853 }
4854 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004855 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004856 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004857 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4858 LOG(FATAL) << DebugName() << " is not defined for float values";
4859 UNREACHABLE();
4860 }
4861 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4862 LOG(FATAL) << DebugName() << " is not defined for double values";
4863 UNREACHABLE();
4864 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004865
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004866 DECLARE_INSTRUCTION(Not);
4867
4868 private:
4869 DISALLOW_COPY_AND_ASSIGN(HNot);
4870};
4871
Vladimir Markofcb503c2016-05-18 12:48:17 +01004872class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004873 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004874 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4875 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004876
4877 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004878 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004879 return true;
4880 }
4881
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004882 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004883 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004884 return !x;
4885 }
4886
Roland Levillain9867bc72015-08-05 10:21:34 +01004887 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004888 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004889 }
4890 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4891 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004892 UNREACHABLE();
4893 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004894 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4895 LOG(FATAL) << DebugName() << " is not defined for float values";
4896 UNREACHABLE();
4897 }
4898 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4899 LOG(FATAL) << DebugName() << " is not defined for double values";
4900 UNREACHABLE();
4901 }
David Brazdil66d126e2015-04-03 16:02:44 +01004902
4903 DECLARE_INSTRUCTION(BooleanNot);
4904
4905 private:
4906 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4907};
4908
Vladimir Markofcb503c2016-05-18 12:48:17 +01004909class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004910 public:
4911 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004912 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004913 : HExpression(result_type,
4914 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4915 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004916 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004917 // Invariant: We should never generate a conversion to a Boolean value.
4918 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004919 }
4920
4921 HInstruction* GetInput() const { return InputAt(0); }
4922 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4923 Primitive::Type GetResultType() const { return GetType(); }
4924
4925 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004926 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4927 return true;
4928 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004929
Mark Mendelle82549b2015-05-06 10:55:34 -04004930 // Try to statically evaluate the conversion and return a HConstant
4931 // containing the result. If the input cannot be converted, return nullptr.
4932 HConstant* TryStaticEvaluation() const;
4933
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004934 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4935 Primitive::Type result_type) {
4936 // Some architectures may not require the 'GC' side effects, but at this point
4937 // in the compilation process we do not know what architecture we will
4938 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004939 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4940 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004941 return SideEffects::CanTriggerGC();
4942 }
4943 return SideEffects::None();
4944 }
4945
Roland Levillaindff1f282014-11-05 14:15:05 +00004946 DECLARE_INSTRUCTION(TypeConversion);
4947
4948 private:
4949 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4950};
4951
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004952static constexpr uint32_t kNoRegNumber = -1;
4953
Vladimir Markofcb503c2016-05-18 12:48:17 +01004954class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004955 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004956 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4957 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004958 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004959 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004960 SetRawInputAt(0, value);
4961 }
4962
Calin Juravle10e244f2015-01-26 18:54:32 +00004963 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004964 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004965 return true;
4966 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004967
Calin Juravle10e244f2015-01-26 18:54:32 +00004968 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004969
Calin Juravle10e244f2015-01-26 18:54:32 +00004970 bool CanThrow() const OVERRIDE { return true; }
4971
4972 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004973
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004974
4975 DECLARE_INSTRUCTION(NullCheck);
4976
4977 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004978 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4979};
4980
4981class FieldInfo : public ValueObject {
4982 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004983 FieldInfo(MemberOffset field_offset,
4984 Primitive::Type field_type,
4985 bool is_volatile,
4986 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004987 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004988 const DexFile& dex_file,
4989 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004990 : field_offset_(field_offset),
4991 field_type_(field_type),
4992 is_volatile_(is_volatile),
4993 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004994 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004995 dex_file_(dex_file),
4996 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004997
4998 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004999 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005000 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005001 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005002 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005003 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005004 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005005
5006 private:
5007 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005008 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005009 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005010 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005011 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005012 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005013 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005014};
5015
Vladimir Markofcb503c2016-05-18 12:48:17 +01005016class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005017 public:
5018 HInstanceFieldGet(HInstruction* value,
5019 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005020 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005021 bool is_volatile,
5022 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005023 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005024 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005025 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005026 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005027 : HExpression(field_type,
Serban Constantinescufca16662016-07-14 09:21:59 +01005028 SideEffectsForArchRuntimeCalls(field_type, is_volatile),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005029 dex_pc),
5030 field_info_(field_offset,
5031 field_type,
5032 is_volatile,
5033 field_idx,
5034 declaring_class_def_index,
5035 dex_file,
5036 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005037 SetRawInputAt(0, value);
5038 }
5039
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005040 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005041
Vladimir Marko372f10e2016-05-17 16:30:10 +01005042 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5043 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005044 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005045 }
5046
Calin Juravle641547a2015-04-21 22:08:51 +01005047 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005048 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005049 }
5050
5051 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005052 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5053 }
5054
Calin Juravle52c48962014-12-16 17:02:57 +00005055 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005056 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005057 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005058 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005059
Serban Constantinescufca16662016-07-14 09:21:59 +01005060 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type field_type, bool is_volatile) {
5061 SideEffects side_effects = SideEffects::FieldReadOfType(field_type, is_volatile);
5062
5063 // MIPS delegates volatile kPrimLong and kPrimDouble loads to a runtime helper.
5064 if (Primitive::Is64BitType(field_type)) {
5065 side_effects.Add(SideEffects::CanTriggerGC());
5066 }
5067 return side_effects;
5068 }
5069
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005070 DECLARE_INSTRUCTION(InstanceFieldGet);
5071
5072 private:
5073 const FieldInfo field_info_;
5074
5075 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5076};
5077
Vladimir Markofcb503c2016-05-18 12:48:17 +01005078class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005079 public:
5080 HInstanceFieldSet(HInstruction* object,
5081 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005082 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005083 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005084 bool is_volatile,
5085 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005086 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005087 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005088 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005089 uint32_t dex_pc)
Serban Constantinescufca16662016-07-14 09:21:59 +01005090 : HTemplateInstruction(SideEffectsForArchRuntimeCalls(field_type, is_volatile),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005091 dex_pc),
5092 field_info_(field_offset,
5093 field_type,
5094 is_volatile,
5095 field_idx,
5096 declaring_class_def_index,
5097 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005098 dex_cache) {
5099 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005100 SetRawInputAt(0, object);
5101 SetRawInputAt(1, value);
5102 }
5103
Calin Juravle641547a2015-04-21 22:08:51 +01005104 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005105 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005106 }
5107
Calin Juravle52c48962014-12-16 17:02:57 +00005108 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005109 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005110 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005111 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005112 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005113 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5114 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005115
Serban Constantinescufca16662016-07-14 09:21:59 +01005116 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type field_type, bool is_volatile) {
5117 SideEffects side_effects = SideEffects::FieldWriteOfType(field_type, is_volatile);
5118
5119 // MIPS delegates volatile kPrimLong and kPrimDouble stores to a runtime helper.
5120 if (Primitive::Is64BitType(field_type)) {
5121 side_effects.Add(SideEffects::CanTriggerGC());
5122 }
5123 return side_effects;
5124 }
5125
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005126 DECLARE_INSTRUCTION(InstanceFieldSet);
5127
5128 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005129 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5130 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5131 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5132 "Too many packed fields.");
5133
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005134 const FieldInfo field_info_;
5135
5136 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5137};
5138
Vladimir Markofcb503c2016-05-18 12:48:17 +01005139class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005140 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005141 HArrayGet(HInstruction* array,
5142 HInstruction* index,
5143 Primitive::Type type,
5144 uint32_t dex_pc,
5145 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005146 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005147 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005148 SetRawInputAt(0, array);
5149 SetRawInputAt(1, index);
5150 }
5151
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005152 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005153 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005154 return true;
5155 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005156 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005157 // TODO: We can be smarter here.
5158 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5159 // which generates the implicit null check. There are cases when these can be removed
5160 // to produce better code. If we ever add optimizations to do so we should allow an
5161 // implicit check here (as long as the address falls in the first page).
5162 return false;
5163 }
5164
David Brazdil4833f5a2015-12-16 10:37:39 +00005165 bool IsEquivalentOf(HArrayGet* other) const {
5166 bool result = (GetDexPc() == other->GetDexPc());
5167 if (kIsDebugBuild && result) {
5168 DCHECK_EQ(GetBlock(), other->GetBlock());
5169 DCHECK_EQ(GetArray(), other->GetArray());
5170 DCHECK_EQ(GetIndex(), other->GetIndex());
5171 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005172 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005173 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005174 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5175 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005176 }
5177 }
5178 return result;
5179 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005180
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005181 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5182
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005183 HInstruction* GetArray() const { return InputAt(0); }
5184 HInstruction* GetIndex() const { return InputAt(1); }
5185
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005186 DECLARE_INSTRUCTION(ArrayGet);
5187
5188 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005189 // We treat a String as an array, creating the HArrayGet from String.charAt()
5190 // intrinsic in the instruction simplifier. We can always determine whether
5191 // a particular HArrayGet is actually a String.charAt() by looking at the type
5192 // of the input but that requires holding the mutator lock, so we prefer to use
5193 // a flag, so that code generators don't need to do the locking.
5194 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5195 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5196 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5197 "Too many packed fields.");
5198
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005199 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5200};
5201
Vladimir Markofcb503c2016-05-18 12:48:17 +01005202class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005203 public:
5204 HArraySet(HInstruction* array,
5205 HInstruction* index,
5206 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005207 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005208 uint32_t dex_pc)
5209 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005210 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5211 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5212 SetPackedFlag<kFlagValueCanBeNull>(true);
5213 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005214 SetRawInputAt(0, array);
5215 SetRawInputAt(1, index);
5216 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005217 // Make a best guess now, may be refined during SSA building.
5218 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005219 }
5220
Calin Juravle77520bc2015-01-12 18:45:46 +00005221 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005222 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005223 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005224 }
5225
Mingyao Yang81014cb2015-06-02 03:16:27 -07005226 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005227 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005228
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005229 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005230 // TODO: Same as for ArrayGet.
5231 return false;
5232 }
5233
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005234 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005235 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005236 }
5237
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005238 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005239 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005240 }
5241
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005242 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005243 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005244 }
5245
Vladimir Markoa1de9182016-02-25 11:37:38 +00005246 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5247 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5248 bool StaticTypeOfArrayIsObjectArray() const {
5249 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5250 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005251
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005252 HInstruction* GetArray() const { return InputAt(0); }
5253 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005254 HInstruction* GetValue() const { return InputAt(2); }
5255
5256 Primitive::Type GetComponentType() const {
5257 // The Dex format does not type floating point index operations. Since the
5258 // `expected_component_type_` is set during building and can therefore not
5259 // be correct, we also check what is the value type. If it is a floating
5260 // point type, we must use that type.
5261 Primitive::Type value_type = GetValue()->GetType();
5262 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5263 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005264 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005265 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005266
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005267 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005268 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005269 }
5270
Aart Bik18b36ab2016-04-13 16:41:35 -07005271 void ComputeSideEffects() {
5272 Primitive::Type type = GetComponentType();
5273 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5274 SideEffectsForArchRuntimeCalls(type)));
5275 }
5276
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005277 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5278 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5279 }
5280
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005281 DECLARE_INSTRUCTION(ArraySet);
5282
5283 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005284 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5285 static constexpr size_t kFieldExpectedComponentTypeSize =
5286 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5287 static constexpr size_t kFlagNeedsTypeCheck =
5288 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5289 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005290 // Cached information for the reference_type_info_ so that codegen
5291 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005292 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5293 static constexpr size_t kNumberOfArraySetPackedBits =
5294 kFlagStaticTypeOfArrayIsObjectArray + 1;
5295 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5296 using ExpectedComponentTypeField =
5297 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005298
5299 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5300};
5301
Vladimir Markofcb503c2016-05-18 12:48:17 +01005302class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005303 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005304 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005305 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005306 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005307 // Note that arrays do not change length, so the instruction does not
5308 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005309 SetRawInputAt(0, array);
5310 }
5311
Calin Juravle77520bc2015-01-12 18:45:46 +00005312 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005313 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005314 return true;
5315 }
Calin Juravle641547a2015-04-21 22:08:51 +01005316 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5317 return obj == InputAt(0);
5318 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005319
Vladimir Markodce016e2016-04-28 13:10:02 +01005320 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5321
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005322 DECLARE_INSTRUCTION(ArrayLength);
5323
5324 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005325 // We treat a String as an array, creating the HArrayLength from String.length()
5326 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5327 // determine whether a particular HArrayLength is actually a String.length() by
5328 // looking at the type of the input but that requires holding the mutator lock, so
5329 // we prefer to use a flag, so that code generators don't need to do the locking.
5330 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5331 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5332 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5333 "Too many packed fields.");
5334
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005335 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5336};
5337
Vladimir Markofcb503c2016-05-18 12:48:17 +01005338class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005339 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005340 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5341 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005342 HBoundsCheck(HInstruction* index,
5343 HInstruction* length,
5344 uint32_t dex_pc,
5345 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5346 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5347 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005348 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005349 SetRawInputAt(0, index);
5350 SetRawInputAt(1, length);
5351 }
5352
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005353 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005354 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005355 return true;
5356 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005357
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005358 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005359
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005360 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005361
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005362 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5363 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5364
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005365 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005366
5367 DECLARE_INSTRUCTION(BoundsCheck);
5368
5369 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005370 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5371 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5372 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5373 // so we need to create an HEnvironment which will be translated to an InlineInfo
5374 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5375 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5376 // from the invoke as we don't want to look again at the dex bytecode.
5377 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5378
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005379 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5380};
5381
Vladimir Markofcb503c2016-05-18 12:48:17 +01005382class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005383 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005384 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005385 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005386
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005387 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005388 return true;
5389 }
5390
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005391 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5392 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005393
5394 DECLARE_INSTRUCTION(SuspendCheck);
5395
5396 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005397 // Only used for code generation, in order to share the same slow path between back edges
5398 // of a same loop.
5399 SlowPathCode* slow_path_;
5400
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005401 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5402};
5403
David Srbecky0cf44932015-12-09 14:09:59 +00005404// Pseudo-instruction which provides the native debugger with mapping information.
5405// It ensures that we can generate line number and local variables at this point.
5406class HNativeDebugInfo : public HTemplateInstruction<0> {
5407 public:
5408 explicit HNativeDebugInfo(uint32_t dex_pc)
5409 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5410
5411 bool NeedsEnvironment() const OVERRIDE {
5412 return true;
5413 }
5414
5415 DECLARE_INSTRUCTION(NativeDebugInfo);
5416
5417 private:
5418 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5419};
5420
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005421/**
5422 * Instruction to load a Class object.
5423 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005424class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005425 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005426 // Determines how to load the Class.
5427 enum class LoadKind {
5428 // Use the Class* from the method's own ArtMethod*.
5429 kReferrersClass,
5430
5431 // Use boot image Class* address that will be known at link time.
5432 // Used for boot image classes referenced by boot image code in non-PIC mode.
5433 kBootImageLinkTimeAddress,
5434
5435 // Use PC-relative boot image Class* address that will be known at link time.
5436 // Used for boot image classes referenced by boot image code in PIC mode.
5437 kBootImageLinkTimePcRelative,
5438
5439 // Use a known boot image Class* address, embedded in the code by the codegen.
5440 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5441 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5442 // GetIncludePatchInformation().
5443 kBootImageAddress,
5444
5445 // Load from the resolved types array at an absolute address.
5446 // Used for classes outside the boot image referenced by JIT-compiled code.
5447 kDexCacheAddress,
5448
5449 // Load from resolved types array in the dex cache using a PC-relative load.
5450 // Used for classes outside boot image when we know that we can access
5451 // the dex cache arrays using a PC-relative load.
5452 kDexCachePcRelative,
5453
5454 // Load from resolved types array accessed through the class loaded from
5455 // the compiled method's own ArtMethod*. This is the default access type when
5456 // all other types are unavailable.
5457 kDexCacheViaMethod,
5458
5459 kLast = kDexCacheViaMethod
5460 };
5461
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005462 HLoadClass(HCurrentMethod* current_method,
5463 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005464 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005465 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005466 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005467 bool needs_access_check,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005468 bool is_in_dex_cache,
5469 bool is_in_boot_image)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005470 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5471 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005472 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005473 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005474 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005475 // Referrers class should not need access check. We never inline unverified
5476 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005477 DCHECK(!is_referrers_class || !needs_access_check);
5478
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005479 SetPackedField<LoadKindField>(
5480 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005481 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5482 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005483 SetPackedFlag<kFlagIsInBootImage>(is_in_boot_image);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005484 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005485 }
5486
5487 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5488 DCHECK(HasAddress(load_kind));
5489 load_data_.address = address;
5490 SetLoadKindInternal(load_kind);
5491 }
5492
5493 void SetLoadKindWithTypeReference(LoadKind load_kind,
5494 const DexFile& dex_file,
5495 uint32_t type_index) {
5496 DCHECK(HasTypeReference(load_kind));
5497 DCHECK(IsSameDexFile(dex_file_, dex_file));
5498 DCHECK_EQ(type_index_, type_index);
5499 SetLoadKindInternal(load_kind);
5500 }
5501
5502 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5503 const DexFile& dex_file,
5504 uint32_t element_index) {
5505 DCHECK(HasDexCacheReference(load_kind));
5506 DCHECK(IsSameDexFile(dex_file_, dex_file));
5507 load_data_.dex_cache_element_index = element_index;
5508 SetLoadKindInternal(load_kind);
5509 }
5510
5511 LoadKind GetLoadKind() const {
5512 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005513 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005514
5515 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005516
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005517 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005518
5519 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5520
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005521 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005522
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005523 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005524 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005525 }
5526
Calin Juravle0ba218d2015-05-19 18:46:01 +01005527 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005528 // The entrypoint the code generator is going to call does not do
5529 // clinit of the class.
5530 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005531 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005532 }
5533
5534 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005535 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005536 (!IsReferrersClass() && !IsInDexCache()) ||
5537 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005538 }
5539
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005540
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005541 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005542 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005543 }
5544
Calin Juravleacf735c2015-02-12 15:25:22 +00005545 ReferenceTypeInfo GetLoadedClassRTI() {
5546 return loaded_class_rti_;
5547 }
5548
5549 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5550 // Make sure we only set exact types (the loaded class should never be merged).
5551 DCHECK(rti.IsExact());
5552 loaded_class_rti_ = rti;
5553 }
5554
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005555 uint32_t GetTypeIndex() const { return type_index_; }
5556 const DexFile& GetDexFile() const { return dex_file_; }
5557
5558 uint32_t GetDexCacheElementOffset() const;
5559
5560 uint64_t GetAddress() const {
5561 DCHECK(HasAddress(GetLoadKind()));
5562 return load_data_.address;
5563 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005564
Vladimir Markoa1de9182016-02-25 11:37:38 +00005565 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005566
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005567 static SideEffects SideEffectsForArchRuntimeCalls() {
5568 return SideEffects::CanTriggerGC();
5569 }
5570
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005571 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005572 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5573 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005574 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005575 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005576
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005577 void MarkInDexCache() {
5578 SetPackedFlag<kFlagIsInDexCache>(true);
5579 DCHECK(!NeedsEnvironment());
5580 RemoveEnvironment();
5581 SetSideEffects(SideEffects::None());
5582 }
5583
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005584 void MarkInBootImage() {
5585 SetPackedFlag<kFlagIsInBootImage>(true);
5586 }
5587
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005588 void AddSpecialInput(HInstruction* special_input);
5589
5590 using HInstruction::GetInputRecords; // Keep the const version visible.
5591 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5592 return ArrayRef<HUserRecord<HInstruction*>>(
5593 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5594 }
5595
5596 Primitive::Type GetType() const OVERRIDE {
5597 return Primitive::kPrimNot;
5598 }
5599
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005600 DECLARE_INSTRUCTION(LoadClass);
5601
5602 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005603 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005604 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005605 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005606 // Whether this instruction must generate the initialization check.
5607 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005608 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005609 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5610 static constexpr size_t kFieldLoadKindSize =
5611 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5612 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005613 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005614 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5615
5616 static bool HasTypeReference(LoadKind load_kind) {
5617 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5618 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5619 load_kind == LoadKind::kDexCacheViaMethod ||
5620 load_kind == LoadKind::kReferrersClass;
5621 }
5622
5623 static bool HasAddress(LoadKind load_kind) {
5624 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5625 }
5626
5627 static bool HasDexCacheReference(LoadKind load_kind) {
5628 return load_kind == LoadKind::kDexCachePcRelative;
5629 }
5630
5631 void SetLoadKindInternal(LoadKind load_kind);
5632
5633 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5634 // For other load kinds it's empty or possibly some architecture-specific instruction
5635 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5636 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005637
5638 const uint16_t type_index_;
5639 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005640
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005641 union {
5642 uint32_t dex_cache_element_index; // Only for dex cache reference.
5643 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5644 } load_data_;
5645
Calin Juravleacf735c2015-02-12 15:25:22 +00005646 ReferenceTypeInfo loaded_class_rti_;
5647
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005648 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5649};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005650std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5651
5652// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5653inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5654 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5655 return load_data_.dex_cache_element_index;
5656}
5657
5658// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5659inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005660 // The special input is used for PC-relative loads on some architectures,
5661 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005662 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005663 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5664 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5665 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005666 DCHECK(special_input_.GetInstruction() == nullptr);
5667 special_input_ = HUserRecord<HInstruction*>(special_input);
5668 special_input->AddUseAt(this, 0);
5669}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005670
Vladimir Marko372f10e2016-05-17 16:30:10 +01005671class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005672 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005673 // Determines how to load the String.
5674 enum class LoadKind {
5675 // Use boot image String* address that will be known at link time.
5676 // Used for boot image strings referenced by boot image code in non-PIC mode.
5677 kBootImageLinkTimeAddress,
5678
5679 // Use PC-relative boot image String* address that will be known at link time.
5680 // Used for boot image strings referenced by boot image code in PIC mode.
5681 kBootImageLinkTimePcRelative,
5682
5683 // Use a known boot image String* address, embedded in the code by the codegen.
5684 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5685 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5686 // GetIncludePatchInformation().
5687 kBootImageAddress,
5688
5689 // Load from the resolved strings array at an absolute address.
5690 // Used for strings outside the boot image referenced by JIT-compiled code.
5691 kDexCacheAddress,
5692
5693 // Load from resolved strings array in the dex cache using a PC-relative load.
5694 // Used for strings outside boot image when we know that we can access
5695 // the dex cache arrays using a PC-relative load.
5696 kDexCachePcRelative,
5697
5698 // Load from resolved strings array accessed through the class loaded from
5699 // the compiled method's own ArtMethod*. This is the default access type when
5700 // all other types are unavailable.
5701 kDexCacheViaMethod,
5702
5703 kLast = kDexCacheViaMethod
5704 };
5705
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005706 HLoadString(HCurrentMethod* current_method,
5707 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005708 const DexFile& dex_file,
5709 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005710 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5711 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005712 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005713 SetPackedFlag<kFlagIsInDexCache>(false);
5714 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5715 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005716 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005717
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005718 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5719 DCHECK(HasAddress(load_kind));
5720 load_data_.address = address;
5721 SetLoadKindInternal(load_kind);
5722 }
5723
5724 void SetLoadKindWithStringReference(LoadKind load_kind,
5725 const DexFile& dex_file,
5726 uint32_t string_index) {
5727 DCHECK(HasStringReference(load_kind));
5728 load_data_.ref.dex_file = &dex_file;
5729 string_index_ = string_index;
5730 SetLoadKindInternal(load_kind);
5731 }
5732
5733 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5734 const DexFile& dex_file,
5735 uint32_t element_index) {
5736 DCHECK(HasDexCacheReference(load_kind));
5737 load_data_.ref.dex_file = &dex_file;
5738 load_data_.ref.dex_cache_element_index = element_index;
5739 SetLoadKindInternal(load_kind);
5740 }
5741
5742 LoadKind GetLoadKind() const {
5743 return GetPackedField<LoadKindField>();
5744 }
5745
5746 const DexFile& GetDexFile() const;
5747
5748 uint32_t GetStringIndex() const {
5749 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5750 return string_index_;
5751 }
5752
5753 uint32_t GetDexCacheElementOffset() const;
5754
5755 uint64_t GetAddress() const {
5756 DCHECK(HasAddress(GetLoadKind()));
5757 return load_data_.address;
5758 }
5759
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005760 bool CanBeMoved() const OVERRIDE { return true; }
5761
Vladimir Marko372f10e2016-05-17 16:30:10 +01005762 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005763
5764 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5765
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005766 // Will call the runtime if we need to load the string through
5767 // the dex cache and the string is not guaranteed to be there yet.
5768 bool NeedsEnvironment() const OVERRIDE {
5769 LoadKind load_kind = GetLoadKind();
5770 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5771 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5772 load_kind == LoadKind::kBootImageAddress) {
5773 return false;
5774 }
5775 return !IsInDexCache();
5776 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005777
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005778 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5779 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5780 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005781
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005782 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005783 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005784
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005785 static SideEffects SideEffectsForArchRuntimeCalls() {
5786 return SideEffects::CanTriggerGC();
5787 }
5788
Vladimir Markoa1de9182016-02-25 11:37:38 +00005789 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5790
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005791 void MarkInDexCache() {
5792 SetPackedFlag<kFlagIsInDexCache>(true);
5793 DCHECK(!NeedsEnvironment());
5794 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005795 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005796 }
5797
Vladimir Marko372f10e2016-05-17 16:30:10 +01005798 void AddSpecialInput(HInstruction* special_input);
5799
5800 using HInstruction::GetInputRecords; // Keep the const version visible.
5801 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5802 return ArrayRef<HUserRecord<HInstruction*>>(
5803 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005804 }
5805
Vladimir Marko372f10e2016-05-17 16:30:10 +01005806 Primitive::Type GetType() const OVERRIDE {
5807 return Primitive::kPrimNot;
5808 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005809
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005810 DECLARE_INSTRUCTION(LoadString);
5811
5812 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005813 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005814 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5815 static constexpr size_t kFieldLoadKindSize =
5816 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5817 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005818 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005819 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005820
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005821 static bool HasStringReference(LoadKind load_kind) {
5822 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5823 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5824 load_kind == LoadKind::kDexCacheViaMethod;
5825 }
5826
5827 static bool HasAddress(LoadKind load_kind) {
5828 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5829 }
5830
5831 static bool HasDexCacheReference(LoadKind load_kind) {
5832 return load_kind == LoadKind::kDexCachePcRelative;
5833 }
5834
5835 void SetLoadKindInternal(LoadKind load_kind);
5836
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005837 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5838 // For other load kinds it's empty or possibly some architecture-specific instruction
5839 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005840 HUserRecord<HInstruction*> special_input_;
5841
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005842 // String index serves also as the hash code and it's also needed for slow-paths,
5843 // so it must not be overwritten with other load data.
5844 uint32_t string_index_;
5845
5846 union {
5847 struct {
5848 const DexFile* dex_file; // For string reference and dex cache reference.
5849 uint32_t dex_cache_element_index; // Only for dex cache reference.
5850 } ref;
5851 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5852 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005853
5854 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5855};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005856std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5857
5858// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5859inline const DexFile& HLoadString::GetDexFile() const {
5860 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5861 << GetLoadKind();
5862 return *load_data_.ref.dex_file;
5863}
5864
5865// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5866inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5867 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5868 return load_data_.ref.dex_cache_element_index;
5869}
5870
5871// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5872inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005873 // The special input is used for PC-relative loads on some architectures,
5874 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005875 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005876 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5877 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5878 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005879 // HLoadString::GetInputRecords() returns an empty array at this point,
5880 // so use the GetInputRecords() from the base class to set the input record.
5881 DCHECK(special_input_.GetInstruction() == nullptr);
5882 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005883 special_input->AddUseAt(this, 0);
5884}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005885
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005886/**
5887 * Performs an initialization check on its Class object input.
5888 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005889class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005890 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005891 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005892 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005893 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005894 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5895 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005896 SetRawInputAt(0, constant);
5897 }
5898
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005899 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005900 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005901 return true;
5902 }
5903
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005904 bool NeedsEnvironment() const OVERRIDE {
5905 // May call runtime to initialize the class.
5906 return true;
5907 }
5908
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005909 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005910
5911 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5912
5913 DECLARE_INSTRUCTION(ClinitCheck);
5914
5915 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005916 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5917};
5918
Vladimir Markofcb503c2016-05-18 12:48:17 +01005919class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005920 public:
5921 HStaticFieldGet(HInstruction* cls,
5922 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005923 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005924 bool is_volatile,
5925 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005926 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005927 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005928 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005929 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005930 : HExpression(field_type,
Serban Constantinescufca16662016-07-14 09:21:59 +01005931 SideEffectsForArchRuntimeCalls(field_type, is_volatile),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005932 dex_pc),
5933 field_info_(field_offset,
5934 field_type,
5935 is_volatile,
5936 field_idx,
5937 declaring_class_def_index,
5938 dex_file,
5939 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005940 SetRawInputAt(0, cls);
5941 }
5942
Calin Juravle52c48962014-12-16 17:02:57 +00005943
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005944 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005945
Vladimir Marko372f10e2016-05-17 16:30:10 +01005946 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5947 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005948 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005949 }
5950
5951 size_t ComputeHashCode() const OVERRIDE {
5952 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5953 }
5954
Calin Juravle52c48962014-12-16 17:02:57 +00005955 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005956 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5957 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005958 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005959
Serban Constantinescufca16662016-07-14 09:21:59 +01005960 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type field_type, bool is_volatile) {
5961 SideEffects side_effects = SideEffects::FieldReadOfType(field_type, is_volatile);
5962
5963 // MIPS delegates volatile kPrimLong and kPrimDouble loads to a runtime helper.
5964 if (Primitive::Is64BitType(field_type)) {
5965 side_effects.Add(SideEffects::CanTriggerGC());
5966 }
5967 return side_effects;
5968 }
5969
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005970 DECLARE_INSTRUCTION(StaticFieldGet);
5971
5972 private:
5973 const FieldInfo field_info_;
5974
5975 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5976};
5977
Vladimir Markofcb503c2016-05-18 12:48:17 +01005978class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005979 public:
5980 HStaticFieldSet(HInstruction* cls,
5981 HInstruction* value,
5982 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005983 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005984 bool is_volatile,
5985 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005986 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005987 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005988 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005989 uint32_t dex_pc)
Serban Constantinescufca16662016-07-14 09:21:59 +01005990 : HTemplateInstruction(SideEffectsForArchRuntimeCalls(field_type, is_volatile),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005991 dex_pc),
5992 field_info_(field_offset,
5993 field_type,
5994 is_volatile,
5995 field_idx,
5996 declaring_class_def_index,
5997 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005998 dex_cache) {
5999 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006000 SetRawInputAt(0, cls);
6001 SetRawInputAt(1, value);
6002 }
6003
Calin Juravle52c48962014-12-16 17:02:57 +00006004 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006005 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6006 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006007 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006008
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006009 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006010 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6011 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006012
Serban Constantinescufca16662016-07-14 09:21:59 +01006013 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type field_type, bool is_volatile) {
6014 SideEffects side_effects = SideEffects::FieldWriteOfType(field_type, is_volatile);
6015
6016 // MIPS delegates volatile kPrimLong and kPrimDouble stores to a runtime helper.
6017 if (Primitive::Is64BitType(field_type)) {
6018 side_effects.Add(SideEffects::CanTriggerGC());
6019 }
6020 return side_effects;
6021 }
6022
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006023 DECLARE_INSTRUCTION(StaticFieldSet);
6024
6025 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006026 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6027 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6028 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6029 "Too many packed fields.");
6030
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006031 const FieldInfo field_info_;
6032
6033 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6034};
6035
Vladimir Markofcb503c2016-05-18 12:48:17 +01006036class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006037 public:
6038 HUnresolvedInstanceFieldGet(HInstruction* obj,
6039 Primitive::Type field_type,
6040 uint32_t field_index,
6041 uint32_t dex_pc)
6042 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6043 field_index_(field_index) {
6044 SetRawInputAt(0, obj);
6045 }
6046
6047 bool NeedsEnvironment() const OVERRIDE { return true; }
6048 bool CanThrow() const OVERRIDE { return true; }
6049
6050 Primitive::Type GetFieldType() const { return GetType(); }
6051 uint32_t GetFieldIndex() const { return field_index_; }
6052
6053 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6054
6055 private:
6056 const uint32_t field_index_;
6057
6058 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6059};
6060
Vladimir Markofcb503c2016-05-18 12:48:17 +01006061class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006062 public:
6063 HUnresolvedInstanceFieldSet(HInstruction* obj,
6064 HInstruction* value,
6065 Primitive::Type field_type,
6066 uint32_t field_index,
6067 uint32_t dex_pc)
6068 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006069 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006070 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006071 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006072 SetRawInputAt(0, obj);
6073 SetRawInputAt(1, value);
6074 }
6075
6076 bool NeedsEnvironment() const OVERRIDE { return true; }
6077 bool CanThrow() const OVERRIDE { return true; }
6078
Vladimir Markoa1de9182016-02-25 11:37:38 +00006079 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006080 uint32_t GetFieldIndex() const { return field_index_; }
6081
6082 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6083
6084 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006085 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6086 static constexpr size_t kFieldFieldTypeSize =
6087 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6088 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6089 kFieldFieldType + kFieldFieldTypeSize;
6090 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6091 "Too many packed fields.");
6092 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6093
Calin Juravlee460d1d2015-09-29 04:52:17 +01006094 const uint32_t field_index_;
6095
6096 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6097};
6098
Vladimir Markofcb503c2016-05-18 12:48:17 +01006099class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006100 public:
6101 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6102 uint32_t field_index,
6103 uint32_t dex_pc)
6104 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6105 field_index_(field_index) {
6106 }
6107
6108 bool NeedsEnvironment() const OVERRIDE { return true; }
6109 bool CanThrow() const OVERRIDE { return true; }
6110
6111 Primitive::Type GetFieldType() const { return GetType(); }
6112 uint32_t GetFieldIndex() const { return field_index_; }
6113
6114 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6115
6116 private:
6117 const uint32_t field_index_;
6118
6119 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6120};
6121
Vladimir Markofcb503c2016-05-18 12:48:17 +01006122class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006123 public:
6124 HUnresolvedStaticFieldSet(HInstruction* value,
6125 Primitive::Type field_type,
6126 uint32_t field_index,
6127 uint32_t dex_pc)
6128 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006129 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006130 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006131 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006132 SetRawInputAt(0, value);
6133 }
6134
6135 bool NeedsEnvironment() const OVERRIDE { return true; }
6136 bool CanThrow() const OVERRIDE { return true; }
6137
Vladimir Markoa1de9182016-02-25 11:37:38 +00006138 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006139 uint32_t GetFieldIndex() const { return field_index_; }
6140
6141 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6142
6143 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006144 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6145 static constexpr size_t kFieldFieldTypeSize =
6146 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6147 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6148 kFieldFieldType + kFieldFieldTypeSize;
6149 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6150 "Too many packed fields.");
6151 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6152
Calin Juravlee460d1d2015-09-29 04:52:17 +01006153 const uint32_t field_index_;
6154
6155 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6156};
6157
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006158// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006159class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006160 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006161 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6162 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006163
David Brazdilbbd733e2015-08-18 17:48:17 +01006164 bool CanBeNull() const OVERRIDE { return false; }
6165
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006166 DECLARE_INSTRUCTION(LoadException);
6167
6168 private:
6169 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6170};
6171
David Brazdilcb1c0552015-08-04 16:22:25 +01006172// Implicit part of move-exception which clears thread-local exception storage.
6173// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006174class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006175 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006176 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6177 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006178
6179 DECLARE_INSTRUCTION(ClearException);
6180
6181 private:
6182 DISALLOW_COPY_AND_ASSIGN(HClearException);
6183};
6184
Vladimir Markofcb503c2016-05-18 12:48:17 +01006185class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006186 public:
6187 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006188 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006189 SetRawInputAt(0, exception);
6190 }
6191
6192 bool IsControlFlow() const OVERRIDE { return true; }
6193
6194 bool NeedsEnvironment() const OVERRIDE { return true; }
6195
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006196 bool CanThrow() const OVERRIDE { return true; }
6197
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006198
6199 DECLARE_INSTRUCTION(Throw);
6200
6201 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006202 DISALLOW_COPY_AND_ASSIGN(HThrow);
6203};
6204
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006205/**
6206 * Implementation strategies for the code generator of a HInstanceOf
6207 * or `HCheckCast`.
6208 */
6209enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006210 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006211 kExactCheck, // Can do a single class compare.
6212 kClassHierarchyCheck, // Can just walk the super class chain.
6213 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6214 kInterfaceCheck, // No optimization yet when checking against an interface.
6215 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006216 kArrayCheck, // No optimization yet when checking against a generic array.
6217 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006218};
6219
Roland Levillain86503782016-02-11 19:07:30 +00006220std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6221
Vladimir Markofcb503c2016-05-18 12:48:17 +01006222class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006223 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006224 HInstanceOf(HInstruction* object,
6225 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006226 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006227 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006228 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006229 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006230 dex_pc) {
6231 SetPackedField<TypeCheckKindField>(check_kind);
6232 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006233 SetRawInputAt(0, object);
6234 SetRawInputAt(1, constant);
6235 }
6236
6237 bool CanBeMoved() const OVERRIDE { return true; }
6238
Vladimir Marko372f10e2016-05-17 16:30:10 +01006239 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006240 return true;
6241 }
6242
6243 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006244 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006245 }
6246
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006247 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006248 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6249 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6250 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6251 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006252
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006253 static bool CanCallRuntime(TypeCheckKind check_kind) {
6254 // Mips currently does runtime calls for any other checks.
6255 return check_kind != TypeCheckKind::kExactCheck;
6256 }
6257
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006258 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006259 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006260 }
6261
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006262 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006263
6264 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006265 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6266 static constexpr size_t kFieldTypeCheckKindSize =
6267 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6268 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6269 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6270 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6271 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006272
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006273 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6274};
6275
Vladimir Markofcb503c2016-05-18 12:48:17 +01006276class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006277 public:
David Brazdilf5552582015-12-27 13:36:12 +00006278 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006279 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006280 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6281 SetPackedFlag<kFlagUpperCanBeNull>(true);
6282 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006283 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006284 SetRawInputAt(0, input);
6285 }
6286
David Brazdilf5552582015-12-27 13:36:12 +00006287 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006288 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006289 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006290 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006291
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006292 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006293 DCHECK(GetUpperCanBeNull() || !can_be_null);
6294 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006295 }
6296
Vladimir Markoa1de9182016-02-25 11:37:38 +00006297 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006298
Calin Juravleb1498f62015-02-16 13:13:29 +00006299 DECLARE_INSTRUCTION(BoundType);
6300
6301 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006302 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6303 // is false then CanBeNull() cannot be true).
6304 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6305 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6306 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6307 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6308
Calin Juravleb1498f62015-02-16 13:13:29 +00006309 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006310 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6311 // It is used to bound the type in cases like:
6312 // if (x instanceof ClassX) {
6313 // // uper_bound_ will be ClassX
6314 // }
David Brazdilf5552582015-12-27 13:36:12 +00006315 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006316
6317 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6318};
6319
Vladimir Markofcb503c2016-05-18 12:48:17 +01006320class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006321 public:
6322 HCheckCast(HInstruction* object,
6323 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006324 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006325 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006326 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6327 SetPackedField<TypeCheckKindField>(check_kind);
6328 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006329 SetRawInputAt(0, object);
6330 SetRawInputAt(1, constant);
6331 }
6332
6333 bool CanBeMoved() const OVERRIDE { return true; }
6334
Vladimir Marko372f10e2016-05-17 16:30:10 +01006335 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006336 return true;
6337 }
6338
6339 bool NeedsEnvironment() const OVERRIDE {
6340 // Instruction may throw a CheckCastError.
6341 return true;
6342 }
6343
6344 bool CanThrow() const OVERRIDE { return true; }
6345
Vladimir Markoa1de9182016-02-25 11:37:38 +00006346 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6347 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6348 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6349 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006350
6351 DECLARE_INSTRUCTION(CheckCast);
6352
6353 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006354 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6355 static constexpr size_t kFieldTypeCheckKindSize =
6356 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6357 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6358 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6359 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6360 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006361
6362 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006363};
6364
Andreas Gampe26de38b2016-07-27 17:53:11 -07006365/**
6366 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6367 * @details We define the combined barrier types that are actually required
6368 * by the Java Memory Model, rather than using exactly the terminology from
6369 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6370 * primitives. Note that the JSR-133 cookbook generally does not deal with
6371 * store atomicity issues, and the recipes there are not always entirely sufficient.
6372 * The current recipe is as follows:
6373 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6374 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6375 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6376 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6377 * class has final fields.
6378 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6379 * store-to-memory instructions. Only generated together with non-temporal stores.
6380 */
6381enum MemBarrierKind {
6382 kAnyStore,
6383 kLoadAny,
6384 kStoreStore,
6385 kAnyAny,
6386 kNTStoreStore,
6387 kLastBarrierKind = kNTStoreStore
6388};
6389std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6390
Vladimir Markofcb503c2016-05-18 12:48:17 +01006391class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006392 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006393 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006394 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006395 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6396 SetPackedField<BarrierKindField>(barrier_kind);
6397 }
Calin Juravle27df7582015-04-17 19:12:31 +01006398
Vladimir Markoa1de9182016-02-25 11:37:38 +00006399 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006400
6401 DECLARE_INSTRUCTION(MemoryBarrier);
6402
6403 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006404 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6405 static constexpr size_t kFieldBarrierKindSize =
6406 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6407 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6408 kFieldBarrierKind + kFieldBarrierKindSize;
6409 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6410 "Too many packed fields.");
6411 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006412
6413 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6414};
6415
Vladimir Markofcb503c2016-05-18 12:48:17 +01006416class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006417 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006418 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006419 kEnter,
6420 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006421 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006422 };
6423
6424 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006425 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006426 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6427 dex_pc) {
6428 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006429 SetRawInputAt(0, object);
6430 }
6431
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006432 // Instruction may go into runtime, so we need an environment.
6433 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006434
6435 bool CanThrow() const OVERRIDE {
6436 // Verifier guarantees that monitor-exit cannot throw.
6437 // This is important because it allows the HGraphBuilder to remove
6438 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6439 return IsEnter();
6440 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006441
Vladimir Markoa1de9182016-02-25 11:37:38 +00006442 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6443 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006444
6445 DECLARE_INSTRUCTION(MonitorOperation);
6446
Calin Juravle52c48962014-12-16 17:02:57 +00006447 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006448 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6449 static constexpr size_t kFieldOperationKindSize =
6450 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6451 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6452 kFieldOperationKind + kFieldOperationKindSize;
6453 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6454 "Too many packed fields.");
6455 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006456
6457 private:
6458 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6459};
6460
Vladimir Markofcb503c2016-05-18 12:48:17 +01006461class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006462 public:
6463 HSelect(HInstruction* condition,
6464 HInstruction* true_value,
6465 HInstruction* false_value,
6466 uint32_t dex_pc)
6467 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6468 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6469
6470 // First input must be `true_value` or `false_value` to allow codegens to
6471 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6472 // that architectures which implement HSelect as a conditional move also
6473 // will not need to invert the condition.
6474 SetRawInputAt(0, false_value);
6475 SetRawInputAt(1, true_value);
6476 SetRawInputAt(2, condition);
6477 }
6478
6479 HInstruction* GetFalseValue() const { return InputAt(0); }
6480 HInstruction* GetTrueValue() const { return InputAt(1); }
6481 HInstruction* GetCondition() const { return InputAt(2); }
6482
6483 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006484 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6485 return true;
6486 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006487
6488 bool CanBeNull() const OVERRIDE {
6489 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6490 }
6491
6492 DECLARE_INSTRUCTION(Select);
6493
6494 private:
6495 DISALLOW_COPY_AND_ASSIGN(HSelect);
6496};
6497
Vladimir Markof9f64412015-09-02 14:05:49 +01006498class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006499 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006500 MoveOperands(Location source,
6501 Location destination,
6502 Primitive::Type type,
6503 HInstruction* instruction)
6504 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006505
6506 Location GetSource() const { return source_; }
6507 Location GetDestination() const { return destination_; }
6508
6509 void SetSource(Location value) { source_ = value; }
6510 void SetDestination(Location value) { destination_ = value; }
6511
6512 // The parallel move resolver marks moves as "in-progress" by clearing the
6513 // destination (but not the source).
6514 Location MarkPending() {
6515 DCHECK(!IsPending());
6516 Location dest = destination_;
6517 destination_ = Location::NoLocation();
6518 return dest;
6519 }
6520
6521 void ClearPending(Location dest) {
6522 DCHECK(IsPending());
6523 destination_ = dest;
6524 }
6525
6526 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006527 DCHECK(source_.IsValid() || destination_.IsInvalid());
6528 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006529 }
6530
6531 // True if this blocks a move from the given location.
6532 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006533 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006534 }
6535
6536 // A move is redundant if it's been eliminated, if its source and
6537 // destination are the same, or if its destination is unneeded.
6538 bool IsRedundant() const {
6539 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6540 }
6541
6542 // We clear both operands to indicate move that's been eliminated.
6543 void Eliminate() {
6544 source_ = destination_ = Location::NoLocation();
6545 }
6546
6547 bool IsEliminated() const {
6548 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6549 return source_.IsInvalid();
6550 }
6551
Alexey Frunze4dda3372015-06-01 18:31:49 -07006552 Primitive::Type GetType() const { return type_; }
6553
Nicolas Geoffray90218252015-04-15 11:56:51 +01006554 bool Is64BitMove() const {
6555 return Primitive::Is64BitType(type_);
6556 }
6557
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006558 HInstruction* GetInstruction() const { return instruction_; }
6559
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006560 private:
6561 Location source_;
6562 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006563 // The type this move is for.
6564 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006565 // The instruction this move is assocatied with. Null when this move is
6566 // for moving an input in the expected locations of user (including a phi user).
6567 // This is only used in debug mode, to ensure we do not connect interval siblings
6568 // in the same parallel move.
6569 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006570};
6571
Roland Levillainc9285912015-12-18 10:38:42 +00006572std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6573
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006574static constexpr size_t kDefaultNumberOfMoves = 4;
6575
Vladimir Markofcb503c2016-05-18 12:48:17 +01006576class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006577 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006578 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006579 : HTemplateInstruction(SideEffects::None(), dex_pc),
6580 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6581 moves_.reserve(kDefaultNumberOfMoves);
6582 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006583
Nicolas Geoffray90218252015-04-15 11:56:51 +01006584 void AddMove(Location source,
6585 Location destination,
6586 Primitive::Type type,
6587 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006588 DCHECK(source.IsValid());
6589 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006590 if (kIsDebugBuild) {
6591 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006592 for (const MoveOperands& move : moves_) {
6593 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006594 // Special case the situation where the move is for the spill slot
6595 // of the instruction.
6596 if ((GetPrevious() == instruction)
6597 || ((GetPrevious() == nullptr)
6598 && instruction->IsPhi()
6599 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006600 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006601 << "Doing parallel moves for the same instruction.";
6602 } else {
6603 DCHECK(false) << "Doing parallel moves for the same instruction.";
6604 }
6605 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006606 }
6607 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006608 for (const MoveOperands& move : moves_) {
6609 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006610 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006611 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006612 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006613 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006614 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006615 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006616 }
6617
Vladimir Marko225b6462015-09-28 12:17:40 +01006618 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006619 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006620 }
6621
Vladimir Marko225b6462015-09-28 12:17:40 +01006622 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006623
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006624 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006625
6626 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006627 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006628
6629 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6630};
6631
Mark Mendell0616ae02015-04-17 12:49:27 -04006632} // namespace art
6633
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006634#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6635#include "nodes_shared.h"
6636#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006637#ifdef ART_ENABLE_CODEGEN_arm
6638#include "nodes_arm.h"
6639#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006640#ifdef ART_ENABLE_CODEGEN_arm64
6641#include "nodes_arm64.h"
6642#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006643#ifdef ART_ENABLE_CODEGEN_mips
6644#include "nodes_mips.h"
6645#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006646#ifdef ART_ENABLE_CODEGEN_x86
6647#include "nodes_x86.h"
6648#endif
6649
6650namespace art {
6651
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006652class HGraphVisitor : public ValueObject {
6653 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006654 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6655 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006656
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006657 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006658 virtual void VisitBasicBlock(HBasicBlock* block);
6659
Roland Levillain633021e2014-10-01 14:12:25 +01006660 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006661 void VisitInsertionOrder();
6662
Roland Levillain633021e2014-10-01 14:12:25 +01006663 // Visit the graph following dominator tree reverse post-order.
6664 void VisitReversePostOrder();
6665
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006666 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006667
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006668 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006669#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006670 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6671
6672 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6673
6674#undef DECLARE_VISIT_INSTRUCTION
6675
6676 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006677 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006678
6679 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6680};
6681
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006682class HGraphDelegateVisitor : public HGraphVisitor {
6683 public:
6684 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6685 virtual ~HGraphDelegateVisitor() {}
6686
6687 // Visit functions that delegate to to super class.
6688#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006689 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006690
6691 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6692
6693#undef DECLARE_VISIT_INSTRUCTION
6694
6695 private:
6696 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6697};
6698
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006699class HInsertionOrderIterator : public ValueObject {
6700 public:
6701 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6702
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006703 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006704 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006705 void Advance() { ++index_; }
6706
6707 private:
6708 const HGraph& graph_;
6709 size_t index_;
6710
6711 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6712};
6713
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006714class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006715 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006716 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6717 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006718 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006719 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006720
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006721 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6722 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006723 void Advance() { ++index_; }
6724
6725 private:
6726 const HGraph& graph_;
6727 size_t index_;
6728
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006729 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006730};
6731
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006732class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006733 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006734 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006735 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006736 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006737 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006738 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006739
6740 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006741 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006742 void Advance() { --index_; }
6743
6744 private:
6745 const HGraph& graph_;
6746 size_t index_;
6747
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006748 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006749};
6750
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006751class HLinearPostOrderIterator : public ValueObject {
6752 public:
6753 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006754 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006755
6756 bool Done() const { return index_ == 0; }
6757
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006758 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006759
6760 void Advance() {
6761 --index_;
6762 DCHECK_GE(index_, 0U);
6763 }
6764
6765 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006766 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006767 size_t index_;
6768
6769 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6770};
6771
6772class HLinearOrderIterator : public ValueObject {
6773 public:
6774 explicit HLinearOrderIterator(const HGraph& graph)
6775 : order_(graph.GetLinearOrder()), index_(0) {}
6776
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006777 bool Done() const { return index_ == order_.size(); }
6778 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006779 void Advance() { ++index_; }
6780
6781 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006782 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006783 size_t index_;
6784
6785 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6786};
6787
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006788// Iterator over the blocks that art part of the loop. Includes blocks part
6789// of an inner loop. The order in which the blocks are iterated is on their
6790// block id.
6791class HBlocksInLoopIterator : public ValueObject {
6792 public:
6793 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6794 : blocks_in_loop_(info.GetBlocks()),
6795 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6796 index_(0) {
6797 if (!blocks_in_loop_.IsBitSet(index_)) {
6798 Advance();
6799 }
6800 }
6801
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006802 bool Done() const { return index_ == blocks_.size(); }
6803 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006804 void Advance() {
6805 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006806 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006807 if (blocks_in_loop_.IsBitSet(index_)) {
6808 break;
6809 }
6810 }
6811 }
6812
6813 private:
6814 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006815 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006816 size_t index_;
6817
6818 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6819};
6820
Mingyao Yang3584bce2015-05-19 16:01:59 -07006821// Iterator over the blocks that art part of the loop. Includes blocks part
6822// of an inner loop. The order in which the blocks are iterated is reverse
6823// post order.
6824class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6825 public:
6826 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6827 : blocks_in_loop_(info.GetBlocks()),
6828 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6829 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006830 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006831 Advance();
6832 }
6833 }
6834
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006835 bool Done() const { return index_ == blocks_.size(); }
6836 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006837 void Advance() {
6838 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006839 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6840 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006841 break;
6842 }
6843 }
6844 }
6845
6846 private:
6847 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006848 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006849 size_t index_;
6850
6851 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6852};
6853
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006854inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006855 if (constant->IsIntConstant()) {
6856 return constant->AsIntConstant()->GetValue();
6857 } else if (constant->IsLongConstant()) {
6858 return constant->AsLongConstant()->GetValue();
6859 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006860 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006861 return 0;
6862 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006863}
6864
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006865#define INSTRUCTION_TYPE_CHECK(type, super) \
6866 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6867 inline const H##type* HInstruction::As##type() const { \
6868 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6869 } \
6870 inline H##type* HInstruction::As##type() { \
6871 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6872 }
6873
6874 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6875#undef INSTRUCTION_TYPE_CHECK
6876
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006877// Create space in `blocks` for adding `number_of_new_blocks` entries
6878// starting at location `at`. Blocks after `at` are moved accordingly.
6879inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6880 size_t number_of_new_blocks,
6881 size_t after) {
6882 DCHECK_LT(after, blocks->size());
6883 size_t old_size = blocks->size();
6884 size_t new_size = old_size + number_of_new_blocks;
6885 blocks->resize(new_size);
6886 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6887}
6888
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006889} // namespace art
6890
6891#endif // ART_COMPILER_OPTIMIZING_NODES_H_