blob: 257ccea79989392eda4203fdee0488ea0cb604be [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 Geoffray762869d2016-07-15 15:28:35 +010035#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010036#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000037#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000038#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010039#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070040#include "primitive.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010041#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042
43namespace art {
44
David Brazdil1abb4192015-02-17 18:33:36 +000045class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010047class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010049class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000051class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000053class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000054class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000055class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000056class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000057class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010058class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010059class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010060class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010061class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000062class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010063class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000064class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000065
Mathieu Chartier736b5602015-09-02 14:54:11 -070066namespace mirror {
67class DexCache;
68} // namespace mirror
69
Nicolas Geoffray818f2102014-02-18 16:43:35 +000070static const int kDefaultNumberOfBlocks = 8;
71static const int kDefaultNumberOfSuccessors = 2;
72static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010073static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010074static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000075static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000076
Roland Levillain5b5b9312016-03-22 14:57:31 +000077// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
78static constexpr int32_t kMaxIntShiftDistance = 0x1f;
79// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
80static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000081
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010082static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070083static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010084
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010085static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
86
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060087static constexpr uint32_t kNoDexPc = -1;
88
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010089inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
90 // For the purposes of the compiler, the dex files must actually be the same object
91 // if we want to safely treat them as the same. This is especially important for JIT
92 // as custom class loaders can open the same underlying file (or memory) multiple
93 // times and provide different class resolution but no two class loaders should ever
94 // use the same DexFile object - doing so is an unsupported hack that can lead to
95 // all sorts of weird failures.
96 return &lhs == &rhs;
97}
98
Dave Allison20dfc792014-06-16 20:44:29 -070099enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700100 // All types.
101 kCondEQ, // ==
102 kCondNE, // !=
103 // Signed integers and floating-point numbers.
104 kCondLT, // <
105 kCondLE, // <=
106 kCondGT, // >
107 kCondGE, // >=
108 // Unsigned integers.
109 kCondB, // <
110 kCondBE, // <=
111 kCondA, // >
112 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100113 // First and last aliases.
114 kCondFirst = kCondEQ,
115 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700116};
117
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000118enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000119 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000120 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000121 kAnalysisFailThrowCatchLoop,
122 kAnalysisFailAmbiguousArrayOp,
123 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000124};
125
Vladimir Markof9f64412015-09-02 14:05:49 +0100126class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100127 public:
128 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
129
130 void AddInstruction(HInstruction* instruction);
131 void RemoveInstruction(HInstruction* instruction);
132
David Brazdilc3d743f2015-04-22 13:40:50 +0100133 // Insert `instruction` before/after an existing instruction `cursor`.
134 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
135 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
136
Roland Levillain6b469232014-09-25 10:10:38 +0100137 // Return true if this list contains `instruction`.
138 bool Contains(HInstruction* instruction) const;
139
Roland Levillainccc07a92014-09-16 14:48:16 +0100140 // Return true if `instruction1` is found before `instruction2` in
141 // this instruction list and false otherwise. Abort if none
142 // of these instructions is found.
143 bool FoundBefore(const HInstruction* instruction1,
144 const HInstruction* instruction2) const;
145
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000146 bool IsEmpty() const { return first_instruction_ == nullptr; }
147 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
148
149 // Update the block of all instructions to be `block`.
150 void SetBlockOfInstructions(HBasicBlock* block) const;
151
152 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000153 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000154 void Add(const HInstructionList& instruction_list);
155
David Brazdil2d7352b2015-04-20 14:52:42 +0100156 // Return the number of instructions in the list. This is an expensive operation.
157 size_t CountSize() const;
158
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100159 private:
160 HInstruction* first_instruction_;
161 HInstruction* last_instruction_;
162
163 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000164 friend class HGraph;
165 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100166 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100167 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100168
169 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
170};
171
David Brazdil4833f5a2015-12-16 10:37:39 +0000172class ReferenceTypeInfo : ValueObject {
173 public:
174 typedef Handle<mirror::Class> TypeHandle;
175
Vladimir Markoa1de9182016-02-25 11:37:38 +0000176 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
177
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700178 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100179 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
180 }
181
Vladimir Markoa1de9182016-02-25 11:37:38 +0000182 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000183 return ReferenceTypeInfo(type_handle, is_exact);
184 }
185
186 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
187
Vladimir Markof39745e2016-01-26 12:16:55 +0000188 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000189 return handle.GetReference() != nullptr;
190 }
191
Vladimir Marko456307a2016-04-19 14:12:13 +0000192 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000193 return IsValidHandle(type_handle_);
194 }
195
196 bool IsExact() const { return is_exact_; }
197
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700198 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000199 DCHECK(IsValid());
200 return GetTypeHandle()->IsObjectClass();
201 }
202
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700203 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000204 DCHECK(IsValid());
205 return GetTypeHandle()->IsStringClass();
206 }
207
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700208 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000209 DCHECK(IsValid());
210 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
211 }
212
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700213 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000214 DCHECK(IsValid());
215 return GetTypeHandle()->IsInterface();
216 }
217
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700218 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000219 DCHECK(IsValid());
220 return GetTypeHandle()->IsArrayClass();
221 }
222
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700223 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000224 DCHECK(IsValid());
225 return GetTypeHandle()->IsPrimitiveArray();
226 }
227
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700228 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000229 DCHECK(IsValid());
230 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
231 }
232
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700233 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000234 DCHECK(IsValid());
235 if (!IsExact()) return false;
236 if (!IsArrayClass()) return false;
237 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
238 }
239
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000241 DCHECK(IsValid());
242 if (!IsExact()) return false;
243 if (!IsArrayClass()) return false;
244 if (!rti.IsArrayClass()) return false;
245 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
246 rti.GetTypeHandle()->GetComponentType());
247 }
248
249 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
250
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700251 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000252 DCHECK(IsValid());
253 DCHECK(rti.IsValid());
254 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
255 }
256
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700257 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000258 DCHECK(IsValid());
259 DCHECK(rti.IsValid());
260 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
261 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
262 }
263
264 // Returns true if the type information provide the same amount of details.
265 // Note that it does not mean that the instructions have the same actual type
266 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700267 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000268 if (!IsValid() && !rti.IsValid()) {
269 // Invalid types are equal.
270 return true;
271 }
272 if (!IsValid() || !rti.IsValid()) {
273 // One is valid, the other not.
274 return false;
275 }
276 return IsExact() == rti.IsExact()
277 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
278 }
279
280 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000281 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
282 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
283 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000284
285 // The class of the object.
286 TypeHandle type_handle_;
287 // Whether or not the type is exact or a superclass of the actual type.
288 // Whether or not we have any information about this type.
289 bool is_exact_;
290};
291
292std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
293
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000294// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100295class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000296 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100297 HGraph(ArenaAllocator* arena,
298 const DexFile& dex_file,
299 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100300 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700301 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100302 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100303 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000304 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100305 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000306 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100307 blocks_(arena->Adapter(kArenaAllocBlockList)),
308 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
309 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700310 entry_block_(nullptr),
311 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100312 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100313 number_of_vregs_(0),
314 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000315 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400316 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000317 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000318 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000319 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000320 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100321 dex_file_(dex_file),
322 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100323 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100324 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100325 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700326 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000327 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100328 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
329 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
330 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
331 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000332 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000333 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
334 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100335 blocks_.reserve(kDefaultNumberOfBlocks);
336 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000337
David Brazdilbadd8262016-02-02 16:28:56 +0000338 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700339 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000340
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000341 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100342 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
343
David Brazdil69ba7b72015-06-23 18:27:30 +0100344 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000345 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100346
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000347 HBasicBlock* GetEntryBlock() const { return entry_block_; }
348 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100349 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000350
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000351 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
352 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000353
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000354 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100355
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100356 void ComputeDominanceInformation();
357 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000358 void ClearLoopInformation();
359 void FindBackEdges(ArenaBitVector* visited);
360 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100361 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100362 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000363
David Brazdil4833f5a2015-12-16 10:37:39 +0000364 // Analyze all natural loops in this graph. Returns a code specifying that it
365 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000366 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000367 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100368
David Brazdilffee3d32015-07-06 11:48:53 +0100369 // Iterate over blocks to compute try block membership. Needs reverse post
370 // order and loop information.
371 void ComputeTryBlockInformation();
372
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000373 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000374 // Returns the instruction to replace the invoke expression or null if the
375 // invoke is for a void method. Note that the caller is responsible for replacing
376 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000377 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000378
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000379 // Update the loop and try membership of `block`, which was spawned from `reference`.
380 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
381 // should be the new back edge.
382 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
383 HBasicBlock* reference,
384 bool replace_if_back_edge);
385
Mingyao Yang3584bce2015-05-19 16:01:59 -0700386 // Need to add a couple of blocks to test if the loop body is entered and
387 // put deoptimization instructions, etc.
388 void TransformLoopHeaderForBCE(HBasicBlock* header);
389
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000390 // Removes `block` from the graph. Assumes `block` has been disconnected from
391 // other blocks and has no instructions or phis.
392 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000393
David Brazdilfc6a86a2015-06-26 10:33:45 +0000394 // Splits the edge between `block` and `successor` while preserving the
395 // indices in the predecessor/successor lists. If there are multiple edges
396 // between the blocks, the lowest indices are used.
397 // Returns the new block which is empty and has the same dex pc as `successor`.
398 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
399
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100400 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
401 void SimplifyLoop(HBasicBlock* header);
402
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000403 int32_t GetNextInstructionId() {
404 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000405 return current_instruction_id_++;
406 }
407
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000408 int32_t GetCurrentInstructionId() const {
409 return current_instruction_id_;
410 }
411
412 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000413 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000414 current_instruction_id_ = id;
415 }
416
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100417 uint16_t GetMaximumNumberOfOutVRegs() const {
418 return maximum_number_of_out_vregs_;
419 }
420
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000421 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
422 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100423 }
424
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100425 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
426 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
427 }
428
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000429 void UpdateTemporariesVRegSlots(size_t slots) {
430 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100431 }
432
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000433 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100434 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000435 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100436 }
437
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100438 void SetNumberOfVRegs(uint16_t number_of_vregs) {
439 number_of_vregs_ = number_of_vregs;
440 }
441
442 uint16_t GetNumberOfVRegs() const {
443 return number_of_vregs_;
444 }
445
446 void SetNumberOfInVRegs(uint16_t value) {
447 number_of_in_vregs_ = value;
448 }
449
David Brazdildee58d62016-04-07 09:54:26 +0000450 uint16_t GetNumberOfInVRegs() const {
451 return number_of_in_vregs_;
452 }
453
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100454 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100455 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100456 return number_of_vregs_ - number_of_in_vregs_;
457 }
458
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100459 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100460 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100461 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100462
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100463 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100464 return linear_order_;
465 }
466
Mark Mendell1152c922015-04-24 17:06:35 -0400467 bool HasBoundsChecks() const {
468 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800469 }
470
Mark Mendell1152c922015-04-24 17:06:35 -0400471 void SetHasBoundsChecks(bool value) {
472 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800473 }
474
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100475 bool ShouldGenerateConstructorBarrier() const {
476 return should_generate_constructor_barrier_;
477 }
478
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000479 bool IsDebuggable() const { return debuggable_; }
480
David Brazdil8d5b8b22015-03-24 10:51:52 +0000481 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000482 // already, it is created and inserted into the graph. This method is only for
483 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600484 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000485
486 // TODO: This is problematic for the consistency of reference type propagation
487 // because it can be created anytime after the pass and thus it will be left
488 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600489 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000490
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600491 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
492 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000493 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600494 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
495 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000496 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600497 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
498 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000499 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600500 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
501 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000502 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000503
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100504 HCurrentMethod* GetCurrentMethod();
505
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100506 const DexFile& GetDexFile() const {
507 return dex_file_;
508 }
509
510 uint32_t GetMethodIdx() const {
511 return method_idx_;
512 }
513
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100514 InvokeType GetInvokeType() const {
515 return invoke_type_;
516 }
517
Mark Mendellc4701932015-04-10 13:18:51 -0400518 InstructionSet GetInstructionSet() const {
519 return instruction_set_;
520 }
521
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000522 bool IsCompilingOsr() const { return osr_; }
523
David Brazdil77a48ae2015-09-15 12:34:04 +0000524 bool HasTryCatch() const { return has_try_catch_; }
525 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100526
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000527 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
528 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
529
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100530 ArtMethod* GetArtMethod() const { return art_method_; }
531 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
532
Mark Mendellf6529172015-11-17 11:16:56 -0500533 // Returns an instruction with the opposite boolean value from 'cond'.
534 // The instruction has been inserted into the graph, either as a constant, or
535 // before cursor.
536 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
537
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000538 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
539
David Brazdil2d7352b2015-04-20 14:52:42 +0100540 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000541 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100542 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000543
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000544 template <class InstructionType, typename ValueType>
545 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600546 ArenaSafeMap<ValueType, InstructionType*>* cache,
547 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000548 // Try to find an existing constant of the given value.
549 InstructionType* constant = nullptr;
550 auto cached_constant = cache->find(value);
551 if (cached_constant != cache->end()) {
552 constant = cached_constant->second;
553 }
554
555 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100556 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000557 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600558 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000559 cache->Overwrite(value, constant);
560 InsertConstant(constant);
561 }
562 return constant;
563 }
564
David Brazdil8d5b8b22015-03-24 10:51:52 +0000565 void InsertConstant(HConstant* instruction);
566
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000567 // Cache a float constant into the graph. This method should only be
568 // called by the SsaBuilder when creating "equivalent" instructions.
569 void CacheFloatConstant(HFloatConstant* constant);
570
571 // See CacheFloatConstant comment.
572 void CacheDoubleConstant(HDoubleConstant* constant);
573
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000574 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000575
576 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100577 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000578
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100579 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100580 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000581
Aart Bik281c6812016-08-26 11:31:48 -0700582 // List of blocks to perform a linear order tree traversal. Unlike the reverse
583 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100584 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100585
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000586 HBasicBlock* entry_block_;
587 HBasicBlock* exit_block_;
588
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100589 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100590 uint16_t maximum_number_of_out_vregs_;
591
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100592 // The number of virtual registers in this method. Contains the parameters.
593 uint16_t number_of_vregs_;
594
595 // The number of virtual registers used by parameters of this method.
596 uint16_t number_of_in_vregs_;
597
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000598 // Number of vreg size slots that the temporaries use (used in baseline compiler).
599 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100600
Mark Mendell1152c922015-04-24 17:06:35 -0400601 // Has bounds checks. We can totally skip BCE if it's false.
602 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800603
David Brazdil77a48ae2015-09-15 12:34:04 +0000604 // Flag whether there are any try/catch blocks in the graph. We will skip
605 // try/catch-related passes if false.
606 bool has_try_catch_;
607
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000608 // Flag whether there are any irreducible loops in the graph.
609 bool has_irreducible_loops_;
610
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000611 // Indicates whether the graph should be compiled in a way that
612 // ensures full debuggability. If false, we can apply more
613 // aggressive optimizations that may limit the level of debugging.
614 const bool debuggable_;
615
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000616 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000617 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000618
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100619 // The dex file from which the method is from.
620 const DexFile& dex_file_;
621
622 // The method index in the dex file.
623 const uint32_t method_idx_;
624
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100625 // If inlined, this encodes how the callee is being invoked.
626 const InvokeType invoke_type_;
627
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100628 // Whether the graph has been transformed to SSA form. Only used
629 // in debug mode to ensure we are not using properties only valid
630 // for non-SSA form (like the number of temporaries).
631 bool in_ssa_form_;
632
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100633 const bool should_generate_constructor_barrier_;
634
Mathieu Chartiere401d142015-04-22 13:56:20 -0700635 const InstructionSet instruction_set_;
636
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000637 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000638 HNullConstant* cached_null_constant_;
639 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000640 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000641 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000642 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000643
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100644 HCurrentMethod* cached_current_method_;
645
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100646 // The ArtMethod this graph is for. Note that for AOT, it may be null,
647 // for example for methods whose declaring class could not be resolved
648 // (such as when the superclass could not be found).
649 ArtMethod* art_method_;
650
David Brazdil4833f5a2015-12-16 10:37:39 +0000651 // Keep the RTI of inexact Object to avoid having to pass stack handle
652 // collection pointer to passes which may create NullConstant.
653 ReferenceTypeInfo inexact_object_rti_;
654
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000655 // Whether we are compiling this graph for on stack replacement: this will
656 // make all loops seen as irreducible and emit special stack maps to mark
657 // compiled code entries which the interpreter can directly jump to.
658 const bool osr_;
659
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000660 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100661 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000662 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000663 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000664 DISALLOW_COPY_AND_ASSIGN(HGraph);
665};
666
Vladimir Markof9f64412015-09-02 14:05:49 +0100667class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000668 public:
669 HLoopInformation(HBasicBlock* header, HGraph* graph)
670 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100671 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000672 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100673 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100674 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100675 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000676 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100677 back_edges_.reserve(kDefaultNumberOfBackEdges);
678 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100679
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000680 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100681 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000682
683 void Dump(std::ostream& os);
684
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100685 HBasicBlock* GetHeader() const {
686 return header_;
687 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000688
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000689 void SetHeader(HBasicBlock* block) {
690 header_ = block;
691 }
692
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100693 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
694 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
695 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
696
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000697 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100698 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000699 }
700
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100701 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100702 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100703 }
704
David Brazdil46e2a392015-03-16 17:31:52 +0000705 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100706 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100707 }
708
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000709 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100710 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000711 }
712
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100713 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100714
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100715 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100716 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100717 }
718
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100719 // Returns the lifetime position of the back edge that has the
720 // greatest lifetime position.
721 size_t GetLifetimeEnd() const;
722
723 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100724 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100725 }
726
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000727 // Finds blocks that are part of this loop.
728 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100729
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100730 // Returns whether this loop information contains `block`.
731 // Note that this loop information *must* be populated before entering this function.
732 bool Contains(const HBasicBlock& block) const;
733
734 // Returns whether this loop information is an inner loop of `other`.
735 // Note that `other` *must* be populated before entering this function.
736 bool IsIn(const HLoopInformation& other) const;
737
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800738 // Returns true if instruction is not defined within this loop.
739 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700740
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100741 const ArenaBitVector& GetBlocks() const { return blocks_; }
742
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000743 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000744 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000745
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000746 void ClearAllBlocks() {
747 blocks_.ClearAllBits();
748 }
749
David Brazdil3f4a5222016-05-06 12:46:21 +0100750 bool HasBackEdgeNotDominatedByHeader() const;
751
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100752 bool IsPopulated() const {
753 return blocks_.GetHighestBitSet() != -1;
754 }
755
Anton Shaminf89381f2016-05-16 16:44:13 +0600756 bool DominatesAllBackEdges(HBasicBlock* block);
757
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000758 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100759 // Internal recursive implementation of `Populate`.
760 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100761 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100762
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000763 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100764 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000765 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100766 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100767 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100768 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000769
770 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
771};
772
David Brazdilec16f792015-08-19 15:04:01 +0100773// Stores try/catch information for basic blocks.
774// Note that HGraph is constructed so that catch blocks cannot simultaneously
775// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100776class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100777 public:
778 // Try block information constructor.
779 explicit TryCatchInformation(const HTryBoundary& try_entry)
780 : try_entry_(&try_entry),
781 catch_dex_file_(nullptr),
782 catch_type_index_(DexFile::kDexNoIndex16) {
783 DCHECK(try_entry_ != nullptr);
784 }
785
786 // Catch block information constructor.
787 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
788 : try_entry_(nullptr),
789 catch_dex_file_(&dex_file),
790 catch_type_index_(catch_type_index) {}
791
792 bool IsTryBlock() const { return try_entry_ != nullptr; }
793
794 const HTryBoundary& GetTryEntry() const {
795 DCHECK(IsTryBlock());
796 return *try_entry_;
797 }
798
799 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
800
801 bool IsCatchAllTypeIndex() const {
802 DCHECK(IsCatchBlock());
803 return catch_type_index_ == DexFile::kDexNoIndex16;
804 }
805
806 uint16_t GetCatchTypeIndex() const {
807 DCHECK(IsCatchBlock());
808 return catch_type_index_;
809 }
810
811 const DexFile& GetCatchDexFile() const {
812 DCHECK(IsCatchBlock());
813 return *catch_dex_file_;
814 }
815
816 private:
817 // One of possibly several TryBoundary instructions entering the block's try.
818 // Only set for try blocks.
819 const HTryBoundary* try_entry_;
820
821 // Exception type information. Only set for catch blocks.
822 const DexFile* catch_dex_file_;
823 const uint16_t catch_type_index_;
824};
825
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100826static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100827static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100828
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000829// A block in a method. Contains the list of instructions represented
830// as a double linked list. Each block knows its predecessors and
831// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100832
Vladimir Markof9f64412015-09-02 14:05:49 +0100833class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000834 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700835 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000836 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000837 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
838 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000839 loop_information_(nullptr),
840 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000841 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100842 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100843 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100844 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000845 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000846 try_catch_information_(nullptr) {
847 predecessors_.reserve(kDefaultNumberOfPredecessors);
848 successors_.reserve(kDefaultNumberOfSuccessors);
849 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
850 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000851
Vladimir Marko60584552015-09-03 13:35:12 +0000852 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100853 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000854 }
855
Vladimir Marko60584552015-09-03 13:35:12 +0000856 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100857 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000858 }
859
David Brazdild26a4112015-11-10 11:07:31 +0000860 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
861 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
862
Vladimir Marko60584552015-09-03 13:35:12 +0000863 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
864 return ContainsElement(successors_, block, start_from);
865 }
866
867 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100868 return dominated_blocks_;
869 }
870
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100871 bool IsEntryBlock() const {
872 return graph_->GetEntryBlock() == this;
873 }
874
875 bool IsExitBlock() const {
876 return graph_->GetExitBlock() == this;
877 }
878
David Brazdil46e2a392015-03-16 17:31:52 +0000879 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000880 bool IsSingleTryBoundary() const;
881
882 // Returns true if this block emits nothing but a jump.
883 bool IsSingleJump() const {
884 HLoopInformation* loop_info = GetLoopInformation();
885 return (IsSingleGoto() || IsSingleTryBoundary())
886 // Back edges generate a suspend check.
887 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
888 }
David Brazdil46e2a392015-03-16 17:31:52 +0000889
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000890 void AddBackEdge(HBasicBlock* back_edge) {
891 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000892 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000893 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100894 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000895 loop_information_->AddBackEdge(back_edge);
896 }
897
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000898 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000899 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000900
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100901 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000902 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600903 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000904
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000905 HBasicBlock* GetDominator() const { return dominator_; }
906 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000907 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
908
909 void RemoveDominatedBlock(HBasicBlock* block) {
910 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100911 }
Vladimir Marko60584552015-09-03 13:35:12 +0000912
913 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
914 ReplaceElement(dominated_blocks_, existing, new_block);
915 }
916
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100917 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000918
919 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100920 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000921 }
922
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100923 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
924 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100925 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100926 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100927 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
928 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000929
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000930 HInstruction* GetFirstInstructionDisregardMoves() const;
931
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000932 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000933 successors_.push_back(block);
934 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000935 }
936
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100937 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
938 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100939 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000940 new_block->predecessors_.push_back(this);
941 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000942 }
943
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000944 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
945 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000946 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000947 new_block->successors_.push_back(this);
948 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000949 }
950
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100951 // Insert `this` between `predecessor` and `successor. This method
952 // preserves the indicies, and will update the first edge found between
953 // `predecessor` and `successor`.
954 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
955 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100956 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000957 successor->predecessors_[predecessor_index] = this;
958 predecessor->successors_[successor_index] = this;
959 successors_.push_back(successor);
960 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100961 }
962
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100963 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000964 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100965 }
966
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000967 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000968 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000969 }
970
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100971 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000972 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100973 }
974
975 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000976 predecessors_.push_back(block);
977 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100978 }
979
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100980 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000981 DCHECK_EQ(predecessors_.size(), 2u);
982 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100983 }
984
David Brazdil769c9e52015-04-27 13:54:09 +0100985 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000986 DCHECK_EQ(successors_.size(), 2u);
987 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100988 }
989
David Brazdilfc6a86a2015-06-26 10:33:45 +0000990 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000991 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100992 }
993
David Brazdilfc6a86a2015-06-26 10:33:45 +0000994 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000995 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100996 }
997
David Brazdilfc6a86a2015-06-26 10:33:45 +0000998 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000999 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001000 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001001 }
1002
1003 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001004 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001005 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001006 }
1007
1008 // Returns whether the first occurrence of `predecessor` in the list of
1009 // predecessors is at index `idx`.
1010 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001011 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001012 return GetPredecessorIndexOf(predecessor) == idx;
1013 }
1014
David Brazdild7558da2015-09-22 13:04:14 +01001015 // Create a new block between this block and its predecessors. The new block
1016 // is added to the graph, all predecessor edges are relinked to it and an edge
1017 // is created to `this`. Returns the new empty block. Reverse post order or
1018 // loop and try/catch information are not updated.
1019 HBasicBlock* CreateImmediateDominator();
1020
David Brazdilfc6a86a2015-06-26 10:33:45 +00001021 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001022 // created, latter block. Note that this method will add the block to the
1023 // graph, create a Goto at the end of the former block and will create an edge
1024 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001025 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001026 HBasicBlock* SplitBefore(HInstruction* cursor);
1027
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001028 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001029 // created block. Note that this method just updates raw block information,
1030 // like predecessors, successors, dominators, and instruction list. It does not
1031 // update the graph, reverse post order, loop information, nor make sure the
1032 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001033 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1034
1035 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1036 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001037
1038 // Merge `other` at the end of `this`. Successors and dominated blocks of
1039 // `other` are changed to be successors and dominated blocks of `this`. Note
1040 // that this method does not update the graph, reverse post order, loop
1041 // information, nor make sure the blocks are consistent (for example ending
1042 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001043 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001044
1045 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1046 // of `this` are moved to `other`.
1047 // Note that this method does not update the graph, reverse post order, loop
1048 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001049 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001050 void ReplaceWith(HBasicBlock* other);
1051
David Brazdil2d7352b2015-04-20 14:52:42 +01001052 // Merge `other` at the end of `this`. This method updates loops, reverse post
1053 // order, links to predecessors, successors, dominators and deletes the block
1054 // from the graph. The two blocks must be successive, i.e. `this` the only
1055 // predecessor of `other` and vice versa.
1056 void MergeWith(HBasicBlock* other);
1057
1058 // Disconnects `this` from all its predecessors, successors and dominator,
1059 // removes it from all loops it is included in and eventually from the graph.
1060 // The block must not dominate any other block. Predecessors and successors
1061 // are safely updated.
1062 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001063
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001064 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001065 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001066 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001067 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001068 // Replace instruction `initial` with `replacement` within this block.
1069 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1070 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001071 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001072 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001073 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1074 // instruction list. With 'ensure_safety' set to true, it verifies that the
1075 // instruction is not in use and removes it from the use lists of its inputs.
1076 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1077 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001078 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001079
1080 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001081 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001082 }
1083
Roland Levillain6b879dd2014-09-22 17:13:44 +01001084 bool IsLoopPreHeaderFirstPredecessor() const {
1085 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001086 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001087 }
1088
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001089 bool IsFirstPredecessorBackEdge() const {
1090 DCHECK(IsLoopHeader());
1091 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1092 }
1093
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001094 HLoopInformation* GetLoopInformation() const {
1095 return loop_information_;
1096 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001097
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001098 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001099 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001100 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001101 void SetInLoop(HLoopInformation* info) {
1102 if (IsLoopHeader()) {
1103 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001104 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001105 loop_information_ = info;
1106 } else if (loop_information_->Contains(*info->GetHeader())) {
1107 // Block is currently part of an outer loop. Make it part of this inner loop.
1108 // Note that a non loop header having a loop information means this loop information
1109 // has already been populated
1110 loop_information_ = info;
1111 } else {
1112 // Block is part of an inner loop. Do not update the loop information.
1113 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1114 // at this point, because this method is being called while populating `info`.
1115 }
1116 }
1117
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001118 // Raw update of the loop information.
1119 void SetLoopInformation(HLoopInformation* info) {
1120 loop_information_ = info;
1121 }
1122
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001123 bool IsInLoop() const { return loop_information_ != nullptr; }
1124
David Brazdilec16f792015-08-19 15:04:01 +01001125 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1126
1127 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1128 try_catch_information_ = try_catch_information;
1129 }
1130
1131 bool IsTryBlock() const {
1132 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1133 }
1134
1135 bool IsCatchBlock() const {
1136 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1137 }
David Brazdilffee3d32015-07-06 11:48:53 +01001138
1139 // Returns the try entry that this block's successors should have. They will
1140 // be in the same try, unless the block ends in a try boundary. In that case,
1141 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001142 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001143
David Brazdild7558da2015-09-22 13:04:14 +01001144 bool HasThrowingInstructions() const;
1145
David Brazdila4b8c212015-05-07 09:59:30 +01001146 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001147 bool Dominates(HBasicBlock* block) const;
1148
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001149 size_t GetLifetimeStart() const { return lifetime_start_; }
1150 size_t GetLifetimeEnd() const { return lifetime_end_; }
1151
1152 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1153 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1154
David Brazdil8d5b8b22015-03-24 10:51:52 +00001155 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001156 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001157 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001158 bool HasSinglePhi() const;
1159
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001160 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001161 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001162 ArenaVector<HBasicBlock*> predecessors_;
1163 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001164 HInstructionList instructions_;
1165 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001166 HLoopInformation* loop_information_;
1167 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001168 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001169 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001170 // The dex program counter of the first instruction of this block.
1171 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001172 size_t lifetime_start_;
1173 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001174 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001175
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001176 friend class HGraph;
1177 friend class HInstruction;
1178
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001179 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1180};
1181
David Brazdilb2bd1c52015-03-25 11:17:37 +00001182// Iterates over the LoopInformation of all loops which contain 'block'
1183// from the innermost to the outermost.
1184class HLoopInformationOutwardIterator : public ValueObject {
1185 public:
1186 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1187 : current_(block.GetLoopInformation()) {}
1188
1189 bool Done() const { return current_ == nullptr; }
1190
1191 void Advance() {
1192 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001193 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001194 }
1195
1196 HLoopInformation* Current() const {
1197 DCHECK(!Done());
1198 return current_;
1199 }
1200
1201 private:
1202 HLoopInformation* current_;
1203
1204 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1205};
1206
Alexandre Ramesef20f712015-06-09 10:29:30 +01001207#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001208 M(Above, Condition) \
1209 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001210 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001211 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001212 M(ArrayGet, Instruction) \
1213 M(ArrayLength, Instruction) \
1214 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001215 M(Below, Condition) \
1216 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001217 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001218 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001219 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001220 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001221 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001222 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001223 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001224 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001225 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001226 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001227 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001228 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001229 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001230 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001231 M(Exit, Instruction) \
1232 M(FloatConstant, Constant) \
1233 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001234 M(GreaterThan, Condition) \
1235 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001236 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001237 M(InstanceFieldGet, Instruction) \
1238 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001239 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001240 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001241 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001242 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001243 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001244 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001245 M(LessThan, Condition) \
1246 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001247 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001248 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001249 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001250 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001251 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001252 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001253 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001254 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001255 M(Neg, UnaryOperation) \
1256 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001257 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001258 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001259 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001260 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001261 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001262 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001263 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001264 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001265 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001266 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001267 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001268 M(Return, Instruction) \
1269 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001270 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001271 M(Shl, BinaryOperation) \
1272 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001273 M(StaticFieldGet, Instruction) \
1274 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001275 M(UnresolvedInstanceFieldGet, Instruction) \
1276 M(UnresolvedInstanceFieldSet, Instruction) \
1277 M(UnresolvedStaticFieldGet, Instruction) \
1278 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001279 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001280 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001281 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001282 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001283 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001284 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001285 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001286 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001287
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001288/*
1289 * Instructions, shared across several (not all) architectures.
1290 */
1291#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1292#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1293#else
1294#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001295 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001296 M(MultiplyAccumulate, Instruction) \
1297 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001298#endif
1299
Vladimir Markob4536b72015-11-24 13:45:23 +00001300#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001301#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001302#else
1303#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1304 M(ArmDexCacheArraysBase, Instruction)
1305#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001306
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001307#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001308#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001309#else
1310#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001311 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001312#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001313
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001314#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001315#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001316#else
1317#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1318 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001319 M(MipsDexCacheArraysBase, Instruction) \
1320 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001321#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001322
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001323#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1324
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001325#ifndef ART_ENABLE_CODEGEN_x86
1326#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1327#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001328#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1329 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001330 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001331 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001332 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001333#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001334
1335#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1336
1337#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1338 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001339 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001340 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1341 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001342 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001343 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001344 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1345 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1346
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001347#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1348 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001349 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001350 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001351 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001352 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001353
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001354#define FOR_EACH_INSTRUCTION(M) \
1355 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1356 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1357
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001358#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001359FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1360#undef FORWARD_DECLARATION
1361
Vladimir Marko372f10e2016-05-17 16:30:10 +01001362#define DECLARE_INSTRUCTION(type) \
1363 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1364 const char* DebugName() const OVERRIDE { return #type; } \
1365 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1366 return other->Is##type(); \
1367 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001368 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001369
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001370#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1371 bool Is##type() const { return As##type() != nullptr; } \
1372 const H##type* As##type() const { return this; } \
1373 H##type* As##type() { return this; }
1374
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001375template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001376class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001377 public:
David Brazdiled596192015-01-23 10:39:45 +00001378 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001379 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001380 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001381
Vladimir Marko46817b82016-03-29 12:21:58 +01001382 // Hook for the IntrusiveForwardList<>.
1383 // TODO: Hide this better.
1384 IntrusiveForwardListHook hook;
1385
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001386 private:
David Brazdiled596192015-01-23 10:39:45 +00001387 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001388 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001389
1390 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001391 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001392
Vladimir Marko46817b82016-03-29 12:21:58 +01001393 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001394
1395 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1396};
1397
David Brazdiled596192015-01-23 10:39:45 +00001398template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001399using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001400
David Brazdil1abb4192015-02-17 18:33:36 +00001401// This class is used by HEnvironment and HInstruction classes to record the
1402// instructions they use and pointers to the corresponding HUseListNodes kept
1403// by the used instructions.
1404template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001405class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001406 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001407 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1408 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001409
Vladimir Marko46817b82016-03-29 12:21:58 +01001410 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1411 : HUserRecord(old_record.instruction_, before_use_node) {}
1412 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1413 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001414 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001415 }
1416
1417 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001418 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1419 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001420
1421 private:
1422 // Instruction used by the user.
1423 HInstruction* instruction_;
1424
Vladimir Marko46817b82016-03-29 12:21:58 +01001425 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1426 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001427};
1428
Vladimir Markoe9004912016-06-16 16:50:52 +01001429// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1430// This is used for HInstruction::GetInputs() to return a container wrapper providing
1431// HInstruction* values even though the underlying container has HUserRecord<>s.
1432struct HInputExtractor {
1433 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1434 return record.GetInstruction();
1435 }
1436 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1437 return record.GetInstruction();
1438 }
1439};
1440
1441using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1442using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1443
Aart Bik854a02b2015-07-14 16:07:00 -07001444/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001445 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001446 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001447 * For write/read dependences on fields/arrays, the dependence analysis uses
1448 * type disambiguation (e.g. a float field write cannot modify the value of an
1449 * integer field read) and the access type (e.g. a reference array write cannot
1450 * modify the value of a reference field read [although it may modify the
1451 * reference fetch prior to reading the field, which is represented by its own
1452 * write/read dependence]). The analysis makes conservative points-to
1453 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1454 * the same, and any reference read depends on any reference read without
1455 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001456 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001457 * The internal representation uses 38-bit and is described in the table below.
1458 * The first line indicates the side effect, and for field/array accesses the
1459 * second line indicates the type of the access (in the order of the
1460 * Primitive::Type enum).
1461 * The two numbered lines below indicate the bit position in the bitfield (read
1462 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001463 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001464 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1465 * +-------------+---------+---------+--------------+---------+---------+
1466 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1467 * | 3 |333333322|222222221| 1 |111111110|000000000|
1468 * | 7 |654321098|765432109| 8 |765432109|876543210|
1469 *
1470 * Note that, to ease the implementation, 'changes' bits are least significant
1471 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001472 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001473class SideEffects : public ValueObject {
1474 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001475 SideEffects() : flags_(0) {}
1476
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001477 static SideEffects None() {
1478 return SideEffects(0);
1479 }
1480
1481 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001482 return SideEffects(kAllChangeBits | kAllDependOnBits);
1483 }
1484
1485 static SideEffects AllChanges() {
1486 return SideEffects(kAllChangeBits);
1487 }
1488
1489 static SideEffects AllDependencies() {
1490 return SideEffects(kAllDependOnBits);
1491 }
1492
1493 static SideEffects AllExceptGCDependency() {
1494 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1495 }
1496
1497 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001498 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001499 }
1500
Aart Bik34c3ba92015-07-20 14:08:59 -07001501 static SideEffects AllWrites() {
1502 return SideEffects(kAllWrites);
1503 }
1504
1505 static SideEffects AllReads() {
1506 return SideEffects(kAllReads);
1507 }
1508
1509 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1510 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001511 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001512 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001513 }
1514
Aart Bik854a02b2015-07-14 16:07:00 -07001515 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001516 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001517 }
1518
Aart Bik34c3ba92015-07-20 14:08:59 -07001519 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1520 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001521 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001522 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001523 }
1524
1525 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001526 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001527 }
1528
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001529 static SideEffects CanTriggerGC() {
1530 return SideEffects(1ULL << kCanTriggerGCBit);
1531 }
1532
1533 static SideEffects DependsOnGC() {
1534 return SideEffects(1ULL << kDependsOnGCBit);
1535 }
1536
Aart Bik854a02b2015-07-14 16:07:00 -07001537 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001538 SideEffects Union(SideEffects other) const {
1539 return SideEffects(flags_ | other.flags_);
1540 }
1541
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001542 SideEffects Exclusion(SideEffects other) const {
1543 return SideEffects(flags_ & ~other.flags_);
1544 }
1545
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001546 void Add(SideEffects other) {
1547 flags_ |= other.flags_;
1548 }
1549
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001550 bool Includes(SideEffects other) const {
1551 return (other.flags_ & flags_) == other.flags_;
1552 }
1553
1554 bool HasSideEffects() const {
1555 return (flags_ & kAllChangeBits);
1556 }
1557
1558 bool HasDependencies() const {
1559 return (flags_ & kAllDependOnBits);
1560 }
1561
1562 // Returns true if there are no side effects or dependencies.
1563 bool DoesNothing() const {
1564 return flags_ == 0;
1565 }
1566
Aart Bik854a02b2015-07-14 16:07:00 -07001567 // Returns true if something is written.
1568 bool DoesAnyWrite() const {
1569 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001570 }
1571
Aart Bik854a02b2015-07-14 16:07:00 -07001572 // Returns true if something is read.
1573 bool DoesAnyRead() const {
1574 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001575 }
1576
Aart Bik854a02b2015-07-14 16:07:00 -07001577 // Returns true if potentially everything is written and read
1578 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001579 bool DoesAllReadWrite() const {
1580 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1581 }
1582
Aart Bik854a02b2015-07-14 16:07:00 -07001583 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001584 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001585 }
1586
Roland Levillain0d5a2812015-11-13 10:07:31 +00001587 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001588 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001589 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1590 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001591 }
1592
1593 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001594 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001595 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001596 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001597 for (int s = kLastBit; s >= 0; s--) {
1598 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1599 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1600 // This is a bit for the GC side effect.
1601 if (current_bit_is_set) {
1602 flags += "GC";
1603 }
Aart Bik854a02b2015-07-14 16:07:00 -07001604 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001605 } else {
1606 // This is a bit for the array/field analysis.
1607 // The underscore character stands for the 'can trigger GC' bit.
1608 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1609 if (current_bit_is_set) {
1610 flags += kDebug[s];
1611 }
1612 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1613 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1614 flags += "|";
1615 }
1616 }
Aart Bik854a02b2015-07-14 16:07:00 -07001617 }
1618 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001619 }
1620
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001621 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001622
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001623 private:
1624 static constexpr int kFieldArrayAnalysisBits = 9;
1625
1626 static constexpr int kFieldWriteOffset = 0;
1627 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1628 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1629 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1630
1631 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1632
1633 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1634 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1635 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1636 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1637
1638 static constexpr int kLastBit = kDependsOnGCBit;
1639 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1640
1641 // Aliases.
1642
1643 static_assert(kChangeBits == kDependOnBits,
1644 "the 'change' bits should match the 'depend on' bits.");
1645
1646 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1647 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1648 static constexpr uint64_t kAllWrites =
1649 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1650 static constexpr uint64_t kAllReads =
1651 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001652
Aart Bik854a02b2015-07-14 16:07:00 -07001653 // Translates type to bit flag.
1654 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1655 CHECK_NE(type, Primitive::kPrimVoid);
1656 const uint64_t one = 1;
1657 const int shift = type; // 0-based consecutive enum
1658 DCHECK_LE(kFieldWriteOffset, shift);
1659 DCHECK_LT(shift, kArrayWriteOffset);
1660 return one << (type + offset);
1661 }
1662
1663 // Private constructor on direct flags value.
1664 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1665
1666 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001667};
1668
David Brazdiled596192015-01-23 10:39:45 +00001669// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001670class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001671 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001672 HEnvironment(ArenaAllocator* arena,
1673 size_t number_of_vregs,
1674 const DexFile& dex_file,
1675 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001676 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001677 InvokeType invoke_type,
1678 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001679 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1680 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001681 parent_(nullptr),
1682 dex_file_(dex_file),
1683 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001684 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001685 invoke_type_(invoke_type),
1686 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001687 }
1688
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001689 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001690 : HEnvironment(arena,
1691 to_copy.Size(),
1692 to_copy.GetDexFile(),
1693 to_copy.GetMethodIdx(),
1694 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001695 to_copy.GetInvokeType(),
1696 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001697
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001698 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001699 if (parent_ != nullptr) {
1700 parent_->SetAndCopyParentChain(allocator, parent);
1701 } else {
1702 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1703 parent_->CopyFrom(parent);
1704 if (parent->GetParent() != nullptr) {
1705 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1706 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001707 }
David Brazdiled596192015-01-23 10:39:45 +00001708 }
1709
Vladimir Marko71bf8092015-09-15 15:33:14 +01001710 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001711 void CopyFrom(HEnvironment* environment);
1712
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001713 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1714 // input to the loop phi instead. This is for inserting instructions that
1715 // require an environment (like HDeoptimization) in the loop pre-header.
1716 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001717
1718 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001719 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001720 }
1721
1722 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001723 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001724 }
1725
David Brazdil1abb4192015-02-17 18:33:36 +00001726 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001727
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001728 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001729
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001730 HEnvironment* GetParent() const { return parent_; }
1731
1732 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001733 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001734 }
1735
1736 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001737 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001738 }
1739
1740 uint32_t GetDexPc() const {
1741 return dex_pc_;
1742 }
1743
1744 uint32_t GetMethodIdx() const {
1745 return method_idx_;
1746 }
1747
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001748 InvokeType GetInvokeType() const {
1749 return invoke_type_;
1750 }
1751
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001752 const DexFile& GetDexFile() const {
1753 return dex_file_;
1754 }
1755
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001756 HInstruction* GetHolder() const {
1757 return holder_;
1758 }
1759
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001760
1761 bool IsFromInlinedInvoke() const {
1762 return GetParent() != nullptr;
1763 }
1764
David Brazdiled596192015-01-23 10:39:45 +00001765 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001766 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1767 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001768 HEnvironment* parent_;
1769 const DexFile& dex_file_;
1770 const uint32_t method_idx_;
1771 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001772 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001773
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001774 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001775 HInstruction* const holder_;
1776
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001777 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001778
1779 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1780};
1781
Vladimir Markof9f64412015-09-02 14:05:49 +01001782class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001783 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001784 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001785 : previous_(nullptr),
1786 next_(nullptr),
1787 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001788 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001789 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001790 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001791 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001792 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001793 locations_(nullptr),
1794 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001795 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001796 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001797 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1798 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1799 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001800
Dave Allison20dfc792014-06-16 20:44:29 -07001801 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001802
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001803#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001804 enum InstructionKind {
1805 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1806 };
1807#undef DECLARE_KIND
1808
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001809 HInstruction* GetNext() const { return next_; }
1810 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001811
Calin Juravle77520bc2015-01-12 18:45:46 +00001812 HInstruction* GetNextDisregardingMoves() const;
1813 HInstruction* GetPreviousDisregardingMoves() const;
1814
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001815 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001816 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001817 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001818 bool IsInBlock() const { return block_ != nullptr; }
1819 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001820 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1821 bool IsIrreducibleLoopHeaderPhi() const {
1822 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1823 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001824
Vladimir Marko372f10e2016-05-17 16:30:10 +01001825 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1826
1827 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1828 // One virtual method is enough, just const_cast<> and then re-add the const.
1829 return ArrayRef<const HUserRecord<HInstruction*>>(
1830 const_cast<HInstruction*>(this)->GetInputRecords());
1831 }
1832
Vladimir Markoe9004912016-06-16 16:50:52 +01001833 HInputsRef GetInputs() {
1834 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001835 }
1836
Vladimir Markoe9004912016-06-16 16:50:52 +01001837 HConstInputsRef GetInputs() const {
1838 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001839 }
1840
1841 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001842 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001843
Vladimir Marko372f10e2016-05-17 16:30:10 +01001844 void SetRawInputAt(size_t index, HInstruction* input) {
1845 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1846 }
1847
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001848 virtual void Accept(HGraphVisitor* visitor) = 0;
1849 virtual const char* DebugName() const = 0;
1850
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001851 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1852
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001853 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001854
1855 uint32_t GetDexPc() const { return dex_pc_; }
1856
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001857 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001858
Roland Levillaine161a2a2014-10-03 12:45:18 +01001859 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001860 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001861
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001862 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001863 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001864
Calin Juravle10e244f2015-01-26 18:54:32 +00001865 // Does not apply for all instructions, but having this at top level greatly
1866 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001867 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001868 virtual bool CanBeNull() const {
1869 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1870 return true;
1871 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001872
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001873 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001874 return false;
1875 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001876
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001877 virtual bool IsActualObject() const {
1878 return GetType() == Primitive::kPrimNot;
1879 }
1880
Calin Juravle2e768302015-07-28 14:41:11 +00001881 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001882
Calin Juravle61d544b2015-02-23 16:46:57 +00001883 ReferenceTypeInfo GetReferenceTypeInfo() const {
1884 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001885 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001886 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001887 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001888
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001889 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001890 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001891 // Note: fixup_end remains valid across push_front().
1892 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1893 HUseListNode<HInstruction*>* new_node =
1894 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1895 uses_.push_front(*new_node);
1896 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001897 }
1898
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001899 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001900 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001901 // Note: env_fixup_end remains valid across push_front().
1902 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1903 HUseListNode<HEnvironment*>* new_node =
1904 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1905 env_uses_.push_front(*new_node);
1906 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001907 }
1908
David Brazdil1abb4192015-02-17 18:33:36 +00001909 void RemoveAsUserOfInput(size_t input) {
1910 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001911 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1912 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1913 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001914 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001915
Vladimir Marko372f10e2016-05-17 16:30:10 +01001916 void RemoveAsUserOfAllInputs() {
1917 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1918 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1919 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1920 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1921 }
1922 }
1923
David Brazdil1abb4192015-02-17 18:33:36 +00001924 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1925 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001926
Vladimir Marko46817b82016-03-29 12:21:58 +01001927 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1928 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1929 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001930 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001931 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001932 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001933
Aart Bikcc42be02016-10-20 16:14:16 -07001934 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07001935 return
1936 !HasSideEffects() &&
1937 !CanThrow() &&
1938 !IsSuspendCheck() &&
1939 !IsControlFlow() &&
1940 !IsNativeDebugInfo() &&
1941 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07001942 // If we added an explicit barrier then we should keep it.
1943 !IsMemoryBarrier();
1944 }
1945
Aart Bikcc42be02016-10-20 16:14:16 -07001946 bool IsDeadAndRemovable() const {
1947 return IsRemovable() && !HasUses();
1948 }
1949
Roland Levillain6c82d402014-10-13 16:10:27 +01001950 // Does this instruction strictly dominate `other_instruction`?
1951 // Returns false if this instruction and `other_instruction` are the same.
1952 // Aborts if this instruction and `other_instruction` are both phis.
1953 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001954
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001955 int GetId() const { return id_; }
1956 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001957
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001958 int GetSsaIndex() const { return ssa_index_; }
1959 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1960 bool HasSsaIndex() const { return ssa_index_ != -1; }
1961
1962 bool HasEnvironment() const { return environment_ != nullptr; }
1963 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001964 // Set the `environment_` field. Raw because this method does not
1965 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001966 void SetRawEnvironment(HEnvironment* environment) {
1967 DCHECK(environment_ == nullptr);
1968 DCHECK_EQ(environment->GetHolder(), this);
1969 environment_ = environment;
1970 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001971
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001972 void InsertRawEnvironment(HEnvironment* environment) {
1973 DCHECK(environment_ != nullptr);
1974 DCHECK_EQ(environment->GetHolder(), this);
1975 DCHECK(environment->GetParent() == nullptr);
1976 environment->parent_ = environment_;
1977 environment_ = environment;
1978 }
1979
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001980 void RemoveEnvironment();
1981
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001982 // Set the environment of this instruction, copying it from `environment`. While
1983 // copying, the uses lists are being updated.
1984 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001985 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001986 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001987 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001988 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001989 if (environment->GetParent() != nullptr) {
1990 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1991 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001992 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001993
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001994 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1995 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001996 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001997 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001998 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001999 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002000 if (environment->GetParent() != nullptr) {
2001 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2002 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002003 }
2004
Nicolas Geoffray39468442014-09-02 15:17:15 +01002005 // Returns the number of entries in the environment. Typically, that is the
2006 // number of dex registers in a method. It could be more in case of inlining.
2007 size_t EnvironmentSize() const;
2008
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002009 LocationSummary* GetLocations() const { return locations_; }
2010 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002011
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002012 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002013 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002014
Alexandre Rames188d4312015-04-09 18:30:21 +01002015 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2016 // uses of this instruction by `other` are *not* updated.
2017 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2018 ReplaceWith(other);
2019 other->ReplaceInput(this, use_index);
2020 }
2021
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002022 // Move `this` instruction before `cursor`.
2023 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002024
Vladimir Markofb337ea2015-11-25 15:25:10 +00002025 // Move `this` before its first user and out of any loops. If there is no
2026 // out-of-loop user that dominates all other users, move the instruction
2027 // to the end of the out-of-loop common dominator of the user's blocks.
2028 //
2029 // This can be used only on non-throwing instructions with no side effects that
2030 // have at least one use but no environment uses.
2031 void MoveBeforeFirstUserAndOutOfLoops();
2032
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002033#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002034 bool Is##type() const; \
2035 const H##type* As##type() const; \
2036 H##type* As##type();
2037
2038 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2039#undef INSTRUCTION_TYPE_CHECK
2040
2041#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002042 bool Is##type() const { return (As##type() != nullptr); } \
2043 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002044 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002045 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002046#undef INSTRUCTION_TYPE_CHECK
2047
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002048 // Returns whether the instruction can be moved within the graph.
2049 virtual bool CanBeMoved() const { return false; }
2050
2051 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002052 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002053 return false;
2054 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002055
2056 // Returns whether any data encoded in the two instructions is equal.
2057 // This method does not look at the inputs. Both instructions must be
2058 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002059 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002060 return false;
2061 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002062
2063 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002064 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002065 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002066 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002067
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002068 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2069 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2070 // the virtual function because the __attribute__((__pure__)) doesn't really
2071 // apply the strong requirement for virtual functions, preventing optimizations.
2072 InstructionKind GetKind() const PURE;
2073 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002074
2075 virtual size_t ComputeHashCode() const {
2076 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002077 for (const HInstruction* input : GetInputs()) {
2078 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002079 }
2080 return result;
2081 }
2082
2083 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002084 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002085 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002086
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002087 size_t GetLifetimePosition() const { return lifetime_position_; }
2088 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2089 LiveInterval* GetLiveInterval() const { return live_interval_; }
2090 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2091 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2092
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002093 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2094
2095 // Returns whether the code generation of the instruction will require to have access
2096 // to the current method. Such instructions are:
2097 // (1): Instructions that require an environment, as calling the runtime requires
2098 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002099 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2100 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002101 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002102 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002103 }
2104
Vladimir Markodc151b22015-10-15 18:02:30 +01002105 // Returns whether the code generation of the instruction will require to have access
2106 // to the dex cache of the current method's declaring class via the current method.
2107 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002108
Mark Mendellc4701932015-04-10 13:18:51 -04002109 // Does this instruction have any use in an environment before
2110 // control flow hits 'other'?
2111 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2112
2113 // Remove all references to environment uses of this instruction.
2114 // The caller must ensure that this is safe to do.
2115 void RemoveEnvironmentUsers();
2116
Vladimir Markoa1de9182016-02-25 11:37:38 +00002117 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2118 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002119
David Brazdil1abb4192015-02-17 18:33:36 +00002120 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002121 // If set, the machine code for this instruction is assumed to be generated by
2122 // its users. Used by liveness analysis to compute use positions accordingly.
2123 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2124 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2125 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2126 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2127
Vladimir Marko372f10e2016-05-17 16:30:10 +01002128 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2129 return GetInputRecords()[i];
2130 }
2131
2132 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2133 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2134 input_records[index] = input;
2135 }
David Brazdil1abb4192015-02-17 18:33:36 +00002136
Vladimir Markoa1de9182016-02-25 11:37:38 +00002137 uint32_t GetPackedFields() const {
2138 return packed_fields_;
2139 }
2140
2141 template <size_t flag>
2142 bool GetPackedFlag() const {
2143 return (packed_fields_ & (1u << flag)) != 0u;
2144 }
2145
2146 template <size_t flag>
2147 void SetPackedFlag(bool value = true) {
2148 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2149 }
2150
2151 template <typename BitFieldType>
2152 typename BitFieldType::value_type GetPackedField() const {
2153 return BitFieldType::Decode(packed_fields_);
2154 }
2155
2156 template <typename BitFieldType>
2157 void SetPackedField(typename BitFieldType::value_type value) {
2158 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2159 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2160 }
2161
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002162 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002163 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2164 auto before_use_node = uses_.before_begin();
2165 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2166 HInstruction* user = use_node->GetUser();
2167 size_t input_index = use_node->GetIndex();
2168 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2169 before_use_node = use_node;
2170 }
2171 }
2172
2173 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2174 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2175 if (next != uses_.end()) {
2176 HInstruction* next_user = next->GetUser();
2177 size_t next_index = next->GetIndex();
2178 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2179 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2180 }
2181 }
2182
2183 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2184 auto before_env_use_node = env_uses_.before_begin();
2185 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2186 HEnvironment* user = env_use_node->GetUser();
2187 size_t input_index = env_use_node->GetIndex();
2188 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2189 before_env_use_node = env_use_node;
2190 }
2191 }
2192
2193 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2194 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2195 if (next != env_uses_.end()) {
2196 HEnvironment* next_user = next->GetUser();
2197 size_t next_index = next->GetIndex();
2198 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2199 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2200 }
2201 }
David Brazdil1abb4192015-02-17 18:33:36 +00002202
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002203 HInstruction* previous_;
2204 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002205 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002206 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002207
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002208 // An instruction gets an id when it is added to the graph.
2209 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002210 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002211 int id_;
2212
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002213 // When doing liveness analysis, instructions that have uses get an SSA index.
2214 int ssa_index_;
2215
Vladimir Markoa1de9182016-02-25 11:37:38 +00002216 // Packed fields.
2217 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002218
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002219 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002220 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002221
2222 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002223 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002224
Nicolas Geoffray39468442014-09-02 15:17:15 +01002225 // The environment associated with this instruction. Not null if the instruction
2226 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002227 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002228
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002229 // Set by the code generator.
2230 LocationSummary* locations_;
2231
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002232 // Set by the liveness analysis.
2233 LiveInterval* live_interval_;
2234
2235 // Set by the liveness analysis, this is the position in a linear
2236 // order of blocks where this instruction's live interval start.
2237 size_t lifetime_position_;
2238
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002239 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002240
Vladimir Markoa1de9182016-02-25 11:37:38 +00002241 // The reference handle part of the reference type info.
2242 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002243 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002244 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002245
David Brazdil1abb4192015-02-17 18:33:36 +00002246 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002247 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002248 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002249 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002250 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002251
2252 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2253};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002254std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002255
2256class HInstructionIterator : public ValueObject {
2257 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002258 explicit HInstructionIterator(const HInstructionList& instructions)
2259 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002260 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002261 }
2262
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002263 bool Done() const { return instruction_ == nullptr; }
2264 HInstruction* Current() const { return instruction_; }
2265 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002266 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002267 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002268 }
2269
2270 private:
2271 HInstruction* instruction_;
2272 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002273
2274 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002275};
2276
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002277class HBackwardInstructionIterator : public ValueObject {
2278 public:
2279 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2280 : instruction_(instructions.last_instruction_) {
2281 next_ = Done() ? nullptr : instruction_->GetPrevious();
2282 }
2283
2284 bool Done() const { return instruction_ == nullptr; }
2285 HInstruction* Current() const { return instruction_; }
2286 void Advance() {
2287 instruction_ = next_;
2288 next_ = Done() ? nullptr : instruction_->GetPrevious();
2289 }
2290
2291 private:
2292 HInstruction* instruction_;
2293 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002294
2295 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002296};
2297
Vladimir Markof9f64412015-09-02 14:05:49 +01002298template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002299class HTemplateInstruction: public HInstruction {
2300 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002301 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002302 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002303 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002304
Vladimir Marko372f10e2016-05-17 16:30:10 +01002305 using HInstruction::GetInputRecords; // Keep the const version visible.
2306 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2307 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002308 }
2309
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002310 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002311 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002312
2313 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002314};
2315
Vladimir Markof9f64412015-09-02 14:05:49 +01002316// HTemplateInstruction specialization for N=0.
2317template<>
2318class HTemplateInstruction<0>: public HInstruction {
2319 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002320 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002321 : HInstruction(side_effects, dex_pc) {}
2322
Vladimir Markof9f64412015-09-02 14:05:49 +01002323 virtual ~HTemplateInstruction() {}
2324
Vladimir Marko372f10e2016-05-17 16:30:10 +01002325 using HInstruction::GetInputRecords; // Keep the const version visible.
2326 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2327 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002328 }
2329
2330 private:
2331 friend class SsaBuilder;
2332};
2333
Dave Allison20dfc792014-06-16 20:44:29 -07002334template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002335class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002336 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002337 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002338 : HTemplateInstruction<N>(side_effects, dex_pc) {
2339 this->template SetPackedField<TypeField>(type);
2340 }
Dave Allison20dfc792014-06-16 20:44:29 -07002341 virtual ~HExpression() {}
2342
Vladimir Markoa1de9182016-02-25 11:37:38 +00002343 Primitive::Type GetType() const OVERRIDE {
2344 return TypeField::Decode(this->GetPackedFields());
2345 }
Dave Allison20dfc792014-06-16 20:44:29 -07002346
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002347 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002348 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2349 static constexpr size_t kFieldTypeSize =
2350 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2351 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2352 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2353 "Too many packed fields.");
2354 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002355};
2356
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002357// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2358// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002359class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002360 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002361 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2362 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002363
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002364 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002365
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002366 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002367
2368 private:
2369 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2370};
2371
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002372// Represents dex's RETURN opcodes. A HReturn is a control flow
2373// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002374class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002375 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002376 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2377 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002378 SetRawInputAt(0, value);
2379 }
2380
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002381 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002382
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002383 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002384
2385 private:
2386 DISALLOW_COPY_AND_ASSIGN(HReturn);
2387};
2388
Vladimir Markofcb503c2016-05-18 12:48:17 +01002389class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002390 public:
2391 HPhi(ArenaAllocator* arena,
2392 uint32_t reg_number,
2393 size_t number_of_inputs,
2394 Primitive::Type type,
2395 uint32_t dex_pc = kNoDexPc)
2396 : HInstruction(SideEffects::None(), dex_pc),
2397 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2398 reg_number_(reg_number) {
2399 SetPackedField<TypeField>(ToPhiType(type));
2400 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2401 // Phis are constructed live and marked dead if conflicting or unused.
2402 // Individual steps of SsaBuilder should assume that if a phi has been
2403 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2404 SetPackedFlag<kFlagIsLive>(true);
2405 SetPackedFlag<kFlagCanBeNull>(true);
2406 }
2407
2408 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2409 static Primitive::Type ToPhiType(Primitive::Type type) {
2410 return Primitive::PrimitiveKind(type);
2411 }
2412
2413 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2414
Vladimir Marko372f10e2016-05-17 16:30:10 +01002415 using HInstruction::GetInputRecords; // Keep the const version visible.
2416 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2417 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2418 }
David Brazdildee58d62016-04-07 09:54:26 +00002419
2420 void AddInput(HInstruction* input);
2421 void RemoveInputAt(size_t index);
2422
2423 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2424 void SetType(Primitive::Type new_type) {
2425 // Make sure that only valid type changes occur. The following are allowed:
2426 // (1) int -> float/ref (primitive type propagation),
2427 // (2) long -> double (primitive type propagation).
2428 DCHECK(GetType() == new_type ||
2429 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2430 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2431 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2432 SetPackedField<TypeField>(new_type);
2433 }
2434
2435 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2436 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2437
2438 uint32_t GetRegNumber() const { return reg_number_; }
2439
2440 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2441 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2442 bool IsDead() const { return !IsLive(); }
2443 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2444
Vladimir Markoe9004912016-06-16 16:50:52 +01002445 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002446 return other != nullptr
2447 && other->IsPhi()
2448 && other->AsPhi()->GetBlock() == GetBlock()
2449 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2450 }
2451
2452 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2453 // An equivalent phi is a phi having the same dex register and type.
2454 // It assumes that phis with the same dex register are adjacent.
2455 HPhi* GetNextEquivalentPhiWithSameType() {
2456 HInstruction* next = GetNext();
2457 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2458 if (next->GetType() == GetType()) {
2459 return next->AsPhi();
2460 }
2461 next = next->GetNext();
2462 }
2463 return nullptr;
2464 }
2465
2466 DECLARE_INSTRUCTION(Phi);
2467
David Brazdildee58d62016-04-07 09:54:26 +00002468 private:
2469 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2470 static constexpr size_t kFieldTypeSize =
2471 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2472 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2473 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2474 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2475 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2476 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2477
Vladimir Marko372f10e2016-05-17 16:30:10 +01002478 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002479 const uint32_t reg_number_;
2480
2481 DISALLOW_COPY_AND_ASSIGN(HPhi);
2482};
2483
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002484// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002485// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002486// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002487class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002488 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002489 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002490
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002491 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002492
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002493 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002494
2495 private:
2496 DISALLOW_COPY_AND_ASSIGN(HExit);
2497};
2498
2499// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002500class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002501 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002502 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002503
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002504 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002505
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002506 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002507 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002508 }
2509
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002510 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002511
2512 private:
2513 DISALLOW_COPY_AND_ASSIGN(HGoto);
2514};
2515
Roland Levillain9867bc72015-08-05 10:21:34 +01002516class HConstant : public HExpression<0> {
2517 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002518 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2519 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002520
2521 bool CanBeMoved() const OVERRIDE { return true; }
2522
Roland Levillain1a653882016-03-18 18:05:57 +00002523 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002524 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002525 // Is this constant 0 in the arithmetic sense?
2526 virtual bool IsArithmeticZero() const { return false; }
2527 // Is this constant a 0-bit pattern?
2528 virtual bool IsZeroBitPattern() const { return false; }
2529 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002530 virtual bool IsOne() const { return false; }
2531
David Brazdil77a48ae2015-09-15 12:34:04 +00002532 virtual uint64_t GetValueAsUint64() const = 0;
2533
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002534 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002535
2536 private:
2537 DISALLOW_COPY_AND_ASSIGN(HConstant);
2538};
2539
Vladimir Markofcb503c2016-05-18 12:48:17 +01002540class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002541 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002542 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002543 return true;
2544 }
2545
David Brazdil77a48ae2015-09-15 12:34:04 +00002546 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2547
Roland Levillain9867bc72015-08-05 10:21:34 +01002548 size_t ComputeHashCode() const OVERRIDE { return 0; }
2549
Roland Levillain1a653882016-03-18 18:05:57 +00002550 // The null constant representation is a 0-bit pattern.
2551 virtual bool IsZeroBitPattern() const { return true; }
2552
Roland Levillain9867bc72015-08-05 10:21:34 +01002553 DECLARE_INSTRUCTION(NullConstant);
2554
2555 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002556 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002557
2558 friend class HGraph;
2559 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2560};
2561
2562// Constants of the type int. Those can be from Dex instructions, or
2563// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002564class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002565 public:
2566 int32_t GetValue() const { return value_; }
2567
David Brazdil9f389d42015-10-01 14:32:56 +01002568 uint64_t GetValueAsUint64() const OVERRIDE {
2569 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2570 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002571
Vladimir Marko372f10e2016-05-17 16:30:10 +01002572 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002573 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002574 return other->AsIntConstant()->value_ == value_;
2575 }
2576
2577 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2578
2579 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002580 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2581 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002582 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2583
Roland Levillain1a653882016-03-18 18:05:57 +00002584 // Integer constants are used to encode Boolean values as well,
2585 // where 1 means true and 0 means false.
2586 bool IsTrue() const { return GetValue() == 1; }
2587 bool IsFalse() const { return GetValue() == 0; }
2588
Roland Levillain9867bc72015-08-05 10:21:34 +01002589 DECLARE_INSTRUCTION(IntConstant);
2590
2591 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002592 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2593 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2594 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2595 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002596
2597 const int32_t value_;
2598
2599 friend class HGraph;
2600 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2601 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2602 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2603};
2604
Vladimir Markofcb503c2016-05-18 12:48:17 +01002605class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002606 public:
2607 int64_t GetValue() const { return value_; }
2608
David Brazdil77a48ae2015-09-15 12:34:04 +00002609 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2610
Vladimir Marko372f10e2016-05-17 16:30:10 +01002611 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002612 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002613 return other->AsLongConstant()->value_ == value_;
2614 }
2615
2616 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2617
2618 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002619 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2620 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002621 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2622
2623 DECLARE_INSTRUCTION(LongConstant);
2624
2625 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002626 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2627 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002628
2629 const int64_t value_;
2630
2631 friend class HGraph;
2632 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2633};
Dave Allison20dfc792014-06-16 20:44:29 -07002634
Vladimir Markofcb503c2016-05-18 12:48:17 +01002635class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002636 public:
2637 float GetValue() const { return value_; }
2638
2639 uint64_t GetValueAsUint64() const OVERRIDE {
2640 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2641 }
2642
Vladimir Marko372f10e2016-05-17 16:30:10 +01002643 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002644 DCHECK(other->IsFloatConstant()) << other->DebugName();
2645 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2646 }
2647
2648 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2649
2650 bool IsMinusOne() const OVERRIDE {
2651 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2652 }
Roland Levillain1a653882016-03-18 18:05:57 +00002653 bool IsArithmeticZero() const OVERRIDE {
2654 return std::fpclassify(value_) == FP_ZERO;
2655 }
2656 bool IsArithmeticPositiveZero() const {
2657 return IsArithmeticZero() && !std::signbit(value_);
2658 }
2659 bool IsArithmeticNegativeZero() const {
2660 return IsArithmeticZero() && std::signbit(value_);
2661 }
2662 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002663 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002664 }
2665 bool IsOne() const OVERRIDE {
2666 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2667 }
2668 bool IsNaN() const {
2669 return std::isnan(value_);
2670 }
2671
2672 DECLARE_INSTRUCTION(FloatConstant);
2673
2674 private:
2675 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2676 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2677 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2678 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2679
2680 const float value_;
2681
2682 // Only the SsaBuilder and HGraph can create floating-point constants.
2683 friend class SsaBuilder;
2684 friend class HGraph;
2685 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2686};
2687
Vladimir Markofcb503c2016-05-18 12:48:17 +01002688class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002689 public:
2690 double GetValue() const { return value_; }
2691
2692 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2693
Vladimir Marko372f10e2016-05-17 16:30:10 +01002694 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002695 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2696 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2697 }
2698
2699 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2700
2701 bool IsMinusOne() const OVERRIDE {
2702 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2703 }
Roland Levillain1a653882016-03-18 18:05:57 +00002704 bool IsArithmeticZero() const OVERRIDE {
2705 return std::fpclassify(value_) == FP_ZERO;
2706 }
2707 bool IsArithmeticPositiveZero() const {
2708 return IsArithmeticZero() && !std::signbit(value_);
2709 }
2710 bool IsArithmeticNegativeZero() const {
2711 return IsArithmeticZero() && std::signbit(value_);
2712 }
2713 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002714 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002715 }
2716 bool IsOne() const OVERRIDE {
2717 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2718 }
2719 bool IsNaN() const {
2720 return std::isnan(value_);
2721 }
2722
2723 DECLARE_INSTRUCTION(DoubleConstant);
2724
2725 private:
2726 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2727 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2728 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2729 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2730
2731 const double value_;
2732
2733 // Only the SsaBuilder and HGraph can create floating-point constants.
2734 friend class SsaBuilder;
2735 friend class HGraph;
2736 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2737};
2738
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002739// Conditional branch. A block ending with an HIf instruction must have
2740// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002741class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002742 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002743 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2744 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002745 SetRawInputAt(0, input);
2746 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002747
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002748 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002749
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002750 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002751 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002752 }
2753
2754 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002755 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002756 }
2757
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002758 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002759
2760 private:
2761 DISALLOW_COPY_AND_ASSIGN(HIf);
2762};
2763
David Brazdilfc6a86a2015-06-26 10:33:45 +00002764
2765// Abstract instruction which marks the beginning and/or end of a try block and
2766// links it to the respective exception handlers. Behaves the same as a Goto in
2767// non-exceptional control flow.
2768// Normal-flow successor is stored at index zero, exception handlers under
2769// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002770class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002771 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002772 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002773 kEntry,
2774 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002775 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002776 };
2777
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002778 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002779 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2780 SetPackedField<BoundaryKindField>(kind);
2781 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002782
2783 bool IsControlFlow() const OVERRIDE { return true; }
2784
2785 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002786 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002787
David Brazdild26a4112015-11-10 11:07:31 +00002788 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2789 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2790 }
2791
David Brazdilfc6a86a2015-06-26 10:33:45 +00002792 // Returns whether `handler` is among its exception handlers (non-zero index
2793 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002794 bool HasExceptionHandler(const HBasicBlock& handler) const {
2795 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002796 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002797 }
2798
2799 // If not present already, adds `handler` to its block's list of exception
2800 // handlers.
2801 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002802 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002803 GetBlock()->AddSuccessor(handler);
2804 }
2805 }
2806
Vladimir Markoa1de9182016-02-25 11:37:38 +00002807 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2808 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002809
David Brazdilffee3d32015-07-06 11:48:53 +01002810 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2811
David Brazdilfc6a86a2015-06-26 10:33:45 +00002812 DECLARE_INSTRUCTION(TryBoundary);
2813
2814 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002815 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2816 static constexpr size_t kFieldBoundaryKindSize =
2817 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2818 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2819 kFieldBoundaryKind + kFieldBoundaryKindSize;
2820 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2821 "Too many packed fields.");
2822 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002823
2824 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2825};
2826
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002827// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002828class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002829 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002830 // We set CanTriggerGC to prevent any intermediate address to be live
2831 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002832 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002833 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002834 SetRawInputAt(0, cond);
2835 }
2836
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002837 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002838 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002839 return true;
2840 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002841 bool NeedsEnvironment() const OVERRIDE { return true; }
2842 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002843
2844 DECLARE_INSTRUCTION(Deoptimize);
2845
2846 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002847 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2848};
2849
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002850// Represents the ArtMethod that was passed as a first argument to
2851// the method. It is used by instructions that depend on it, like
2852// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002853class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002854 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002855 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2856 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002857
2858 DECLARE_INSTRUCTION(CurrentMethod);
2859
2860 private:
2861 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2862};
2863
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002864// Fetches an ArtMethod from the virtual table or the interface method table
2865// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002866class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002867 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002868 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002869 kVTable,
2870 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002871 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002872 };
2873 HClassTableGet(HInstruction* cls,
2874 Primitive::Type type,
2875 TableKind kind,
2876 size_t index,
2877 uint32_t dex_pc)
2878 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002879 index_(index) {
2880 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002881 SetRawInputAt(0, cls);
2882 }
2883
2884 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002885 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002886 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002887 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002888 }
2889
Vladimir Markoa1de9182016-02-25 11:37:38 +00002890 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002891 size_t GetIndex() const { return index_; }
2892
2893 DECLARE_INSTRUCTION(ClassTableGet);
2894
2895 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002896 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2897 static constexpr size_t kFieldTableKindSize =
2898 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2899 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2900 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2901 "Too many packed fields.");
2902 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2903
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002904 // The index of the ArtMethod in the table.
2905 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002906
2907 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2908};
2909
Mark Mendellfe57faa2015-09-18 09:26:15 -04002910// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2911// have one successor for each entry in the switch table, and the final successor
2912// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002913class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002914 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002915 HPackedSwitch(int32_t start_value,
2916 uint32_t num_entries,
2917 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002918 uint32_t dex_pc = kNoDexPc)
2919 : HTemplateInstruction(SideEffects::None(), dex_pc),
2920 start_value_(start_value),
2921 num_entries_(num_entries) {
2922 SetRawInputAt(0, input);
2923 }
2924
2925 bool IsControlFlow() const OVERRIDE { return true; }
2926
2927 int32_t GetStartValue() const { return start_value_; }
2928
Vladimir Marko211c2112015-09-24 16:52:33 +01002929 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002930
2931 HBasicBlock* GetDefaultBlock() const {
2932 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002933 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002934 }
2935 DECLARE_INSTRUCTION(PackedSwitch);
2936
2937 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002938 const int32_t start_value_;
2939 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002940
2941 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2942};
2943
Roland Levillain88cb1752014-10-20 16:36:47 +01002944class HUnaryOperation : public HExpression<1> {
2945 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002946 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2947 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002948 SetRawInputAt(0, input);
2949 }
2950
2951 HInstruction* GetInput() const { return InputAt(0); }
2952 Primitive::Type GetResultType() const { return GetType(); }
2953
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002954 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002955 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002956 return true;
2957 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002958
Roland Levillain31dd3d62016-02-16 12:21:02 +00002959 // Try to statically evaluate `this` and return a HConstant
2960 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002961 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002962 HConstant* TryStaticEvaluation() const;
2963
2964 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002965 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2966 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002967 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2968 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002969
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002970 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002971
2972 private:
2973 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2974};
2975
Dave Allison20dfc792014-06-16 20:44:29 -07002976class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002977 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002978 HBinaryOperation(Primitive::Type result_type,
2979 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002980 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002981 SideEffects side_effects = SideEffects::None(),
2982 uint32_t dex_pc = kNoDexPc)
2983 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002984 SetRawInputAt(0, left);
2985 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002986 }
2987
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002988 HInstruction* GetLeft() const { return InputAt(0); }
2989 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002990 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002991
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002992 virtual bool IsCommutative() const { return false; }
2993
2994 // Put constant on the right.
2995 // Returns whether order is changed.
2996 bool OrderInputsWithConstantOnTheRight() {
2997 HInstruction* left = InputAt(0);
2998 HInstruction* right = InputAt(1);
2999 if (left->IsConstant() && !right->IsConstant()) {
3000 ReplaceInput(right, 0);
3001 ReplaceInput(left, 1);
3002 return true;
3003 }
3004 return false;
3005 }
3006
3007 // Order inputs by instruction id, but favor constant on the right side.
3008 // This helps GVN for commutative ops.
3009 void OrderInputs() {
3010 DCHECK(IsCommutative());
3011 HInstruction* left = InputAt(0);
3012 HInstruction* right = InputAt(1);
3013 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3014 return;
3015 }
3016 if (OrderInputsWithConstantOnTheRight()) {
3017 return;
3018 }
3019 // Order according to instruction id.
3020 if (left->GetId() > right->GetId()) {
3021 ReplaceInput(right, 0);
3022 ReplaceInput(left, 1);
3023 }
3024 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003025
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003026 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003027 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003028 return true;
3029 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003030
Roland Levillain31dd3d62016-02-16 12:21:02 +00003031 // Try to statically evaluate `this` and return a HConstant
3032 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003033 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003034 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003035
3036 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003037 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3038 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003039 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3040 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003041 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003042 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3043 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003044 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3045 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003046 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3047 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003048 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003049 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3050 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003051
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003052 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003053 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003054 HConstant* GetConstantRight() const;
3055
3056 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003057 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003058 HInstruction* GetLeastConstantLeft() const;
3059
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003060 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003061
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003062 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003063 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3064};
3065
Mark Mendellc4701932015-04-10 13:18:51 -04003066// The comparison bias applies for floating point operations and indicates how NaN
3067// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003068enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003069 kNoBias, // bias is not applicable (i.e. for long operation)
3070 kGtBias, // return 1 for NaN comparisons
3071 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003072 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003073};
3074
Roland Levillain31dd3d62016-02-16 12:21:02 +00003075std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3076
Dave Allison20dfc792014-06-16 20:44:29 -07003077class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003078 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003079 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003080 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3081 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3082 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003083
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003084 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003085 // `instruction`, and disregard moves in between.
3086 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003087
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003088 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003089
3090 virtual IfCondition GetCondition() const = 0;
3091
Mark Mendellc4701932015-04-10 13:18:51 -04003092 virtual IfCondition GetOppositeCondition() const = 0;
3093
Vladimir Markoa1de9182016-02-25 11:37:38 +00003094 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003095 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3096
Vladimir Markoa1de9182016-02-25 11:37:38 +00003097 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3098 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003099
Vladimir Marko372f10e2016-05-17 16:30:10 +01003100 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003101 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003102 }
3103
Roland Levillain4fa13f62015-07-06 18:11:54 +01003104 bool IsFPConditionTrueIfNaN() 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 == kCondNE) {
3108 return true;
3109 } else if (if_cond == kCondEQ) {
3110 return false;
3111 }
3112 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003113 }
3114
3115 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003116 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003117 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003118 if (if_cond == kCondEQ) {
3119 return true;
3120 } else if (if_cond == kCondNE) {
3121 return false;
3122 }
3123 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003124 }
3125
Roland Levillain31dd3d62016-02-16 12:21:02 +00003126 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003127 // Needed if we merge a HCompare into a HCondition.
3128 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3129 static constexpr size_t kFieldComparisonBiasSize =
3130 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3131 static constexpr size_t kNumberOfConditionPackedBits =
3132 kFieldComparisonBias + kFieldComparisonBiasSize;
3133 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3134 using ComparisonBiasField =
3135 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3136
Roland Levillain31dd3d62016-02-16 12:21:02 +00003137 template <typename T>
3138 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3139
3140 template <typename T>
3141 int32_t CompareFP(T x, T y) const {
3142 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3143 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3144 // Handle the bias.
3145 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3146 }
3147
3148 // Return an integer constant containing the result of a condition evaluated at compile time.
3149 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3150 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3151 }
3152
Dave Allison20dfc792014-06-16 20:44:29 -07003153 private:
3154 DISALLOW_COPY_AND_ASSIGN(HCondition);
3155};
3156
3157// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003158class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003159 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003160 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3161 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003162
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003163 bool IsCommutative() const OVERRIDE { return true; }
3164
Vladimir Marko9e23df52015-11-10 17:14:35 +00003165 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3166 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003167 return MakeConstantCondition(true, GetDexPc());
3168 }
3169 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3170 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3171 }
3172 // In the following Evaluate methods, a HCompare instruction has
3173 // been merged into this HEqual instruction; evaluate it as
3174 // `Compare(x, y) == 0`.
3175 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3176 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3177 GetDexPc());
3178 }
3179 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3180 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3181 }
3182 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3183 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003184 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003185
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003186 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003187
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003188 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003189 return kCondEQ;
3190 }
3191
Mark Mendellc4701932015-04-10 13:18:51 -04003192 IfCondition GetOppositeCondition() const OVERRIDE {
3193 return kCondNE;
3194 }
3195
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003196 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003197 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003198
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003199 DISALLOW_COPY_AND_ASSIGN(HEqual);
3200};
3201
Vladimir Markofcb503c2016-05-18 12:48:17 +01003202class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003203 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003204 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3205 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003206
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003207 bool IsCommutative() const OVERRIDE { return true; }
3208
Vladimir Marko9e23df52015-11-10 17:14:35 +00003209 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3210 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003211 return MakeConstantCondition(false, GetDexPc());
3212 }
3213 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3214 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3215 }
3216 // In the following Evaluate methods, a HCompare instruction has
3217 // been merged into this HNotEqual instruction; evaluate it as
3218 // `Compare(x, y) != 0`.
3219 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3220 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3221 }
3222 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3223 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3224 }
3225 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3226 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003227 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003228
Dave Allison20dfc792014-06-16 20:44:29 -07003229 DECLARE_INSTRUCTION(NotEqual);
3230
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003231 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003232 return kCondNE;
3233 }
3234
Mark Mendellc4701932015-04-10 13:18:51 -04003235 IfCondition GetOppositeCondition() const OVERRIDE {
3236 return kCondEQ;
3237 }
3238
Dave Allison20dfc792014-06-16 20:44:29 -07003239 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003240 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003241
Dave Allison20dfc792014-06-16 20:44:29 -07003242 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3243};
3244
Vladimir Markofcb503c2016-05-18 12:48:17 +01003245class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003246 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003247 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3248 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003249
Roland Levillain9867bc72015-08-05 10:21:34 +01003250 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003251 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003252 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003253 // In the following Evaluate methods, a HCompare instruction has
3254 // been merged into this HLessThan instruction; evaluate it as
3255 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003256 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003257 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3258 }
3259 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3260 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3261 }
3262 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3263 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003264 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003265
Dave Allison20dfc792014-06-16 20:44:29 -07003266 DECLARE_INSTRUCTION(LessThan);
3267
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003268 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003269 return kCondLT;
3270 }
3271
Mark Mendellc4701932015-04-10 13:18:51 -04003272 IfCondition GetOppositeCondition() const OVERRIDE {
3273 return kCondGE;
3274 }
3275
Dave Allison20dfc792014-06-16 20:44:29 -07003276 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003277 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003278
Dave Allison20dfc792014-06-16 20:44:29 -07003279 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3280};
3281
Vladimir Markofcb503c2016-05-18 12:48:17 +01003282class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003283 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003284 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3285 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003286
Roland Levillain9867bc72015-08-05 10:21:34 +01003287 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003288 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003289 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003290 // In the following Evaluate methods, a HCompare instruction has
3291 // been merged into this HLessThanOrEqual instruction; evaluate it as
3292 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003293 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003294 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3295 }
3296 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3297 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3298 }
3299 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3300 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003301 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003302
Dave Allison20dfc792014-06-16 20:44:29 -07003303 DECLARE_INSTRUCTION(LessThanOrEqual);
3304
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003305 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003306 return kCondLE;
3307 }
3308
Mark Mendellc4701932015-04-10 13:18:51 -04003309 IfCondition GetOppositeCondition() const OVERRIDE {
3310 return kCondGT;
3311 }
3312
Dave Allison20dfc792014-06-16 20:44:29 -07003313 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003314 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003315
Dave Allison20dfc792014-06-16 20:44:29 -07003316 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3317};
3318
Vladimir Markofcb503c2016-05-18 12:48:17 +01003319class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003320 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003321 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3322 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003323
Roland Levillain9867bc72015-08-05 10:21:34 +01003324 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003325 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003326 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003327 // In the following Evaluate methods, a HCompare instruction has
3328 // been merged into this HGreaterThan instruction; evaluate it as
3329 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003330 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003331 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3332 }
3333 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3334 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3335 }
3336 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3337 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003338 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003339
Dave Allison20dfc792014-06-16 20:44:29 -07003340 DECLARE_INSTRUCTION(GreaterThan);
3341
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003342 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003343 return kCondGT;
3344 }
3345
Mark Mendellc4701932015-04-10 13:18:51 -04003346 IfCondition GetOppositeCondition() const OVERRIDE {
3347 return kCondLE;
3348 }
3349
Dave Allison20dfc792014-06-16 20:44:29 -07003350 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003351 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003352
Dave Allison20dfc792014-06-16 20:44:29 -07003353 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3354};
3355
Vladimir Markofcb503c2016-05-18 12:48:17 +01003356class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003357 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003358 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3359 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003360
Roland Levillain9867bc72015-08-05 10:21:34 +01003361 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003362 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003363 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003364 // In the following Evaluate methods, a HCompare instruction has
3365 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3366 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003367 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003368 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3369 }
3370 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3371 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3372 }
3373 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3374 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003375 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003376
Dave Allison20dfc792014-06-16 20:44:29 -07003377 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3378
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003379 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003380 return kCondGE;
3381 }
3382
Mark Mendellc4701932015-04-10 13:18:51 -04003383 IfCondition GetOppositeCondition() const OVERRIDE {
3384 return kCondLT;
3385 }
3386
Dave Allison20dfc792014-06-16 20:44:29 -07003387 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003388 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003389
Dave Allison20dfc792014-06-16 20:44:29 -07003390 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3391};
3392
Vladimir Markofcb503c2016-05-18 12:48:17 +01003393class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003394 public:
3395 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3396 : HCondition(first, second, dex_pc) {}
3397
3398 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003399 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003400 }
3401 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003402 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3403 }
3404 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3405 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3406 LOG(FATAL) << DebugName() << " is not defined for float values";
3407 UNREACHABLE();
3408 }
3409 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3410 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3411 LOG(FATAL) << DebugName() << " is not defined for double values";
3412 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003413 }
3414
3415 DECLARE_INSTRUCTION(Below);
3416
3417 IfCondition GetCondition() const OVERRIDE {
3418 return kCondB;
3419 }
3420
3421 IfCondition GetOppositeCondition() const OVERRIDE {
3422 return kCondAE;
3423 }
3424
3425 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003426 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003427 return MakeUnsigned(x) < MakeUnsigned(y);
3428 }
Aart Bike9f37602015-10-09 11:15:55 -07003429
3430 DISALLOW_COPY_AND_ASSIGN(HBelow);
3431};
3432
Vladimir Markofcb503c2016-05-18 12:48:17 +01003433class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003434 public:
3435 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3436 : HCondition(first, second, dex_pc) {}
3437
3438 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003439 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003440 }
3441 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003442 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3443 }
3444 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3445 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3446 LOG(FATAL) << DebugName() << " is not defined for float values";
3447 UNREACHABLE();
3448 }
3449 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3450 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3451 LOG(FATAL) << DebugName() << " is not defined for double values";
3452 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003453 }
3454
3455 DECLARE_INSTRUCTION(BelowOrEqual);
3456
3457 IfCondition GetCondition() const OVERRIDE {
3458 return kCondBE;
3459 }
3460
3461 IfCondition GetOppositeCondition() const OVERRIDE {
3462 return kCondA;
3463 }
3464
3465 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003466 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003467 return MakeUnsigned(x) <= MakeUnsigned(y);
3468 }
Aart Bike9f37602015-10-09 11:15:55 -07003469
3470 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3471};
3472
Vladimir Markofcb503c2016-05-18 12:48:17 +01003473class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003474 public:
3475 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3476 : HCondition(first, second, dex_pc) {}
3477
3478 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003479 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003480 }
3481 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003482 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3483 }
3484 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3485 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3486 LOG(FATAL) << DebugName() << " is not defined for float values";
3487 UNREACHABLE();
3488 }
3489 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3490 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3491 LOG(FATAL) << DebugName() << " is not defined for double values";
3492 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003493 }
3494
3495 DECLARE_INSTRUCTION(Above);
3496
3497 IfCondition GetCondition() const OVERRIDE {
3498 return kCondA;
3499 }
3500
3501 IfCondition GetOppositeCondition() const OVERRIDE {
3502 return kCondBE;
3503 }
3504
3505 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003506 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003507 return MakeUnsigned(x) > MakeUnsigned(y);
3508 }
Aart Bike9f37602015-10-09 11:15:55 -07003509
3510 DISALLOW_COPY_AND_ASSIGN(HAbove);
3511};
3512
Vladimir Markofcb503c2016-05-18 12:48:17 +01003513class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003514 public:
3515 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3516 : HCondition(first, second, dex_pc) {}
3517
3518 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003519 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003520 }
3521 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003522 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3523 }
3524 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3525 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3526 LOG(FATAL) << DebugName() << " is not defined for float values";
3527 UNREACHABLE();
3528 }
3529 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3530 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3531 LOG(FATAL) << DebugName() << " is not defined for double values";
3532 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003533 }
3534
3535 DECLARE_INSTRUCTION(AboveOrEqual);
3536
3537 IfCondition GetCondition() const OVERRIDE {
3538 return kCondAE;
3539 }
3540
3541 IfCondition GetOppositeCondition() const OVERRIDE {
3542 return kCondB;
3543 }
3544
3545 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003546 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003547 return MakeUnsigned(x) >= MakeUnsigned(y);
3548 }
Aart Bike9f37602015-10-09 11:15:55 -07003549
3550 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3551};
Dave Allison20dfc792014-06-16 20:44:29 -07003552
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003553// Instruction to check how two inputs compare to each other.
3554// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003555class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003556 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003557 // Note that `comparison_type` is the type of comparison performed
3558 // between the comparison's inputs, not the type of the instantiated
3559 // HCompare instruction (which is always Primitive::kPrimInt).
3560 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003561 HInstruction* first,
3562 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003563 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003564 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003565 : HBinaryOperation(Primitive::kPrimInt,
3566 first,
3567 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003568 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003569 dex_pc) {
3570 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003571 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3572 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003573 }
3574
Roland Levillain9867bc72015-08-05 10:21:34 +01003575 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003576 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3577
3578 template <typename T>
3579 int32_t ComputeFP(T x, T y) const {
3580 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3581 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3582 // Handle the bias.
3583 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3584 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003585
Roland Levillain9867bc72015-08-05 10:21:34 +01003586 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003587 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3588 // reach this code path when processing a freshly built HIR
3589 // graph. However HCompare integer instructions can be synthesized
3590 // by the instruction simplifier to implement IntegerCompare and
3591 // IntegerSignum intrinsics, so we have to handle this case.
3592 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003593 }
3594 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003595 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3596 }
3597 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3598 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3599 }
3600 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3601 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003602 }
3603
Vladimir Marko372f10e2016-05-17 16:30:10 +01003604 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003605 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003606 }
3607
Vladimir Markoa1de9182016-02-25 11:37:38 +00003608 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003609
Roland Levillain31dd3d62016-02-16 12:21:02 +00003610 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003611 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003612 bool IsGtBias() const {
3613 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003614 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003615 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003616
Roland Levillain1693a1f2016-03-15 14:57:31 +00003617 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3618 // Comparisons do not require a runtime call in any back end.
3619 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003620 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003621
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003622 DECLARE_INSTRUCTION(Compare);
3623
Roland Levillain31dd3d62016-02-16 12:21:02 +00003624 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003625 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3626 static constexpr size_t kFieldComparisonBiasSize =
3627 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3628 static constexpr size_t kNumberOfComparePackedBits =
3629 kFieldComparisonBias + kFieldComparisonBiasSize;
3630 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3631 using ComparisonBiasField =
3632 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3633
Roland Levillain31dd3d62016-02-16 12:21:02 +00003634 // Return an integer constant containing the result of a comparison evaluated at compile time.
3635 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3636 DCHECK(value == -1 || value == 0 || value == 1) << value;
3637 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3638 }
3639
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003640 private:
3641 DISALLOW_COPY_AND_ASSIGN(HCompare);
3642};
3643
Vladimir Markofcb503c2016-05-18 12:48:17 +01003644class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003645 public:
3646 HNewInstance(HInstruction* cls,
3647 HCurrentMethod* current_method,
3648 uint32_t dex_pc,
3649 uint16_t type_index,
3650 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003651 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003652 bool finalizable,
3653 QuickEntrypointEnum entrypoint)
3654 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3655 type_index_(type_index),
3656 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003657 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003658 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003659 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003660 SetRawInputAt(0, cls);
3661 SetRawInputAt(1, current_method);
3662 }
3663
3664 uint16_t GetTypeIndex() const { return type_index_; }
3665 const DexFile& GetDexFile() const { return dex_file_; }
3666
3667 // Calls runtime so needs an environment.
3668 bool NeedsEnvironment() const OVERRIDE { return true; }
3669
Mingyao Yang062157f2016-03-02 10:15:36 -08003670 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3671 bool CanThrow() const OVERRIDE { return true; }
3672
3673 // Needs to call into runtime to make sure it's instantiable/accessible.
3674 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003675
Vladimir Markoa1de9182016-02-25 11:37:38 +00003676 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003677
3678 bool CanBeNull() const OVERRIDE { return false; }
3679
3680 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3681
3682 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3683 entrypoint_ = entrypoint;
3684 }
3685
3686 bool IsStringAlloc() const;
3687
3688 DECLARE_INSTRUCTION(NewInstance);
3689
3690 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003691 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3692 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003693 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3694 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3695 "Too many packed fields.");
3696
David Brazdil6de19382016-01-08 17:37:10 +00003697 const uint16_t type_index_;
3698 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003699 QuickEntrypointEnum entrypoint_;
3700
3701 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3702};
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
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004047 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004048 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004049 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004050 }
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
Vladimir Markoaad75c62016-10-03 08:46:48 +00005663 // Load from an entry in the .bss section using a PC-relative load.
5664 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5665 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005666
5667 // Load from resolved strings array accessed through the class loaded from
5668 // the compiled method's own ArtMethod*. This is the default access type when
5669 // all other types are unavailable.
5670 kDexCacheViaMethod,
5671
5672 kLast = kDexCacheViaMethod
5673 };
5674
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005675 HLoadString(HCurrentMethod* current_method,
5676 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005677 const DexFile& dex_file,
5678 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005679 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5680 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005681 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005682 SetPackedFlag<kFlagIsInDexCache>(false);
5683 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005684 load_data_.dex_file_ = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005685 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005686
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005687 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5688 DCHECK(HasAddress(load_kind));
5689 load_data_.address = address;
5690 SetLoadKindInternal(load_kind);
5691 }
5692
5693 void SetLoadKindWithStringReference(LoadKind load_kind,
5694 const DexFile& dex_file,
5695 uint32_t string_index) {
5696 DCHECK(HasStringReference(load_kind));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005697 load_data_.dex_file_ = &dex_file;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005698 string_index_ = string_index;
5699 SetLoadKindInternal(load_kind);
5700 }
5701
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005702 LoadKind GetLoadKind() const {
5703 return GetPackedField<LoadKindField>();
5704 }
5705
5706 const DexFile& GetDexFile() const;
5707
5708 uint32_t GetStringIndex() const {
5709 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5710 return string_index_;
5711 }
5712
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005713 uint64_t GetAddress() const {
5714 DCHECK(HasAddress(GetLoadKind()));
5715 return load_data_.address;
5716 }
5717
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005718 bool CanBeMoved() const OVERRIDE { return true; }
5719
Vladimir Marko372f10e2016-05-17 16:30:10 +01005720 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005721
5722 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5723
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005724 // Will call the runtime if we need to load the string through
5725 // the dex cache and the string is not guaranteed to be there yet.
5726 bool NeedsEnvironment() const OVERRIDE {
5727 LoadKind load_kind = GetLoadKind();
5728 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5729 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5730 load_kind == LoadKind::kBootImageAddress) {
5731 return false;
5732 }
5733 return !IsInDexCache();
5734 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005735
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005736 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5737 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5738 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005739
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005740 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005741 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005742
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005743 static SideEffects SideEffectsForArchRuntimeCalls() {
5744 return SideEffects::CanTriggerGC();
5745 }
5746
Vladimir Markoa1de9182016-02-25 11:37:38 +00005747 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5748
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005749 void MarkInDexCache() {
5750 SetPackedFlag<kFlagIsInDexCache>(true);
5751 DCHECK(!NeedsEnvironment());
5752 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005753 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005754 }
5755
Vladimir Marko372f10e2016-05-17 16:30:10 +01005756 void AddSpecialInput(HInstruction* special_input);
5757
5758 using HInstruction::GetInputRecords; // Keep the const version visible.
5759 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5760 return ArrayRef<HUserRecord<HInstruction*>>(
5761 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005762 }
5763
Vladimir Marko372f10e2016-05-17 16:30:10 +01005764 Primitive::Type GetType() const OVERRIDE {
5765 return Primitive::kPrimNot;
5766 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005767
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005768 DECLARE_INSTRUCTION(LoadString);
5769
5770 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005771 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005772 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5773 static constexpr size_t kFieldLoadKindSize =
5774 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5775 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005776 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005777 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005778
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005779 static bool HasStringReference(LoadKind load_kind) {
5780 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5781 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005782 load_kind == LoadKind::kBssEntry ||
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005783 load_kind == LoadKind::kDexCacheViaMethod;
5784 }
5785
5786 static bool HasAddress(LoadKind load_kind) {
5787 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5788 }
5789
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005790 void SetLoadKindInternal(LoadKind load_kind);
5791
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005792 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5793 // For other load kinds it's empty or possibly some architecture-specific instruction
5794 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005795 HUserRecord<HInstruction*> special_input_;
5796
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005797 // String index serves also as the hash code and it's also needed for slow-paths,
5798 // so it must not be overwritten with other load data.
5799 uint32_t string_index_;
5800
5801 union {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005802 const DexFile* dex_file_; // For string reference.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005803 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5804 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005805
5806 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5807};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005808std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5809
5810// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5811inline const DexFile& HLoadString::GetDexFile() const {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005812 DCHECK(HasStringReference(GetLoadKind())) << GetLoadKind();
5813 return *load_data_.dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005814}
5815
5816// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5817inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005818 // The special input is used for PC-relative loads on some architectures,
5819 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005820 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005821 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005822 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5823 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005824 // HLoadString::GetInputRecords() returns an empty array at this point,
5825 // so use the GetInputRecords() from the base class to set the input record.
5826 DCHECK(special_input_.GetInstruction() == nullptr);
5827 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005828 special_input->AddUseAt(this, 0);
5829}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005830
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005831/**
5832 * Performs an initialization check on its Class object input.
5833 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005834class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005835 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005836 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005837 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005838 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005839 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5840 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005841 SetRawInputAt(0, constant);
5842 }
5843
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005844 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005845 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005846 return true;
5847 }
5848
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005849 bool NeedsEnvironment() const OVERRIDE {
5850 // May call runtime to initialize the class.
5851 return true;
5852 }
5853
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005854 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005855
5856 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5857
5858 DECLARE_INSTRUCTION(ClinitCheck);
5859
5860 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005861 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5862};
5863
Vladimir Markofcb503c2016-05-18 12:48:17 +01005864class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005865 public:
5866 HStaticFieldGet(HInstruction* cls,
5867 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005868 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005869 bool is_volatile,
5870 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005871 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005872 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005873 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005874 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005875 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005876 field_info_(field_offset,
5877 field_type,
5878 is_volatile,
5879 field_idx,
5880 declaring_class_def_index,
5881 dex_file,
5882 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005883 SetRawInputAt(0, cls);
5884 }
5885
Calin Juravle52c48962014-12-16 17:02:57 +00005886
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005887 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005888
Vladimir Marko372f10e2016-05-17 16:30:10 +01005889 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5890 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005891 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005892 }
5893
5894 size_t ComputeHashCode() const OVERRIDE {
5895 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5896 }
5897
Calin Juravle52c48962014-12-16 17:02:57 +00005898 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005899 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5900 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005901 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005902
5903 DECLARE_INSTRUCTION(StaticFieldGet);
5904
5905 private:
5906 const FieldInfo field_info_;
5907
5908 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5909};
5910
Vladimir Markofcb503c2016-05-18 12:48:17 +01005911class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005912 public:
5913 HStaticFieldSet(HInstruction* cls,
5914 HInstruction* value,
5915 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005916 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005917 bool is_volatile,
5918 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005919 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005920 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005921 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005922 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005923 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005924 field_info_(field_offset,
5925 field_type,
5926 is_volatile,
5927 field_idx,
5928 declaring_class_def_index,
5929 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005930 dex_cache) {
5931 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005932 SetRawInputAt(0, cls);
5933 SetRawInputAt(1, value);
5934 }
5935
Calin Juravle52c48962014-12-16 17:02:57 +00005936 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005937 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5938 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005939 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005940
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005941 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005942 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5943 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005944
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005945 DECLARE_INSTRUCTION(StaticFieldSet);
5946
5947 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005948 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5949 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5950 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5951 "Too many packed fields.");
5952
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005953 const FieldInfo field_info_;
5954
5955 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5956};
5957
Vladimir Markofcb503c2016-05-18 12:48:17 +01005958class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005959 public:
5960 HUnresolvedInstanceFieldGet(HInstruction* obj,
5961 Primitive::Type field_type,
5962 uint32_t field_index,
5963 uint32_t dex_pc)
5964 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5965 field_index_(field_index) {
5966 SetRawInputAt(0, obj);
5967 }
5968
5969 bool NeedsEnvironment() const OVERRIDE { return true; }
5970 bool CanThrow() const OVERRIDE { return true; }
5971
5972 Primitive::Type GetFieldType() const { return GetType(); }
5973 uint32_t GetFieldIndex() const { return field_index_; }
5974
5975 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5976
5977 private:
5978 const uint32_t field_index_;
5979
5980 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5981};
5982
Vladimir Markofcb503c2016-05-18 12:48:17 +01005983class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005984 public:
5985 HUnresolvedInstanceFieldSet(HInstruction* obj,
5986 HInstruction* value,
5987 Primitive::Type field_type,
5988 uint32_t field_index,
5989 uint32_t dex_pc)
5990 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005991 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005992 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005993 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005994 SetRawInputAt(0, obj);
5995 SetRawInputAt(1, value);
5996 }
5997
5998 bool NeedsEnvironment() const OVERRIDE { return true; }
5999 bool CanThrow() const OVERRIDE { return true; }
6000
Vladimir Markoa1de9182016-02-25 11:37:38 +00006001 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006002 uint32_t GetFieldIndex() const { return field_index_; }
6003
6004 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6005
6006 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006007 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6008 static constexpr size_t kFieldFieldTypeSize =
6009 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6010 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6011 kFieldFieldType + kFieldFieldTypeSize;
6012 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6013 "Too many packed fields.");
6014 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6015
Calin Juravlee460d1d2015-09-29 04:52:17 +01006016 const uint32_t field_index_;
6017
6018 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6019};
6020
Vladimir Markofcb503c2016-05-18 12:48:17 +01006021class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006022 public:
6023 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6024 uint32_t field_index,
6025 uint32_t dex_pc)
6026 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6027 field_index_(field_index) {
6028 }
6029
6030 bool NeedsEnvironment() const OVERRIDE { return true; }
6031 bool CanThrow() const OVERRIDE { return true; }
6032
6033 Primitive::Type GetFieldType() const { return GetType(); }
6034 uint32_t GetFieldIndex() const { return field_index_; }
6035
6036 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6037
6038 private:
6039 const uint32_t field_index_;
6040
6041 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6042};
6043
Vladimir Markofcb503c2016-05-18 12:48:17 +01006044class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006045 public:
6046 HUnresolvedStaticFieldSet(HInstruction* value,
6047 Primitive::Type field_type,
6048 uint32_t field_index,
6049 uint32_t dex_pc)
6050 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006051 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006052 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006053 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006054 SetRawInputAt(0, value);
6055 }
6056
6057 bool NeedsEnvironment() const OVERRIDE { return true; }
6058 bool CanThrow() const OVERRIDE { return true; }
6059
Vladimir Markoa1de9182016-02-25 11:37:38 +00006060 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006061 uint32_t GetFieldIndex() const { return field_index_; }
6062
6063 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6064
6065 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006066 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6067 static constexpr size_t kFieldFieldTypeSize =
6068 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6069 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6070 kFieldFieldType + kFieldFieldTypeSize;
6071 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6072 "Too many packed fields.");
6073 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6074
Calin Juravlee460d1d2015-09-29 04:52:17 +01006075 const uint32_t field_index_;
6076
6077 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6078};
6079
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006080// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006081class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006082 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006083 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6084 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006085
David Brazdilbbd733e2015-08-18 17:48:17 +01006086 bool CanBeNull() const OVERRIDE { return false; }
6087
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006088 DECLARE_INSTRUCTION(LoadException);
6089
6090 private:
6091 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6092};
6093
David Brazdilcb1c0552015-08-04 16:22:25 +01006094// Implicit part of move-exception which clears thread-local exception storage.
6095// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006096class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006097 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006098 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6099 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006100
6101 DECLARE_INSTRUCTION(ClearException);
6102
6103 private:
6104 DISALLOW_COPY_AND_ASSIGN(HClearException);
6105};
6106
Vladimir Markofcb503c2016-05-18 12:48:17 +01006107class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006108 public:
6109 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006110 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006111 SetRawInputAt(0, exception);
6112 }
6113
6114 bool IsControlFlow() const OVERRIDE { return true; }
6115
6116 bool NeedsEnvironment() const OVERRIDE { return true; }
6117
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006118 bool CanThrow() const OVERRIDE { return true; }
6119
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006120
6121 DECLARE_INSTRUCTION(Throw);
6122
6123 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006124 DISALLOW_COPY_AND_ASSIGN(HThrow);
6125};
6126
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006127/**
6128 * Implementation strategies for the code generator of a HInstanceOf
6129 * or `HCheckCast`.
6130 */
6131enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006132 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006133 kExactCheck, // Can do a single class compare.
6134 kClassHierarchyCheck, // Can just walk the super class chain.
6135 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6136 kInterfaceCheck, // No optimization yet when checking against an interface.
6137 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006138 kArrayCheck, // No optimization yet when checking against a generic array.
6139 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006140};
6141
Roland Levillain86503782016-02-11 19:07:30 +00006142std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6143
Vladimir Markofcb503c2016-05-18 12:48:17 +01006144class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006145 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006146 HInstanceOf(HInstruction* object,
6147 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006148 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006149 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006150 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006151 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006152 dex_pc) {
6153 SetPackedField<TypeCheckKindField>(check_kind);
6154 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006155 SetRawInputAt(0, object);
6156 SetRawInputAt(1, constant);
6157 }
6158
6159 bool CanBeMoved() const OVERRIDE { return true; }
6160
Vladimir Marko372f10e2016-05-17 16:30:10 +01006161 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006162 return true;
6163 }
6164
6165 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006166 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006167 }
6168
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006169 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006170 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6171 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6172 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6173 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006174
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006175 static bool CanCallRuntime(TypeCheckKind check_kind) {
6176 // Mips currently does runtime calls for any other checks.
6177 return check_kind != TypeCheckKind::kExactCheck;
6178 }
6179
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006180 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006181 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006182 }
6183
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006184 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006185
6186 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006187 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6188 static constexpr size_t kFieldTypeCheckKindSize =
6189 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6190 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6191 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6192 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6193 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006194
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006195 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6196};
6197
Vladimir Markofcb503c2016-05-18 12:48:17 +01006198class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006199 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006200 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006201 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006202 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6203 SetPackedFlag<kFlagUpperCanBeNull>(true);
6204 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006205 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006206 SetRawInputAt(0, input);
6207 }
6208
David Brazdilf5552582015-12-27 13:36:12 +00006209 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006210 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006211 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006212 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006213
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006214 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006215 DCHECK(GetUpperCanBeNull() || !can_be_null);
6216 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006217 }
6218
Vladimir Markoa1de9182016-02-25 11:37:38 +00006219 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006220
Calin Juravleb1498f62015-02-16 13:13:29 +00006221 DECLARE_INSTRUCTION(BoundType);
6222
6223 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006224 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6225 // is false then CanBeNull() cannot be true).
6226 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6227 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6228 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6229 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6230
Calin Juravleb1498f62015-02-16 13:13:29 +00006231 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006232 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6233 // It is used to bound the type in cases like:
6234 // if (x instanceof ClassX) {
6235 // // uper_bound_ will be ClassX
6236 // }
David Brazdilf5552582015-12-27 13:36:12 +00006237 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006238
6239 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6240};
6241
Vladimir Markofcb503c2016-05-18 12:48:17 +01006242class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006243 public:
6244 HCheckCast(HInstruction* object,
6245 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006246 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006247 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006248 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6249 SetPackedField<TypeCheckKindField>(check_kind);
6250 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006251 SetRawInputAt(0, object);
6252 SetRawInputAt(1, constant);
6253 }
6254
6255 bool CanBeMoved() const OVERRIDE { return true; }
6256
Vladimir Marko372f10e2016-05-17 16:30:10 +01006257 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006258 return true;
6259 }
6260
6261 bool NeedsEnvironment() const OVERRIDE {
6262 // Instruction may throw a CheckCastError.
6263 return true;
6264 }
6265
6266 bool CanThrow() const OVERRIDE { return true; }
6267
Vladimir Markoa1de9182016-02-25 11:37:38 +00006268 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6269 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6270 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6271 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006272
6273 DECLARE_INSTRUCTION(CheckCast);
6274
6275 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006276 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6277 static constexpr size_t kFieldTypeCheckKindSize =
6278 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6279 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6280 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6281 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6282 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006283
6284 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006285};
6286
Andreas Gampe26de38b2016-07-27 17:53:11 -07006287/**
6288 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6289 * @details We define the combined barrier types that are actually required
6290 * by the Java Memory Model, rather than using exactly the terminology from
6291 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6292 * primitives. Note that the JSR-133 cookbook generally does not deal with
6293 * store atomicity issues, and the recipes there are not always entirely sufficient.
6294 * The current recipe is as follows:
6295 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6296 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6297 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6298 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6299 * class has final fields.
6300 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6301 * store-to-memory instructions. Only generated together with non-temporal stores.
6302 */
6303enum MemBarrierKind {
6304 kAnyStore,
6305 kLoadAny,
6306 kStoreStore,
6307 kAnyAny,
6308 kNTStoreStore,
6309 kLastBarrierKind = kNTStoreStore
6310};
6311std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6312
Vladimir Markofcb503c2016-05-18 12:48:17 +01006313class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006314 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006315 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006316 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006317 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6318 SetPackedField<BarrierKindField>(barrier_kind);
6319 }
Calin Juravle27df7582015-04-17 19:12:31 +01006320
Vladimir Markoa1de9182016-02-25 11:37:38 +00006321 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006322
6323 DECLARE_INSTRUCTION(MemoryBarrier);
6324
6325 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006326 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6327 static constexpr size_t kFieldBarrierKindSize =
6328 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6329 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6330 kFieldBarrierKind + kFieldBarrierKindSize;
6331 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6332 "Too many packed fields.");
6333 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006334
6335 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6336};
6337
Vladimir Markofcb503c2016-05-18 12:48:17 +01006338class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006339 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006340 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006341 kEnter,
6342 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006343 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006344 };
6345
6346 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006347 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006348 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6349 dex_pc) {
6350 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006351 SetRawInputAt(0, object);
6352 }
6353
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006354 // Instruction may go into runtime, so we need an environment.
6355 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006356
6357 bool CanThrow() const OVERRIDE {
6358 // Verifier guarantees that monitor-exit cannot throw.
6359 // This is important because it allows the HGraphBuilder to remove
6360 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6361 return IsEnter();
6362 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006363
Vladimir Markoa1de9182016-02-25 11:37:38 +00006364 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6365 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006366
6367 DECLARE_INSTRUCTION(MonitorOperation);
6368
Calin Juravle52c48962014-12-16 17:02:57 +00006369 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006370 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6371 static constexpr size_t kFieldOperationKindSize =
6372 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6373 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6374 kFieldOperationKind + kFieldOperationKindSize;
6375 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6376 "Too many packed fields.");
6377 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006378
6379 private:
6380 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6381};
6382
Vladimir Markofcb503c2016-05-18 12:48:17 +01006383class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006384 public:
6385 HSelect(HInstruction* condition,
6386 HInstruction* true_value,
6387 HInstruction* false_value,
6388 uint32_t dex_pc)
6389 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6390 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6391
6392 // First input must be `true_value` or `false_value` to allow codegens to
6393 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6394 // that architectures which implement HSelect as a conditional move also
6395 // will not need to invert the condition.
6396 SetRawInputAt(0, false_value);
6397 SetRawInputAt(1, true_value);
6398 SetRawInputAt(2, condition);
6399 }
6400
6401 HInstruction* GetFalseValue() const { return InputAt(0); }
6402 HInstruction* GetTrueValue() const { return InputAt(1); }
6403 HInstruction* GetCondition() const { return InputAt(2); }
6404
6405 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006406 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6407 return true;
6408 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006409
6410 bool CanBeNull() const OVERRIDE {
6411 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6412 }
6413
6414 DECLARE_INSTRUCTION(Select);
6415
6416 private:
6417 DISALLOW_COPY_AND_ASSIGN(HSelect);
6418};
6419
Vladimir Markof9f64412015-09-02 14:05:49 +01006420class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006421 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006422 MoveOperands(Location source,
6423 Location destination,
6424 Primitive::Type type,
6425 HInstruction* instruction)
6426 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006427
6428 Location GetSource() const { return source_; }
6429 Location GetDestination() const { return destination_; }
6430
6431 void SetSource(Location value) { source_ = value; }
6432 void SetDestination(Location value) { destination_ = value; }
6433
6434 // The parallel move resolver marks moves as "in-progress" by clearing the
6435 // destination (but not the source).
6436 Location MarkPending() {
6437 DCHECK(!IsPending());
6438 Location dest = destination_;
6439 destination_ = Location::NoLocation();
6440 return dest;
6441 }
6442
6443 void ClearPending(Location dest) {
6444 DCHECK(IsPending());
6445 destination_ = dest;
6446 }
6447
6448 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006449 DCHECK(source_.IsValid() || destination_.IsInvalid());
6450 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006451 }
6452
6453 // True if this blocks a move from the given location.
6454 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006455 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006456 }
6457
6458 // A move is redundant if it's been eliminated, if its source and
6459 // destination are the same, or if its destination is unneeded.
6460 bool IsRedundant() const {
6461 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6462 }
6463
6464 // We clear both operands to indicate move that's been eliminated.
6465 void Eliminate() {
6466 source_ = destination_ = Location::NoLocation();
6467 }
6468
6469 bool IsEliminated() const {
6470 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6471 return source_.IsInvalid();
6472 }
6473
Alexey Frunze4dda3372015-06-01 18:31:49 -07006474 Primitive::Type GetType() const { return type_; }
6475
Nicolas Geoffray90218252015-04-15 11:56:51 +01006476 bool Is64BitMove() const {
6477 return Primitive::Is64BitType(type_);
6478 }
6479
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006480 HInstruction* GetInstruction() const { return instruction_; }
6481
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006482 private:
6483 Location source_;
6484 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006485 // The type this move is for.
6486 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006487 // The instruction this move is assocatied with. Null when this move is
6488 // for moving an input in the expected locations of user (including a phi user).
6489 // This is only used in debug mode, to ensure we do not connect interval siblings
6490 // in the same parallel move.
6491 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006492};
6493
Roland Levillainc9285912015-12-18 10:38:42 +00006494std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6495
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006496static constexpr size_t kDefaultNumberOfMoves = 4;
6497
Vladimir Markofcb503c2016-05-18 12:48:17 +01006498class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006499 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006500 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006501 : HTemplateInstruction(SideEffects::None(), dex_pc),
6502 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6503 moves_.reserve(kDefaultNumberOfMoves);
6504 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006505
Nicolas Geoffray90218252015-04-15 11:56:51 +01006506 void AddMove(Location source,
6507 Location destination,
6508 Primitive::Type type,
6509 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006510 DCHECK(source.IsValid());
6511 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006512 if (kIsDebugBuild) {
6513 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006514 for (const MoveOperands& move : moves_) {
6515 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006516 // Special case the situation where the move is for the spill slot
6517 // of the instruction.
6518 if ((GetPrevious() == instruction)
6519 || ((GetPrevious() == nullptr)
6520 && instruction->IsPhi()
6521 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006522 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006523 << "Doing parallel moves for the same instruction.";
6524 } else {
6525 DCHECK(false) << "Doing parallel moves for the same instruction.";
6526 }
6527 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006528 }
6529 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006530 for (const MoveOperands& move : moves_) {
6531 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006532 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006533 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006534 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006535 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006536 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006537 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006538 }
6539
Vladimir Marko225b6462015-09-28 12:17:40 +01006540 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006541 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006542 }
6543
Vladimir Marko225b6462015-09-28 12:17:40 +01006544 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006545
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006546 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006547
6548 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006549 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006550
6551 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6552};
6553
Mark Mendell0616ae02015-04-17 12:49:27 -04006554} // namespace art
6555
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006556#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6557#include "nodes_shared.h"
6558#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006559#ifdef ART_ENABLE_CODEGEN_arm
6560#include "nodes_arm.h"
6561#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006562#ifdef ART_ENABLE_CODEGEN_arm64
6563#include "nodes_arm64.h"
6564#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006565#ifdef ART_ENABLE_CODEGEN_mips
6566#include "nodes_mips.h"
6567#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006568#ifdef ART_ENABLE_CODEGEN_x86
6569#include "nodes_x86.h"
6570#endif
6571
6572namespace art {
6573
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006574class HGraphVisitor : public ValueObject {
6575 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006576 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6577 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006578
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006579 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006580 virtual void VisitBasicBlock(HBasicBlock* block);
6581
Roland Levillain633021e2014-10-01 14:12:25 +01006582 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006583 void VisitInsertionOrder();
6584
Roland Levillain633021e2014-10-01 14:12:25 +01006585 // Visit the graph following dominator tree reverse post-order.
6586 void VisitReversePostOrder();
6587
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006588 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006589
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006590 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006591#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006592 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6593
6594 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6595
6596#undef DECLARE_VISIT_INSTRUCTION
6597
6598 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006599 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006600
6601 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6602};
6603
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006604class HGraphDelegateVisitor : public HGraphVisitor {
6605 public:
6606 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6607 virtual ~HGraphDelegateVisitor() {}
6608
6609 // Visit functions that delegate to to super class.
6610#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006611 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006612
6613 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6614
6615#undef DECLARE_VISIT_INSTRUCTION
6616
6617 private:
6618 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6619};
6620
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006621class HInsertionOrderIterator : public ValueObject {
6622 public:
6623 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6624
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006625 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006626 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006627 void Advance() { ++index_; }
6628
6629 private:
6630 const HGraph& graph_;
6631 size_t index_;
6632
6633 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6634};
6635
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006636class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006637 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006638 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6639 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006640 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006641 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006642
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006643 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6644 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006645 void Advance() { ++index_; }
6646
6647 private:
6648 const HGraph& graph_;
6649 size_t index_;
6650
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006651 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006652};
6653
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006654class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006655 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006656 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006657 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006658 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006659 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006660 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006661
6662 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006663 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006664 void Advance() { --index_; }
6665
6666 private:
6667 const HGraph& graph_;
6668 size_t index_;
6669
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006670 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006671};
6672
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006673// Iterator over the blocks that art part of the loop. Includes blocks part
6674// of an inner loop. The order in which the blocks are iterated is on their
6675// block id.
6676class HBlocksInLoopIterator : public ValueObject {
6677 public:
6678 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6679 : blocks_in_loop_(info.GetBlocks()),
6680 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6681 index_(0) {
6682 if (!blocks_in_loop_.IsBitSet(index_)) {
6683 Advance();
6684 }
6685 }
6686
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006687 bool Done() const { return index_ == blocks_.size(); }
6688 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006689 void Advance() {
6690 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006691 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006692 if (blocks_in_loop_.IsBitSet(index_)) {
6693 break;
6694 }
6695 }
6696 }
6697
6698 private:
6699 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006700 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006701 size_t index_;
6702
6703 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6704};
6705
Mingyao Yang3584bce2015-05-19 16:01:59 -07006706// Iterator over the blocks that art part of the loop. Includes blocks part
6707// of an inner loop. The order in which the blocks are iterated is reverse
6708// post order.
6709class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6710 public:
6711 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6712 : blocks_in_loop_(info.GetBlocks()),
6713 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6714 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006715 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006716 Advance();
6717 }
6718 }
6719
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006720 bool Done() const { return index_ == blocks_.size(); }
6721 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006722 void Advance() {
6723 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006724 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6725 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006726 break;
6727 }
6728 }
6729 }
6730
6731 private:
6732 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006733 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006734 size_t index_;
6735
6736 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6737};
6738
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006739inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006740 if (constant->IsIntConstant()) {
6741 return constant->AsIntConstant()->GetValue();
6742 } else if (constant->IsLongConstant()) {
6743 return constant->AsLongConstant()->GetValue();
6744 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006745 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006746 return 0;
6747 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006748}
6749
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006750#define INSTRUCTION_TYPE_CHECK(type, super) \
6751 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6752 inline const H##type* HInstruction::As##type() const { \
6753 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6754 } \
6755 inline H##type* HInstruction::As##type() { \
6756 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6757 }
6758
6759 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6760#undef INSTRUCTION_TYPE_CHECK
6761
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006762// Create space in `blocks` for adding `number_of_new_blocks` entries
6763// starting at location `at`. Blocks after `at` are moved accordingly.
6764inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6765 size_t number_of_new_blocks,
6766 size_t after) {
6767 DCHECK_LT(after, blocks->size());
6768 size_t old_size = blocks->size();
6769 size_t new_size = old_size + number_of_new_blocks;
6770 blocks->resize(new_size);
6771 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6772}
6773
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006774} // namespace art
6775
6776#endif // ART_COMPILER_OPTIMIZING_NODES_H_