blob: 4c6d28f0c8b3b48c993dc47752613b175c14a310 [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
Aart Bik20e9db62016-09-14 10:52:13 -0700368 // Computes the linear order (should be called before using HLinearOrderIterator).
369 // Linearizes the graph such that:
370 // (1): a block is always after its dominator,
371 // (2): blocks of loops are contiguous.
372 // This creates a natural and efficient ordering when visualizing live ranges.
373 void Linearize();
374
David Brazdilffee3d32015-07-06 11:48:53 +0100375 // Iterate over blocks to compute try block membership. Needs reverse post
376 // order and loop information.
377 void ComputeTryBlockInformation();
378
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000379 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000380 // Returns the instruction to replace the invoke expression or null if the
381 // invoke is for a void method. Note that the caller is responsible for replacing
382 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000383 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000384
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000385 // Update the loop and try membership of `block`, which was spawned from `reference`.
386 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
387 // should be the new back edge.
388 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
389 HBasicBlock* reference,
390 bool replace_if_back_edge);
391
Mingyao Yang3584bce2015-05-19 16:01:59 -0700392 // Need to add a couple of blocks to test if the loop body is entered and
393 // put deoptimization instructions, etc.
394 void TransformLoopHeaderForBCE(HBasicBlock* header);
395
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000396 // Removes `block` from the graph. Assumes `block` has been disconnected from
397 // other blocks and has no instructions or phis.
398 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000399
David Brazdilfc6a86a2015-06-26 10:33:45 +0000400 // Splits the edge between `block` and `successor` while preserving the
401 // indices in the predecessor/successor lists. If there are multiple edges
402 // between the blocks, the lowest indices are used.
403 // Returns the new block which is empty and has the same dex pc as `successor`.
404 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
405
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100406 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
407 void SimplifyLoop(HBasicBlock* header);
408
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000409 int32_t GetNextInstructionId() {
410 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000411 return current_instruction_id_++;
412 }
413
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000414 int32_t GetCurrentInstructionId() const {
415 return current_instruction_id_;
416 }
417
418 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000419 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000420 current_instruction_id_ = id;
421 }
422
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100423 uint16_t GetMaximumNumberOfOutVRegs() const {
424 return maximum_number_of_out_vregs_;
425 }
426
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000427 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
428 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100429 }
430
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100431 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
432 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
433 }
434
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000435 void UpdateTemporariesVRegSlots(size_t slots) {
436 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100437 }
438
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000439 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100440 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000441 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100442 }
443
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100444 void SetNumberOfVRegs(uint16_t number_of_vregs) {
445 number_of_vregs_ = number_of_vregs;
446 }
447
448 uint16_t GetNumberOfVRegs() const {
449 return number_of_vregs_;
450 }
451
452 void SetNumberOfInVRegs(uint16_t value) {
453 number_of_in_vregs_ = value;
454 }
455
David Brazdildee58d62016-04-07 09:54:26 +0000456 uint16_t GetNumberOfInVRegs() const {
457 return number_of_in_vregs_;
458 }
459
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100460 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100461 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100462 return number_of_vregs_ - number_of_in_vregs_;
463 }
464
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100465 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100466 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100467 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100468
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100469 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100470 return linear_order_;
471 }
472
Mark Mendell1152c922015-04-24 17:06:35 -0400473 bool HasBoundsChecks() const {
474 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800475 }
476
Mark Mendell1152c922015-04-24 17:06:35 -0400477 void SetHasBoundsChecks(bool value) {
478 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800479 }
480
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100481 bool ShouldGenerateConstructorBarrier() const {
482 return should_generate_constructor_barrier_;
483 }
484
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000485 bool IsDebuggable() const { return debuggable_; }
486
David Brazdil8d5b8b22015-03-24 10:51:52 +0000487 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000488 // already, it is created and inserted into the graph. This method is only for
489 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600490 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000491
492 // TODO: This is problematic for the consistency of reference type propagation
493 // because it can be created anytime after the pass and thus it will be left
494 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600495 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000496
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600497 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
498 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000499 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600500 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
501 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000502 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600503 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
504 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000505 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600506 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
507 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000508 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000509
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100510 HCurrentMethod* GetCurrentMethod();
511
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100512 const DexFile& GetDexFile() const {
513 return dex_file_;
514 }
515
516 uint32_t GetMethodIdx() const {
517 return method_idx_;
518 }
519
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100520 InvokeType GetInvokeType() const {
521 return invoke_type_;
522 }
523
Mark Mendellc4701932015-04-10 13:18:51 -0400524 InstructionSet GetInstructionSet() const {
525 return instruction_set_;
526 }
527
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000528 bool IsCompilingOsr() const { return osr_; }
529
David Brazdil77a48ae2015-09-15 12:34:04 +0000530 bool HasTryCatch() const { return has_try_catch_; }
531 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100532
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000533 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
534 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
535
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100536 ArtMethod* GetArtMethod() const { return art_method_; }
537 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
538
Mark Mendellf6529172015-11-17 11:16:56 -0500539 // Returns an instruction with the opposite boolean value from 'cond'.
540 // The instruction has been inserted into the graph, either as a constant, or
541 // before cursor.
542 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
543
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000544 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
545
David Brazdil2d7352b2015-04-20 14:52:42 +0100546 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000547 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100548 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000549
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000550 template <class InstructionType, typename ValueType>
551 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600552 ArenaSafeMap<ValueType, InstructionType*>* cache,
553 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000554 // Try to find an existing constant of the given value.
555 InstructionType* constant = nullptr;
556 auto cached_constant = cache->find(value);
557 if (cached_constant != cache->end()) {
558 constant = cached_constant->second;
559 }
560
561 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100562 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000563 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600564 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000565 cache->Overwrite(value, constant);
566 InsertConstant(constant);
567 }
568 return constant;
569 }
570
David Brazdil8d5b8b22015-03-24 10:51:52 +0000571 void InsertConstant(HConstant* instruction);
572
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000573 // Cache a float constant into the graph. This method should only be
574 // called by the SsaBuilder when creating "equivalent" instructions.
575 void CacheFloatConstant(HFloatConstant* constant);
576
577 // See CacheFloatConstant comment.
578 void CacheDoubleConstant(HDoubleConstant* constant);
579
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000580 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000581
582 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100583 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000584
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100585 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100586 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000587
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100588 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100589 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100590
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000591 HBasicBlock* entry_block_;
592 HBasicBlock* exit_block_;
593
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100594 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100595 uint16_t maximum_number_of_out_vregs_;
596
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100597 // The number of virtual registers in this method. Contains the parameters.
598 uint16_t number_of_vregs_;
599
600 // The number of virtual registers used by parameters of this method.
601 uint16_t number_of_in_vregs_;
602
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000603 // Number of vreg size slots that the temporaries use (used in baseline compiler).
604 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100605
Mark Mendell1152c922015-04-24 17:06:35 -0400606 // Has bounds checks. We can totally skip BCE if it's false.
607 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800608
David Brazdil77a48ae2015-09-15 12:34:04 +0000609 // Flag whether there are any try/catch blocks in the graph. We will skip
610 // try/catch-related passes if false.
611 bool has_try_catch_;
612
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000613 // Flag whether there are any irreducible loops in the graph.
614 bool has_irreducible_loops_;
615
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000616 // Indicates whether the graph should be compiled in a way that
617 // ensures full debuggability. If false, we can apply more
618 // aggressive optimizations that may limit the level of debugging.
619 const bool debuggable_;
620
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000621 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000622 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000623
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100624 // The dex file from which the method is from.
625 const DexFile& dex_file_;
626
627 // The method index in the dex file.
628 const uint32_t method_idx_;
629
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100630 // If inlined, this encodes how the callee is being invoked.
631 const InvokeType invoke_type_;
632
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100633 // Whether the graph has been transformed to SSA form. Only used
634 // in debug mode to ensure we are not using properties only valid
635 // for non-SSA form (like the number of temporaries).
636 bool in_ssa_form_;
637
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100638 const bool should_generate_constructor_barrier_;
639
Mathieu Chartiere401d142015-04-22 13:56:20 -0700640 const InstructionSet instruction_set_;
641
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000642 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000643 HNullConstant* cached_null_constant_;
644 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000645 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000646 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000647 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000648
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100649 HCurrentMethod* cached_current_method_;
650
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100651 // The ArtMethod this graph is for. Note that for AOT, it may be null,
652 // for example for methods whose declaring class could not be resolved
653 // (such as when the superclass could not be found).
654 ArtMethod* art_method_;
655
David Brazdil4833f5a2015-12-16 10:37:39 +0000656 // Keep the RTI of inexact Object to avoid having to pass stack handle
657 // collection pointer to passes which may create NullConstant.
658 ReferenceTypeInfo inexact_object_rti_;
659
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000660 // Whether we are compiling this graph for on stack replacement: this will
661 // make all loops seen as irreducible and emit special stack maps to mark
662 // compiled code entries which the interpreter can directly jump to.
663 const bool osr_;
664
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000665 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100666 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000667 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000668 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000669 DISALLOW_COPY_AND_ASSIGN(HGraph);
670};
671
Vladimir Markof9f64412015-09-02 14:05:49 +0100672class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000673 public:
674 HLoopInformation(HBasicBlock* header, HGraph* graph)
675 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100676 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000677 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100678 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100679 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100680 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000681 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100682 back_edges_.reserve(kDefaultNumberOfBackEdges);
683 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100684
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000685 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100686 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000687
688 void Dump(std::ostream& os);
689
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100690 HBasicBlock* GetHeader() const {
691 return header_;
692 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000693
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000694 void SetHeader(HBasicBlock* block) {
695 header_ = block;
696 }
697
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100698 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
699 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
700 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
701
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000702 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100703 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000704 }
705
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100706 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100707 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100708 }
709
David Brazdil46e2a392015-03-16 17:31:52 +0000710 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100711 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100712 }
713
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000714 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100715 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000716 }
717
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100718 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100719
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100720 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100721 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100722 }
723
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100724 // Returns the lifetime position of the back edge that has the
725 // greatest lifetime position.
726 size_t GetLifetimeEnd() const;
727
728 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100729 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100730 }
731
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000732 // Finds blocks that are part of this loop.
733 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100734
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100735 // Returns whether this loop information contains `block`.
736 // Note that this loop information *must* be populated before entering this function.
737 bool Contains(const HBasicBlock& block) const;
738
739 // Returns whether this loop information is an inner loop of `other`.
740 // Note that `other` *must* be populated before entering this function.
741 bool IsIn(const HLoopInformation& other) const;
742
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800743 // Returns true if instruction is not defined within this loop.
744 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700745
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100746 const ArenaBitVector& GetBlocks() const { return blocks_; }
747
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000748 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000749 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000750
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000751 void ClearAllBlocks() {
752 blocks_.ClearAllBits();
753 }
754
David Brazdil3f4a5222016-05-06 12:46:21 +0100755 bool HasBackEdgeNotDominatedByHeader() const;
756
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100757 bool IsPopulated() const {
758 return blocks_.GetHighestBitSet() != -1;
759 }
760
Anton Shaminf89381f2016-05-16 16:44:13 +0600761 bool DominatesAllBackEdges(HBasicBlock* block);
762
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000763 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100764 // Internal recursive implementation of `Populate`.
765 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100766 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100767
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000768 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100769 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000770 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100771 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100772 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100773 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000774
775 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
776};
777
David Brazdilec16f792015-08-19 15:04:01 +0100778// Stores try/catch information for basic blocks.
779// Note that HGraph is constructed so that catch blocks cannot simultaneously
780// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100781class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100782 public:
783 // Try block information constructor.
784 explicit TryCatchInformation(const HTryBoundary& try_entry)
785 : try_entry_(&try_entry),
786 catch_dex_file_(nullptr),
787 catch_type_index_(DexFile::kDexNoIndex16) {
788 DCHECK(try_entry_ != nullptr);
789 }
790
791 // Catch block information constructor.
792 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
793 : try_entry_(nullptr),
794 catch_dex_file_(&dex_file),
795 catch_type_index_(catch_type_index) {}
796
797 bool IsTryBlock() const { return try_entry_ != nullptr; }
798
799 const HTryBoundary& GetTryEntry() const {
800 DCHECK(IsTryBlock());
801 return *try_entry_;
802 }
803
804 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
805
806 bool IsCatchAllTypeIndex() const {
807 DCHECK(IsCatchBlock());
808 return catch_type_index_ == DexFile::kDexNoIndex16;
809 }
810
811 uint16_t GetCatchTypeIndex() const {
812 DCHECK(IsCatchBlock());
813 return catch_type_index_;
814 }
815
816 const DexFile& GetCatchDexFile() const {
817 DCHECK(IsCatchBlock());
818 return *catch_dex_file_;
819 }
820
821 private:
822 // One of possibly several TryBoundary instructions entering the block's try.
823 // Only set for try blocks.
824 const HTryBoundary* try_entry_;
825
826 // Exception type information. Only set for catch blocks.
827 const DexFile* catch_dex_file_;
828 const uint16_t catch_type_index_;
829};
830
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100831static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100832static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100833
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000834// A block in a method. Contains the list of instructions represented
835// as a double linked list. Each block knows its predecessors and
836// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100837
Vladimir Markof9f64412015-09-02 14:05:49 +0100838class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000839 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600840 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000841 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000842 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
843 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000844 loop_information_(nullptr),
845 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000846 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100847 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100848 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100849 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000850 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000851 try_catch_information_(nullptr) {
852 predecessors_.reserve(kDefaultNumberOfPredecessors);
853 successors_.reserve(kDefaultNumberOfSuccessors);
854 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
855 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000856
Vladimir Marko60584552015-09-03 13:35:12 +0000857 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100858 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000859 }
860
Vladimir Marko60584552015-09-03 13:35:12 +0000861 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100862 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000863 }
864
David Brazdild26a4112015-11-10 11:07:31 +0000865 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
866 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
867
Vladimir Marko60584552015-09-03 13:35:12 +0000868 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
869 return ContainsElement(successors_, block, start_from);
870 }
871
872 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100873 return dominated_blocks_;
874 }
875
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100876 bool IsEntryBlock() const {
877 return graph_->GetEntryBlock() == this;
878 }
879
880 bool IsExitBlock() const {
881 return graph_->GetExitBlock() == this;
882 }
883
David Brazdil46e2a392015-03-16 17:31:52 +0000884 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000885 bool IsSingleTryBoundary() const;
886
887 // Returns true if this block emits nothing but a jump.
888 bool IsSingleJump() const {
889 HLoopInformation* loop_info = GetLoopInformation();
890 return (IsSingleGoto() || IsSingleTryBoundary())
891 // Back edges generate a suspend check.
892 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
893 }
David Brazdil46e2a392015-03-16 17:31:52 +0000894
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000895 void AddBackEdge(HBasicBlock* back_edge) {
896 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000897 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000898 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100899 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000900 loop_information_->AddBackEdge(back_edge);
901 }
902
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000903 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000904 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000905
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100906 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000907 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600908 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000909
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000910 HBasicBlock* GetDominator() const { return dominator_; }
911 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000912 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
913
914 void RemoveDominatedBlock(HBasicBlock* block) {
915 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100916 }
Vladimir Marko60584552015-09-03 13:35:12 +0000917
918 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
919 ReplaceElement(dominated_blocks_, existing, new_block);
920 }
921
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100922 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000923
924 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100925 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000926 }
927
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100928 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
929 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100930 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100931 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100932 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
933 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000934
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000935 HInstruction* GetFirstInstructionDisregardMoves() const;
936
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000937 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000938 successors_.push_back(block);
939 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000940 }
941
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100942 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
943 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100944 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000945 new_block->predecessors_.push_back(this);
946 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000947 }
948
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000949 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
950 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000951 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000952 new_block->successors_.push_back(this);
953 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000954 }
955
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100956 // Insert `this` between `predecessor` and `successor. This method
957 // preserves the indicies, and will update the first edge found between
958 // `predecessor` and `successor`.
959 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
960 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100961 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000962 successor->predecessors_[predecessor_index] = this;
963 predecessor->successors_[successor_index] = this;
964 successors_.push_back(successor);
965 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100966 }
967
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100968 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000969 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100970 }
971
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000972 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000973 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000974 }
975
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100976 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000977 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100978 }
979
980 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000981 predecessors_.push_back(block);
982 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100983 }
984
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100985 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000986 DCHECK_EQ(predecessors_.size(), 2u);
987 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100988 }
989
David Brazdil769c9e52015-04-27 13:54:09 +0100990 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000991 DCHECK_EQ(successors_.size(), 2u);
992 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100993 }
994
David Brazdilfc6a86a2015-06-26 10:33:45 +0000995 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000996 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100997 }
998
David Brazdilfc6a86a2015-06-26 10:33:45 +0000999 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001000 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001001 }
1002
David Brazdilfc6a86a2015-06-26 10:33:45 +00001003 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001004 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001005 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001006 }
1007
1008 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001009 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001010 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001011 }
1012
1013 // Returns whether the first occurrence of `predecessor` in the list of
1014 // predecessors is at index `idx`.
1015 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001016 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001017 return GetPredecessorIndexOf(predecessor) == idx;
1018 }
1019
David Brazdild7558da2015-09-22 13:04:14 +01001020 // Create a new block between this block and its predecessors. The new block
1021 // is added to the graph, all predecessor edges are relinked to it and an edge
1022 // is created to `this`. Returns the new empty block. Reverse post order or
1023 // loop and try/catch information are not updated.
1024 HBasicBlock* CreateImmediateDominator();
1025
David Brazdilfc6a86a2015-06-26 10:33:45 +00001026 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001027 // created, latter block. Note that this method will add the block to the
1028 // graph, create a Goto at the end of the former block and will create an edge
1029 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001030 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001031 HBasicBlock* SplitBefore(HInstruction* cursor);
1032
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001033 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001034 // created block. Note that this method just updates raw block information,
1035 // like predecessors, successors, dominators, and instruction list. It does not
1036 // update the graph, reverse post order, loop information, nor make sure the
1037 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001038 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1039
1040 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1041 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001042
1043 // Merge `other` at the end of `this`. Successors and dominated blocks of
1044 // `other` are changed to be successors and dominated blocks of `this`. Note
1045 // that this method does not update the graph, reverse post order, loop
1046 // information, nor make sure the blocks are consistent (for example ending
1047 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001048 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001049
1050 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1051 // of `this` are moved to `other`.
1052 // Note that this method does not update the graph, reverse post order, loop
1053 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001054 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001055 void ReplaceWith(HBasicBlock* other);
1056
David Brazdil2d7352b2015-04-20 14:52:42 +01001057 // Merge `other` at the end of `this`. This method updates loops, reverse post
1058 // order, links to predecessors, successors, dominators and deletes the block
1059 // from the graph. The two blocks must be successive, i.e. `this` the only
1060 // predecessor of `other` and vice versa.
1061 void MergeWith(HBasicBlock* other);
1062
1063 // Disconnects `this` from all its predecessors, successors and dominator,
1064 // removes it from all loops it is included in and eventually from the graph.
1065 // The block must not dominate any other block. Predecessors and successors
1066 // are safely updated.
1067 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001068
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001069 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001070 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001071 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001072 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001073 // Replace instruction `initial` with `replacement` within this block.
1074 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1075 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001076 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001077 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001078 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1079 // instruction list. With 'ensure_safety' set to true, it verifies that the
1080 // instruction is not in use and removes it from the use lists of its inputs.
1081 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1082 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001083 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001084
1085 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001086 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001087 }
1088
Roland Levillain6b879dd2014-09-22 17:13:44 +01001089 bool IsLoopPreHeaderFirstPredecessor() const {
1090 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001091 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001092 }
1093
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001094 bool IsFirstPredecessorBackEdge() const {
1095 DCHECK(IsLoopHeader());
1096 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1097 }
1098
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001099 HLoopInformation* GetLoopInformation() const {
1100 return loop_information_;
1101 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001102
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001103 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001104 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001105 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001106 void SetInLoop(HLoopInformation* info) {
1107 if (IsLoopHeader()) {
1108 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001109 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001110 loop_information_ = info;
1111 } else if (loop_information_->Contains(*info->GetHeader())) {
1112 // Block is currently part of an outer loop. Make it part of this inner loop.
1113 // Note that a non loop header having a loop information means this loop information
1114 // has already been populated
1115 loop_information_ = info;
1116 } else {
1117 // Block is part of an inner loop. Do not update the loop information.
1118 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1119 // at this point, because this method is being called while populating `info`.
1120 }
1121 }
1122
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001123 // Raw update of the loop information.
1124 void SetLoopInformation(HLoopInformation* info) {
1125 loop_information_ = info;
1126 }
1127
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001128 bool IsInLoop() const { return loop_information_ != nullptr; }
1129
David Brazdilec16f792015-08-19 15:04:01 +01001130 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1131
1132 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1133 try_catch_information_ = try_catch_information;
1134 }
1135
1136 bool IsTryBlock() const {
1137 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1138 }
1139
1140 bool IsCatchBlock() const {
1141 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1142 }
David Brazdilffee3d32015-07-06 11:48:53 +01001143
1144 // Returns the try entry that this block's successors should have. They will
1145 // be in the same try, unless the block ends in a try boundary. In that case,
1146 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001147 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001148
David Brazdild7558da2015-09-22 13:04:14 +01001149 bool HasThrowingInstructions() const;
1150
David Brazdila4b8c212015-05-07 09:59:30 +01001151 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001152 bool Dominates(HBasicBlock* block) const;
1153
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001154 size_t GetLifetimeStart() const { return lifetime_start_; }
1155 size_t GetLifetimeEnd() const { return lifetime_end_; }
1156
1157 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1158 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1159
David Brazdil8d5b8b22015-03-24 10:51:52 +00001160 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001161 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001162 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001163 bool HasSinglePhi() const;
1164
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001165 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001166 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001167 ArenaVector<HBasicBlock*> predecessors_;
1168 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001169 HInstructionList instructions_;
1170 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001171 HLoopInformation* loop_information_;
1172 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001173 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001174 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001175 // The dex program counter of the first instruction of this block.
1176 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001177 size_t lifetime_start_;
1178 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001179 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001180
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001181 friend class HGraph;
1182 friend class HInstruction;
1183
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001184 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1185};
1186
David Brazdilb2bd1c52015-03-25 11:17:37 +00001187// Iterates over the LoopInformation of all loops which contain 'block'
1188// from the innermost to the outermost.
1189class HLoopInformationOutwardIterator : public ValueObject {
1190 public:
1191 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1192 : current_(block.GetLoopInformation()) {}
1193
1194 bool Done() const { return current_ == nullptr; }
1195
1196 void Advance() {
1197 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001198 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001199 }
1200
1201 HLoopInformation* Current() const {
1202 DCHECK(!Done());
1203 return current_;
1204 }
1205
1206 private:
1207 HLoopInformation* current_;
1208
1209 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1210};
1211
Alexandre Ramesef20f712015-06-09 10:29:30 +01001212#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001213 M(Above, Condition) \
1214 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001215 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001216 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001217 M(ArrayGet, Instruction) \
1218 M(ArrayLength, Instruction) \
1219 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001220 M(Below, Condition) \
1221 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001222 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001223 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001224 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001225 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001226 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001227 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001228 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001229 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001230 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001231 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001232 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001233 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001234 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001235 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001236 M(Exit, Instruction) \
1237 M(FloatConstant, Constant) \
1238 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001239 M(GreaterThan, Condition) \
1240 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001241 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001242 M(InstanceFieldGet, Instruction) \
1243 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001244 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001245 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001246 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001247 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001248 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001249 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001250 M(LessThan, Condition) \
1251 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001252 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001253 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001254 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001255 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001256 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001257 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001258 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001259 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001260 M(Neg, UnaryOperation) \
1261 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001262 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001263 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001264 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001265 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001266 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001267 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001268 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001269 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001270 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001271 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001272 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001273 M(Return, Instruction) \
1274 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001275 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001276 M(Shl, BinaryOperation) \
1277 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001278 M(StaticFieldGet, Instruction) \
1279 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001280 M(UnresolvedInstanceFieldGet, Instruction) \
1281 M(UnresolvedInstanceFieldSet, Instruction) \
1282 M(UnresolvedStaticFieldGet, Instruction) \
1283 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001284 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001285 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001286 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001287 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001288 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001289 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001290 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001291 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001292
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001293/*
1294 * Instructions, shared across several (not all) architectures.
1295 */
1296#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1297#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1298#else
1299#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001300 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001301 M(MultiplyAccumulate, Instruction) \
1302 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001303#endif
1304
Vladimir Markob4536b72015-11-24 13:45:23 +00001305#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001306#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001307#else
1308#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1309 M(ArmDexCacheArraysBase, Instruction)
1310#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001311
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001312#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001313#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001314#else
1315#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001316 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001317#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001318
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001319#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001320#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001321#else
1322#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1323 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001324 M(MipsDexCacheArraysBase, Instruction) \
1325 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001326#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001327
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001328#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1329
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001330#ifndef ART_ENABLE_CODEGEN_x86
1331#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1332#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001333#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1334 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001335 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001336 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001337 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001338#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001339
1340#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1341
1342#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1343 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001344 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001345 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1346 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001347 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001348 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001349 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1350 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1351
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001352#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1353 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001354 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001355 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001356 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001357 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001358
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001359#define FOR_EACH_INSTRUCTION(M) \
1360 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1361 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1362
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001363#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001364FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1365#undef FORWARD_DECLARATION
1366
Vladimir Marko372f10e2016-05-17 16:30:10 +01001367#define DECLARE_INSTRUCTION(type) \
1368 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1369 const char* DebugName() const OVERRIDE { return #type; } \
1370 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1371 return other->Is##type(); \
1372 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001373 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001374
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001375#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1376 bool Is##type() const { return As##type() != nullptr; } \
1377 const H##type* As##type() const { return this; } \
1378 H##type* As##type() { return this; }
1379
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001380template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001381class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001382 public:
David Brazdiled596192015-01-23 10:39:45 +00001383 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001384 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001385 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001386
Vladimir Marko46817b82016-03-29 12:21:58 +01001387 // Hook for the IntrusiveForwardList<>.
1388 // TODO: Hide this better.
1389 IntrusiveForwardListHook hook;
1390
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001391 private:
David Brazdiled596192015-01-23 10:39:45 +00001392 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001393 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001394
1395 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001396 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001397
Vladimir Marko46817b82016-03-29 12:21:58 +01001398 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001399
1400 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1401};
1402
David Brazdiled596192015-01-23 10:39:45 +00001403template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001404using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001405
David Brazdil1abb4192015-02-17 18:33:36 +00001406// This class is used by HEnvironment and HInstruction classes to record the
1407// instructions they use and pointers to the corresponding HUseListNodes kept
1408// by the used instructions.
1409template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001410class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001411 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001412 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1413 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001414
Vladimir Marko46817b82016-03-29 12:21:58 +01001415 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1416 : HUserRecord(old_record.instruction_, before_use_node) {}
1417 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1418 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001419 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001420 }
1421
1422 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001423 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1424 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001425
1426 private:
1427 // Instruction used by the user.
1428 HInstruction* instruction_;
1429
Vladimir Marko46817b82016-03-29 12:21:58 +01001430 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1431 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001432};
1433
Vladimir Markoe9004912016-06-16 16:50:52 +01001434// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1435// This is used for HInstruction::GetInputs() to return a container wrapper providing
1436// HInstruction* values even though the underlying container has HUserRecord<>s.
1437struct HInputExtractor {
1438 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1439 return record.GetInstruction();
1440 }
1441 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1442 return record.GetInstruction();
1443 }
1444};
1445
1446using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1447using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1448
Aart Bik854a02b2015-07-14 16:07:00 -07001449/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001450 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001451 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001452 * For write/read dependences on fields/arrays, the dependence analysis uses
1453 * type disambiguation (e.g. a float field write cannot modify the value of an
1454 * integer field read) and the access type (e.g. a reference array write cannot
1455 * modify the value of a reference field read [although it may modify the
1456 * reference fetch prior to reading the field, which is represented by its own
1457 * write/read dependence]). The analysis makes conservative points-to
1458 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1459 * the same, and any reference read depends on any reference read without
1460 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001461 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001462 * The internal representation uses 38-bit and is described in the table below.
1463 * The first line indicates the side effect, and for field/array accesses the
1464 * second line indicates the type of the access (in the order of the
1465 * Primitive::Type enum).
1466 * The two numbered lines below indicate the bit position in the bitfield (read
1467 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001468 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001469 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1470 * +-------------+---------+---------+--------------+---------+---------+
1471 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1472 * | 3 |333333322|222222221| 1 |111111110|000000000|
1473 * | 7 |654321098|765432109| 8 |765432109|876543210|
1474 *
1475 * Note that, to ease the implementation, 'changes' bits are least significant
1476 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001477 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001478class SideEffects : public ValueObject {
1479 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001480 SideEffects() : flags_(0) {}
1481
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001482 static SideEffects None() {
1483 return SideEffects(0);
1484 }
1485
1486 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001487 return SideEffects(kAllChangeBits | kAllDependOnBits);
1488 }
1489
1490 static SideEffects AllChanges() {
1491 return SideEffects(kAllChangeBits);
1492 }
1493
1494 static SideEffects AllDependencies() {
1495 return SideEffects(kAllDependOnBits);
1496 }
1497
1498 static SideEffects AllExceptGCDependency() {
1499 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1500 }
1501
1502 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001503 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001504 }
1505
Aart Bik34c3ba92015-07-20 14:08:59 -07001506 static SideEffects AllWrites() {
1507 return SideEffects(kAllWrites);
1508 }
1509
1510 static SideEffects AllReads() {
1511 return SideEffects(kAllReads);
1512 }
1513
1514 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1515 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001516 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001517 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001518 }
1519
Aart Bik854a02b2015-07-14 16:07:00 -07001520 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001521 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001522 }
1523
Aart Bik34c3ba92015-07-20 14:08:59 -07001524 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1525 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001526 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001527 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001528 }
1529
1530 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001531 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001532 }
1533
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001534 static SideEffects CanTriggerGC() {
1535 return SideEffects(1ULL << kCanTriggerGCBit);
1536 }
1537
1538 static SideEffects DependsOnGC() {
1539 return SideEffects(1ULL << kDependsOnGCBit);
1540 }
1541
Aart Bik854a02b2015-07-14 16:07:00 -07001542 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001543 SideEffects Union(SideEffects other) const {
1544 return SideEffects(flags_ | other.flags_);
1545 }
1546
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001547 SideEffects Exclusion(SideEffects other) const {
1548 return SideEffects(flags_ & ~other.flags_);
1549 }
1550
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001551 void Add(SideEffects other) {
1552 flags_ |= other.flags_;
1553 }
1554
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001555 bool Includes(SideEffects other) const {
1556 return (other.flags_ & flags_) == other.flags_;
1557 }
1558
1559 bool HasSideEffects() const {
1560 return (flags_ & kAllChangeBits);
1561 }
1562
1563 bool HasDependencies() const {
1564 return (flags_ & kAllDependOnBits);
1565 }
1566
1567 // Returns true if there are no side effects or dependencies.
1568 bool DoesNothing() const {
1569 return flags_ == 0;
1570 }
1571
Aart Bik854a02b2015-07-14 16:07:00 -07001572 // Returns true if something is written.
1573 bool DoesAnyWrite() const {
1574 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001575 }
1576
Aart Bik854a02b2015-07-14 16:07:00 -07001577 // Returns true if something is read.
1578 bool DoesAnyRead() const {
1579 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001580 }
1581
Aart Bik854a02b2015-07-14 16:07:00 -07001582 // Returns true if potentially everything is written and read
1583 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001584 bool DoesAllReadWrite() const {
1585 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1586 }
1587
Aart Bik854a02b2015-07-14 16:07:00 -07001588 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001589 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001590 }
1591
Roland Levillain0d5a2812015-11-13 10:07:31 +00001592 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001593 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001594 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1595 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001596 }
1597
1598 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001599 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001600 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001601 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001602 for (int s = kLastBit; s >= 0; s--) {
1603 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1604 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1605 // This is a bit for the GC side effect.
1606 if (current_bit_is_set) {
1607 flags += "GC";
1608 }
Aart Bik854a02b2015-07-14 16:07:00 -07001609 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001610 } else {
1611 // This is a bit for the array/field analysis.
1612 // The underscore character stands for the 'can trigger GC' bit.
1613 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1614 if (current_bit_is_set) {
1615 flags += kDebug[s];
1616 }
1617 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1618 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1619 flags += "|";
1620 }
1621 }
Aart Bik854a02b2015-07-14 16:07:00 -07001622 }
1623 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001624 }
1625
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001626 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001627
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001628 private:
1629 static constexpr int kFieldArrayAnalysisBits = 9;
1630
1631 static constexpr int kFieldWriteOffset = 0;
1632 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1633 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1634 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1635
1636 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1637
1638 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1639 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1640 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1641 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1642
1643 static constexpr int kLastBit = kDependsOnGCBit;
1644 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1645
1646 // Aliases.
1647
1648 static_assert(kChangeBits == kDependOnBits,
1649 "the 'change' bits should match the 'depend on' bits.");
1650
1651 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1652 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1653 static constexpr uint64_t kAllWrites =
1654 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1655 static constexpr uint64_t kAllReads =
1656 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001657
Aart Bik854a02b2015-07-14 16:07:00 -07001658 // Translates type to bit flag.
1659 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1660 CHECK_NE(type, Primitive::kPrimVoid);
1661 const uint64_t one = 1;
1662 const int shift = type; // 0-based consecutive enum
1663 DCHECK_LE(kFieldWriteOffset, shift);
1664 DCHECK_LT(shift, kArrayWriteOffset);
1665 return one << (type + offset);
1666 }
1667
1668 // Private constructor on direct flags value.
1669 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1670
1671 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001672};
1673
David Brazdiled596192015-01-23 10:39:45 +00001674// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001675class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001676 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001677 HEnvironment(ArenaAllocator* arena,
1678 size_t number_of_vregs,
1679 const DexFile& dex_file,
1680 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001681 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001682 InvokeType invoke_type,
1683 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001684 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1685 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001686 parent_(nullptr),
1687 dex_file_(dex_file),
1688 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001689 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001690 invoke_type_(invoke_type),
1691 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001692 }
1693
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001694 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001695 : HEnvironment(arena,
1696 to_copy.Size(),
1697 to_copy.GetDexFile(),
1698 to_copy.GetMethodIdx(),
1699 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001700 to_copy.GetInvokeType(),
1701 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001702
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001703 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001704 if (parent_ != nullptr) {
1705 parent_->SetAndCopyParentChain(allocator, parent);
1706 } else {
1707 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1708 parent_->CopyFrom(parent);
1709 if (parent->GetParent() != nullptr) {
1710 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1711 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001712 }
David Brazdiled596192015-01-23 10:39:45 +00001713 }
1714
Vladimir Marko71bf8092015-09-15 15:33:14 +01001715 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001716 void CopyFrom(HEnvironment* environment);
1717
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001718 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1719 // input to the loop phi instead. This is for inserting instructions that
1720 // require an environment (like HDeoptimization) in the loop pre-header.
1721 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001722
1723 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001724 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001725 }
1726
1727 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001728 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001729 }
1730
David Brazdil1abb4192015-02-17 18:33:36 +00001731 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001732
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001733 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001734
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001735 HEnvironment* GetParent() const { return parent_; }
1736
1737 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001738 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001739 }
1740
1741 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001742 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001743 }
1744
1745 uint32_t GetDexPc() const {
1746 return dex_pc_;
1747 }
1748
1749 uint32_t GetMethodIdx() const {
1750 return method_idx_;
1751 }
1752
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001753 InvokeType GetInvokeType() const {
1754 return invoke_type_;
1755 }
1756
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001757 const DexFile& GetDexFile() const {
1758 return dex_file_;
1759 }
1760
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001761 HInstruction* GetHolder() const {
1762 return holder_;
1763 }
1764
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001765
1766 bool IsFromInlinedInvoke() const {
1767 return GetParent() != nullptr;
1768 }
1769
David Brazdiled596192015-01-23 10:39:45 +00001770 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001771 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1772 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001773 HEnvironment* parent_;
1774 const DexFile& dex_file_;
1775 const uint32_t method_idx_;
1776 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001777 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001778
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001779 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001780 HInstruction* const holder_;
1781
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001782 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001783
1784 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1785};
1786
Vladimir Markof9f64412015-09-02 14:05:49 +01001787class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001788 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001789 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001790 : previous_(nullptr),
1791 next_(nullptr),
1792 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001793 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001794 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001795 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001796 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001797 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001798 locations_(nullptr),
1799 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001800 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001801 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001802 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1803 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1804 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001805
Dave Allison20dfc792014-06-16 20:44:29 -07001806 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001807
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001808#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001809 enum InstructionKind {
1810 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1811 };
1812#undef DECLARE_KIND
1813
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001814 HInstruction* GetNext() const { return next_; }
1815 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001816
Calin Juravle77520bc2015-01-12 18:45:46 +00001817 HInstruction* GetNextDisregardingMoves() const;
1818 HInstruction* GetPreviousDisregardingMoves() const;
1819
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001820 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001821 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001822 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001823 bool IsInBlock() const { return block_ != nullptr; }
1824 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001825 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1826 bool IsIrreducibleLoopHeaderPhi() const {
1827 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1828 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001829
Vladimir Marko372f10e2016-05-17 16:30:10 +01001830 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1831
1832 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1833 // One virtual method is enough, just const_cast<> and then re-add the const.
1834 return ArrayRef<const HUserRecord<HInstruction*>>(
1835 const_cast<HInstruction*>(this)->GetInputRecords());
1836 }
1837
Vladimir Markoe9004912016-06-16 16:50:52 +01001838 HInputsRef GetInputs() {
1839 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001840 }
1841
Vladimir Markoe9004912016-06-16 16:50:52 +01001842 HConstInputsRef GetInputs() const {
1843 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001844 }
1845
1846 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001847 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001848
Vladimir Marko372f10e2016-05-17 16:30:10 +01001849 void SetRawInputAt(size_t index, HInstruction* input) {
1850 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1851 }
1852
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001853 virtual void Accept(HGraphVisitor* visitor) = 0;
1854 virtual const char* DebugName() const = 0;
1855
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001856 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1857
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001858 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001859
1860 uint32_t GetDexPc() const { return dex_pc_; }
1861
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001862 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001863
Roland Levillaine161a2a2014-10-03 12:45:18 +01001864 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001865 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001866
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001867 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001868 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001869
Calin Juravle10e244f2015-01-26 18:54:32 +00001870 // Does not apply for all instructions, but having this at top level greatly
1871 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001872 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001873 virtual bool CanBeNull() const {
1874 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1875 return true;
1876 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001877
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001878 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001879 return false;
1880 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001881
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001882 virtual bool IsActualObject() const {
1883 return GetType() == Primitive::kPrimNot;
1884 }
1885
Calin Juravle2e768302015-07-28 14:41:11 +00001886 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001887
Calin Juravle61d544b2015-02-23 16:46:57 +00001888 ReferenceTypeInfo GetReferenceTypeInfo() const {
1889 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001890 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001891 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001892 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001893
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001894 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001895 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001896 // Note: fixup_end remains valid across push_front().
1897 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1898 HUseListNode<HInstruction*>* new_node =
1899 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1900 uses_.push_front(*new_node);
1901 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001902 }
1903
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001904 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001905 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001906 // Note: env_fixup_end remains valid across push_front().
1907 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1908 HUseListNode<HEnvironment*>* new_node =
1909 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1910 env_uses_.push_front(*new_node);
1911 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001912 }
1913
David Brazdil1abb4192015-02-17 18:33:36 +00001914 void RemoveAsUserOfInput(size_t input) {
1915 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001916 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);
David Brazdil1abb4192015-02-17 18:33:36 +00001919 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001920
Vladimir Marko372f10e2016-05-17 16:30:10 +01001921 void RemoveAsUserOfAllInputs() {
1922 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1923 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1924 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1925 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1926 }
1927 }
1928
David Brazdil1abb4192015-02-17 18:33:36 +00001929 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1930 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001931
Vladimir Marko46817b82016-03-29 12:21:58 +01001932 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1933 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1934 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001935 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001936 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001937 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001938
Roland Levillain6c82d402014-10-13 16:10:27 +01001939 // Does this instruction strictly dominate `other_instruction`?
1940 // Returns false if this instruction and `other_instruction` are the same.
1941 // Aborts if this instruction and `other_instruction` are both phis.
1942 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001943
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001944 int GetId() const { return id_; }
1945 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001946
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001947 int GetSsaIndex() const { return ssa_index_; }
1948 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1949 bool HasSsaIndex() const { return ssa_index_ != -1; }
1950
1951 bool HasEnvironment() const { return environment_ != nullptr; }
1952 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001953 // Set the `environment_` field. Raw because this method does not
1954 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001955 void SetRawEnvironment(HEnvironment* environment) {
1956 DCHECK(environment_ == nullptr);
1957 DCHECK_EQ(environment->GetHolder(), this);
1958 environment_ = environment;
1959 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001960
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001961 void InsertRawEnvironment(HEnvironment* environment) {
1962 DCHECK(environment_ != nullptr);
1963 DCHECK_EQ(environment->GetHolder(), this);
1964 DCHECK(environment->GetParent() == nullptr);
1965 environment->parent_ = environment_;
1966 environment_ = environment;
1967 }
1968
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001969 void RemoveEnvironment();
1970
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001971 // Set the environment of this instruction, copying it from `environment`. While
1972 // copying, the uses lists are being updated.
1973 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001974 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001975 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001976 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001977 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001978 if (environment->GetParent() != nullptr) {
1979 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1980 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001981 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001982
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001983 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1984 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001985 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001986 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001987 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001988 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001989 if (environment->GetParent() != nullptr) {
1990 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1991 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001992 }
1993
Nicolas Geoffray39468442014-09-02 15:17:15 +01001994 // Returns the number of entries in the environment. Typically, that is the
1995 // number of dex registers in a method. It could be more in case of inlining.
1996 size_t EnvironmentSize() const;
1997
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001998 LocationSummary* GetLocations() const { return locations_; }
1999 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002000
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002001 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002002 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002003
Alexandre Rames188d4312015-04-09 18:30:21 +01002004 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2005 // uses of this instruction by `other` are *not* updated.
2006 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2007 ReplaceWith(other);
2008 other->ReplaceInput(this, use_index);
2009 }
2010
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002011 // Move `this` instruction before `cursor`.
2012 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002013
Vladimir Markofb337ea2015-11-25 15:25:10 +00002014 // Move `this` before its first user and out of any loops. If there is no
2015 // out-of-loop user that dominates all other users, move the instruction
2016 // to the end of the out-of-loop common dominator of the user's blocks.
2017 //
2018 // This can be used only on non-throwing instructions with no side effects that
2019 // have at least one use but no environment uses.
2020 void MoveBeforeFirstUserAndOutOfLoops();
2021
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002022#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002023 bool Is##type() const; \
2024 const H##type* As##type() const; \
2025 H##type* As##type();
2026
2027 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2028#undef INSTRUCTION_TYPE_CHECK
2029
2030#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002031 bool Is##type() const { return (As##type() != nullptr); } \
2032 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002033 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002034 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002035#undef INSTRUCTION_TYPE_CHECK
2036
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002037 // Returns whether the instruction can be moved within the graph.
2038 virtual bool CanBeMoved() const { return false; }
2039
2040 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002041 virtual bool InstructionTypeEquals(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 any data encoded in the two instructions is equal.
2046 // This method does not look at the inputs. Both instructions must be
2047 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002048 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002049 return false;
2050 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002051
2052 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002053 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002054 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002055 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002056
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002057 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2058 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2059 // the virtual function because the __attribute__((__pure__)) doesn't really
2060 // apply the strong requirement for virtual functions, preventing optimizations.
2061 InstructionKind GetKind() const PURE;
2062 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002063
2064 virtual size_t ComputeHashCode() const {
2065 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002066 for (const HInstruction* input : GetInputs()) {
2067 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002068 }
2069 return result;
2070 }
2071
2072 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002073 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002074 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002075
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002076 size_t GetLifetimePosition() const { return lifetime_position_; }
2077 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2078 LiveInterval* GetLiveInterval() const { return live_interval_; }
2079 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2080 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2081
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002082 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2083
2084 // Returns whether the code generation of the instruction will require to have access
2085 // to the current method. Such instructions are:
2086 // (1): Instructions that require an environment, as calling the runtime requires
2087 // to walk the stack and have the current method stored at a specific stack address.
2088 // (2): Object literals like classes and strings, that are loaded from the dex cache
2089 // fields of the current method.
2090 bool NeedsCurrentMethod() const {
2091 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2092 }
2093
Vladimir Markodc151b22015-10-15 18:02:30 +01002094 // Returns whether the code generation of the instruction will require to have access
2095 // to the dex cache of the current method's declaring class via the current method.
2096 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002097
Mark Mendellc4701932015-04-10 13:18:51 -04002098 // Does this instruction have any use in an environment before
2099 // control flow hits 'other'?
2100 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2101
2102 // Remove all references to environment uses of this instruction.
2103 // The caller must ensure that this is safe to do.
2104 void RemoveEnvironmentUsers();
2105
Vladimir Markoa1de9182016-02-25 11:37:38 +00002106 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2107 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002108
David Brazdil1abb4192015-02-17 18:33:36 +00002109 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002110 // If set, the machine code for this instruction is assumed to be generated by
2111 // its users. Used by liveness analysis to compute use positions accordingly.
2112 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2113 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2114 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2115 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2116
Vladimir Marko372f10e2016-05-17 16:30:10 +01002117 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2118 return GetInputRecords()[i];
2119 }
2120
2121 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2122 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2123 input_records[index] = input;
2124 }
David Brazdil1abb4192015-02-17 18:33:36 +00002125
Vladimir Markoa1de9182016-02-25 11:37:38 +00002126 uint32_t GetPackedFields() const {
2127 return packed_fields_;
2128 }
2129
2130 template <size_t flag>
2131 bool GetPackedFlag() const {
2132 return (packed_fields_ & (1u << flag)) != 0u;
2133 }
2134
2135 template <size_t flag>
2136 void SetPackedFlag(bool value = true) {
2137 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2138 }
2139
2140 template <typename BitFieldType>
2141 typename BitFieldType::value_type GetPackedField() const {
2142 return BitFieldType::Decode(packed_fields_);
2143 }
2144
2145 template <typename BitFieldType>
2146 void SetPackedField(typename BitFieldType::value_type value) {
2147 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2148 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2149 }
2150
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002151 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002152 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2153 auto before_use_node = uses_.before_begin();
2154 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2155 HInstruction* user = use_node->GetUser();
2156 size_t input_index = use_node->GetIndex();
2157 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2158 before_use_node = use_node;
2159 }
2160 }
2161
2162 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2163 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2164 if (next != uses_.end()) {
2165 HInstruction* next_user = next->GetUser();
2166 size_t next_index = next->GetIndex();
2167 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2168 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2169 }
2170 }
2171
2172 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2173 auto before_env_use_node = env_uses_.before_begin();
2174 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2175 HEnvironment* user = env_use_node->GetUser();
2176 size_t input_index = env_use_node->GetIndex();
2177 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2178 before_env_use_node = env_use_node;
2179 }
2180 }
2181
2182 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2183 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2184 if (next != env_uses_.end()) {
2185 HEnvironment* next_user = next->GetUser();
2186 size_t next_index = next->GetIndex();
2187 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2188 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2189 }
2190 }
David Brazdil1abb4192015-02-17 18:33:36 +00002191
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002192 HInstruction* previous_;
2193 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002194 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002195 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002196
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002197 // An instruction gets an id when it is added to the graph.
2198 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002199 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002200 int id_;
2201
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002202 // When doing liveness analysis, instructions that have uses get an SSA index.
2203 int ssa_index_;
2204
Vladimir Markoa1de9182016-02-25 11:37:38 +00002205 // Packed fields.
2206 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002207
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002208 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002209 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002210
2211 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002212 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002213
Nicolas Geoffray39468442014-09-02 15:17:15 +01002214 // The environment associated with this instruction. Not null if the instruction
2215 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002216 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002217
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002218 // Set by the code generator.
2219 LocationSummary* locations_;
2220
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002221 // Set by the liveness analysis.
2222 LiveInterval* live_interval_;
2223
2224 // Set by the liveness analysis, this is the position in a linear
2225 // order of blocks where this instruction's live interval start.
2226 size_t lifetime_position_;
2227
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002228 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002229
Vladimir Markoa1de9182016-02-25 11:37:38 +00002230 // The reference handle part of the reference type info.
2231 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002232 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002233 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002234
David Brazdil1abb4192015-02-17 18:33:36 +00002235 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002236 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002237 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002238 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002239 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002240
2241 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2242};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002243std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002244
2245class HInstructionIterator : public ValueObject {
2246 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002247 explicit HInstructionIterator(const HInstructionList& instructions)
2248 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002249 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002250 }
2251
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002252 bool Done() const { return instruction_ == nullptr; }
2253 HInstruction* Current() const { return instruction_; }
2254 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002255 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002256 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002257 }
2258
2259 private:
2260 HInstruction* instruction_;
2261 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002262
2263 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002264};
2265
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002266class HBackwardInstructionIterator : public ValueObject {
2267 public:
2268 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2269 : instruction_(instructions.last_instruction_) {
2270 next_ = Done() ? nullptr : instruction_->GetPrevious();
2271 }
2272
2273 bool Done() const { return instruction_ == nullptr; }
2274 HInstruction* Current() const { return instruction_; }
2275 void Advance() {
2276 instruction_ = next_;
2277 next_ = Done() ? nullptr : instruction_->GetPrevious();
2278 }
2279
2280 private:
2281 HInstruction* instruction_;
2282 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002283
2284 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002285};
2286
Vladimir Markof9f64412015-09-02 14:05:49 +01002287template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002288class HTemplateInstruction: public HInstruction {
2289 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002290 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002291 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002292 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002293
Vladimir Marko372f10e2016-05-17 16:30:10 +01002294 using HInstruction::GetInputRecords; // Keep the const version visible.
2295 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2296 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002297 }
2298
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002299 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002300 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002301
2302 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002303};
2304
Vladimir Markof9f64412015-09-02 14:05:49 +01002305// HTemplateInstruction specialization for N=0.
2306template<>
2307class HTemplateInstruction<0>: public HInstruction {
2308 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002309 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002310 : HInstruction(side_effects, dex_pc) {}
2311
Vladimir Markof9f64412015-09-02 14:05:49 +01002312 virtual ~HTemplateInstruction() {}
2313
Vladimir Marko372f10e2016-05-17 16:30:10 +01002314 using HInstruction::GetInputRecords; // Keep the const version visible.
2315 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2316 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002317 }
2318
2319 private:
2320 friend class SsaBuilder;
2321};
2322
Dave Allison20dfc792014-06-16 20:44:29 -07002323template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002324class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002325 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002326 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002327 : HTemplateInstruction<N>(side_effects, dex_pc) {
2328 this->template SetPackedField<TypeField>(type);
2329 }
Dave Allison20dfc792014-06-16 20:44:29 -07002330 virtual ~HExpression() {}
2331
Vladimir Markoa1de9182016-02-25 11:37:38 +00002332 Primitive::Type GetType() const OVERRIDE {
2333 return TypeField::Decode(this->GetPackedFields());
2334 }
Dave Allison20dfc792014-06-16 20:44:29 -07002335
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002336 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002337 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2338 static constexpr size_t kFieldTypeSize =
2339 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2340 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2341 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2342 "Too many packed fields.");
2343 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002344};
2345
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002346// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2347// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002348class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002349 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002350 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2351 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002352
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002353 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002354
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002355 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002356
2357 private:
2358 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2359};
2360
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002361// Represents dex's RETURN opcodes. A HReturn is a control flow
2362// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002363class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002364 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002365 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2366 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002367 SetRawInputAt(0, value);
2368 }
2369
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002370 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002371
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002372 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002373
2374 private:
2375 DISALLOW_COPY_AND_ASSIGN(HReturn);
2376};
2377
Vladimir Markofcb503c2016-05-18 12:48:17 +01002378class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002379 public:
2380 HPhi(ArenaAllocator* arena,
2381 uint32_t reg_number,
2382 size_t number_of_inputs,
2383 Primitive::Type type,
2384 uint32_t dex_pc = kNoDexPc)
2385 : HInstruction(SideEffects::None(), dex_pc),
2386 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2387 reg_number_(reg_number) {
2388 SetPackedField<TypeField>(ToPhiType(type));
2389 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2390 // Phis are constructed live and marked dead if conflicting or unused.
2391 // Individual steps of SsaBuilder should assume that if a phi has been
2392 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2393 SetPackedFlag<kFlagIsLive>(true);
2394 SetPackedFlag<kFlagCanBeNull>(true);
2395 }
2396
2397 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2398 static Primitive::Type ToPhiType(Primitive::Type type) {
2399 return Primitive::PrimitiveKind(type);
2400 }
2401
2402 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2403
Vladimir Marko372f10e2016-05-17 16:30:10 +01002404 using HInstruction::GetInputRecords; // Keep the const version visible.
2405 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2406 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2407 }
David Brazdildee58d62016-04-07 09:54:26 +00002408
2409 void AddInput(HInstruction* input);
2410 void RemoveInputAt(size_t index);
2411
2412 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2413 void SetType(Primitive::Type new_type) {
2414 // Make sure that only valid type changes occur. The following are allowed:
2415 // (1) int -> float/ref (primitive type propagation),
2416 // (2) long -> double (primitive type propagation).
2417 DCHECK(GetType() == new_type ||
2418 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2419 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2420 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2421 SetPackedField<TypeField>(new_type);
2422 }
2423
2424 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2425 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2426
2427 uint32_t GetRegNumber() const { return reg_number_; }
2428
2429 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2430 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2431 bool IsDead() const { return !IsLive(); }
2432 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2433
Vladimir Markoe9004912016-06-16 16:50:52 +01002434 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002435 return other != nullptr
2436 && other->IsPhi()
2437 && other->AsPhi()->GetBlock() == GetBlock()
2438 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2439 }
2440
2441 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2442 // An equivalent phi is a phi having the same dex register and type.
2443 // It assumes that phis with the same dex register are adjacent.
2444 HPhi* GetNextEquivalentPhiWithSameType() {
2445 HInstruction* next = GetNext();
2446 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2447 if (next->GetType() == GetType()) {
2448 return next->AsPhi();
2449 }
2450 next = next->GetNext();
2451 }
2452 return nullptr;
2453 }
2454
2455 DECLARE_INSTRUCTION(Phi);
2456
David Brazdildee58d62016-04-07 09:54:26 +00002457 private:
2458 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2459 static constexpr size_t kFieldTypeSize =
2460 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2461 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2462 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2463 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2464 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2465 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2466
Vladimir Marko372f10e2016-05-17 16:30:10 +01002467 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002468 const uint32_t reg_number_;
2469
2470 DISALLOW_COPY_AND_ASSIGN(HPhi);
2471};
2472
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002473// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002474// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002475// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002476class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002477 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002478 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002479
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002480 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002481
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002482 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002483
2484 private:
2485 DISALLOW_COPY_AND_ASSIGN(HExit);
2486};
2487
2488// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002489class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002490 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002491 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002492
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002493 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002494
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002495 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002496 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002497 }
2498
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002499 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002500
2501 private:
2502 DISALLOW_COPY_AND_ASSIGN(HGoto);
2503};
2504
Roland Levillain9867bc72015-08-05 10:21:34 +01002505class HConstant : public HExpression<0> {
2506 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002507 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2508 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002509
2510 bool CanBeMoved() const OVERRIDE { return true; }
2511
Roland Levillain1a653882016-03-18 18:05:57 +00002512 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002513 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002514 // Is this constant 0 in the arithmetic sense?
2515 virtual bool IsArithmeticZero() const { return false; }
2516 // Is this constant a 0-bit pattern?
2517 virtual bool IsZeroBitPattern() const { return false; }
2518 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002519 virtual bool IsOne() const { return false; }
2520
David Brazdil77a48ae2015-09-15 12:34:04 +00002521 virtual uint64_t GetValueAsUint64() const = 0;
2522
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002523 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002524
2525 private:
2526 DISALLOW_COPY_AND_ASSIGN(HConstant);
2527};
2528
Vladimir Markofcb503c2016-05-18 12:48:17 +01002529class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002530 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002531 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002532 return true;
2533 }
2534
David Brazdil77a48ae2015-09-15 12:34:04 +00002535 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2536
Roland Levillain9867bc72015-08-05 10:21:34 +01002537 size_t ComputeHashCode() const OVERRIDE { return 0; }
2538
Roland Levillain1a653882016-03-18 18:05:57 +00002539 // The null constant representation is a 0-bit pattern.
2540 virtual bool IsZeroBitPattern() const { return true; }
2541
Roland Levillain9867bc72015-08-05 10:21:34 +01002542 DECLARE_INSTRUCTION(NullConstant);
2543
2544 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002545 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002546
2547 friend class HGraph;
2548 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2549};
2550
2551// Constants of the type int. Those can be from Dex instructions, or
2552// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002553class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002554 public:
2555 int32_t GetValue() const { return value_; }
2556
David Brazdil9f389d42015-10-01 14:32:56 +01002557 uint64_t GetValueAsUint64() const OVERRIDE {
2558 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2559 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002560
Vladimir Marko372f10e2016-05-17 16:30:10 +01002561 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002562 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002563 return other->AsIntConstant()->value_ == value_;
2564 }
2565
2566 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2567
2568 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002569 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2570 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002571 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2572
Roland Levillain1a653882016-03-18 18:05:57 +00002573 // Integer constants are used to encode Boolean values as well,
2574 // where 1 means true and 0 means false.
2575 bool IsTrue() const { return GetValue() == 1; }
2576 bool IsFalse() const { return GetValue() == 0; }
2577
Roland Levillain9867bc72015-08-05 10:21:34 +01002578 DECLARE_INSTRUCTION(IntConstant);
2579
2580 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002581 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2582 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2583 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2584 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002585
2586 const int32_t value_;
2587
2588 friend class HGraph;
2589 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2590 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2591 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2592};
2593
Vladimir Markofcb503c2016-05-18 12:48:17 +01002594class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002595 public:
2596 int64_t GetValue() const { return value_; }
2597
David Brazdil77a48ae2015-09-15 12:34:04 +00002598 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2599
Vladimir Marko372f10e2016-05-17 16:30:10 +01002600 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002601 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002602 return other->AsLongConstant()->value_ == value_;
2603 }
2604
2605 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2606
2607 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002608 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2609 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002610 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2611
2612 DECLARE_INSTRUCTION(LongConstant);
2613
2614 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002615 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2616 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002617
2618 const int64_t value_;
2619
2620 friend class HGraph;
2621 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2622};
Dave Allison20dfc792014-06-16 20:44:29 -07002623
Vladimir Markofcb503c2016-05-18 12:48:17 +01002624class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002625 public:
2626 float GetValue() const { return value_; }
2627
2628 uint64_t GetValueAsUint64() const OVERRIDE {
2629 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2630 }
2631
Vladimir Marko372f10e2016-05-17 16:30:10 +01002632 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002633 DCHECK(other->IsFloatConstant()) << other->DebugName();
2634 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2635 }
2636
2637 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2638
2639 bool IsMinusOne() const OVERRIDE {
2640 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2641 }
Roland Levillain1a653882016-03-18 18:05:57 +00002642 bool IsArithmeticZero() const OVERRIDE {
2643 return std::fpclassify(value_) == FP_ZERO;
2644 }
2645 bool IsArithmeticPositiveZero() const {
2646 return IsArithmeticZero() && !std::signbit(value_);
2647 }
2648 bool IsArithmeticNegativeZero() const {
2649 return IsArithmeticZero() && std::signbit(value_);
2650 }
2651 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002652 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002653 }
2654 bool IsOne() const OVERRIDE {
2655 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2656 }
2657 bool IsNaN() const {
2658 return std::isnan(value_);
2659 }
2660
2661 DECLARE_INSTRUCTION(FloatConstant);
2662
2663 private:
2664 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2665 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2666 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2667 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2668
2669 const float value_;
2670
2671 // Only the SsaBuilder and HGraph can create floating-point constants.
2672 friend class SsaBuilder;
2673 friend class HGraph;
2674 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2675};
2676
Vladimir Markofcb503c2016-05-18 12:48:17 +01002677class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002678 public:
2679 double GetValue() const { return value_; }
2680
2681 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2682
Vladimir Marko372f10e2016-05-17 16:30:10 +01002683 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002684 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2685 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2686 }
2687
2688 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2689
2690 bool IsMinusOne() const OVERRIDE {
2691 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2692 }
Roland Levillain1a653882016-03-18 18:05:57 +00002693 bool IsArithmeticZero() const OVERRIDE {
2694 return std::fpclassify(value_) == FP_ZERO;
2695 }
2696 bool IsArithmeticPositiveZero() const {
2697 return IsArithmeticZero() && !std::signbit(value_);
2698 }
2699 bool IsArithmeticNegativeZero() const {
2700 return IsArithmeticZero() && std::signbit(value_);
2701 }
2702 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002703 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002704 }
2705 bool IsOne() const OVERRIDE {
2706 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2707 }
2708 bool IsNaN() const {
2709 return std::isnan(value_);
2710 }
2711
2712 DECLARE_INSTRUCTION(DoubleConstant);
2713
2714 private:
2715 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2716 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2717 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2718 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2719
2720 const double value_;
2721
2722 // Only the SsaBuilder and HGraph can create floating-point constants.
2723 friend class SsaBuilder;
2724 friend class HGraph;
2725 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2726};
2727
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002728// Conditional branch. A block ending with an HIf instruction must have
2729// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002730class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002731 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002732 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2733 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002734 SetRawInputAt(0, input);
2735 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002736
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002737 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002738
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002739 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002740 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002741 }
2742
2743 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002744 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002745 }
2746
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002747 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002748
2749 private:
2750 DISALLOW_COPY_AND_ASSIGN(HIf);
2751};
2752
David Brazdilfc6a86a2015-06-26 10:33:45 +00002753
2754// Abstract instruction which marks the beginning and/or end of a try block and
2755// links it to the respective exception handlers. Behaves the same as a Goto in
2756// non-exceptional control flow.
2757// Normal-flow successor is stored at index zero, exception handlers under
2758// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002759class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002760 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002761 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002762 kEntry,
2763 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002764 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002765 };
2766
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002767 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002768 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2769 SetPackedField<BoundaryKindField>(kind);
2770 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002771
2772 bool IsControlFlow() const OVERRIDE { return true; }
2773
2774 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002775 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002776
David Brazdild26a4112015-11-10 11:07:31 +00002777 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2778 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2779 }
2780
David Brazdilfc6a86a2015-06-26 10:33:45 +00002781 // Returns whether `handler` is among its exception handlers (non-zero index
2782 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002783 bool HasExceptionHandler(const HBasicBlock& handler) const {
2784 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002785 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002786 }
2787
2788 // If not present already, adds `handler` to its block's list of exception
2789 // handlers.
2790 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002791 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002792 GetBlock()->AddSuccessor(handler);
2793 }
2794 }
2795
Vladimir Markoa1de9182016-02-25 11:37:38 +00002796 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2797 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002798
David Brazdilffee3d32015-07-06 11:48:53 +01002799 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2800
David Brazdilfc6a86a2015-06-26 10:33:45 +00002801 DECLARE_INSTRUCTION(TryBoundary);
2802
2803 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002804 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2805 static constexpr size_t kFieldBoundaryKindSize =
2806 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2807 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2808 kFieldBoundaryKind + kFieldBoundaryKindSize;
2809 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2810 "Too many packed fields.");
2811 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002812
2813 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2814};
2815
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002816// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002817class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002818 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002819 // We set CanTriggerGC to prevent any intermediate address to be live
2820 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002821 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002822 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002823 SetRawInputAt(0, cond);
2824 }
2825
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002826 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002827 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002828 return true;
2829 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002830 bool NeedsEnvironment() const OVERRIDE { return true; }
2831 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002832
2833 DECLARE_INSTRUCTION(Deoptimize);
2834
2835 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002836 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2837};
2838
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002839// Represents the ArtMethod that was passed as a first argument to
2840// the method. It is used by instructions that depend on it, like
2841// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002842class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002843 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002844 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2845 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002846
2847 DECLARE_INSTRUCTION(CurrentMethod);
2848
2849 private:
2850 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2851};
2852
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002853// Fetches an ArtMethod from the virtual table or the interface method table
2854// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002855class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002856 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002857 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002858 kVTable,
2859 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002860 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002861 };
2862 HClassTableGet(HInstruction* cls,
2863 Primitive::Type type,
2864 TableKind kind,
2865 size_t index,
2866 uint32_t dex_pc)
2867 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002868 index_(index) {
2869 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002870 SetRawInputAt(0, cls);
2871 }
2872
2873 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002874 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002875 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002876 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002877 }
2878
Vladimir Markoa1de9182016-02-25 11:37:38 +00002879 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002880 size_t GetIndex() const { return index_; }
2881
2882 DECLARE_INSTRUCTION(ClassTableGet);
2883
2884 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002885 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2886 static constexpr size_t kFieldTableKindSize =
2887 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2888 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2889 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2890 "Too many packed fields.");
2891 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2892
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002893 // The index of the ArtMethod in the table.
2894 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002895
2896 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2897};
2898
Mark Mendellfe57faa2015-09-18 09:26:15 -04002899// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2900// have one successor for each entry in the switch table, and the final successor
2901// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002902class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002903 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002904 HPackedSwitch(int32_t start_value,
2905 uint32_t num_entries,
2906 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002907 uint32_t dex_pc = kNoDexPc)
2908 : HTemplateInstruction(SideEffects::None(), dex_pc),
2909 start_value_(start_value),
2910 num_entries_(num_entries) {
2911 SetRawInputAt(0, input);
2912 }
2913
2914 bool IsControlFlow() const OVERRIDE { return true; }
2915
2916 int32_t GetStartValue() const { return start_value_; }
2917
Vladimir Marko211c2112015-09-24 16:52:33 +01002918 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002919
2920 HBasicBlock* GetDefaultBlock() const {
2921 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002922 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002923 }
2924 DECLARE_INSTRUCTION(PackedSwitch);
2925
2926 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002927 const int32_t start_value_;
2928 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002929
2930 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2931};
2932
Roland Levillain88cb1752014-10-20 16:36:47 +01002933class HUnaryOperation : public HExpression<1> {
2934 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002935 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2936 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002937 SetRawInputAt(0, input);
2938 }
2939
2940 HInstruction* GetInput() const { return InputAt(0); }
2941 Primitive::Type GetResultType() const { return GetType(); }
2942
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002943 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002944 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002945 return true;
2946 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002947
Roland Levillain31dd3d62016-02-16 12:21:02 +00002948 // Try to statically evaluate `this` and return a HConstant
2949 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002950 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002951 HConstant* TryStaticEvaluation() const;
2952
2953 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002954 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2955 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002956 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2957 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002958
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002959 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002960
2961 private:
2962 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2963};
2964
Dave Allison20dfc792014-06-16 20:44:29 -07002965class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002966 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002967 HBinaryOperation(Primitive::Type result_type,
2968 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002969 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002970 SideEffects side_effects = SideEffects::None(),
2971 uint32_t dex_pc = kNoDexPc)
2972 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002973 SetRawInputAt(0, left);
2974 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002975 }
2976
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002977 HInstruction* GetLeft() const { return InputAt(0); }
2978 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002979 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002980
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002981 virtual bool IsCommutative() const { return false; }
2982
2983 // Put constant on the right.
2984 // Returns whether order is changed.
2985 bool OrderInputsWithConstantOnTheRight() {
2986 HInstruction* left = InputAt(0);
2987 HInstruction* right = InputAt(1);
2988 if (left->IsConstant() && !right->IsConstant()) {
2989 ReplaceInput(right, 0);
2990 ReplaceInput(left, 1);
2991 return true;
2992 }
2993 return false;
2994 }
2995
2996 // Order inputs by instruction id, but favor constant on the right side.
2997 // This helps GVN for commutative ops.
2998 void OrderInputs() {
2999 DCHECK(IsCommutative());
3000 HInstruction* left = InputAt(0);
3001 HInstruction* right = InputAt(1);
3002 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3003 return;
3004 }
3005 if (OrderInputsWithConstantOnTheRight()) {
3006 return;
3007 }
3008 // Order according to instruction id.
3009 if (left->GetId() > right->GetId()) {
3010 ReplaceInput(right, 0);
3011 ReplaceInput(left, 1);
3012 }
3013 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003014
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003015 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003016 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003017 return true;
3018 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003019
Roland Levillain31dd3d62016-02-16 12:21:02 +00003020 // Try to statically evaluate `this` and return a HConstant
3021 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003022 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003023 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003024
3025 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003026 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3027 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003028 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3029 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003030 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003031 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3032 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003033 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3034 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003035 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3036 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003037 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003038 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3039 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003040
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003041 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003042 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003043 HConstant* GetConstantRight() const;
3044
3045 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003046 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003047 HInstruction* GetLeastConstantLeft() const;
3048
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003049 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003050
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003051 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003052 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3053};
3054
Mark Mendellc4701932015-04-10 13:18:51 -04003055// The comparison bias applies for floating point operations and indicates how NaN
3056// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003057enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003058 kNoBias, // bias is not applicable (i.e. for long operation)
3059 kGtBias, // return 1 for NaN comparisons
3060 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003061 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003062};
3063
Roland Levillain31dd3d62016-02-16 12:21:02 +00003064std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3065
Dave Allison20dfc792014-06-16 20:44:29 -07003066class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003067 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003068 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003069 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3070 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3071 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003072
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003073 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003074 // `instruction`, and disregard moves in between.
3075 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003076
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003077 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003078
3079 virtual IfCondition GetCondition() const = 0;
3080
Mark Mendellc4701932015-04-10 13:18:51 -04003081 virtual IfCondition GetOppositeCondition() const = 0;
3082
Vladimir Markoa1de9182016-02-25 11:37:38 +00003083 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003084 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3085
Vladimir Markoa1de9182016-02-25 11:37:38 +00003086 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3087 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003088
Vladimir Marko372f10e2016-05-17 16:30:10 +01003089 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003090 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003091 }
3092
Roland Levillain4fa13f62015-07-06 18:11:54 +01003093 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003094 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003095 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003096 if (if_cond == kCondNE) {
3097 return true;
3098 } else if (if_cond == kCondEQ) {
3099 return false;
3100 }
3101 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003102 }
3103
3104 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003105 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003106 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003107 if (if_cond == kCondEQ) {
3108 return true;
3109 } else if (if_cond == kCondNE) {
3110 return false;
3111 }
3112 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003113 }
3114
Roland Levillain31dd3d62016-02-16 12:21:02 +00003115 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003116 // Needed if we merge a HCompare into a HCondition.
3117 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3118 static constexpr size_t kFieldComparisonBiasSize =
3119 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3120 static constexpr size_t kNumberOfConditionPackedBits =
3121 kFieldComparisonBias + kFieldComparisonBiasSize;
3122 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3123 using ComparisonBiasField =
3124 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3125
Roland Levillain31dd3d62016-02-16 12:21:02 +00003126 template <typename T>
3127 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3128
3129 template <typename T>
3130 int32_t CompareFP(T x, T y) const {
3131 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3132 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3133 // Handle the bias.
3134 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3135 }
3136
3137 // Return an integer constant containing the result of a condition evaluated at compile time.
3138 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3139 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3140 }
3141
Dave Allison20dfc792014-06-16 20:44:29 -07003142 private:
3143 DISALLOW_COPY_AND_ASSIGN(HCondition);
3144};
3145
3146// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003147class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003148 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003149 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3150 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003151
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003152 bool IsCommutative() const OVERRIDE { return true; }
3153
Vladimir Marko9e23df52015-11-10 17:14:35 +00003154 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3155 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003156 return MakeConstantCondition(true, GetDexPc());
3157 }
3158 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3159 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3160 }
3161 // In the following Evaluate methods, a HCompare instruction has
3162 // been merged into this HEqual instruction; evaluate it as
3163 // `Compare(x, y) == 0`.
3164 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3165 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3166 GetDexPc());
3167 }
3168 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3169 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3170 }
3171 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3172 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003173 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003174
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003175 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003176
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003177 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003178 return kCondEQ;
3179 }
3180
Mark Mendellc4701932015-04-10 13:18:51 -04003181 IfCondition GetOppositeCondition() const OVERRIDE {
3182 return kCondNE;
3183 }
3184
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003185 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003186 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003187
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003188 DISALLOW_COPY_AND_ASSIGN(HEqual);
3189};
3190
Vladimir Markofcb503c2016-05-18 12:48:17 +01003191class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003192 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003193 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3194 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003195
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003196 bool IsCommutative() const OVERRIDE { return true; }
3197
Vladimir Marko9e23df52015-11-10 17:14:35 +00003198 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3199 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003200 return MakeConstantCondition(false, GetDexPc());
3201 }
3202 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3203 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3204 }
3205 // In the following Evaluate methods, a HCompare instruction has
3206 // been merged into this HNotEqual instruction; evaluate it as
3207 // `Compare(x, y) != 0`.
3208 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3209 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3210 }
3211 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3212 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3213 }
3214 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3215 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003216 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003217
Dave Allison20dfc792014-06-16 20:44:29 -07003218 DECLARE_INSTRUCTION(NotEqual);
3219
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003220 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003221 return kCondNE;
3222 }
3223
Mark Mendellc4701932015-04-10 13:18:51 -04003224 IfCondition GetOppositeCondition() const OVERRIDE {
3225 return kCondEQ;
3226 }
3227
Dave Allison20dfc792014-06-16 20:44:29 -07003228 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003229 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003230
Dave Allison20dfc792014-06-16 20:44:29 -07003231 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3232};
3233
Vladimir Markofcb503c2016-05-18 12:48:17 +01003234class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003235 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003236 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3237 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003238
Roland Levillain9867bc72015-08-05 10:21:34 +01003239 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003240 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003241 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003242 // In the following Evaluate methods, a HCompare instruction has
3243 // been merged into this HLessThan instruction; evaluate it as
3244 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003245 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003246 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3247 }
3248 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3249 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3250 }
3251 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3252 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003253 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003254
Dave Allison20dfc792014-06-16 20:44:29 -07003255 DECLARE_INSTRUCTION(LessThan);
3256
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003257 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003258 return kCondLT;
3259 }
3260
Mark Mendellc4701932015-04-10 13:18:51 -04003261 IfCondition GetOppositeCondition() const OVERRIDE {
3262 return kCondGE;
3263 }
3264
Dave Allison20dfc792014-06-16 20:44:29 -07003265 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003266 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003267
Dave Allison20dfc792014-06-16 20:44:29 -07003268 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3269};
3270
Vladimir Markofcb503c2016-05-18 12:48:17 +01003271class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003272 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003273 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3274 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003275
Roland Levillain9867bc72015-08-05 10:21:34 +01003276 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003277 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003278 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003279 // In the following Evaluate methods, a HCompare instruction has
3280 // been merged into this HLessThanOrEqual instruction; evaluate it as
3281 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003282 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003283 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3284 }
3285 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3286 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3287 }
3288 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3289 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003290 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003291
Dave Allison20dfc792014-06-16 20:44:29 -07003292 DECLARE_INSTRUCTION(LessThanOrEqual);
3293
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003294 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003295 return kCondLE;
3296 }
3297
Mark Mendellc4701932015-04-10 13:18:51 -04003298 IfCondition GetOppositeCondition() const OVERRIDE {
3299 return kCondGT;
3300 }
3301
Dave Allison20dfc792014-06-16 20:44:29 -07003302 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003303 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003304
Dave Allison20dfc792014-06-16 20:44:29 -07003305 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3306};
3307
Vladimir Markofcb503c2016-05-18 12:48:17 +01003308class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003309 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003310 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3311 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003312
Roland Levillain9867bc72015-08-05 10:21:34 +01003313 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003314 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003315 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003316 // In the following Evaluate methods, a HCompare instruction has
3317 // been merged into this HGreaterThan instruction; evaluate it as
3318 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003319 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003320 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3321 }
3322 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3323 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3324 }
3325 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3326 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003327 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003328
Dave Allison20dfc792014-06-16 20:44:29 -07003329 DECLARE_INSTRUCTION(GreaterThan);
3330
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003331 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003332 return kCondGT;
3333 }
3334
Mark Mendellc4701932015-04-10 13:18:51 -04003335 IfCondition GetOppositeCondition() const OVERRIDE {
3336 return kCondLE;
3337 }
3338
Dave Allison20dfc792014-06-16 20:44:29 -07003339 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003340 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003341
Dave Allison20dfc792014-06-16 20:44:29 -07003342 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3343};
3344
Vladimir Markofcb503c2016-05-18 12:48:17 +01003345class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003346 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003347 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3348 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003349
Roland Levillain9867bc72015-08-05 10:21:34 +01003350 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003351 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003352 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003353 // In the following Evaluate methods, a HCompare instruction has
3354 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3355 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003356 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003357 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3358 }
3359 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3360 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3361 }
3362 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3363 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003364 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003365
Dave Allison20dfc792014-06-16 20:44:29 -07003366 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3367
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003368 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003369 return kCondGE;
3370 }
3371
Mark Mendellc4701932015-04-10 13:18:51 -04003372 IfCondition GetOppositeCondition() const OVERRIDE {
3373 return kCondLT;
3374 }
3375
Dave Allison20dfc792014-06-16 20:44:29 -07003376 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003377 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003378
Dave Allison20dfc792014-06-16 20:44:29 -07003379 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3380};
3381
Vladimir Markofcb503c2016-05-18 12:48:17 +01003382class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003383 public:
3384 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3385 : HCondition(first, second, dex_pc) {}
3386
3387 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003388 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003389 }
3390 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003391 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3392 }
3393 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3394 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3395 LOG(FATAL) << DebugName() << " is not defined for float values";
3396 UNREACHABLE();
3397 }
3398 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3399 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3400 LOG(FATAL) << DebugName() << " is not defined for double values";
3401 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003402 }
3403
3404 DECLARE_INSTRUCTION(Below);
3405
3406 IfCondition GetCondition() const OVERRIDE {
3407 return kCondB;
3408 }
3409
3410 IfCondition GetOppositeCondition() const OVERRIDE {
3411 return kCondAE;
3412 }
3413
3414 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003415 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003416 return MakeUnsigned(x) < MakeUnsigned(y);
3417 }
Aart Bike9f37602015-10-09 11:15:55 -07003418
3419 DISALLOW_COPY_AND_ASSIGN(HBelow);
3420};
3421
Vladimir Markofcb503c2016-05-18 12:48:17 +01003422class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003423 public:
3424 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3425 : HCondition(first, second, dex_pc) {}
3426
3427 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003428 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003429 }
3430 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003431 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3432 }
3433 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3434 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3435 LOG(FATAL) << DebugName() << " is not defined for float values";
3436 UNREACHABLE();
3437 }
3438 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3439 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3440 LOG(FATAL) << DebugName() << " is not defined for double values";
3441 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003442 }
3443
3444 DECLARE_INSTRUCTION(BelowOrEqual);
3445
3446 IfCondition GetCondition() const OVERRIDE {
3447 return kCondBE;
3448 }
3449
3450 IfCondition GetOppositeCondition() const OVERRIDE {
3451 return kCondA;
3452 }
3453
3454 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003455 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003456 return MakeUnsigned(x) <= MakeUnsigned(y);
3457 }
Aart Bike9f37602015-10-09 11:15:55 -07003458
3459 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3460};
3461
Vladimir Markofcb503c2016-05-18 12:48:17 +01003462class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003463 public:
3464 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3465 : HCondition(first, second, dex_pc) {}
3466
3467 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003468 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003469 }
3470 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003471 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3472 }
3473 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3474 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3475 LOG(FATAL) << DebugName() << " is not defined for float values";
3476 UNREACHABLE();
3477 }
3478 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3479 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3480 LOG(FATAL) << DebugName() << " is not defined for double values";
3481 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003482 }
3483
3484 DECLARE_INSTRUCTION(Above);
3485
3486 IfCondition GetCondition() const OVERRIDE {
3487 return kCondA;
3488 }
3489
3490 IfCondition GetOppositeCondition() const OVERRIDE {
3491 return kCondBE;
3492 }
3493
3494 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003495 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003496 return MakeUnsigned(x) > MakeUnsigned(y);
3497 }
Aart Bike9f37602015-10-09 11:15:55 -07003498
3499 DISALLOW_COPY_AND_ASSIGN(HAbove);
3500};
3501
Vladimir Markofcb503c2016-05-18 12:48:17 +01003502class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003503 public:
3504 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3505 : HCondition(first, second, dex_pc) {}
3506
3507 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003508 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003509 }
3510 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003511 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3512 }
3513 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3514 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3515 LOG(FATAL) << DebugName() << " is not defined for float values";
3516 UNREACHABLE();
3517 }
3518 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3519 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3520 LOG(FATAL) << DebugName() << " is not defined for double values";
3521 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003522 }
3523
3524 DECLARE_INSTRUCTION(AboveOrEqual);
3525
3526 IfCondition GetCondition() const OVERRIDE {
3527 return kCondAE;
3528 }
3529
3530 IfCondition GetOppositeCondition() const OVERRIDE {
3531 return kCondB;
3532 }
3533
3534 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003535 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003536 return MakeUnsigned(x) >= MakeUnsigned(y);
3537 }
Aart Bike9f37602015-10-09 11:15:55 -07003538
3539 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3540};
Dave Allison20dfc792014-06-16 20:44:29 -07003541
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003542// Instruction to check how two inputs compare to each other.
3543// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003544class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003545 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003546 // Note that `comparison_type` is the type of comparison performed
3547 // between the comparison's inputs, not the type of the instantiated
3548 // HCompare instruction (which is always Primitive::kPrimInt).
3549 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003550 HInstruction* first,
3551 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003552 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003553 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003554 : HBinaryOperation(Primitive::kPrimInt,
3555 first,
3556 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003557 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003558 dex_pc) {
3559 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003560 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3561 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003562 }
3563
Roland Levillain9867bc72015-08-05 10:21:34 +01003564 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003565 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3566
3567 template <typename T>
3568 int32_t ComputeFP(T x, T y) const {
3569 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3570 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3571 // Handle the bias.
3572 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3573 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003574
Roland Levillain9867bc72015-08-05 10:21:34 +01003575 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003576 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3577 // reach this code path when processing a freshly built HIR
3578 // graph. However HCompare integer instructions can be synthesized
3579 // by the instruction simplifier to implement IntegerCompare and
3580 // IntegerSignum intrinsics, so we have to handle this case.
3581 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003582 }
3583 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003584 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3585 }
3586 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3587 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3588 }
3589 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3590 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003591 }
3592
Vladimir Marko372f10e2016-05-17 16:30:10 +01003593 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003594 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003595 }
3596
Vladimir Markoa1de9182016-02-25 11:37:38 +00003597 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003598
Roland Levillain31dd3d62016-02-16 12:21:02 +00003599 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003600 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003601 bool IsGtBias() const {
3602 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003603 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003604 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003605
Roland Levillain1693a1f2016-03-15 14:57:31 +00003606 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3607 // Comparisons do not require a runtime call in any back end.
3608 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003609 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003610
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003611 DECLARE_INSTRUCTION(Compare);
3612
Roland Levillain31dd3d62016-02-16 12:21:02 +00003613 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003614 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3615 static constexpr size_t kFieldComparisonBiasSize =
3616 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3617 static constexpr size_t kNumberOfComparePackedBits =
3618 kFieldComparisonBias + kFieldComparisonBiasSize;
3619 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3620 using ComparisonBiasField =
3621 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3622
Roland Levillain31dd3d62016-02-16 12:21:02 +00003623 // Return an integer constant containing the result of a comparison evaluated at compile time.
3624 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3625 DCHECK(value == -1 || value == 0 || value == 1) << value;
3626 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3627 }
3628
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003629 private:
3630 DISALLOW_COPY_AND_ASSIGN(HCompare);
3631};
3632
Vladimir Markofcb503c2016-05-18 12:48:17 +01003633class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003634 public:
3635 HNewInstance(HInstruction* cls,
3636 HCurrentMethod* current_method,
3637 uint32_t dex_pc,
3638 uint16_t type_index,
3639 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003640 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003641 bool finalizable,
3642 QuickEntrypointEnum entrypoint)
3643 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3644 type_index_(type_index),
3645 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003646 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003647 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003648 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003649 SetRawInputAt(0, cls);
3650 SetRawInputAt(1, current_method);
3651 }
3652
3653 uint16_t GetTypeIndex() const { return type_index_; }
3654 const DexFile& GetDexFile() const { return dex_file_; }
3655
3656 // Calls runtime so needs an environment.
3657 bool NeedsEnvironment() const OVERRIDE { return true; }
3658
Mingyao Yang062157f2016-03-02 10:15:36 -08003659 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3660 bool CanThrow() const OVERRIDE { return true; }
3661
3662 // Needs to call into runtime to make sure it's instantiable/accessible.
3663 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003664
Vladimir Markoa1de9182016-02-25 11:37:38 +00003665 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003666
3667 bool CanBeNull() const OVERRIDE { return false; }
3668
3669 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3670
3671 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3672 entrypoint_ = entrypoint;
3673 }
3674
3675 bool IsStringAlloc() const;
3676
3677 DECLARE_INSTRUCTION(NewInstance);
3678
3679 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003680 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3681 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003682 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3683 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3684 "Too many packed fields.");
3685
David Brazdil6de19382016-01-08 17:37:10 +00003686 const uint16_t type_index_;
3687 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003688 QuickEntrypointEnum entrypoint_;
3689
3690 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3691};
3692
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003693enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003694#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3695 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003696#include "intrinsics_list.h"
3697 kNone,
3698 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3699#undef INTRINSICS_LIST
3700#undef OPTIMIZING_INTRINSICS
3701};
3702std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3703
Agi Csaki05f20562015-08-19 14:58:14 -07003704enum IntrinsicNeedsEnvironmentOrCache {
3705 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3706 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003707};
3708
Aart Bik5d75afe2015-12-14 11:57:01 -08003709enum IntrinsicSideEffects {
3710 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3711 kReadSideEffects, // Intrinsic may read heap memory.
3712 kWriteSideEffects, // Intrinsic may write heap memory.
3713 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3714};
3715
3716enum IntrinsicExceptions {
3717 kNoThrow, // Intrinsic does not throw any exceptions.
3718 kCanThrow // Intrinsic may throw exceptions.
3719};
3720
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003721class HInvoke : public HInstruction {
3722 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003723 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003724
Vladimir Marko372f10e2016-05-17 16:30:10 +01003725 using HInstruction::GetInputRecords; // Keep the const version visible.
3726 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3727 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3728 }
3729
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003730 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003731 SetRawInputAt(index, argument);
3732 }
3733
Roland Levillain3e3d7332015-04-28 11:00:54 +01003734 // Return the number of arguments. This number can be lower than
3735 // the number of inputs returned by InputCount(), as some invoke
3736 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3737 // inputs at the end of their list of inputs.
3738 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3739
Vladimir Markoa1de9182016-02-25 11:37:38 +00003740 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003741
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003742 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003743 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003744
Vladimir Markoa1de9182016-02-25 11:37:38 +00003745 InvokeType GetOriginalInvokeType() const {
3746 return GetPackedField<OriginalInvokeTypeField>();
3747 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003748
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003749 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003750 return intrinsic_;
3751 }
3752
Aart Bik5d75afe2015-12-14 11:57:01 -08003753 void SetIntrinsic(Intrinsics intrinsic,
3754 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3755 IntrinsicSideEffects side_effects,
3756 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003757
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003758 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003759 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003760 }
3761
Vladimir Markoa1de9182016-02-25 11:37:38 +00003762 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003763
3764 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3765
Vladimir Marko372f10e2016-05-17 16:30:10 +01003766 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003767 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3768 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003769
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003770 uint32_t* GetIntrinsicOptimizations() {
3771 return &intrinsic_optimizations_;
3772 }
3773
3774 const uint32_t* GetIntrinsicOptimizations() const {
3775 return &intrinsic_optimizations_;
3776 }
3777
3778 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3779
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003780 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003781
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003782 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003783 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3784 static constexpr size_t kFieldOriginalInvokeTypeSize =
3785 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3786 static constexpr size_t kFieldReturnType =
3787 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3788 static constexpr size_t kFieldReturnTypeSize =
3789 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3790 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3791 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3792 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3793 using OriginalInvokeTypeField =
3794 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3795 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3796
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003797 HInvoke(ArenaAllocator* arena,
3798 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003799 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003800 Primitive::Type return_type,
3801 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003802 uint32_t dex_method_index,
3803 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003804 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003805 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003806 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003807 inputs_(number_of_arguments + number_of_other_inputs,
3808 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003809 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003810 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003811 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003812 SetPackedField<ReturnTypeField>(return_type);
3813 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3814 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003815 }
3816
Vladimir Markoa1de9182016-02-25 11:37:38 +00003817 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003818
Roland Levillain3e3d7332015-04-28 11:00:54 +01003819 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003820 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003821 const uint32_t dex_method_index_;
3822 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003823
3824 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3825 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003826
3827 private:
3828 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3829};
3830
Vladimir Markofcb503c2016-05-18 12:48:17 +01003831class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003832 public:
3833 HInvokeUnresolved(ArenaAllocator* arena,
3834 uint32_t number_of_arguments,
3835 Primitive::Type return_type,
3836 uint32_t dex_pc,
3837 uint32_t dex_method_index,
3838 InvokeType invoke_type)
3839 : HInvoke(arena,
3840 number_of_arguments,
3841 0u /* number_of_other_inputs */,
3842 return_type,
3843 dex_pc,
3844 dex_method_index,
3845 invoke_type) {
3846 }
3847
3848 DECLARE_INSTRUCTION(InvokeUnresolved);
3849
3850 private:
3851 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3852};
3853
Vladimir Markofcb503c2016-05-18 12:48:17 +01003854class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003855 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003856 // Requirements of this method call regarding the class
3857 // initialization (clinit) check of its declaring class.
3858 enum class ClinitCheckRequirement {
3859 kNone, // Class already initialized.
3860 kExplicit, // Static call having explicit clinit check as last input.
3861 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003862 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003863 };
3864
Vladimir Marko58155012015-08-19 12:49:41 +00003865 // Determines how to load the target ArtMethod*.
3866 enum class MethodLoadKind {
3867 // Use a String init ArtMethod* loaded from Thread entrypoints.
3868 kStringInit,
3869
3870 // Use the method's own ArtMethod* loaded by the register allocator.
3871 kRecursive,
3872
3873 // Use ArtMethod* at a known address, embed the direct address in the code.
3874 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3875 kDirectAddress,
3876
3877 // Use ArtMethod* at an address that will be known at link time, embed the direct
3878 // address in the code. If the image is relocatable, emit .patch_oat entry.
3879 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3880 // the image relocatable or not.
3881 kDirectAddressWithFixup,
3882
Vladimir Markoa1de9182016-02-25 11:37:38 +00003883 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003884 // Used when we need to use the dex cache, for example for invoke-static that
3885 // may cause class initialization (the entry may point to a resolution method),
3886 // and we know that we can access the dex cache arrays using a PC-relative load.
3887 kDexCachePcRelative,
3888
3889 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3890 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3891 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3892 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3893 kDexCacheViaMethod,
3894 };
3895
3896 // Determines the location of the code pointer.
3897 enum class CodePtrLocation {
3898 // Recursive call, use local PC-relative call instruction.
3899 kCallSelf,
3900
3901 // Use PC-relative call instruction patched at link time.
3902 // Used for calls within an oat file, boot->boot or app->app.
3903 kCallPCRelative,
3904
3905 // Call to a known target address, embed the direct address in code.
3906 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3907 kCallDirect,
3908
3909 // Call to a target address that will be known at link time, embed the direct
3910 // address in code. If the image is relocatable, emit .patch_oat entry.
3911 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3912 // the image relocatable or not.
3913 kCallDirectWithFixup,
3914
3915 // Use code pointer from the ArtMethod*.
3916 // Used when we don't know the target code. This is also the last-resort-kind used when
3917 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3918 kCallArtMethod,
3919 };
3920
3921 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003922 MethodLoadKind method_load_kind;
3923 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003924 // The method load data holds
3925 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3926 // Note that there are multiple string init methods, each having its own offset.
3927 // - the method address for kDirectAddress
3928 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003929 uint64_t method_load_data;
3930 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003931 };
3932
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003933 HInvokeStaticOrDirect(ArenaAllocator* arena,
3934 uint32_t number_of_arguments,
3935 Primitive::Type return_type,
3936 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003937 uint32_t method_index,
3938 MethodReference target_method,
3939 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003940 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003941 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003942 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003943 : HInvoke(arena,
3944 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003945 // There is potentially one extra argument for the HCurrentMethod node, and
3946 // potentially one other if the clinit check is explicit, and potentially
3947 // one other if the method is a string factory.
3948 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003949 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003950 return_type,
3951 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003952 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003953 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003954 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003955 dispatch_info_(dispatch_info) {
3956 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3957 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3958 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003959
Vladimir Markodc151b22015-10-15 18:02:30 +01003960 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003961 bool had_current_method_input = HasCurrentMethodInput();
3962 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3963
3964 // Using the current method is the default and once we find a better
3965 // method load kind, we should not go back to using the current method.
3966 DCHECK(had_current_method_input || !needs_current_method_input);
3967
3968 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003969 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3970 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003971 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003972 dispatch_info_ = dispatch_info;
3973 }
3974
Vladimir Markoc53c0792015-11-19 15:48:33 +00003975 void AddSpecialInput(HInstruction* input) {
3976 // We allow only one special input.
3977 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3978 DCHECK(InputCount() == GetSpecialInputIndex() ||
3979 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3980 InsertInputAt(GetSpecialInputIndex(), input);
3981 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003982
Vladimir Marko372f10e2016-05-17 16:30:10 +01003983 using HInstruction::GetInputRecords; // Keep the const version visible.
3984 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3985 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
3986 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
3987 DCHECK(!input_records.empty());
3988 DCHECK_GT(input_records.size(), GetNumberOfArguments());
3989 HInstruction* last_input = input_records.back().GetInstruction();
3990 // Note: `last_input` may be null during arguments setup.
3991 if (last_input != nullptr) {
3992 // `last_input` is the last input of a static invoke marked as having
3993 // an explicit clinit check. It must either be:
3994 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3995 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3996 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
3997 }
3998 }
3999 return input_records;
4000 }
4001
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004002 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004003 // We access the method via the dex cache so we can't do an implicit null check.
4004 // TODO: for intrinsics we can generate implicit null checks.
4005 return false;
4006 }
4007
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004008 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004009 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004010 }
4011
Vladimir Markoc53c0792015-11-19 15:48:33 +00004012 // Get the index of the special input, if any.
4013 //
David Brazdil6de19382016-01-08 17:37:10 +00004014 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4015 // method pointer; otherwise there may be one platform-specific special input,
4016 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004017 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004018 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004019
Vladimir Markoa1de9182016-02-25 11:37:38 +00004020 InvokeType GetOptimizedInvokeType() const {
4021 return GetPackedField<OptimizedInvokeTypeField>();
4022 }
4023
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004024 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004025 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004026 }
4027
Vladimir Marko58155012015-08-19 12:49:41 +00004028 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4029 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4030 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004031 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004032 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004033 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004034 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004035 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4036 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004037 bool HasCurrentMethodInput() const {
4038 // This function can be called only after the invoke has been fully initialized by the builder.
4039 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004040 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004041 return true;
4042 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004043 DCHECK(InputCount() == GetSpecialInputIndex() ||
4044 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004045 return false;
4046 }
4047 }
Vladimir Marko58155012015-08-19 12:49:41 +00004048 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4049 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004050 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004051
4052 int32_t GetStringInitOffset() const {
4053 DCHECK(IsStringInit());
4054 return dispatch_info_.method_load_data;
4055 }
4056
4057 uint64_t GetMethodAddress() const {
4058 DCHECK(HasMethodAddress());
4059 return dispatch_info_.method_load_data;
4060 }
4061
4062 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004063 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004064 return dispatch_info_.method_load_data;
4065 }
4066
4067 uint64_t GetDirectCodePtr() const {
4068 DCHECK(HasDirectCodePtr());
4069 return dispatch_info_.direct_code_ptr;
4070 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004071
Vladimir Markoa1de9182016-02-25 11:37:38 +00004072 ClinitCheckRequirement GetClinitCheckRequirement() const {
4073 return GetPackedField<ClinitCheckRequirementField>();
4074 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004075
Roland Levillain4c0eb422015-04-24 16:43:49 +01004076 // Is this instruction a call to a static method?
4077 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004078 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004079 }
4080
Vladimir Markofbb184a2015-11-13 14:47:00 +00004081 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4082 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4083 // instruction; only relevant for static calls with explicit clinit check.
4084 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004085 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004086 size_t last_input_index = inputs_.size() - 1u;
4087 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004088 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004089 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004090 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004091 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004092 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004093 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004094 }
4095
4096 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004097 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004098 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004099 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004100 }
4101
4102 // Is this a call to a static method whose declaring class has an
4103 // implicit intialization check requirement?
4104 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004105 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004106 }
4107
Vladimir Markob554b5a2015-11-06 12:57:55 +00004108 // Does this method load kind need the current method as an input?
4109 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4110 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4111 }
4112
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004113 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004114
Roland Levillain4c0eb422015-04-24 16:43:49 +01004115 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004116 void InsertInputAt(size_t index, HInstruction* input);
4117 void RemoveInputAt(size_t index);
4118
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004119 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004120 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4121 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4122 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4123 static constexpr size_t kFieldClinitCheckRequirement =
4124 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4125 static constexpr size_t kFieldClinitCheckRequirementSize =
4126 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4127 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4128 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4129 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4130 "Too many packed fields.");
4131 using OptimizedInvokeTypeField =
4132 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4133 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4134 kFieldClinitCheckRequirement,
4135 kFieldClinitCheckRequirementSize>;
4136
Vladimir Marko58155012015-08-19 12:49:41 +00004137 // The target method may refer to different dex file or method index than the original
4138 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4139 // in derived class to increase visibility.
4140 MethodReference target_method_;
4141 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004142
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004143 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004144};
Vladimir Markof64242a2015-12-01 14:58:23 +00004145std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004146std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004147
Vladimir Markofcb503c2016-05-18 12:48:17 +01004148class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004149 public:
4150 HInvokeVirtual(ArenaAllocator* arena,
4151 uint32_t number_of_arguments,
4152 Primitive::Type return_type,
4153 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004154 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004155 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004156 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004157 vtable_index_(vtable_index) {}
4158
Calin Juravle641547a2015-04-21 22:08:51 +01004159 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004160 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004161 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004162 }
4163
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004164 uint32_t GetVTableIndex() const { return vtable_index_; }
4165
4166 DECLARE_INSTRUCTION(InvokeVirtual);
4167
4168 private:
4169 const uint32_t vtable_index_;
4170
4171 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4172};
4173
Vladimir Markofcb503c2016-05-18 12:48:17 +01004174class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004175 public:
4176 HInvokeInterface(ArenaAllocator* arena,
4177 uint32_t number_of_arguments,
4178 Primitive::Type return_type,
4179 uint32_t dex_pc,
4180 uint32_t dex_method_index,
4181 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004182 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004183 imt_index_(imt_index) {}
4184
Calin Juravle641547a2015-04-21 22:08:51 +01004185 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004186 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004187 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004188 }
4189
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004190 uint32_t GetImtIndex() const { return imt_index_; }
4191 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4192
4193 DECLARE_INSTRUCTION(InvokeInterface);
4194
4195 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004196 const uint32_t imt_index_;
4197
4198 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4199};
4200
Vladimir Markofcb503c2016-05-18 12:48:17 +01004201class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004202 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004203 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004204 : HUnaryOperation(result_type, input, dex_pc) {
4205 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4206 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004207
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004208 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004209
4210 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004211 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004212 }
4213 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004214 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004215 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004216 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4217 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4218 }
4219 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4220 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4221 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004222
Roland Levillain88cb1752014-10-20 16:36:47 +01004223 DECLARE_INSTRUCTION(Neg);
4224
4225 private:
4226 DISALLOW_COPY_AND_ASSIGN(HNeg);
4227};
4228
Vladimir Markofcb503c2016-05-18 12:48:17 +01004229class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004230 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004231 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004232 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004233 uint32_t dex_pc,
4234 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004235 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004236 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004237 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004238 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004239 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004240 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004241 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004242 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004243 }
4244
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004245 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004246 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004247
4248 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004249 bool NeedsEnvironment() const OVERRIDE { return true; }
4250
Mingyao Yang0c365e62015-03-31 15:09:29 -07004251 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4252 bool CanThrow() const OVERRIDE { return true; }
4253
Calin Juravle10e244f2015-01-26 18:54:32 +00004254 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004255
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004256 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4257
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004258 DECLARE_INSTRUCTION(NewArray);
4259
4260 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004261 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004262 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004263 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004264
4265 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4266};
4267
Vladimir Markofcb503c2016-05-18 12:48:17 +01004268class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004269 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004270 HAdd(Primitive::Type result_type,
4271 HInstruction* left,
4272 HInstruction* right,
4273 uint32_t dex_pc = kNoDexPc)
4274 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004275
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004276 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004277
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004278 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004279
4280 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004281 return GetBlock()->GetGraph()->GetIntConstant(
4282 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004283 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004284 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004285 return GetBlock()->GetGraph()->GetLongConstant(
4286 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004287 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004288 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4289 return GetBlock()->GetGraph()->GetFloatConstant(
4290 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4291 }
4292 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4293 return GetBlock()->GetGraph()->GetDoubleConstant(
4294 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4295 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004296
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004297 DECLARE_INSTRUCTION(Add);
4298
4299 private:
4300 DISALLOW_COPY_AND_ASSIGN(HAdd);
4301};
4302
Vladimir Markofcb503c2016-05-18 12:48:17 +01004303class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004304 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004305 HSub(Primitive::Type result_type,
4306 HInstruction* left,
4307 HInstruction* right,
4308 uint32_t dex_pc = kNoDexPc)
4309 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004310
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004311 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004312
4313 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004314 return GetBlock()->GetGraph()->GetIntConstant(
4315 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004316 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004317 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004318 return GetBlock()->GetGraph()->GetLongConstant(
4319 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004320 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004321 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4322 return GetBlock()->GetGraph()->GetFloatConstant(
4323 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4324 }
4325 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4326 return GetBlock()->GetGraph()->GetDoubleConstant(
4327 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4328 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004329
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004330 DECLARE_INSTRUCTION(Sub);
4331
4332 private:
4333 DISALLOW_COPY_AND_ASSIGN(HSub);
4334};
4335
Vladimir Markofcb503c2016-05-18 12:48:17 +01004336class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004337 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004338 HMul(Primitive::Type result_type,
4339 HInstruction* left,
4340 HInstruction* right,
4341 uint32_t dex_pc = kNoDexPc)
4342 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004343
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004344 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004345
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004346 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004347
4348 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004349 return GetBlock()->GetGraph()->GetIntConstant(
4350 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004351 }
4352 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004353 return GetBlock()->GetGraph()->GetLongConstant(
4354 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004355 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004356 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4357 return GetBlock()->GetGraph()->GetFloatConstant(
4358 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4359 }
4360 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4361 return GetBlock()->GetGraph()->GetDoubleConstant(
4362 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4363 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004364
4365 DECLARE_INSTRUCTION(Mul);
4366
4367 private:
4368 DISALLOW_COPY_AND_ASSIGN(HMul);
4369};
4370
Vladimir Markofcb503c2016-05-18 12:48:17 +01004371class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004372 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004373 HDiv(Primitive::Type result_type,
4374 HInstruction* left,
4375 HInstruction* right,
4376 uint32_t dex_pc)
4377 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004378
Roland Levillain9867bc72015-08-05 10:21:34 +01004379 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004380 T ComputeIntegral(T x, T y) const {
4381 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004382 // Our graph structure ensures we never have 0 for `y` during
4383 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004384 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004385 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004386 return (y == -1) ? -x : x / y;
4387 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004388
Roland Levillain31dd3d62016-02-16 12:21:02 +00004389 template <typename T>
4390 T ComputeFP(T x, T y) const {
4391 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4392 return x / y;
4393 }
4394
Roland Levillain9867bc72015-08-05 10:21:34 +01004395 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004396 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004397 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004398 }
4399 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004400 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004401 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4402 }
4403 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4404 return GetBlock()->GetGraph()->GetFloatConstant(
4405 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4406 }
4407 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4408 return GetBlock()->GetGraph()->GetDoubleConstant(
4409 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004410 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004411
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004412 static SideEffects SideEffectsForArchRuntimeCalls() {
4413 // The generated code can use a runtime call.
4414 return SideEffects::CanTriggerGC();
4415 }
4416
Calin Juravle7c4954d2014-10-28 16:57:40 +00004417 DECLARE_INSTRUCTION(Div);
4418
4419 private:
4420 DISALLOW_COPY_AND_ASSIGN(HDiv);
4421};
4422
Vladimir Markofcb503c2016-05-18 12:48:17 +01004423class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004424 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004425 HRem(Primitive::Type result_type,
4426 HInstruction* left,
4427 HInstruction* right,
4428 uint32_t dex_pc)
4429 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004430
Roland Levillain9867bc72015-08-05 10:21:34 +01004431 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004432 T ComputeIntegral(T x, T y) const {
4433 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004434 // Our graph structure ensures we never have 0 for `y` during
4435 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004436 DCHECK_NE(y, 0);
4437 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4438 return (y == -1) ? 0 : x % y;
4439 }
4440
Roland Levillain31dd3d62016-02-16 12:21:02 +00004441 template <typename T>
4442 T ComputeFP(T x, T y) const {
4443 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4444 return std::fmod(x, y);
4445 }
4446
Roland Levillain9867bc72015-08-05 10:21:34 +01004447 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004448 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004449 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004450 }
4451 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004452 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004453 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004454 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004455 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4456 return GetBlock()->GetGraph()->GetFloatConstant(
4457 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4458 }
4459 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4460 return GetBlock()->GetGraph()->GetDoubleConstant(
4461 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4462 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004463
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004464 static SideEffects SideEffectsForArchRuntimeCalls() {
4465 return SideEffects::CanTriggerGC();
4466 }
4467
Calin Juravlebacfec32014-11-14 15:54:36 +00004468 DECLARE_INSTRUCTION(Rem);
4469
4470 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004471 DISALLOW_COPY_AND_ASSIGN(HRem);
4472};
4473
Vladimir Markofcb503c2016-05-18 12:48:17 +01004474class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004475 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004476 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4477 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004478 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004479 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004480 SetRawInputAt(0, value);
4481 }
4482
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004483 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4484
Calin Juravled0d48522014-11-04 16:40:20 +00004485 bool CanBeMoved() const OVERRIDE { return true; }
4486
Vladimir Marko372f10e2016-05-17 16:30:10 +01004487 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004488 return true;
4489 }
4490
4491 bool NeedsEnvironment() const OVERRIDE { return true; }
4492 bool CanThrow() const OVERRIDE { return true; }
4493
Calin Juravled0d48522014-11-04 16:40:20 +00004494 DECLARE_INSTRUCTION(DivZeroCheck);
4495
4496 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004497 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4498};
4499
Vladimir Markofcb503c2016-05-18 12:48:17 +01004500class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004501 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004502 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004503 HInstruction* value,
4504 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004505 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004506 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4507 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4508 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004509 }
4510
Roland Levillain5b5b9312016-03-22 14:57:31 +00004511 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004512 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004513 return value << (distance & max_shift_distance);
4514 }
4515
4516 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004517 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004518 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004519 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004520 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004521 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004522 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004523 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004524 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4525 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4526 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4527 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004528 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004529 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4530 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004531 LOG(FATAL) << DebugName() << " is not defined for float values";
4532 UNREACHABLE();
4533 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004534 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4535 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004536 LOG(FATAL) << DebugName() << " is not defined for double values";
4537 UNREACHABLE();
4538 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004539
4540 DECLARE_INSTRUCTION(Shl);
4541
4542 private:
4543 DISALLOW_COPY_AND_ASSIGN(HShl);
4544};
4545
Vladimir Markofcb503c2016-05-18 12:48:17 +01004546class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004547 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004548 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004549 HInstruction* value,
4550 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004551 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004552 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4553 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4554 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004555 }
4556
Roland Levillain5b5b9312016-03-22 14:57:31 +00004557 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004558 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004559 return value >> (distance & max_shift_distance);
4560 }
4561
4562 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004563 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004564 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004565 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004566 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004567 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004568 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004569 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004570 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4571 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4572 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4573 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004574 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004575 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4576 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004577 LOG(FATAL) << DebugName() << " is not defined for float values";
4578 UNREACHABLE();
4579 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004580 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4581 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004582 LOG(FATAL) << DebugName() << " is not defined for double values";
4583 UNREACHABLE();
4584 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004585
4586 DECLARE_INSTRUCTION(Shr);
4587
4588 private:
4589 DISALLOW_COPY_AND_ASSIGN(HShr);
4590};
4591
Vladimir Markofcb503c2016-05-18 12:48:17 +01004592class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004593 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004594 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004595 HInstruction* value,
4596 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004597 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004598 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4599 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4600 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004601 }
4602
Roland Levillain5b5b9312016-03-22 14:57:31 +00004603 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004604 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004605 typedef typename std::make_unsigned<T>::type V;
4606 V ux = static_cast<V>(value);
4607 return static_cast<T>(ux >> (distance & max_shift_distance));
4608 }
4609
4610 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004611 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004612 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004613 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004614 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004615 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004616 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004617 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004618 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4619 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4620 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4621 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004622 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004623 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4624 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004625 LOG(FATAL) << DebugName() << " is not defined for float values";
4626 UNREACHABLE();
4627 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004628 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4629 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004630 LOG(FATAL) << DebugName() << " is not defined for double values";
4631 UNREACHABLE();
4632 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004633
4634 DECLARE_INSTRUCTION(UShr);
4635
4636 private:
4637 DISALLOW_COPY_AND_ASSIGN(HUShr);
4638};
4639
Vladimir Markofcb503c2016-05-18 12:48:17 +01004640class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004641 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004642 HAnd(Primitive::Type result_type,
4643 HInstruction* left,
4644 HInstruction* right,
4645 uint32_t dex_pc = kNoDexPc)
4646 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004647
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004648 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004649
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004650 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004651
4652 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004653 return GetBlock()->GetGraph()->GetIntConstant(
4654 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004655 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004656 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004657 return GetBlock()->GetGraph()->GetLongConstant(
4658 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004659 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004660 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4661 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4662 LOG(FATAL) << DebugName() << " is not defined for float values";
4663 UNREACHABLE();
4664 }
4665 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4666 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4667 LOG(FATAL) << DebugName() << " is not defined for double values";
4668 UNREACHABLE();
4669 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004670
4671 DECLARE_INSTRUCTION(And);
4672
4673 private:
4674 DISALLOW_COPY_AND_ASSIGN(HAnd);
4675};
4676
Vladimir Markofcb503c2016-05-18 12:48:17 +01004677class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004678 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004679 HOr(Primitive::Type result_type,
4680 HInstruction* left,
4681 HInstruction* right,
4682 uint32_t dex_pc = kNoDexPc)
4683 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004684
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004685 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004686
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004687 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004688
4689 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004690 return GetBlock()->GetGraph()->GetIntConstant(
4691 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004692 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004693 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004694 return GetBlock()->GetGraph()->GetLongConstant(
4695 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004696 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004697 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4698 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4699 LOG(FATAL) << DebugName() << " is not defined for float values";
4700 UNREACHABLE();
4701 }
4702 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4703 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4704 LOG(FATAL) << DebugName() << " is not defined for double values";
4705 UNREACHABLE();
4706 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004707
4708 DECLARE_INSTRUCTION(Or);
4709
4710 private:
4711 DISALLOW_COPY_AND_ASSIGN(HOr);
4712};
4713
Vladimir Markofcb503c2016-05-18 12:48:17 +01004714class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004715 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004716 HXor(Primitive::Type result_type,
4717 HInstruction* left,
4718 HInstruction* right,
4719 uint32_t dex_pc = kNoDexPc)
4720 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004721
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004722 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004723
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004724 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004725
4726 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004727 return GetBlock()->GetGraph()->GetIntConstant(
4728 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004729 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004730 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004731 return GetBlock()->GetGraph()->GetLongConstant(
4732 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004733 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004734 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4735 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4736 LOG(FATAL) << DebugName() << " is not defined for float values";
4737 UNREACHABLE();
4738 }
4739 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4740 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4741 LOG(FATAL) << DebugName() << " is not defined for double values";
4742 UNREACHABLE();
4743 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004744
4745 DECLARE_INSTRUCTION(Xor);
4746
4747 private:
4748 DISALLOW_COPY_AND_ASSIGN(HXor);
4749};
4750
Vladimir Markofcb503c2016-05-18 12:48:17 +01004751class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004752 public:
4753 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004754 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004755 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4756 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004757 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004758
Roland Levillain5b5b9312016-03-22 14:57:31 +00004759 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004760 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004761 typedef typename std::make_unsigned<T>::type V;
4762 V ux = static_cast<V>(value);
4763 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004764 return static_cast<T>(ux);
4765 } else {
4766 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004767 return static_cast<T>(ux >> (distance & max_shift_value)) |
4768 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004769 }
4770 }
4771
Roland Levillain5b5b9312016-03-22 14:57:31 +00004772 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004773 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004774 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004775 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004776 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004777 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004778 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004779 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004780 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4781 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4782 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4783 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004784 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004785 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4786 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004787 LOG(FATAL) << DebugName() << " is not defined for float values";
4788 UNREACHABLE();
4789 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004790 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4791 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004792 LOG(FATAL) << DebugName() << " is not defined for double values";
4793 UNREACHABLE();
4794 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004795
4796 DECLARE_INSTRUCTION(Ror);
4797
4798 private:
4799 DISALLOW_COPY_AND_ASSIGN(HRor);
4800};
4801
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004802// The value of a parameter in this method. Its location depends on
4803// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004804class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004805 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004806 HParameterValue(const DexFile& dex_file,
4807 uint16_t type_index,
4808 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004809 Primitive::Type parameter_type,
4810 bool is_this = false)
4811 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004812 dex_file_(dex_file),
4813 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004814 index_(index) {
4815 SetPackedFlag<kFlagIsThis>(is_this);
4816 SetPackedFlag<kFlagCanBeNull>(!is_this);
4817 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004818
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004819 const DexFile& GetDexFile() const { return dex_file_; }
4820 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004821 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004822 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004823
Vladimir Markoa1de9182016-02-25 11:37:38 +00004824 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4825 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004826
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004827 DECLARE_INSTRUCTION(ParameterValue);
4828
4829 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004830 // Whether or not the parameter value corresponds to 'this' argument.
4831 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4832 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4833 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4834 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4835 "Too many packed fields.");
4836
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004837 const DexFile& dex_file_;
4838 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004839 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004840 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004841 const uint8_t index_;
4842
4843 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4844};
4845
Vladimir Markofcb503c2016-05-18 12:48:17 +01004846class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004847 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004848 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4849 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004850
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004851 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004852 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004853 return true;
4854 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004855
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004856 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004857
4858 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004859 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004860 }
4861 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004862 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004863 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004864 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4865 LOG(FATAL) << DebugName() << " is not defined for float values";
4866 UNREACHABLE();
4867 }
4868 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4869 LOG(FATAL) << DebugName() << " is not defined for double values";
4870 UNREACHABLE();
4871 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004872
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004873 DECLARE_INSTRUCTION(Not);
4874
4875 private:
4876 DISALLOW_COPY_AND_ASSIGN(HNot);
4877};
4878
Vladimir Markofcb503c2016-05-18 12:48:17 +01004879class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004880 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004881 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4882 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004883
4884 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004885 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004886 return true;
4887 }
4888
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004889 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004890 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004891 return !x;
4892 }
4893
Roland Levillain9867bc72015-08-05 10:21:34 +01004894 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004895 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004896 }
4897 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4898 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004899 UNREACHABLE();
4900 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004901 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4902 LOG(FATAL) << DebugName() << " is not defined for float values";
4903 UNREACHABLE();
4904 }
4905 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4906 LOG(FATAL) << DebugName() << " is not defined for double values";
4907 UNREACHABLE();
4908 }
David Brazdil66d126e2015-04-03 16:02:44 +01004909
4910 DECLARE_INSTRUCTION(BooleanNot);
4911
4912 private:
4913 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4914};
4915
Vladimir Markofcb503c2016-05-18 12:48:17 +01004916class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004917 public:
4918 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004919 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004920 : HExpression(result_type,
4921 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4922 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004923 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004924 // Invariant: We should never generate a conversion to a Boolean value.
4925 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004926 }
4927
4928 HInstruction* GetInput() const { return InputAt(0); }
4929 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4930 Primitive::Type GetResultType() const { return GetType(); }
4931
4932 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004933 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4934 return true;
4935 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004936
Mark Mendelle82549b2015-05-06 10:55:34 -04004937 // Try to statically evaluate the conversion and return a HConstant
4938 // containing the result. If the input cannot be converted, return nullptr.
4939 HConstant* TryStaticEvaluation() const;
4940
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004941 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4942 Primitive::Type result_type) {
4943 // Some architectures may not require the 'GC' side effects, but at this point
4944 // in the compilation process we do not know what architecture we will
4945 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004946 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4947 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004948 return SideEffects::CanTriggerGC();
4949 }
4950 return SideEffects::None();
4951 }
4952
Roland Levillaindff1f282014-11-05 14:15:05 +00004953 DECLARE_INSTRUCTION(TypeConversion);
4954
4955 private:
4956 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4957};
4958
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004959static constexpr uint32_t kNoRegNumber = -1;
4960
Vladimir Markofcb503c2016-05-18 12:48:17 +01004961class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004962 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004963 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4964 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004965 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004966 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004967 SetRawInputAt(0, value);
4968 }
4969
Calin Juravle10e244f2015-01-26 18:54:32 +00004970 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004971 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004972 return true;
4973 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004974
Calin Juravle10e244f2015-01-26 18:54:32 +00004975 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004976
Calin Juravle10e244f2015-01-26 18:54:32 +00004977 bool CanThrow() const OVERRIDE { return true; }
4978
4979 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004980
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004981
4982 DECLARE_INSTRUCTION(NullCheck);
4983
4984 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004985 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4986};
4987
4988class FieldInfo : public ValueObject {
4989 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004990 FieldInfo(MemberOffset field_offset,
4991 Primitive::Type field_type,
4992 bool is_volatile,
4993 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004994 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004995 const DexFile& dex_file,
4996 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004997 : field_offset_(field_offset),
4998 field_type_(field_type),
4999 is_volatile_(is_volatile),
5000 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005001 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005002 dex_file_(dex_file),
5003 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005004
5005 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005006 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005007 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005008 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005009 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005010 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005011 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005012
5013 private:
5014 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005015 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005016 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005017 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005018 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005019 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005020 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005021};
5022
Vladimir Markofcb503c2016-05-18 12:48:17 +01005023class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005024 public:
5025 HInstanceFieldGet(HInstruction* value,
5026 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005027 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005028 bool is_volatile,
5029 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005030 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005031 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005032 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005033 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005034 : HExpression(field_type,
Serban Constantinescufca16662016-07-14 09:21:59 +01005035 SideEffectsForArchRuntimeCalls(field_type, is_volatile),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005036 dex_pc),
5037 field_info_(field_offset,
5038 field_type,
5039 is_volatile,
5040 field_idx,
5041 declaring_class_def_index,
5042 dex_file,
5043 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005044 SetRawInputAt(0, value);
5045 }
5046
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005047 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005048
Vladimir Marko372f10e2016-05-17 16:30:10 +01005049 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5050 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005051 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005052 }
5053
Calin Juravle641547a2015-04-21 22:08:51 +01005054 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005055 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005056 }
5057
5058 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005059 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5060 }
5061
Calin Juravle52c48962014-12-16 17:02:57 +00005062 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005063 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005064 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005065 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005066
Serban Constantinescufca16662016-07-14 09:21:59 +01005067 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type field_type, bool is_volatile) {
5068 SideEffects side_effects = SideEffects::FieldReadOfType(field_type, is_volatile);
5069
5070 // MIPS delegates volatile kPrimLong and kPrimDouble loads to a runtime helper.
5071 if (Primitive::Is64BitType(field_type)) {
5072 side_effects.Add(SideEffects::CanTriggerGC());
5073 }
5074 return side_effects;
5075 }
5076
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005077 DECLARE_INSTRUCTION(InstanceFieldGet);
5078
5079 private:
5080 const FieldInfo field_info_;
5081
5082 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5083};
5084
Vladimir Markofcb503c2016-05-18 12:48:17 +01005085class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005086 public:
5087 HInstanceFieldSet(HInstruction* object,
5088 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005089 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005090 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005091 bool is_volatile,
5092 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005093 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005094 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005095 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005096 uint32_t dex_pc)
Serban Constantinescufca16662016-07-14 09:21:59 +01005097 : HTemplateInstruction(SideEffectsForArchRuntimeCalls(field_type, is_volatile),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005098 dex_pc),
5099 field_info_(field_offset,
5100 field_type,
5101 is_volatile,
5102 field_idx,
5103 declaring_class_def_index,
5104 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005105 dex_cache) {
5106 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005107 SetRawInputAt(0, object);
5108 SetRawInputAt(1, value);
5109 }
5110
Calin Juravle641547a2015-04-21 22:08:51 +01005111 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005112 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005113 }
5114
Calin Juravle52c48962014-12-16 17:02:57 +00005115 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005116 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005117 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005118 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005119 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005120 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5121 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005122
Serban Constantinescufca16662016-07-14 09:21:59 +01005123 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type field_type, bool is_volatile) {
5124 SideEffects side_effects = SideEffects::FieldWriteOfType(field_type, is_volatile);
5125
5126 // MIPS delegates volatile kPrimLong and kPrimDouble stores to a runtime helper.
5127 if (Primitive::Is64BitType(field_type)) {
5128 side_effects.Add(SideEffects::CanTriggerGC());
5129 }
5130 return side_effects;
5131 }
5132
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005133 DECLARE_INSTRUCTION(InstanceFieldSet);
5134
5135 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005136 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5137 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5138 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5139 "Too many packed fields.");
5140
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005141 const FieldInfo field_info_;
5142
5143 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5144};
5145
Vladimir Markofcb503c2016-05-18 12:48:17 +01005146class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005147 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005148 HArrayGet(HInstruction* array,
5149 HInstruction* index,
5150 Primitive::Type type,
5151 uint32_t dex_pc,
5152 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005153 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005154 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005155 SetRawInputAt(0, array);
5156 SetRawInputAt(1, index);
5157 }
5158
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005159 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005160 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005161 return true;
5162 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005163 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005164 // TODO: We can be smarter here.
5165 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5166 // which generates the implicit null check. There are cases when these can be removed
5167 // to produce better code. If we ever add optimizations to do so we should allow an
5168 // implicit check here (as long as the address falls in the first page).
5169 return false;
5170 }
5171
David Brazdil4833f5a2015-12-16 10:37:39 +00005172 bool IsEquivalentOf(HArrayGet* other) const {
5173 bool result = (GetDexPc() == other->GetDexPc());
5174 if (kIsDebugBuild && result) {
5175 DCHECK_EQ(GetBlock(), other->GetBlock());
5176 DCHECK_EQ(GetArray(), other->GetArray());
5177 DCHECK_EQ(GetIndex(), other->GetIndex());
5178 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005179 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005180 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005181 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5182 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005183 }
5184 }
5185 return result;
5186 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005187
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005188 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5189
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005190 HInstruction* GetArray() const { return InputAt(0); }
5191 HInstruction* GetIndex() const { return InputAt(1); }
5192
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005193 DECLARE_INSTRUCTION(ArrayGet);
5194
5195 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005196 // We treat a String as an array, creating the HArrayGet from String.charAt()
5197 // intrinsic in the instruction simplifier. We can always determine whether
5198 // a particular HArrayGet is actually a String.charAt() by looking at the type
5199 // of the input but that requires holding the mutator lock, so we prefer to use
5200 // a flag, so that code generators don't need to do the locking.
5201 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5202 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5203 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5204 "Too many packed fields.");
5205
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005206 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5207};
5208
Vladimir Markofcb503c2016-05-18 12:48:17 +01005209class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005210 public:
5211 HArraySet(HInstruction* array,
5212 HInstruction* index,
5213 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005214 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005215 uint32_t dex_pc)
5216 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005217 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5218 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5219 SetPackedFlag<kFlagValueCanBeNull>(true);
5220 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005221 SetRawInputAt(0, array);
5222 SetRawInputAt(1, index);
5223 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005224 // Make a best guess now, may be refined during SSA building.
5225 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005226 }
5227
Calin Juravle77520bc2015-01-12 18:45:46 +00005228 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005229 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005230 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005231 }
5232
Mingyao Yang81014cb2015-06-02 03:16:27 -07005233 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005234 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005235
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005236 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005237 // TODO: Same as for ArrayGet.
5238 return false;
5239 }
5240
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005241 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005242 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005243 }
5244
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005245 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005246 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005247 }
5248
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005249 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005250 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005251 }
5252
Vladimir Markoa1de9182016-02-25 11:37:38 +00005253 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5254 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5255 bool StaticTypeOfArrayIsObjectArray() const {
5256 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5257 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005258
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005259 HInstruction* GetArray() const { return InputAt(0); }
5260 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005261 HInstruction* GetValue() const { return InputAt(2); }
5262
5263 Primitive::Type GetComponentType() const {
5264 // The Dex format does not type floating point index operations. Since the
5265 // `expected_component_type_` is set during building and can therefore not
5266 // be correct, we also check what is the value type. If it is a floating
5267 // point type, we must use that type.
5268 Primitive::Type value_type = GetValue()->GetType();
5269 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5270 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005271 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005272 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005273
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005274 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005275 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005276 }
5277
Aart Bik18b36ab2016-04-13 16:41:35 -07005278 void ComputeSideEffects() {
5279 Primitive::Type type = GetComponentType();
5280 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5281 SideEffectsForArchRuntimeCalls(type)));
5282 }
5283
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005284 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5285 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5286 }
5287
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005288 DECLARE_INSTRUCTION(ArraySet);
5289
5290 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005291 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5292 static constexpr size_t kFieldExpectedComponentTypeSize =
5293 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5294 static constexpr size_t kFlagNeedsTypeCheck =
5295 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5296 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005297 // Cached information for the reference_type_info_ so that codegen
5298 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005299 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5300 static constexpr size_t kNumberOfArraySetPackedBits =
5301 kFlagStaticTypeOfArrayIsObjectArray + 1;
5302 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5303 using ExpectedComponentTypeField =
5304 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005305
5306 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5307};
5308
Vladimir Markofcb503c2016-05-18 12:48:17 +01005309class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005310 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005311 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005312 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005313 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005314 // Note that arrays do not change length, so the instruction does not
5315 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005316 SetRawInputAt(0, array);
5317 }
5318
Calin Juravle77520bc2015-01-12 18:45:46 +00005319 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005320 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005321 return true;
5322 }
Calin Juravle641547a2015-04-21 22:08:51 +01005323 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5324 return obj == InputAt(0);
5325 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005326
Vladimir Markodce016e2016-04-28 13:10:02 +01005327 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5328
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005329 DECLARE_INSTRUCTION(ArrayLength);
5330
5331 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005332 // We treat a String as an array, creating the HArrayLength from String.length()
5333 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5334 // determine whether a particular HArrayLength is actually a String.length() by
5335 // looking at the type of the input but that requires holding the mutator lock, so
5336 // we prefer to use a flag, so that code generators don't need to do the locking.
5337 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5338 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5339 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5340 "Too many packed fields.");
5341
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005342 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5343};
5344
Vladimir Markofcb503c2016-05-18 12:48:17 +01005345class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005346 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005347 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5348 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005349 HBoundsCheck(HInstruction* index,
5350 HInstruction* length,
5351 uint32_t dex_pc,
5352 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5353 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5354 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005355 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005356 SetRawInputAt(0, index);
5357 SetRawInputAt(1, length);
5358 }
5359
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005360 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005361 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005362 return true;
5363 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005364
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005365 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005366
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005367 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005368
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005369 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5370 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5371
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005372 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005373
5374 DECLARE_INSTRUCTION(BoundsCheck);
5375
5376 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005377 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5378 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5379 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5380 // so we need to create an HEnvironment which will be translated to an InlineInfo
5381 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5382 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5383 // from the invoke as we don't want to look again at the dex bytecode.
5384 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5385
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005386 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5387};
5388
Vladimir Markofcb503c2016-05-18 12:48:17 +01005389class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005390 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005391 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005392 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005393
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005394 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005395 return true;
5396 }
5397
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005398 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5399 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005400
5401 DECLARE_INSTRUCTION(SuspendCheck);
5402
5403 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005404 // Only used for code generation, in order to share the same slow path between back edges
5405 // of a same loop.
5406 SlowPathCode* slow_path_;
5407
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005408 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5409};
5410
David Srbecky0cf44932015-12-09 14:09:59 +00005411// Pseudo-instruction which provides the native debugger with mapping information.
5412// It ensures that we can generate line number and local variables at this point.
5413class HNativeDebugInfo : public HTemplateInstruction<0> {
5414 public:
5415 explicit HNativeDebugInfo(uint32_t dex_pc)
5416 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5417
5418 bool NeedsEnvironment() const OVERRIDE {
5419 return true;
5420 }
5421
5422 DECLARE_INSTRUCTION(NativeDebugInfo);
5423
5424 private:
5425 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5426};
5427
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005428/**
5429 * Instruction to load a Class object.
5430 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005431class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005432 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005433 // Determines how to load the Class.
5434 enum class LoadKind {
5435 // Use the Class* from the method's own ArtMethod*.
5436 kReferrersClass,
5437
5438 // Use boot image Class* address that will be known at link time.
5439 // Used for boot image classes referenced by boot image code in non-PIC mode.
5440 kBootImageLinkTimeAddress,
5441
5442 // Use PC-relative boot image Class* address that will be known at link time.
5443 // Used for boot image classes referenced by boot image code in PIC mode.
5444 kBootImageLinkTimePcRelative,
5445
5446 // Use a known boot image Class* address, embedded in the code by the codegen.
5447 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5448 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5449 // GetIncludePatchInformation().
5450 kBootImageAddress,
5451
5452 // Load from the resolved types array at an absolute address.
5453 // Used for classes outside the boot image referenced by JIT-compiled code.
5454 kDexCacheAddress,
5455
5456 // Load from resolved types array in the dex cache using a PC-relative load.
5457 // Used for classes outside boot image when we know that we can access
5458 // the dex cache arrays using a PC-relative load.
5459 kDexCachePcRelative,
5460
5461 // Load from resolved types array accessed through the class loaded from
5462 // the compiled method's own ArtMethod*. This is the default access type when
5463 // all other types are unavailable.
5464 kDexCacheViaMethod,
5465
5466 kLast = kDexCacheViaMethod
5467 };
5468
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005469 HLoadClass(HCurrentMethod* current_method,
5470 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005471 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005472 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005473 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005474 bool needs_access_check,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005475 bool is_in_dex_cache,
5476 bool is_in_boot_image)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005477 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5478 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005479 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005480 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005481 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005482 // Referrers class should not need access check. We never inline unverified
5483 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005484 DCHECK(!is_referrers_class || !needs_access_check);
5485
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005486 SetPackedField<LoadKindField>(
5487 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005488 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5489 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005490 SetPackedFlag<kFlagIsInBootImage>(is_in_boot_image);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005491 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005492 }
5493
5494 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5495 DCHECK(HasAddress(load_kind));
5496 load_data_.address = address;
5497 SetLoadKindInternal(load_kind);
5498 }
5499
5500 void SetLoadKindWithTypeReference(LoadKind load_kind,
5501 const DexFile& dex_file,
5502 uint32_t type_index) {
5503 DCHECK(HasTypeReference(load_kind));
5504 DCHECK(IsSameDexFile(dex_file_, dex_file));
5505 DCHECK_EQ(type_index_, type_index);
5506 SetLoadKindInternal(load_kind);
5507 }
5508
5509 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5510 const DexFile& dex_file,
5511 uint32_t element_index) {
5512 DCHECK(HasDexCacheReference(load_kind));
5513 DCHECK(IsSameDexFile(dex_file_, dex_file));
5514 load_data_.dex_cache_element_index = element_index;
5515 SetLoadKindInternal(load_kind);
5516 }
5517
5518 LoadKind GetLoadKind() const {
5519 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005520 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005521
5522 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005523
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005524 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005525
5526 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5527
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005528 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005529
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005530 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005531 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005532 }
5533
Calin Juravle0ba218d2015-05-19 18:46:01 +01005534 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005535 // The entrypoint the code generator is going to call does not do
5536 // clinit of the class.
5537 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005538 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005539 }
5540
5541 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005542 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005543 (!IsReferrersClass() && !IsInDexCache()) ||
5544 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005545 }
5546
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005547
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005548 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005549 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005550 }
5551
Calin Juravleacf735c2015-02-12 15:25:22 +00005552 ReferenceTypeInfo GetLoadedClassRTI() {
5553 return loaded_class_rti_;
5554 }
5555
5556 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5557 // Make sure we only set exact types (the loaded class should never be merged).
5558 DCHECK(rti.IsExact());
5559 loaded_class_rti_ = rti;
5560 }
5561
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005562 uint32_t GetTypeIndex() const { return type_index_; }
5563 const DexFile& GetDexFile() const { return dex_file_; }
5564
5565 uint32_t GetDexCacheElementOffset() const;
5566
5567 uint64_t GetAddress() const {
5568 DCHECK(HasAddress(GetLoadKind()));
5569 return load_data_.address;
5570 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005571
Vladimir Markoa1de9182016-02-25 11:37:38 +00005572 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005573
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005574 static SideEffects SideEffectsForArchRuntimeCalls() {
5575 return SideEffects::CanTriggerGC();
5576 }
5577
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005578 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005579 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5580 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005581 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005582 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005583
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005584 void MarkInDexCache() {
5585 SetPackedFlag<kFlagIsInDexCache>(true);
5586 DCHECK(!NeedsEnvironment());
5587 RemoveEnvironment();
5588 SetSideEffects(SideEffects::None());
5589 }
5590
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005591 void MarkInBootImage() {
5592 SetPackedFlag<kFlagIsInBootImage>(true);
5593 }
5594
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005595 void AddSpecialInput(HInstruction* special_input);
5596
5597 using HInstruction::GetInputRecords; // Keep the const version visible.
5598 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5599 return ArrayRef<HUserRecord<HInstruction*>>(
5600 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5601 }
5602
5603 Primitive::Type GetType() const OVERRIDE {
5604 return Primitive::kPrimNot;
5605 }
5606
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005607 DECLARE_INSTRUCTION(LoadClass);
5608
5609 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005610 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005611 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005612 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005613 // Whether this instruction must generate the initialization check.
5614 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005615 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005616 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5617 static constexpr size_t kFieldLoadKindSize =
5618 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5619 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005620 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005621 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5622
5623 static bool HasTypeReference(LoadKind load_kind) {
5624 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5625 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5626 load_kind == LoadKind::kDexCacheViaMethod ||
5627 load_kind == LoadKind::kReferrersClass;
5628 }
5629
5630 static bool HasAddress(LoadKind load_kind) {
5631 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5632 }
5633
5634 static bool HasDexCacheReference(LoadKind load_kind) {
5635 return load_kind == LoadKind::kDexCachePcRelative;
5636 }
5637
5638 void SetLoadKindInternal(LoadKind load_kind);
5639
5640 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5641 // For other load kinds it's empty or possibly some architecture-specific instruction
5642 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5643 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005644
5645 const uint16_t type_index_;
5646 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005647
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005648 union {
5649 uint32_t dex_cache_element_index; // Only for dex cache reference.
5650 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5651 } load_data_;
5652
Calin Juravleacf735c2015-02-12 15:25:22 +00005653 ReferenceTypeInfo loaded_class_rti_;
5654
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005655 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5656};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005657std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5658
5659// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5660inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5661 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5662 return load_data_.dex_cache_element_index;
5663}
5664
5665// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5666inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005667 // The special input is used for PC-relative loads on some architectures,
5668 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005669 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005670 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5671 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5672 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005673 DCHECK(special_input_.GetInstruction() == nullptr);
5674 special_input_ = HUserRecord<HInstruction*>(special_input);
5675 special_input->AddUseAt(this, 0);
5676}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005677
Vladimir Marko372f10e2016-05-17 16:30:10 +01005678class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005679 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005680 // Determines how to load the String.
5681 enum class LoadKind {
5682 // Use boot image String* address that will be known at link time.
5683 // Used for boot image strings referenced by boot image code in non-PIC mode.
5684 kBootImageLinkTimeAddress,
5685
5686 // Use PC-relative boot image String* address that will be known at link time.
5687 // Used for boot image strings referenced by boot image code in PIC mode.
5688 kBootImageLinkTimePcRelative,
5689
5690 // Use a known boot image String* address, embedded in the code by the codegen.
5691 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5692 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5693 // GetIncludePatchInformation().
5694 kBootImageAddress,
5695
5696 // Load from the resolved strings array at an absolute address.
5697 // Used for strings outside the boot image referenced by JIT-compiled code.
5698 kDexCacheAddress,
5699
5700 // Load from resolved strings array in the dex cache using a PC-relative load.
5701 // Used for strings outside boot image when we know that we can access
5702 // the dex cache arrays using a PC-relative load.
5703 kDexCachePcRelative,
5704
5705 // Load from resolved strings array accessed through the class loaded from
5706 // the compiled method's own ArtMethod*. This is the default access type when
5707 // all other types are unavailable.
5708 kDexCacheViaMethod,
5709
5710 kLast = kDexCacheViaMethod
5711 };
5712
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005713 HLoadString(HCurrentMethod* current_method,
5714 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005715 const DexFile& dex_file,
5716 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005717 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5718 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005719 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005720 SetPackedFlag<kFlagIsInDexCache>(false);
5721 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5722 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005723 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005724
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005725 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5726 DCHECK(HasAddress(load_kind));
5727 load_data_.address = address;
5728 SetLoadKindInternal(load_kind);
5729 }
5730
5731 void SetLoadKindWithStringReference(LoadKind load_kind,
5732 const DexFile& dex_file,
5733 uint32_t string_index) {
5734 DCHECK(HasStringReference(load_kind));
5735 load_data_.ref.dex_file = &dex_file;
5736 string_index_ = string_index;
5737 SetLoadKindInternal(load_kind);
5738 }
5739
5740 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5741 const DexFile& dex_file,
5742 uint32_t element_index) {
5743 DCHECK(HasDexCacheReference(load_kind));
5744 load_data_.ref.dex_file = &dex_file;
5745 load_data_.ref.dex_cache_element_index = element_index;
5746 SetLoadKindInternal(load_kind);
5747 }
5748
5749 LoadKind GetLoadKind() const {
5750 return GetPackedField<LoadKindField>();
5751 }
5752
5753 const DexFile& GetDexFile() const;
5754
5755 uint32_t GetStringIndex() const {
5756 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5757 return string_index_;
5758 }
5759
5760 uint32_t GetDexCacheElementOffset() const;
5761
5762 uint64_t GetAddress() const {
5763 DCHECK(HasAddress(GetLoadKind()));
5764 return load_data_.address;
5765 }
5766
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005767 bool CanBeMoved() const OVERRIDE { return true; }
5768
Vladimir Marko372f10e2016-05-17 16:30:10 +01005769 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005770
5771 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5772
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005773 // Will call the runtime if we need to load the string through
5774 // the dex cache and the string is not guaranteed to be there yet.
5775 bool NeedsEnvironment() const OVERRIDE {
5776 LoadKind load_kind = GetLoadKind();
5777 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5778 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5779 load_kind == LoadKind::kBootImageAddress) {
5780 return false;
5781 }
5782 return !IsInDexCache();
5783 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005784
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005785 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5786 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5787 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005788
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005789 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005790 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005791
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005792 static SideEffects SideEffectsForArchRuntimeCalls() {
5793 return SideEffects::CanTriggerGC();
5794 }
5795
Vladimir Markoa1de9182016-02-25 11:37:38 +00005796 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5797
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005798 void MarkInDexCache() {
5799 SetPackedFlag<kFlagIsInDexCache>(true);
5800 DCHECK(!NeedsEnvironment());
5801 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005802 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005803 }
5804
Vladimir Marko372f10e2016-05-17 16:30:10 +01005805 void AddSpecialInput(HInstruction* special_input);
5806
5807 using HInstruction::GetInputRecords; // Keep the const version visible.
5808 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5809 return ArrayRef<HUserRecord<HInstruction*>>(
5810 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005811 }
5812
Vladimir Marko372f10e2016-05-17 16:30:10 +01005813 Primitive::Type GetType() const OVERRIDE {
5814 return Primitive::kPrimNot;
5815 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005816
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005817 DECLARE_INSTRUCTION(LoadString);
5818
5819 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005820 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005821 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5822 static constexpr size_t kFieldLoadKindSize =
5823 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5824 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005825 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005826 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005827
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005828 static bool HasStringReference(LoadKind load_kind) {
5829 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5830 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5831 load_kind == LoadKind::kDexCacheViaMethod;
5832 }
5833
5834 static bool HasAddress(LoadKind load_kind) {
5835 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5836 }
5837
5838 static bool HasDexCacheReference(LoadKind load_kind) {
5839 return load_kind == LoadKind::kDexCachePcRelative;
5840 }
5841
5842 void SetLoadKindInternal(LoadKind load_kind);
5843
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005844 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5845 // For other load kinds it's empty or possibly some architecture-specific instruction
5846 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005847 HUserRecord<HInstruction*> special_input_;
5848
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005849 // String index serves also as the hash code and it's also needed for slow-paths,
5850 // so it must not be overwritten with other load data.
5851 uint32_t string_index_;
5852
5853 union {
5854 struct {
5855 const DexFile* dex_file; // For string reference and dex cache reference.
5856 uint32_t dex_cache_element_index; // Only for dex cache reference.
5857 } ref;
5858 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5859 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005860
5861 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5862};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005863std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5864
5865// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5866inline const DexFile& HLoadString::GetDexFile() const {
5867 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5868 << GetLoadKind();
5869 return *load_data_.ref.dex_file;
5870}
5871
5872// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5873inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5874 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5875 return load_data_.ref.dex_cache_element_index;
5876}
5877
5878// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5879inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005880 // The special input is used for PC-relative loads on some architectures,
5881 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005882 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005883 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5884 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5885 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005886 // HLoadString::GetInputRecords() returns an empty array at this point,
5887 // so use the GetInputRecords() from the base class to set the input record.
5888 DCHECK(special_input_.GetInstruction() == nullptr);
5889 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005890 special_input->AddUseAt(this, 0);
5891}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005892
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005893/**
5894 * Performs an initialization check on its Class object input.
5895 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005896class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005897 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005898 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005899 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005900 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005901 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5902 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005903 SetRawInputAt(0, constant);
5904 }
5905
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005906 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005907 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005908 return true;
5909 }
5910
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005911 bool NeedsEnvironment() const OVERRIDE {
5912 // May call runtime to initialize the class.
5913 return true;
5914 }
5915
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005916 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005917
5918 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5919
5920 DECLARE_INSTRUCTION(ClinitCheck);
5921
5922 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005923 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5924};
5925
Vladimir Markofcb503c2016-05-18 12:48:17 +01005926class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005927 public:
5928 HStaticFieldGet(HInstruction* cls,
5929 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005930 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005931 bool is_volatile,
5932 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005933 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005934 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005935 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005936 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005937 : HExpression(field_type,
Serban Constantinescufca16662016-07-14 09:21:59 +01005938 SideEffectsForArchRuntimeCalls(field_type, is_volatile),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005939 dex_pc),
5940 field_info_(field_offset,
5941 field_type,
5942 is_volatile,
5943 field_idx,
5944 declaring_class_def_index,
5945 dex_file,
5946 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005947 SetRawInputAt(0, cls);
5948 }
5949
Calin Juravle52c48962014-12-16 17:02:57 +00005950
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005951 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005952
Vladimir Marko372f10e2016-05-17 16:30:10 +01005953 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5954 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005955 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005956 }
5957
5958 size_t ComputeHashCode() const OVERRIDE {
5959 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5960 }
5961
Calin Juravle52c48962014-12-16 17:02:57 +00005962 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005963 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5964 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005965 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005966
Serban Constantinescufca16662016-07-14 09:21:59 +01005967 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type field_type, bool is_volatile) {
5968 SideEffects side_effects = SideEffects::FieldReadOfType(field_type, is_volatile);
5969
5970 // MIPS delegates volatile kPrimLong and kPrimDouble loads to a runtime helper.
5971 if (Primitive::Is64BitType(field_type)) {
5972 side_effects.Add(SideEffects::CanTriggerGC());
5973 }
5974 return side_effects;
5975 }
5976
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005977 DECLARE_INSTRUCTION(StaticFieldGet);
5978
5979 private:
5980 const FieldInfo field_info_;
5981
5982 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5983};
5984
Vladimir Markofcb503c2016-05-18 12:48:17 +01005985class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005986 public:
5987 HStaticFieldSet(HInstruction* cls,
5988 HInstruction* value,
5989 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005990 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005991 bool is_volatile,
5992 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005993 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005994 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005995 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005996 uint32_t dex_pc)
Serban Constantinescufca16662016-07-14 09:21:59 +01005997 : HTemplateInstruction(SideEffectsForArchRuntimeCalls(field_type, is_volatile),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005998 dex_pc),
5999 field_info_(field_offset,
6000 field_type,
6001 is_volatile,
6002 field_idx,
6003 declaring_class_def_index,
6004 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006005 dex_cache) {
6006 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006007 SetRawInputAt(0, cls);
6008 SetRawInputAt(1, value);
6009 }
6010
Calin Juravle52c48962014-12-16 17:02:57 +00006011 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006012 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6013 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006014 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006015
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006016 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006017 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6018 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006019
Serban Constantinescufca16662016-07-14 09:21:59 +01006020 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type field_type, bool is_volatile) {
6021 SideEffects side_effects = SideEffects::FieldWriteOfType(field_type, is_volatile);
6022
6023 // MIPS delegates volatile kPrimLong and kPrimDouble stores to a runtime helper.
6024 if (Primitive::Is64BitType(field_type)) {
6025 side_effects.Add(SideEffects::CanTriggerGC());
6026 }
6027 return side_effects;
6028 }
6029
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006030 DECLARE_INSTRUCTION(StaticFieldSet);
6031
6032 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006033 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6034 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6035 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6036 "Too many packed fields.");
6037
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006038 const FieldInfo field_info_;
6039
6040 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6041};
6042
Vladimir Markofcb503c2016-05-18 12:48:17 +01006043class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006044 public:
6045 HUnresolvedInstanceFieldGet(HInstruction* obj,
6046 Primitive::Type field_type,
6047 uint32_t field_index,
6048 uint32_t dex_pc)
6049 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6050 field_index_(field_index) {
6051 SetRawInputAt(0, obj);
6052 }
6053
6054 bool NeedsEnvironment() const OVERRIDE { return true; }
6055 bool CanThrow() const OVERRIDE { return true; }
6056
6057 Primitive::Type GetFieldType() const { return GetType(); }
6058 uint32_t GetFieldIndex() const { return field_index_; }
6059
6060 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6061
6062 private:
6063 const uint32_t field_index_;
6064
6065 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6066};
6067
Vladimir Markofcb503c2016-05-18 12:48:17 +01006068class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006069 public:
6070 HUnresolvedInstanceFieldSet(HInstruction* obj,
6071 HInstruction* value,
6072 Primitive::Type field_type,
6073 uint32_t field_index,
6074 uint32_t dex_pc)
6075 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006076 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006077 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006078 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006079 SetRawInputAt(0, obj);
6080 SetRawInputAt(1, value);
6081 }
6082
6083 bool NeedsEnvironment() const OVERRIDE { return true; }
6084 bool CanThrow() const OVERRIDE { return true; }
6085
Vladimir Markoa1de9182016-02-25 11:37:38 +00006086 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006087 uint32_t GetFieldIndex() const { return field_index_; }
6088
6089 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6090
6091 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006092 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6093 static constexpr size_t kFieldFieldTypeSize =
6094 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6095 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6096 kFieldFieldType + kFieldFieldTypeSize;
6097 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6098 "Too many packed fields.");
6099 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6100
Calin Juravlee460d1d2015-09-29 04:52:17 +01006101 const uint32_t field_index_;
6102
6103 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6104};
6105
Vladimir Markofcb503c2016-05-18 12:48:17 +01006106class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006107 public:
6108 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6109 uint32_t field_index,
6110 uint32_t dex_pc)
6111 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6112 field_index_(field_index) {
6113 }
6114
6115 bool NeedsEnvironment() const OVERRIDE { return true; }
6116 bool CanThrow() const OVERRIDE { return true; }
6117
6118 Primitive::Type GetFieldType() const { return GetType(); }
6119 uint32_t GetFieldIndex() const { return field_index_; }
6120
6121 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6122
6123 private:
6124 const uint32_t field_index_;
6125
6126 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6127};
6128
Vladimir Markofcb503c2016-05-18 12:48:17 +01006129class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006130 public:
6131 HUnresolvedStaticFieldSet(HInstruction* value,
6132 Primitive::Type field_type,
6133 uint32_t field_index,
6134 uint32_t dex_pc)
6135 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006136 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006137 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006138 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006139 SetRawInputAt(0, value);
6140 }
6141
6142 bool NeedsEnvironment() const OVERRIDE { return true; }
6143 bool CanThrow() const OVERRIDE { return true; }
6144
Vladimir Markoa1de9182016-02-25 11:37:38 +00006145 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006146 uint32_t GetFieldIndex() const { return field_index_; }
6147
6148 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6149
6150 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006151 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6152 static constexpr size_t kFieldFieldTypeSize =
6153 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6154 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6155 kFieldFieldType + kFieldFieldTypeSize;
6156 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6157 "Too many packed fields.");
6158 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6159
Calin Juravlee460d1d2015-09-29 04:52:17 +01006160 const uint32_t field_index_;
6161
6162 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6163};
6164
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006165// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006166class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006167 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006168 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6169 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006170
David Brazdilbbd733e2015-08-18 17:48:17 +01006171 bool CanBeNull() const OVERRIDE { return false; }
6172
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006173 DECLARE_INSTRUCTION(LoadException);
6174
6175 private:
6176 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6177};
6178
David Brazdilcb1c0552015-08-04 16:22:25 +01006179// Implicit part of move-exception which clears thread-local exception storage.
6180// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006181class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006182 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006183 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6184 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006185
6186 DECLARE_INSTRUCTION(ClearException);
6187
6188 private:
6189 DISALLOW_COPY_AND_ASSIGN(HClearException);
6190};
6191
Vladimir Markofcb503c2016-05-18 12:48:17 +01006192class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006193 public:
6194 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006195 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006196 SetRawInputAt(0, exception);
6197 }
6198
6199 bool IsControlFlow() const OVERRIDE { return true; }
6200
6201 bool NeedsEnvironment() const OVERRIDE { return true; }
6202
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006203 bool CanThrow() const OVERRIDE { return true; }
6204
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006205
6206 DECLARE_INSTRUCTION(Throw);
6207
6208 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006209 DISALLOW_COPY_AND_ASSIGN(HThrow);
6210};
6211
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006212/**
6213 * Implementation strategies for the code generator of a HInstanceOf
6214 * or `HCheckCast`.
6215 */
6216enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006217 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006218 kExactCheck, // Can do a single class compare.
6219 kClassHierarchyCheck, // Can just walk the super class chain.
6220 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6221 kInterfaceCheck, // No optimization yet when checking against an interface.
6222 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006223 kArrayCheck, // No optimization yet when checking against a generic array.
6224 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006225};
6226
Roland Levillain86503782016-02-11 19:07:30 +00006227std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6228
Vladimir Markofcb503c2016-05-18 12:48:17 +01006229class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006230 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006231 HInstanceOf(HInstruction* object,
6232 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006233 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006234 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006235 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006236 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006237 dex_pc) {
6238 SetPackedField<TypeCheckKindField>(check_kind);
6239 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006240 SetRawInputAt(0, object);
6241 SetRawInputAt(1, constant);
6242 }
6243
6244 bool CanBeMoved() const OVERRIDE { return true; }
6245
Vladimir Marko372f10e2016-05-17 16:30:10 +01006246 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006247 return true;
6248 }
6249
6250 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006251 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006252 }
6253
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006254 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006255 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6256 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6257 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6258 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006259
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006260 static bool CanCallRuntime(TypeCheckKind check_kind) {
6261 // Mips currently does runtime calls for any other checks.
6262 return check_kind != TypeCheckKind::kExactCheck;
6263 }
6264
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006265 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006266 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006267 }
6268
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006269 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006270
6271 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006272 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6273 static constexpr size_t kFieldTypeCheckKindSize =
6274 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6275 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6276 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6277 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6278 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006279
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006280 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6281};
6282
Vladimir Markofcb503c2016-05-18 12:48:17 +01006283class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006284 public:
David Brazdilf5552582015-12-27 13:36:12 +00006285 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006286 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006287 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6288 SetPackedFlag<kFlagUpperCanBeNull>(true);
6289 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006290 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006291 SetRawInputAt(0, input);
6292 }
6293
David Brazdilf5552582015-12-27 13:36:12 +00006294 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006295 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006296 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006297 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006298
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006299 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006300 DCHECK(GetUpperCanBeNull() || !can_be_null);
6301 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006302 }
6303
Vladimir Markoa1de9182016-02-25 11:37:38 +00006304 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006305
Calin Juravleb1498f62015-02-16 13:13:29 +00006306 DECLARE_INSTRUCTION(BoundType);
6307
6308 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006309 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6310 // is false then CanBeNull() cannot be true).
6311 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6312 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6313 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6314 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6315
Calin Juravleb1498f62015-02-16 13:13:29 +00006316 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006317 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6318 // It is used to bound the type in cases like:
6319 // if (x instanceof ClassX) {
6320 // // uper_bound_ will be ClassX
6321 // }
David Brazdilf5552582015-12-27 13:36:12 +00006322 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006323
6324 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6325};
6326
Vladimir Markofcb503c2016-05-18 12:48:17 +01006327class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006328 public:
6329 HCheckCast(HInstruction* object,
6330 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006331 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006332 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006333 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6334 SetPackedField<TypeCheckKindField>(check_kind);
6335 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006336 SetRawInputAt(0, object);
6337 SetRawInputAt(1, constant);
6338 }
6339
6340 bool CanBeMoved() const OVERRIDE { return true; }
6341
Vladimir Marko372f10e2016-05-17 16:30:10 +01006342 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006343 return true;
6344 }
6345
6346 bool NeedsEnvironment() const OVERRIDE {
6347 // Instruction may throw a CheckCastError.
6348 return true;
6349 }
6350
6351 bool CanThrow() const OVERRIDE { return true; }
6352
Vladimir Markoa1de9182016-02-25 11:37:38 +00006353 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6354 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6355 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6356 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006357
6358 DECLARE_INSTRUCTION(CheckCast);
6359
6360 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006361 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6362 static constexpr size_t kFieldTypeCheckKindSize =
6363 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6364 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6365 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6366 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6367 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006368
6369 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006370};
6371
Andreas Gampe26de38b2016-07-27 17:53:11 -07006372/**
6373 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6374 * @details We define the combined barrier types that are actually required
6375 * by the Java Memory Model, rather than using exactly the terminology from
6376 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6377 * primitives. Note that the JSR-133 cookbook generally does not deal with
6378 * store atomicity issues, and the recipes there are not always entirely sufficient.
6379 * The current recipe is as follows:
6380 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6381 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6382 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6383 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6384 * class has final fields.
6385 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6386 * store-to-memory instructions. Only generated together with non-temporal stores.
6387 */
6388enum MemBarrierKind {
6389 kAnyStore,
6390 kLoadAny,
6391 kStoreStore,
6392 kAnyAny,
6393 kNTStoreStore,
6394 kLastBarrierKind = kNTStoreStore
6395};
6396std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6397
Vladimir Markofcb503c2016-05-18 12:48:17 +01006398class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006399 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006400 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006401 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006402 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6403 SetPackedField<BarrierKindField>(barrier_kind);
6404 }
Calin Juravle27df7582015-04-17 19:12:31 +01006405
Vladimir Markoa1de9182016-02-25 11:37:38 +00006406 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006407
6408 DECLARE_INSTRUCTION(MemoryBarrier);
6409
6410 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006411 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6412 static constexpr size_t kFieldBarrierKindSize =
6413 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6414 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6415 kFieldBarrierKind + kFieldBarrierKindSize;
6416 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6417 "Too many packed fields.");
6418 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006419
6420 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6421};
6422
Vladimir Markofcb503c2016-05-18 12:48:17 +01006423class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006424 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006425 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006426 kEnter,
6427 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006428 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006429 };
6430
6431 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006432 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006433 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6434 dex_pc) {
6435 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006436 SetRawInputAt(0, object);
6437 }
6438
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006439 // Instruction may go into runtime, so we need an environment.
6440 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006441
6442 bool CanThrow() const OVERRIDE {
6443 // Verifier guarantees that monitor-exit cannot throw.
6444 // This is important because it allows the HGraphBuilder to remove
6445 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6446 return IsEnter();
6447 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006448
Vladimir Markoa1de9182016-02-25 11:37:38 +00006449 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6450 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006451
6452 DECLARE_INSTRUCTION(MonitorOperation);
6453
Calin Juravle52c48962014-12-16 17:02:57 +00006454 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006455 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6456 static constexpr size_t kFieldOperationKindSize =
6457 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6458 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6459 kFieldOperationKind + kFieldOperationKindSize;
6460 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6461 "Too many packed fields.");
6462 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006463
6464 private:
6465 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6466};
6467
Vladimir Markofcb503c2016-05-18 12:48:17 +01006468class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006469 public:
6470 HSelect(HInstruction* condition,
6471 HInstruction* true_value,
6472 HInstruction* false_value,
6473 uint32_t dex_pc)
6474 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6475 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6476
6477 // First input must be `true_value` or `false_value` to allow codegens to
6478 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6479 // that architectures which implement HSelect as a conditional move also
6480 // will not need to invert the condition.
6481 SetRawInputAt(0, false_value);
6482 SetRawInputAt(1, true_value);
6483 SetRawInputAt(2, condition);
6484 }
6485
6486 HInstruction* GetFalseValue() const { return InputAt(0); }
6487 HInstruction* GetTrueValue() const { return InputAt(1); }
6488 HInstruction* GetCondition() const { return InputAt(2); }
6489
6490 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006491 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6492 return true;
6493 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006494
6495 bool CanBeNull() const OVERRIDE {
6496 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6497 }
6498
6499 DECLARE_INSTRUCTION(Select);
6500
6501 private:
6502 DISALLOW_COPY_AND_ASSIGN(HSelect);
6503};
6504
Vladimir Markof9f64412015-09-02 14:05:49 +01006505class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006506 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006507 MoveOperands(Location source,
6508 Location destination,
6509 Primitive::Type type,
6510 HInstruction* instruction)
6511 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006512
6513 Location GetSource() const { return source_; }
6514 Location GetDestination() const { return destination_; }
6515
6516 void SetSource(Location value) { source_ = value; }
6517 void SetDestination(Location value) { destination_ = value; }
6518
6519 // The parallel move resolver marks moves as "in-progress" by clearing the
6520 // destination (but not the source).
6521 Location MarkPending() {
6522 DCHECK(!IsPending());
6523 Location dest = destination_;
6524 destination_ = Location::NoLocation();
6525 return dest;
6526 }
6527
6528 void ClearPending(Location dest) {
6529 DCHECK(IsPending());
6530 destination_ = dest;
6531 }
6532
6533 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006534 DCHECK(source_.IsValid() || destination_.IsInvalid());
6535 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006536 }
6537
6538 // True if this blocks a move from the given location.
6539 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006540 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006541 }
6542
6543 // A move is redundant if it's been eliminated, if its source and
6544 // destination are the same, or if its destination is unneeded.
6545 bool IsRedundant() const {
6546 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6547 }
6548
6549 // We clear both operands to indicate move that's been eliminated.
6550 void Eliminate() {
6551 source_ = destination_ = Location::NoLocation();
6552 }
6553
6554 bool IsEliminated() const {
6555 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6556 return source_.IsInvalid();
6557 }
6558
Alexey Frunze4dda3372015-06-01 18:31:49 -07006559 Primitive::Type GetType() const { return type_; }
6560
Nicolas Geoffray90218252015-04-15 11:56:51 +01006561 bool Is64BitMove() const {
6562 return Primitive::Is64BitType(type_);
6563 }
6564
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006565 HInstruction* GetInstruction() const { return instruction_; }
6566
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006567 private:
6568 Location source_;
6569 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006570 // The type this move is for.
6571 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006572 // The instruction this move is assocatied with. Null when this move is
6573 // for moving an input in the expected locations of user (including a phi user).
6574 // This is only used in debug mode, to ensure we do not connect interval siblings
6575 // in the same parallel move.
6576 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006577};
6578
Roland Levillainc9285912015-12-18 10:38:42 +00006579std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6580
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006581static constexpr size_t kDefaultNumberOfMoves = 4;
6582
Vladimir Markofcb503c2016-05-18 12:48:17 +01006583class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006584 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006585 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006586 : HTemplateInstruction(SideEffects::None(), dex_pc),
6587 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6588 moves_.reserve(kDefaultNumberOfMoves);
6589 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006590
Nicolas Geoffray90218252015-04-15 11:56:51 +01006591 void AddMove(Location source,
6592 Location destination,
6593 Primitive::Type type,
6594 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006595 DCHECK(source.IsValid());
6596 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006597 if (kIsDebugBuild) {
6598 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006599 for (const MoveOperands& move : moves_) {
6600 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006601 // Special case the situation where the move is for the spill slot
6602 // of the instruction.
6603 if ((GetPrevious() == instruction)
6604 || ((GetPrevious() == nullptr)
6605 && instruction->IsPhi()
6606 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006607 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006608 << "Doing parallel moves for the same instruction.";
6609 } else {
6610 DCHECK(false) << "Doing parallel moves for the same instruction.";
6611 }
6612 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006613 }
6614 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006615 for (const MoveOperands& move : moves_) {
6616 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006617 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006618 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006619 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006620 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006621 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006622 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006623 }
6624
Vladimir Marko225b6462015-09-28 12:17:40 +01006625 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006626 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006627 }
6628
Vladimir Marko225b6462015-09-28 12:17:40 +01006629 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006630
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006631 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006632
6633 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006634 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006635
6636 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6637};
6638
Mark Mendell0616ae02015-04-17 12:49:27 -04006639} // namespace art
6640
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006641#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6642#include "nodes_shared.h"
6643#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006644#ifdef ART_ENABLE_CODEGEN_arm
6645#include "nodes_arm.h"
6646#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006647#ifdef ART_ENABLE_CODEGEN_arm64
6648#include "nodes_arm64.h"
6649#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006650#ifdef ART_ENABLE_CODEGEN_mips
6651#include "nodes_mips.h"
6652#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006653#ifdef ART_ENABLE_CODEGEN_x86
6654#include "nodes_x86.h"
6655#endif
6656
6657namespace art {
6658
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006659class HGraphVisitor : public ValueObject {
6660 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006661 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6662 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006663
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006664 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006665 virtual void VisitBasicBlock(HBasicBlock* block);
6666
Roland Levillain633021e2014-10-01 14:12:25 +01006667 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006668 void VisitInsertionOrder();
6669
Roland Levillain633021e2014-10-01 14:12:25 +01006670 // Visit the graph following dominator tree reverse post-order.
6671 void VisitReversePostOrder();
6672
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006673 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006674
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006675 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006676#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006677 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6678
6679 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6680
6681#undef DECLARE_VISIT_INSTRUCTION
6682
6683 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006684 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006685
6686 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6687};
6688
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006689class HGraphDelegateVisitor : public HGraphVisitor {
6690 public:
6691 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6692 virtual ~HGraphDelegateVisitor() {}
6693
6694 // Visit functions that delegate to to super class.
6695#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006696 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006697
6698 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6699
6700#undef DECLARE_VISIT_INSTRUCTION
6701
6702 private:
6703 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6704};
6705
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006706class HInsertionOrderIterator : public ValueObject {
6707 public:
6708 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6709
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006710 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006711 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006712 void Advance() { ++index_; }
6713
6714 private:
6715 const HGraph& graph_;
6716 size_t index_;
6717
6718 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6719};
6720
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006721class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006722 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006723 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6724 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006725 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006726 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006727
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006728 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6729 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006730 void Advance() { ++index_; }
6731
6732 private:
6733 const HGraph& graph_;
6734 size_t index_;
6735
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006736 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006737};
6738
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006739class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006740 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006741 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006742 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006743 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006744 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006745 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006746
6747 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006748 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006749 void Advance() { --index_; }
6750
6751 private:
6752 const HGraph& graph_;
6753 size_t index_;
6754
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006755 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006756};
6757
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006758class HLinearPostOrderIterator : public ValueObject {
6759 public:
6760 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006761 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006762
6763 bool Done() const { return index_ == 0; }
6764
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006765 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006766
6767 void Advance() {
6768 --index_;
6769 DCHECK_GE(index_, 0U);
6770 }
6771
6772 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006773 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006774 size_t index_;
6775
6776 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6777};
6778
6779class HLinearOrderIterator : public ValueObject {
6780 public:
6781 explicit HLinearOrderIterator(const HGraph& graph)
6782 : order_(graph.GetLinearOrder()), index_(0) {}
6783
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006784 bool Done() const { return index_ == order_.size(); }
6785 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006786 void Advance() { ++index_; }
6787
6788 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006789 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006790 size_t index_;
6791
6792 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6793};
6794
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006795// Iterator over the blocks that art part of the loop. Includes blocks part
6796// of an inner loop. The order in which the blocks are iterated is on their
6797// block id.
6798class HBlocksInLoopIterator : public ValueObject {
6799 public:
6800 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6801 : blocks_in_loop_(info.GetBlocks()),
6802 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6803 index_(0) {
6804 if (!blocks_in_loop_.IsBitSet(index_)) {
6805 Advance();
6806 }
6807 }
6808
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006809 bool Done() const { return index_ == blocks_.size(); }
6810 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006811 void Advance() {
6812 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006813 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006814 if (blocks_in_loop_.IsBitSet(index_)) {
6815 break;
6816 }
6817 }
6818 }
6819
6820 private:
6821 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006822 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006823 size_t index_;
6824
6825 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6826};
6827
Mingyao Yang3584bce2015-05-19 16:01:59 -07006828// Iterator over the blocks that art part of the loop. Includes blocks part
6829// of an inner loop. The order in which the blocks are iterated is reverse
6830// post order.
6831class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6832 public:
6833 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6834 : blocks_in_loop_(info.GetBlocks()),
6835 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6836 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006837 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006838 Advance();
6839 }
6840 }
6841
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006842 bool Done() const { return index_ == blocks_.size(); }
6843 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006844 void Advance() {
6845 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006846 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6847 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006848 break;
6849 }
6850 }
6851 }
6852
6853 private:
6854 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006855 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006856 size_t index_;
6857
6858 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6859};
6860
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006861inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006862 if (constant->IsIntConstant()) {
6863 return constant->AsIntConstant()->GetValue();
6864 } else if (constant->IsLongConstant()) {
6865 return constant->AsLongConstant()->GetValue();
6866 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006867 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006868 return 0;
6869 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006870}
6871
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006872#define INSTRUCTION_TYPE_CHECK(type, super) \
6873 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6874 inline const H##type* HInstruction::As##type() const { \
6875 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6876 } \
6877 inline H##type* HInstruction::As##type() { \
6878 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6879 }
6880
6881 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6882#undef INSTRUCTION_TYPE_CHECK
6883
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006884// Create space in `blocks` for adding `number_of_new_blocks` entries
6885// starting at location `at`. Blocks after `at` are moved accordingly.
6886inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6887 size_t number_of_new_blocks,
6888 size_t after) {
6889 DCHECK_LT(after, blocks->size());
6890 size_t old_size = blocks->size();
6891 size_t new_size = old_size + number_of_new_blocks;
6892 blocks->resize(new_size);
6893 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6894}
6895
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006896} // namespace art
6897
6898#endif // ART_COMPILER_OPTIMIZING_NODES_H_