blob: b0e61e6efc77998f46e3a56712c220f3fe358e6c [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.
339 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
340
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
Aart Bik281c6812016-08-26 11:31:48 -0700369 // Computes a linear order for the current graph (should be called before
370 // using HLinearOrderIterator). Linearizes the graph such that:
Aart Bik20e9db62016-09-14 10:52:13 -0700371 // (1): a block is always after its dominator,
372 // (2): blocks of loops are contiguous.
373 // This creates a natural and efficient ordering when visualizing live ranges.
374 void Linearize();
375
David Brazdilffee3d32015-07-06 11:48:53 +0100376 // Iterate over blocks to compute try block membership. Needs reverse post
377 // order and loop information.
378 void ComputeTryBlockInformation();
379
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000380 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000381 // Returns the instruction to replace the invoke expression or null if the
382 // invoke is for a void method. Note that the caller is responsible for replacing
383 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000384 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000385
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000386 // Update the loop and try membership of `block`, which was spawned from `reference`.
387 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
388 // should be the new back edge.
389 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
390 HBasicBlock* reference,
391 bool replace_if_back_edge);
392
Mingyao Yang3584bce2015-05-19 16:01:59 -0700393 // Need to add a couple of blocks to test if the loop body is entered and
394 // put deoptimization instructions, etc.
395 void TransformLoopHeaderForBCE(HBasicBlock* header);
396
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000397 // Removes `block` from the graph. Assumes `block` has been disconnected from
398 // other blocks and has no instructions or phis.
399 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000400
David Brazdilfc6a86a2015-06-26 10:33:45 +0000401 // Splits the edge between `block` and `successor` while preserving the
402 // indices in the predecessor/successor lists. If there are multiple edges
403 // between the blocks, the lowest indices are used.
404 // Returns the new block which is empty and has the same dex pc as `successor`.
405 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
406
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100407 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
408 void SimplifyLoop(HBasicBlock* header);
409
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000410 int32_t GetNextInstructionId() {
411 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000412 return current_instruction_id_++;
413 }
414
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000415 int32_t GetCurrentInstructionId() const {
416 return current_instruction_id_;
417 }
418
419 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000420 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000421 current_instruction_id_ = id;
422 }
423
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100424 uint16_t GetMaximumNumberOfOutVRegs() const {
425 return maximum_number_of_out_vregs_;
426 }
427
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000428 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
429 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100430 }
431
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100432 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
433 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
434 }
435
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000436 void UpdateTemporariesVRegSlots(size_t slots) {
437 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100438 }
439
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000440 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100441 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000442 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100443 }
444
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100445 void SetNumberOfVRegs(uint16_t number_of_vregs) {
446 number_of_vregs_ = number_of_vregs;
447 }
448
449 uint16_t GetNumberOfVRegs() const {
450 return number_of_vregs_;
451 }
452
453 void SetNumberOfInVRegs(uint16_t value) {
454 number_of_in_vregs_ = value;
455 }
456
David Brazdildee58d62016-04-07 09:54:26 +0000457 uint16_t GetNumberOfInVRegs() const {
458 return number_of_in_vregs_;
459 }
460
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100461 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100462 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100463 return number_of_vregs_ - number_of_in_vregs_;
464 }
465
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100466 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100467 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100468 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100469
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100470 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100471 return linear_order_;
472 }
473
Mark Mendell1152c922015-04-24 17:06:35 -0400474 bool HasBoundsChecks() const {
475 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800476 }
477
Mark Mendell1152c922015-04-24 17:06:35 -0400478 void SetHasBoundsChecks(bool value) {
479 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800480 }
481
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100482 bool ShouldGenerateConstructorBarrier() const {
483 return should_generate_constructor_barrier_;
484 }
485
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000486 bool IsDebuggable() const { return debuggable_; }
487
David Brazdil8d5b8b22015-03-24 10:51:52 +0000488 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000489 // already, it is created and inserted into the graph. This method is only for
490 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600491 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000492
493 // TODO: This is problematic for the consistency of reference type propagation
494 // because it can be created anytime after the pass and thus it will be left
495 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600496 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000497
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600498 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
499 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000500 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600501 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
502 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000503 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600504 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
505 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000506 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600507 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
508 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000509 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000510
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100511 HCurrentMethod* GetCurrentMethod();
512
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100513 const DexFile& GetDexFile() const {
514 return dex_file_;
515 }
516
517 uint32_t GetMethodIdx() const {
518 return method_idx_;
519 }
520
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100521 InvokeType GetInvokeType() const {
522 return invoke_type_;
523 }
524
Mark Mendellc4701932015-04-10 13:18:51 -0400525 InstructionSet GetInstructionSet() const {
526 return instruction_set_;
527 }
528
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000529 bool IsCompilingOsr() const { return osr_; }
530
David Brazdil77a48ae2015-09-15 12:34:04 +0000531 bool HasTryCatch() const { return has_try_catch_; }
532 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100533
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000534 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
535 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
536
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100537 ArtMethod* GetArtMethod() const { return art_method_; }
538 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
539
Mark Mendellf6529172015-11-17 11:16:56 -0500540 // Returns an instruction with the opposite boolean value from 'cond'.
541 // The instruction has been inserted into the graph, either as a constant, or
542 // before cursor.
543 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
544
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000545 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
546
David Brazdil2d7352b2015-04-20 14:52:42 +0100547 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000548 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100549 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000550
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000551 template <class InstructionType, typename ValueType>
552 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600553 ArenaSafeMap<ValueType, InstructionType*>* cache,
554 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000555 // Try to find an existing constant of the given value.
556 InstructionType* constant = nullptr;
557 auto cached_constant = cache->find(value);
558 if (cached_constant != cache->end()) {
559 constant = cached_constant->second;
560 }
561
562 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100563 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000564 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600565 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000566 cache->Overwrite(value, constant);
567 InsertConstant(constant);
568 }
569 return constant;
570 }
571
David Brazdil8d5b8b22015-03-24 10:51:52 +0000572 void InsertConstant(HConstant* instruction);
573
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000574 // Cache a float constant into the graph. This method should only be
575 // called by the SsaBuilder when creating "equivalent" instructions.
576 void CacheFloatConstant(HFloatConstant* constant);
577
578 // See CacheFloatConstant comment.
579 void CacheDoubleConstant(HDoubleConstant* constant);
580
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000581 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000582
583 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100584 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000585
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100586 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100587 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000588
Aart Bik281c6812016-08-26 11:31:48 -0700589 // List of blocks to perform a linear order tree traversal. Unlike the reverse
590 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100591 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100592
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000593 HBasicBlock* entry_block_;
594 HBasicBlock* exit_block_;
595
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100596 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100597 uint16_t maximum_number_of_out_vregs_;
598
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100599 // The number of virtual registers in this method. Contains the parameters.
600 uint16_t number_of_vregs_;
601
602 // The number of virtual registers used by parameters of this method.
603 uint16_t number_of_in_vregs_;
604
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000605 // Number of vreg size slots that the temporaries use (used in baseline compiler).
606 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100607
Mark Mendell1152c922015-04-24 17:06:35 -0400608 // Has bounds checks. We can totally skip BCE if it's false.
609 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800610
David Brazdil77a48ae2015-09-15 12:34:04 +0000611 // Flag whether there are any try/catch blocks in the graph. We will skip
612 // try/catch-related passes if false.
613 bool has_try_catch_;
614
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000615 // Flag whether there are any irreducible loops in the graph.
616 bool has_irreducible_loops_;
617
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000618 // Indicates whether the graph should be compiled in a way that
619 // ensures full debuggability. If false, we can apply more
620 // aggressive optimizations that may limit the level of debugging.
621 const bool debuggable_;
622
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000623 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000624 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000625
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100626 // The dex file from which the method is from.
627 const DexFile& dex_file_;
628
629 // The method index in the dex file.
630 const uint32_t method_idx_;
631
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100632 // If inlined, this encodes how the callee is being invoked.
633 const InvokeType invoke_type_;
634
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100635 // Whether the graph has been transformed to SSA form. Only used
636 // in debug mode to ensure we are not using properties only valid
637 // for non-SSA form (like the number of temporaries).
638 bool in_ssa_form_;
639
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100640 const bool should_generate_constructor_barrier_;
641
Mathieu Chartiere401d142015-04-22 13:56:20 -0700642 const InstructionSet instruction_set_;
643
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000644 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000645 HNullConstant* cached_null_constant_;
646 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000647 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000648 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000649 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000650
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100651 HCurrentMethod* cached_current_method_;
652
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100653 // The ArtMethod this graph is for. Note that for AOT, it may be null,
654 // for example for methods whose declaring class could not be resolved
655 // (such as when the superclass could not be found).
656 ArtMethod* art_method_;
657
David Brazdil4833f5a2015-12-16 10:37:39 +0000658 // Keep the RTI of inexact Object to avoid having to pass stack handle
659 // collection pointer to passes which may create NullConstant.
660 ReferenceTypeInfo inexact_object_rti_;
661
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000662 // Whether we are compiling this graph for on stack replacement: this will
663 // make all loops seen as irreducible and emit special stack maps to mark
664 // compiled code entries which the interpreter can directly jump to.
665 const bool osr_;
666
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000667 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100668 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000669 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000670 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000671 DISALLOW_COPY_AND_ASSIGN(HGraph);
672};
673
Vladimir Markof9f64412015-09-02 14:05:49 +0100674class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000675 public:
676 HLoopInformation(HBasicBlock* header, HGraph* graph)
677 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100678 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000679 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100680 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100681 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100682 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000683 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100684 back_edges_.reserve(kDefaultNumberOfBackEdges);
685 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100686
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000687 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100688 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000689
690 void Dump(std::ostream& os);
691
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100692 HBasicBlock* GetHeader() const {
693 return header_;
694 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000695
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000696 void SetHeader(HBasicBlock* block) {
697 header_ = block;
698 }
699
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100700 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
701 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
702 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
703
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000704 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100705 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000706 }
707
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100708 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100709 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100710 }
711
David Brazdil46e2a392015-03-16 17:31:52 +0000712 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100713 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100714 }
715
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000716 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100717 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000718 }
719
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100720 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100721
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100722 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100723 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100724 }
725
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100726 // Returns the lifetime position of the back edge that has the
727 // greatest lifetime position.
728 size_t GetLifetimeEnd() const;
729
730 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100731 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100732 }
733
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000734 // Finds blocks that are part of this loop.
735 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100736
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100737 // Returns whether this loop information contains `block`.
738 // Note that this loop information *must* be populated before entering this function.
739 bool Contains(const HBasicBlock& block) const;
740
741 // Returns whether this loop information is an inner loop of `other`.
742 // Note that `other` *must* be populated before entering this function.
743 bool IsIn(const HLoopInformation& other) const;
744
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800745 // Returns true if instruction is not defined within this loop.
746 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700747
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100748 const ArenaBitVector& GetBlocks() const { return blocks_; }
749
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000750 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000751 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000752
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000753 void ClearAllBlocks() {
754 blocks_.ClearAllBits();
755 }
756
David Brazdil3f4a5222016-05-06 12:46:21 +0100757 bool HasBackEdgeNotDominatedByHeader() const;
758
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100759 bool IsPopulated() const {
760 return blocks_.GetHighestBitSet() != -1;
761 }
762
Anton Shaminf89381f2016-05-16 16:44:13 +0600763 bool DominatesAllBackEdges(HBasicBlock* block);
764
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000765 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100766 // Internal recursive implementation of `Populate`.
767 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100768 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100769
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000770 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100771 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000772 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100773 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100774 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100775 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000776
777 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
778};
779
David Brazdilec16f792015-08-19 15:04:01 +0100780// Stores try/catch information for basic blocks.
781// Note that HGraph is constructed so that catch blocks cannot simultaneously
782// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100783class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100784 public:
785 // Try block information constructor.
786 explicit TryCatchInformation(const HTryBoundary& try_entry)
787 : try_entry_(&try_entry),
788 catch_dex_file_(nullptr),
789 catch_type_index_(DexFile::kDexNoIndex16) {
790 DCHECK(try_entry_ != nullptr);
791 }
792
793 // Catch block information constructor.
794 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
795 : try_entry_(nullptr),
796 catch_dex_file_(&dex_file),
797 catch_type_index_(catch_type_index) {}
798
799 bool IsTryBlock() const { return try_entry_ != nullptr; }
800
801 const HTryBoundary& GetTryEntry() const {
802 DCHECK(IsTryBlock());
803 return *try_entry_;
804 }
805
806 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
807
808 bool IsCatchAllTypeIndex() const {
809 DCHECK(IsCatchBlock());
810 return catch_type_index_ == DexFile::kDexNoIndex16;
811 }
812
813 uint16_t GetCatchTypeIndex() const {
814 DCHECK(IsCatchBlock());
815 return catch_type_index_;
816 }
817
818 const DexFile& GetCatchDexFile() const {
819 DCHECK(IsCatchBlock());
820 return *catch_dex_file_;
821 }
822
823 private:
824 // One of possibly several TryBoundary instructions entering the block's try.
825 // Only set for try blocks.
826 const HTryBoundary* try_entry_;
827
828 // Exception type information. Only set for catch blocks.
829 const DexFile* catch_dex_file_;
830 const uint16_t catch_type_index_;
831};
832
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100833static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100834static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100835
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000836// A block in a method. Contains the list of instructions represented
837// as a double linked list. Each block knows its predecessors and
838// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100839
Vladimir Markof9f64412015-09-02 14:05:49 +0100840class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000841 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700842 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000843 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000844 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
845 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000846 loop_information_(nullptr),
847 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000848 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100849 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100850 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100851 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000852 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000853 try_catch_information_(nullptr) {
854 predecessors_.reserve(kDefaultNumberOfPredecessors);
855 successors_.reserve(kDefaultNumberOfSuccessors);
856 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
857 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000858
Vladimir Marko60584552015-09-03 13:35:12 +0000859 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100860 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000861 }
862
Vladimir Marko60584552015-09-03 13:35:12 +0000863 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100864 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000865 }
866
David Brazdild26a4112015-11-10 11:07:31 +0000867 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
868 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
869
Vladimir Marko60584552015-09-03 13:35:12 +0000870 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
871 return ContainsElement(successors_, block, start_from);
872 }
873
874 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100875 return dominated_blocks_;
876 }
877
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100878 bool IsEntryBlock() const {
879 return graph_->GetEntryBlock() == this;
880 }
881
882 bool IsExitBlock() const {
883 return graph_->GetExitBlock() == this;
884 }
885
David Brazdil46e2a392015-03-16 17:31:52 +0000886 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000887 bool IsSingleTryBoundary() const;
888
889 // Returns true if this block emits nothing but a jump.
890 bool IsSingleJump() const {
891 HLoopInformation* loop_info = GetLoopInformation();
892 return (IsSingleGoto() || IsSingleTryBoundary())
893 // Back edges generate a suspend check.
894 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
895 }
David Brazdil46e2a392015-03-16 17:31:52 +0000896
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000897 void AddBackEdge(HBasicBlock* back_edge) {
898 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000899 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000900 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100901 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000902 loop_information_->AddBackEdge(back_edge);
903 }
904
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000905 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000906 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000907
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100908 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000909 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600910 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000911
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000912 HBasicBlock* GetDominator() const { return dominator_; }
913 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000914 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
915
916 void RemoveDominatedBlock(HBasicBlock* block) {
917 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100918 }
Vladimir Marko60584552015-09-03 13:35:12 +0000919
920 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
921 ReplaceElement(dominated_blocks_, existing, new_block);
922 }
923
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100924 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000925
926 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100927 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000928 }
929
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100930 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
931 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100932 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100933 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100934 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
935 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000936
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000937 HInstruction* GetFirstInstructionDisregardMoves() const;
938
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000939 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000940 successors_.push_back(block);
941 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000942 }
943
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100944 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
945 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100946 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000947 new_block->predecessors_.push_back(this);
948 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000949 }
950
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000951 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
952 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000953 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000954 new_block->successors_.push_back(this);
955 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000956 }
957
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100958 // Insert `this` between `predecessor` and `successor. This method
959 // preserves the indicies, and will update the first edge found between
960 // `predecessor` and `successor`.
961 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
962 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100963 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000964 successor->predecessors_[predecessor_index] = this;
965 predecessor->successors_[successor_index] = this;
966 successors_.push_back(successor);
967 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100968 }
969
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100970 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000971 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100972 }
973
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000974 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000975 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000976 }
977
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100978 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000979 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100980 }
981
982 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000983 predecessors_.push_back(block);
984 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100985 }
986
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100987 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000988 DCHECK_EQ(predecessors_.size(), 2u);
989 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100990 }
991
David Brazdil769c9e52015-04-27 13:54:09 +0100992 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000993 DCHECK_EQ(successors_.size(), 2u);
994 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100995 }
996
David Brazdilfc6a86a2015-06-26 10:33:45 +0000997 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000998 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100999 }
1000
David Brazdilfc6a86a2015-06-26 10:33:45 +00001001 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001002 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001003 }
1004
David Brazdilfc6a86a2015-06-26 10:33:45 +00001005 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001006 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001007 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001008 }
1009
1010 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001011 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001012 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001013 }
1014
1015 // Returns whether the first occurrence of `predecessor` in the list of
1016 // predecessors is at index `idx`.
1017 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001018 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001019 return GetPredecessorIndexOf(predecessor) == idx;
1020 }
1021
David Brazdild7558da2015-09-22 13:04:14 +01001022 // Create a new block between this block and its predecessors. The new block
1023 // is added to the graph, all predecessor edges are relinked to it and an edge
1024 // is created to `this`. Returns the new empty block. Reverse post order or
1025 // loop and try/catch information are not updated.
1026 HBasicBlock* CreateImmediateDominator();
1027
David Brazdilfc6a86a2015-06-26 10:33:45 +00001028 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001029 // created, latter block. Note that this method will add the block to the
1030 // graph, create a Goto at the end of the former block and will create an edge
1031 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001032 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001033 HBasicBlock* SplitBefore(HInstruction* cursor);
1034
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001035 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001036 // created block. Note that this method just updates raw block information,
1037 // like predecessors, successors, dominators, and instruction list. It does not
1038 // update the graph, reverse post order, loop information, nor make sure the
1039 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001040 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1041
1042 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1043 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001044
1045 // Merge `other` at the end of `this`. Successors and dominated blocks of
1046 // `other` are changed to be successors and dominated blocks of `this`. Note
1047 // that this method does not update the graph, reverse post order, loop
1048 // information, nor make sure the blocks are consistent (for example ending
1049 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001050 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001051
1052 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1053 // of `this` are moved to `other`.
1054 // Note that this method does not update the graph, reverse post order, loop
1055 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001056 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001057 void ReplaceWith(HBasicBlock* other);
1058
David Brazdil2d7352b2015-04-20 14:52:42 +01001059 // Merge `other` at the end of `this`. This method updates loops, reverse post
1060 // order, links to predecessors, successors, dominators and deletes the block
1061 // from the graph. The two blocks must be successive, i.e. `this` the only
1062 // predecessor of `other` and vice versa.
1063 void MergeWith(HBasicBlock* other);
1064
1065 // Disconnects `this` from all its predecessors, successors and dominator,
1066 // removes it from all loops it is included in and eventually from the graph.
1067 // The block must not dominate any other block. Predecessors and successors
1068 // are safely updated.
1069 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001070
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001071 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001072 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001073 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001074 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001075 // Replace instruction `initial` with `replacement` within this block.
1076 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1077 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001078 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001079 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001080 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1081 // instruction list. With 'ensure_safety' set to true, it verifies that the
1082 // instruction is not in use and removes it from the use lists of its inputs.
1083 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1084 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001085 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001086
1087 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001088 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001089 }
1090
Roland Levillain6b879dd2014-09-22 17:13:44 +01001091 bool IsLoopPreHeaderFirstPredecessor() const {
1092 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001093 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001094 }
1095
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001096 bool IsFirstPredecessorBackEdge() const {
1097 DCHECK(IsLoopHeader());
1098 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1099 }
1100
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001101 HLoopInformation* GetLoopInformation() const {
1102 return loop_information_;
1103 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001104
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001105 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001106 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001107 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001108 void SetInLoop(HLoopInformation* info) {
1109 if (IsLoopHeader()) {
1110 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001111 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001112 loop_information_ = info;
1113 } else if (loop_information_->Contains(*info->GetHeader())) {
1114 // Block is currently part of an outer loop. Make it part of this inner loop.
1115 // Note that a non loop header having a loop information means this loop information
1116 // has already been populated
1117 loop_information_ = info;
1118 } else {
1119 // Block is part of an inner loop. Do not update the loop information.
1120 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1121 // at this point, because this method is being called while populating `info`.
1122 }
1123 }
1124
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001125 // Raw update of the loop information.
1126 void SetLoopInformation(HLoopInformation* info) {
1127 loop_information_ = info;
1128 }
1129
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001130 bool IsInLoop() const { return loop_information_ != nullptr; }
1131
David Brazdilec16f792015-08-19 15:04:01 +01001132 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1133
1134 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1135 try_catch_information_ = try_catch_information;
1136 }
1137
1138 bool IsTryBlock() const {
1139 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1140 }
1141
1142 bool IsCatchBlock() const {
1143 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1144 }
David Brazdilffee3d32015-07-06 11:48:53 +01001145
1146 // Returns the try entry that this block's successors should have. They will
1147 // be in the same try, unless the block ends in a try boundary. In that case,
1148 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001149 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001150
David Brazdild7558da2015-09-22 13:04:14 +01001151 bool HasThrowingInstructions() const;
1152
David Brazdila4b8c212015-05-07 09:59:30 +01001153 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001154 bool Dominates(HBasicBlock* block) const;
1155
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001156 size_t GetLifetimeStart() const { return lifetime_start_; }
1157 size_t GetLifetimeEnd() const { return lifetime_end_; }
1158
1159 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1160 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1161
David Brazdil8d5b8b22015-03-24 10:51:52 +00001162 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001163 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001164 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001165 bool HasSinglePhi() const;
1166
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001167 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001168 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001169 ArenaVector<HBasicBlock*> predecessors_;
1170 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001171 HInstructionList instructions_;
1172 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001173 HLoopInformation* loop_information_;
1174 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001175 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001176 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001177 // The dex program counter of the first instruction of this block.
1178 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001179 size_t lifetime_start_;
1180 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001181 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001182
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001183 friend class HGraph;
1184 friend class HInstruction;
1185
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001186 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1187};
1188
David Brazdilb2bd1c52015-03-25 11:17:37 +00001189// Iterates over the LoopInformation of all loops which contain 'block'
1190// from the innermost to the outermost.
1191class HLoopInformationOutwardIterator : public ValueObject {
1192 public:
1193 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1194 : current_(block.GetLoopInformation()) {}
1195
1196 bool Done() const { return current_ == nullptr; }
1197
1198 void Advance() {
1199 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001200 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001201 }
1202
1203 HLoopInformation* Current() const {
1204 DCHECK(!Done());
1205 return current_;
1206 }
1207
1208 private:
1209 HLoopInformation* current_;
1210
1211 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1212};
1213
Alexandre Ramesef20f712015-06-09 10:29:30 +01001214#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001215 M(Above, Condition) \
1216 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001217 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001218 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001219 M(ArrayGet, Instruction) \
1220 M(ArrayLength, Instruction) \
1221 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001222 M(Below, Condition) \
1223 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001224 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001225 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001226 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001227 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001228 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001229 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001230 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001231 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001232 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001233 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001234 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001235 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001236 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001237 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001238 M(Exit, Instruction) \
1239 M(FloatConstant, Constant) \
1240 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001241 M(GreaterThan, Condition) \
1242 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001243 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001244 M(InstanceFieldGet, Instruction) \
1245 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001246 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001247 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001248 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001249 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001250 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001251 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001252 M(LessThan, Condition) \
1253 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001254 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001255 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001256 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001257 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001258 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001259 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001260 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001261 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001262 M(Neg, UnaryOperation) \
1263 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001264 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001265 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001266 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001267 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001268 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001269 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001270 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001271 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001272 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001273 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001274 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001275 M(Return, Instruction) \
1276 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001277 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001278 M(Shl, BinaryOperation) \
1279 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001280 M(StaticFieldGet, Instruction) \
1281 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001282 M(UnresolvedInstanceFieldGet, Instruction) \
1283 M(UnresolvedInstanceFieldSet, Instruction) \
1284 M(UnresolvedStaticFieldGet, Instruction) \
1285 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001286 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001287 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001288 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001289 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001290 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001291 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001292 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001293 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001294
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001295/*
1296 * Instructions, shared across several (not all) architectures.
1297 */
1298#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1299#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1300#else
1301#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001302 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001303 M(MultiplyAccumulate, Instruction) \
1304 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001305#endif
1306
Vladimir Markob4536b72015-11-24 13:45:23 +00001307#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001308#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001309#else
1310#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1311 M(ArmDexCacheArraysBase, Instruction)
1312#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001313
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001314#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001315#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001316#else
1317#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001318 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001319#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001320
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001321#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001322#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001323#else
1324#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1325 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001326 M(MipsDexCacheArraysBase, Instruction) \
1327 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001328#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001329
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001330#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1331
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001332#ifndef ART_ENABLE_CODEGEN_x86
1333#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1334#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001335#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1336 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001337 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001338 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001339 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001340#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001341
1342#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1343
1344#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1345 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001346 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001347 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1348 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001349 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001350 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001351 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1352 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1353
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001354#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1355 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001356 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001357 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001358 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001359 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001360
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001361#define FOR_EACH_INSTRUCTION(M) \
1362 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1363 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1364
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001365#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001366FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1367#undef FORWARD_DECLARATION
1368
Vladimir Marko372f10e2016-05-17 16:30:10 +01001369#define DECLARE_INSTRUCTION(type) \
1370 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1371 const char* DebugName() const OVERRIDE { return #type; } \
1372 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1373 return other->Is##type(); \
1374 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001375 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001376
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001377#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1378 bool Is##type() const { return As##type() != nullptr; } \
1379 const H##type* As##type() const { return this; } \
1380 H##type* As##type() { return this; }
1381
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001382template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001383class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001384 public:
David Brazdiled596192015-01-23 10:39:45 +00001385 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001386 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001387 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001388
Vladimir Marko46817b82016-03-29 12:21:58 +01001389 // Hook for the IntrusiveForwardList<>.
1390 // TODO: Hide this better.
1391 IntrusiveForwardListHook hook;
1392
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001393 private:
David Brazdiled596192015-01-23 10:39:45 +00001394 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001395 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001396
1397 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001398 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001399
Vladimir Marko46817b82016-03-29 12:21:58 +01001400 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001401
1402 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1403};
1404
David Brazdiled596192015-01-23 10:39:45 +00001405template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001406using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001407
David Brazdil1abb4192015-02-17 18:33:36 +00001408// This class is used by HEnvironment and HInstruction classes to record the
1409// instructions they use and pointers to the corresponding HUseListNodes kept
1410// by the used instructions.
1411template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001412class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001413 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001414 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1415 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001416
Vladimir Marko46817b82016-03-29 12:21:58 +01001417 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1418 : HUserRecord(old_record.instruction_, before_use_node) {}
1419 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1420 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001421 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001422 }
1423
1424 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001425 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1426 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001427
1428 private:
1429 // Instruction used by the user.
1430 HInstruction* instruction_;
1431
Vladimir Marko46817b82016-03-29 12:21:58 +01001432 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1433 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001434};
1435
Vladimir Markoe9004912016-06-16 16:50:52 +01001436// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1437// This is used for HInstruction::GetInputs() to return a container wrapper providing
1438// HInstruction* values even though the underlying container has HUserRecord<>s.
1439struct HInputExtractor {
1440 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1441 return record.GetInstruction();
1442 }
1443 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1444 return record.GetInstruction();
1445 }
1446};
1447
1448using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1449using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1450
Aart Bik854a02b2015-07-14 16:07:00 -07001451/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001452 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001453 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001454 * For write/read dependences on fields/arrays, the dependence analysis uses
1455 * type disambiguation (e.g. a float field write cannot modify the value of an
1456 * integer field read) and the access type (e.g. a reference array write cannot
1457 * modify the value of a reference field read [although it may modify the
1458 * reference fetch prior to reading the field, which is represented by its own
1459 * write/read dependence]). The analysis makes conservative points-to
1460 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1461 * the same, and any reference read depends on any reference read without
1462 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001463 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001464 * The internal representation uses 38-bit and is described in the table below.
1465 * The first line indicates the side effect, and for field/array accesses the
1466 * second line indicates the type of the access (in the order of the
1467 * Primitive::Type enum).
1468 * The two numbered lines below indicate the bit position in the bitfield (read
1469 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001470 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001471 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1472 * +-------------+---------+---------+--------------+---------+---------+
1473 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1474 * | 3 |333333322|222222221| 1 |111111110|000000000|
1475 * | 7 |654321098|765432109| 8 |765432109|876543210|
1476 *
1477 * Note that, to ease the implementation, 'changes' bits are least significant
1478 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001479 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001480class SideEffects : public ValueObject {
1481 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001482 SideEffects() : flags_(0) {}
1483
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001484 static SideEffects None() {
1485 return SideEffects(0);
1486 }
1487
1488 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001489 return SideEffects(kAllChangeBits | kAllDependOnBits);
1490 }
1491
1492 static SideEffects AllChanges() {
1493 return SideEffects(kAllChangeBits);
1494 }
1495
1496 static SideEffects AllDependencies() {
1497 return SideEffects(kAllDependOnBits);
1498 }
1499
1500 static SideEffects AllExceptGCDependency() {
1501 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1502 }
1503
1504 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001505 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001506 }
1507
Aart Bik34c3ba92015-07-20 14:08:59 -07001508 static SideEffects AllWrites() {
1509 return SideEffects(kAllWrites);
1510 }
1511
1512 static SideEffects AllReads() {
1513 return SideEffects(kAllReads);
1514 }
1515
1516 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1517 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001518 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001519 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001520 }
1521
Aart Bik854a02b2015-07-14 16:07:00 -07001522 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001523 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001524 }
1525
Aart Bik34c3ba92015-07-20 14:08:59 -07001526 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1527 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001528 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001529 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001530 }
1531
1532 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001533 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001534 }
1535
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001536 static SideEffects CanTriggerGC() {
1537 return SideEffects(1ULL << kCanTriggerGCBit);
1538 }
1539
1540 static SideEffects DependsOnGC() {
1541 return SideEffects(1ULL << kDependsOnGCBit);
1542 }
1543
Aart Bik854a02b2015-07-14 16:07:00 -07001544 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001545 SideEffects Union(SideEffects other) const {
1546 return SideEffects(flags_ | other.flags_);
1547 }
1548
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001549 SideEffects Exclusion(SideEffects other) const {
1550 return SideEffects(flags_ & ~other.flags_);
1551 }
1552
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001553 void Add(SideEffects other) {
1554 flags_ |= other.flags_;
1555 }
1556
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001557 bool Includes(SideEffects other) const {
1558 return (other.flags_ & flags_) == other.flags_;
1559 }
1560
1561 bool HasSideEffects() const {
1562 return (flags_ & kAllChangeBits);
1563 }
1564
1565 bool HasDependencies() const {
1566 return (flags_ & kAllDependOnBits);
1567 }
1568
1569 // Returns true if there are no side effects or dependencies.
1570 bool DoesNothing() const {
1571 return flags_ == 0;
1572 }
1573
Aart Bik854a02b2015-07-14 16:07:00 -07001574 // Returns true if something is written.
1575 bool DoesAnyWrite() const {
1576 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001577 }
1578
Aart Bik854a02b2015-07-14 16:07:00 -07001579 // Returns true if something is read.
1580 bool DoesAnyRead() const {
1581 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001582 }
1583
Aart Bik854a02b2015-07-14 16:07:00 -07001584 // Returns true if potentially everything is written and read
1585 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001586 bool DoesAllReadWrite() const {
1587 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1588 }
1589
Aart Bik854a02b2015-07-14 16:07:00 -07001590 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001591 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001592 }
1593
Roland Levillain0d5a2812015-11-13 10:07:31 +00001594 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001595 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001596 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1597 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001598 }
1599
1600 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001601 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001602 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001603 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001604 for (int s = kLastBit; s >= 0; s--) {
1605 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1606 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1607 // This is a bit for the GC side effect.
1608 if (current_bit_is_set) {
1609 flags += "GC";
1610 }
Aart Bik854a02b2015-07-14 16:07:00 -07001611 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001612 } else {
1613 // This is a bit for the array/field analysis.
1614 // The underscore character stands for the 'can trigger GC' bit.
1615 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1616 if (current_bit_is_set) {
1617 flags += kDebug[s];
1618 }
1619 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1620 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1621 flags += "|";
1622 }
1623 }
Aart Bik854a02b2015-07-14 16:07:00 -07001624 }
1625 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001626 }
1627
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001628 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001629
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001630 private:
1631 static constexpr int kFieldArrayAnalysisBits = 9;
1632
1633 static constexpr int kFieldWriteOffset = 0;
1634 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1635 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1636 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1637
1638 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1639
1640 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1641 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1642 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1643 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1644
1645 static constexpr int kLastBit = kDependsOnGCBit;
1646 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1647
1648 // Aliases.
1649
1650 static_assert(kChangeBits == kDependOnBits,
1651 "the 'change' bits should match the 'depend on' bits.");
1652
1653 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1654 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1655 static constexpr uint64_t kAllWrites =
1656 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1657 static constexpr uint64_t kAllReads =
1658 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001659
Aart Bik854a02b2015-07-14 16:07:00 -07001660 // Translates type to bit flag.
1661 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1662 CHECK_NE(type, Primitive::kPrimVoid);
1663 const uint64_t one = 1;
1664 const int shift = type; // 0-based consecutive enum
1665 DCHECK_LE(kFieldWriteOffset, shift);
1666 DCHECK_LT(shift, kArrayWriteOffset);
1667 return one << (type + offset);
1668 }
1669
1670 // Private constructor on direct flags value.
1671 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1672
1673 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001674};
1675
David Brazdiled596192015-01-23 10:39:45 +00001676// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001677class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001678 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001679 HEnvironment(ArenaAllocator* arena,
1680 size_t number_of_vregs,
1681 const DexFile& dex_file,
1682 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001683 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001684 InvokeType invoke_type,
1685 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001686 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1687 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001688 parent_(nullptr),
1689 dex_file_(dex_file),
1690 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001691 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001692 invoke_type_(invoke_type),
1693 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001694 }
1695
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001696 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001697 : HEnvironment(arena,
1698 to_copy.Size(),
1699 to_copy.GetDexFile(),
1700 to_copy.GetMethodIdx(),
1701 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001702 to_copy.GetInvokeType(),
1703 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001704
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001705 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001706 if (parent_ != nullptr) {
1707 parent_->SetAndCopyParentChain(allocator, parent);
1708 } else {
1709 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1710 parent_->CopyFrom(parent);
1711 if (parent->GetParent() != nullptr) {
1712 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1713 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001714 }
David Brazdiled596192015-01-23 10:39:45 +00001715 }
1716
Vladimir Marko71bf8092015-09-15 15:33:14 +01001717 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001718 void CopyFrom(HEnvironment* environment);
1719
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001720 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1721 // input to the loop phi instead. This is for inserting instructions that
1722 // require an environment (like HDeoptimization) in the loop pre-header.
1723 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001724
1725 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001726 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001727 }
1728
1729 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001730 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001731 }
1732
David Brazdil1abb4192015-02-17 18:33:36 +00001733 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001734
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001735 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001736
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001737 HEnvironment* GetParent() const { return parent_; }
1738
1739 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001740 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001741 }
1742
1743 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001744 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001745 }
1746
1747 uint32_t GetDexPc() const {
1748 return dex_pc_;
1749 }
1750
1751 uint32_t GetMethodIdx() const {
1752 return method_idx_;
1753 }
1754
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001755 InvokeType GetInvokeType() const {
1756 return invoke_type_;
1757 }
1758
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001759 const DexFile& GetDexFile() const {
1760 return dex_file_;
1761 }
1762
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001763 HInstruction* GetHolder() const {
1764 return holder_;
1765 }
1766
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001767
1768 bool IsFromInlinedInvoke() const {
1769 return GetParent() != nullptr;
1770 }
1771
David Brazdiled596192015-01-23 10:39:45 +00001772 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001773 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1774 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001775 HEnvironment* parent_;
1776 const DexFile& dex_file_;
1777 const uint32_t method_idx_;
1778 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001779 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001780
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001781 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001782 HInstruction* const holder_;
1783
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001784 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001785
1786 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1787};
1788
Vladimir Markof9f64412015-09-02 14:05:49 +01001789class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001790 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001791 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001792 : previous_(nullptr),
1793 next_(nullptr),
1794 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001795 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001796 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001797 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001798 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001799 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001800 locations_(nullptr),
1801 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001802 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001803 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001804 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1805 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1806 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001807
Dave Allison20dfc792014-06-16 20:44:29 -07001808 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001809
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001810#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001811 enum InstructionKind {
1812 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1813 };
1814#undef DECLARE_KIND
1815
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001816 HInstruction* GetNext() const { return next_; }
1817 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001818
Calin Juravle77520bc2015-01-12 18:45:46 +00001819 HInstruction* GetNextDisregardingMoves() const;
1820 HInstruction* GetPreviousDisregardingMoves() const;
1821
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001822 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001823 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001824 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001825 bool IsInBlock() const { return block_ != nullptr; }
1826 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001827 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1828 bool IsIrreducibleLoopHeaderPhi() const {
1829 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1830 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001831
Vladimir Marko372f10e2016-05-17 16:30:10 +01001832 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1833
1834 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1835 // One virtual method is enough, just const_cast<> and then re-add the const.
1836 return ArrayRef<const HUserRecord<HInstruction*>>(
1837 const_cast<HInstruction*>(this)->GetInputRecords());
1838 }
1839
Vladimir Markoe9004912016-06-16 16:50:52 +01001840 HInputsRef GetInputs() {
1841 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001842 }
1843
Vladimir Markoe9004912016-06-16 16:50:52 +01001844 HConstInputsRef GetInputs() const {
1845 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001846 }
1847
1848 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001849 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001850
Vladimir Marko372f10e2016-05-17 16:30:10 +01001851 void SetRawInputAt(size_t index, HInstruction* input) {
1852 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1853 }
1854
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001855 virtual void Accept(HGraphVisitor* visitor) = 0;
1856 virtual const char* DebugName() const = 0;
1857
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001858 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1859
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001860 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001861
1862 uint32_t GetDexPc() const { return dex_pc_; }
1863
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001864 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001865
Roland Levillaine161a2a2014-10-03 12:45:18 +01001866 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001867 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001868
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001869 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001870 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001871
Calin Juravle10e244f2015-01-26 18:54:32 +00001872 // Does not apply for all instructions, but having this at top level greatly
1873 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001874 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001875 virtual bool CanBeNull() const {
1876 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1877 return true;
1878 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001879
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001880 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001881 return false;
1882 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001883
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001884 virtual bool IsActualObject() const {
1885 return GetType() == Primitive::kPrimNot;
1886 }
1887
Calin Juravle2e768302015-07-28 14:41:11 +00001888 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001889
Calin Juravle61d544b2015-02-23 16:46:57 +00001890 ReferenceTypeInfo GetReferenceTypeInfo() const {
1891 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001892 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001893 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001894 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001895
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001896 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001897 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001898 // Note: fixup_end remains valid across push_front().
1899 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1900 HUseListNode<HInstruction*>* new_node =
1901 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1902 uses_.push_front(*new_node);
1903 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001904 }
1905
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001906 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001907 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001908 // Note: env_fixup_end remains valid across push_front().
1909 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1910 HUseListNode<HEnvironment*>* new_node =
1911 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1912 env_uses_.push_front(*new_node);
1913 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001914 }
1915
David Brazdil1abb4192015-02-17 18:33:36 +00001916 void RemoveAsUserOfInput(size_t input) {
1917 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001918 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);
David Brazdil1abb4192015-02-17 18:33:36 +00001921 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001922
Vladimir Marko372f10e2016-05-17 16:30:10 +01001923 void RemoveAsUserOfAllInputs() {
1924 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1925 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1926 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1927 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1928 }
1929 }
1930
David Brazdil1abb4192015-02-17 18:33:36 +00001931 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1932 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001933
Vladimir Marko46817b82016-03-29 12:21:58 +01001934 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1935 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1936 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001937 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001938 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001939 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001940
Roland Levillain6c82d402014-10-13 16:10:27 +01001941 // Does this instruction strictly dominate `other_instruction`?
1942 // Returns false if this instruction and `other_instruction` are the same.
1943 // Aborts if this instruction and `other_instruction` are both phis.
1944 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001945
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001946 int GetId() const { return id_; }
1947 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001948
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001949 int GetSsaIndex() const { return ssa_index_; }
1950 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1951 bool HasSsaIndex() const { return ssa_index_ != -1; }
1952
1953 bool HasEnvironment() const { return environment_ != nullptr; }
1954 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001955 // Set the `environment_` field. Raw because this method does not
1956 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001957 void SetRawEnvironment(HEnvironment* environment) {
1958 DCHECK(environment_ == nullptr);
1959 DCHECK_EQ(environment->GetHolder(), this);
1960 environment_ = environment;
1961 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001962
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001963 void InsertRawEnvironment(HEnvironment* environment) {
1964 DCHECK(environment_ != nullptr);
1965 DCHECK_EQ(environment->GetHolder(), this);
1966 DCHECK(environment->GetParent() == nullptr);
1967 environment->parent_ = environment_;
1968 environment_ = environment;
1969 }
1970
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001971 void RemoveEnvironment();
1972
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001973 // Set the environment of this instruction, copying it from `environment`. While
1974 // copying, the uses lists are being updated.
1975 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001976 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001977 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001978 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001979 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001980 if (environment->GetParent() != nullptr) {
1981 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1982 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001983 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001984
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001985 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1986 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001987 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001988 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001989 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001990 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001991 if (environment->GetParent() != nullptr) {
1992 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1993 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001994 }
1995
Nicolas Geoffray39468442014-09-02 15:17:15 +01001996 // Returns the number of entries in the environment. Typically, that is the
1997 // number of dex registers in a method. It could be more in case of inlining.
1998 size_t EnvironmentSize() const;
1999
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002000 LocationSummary* GetLocations() const { return locations_; }
2001 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002002
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002003 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002004 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002005
Alexandre Rames188d4312015-04-09 18:30:21 +01002006 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2007 // uses of this instruction by `other` are *not* updated.
2008 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2009 ReplaceWith(other);
2010 other->ReplaceInput(this, use_index);
2011 }
2012
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002013 // Move `this` instruction before `cursor`.
2014 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002015
Vladimir Markofb337ea2015-11-25 15:25:10 +00002016 // Move `this` before its first user and out of any loops. If there is no
2017 // out-of-loop user that dominates all other users, move the instruction
2018 // to the end of the out-of-loop common dominator of the user's blocks.
2019 //
2020 // This can be used only on non-throwing instructions with no side effects that
2021 // have at least one use but no environment uses.
2022 void MoveBeforeFirstUserAndOutOfLoops();
2023
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002024#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002025 bool Is##type() const; \
2026 const H##type* As##type() const; \
2027 H##type* As##type();
2028
2029 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2030#undef INSTRUCTION_TYPE_CHECK
2031
2032#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002033 bool Is##type() const { return (As##type() != nullptr); } \
2034 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002035 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002036 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002037#undef INSTRUCTION_TYPE_CHECK
2038
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002039 // Returns whether the instruction can be moved within the graph.
2040 virtual bool CanBeMoved() const { return false; }
2041
2042 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002043 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002044 return false;
2045 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002046
2047 // Returns whether any data encoded in the two instructions is equal.
2048 // This method does not look at the inputs. Both instructions must be
2049 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002050 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002051 return false;
2052 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002053
2054 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002055 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002056 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002057 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002058
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002059 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2060 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2061 // the virtual function because the __attribute__((__pure__)) doesn't really
2062 // apply the strong requirement for virtual functions, preventing optimizations.
2063 InstructionKind GetKind() const PURE;
2064 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002065
2066 virtual size_t ComputeHashCode() const {
2067 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002068 for (const HInstruction* input : GetInputs()) {
2069 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002070 }
2071 return result;
2072 }
2073
2074 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002075 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002076 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002077
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002078 size_t GetLifetimePosition() const { return lifetime_position_; }
2079 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2080 LiveInterval* GetLiveInterval() const { return live_interval_; }
2081 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2082 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2083
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002084 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2085
2086 // Returns whether the code generation of the instruction will require to have access
2087 // to the current method. Such instructions are:
2088 // (1): Instructions that require an environment, as calling the runtime requires
2089 // to walk the stack and have the current method stored at a specific stack address.
2090 // (2): Object literals like classes and strings, that are loaded from the dex cache
2091 // fields of the current method.
2092 bool NeedsCurrentMethod() const {
2093 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2094 }
2095
Vladimir Markodc151b22015-10-15 18:02:30 +01002096 // Returns whether the code generation of the instruction will require to have access
2097 // to the dex cache of the current method's declaring class via the current method.
2098 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002099
Mark Mendellc4701932015-04-10 13:18:51 -04002100 // Does this instruction have any use in an environment before
2101 // control flow hits 'other'?
2102 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2103
2104 // Remove all references to environment uses of this instruction.
2105 // The caller must ensure that this is safe to do.
2106 void RemoveEnvironmentUsers();
2107
Vladimir Markoa1de9182016-02-25 11:37:38 +00002108 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2109 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002110
David Brazdil1abb4192015-02-17 18:33:36 +00002111 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002112 // If set, the machine code for this instruction is assumed to be generated by
2113 // its users. Used by liveness analysis to compute use positions accordingly.
2114 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2115 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2116 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2117 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2118
Vladimir Marko372f10e2016-05-17 16:30:10 +01002119 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2120 return GetInputRecords()[i];
2121 }
2122
2123 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2124 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2125 input_records[index] = input;
2126 }
David Brazdil1abb4192015-02-17 18:33:36 +00002127
Vladimir Markoa1de9182016-02-25 11:37:38 +00002128 uint32_t GetPackedFields() const {
2129 return packed_fields_;
2130 }
2131
2132 template <size_t flag>
2133 bool GetPackedFlag() const {
2134 return (packed_fields_ & (1u << flag)) != 0u;
2135 }
2136
2137 template <size_t flag>
2138 void SetPackedFlag(bool value = true) {
2139 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2140 }
2141
2142 template <typename BitFieldType>
2143 typename BitFieldType::value_type GetPackedField() const {
2144 return BitFieldType::Decode(packed_fields_);
2145 }
2146
2147 template <typename BitFieldType>
2148 void SetPackedField(typename BitFieldType::value_type value) {
2149 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2150 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2151 }
2152
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002153 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002154 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2155 auto before_use_node = uses_.before_begin();
2156 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2157 HInstruction* user = use_node->GetUser();
2158 size_t input_index = use_node->GetIndex();
2159 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2160 before_use_node = use_node;
2161 }
2162 }
2163
2164 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2165 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2166 if (next != uses_.end()) {
2167 HInstruction* next_user = next->GetUser();
2168 size_t next_index = next->GetIndex();
2169 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2170 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2171 }
2172 }
2173
2174 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2175 auto before_env_use_node = env_uses_.before_begin();
2176 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2177 HEnvironment* user = env_use_node->GetUser();
2178 size_t input_index = env_use_node->GetIndex();
2179 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2180 before_env_use_node = env_use_node;
2181 }
2182 }
2183
2184 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2185 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2186 if (next != env_uses_.end()) {
2187 HEnvironment* next_user = next->GetUser();
2188 size_t next_index = next->GetIndex();
2189 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2190 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2191 }
2192 }
David Brazdil1abb4192015-02-17 18:33:36 +00002193
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002194 HInstruction* previous_;
2195 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002196 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002197 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002198
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002199 // An instruction gets an id when it is added to the graph.
2200 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002201 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002202 int id_;
2203
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002204 // When doing liveness analysis, instructions that have uses get an SSA index.
2205 int ssa_index_;
2206
Vladimir Markoa1de9182016-02-25 11:37:38 +00002207 // Packed fields.
2208 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002209
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002210 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002211 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002212
2213 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002214 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002215
Nicolas Geoffray39468442014-09-02 15:17:15 +01002216 // The environment associated with this instruction. Not null if the instruction
2217 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002218 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002219
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002220 // Set by the code generator.
2221 LocationSummary* locations_;
2222
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002223 // Set by the liveness analysis.
2224 LiveInterval* live_interval_;
2225
2226 // Set by the liveness analysis, this is the position in a linear
2227 // order of blocks where this instruction's live interval start.
2228 size_t lifetime_position_;
2229
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002230 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002231
Vladimir Markoa1de9182016-02-25 11:37:38 +00002232 // The reference handle part of the reference type info.
2233 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002234 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002235 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002236
David Brazdil1abb4192015-02-17 18:33:36 +00002237 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002238 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002239 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002240 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002241 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002242
2243 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2244};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002245std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002246
2247class HInstructionIterator : public ValueObject {
2248 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002249 explicit HInstructionIterator(const HInstructionList& instructions)
2250 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002251 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002252 }
2253
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002254 bool Done() const { return instruction_ == nullptr; }
2255 HInstruction* Current() const { return instruction_; }
2256 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002257 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002258 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002259 }
2260
2261 private:
2262 HInstruction* instruction_;
2263 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002264
2265 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002266};
2267
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002268class HBackwardInstructionIterator : public ValueObject {
2269 public:
2270 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2271 : instruction_(instructions.last_instruction_) {
2272 next_ = Done() ? nullptr : instruction_->GetPrevious();
2273 }
2274
2275 bool Done() const { return instruction_ == nullptr; }
2276 HInstruction* Current() const { return instruction_; }
2277 void Advance() {
2278 instruction_ = next_;
2279 next_ = Done() ? nullptr : instruction_->GetPrevious();
2280 }
2281
2282 private:
2283 HInstruction* instruction_;
2284 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002285
2286 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002287};
2288
Vladimir Markof9f64412015-09-02 14:05:49 +01002289template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002290class HTemplateInstruction: public HInstruction {
2291 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002292 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002293 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002294 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002295
Vladimir Marko372f10e2016-05-17 16:30:10 +01002296 using HInstruction::GetInputRecords; // Keep the const version visible.
2297 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2298 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002299 }
2300
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002301 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002302 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002303
2304 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002305};
2306
Vladimir Markof9f64412015-09-02 14:05:49 +01002307// HTemplateInstruction specialization for N=0.
2308template<>
2309class HTemplateInstruction<0>: public HInstruction {
2310 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002311 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002312 : HInstruction(side_effects, dex_pc) {}
2313
Vladimir Markof9f64412015-09-02 14:05:49 +01002314 virtual ~HTemplateInstruction() {}
2315
Vladimir Marko372f10e2016-05-17 16:30:10 +01002316 using HInstruction::GetInputRecords; // Keep the const version visible.
2317 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2318 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002319 }
2320
2321 private:
2322 friend class SsaBuilder;
2323};
2324
Dave Allison20dfc792014-06-16 20:44:29 -07002325template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002326class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002327 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002328 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002329 : HTemplateInstruction<N>(side_effects, dex_pc) {
2330 this->template SetPackedField<TypeField>(type);
2331 }
Dave Allison20dfc792014-06-16 20:44:29 -07002332 virtual ~HExpression() {}
2333
Vladimir Markoa1de9182016-02-25 11:37:38 +00002334 Primitive::Type GetType() const OVERRIDE {
2335 return TypeField::Decode(this->GetPackedFields());
2336 }
Dave Allison20dfc792014-06-16 20:44:29 -07002337
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002338 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002339 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2340 static constexpr size_t kFieldTypeSize =
2341 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2342 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2343 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2344 "Too many packed fields.");
2345 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002346};
2347
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002348// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2349// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002350class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002351 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002352 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2353 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002354
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002355 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002356
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002357 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002358
2359 private:
2360 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2361};
2362
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002363// Represents dex's RETURN opcodes. A HReturn is a control flow
2364// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002365class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002366 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002367 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2368 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002369 SetRawInputAt(0, value);
2370 }
2371
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002372 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002373
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002374 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002375
2376 private:
2377 DISALLOW_COPY_AND_ASSIGN(HReturn);
2378};
2379
Vladimir Markofcb503c2016-05-18 12:48:17 +01002380class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002381 public:
2382 HPhi(ArenaAllocator* arena,
2383 uint32_t reg_number,
2384 size_t number_of_inputs,
2385 Primitive::Type type,
2386 uint32_t dex_pc = kNoDexPc)
2387 : HInstruction(SideEffects::None(), dex_pc),
2388 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2389 reg_number_(reg_number) {
2390 SetPackedField<TypeField>(ToPhiType(type));
2391 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2392 // Phis are constructed live and marked dead if conflicting or unused.
2393 // Individual steps of SsaBuilder should assume that if a phi has been
2394 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2395 SetPackedFlag<kFlagIsLive>(true);
2396 SetPackedFlag<kFlagCanBeNull>(true);
2397 }
2398
2399 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2400 static Primitive::Type ToPhiType(Primitive::Type type) {
2401 return Primitive::PrimitiveKind(type);
2402 }
2403
2404 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2405
Vladimir Marko372f10e2016-05-17 16:30:10 +01002406 using HInstruction::GetInputRecords; // Keep the const version visible.
2407 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2408 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2409 }
David Brazdildee58d62016-04-07 09:54:26 +00002410
2411 void AddInput(HInstruction* input);
2412 void RemoveInputAt(size_t index);
2413
2414 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2415 void SetType(Primitive::Type new_type) {
2416 // Make sure that only valid type changes occur. The following are allowed:
2417 // (1) int -> float/ref (primitive type propagation),
2418 // (2) long -> double (primitive type propagation).
2419 DCHECK(GetType() == new_type ||
2420 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2421 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2422 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2423 SetPackedField<TypeField>(new_type);
2424 }
2425
2426 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2427 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2428
2429 uint32_t GetRegNumber() const { return reg_number_; }
2430
2431 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2432 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2433 bool IsDead() const { return !IsLive(); }
2434 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2435
Vladimir Markoe9004912016-06-16 16:50:52 +01002436 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002437 return other != nullptr
2438 && other->IsPhi()
2439 && other->AsPhi()->GetBlock() == GetBlock()
2440 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2441 }
2442
2443 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2444 // An equivalent phi is a phi having the same dex register and type.
2445 // It assumes that phis with the same dex register are adjacent.
2446 HPhi* GetNextEquivalentPhiWithSameType() {
2447 HInstruction* next = GetNext();
2448 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2449 if (next->GetType() == GetType()) {
2450 return next->AsPhi();
2451 }
2452 next = next->GetNext();
2453 }
2454 return nullptr;
2455 }
2456
2457 DECLARE_INSTRUCTION(Phi);
2458
David Brazdildee58d62016-04-07 09:54:26 +00002459 private:
2460 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2461 static constexpr size_t kFieldTypeSize =
2462 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2463 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2464 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2465 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2466 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2467 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2468
Vladimir Marko372f10e2016-05-17 16:30:10 +01002469 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002470 const uint32_t reg_number_;
2471
2472 DISALLOW_COPY_AND_ASSIGN(HPhi);
2473};
2474
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002475// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002476// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002477// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002478class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002479 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002480 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002481
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002482 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002483
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002484 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002485
2486 private:
2487 DISALLOW_COPY_AND_ASSIGN(HExit);
2488};
2489
2490// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002491class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002492 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002493 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002494
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002495 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002496
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002497 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002498 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002499 }
2500
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002501 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002502
2503 private:
2504 DISALLOW_COPY_AND_ASSIGN(HGoto);
2505};
2506
Roland Levillain9867bc72015-08-05 10:21:34 +01002507class HConstant : public HExpression<0> {
2508 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002509 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2510 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002511
2512 bool CanBeMoved() const OVERRIDE { return true; }
2513
Roland Levillain1a653882016-03-18 18:05:57 +00002514 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002515 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002516 // Is this constant 0 in the arithmetic sense?
2517 virtual bool IsArithmeticZero() const { return false; }
2518 // Is this constant a 0-bit pattern?
2519 virtual bool IsZeroBitPattern() const { return false; }
2520 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002521 virtual bool IsOne() const { return false; }
2522
David Brazdil77a48ae2015-09-15 12:34:04 +00002523 virtual uint64_t GetValueAsUint64() const = 0;
2524
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002525 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002526
2527 private:
2528 DISALLOW_COPY_AND_ASSIGN(HConstant);
2529};
2530
Vladimir Markofcb503c2016-05-18 12:48:17 +01002531class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002532 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002533 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002534 return true;
2535 }
2536
David Brazdil77a48ae2015-09-15 12:34:04 +00002537 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2538
Roland Levillain9867bc72015-08-05 10:21:34 +01002539 size_t ComputeHashCode() const OVERRIDE { return 0; }
2540
Roland Levillain1a653882016-03-18 18:05:57 +00002541 // The null constant representation is a 0-bit pattern.
2542 virtual bool IsZeroBitPattern() const { return true; }
2543
Roland Levillain9867bc72015-08-05 10:21:34 +01002544 DECLARE_INSTRUCTION(NullConstant);
2545
2546 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002547 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002548
2549 friend class HGraph;
2550 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2551};
2552
2553// Constants of the type int. Those can be from Dex instructions, or
2554// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002555class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002556 public:
2557 int32_t GetValue() const { return value_; }
2558
David Brazdil9f389d42015-10-01 14:32:56 +01002559 uint64_t GetValueAsUint64() const OVERRIDE {
2560 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2561 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002562
Vladimir Marko372f10e2016-05-17 16:30:10 +01002563 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002564 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002565 return other->AsIntConstant()->value_ == value_;
2566 }
2567
2568 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2569
2570 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002571 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2572 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002573 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2574
Roland Levillain1a653882016-03-18 18:05:57 +00002575 // Integer constants are used to encode Boolean values as well,
2576 // where 1 means true and 0 means false.
2577 bool IsTrue() const { return GetValue() == 1; }
2578 bool IsFalse() const { return GetValue() == 0; }
2579
Roland Levillain9867bc72015-08-05 10:21:34 +01002580 DECLARE_INSTRUCTION(IntConstant);
2581
2582 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002583 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2584 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2585 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2586 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002587
2588 const int32_t value_;
2589
2590 friend class HGraph;
2591 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2592 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2593 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2594};
2595
Vladimir Markofcb503c2016-05-18 12:48:17 +01002596class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002597 public:
2598 int64_t GetValue() const { return value_; }
2599
David Brazdil77a48ae2015-09-15 12:34:04 +00002600 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2601
Vladimir Marko372f10e2016-05-17 16:30:10 +01002602 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002603 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002604 return other->AsLongConstant()->value_ == value_;
2605 }
2606
2607 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2608
2609 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002610 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2611 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002612 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2613
2614 DECLARE_INSTRUCTION(LongConstant);
2615
2616 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002617 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2618 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002619
2620 const int64_t value_;
2621
2622 friend class HGraph;
2623 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2624};
Dave Allison20dfc792014-06-16 20:44:29 -07002625
Vladimir Markofcb503c2016-05-18 12:48:17 +01002626class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002627 public:
2628 float GetValue() const { return value_; }
2629
2630 uint64_t GetValueAsUint64() const OVERRIDE {
2631 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2632 }
2633
Vladimir Marko372f10e2016-05-17 16:30:10 +01002634 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002635 DCHECK(other->IsFloatConstant()) << other->DebugName();
2636 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2637 }
2638
2639 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2640
2641 bool IsMinusOne() const OVERRIDE {
2642 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2643 }
Roland Levillain1a653882016-03-18 18:05:57 +00002644 bool IsArithmeticZero() const OVERRIDE {
2645 return std::fpclassify(value_) == FP_ZERO;
2646 }
2647 bool IsArithmeticPositiveZero() const {
2648 return IsArithmeticZero() && !std::signbit(value_);
2649 }
2650 bool IsArithmeticNegativeZero() const {
2651 return IsArithmeticZero() && std::signbit(value_);
2652 }
2653 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002654 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002655 }
2656 bool IsOne() const OVERRIDE {
2657 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2658 }
2659 bool IsNaN() const {
2660 return std::isnan(value_);
2661 }
2662
2663 DECLARE_INSTRUCTION(FloatConstant);
2664
2665 private:
2666 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2667 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2668 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2669 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2670
2671 const float value_;
2672
2673 // Only the SsaBuilder and HGraph can create floating-point constants.
2674 friend class SsaBuilder;
2675 friend class HGraph;
2676 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2677};
2678
Vladimir Markofcb503c2016-05-18 12:48:17 +01002679class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002680 public:
2681 double GetValue() const { return value_; }
2682
2683 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2684
Vladimir Marko372f10e2016-05-17 16:30:10 +01002685 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002686 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2687 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2688 }
2689
2690 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2691
2692 bool IsMinusOne() const OVERRIDE {
2693 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2694 }
Roland Levillain1a653882016-03-18 18:05:57 +00002695 bool IsArithmeticZero() const OVERRIDE {
2696 return std::fpclassify(value_) == FP_ZERO;
2697 }
2698 bool IsArithmeticPositiveZero() const {
2699 return IsArithmeticZero() && !std::signbit(value_);
2700 }
2701 bool IsArithmeticNegativeZero() const {
2702 return IsArithmeticZero() && std::signbit(value_);
2703 }
2704 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002705 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002706 }
2707 bool IsOne() const OVERRIDE {
2708 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2709 }
2710 bool IsNaN() const {
2711 return std::isnan(value_);
2712 }
2713
2714 DECLARE_INSTRUCTION(DoubleConstant);
2715
2716 private:
2717 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2718 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2719 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2720 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2721
2722 const double value_;
2723
2724 // Only the SsaBuilder and HGraph can create floating-point constants.
2725 friend class SsaBuilder;
2726 friend class HGraph;
2727 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2728};
2729
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002730// Conditional branch. A block ending with an HIf instruction must have
2731// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002732class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002733 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002734 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2735 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002736 SetRawInputAt(0, input);
2737 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002738
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002739 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002740
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002741 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002742 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002743 }
2744
2745 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002746 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002747 }
2748
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002749 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002750
2751 private:
2752 DISALLOW_COPY_AND_ASSIGN(HIf);
2753};
2754
David Brazdilfc6a86a2015-06-26 10:33:45 +00002755
2756// Abstract instruction which marks the beginning and/or end of a try block and
2757// links it to the respective exception handlers. Behaves the same as a Goto in
2758// non-exceptional control flow.
2759// Normal-flow successor is stored at index zero, exception handlers under
2760// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002761class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002762 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002763 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002764 kEntry,
2765 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002766 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002767 };
2768
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002769 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002770 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2771 SetPackedField<BoundaryKindField>(kind);
2772 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002773
2774 bool IsControlFlow() const OVERRIDE { return true; }
2775
2776 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002777 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002778
David Brazdild26a4112015-11-10 11:07:31 +00002779 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2780 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2781 }
2782
David Brazdilfc6a86a2015-06-26 10:33:45 +00002783 // Returns whether `handler` is among its exception handlers (non-zero index
2784 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002785 bool HasExceptionHandler(const HBasicBlock& handler) const {
2786 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002787 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002788 }
2789
2790 // If not present already, adds `handler` to its block's list of exception
2791 // handlers.
2792 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002793 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002794 GetBlock()->AddSuccessor(handler);
2795 }
2796 }
2797
Vladimir Markoa1de9182016-02-25 11:37:38 +00002798 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2799 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002800
David Brazdilffee3d32015-07-06 11:48:53 +01002801 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2802
David Brazdilfc6a86a2015-06-26 10:33:45 +00002803 DECLARE_INSTRUCTION(TryBoundary);
2804
2805 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002806 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2807 static constexpr size_t kFieldBoundaryKindSize =
2808 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2809 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2810 kFieldBoundaryKind + kFieldBoundaryKindSize;
2811 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2812 "Too many packed fields.");
2813 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002814
2815 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2816};
2817
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002818// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002819class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002820 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002821 // We set CanTriggerGC to prevent any intermediate address to be live
2822 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002823 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002824 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002825 SetRawInputAt(0, cond);
2826 }
2827
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002828 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002829 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002830 return true;
2831 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002832 bool NeedsEnvironment() const OVERRIDE { return true; }
2833 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002834
2835 DECLARE_INSTRUCTION(Deoptimize);
2836
2837 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002838 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2839};
2840
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002841// Represents the ArtMethod that was passed as a first argument to
2842// the method. It is used by instructions that depend on it, like
2843// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002844class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002845 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002846 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2847 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002848
2849 DECLARE_INSTRUCTION(CurrentMethod);
2850
2851 private:
2852 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2853};
2854
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002855// Fetches an ArtMethod from the virtual table or the interface method table
2856// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002857class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002858 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002859 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002860 kVTable,
2861 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002862 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002863 };
2864 HClassTableGet(HInstruction* cls,
2865 Primitive::Type type,
2866 TableKind kind,
2867 size_t index,
2868 uint32_t dex_pc)
2869 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002870 index_(index) {
2871 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002872 SetRawInputAt(0, cls);
2873 }
2874
2875 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002876 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002877 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002878 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002879 }
2880
Vladimir Markoa1de9182016-02-25 11:37:38 +00002881 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002882 size_t GetIndex() const { return index_; }
2883
2884 DECLARE_INSTRUCTION(ClassTableGet);
2885
2886 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002887 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2888 static constexpr size_t kFieldTableKindSize =
2889 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2890 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2891 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2892 "Too many packed fields.");
2893 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2894
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002895 // The index of the ArtMethod in the table.
2896 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002897
2898 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2899};
2900
Mark Mendellfe57faa2015-09-18 09:26:15 -04002901// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2902// have one successor for each entry in the switch table, and the final successor
2903// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002904class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002905 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002906 HPackedSwitch(int32_t start_value,
2907 uint32_t num_entries,
2908 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002909 uint32_t dex_pc = kNoDexPc)
2910 : HTemplateInstruction(SideEffects::None(), dex_pc),
2911 start_value_(start_value),
2912 num_entries_(num_entries) {
2913 SetRawInputAt(0, input);
2914 }
2915
2916 bool IsControlFlow() const OVERRIDE { return true; }
2917
2918 int32_t GetStartValue() const { return start_value_; }
2919
Vladimir Marko211c2112015-09-24 16:52:33 +01002920 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002921
2922 HBasicBlock* GetDefaultBlock() const {
2923 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002924 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002925 }
2926 DECLARE_INSTRUCTION(PackedSwitch);
2927
2928 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002929 const int32_t start_value_;
2930 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002931
2932 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2933};
2934
Roland Levillain88cb1752014-10-20 16:36:47 +01002935class HUnaryOperation : public HExpression<1> {
2936 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002937 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2938 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002939 SetRawInputAt(0, input);
2940 }
2941
2942 HInstruction* GetInput() const { return InputAt(0); }
2943 Primitive::Type GetResultType() const { return GetType(); }
2944
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002945 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002946 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002947 return true;
2948 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002949
Roland Levillain31dd3d62016-02-16 12:21:02 +00002950 // Try to statically evaluate `this` and return a HConstant
2951 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002952 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002953 HConstant* TryStaticEvaluation() const;
2954
2955 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002956 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2957 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002958 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2959 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002960
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002961 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002962
2963 private:
2964 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2965};
2966
Dave Allison20dfc792014-06-16 20:44:29 -07002967class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002968 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002969 HBinaryOperation(Primitive::Type result_type,
2970 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002971 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002972 SideEffects side_effects = SideEffects::None(),
2973 uint32_t dex_pc = kNoDexPc)
2974 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002975 SetRawInputAt(0, left);
2976 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002977 }
2978
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002979 HInstruction* GetLeft() const { return InputAt(0); }
2980 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002981 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002982
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002983 virtual bool IsCommutative() const { return false; }
2984
2985 // Put constant on the right.
2986 // Returns whether order is changed.
2987 bool OrderInputsWithConstantOnTheRight() {
2988 HInstruction* left = InputAt(0);
2989 HInstruction* right = InputAt(1);
2990 if (left->IsConstant() && !right->IsConstant()) {
2991 ReplaceInput(right, 0);
2992 ReplaceInput(left, 1);
2993 return true;
2994 }
2995 return false;
2996 }
2997
2998 // Order inputs by instruction id, but favor constant on the right side.
2999 // This helps GVN for commutative ops.
3000 void OrderInputs() {
3001 DCHECK(IsCommutative());
3002 HInstruction* left = InputAt(0);
3003 HInstruction* right = InputAt(1);
3004 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3005 return;
3006 }
3007 if (OrderInputsWithConstantOnTheRight()) {
3008 return;
3009 }
3010 // Order according to instruction id.
3011 if (left->GetId() > right->GetId()) {
3012 ReplaceInput(right, 0);
3013 ReplaceInput(left, 1);
3014 }
3015 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003016
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003017 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003018 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003019 return true;
3020 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003021
Roland Levillain31dd3d62016-02-16 12:21:02 +00003022 // Try to statically evaluate `this` and return a HConstant
3023 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003024 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003025 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003026
3027 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003028 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3029 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003030 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3031 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003032 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003033 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3034 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003035 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3036 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003037 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3038 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003039 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003040 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3041 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003042
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003043 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003044 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003045 HConstant* GetConstantRight() const;
3046
3047 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003048 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003049 HInstruction* GetLeastConstantLeft() const;
3050
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003051 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003052
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003053 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003054 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3055};
3056
Mark Mendellc4701932015-04-10 13:18:51 -04003057// The comparison bias applies for floating point operations and indicates how NaN
3058// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003059enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003060 kNoBias, // bias is not applicable (i.e. for long operation)
3061 kGtBias, // return 1 for NaN comparisons
3062 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003063 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003064};
3065
Roland Levillain31dd3d62016-02-16 12:21:02 +00003066std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3067
Dave Allison20dfc792014-06-16 20:44:29 -07003068class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003069 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003070 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003071 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3072 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3073 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003074
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003075 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003076 // `instruction`, and disregard moves in between.
3077 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003078
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003079 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003080
3081 virtual IfCondition GetCondition() const = 0;
3082
Mark Mendellc4701932015-04-10 13:18:51 -04003083 virtual IfCondition GetOppositeCondition() const = 0;
3084
Vladimir Markoa1de9182016-02-25 11:37:38 +00003085 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003086 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3087
Vladimir Markoa1de9182016-02-25 11:37:38 +00003088 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3089 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003090
Vladimir Marko372f10e2016-05-17 16:30:10 +01003091 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003092 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003093 }
3094
Roland Levillain4fa13f62015-07-06 18:11:54 +01003095 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003096 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003097 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003098 if (if_cond == kCondNE) {
3099 return true;
3100 } else if (if_cond == kCondEQ) {
3101 return false;
3102 }
3103 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003104 }
3105
3106 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003107 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003108 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003109 if (if_cond == kCondEQ) {
3110 return true;
3111 } else if (if_cond == kCondNE) {
3112 return false;
3113 }
3114 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003115 }
3116
Roland Levillain31dd3d62016-02-16 12:21:02 +00003117 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003118 // Needed if we merge a HCompare into a HCondition.
3119 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3120 static constexpr size_t kFieldComparisonBiasSize =
3121 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3122 static constexpr size_t kNumberOfConditionPackedBits =
3123 kFieldComparisonBias + kFieldComparisonBiasSize;
3124 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3125 using ComparisonBiasField =
3126 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3127
Roland Levillain31dd3d62016-02-16 12:21:02 +00003128 template <typename T>
3129 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3130
3131 template <typename T>
3132 int32_t CompareFP(T x, T y) const {
3133 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3134 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3135 // Handle the bias.
3136 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3137 }
3138
3139 // Return an integer constant containing the result of a condition evaluated at compile time.
3140 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3141 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3142 }
3143
Dave Allison20dfc792014-06-16 20:44:29 -07003144 private:
3145 DISALLOW_COPY_AND_ASSIGN(HCondition);
3146};
3147
3148// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003149class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003150 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003151 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3152 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003153
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003154 bool IsCommutative() const OVERRIDE { return true; }
3155
Vladimir Marko9e23df52015-11-10 17:14:35 +00003156 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3157 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003158 return MakeConstantCondition(true, GetDexPc());
3159 }
3160 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3161 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3162 }
3163 // In the following Evaluate methods, a HCompare instruction has
3164 // been merged into this HEqual instruction; evaluate it as
3165 // `Compare(x, y) == 0`.
3166 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3167 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3168 GetDexPc());
3169 }
3170 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3171 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3172 }
3173 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3174 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003175 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003176
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003177 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003178
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003179 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003180 return kCondEQ;
3181 }
3182
Mark Mendellc4701932015-04-10 13:18:51 -04003183 IfCondition GetOppositeCondition() const OVERRIDE {
3184 return kCondNE;
3185 }
3186
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003187 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003188 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003189
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003190 DISALLOW_COPY_AND_ASSIGN(HEqual);
3191};
3192
Vladimir Markofcb503c2016-05-18 12:48:17 +01003193class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003194 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003195 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3196 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003197
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003198 bool IsCommutative() const OVERRIDE { return true; }
3199
Vladimir Marko9e23df52015-11-10 17:14:35 +00003200 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3201 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003202 return MakeConstantCondition(false, GetDexPc());
3203 }
3204 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3205 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3206 }
3207 // In the following Evaluate methods, a HCompare instruction has
3208 // been merged into this HNotEqual instruction; evaluate it as
3209 // `Compare(x, y) != 0`.
3210 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3211 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3212 }
3213 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3214 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3215 }
3216 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3217 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003218 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003219
Dave Allison20dfc792014-06-16 20:44:29 -07003220 DECLARE_INSTRUCTION(NotEqual);
3221
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003222 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003223 return kCondNE;
3224 }
3225
Mark Mendellc4701932015-04-10 13:18:51 -04003226 IfCondition GetOppositeCondition() const OVERRIDE {
3227 return kCondEQ;
3228 }
3229
Dave Allison20dfc792014-06-16 20:44:29 -07003230 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003231 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003232
Dave Allison20dfc792014-06-16 20:44:29 -07003233 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3234};
3235
Vladimir Markofcb503c2016-05-18 12:48:17 +01003236class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003237 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003238 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3239 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003240
Roland Levillain9867bc72015-08-05 10:21:34 +01003241 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003242 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003243 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003244 // In the following Evaluate methods, a HCompare instruction has
3245 // been merged into this HLessThan instruction; evaluate it as
3246 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003247 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003248 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3249 }
3250 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3251 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3252 }
3253 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3254 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003255 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003256
Dave Allison20dfc792014-06-16 20:44:29 -07003257 DECLARE_INSTRUCTION(LessThan);
3258
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003259 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003260 return kCondLT;
3261 }
3262
Mark Mendellc4701932015-04-10 13:18:51 -04003263 IfCondition GetOppositeCondition() const OVERRIDE {
3264 return kCondGE;
3265 }
3266
Dave Allison20dfc792014-06-16 20:44:29 -07003267 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003268 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003269
Dave Allison20dfc792014-06-16 20:44:29 -07003270 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3271};
3272
Vladimir Markofcb503c2016-05-18 12:48:17 +01003273class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003274 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003275 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3276 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003277
Roland Levillain9867bc72015-08-05 10:21:34 +01003278 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003279 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003280 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003281 // In the following Evaluate methods, a HCompare instruction has
3282 // been merged into this HLessThanOrEqual instruction; evaluate it as
3283 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003284 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003285 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3286 }
3287 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3288 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3289 }
3290 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3291 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003292 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003293
Dave Allison20dfc792014-06-16 20:44:29 -07003294 DECLARE_INSTRUCTION(LessThanOrEqual);
3295
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003296 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003297 return kCondLE;
3298 }
3299
Mark Mendellc4701932015-04-10 13:18:51 -04003300 IfCondition GetOppositeCondition() const OVERRIDE {
3301 return kCondGT;
3302 }
3303
Dave Allison20dfc792014-06-16 20:44:29 -07003304 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003305 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003306
Dave Allison20dfc792014-06-16 20:44:29 -07003307 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3308};
3309
Vladimir Markofcb503c2016-05-18 12:48:17 +01003310class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003311 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003312 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3313 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003314
Roland Levillain9867bc72015-08-05 10:21:34 +01003315 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003316 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003317 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003318 // In the following Evaluate methods, a HCompare instruction has
3319 // been merged into this HGreaterThan instruction; evaluate it as
3320 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003321 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003322 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3323 }
3324 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3325 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3326 }
3327 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3328 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003329 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003330
Dave Allison20dfc792014-06-16 20:44:29 -07003331 DECLARE_INSTRUCTION(GreaterThan);
3332
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003333 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003334 return kCondGT;
3335 }
3336
Mark Mendellc4701932015-04-10 13:18:51 -04003337 IfCondition GetOppositeCondition() const OVERRIDE {
3338 return kCondLE;
3339 }
3340
Dave Allison20dfc792014-06-16 20:44:29 -07003341 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003342 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003343
Dave Allison20dfc792014-06-16 20:44:29 -07003344 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3345};
3346
Vladimir Markofcb503c2016-05-18 12:48:17 +01003347class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003348 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003349 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3350 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003351
Roland Levillain9867bc72015-08-05 10:21:34 +01003352 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003353 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003354 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003355 // In the following Evaluate methods, a HCompare instruction has
3356 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3357 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003358 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003359 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3360 }
3361 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3362 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3363 }
3364 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3365 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003366 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003367
Dave Allison20dfc792014-06-16 20:44:29 -07003368 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3369
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003370 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003371 return kCondGE;
3372 }
3373
Mark Mendellc4701932015-04-10 13:18:51 -04003374 IfCondition GetOppositeCondition() const OVERRIDE {
3375 return kCondLT;
3376 }
3377
Dave Allison20dfc792014-06-16 20:44:29 -07003378 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003379 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003380
Dave Allison20dfc792014-06-16 20:44:29 -07003381 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3382};
3383
Vladimir Markofcb503c2016-05-18 12:48:17 +01003384class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003385 public:
3386 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3387 : HCondition(first, second, dex_pc) {}
3388
3389 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003390 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003391 }
3392 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003393 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3394 }
3395 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3396 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3397 LOG(FATAL) << DebugName() << " is not defined for float values";
3398 UNREACHABLE();
3399 }
3400 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3401 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3402 LOG(FATAL) << DebugName() << " is not defined for double values";
3403 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003404 }
3405
3406 DECLARE_INSTRUCTION(Below);
3407
3408 IfCondition GetCondition() const OVERRIDE {
3409 return kCondB;
3410 }
3411
3412 IfCondition GetOppositeCondition() const OVERRIDE {
3413 return kCondAE;
3414 }
3415
3416 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003417 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003418 return MakeUnsigned(x) < MakeUnsigned(y);
3419 }
Aart Bike9f37602015-10-09 11:15:55 -07003420
3421 DISALLOW_COPY_AND_ASSIGN(HBelow);
3422};
3423
Vladimir Markofcb503c2016-05-18 12:48:17 +01003424class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003425 public:
3426 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3427 : HCondition(first, second, dex_pc) {}
3428
3429 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003430 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003431 }
3432 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003433 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3434 }
3435 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3436 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3437 LOG(FATAL) << DebugName() << " is not defined for float values";
3438 UNREACHABLE();
3439 }
3440 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3441 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3442 LOG(FATAL) << DebugName() << " is not defined for double values";
3443 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003444 }
3445
3446 DECLARE_INSTRUCTION(BelowOrEqual);
3447
3448 IfCondition GetCondition() const OVERRIDE {
3449 return kCondBE;
3450 }
3451
3452 IfCondition GetOppositeCondition() const OVERRIDE {
3453 return kCondA;
3454 }
3455
3456 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003457 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003458 return MakeUnsigned(x) <= MakeUnsigned(y);
3459 }
Aart Bike9f37602015-10-09 11:15:55 -07003460
3461 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3462};
3463
Vladimir Markofcb503c2016-05-18 12:48:17 +01003464class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003465 public:
3466 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3467 : HCondition(first, second, dex_pc) {}
3468
3469 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003470 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003471 }
3472 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003473 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3474 }
3475 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3476 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3477 LOG(FATAL) << DebugName() << " is not defined for float values";
3478 UNREACHABLE();
3479 }
3480 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3481 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3482 LOG(FATAL) << DebugName() << " is not defined for double values";
3483 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003484 }
3485
3486 DECLARE_INSTRUCTION(Above);
3487
3488 IfCondition GetCondition() const OVERRIDE {
3489 return kCondA;
3490 }
3491
3492 IfCondition GetOppositeCondition() const OVERRIDE {
3493 return kCondBE;
3494 }
3495
3496 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003497 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003498 return MakeUnsigned(x) > MakeUnsigned(y);
3499 }
Aart Bike9f37602015-10-09 11:15:55 -07003500
3501 DISALLOW_COPY_AND_ASSIGN(HAbove);
3502};
3503
Vladimir Markofcb503c2016-05-18 12:48:17 +01003504class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003505 public:
3506 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3507 : HCondition(first, second, dex_pc) {}
3508
3509 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003510 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003511 }
3512 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003513 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3514 }
3515 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3516 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3517 LOG(FATAL) << DebugName() << " is not defined for float values";
3518 UNREACHABLE();
3519 }
3520 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3521 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3522 LOG(FATAL) << DebugName() << " is not defined for double values";
3523 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003524 }
3525
3526 DECLARE_INSTRUCTION(AboveOrEqual);
3527
3528 IfCondition GetCondition() const OVERRIDE {
3529 return kCondAE;
3530 }
3531
3532 IfCondition GetOppositeCondition() const OVERRIDE {
3533 return kCondB;
3534 }
3535
3536 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003537 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003538 return MakeUnsigned(x) >= MakeUnsigned(y);
3539 }
Aart Bike9f37602015-10-09 11:15:55 -07003540
3541 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3542};
Dave Allison20dfc792014-06-16 20:44:29 -07003543
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003544// Instruction to check how two inputs compare to each other.
3545// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003546class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003547 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003548 // Note that `comparison_type` is the type of comparison performed
3549 // between the comparison's inputs, not the type of the instantiated
3550 // HCompare instruction (which is always Primitive::kPrimInt).
3551 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003552 HInstruction* first,
3553 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003554 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003555 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003556 : HBinaryOperation(Primitive::kPrimInt,
3557 first,
3558 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003559 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003560 dex_pc) {
3561 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003562 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3563 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003564 }
3565
Roland Levillain9867bc72015-08-05 10:21:34 +01003566 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003567 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3568
3569 template <typename T>
3570 int32_t ComputeFP(T x, T y) const {
3571 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3572 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3573 // Handle the bias.
3574 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3575 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003576
Roland Levillain9867bc72015-08-05 10:21:34 +01003577 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003578 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3579 // reach this code path when processing a freshly built HIR
3580 // graph. However HCompare integer instructions can be synthesized
3581 // by the instruction simplifier to implement IntegerCompare and
3582 // IntegerSignum intrinsics, so we have to handle this case.
3583 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003584 }
3585 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003586 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3587 }
3588 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3589 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3590 }
3591 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3592 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003593 }
3594
Vladimir Marko372f10e2016-05-17 16:30:10 +01003595 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003596 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003597 }
3598
Vladimir Markoa1de9182016-02-25 11:37:38 +00003599 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003600
Roland Levillain31dd3d62016-02-16 12:21:02 +00003601 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003602 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003603 bool IsGtBias() const {
3604 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003605 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003606 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003607
Roland Levillain1693a1f2016-03-15 14:57:31 +00003608 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3609 // Comparisons do not require a runtime call in any back end.
3610 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003611 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003612
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003613 DECLARE_INSTRUCTION(Compare);
3614
Roland Levillain31dd3d62016-02-16 12:21:02 +00003615 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003616 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3617 static constexpr size_t kFieldComparisonBiasSize =
3618 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3619 static constexpr size_t kNumberOfComparePackedBits =
3620 kFieldComparisonBias + kFieldComparisonBiasSize;
3621 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3622 using ComparisonBiasField =
3623 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3624
Roland Levillain31dd3d62016-02-16 12:21:02 +00003625 // Return an integer constant containing the result of a comparison evaluated at compile time.
3626 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3627 DCHECK(value == -1 || value == 0 || value == 1) << value;
3628 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3629 }
3630
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003631 private:
3632 DISALLOW_COPY_AND_ASSIGN(HCompare);
3633};
3634
Vladimir Markofcb503c2016-05-18 12:48:17 +01003635class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003636 public:
3637 HNewInstance(HInstruction* cls,
3638 HCurrentMethod* current_method,
3639 uint32_t dex_pc,
3640 uint16_t type_index,
3641 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003642 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003643 bool finalizable,
3644 QuickEntrypointEnum entrypoint)
3645 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3646 type_index_(type_index),
3647 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003648 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003649 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003650 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003651 SetRawInputAt(0, cls);
3652 SetRawInputAt(1, current_method);
3653 }
3654
3655 uint16_t GetTypeIndex() const { return type_index_; }
3656 const DexFile& GetDexFile() const { return dex_file_; }
3657
3658 // Calls runtime so needs an environment.
3659 bool NeedsEnvironment() const OVERRIDE { return true; }
3660
Mingyao Yang062157f2016-03-02 10:15:36 -08003661 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3662 bool CanThrow() const OVERRIDE { return true; }
3663
3664 // Needs to call into runtime to make sure it's instantiable/accessible.
3665 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003666
Vladimir Markoa1de9182016-02-25 11:37:38 +00003667 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003668
3669 bool CanBeNull() const OVERRIDE { return false; }
3670
3671 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3672
3673 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3674 entrypoint_ = entrypoint;
3675 }
3676
3677 bool IsStringAlloc() const;
3678
3679 DECLARE_INSTRUCTION(NewInstance);
3680
3681 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003682 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3683 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003684 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3685 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3686 "Too many packed fields.");
3687
David Brazdil6de19382016-01-08 17:37:10 +00003688 const uint16_t type_index_;
3689 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003690 QuickEntrypointEnum entrypoint_;
3691
3692 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3693};
3694
Agi Csaki05f20562015-08-19 14:58:14 -07003695enum IntrinsicNeedsEnvironmentOrCache {
3696 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3697 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003698};
3699
Aart Bik5d75afe2015-12-14 11:57:01 -08003700enum IntrinsicSideEffects {
3701 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3702 kReadSideEffects, // Intrinsic may read heap memory.
3703 kWriteSideEffects, // Intrinsic may write heap memory.
3704 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3705};
3706
3707enum IntrinsicExceptions {
3708 kNoThrow, // Intrinsic does not throw any exceptions.
3709 kCanThrow // Intrinsic may throw exceptions.
3710};
3711
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003712class HInvoke : public HInstruction {
3713 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003714 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003715
Vladimir Marko372f10e2016-05-17 16:30:10 +01003716 using HInstruction::GetInputRecords; // Keep the const version visible.
3717 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3718 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3719 }
3720
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003721 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003722 SetRawInputAt(index, argument);
3723 }
3724
Roland Levillain3e3d7332015-04-28 11:00:54 +01003725 // Return the number of arguments. This number can be lower than
3726 // the number of inputs returned by InputCount(), as some invoke
3727 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3728 // inputs at the end of their list of inputs.
3729 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3730
Vladimir Markoa1de9182016-02-25 11:37:38 +00003731 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003732
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003733 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003734 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003735
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003736 InvokeType GetInvokeType() const {
3737 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003738 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003739
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003740 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003741 return intrinsic_;
3742 }
3743
Aart Bik5d75afe2015-12-14 11:57:01 -08003744 void SetIntrinsic(Intrinsics intrinsic,
3745 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3746 IntrinsicSideEffects side_effects,
3747 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003748
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003749 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003750 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003751 }
3752
Vladimir Markoa1de9182016-02-25 11:37:38 +00003753 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003754
3755 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3756
Vladimir Marko372f10e2016-05-17 16:30:10 +01003757 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003758 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3759 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003760
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003761 uint32_t* GetIntrinsicOptimizations() {
3762 return &intrinsic_optimizations_;
3763 }
3764
3765 const uint32_t* GetIntrinsicOptimizations() const {
3766 return &intrinsic_optimizations_;
3767 }
3768
3769 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3770
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003771 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3772
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003773 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003774
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003775 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003776 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3777 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003778 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3779 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003780 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003781 static constexpr size_t kFieldReturnTypeSize =
3782 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3783 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3784 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3785 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003786 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003787 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3788
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003789 HInvoke(ArenaAllocator* arena,
3790 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003791 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003792 Primitive::Type return_type,
3793 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003794 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003795 ArtMethod* resolved_method,
3796 InvokeType invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003797 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003798 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003799 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003800 resolved_method_(resolved_method),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003801 inputs_(number_of_arguments + number_of_other_inputs,
3802 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003803 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003804 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003805 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003806 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003807 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003808 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003809 }
3810
Vladimir Markoa1de9182016-02-25 11:37:38 +00003811 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003812
Roland Levillain3e3d7332015-04-28 11:00:54 +01003813 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003814 ArtMethod* const resolved_method_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003815 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003816 const uint32_t dex_method_index_;
3817 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003818
3819 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3820 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003821
3822 private:
3823 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3824};
3825
Vladimir Markofcb503c2016-05-18 12:48:17 +01003826class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003827 public:
3828 HInvokeUnresolved(ArenaAllocator* arena,
3829 uint32_t number_of_arguments,
3830 Primitive::Type return_type,
3831 uint32_t dex_pc,
3832 uint32_t dex_method_index,
3833 InvokeType invoke_type)
3834 : HInvoke(arena,
3835 number_of_arguments,
3836 0u /* number_of_other_inputs */,
3837 return_type,
3838 dex_pc,
3839 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003840 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003841 invoke_type) {
3842 }
3843
3844 DECLARE_INSTRUCTION(InvokeUnresolved);
3845
3846 private:
3847 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3848};
3849
Vladimir Markofcb503c2016-05-18 12:48:17 +01003850class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003851 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003852 // Requirements of this method call regarding the class
3853 // initialization (clinit) check of its declaring class.
3854 enum class ClinitCheckRequirement {
3855 kNone, // Class already initialized.
3856 kExplicit, // Static call having explicit clinit check as last input.
3857 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003858 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003859 };
3860
Vladimir Marko58155012015-08-19 12:49:41 +00003861 // Determines how to load the target ArtMethod*.
3862 enum class MethodLoadKind {
3863 // Use a String init ArtMethod* loaded from Thread entrypoints.
3864 kStringInit,
3865
3866 // Use the method's own ArtMethod* loaded by the register allocator.
3867 kRecursive,
3868
3869 // Use ArtMethod* at a known address, embed the direct address in the code.
3870 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3871 kDirectAddress,
3872
3873 // Use ArtMethod* at an address that will be known at link time, embed the direct
3874 // address in the code. If the image is relocatable, emit .patch_oat entry.
3875 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3876 // the image relocatable or not.
3877 kDirectAddressWithFixup,
3878
Vladimir Markoa1de9182016-02-25 11:37:38 +00003879 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003880 // Used when we need to use the dex cache, for example for invoke-static that
3881 // may cause class initialization (the entry may point to a resolution method),
3882 // and we know that we can access the dex cache arrays using a PC-relative load.
3883 kDexCachePcRelative,
3884
3885 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3886 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3887 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3888 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3889 kDexCacheViaMethod,
3890 };
3891
3892 // Determines the location of the code pointer.
3893 enum class CodePtrLocation {
3894 // Recursive call, use local PC-relative call instruction.
3895 kCallSelf,
3896
3897 // Use PC-relative call instruction patched at link time.
3898 // Used for calls within an oat file, boot->boot or app->app.
3899 kCallPCRelative,
3900
3901 // Call to a known target address, embed the direct address in code.
3902 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3903 kCallDirect,
3904
3905 // Call to a target address that will be known at link time, embed the direct
3906 // address in code. If the image is relocatable, emit .patch_oat entry.
3907 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3908 // the image relocatable or not.
3909 kCallDirectWithFixup,
3910
3911 // Use code pointer from the ArtMethod*.
3912 // Used when we don't know the target code. This is also the last-resort-kind used when
3913 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3914 kCallArtMethod,
3915 };
3916
3917 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003918 MethodLoadKind method_load_kind;
3919 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003920 // The method load data holds
3921 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3922 // Note that there are multiple string init methods, each having its own offset.
3923 // - the method address for kDirectAddress
3924 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003925 uint64_t method_load_data;
3926 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003927 };
3928
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003929 HInvokeStaticOrDirect(ArenaAllocator* arena,
3930 uint32_t number_of_arguments,
3931 Primitive::Type return_type,
3932 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003933 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003934 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00003935 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003936 InvokeType invoke_type,
3937 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003938 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003939 : HInvoke(arena,
3940 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003941 // There is potentially one extra argument for the HCurrentMethod node, and
3942 // potentially one other if the clinit check is explicit, and potentially
3943 // one other if the method is a string factory.
3944 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003945 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003946 return_type,
3947 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003948 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003949 resolved_method,
3950 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003951 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003952 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003953 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3954 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003955
Vladimir Markodc151b22015-10-15 18:02:30 +01003956 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003957 bool had_current_method_input = HasCurrentMethodInput();
3958 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3959
3960 // Using the current method is the default and once we find a better
3961 // method load kind, we should not go back to using the current method.
3962 DCHECK(had_current_method_input || !needs_current_method_input);
3963
3964 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003965 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3966 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003967 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003968 dispatch_info_ = dispatch_info;
3969 }
3970
Vladimir Markoc53c0792015-11-19 15:48:33 +00003971 void AddSpecialInput(HInstruction* input) {
3972 // We allow only one special input.
3973 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3974 DCHECK(InputCount() == GetSpecialInputIndex() ||
3975 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3976 InsertInputAt(GetSpecialInputIndex(), input);
3977 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003978
Vladimir Marko372f10e2016-05-17 16:30:10 +01003979 using HInstruction::GetInputRecords; // Keep the const version visible.
3980 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3981 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
3982 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
3983 DCHECK(!input_records.empty());
3984 DCHECK_GT(input_records.size(), GetNumberOfArguments());
3985 HInstruction* last_input = input_records.back().GetInstruction();
3986 // Note: `last_input` may be null during arguments setup.
3987 if (last_input != nullptr) {
3988 // `last_input` is the last input of a static invoke marked as having
3989 // an explicit clinit check. It must either be:
3990 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3991 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3992 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
3993 }
3994 }
3995 return input_records;
3996 }
3997
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003998 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003999 // We access the method via the dex cache so we can't do an implicit null check.
4000 // TODO: for intrinsics we can generate implicit null checks.
4001 return false;
4002 }
4003
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004004 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004005 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004006 }
4007
Vladimir Markoc53c0792015-11-19 15:48:33 +00004008 // Get the index of the special input, if any.
4009 //
David Brazdil6de19382016-01-08 17:37:10 +00004010 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4011 // method pointer; otherwise there may be one platform-specific special input,
4012 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004013 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004014 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004015
Vladimir Marko58155012015-08-19 12:49:41 +00004016 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4017 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4018 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004019 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004020 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004021 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004022 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004023 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4024 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004025 bool HasCurrentMethodInput() const {
4026 // This function can be called only after the invoke has been fully initialized by the builder.
4027 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004028 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004029 return true;
4030 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004031 DCHECK(InputCount() == GetSpecialInputIndex() ||
4032 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004033 return false;
4034 }
4035 }
Vladimir Marko58155012015-08-19 12:49:41 +00004036 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
Vladimir Marko58155012015-08-19 12:49:41 +00004037
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004038 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004039 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004040 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004041 }
4042
4043 uint64_t GetMethodAddress() const {
4044 DCHECK(HasMethodAddress());
4045 return dispatch_info_.method_load_data;
4046 }
4047
4048 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004049 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004050 return dispatch_info_.method_load_data;
4051 }
4052
4053 uint64_t GetDirectCodePtr() const {
4054 DCHECK(HasDirectCodePtr());
4055 return dispatch_info_.direct_code_ptr;
4056 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004057
Vladimir Markoa1de9182016-02-25 11:37:38 +00004058 ClinitCheckRequirement GetClinitCheckRequirement() const {
4059 return GetPackedField<ClinitCheckRequirementField>();
4060 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004061
Roland Levillain4c0eb422015-04-24 16:43:49 +01004062 // Is this instruction a call to a static method?
4063 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004064 return GetInvokeType() == kStatic;
4065 }
4066
4067 MethodReference GetTargetMethod() const {
4068 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004069 }
4070
Vladimir Markofbb184a2015-11-13 14:47:00 +00004071 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4072 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4073 // instruction; only relevant for static calls with explicit clinit check.
4074 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004075 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004076 size_t last_input_index = inputs_.size() - 1u;
4077 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004078 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004079 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004080 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004081 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004082 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004083 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004084 }
4085
4086 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004087 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004088 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004089 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004090 }
4091
4092 // Is this a call to a static method whose declaring class has an
4093 // implicit intialization check requirement?
4094 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004095 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004096 }
4097
Vladimir Markob554b5a2015-11-06 12:57:55 +00004098 // Does this method load kind need the current method as an input?
4099 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4100 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4101 }
4102
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004103 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004104
Roland Levillain4c0eb422015-04-24 16:43:49 +01004105 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004106 void InsertInputAt(size_t index, HInstruction* input);
4107 void RemoveInputAt(size_t index);
4108
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004109 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004110 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004111 static constexpr size_t kFieldClinitCheckRequirementSize =
4112 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4113 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4114 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4115 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4116 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004117 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4118 kFieldClinitCheckRequirement,
4119 kFieldClinitCheckRequirementSize>;
4120
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004121 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004122 MethodReference target_method_;
4123 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004124
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004125 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004126};
Vladimir Markof64242a2015-12-01 14:58:23 +00004127std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004128std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004129
Vladimir Markofcb503c2016-05-18 12:48:17 +01004130class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004131 public:
4132 HInvokeVirtual(ArenaAllocator* arena,
4133 uint32_t number_of_arguments,
4134 Primitive::Type return_type,
4135 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004136 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004137 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004138 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004139 : HInvoke(arena,
4140 number_of_arguments,
4141 0u,
4142 return_type,
4143 dex_pc,
4144 dex_method_index,
4145 resolved_method,
4146 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004147 vtable_index_(vtable_index) {}
4148
Calin Juravle641547a2015-04-21 22:08:51 +01004149 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004150 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004151 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004152 }
4153
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004154 uint32_t GetVTableIndex() const { return vtable_index_; }
4155
4156 DECLARE_INSTRUCTION(InvokeVirtual);
4157
4158 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004159 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004160 const uint32_t vtable_index_;
4161
4162 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4163};
4164
Vladimir Markofcb503c2016-05-18 12:48:17 +01004165class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004166 public:
4167 HInvokeInterface(ArenaAllocator* arena,
4168 uint32_t number_of_arguments,
4169 Primitive::Type return_type,
4170 uint32_t dex_pc,
4171 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004172 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004173 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004174 : HInvoke(arena,
4175 number_of_arguments,
4176 0u,
4177 return_type,
4178 dex_pc,
4179 dex_method_index,
4180 resolved_method,
4181 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004182 imt_index_(imt_index) {}
4183
Calin Juravle641547a2015-04-21 22:08:51 +01004184 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004185 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004186 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004187 }
4188
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004189 uint32_t GetImtIndex() const { return imt_index_; }
4190 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4191
4192 DECLARE_INSTRUCTION(InvokeInterface);
4193
4194 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004195 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004196 const uint32_t imt_index_;
4197
4198 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4199};
4200
Vladimir Markofcb503c2016-05-18 12:48:17 +01004201class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004202 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004203 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004204 : HUnaryOperation(result_type, input, dex_pc) {
4205 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4206 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004207
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004208 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004209
4210 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004211 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004212 }
4213 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004214 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004215 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004216 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4217 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4218 }
4219 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4220 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4221 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004222
Roland Levillain88cb1752014-10-20 16:36:47 +01004223 DECLARE_INSTRUCTION(Neg);
4224
4225 private:
4226 DISALLOW_COPY_AND_ASSIGN(HNeg);
4227};
4228
Vladimir Markofcb503c2016-05-18 12:48:17 +01004229class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004230 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004231 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004232 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004233 uint32_t dex_pc,
4234 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004235 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004236 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004237 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004238 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004239 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004240 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004241 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004242 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004243 }
4244
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004245 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004246 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004247
4248 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004249 bool NeedsEnvironment() const OVERRIDE { return true; }
4250
Mingyao Yang0c365e62015-03-31 15:09:29 -07004251 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4252 bool CanThrow() const OVERRIDE { return true; }
4253
Calin Juravle10e244f2015-01-26 18:54:32 +00004254 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004255
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004256 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4257
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004258 DECLARE_INSTRUCTION(NewArray);
4259
4260 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004261 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004262 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004263 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004264
4265 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4266};
4267
Vladimir Markofcb503c2016-05-18 12:48:17 +01004268class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004269 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004270 HAdd(Primitive::Type result_type,
4271 HInstruction* left,
4272 HInstruction* right,
4273 uint32_t dex_pc = kNoDexPc)
4274 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004275
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004276 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004277
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004278 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004279
4280 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004281 return GetBlock()->GetGraph()->GetIntConstant(
4282 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004283 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004284 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004285 return GetBlock()->GetGraph()->GetLongConstant(
4286 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004287 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004288 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4289 return GetBlock()->GetGraph()->GetFloatConstant(
4290 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4291 }
4292 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4293 return GetBlock()->GetGraph()->GetDoubleConstant(
4294 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4295 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004296
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004297 DECLARE_INSTRUCTION(Add);
4298
4299 private:
4300 DISALLOW_COPY_AND_ASSIGN(HAdd);
4301};
4302
Vladimir Markofcb503c2016-05-18 12:48:17 +01004303class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004304 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004305 HSub(Primitive::Type result_type,
4306 HInstruction* left,
4307 HInstruction* right,
4308 uint32_t dex_pc = kNoDexPc)
4309 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004310
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004311 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004312
4313 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004314 return GetBlock()->GetGraph()->GetIntConstant(
4315 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004316 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004317 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004318 return GetBlock()->GetGraph()->GetLongConstant(
4319 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004320 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004321 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4322 return GetBlock()->GetGraph()->GetFloatConstant(
4323 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4324 }
4325 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4326 return GetBlock()->GetGraph()->GetDoubleConstant(
4327 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4328 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004329
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004330 DECLARE_INSTRUCTION(Sub);
4331
4332 private:
4333 DISALLOW_COPY_AND_ASSIGN(HSub);
4334};
4335
Vladimir Markofcb503c2016-05-18 12:48:17 +01004336class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004337 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004338 HMul(Primitive::Type result_type,
4339 HInstruction* left,
4340 HInstruction* right,
4341 uint32_t dex_pc = kNoDexPc)
4342 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004343
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004344 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004345
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004346 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004347
4348 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004349 return GetBlock()->GetGraph()->GetIntConstant(
4350 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004351 }
4352 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004353 return GetBlock()->GetGraph()->GetLongConstant(
4354 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004355 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004356 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4357 return GetBlock()->GetGraph()->GetFloatConstant(
4358 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4359 }
4360 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4361 return GetBlock()->GetGraph()->GetDoubleConstant(
4362 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4363 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004364
4365 DECLARE_INSTRUCTION(Mul);
4366
4367 private:
4368 DISALLOW_COPY_AND_ASSIGN(HMul);
4369};
4370
Vladimir Markofcb503c2016-05-18 12:48:17 +01004371class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004372 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004373 HDiv(Primitive::Type result_type,
4374 HInstruction* left,
4375 HInstruction* right,
4376 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004377 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004378
Roland Levillain9867bc72015-08-05 10:21:34 +01004379 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004380 T ComputeIntegral(T x, T y) const {
4381 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004382 // Our graph structure ensures we never have 0 for `y` during
4383 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004384 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004385 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004386 return (y == -1) ? -x : x / y;
4387 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004388
Roland Levillain31dd3d62016-02-16 12:21:02 +00004389 template <typename T>
4390 T ComputeFP(T x, T y) const {
4391 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4392 return x / y;
4393 }
4394
Roland Levillain9867bc72015-08-05 10:21:34 +01004395 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004396 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004397 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004398 }
4399 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004400 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004401 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4402 }
4403 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4404 return GetBlock()->GetGraph()->GetFloatConstant(
4405 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4406 }
4407 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4408 return GetBlock()->GetGraph()->GetDoubleConstant(
4409 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004410 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004411
4412 DECLARE_INSTRUCTION(Div);
4413
4414 private:
4415 DISALLOW_COPY_AND_ASSIGN(HDiv);
4416};
4417
Vladimir Markofcb503c2016-05-18 12:48:17 +01004418class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004419 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004420 HRem(Primitive::Type result_type,
4421 HInstruction* left,
4422 HInstruction* right,
4423 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004424 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004425
Roland Levillain9867bc72015-08-05 10:21:34 +01004426 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004427 T ComputeIntegral(T x, T y) const {
4428 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004429 // Our graph structure ensures we never have 0 for `y` during
4430 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004431 DCHECK_NE(y, 0);
4432 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4433 return (y == -1) ? 0 : x % y;
4434 }
4435
Roland Levillain31dd3d62016-02-16 12:21:02 +00004436 template <typename T>
4437 T ComputeFP(T x, T y) const {
4438 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4439 return std::fmod(x, y);
4440 }
4441
Roland Levillain9867bc72015-08-05 10:21:34 +01004442 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004443 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004444 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004445 }
4446 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004447 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004448 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004449 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004450 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4451 return GetBlock()->GetGraph()->GetFloatConstant(
4452 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4453 }
4454 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4455 return GetBlock()->GetGraph()->GetDoubleConstant(
4456 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4457 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004458
4459 DECLARE_INSTRUCTION(Rem);
4460
4461 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004462 DISALLOW_COPY_AND_ASSIGN(HRem);
4463};
4464
Vladimir Markofcb503c2016-05-18 12:48:17 +01004465class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004466 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004467 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4468 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004469 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004470 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004471 SetRawInputAt(0, value);
4472 }
4473
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004474 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4475
Calin Juravled0d48522014-11-04 16:40:20 +00004476 bool CanBeMoved() const OVERRIDE { return true; }
4477
Vladimir Marko372f10e2016-05-17 16:30:10 +01004478 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004479 return true;
4480 }
4481
4482 bool NeedsEnvironment() const OVERRIDE { return true; }
4483 bool CanThrow() const OVERRIDE { return true; }
4484
Calin Juravled0d48522014-11-04 16:40:20 +00004485 DECLARE_INSTRUCTION(DivZeroCheck);
4486
4487 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004488 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4489};
4490
Vladimir Markofcb503c2016-05-18 12:48:17 +01004491class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004492 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004493 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004494 HInstruction* value,
4495 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004496 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004497 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4498 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4499 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004500 }
4501
Roland Levillain5b5b9312016-03-22 14:57:31 +00004502 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004503 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004504 return value << (distance & max_shift_distance);
4505 }
4506
4507 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004508 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004509 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004510 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004511 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004512 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004513 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004514 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004515 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4516 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4517 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4518 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004519 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004520 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4521 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004522 LOG(FATAL) << DebugName() << " is not defined for float values";
4523 UNREACHABLE();
4524 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004525 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4526 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004527 LOG(FATAL) << DebugName() << " is not defined for double values";
4528 UNREACHABLE();
4529 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004530
4531 DECLARE_INSTRUCTION(Shl);
4532
4533 private:
4534 DISALLOW_COPY_AND_ASSIGN(HShl);
4535};
4536
Vladimir Markofcb503c2016-05-18 12:48:17 +01004537class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004538 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004539 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004540 HInstruction* value,
4541 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004542 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004543 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4544 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4545 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004546 }
4547
Roland Levillain5b5b9312016-03-22 14:57:31 +00004548 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004549 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004550 return value >> (distance & max_shift_distance);
4551 }
4552
4553 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004554 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004555 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004556 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004557 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004558 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004559 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004560 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004561 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4562 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4563 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4564 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004565 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004566 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4567 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004568 LOG(FATAL) << DebugName() << " is not defined for float values";
4569 UNREACHABLE();
4570 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004571 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4572 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004573 LOG(FATAL) << DebugName() << " is not defined for double values";
4574 UNREACHABLE();
4575 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004576
4577 DECLARE_INSTRUCTION(Shr);
4578
4579 private:
4580 DISALLOW_COPY_AND_ASSIGN(HShr);
4581};
4582
Vladimir Markofcb503c2016-05-18 12:48:17 +01004583class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004584 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004585 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004586 HInstruction* value,
4587 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004588 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004589 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4590 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4591 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004592 }
4593
Roland Levillain5b5b9312016-03-22 14:57:31 +00004594 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004595 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004596 typedef typename std::make_unsigned<T>::type V;
4597 V ux = static_cast<V>(value);
4598 return static_cast<T>(ux >> (distance & max_shift_distance));
4599 }
4600
4601 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004602 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004603 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004604 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004605 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004606 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004607 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004608 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004609 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4610 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4611 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4612 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004613 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004614 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4615 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004616 LOG(FATAL) << DebugName() << " is not defined for float values";
4617 UNREACHABLE();
4618 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004619 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4620 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004621 LOG(FATAL) << DebugName() << " is not defined for double values";
4622 UNREACHABLE();
4623 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004624
4625 DECLARE_INSTRUCTION(UShr);
4626
4627 private:
4628 DISALLOW_COPY_AND_ASSIGN(HUShr);
4629};
4630
Vladimir Markofcb503c2016-05-18 12:48:17 +01004631class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004632 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004633 HAnd(Primitive::Type result_type,
4634 HInstruction* left,
4635 HInstruction* right,
4636 uint32_t dex_pc = kNoDexPc)
4637 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004638
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004639 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004640
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004641 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004642
4643 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004644 return GetBlock()->GetGraph()->GetIntConstant(
4645 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004646 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004647 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004648 return GetBlock()->GetGraph()->GetLongConstant(
4649 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004650 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004651 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4652 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4653 LOG(FATAL) << DebugName() << " is not defined for float values";
4654 UNREACHABLE();
4655 }
4656 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4657 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4658 LOG(FATAL) << DebugName() << " is not defined for double values";
4659 UNREACHABLE();
4660 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004661
4662 DECLARE_INSTRUCTION(And);
4663
4664 private:
4665 DISALLOW_COPY_AND_ASSIGN(HAnd);
4666};
4667
Vladimir Markofcb503c2016-05-18 12:48:17 +01004668class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004669 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004670 HOr(Primitive::Type result_type,
4671 HInstruction* left,
4672 HInstruction* right,
4673 uint32_t dex_pc = kNoDexPc)
4674 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004675
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004676 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004677
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004678 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004679
4680 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004681 return GetBlock()->GetGraph()->GetIntConstant(
4682 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004683 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004684 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004685 return GetBlock()->GetGraph()->GetLongConstant(
4686 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004687 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004688 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4689 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4690 LOG(FATAL) << DebugName() << " is not defined for float values";
4691 UNREACHABLE();
4692 }
4693 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4694 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4695 LOG(FATAL) << DebugName() << " is not defined for double values";
4696 UNREACHABLE();
4697 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004698
4699 DECLARE_INSTRUCTION(Or);
4700
4701 private:
4702 DISALLOW_COPY_AND_ASSIGN(HOr);
4703};
4704
Vladimir Markofcb503c2016-05-18 12:48:17 +01004705class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004706 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004707 HXor(Primitive::Type result_type,
4708 HInstruction* left,
4709 HInstruction* right,
4710 uint32_t dex_pc = kNoDexPc)
4711 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004712
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004713 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004714
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004715 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004716
4717 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004718 return GetBlock()->GetGraph()->GetIntConstant(
4719 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004720 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004721 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004722 return GetBlock()->GetGraph()->GetLongConstant(
4723 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004724 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004725 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4726 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4727 LOG(FATAL) << DebugName() << " is not defined for float values";
4728 UNREACHABLE();
4729 }
4730 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4731 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4732 LOG(FATAL) << DebugName() << " is not defined for double values";
4733 UNREACHABLE();
4734 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004735
4736 DECLARE_INSTRUCTION(Xor);
4737
4738 private:
4739 DISALLOW_COPY_AND_ASSIGN(HXor);
4740};
4741
Vladimir Markofcb503c2016-05-18 12:48:17 +01004742class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004743 public:
4744 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004745 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004746 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4747 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004748 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004749
Roland Levillain5b5b9312016-03-22 14:57:31 +00004750 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004751 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004752 typedef typename std::make_unsigned<T>::type V;
4753 V ux = static_cast<V>(value);
4754 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004755 return static_cast<T>(ux);
4756 } else {
4757 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004758 return static_cast<T>(ux >> (distance & max_shift_value)) |
4759 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004760 }
4761 }
4762
Roland Levillain5b5b9312016-03-22 14:57:31 +00004763 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004764 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004765 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004766 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004767 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004768 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004769 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004770 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004771 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4772 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4773 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4774 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004775 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004776 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4777 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004778 LOG(FATAL) << DebugName() << " is not defined for float values";
4779 UNREACHABLE();
4780 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004781 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4782 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004783 LOG(FATAL) << DebugName() << " is not defined for double values";
4784 UNREACHABLE();
4785 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004786
4787 DECLARE_INSTRUCTION(Ror);
4788
4789 private:
4790 DISALLOW_COPY_AND_ASSIGN(HRor);
4791};
4792
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004793// The value of a parameter in this method. Its location depends on
4794// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004795class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004796 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004797 HParameterValue(const DexFile& dex_file,
4798 uint16_t type_index,
4799 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004800 Primitive::Type parameter_type,
4801 bool is_this = false)
4802 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004803 dex_file_(dex_file),
4804 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004805 index_(index) {
4806 SetPackedFlag<kFlagIsThis>(is_this);
4807 SetPackedFlag<kFlagCanBeNull>(!is_this);
4808 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004809
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004810 const DexFile& GetDexFile() const { return dex_file_; }
4811 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004812 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004813 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004814
Vladimir Markoa1de9182016-02-25 11:37:38 +00004815 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4816 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004817
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004818 DECLARE_INSTRUCTION(ParameterValue);
4819
4820 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004821 // Whether or not the parameter value corresponds to 'this' argument.
4822 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4823 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4824 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4825 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4826 "Too many packed fields.");
4827
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004828 const DexFile& dex_file_;
4829 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004830 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004831 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004832 const uint8_t index_;
4833
4834 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4835};
4836
Vladimir Markofcb503c2016-05-18 12:48:17 +01004837class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004838 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004839 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4840 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004841
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004842 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004843 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004844 return true;
4845 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004846
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004847 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004848
4849 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004850 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004851 }
4852 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004853 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004854 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004855 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4856 LOG(FATAL) << DebugName() << " is not defined for float values";
4857 UNREACHABLE();
4858 }
4859 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4860 LOG(FATAL) << DebugName() << " is not defined for double values";
4861 UNREACHABLE();
4862 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004863
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004864 DECLARE_INSTRUCTION(Not);
4865
4866 private:
4867 DISALLOW_COPY_AND_ASSIGN(HNot);
4868};
4869
Vladimir Markofcb503c2016-05-18 12:48:17 +01004870class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004871 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004872 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4873 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004874
4875 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004876 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004877 return true;
4878 }
4879
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004880 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004881 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004882 return !x;
4883 }
4884
Roland Levillain9867bc72015-08-05 10:21:34 +01004885 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004886 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004887 }
4888 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4889 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004890 UNREACHABLE();
4891 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004892 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4893 LOG(FATAL) << DebugName() << " is not defined for float values";
4894 UNREACHABLE();
4895 }
4896 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4897 LOG(FATAL) << DebugName() << " is not defined for double values";
4898 UNREACHABLE();
4899 }
David Brazdil66d126e2015-04-03 16:02:44 +01004900
4901 DECLARE_INSTRUCTION(BooleanNot);
4902
4903 private:
4904 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4905};
4906
Vladimir Markofcb503c2016-05-18 12:48:17 +01004907class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004908 public:
4909 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004910 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004911 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004912 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004913 // Invariant: We should never generate a conversion to a Boolean value.
4914 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004915 }
4916
4917 HInstruction* GetInput() const { return InputAt(0); }
4918 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4919 Primitive::Type GetResultType() const { return GetType(); }
4920
4921 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004922 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4923 return true;
4924 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004925
Mark Mendelle82549b2015-05-06 10:55:34 -04004926 // Try to statically evaluate the conversion and return a HConstant
4927 // containing the result. If the input cannot be converted, return nullptr.
4928 HConstant* TryStaticEvaluation() const;
4929
Roland Levillaindff1f282014-11-05 14:15:05 +00004930 DECLARE_INSTRUCTION(TypeConversion);
4931
4932 private:
4933 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4934};
4935
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004936static constexpr uint32_t kNoRegNumber = -1;
4937
Vladimir Markofcb503c2016-05-18 12:48:17 +01004938class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004939 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004940 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4941 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004942 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004943 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004944 SetRawInputAt(0, value);
4945 }
4946
Calin Juravle10e244f2015-01-26 18:54:32 +00004947 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004948 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004949 return true;
4950 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004951
Calin Juravle10e244f2015-01-26 18:54:32 +00004952 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004953
Calin Juravle10e244f2015-01-26 18:54:32 +00004954 bool CanThrow() const OVERRIDE { return true; }
4955
4956 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004957
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004958
4959 DECLARE_INSTRUCTION(NullCheck);
4960
4961 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004962 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4963};
4964
4965class FieldInfo : public ValueObject {
4966 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004967 FieldInfo(MemberOffset field_offset,
4968 Primitive::Type field_type,
4969 bool is_volatile,
4970 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004971 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004972 const DexFile& dex_file,
4973 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004974 : field_offset_(field_offset),
4975 field_type_(field_type),
4976 is_volatile_(is_volatile),
4977 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004978 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004979 dex_file_(dex_file),
4980 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004981
4982 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004983 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004984 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004985 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004986 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004987 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004988 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004989
4990 private:
4991 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004992 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004993 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004994 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004995 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004996 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004997 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004998};
4999
Vladimir Markofcb503c2016-05-18 12:48:17 +01005000class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005001 public:
5002 HInstanceFieldGet(HInstruction* value,
5003 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005004 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005005 bool is_volatile,
5006 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005007 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005008 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005009 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005010 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005011 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005012 field_info_(field_offset,
5013 field_type,
5014 is_volatile,
5015 field_idx,
5016 declaring_class_def_index,
5017 dex_file,
5018 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005019 SetRawInputAt(0, value);
5020 }
5021
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005022 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005023
Vladimir Marko372f10e2016-05-17 16:30:10 +01005024 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5025 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005026 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005027 }
5028
Calin Juravle641547a2015-04-21 22:08:51 +01005029 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005030 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005031 }
5032
5033 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005034 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5035 }
5036
Calin Juravle52c48962014-12-16 17:02:57 +00005037 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005038 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005039 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005040 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005041
5042 DECLARE_INSTRUCTION(InstanceFieldGet);
5043
5044 private:
5045 const FieldInfo field_info_;
5046
5047 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5048};
5049
Vladimir Markofcb503c2016-05-18 12:48:17 +01005050class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005051 public:
5052 HInstanceFieldSet(HInstruction* object,
5053 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005054 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005055 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005056 bool is_volatile,
5057 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005058 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005059 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005060 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005061 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005062 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005063 field_info_(field_offset,
5064 field_type,
5065 is_volatile,
5066 field_idx,
5067 declaring_class_def_index,
5068 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005069 dex_cache) {
5070 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005071 SetRawInputAt(0, object);
5072 SetRawInputAt(1, value);
5073 }
5074
Calin Juravle641547a2015-04-21 22:08:51 +01005075 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005076 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005077 }
5078
Calin Juravle52c48962014-12-16 17:02:57 +00005079 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005080 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005081 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005082 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005083 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005084 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5085 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005086
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005087 DECLARE_INSTRUCTION(InstanceFieldSet);
5088
5089 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005090 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5091 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5092 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5093 "Too many packed fields.");
5094
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005095 const FieldInfo field_info_;
5096
5097 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5098};
5099
Vladimir Markofcb503c2016-05-18 12:48:17 +01005100class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005101 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005102 HArrayGet(HInstruction* array,
5103 HInstruction* index,
5104 Primitive::Type type,
5105 uint32_t dex_pc,
5106 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005107 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005108 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005109 SetRawInputAt(0, array);
5110 SetRawInputAt(1, index);
5111 }
5112
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005113 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005114 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005115 return true;
5116 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005117 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005118 // TODO: We can be smarter here.
5119 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5120 // which generates the implicit null check. There are cases when these can be removed
5121 // to produce better code. If we ever add optimizations to do so we should allow an
5122 // implicit check here (as long as the address falls in the first page).
5123 return false;
5124 }
5125
David Brazdil4833f5a2015-12-16 10:37:39 +00005126 bool IsEquivalentOf(HArrayGet* other) const {
5127 bool result = (GetDexPc() == other->GetDexPc());
5128 if (kIsDebugBuild && result) {
5129 DCHECK_EQ(GetBlock(), other->GetBlock());
5130 DCHECK_EQ(GetArray(), other->GetArray());
5131 DCHECK_EQ(GetIndex(), other->GetIndex());
5132 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005133 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005134 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005135 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5136 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005137 }
5138 }
5139 return result;
5140 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005141
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005142 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5143
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005144 HInstruction* GetArray() const { return InputAt(0); }
5145 HInstruction* GetIndex() const { return InputAt(1); }
5146
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005147 DECLARE_INSTRUCTION(ArrayGet);
5148
5149 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005150 // We treat a String as an array, creating the HArrayGet from String.charAt()
5151 // intrinsic in the instruction simplifier. We can always determine whether
5152 // a particular HArrayGet is actually a String.charAt() by looking at the type
5153 // of the input but that requires holding the mutator lock, so we prefer to use
5154 // a flag, so that code generators don't need to do the locking.
5155 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5156 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5157 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5158 "Too many packed fields.");
5159
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005160 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5161};
5162
Vladimir Markofcb503c2016-05-18 12:48:17 +01005163class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005164 public:
5165 HArraySet(HInstruction* array,
5166 HInstruction* index,
5167 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005168 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005169 uint32_t dex_pc)
5170 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005171 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5172 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5173 SetPackedFlag<kFlagValueCanBeNull>(true);
5174 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005175 SetRawInputAt(0, array);
5176 SetRawInputAt(1, index);
5177 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005178 // Make a best guess now, may be refined during SSA building.
5179 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005180 }
5181
Calin Juravle77520bc2015-01-12 18:45:46 +00005182 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005183 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005184 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005185 }
5186
Mingyao Yang81014cb2015-06-02 03:16:27 -07005187 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005188 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005189
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005190 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005191 // TODO: Same as for ArrayGet.
5192 return false;
5193 }
5194
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005195 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005196 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005197 }
5198
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005199 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005200 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005201 }
5202
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005203 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005204 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005205 }
5206
Vladimir Markoa1de9182016-02-25 11:37:38 +00005207 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5208 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5209 bool StaticTypeOfArrayIsObjectArray() const {
5210 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5211 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005212
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005213 HInstruction* GetArray() const { return InputAt(0); }
5214 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005215 HInstruction* GetValue() const { return InputAt(2); }
5216
5217 Primitive::Type GetComponentType() const {
5218 // The Dex format does not type floating point index operations. Since the
5219 // `expected_component_type_` is set during building and can therefore not
5220 // be correct, we also check what is the value type. If it is a floating
5221 // point type, we must use that type.
5222 Primitive::Type value_type = GetValue()->GetType();
5223 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5224 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005225 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005226 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005227
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005228 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005229 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005230 }
5231
Aart Bik18b36ab2016-04-13 16:41:35 -07005232 void ComputeSideEffects() {
5233 Primitive::Type type = GetComponentType();
5234 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5235 SideEffectsForArchRuntimeCalls(type)));
5236 }
5237
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005238 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5239 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5240 }
5241
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005242 DECLARE_INSTRUCTION(ArraySet);
5243
5244 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005245 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5246 static constexpr size_t kFieldExpectedComponentTypeSize =
5247 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5248 static constexpr size_t kFlagNeedsTypeCheck =
5249 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5250 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005251 // Cached information for the reference_type_info_ so that codegen
5252 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005253 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5254 static constexpr size_t kNumberOfArraySetPackedBits =
5255 kFlagStaticTypeOfArrayIsObjectArray + 1;
5256 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5257 using ExpectedComponentTypeField =
5258 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005259
5260 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5261};
5262
Vladimir Markofcb503c2016-05-18 12:48:17 +01005263class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005264 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005265 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005266 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005267 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005268 // Note that arrays do not change length, so the instruction does not
5269 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005270 SetRawInputAt(0, array);
5271 }
5272
Calin Juravle77520bc2015-01-12 18:45:46 +00005273 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005274 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005275 return true;
5276 }
Calin Juravle641547a2015-04-21 22:08:51 +01005277 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5278 return obj == InputAt(0);
5279 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005280
Vladimir Markodce016e2016-04-28 13:10:02 +01005281 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5282
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005283 DECLARE_INSTRUCTION(ArrayLength);
5284
5285 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005286 // We treat a String as an array, creating the HArrayLength from String.length()
5287 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5288 // determine whether a particular HArrayLength is actually a String.length() by
5289 // looking at the type of the input but that requires holding the mutator lock, so
5290 // we prefer to use a flag, so that code generators don't need to do the locking.
5291 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5292 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5293 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5294 "Too many packed fields.");
5295
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005296 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5297};
5298
Vladimir Markofcb503c2016-05-18 12:48:17 +01005299class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005300 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005301 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5302 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005303 HBoundsCheck(HInstruction* index,
5304 HInstruction* length,
5305 uint32_t dex_pc,
5306 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5307 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5308 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005309 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005310 SetRawInputAt(0, index);
5311 SetRawInputAt(1, length);
5312 }
5313
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005314 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005315 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005316 return true;
5317 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005318
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005319 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005320
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005321 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005322
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005323 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5324 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5325
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005326 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005327
5328 DECLARE_INSTRUCTION(BoundsCheck);
5329
5330 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005331 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5332 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5333 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5334 // so we need to create an HEnvironment which will be translated to an InlineInfo
5335 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5336 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5337 // from the invoke as we don't want to look again at the dex bytecode.
5338 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5339
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005340 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5341};
5342
Vladimir Markofcb503c2016-05-18 12:48:17 +01005343class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005344 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005345 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005346 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005347
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005348 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005349 return true;
5350 }
5351
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005352 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5353 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005354
5355 DECLARE_INSTRUCTION(SuspendCheck);
5356
5357 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005358 // Only used for code generation, in order to share the same slow path between back edges
5359 // of a same loop.
5360 SlowPathCode* slow_path_;
5361
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005362 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5363};
5364
David Srbecky0cf44932015-12-09 14:09:59 +00005365// Pseudo-instruction which provides the native debugger with mapping information.
5366// It ensures that we can generate line number and local variables at this point.
5367class HNativeDebugInfo : public HTemplateInstruction<0> {
5368 public:
5369 explicit HNativeDebugInfo(uint32_t dex_pc)
5370 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5371
5372 bool NeedsEnvironment() const OVERRIDE {
5373 return true;
5374 }
5375
5376 DECLARE_INSTRUCTION(NativeDebugInfo);
5377
5378 private:
5379 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5380};
5381
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005382/**
5383 * Instruction to load a Class object.
5384 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005385class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005386 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005387 // Determines how to load the Class.
5388 enum class LoadKind {
5389 // Use the Class* from the method's own ArtMethod*.
5390 kReferrersClass,
5391
5392 // Use boot image Class* address that will be known at link time.
5393 // Used for boot image classes referenced by boot image code in non-PIC mode.
5394 kBootImageLinkTimeAddress,
5395
5396 // Use PC-relative boot image Class* address that will be known at link time.
5397 // Used for boot image classes referenced by boot image code in PIC mode.
5398 kBootImageLinkTimePcRelative,
5399
5400 // Use a known boot image Class* address, embedded in the code by the codegen.
5401 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5402 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5403 // GetIncludePatchInformation().
5404 kBootImageAddress,
5405
5406 // Load from the resolved types array at an absolute address.
5407 // Used for classes outside the boot image referenced by JIT-compiled code.
5408 kDexCacheAddress,
5409
5410 // Load from resolved types array in the dex cache using a PC-relative load.
5411 // Used for classes outside boot image when we know that we can access
5412 // the dex cache arrays using a PC-relative load.
5413 kDexCachePcRelative,
5414
5415 // Load from resolved types array accessed through the class loaded from
5416 // the compiled method's own ArtMethod*. This is the default access type when
5417 // all other types are unavailable.
5418 kDexCacheViaMethod,
5419
5420 kLast = kDexCacheViaMethod
5421 };
5422
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005423 HLoadClass(HCurrentMethod* current_method,
5424 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005425 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005426 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005427 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005428 bool needs_access_check,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005429 bool is_in_dex_cache,
5430 bool is_in_boot_image)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005431 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5432 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005433 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005434 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005435 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005436 // Referrers class should not need access check. We never inline unverified
5437 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005438 DCHECK(!is_referrers_class || !needs_access_check);
5439
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005440 SetPackedField<LoadKindField>(
5441 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005442 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5443 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005444 SetPackedFlag<kFlagIsInBootImage>(is_in_boot_image);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005445 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005446 }
5447
5448 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5449 DCHECK(HasAddress(load_kind));
5450 load_data_.address = address;
5451 SetLoadKindInternal(load_kind);
5452 }
5453
5454 void SetLoadKindWithTypeReference(LoadKind load_kind,
5455 const DexFile& dex_file,
5456 uint32_t type_index) {
5457 DCHECK(HasTypeReference(load_kind));
5458 DCHECK(IsSameDexFile(dex_file_, dex_file));
5459 DCHECK_EQ(type_index_, type_index);
5460 SetLoadKindInternal(load_kind);
5461 }
5462
5463 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5464 const DexFile& dex_file,
5465 uint32_t element_index) {
5466 DCHECK(HasDexCacheReference(load_kind));
5467 DCHECK(IsSameDexFile(dex_file_, dex_file));
5468 load_data_.dex_cache_element_index = element_index;
5469 SetLoadKindInternal(load_kind);
5470 }
5471
5472 LoadKind GetLoadKind() const {
5473 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005474 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005475
5476 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005477
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005478 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005479
5480 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5481
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005482 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005483
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005484 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005485 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005486 }
5487
Calin Juravle0ba218d2015-05-19 18:46:01 +01005488 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005489 // The entrypoint the code generator is going to call does not do
5490 // clinit of the class.
5491 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005492 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005493 }
5494
5495 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005496 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005497 (!IsReferrersClass() && !IsInDexCache()) ||
5498 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005499 }
5500
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005501
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005502 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005503 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005504 }
5505
Calin Juravleacf735c2015-02-12 15:25:22 +00005506 ReferenceTypeInfo GetLoadedClassRTI() {
5507 return loaded_class_rti_;
5508 }
5509
5510 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5511 // Make sure we only set exact types (the loaded class should never be merged).
5512 DCHECK(rti.IsExact());
5513 loaded_class_rti_ = rti;
5514 }
5515
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005516 uint32_t GetTypeIndex() const { return type_index_; }
5517 const DexFile& GetDexFile() const { return dex_file_; }
5518
5519 uint32_t GetDexCacheElementOffset() const;
5520
5521 uint64_t GetAddress() const {
5522 DCHECK(HasAddress(GetLoadKind()));
5523 return load_data_.address;
5524 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005525
Vladimir Markoa1de9182016-02-25 11:37:38 +00005526 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005527
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005528 static SideEffects SideEffectsForArchRuntimeCalls() {
5529 return SideEffects::CanTriggerGC();
5530 }
5531
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005532 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005533 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5534 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005535 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005536 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005537
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005538 void MarkInDexCache() {
5539 SetPackedFlag<kFlagIsInDexCache>(true);
5540 DCHECK(!NeedsEnvironment());
5541 RemoveEnvironment();
5542 SetSideEffects(SideEffects::None());
5543 }
5544
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005545 void MarkInBootImage() {
5546 SetPackedFlag<kFlagIsInBootImage>(true);
5547 }
5548
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005549 void AddSpecialInput(HInstruction* special_input);
5550
5551 using HInstruction::GetInputRecords; // Keep the const version visible.
5552 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5553 return ArrayRef<HUserRecord<HInstruction*>>(
5554 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5555 }
5556
5557 Primitive::Type GetType() const OVERRIDE {
5558 return Primitive::kPrimNot;
5559 }
5560
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005561 DECLARE_INSTRUCTION(LoadClass);
5562
5563 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005564 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005565 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005566 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005567 // Whether this instruction must generate the initialization check.
5568 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005569 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005570 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5571 static constexpr size_t kFieldLoadKindSize =
5572 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5573 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005574 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005575 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5576
5577 static bool HasTypeReference(LoadKind load_kind) {
5578 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5579 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5580 load_kind == LoadKind::kDexCacheViaMethod ||
5581 load_kind == LoadKind::kReferrersClass;
5582 }
5583
5584 static bool HasAddress(LoadKind load_kind) {
5585 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5586 }
5587
5588 static bool HasDexCacheReference(LoadKind load_kind) {
5589 return load_kind == LoadKind::kDexCachePcRelative;
5590 }
5591
5592 void SetLoadKindInternal(LoadKind load_kind);
5593
5594 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5595 // For other load kinds it's empty or possibly some architecture-specific instruction
5596 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5597 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005598
5599 const uint16_t type_index_;
5600 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005601
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005602 union {
5603 uint32_t dex_cache_element_index; // Only for dex cache reference.
5604 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5605 } load_data_;
5606
Calin Juravleacf735c2015-02-12 15:25:22 +00005607 ReferenceTypeInfo loaded_class_rti_;
5608
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005609 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5610};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005611std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5612
5613// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5614inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5615 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5616 return load_data_.dex_cache_element_index;
5617}
5618
5619// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5620inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005621 // The special input is used for PC-relative loads on some architectures,
5622 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005623 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005624 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5625 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5626 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005627 DCHECK(special_input_.GetInstruction() == nullptr);
5628 special_input_ = HUserRecord<HInstruction*>(special_input);
5629 special_input->AddUseAt(this, 0);
5630}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005631
Vladimir Marko372f10e2016-05-17 16:30:10 +01005632class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005633 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005634 // Determines how to load the String.
5635 enum class LoadKind {
5636 // Use boot image String* address that will be known at link time.
5637 // Used for boot image strings referenced by boot image code in non-PIC mode.
5638 kBootImageLinkTimeAddress,
5639
5640 // Use PC-relative boot image String* address that will be known at link time.
5641 // Used for boot image strings referenced by boot image code in PIC mode.
5642 kBootImageLinkTimePcRelative,
5643
5644 // Use a known boot image String* address, embedded in the code by the codegen.
5645 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5646 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5647 // GetIncludePatchInformation().
5648 kBootImageAddress,
5649
5650 // Load from the resolved strings array at an absolute address.
5651 // Used for strings outside the boot image referenced by JIT-compiled code.
5652 kDexCacheAddress,
5653
Vladimir Marko5f926052016-09-30 17:04:49 +00005654 // Load from resolved strings array in the dex cache using a PC-relative load.
5655 // Used for strings outside boot image when we know that we can access
5656 // the dex cache arrays using a PC-relative load.
5657 kDexCachePcRelative,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005658
5659 // Load from resolved strings array accessed through the class loaded from
5660 // the compiled method's own ArtMethod*. This is the default access type when
5661 // all other types are unavailable.
5662 kDexCacheViaMethod,
5663
5664 kLast = kDexCacheViaMethod
5665 };
5666
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005667 HLoadString(HCurrentMethod* current_method,
5668 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005669 const DexFile& dex_file,
5670 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005671 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5672 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005673 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005674 SetPackedFlag<kFlagIsInDexCache>(false);
5675 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Vladimir Marko5f926052016-09-30 17:04:49 +00005676 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005677 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005678
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005679 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5680 DCHECK(HasAddress(load_kind));
5681 load_data_.address = address;
5682 SetLoadKindInternal(load_kind);
5683 }
5684
5685 void SetLoadKindWithStringReference(LoadKind load_kind,
5686 const DexFile& dex_file,
5687 uint32_t string_index) {
5688 DCHECK(HasStringReference(load_kind));
Vladimir Marko5f926052016-09-30 17:04:49 +00005689 load_data_.ref.dex_file = &dex_file;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005690 string_index_ = string_index;
5691 SetLoadKindInternal(load_kind);
5692 }
5693
Vladimir Marko5f926052016-09-30 17:04:49 +00005694 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5695 const DexFile& dex_file,
5696 uint32_t element_index) {
5697 DCHECK(HasDexCacheReference(load_kind));
5698 load_data_.ref.dex_file = &dex_file;
5699 load_data_.ref.dex_cache_element_index = element_index;
5700 SetLoadKindInternal(load_kind);
5701 }
5702
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005703 LoadKind GetLoadKind() const {
5704 return GetPackedField<LoadKindField>();
5705 }
5706
5707 const DexFile& GetDexFile() const;
5708
5709 uint32_t GetStringIndex() const {
5710 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5711 return string_index_;
5712 }
5713
Vladimir Marko5f926052016-09-30 17:04:49 +00005714 uint32_t GetDexCacheElementOffset() const;
5715
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005716 uint64_t GetAddress() const {
5717 DCHECK(HasAddress(GetLoadKind()));
5718 return load_data_.address;
5719 }
5720
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005721 bool CanBeMoved() const OVERRIDE { return true; }
5722
Vladimir Marko372f10e2016-05-17 16:30:10 +01005723 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005724
5725 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5726
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005727 // Will call the runtime if we need to load the string through
5728 // the dex cache and the string is not guaranteed to be there yet.
5729 bool NeedsEnvironment() const OVERRIDE {
5730 LoadKind load_kind = GetLoadKind();
5731 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5732 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5733 load_kind == LoadKind::kBootImageAddress) {
5734 return false;
5735 }
5736 return !IsInDexCache();
5737 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005738
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005739 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5740 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5741 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005742
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005743 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005744 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005745
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005746 static SideEffects SideEffectsForArchRuntimeCalls() {
5747 return SideEffects::CanTriggerGC();
5748 }
5749
Vladimir Markoa1de9182016-02-25 11:37:38 +00005750 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5751
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005752 void MarkInDexCache() {
5753 SetPackedFlag<kFlagIsInDexCache>(true);
5754 DCHECK(!NeedsEnvironment());
5755 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005756 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005757 }
5758
Vladimir Marko372f10e2016-05-17 16:30:10 +01005759 void AddSpecialInput(HInstruction* special_input);
5760
5761 using HInstruction::GetInputRecords; // Keep the const version visible.
5762 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5763 return ArrayRef<HUserRecord<HInstruction*>>(
5764 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005765 }
5766
Vladimir Marko372f10e2016-05-17 16:30:10 +01005767 Primitive::Type GetType() const OVERRIDE {
5768 return Primitive::kPrimNot;
5769 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005770
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005771 DECLARE_INSTRUCTION(LoadString);
5772
5773 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005774 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005775 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5776 static constexpr size_t kFieldLoadKindSize =
5777 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5778 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005779 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005780 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005781
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005782 static bool HasStringReference(LoadKind load_kind) {
5783 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5784 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5785 load_kind == LoadKind::kDexCacheViaMethod;
5786 }
5787
5788 static bool HasAddress(LoadKind load_kind) {
5789 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5790 }
5791
Vladimir Marko5f926052016-09-30 17:04:49 +00005792 static bool HasDexCacheReference(LoadKind load_kind) {
5793 return load_kind == LoadKind::kDexCachePcRelative;
5794 }
5795
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005796 void SetLoadKindInternal(LoadKind load_kind);
5797
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005798 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5799 // For other load kinds it's empty or possibly some architecture-specific instruction
5800 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005801 HUserRecord<HInstruction*> special_input_;
5802
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005803 // String index serves also as the hash code and it's also needed for slow-paths,
5804 // so it must not be overwritten with other load data.
5805 uint32_t string_index_;
5806
5807 union {
Vladimir Marko5f926052016-09-30 17:04:49 +00005808 struct {
5809 const DexFile* dex_file; // For string reference and dex cache reference.
5810 uint32_t dex_cache_element_index; // Only for dex cache reference.
5811 } ref;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005812 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5813 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005814
5815 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5816};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005817std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5818
5819// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5820inline const DexFile& HLoadString::GetDexFile() const {
Vladimir Marko5f926052016-09-30 17:04:49 +00005821 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5822 << GetLoadKind();
5823 return *load_data_.ref.dex_file;
5824}
5825
5826// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5827inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5828 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5829 return load_data_.ref.dex_cache_element_index;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005830}
5831
5832// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5833inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005834 // The special input is used for PC-relative loads on some architectures,
5835 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005836 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko5f926052016-09-30 17:04:49 +00005837 GetLoadKind() == LoadKind::kDexCachePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005838 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5839 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005840 // HLoadString::GetInputRecords() returns an empty array at this point,
5841 // so use the GetInputRecords() from the base class to set the input record.
5842 DCHECK(special_input_.GetInstruction() == nullptr);
5843 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005844 special_input->AddUseAt(this, 0);
5845}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005846
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005847/**
5848 * Performs an initialization check on its Class object input.
5849 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005850class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005851 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005852 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005853 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005854 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005855 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5856 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005857 SetRawInputAt(0, constant);
5858 }
5859
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005860 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005861 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005862 return true;
5863 }
5864
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005865 bool NeedsEnvironment() const OVERRIDE {
5866 // May call runtime to initialize the class.
5867 return true;
5868 }
5869
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005870 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005871
5872 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5873
5874 DECLARE_INSTRUCTION(ClinitCheck);
5875
5876 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005877 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5878};
5879
Vladimir Markofcb503c2016-05-18 12:48:17 +01005880class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005881 public:
5882 HStaticFieldGet(HInstruction* cls,
5883 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005884 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005885 bool is_volatile,
5886 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005887 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005888 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005889 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005890 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005891 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005892 field_info_(field_offset,
5893 field_type,
5894 is_volatile,
5895 field_idx,
5896 declaring_class_def_index,
5897 dex_file,
5898 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005899 SetRawInputAt(0, cls);
5900 }
5901
Calin Juravle52c48962014-12-16 17:02:57 +00005902
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005903 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005904
Vladimir Marko372f10e2016-05-17 16:30:10 +01005905 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5906 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005907 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005908 }
5909
5910 size_t ComputeHashCode() const OVERRIDE {
5911 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5912 }
5913
Calin Juravle52c48962014-12-16 17:02:57 +00005914 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005915 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5916 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005917 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005918
5919 DECLARE_INSTRUCTION(StaticFieldGet);
5920
5921 private:
5922 const FieldInfo field_info_;
5923
5924 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5925};
5926
Vladimir Markofcb503c2016-05-18 12:48:17 +01005927class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005928 public:
5929 HStaticFieldSet(HInstruction* cls,
5930 HInstruction* value,
5931 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005932 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005933 bool is_volatile,
5934 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005935 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005936 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005937 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005938 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005939 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005940 field_info_(field_offset,
5941 field_type,
5942 is_volatile,
5943 field_idx,
5944 declaring_class_def_index,
5945 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005946 dex_cache) {
5947 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005948 SetRawInputAt(0, cls);
5949 SetRawInputAt(1, value);
5950 }
5951
Calin Juravle52c48962014-12-16 17:02:57 +00005952 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005953 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5954 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005955 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005956
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005957 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005958 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5959 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005960
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005961 DECLARE_INSTRUCTION(StaticFieldSet);
5962
5963 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005964 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5965 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5966 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5967 "Too many packed fields.");
5968
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005969 const FieldInfo field_info_;
5970
5971 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5972};
5973
Vladimir Markofcb503c2016-05-18 12:48:17 +01005974class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005975 public:
5976 HUnresolvedInstanceFieldGet(HInstruction* obj,
5977 Primitive::Type field_type,
5978 uint32_t field_index,
5979 uint32_t dex_pc)
5980 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5981 field_index_(field_index) {
5982 SetRawInputAt(0, obj);
5983 }
5984
5985 bool NeedsEnvironment() const OVERRIDE { return true; }
5986 bool CanThrow() const OVERRIDE { return true; }
5987
5988 Primitive::Type GetFieldType() const { return GetType(); }
5989 uint32_t GetFieldIndex() const { return field_index_; }
5990
5991 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5992
5993 private:
5994 const uint32_t field_index_;
5995
5996 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5997};
5998
Vladimir Markofcb503c2016-05-18 12:48:17 +01005999class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006000 public:
6001 HUnresolvedInstanceFieldSet(HInstruction* obj,
6002 HInstruction* value,
6003 Primitive::Type field_type,
6004 uint32_t field_index,
6005 uint32_t dex_pc)
6006 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006007 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006008 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006009 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006010 SetRawInputAt(0, obj);
6011 SetRawInputAt(1, value);
6012 }
6013
6014 bool NeedsEnvironment() const OVERRIDE { return true; }
6015 bool CanThrow() const OVERRIDE { return true; }
6016
Vladimir Markoa1de9182016-02-25 11:37:38 +00006017 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006018 uint32_t GetFieldIndex() const { return field_index_; }
6019
6020 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6021
6022 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006023 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6024 static constexpr size_t kFieldFieldTypeSize =
6025 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6026 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6027 kFieldFieldType + kFieldFieldTypeSize;
6028 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6029 "Too many packed fields.");
6030 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6031
Calin Juravlee460d1d2015-09-29 04:52:17 +01006032 const uint32_t field_index_;
6033
6034 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6035};
6036
Vladimir Markofcb503c2016-05-18 12:48:17 +01006037class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006038 public:
6039 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6040 uint32_t field_index,
6041 uint32_t dex_pc)
6042 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6043 field_index_(field_index) {
6044 }
6045
6046 bool NeedsEnvironment() const OVERRIDE { return true; }
6047 bool CanThrow() const OVERRIDE { return true; }
6048
6049 Primitive::Type GetFieldType() const { return GetType(); }
6050 uint32_t GetFieldIndex() const { return field_index_; }
6051
6052 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6053
6054 private:
6055 const uint32_t field_index_;
6056
6057 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6058};
6059
Vladimir Markofcb503c2016-05-18 12:48:17 +01006060class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006061 public:
6062 HUnresolvedStaticFieldSet(HInstruction* value,
6063 Primitive::Type field_type,
6064 uint32_t field_index,
6065 uint32_t dex_pc)
6066 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006067 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006068 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006069 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006070 SetRawInputAt(0, value);
6071 }
6072
6073 bool NeedsEnvironment() const OVERRIDE { return true; }
6074 bool CanThrow() const OVERRIDE { return true; }
6075
Vladimir Markoa1de9182016-02-25 11:37:38 +00006076 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006077 uint32_t GetFieldIndex() const { return field_index_; }
6078
6079 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6080
6081 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006082 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6083 static constexpr size_t kFieldFieldTypeSize =
6084 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6085 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6086 kFieldFieldType + kFieldFieldTypeSize;
6087 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6088 "Too many packed fields.");
6089 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6090
Calin Juravlee460d1d2015-09-29 04:52:17 +01006091 const uint32_t field_index_;
6092
6093 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6094};
6095
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006096// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006097class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006098 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006099 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6100 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006101
David Brazdilbbd733e2015-08-18 17:48:17 +01006102 bool CanBeNull() const OVERRIDE { return false; }
6103
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006104 DECLARE_INSTRUCTION(LoadException);
6105
6106 private:
6107 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6108};
6109
David Brazdilcb1c0552015-08-04 16:22:25 +01006110// Implicit part of move-exception which clears thread-local exception storage.
6111// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006112class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006113 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006114 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6115 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006116
6117 DECLARE_INSTRUCTION(ClearException);
6118
6119 private:
6120 DISALLOW_COPY_AND_ASSIGN(HClearException);
6121};
6122
Vladimir Markofcb503c2016-05-18 12:48:17 +01006123class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006124 public:
6125 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006126 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006127 SetRawInputAt(0, exception);
6128 }
6129
6130 bool IsControlFlow() const OVERRIDE { return true; }
6131
6132 bool NeedsEnvironment() const OVERRIDE { return true; }
6133
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006134 bool CanThrow() const OVERRIDE { return true; }
6135
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006136
6137 DECLARE_INSTRUCTION(Throw);
6138
6139 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006140 DISALLOW_COPY_AND_ASSIGN(HThrow);
6141};
6142
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006143/**
6144 * Implementation strategies for the code generator of a HInstanceOf
6145 * or `HCheckCast`.
6146 */
6147enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006148 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006149 kExactCheck, // Can do a single class compare.
6150 kClassHierarchyCheck, // Can just walk the super class chain.
6151 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6152 kInterfaceCheck, // No optimization yet when checking against an interface.
6153 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006154 kArrayCheck, // No optimization yet when checking against a generic array.
6155 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006156};
6157
Roland Levillain86503782016-02-11 19:07:30 +00006158std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6159
Vladimir Markofcb503c2016-05-18 12:48:17 +01006160class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006161 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006162 HInstanceOf(HInstruction* object,
6163 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006164 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006165 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006166 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006167 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006168 dex_pc) {
6169 SetPackedField<TypeCheckKindField>(check_kind);
6170 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006171 SetRawInputAt(0, object);
6172 SetRawInputAt(1, constant);
6173 }
6174
6175 bool CanBeMoved() const OVERRIDE { return true; }
6176
Vladimir Marko372f10e2016-05-17 16:30:10 +01006177 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006178 return true;
6179 }
6180
6181 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006182 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006183 }
6184
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006185 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006186 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6187 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6188 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6189 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006190
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006191 static bool CanCallRuntime(TypeCheckKind check_kind) {
6192 // Mips currently does runtime calls for any other checks.
6193 return check_kind != TypeCheckKind::kExactCheck;
6194 }
6195
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006196 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006197 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006198 }
6199
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006200 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006201
6202 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006203 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6204 static constexpr size_t kFieldTypeCheckKindSize =
6205 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6206 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6207 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6208 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6209 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006210
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006211 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6212};
6213
Vladimir Markofcb503c2016-05-18 12:48:17 +01006214class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006215 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006216 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006217 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006218 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6219 SetPackedFlag<kFlagUpperCanBeNull>(true);
6220 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006221 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006222 SetRawInputAt(0, input);
6223 }
6224
David Brazdilf5552582015-12-27 13:36:12 +00006225 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006226 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006227 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006228 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006229
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006230 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006231 DCHECK(GetUpperCanBeNull() || !can_be_null);
6232 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006233 }
6234
Vladimir Markoa1de9182016-02-25 11:37:38 +00006235 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006236
Calin Juravleb1498f62015-02-16 13:13:29 +00006237 DECLARE_INSTRUCTION(BoundType);
6238
6239 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006240 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6241 // is false then CanBeNull() cannot be true).
6242 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6243 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6244 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6245 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6246
Calin Juravleb1498f62015-02-16 13:13:29 +00006247 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006248 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6249 // It is used to bound the type in cases like:
6250 // if (x instanceof ClassX) {
6251 // // uper_bound_ will be ClassX
6252 // }
David Brazdilf5552582015-12-27 13:36:12 +00006253 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006254
6255 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6256};
6257
Vladimir Markofcb503c2016-05-18 12:48:17 +01006258class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006259 public:
6260 HCheckCast(HInstruction* object,
6261 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006262 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006263 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006264 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6265 SetPackedField<TypeCheckKindField>(check_kind);
6266 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006267 SetRawInputAt(0, object);
6268 SetRawInputAt(1, constant);
6269 }
6270
6271 bool CanBeMoved() const OVERRIDE { return true; }
6272
Vladimir Marko372f10e2016-05-17 16:30:10 +01006273 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006274 return true;
6275 }
6276
6277 bool NeedsEnvironment() const OVERRIDE {
6278 // Instruction may throw a CheckCastError.
6279 return true;
6280 }
6281
6282 bool CanThrow() const OVERRIDE { return true; }
6283
Vladimir Markoa1de9182016-02-25 11:37:38 +00006284 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6285 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6286 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6287 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006288
6289 DECLARE_INSTRUCTION(CheckCast);
6290
6291 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006292 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6293 static constexpr size_t kFieldTypeCheckKindSize =
6294 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6295 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6296 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6297 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6298 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006299
6300 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006301};
6302
Andreas Gampe26de38b2016-07-27 17:53:11 -07006303/**
6304 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6305 * @details We define the combined barrier types that are actually required
6306 * by the Java Memory Model, rather than using exactly the terminology from
6307 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6308 * primitives. Note that the JSR-133 cookbook generally does not deal with
6309 * store atomicity issues, and the recipes there are not always entirely sufficient.
6310 * The current recipe is as follows:
6311 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6312 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6313 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6314 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6315 * class has final fields.
6316 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6317 * store-to-memory instructions. Only generated together with non-temporal stores.
6318 */
6319enum MemBarrierKind {
6320 kAnyStore,
6321 kLoadAny,
6322 kStoreStore,
6323 kAnyAny,
6324 kNTStoreStore,
6325 kLastBarrierKind = kNTStoreStore
6326};
6327std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6328
Vladimir Markofcb503c2016-05-18 12:48:17 +01006329class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006330 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006331 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006332 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006333 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6334 SetPackedField<BarrierKindField>(barrier_kind);
6335 }
Calin Juravle27df7582015-04-17 19:12:31 +01006336
Vladimir Markoa1de9182016-02-25 11:37:38 +00006337 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006338
6339 DECLARE_INSTRUCTION(MemoryBarrier);
6340
6341 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006342 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6343 static constexpr size_t kFieldBarrierKindSize =
6344 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6345 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6346 kFieldBarrierKind + kFieldBarrierKindSize;
6347 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6348 "Too many packed fields.");
6349 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006350
6351 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6352};
6353
Vladimir Markofcb503c2016-05-18 12:48:17 +01006354class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006355 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006356 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006357 kEnter,
6358 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006359 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006360 };
6361
6362 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006363 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006364 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6365 dex_pc) {
6366 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006367 SetRawInputAt(0, object);
6368 }
6369
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006370 // Instruction may go into runtime, so we need an environment.
6371 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006372
6373 bool CanThrow() const OVERRIDE {
6374 // Verifier guarantees that monitor-exit cannot throw.
6375 // This is important because it allows the HGraphBuilder to remove
6376 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6377 return IsEnter();
6378 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006379
Vladimir Markoa1de9182016-02-25 11:37:38 +00006380 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6381 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006382
6383 DECLARE_INSTRUCTION(MonitorOperation);
6384
Calin Juravle52c48962014-12-16 17:02:57 +00006385 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006386 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6387 static constexpr size_t kFieldOperationKindSize =
6388 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6389 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6390 kFieldOperationKind + kFieldOperationKindSize;
6391 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6392 "Too many packed fields.");
6393 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006394
6395 private:
6396 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6397};
6398
Vladimir Markofcb503c2016-05-18 12:48:17 +01006399class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006400 public:
6401 HSelect(HInstruction* condition,
6402 HInstruction* true_value,
6403 HInstruction* false_value,
6404 uint32_t dex_pc)
6405 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6406 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6407
6408 // First input must be `true_value` or `false_value` to allow codegens to
6409 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6410 // that architectures which implement HSelect as a conditional move also
6411 // will not need to invert the condition.
6412 SetRawInputAt(0, false_value);
6413 SetRawInputAt(1, true_value);
6414 SetRawInputAt(2, condition);
6415 }
6416
6417 HInstruction* GetFalseValue() const { return InputAt(0); }
6418 HInstruction* GetTrueValue() const { return InputAt(1); }
6419 HInstruction* GetCondition() const { return InputAt(2); }
6420
6421 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006422 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6423 return true;
6424 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006425
6426 bool CanBeNull() const OVERRIDE {
6427 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6428 }
6429
6430 DECLARE_INSTRUCTION(Select);
6431
6432 private:
6433 DISALLOW_COPY_AND_ASSIGN(HSelect);
6434};
6435
Vladimir Markof9f64412015-09-02 14:05:49 +01006436class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006437 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006438 MoveOperands(Location source,
6439 Location destination,
6440 Primitive::Type type,
6441 HInstruction* instruction)
6442 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006443
6444 Location GetSource() const { return source_; }
6445 Location GetDestination() const { return destination_; }
6446
6447 void SetSource(Location value) { source_ = value; }
6448 void SetDestination(Location value) { destination_ = value; }
6449
6450 // The parallel move resolver marks moves as "in-progress" by clearing the
6451 // destination (but not the source).
6452 Location MarkPending() {
6453 DCHECK(!IsPending());
6454 Location dest = destination_;
6455 destination_ = Location::NoLocation();
6456 return dest;
6457 }
6458
6459 void ClearPending(Location dest) {
6460 DCHECK(IsPending());
6461 destination_ = dest;
6462 }
6463
6464 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006465 DCHECK(source_.IsValid() || destination_.IsInvalid());
6466 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006467 }
6468
6469 // True if this blocks a move from the given location.
6470 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006471 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006472 }
6473
6474 // A move is redundant if it's been eliminated, if its source and
6475 // destination are the same, or if its destination is unneeded.
6476 bool IsRedundant() const {
6477 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6478 }
6479
6480 // We clear both operands to indicate move that's been eliminated.
6481 void Eliminate() {
6482 source_ = destination_ = Location::NoLocation();
6483 }
6484
6485 bool IsEliminated() const {
6486 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6487 return source_.IsInvalid();
6488 }
6489
Alexey Frunze4dda3372015-06-01 18:31:49 -07006490 Primitive::Type GetType() const { return type_; }
6491
Nicolas Geoffray90218252015-04-15 11:56:51 +01006492 bool Is64BitMove() const {
6493 return Primitive::Is64BitType(type_);
6494 }
6495
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006496 HInstruction* GetInstruction() const { return instruction_; }
6497
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006498 private:
6499 Location source_;
6500 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006501 // The type this move is for.
6502 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006503 // The instruction this move is assocatied with. Null when this move is
6504 // for moving an input in the expected locations of user (including a phi user).
6505 // This is only used in debug mode, to ensure we do not connect interval siblings
6506 // in the same parallel move.
6507 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006508};
6509
Roland Levillainc9285912015-12-18 10:38:42 +00006510std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6511
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006512static constexpr size_t kDefaultNumberOfMoves = 4;
6513
Vladimir Markofcb503c2016-05-18 12:48:17 +01006514class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006515 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006516 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006517 : HTemplateInstruction(SideEffects::None(), dex_pc),
6518 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6519 moves_.reserve(kDefaultNumberOfMoves);
6520 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006521
Nicolas Geoffray90218252015-04-15 11:56:51 +01006522 void AddMove(Location source,
6523 Location destination,
6524 Primitive::Type type,
6525 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006526 DCHECK(source.IsValid());
6527 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006528 if (kIsDebugBuild) {
6529 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006530 for (const MoveOperands& move : moves_) {
6531 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006532 // Special case the situation where the move is for the spill slot
6533 // of the instruction.
6534 if ((GetPrevious() == instruction)
6535 || ((GetPrevious() == nullptr)
6536 && instruction->IsPhi()
6537 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006538 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006539 << "Doing parallel moves for the same instruction.";
6540 } else {
6541 DCHECK(false) << "Doing parallel moves for the same instruction.";
6542 }
6543 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006544 }
6545 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006546 for (const MoveOperands& move : moves_) {
6547 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006548 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006549 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006550 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006551 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006552 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006553 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006554 }
6555
Vladimir Marko225b6462015-09-28 12:17:40 +01006556 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006557 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006558 }
6559
Vladimir Marko225b6462015-09-28 12:17:40 +01006560 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006561
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006562 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006563
6564 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006565 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006566
6567 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6568};
6569
Mark Mendell0616ae02015-04-17 12:49:27 -04006570} // namespace art
6571
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006572#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6573#include "nodes_shared.h"
6574#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006575#ifdef ART_ENABLE_CODEGEN_arm
6576#include "nodes_arm.h"
6577#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006578#ifdef ART_ENABLE_CODEGEN_arm64
6579#include "nodes_arm64.h"
6580#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006581#ifdef ART_ENABLE_CODEGEN_mips
6582#include "nodes_mips.h"
6583#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006584#ifdef ART_ENABLE_CODEGEN_x86
6585#include "nodes_x86.h"
6586#endif
6587
6588namespace art {
6589
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006590class HGraphVisitor : public ValueObject {
6591 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006592 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6593 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006594
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006595 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006596 virtual void VisitBasicBlock(HBasicBlock* block);
6597
Roland Levillain633021e2014-10-01 14:12:25 +01006598 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006599 void VisitInsertionOrder();
6600
Roland Levillain633021e2014-10-01 14:12:25 +01006601 // Visit the graph following dominator tree reverse post-order.
6602 void VisitReversePostOrder();
6603
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006604 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006605
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006606 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006607#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006608 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6609
6610 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6611
6612#undef DECLARE_VISIT_INSTRUCTION
6613
6614 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006615 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006616
6617 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6618};
6619
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006620class HGraphDelegateVisitor : public HGraphVisitor {
6621 public:
6622 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6623 virtual ~HGraphDelegateVisitor() {}
6624
6625 // Visit functions that delegate to to super class.
6626#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006627 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006628
6629 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6630
6631#undef DECLARE_VISIT_INSTRUCTION
6632
6633 private:
6634 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6635};
6636
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006637class HInsertionOrderIterator : public ValueObject {
6638 public:
6639 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6640
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006641 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006642 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006643 void Advance() { ++index_; }
6644
6645 private:
6646 const HGraph& graph_;
6647 size_t index_;
6648
6649 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6650};
6651
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006652class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006653 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006654 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6655 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006656 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006657 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006658
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006659 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6660 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006661 void Advance() { ++index_; }
6662
6663 private:
6664 const HGraph& graph_;
6665 size_t index_;
6666
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006667 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006668};
6669
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006670class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006671 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006672 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006673 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006674 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006675 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006676 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006677
6678 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006679 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006680 void Advance() { --index_; }
6681
6682 private:
6683 const HGraph& graph_;
6684 size_t index_;
6685
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006686 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006687};
6688
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006689class HLinearPostOrderIterator : public ValueObject {
6690 public:
6691 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006692 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006693
6694 bool Done() const { return index_ == 0; }
6695
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006696 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006697
6698 void Advance() {
6699 --index_;
6700 DCHECK_GE(index_, 0U);
6701 }
6702
6703 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006704 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006705 size_t index_;
6706
6707 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6708};
6709
6710class HLinearOrderIterator : public ValueObject {
6711 public:
6712 explicit HLinearOrderIterator(const HGraph& graph)
6713 : order_(graph.GetLinearOrder()), index_(0) {}
6714
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006715 bool Done() const { return index_ == order_.size(); }
6716 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006717 void Advance() { ++index_; }
6718
6719 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006720 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006721 size_t index_;
6722
6723 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6724};
6725
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006726// Iterator over the blocks that art part of the loop. Includes blocks part
6727// of an inner loop. The order in which the blocks are iterated is on their
6728// block id.
6729class HBlocksInLoopIterator : public ValueObject {
6730 public:
6731 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6732 : blocks_in_loop_(info.GetBlocks()),
6733 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6734 index_(0) {
6735 if (!blocks_in_loop_.IsBitSet(index_)) {
6736 Advance();
6737 }
6738 }
6739
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006740 bool Done() const { return index_ == blocks_.size(); }
6741 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006742 void Advance() {
6743 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006744 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006745 if (blocks_in_loop_.IsBitSet(index_)) {
6746 break;
6747 }
6748 }
6749 }
6750
6751 private:
6752 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006753 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006754 size_t index_;
6755
6756 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6757};
6758
Mingyao Yang3584bce2015-05-19 16:01:59 -07006759// Iterator over the blocks that art part of the loop. Includes blocks part
6760// of an inner loop. The order in which the blocks are iterated is reverse
6761// post order.
6762class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6763 public:
6764 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6765 : blocks_in_loop_(info.GetBlocks()),
6766 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6767 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006768 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006769 Advance();
6770 }
6771 }
6772
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006773 bool Done() const { return index_ == blocks_.size(); }
6774 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006775 void Advance() {
6776 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006777 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6778 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006779 break;
6780 }
6781 }
6782 }
6783
6784 private:
6785 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006786 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006787 size_t index_;
6788
6789 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6790};
6791
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006792inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006793 if (constant->IsIntConstant()) {
6794 return constant->AsIntConstant()->GetValue();
6795 } else if (constant->IsLongConstant()) {
6796 return constant->AsLongConstant()->GetValue();
6797 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006798 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006799 return 0;
6800 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006801}
6802
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006803#define INSTRUCTION_TYPE_CHECK(type, super) \
6804 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6805 inline const H##type* HInstruction::As##type() const { \
6806 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6807 } \
6808 inline H##type* HInstruction::As##type() { \
6809 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6810 }
6811
6812 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6813#undef INSTRUCTION_TYPE_CHECK
6814
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006815// Create space in `blocks` for adding `number_of_new_blocks` entries
6816// starting at location `at`. Blocks after `at` are moved accordingly.
6817inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6818 size_t number_of_new_blocks,
6819 size_t after) {
6820 DCHECK_LT(after, blocks->size());
6821 size_t old_size = blocks->size();
6822 size_t new_size = old_size + number_of_new_blocks;
6823 blocks->resize(new_size);
6824 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6825}
6826
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006827} // namespace art
6828
6829#endif // ART_COMPILER_OPTIMIZING_NODES_H_