blob: 57ae555caa4eedbd4f94d21ef22fddf01c5568c5 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko60584552015-09-03 13:35:12 +000028#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010029#include "base/transform_array_ref.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010030#include "dex_file.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000031#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000032#include "handle.h"
33#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000034#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010035#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000036#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000037#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010038#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070039#include "primitive.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010040#include "utils/intrusive_forward_list.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:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700840 explicit 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
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003745 InvokeType GetInvokeType() const {
3746 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003747 }
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
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003780 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3781
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003782 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003783
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003784 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003785 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3786 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003787 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3788 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003789 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003790 static constexpr size_t kFieldReturnTypeSize =
3791 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3792 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3793 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3794 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003795 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003796 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3797
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003798 HInvoke(ArenaAllocator* arena,
3799 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003800 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003801 Primitive::Type return_type,
3802 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003803 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003804 ArtMethod* resolved_method,
3805 InvokeType invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003806 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003807 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003808 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003809 resolved_method_(resolved_method),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003810 inputs_(number_of_arguments + number_of_other_inputs,
3811 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003812 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003813 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003814 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003815 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003816 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003817 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003818 }
3819
Vladimir Markoa1de9182016-02-25 11:37:38 +00003820 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003821
Roland Levillain3e3d7332015-04-28 11:00:54 +01003822 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003823 ArtMethod* const resolved_method_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003824 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003825 const uint32_t dex_method_index_;
3826 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003827
3828 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3829 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003830
3831 private:
3832 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3833};
3834
Vladimir Markofcb503c2016-05-18 12:48:17 +01003835class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003836 public:
3837 HInvokeUnresolved(ArenaAllocator* arena,
3838 uint32_t number_of_arguments,
3839 Primitive::Type return_type,
3840 uint32_t dex_pc,
3841 uint32_t dex_method_index,
3842 InvokeType invoke_type)
3843 : HInvoke(arena,
3844 number_of_arguments,
3845 0u /* number_of_other_inputs */,
3846 return_type,
3847 dex_pc,
3848 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003849 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003850 invoke_type) {
3851 }
3852
3853 DECLARE_INSTRUCTION(InvokeUnresolved);
3854
3855 private:
3856 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3857};
3858
Vladimir Markofcb503c2016-05-18 12:48:17 +01003859class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003860 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003861 // Requirements of this method call regarding the class
3862 // initialization (clinit) check of its declaring class.
3863 enum class ClinitCheckRequirement {
3864 kNone, // Class already initialized.
3865 kExplicit, // Static call having explicit clinit check as last input.
3866 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003867 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003868 };
3869
Vladimir Marko58155012015-08-19 12:49:41 +00003870 // Determines how to load the target ArtMethod*.
3871 enum class MethodLoadKind {
3872 // Use a String init ArtMethod* loaded from Thread entrypoints.
3873 kStringInit,
3874
3875 // Use the method's own ArtMethod* loaded by the register allocator.
3876 kRecursive,
3877
3878 // Use ArtMethod* at a known address, embed the direct address in the code.
3879 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3880 kDirectAddress,
3881
3882 // Use ArtMethod* at an address that will be known at link time, embed the direct
3883 // address in the code. If the image is relocatable, emit .patch_oat entry.
3884 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3885 // the image relocatable or not.
3886 kDirectAddressWithFixup,
3887
Vladimir Markoa1de9182016-02-25 11:37:38 +00003888 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003889 // Used when we need to use the dex cache, for example for invoke-static that
3890 // may cause class initialization (the entry may point to a resolution method),
3891 // and we know that we can access the dex cache arrays using a PC-relative load.
3892 kDexCachePcRelative,
3893
3894 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3895 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3896 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3897 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3898 kDexCacheViaMethod,
3899 };
3900
3901 // Determines the location of the code pointer.
3902 enum class CodePtrLocation {
3903 // Recursive call, use local PC-relative call instruction.
3904 kCallSelf,
3905
3906 // Use PC-relative call instruction patched at link time.
3907 // Used for calls within an oat file, boot->boot or app->app.
3908 kCallPCRelative,
3909
3910 // Call to a known target address, embed the direct address in code.
3911 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3912 kCallDirect,
3913
3914 // Call to a target address that will be known at link time, embed the direct
3915 // address in code. If the image is relocatable, emit .patch_oat entry.
3916 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3917 // the image relocatable or not.
3918 kCallDirectWithFixup,
3919
3920 // Use code pointer from the ArtMethod*.
3921 // Used when we don't know the target code. This is also the last-resort-kind used when
3922 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3923 kCallArtMethod,
3924 };
3925
3926 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003927 MethodLoadKind method_load_kind;
3928 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003929 // The method load data holds
3930 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3931 // Note that there are multiple string init methods, each having its own offset.
3932 // - the method address for kDirectAddress
3933 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003934 uint64_t method_load_data;
3935 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003936 };
3937
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003938 HInvokeStaticOrDirect(ArenaAllocator* arena,
3939 uint32_t number_of_arguments,
3940 Primitive::Type return_type,
3941 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003942 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003943 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00003944 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003945 InvokeType invoke_type,
3946 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003947 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003948 : HInvoke(arena,
3949 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003950 // There is potentially one extra argument for the HCurrentMethod node, and
3951 // potentially one other if the clinit check is explicit, and potentially
3952 // one other if the method is a string factory.
3953 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003954 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003955 return_type,
3956 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003957 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003958 resolved_method,
3959 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003960 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003961 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003962 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3963 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003964
Vladimir Markodc151b22015-10-15 18:02:30 +01003965 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003966 bool had_current_method_input = HasCurrentMethodInput();
3967 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3968
3969 // Using the current method is the default and once we find a better
3970 // method load kind, we should not go back to using the current method.
3971 DCHECK(had_current_method_input || !needs_current_method_input);
3972
3973 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003974 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3975 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003976 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003977 dispatch_info_ = dispatch_info;
3978 }
3979
Vladimir Markoc53c0792015-11-19 15:48:33 +00003980 void AddSpecialInput(HInstruction* input) {
3981 // We allow only one special input.
3982 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3983 DCHECK(InputCount() == GetSpecialInputIndex() ||
3984 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3985 InsertInputAt(GetSpecialInputIndex(), input);
3986 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003987
Vladimir Marko372f10e2016-05-17 16:30:10 +01003988 using HInstruction::GetInputRecords; // Keep the const version visible.
3989 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3990 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
3991 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
3992 DCHECK(!input_records.empty());
3993 DCHECK_GT(input_records.size(), GetNumberOfArguments());
3994 HInstruction* last_input = input_records.back().GetInstruction();
3995 // Note: `last_input` may be null during arguments setup.
3996 if (last_input != nullptr) {
3997 // `last_input` is the last input of a static invoke marked as having
3998 // an explicit clinit check. It must either be:
3999 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4000 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4001 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4002 }
4003 }
4004 return input_records;
4005 }
4006
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004007 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004008 // We access the method via the dex cache so we can't do an implicit null check.
4009 // TODO: for intrinsics we can generate implicit null checks.
4010 return false;
4011 }
4012
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004013 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004014 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004015 }
4016
Vladimir Markoc53c0792015-11-19 15:48:33 +00004017 // Get the index of the special input, if any.
4018 //
David Brazdil6de19382016-01-08 17:37:10 +00004019 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4020 // method pointer; otherwise there may be one platform-specific special input,
4021 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004022 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004023 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004024
Vladimir Marko58155012015-08-19 12:49:41 +00004025 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4026 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4027 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004028 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004029 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004030 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004031 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004032 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4033 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004034 bool HasCurrentMethodInput() const {
4035 // This function can be called only after the invoke has been fully initialized by the builder.
4036 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004037 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004038 return true;
4039 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004040 DCHECK(InputCount() == GetSpecialInputIndex() ||
4041 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004042 return false;
4043 }
4044 }
Vladimir Marko58155012015-08-19 12:49:41 +00004045 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
Vladimir Marko58155012015-08-19 12:49:41 +00004046
4047 int32_t GetStringInitOffset() const {
4048 DCHECK(IsStringInit());
4049 return dispatch_info_.method_load_data;
4050 }
4051
4052 uint64_t GetMethodAddress() const {
4053 DCHECK(HasMethodAddress());
4054 return dispatch_info_.method_load_data;
4055 }
4056
4057 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004058 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004059 return dispatch_info_.method_load_data;
4060 }
4061
4062 uint64_t GetDirectCodePtr() const {
4063 DCHECK(HasDirectCodePtr());
4064 return dispatch_info_.direct_code_ptr;
4065 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004066
Vladimir Markoa1de9182016-02-25 11:37:38 +00004067 ClinitCheckRequirement GetClinitCheckRequirement() const {
4068 return GetPackedField<ClinitCheckRequirementField>();
4069 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004070
Roland Levillain4c0eb422015-04-24 16:43:49 +01004071 // Is this instruction a call to a static method?
4072 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004073 return GetInvokeType() == kStatic;
4074 }
4075
4076 MethodReference GetTargetMethod() const {
4077 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004078 }
4079
Vladimir Markofbb184a2015-11-13 14:47:00 +00004080 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4081 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4082 // instruction; only relevant for static calls with explicit clinit check.
4083 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004084 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004085 size_t last_input_index = inputs_.size() - 1u;
4086 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004087 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004088 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004089 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004090 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004091 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004092 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004093 }
4094
4095 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004096 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004097 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004098 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004099 }
4100
4101 // Is this a call to a static method whose declaring class has an
4102 // implicit intialization check requirement?
4103 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004104 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004105 }
4106
Vladimir Markob554b5a2015-11-06 12:57:55 +00004107 // Does this method load kind need the current method as an input?
4108 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4109 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4110 }
4111
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004112 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004113
Roland Levillain4c0eb422015-04-24 16:43:49 +01004114 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004115 void InsertInputAt(size_t index, HInstruction* input);
4116 void RemoveInputAt(size_t index);
4117
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004118 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004119 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004120 static constexpr size_t kFieldClinitCheckRequirementSize =
4121 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4122 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4123 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4124 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4125 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004126 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4127 kFieldClinitCheckRequirement,
4128 kFieldClinitCheckRequirementSize>;
4129
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004130 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004131 MethodReference target_method_;
4132 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004133
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004134 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004135};
Vladimir Markof64242a2015-12-01 14:58:23 +00004136std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004137std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004138
Vladimir Markofcb503c2016-05-18 12:48:17 +01004139class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004140 public:
4141 HInvokeVirtual(ArenaAllocator* arena,
4142 uint32_t number_of_arguments,
4143 Primitive::Type return_type,
4144 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004145 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004146 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004147 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004148 : HInvoke(arena,
4149 number_of_arguments,
4150 0u,
4151 return_type,
4152 dex_pc,
4153 dex_method_index,
4154 resolved_method,
4155 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004156 vtable_index_(vtable_index) {}
4157
Calin Juravle641547a2015-04-21 22:08:51 +01004158 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004159 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004160 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004161 }
4162
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004163 uint32_t GetVTableIndex() const { return vtable_index_; }
4164
4165 DECLARE_INSTRUCTION(InvokeVirtual);
4166
4167 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004168 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004169 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,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004181 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004182 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004183 : HInvoke(arena,
4184 number_of_arguments,
4185 0u,
4186 return_type,
4187 dex_pc,
4188 dex_method_index,
4189 resolved_method,
4190 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004191 imt_index_(imt_index) {}
4192
Calin Juravle641547a2015-04-21 22:08:51 +01004193 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004194 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004195 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004196 }
4197
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004198 uint32_t GetImtIndex() const { return imt_index_; }
4199 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4200
4201 DECLARE_INSTRUCTION(InvokeInterface);
4202
4203 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004204 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004205 const uint32_t imt_index_;
4206
4207 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4208};
4209
Vladimir Markofcb503c2016-05-18 12:48:17 +01004210class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004211 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004212 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004213 : HUnaryOperation(result_type, input, dex_pc) {
4214 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4215 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004216
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004217 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004218
4219 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004220 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004221 }
4222 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004223 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004224 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004225 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4226 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4227 }
4228 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4229 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4230 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004231
Roland Levillain88cb1752014-10-20 16:36:47 +01004232 DECLARE_INSTRUCTION(Neg);
4233
4234 private:
4235 DISALLOW_COPY_AND_ASSIGN(HNeg);
4236};
4237
Vladimir Markofcb503c2016-05-18 12:48:17 +01004238class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004239 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004240 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004241 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004242 uint32_t dex_pc,
4243 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004244 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004245 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004246 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004247 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004248 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004249 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004250 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004251 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004252 }
4253
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004254 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004255 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004256
4257 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004258 bool NeedsEnvironment() const OVERRIDE { return true; }
4259
Mingyao Yang0c365e62015-03-31 15:09:29 -07004260 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4261 bool CanThrow() const OVERRIDE { return true; }
4262
Calin Juravle10e244f2015-01-26 18:54:32 +00004263 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004264
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004265 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4266
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004267 DECLARE_INSTRUCTION(NewArray);
4268
4269 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004270 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004271 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004272 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004273
4274 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4275};
4276
Vladimir Markofcb503c2016-05-18 12:48:17 +01004277class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004278 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004279 HAdd(Primitive::Type result_type,
4280 HInstruction* left,
4281 HInstruction* right,
4282 uint32_t dex_pc = kNoDexPc)
4283 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004284
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004285 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004286
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004287 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004288
4289 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004290 return GetBlock()->GetGraph()->GetIntConstant(
4291 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004292 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004293 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004294 return GetBlock()->GetGraph()->GetLongConstant(
4295 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004296 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004297 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4298 return GetBlock()->GetGraph()->GetFloatConstant(
4299 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4300 }
4301 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4302 return GetBlock()->GetGraph()->GetDoubleConstant(
4303 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4304 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004305
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004306 DECLARE_INSTRUCTION(Add);
4307
4308 private:
4309 DISALLOW_COPY_AND_ASSIGN(HAdd);
4310};
4311
Vladimir Markofcb503c2016-05-18 12:48:17 +01004312class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004313 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004314 HSub(Primitive::Type result_type,
4315 HInstruction* left,
4316 HInstruction* right,
4317 uint32_t dex_pc = kNoDexPc)
4318 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004319
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004320 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004321
4322 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004323 return GetBlock()->GetGraph()->GetIntConstant(
4324 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004325 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004326 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004327 return GetBlock()->GetGraph()->GetLongConstant(
4328 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004329 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004330 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4331 return GetBlock()->GetGraph()->GetFloatConstant(
4332 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4333 }
4334 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4335 return GetBlock()->GetGraph()->GetDoubleConstant(
4336 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4337 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004338
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004339 DECLARE_INSTRUCTION(Sub);
4340
4341 private:
4342 DISALLOW_COPY_AND_ASSIGN(HSub);
4343};
4344
Vladimir Markofcb503c2016-05-18 12:48:17 +01004345class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004346 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004347 HMul(Primitive::Type result_type,
4348 HInstruction* left,
4349 HInstruction* right,
4350 uint32_t dex_pc = kNoDexPc)
4351 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004352
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004353 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004354
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004355 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004356
4357 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004358 return GetBlock()->GetGraph()->GetIntConstant(
4359 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004360 }
4361 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004362 return GetBlock()->GetGraph()->GetLongConstant(
4363 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004364 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004365 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4366 return GetBlock()->GetGraph()->GetFloatConstant(
4367 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4368 }
4369 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4370 return GetBlock()->GetGraph()->GetDoubleConstant(
4371 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4372 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004373
4374 DECLARE_INSTRUCTION(Mul);
4375
4376 private:
4377 DISALLOW_COPY_AND_ASSIGN(HMul);
4378};
4379
Vladimir Markofcb503c2016-05-18 12:48:17 +01004380class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004381 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004382 HDiv(Primitive::Type result_type,
4383 HInstruction* left,
4384 HInstruction* right,
4385 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004386 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004387
Roland Levillain9867bc72015-08-05 10:21:34 +01004388 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004389 T ComputeIntegral(T x, T y) const {
4390 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004391 // Our graph structure ensures we never have 0 for `y` during
4392 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004393 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004394 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004395 return (y == -1) ? -x : x / y;
4396 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004397
Roland Levillain31dd3d62016-02-16 12:21:02 +00004398 template <typename T>
4399 T ComputeFP(T x, T y) const {
4400 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4401 return x / y;
4402 }
4403
Roland Levillain9867bc72015-08-05 10:21:34 +01004404 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004405 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004406 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004407 }
4408 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004409 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004410 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4411 }
4412 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4413 return GetBlock()->GetGraph()->GetFloatConstant(
4414 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4415 }
4416 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4417 return GetBlock()->GetGraph()->GetDoubleConstant(
4418 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004419 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004420
4421 DECLARE_INSTRUCTION(Div);
4422
4423 private:
4424 DISALLOW_COPY_AND_ASSIGN(HDiv);
4425};
4426
Vladimir Markofcb503c2016-05-18 12:48:17 +01004427class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004428 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004429 HRem(Primitive::Type result_type,
4430 HInstruction* left,
4431 HInstruction* right,
4432 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004433 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004434
Roland Levillain9867bc72015-08-05 10:21:34 +01004435 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004436 T ComputeIntegral(T x, T y) const {
4437 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004438 // Our graph structure ensures we never have 0 for `y` during
4439 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004440 DCHECK_NE(y, 0);
4441 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4442 return (y == -1) ? 0 : x % y;
4443 }
4444
Roland Levillain31dd3d62016-02-16 12:21:02 +00004445 template <typename T>
4446 T ComputeFP(T x, T y) const {
4447 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4448 return std::fmod(x, y);
4449 }
4450
Roland Levillain9867bc72015-08-05 10:21:34 +01004451 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004452 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004453 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004454 }
4455 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004456 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004457 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004458 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004459 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4460 return GetBlock()->GetGraph()->GetFloatConstant(
4461 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4462 }
4463 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4464 return GetBlock()->GetGraph()->GetDoubleConstant(
4465 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4466 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004467
4468 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)
Alexandre Rames91a65162016-09-19 13:54:30 +01004920 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004921 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004922 // Invariant: We should never generate a conversion to a Boolean value.
4923 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004924 }
4925
4926 HInstruction* GetInput() const { return InputAt(0); }
4927 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4928 Primitive::Type GetResultType() const { return GetType(); }
4929
4930 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004931 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4932 return true;
4933 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004934
Mark Mendelle82549b2015-05-06 10:55:34 -04004935 // Try to statically evaluate the conversion and return a HConstant
4936 // containing the result. If the input cannot be converted, return nullptr.
4937 HConstant* TryStaticEvaluation() const;
4938
Roland Levillaindff1f282014-11-05 14:15:05 +00004939 DECLARE_INSTRUCTION(TypeConversion);
4940
4941 private:
4942 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4943};
4944
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004945static constexpr uint32_t kNoRegNumber = -1;
4946
Vladimir Markofcb503c2016-05-18 12:48:17 +01004947class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004948 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004949 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4950 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004951 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004952 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004953 SetRawInputAt(0, value);
4954 }
4955
Calin Juravle10e244f2015-01-26 18:54:32 +00004956 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004957 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004958 return true;
4959 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004960
Calin Juravle10e244f2015-01-26 18:54:32 +00004961 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004962
Calin Juravle10e244f2015-01-26 18:54:32 +00004963 bool CanThrow() const OVERRIDE { return true; }
4964
4965 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004966
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004967
4968 DECLARE_INSTRUCTION(NullCheck);
4969
4970 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004971 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4972};
4973
4974class FieldInfo : public ValueObject {
4975 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004976 FieldInfo(MemberOffset field_offset,
4977 Primitive::Type field_type,
4978 bool is_volatile,
4979 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004980 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004981 const DexFile& dex_file,
4982 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004983 : field_offset_(field_offset),
4984 field_type_(field_type),
4985 is_volatile_(is_volatile),
4986 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004987 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004988 dex_file_(dex_file),
4989 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004990
4991 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004992 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004993 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004994 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004995 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004996 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004997 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004998
4999 private:
5000 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005001 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005002 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005003 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005004 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005005 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005006 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005007};
5008
Vladimir Markofcb503c2016-05-18 12:48:17 +01005009class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005010 public:
5011 HInstanceFieldGet(HInstruction* value,
5012 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005013 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005014 bool is_volatile,
5015 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005016 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005017 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005018 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005019 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005020 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005021 field_info_(field_offset,
5022 field_type,
5023 is_volatile,
5024 field_idx,
5025 declaring_class_def_index,
5026 dex_file,
5027 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005028 SetRawInputAt(0, value);
5029 }
5030
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005031 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005032
Vladimir Marko372f10e2016-05-17 16:30:10 +01005033 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5034 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005035 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005036 }
5037
Calin Juravle641547a2015-04-21 22:08:51 +01005038 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005039 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005040 }
5041
5042 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005043 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5044 }
5045
Calin Juravle52c48962014-12-16 17:02:57 +00005046 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005047 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005048 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005049 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005050
5051 DECLARE_INSTRUCTION(InstanceFieldGet);
5052
5053 private:
5054 const FieldInfo field_info_;
5055
5056 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5057};
5058
Vladimir Markofcb503c2016-05-18 12:48:17 +01005059class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005060 public:
5061 HInstanceFieldSet(HInstruction* object,
5062 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005063 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005064 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005065 bool is_volatile,
5066 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005067 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005068 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005069 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005070 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005071 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005072 field_info_(field_offset,
5073 field_type,
5074 is_volatile,
5075 field_idx,
5076 declaring_class_def_index,
5077 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005078 dex_cache) {
5079 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005080 SetRawInputAt(0, object);
5081 SetRawInputAt(1, value);
5082 }
5083
Calin Juravle641547a2015-04-21 22:08:51 +01005084 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005085 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005086 }
5087
Calin Juravle52c48962014-12-16 17:02:57 +00005088 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005089 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005090 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005091 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005092 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005093 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5094 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005095
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005096 DECLARE_INSTRUCTION(InstanceFieldSet);
5097
5098 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005099 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5100 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5101 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5102 "Too many packed fields.");
5103
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005104 const FieldInfo field_info_;
5105
5106 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5107};
5108
Vladimir Markofcb503c2016-05-18 12:48:17 +01005109class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005111 HArrayGet(HInstruction* array,
5112 HInstruction* index,
5113 Primitive::Type type,
5114 uint32_t dex_pc,
5115 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005116 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005117 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005118 SetRawInputAt(0, array);
5119 SetRawInputAt(1, index);
5120 }
5121
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005122 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005123 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005124 return true;
5125 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005126 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005127 // TODO: We can be smarter here.
5128 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5129 // which generates the implicit null check. There are cases when these can be removed
5130 // to produce better code. If we ever add optimizations to do so we should allow an
5131 // implicit check here (as long as the address falls in the first page).
5132 return false;
5133 }
5134
David Brazdil4833f5a2015-12-16 10:37:39 +00005135 bool IsEquivalentOf(HArrayGet* other) const {
5136 bool result = (GetDexPc() == other->GetDexPc());
5137 if (kIsDebugBuild && result) {
5138 DCHECK_EQ(GetBlock(), other->GetBlock());
5139 DCHECK_EQ(GetArray(), other->GetArray());
5140 DCHECK_EQ(GetIndex(), other->GetIndex());
5141 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005142 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005143 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005144 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5145 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005146 }
5147 }
5148 return result;
5149 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005150
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005151 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5152
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005153 HInstruction* GetArray() const { return InputAt(0); }
5154 HInstruction* GetIndex() const { return InputAt(1); }
5155
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005156 DECLARE_INSTRUCTION(ArrayGet);
5157
5158 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005159 // We treat a String as an array, creating the HArrayGet from String.charAt()
5160 // intrinsic in the instruction simplifier. We can always determine whether
5161 // a particular HArrayGet is actually a String.charAt() by looking at the type
5162 // of the input but that requires holding the mutator lock, so we prefer to use
5163 // a flag, so that code generators don't need to do the locking.
5164 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5165 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5166 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5167 "Too many packed fields.");
5168
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005169 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5170};
5171
Vladimir Markofcb503c2016-05-18 12:48:17 +01005172class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005173 public:
5174 HArraySet(HInstruction* array,
5175 HInstruction* index,
5176 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005177 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005178 uint32_t dex_pc)
5179 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005180 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5181 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5182 SetPackedFlag<kFlagValueCanBeNull>(true);
5183 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005184 SetRawInputAt(0, array);
5185 SetRawInputAt(1, index);
5186 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005187 // Make a best guess now, may be refined during SSA building.
5188 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005189 }
5190
Calin Juravle77520bc2015-01-12 18:45:46 +00005191 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005192 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005193 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005194 }
5195
Mingyao Yang81014cb2015-06-02 03:16:27 -07005196 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005197 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005198
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005199 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005200 // TODO: Same as for ArrayGet.
5201 return false;
5202 }
5203
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005204 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005205 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005206 }
5207
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005208 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005209 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005210 }
5211
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005212 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005213 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005214 }
5215
Vladimir Markoa1de9182016-02-25 11:37:38 +00005216 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5217 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5218 bool StaticTypeOfArrayIsObjectArray() const {
5219 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5220 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005221
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005222 HInstruction* GetArray() const { return InputAt(0); }
5223 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005224 HInstruction* GetValue() const { return InputAt(2); }
5225
5226 Primitive::Type GetComponentType() const {
5227 // The Dex format does not type floating point index operations. Since the
5228 // `expected_component_type_` is set during building and can therefore not
5229 // be correct, we also check what is the value type. If it is a floating
5230 // point type, we must use that type.
5231 Primitive::Type value_type = GetValue()->GetType();
5232 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5233 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005234 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005235 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005236
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005237 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005238 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005239 }
5240
Aart Bik18b36ab2016-04-13 16:41:35 -07005241 void ComputeSideEffects() {
5242 Primitive::Type type = GetComponentType();
5243 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5244 SideEffectsForArchRuntimeCalls(type)));
5245 }
5246
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005247 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5248 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5249 }
5250
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005251 DECLARE_INSTRUCTION(ArraySet);
5252
5253 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005254 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5255 static constexpr size_t kFieldExpectedComponentTypeSize =
5256 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5257 static constexpr size_t kFlagNeedsTypeCheck =
5258 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5259 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005260 // Cached information for the reference_type_info_ so that codegen
5261 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005262 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5263 static constexpr size_t kNumberOfArraySetPackedBits =
5264 kFlagStaticTypeOfArrayIsObjectArray + 1;
5265 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5266 using ExpectedComponentTypeField =
5267 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005268
5269 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5270};
5271
Vladimir Markofcb503c2016-05-18 12:48:17 +01005272class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005273 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005274 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005275 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005276 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005277 // Note that arrays do not change length, so the instruction does not
5278 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005279 SetRawInputAt(0, array);
5280 }
5281
Calin Juravle77520bc2015-01-12 18:45:46 +00005282 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005283 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005284 return true;
5285 }
Calin Juravle641547a2015-04-21 22:08:51 +01005286 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5287 return obj == InputAt(0);
5288 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005289
Vladimir Markodce016e2016-04-28 13:10:02 +01005290 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5291
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005292 DECLARE_INSTRUCTION(ArrayLength);
5293
5294 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005295 // We treat a String as an array, creating the HArrayLength from String.length()
5296 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5297 // determine whether a particular HArrayLength is actually a String.length() by
5298 // looking at the type of the input but that requires holding the mutator lock, so
5299 // we prefer to use a flag, so that code generators don't need to do the locking.
5300 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5301 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5302 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5303 "Too many packed fields.");
5304
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005305 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5306};
5307
Vladimir Markofcb503c2016-05-18 12:48:17 +01005308class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005309 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005310 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5311 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005312 HBoundsCheck(HInstruction* index,
5313 HInstruction* length,
5314 uint32_t dex_pc,
5315 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5316 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5317 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005318 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005319 SetRawInputAt(0, index);
5320 SetRawInputAt(1, length);
5321 }
5322
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005323 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005324 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005325 return true;
5326 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005327
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005328 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005329
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005330 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005331
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005332 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5333 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5334
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005335 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005336
5337 DECLARE_INSTRUCTION(BoundsCheck);
5338
5339 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005340 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5341 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5342 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5343 // so we need to create an HEnvironment which will be translated to an InlineInfo
5344 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5345 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5346 // from the invoke as we don't want to look again at the dex bytecode.
5347 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5348
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005349 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5350};
5351
Vladimir Markofcb503c2016-05-18 12:48:17 +01005352class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005353 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005354 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005355 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005356
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005357 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005358 return true;
5359 }
5360
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005361 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5362 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005363
5364 DECLARE_INSTRUCTION(SuspendCheck);
5365
5366 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005367 // Only used for code generation, in order to share the same slow path between back edges
5368 // of a same loop.
5369 SlowPathCode* slow_path_;
5370
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005371 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5372};
5373
David Srbecky0cf44932015-12-09 14:09:59 +00005374// Pseudo-instruction which provides the native debugger with mapping information.
5375// It ensures that we can generate line number and local variables at this point.
5376class HNativeDebugInfo : public HTemplateInstruction<0> {
5377 public:
5378 explicit HNativeDebugInfo(uint32_t dex_pc)
5379 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5380
5381 bool NeedsEnvironment() const OVERRIDE {
5382 return true;
5383 }
5384
5385 DECLARE_INSTRUCTION(NativeDebugInfo);
5386
5387 private:
5388 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5389};
5390
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005391/**
5392 * Instruction to load a Class object.
5393 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005394class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005395 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005396 // Determines how to load the Class.
5397 enum class LoadKind {
5398 // Use the Class* from the method's own ArtMethod*.
5399 kReferrersClass,
5400
5401 // Use boot image Class* address that will be known at link time.
5402 // Used for boot image classes referenced by boot image code in non-PIC mode.
5403 kBootImageLinkTimeAddress,
5404
5405 // Use PC-relative boot image Class* address that will be known at link time.
5406 // Used for boot image classes referenced by boot image code in PIC mode.
5407 kBootImageLinkTimePcRelative,
5408
5409 // Use a known boot image Class* address, embedded in the code by the codegen.
5410 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5411 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5412 // GetIncludePatchInformation().
5413 kBootImageAddress,
5414
5415 // Load from the resolved types array at an absolute address.
5416 // Used for classes outside the boot image referenced by JIT-compiled code.
5417 kDexCacheAddress,
5418
5419 // Load from resolved types array in the dex cache using a PC-relative load.
5420 // Used for classes outside boot image when we know that we can access
5421 // the dex cache arrays using a PC-relative load.
5422 kDexCachePcRelative,
5423
5424 // Load from resolved types array accessed through the class loaded from
5425 // the compiled method's own ArtMethod*. This is the default access type when
5426 // all other types are unavailable.
5427 kDexCacheViaMethod,
5428
5429 kLast = kDexCacheViaMethod
5430 };
5431
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005432 HLoadClass(HCurrentMethod* current_method,
5433 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005434 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005435 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005436 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005437 bool needs_access_check,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005438 bool is_in_dex_cache,
5439 bool is_in_boot_image)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005440 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5441 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005442 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005443 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005444 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005445 // Referrers class should not need access check. We never inline unverified
5446 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005447 DCHECK(!is_referrers_class || !needs_access_check);
5448
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005449 SetPackedField<LoadKindField>(
5450 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005451 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5452 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005453 SetPackedFlag<kFlagIsInBootImage>(is_in_boot_image);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005454 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005455 }
5456
5457 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5458 DCHECK(HasAddress(load_kind));
5459 load_data_.address = address;
5460 SetLoadKindInternal(load_kind);
5461 }
5462
5463 void SetLoadKindWithTypeReference(LoadKind load_kind,
5464 const DexFile& dex_file,
5465 uint32_t type_index) {
5466 DCHECK(HasTypeReference(load_kind));
5467 DCHECK(IsSameDexFile(dex_file_, dex_file));
5468 DCHECK_EQ(type_index_, type_index);
5469 SetLoadKindInternal(load_kind);
5470 }
5471
5472 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5473 const DexFile& dex_file,
5474 uint32_t element_index) {
5475 DCHECK(HasDexCacheReference(load_kind));
5476 DCHECK(IsSameDexFile(dex_file_, dex_file));
5477 load_data_.dex_cache_element_index = element_index;
5478 SetLoadKindInternal(load_kind);
5479 }
5480
5481 LoadKind GetLoadKind() const {
5482 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005483 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005484
5485 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005486
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005487 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005488
5489 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5490
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005491 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005492
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005493 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005494 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005495 }
5496
Calin Juravle0ba218d2015-05-19 18:46:01 +01005497 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005498 // The entrypoint the code generator is going to call does not do
5499 // clinit of the class.
5500 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005501 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005502 }
5503
5504 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005505 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005506 (!IsReferrersClass() && !IsInDexCache()) ||
5507 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005508 }
5509
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005510
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005511 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005512 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005513 }
5514
Calin Juravleacf735c2015-02-12 15:25:22 +00005515 ReferenceTypeInfo GetLoadedClassRTI() {
5516 return loaded_class_rti_;
5517 }
5518
5519 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5520 // Make sure we only set exact types (the loaded class should never be merged).
5521 DCHECK(rti.IsExact());
5522 loaded_class_rti_ = rti;
5523 }
5524
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005525 uint32_t GetTypeIndex() const { return type_index_; }
5526 const DexFile& GetDexFile() const { return dex_file_; }
5527
5528 uint32_t GetDexCacheElementOffset() const;
5529
5530 uint64_t GetAddress() const {
5531 DCHECK(HasAddress(GetLoadKind()));
5532 return load_data_.address;
5533 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005534
Vladimir Markoa1de9182016-02-25 11:37:38 +00005535 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005536
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005537 static SideEffects SideEffectsForArchRuntimeCalls() {
5538 return SideEffects::CanTriggerGC();
5539 }
5540
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005541 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005542 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5543 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005544 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005545 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005546
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005547 void MarkInDexCache() {
5548 SetPackedFlag<kFlagIsInDexCache>(true);
5549 DCHECK(!NeedsEnvironment());
5550 RemoveEnvironment();
5551 SetSideEffects(SideEffects::None());
5552 }
5553
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005554 void MarkInBootImage() {
5555 SetPackedFlag<kFlagIsInBootImage>(true);
5556 }
5557
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005558 void AddSpecialInput(HInstruction* special_input);
5559
5560 using HInstruction::GetInputRecords; // Keep the const version visible.
5561 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5562 return ArrayRef<HUserRecord<HInstruction*>>(
5563 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5564 }
5565
5566 Primitive::Type GetType() const OVERRIDE {
5567 return Primitive::kPrimNot;
5568 }
5569
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005570 DECLARE_INSTRUCTION(LoadClass);
5571
5572 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005573 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005574 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005575 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005576 // Whether this instruction must generate the initialization check.
5577 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005578 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005579 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5580 static constexpr size_t kFieldLoadKindSize =
5581 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5582 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005583 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005584 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5585
5586 static bool HasTypeReference(LoadKind load_kind) {
5587 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5588 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5589 load_kind == LoadKind::kDexCacheViaMethod ||
5590 load_kind == LoadKind::kReferrersClass;
5591 }
5592
5593 static bool HasAddress(LoadKind load_kind) {
5594 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5595 }
5596
5597 static bool HasDexCacheReference(LoadKind load_kind) {
5598 return load_kind == LoadKind::kDexCachePcRelative;
5599 }
5600
5601 void SetLoadKindInternal(LoadKind load_kind);
5602
5603 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5604 // For other load kinds it's empty or possibly some architecture-specific instruction
5605 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5606 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005607
5608 const uint16_t type_index_;
5609 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005610
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005611 union {
5612 uint32_t dex_cache_element_index; // Only for dex cache reference.
5613 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5614 } load_data_;
5615
Calin Juravleacf735c2015-02-12 15:25:22 +00005616 ReferenceTypeInfo loaded_class_rti_;
5617
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005618 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5619};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005620std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5621
5622// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5623inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5624 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5625 return load_data_.dex_cache_element_index;
5626}
5627
5628// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5629inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005630 // The special input is used for PC-relative loads on some architectures,
5631 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005632 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005633 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5634 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5635 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005636 DCHECK(special_input_.GetInstruction() == nullptr);
5637 special_input_ = HUserRecord<HInstruction*>(special_input);
5638 special_input->AddUseAt(this, 0);
5639}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005640
Vladimir Marko372f10e2016-05-17 16:30:10 +01005641class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005642 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005643 // Determines how to load the String.
5644 enum class LoadKind {
5645 // Use boot image String* address that will be known at link time.
5646 // Used for boot image strings referenced by boot image code in non-PIC mode.
5647 kBootImageLinkTimeAddress,
5648
5649 // Use PC-relative boot image String* address that will be known at link time.
5650 // Used for boot image strings referenced by boot image code in PIC mode.
5651 kBootImageLinkTimePcRelative,
5652
5653 // Use a known boot image String* address, embedded in the code by the codegen.
5654 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5655 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5656 // GetIncludePatchInformation().
5657 kBootImageAddress,
5658
5659 // Load from the resolved strings array at an absolute address.
5660 // Used for strings outside the boot image referenced by JIT-compiled code.
5661 kDexCacheAddress,
5662
5663 // Load from resolved strings array in the dex cache using a PC-relative load.
5664 // Used for strings outside boot image when we know that we can access
5665 // the dex cache arrays using a PC-relative load.
5666 kDexCachePcRelative,
5667
5668 // Load from resolved strings array accessed through the class loaded from
5669 // the compiled method's own ArtMethod*. This is the default access type when
5670 // all other types are unavailable.
5671 kDexCacheViaMethod,
5672
5673 kLast = kDexCacheViaMethod
5674 };
5675
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005676 HLoadString(HCurrentMethod* current_method,
5677 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005678 const DexFile& dex_file,
5679 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005680 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5681 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005682 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005683 SetPackedFlag<kFlagIsInDexCache>(false);
5684 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5685 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005686 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005687
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005688 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5689 DCHECK(HasAddress(load_kind));
5690 load_data_.address = address;
5691 SetLoadKindInternal(load_kind);
5692 }
5693
5694 void SetLoadKindWithStringReference(LoadKind load_kind,
5695 const DexFile& dex_file,
5696 uint32_t string_index) {
5697 DCHECK(HasStringReference(load_kind));
5698 load_data_.ref.dex_file = &dex_file;
5699 string_index_ = string_index;
5700 SetLoadKindInternal(load_kind);
5701 }
5702
5703 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5704 const DexFile& dex_file,
5705 uint32_t element_index) {
5706 DCHECK(HasDexCacheReference(load_kind));
5707 load_data_.ref.dex_file = &dex_file;
5708 load_data_.ref.dex_cache_element_index = element_index;
5709 SetLoadKindInternal(load_kind);
5710 }
5711
5712 LoadKind GetLoadKind() const {
5713 return GetPackedField<LoadKindField>();
5714 }
5715
5716 const DexFile& GetDexFile() const;
5717
5718 uint32_t GetStringIndex() const {
5719 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5720 return string_index_;
5721 }
5722
5723 uint32_t GetDexCacheElementOffset() const;
5724
5725 uint64_t GetAddress() const {
5726 DCHECK(HasAddress(GetLoadKind()));
5727 return load_data_.address;
5728 }
5729
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005730 bool CanBeMoved() const OVERRIDE { return true; }
5731
Vladimir Marko372f10e2016-05-17 16:30:10 +01005732 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005733
5734 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5735
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005736 // Will call the runtime if we need to load the string through
5737 // the dex cache and the string is not guaranteed to be there yet.
5738 bool NeedsEnvironment() const OVERRIDE {
5739 LoadKind load_kind = GetLoadKind();
5740 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5741 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5742 load_kind == LoadKind::kBootImageAddress) {
5743 return false;
5744 }
5745 return !IsInDexCache();
5746 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005747
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005748 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5749 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5750 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005751
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005752 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005753 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005754
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005755 static SideEffects SideEffectsForArchRuntimeCalls() {
5756 return SideEffects::CanTriggerGC();
5757 }
5758
Vladimir Markoa1de9182016-02-25 11:37:38 +00005759 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5760
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005761 void MarkInDexCache() {
5762 SetPackedFlag<kFlagIsInDexCache>(true);
5763 DCHECK(!NeedsEnvironment());
5764 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005765 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005766 }
5767
Vladimir Marko372f10e2016-05-17 16:30:10 +01005768 void AddSpecialInput(HInstruction* special_input);
5769
5770 using HInstruction::GetInputRecords; // Keep the const version visible.
5771 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5772 return ArrayRef<HUserRecord<HInstruction*>>(
5773 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005774 }
5775
Vladimir Marko372f10e2016-05-17 16:30:10 +01005776 Primitive::Type GetType() const OVERRIDE {
5777 return Primitive::kPrimNot;
5778 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005779
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005780 DECLARE_INSTRUCTION(LoadString);
5781
5782 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005783 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005784 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5785 static constexpr size_t kFieldLoadKindSize =
5786 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5787 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005788 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005789 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005790
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005791 static bool HasStringReference(LoadKind load_kind) {
5792 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5793 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5794 load_kind == LoadKind::kDexCacheViaMethod;
5795 }
5796
5797 static bool HasAddress(LoadKind load_kind) {
5798 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5799 }
5800
5801 static bool HasDexCacheReference(LoadKind load_kind) {
5802 return load_kind == LoadKind::kDexCachePcRelative;
5803 }
5804
5805 void SetLoadKindInternal(LoadKind load_kind);
5806
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005807 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5808 // For other load kinds it's empty or possibly some architecture-specific instruction
5809 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005810 HUserRecord<HInstruction*> special_input_;
5811
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005812 // String index serves also as the hash code and it's also needed for slow-paths,
5813 // so it must not be overwritten with other load data.
5814 uint32_t string_index_;
5815
5816 union {
5817 struct {
5818 const DexFile* dex_file; // For string reference and dex cache reference.
5819 uint32_t dex_cache_element_index; // Only for dex cache reference.
5820 } ref;
5821 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5822 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005823
5824 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5825};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005826std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5827
5828// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5829inline const DexFile& HLoadString::GetDexFile() const {
5830 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5831 << GetLoadKind();
5832 return *load_data_.ref.dex_file;
5833}
5834
5835// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5836inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5837 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5838 return load_data_.ref.dex_cache_element_index;
5839}
5840
5841// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5842inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005843 // The special input is used for PC-relative loads on some architectures,
5844 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005845 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005846 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5847 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5848 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005849 // HLoadString::GetInputRecords() returns an empty array at this point,
5850 // so use the GetInputRecords() from the base class to set the input record.
5851 DCHECK(special_input_.GetInstruction() == nullptr);
5852 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005853 special_input->AddUseAt(this, 0);
5854}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005855
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005856/**
5857 * Performs an initialization check on its Class object input.
5858 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005859class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005860 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005861 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005862 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005863 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005864 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5865 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005866 SetRawInputAt(0, constant);
5867 }
5868
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005869 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005870 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005871 return true;
5872 }
5873
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005874 bool NeedsEnvironment() const OVERRIDE {
5875 // May call runtime to initialize the class.
5876 return true;
5877 }
5878
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005879 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005880
5881 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5882
5883 DECLARE_INSTRUCTION(ClinitCheck);
5884
5885 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005886 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5887};
5888
Vladimir Markofcb503c2016-05-18 12:48:17 +01005889class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005890 public:
5891 HStaticFieldGet(HInstruction* cls,
5892 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005893 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005894 bool is_volatile,
5895 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005896 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005897 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005898 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005899 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005900 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005901 field_info_(field_offset,
5902 field_type,
5903 is_volatile,
5904 field_idx,
5905 declaring_class_def_index,
5906 dex_file,
5907 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005908 SetRawInputAt(0, cls);
5909 }
5910
Calin Juravle52c48962014-12-16 17:02:57 +00005911
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005912 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005913
Vladimir Marko372f10e2016-05-17 16:30:10 +01005914 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5915 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005916 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005917 }
5918
5919 size_t ComputeHashCode() const OVERRIDE {
5920 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5921 }
5922
Calin Juravle52c48962014-12-16 17:02:57 +00005923 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005924 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5925 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005926 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005927
5928 DECLARE_INSTRUCTION(StaticFieldGet);
5929
5930 private:
5931 const FieldInfo field_info_;
5932
5933 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5934};
5935
Vladimir Markofcb503c2016-05-18 12:48:17 +01005936class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005937 public:
5938 HStaticFieldSet(HInstruction* cls,
5939 HInstruction* value,
5940 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005941 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005942 bool is_volatile,
5943 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005944 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005945 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005946 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005947 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005948 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005949 field_info_(field_offset,
5950 field_type,
5951 is_volatile,
5952 field_idx,
5953 declaring_class_def_index,
5954 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005955 dex_cache) {
5956 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005957 SetRawInputAt(0, cls);
5958 SetRawInputAt(1, value);
5959 }
5960
Calin Juravle52c48962014-12-16 17:02:57 +00005961 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005962 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5963 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005964 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005965
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005966 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005967 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5968 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005969
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005970 DECLARE_INSTRUCTION(StaticFieldSet);
5971
5972 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005973 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5974 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5975 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5976 "Too many packed fields.");
5977
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005978 const FieldInfo field_info_;
5979
5980 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5981};
5982
Vladimir Markofcb503c2016-05-18 12:48:17 +01005983class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005984 public:
5985 HUnresolvedInstanceFieldGet(HInstruction* obj,
5986 Primitive::Type field_type,
5987 uint32_t field_index,
5988 uint32_t dex_pc)
5989 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5990 field_index_(field_index) {
5991 SetRawInputAt(0, obj);
5992 }
5993
5994 bool NeedsEnvironment() const OVERRIDE { return true; }
5995 bool CanThrow() const OVERRIDE { return true; }
5996
5997 Primitive::Type GetFieldType() const { return GetType(); }
5998 uint32_t GetFieldIndex() const { return field_index_; }
5999
6000 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6001
6002 private:
6003 const uint32_t field_index_;
6004
6005 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6006};
6007
Vladimir Markofcb503c2016-05-18 12:48:17 +01006008class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006009 public:
6010 HUnresolvedInstanceFieldSet(HInstruction* obj,
6011 HInstruction* value,
6012 Primitive::Type field_type,
6013 uint32_t field_index,
6014 uint32_t dex_pc)
6015 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006016 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006017 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006018 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006019 SetRawInputAt(0, obj);
6020 SetRawInputAt(1, value);
6021 }
6022
6023 bool NeedsEnvironment() const OVERRIDE { return true; }
6024 bool CanThrow() const OVERRIDE { return true; }
6025
Vladimir Markoa1de9182016-02-25 11:37:38 +00006026 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006027 uint32_t GetFieldIndex() const { return field_index_; }
6028
6029 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6030
6031 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006032 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6033 static constexpr size_t kFieldFieldTypeSize =
6034 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6035 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6036 kFieldFieldType + kFieldFieldTypeSize;
6037 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6038 "Too many packed fields.");
6039 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6040
Calin Juravlee460d1d2015-09-29 04:52:17 +01006041 const uint32_t field_index_;
6042
6043 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6044};
6045
Vladimir Markofcb503c2016-05-18 12:48:17 +01006046class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006047 public:
6048 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6049 uint32_t field_index,
6050 uint32_t dex_pc)
6051 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6052 field_index_(field_index) {
6053 }
6054
6055 bool NeedsEnvironment() const OVERRIDE { return true; }
6056 bool CanThrow() const OVERRIDE { return true; }
6057
6058 Primitive::Type GetFieldType() const { return GetType(); }
6059 uint32_t GetFieldIndex() const { return field_index_; }
6060
6061 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6062
6063 private:
6064 const uint32_t field_index_;
6065
6066 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6067};
6068
Vladimir Markofcb503c2016-05-18 12:48:17 +01006069class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006070 public:
6071 HUnresolvedStaticFieldSet(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, value);
6080 }
6081
6082 bool NeedsEnvironment() const OVERRIDE { return true; }
6083 bool CanThrow() const OVERRIDE { return true; }
6084
Vladimir Markoa1de9182016-02-25 11:37:38 +00006085 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006086 uint32_t GetFieldIndex() const { return field_index_; }
6087
6088 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6089
6090 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006091 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6092 static constexpr size_t kFieldFieldTypeSize =
6093 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6094 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6095 kFieldFieldType + kFieldFieldTypeSize;
6096 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6097 "Too many packed fields.");
6098 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6099
Calin Juravlee460d1d2015-09-29 04:52:17 +01006100 const uint32_t field_index_;
6101
6102 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6103};
6104
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006105// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006106class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006107 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006108 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6109 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006110
David Brazdilbbd733e2015-08-18 17:48:17 +01006111 bool CanBeNull() const OVERRIDE { return false; }
6112
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006113 DECLARE_INSTRUCTION(LoadException);
6114
6115 private:
6116 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6117};
6118
David Brazdilcb1c0552015-08-04 16:22:25 +01006119// Implicit part of move-exception which clears thread-local exception storage.
6120// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006121class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006122 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006123 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6124 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006125
6126 DECLARE_INSTRUCTION(ClearException);
6127
6128 private:
6129 DISALLOW_COPY_AND_ASSIGN(HClearException);
6130};
6131
Vladimir Markofcb503c2016-05-18 12:48:17 +01006132class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006133 public:
6134 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006135 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006136 SetRawInputAt(0, exception);
6137 }
6138
6139 bool IsControlFlow() const OVERRIDE { return true; }
6140
6141 bool NeedsEnvironment() const OVERRIDE { return true; }
6142
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006143 bool CanThrow() const OVERRIDE { return true; }
6144
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006145
6146 DECLARE_INSTRUCTION(Throw);
6147
6148 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006149 DISALLOW_COPY_AND_ASSIGN(HThrow);
6150};
6151
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006152/**
6153 * Implementation strategies for the code generator of a HInstanceOf
6154 * or `HCheckCast`.
6155 */
6156enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006157 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006158 kExactCheck, // Can do a single class compare.
6159 kClassHierarchyCheck, // Can just walk the super class chain.
6160 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6161 kInterfaceCheck, // No optimization yet when checking against an interface.
6162 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006163 kArrayCheck, // No optimization yet when checking against a generic array.
6164 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006165};
6166
Roland Levillain86503782016-02-11 19:07:30 +00006167std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6168
Vladimir Markofcb503c2016-05-18 12:48:17 +01006169class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006170 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006171 HInstanceOf(HInstruction* object,
6172 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006173 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006174 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006175 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006176 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006177 dex_pc) {
6178 SetPackedField<TypeCheckKindField>(check_kind);
6179 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006180 SetRawInputAt(0, object);
6181 SetRawInputAt(1, constant);
6182 }
6183
6184 bool CanBeMoved() const OVERRIDE { return true; }
6185
Vladimir Marko372f10e2016-05-17 16:30:10 +01006186 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006187 return true;
6188 }
6189
6190 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006191 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006192 }
6193
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006194 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006195 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6196 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6197 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6198 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006199
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006200 static bool CanCallRuntime(TypeCheckKind check_kind) {
6201 // Mips currently does runtime calls for any other checks.
6202 return check_kind != TypeCheckKind::kExactCheck;
6203 }
6204
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006205 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006206 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006207 }
6208
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006209 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006210
6211 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006212 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6213 static constexpr size_t kFieldTypeCheckKindSize =
6214 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6215 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6216 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6217 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6218 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006219
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006220 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6221};
6222
Vladimir Markofcb503c2016-05-18 12:48:17 +01006223class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006224 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006225 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006226 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006227 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6228 SetPackedFlag<kFlagUpperCanBeNull>(true);
6229 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006230 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006231 SetRawInputAt(0, input);
6232 }
6233
David Brazdilf5552582015-12-27 13:36:12 +00006234 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006235 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006236 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006237 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006238
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006239 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006240 DCHECK(GetUpperCanBeNull() || !can_be_null);
6241 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006242 }
6243
Vladimir Markoa1de9182016-02-25 11:37:38 +00006244 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006245
Calin Juravleb1498f62015-02-16 13:13:29 +00006246 DECLARE_INSTRUCTION(BoundType);
6247
6248 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006249 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6250 // is false then CanBeNull() cannot be true).
6251 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6252 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6253 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6254 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6255
Calin Juravleb1498f62015-02-16 13:13:29 +00006256 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006257 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6258 // It is used to bound the type in cases like:
6259 // if (x instanceof ClassX) {
6260 // // uper_bound_ will be ClassX
6261 // }
David Brazdilf5552582015-12-27 13:36:12 +00006262 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006263
6264 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6265};
6266
Vladimir Markofcb503c2016-05-18 12:48:17 +01006267class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006268 public:
6269 HCheckCast(HInstruction* object,
6270 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006271 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006272 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006273 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6274 SetPackedField<TypeCheckKindField>(check_kind);
6275 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006276 SetRawInputAt(0, object);
6277 SetRawInputAt(1, constant);
6278 }
6279
6280 bool CanBeMoved() const OVERRIDE { return true; }
6281
Vladimir Marko372f10e2016-05-17 16:30:10 +01006282 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006283 return true;
6284 }
6285
6286 bool NeedsEnvironment() const OVERRIDE {
6287 // Instruction may throw a CheckCastError.
6288 return true;
6289 }
6290
6291 bool CanThrow() const OVERRIDE { return true; }
6292
Vladimir Markoa1de9182016-02-25 11:37:38 +00006293 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6294 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6295 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6296 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006297
6298 DECLARE_INSTRUCTION(CheckCast);
6299
6300 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006301 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6302 static constexpr size_t kFieldTypeCheckKindSize =
6303 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6304 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6305 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6306 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6307 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006308
6309 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006310};
6311
Andreas Gampe26de38b2016-07-27 17:53:11 -07006312/**
6313 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6314 * @details We define the combined barrier types that are actually required
6315 * by the Java Memory Model, rather than using exactly the terminology from
6316 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6317 * primitives. Note that the JSR-133 cookbook generally does not deal with
6318 * store atomicity issues, and the recipes there are not always entirely sufficient.
6319 * The current recipe is as follows:
6320 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6321 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6322 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6323 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6324 * class has final fields.
6325 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6326 * store-to-memory instructions. Only generated together with non-temporal stores.
6327 */
6328enum MemBarrierKind {
6329 kAnyStore,
6330 kLoadAny,
6331 kStoreStore,
6332 kAnyAny,
6333 kNTStoreStore,
6334 kLastBarrierKind = kNTStoreStore
6335};
6336std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6337
Vladimir Markofcb503c2016-05-18 12:48:17 +01006338class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006339 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006340 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006341 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006342 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6343 SetPackedField<BarrierKindField>(barrier_kind);
6344 }
Calin Juravle27df7582015-04-17 19:12:31 +01006345
Vladimir Markoa1de9182016-02-25 11:37:38 +00006346 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006347
6348 DECLARE_INSTRUCTION(MemoryBarrier);
6349
6350 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006351 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6352 static constexpr size_t kFieldBarrierKindSize =
6353 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6354 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6355 kFieldBarrierKind + kFieldBarrierKindSize;
6356 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6357 "Too many packed fields.");
6358 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006359
6360 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6361};
6362
Vladimir Markofcb503c2016-05-18 12:48:17 +01006363class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006364 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006365 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006366 kEnter,
6367 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006368 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006369 };
6370
6371 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006372 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006373 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6374 dex_pc) {
6375 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006376 SetRawInputAt(0, object);
6377 }
6378
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006379 // Instruction may go into runtime, so we need an environment.
6380 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006381
6382 bool CanThrow() const OVERRIDE {
6383 // Verifier guarantees that monitor-exit cannot throw.
6384 // This is important because it allows the HGraphBuilder to remove
6385 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6386 return IsEnter();
6387 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006388
Vladimir Markoa1de9182016-02-25 11:37:38 +00006389 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6390 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006391
6392 DECLARE_INSTRUCTION(MonitorOperation);
6393
Calin Juravle52c48962014-12-16 17:02:57 +00006394 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006395 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6396 static constexpr size_t kFieldOperationKindSize =
6397 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6398 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6399 kFieldOperationKind + kFieldOperationKindSize;
6400 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6401 "Too many packed fields.");
6402 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006403
6404 private:
6405 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6406};
6407
Vladimir Markofcb503c2016-05-18 12:48:17 +01006408class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006409 public:
6410 HSelect(HInstruction* condition,
6411 HInstruction* true_value,
6412 HInstruction* false_value,
6413 uint32_t dex_pc)
6414 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6415 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6416
6417 // First input must be `true_value` or `false_value` to allow codegens to
6418 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6419 // that architectures which implement HSelect as a conditional move also
6420 // will not need to invert the condition.
6421 SetRawInputAt(0, false_value);
6422 SetRawInputAt(1, true_value);
6423 SetRawInputAt(2, condition);
6424 }
6425
6426 HInstruction* GetFalseValue() const { return InputAt(0); }
6427 HInstruction* GetTrueValue() const { return InputAt(1); }
6428 HInstruction* GetCondition() const { return InputAt(2); }
6429
6430 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006431 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6432 return true;
6433 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006434
6435 bool CanBeNull() const OVERRIDE {
6436 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6437 }
6438
6439 DECLARE_INSTRUCTION(Select);
6440
6441 private:
6442 DISALLOW_COPY_AND_ASSIGN(HSelect);
6443};
6444
Vladimir Markof9f64412015-09-02 14:05:49 +01006445class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006446 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006447 MoveOperands(Location source,
6448 Location destination,
6449 Primitive::Type type,
6450 HInstruction* instruction)
6451 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006452
6453 Location GetSource() const { return source_; }
6454 Location GetDestination() const { return destination_; }
6455
6456 void SetSource(Location value) { source_ = value; }
6457 void SetDestination(Location value) { destination_ = value; }
6458
6459 // The parallel move resolver marks moves as "in-progress" by clearing the
6460 // destination (but not the source).
6461 Location MarkPending() {
6462 DCHECK(!IsPending());
6463 Location dest = destination_;
6464 destination_ = Location::NoLocation();
6465 return dest;
6466 }
6467
6468 void ClearPending(Location dest) {
6469 DCHECK(IsPending());
6470 destination_ = dest;
6471 }
6472
6473 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006474 DCHECK(source_.IsValid() || destination_.IsInvalid());
6475 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006476 }
6477
6478 // True if this blocks a move from the given location.
6479 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006480 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006481 }
6482
6483 // A move is redundant if it's been eliminated, if its source and
6484 // destination are the same, or if its destination is unneeded.
6485 bool IsRedundant() const {
6486 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6487 }
6488
6489 // We clear both operands to indicate move that's been eliminated.
6490 void Eliminate() {
6491 source_ = destination_ = Location::NoLocation();
6492 }
6493
6494 bool IsEliminated() const {
6495 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6496 return source_.IsInvalid();
6497 }
6498
Alexey Frunze4dda3372015-06-01 18:31:49 -07006499 Primitive::Type GetType() const { return type_; }
6500
Nicolas Geoffray90218252015-04-15 11:56:51 +01006501 bool Is64BitMove() const {
6502 return Primitive::Is64BitType(type_);
6503 }
6504
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006505 HInstruction* GetInstruction() const { return instruction_; }
6506
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006507 private:
6508 Location source_;
6509 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006510 // The type this move is for.
6511 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006512 // The instruction this move is assocatied with. Null when this move is
6513 // for moving an input in the expected locations of user (including a phi user).
6514 // This is only used in debug mode, to ensure we do not connect interval siblings
6515 // in the same parallel move.
6516 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006517};
6518
Roland Levillainc9285912015-12-18 10:38:42 +00006519std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6520
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006521static constexpr size_t kDefaultNumberOfMoves = 4;
6522
Vladimir Markofcb503c2016-05-18 12:48:17 +01006523class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006524 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006525 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006526 : HTemplateInstruction(SideEffects::None(), dex_pc),
6527 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6528 moves_.reserve(kDefaultNumberOfMoves);
6529 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006530
Nicolas Geoffray90218252015-04-15 11:56:51 +01006531 void AddMove(Location source,
6532 Location destination,
6533 Primitive::Type type,
6534 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006535 DCHECK(source.IsValid());
6536 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006537 if (kIsDebugBuild) {
6538 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006539 for (const MoveOperands& move : moves_) {
6540 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006541 // Special case the situation where the move is for the spill slot
6542 // of the instruction.
6543 if ((GetPrevious() == instruction)
6544 || ((GetPrevious() == nullptr)
6545 && instruction->IsPhi()
6546 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006547 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006548 << "Doing parallel moves for the same instruction.";
6549 } else {
6550 DCHECK(false) << "Doing parallel moves for the same instruction.";
6551 }
6552 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006553 }
6554 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006555 for (const MoveOperands& move : moves_) {
6556 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006557 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006558 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006559 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006560 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006561 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006562 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006563 }
6564
Vladimir Marko225b6462015-09-28 12:17:40 +01006565 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006566 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006567 }
6568
Vladimir Marko225b6462015-09-28 12:17:40 +01006569 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006570
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006571 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006572
6573 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006574 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006575
6576 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6577};
6578
Mark Mendell0616ae02015-04-17 12:49:27 -04006579} // namespace art
6580
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006581#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6582#include "nodes_shared.h"
6583#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006584#ifdef ART_ENABLE_CODEGEN_arm
6585#include "nodes_arm.h"
6586#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006587#ifdef ART_ENABLE_CODEGEN_arm64
6588#include "nodes_arm64.h"
6589#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006590#ifdef ART_ENABLE_CODEGEN_mips
6591#include "nodes_mips.h"
6592#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006593#ifdef ART_ENABLE_CODEGEN_x86
6594#include "nodes_x86.h"
6595#endif
6596
6597namespace art {
6598
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006599class HGraphVisitor : public ValueObject {
6600 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006601 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6602 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006603
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006604 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006605 virtual void VisitBasicBlock(HBasicBlock* block);
6606
Roland Levillain633021e2014-10-01 14:12:25 +01006607 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006608 void VisitInsertionOrder();
6609
Roland Levillain633021e2014-10-01 14:12:25 +01006610 // Visit the graph following dominator tree reverse post-order.
6611 void VisitReversePostOrder();
6612
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006613 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006614
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006615 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006616#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006617 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6618
6619 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6620
6621#undef DECLARE_VISIT_INSTRUCTION
6622
6623 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006624 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006625
6626 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6627};
6628
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006629class HGraphDelegateVisitor : public HGraphVisitor {
6630 public:
6631 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6632 virtual ~HGraphDelegateVisitor() {}
6633
6634 // Visit functions that delegate to to super class.
6635#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006636 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006637
6638 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6639
6640#undef DECLARE_VISIT_INSTRUCTION
6641
6642 private:
6643 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6644};
6645
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006646class HInsertionOrderIterator : public ValueObject {
6647 public:
6648 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6649
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006650 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006651 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006652 void Advance() { ++index_; }
6653
6654 private:
6655 const HGraph& graph_;
6656 size_t index_;
6657
6658 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6659};
6660
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006661class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006662 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006663 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6664 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006665 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006666 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006667
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006668 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6669 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006670 void Advance() { ++index_; }
6671
6672 private:
6673 const HGraph& graph_;
6674 size_t index_;
6675
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006676 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006677};
6678
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006679class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006680 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006681 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006682 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006683 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006684 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006685 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006686
6687 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006688 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006689 void Advance() { --index_; }
6690
6691 private:
6692 const HGraph& graph_;
6693 size_t index_;
6694
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006695 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006696};
6697
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006698class HLinearPostOrderIterator : public ValueObject {
6699 public:
6700 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006701 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006702
6703 bool Done() const { return index_ == 0; }
6704
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006705 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006706
6707 void Advance() {
6708 --index_;
6709 DCHECK_GE(index_, 0U);
6710 }
6711
6712 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006713 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006714 size_t index_;
6715
6716 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6717};
6718
6719class HLinearOrderIterator : public ValueObject {
6720 public:
6721 explicit HLinearOrderIterator(const HGraph& graph)
6722 : order_(graph.GetLinearOrder()), index_(0) {}
6723
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006724 bool Done() const { return index_ == order_.size(); }
6725 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006726 void Advance() { ++index_; }
6727
6728 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006729 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006730 size_t index_;
6731
6732 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6733};
6734
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006735// Iterator over the blocks that art part of the loop. Includes blocks part
6736// of an inner loop. The order in which the blocks are iterated is on their
6737// block id.
6738class HBlocksInLoopIterator : public ValueObject {
6739 public:
6740 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6741 : blocks_in_loop_(info.GetBlocks()),
6742 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6743 index_(0) {
6744 if (!blocks_in_loop_.IsBitSet(index_)) {
6745 Advance();
6746 }
6747 }
6748
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006749 bool Done() const { return index_ == blocks_.size(); }
6750 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006751 void Advance() {
6752 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006753 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006754 if (blocks_in_loop_.IsBitSet(index_)) {
6755 break;
6756 }
6757 }
6758 }
6759
6760 private:
6761 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006762 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006763 size_t index_;
6764
6765 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6766};
6767
Mingyao Yang3584bce2015-05-19 16:01:59 -07006768// Iterator over the blocks that art part of the loop. Includes blocks part
6769// of an inner loop. The order in which the blocks are iterated is reverse
6770// post order.
6771class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6772 public:
6773 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6774 : blocks_in_loop_(info.GetBlocks()),
6775 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6776 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006777 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006778 Advance();
6779 }
6780 }
6781
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006782 bool Done() const { return index_ == blocks_.size(); }
6783 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006784 void Advance() {
6785 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006786 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6787 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006788 break;
6789 }
6790 }
6791 }
6792
6793 private:
6794 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006795 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006796 size_t index_;
6797
6798 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6799};
6800
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006801inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006802 if (constant->IsIntConstant()) {
6803 return constant->AsIntConstant()->GetValue();
6804 } else if (constant->IsLongConstant()) {
6805 return constant->AsLongConstant()->GetValue();
6806 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006807 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006808 return 0;
6809 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006810}
6811
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006812#define INSTRUCTION_TYPE_CHECK(type, super) \
6813 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6814 inline const H##type* HInstruction::As##type() const { \
6815 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6816 } \
6817 inline H##type* HInstruction::As##type() { \
6818 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6819 }
6820
6821 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6822#undef INSTRUCTION_TYPE_CHECK
6823
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006824// Create space in `blocks` for adding `number_of_new_blocks` entries
6825// starting at location `at`. Blocks after `at` are moved accordingly.
6826inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6827 size_t number_of_new_blocks,
6828 size_t after) {
6829 DCHECK_LT(after, blocks->size());
6830 size_t old_size = blocks->size();
6831 size_t new_size = old_size + number_of_new_blocks;
6832 blocks->resize(new_size);
6833 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6834}
6835
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006836} // namespace art
6837
6838#endif // ART_COMPILER_OPTIMIZING_NODES_H_