blob: daefc3c3aa4aae6f21422eca3e4862dd3e070ed8 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko60584552015-09-03 13:35:12 +000028#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010029#include "base/transform_array_ref.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010030#include "dex_file.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000031#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000032#include "handle.h"
33#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000034#include "invoke_type.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010035#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010036#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000037#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000038#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010039#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070040#include "primitive.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010041#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042
43namespace art {
44
David Brazdil1abb4192015-02-17 18:33:36 +000045class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010047class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010049class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000051class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000053class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000054class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000055class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000056class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000057class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010058class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010059class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010060class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010061class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000062class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010063class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000064class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000065
Mathieu Chartier736b5602015-09-02 14:54:11 -070066namespace mirror {
67class DexCache;
68} // namespace mirror
69
Nicolas Geoffray818f2102014-02-18 16:43:35 +000070static const int kDefaultNumberOfBlocks = 8;
71static const int kDefaultNumberOfSuccessors = 2;
72static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010073static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010074static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000075static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000076
Roland Levillain5b5b9312016-03-22 14:57:31 +000077// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
78static constexpr int32_t kMaxIntShiftDistance = 0x1f;
79// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
80static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000081
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010082static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070083static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010084
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010085static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
86
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060087static constexpr uint32_t kNoDexPc = -1;
88
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010089inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
90 // For the purposes of the compiler, the dex files must actually be the same object
91 // if we want to safely treat them as the same. This is especially important for JIT
92 // as custom class loaders can open the same underlying file (or memory) multiple
93 // times and provide different class resolution but no two class loaders should ever
94 // use the same DexFile object - doing so is an unsupported hack that can lead to
95 // all sorts of weird failures.
96 return &lhs == &rhs;
97}
98
Dave Allison20dfc792014-06-16 20:44:29 -070099enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700100 // All types.
101 kCondEQ, // ==
102 kCondNE, // !=
103 // Signed integers and floating-point numbers.
104 kCondLT, // <
105 kCondLE, // <=
106 kCondGT, // >
107 kCondGE, // >=
108 // Unsigned integers.
109 kCondB, // <
110 kCondBE, // <=
111 kCondA, // >
112 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100113 // First and last aliases.
114 kCondFirst = kCondEQ,
115 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700116};
117
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000118enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000119 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000120 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000121 kAnalysisFailThrowCatchLoop,
122 kAnalysisFailAmbiguousArrayOp,
123 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000124};
125
Vladimir Markof9f64412015-09-02 14:05:49 +0100126class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100127 public:
128 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
129
130 void AddInstruction(HInstruction* instruction);
131 void RemoveInstruction(HInstruction* instruction);
132
David Brazdilc3d743f2015-04-22 13:40:50 +0100133 // Insert `instruction` before/after an existing instruction `cursor`.
134 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
135 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
136
Roland Levillain6b469232014-09-25 10:10:38 +0100137 // Return true if this list contains `instruction`.
138 bool Contains(HInstruction* instruction) const;
139
Roland Levillainccc07a92014-09-16 14:48:16 +0100140 // Return true if `instruction1` is found before `instruction2` in
141 // this instruction list and false otherwise. Abort if none
142 // of these instructions is found.
143 bool FoundBefore(const HInstruction* instruction1,
144 const HInstruction* instruction2) const;
145
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000146 bool IsEmpty() const { return first_instruction_ == nullptr; }
147 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
148
149 // Update the block of all instructions to be `block`.
150 void SetBlockOfInstructions(HBasicBlock* block) const;
151
152 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000153 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000154 void Add(const HInstructionList& instruction_list);
155
David Brazdil2d7352b2015-04-20 14:52:42 +0100156 // Return the number of instructions in the list. This is an expensive operation.
157 size_t CountSize() const;
158
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100159 private:
160 HInstruction* first_instruction_;
161 HInstruction* last_instruction_;
162
163 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000164 friend class HGraph;
165 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100166 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100167 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100168
169 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
170};
171
David Brazdil4833f5a2015-12-16 10:37:39 +0000172class ReferenceTypeInfo : ValueObject {
173 public:
174 typedef Handle<mirror::Class> TypeHandle;
175
Vladimir Markoa1de9182016-02-25 11:37:38 +0000176 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
177
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700178 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100179 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
180 }
181
Vladimir Markoa1de9182016-02-25 11:37:38 +0000182 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000183 return ReferenceTypeInfo(type_handle, is_exact);
184 }
185
186 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
187
Vladimir Markof39745e2016-01-26 12:16:55 +0000188 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000189 return handle.GetReference() != nullptr;
190 }
191
Vladimir Marko456307a2016-04-19 14:12:13 +0000192 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000193 return IsValidHandle(type_handle_);
194 }
195
196 bool IsExact() const { return is_exact_; }
197
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700198 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000199 DCHECK(IsValid());
200 return GetTypeHandle()->IsObjectClass();
201 }
202
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700203 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000204 DCHECK(IsValid());
205 return GetTypeHandle()->IsStringClass();
206 }
207
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700208 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000209 DCHECK(IsValid());
210 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
211 }
212
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700213 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000214 DCHECK(IsValid());
215 return GetTypeHandle()->IsInterface();
216 }
217
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700218 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000219 DCHECK(IsValid());
220 return GetTypeHandle()->IsArrayClass();
221 }
222
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700223 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000224 DCHECK(IsValid());
225 return GetTypeHandle()->IsPrimitiveArray();
226 }
227
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700228 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000229 DCHECK(IsValid());
230 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
231 }
232
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700233 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000234 DCHECK(IsValid());
235 if (!IsExact()) return false;
236 if (!IsArrayClass()) return false;
237 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
238 }
239
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000241 DCHECK(IsValid());
242 if (!IsExact()) return false;
243 if (!IsArrayClass()) return false;
244 if (!rti.IsArrayClass()) return false;
245 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
246 rti.GetTypeHandle()->GetComponentType());
247 }
248
249 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
250
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700251 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000252 DCHECK(IsValid());
253 DCHECK(rti.IsValid());
254 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
255 }
256
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700257 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000258 DCHECK(IsValid());
259 DCHECK(rti.IsValid());
260 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
261 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
262 }
263
264 // Returns true if the type information provide the same amount of details.
265 // Note that it does not mean that the instructions have the same actual type
266 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700267 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000268 if (!IsValid() && !rti.IsValid()) {
269 // Invalid types are equal.
270 return true;
271 }
272 if (!IsValid() || !rti.IsValid()) {
273 // One is valid, the other not.
274 return false;
275 }
276 return IsExact() == rti.IsExact()
277 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
278 }
279
280 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000281 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
282 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
283 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000284
285 // The class of the object.
286 TypeHandle type_handle_;
287 // Whether or not the type is exact or a superclass of the actual type.
288 // Whether or not we have any information about this type.
289 bool is_exact_;
290};
291
292std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
293
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000294// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100295class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000296 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100297 HGraph(ArenaAllocator* arena,
298 const DexFile& dex_file,
299 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100300 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700301 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100302 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100303 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000304 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100305 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000306 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100307 blocks_(arena->Adapter(kArenaAllocBlockList)),
308 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
309 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700310 entry_block_(nullptr),
311 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100312 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100313 number_of_vregs_(0),
314 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000315 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400316 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000317 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000318 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000319 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000320 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100321 dex_file_(dex_file),
322 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100323 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100324 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100325 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700326 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000327 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100328 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
329 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
330 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
331 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000332 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000333 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
334 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100335 blocks_.reserve(kDefaultNumberOfBlocks);
336 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000337
David Brazdilbadd8262016-02-02 16:28:56 +0000338 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700339 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000340
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000341 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100342 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
343
David Brazdil69ba7b72015-06-23 18:27:30 +0100344 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000345 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100346
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000347 HBasicBlock* GetEntryBlock() const { return entry_block_; }
348 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100349 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000350
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000351 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
352 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000353
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000354 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100355
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100356 void ComputeDominanceInformation();
357 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000358 void ClearLoopInformation();
359 void FindBackEdges(ArenaBitVector* visited);
360 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100361 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100362 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000363
David Brazdil4833f5a2015-12-16 10:37:39 +0000364 // Analyze all natural loops in this graph. Returns a code specifying that it
365 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000366 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000367 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100368
David Brazdilffee3d32015-07-06 11:48:53 +0100369 // Iterate over blocks to compute try block membership. Needs reverse post
370 // order and loop information.
371 void ComputeTryBlockInformation();
372
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000373 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000374 // Returns the instruction to replace the invoke expression or null if the
375 // invoke is for a void method. Note that the caller is responsible for replacing
376 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000377 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000378
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000379 // Update the loop and try membership of `block`, which was spawned from `reference`.
380 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
381 // should be the new back edge.
382 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
383 HBasicBlock* reference,
384 bool replace_if_back_edge);
385
Mingyao Yang3584bce2015-05-19 16:01:59 -0700386 // Need to add a couple of blocks to test if the loop body is entered and
387 // put deoptimization instructions, etc.
388 void TransformLoopHeaderForBCE(HBasicBlock* header);
389
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000390 // Removes `block` from the graph. Assumes `block` has been disconnected from
391 // other blocks and has no instructions or phis.
392 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000393
David Brazdilfc6a86a2015-06-26 10:33:45 +0000394 // Splits the edge between `block` and `successor` while preserving the
395 // indices in the predecessor/successor lists. If there are multiple edges
396 // between the blocks, the lowest indices are used.
397 // Returns the new block which is empty and has the same dex pc as `successor`.
398 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
399
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100400 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
401 void SimplifyLoop(HBasicBlock* header);
402
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000403 int32_t GetNextInstructionId() {
404 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000405 return current_instruction_id_++;
406 }
407
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000408 int32_t GetCurrentInstructionId() const {
409 return current_instruction_id_;
410 }
411
412 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000413 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000414 current_instruction_id_ = id;
415 }
416
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100417 uint16_t GetMaximumNumberOfOutVRegs() const {
418 return maximum_number_of_out_vregs_;
419 }
420
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000421 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
422 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100423 }
424
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100425 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
426 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
427 }
428
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000429 void UpdateTemporariesVRegSlots(size_t slots) {
430 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100431 }
432
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000433 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100434 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000435 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100436 }
437
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100438 void SetNumberOfVRegs(uint16_t number_of_vregs) {
439 number_of_vregs_ = number_of_vregs;
440 }
441
442 uint16_t GetNumberOfVRegs() const {
443 return number_of_vregs_;
444 }
445
446 void SetNumberOfInVRegs(uint16_t value) {
447 number_of_in_vregs_ = value;
448 }
449
David Brazdildee58d62016-04-07 09:54:26 +0000450 uint16_t GetNumberOfInVRegs() const {
451 return number_of_in_vregs_;
452 }
453
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100454 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100455 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100456 return number_of_vregs_ - number_of_in_vregs_;
457 }
458
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100459 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100460 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100461 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100462
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100463 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100464 return linear_order_;
465 }
466
Mark Mendell1152c922015-04-24 17:06:35 -0400467 bool HasBoundsChecks() const {
468 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800469 }
470
Mark Mendell1152c922015-04-24 17:06:35 -0400471 void SetHasBoundsChecks(bool value) {
472 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800473 }
474
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100475 bool ShouldGenerateConstructorBarrier() const {
476 return should_generate_constructor_barrier_;
477 }
478
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000479 bool IsDebuggable() const { return debuggable_; }
480
David Brazdil8d5b8b22015-03-24 10:51:52 +0000481 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000482 // already, it is created and inserted into the graph. This method is only for
483 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600484 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000485
486 // TODO: This is problematic for the consistency of reference type propagation
487 // because it can be created anytime after the pass and thus it will be left
488 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600489 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000490
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600491 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
492 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000493 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600494 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
495 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000496 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600497 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
498 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000499 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600500 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
501 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000502 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000503
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100504 HCurrentMethod* GetCurrentMethod();
505
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100506 const DexFile& GetDexFile() const {
507 return dex_file_;
508 }
509
510 uint32_t GetMethodIdx() const {
511 return method_idx_;
512 }
513
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100514 InvokeType GetInvokeType() const {
515 return invoke_type_;
516 }
517
Mark Mendellc4701932015-04-10 13:18:51 -0400518 InstructionSet GetInstructionSet() const {
519 return instruction_set_;
520 }
521
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000522 bool IsCompilingOsr() const { return osr_; }
523
David Brazdil77a48ae2015-09-15 12:34:04 +0000524 bool HasTryCatch() const { return has_try_catch_; }
525 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100526
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000527 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
528 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
529
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100530 ArtMethod* GetArtMethod() const { return art_method_; }
531 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
532
Mark Mendellf6529172015-11-17 11:16:56 -0500533 // Returns an instruction with the opposite boolean value from 'cond'.
534 // The instruction has been inserted into the graph, either as a constant, or
535 // before cursor.
536 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
537
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000538 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
539
David Brazdil2d7352b2015-04-20 14:52:42 +0100540 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000541 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100542 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000543
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000544 template <class InstructionType, typename ValueType>
545 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600546 ArenaSafeMap<ValueType, InstructionType*>* cache,
547 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000548 // Try to find an existing constant of the given value.
549 InstructionType* constant = nullptr;
550 auto cached_constant = cache->find(value);
551 if (cached_constant != cache->end()) {
552 constant = cached_constant->second;
553 }
554
555 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100556 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000557 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600558 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000559 cache->Overwrite(value, constant);
560 InsertConstant(constant);
561 }
562 return constant;
563 }
564
David Brazdil8d5b8b22015-03-24 10:51:52 +0000565 void InsertConstant(HConstant* instruction);
566
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000567 // Cache a float constant into the graph. This method should only be
568 // called by the SsaBuilder when creating "equivalent" instructions.
569 void CacheFloatConstant(HFloatConstant* constant);
570
571 // See CacheFloatConstant comment.
572 void CacheDoubleConstant(HDoubleConstant* constant);
573
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000574 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000575
576 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100577 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000578
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100579 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100580 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000581
Aart Bik281c6812016-08-26 11:31:48 -0700582 // List of blocks to perform a linear order tree traversal. Unlike the reverse
583 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100584 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100585
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000586 HBasicBlock* entry_block_;
587 HBasicBlock* exit_block_;
588
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100589 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100590 uint16_t maximum_number_of_out_vregs_;
591
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100592 // The number of virtual registers in this method. Contains the parameters.
593 uint16_t number_of_vregs_;
594
595 // The number of virtual registers used by parameters of this method.
596 uint16_t number_of_in_vregs_;
597
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000598 // Number of vreg size slots that the temporaries use (used in baseline compiler).
599 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100600
Mark Mendell1152c922015-04-24 17:06:35 -0400601 // Has bounds checks. We can totally skip BCE if it's false.
602 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800603
David Brazdil77a48ae2015-09-15 12:34:04 +0000604 // Flag whether there are any try/catch blocks in the graph. We will skip
605 // try/catch-related passes if false.
606 bool has_try_catch_;
607
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000608 // Flag whether there are any irreducible loops in the graph.
609 bool has_irreducible_loops_;
610
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000611 // Indicates whether the graph should be compiled in a way that
612 // ensures full debuggability. If false, we can apply more
613 // aggressive optimizations that may limit the level of debugging.
614 const bool debuggable_;
615
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000616 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000617 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000618
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100619 // The dex file from which the method is from.
620 const DexFile& dex_file_;
621
622 // The method index in the dex file.
623 const uint32_t method_idx_;
624
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100625 // If inlined, this encodes how the callee is being invoked.
626 const InvokeType invoke_type_;
627
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100628 // Whether the graph has been transformed to SSA form. Only used
629 // in debug mode to ensure we are not using properties only valid
630 // for non-SSA form (like the number of temporaries).
631 bool in_ssa_form_;
632
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100633 const bool should_generate_constructor_barrier_;
634
Mathieu Chartiere401d142015-04-22 13:56:20 -0700635 const InstructionSet instruction_set_;
636
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000637 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000638 HNullConstant* cached_null_constant_;
639 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000640 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000641 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000642 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000643
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100644 HCurrentMethod* cached_current_method_;
645
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100646 // The ArtMethod this graph is for. Note that for AOT, it may be null,
647 // for example for methods whose declaring class could not be resolved
648 // (such as when the superclass could not be found).
649 ArtMethod* art_method_;
650
David Brazdil4833f5a2015-12-16 10:37:39 +0000651 // Keep the RTI of inexact Object to avoid having to pass stack handle
652 // collection pointer to passes which may create NullConstant.
653 ReferenceTypeInfo inexact_object_rti_;
654
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000655 // Whether we are compiling this graph for on stack replacement: this will
656 // make all loops seen as irreducible and emit special stack maps to mark
657 // compiled code entries which the interpreter can directly jump to.
658 const bool osr_;
659
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000660 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100661 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000662 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000663 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000664 DISALLOW_COPY_AND_ASSIGN(HGraph);
665};
666
Vladimir Markof9f64412015-09-02 14:05:49 +0100667class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000668 public:
669 HLoopInformation(HBasicBlock* header, HGraph* graph)
670 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100671 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000672 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100673 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100674 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100675 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000676 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100677 back_edges_.reserve(kDefaultNumberOfBackEdges);
678 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100679
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000680 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100681 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000682
683 void Dump(std::ostream& os);
684
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100685 HBasicBlock* GetHeader() const {
686 return header_;
687 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000688
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000689 void SetHeader(HBasicBlock* block) {
690 header_ = block;
691 }
692
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100693 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
694 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
695 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
696
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000697 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100698 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000699 }
700
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100701 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100702 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100703 }
704
David Brazdil46e2a392015-03-16 17:31:52 +0000705 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100706 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100707 }
708
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000709 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100710 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000711 }
712
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100713 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100714
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100715 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100716 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100717 }
718
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100719 // Returns the lifetime position of the back edge that has the
720 // greatest lifetime position.
721 size_t GetLifetimeEnd() const;
722
723 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100724 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100725 }
726
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000727 // Finds blocks that are part of this loop.
728 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100729
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100730 // Returns whether this loop information contains `block`.
731 // Note that this loop information *must* be populated before entering this function.
732 bool Contains(const HBasicBlock& block) const;
733
734 // Returns whether this loop information is an inner loop of `other`.
735 // Note that `other` *must* be populated before entering this function.
736 bool IsIn(const HLoopInformation& other) const;
737
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800738 // Returns true if instruction is not defined within this loop.
739 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700740
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100741 const ArenaBitVector& GetBlocks() const { return blocks_; }
742
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000743 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000744 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000745
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000746 void ClearAllBlocks() {
747 blocks_.ClearAllBits();
748 }
749
David Brazdil3f4a5222016-05-06 12:46:21 +0100750 bool HasBackEdgeNotDominatedByHeader() const;
751
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100752 bool IsPopulated() const {
753 return blocks_.GetHighestBitSet() != -1;
754 }
755
Anton Shaminf89381f2016-05-16 16:44:13 +0600756 bool DominatesAllBackEdges(HBasicBlock* block);
757
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000758 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100759 // Internal recursive implementation of `Populate`.
760 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100761 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100762
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000763 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100764 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000765 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100766 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100767 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100768 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000769
770 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
771};
772
David Brazdilec16f792015-08-19 15:04:01 +0100773// Stores try/catch information for basic blocks.
774// Note that HGraph is constructed so that catch blocks cannot simultaneously
775// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100776class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100777 public:
778 // Try block information constructor.
779 explicit TryCatchInformation(const HTryBoundary& try_entry)
780 : try_entry_(&try_entry),
781 catch_dex_file_(nullptr),
782 catch_type_index_(DexFile::kDexNoIndex16) {
783 DCHECK(try_entry_ != nullptr);
784 }
785
786 // Catch block information constructor.
787 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
788 : try_entry_(nullptr),
789 catch_dex_file_(&dex_file),
790 catch_type_index_(catch_type_index) {}
791
792 bool IsTryBlock() const { return try_entry_ != nullptr; }
793
794 const HTryBoundary& GetTryEntry() const {
795 DCHECK(IsTryBlock());
796 return *try_entry_;
797 }
798
799 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
800
801 bool IsCatchAllTypeIndex() const {
802 DCHECK(IsCatchBlock());
803 return catch_type_index_ == DexFile::kDexNoIndex16;
804 }
805
806 uint16_t GetCatchTypeIndex() const {
807 DCHECK(IsCatchBlock());
808 return catch_type_index_;
809 }
810
811 const DexFile& GetCatchDexFile() const {
812 DCHECK(IsCatchBlock());
813 return *catch_dex_file_;
814 }
815
816 private:
817 // One of possibly several TryBoundary instructions entering the block's try.
818 // Only set for try blocks.
819 const HTryBoundary* try_entry_;
820
821 // Exception type information. Only set for catch blocks.
822 const DexFile* catch_dex_file_;
823 const uint16_t catch_type_index_;
824};
825
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100826static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100827static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100828
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000829// A block in a method. Contains the list of instructions represented
830// as a double linked list. Each block knows its predecessors and
831// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100832
Vladimir Markof9f64412015-09-02 14:05:49 +0100833class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000834 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700835 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000836 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000837 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
838 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000839 loop_information_(nullptr),
840 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000841 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100842 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100843 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100844 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000845 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000846 try_catch_information_(nullptr) {
847 predecessors_.reserve(kDefaultNumberOfPredecessors);
848 successors_.reserve(kDefaultNumberOfSuccessors);
849 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
850 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000851
Vladimir Marko60584552015-09-03 13:35:12 +0000852 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100853 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000854 }
855
Vladimir Marko60584552015-09-03 13:35:12 +0000856 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100857 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000858 }
859
David Brazdild26a4112015-11-10 11:07:31 +0000860 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
861 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
862
Vladimir Marko60584552015-09-03 13:35:12 +0000863 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
864 return ContainsElement(successors_, block, start_from);
865 }
866
867 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100868 return dominated_blocks_;
869 }
870
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100871 bool IsEntryBlock() const {
872 return graph_->GetEntryBlock() == this;
873 }
874
875 bool IsExitBlock() const {
876 return graph_->GetExitBlock() == this;
877 }
878
David Brazdil46e2a392015-03-16 17:31:52 +0000879 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000880 bool IsSingleTryBoundary() const;
881
882 // Returns true if this block emits nothing but a jump.
883 bool IsSingleJump() const {
884 HLoopInformation* loop_info = GetLoopInformation();
885 return (IsSingleGoto() || IsSingleTryBoundary())
886 // Back edges generate a suspend check.
887 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
888 }
David Brazdil46e2a392015-03-16 17:31:52 +0000889
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000890 void AddBackEdge(HBasicBlock* back_edge) {
891 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000892 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000893 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100894 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000895 loop_information_->AddBackEdge(back_edge);
896 }
897
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000898 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000899 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000900
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100901 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000902 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600903 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000904
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000905 HBasicBlock* GetDominator() const { return dominator_; }
906 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000907 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
908
909 void RemoveDominatedBlock(HBasicBlock* block) {
910 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100911 }
Vladimir Marko60584552015-09-03 13:35:12 +0000912
913 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
914 ReplaceElement(dominated_blocks_, existing, new_block);
915 }
916
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100917 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000918
919 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100920 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000921 }
922
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100923 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
924 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100925 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100926 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100927 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
928 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000929
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000930 HInstruction* GetFirstInstructionDisregardMoves() const;
931
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000932 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000933 successors_.push_back(block);
934 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000935 }
936
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100937 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
938 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100939 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000940 new_block->predecessors_.push_back(this);
941 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000942 }
943
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000944 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
945 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000946 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000947 new_block->successors_.push_back(this);
948 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000949 }
950
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100951 // Insert `this` between `predecessor` and `successor. This method
952 // preserves the indicies, and will update the first edge found between
953 // `predecessor` and `successor`.
954 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
955 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100956 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000957 successor->predecessors_[predecessor_index] = this;
958 predecessor->successors_[successor_index] = this;
959 successors_.push_back(successor);
960 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100961 }
962
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100963 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000964 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100965 }
966
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000967 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000968 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000969 }
970
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100971 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000972 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100973 }
974
975 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000976 predecessors_.push_back(block);
977 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100978 }
979
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100980 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000981 DCHECK_EQ(predecessors_.size(), 2u);
982 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100983 }
984
David Brazdil769c9e52015-04-27 13:54:09 +0100985 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000986 DCHECK_EQ(successors_.size(), 2u);
987 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100988 }
989
David Brazdilfc6a86a2015-06-26 10:33:45 +0000990 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000991 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100992 }
993
David Brazdilfc6a86a2015-06-26 10:33:45 +0000994 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000995 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100996 }
997
David Brazdilfc6a86a2015-06-26 10:33:45 +0000998 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000999 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001000 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001001 }
1002
1003 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001004 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001005 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001006 }
1007
1008 // Returns whether the first occurrence of `predecessor` in the list of
1009 // predecessors is at index `idx`.
1010 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001011 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001012 return GetPredecessorIndexOf(predecessor) == idx;
1013 }
1014
David Brazdild7558da2015-09-22 13:04:14 +01001015 // Create a new block between this block and its predecessors. The new block
1016 // is added to the graph, all predecessor edges are relinked to it and an edge
1017 // is created to `this`. Returns the new empty block. Reverse post order or
1018 // loop and try/catch information are not updated.
1019 HBasicBlock* CreateImmediateDominator();
1020
David Brazdilfc6a86a2015-06-26 10:33:45 +00001021 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001022 // created, latter block. Note that this method will add the block to the
1023 // graph, create a Goto at the end of the former block and will create an edge
1024 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001025 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001026 HBasicBlock* SplitBefore(HInstruction* cursor);
1027
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001028 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001029 // created block. Note that this method just updates raw block information,
1030 // like predecessors, successors, dominators, and instruction list. It does not
1031 // update the graph, reverse post order, loop information, nor make sure the
1032 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001033 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1034
1035 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1036 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001037
1038 // Merge `other` at the end of `this`. Successors and dominated blocks of
1039 // `other` are changed to be successors and dominated blocks of `this`. Note
1040 // that this method does not update the graph, reverse post order, loop
1041 // information, nor make sure the blocks are consistent (for example ending
1042 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001043 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001044
1045 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1046 // of `this` are moved to `other`.
1047 // Note that this method does not update the graph, reverse post order, loop
1048 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001049 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001050 void ReplaceWith(HBasicBlock* other);
1051
David Brazdil2d7352b2015-04-20 14:52:42 +01001052 // Merge `other` at the end of `this`. This method updates loops, reverse post
1053 // order, links to predecessors, successors, dominators and deletes the block
1054 // from the graph. The two blocks must be successive, i.e. `this` the only
1055 // predecessor of `other` and vice versa.
1056 void MergeWith(HBasicBlock* other);
1057
1058 // Disconnects `this` from all its predecessors, successors and dominator,
1059 // removes it from all loops it is included in and eventually from the graph.
1060 // The block must not dominate any other block. Predecessors and successors
1061 // are safely updated.
1062 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001063
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001064 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001065 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001066 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001067 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001068 // Replace instruction `initial` with `replacement` within this block.
1069 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1070 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001071 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001072 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001073 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1074 // instruction list. With 'ensure_safety' set to true, it verifies that the
1075 // instruction is not in use and removes it from the use lists of its inputs.
1076 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1077 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001078 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001079
1080 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001081 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001082 }
1083
Roland Levillain6b879dd2014-09-22 17:13:44 +01001084 bool IsLoopPreHeaderFirstPredecessor() const {
1085 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001086 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001087 }
1088
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001089 bool IsFirstPredecessorBackEdge() const {
1090 DCHECK(IsLoopHeader());
1091 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1092 }
1093
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001094 HLoopInformation* GetLoopInformation() const {
1095 return loop_information_;
1096 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001097
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001098 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001099 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001100 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001101 void SetInLoop(HLoopInformation* info) {
1102 if (IsLoopHeader()) {
1103 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001104 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001105 loop_information_ = info;
1106 } else if (loop_information_->Contains(*info->GetHeader())) {
1107 // Block is currently part of an outer loop. Make it part of this inner loop.
1108 // Note that a non loop header having a loop information means this loop information
1109 // has already been populated
1110 loop_information_ = info;
1111 } else {
1112 // Block is part of an inner loop. Do not update the loop information.
1113 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1114 // at this point, because this method is being called while populating `info`.
1115 }
1116 }
1117
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001118 // Raw update of the loop information.
1119 void SetLoopInformation(HLoopInformation* info) {
1120 loop_information_ = info;
1121 }
1122
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001123 bool IsInLoop() const { return loop_information_ != nullptr; }
1124
David Brazdilec16f792015-08-19 15:04:01 +01001125 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1126
1127 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1128 try_catch_information_ = try_catch_information;
1129 }
1130
1131 bool IsTryBlock() const {
1132 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1133 }
1134
1135 bool IsCatchBlock() const {
1136 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1137 }
David Brazdilffee3d32015-07-06 11:48:53 +01001138
1139 // Returns the try entry that this block's successors should have. They will
1140 // be in the same try, unless the block ends in a try boundary. In that case,
1141 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001142 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001143
David Brazdild7558da2015-09-22 13:04:14 +01001144 bool HasThrowingInstructions() const;
1145
David Brazdila4b8c212015-05-07 09:59:30 +01001146 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001147 bool Dominates(HBasicBlock* block) const;
1148
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001149 size_t GetLifetimeStart() const { return lifetime_start_; }
1150 size_t GetLifetimeEnd() const { return lifetime_end_; }
1151
1152 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1153 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1154
David Brazdil8d5b8b22015-03-24 10:51:52 +00001155 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001156 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001157 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001158 bool HasSinglePhi() const;
1159
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001160 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001161 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001162 ArenaVector<HBasicBlock*> predecessors_;
1163 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001164 HInstructionList instructions_;
1165 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001166 HLoopInformation* loop_information_;
1167 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001168 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001169 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001170 // The dex program counter of the first instruction of this block.
1171 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001172 size_t lifetime_start_;
1173 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001174 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001175
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001176 friend class HGraph;
1177 friend class HInstruction;
1178
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001179 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1180};
1181
David Brazdilb2bd1c52015-03-25 11:17:37 +00001182// Iterates over the LoopInformation of all loops which contain 'block'
1183// from the innermost to the outermost.
1184class HLoopInformationOutwardIterator : public ValueObject {
1185 public:
1186 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1187 : current_(block.GetLoopInformation()) {}
1188
1189 bool Done() const { return current_ == nullptr; }
1190
1191 void Advance() {
1192 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001193 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001194 }
1195
1196 HLoopInformation* Current() const {
1197 DCHECK(!Done());
1198 return current_;
1199 }
1200
1201 private:
1202 HLoopInformation* current_;
1203
1204 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1205};
1206
Alexandre Ramesef20f712015-06-09 10:29:30 +01001207#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001208 M(Above, Condition) \
1209 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001210 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001211 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001212 M(ArrayGet, Instruction) \
1213 M(ArrayLength, Instruction) \
1214 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001215 M(Below, Condition) \
1216 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001217 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001218 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001219 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001220 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001221 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001222 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001223 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001224 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001225 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001226 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001227 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001228 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001229 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001230 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001231 M(Exit, Instruction) \
1232 M(FloatConstant, Constant) \
1233 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001234 M(GreaterThan, Condition) \
1235 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001236 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001237 M(InstanceFieldGet, Instruction) \
1238 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001239 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001240 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001241 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001242 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001243 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001244 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001245 M(LessThan, Condition) \
1246 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001247 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001248 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001249 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001250 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001251 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001252 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001253 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001254 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001255 M(Neg, UnaryOperation) \
1256 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001257 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001258 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001259 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001260 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001261 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001262 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001263 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001264 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001265 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001266 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001267 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001268 M(Return, Instruction) \
1269 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001270 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001271 M(Shl, BinaryOperation) \
1272 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001273 M(StaticFieldGet, Instruction) \
1274 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001275 M(UnresolvedInstanceFieldGet, Instruction) \
1276 M(UnresolvedInstanceFieldSet, Instruction) \
1277 M(UnresolvedStaticFieldGet, Instruction) \
1278 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001279 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001280 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001281 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001282 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001283 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001284 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001285 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001286 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001287
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001288/*
1289 * Instructions, shared across several (not all) architectures.
1290 */
1291#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1292#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1293#else
1294#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001295 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001296 M(MultiplyAccumulate, Instruction) \
1297 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001298#endif
1299
Vladimir Markob4536b72015-11-24 13:45:23 +00001300#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001301#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001302#else
1303#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1304 M(ArmDexCacheArraysBase, Instruction)
1305#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001306
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001307#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001308#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001309#else
1310#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001311 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001312#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001313
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001314#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001315#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001316#else
1317#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1318 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001319 M(MipsDexCacheArraysBase, Instruction) \
1320 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001321#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001322
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001323#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1324
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001325#ifndef ART_ENABLE_CODEGEN_x86
1326#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1327#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001328#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1329 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001330 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001331 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001332 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001333#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001334
1335#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1336
1337#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1338 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001339 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001340 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1341 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001342 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001343 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001344 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1345 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1346
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001347#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1348 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001349 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001350 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001351 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001352 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001353
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001354#define FOR_EACH_INSTRUCTION(M) \
1355 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1356 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1357
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001358#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001359FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1360#undef FORWARD_DECLARATION
1361
Vladimir Marko372f10e2016-05-17 16:30:10 +01001362#define DECLARE_INSTRUCTION(type) \
1363 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1364 const char* DebugName() const OVERRIDE { return #type; } \
1365 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1366 return other->Is##type(); \
1367 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001368 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001369
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001370#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1371 bool Is##type() const { return As##type() != nullptr; } \
1372 const H##type* As##type() const { return this; } \
1373 H##type* As##type() { return this; }
1374
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001375template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001376class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001377 public:
David Brazdiled596192015-01-23 10:39:45 +00001378 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001379 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001380 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001381
Vladimir Marko46817b82016-03-29 12:21:58 +01001382 // Hook for the IntrusiveForwardList<>.
1383 // TODO: Hide this better.
1384 IntrusiveForwardListHook hook;
1385
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001386 private:
David Brazdiled596192015-01-23 10:39:45 +00001387 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001388 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001389
1390 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001391 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001392
Vladimir Marko46817b82016-03-29 12:21:58 +01001393 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001394
1395 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1396};
1397
David Brazdiled596192015-01-23 10:39:45 +00001398template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001399using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001400
David Brazdil1abb4192015-02-17 18:33:36 +00001401// This class is used by HEnvironment and HInstruction classes to record the
1402// instructions they use and pointers to the corresponding HUseListNodes kept
1403// by the used instructions.
1404template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001405class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001406 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001407 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1408 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001409
Vladimir Marko46817b82016-03-29 12:21:58 +01001410 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1411 : HUserRecord(old_record.instruction_, before_use_node) {}
1412 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1413 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001414 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001415 }
1416
1417 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001418 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1419 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001420
1421 private:
1422 // Instruction used by the user.
1423 HInstruction* instruction_;
1424
Vladimir Marko46817b82016-03-29 12:21:58 +01001425 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1426 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001427};
1428
Vladimir Markoe9004912016-06-16 16:50:52 +01001429// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1430// This is used for HInstruction::GetInputs() to return a container wrapper providing
1431// HInstruction* values even though the underlying container has HUserRecord<>s.
1432struct HInputExtractor {
1433 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1434 return record.GetInstruction();
1435 }
1436 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1437 return record.GetInstruction();
1438 }
1439};
1440
1441using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1442using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1443
Aart Bik854a02b2015-07-14 16:07:00 -07001444/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001445 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001446 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001447 * For write/read dependences on fields/arrays, the dependence analysis uses
1448 * type disambiguation (e.g. a float field write cannot modify the value of an
1449 * integer field read) and the access type (e.g. a reference array write cannot
1450 * modify the value of a reference field read [although it may modify the
1451 * reference fetch prior to reading the field, which is represented by its own
1452 * write/read dependence]). The analysis makes conservative points-to
1453 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1454 * the same, and any reference read depends on any reference read without
1455 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001456 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001457 * The internal representation uses 38-bit and is described in the table below.
1458 * The first line indicates the side effect, and for field/array accesses the
1459 * second line indicates the type of the access (in the order of the
1460 * Primitive::Type enum).
1461 * The two numbered lines below indicate the bit position in the bitfield (read
1462 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001463 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001464 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1465 * +-------------+---------+---------+--------------+---------+---------+
1466 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1467 * | 3 |333333322|222222221| 1 |111111110|000000000|
1468 * | 7 |654321098|765432109| 8 |765432109|876543210|
1469 *
1470 * Note that, to ease the implementation, 'changes' bits are least significant
1471 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001472 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001473class SideEffects : public ValueObject {
1474 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001475 SideEffects() : flags_(0) {}
1476
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001477 static SideEffects None() {
1478 return SideEffects(0);
1479 }
1480
1481 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001482 return SideEffects(kAllChangeBits | kAllDependOnBits);
1483 }
1484
1485 static SideEffects AllChanges() {
1486 return SideEffects(kAllChangeBits);
1487 }
1488
1489 static SideEffects AllDependencies() {
1490 return SideEffects(kAllDependOnBits);
1491 }
1492
1493 static SideEffects AllExceptGCDependency() {
1494 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1495 }
1496
1497 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001498 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001499 }
1500
Aart Bik34c3ba92015-07-20 14:08:59 -07001501 static SideEffects AllWrites() {
1502 return SideEffects(kAllWrites);
1503 }
1504
1505 static SideEffects AllReads() {
1506 return SideEffects(kAllReads);
1507 }
1508
1509 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1510 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001511 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001512 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001513 }
1514
Aart Bik854a02b2015-07-14 16:07:00 -07001515 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001516 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001517 }
1518
Aart Bik34c3ba92015-07-20 14:08:59 -07001519 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1520 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001521 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001522 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001523 }
1524
1525 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001526 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001527 }
1528
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001529 static SideEffects CanTriggerGC() {
1530 return SideEffects(1ULL << kCanTriggerGCBit);
1531 }
1532
1533 static SideEffects DependsOnGC() {
1534 return SideEffects(1ULL << kDependsOnGCBit);
1535 }
1536
Aart Bik854a02b2015-07-14 16:07:00 -07001537 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001538 SideEffects Union(SideEffects other) const {
1539 return SideEffects(flags_ | other.flags_);
1540 }
1541
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001542 SideEffects Exclusion(SideEffects other) const {
1543 return SideEffects(flags_ & ~other.flags_);
1544 }
1545
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001546 void Add(SideEffects other) {
1547 flags_ |= other.flags_;
1548 }
1549
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001550 bool Includes(SideEffects other) const {
1551 return (other.flags_ & flags_) == other.flags_;
1552 }
1553
1554 bool HasSideEffects() const {
1555 return (flags_ & kAllChangeBits);
1556 }
1557
1558 bool HasDependencies() const {
1559 return (flags_ & kAllDependOnBits);
1560 }
1561
1562 // Returns true if there are no side effects or dependencies.
1563 bool DoesNothing() const {
1564 return flags_ == 0;
1565 }
1566
Aart Bik854a02b2015-07-14 16:07:00 -07001567 // Returns true if something is written.
1568 bool DoesAnyWrite() const {
1569 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001570 }
1571
Aart Bik854a02b2015-07-14 16:07:00 -07001572 // Returns true if something is read.
1573 bool DoesAnyRead() const {
1574 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001575 }
1576
Aart Bik854a02b2015-07-14 16:07:00 -07001577 // Returns true if potentially everything is written and read
1578 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001579 bool DoesAllReadWrite() const {
1580 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1581 }
1582
Aart Bik854a02b2015-07-14 16:07:00 -07001583 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001584 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001585 }
1586
Roland Levillain0d5a2812015-11-13 10:07:31 +00001587 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001588 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001589 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1590 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001591 }
1592
1593 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001594 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001595 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001596 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001597 for (int s = kLastBit; s >= 0; s--) {
1598 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1599 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1600 // This is a bit for the GC side effect.
1601 if (current_bit_is_set) {
1602 flags += "GC";
1603 }
Aart Bik854a02b2015-07-14 16:07:00 -07001604 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001605 } else {
1606 // This is a bit for the array/field analysis.
1607 // The underscore character stands for the 'can trigger GC' bit.
1608 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1609 if (current_bit_is_set) {
1610 flags += kDebug[s];
1611 }
1612 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1613 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1614 flags += "|";
1615 }
1616 }
Aart Bik854a02b2015-07-14 16:07:00 -07001617 }
1618 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001619 }
1620
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001621 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001622
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001623 private:
1624 static constexpr int kFieldArrayAnalysisBits = 9;
1625
1626 static constexpr int kFieldWriteOffset = 0;
1627 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1628 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1629 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1630
1631 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1632
1633 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1634 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1635 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1636 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1637
1638 static constexpr int kLastBit = kDependsOnGCBit;
1639 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1640
1641 // Aliases.
1642
1643 static_assert(kChangeBits == kDependOnBits,
1644 "the 'change' bits should match the 'depend on' bits.");
1645
1646 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1647 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1648 static constexpr uint64_t kAllWrites =
1649 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1650 static constexpr uint64_t kAllReads =
1651 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001652
Aart Bik854a02b2015-07-14 16:07:00 -07001653 // Translates type to bit flag.
1654 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1655 CHECK_NE(type, Primitive::kPrimVoid);
1656 const uint64_t one = 1;
1657 const int shift = type; // 0-based consecutive enum
1658 DCHECK_LE(kFieldWriteOffset, shift);
1659 DCHECK_LT(shift, kArrayWriteOffset);
1660 return one << (type + offset);
1661 }
1662
1663 // Private constructor on direct flags value.
1664 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1665
1666 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001667};
1668
David Brazdiled596192015-01-23 10:39:45 +00001669// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001670class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001671 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001672 HEnvironment(ArenaAllocator* arena,
1673 size_t number_of_vregs,
1674 const DexFile& dex_file,
1675 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001676 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001677 InvokeType invoke_type,
1678 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001679 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1680 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001681 parent_(nullptr),
1682 dex_file_(dex_file),
1683 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001684 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001685 invoke_type_(invoke_type),
1686 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001687 }
1688
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001689 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001690 : HEnvironment(arena,
1691 to_copy.Size(),
1692 to_copy.GetDexFile(),
1693 to_copy.GetMethodIdx(),
1694 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001695 to_copy.GetInvokeType(),
1696 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001697
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001698 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001699 if (parent_ != nullptr) {
1700 parent_->SetAndCopyParentChain(allocator, parent);
1701 } else {
1702 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1703 parent_->CopyFrom(parent);
1704 if (parent->GetParent() != nullptr) {
1705 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1706 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001707 }
David Brazdiled596192015-01-23 10:39:45 +00001708 }
1709
Vladimir Marko71bf8092015-09-15 15:33:14 +01001710 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001711 void CopyFrom(HEnvironment* environment);
1712
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001713 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1714 // input to the loop phi instead. This is for inserting instructions that
1715 // require an environment (like HDeoptimization) in the loop pre-header.
1716 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001717
1718 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001719 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001720 }
1721
1722 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001723 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001724 }
1725
David Brazdil1abb4192015-02-17 18:33:36 +00001726 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001727
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001728 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001729
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001730 HEnvironment* GetParent() const { return parent_; }
1731
1732 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001733 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001734 }
1735
1736 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001737 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001738 }
1739
1740 uint32_t GetDexPc() const {
1741 return dex_pc_;
1742 }
1743
1744 uint32_t GetMethodIdx() const {
1745 return method_idx_;
1746 }
1747
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001748 InvokeType GetInvokeType() const {
1749 return invoke_type_;
1750 }
1751
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001752 const DexFile& GetDexFile() const {
1753 return dex_file_;
1754 }
1755
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001756 HInstruction* GetHolder() const {
1757 return holder_;
1758 }
1759
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001760
1761 bool IsFromInlinedInvoke() const {
1762 return GetParent() != nullptr;
1763 }
1764
David Brazdiled596192015-01-23 10:39:45 +00001765 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001766 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1767 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001768 HEnvironment* parent_;
1769 const DexFile& dex_file_;
1770 const uint32_t method_idx_;
1771 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001772 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001773
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001774 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001775 HInstruction* const holder_;
1776
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001777 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001778
1779 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1780};
1781
Vladimir Markof9f64412015-09-02 14:05:49 +01001782class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001783 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001784 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001785 : previous_(nullptr),
1786 next_(nullptr),
1787 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001788 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001789 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001790 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001791 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001792 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001793 locations_(nullptr),
1794 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001795 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001796 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001797 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1798 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1799 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001800
Dave Allison20dfc792014-06-16 20:44:29 -07001801 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001802
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001803#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001804 enum InstructionKind {
1805 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1806 };
1807#undef DECLARE_KIND
1808
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001809 HInstruction* GetNext() const { return next_; }
1810 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001811
Calin Juravle77520bc2015-01-12 18:45:46 +00001812 HInstruction* GetNextDisregardingMoves() const;
1813 HInstruction* GetPreviousDisregardingMoves() const;
1814
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001815 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001816 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001817 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001818 bool IsInBlock() const { return block_ != nullptr; }
1819 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001820 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1821 bool IsIrreducibleLoopHeaderPhi() const {
1822 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1823 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001824
Vladimir Marko372f10e2016-05-17 16:30:10 +01001825 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1826
1827 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1828 // One virtual method is enough, just const_cast<> and then re-add the const.
1829 return ArrayRef<const HUserRecord<HInstruction*>>(
1830 const_cast<HInstruction*>(this)->GetInputRecords());
1831 }
1832
Vladimir Markoe9004912016-06-16 16:50:52 +01001833 HInputsRef GetInputs() {
1834 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001835 }
1836
Vladimir Markoe9004912016-06-16 16:50:52 +01001837 HConstInputsRef GetInputs() const {
1838 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001839 }
1840
1841 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001842 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001843
Vladimir Marko372f10e2016-05-17 16:30:10 +01001844 void SetRawInputAt(size_t index, HInstruction* input) {
1845 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1846 }
1847
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001848 virtual void Accept(HGraphVisitor* visitor) = 0;
1849 virtual const char* DebugName() const = 0;
1850
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001851 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1852
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001853 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001854
1855 uint32_t GetDexPc() const { return dex_pc_; }
1856
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001857 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001858
Roland Levillaine161a2a2014-10-03 12:45:18 +01001859 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001860 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001861
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001862 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001863 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001864
Calin Juravle10e244f2015-01-26 18:54:32 +00001865 // Does not apply for all instructions, but having this at top level greatly
1866 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001867 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001868 virtual bool CanBeNull() const {
1869 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1870 return true;
1871 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001872
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001873 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001874 return false;
1875 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001876
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001877 virtual bool IsActualObject() const {
1878 return GetType() == Primitive::kPrimNot;
1879 }
1880
Calin Juravle2e768302015-07-28 14:41:11 +00001881 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001882
Calin Juravle61d544b2015-02-23 16:46:57 +00001883 ReferenceTypeInfo GetReferenceTypeInfo() const {
1884 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001885 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001886 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001887 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001888
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001889 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001890 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001891 // Note: fixup_end remains valid across push_front().
1892 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1893 HUseListNode<HInstruction*>* new_node =
1894 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1895 uses_.push_front(*new_node);
1896 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001897 }
1898
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001899 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001900 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001901 // Note: env_fixup_end remains valid across push_front().
1902 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1903 HUseListNode<HEnvironment*>* new_node =
1904 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1905 env_uses_.push_front(*new_node);
1906 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001907 }
1908
David Brazdil1abb4192015-02-17 18:33:36 +00001909 void RemoveAsUserOfInput(size_t input) {
1910 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001911 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1912 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1913 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001914 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001915
Vladimir Marko372f10e2016-05-17 16:30:10 +01001916 void RemoveAsUserOfAllInputs() {
1917 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1918 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1919 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1920 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1921 }
1922 }
1923
David Brazdil1abb4192015-02-17 18:33:36 +00001924 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1925 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001926
Vladimir Marko46817b82016-03-29 12:21:58 +01001927 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1928 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1929 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001930 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001931 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001932 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001933
Aart Bik482095d2016-10-10 15:39:10 -07001934 bool IsDeadAndRemovable() const {
1935 return
1936 !HasSideEffects() &&
1937 !CanThrow() &&
1938 !IsSuspendCheck() &&
1939 !IsControlFlow() &&
1940 !IsNativeDebugInfo() &&
1941 !IsParameterValue() &&
1942 !HasUses() &&
1943 // If we added an explicit barrier then we should keep it.
1944 !IsMemoryBarrier();
1945 }
1946
Roland Levillain6c82d402014-10-13 16:10:27 +01001947 // Does this instruction strictly dominate `other_instruction`?
1948 // Returns false if this instruction and `other_instruction` are the same.
1949 // Aborts if this instruction and `other_instruction` are both phis.
1950 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001951
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001952 int GetId() const { return id_; }
1953 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001954
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001955 int GetSsaIndex() const { return ssa_index_; }
1956 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1957 bool HasSsaIndex() const { return ssa_index_ != -1; }
1958
1959 bool HasEnvironment() const { return environment_ != nullptr; }
1960 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001961 // Set the `environment_` field. Raw because this method does not
1962 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001963 void SetRawEnvironment(HEnvironment* environment) {
1964 DCHECK(environment_ == nullptr);
1965 DCHECK_EQ(environment->GetHolder(), this);
1966 environment_ = environment;
1967 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001968
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001969 void InsertRawEnvironment(HEnvironment* environment) {
1970 DCHECK(environment_ != nullptr);
1971 DCHECK_EQ(environment->GetHolder(), this);
1972 DCHECK(environment->GetParent() == nullptr);
1973 environment->parent_ = environment_;
1974 environment_ = environment;
1975 }
1976
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001977 void RemoveEnvironment();
1978
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001979 // Set the environment of this instruction, copying it from `environment`. While
1980 // copying, the uses lists are being updated.
1981 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001982 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001983 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001984 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001985 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001986 if (environment->GetParent() != nullptr) {
1987 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1988 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001989 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001990
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001991 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1992 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001993 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001994 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001995 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001996 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001997 if (environment->GetParent() != nullptr) {
1998 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1999 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002000 }
2001
Nicolas Geoffray39468442014-09-02 15:17:15 +01002002 // Returns the number of entries in the environment. Typically, that is the
2003 // number of dex registers in a method. It could be more in case of inlining.
2004 size_t EnvironmentSize() const;
2005
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002006 LocationSummary* GetLocations() const { return locations_; }
2007 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002008
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002009 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002010 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002011
Alexandre Rames188d4312015-04-09 18:30:21 +01002012 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2013 // uses of this instruction by `other` are *not* updated.
2014 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2015 ReplaceWith(other);
2016 other->ReplaceInput(this, use_index);
2017 }
2018
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002019 // Move `this` instruction before `cursor`.
2020 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002021
Vladimir Markofb337ea2015-11-25 15:25:10 +00002022 // Move `this` before its first user and out of any loops. If there is no
2023 // out-of-loop user that dominates all other users, move the instruction
2024 // to the end of the out-of-loop common dominator of the user's blocks.
2025 //
2026 // This can be used only on non-throwing instructions with no side effects that
2027 // have at least one use but no environment uses.
2028 void MoveBeforeFirstUserAndOutOfLoops();
2029
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002030#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002031 bool Is##type() const; \
2032 const H##type* As##type() const; \
2033 H##type* As##type();
2034
2035 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2036#undef INSTRUCTION_TYPE_CHECK
2037
2038#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002039 bool Is##type() const { return (As##type() != nullptr); } \
2040 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002041 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002042 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002043#undef INSTRUCTION_TYPE_CHECK
2044
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002045 // Returns whether the instruction can be moved within the graph.
2046 virtual bool CanBeMoved() const { return false; }
2047
2048 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002049 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002050 return false;
2051 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002052
2053 // Returns whether any data encoded in the two instructions is equal.
2054 // This method does not look at the inputs. Both instructions must be
2055 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002056 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002057 return false;
2058 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002059
2060 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002061 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002062 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002063 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002064
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002065 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2066 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2067 // the virtual function because the __attribute__((__pure__)) doesn't really
2068 // apply the strong requirement for virtual functions, preventing optimizations.
2069 InstructionKind GetKind() const PURE;
2070 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002071
2072 virtual size_t ComputeHashCode() const {
2073 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002074 for (const HInstruction* input : GetInputs()) {
2075 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002076 }
2077 return result;
2078 }
2079
2080 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002081 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002082 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002083
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002084 size_t GetLifetimePosition() const { return lifetime_position_; }
2085 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2086 LiveInterval* GetLiveInterval() const { return live_interval_; }
2087 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2088 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2089
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002090 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2091
2092 // Returns whether the code generation of the instruction will require to have access
2093 // to the current method. Such instructions are:
2094 // (1): Instructions that require an environment, as calling the runtime requires
2095 // to walk the stack and have the current method stored at a specific stack address.
2096 // (2): Object literals like classes and strings, that are loaded from the dex cache
2097 // fields of the current method.
2098 bool NeedsCurrentMethod() const {
2099 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2100 }
2101
Vladimir Markodc151b22015-10-15 18:02:30 +01002102 // Returns whether the code generation of the instruction will require to have access
2103 // to the dex cache of the current method's declaring class via the current method.
2104 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002105
Mark Mendellc4701932015-04-10 13:18:51 -04002106 // Does this instruction have any use in an environment before
2107 // control flow hits 'other'?
2108 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2109
2110 // Remove all references to environment uses of this instruction.
2111 // The caller must ensure that this is safe to do.
2112 void RemoveEnvironmentUsers();
2113
Vladimir Markoa1de9182016-02-25 11:37:38 +00002114 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2115 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002116
David Brazdil1abb4192015-02-17 18:33:36 +00002117 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002118 // If set, the machine code for this instruction is assumed to be generated by
2119 // its users. Used by liveness analysis to compute use positions accordingly.
2120 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2121 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2122 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2123 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2124
Vladimir Marko372f10e2016-05-17 16:30:10 +01002125 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2126 return GetInputRecords()[i];
2127 }
2128
2129 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2130 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2131 input_records[index] = input;
2132 }
David Brazdil1abb4192015-02-17 18:33:36 +00002133
Vladimir Markoa1de9182016-02-25 11:37:38 +00002134 uint32_t GetPackedFields() const {
2135 return packed_fields_;
2136 }
2137
2138 template <size_t flag>
2139 bool GetPackedFlag() const {
2140 return (packed_fields_ & (1u << flag)) != 0u;
2141 }
2142
2143 template <size_t flag>
2144 void SetPackedFlag(bool value = true) {
2145 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2146 }
2147
2148 template <typename BitFieldType>
2149 typename BitFieldType::value_type GetPackedField() const {
2150 return BitFieldType::Decode(packed_fields_);
2151 }
2152
2153 template <typename BitFieldType>
2154 void SetPackedField(typename BitFieldType::value_type value) {
2155 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2156 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2157 }
2158
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002159 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002160 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2161 auto before_use_node = uses_.before_begin();
2162 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2163 HInstruction* user = use_node->GetUser();
2164 size_t input_index = use_node->GetIndex();
2165 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2166 before_use_node = use_node;
2167 }
2168 }
2169
2170 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2171 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2172 if (next != uses_.end()) {
2173 HInstruction* next_user = next->GetUser();
2174 size_t next_index = next->GetIndex();
2175 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2176 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2177 }
2178 }
2179
2180 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2181 auto before_env_use_node = env_uses_.before_begin();
2182 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2183 HEnvironment* user = env_use_node->GetUser();
2184 size_t input_index = env_use_node->GetIndex();
2185 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2186 before_env_use_node = env_use_node;
2187 }
2188 }
2189
2190 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2191 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2192 if (next != env_uses_.end()) {
2193 HEnvironment* next_user = next->GetUser();
2194 size_t next_index = next->GetIndex();
2195 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2196 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2197 }
2198 }
David Brazdil1abb4192015-02-17 18:33:36 +00002199
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002200 HInstruction* previous_;
2201 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002202 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002203 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002204
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002205 // An instruction gets an id when it is added to the graph.
2206 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002207 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002208 int id_;
2209
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002210 // When doing liveness analysis, instructions that have uses get an SSA index.
2211 int ssa_index_;
2212
Vladimir Markoa1de9182016-02-25 11:37:38 +00002213 // Packed fields.
2214 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002215
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002216 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002217 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002218
2219 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002220 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002221
Nicolas Geoffray39468442014-09-02 15:17:15 +01002222 // The environment associated with this instruction. Not null if the instruction
2223 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002224 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002225
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002226 // Set by the code generator.
2227 LocationSummary* locations_;
2228
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002229 // Set by the liveness analysis.
2230 LiveInterval* live_interval_;
2231
2232 // Set by the liveness analysis, this is the position in a linear
2233 // order of blocks where this instruction's live interval start.
2234 size_t lifetime_position_;
2235
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002236 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002237
Vladimir Markoa1de9182016-02-25 11:37:38 +00002238 // The reference handle part of the reference type info.
2239 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002240 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002241 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002242
David Brazdil1abb4192015-02-17 18:33:36 +00002243 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002244 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002245 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002246 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002247 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002248
2249 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2250};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002251std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002252
2253class HInstructionIterator : public ValueObject {
2254 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002255 explicit HInstructionIterator(const HInstructionList& instructions)
2256 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002257 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002258 }
2259
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002260 bool Done() const { return instruction_ == nullptr; }
2261 HInstruction* Current() const { return instruction_; }
2262 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002263 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002264 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002265 }
2266
2267 private:
2268 HInstruction* instruction_;
2269 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002270
2271 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002272};
2273
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002274class HBackwardInstructionIterator : public ValueObject {
2275 public:
2276 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2277 : instruction_(instructions.last_instruction_) {
2278 next_ = Done() ? nullptr : instruction_->GetPrevious();
2279 }
2280
2281 bool Done() const { return instruction_ == nullptr; }
2282 HInstruction* Current() const { return instruction_; }
2283 void Advance() {
2284 instruction_ = next_;
2285 next_ = Done() ? nullptr : instruction_->GetPrevious();
2286 }
2287
2288 private:
2289 HInstruction* instruction_;
2290 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002291
2292 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002293};
2294
Vladimir Markof9f64412015-09-02 14:05:49 +01002295template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002296class HTemplateInstruction: public HInstruction {
2297 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002298 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002299 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002300 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002301
Vladimir Marko372f10e2016-05-17 16:30:10 +01002302 using HInstruction::GetInputRecords; // Keep the const version visible.
2303 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2304 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002305 }
2306
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002307 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002308 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002309
2310 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002311};
2312
Vladimir Markof9f64412015-09-02 14:05:49 +01002313// HTemplateInstruction specialization for N=0.
2314template<>
2315class HTemplateInstruction<0>: public HInstruction {
2316 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002317 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002318 : HInstruction(side_effects, dex_pc) {}
2319
Vladimir Markof9f64412015-09-02 14:05:49 +01002320 virtual ~HTemplateInstruction() {}
2321
Vladimir Marko372f10e2016-05-17 16:30:10 +01002322 using HInstruction::GetInputRecords; // Keep the const version visible.
2323 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2324 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002325 }
2326
2327 private:
2328 friend class SsaBuilder;
2329};
2330
Dave Allison20dfc792014-06-16 20:44:29 -07002331template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002332class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002333 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002334 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002335 : HTemplateInstruction<N>(side_effects, dex_pc) {
2336 this->template SetPackedField<TypeField>(type);
2337 }
Dave Allison20dfc792014-06-16 20:44:29 -07002338 virtual ~HExpression() {}
2339
Vladimir Markoa1de9182016-02-25 11:37:38 +00002340 Primitive::Type GetType() const OVERRIDE {
2341 return TypeField::Decode(this->GetPackedFields());
2342 }
Dave Allison20dfc792014-06-16 20:44:29 -07002343
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002344 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002345 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2346 static constexpr size_t kFieldTypeSize =
2347 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2348 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2349 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2350 "Too many packed fields.");
2351 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002352};
2353
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002354// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2355// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002356class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002357 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002358 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2359 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002360
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002361 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002362
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002363 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002364
2365 private:
2366 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2367};
2368
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002369// Represents dex's RETURN opcodes. A HReturn is a control flow
2370// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002371class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002372 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002373 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2374 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002375 SetRawInputAt(0, value);
2376 }
2377
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002378 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002379
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002380 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002381
2382 private:
2383 DISALLOW_COPY_AND_ASSIGN(HReturn);
2384};
2385
Vladimir Markofcb503c2016-05-18 12:48:17 +01002386class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002387 public:
2388 HPhi(ArenaAllocator* arena,
2389 uint32_t reg_number,
2390 size_t number_of_inputs,
2391 Primitive::Type type,
2392 uint32_t dex_pc = kNoDexPc)
2393 : HInstruction(SideEffects::None(), dex_pc),
2394 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2395 reg_number_(reg_number) {
2396 SetPackedField<TypeField>(ToPhiType(type));
2397 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2398 // Phis are constructed live and marked dead if conflicting or unused.
2399 // Individual steps of SsaBuilder should assume that if a phi has been
2400 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2401 SetPackedFlag<kFlagIsLive>(true);
2402 SetPackedFlag<kFlagCanBeNull>(true);
2403 }
2404
2405 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2406 static Primitive::Type ToPhiType(Primitive::Type type) {
2407 return Primitive::PrimitiveKind(type);
2408 }
2409
2410 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2411
Vladimir Marko372f10e2016-05-17 16:30:10 +01002412 using HInstruction::GetInputRecords; // Keep the const version visible.
2413 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2414 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2415 }
David Brazdildee58d62016-04-07 09:54:26 +00002416
2417 void AddInput(HInstruction* input);
2418 void RemoveInputAt(size_t index);
2419
2420 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2421 void SetType(Primitive::Type new_type) {
2422 // Make sure that only valid type changes occur. The following are allowed:
2423 // (1) int -> float/ref (primitive type propagation),
2424 // (2) long -> double (primitive type propagation).
2425 DCHECK(GetType() == new_type ||
2426 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2427 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2428 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2429 SetPackedField<TypeField>(new_type);
2430 }
2431
2432 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2433 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2434
2435 uint32_t GetRegNumber() const { return reg_number_; }
2436
2437 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2438 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2439 bool IsDead() const { return !IsLive(); }
2440 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2441
Vladimir Markoe9004912016-06-16 16:50:52 +01002442 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002443 return other != nullptr
2444 && other->IsPhi()
2445 && other->AsPhi()->GetBlock() == GetBlock()
2446 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2447 }
2448
2449 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2450 // An equivalent phi is a phi having the same dex register and type.
2451 // It assumes that phis with the same dex register are adjacent.
2452 HPhi* GetNextEquivalentPhiWithSameType() {
2453 HInstruction* next = GetNext();
2454 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2455 if (next->GetType() == GetType()) {
2456 return next->AsPhi();
2457 }
2458 next = next->GetNext();
2459 }
2460 return nullptr;
2461 }
2462
2463 DECLARE_INSTRUCTION(Phi);
2464
David Brazdildee58d62016-04-07 09:54:26 +00002465 private:
2466 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2467 static constexpr size_t kFieldTypeSize =
2468 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2469 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2470 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2471 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2472 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2473 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2474
Vladimir Marko372f10e2016-05-17 16:30:10 +01002475 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002476 const uint32_t reg_number_;
2477
2478 DISALLOW_COPY_AND_ASSIGN(HPhi);
2479};
2480
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002481// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002482// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002483// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002484class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002485 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002486 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002487
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002488 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002489
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002490 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002491
2492 private:
2493 DISALLOW_COPY_AND_ASSIGN(HExit);
2494};
2495
2496// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002497class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002498 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002499 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002500
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002501 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002502
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002503 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002504 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002505 }
2506
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002507 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002508
2509 private:
2510 DISALLOW_COPY_AND_ASSIGN(HGoto);
2511};
2512
Roland Levillain9867bc72015-08-05 10:21:34 +01002513class HConstant : public HExpression<0> {
2514 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002515 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2516 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002517
2518 bool CanBeMoved() const OVERRIDE { return true; }
2519
Roland Levillain1a653882016-03-18 18:05:57 +00002520 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002521 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002522 // Is this constant 0 in the arithmetic sense?
2523 virtual bool IsArithmeticZero() const { return false; }
2524 // Is this constant a 0-bit pattern?
2525 virtual bool IsZeroBitPattern() const { return false; }
2526 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002527 virtual bool IsOne() const { return false; }
2528
David Brazdil77a48ae2015-09-15 12:34:04 +00002529 virtual uint64_t GetValueAsUint64() const = 0;
2530
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002531 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002532
2533 private:
2534 DISALLOW_COPY_AND_ASSIGN(HConstant);
2535};
2536
Vladimir Markofcb503c2016-05-18 12:48:17 +01002537class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002538 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002539 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002540 return true;
2541 }
2542
David Brazdil77a48ae2015-09-15 12:34:04 +00002543 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2544
Roland Levillain9867bc72015-08-05 10:21:34 +01002545 size_t ComputeHashCode() const OVERRIDE { return 0; }
2546
Roland Levillain1a653882016-03-18 18:05:57 +00002547 // The null constant representation is a 0-bit pattern.
2548 virtual bool IsZeroBitPattern() const { return true; }
2549
Roland Levillain9867bc72015-08-05 10:21:34 +01002550 DECLARE_INSTRUCTION(NullConstant);
2551
2552 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002553 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002554
2555 friend class HGraph;
2556 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2557};
2558
2559// Constants of the type int. Those can be from Dex instructions, or
2560// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002561class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002562 public:
2563 int32_t GetValue() const { return value_; }
2564
David Brazdil9f389d42015-10-01 14:32:56 +01002565 uint64_t GetValueAsUint64() const OVERRIDE {
2566 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2567 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002568
Vladimir Marko372f10e2016-05-17 16:30:10 +01002569 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002570 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002571 return other->AsIntConstant()->value_ == value_;
2572 }
2573
2574 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2575
2576 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002577 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2578 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002579 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2580
Roland Levillain1a653882016-03-18 18:05:57 +00002581 // Integer constants are used to encode Boolean values as well,
2582 // where 1 means true and 0 means false.
2583 bool IsTrue() const { return GetValue() == 1; }
2584 bool IsFalse() const { return GetValue() == 0; }
2585
Roland Levillain9867bc72015-08-05 10:21:34 +01002586 DECLARE_INSTRUCTION(IntConstant);
2587
2588 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002589 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2590 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2591 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2592 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002593
2594 const int32_t value_;
2595
2596 friend class HGraph;
2597 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2598 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2599 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2600};
2601
Vladimir Markofcb503c2016-05-18 12:48:17 +01002602class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002603 public:
2604 int64_t GetValue() const { return value_; }
2605
David Brazdil77a48ae2015-09-15 12:34:04 +00002606 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2607
Vladimir Marko372f10e2016-05-17 16:30:10 +01002608 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002609 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002610 return other->AsLongConstant()->value_ == value_;
2611 }
2612
2613 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2614
2615 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002616 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2617 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002618 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2619
2620 DECLARE_INSTRUCTION(LongConstant);
2621
2622 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002623 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2624 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002625
2626 const int64_t value_;
2627
2628 friend class HGraph;
2629 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2630};
Dave Allison20dfc792014-06-16 20:44:29 -07002631
Vladimir Markofcb503c2016-05-18 12:48:17 +01002632class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002633 public:
2634 float GetValue() const { return value_; }
2635
2636 uint64_t GetValueAsUint64() const OVERRIDE {
2637 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2638 }
2639
Vladimir Marko372f10e2016-05-17 16:30:10 +01002640 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002641 DCHECK(other->IsFloatConstant()) << other->DebugName();
2642 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2643 }
2644
2645 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2646
2647 bool IsMinusOne() const OVERRIDE {
2648 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2649 }
Roland Levillain1a653882016-03-18 18:05:57 +00002650 bool IsArithmeticZero() const OVERRIDE {
2651 return std::fpclassify(value_) == FP_ZERO;
2652 }
2653 bool IsArithmeticPositiveZero() const {
2654 return IsArithmeticZero() && !std::signbit(value_);
2655 }
2656 bool IsArithmeticNegativeZero() const {
2657 return IsArithmeticZero() && std::signbit(value_);
2658 }
2659 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002660 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002661 }
2662 bool IsOne() const OVERRIDE {
2663 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2664 }
2665 bool IsNaN() const {
2666 return std::isnan(value_);
2667 }
2668
2669 DECLARE_INSTRUCTION(FloatConstant);
2670
2671 private:
2672 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2673 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2674 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2675 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2676
2677 const float value_;
2678
2679 // Only the SsaBuilder and HGraph can create floating-point constants.
2680 friend class SsaBuilder;
2681 friend class HGraph;
2682 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2683};
2684
Vladimir Markofcb503c2016-05-18 12:48:17 +01002685class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002686 public:
2687 double GetValue() const { return value_; }
2688
2689 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2690
Vladimir Marko372f10e2016-05-17 16:30:10 +01002691 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002692 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2693 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2694 }
2695
2696 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2697
2698 bool IsMinusOne() const OVERRIDE {
2699 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2700 }
Roland Levillain1a653882016-03-18 18:05:57 +00002701 bool IsArithmeticZero() const OVERRIDE {
2702 return std::fpclassify(value_) == FP_ZERO;
2703 }
2704 bool IsArithmeticPositiveZero() const {
2705 return IsArithmeticZero() && !std::signbit(value_);
2706 }
2707 bool IsArithmeticNegativeZero() const {
2708 return IsArithmeticZero() && std::signbit(value_);
2709 }
2710 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002711 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002712 }
2713 bool IsOne() const OVERRIDE {
2714 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2715 }
2716 bool IsNaN() const {
2717 return std::isnan(value_);
2718 }
2719
2720 DECLARE_INSTRUCTION(DoubleConstant);
2721
2722 private:
2723 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2724 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2725 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2726 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2727
2728 const double value_;
2729
2730 // Only the SsaBuilder and HGraph can create floating-point constants.
2731 friend class SsaBuilder;
2732 friend class HGraph;
2733 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2734};
2735
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002736// Conditional branch. A block ending with an HIf instruction must have
2737// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002738class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002739 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002740 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2741 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002742 SetRawInputAt(0, input);
2743 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002744
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002745 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002746
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002747 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002748 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002749 }
2750
2751 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002752 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002753 }
2754
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002755 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002756
2757 private:
2758 DISALLOW_COPY_AND_ASSIGN(HIf);
2759};
2760
David Brazdilfc6a86a2015-06-26 10:33:45 +00002761
2762// Abstract instruction which marks the beginning and/or end of a try block and
2763// links it to the respective exception handlers. Behaves the same as a Goto in
2764// non-exceptional control flow.
2765// Normal-flow successor is stored at index zero, exception handlers under
2766// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002767class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002768 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002769 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002770 kEntry,
2771 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002772 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002773 };
2774
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002775 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002776 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2777 SetPackedField<BoundaryKindField>(kind);
2778 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002779
2780 bool IsControlFlow() const OVERRIDE { return true; }
2781
2782 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002783 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002784
David Brazdild26a4112015-11-10 11:07:31 +00002785 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2786 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2787 }
2788
David Brazdilfc6a86a2015-06-26 10:33:45 +00002789 // Returns whether `handler` is among its exception handlers (non-zero index
2790 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002791 bool HasExceptionHandler(const HBasicBlock& handler) const {
2792 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002793 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002794 }
2795
2796 // If not present already, adds `handler` to its block's list of exception
2797 // handlers.
2798 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002799 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002800 GetBlock()->AddSuccessor(handler);
2801 }
2802 }
2803
Vladimir Markoa1de9182016-02-25 11:37:38 +00002804 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2805 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002806
David Brazdilffee3d32015-07-06 11:48:53 +01002807 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2808
David Brazdilfc6a86a2015-06-26 10:33:45 +00002809 DECLARE_INSTRUCTION(TryBoundary);
2810
2811 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002812 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2813 static constexpr size_t kFieldBoundaryKindSize =
2814 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2815 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2816 kFieldBoundaryKind + kFieldBoundaryKindSize;
2817 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2818 "Too many packed fields.");
2819 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002820
2821 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2822};
2823
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002824// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002825class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002826 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002827 // We set CanTriggerGC to prevent any intermediate address to be live
2828 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002829 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002830 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002831 SetRawInputAt(0, cond);
2832 }
2833
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002834 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002835 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002836 return true;
2837 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002838 bool NeedsEnvironment() const OVERRIDE { return true; }
2839 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002840
2841 DECLARE_INSTRUCTION(Deoptimize);
2842
2843 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002844 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2845};
2846
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002847// Represents the ArtMethod that was passed as a first argument to
2848// the method. It is used by instructions that depend on it, like
2849// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002850class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002851 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002852 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2853 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002854
2855 DECLARE_INSTRUCTION(CurrentMethod);
2856
2857 private:
2858 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2859};
2860
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002861// Fetches an ArtMethod from the virtual table or the interface method table
2862// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002863class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002864 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002865 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002866 kVTable,
2867 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002868 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002869 };
2870 HClassTableGet(HInstruction* cls,
2871 Primitive::Type type,
2872 TableKind kind,
2873 size_t index,
2874 uint32_t dex_pc)
2875 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002876 index_(index) {
2877 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002878 SetRawInputAt(0, cls);
2879 }
2880
2881 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002882 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002883 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002884 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002885 }
2886
Vladimir Markoa1de9182016-02-25 11:37:38 +00002887 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002888 size_t GetIndex() const { return index_; }
2889
2890 DECLARE_INSTRUCTION(ClassTableGet);
2891
2892 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002893 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2894 static constexpr size_t kFieldTableKindSize =
2895 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2896 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2897 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2898 "Too many packed fields.");
2899 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2900
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002901 // The index of the ArtMethod in the table.
2902 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002903
2904 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2905};
2906
Mark Mendellfe57faa2015-09-18 09:26:15 -04002907// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2908// have one successor for each entry in the switch table, and the final successor
2909// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002910class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002911 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002912 HPackedSwitch(int32_t start_value,
2913 uint32_t num_entries,
2914 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002915 uint32_t dex_pc = kNoDexPc)
2916 : HTemplateInstruction(SideEffects::None(), dex_pc),
2917 start_value_(start_value),
2918 num_entries_(num_entries) {
2919 SetRawInputAt(0, input);
2920 }
2921
2922 bool IsControlFlow() const OVERRIDE { return true; }
2923
2924 int32_t GetStartValue() const { return start_value_; }
2925
Vladimir Marko211c2112015-09-24 16:52:33 +01002926 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002927
2928 HBasicBlock* GetDefaultBlock() const {
2929 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002930 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002931 }
2932 DECLARE_INSTRUCTION(PackedSwitch);
2933
2934 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002935 const int32_t start_value_;
2936 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002937
2938 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2939};
2940
Roland Levillain88cb1752014-10-20 16:36:47 +01002941class HUnaryOperation : public HExpression<1> {
2942 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002943 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2944 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002945 SetRawInputAt(0, input);
2946 }
2947
2948 HInstruction* GetInput() const { return InputAt(0); }
2949 Primitive::Type GetResultType() const { return GetType(); }
2950
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002951 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002952 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002953 return true;
2954 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002955
Roland Levillain31dd3d62016-02-16 12:21:02 +00002956 // Try to statically evaluate `this` and return a HConstant
2957 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002958 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002959 HConstant* TryStaticEvaluation() const;
2960
2961 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002962 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2963 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002964 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2965 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002966
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002967 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002968
2969 private:
2970 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2971};
2972
Dave Allison20dfc792014-06-16 20:44:29 -07002973class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002974 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002975 HBinaryOperation(Primitive::Type result_type,
2976 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002977 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002978 SideEffects side_effects = SideEffects::None(),
2979 uint32_t dex_pc = kNoDexPc)
2980 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002981 SetRawInputAt(0, left);
2982 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002983 }
2984
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002985 HInstruction* GetLeft() const { return InputAt(0); }
2986 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002987 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002988
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002989 virtual bool IsCommutative() const { return false; }
2990
2991 // Put constant on the right.
2992 // Returns whether order is changed.
2993 bool OrderInputsWithConstantOnTheRight() {
2994 HInstruction* left = InputAt(0);
2995 HInstruction* right = InputAt(1);
2996 if (left->IsConstant() && !right->IsConstant()) {
2997 ReplaceInput(right, 0);
2998 ReplaceInput(left, 1);
2999 return true;
3000 }
3001 return false;
3002 }
3003
3004 // Order inputs by instruction id, but favor constant on the right side.
3005 // This helps GVN for commutative ops.
3006 void OrderInputs() {
3007 DCHECK(IsCommutative());
3008 HInstruction* left = InputAt(0);
3009 HInstruction* right = InputAt(1);
3010 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3011 return;
3012 }
3013 if (OrderInputsWithConstantOnTheRight()) {
3014 return;
3015 }
3016 // Order according to instruction id.
3017 if (left->GetId() > right->GetId()) {
3018 ReplaceInput(right, 0);
3019 ReplaceInput(left, 1);
3020 }
3021 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003022
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003023 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003024 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003025 return true;
3026 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003027
Roland Levillain31dd3d62016-02-16 12:21:02 +00003028 // Try to statically evaluate `this` and return a HConstant
3029 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003030 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003031 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003032
3033 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003034 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3035 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003036 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3037 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003038 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003039 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3040 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003041 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3042 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003043 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3044 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003045 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003046 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3047 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003048
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003049 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003050 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003051 HConstant* GetConstantRight() const;
3052
3053 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003054 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003055 HInstruction* GetLeastConstantLeft() const;
3056
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003057 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003058
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003059 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003060 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3061};
3062
Mark Mendellc4701932015-04-10 13:18:51 -04003063// The comparison bias applies for floating point operations and indicates how NaN
3064// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003065enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003066 kNoBias, // bias is not applicable (i.e. for long operation)
3067 kGtBias, // return 1 for NaN comparisons
3068 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003069 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003070};
3071
Roland Levillain31dd3d62016-02-16 12:21:02 +00003072std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3073
Dave Allison20dfc792014-06-16 20:44:29 -07003074class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003075 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003076 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003077 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3078 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3079 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003080
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003081 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003082 // `instruction`, and disregard moves in between.
3083 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003084
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003085 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003086
3087 virtual IfCondition GetCondition() const = 0;
3088
Mark Mendellc4701932015-04-10 13:18:51 -04003089 virtual IfCondition GetOppositeCondition() const = 0;
3090
Vladimir Markoa1de9182016-02-25 11:37:38 +00003091 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003092 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3093
Vladimir Markoa1de9182016-02-25 11:37:38 +00003094 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3095 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003096
Vladimir Marko372f10e2016-05-17 16:30:10 +01003097 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003098 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003099 }
3100
Roland Levillain4fa13f62015-07-06 18:11:54 +01003101 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003102 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003103 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003104 if (if_cond == kCondNE) {
3105 return true;
3106 } else if (if_cond == kCondEQ) {
3107 return false;
3108 }
3109 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003110 }
3111
3112 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003113 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003114 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003115 if (if_cond == kCondEQ) {
3116 return true;
3117 } else if (if_cond == kCondNE) {
3118 return false;
3119 }
3120 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003121 }
3122
Roland Levillain31dd3d62016-02-16 12:21:02 +00003123 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003124 // Needed if we merge a HCompare into a HCondition.
3125 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3126 static constexpr size_t kFieldComparisonBiasSize =
3127 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3128 static constexpr size_t kNumberOfConditionPackedBits =
3129 kFieldComparisonBias + kFieldComparisonBiasSize;
3130 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3131 using ComparisonBiasField =
3132 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3133
Roland Levillain31dd3d62016-02-16 12:21:02 +00003134 template <typename T>
3135 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3136
3137 template <typename T>
3138 int32_t CompareFP(T x, T y) const {
3139 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3140 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3141 // Handle the bias.
3142 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3143 }
3144
3145 // Return an integer constant containing the result of a condition evaluated at compile time.
3146 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3147 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3148 }
3149
Dave Allison20dfc792014-06-16 20:44:29 -07003150 private:
3151 DISALLOW_COPY_AND_ASSIGN(HCondition);
3152};
3153
3154// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003155class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003156 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003157 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3158 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003159
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003160 bool IsCommutative() const OVERRIDE { return true; }
3161
Vladimir Marko9e23df52015-11-10 17:14:35 +00003162 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3163 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003164 return MakeConstantCondition(true, GetDexPc());
3165 }
3166 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3167 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3168 }
3169 // In the following Evaluate methods, a HCompare instruction has
3170 // been merged into this HEqual instruction; evaluate it as
3171 // `Compare(x, y) == 0`.
3172 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3173 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3174 GetDexPc());
3175 }
3176 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3177 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3178 }
3179 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3180 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003181 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003182
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003183 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003184
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003185 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003186 return kCondEQ;
3187 }
3188
Mark Mendellc4701932015-04-10 13:18:51 -04003189 IfCondition GetOppositeCondition() const OVERRIDE {
3190 return kCondNE;
3191 }
3192
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003193 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003194 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003195
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003196 DISALLOW_COPY_AND_ASSIGN(HEqual);
3197};
3198
Vladimir Markofcb503c2016-05-18 12:48:17 +01003199class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003200 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003201 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3202 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003203
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003204 bool IsCommutative() const OVERRIDE { return true; }
3205
Vladimir Marko9e23df52015-11-10 17:14:35 +00003206 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3207 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003208 return MakeConstantCondition(false, GetDexPc());
3209 }
3210 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3211 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3212 }
3213 // In the following Evaluate methods, a HCompare instruction has
3214 // been merged into this HNotEqual instruction; evaluate it as
3215 // `Compare(x, y) != 0`.
3216 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3217 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3218 }
3219 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3220 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3221 }
3222 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3223 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003224 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003225
Dave Allison20dfc792014-06-16 20:44:29 -07003226 DECLARE_INSTRUCTION(NotEqual);
3227
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003228 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003229 return kCondNE;
3230 }
3231
Mark Mendellc4701932015-04-10 13:18:51 -04003232 IfCondition GetOppositeCondition() const OVERRIDE {
3233 return kCondEQ;
3234 }
3235
Dave Allison20dfc792014-06-16 20:44:29 -07003236 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003237 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003238
Dave Allison20dfc792014-06-16 20:44:29 -07003239 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3240};
3241
Vladimir Markofcb503c2016-05-18 12:48:17 +01003242class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003243 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003244 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3245 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003246
Roland Levillain9867bc72015-08-05 10:21:34 +01003247 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003248 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003249 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003250 // In the following Evaluate methods, a HCompare instruction has
3251 // been merged into this HLessThan instruction; evaluate it as
3252 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003253 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003254 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3255 }
3256 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3257 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3258 }
3259 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3260 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003261 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003262
Dave Allison20dfc792014-06-16 20:44:29 -07003263 DECLARE_INSTRUCTION(LessThan);
3264
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003265 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003266 return kCondLT;
3267 }
3268
Mark Mendellc4701932015-04-10 13:18:51 -04003269 IfCondition GetOppositeCondition() const OVERRIDE {
3270 return kCondGE;
3271 }
3272
Dave Allison20dfc792014-06-16 20:44:29 -07003273 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003274 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003275
Dave Allison20dfc792014-06-16 20:44:29 -07003276 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3277};
3278
Vladimir Markofcb503c2016-05-18 12:48:17 +01003279class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003280 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003281 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3282 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003283
Roland Levillain9867bc72015-08-05 10:21:34 +01003284 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003285 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003286 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003287 // In the following Evaluate methods, a HCompare instruction has
3288 // been merged into this HLessThanOrEqual instruction; evaluate it as
3289 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003290 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003291 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3292 }
3293 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3294 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3295 }
3296 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3297 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003298 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003299
Dave Allison20dfc792014-06-16 20:44:29 -07003300 DECLARE_INSTRUCTION(LessThanOrEqual);
3301
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003302 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003303 return kCondLE;
3304 }
3305
Mark Mendellc4701932015-04-10 13:18:51 -04003306 IfCondition GetOppositeCondition() const OVERRIDE {
3307 return kCondGT;
3308 }
3309
Dave Allison20dfc792014-06-16 20:44:29 -07003310 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003311 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003312
Dave Allison20dfc792014-06-16 20:44:29 -07003313 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3314};
3315
Vladimir Markofcb503c2016-05-18 12:48:17 +01003316class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003317 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003318 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3319 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003320
Roland Levillain9867bc72015-08-05 10:21:34 +01003321 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003322 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003323 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003324 // In the following Evaluate methods, a HCompare instruction has
3325 // been merged into this HGreaterThan instruction; evaluate it as
3326 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003327 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003328 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3329 }
3330 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3331 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3332 }
3333 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3334 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003335 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003336
Dave Allison20dfc792014-06-16 20:44:29 -07003337 DECLARE_INSTRUCTION(GreaterThan);
3338
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003339 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003340 return kCondGT;
3341 }
3342
Mark Mendellc4701932015-04-10 13:18:51 -04003343 IfCondition GetOppositeCondition() const OVERRIDE {
3344 return kCondLE;
3345 }
3346
Dave Allison20dfc792014-06-16 20:44:29 -07003347 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003348 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003349
Dave Allison20dfc792014-06-16 20:44:29 -07003350 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3351};
3352
Vladimir Markofcb503c2016-05-18 12:48:17 +01003353class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003354 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003355 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3356 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003357
Roland Levillain9867bc72015-08-05 10:21:34 +01003358 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003359 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003360 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003361 // In the following Evaluate methods, a HCompare instruction has
3362 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3363 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003364 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003365 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3366 }
3367 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3368 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3369 }
3370 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3371 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003372 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003373
Dave Allison20dfc792014-06-16 20:44:29 -07003374 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3375
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003376 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003377 return kCondGE;
3378 }
3379
Mark Mendellc4701932015-04-10 13:18:51 -04003380 IfCondition GetOppositeCondition() const OVERRIDE {
3381 return kCondLT;
3382 }
3383
Dave Allison20dfc792014-06-16 20:44:29 -07003384 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003385 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003386
Dave Allison20dfc792014-06-16 20:44:29 -07003387 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3388};
3389
Vladimir Markofcb503c2016-05-18 12:48:17 +01003390class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003391 public:
3392 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3393 : HCondition(first, second, dex_pc) {}
3394
3395 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003396 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003397 }
3398 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003399 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3400 }
3401 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3402 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3403 LOG(FATAL) << DebugName() << " is not defined for float values";
3404 UNREACHABLE();
3405 }
3406 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3407 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3408 LOG(FATAL) << DebugName() << " is not defined for double values";
3409 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003410 }
3411
3412 DECLARE_INSTRUCTION(Below);
3413
3414 IfCondition GetCondition() const OVERRIDE {
3415 return kCondB;
3416 }
3417
3418 IfCondition GetOppositeCondition() const OVERRIDE {
3419 return kCondAE;
3420 }
3421
3422 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003423 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003424 return MakeUnsigned(x) < MakeUnsigned(y);
3425 }
Aart Bike9f37602015-10-09 11:15:55 -07003426
3427 DISALLOW_COPY_AND_ASSIGN(HBelow);
3428};
3429
Vladimir Markofcb503c2016-05-18 12:48:17 +01003430class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003431 public:
3432 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3433 : HCondition(first, second, dex_pc) {}
3434
3435 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003436 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003437 }
3438 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003439 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3440 }
3441 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3442 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3443 LOG(FATAL) << DebugName() << " is not defined for float values";
3444 UNREACHABLE();
3445 }
3446 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3447 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3448 LOG(FATAL) << DebugName() << " is not defined for double values";
3449 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003450 }
3451
3452 DECLARE_INSTRUCTION(BelowOrEqual);
3453
3454 IfCondition GetCondition() const OVERRIDE {
3455 return kCondBE;
3456 }
3457
3458 IfCondition GetOppositeCondition() const OVERRIDE {
3459 return kCondA;
3460 }
3461
3462 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003463 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003464 return MakeUnsigned(x) <= MakeUnsigned(y);
3465 }
Aart Bike9f37602015-10-09 11:15:55 -07003466
3467 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3468};
3469
Vladimir Markofcb503c2016-05-18 12:48:17 +01003470class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003471 public:
3472 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3473 : HCondition(first, second, dex_pc) {}
3474
3475 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003476 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003477 }
3478 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003479 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3480 }
3481 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3482 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3483 LOG(FATAL) << DebugName() << " is not defined for float values";
3484 UNREACHABLE();
3485 }
3486 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3487 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3488 LOG(FATAL) << DebugName() << " is not defined for double values";
3489 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003490 }
3491
3492 DECLARE_INSTRUCTION(Above);
3493
3494 IfCondition GetCondition() const OVERRIDE {
3495 return kCondA;
3496 }
3497
3498 IfCondition GetOppositeCondition() const OVERRIDE {
3499 return kCondBE;
3500 }
3501
3502 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003503 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003504 return MakeUnsigned(x) > MakeUnsigned(y);
3505 }
Aart Bike9f37602015-10-09 11:15:55 -07003506
3507 DISALLOW_COPY_AND_ASSIGN(HAbove);
3508};
3509
Vladimir Markofcb503c2016-05-18 12:48:17 +01003510class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003511 public:
3512 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3513 : HCondition(first, second, dex_pc) {}
3514
3515 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003516 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003517 }
3518 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003519 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3520 }
3521 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3522 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3523 LOG(FATAL) << DebugName() << " is not defined for float values";
3524 UNREACHABLE();
3525 }
3526 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3527 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3528 LOG(FATAL) << DebugName() << " is not defined for double values";
3529 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003530 }
3531
3532 DECLARE_INSTRUCTION(AboveOrEqual);
3533
3534 IfCondition GetCondition() const OVERRIDE {
3535 return kCondAE;
3536 }
3537
3538 IfCondition GetOppositeCondition() const OVERRIDE {
3539 return kCondB;
3540 }
3541
3542 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003543 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003544 return MakeUnsigned(x) >= MakeUnsigned(y);
3545 }
Aart Bike9f37602015-10-09 11:15:55 -07003546
3547 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3548};
Dave Allison20dfc792014-06-16 20:44:29 -07003549
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003550// Instruction to check how two inputs compare to each other.
3551// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003552class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003553 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003554 // Note that `comparison_type` is the type of comparison performed
3555 // between the comparison's inputs, not the type of the instantiated
3556 // HCompare instruction (which is always Primitive::kPrimInt).
3557 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003558 HInstruction* first,
3559 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003560 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003561 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003562 : HBinaryOperation(Primitive::kPrimInt,
3563 first,
3564 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003565 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003566 dex_pc) {
3567 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003568 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3569 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003570 }
3571
Roland Levillain9867bc72015-08-05 10:21:34 +01003572 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003573 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3574
3575 template <typename T>
3576 int32_t ComputeFP(T x, T y) const {
3577 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3578 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3579 // Handle the bias.
3580 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3581 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003582
Roland Levillain9867bc72015-08-05 10:21:34 +01003583 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003584 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3585 // reach this code path when processing a freshly built HIR
3586 // graph. However HCompare integer instructions can be synthesized
3587 // by the instruction simplifier to implement IntegerCompare and
3588 // IntegerSignum intrinsics, so we have to handle this case.
3589 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003590 }
3591 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003592 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3593 }
3594 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3595 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3596 }
3597 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3598 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003599 }
3600
Vladimir Marko372f10e2016-05-17 16:30:10 +01003601 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003602 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003603 }
3604
Vladimir Markoa1de9182016-02-25 11:37:38 +00003605 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003606
Roland Levillain31dd3d62016-02-16 12:21:02 +00003607 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003608 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003609 bool IsGtBias() const {
3610 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003611 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003612 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003613
Roland Levillain1693a1f2016-03-15 14:57:31 +00003614 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3615 // Comparisons do not require a runtime call in any back end.
3616 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003617 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003618
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003619 DECLARE_INSTRUCTION(Compare);
3620
Roland Levillain31dd3d62016-02-16 12:21:02 +00003621 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003622 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3623 static constexpr size_t kFieldComparisonBiasSize =
3624 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3625 static constexpr size_t kNumberOfComparePackedBits =
3626 kFieldComparisonBias + kFieldComparisonBiasSize;
3627 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3628 using ComparisonBiasField =
3629 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3630
Roland Levillain31dd3d62016-02-16 12:21:02 +00003631 // Return an integer constant containing the result of a comparison evaluated at compile time.
3632 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3633 DCHECK(value == -1 || value == 0 || value == 1) << value;
3634 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3635 }
3636
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003637 private:
3638 DISALLOW_COPY_AND_ASSIGN(HCompare);
3639};
3640
Vladimir Markofcb503c2016-05-18 12:48:17 +01003641class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003642 public:
3643 HNewInstance(HInstruction* cls,
3644 HCurrentMethod* current_method,
3645 uint32_t dex_pc,
3646 uint16_t type_index,
3647 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003648 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003649 bool finalizable,
3650 QuickEntrypointEnum entrypoint)
3651 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3652 type_index_(type_index),
3653 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003654 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003655 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003656 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003657 SetRawInputAt(0, cls);
3658 SetRawInputAt(1, current_method);
3659 }
3660
3661 uint16_t GetTypeIndex() const { return type_index_; }
3662 const DexFile& GetDexFile() const { return dex_file_; }
3663
3664 // Calls runtime so needs an environment.
3665 bool NeedsEnvironment() const OVERRIDE { return true; }
3666
Mingyao Yang062157f2016-03-02 10:15:36 -08003667 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3668 bool CanThrow() const OVERRIDE { return true; }
3669
3670 // Needs to call into runtime to make sure it's instantiable/accessible.
3671 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003672
Vladimir Markoa1de9182016-02-25 11:37:38 +00003673 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003674
3675 bool CanBeNull() const OVERRIDE { return false; }
3676
3677 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3678
3679 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3680 entrypoint_ = entrypoint;
3681 }
3682
3683 bool IsStringAlloc() const;
3684
3685 DECLARE_INSTRUCTION(NewInstance);
3686
3687 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003688 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3689 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003690 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3691 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3692 "Too many packed fields.");
3693
David Brazdil6de19382016-01-08 17:37:10 +00003694 const uint16_t type_index_;
3695 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003696 QuickEntrypointEnum entrypoint_;
3697
3698 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3699};
3700
Agi Csaki05f20562015-08-19 14:58:14 -07003701enum IntrinsicNeedsEnvironmentOrCache {
3702 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3703 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003704};
3705
Aart Bik5d75afe2015-12-14 11:57:01 -08003706enum IntrinsicSideEffects {
3707 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3708 kReadSideEffects, // Intrinsic may read heap memory.
3709 kWriteSideEffects, // Intrinsic may write heap memory.
3710 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3711};
3712
3713enum IntrinsicExceptions {
3714 kNoThrow, // Intrinsic does not throw any exceptions.
3715 kCanThrow // Intrinsic may throw exceptions.
3716};
3717
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003718class HInvoke : public HInstruction {
3719 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003720 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003721
Vladimir Marko372f10e2016-05-17 16:30:10 +01003722 using HInstruction::GetInputRecords; // Keep the const version visible.
3723 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3724 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3725 }
3726
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003727 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003728 SetRawInputAt(index, argument);
3729 }
3730
Roland Levillain3e3d7332015-04-28 11:00:54 +01003731 // Return the number of arguments. This number can be lower than
3732 // the number of inputs returned by InputCount(), as some invoke
3733 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3734 // inputs at the end of their list of inputs.
3735 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3736
Vladimir Markoa1de9182016-02-25 11:37:38 +00003737 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003738
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003739 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003740 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003741
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003742 InvokeType GetInvokeType() const {
3743 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003744 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003745
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003746 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003747 return intrinsic_;
3748 }
3749
Aart Bik5d75afe2015-12-14 11:57:01 -08003750 void SetIntrinsic(Intrinsics intrinsic,
3751 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3752 IntrinsicSideEffects side_effects,
3753 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003754
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003755 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003756 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003757 }
3758
Vladimir Markoa1de9182016-02-25 11:37:38 +00003759 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003760
3761 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3762
Vladimir Marko372f10e2016-05-17 16:30:10 +01003763 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003764 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3765 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003766
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003767 uint32_t* GetIntrinsicOptimizations() {
3768 return &intrinsic_optimizations_;
3769 }
3770
3771 const uint32_t* GetIntrinsicOptimizations() const {
3772 return &intrinsic_optimizations_;
3773 }
3774
3775 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3776
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003777 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3778
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003779 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003780
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003781 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003782 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3783 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003784 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3785 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003786 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003787 static constexpr size_t kFieldReturnTypeSize =
3788 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3789 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3790 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3791 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003792 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003793 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3794
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003795 HInvoke(ArenaAllocator* arena,
3796 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003797 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003798 Primitive::Type return_type,
3799 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003800 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003801 ArtMethod* resolved_method,
3802 InvokeType invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003803 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003804 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003805 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003806 resolved_method_(resolved_method),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003807 inputs_(number_of_arguments + number_of_other_inputs,
3808 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003809 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003810 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003811 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003812 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003813 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003814 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003815 }
3816
Vladimir Markoa1de9182016-02-25 11:37:38 +00003817 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003818
Roland Levillain3e3d7332015-04-28 11:00:54 +01003819 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003820 ArtMethod* const resolved_method_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003821 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003822 const uint32_t dex_method_index_;
3823 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003824
3825 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3826 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003827
3828 private:
3829 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3830};
3831
Vladimir Markofcb503c2016-05-18 12:48:17 +01003832class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003833 public:
3834 HInvokeUnresolved(ArenaAllocator* arena,
3835 uint32_t number_of_arguments,
3836 Primitive::Type return_type,
3837 uint32_t dex_pc,
3838 uint32_t dex_method_index,
3839 InvokeType invoke_type)
3840 : HInvoke(arena,
3841 number_of_arguments,
3842 0u /* number_of_other_inputs */,
3843 return_type,
3844 dex_pc,
3845 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003846 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003847 invoke_type) {
3848 }
3849
3850 DECLARE_INSTRUCTION(InvokeUnresolved);
3851
3852 private:
3853 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3854};
3855
Vladimir Markofcb503c2016-05-18 12:48:17 +01003856class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003857 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003858 // Requirements of this method call regarding the class
3859 // initialization (clinit) check of its declaring class.
3860 enum class ClinitCheckRequirement {
3861 kNone, // Class already initialized.
3862 kExplicit, // Static call having explicit clinit check as last input.
3863 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003864 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003865 };
3866
Vladimir Marko58155012015-08-19 12:49:41 +00003867 // Determines how to load the target ArtMethod*.
3868 enum class MethodLoadKind {
3869 // Use a String init ArtMethod* loaded from Thread entrypoints.
3870 kStringInit,
3871
3872 // Use the method's own ArtMethod* loaded by the register allocator.
3873 kRecursive,
3874
3875 // Use ArtMethod* at a known address, embed the direct address in the code.
3876 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3877 kDirectAddress,
3878
3879 // Use ArtMethod* at an address that will be known at link time, embed the direct
3880 // address in the code. If the image is relocatable, emit .patch_oat entry.
3881 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3882 // the image relocatable or not.
3883 kDirectAddressWithFixup,
3884
Vladimir Markoa1de9182016-02-25 11:37:38 +00003885 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003886 // Used when we need to use the dex cache, for example for invoke-static that
3887 // may cause class initialization (the entry may point to a resolution method),
3888 // and we know that we can access the dex cache arrays using a PC-relative load.
3889 kDexCachePcRelative,
3890
3891 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3892 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3893 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3894 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3895 kDexCacheViaMethod,
3896 };
3897
3898 // Determines the location of the code pointer.
3899 enum class CodePtrLocation {
3900 // Recursive call, use local PC-relative call instruction.
3901 kCallSelf,
3902
3903 // Use PC-relative call instruction patched at link time.
3904 // Used for calls within an oat file, boot->boot or app->app.
3905 kCallPCRelative,
3906
3907 // Call to a known target address, embed the direct address in code.
3908 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3909 kCallDirect,
3910
3911 // Call to a target address that will be known at link time, embed the direct
3912 // address in code. If the image is relocatable, emit .patch_oat entry.
3913 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3914 // the image relocatable or not.
3915 kCallDirectWithFixup,
3916
3917 // Use code pointer from the ArtMethod*.
3918 // Used when we don't know the target code. This is also the last-resort-kind used when
3919 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3920 kCallArtMethod,
3921 };
3922
3923 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003924 MethodLoadKind method_load_kind;
3925 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003926 // The method load data holds
3927 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3928 // Note that there are multiple string init methods, each having its own offset.
3929 // - the method address for kDirectAddress
3930 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003931 uint64_t method_load_data;
3932 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003933 };
3934
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003935 HInvokeStaticOrDirect(ArenaAllocator* arena,
3936 uint32_t number_of_arguments,
3937 Primitive::Type return_type,
3938 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003939 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003940 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00003941 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003942 InvokeType invoke_type,
3943 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003944 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003945 : HInvoke(arena,
3946 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003947 // There is potentially one extra argument for the HCurrentMethod node, and
3948 // potentially one other if the clinit check is explicit, and potentially
3949 // one other if the method is a string factory.
3950 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003951 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003952 return_type,
3953 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003954 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003955 resolved_method,
3956 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003957 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003958 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003959 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3960 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003961
Vladimir Markodc151b22015-10-15 18:02:30 +01003962 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003963 bool had_current_method_input = HasCurrentMethodInput();
3964 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3965
3966 // Using the current method is the default and once we find a better
3967 // method load kind, we should not go back to using the current method.
3968 DCHECK(had_current_method_input || !needs_current_method_input);
3969
3970 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003971 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3972 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003973 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003974 dispatch_info_ = dispatch_info;
3975 }
3976
Vladimir Markoc53c0792015-11-19 15:48:33 +00003977 void AddSpecialInput(HInstruction* input) {
3978 // We allow only one special input.
3979 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3980 DCHECK(InputCount() == GetSpecialInputIndex() ||
3981 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3982 InsertInputAt(GetSpecialInputIndex(), input);
3983 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003984
Vladimir Marko372f10e2016-05-17 16:30:10 +01003985 using HInstruction::GetInputRecords; // Keep the const version visible.
3986 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3987 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
3988 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
3989 DCHECK(!input_records.empty());
3990 DCHECK_GT(input_records.size(), GetNumberOfArguments());
3991 HInstruction* last_input = input_records.back().GetInstruction();
3992 // Note: `last_input` may be null during arguments setup.
3993 if (last_input != nullptr) {
3994 // `last_input` is the last input of a static invoke marked as having
3995 // an explicit clinit check. It must either be:
3996 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3997 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3998 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
3999 }
4000 }
4001 return input_records;
4002 }
4003
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004004 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004005 // We access the method via the dex cache so we can't do an implicit null check.
4006 // TODO: for intrinsics we can generate implicit null checks.
4007 return false;
4008 }
4009
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004010 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004011 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004012 }
4013
Vladimir Markoc53c0792015-11-19 15:48:33 +00004014 // Get the index of the special input, if any.
4015 //
David Brazdil6de19382016-01-08 17:37:10 +00004016 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4017 // method pointer; otherwise there may be one platform-specific special input,
4018 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004019 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004020 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004021
Vladimir Marko58155012015-08-19 12:49:41 +00004022 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4023 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4024 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004025 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004026 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004027 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004028 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004029 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4030 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004031 bool HasCurrentMethodInput() const {
4032 // This function can be called only after the invoke has been fully initialized by the builder.
4033 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004034 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004035 return true;
4036 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004037 DCHECK(InputCount() == GetSpecialInputIndex() ||
4038 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004039 return false;
4040 }
4041 }
Vladimir Marko58155012015-08-19 12:49:41 +00004042 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
Vladimir Marko58155012015-08-19 12:49:41 +00004043
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004044 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004045 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004046 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004047 }
4048
4049 uint64_t GetMethodAddress() const {
4050 DCHECK(HasMethodAddress());
4051 return dispatch_info_.method_load_data;
4052 }
4053
4054 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004055 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004056 return dispatch_info_.method_load_data;
4057 }
4058
4059 uint64_t GetDirectCodePtr() const {
4060 DCHECK(HasDirectCodePtr());
4061 return dispatch_info_.direct_code_ptr;
4062 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004063
Vladimir Markoa1de9182016-02-25 11:37:38 +00004064 ClinitCheckRequirement GetClinitCheckRequirement() const {
4065 return GetPackedField<ClinitCheckRequirementField>();
4066 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004067
Roland Levillain4c0eb422015-04-24 16:43:49 +01004068 // Is this instruction a call to a static method?
4069 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004070 return GetInvokeType() == kStatic;
4071 }
4072
4073 MethodReference GetTargetMethod() const {
4074 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004075 }
4076
Vladimir Markofbb184a2015-11-13 14:47:00 +00004077 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4078 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4079 // instruction; only relevant for static calls with explicit clinit check.
4080 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004081 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004082 size_t last_input_index = inputs_.size() - 1u;
4083 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004084 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004085 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004086 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004087 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004088 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004089 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004090 }
4091
4092 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004093 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004094 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004095 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004096 }
4097
4098 // Is this a call to a static method whose declaring class has an
4099 // implicit intialization check requirement?
4100 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004101 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004102 }
4103
Vladimir Markob554b5a2015-11-06 12:57:55 +00004104 // Does this method load kind need the current method as an input?
4105 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4106 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4107 }
4108
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004109 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004110
Roland Levillain4c0eb422015-04-24 16:43:49 +01004111 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004112 void InsertInputAt(size_t index, HInstruction* input);
4113 void RemoveInputAt(size_t index);
4114
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004115 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004116 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004117 static constexpr size_t kFieldClinitCheckRequirementSize =
4118 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4119 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4120 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4121 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4122 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004123 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4124 kFieldClinitCheckRequirement,
4125 kFieldClinitCheckRequirementSize>;
4126
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004127 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004128 MethodReference target_method_;
4129 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004130
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004131 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004132};
Vladimir Markof64242a2015-12-01 14:58:23 +00004133std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004134std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004135
Vladimir Markofcb503c2016-05-18 12:48:17 +01004136class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004137 public:
4138 HInvokeVirtual(ArenaAllocator* arena,
4139 uint32_t number_of_arguments,
4140 Primitive::Type return_type,
4141 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004142 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004143 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004144 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004145 : HInvoke(arena,
4146 number_of_arguments,
4147 0u,
4148 return_type,
4149 dex_pc,
4150 dex_method_index,
4151 resolved_method,
4152 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004153 vtable_index_(vtable_index) {}
4154
Calin Juravle641547a2015-04-21 22:08:51 +01004155 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004156 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004157 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004158 }
4159
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004160 uint32_t GetVTableIndex() const { return vtable_index_; }
4161
4162 DECLARE_INSTRUCTION(InvokeVirtual);
4163
4164 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004165 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004166 const uint32_t vtable_index_;
4167
4168 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4169};
4170
Vladimir Markofcb503c2016-05-18 12:48:17 +01004171class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004172 public:
4173 HInvokeInterface(ArenaAllocator* arena,
4174 uint32_t number_of_arguments,
4175 Primitive::Type return_type,
4176 uint32_t dex_pc,
4177 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004178 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004179 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004180 : HInvoke(arena,
4181 number_of_arguments,
4182 0u,
4183 return_type,
4184 dex_pc,
4185 dex_method_index,
4186 resolved_method,
4187 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004188 imt_index_(imt_index) {}
4189
Calin Juravle641547a2015-04-21 22:08:51 +01004190 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004191 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004192 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004193 }
4194
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004195 uint32_t GetImtIndex() const { return imt_index_; }
4196 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4197
4198 DECLARE_INSTRUCTION(InvokeInterface);
4199
4200 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004201 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004202 const uint32_t imt_index_;
4203
4204 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4205};
4206
Vladimir Markofcb503c2016-05-18 12:48:17 +01004207class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004208 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004209 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004210 : HUnaryOperation(result_type, input, dex_pc) {
4211 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4212 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004213
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004214 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004215
4216 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004217 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004218 }
4219 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004220 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004221 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004222 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4223 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4224 }
4225 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4226 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4227 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004228
Roland Levillain88cb1752014-10-20 16:36:47 +01004229 DECLARE_INSTRUCTION(Neg);
4230
4231 private:
4232 DISALLOW_COPY_AND_ASSIGN(HNeg);
4233};
4234
Vladimir Markofcb503c2016-05-18 12:48:17 +01004235class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004236 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004237 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004238 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004239 uint32_t dex_pc,
4240 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004241 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004242 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004243 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004244 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004245 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004246 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004247 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004248 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004249 }
4250
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004251 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004252 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004253
4254 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004255 bool NeedsEnvironment() const OVERRIDE { return true; }
4256
Mingyao Yang0c365e62015-03-31 15:09:29 -07004257 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4258 bool CanThrow() const OVERRIDE { return true; }
4259
Calin Juravle10e244f2015-01-26 18:54:32 +00004260 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004261
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004262 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4263
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004264 DECLARE_INSTRUCTION(NewArray);
4265
4266 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004267 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004268 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004269 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004270
4271 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4272};
4273
Vladimir Markofcb503c2016-05-18 12:48:17 +01004274class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004275 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004276 HAdd(Primitive::Type result_type,
4277 HInstruction* left,
4278 HInstruction* right,
4279 uint32_t dex_pc = kNoDexPc)
4280 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004281
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004282 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004283
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004284 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004285
4286 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004287 return GetBlock()->GetGraph()->GetIntConstant(
4288 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004289 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004290 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004291 return GetBlock()->GetGraph()->GetLongConstant(
4292 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004293 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004294 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4295 return GetBlock()->GetGraph()->GetFloatConstant(
4296 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4297 }
4298 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4299 return GetBlock()->GetGraph()->GetDoubleConstant(
4300 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4301 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004302
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004303 DECLARE_INSTRUCTION(Add);
4304
4305 private:
4306 DISALLOW_COPY_AND_ASSIGN(HAdd);
4307};
4308
Vladimir Markofcb503c2016-05-18 12:48:17 +01004309class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004310 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004311 HSub(Primitive::Type result_type,
4312 HInstruction* left,
4313 HInstruction* right,
4314 uint32_t dex_pc = kNoDexPc)
4315 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004316
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004317 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004318
4319 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004320 return GetBlock()->GetGraph()->GetIntConstant(
4321 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004322 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004323 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004324 return GetBlock()->GetGraph()->GetLongConstant(
4325 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004326 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004327 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4328 return GetBlock()->GetGraph()->GetFloatConstant(
4329 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4330 }
4331 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4332 return GetBlock()->GetGraph()->GetDoubleConstant(
4333 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4334 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004335
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004336 DECLARE_INSTRUCTION(Sub);
4337
4338 private:
4339 DISALLOW_COPY_AND_ASSIGN(HSub);
4340};
4341
Vladimir Markofcb503c2016-05-18 12:48:17 +01004342class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004343 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004344 HMul(Primitive::Type result_type,
4345 HInstruction* left,
4346 HInstruction* right,
4347 uint32_t dex_pc = kNoDexPc)
4348 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004349
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004350 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004351
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004352 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004353
4354 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004355 return GetBlock()->GetGraph()->GetIntConstant(
4356 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004357 }
4358 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004359 return GetBlock()->GetGraph()->GetLongConstant(
4360 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004361 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004362 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4363 return GetBlock()->GetGraph()->GetFloatConstant(
4364 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4365 }
4366 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4367 return GetBlock()->GetGraph()->GetDoubleConstant(
4368 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4369 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004370
4371 DECLARE_INSTRUCTION(Mul);
4372
4373 private:
4374 DISALLOW_COPY_AND_ASSIGN(HMul);
4375};
4376
Vladimir Markofcb503c2016-05-18 12:48:17 +01004377class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004378 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004379 HDiv(Primitive::Type result_type,
4380 HInstruction* left,
4381 HInstruction* right,
4382 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004383 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004384
Roland Levillain9867bc72015-08-05 10:21:34 +01004385 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004386 T ComputeIntegral(T x, T y) const {
4387 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004388 // Our graph structure ensures we never have 0 for `y` during
4389 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004390 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004391 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004392 return (y == -1) ? -x : x / y;
4393 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004394
Roland Levillain31dd3d62016-02-16 12:21:02 +00004395 template <typename T>
4396 T ComputeFP(T x, T y) const {
4397 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4398 return x / y;
4399 }
4400
Roland Levillain9867bc72015-08-05 10:21:34 +01004401 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004402 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004403 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004404 }
4405 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004406 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004407 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4408 }
4409 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4410 return GetBlock()->GetGraph()->GetFloatConstant(
4411 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4412 }
4413 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4414 return GetBlock()->GetGraph()->GetDoubleConstant(
4415 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004416 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004417
4418 DECLARE_INSTRUCTION(Div);
4419
4420 private:
4421 DISALLOW_COPY_AND_ASSIGN(HDiv);
4422};
4423
Vladimir Markofcb503c2016-05-18 12:48:17 +01004424class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004425 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004426 HRem(Primitive::Type result_type,
4427 HInstruction* left,
4428 HInstruction* right,
4429 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004430 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004431
Roland Levillain9867bc72015-08-05 10:21:34 +01004432 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004433 T ComputeIntegral(T x, T y) const {
4434 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004435 // Our graph structure ensures we never have 0 for `y` during
4436 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004437 DCHECK_NE(y, 0);
4438 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4439 return (y == -1) ? 0 : x % y;
4440 }
4441
Roland Levillain31dd3d62016-02-16 12:21:02 +00004442 template <typename T>
4443 T ComputeFP(T x, T y) const {
4444 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4445 return std::fmod(x, y);
4446 }
4447
Roland Levillain9867bc72015-08-05 10:21:34 +01004448 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004449 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004450 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004451 }
4452 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004453 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004454 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004455 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004456 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4457 return GetBlock()->GetGraph()->GetFloatConstant(
4458 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4459 }
4460 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4461 return GetBlock()->GetGraph()->GetDoubleConstant(
4462 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4463 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004464
4465 DECLARE_INSTRUCTION(Rem);
4466
4467 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004468 DISALLOW_COPY_AND_ASSIGN(HRem);
4469};
4470
Vladimir Markofcb503c2016-05-18 12:48:17 +01004471class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004472 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004473 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4474 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004475 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004476 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004477 SetRawInputAt(0, value);
4478 }
4479
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004480 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4481
Calin Juravled0d48522014-11-04 16:40:20 +00004482 bool CanBeMoved() const OVERRIDE { return true; }
4483
Vladimir Marko372f10e2016-05-17 16:30:10 +01004484 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004485 return true;
4486 }
4487
4488 bool NeedsEnvironment() const OVERRIDE { return true; }
4489 bool CanThrow() const OVERRIDE { return true; }
4490
Calin Juravled0d48522014-11-04 16:40:20 +00004491 DECLARE_INSTRUCTION(DivZeroCheck);
4492
4493 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004494 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4495};
4496
Vladimir Markofcb503c2016-05-18 12:48:17 +01004497class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004498 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004499 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004500 HInstruction* value,
4501 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004502 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004503 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4504 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4505 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004506 }
4507
Roland Levillain5b5b9312016-03-22 14:57:31 +00004508 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004509 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004510 return value << (distance & max_shift_distance);
4511 }
4512
4513 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004514 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004515 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004516 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004517 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004518 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004519 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004520 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004521 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4522 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4523 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4524 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004525 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004526 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4527 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004528 LOG(FATAL) << DebugName() << " is not defined for float values";
4529 UNREACHABLE();
4530 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004531 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4532 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004533 LOG(FATAL) << DebugName() << " is not defined for double values";
4534 UNREACHABLE();
4535 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004536
4537 DECLARE_INSTRUCTION(Shl);
4538
4539 private:
4540 DISALLOW_COPY_AND_ASSIGN(HShl);
4541};
4542
Vladimir Markofcb503c2016-05-18 12:48:17 +01004543class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004544 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004545 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004546 HInstruction* value,
4547 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004548 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004549 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4550 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4551 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004552 }
4553
Roland Levillain5b5b9312016-03-22 14:57:31 +00004554 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004555 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004556 return value >> (distance & max_shift_distance);
4557 }
4558
4559 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004560 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004561 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004562 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004563 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004564 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004565 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004566 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004567 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4568 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4569 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4570 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004571 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004572 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4573 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004574 LOG(FATAL) << DebugName() << " is not defined for float values";
4575 UNREACHABLE();
4576 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004577 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4578 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004579 LOG(FATAL) << DebugName() << " is not defined for double values";
4580 UNREACHABLE();
4581 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004582
4583 DECLARE_INSTRUCTION(Shr);
4584
4585 private:
4586 DISALLOW_COPY_AND_ASSIGN(HShr);
4587};
4588
Vladimir Markofcb503c2016-05-18 12:48:17 +01004589class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004590 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004591 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004592 HInstruction* value,
4593 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004594 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004595 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4596 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4597 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004598 }
4599
Roland Levillain5b5b9312016-03-22 14:57:31 +00004600 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004601 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004602 typedef typename std::make_unsigned<T>::type V;
4603 V ux = static_cast<V>(value);
4604 return static_cast<T>(ux >> (distance & max_shift_distance));
4605 }
4606
4607 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004608 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004609 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004610 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004611 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004612 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004613 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004614 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004615 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4616 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4617 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4618 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004619 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004620 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4621 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004622 LOG(FATAL) << DebugName() << " is not defined for float values";
4623 UNREACHABLE();
4624 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004625 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4626 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004627 LOG(FATAL) << DebugName() << " is not defined for double values";
4628 UNREACHABLE();
4629 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004630
4631 DECLARE_INSTRUCTION(UShr);
4632
4633 private:
4634 DISALLOW_COPY_AND_ASSIGN(HUShr);
4635};
4636
Vladimir Markofcb503c2016-05-18 12:48:17 +01004637class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004638 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004639 HAnd(Primitive::Type result_type,
4640 HInstruction* left,
4641 HInstruction* right,
4642 uint32_t dex_pc = kNoDexPc)
4643 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004644
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004645 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004646
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004647 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004648
4649 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004650 return GetBlock()->GetGraph()->GetIntConstant(
4651 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004652 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004653 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004654 return GetBlock()->GetGraph()->GetLongConstant(
4655 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004656 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004657 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4658 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4659 LOG(FATAL) << DebugName() << " is not defined for float values";
4660 UNREACHABLE();
4661 }
4662 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4663 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4664 LOG(FATAL) << DebugName() << " is not defined for double values";
4665 UNREACHABLE();
4666 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004667
4668 DECLARE_INSTRUCTION(And);
4669
4670 private:
4671 DISALLOW_COPY_AND_ASSIGN(HAnd);
4672};
4673
Vladimir Markofcb503c2016-05-18 12:48:17 +01004674class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004675 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004676 HOr(Primitive::Type result_type,
4677 HInstruction* left,
4678 HInstruction* right,
4679 uint32_t dex_pc = kNoDexPc)
4680 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004681
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004682 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004683
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004684 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004685
4686 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004687 return GetBlock()->GetGraph()->GetIntConstant(
4688 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004689 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004690 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004691 return GetBlock()->GetGraph()->GetLongConstant(
4692 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004693 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004694 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4695 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4696 LOG(FATAL) << DebugName() << " is not defined for float values";
4697 UNREACHABLE();
4698 }
4699 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4700 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4701 LOG(FATAL) << DebugName() << " is not defined for double values";
4702 UNREACHABLE();
4703 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004704
4705 DECLARE_INSTRUCTION(Or);
4706
4707 private:
4708 DISALLOW_COPY_AND_ASSIGN(HOr);
4709};
4710
Vladimir Markofcb503c2016-05-18 12:48:17 +01004711class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004712 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004713 HXor(Primitive::Type result_type,
4714 HInstruction* left,
4715 HInstruction* right,
4716 uint32_t dex_pc = kNoDexPc)
4717 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004718
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004719 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004720
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004721 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004722
4723 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004724 return GetBlock()->GetGraph()->GetIntConstant(
4725 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004726 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004727 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004728 return GetBlock()->GetGraph()->GetLongConstant(
4729 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004730 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004731 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4732 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4733 LOG(FATAL) << DebugName() << " is not defined for float values";
4734 UNREACHABLE();
4735 }
4736 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4737 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4738 LOG(FATAL) << DebugName() << " is not defined for double values";
4739 UNREACHABLE();
4740 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004741
4742 DECLARE_INSTRUCTION(Xor);
4743
4744 private:
4745 DISALLOW_COPY_AND_ASSIGN(HXor);
4746};
4747
Vladimir Markofcb503c2016-05-18 12:48:17 +01004748class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004749 public:
4750 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004751 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004752 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4753 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004754 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004755
Roland Levillain5b5b9312016-03-22 14:57:31 +00004756 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004757 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004758 typedef typename std::make_unsigned<T>::type V;
4759 V ux = static_cast<V>(value);
4760 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004761 return static_cast<T>(ux);
4762 } else {
4763 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004764 return static_cast<T>(ux >> (distance & max_shift_value)) |
4765 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004766 }
4767 }
4768
Roland Levillain5b5b9312016-03-22 14:57:31 +00004769 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004770 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004771 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004772 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004773 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004774 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004775 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004776 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004777 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4778 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4779 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4780 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004781 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004782 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4783 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004784 LOG(FATAL) << DebugName() << " is not defined for float values";
4785 UNREACHABLE();
4786 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004787 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4788 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004789 LOG(FATAL) << DebugName() << " is not defined for double values";
4790 UNREACHABLE();
4791 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004792
4793 DECLARE_INSTRUCTION(Ror);
4794
4795 private:
4796 DISALLOW_COPY_AND_ASSIGN(HRor);
4797};
4798
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004799// The value of a parameter in this method. Its location depends on
4800// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004801class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004802 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004803 HParameterValue(const DexFile& dex_file,
4804 uint16_t type_index,
4805 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004806 Primitive::Type parameter_type,
4807 bool is_this = false)
4808 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004809 dex_file_(dex_file),
4810 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004811 index_(index) {
4812 SetPackedFlag<kFlagIsThis>(is_this);
4813 SetPackedFlag<kFlagCanBeNull>(!is_this);
4814 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004815
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004816 const DexFile& GetDexFile() const { return dex_file_; }
4817 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004818 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004819 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004820
Vladimir Markoa1de9182016-02-25 11:37:38 +00004821 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4822 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004823
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004824 DECLARE_INSTRUCTION(ParameterValue);
4825
4826 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004827 // Whether or not the parameter value corresponds to 'this' argument.
4828 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4829 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4830 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4831 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4832 "Too many packed fields.");
4833
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004834 const DexFile& dex_file_;
4835 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004836 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004837 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004838 const uint8_t index_;
4839
4840 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4841};
4842
Vladimir Markofcb503c2016-05-18 12:48:17 +01004843class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004844 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004845 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4846 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004847
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004848 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004849 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004850 return true;
4851 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004852
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004853 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004854
4855 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004856 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004857 }
4858 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004859 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004860 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004861 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4862 LOG(FATAL) << DebugName() << " is not defined for float values";
4863 UNREACHABLE();
4864 }
4865 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4866 LOG(FATAL) << DebugName() << " is not defined for double values";
4867 UNREACHABLE();
4868 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004869
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004870 DECLARE_INSTRUCTION(Not);
4871
4872 private:
4873 DISALLOW_COPY_AND_ASSIGN(HNot);
4874};
4875
Vladimir Markofcb503c2016-05-18 12:48:17 +01004876class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004877 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004878 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4879 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004880
4881 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004882 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004883 return true;
4884 }
4885
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004886 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004887 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004888 return !x;
4889 }
4890
Roland Levillain9867bc72015-08-05 10:21:34 +01004891 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004892 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004893 }
4894 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4895 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004896 UNREACHABLE();
4897 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004898 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4899 LOG(FATAL) << DebugName() << " is not defined for float values";
4900 UNREACHABLE();
4901 }
4902 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4903 LOG(FATAL) << DebugName() << " is not defined for double values";
4904 UNREACHABLE();
4905 }
David Brazdil66d126e2015-04-03 16:02:44 +01004906
4907 DECLARE_INSTRUCTION(BooleanNot);
4908
4909 private:
4910 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4911};
4912
Vladimir Markofcb503c2016-05-18 12:48:17 +01004913class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004914 public:
4915 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004916 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004917 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004918 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004919 // Invariant: We should never generate a conversion to a Boolean value.
4920 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004921 }
4922
4923 HInstruction* GetInput() const { return InputAt(0); }
4924 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4925 Primitive::Type GetResultType() const { return GetType(); }
4926
4927 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004928 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4929 return true;
4930 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004931
Mark Mendelle82549b2015-05-06 10:55:34 -04004932 // Try to statically evaluate the conversion and return a HConstant
4933 // containing the result. If the input cannot be converted, return nullptr.
4934 HConstant* TryStaticEvaluation() const;
4935
Roland Levillaindff1f282014-11-05 14:15:05 +00004936 DECLARE_INSTRUCTION(TypeConversion);
4937
4938 private:
4939 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4940};
4941
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004942static constexpr uint32_t kNoRegNumber = -1;
4943
Vladimir Markofcb503c2016-05-18 12:48:17 +01004944class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004945 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004946 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4947 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004948 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004949 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004950 SetRawInputAt(0, value);
4951 }
4952
Calin Juravle10e244f2015-01-26 18:54:32 +00004953 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004954 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004955 return true;
4956 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004957
Calin Juravle10e244f2015-01-26 18:54:32 +00004958 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004959
Calin Juravle10e244f2015-01-26 18:54:32 +00004960 bool CanThrow() const OVERRIDE { return true; }
4961
4962 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004963
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004964
4965 DECLARE_INSTRUCTION(NullCheck);
4966
4967 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004968 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4969};
4970
4971class FieldInfo : public ValueObject {
4972 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004973 FieldInfo(MemberOffset field_offset,
4974 Primitive::Type field_type,
4975 bool is_volatile,
4976 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004977 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004978 const DexFile& dex_file,
4979 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004980 : field_offset_(field_offset),
4981 field_type_(field_type),
4982 is_volatile_(is_volatile),
4983 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004984 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004985 dex_file_(dex_file),
4986 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004987
4988 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004989 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004990 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004991 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004992 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004993 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004994 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004995
4996 private:
4997 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004998 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004999 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005000 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005001 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005002 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005003 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005004};
5005
Vladimir Markofcb503c2016-05-18 12:48:17 +01005006class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005007 public:
5008 HInstanceFieldGet(HInstruction* value,
5009 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005010 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005011 bool is_volatile,
5012 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005013 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005014 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005015 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005016 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005017 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005018 field_info_(field_offset,
5019 field_type,
5020 is_volatile,
5021 field_idx,
5022 declaring_class_def_index,
5023 dex_file,
5024 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005025 SetRawInputAt(0, value);
5026 }
5027
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005028 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005029
Vladimir Marko372f10e2016-05-17 16:30:10 +01005030 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5031 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005032 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005033 }
5034
Calin Juravle641547a2015-04-21 22:08:51 +01005035 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005036 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005037 }
5038
5039 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005040 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5041 }
5042
Calin Juravle52c48962014-12-16 17:02:57 +00005043 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005044 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005045 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005046 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005047
5048 DECLARE_INSTRUCTION(InstanceFieldGet);
5049
5050 private:
5051 const FieldInfo field_info_;
5052
5053 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5054};
5055
Vladimir Markofcb503c2016-05-18 12:48:17 +01005056class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005057 public:
5058 HInstanceFieldSet(HInstruction* object,
5059 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005060 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005061 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005062 bool is_volatile,
5063 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005064 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005065 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005066 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005067 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005068 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005069 field_info_(field_offset,
5070 field_type,
5071 is_volatile,
5072 field_idx,
5073 declaring_class_def_index,
5074 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005075 dex_cache) {
5076 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005077 SetRawInputAt(0, object);
5078 SetRawInputAt(1, value);
5079 }
5080
Calin Juravle641547a2015-04-21 22:08:51 +01005081 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005082 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005083 }
5084
Calin Juravle52c48962014-12-16 17:02:57 +00005085 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005086 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005087 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005088 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005089 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005090 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5091 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005092
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005093 DECLARE_INSTRUCTION(InstanceFieldSet);
5094
5095 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005096 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5097 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5098 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5099 "Too many packed fields.");
5100
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005101 const FieldInfo field_info_;
5102
5103 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5104};
5105
Vladimir Markofcb503c2016-05-18 12:48:17 +01005106class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005107 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005108 HArrayGet(HInstruction* array,
5109 HInstruction* index,
5110 Primitive::Type type,
5111 uint32_t dex_pc,
5112 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005113 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005114 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005115 SetRawInputAt(0, array);
5116 SetRawInputAt(1, index);
5117 }
5118
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005119 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005120 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005121 return true;
5122 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005123 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005124 // TODO: We can be smarter here.
5125 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5126 // which generates the implicit null check. There are cases when these can be removed
5127 // to produce better code. If we ever add optimizations to do so we should allow an
5128 // implicit check here (as long as the address falls in the first page).
5129 return false;
5130 }
5131
David Brazdil4833f5a2015-12-16 10:37:39 +00005132 bool IsEquivalentOf(HArrayGet* other) const {
5133 bool result = (GetDexPc() == other->GetDexPc());
5134 if (kIsDebugBuild && result) {
5135 DCHECK_EQ(GetBlock(), other->GetBlock());
5136 DCHECK_EQ(GetArray(), other->GetArray());
5137 DCHECK_EQ(GetIndex(), other->GetIndex());
5138 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005139 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005140 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005141 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5142 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005143 }
5144 }
5145 return result;
5146 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005147
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005148 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5149
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005150 HInstruction* GetArray() const { return InputAt(0); }
5151 HInstruction* GetIndex() const { return InputAt(1); }
5152
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005153 DECLARE_INSTRUCTION(ArrayGet);
5154
5155 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005156 // We treat a String as an array, creating the HArrayGet from String.charAt()
5157 // intrinsic in the instruction simplifier. We can always determine whether
5158 // a particular HArrayGet is actually a String.charAt() by looking at the type
5159 // of the input but that requires holding the mutator lock, so we prefer to use
5160 // a flag, so that code generators don't need to do the locking.
5161 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5162 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5163 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5164 "Too many packed fields.");
5165
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005166 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5167};
5168
Vladimir Markofcb503c2016-05-18 12:48:17 +01005169class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005170 public:
5171 HArraySet(HInstruction* array,
5172 HInstruction* index,
5173 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005174 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005175 uint32_t dex_pc)
5176 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005177 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5178 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5179 SetPackedFlag<kFlagValueCanBeNull>(true);
5180 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005181 SetRawInputAt(0, array);
5182 SetRawInputAt(1, index);
5183 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005184 // Make a best guess now, may be refined during SSA building.
5185 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005186 }
5187
Calin Juravle77520bc2015-01-12 18:45:46 +00005188 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005189 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005190 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005191 }
5192
Mingyao Yang81014cb2015-06-02 03:16:27 -07005193 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005194 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005195
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005196 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005197 // TODO: Same as for ArrayGet.
5198 return false;
5199 }
5200
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005201 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005202 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005203 }
5204
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005205 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005206 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005207 }
5208
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005209 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005210 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005211 }
5212
Vladimir Markoa1de9182016-02-25 11:37:38 +00005213 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5214 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5215 bool StaticTypeOfArrayIsObjectArray() const {
5216 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5217 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005218
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005219 HInstruction* GetArray() const { return InputAt(0); }
5220 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005221 HInstruction* GetValue() const { return InputAt(2); }
5222
5223 Primitive::Type GetComponentType() const {
5224 // The Dex format does not type floating point index operations. Since the
5225 // `expected_component_type_` is set during building and can therefore not
5226 // be correct, we also check what is the value type. If it is a floating
5227 // point type, we must use that type.
5228 Primitive::Type value_type = GetValue()->GetType();
5229 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5230 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005231 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005232 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005233
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005234 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005235 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005236 }
5237
Aart Bik18b36ab2016-04-13 16:41:35 -07005238 void ComputeSideEffects() {
5239 Primitive::Type type = GetComponentType();
5240 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5241 SideEffectsForArchRuntimeCalls(type)));
5242 }
5243
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005244 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5245 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5246 }
5247
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005248 DECLARE_INSTRUCTION(ArraySet);
5249
5250 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005251 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5252 static constexpr size_t kFieldExpectedComponentTypeSize =
5253 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5254 static constexpr size_t kFlagNeedsTypeCheck =
5255 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5256 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005257 // Cached information for the reference_type_info_ so that codegen
5258 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005259 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5260 static constexpr size_t kNumberOfArraySetPackedBits =
5261 kFlagStaticTypeOfArrayIsObjectArray + 1;
5262 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5263 using ExpectedComponentTypeField =
5264 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005265
5266 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5267};
5268
Vladimir Markofcb503c2016-05-18 12:48:17 +01005269class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005270 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005271 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005272 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005273 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005274 // Note that arrays do not change length, so the instruction does not
5275 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005276 SetRawInputAt(0, array);
5277 }
5278
Calin Juravle77520bc2015-01-12 18:45:46 +00005279 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005280 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005281 return true;
5282 }
Calin Juravle641547a2015-04-21 22:08:51 +01005283 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5284 return obj == InputAt(0);
5285 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005286
Vladimir Markodce016e2016-04-28 13:10:02 +01005287 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5288
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005289 DECLARE_INSTRUCTION(ArrayLength);
5290
5291 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005292 // We treat a String as an array, creating the HArrayLength from String.length()
5293 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5294 // determine whether a particular HArrayLength is actually a String.length() by
5295 // looking at the type of the input but that requires holding the mutator lock, so
5296 // we prefer to use a flag, so that code generators don't need to do the locking.
5297 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5298 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5299 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5300 "Too many packed fields.");
5301
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005302 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5303};
5304
Vladimir Markofcb503c2016-05-18 12:48:17 +01005305class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005306 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005307 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5308 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005309 HBoundsCheck(HInstruction* index,
5310 HInstruction* length,
5311 uint32_t dex_pc,
5312 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5313 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5314 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005315 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005316 SetRawInputAt(0, index);
5317 SetRawInputAt(1, length);
5318 }
5319
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005320 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005321 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005322 return true;
5323 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005324
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005325 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005326
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005327 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005328
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005329 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5330 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5331
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005332 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005333
5334 DECLARE_INSTRUCTION(BoundsCheck);
5335
5336 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005337 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5338 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5339 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5340 // so we need to create an HEnvironment which will be translated to an InlineInfo
5341 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5342 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5343 // from the invoke as we don't want to look again at the dex bytecode.
5344 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5345
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005346 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5347};
5348
Vladimir Markofcb503c2016-05-18 12:48:17 +01005349class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005350 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005351 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005352 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005353
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005354 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005355 return true;
5356 }
5357
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005358 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5359 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005360
5361 DECLARE_INSTRUCTION(SuspendCheck);
5362
5363 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005364 // Only used for code generation, in order to share the same slow path between back edges
5365 // of a same loop.
5366 SlowPathCode* slow_path_;
5367
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005368 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5369};
5370
David Srbecky0cf44932015-12-09 14:09:59 +00005371// Pseudo-instruction which provides the native debugger with mapping information.
5372// It ensures that we can generate line number and local variables at this point.
5373class HNativeDebugInfo : public HTemplateInstruction<0> {
5374 public:
5375 explicit HNativeDebugInfo(uint32_t dex_pc)
5376 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5377
5378 bool NeedsEnvironment() const OVERRIDE {
5379 return true;
5380 }
5381
5382 DECLARE_INSTRUCTION(NativeDebugInfo);
5383
5384 private:
5385 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5386};
5387
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005388/**
5389 * Instruction to load a Class object.
5390 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005391class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005392 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005393 // Determines how to load the Class.
5394 enum class LoadKind {
5395 // Use the Class* from the method's own ArtMethod*.
5396 kReferrersClass,
5397
5398 // Use boot image Class* address that will be known at link time.
5399 // Used for boot image classes referenced by boot image code in non-PIC mode.
5400 kBootImageLinkTimeAddress,
5401
5402 // Use PC-relative boot image Class* address that will be known at link time.
5403 // Used for boot image classes referenced by boot image code in PIC mode.
5404 kBootImageLinkTimePcRelative,
5405
5406 // Use a known boot image Class* address, embedded in the code by the codegen.
5407 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5408 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5409 // GetIncludePatchInformation().
5410 kBootImageAddress,
5411
5412 // Load from the resolved types array at an absolute address.
5413 // Used for classes outside the boot image referenced by JIT-compiled code.
5414 kDexCacheAddress,
5415
5416 // Load from resolved types array in the dex cache using a PC-relative load.
5417 // Used for classes outside boot image when we know that we can access
5418 // the dex cache arrays using a PC-relative load.
5419 kDexCachePcRelative,
5420
5421 // Load from resolved types array accessed through the class loaded from
5422 // the compiled method's own ArtMethod*. This is the default access type when
5423 // all other types are unavailable.
5424 kDexCacheViaMethod,
5425
5426 kLast = kDexCacheViaMethod
5427 };
5428
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005429 HLoadClass(HCurrentMethod* current_method,
5430 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005431 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005432 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005433 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005434 bool needs_access_check,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005435 bool is_in_dex_cache,
5436 bool is_in_boot_image)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005437 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5438 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005439 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005440 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005441 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005442 // Referrers class should not need access check. We never inline unverified
5443 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005444 DCHECK(!is_referrers_class || !needs_access_check);
5445
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005446 SetPackedField<LoadKindField>(
5447 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005448 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5449 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005450 SetPackedFlag<kFlagIsInBootImage>(is_in_boot_image);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005451 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005452 }
5453
5454 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5455 DCHECK(HasAddress(load_kind));
5456 load_data_.address = address;
5457 SetLoadKindInternal(load_kind);
5458 }
5459
5460 void SetLoadKindWithTypeReference(LoadKind load_kind,
5461 const DexFile& dex_file,
5462 uint32_t type_index) {
5463 DCHECK(HasTypeReference(load_kind));
5464 DCHECK(IsSameDexFile(dex_file_, dex_file));
5465 DCHECK_EQ(type_index_, type_index);
5466 SetLoadKindInternal(load_kind);
5467 }
5468
5469 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5470 const DexFile& dex_file,
5471 uint32_t element_index) {
5472 DCHECK(HasDexCacheReference(load_kind));
5473 DCHECK(IsSameDexFile(dex_file_, dex_file));
5474 load_data_.dex_cache_element_index = element_index;
5475 SetLoadKindInternal(load_kind);
5476 }
5477
5478 LoadKind GetLoadKind() const {
5479 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005480 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005481
5482 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005483
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005484 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005485
5486 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5487
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005488 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005489
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005490 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005491 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005492 }
5493
Calin Juravle0ba218d2015-05-19 18:46:01 +01005494 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005495 // The entrypoint the code generator is going to call does not do
5496 // clinit of the class.
5497 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005498 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005499 }
5500
5501 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005502 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005503 (!IsReferrersClass() && !IsInDexCache()) ||
5504 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005505 }
5506
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005507
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005508 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005509 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005510 }
5511
Calin Juravleacf735c2015-02-12 15:25:22 +00005512 ReferenceTypeInfo GetLoadedClassRTI() {
5513 return loaded_class_rti_;
5514 }
5515
5516 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5517 // Make sure we only set exact types (the loaded class should never be merged).
5518 DCHECK(rti.IsExact());
5519 loaded_class_rti_ = rti;
5520 }
5521
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005522 uint32_t GetTypeIndex() const { return type_index_; }
5523 const DexFile& GetDexFile() const { return dex_file_; }
5524
5525 uint32_t GetDexCacheElementOffset() const;
5526
5527 uint64_t GetAddress() const {
5528 DCHECK(HasAddress(GetLoadKind()));
5529 return load_data_.address;
5530 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005531
Vladimir Markoa1de9182016-02-25 11:37:38 +00005532 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005533
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005534 static SideEffects SideEffectsForArchRuntimeCalls() {
5535 return SideEffects::CanTriggerGC();
5536 }
5537
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005538 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005539 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5540 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005541 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005542 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005543
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005544 void MarkInDexCache() {
5545 SetPackedFlag<kFlagIsInDexCache>(true);
5546 DCHECK(!NeedsEnvironment());
5547 RemoveEnvironment();
5548 SetSideEffects(SideEffects::None());
5549 }
5550
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005551 void MarkInBootImage() {
5552 SetPackedFlag<kFlagIsInBootImage>(true);
5553 }
5554
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005555 void AddSpecialInput(HInstruction* special_input);
5556
5557 using HInstruction::GetInputRecords; // Keep the const version visible.
5558 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5559 return ArrayRef<HUserRecord<HInstruction*>>(
5560 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5561 }
5562
5563 Primitive::Type GetType() const OVERRIDE {
5564 return Primitive::kPrimNot;
5565 }
5566
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005567 DECLARE_INSTRUCTION(LoadClass);
5568
5569 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005570 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005571 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005572 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005573 // Whether this instruction must generate the initialization check.
5574 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005575 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005576 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5577 static constexpr size_t kFieldLoadKindSize =
5578 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5579 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005580 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005581 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5582
5583 static bool HasTypeReference(LoadKind load_kind) {
5584 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5585 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5586 load_kind == LoadKind::kDexCacheViaMethod ||
5587 load_kind == LoadKind::kReferrersClass;
5588 }
5589
5590 static bool HasAddress(LoadKind load_kind) {
5591 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5592 }
5593
5594 static bool HasDexCacheReference(LoadKind load_kind) {
5595 return load_kind == LoadKind::kDexCachePcRelative;
5596 }
5597
5598 void SetLoadKindInternal(LoadKind load_kind);
5599
5600 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5601 // For other load kinds it's empty or possibly some architecture-specific instruction
5602 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5603 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005604
5605 const uint16_t type_index_;
5606 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005607
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005608 union {
5609 uint32_t dex_cache_element_index; // Only for dex cache reference.
5610 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5611 } load_data_;
5612
Calin Juravleacf735c2015-02-12 15:25:22 +00005613 ReferenceTypeInfo loaded_class_rti_;
5614
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005615 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5616};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005617std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5618
5619// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5620inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5621 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5622 return load_data_.dex_cache_element_index;
5623}
5624
5625// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5626inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005627 // The special input is used for PC-relative loads on some architectures,
5628 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005629 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005630 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5631 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5632 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005633 DCHECK(special_input_.GetInstruction() == nullptr);
5634 special_input_ = HUserRecord<HInstruction*>(special_input);
5635 special_input->AddUseAt(this, 0);
5636}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005637
Vladimir Marko372f10e2016-05-17 16:30:10 +01005638class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005639 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005640 // Determines how to load the String.
5641 enum class LoadKind {
5642 // Use boot image String* address that will be known at link time.
5643 // Used for boot image strings referenced by boot image code in non-PIC mode.
5644 kBootImageLinkTimeAddress,
5645
5646 // Use PC-relative boot image String* address that will be known at link time.
5647 // Used for boot image strings referenced by boot image code in PIC mode.
5648 kBootImageLinkTimePcRelative,
5649
5650 // Use a known boot image String* address, embedded in the code by the codegen.
5651 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5652 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5653 // GetIncludePatchInformation().
5654 kBootImageAddress,
5655
5656 // Load from the resolved strings array at an absolute address.
5657 // Used for strings outside the boot image referenced by JIT-compiled code.
5658 kDexCacheAddress,
5659
Vladimir Markoaad75c62016-10-03 08:46:48 +00005660 // Load from an entry in the .bss section using a PC-relative load.
5661 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5662 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005663
5664 // Load from resolved strings array accessed through the class loaded from
5665 // the compiled method's own ArtMethod*. This is the default access type when
5666 // all other types are unavailable.
5667 kDexCacheViaMethod,
5668
5669 kLast = kDexCacheViaMethod
5670 };
5671
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005672 HLoadString(HCurrentMethod* current_method,
5673 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005674 const DexFile& dex_file,
5675 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005676 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5677 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005678 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005679 SetPackedFlag<kFlagIsInDexCache>(false);
5680 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005681 load_data_.dex_file_ = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005682 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005683
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005684 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5685 DCHECK(HasAddress(load_kind));
5686 load_data_.address = address;
5687 SetLoadKindInternal(load_kind);
5688 }
5689
5690 void SetLoadKindWithStringReference(LoadKind load_kind,
5691 const DexFile& dex_file,
5692 uint32_t string_index) {
5693 DCHECK(HasStringReference(load_kind));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005694 load_data_.dex_file_ = &dex_file;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005695 string_index_ = string_index;
5696 SetLoadKindInternal(load_kind);
5697 }
5698
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005699 LoadKind GetLoadKind() const {
5700 return GetPackedField<LoadKindField>();
5701 }
5702
5703 const DexFile& GetDexFile() const;
5704
5705 uint32_t GetStringIndex() const {
5706 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5707 return string_index_;
5708 }
5709
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005710 uint64_t GetAddress() const {
5711 DCHECK(HasAddress(GetLoadKind()));
5712 return load_data_.address;
5713 }
5714
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005715 bool CanBeMoved() const OVERRIDE { return true; }
5716
Vladimir Marko372f10e2016-05-17 16:30:10 +01005717 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005718
5719 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5720
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005721 // Will call the runtime if we need to load the string through
5722 // the dex cache and the string is not guaranteed to be there yet.
5723 bool NeedsEnvironment() const OVERRIDE {
5724 LoadKind load_kind = GetLoadKind();
5725 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5726 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5727 load_kind == LoadKind::kBootImageAddress) {
5728 return false;
5729 }
5730 return !IsInDexCache();
5731 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005732
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005733 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5734 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5735 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005736
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005737 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005738 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005739
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005740 static SideEffects SideEffectsForArchRuntimeCalls() {
5741 return SideEffects::CanTriggerGC();
5742 }
5743
Vladimir Markoa1de9182016-02-25 11:37:38 +00005744 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5745
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005746 void MarkInDexCache() {
5747 SetPackedFlag<kFlagIsInDexCache>(true);
5748 DCHECK(!NeedsEnvironment());
5749 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005750 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005751 }
5752
Vladimir Marko372f10e2016-05-17 16:30:10 +01005753 void AddSpecialInput(HInstruction* special_input);
5754
5755 using HInstruction::GetInputRecords; // Keep the const version visible.
5756 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5757 return ArrayRef<HUserRecord<HInstruction*>>(
5758 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005759 }
5760
Vladimir Marko372f10e2016-05-17 16:30:10 +01005761 Primitive::Type GetType() const OVERRIDE {
5762 return Primitive::kPrimNot;
5763 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005764
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005765 DECLARE_INSTRUCTION(LoadString);
5766
5767 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005768 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005769 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5770 static constexpr size_t kFieldLoadKindSize =
5771 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5772 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005773 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005774 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005775
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005776 static bool HasStringReference(LoadKind load_kind) {
5777 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5778 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005779 load_kind == LoadKind::kBssEntry ||
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005780 load_kind == LoadKind::kDexCacheViaMethod;
5781 }
5782
5783 static bool HasAddress(LoadKind load_kind) {
5784 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5785 }
5786
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005787 void SetLoadKindInternal(LoadKind load_kind);
5788
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005789 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5790 // For other load kinds it's empty or possibly some architecture-specific instruction
5791 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005792 HUserRecord<HInstruction*> special_input_;
5793
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005794 // String index serves also as the hash code and it's also needed for slow-paths,
5795 // so it must not be overwritten with other load data.
5796 uint32_t string_index_;
5797
5798 union {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005799 const DexFile* dex_file_; // For string reference.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005800 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5801 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005802
5803 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5804};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005805std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5806
5807// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5808inline const DexFile& HLoadString::GetDexFile() const {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005809 DCHECK(HasStringReference(GetLoadKind())) << GetLoadKind();
5810 return *load_data_.dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005811}
5812
5813// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5814inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005815 // The special input is used for PC-relative loads on some architectures,
5816 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005817 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005818 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005819 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5820 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005821 // HLoadString::GetInputRecords() returns an empty array at this point,
5822 // so use the GetInputRecords() from the base class to set the input record.
5823 DCHECK(special_input_.GetInstruction() == nullptr);
5824 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005825 special_input->AddUseAt(this, 0);
5826}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005827
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005828/**
5829 * Performs an initialization check on its Class object input.
5830 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005831class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005832 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005833 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005834 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005835 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005836 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5837 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005838 SetRawInputAt(0, constant);
5839 }
5840
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005841 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005842 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005843 return true;
5844 }
5845
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005846 bool NeedsEnvironment() const OVERRIDE {
5847 // May call runtime to initialize the class.
5848 return true;
5849 }
5850
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005851 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005852
5853 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5854
5855 DECLARE_INSTRUCTION(ClinitCheck);
5856
5857 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005858 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5859};
5860
Vladimir Markofcb503c2016-05-18 12:48:17 +01005861class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005862 public:
5863 HStaticFieldGet(HInstruction* cls,
5864 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005865 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005866 bool is_volatile,
5867 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005868 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005869 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005870 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005871 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005872 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005873 field_info_(field_offset,
5874 field_type,
5875 is_volatile,
5876 field_idx,
5877 declaring_class_def_index,
5878 dex_file,
5879 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005880 SetRawInputAt(0, cls);
5881 }
5882
Calin Juravle52c48962014-12-16 17:02:57 +00005883
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005884 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005885
Vladimir Marko372f10e2016-05-17 16:30:10 +01005886 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5887 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005888 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005889 }
5890
5891 size_t ComputeHashCode() const OVERRIDE {
5892 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5893 }
5894
Calin Juravle52c48962014-12-16 17:02:57 +00005895 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005896 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5897 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005898 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005899
5900 DECLARE_INSTRUCTION(StaticFieldGet);
5901
5902 private:
5903 const FieldInfo field_info_;
5904
5905 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5906};
5907
Vladimir Markofcb503c2016-05-18 12:48:17 +01005908class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005909 public:
5910 HStaticFieldSet(HInstruction* cls,
5911 HInstruction* value,
5912 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005913 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005914 bool is_volatile,
5915 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005916 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005917 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005918 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005919 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005920 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005921 field_info_(field_offset,
5922 field_type,
5923 is_volatile,
5924 field_idx,
5925 declaring_class_def_index,
5926 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005927 dex_cache) {
5928 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005929 SetRawInputAt(0, cls);
5930 SetRawInputAt(1, value);
5931 }
5932
Calin Juravle52c48962014-12-16 17:02:57 +00005933 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005934 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5935 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005936 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005937
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005938 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005939 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5940 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005941
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005942 DECLARE_INSTRUCTION(StaticFieldSet);
5943
5944 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005945 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5946 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5947 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5948 "Too many packed fields.");
5949
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005950 const FieldInfo field_info_;
5951
5952 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5953};
5954
Vladimir Markofcb503c2016-05-18 12:48:17 +01005955class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005956 public:
5957 HUnresolvedInstanceFieldGet(HInstruction* obj,
5958 Primitive::Type field_type,
5959 uint32_t field_index,
5960 uint32_t dex_pc)
5961 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5962 field_index_(field_index) {
5963 SetRawInputAt(0, obj);
5964 }
5965
5966 bool NeedsEnvironment() const OVERRIDE { return true; }
5967 bool CanThrow() const OVERRIDE { return true; }
5968
5969 Primitive::Type GetFieldType() const { return GetType(); }
5970 uint32_t GetFieldIndex() const { return field_index_; }
5971
5972 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5973
5974 private:
5975 const uint32_t field_index_;
5976
5977 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5978};
5979
Vladimir Markofcb503c2016-05-18 12:48:17 +01005980class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005981 public:
5982 HUnresolvedInstanceFieldSet(HInstruction* obj,
5983 HInstruction* value,
5984 Primitive::Type field_type,
5985 uint32_t field_index,
5986 uint32_t dex_pc)
5987 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005988 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005989 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005990 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005991 SetRawInputAt(0, obj);
5992 SetRawInputAt(1, value);
5993 }
5994
5995 bool NeedsEnvironment() const OVERRIDE { return true; }
5996 bool CanThrow() const OVERRIDE { return true; }
5997
Vladimir Markoa1de9182016-02-25 11:37:38 +00005998 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005999 uint32_t GetFieldIndex() const { return field_index_; }
6000
6001 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6002
6003 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006004 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6005 static constexpr size_t kFieldFieldTypeSize =
6006 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6007 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6008 kFieldFieldType + kFieldFieldTypeSize;
6009 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6010 "Too many packed fields.");
6011 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6012
Calin Juravlee460d1d2015-09-29 04:52:17 +01006013 const uint32_t field_index_;
6014
6015 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6016};
6017
Vladimir Markofcb503c2016-05-18 12:48:17 +01006018class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006019 public:
6020 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6021 uint32_t field_index,
6022 uint32_t dex_pc)
6023 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6024 field_index_(field_index) {
6025 }
6026
6027 bool NeedsEnvironment() const OVERRIDE { return true; }
6028 bool CanThrow() const OVERRIDE { return true; }
6029
6030 Primitive::Type GetFieldType() const { return GetType(); }
6031 uint32_t GetFieldIndex() const { return field_index_; }
6032
6033 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6034
6035 private:
6036 const uint32_t field_index_;
6037
6038 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6039};
6040
Vladimir Markofcb503c2016-05-18 12:48:17 +01006041class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006042 public:
6043 HUnresolvedStaticFieldSet(HInstruction* value,
6044 Primitive::Type field_type,
6045 uint32_t field_index,
6046 uint32_t dex_pc)
6047 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006048 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006049 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006050 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006051 SetRawInputAt(0, value);
6052 }
6053
6054 bool NeedsEnvironment() const OVERRIDE { return true; }
6055 bool CanThrow() const OVERRIDE { return true; }
6056
Vladimir Markoa1de9182016-02-25 11:37:38 +00006057 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006058 uint32_t GetFieldIndex() const { return field_index_; }
6059
6060 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6061
6062 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006063 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6064 static constexpr size_t kFieldFieldTypeSize =
6065 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6066 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6067 kFieldFieldType + kFieldFieldTypeSize;
6068 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6069 "Too many packed fields.");
6070 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6071
Calin Juravlee460d1d2015-09-29 04:52:17 +01006072 const uint32_t field_index_;
6073
6074 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6075};
6076
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006077// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006078class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006079 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006080 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6081 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006082
David Brazdilbbd733e2015-08-18 17:48:17 +01006083 bool CanBeNull() const OVERRIDE { return false; }
6084
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006085 DECLARE_INSTRUCTION(LoadException);
6086
6087 private:
6088 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6089};
6090
David Brazdilcb1c0552015-08-04 16:22:25 +01006091// Implicit part of move-exception which clears thread-local exception storage.
6092// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006093class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006094 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006095 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6096 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006097
6098 DECLARE_INSTRUCTION(ClearException);
6099
6100 private:
6101 DISALLOW_COPY_AND_ASSIGN(HClearException);
6102};
6103
Vladimir Markofcb503c2016-05-18 12:48:17 +01006104class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006105 public:
6106 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006107 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006108 SetRawInputAt(0, exception);
6109 }
6110
6111 bool IsControlFlow() const OVERRIDE { return true; }
6112
6113 bool NeedsEnvironment() const OVERRIDE { return true; }
6114
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006115 bool CanThrow() const OVERRIDE { return true; }
6116
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006117
6118 DECLARE_INSTRUCTION(Throw);
6119
6120 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006121 DISALLOW_COPY_AND_ASSIGN(HThrow);
6122};
6123
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006124/**
6125 * Implementation strategies for the code generator of a HInstanceOf
6126 * or `HCheckCast`.
6127 */
6128enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006129 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006130 kExactCheck, // Can do a single class compare.
6131 kClassHierarchyCheck, // Can just walk the super class chain.
6132 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6133 kInterfaceCheck, // No optimization yet when checking against an interface.
6134 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006135 kArrayCheck, // No optimization yet when checking against a generic array.
6136 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006137};
6138
Roland Levillain86503782016-02-11 19:07:30 +00006139std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6140
Vladimir Markofcb503c2016-05-18 12:48:17 +01006141class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006142 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006143 HInstanceOf(HInstruction* object,
6144 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006145 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006146 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006147 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006148 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006149 dex_pc) {
6150 SetPackedField<TypeCheckKindField>(check_kind);
6151 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006152 SetRawInputAt(0, object);
6153 SetRawInputAt(1, constant);
6154 }
6155
6156 bool CanBeMoved() const OVERRIDE { return true; }
6157
Vladimir Marko372f10e2016-05-17 16:30:10 +01006158 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006159 return true;
6160 }
6161
6162 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006163 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006164 }
6165
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006166 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006167 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6168 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6169 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6170 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006171
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006172 static bool CanCallRuntime(TypeCheckKind check_kind) {
6173 // Mips currently does runtime calls for any other checks.
6174 return check_kind != TypeCheckKind::kExactCheck;
6175 }
6176
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006177 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006178 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006179 }
6180
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006181 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006182
6183 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006184 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6185 static constexpr size_t kFieldTypeCheckKindSize =
6186 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6187 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6188 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6189 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6190 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006191
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006192 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6193};
6194
Vladimir Markofcb503c2016-05-18 12:48:17 +01006195class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006196 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006197 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006198 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006199 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6200 SetPackedFlag<kFlagUpperCanBeNull>(true);
6201 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006202 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006203 SetRawInputAt(0, input);
6204 }
6205
David Brazdilf5552582015-12-27 13:36:12 +00006206 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006207 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006208 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006209 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006210
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006211 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006212 DCHECK(GetUpperCanBeNull() || !can_be_null);
6213 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006214 }
6215
Vladimir Markoa1de9182016-02-25 11:37:38 +00006216 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006217
Calin Juravleb1498f62015-02-16 13:13:29 +00006218 DECLARE_INSTRUCTION(BoundType);
6219
6220 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006221 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6222 // is false then CanBeNull() cannot be true).
6223 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6224 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6225 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6226 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6227
Calin Juravleb1498f62015-02-16 13:13:29 +00006228 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006229 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6230 // It is used to bound the type in cases like:
6231 // if (x instanceof ClassX) {
6232 // // uper_bound_ will be ClassX
6233 // }
David Brazdilf5552582015-12-27 13:36:12 +00006234 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006235
6236 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6237};
6238
Vladimir Markofcb503c2016-05-18 12:48:17 +01006239class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006240 public:
6241 HCheckCast(HInstruction* object,
6242 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006243 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006244 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006245 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6246 SetPackedField<TypeCheckKindField>(check_kind);
6247 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006248 SetRawInputAt(0, object);
6249 SetRawInputAt(1, constant);
6250 }
6251
6252 bool CanBeMoved() const OVERRIDE { return true; }
6253
Vladimir Marko372f10e2016-05-17 16:30:10 +01006254 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006255 return true;
6256 }
6257
6258 bool NeedsEnvironment() const OVERRIDE {
6259 // Instruction may throw a CheckCastError.
6260 return true;
6261 }
6262
6263 bool CanThrow() const OVERRIDE { return true; }
6264
Vladimir Markoa1de9182016-02-25 11:37:38 +00006265 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6266 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6267 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6268 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006269
6270 DECLARE_INSTRUCTION(CheckCast);
6271
6272 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006273 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6274 static constexpr size_t kFieldTypeCheckKindSize =
6275 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6276 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6277 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6278 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6279 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006280
6281 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006282};
6283
Andreas Gampe26de38b2016-07-27 17:53:11 -07006284/**
6285 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6286 * @details We define the combined barrier types that are actually required
6287 * by the Java Memory Model, rather than using exactly the terminology from
6288 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6289 * primitives. Note that the JSR-133 cookbook generally does not deal with
6290 * store atomicity issues, and the recipes there are not always entirely sufficient.
6291 * The current recipe is as follows:
6292 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6293 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6294 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6295 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6296 * class has final fields.
6297 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6298 * store-to-memory instructions. Only generated together with non-temporal stores.
6299 */
6300enum MemBarrierKind {
6301 kAnyStore,
6302 kLoadAny,
6303 kStoreStore,
6304 kAnyAny,
6305 kNTStoreStore,
6306 kLastBarrierKind = kNTStoreStore
6307};
6308std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6309
Vladimir Markofcb503c2016-05-18 12:48:17 +01006310class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006311 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006312 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006313 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006314 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6315 SetPackedField<BarrierKindField>(barrier_kind);
6316 }
Calin Juravle27df7582015-04-17 19:12:31 +01006317
Vladimir Markoa1de9182016-02-25 11:37:38 +00006318 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006319
6320 DECLARE_INSTRUCTION(MemoryBarrier);
6321
6322 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006323 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6324 static constexpr size_t kFieldBarrierKindSize =
6325 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6326 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6327 kFieldBarrierKind + kFieldBarrierKindSize;
6328 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6329 "Too many packed fields.");
6330 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006331
6332 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6333};
6334
Vladimir Markofcb503c2016-05-18 12:48:17 +01006335class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006336 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006337 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006338 kEnter,
6339 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006340 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006341 };
6342
6343 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006344 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006345 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6346 dex_pc) {
6347 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006348 SetRawInputAt(0, object);
6349 }
6350
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006351 // Instruction may go into runtime, so we need an environment.
6352 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006353
6354 bool CanThrow() const OVERRIDE {
6355 // Verifier guarantees that monitor-exit cannot throw.
6356 // This is important because it allows the HGraphBuilder to remove
6357 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6358 return IsEnter();
6359 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006360
Vladimir Markoa1de9182016-02-25 11:37:38 +00006361 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6362 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006363
6364 DECLARE_INSTRUCTION(MonitorOperation);
6365
Calin Juravle52c48962014-12-16 17:02:57 +00006366 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006367 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6368 static constexpr size_t kFieldOperationKindSize =
6369 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6370 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6371 kFieldOperationKind + kFieldOperationKindSize;
6372 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6373 "Too many packed fields.");
6374 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006375
6376 private:
6377 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6378};
6379
Vladimir Markofcb503c2016-05-18 12:48:17 +01006380class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006381 public:
6382 HSelect(HInstruction* condition,
6383 HInstruction* true_value,
6384 HInstruction* false_value,
6385 uint32_t dex_pc)
6386 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6387 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6388
6389 // First input must be `true_value` or `false_value` to allow codegens to
6390 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6391 // that architectures which implement HSelect as a conditional move also
6392 // will not need to invert the condition.
6393 SetRawInputAt(0, false_value);
6394 SetRawInputAt(1, true_value);
6395 SetRawInputAt(2, condition);
6396 }
6397
6398 HInstruction* GetFalseValue() const { return InputAt(0); }
6399 HInstruction* GetTrueValue() const { return InputAt(1); }
6400 HInstruction* GetCondition() const { return InputAt(2); }
6401
6402 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006403 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6404 return true;
6405 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006406
6407 bool CanBeNull() const OVERRIDE {
6408 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6409 }
6410
6411 DECLARE_INSTRUCTION(Select);
6412
6413 private:
6414 DISALLOW_COPY_AND_ASSIGN(HSelect);
6415};
6416
Vladimir Markof9f64412015-09-02 14:05:49 +01006417class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006418 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006419 MoveOperands(Location source,
6420 Location destination,
6421 Primitive::Type type,
6422 HInstruction* instruction)
6423 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006424
6425 Location GetSource() const { return source_; }
6426 Location GetDestination() const { return destination_; }
6427
6428 void SetSource(Location value) { source_ = value; }
6429 void SetDestination(Location value) { destination_ = value; }
6430
6431 // The parallel move resolver marks moves as "in-progress" by clearing the
6432 // destination (but not the source).
6433 Location MarkPending() {
6434 DCHECK(!IsPending());
6435 Location dest = destination_;
6436 destination_ = Location::NoLocation();
6437 return dest;
6438 }
6439
6440 void ClearPending(Location dest) {
6441 DCHECK(IsPending());
6442 destination_ = dest;
6443 }
6444
6445 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006446 DCHECK(source_.IsValid() || destination_.IsInvalid());
6447 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006448 }
6449
6450 // True if this blocks a move from the given location.
6451 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006452 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006453 }
6454
6455 // A move is redundant if it's been eliminated, if its source and
6456 // destination are the same, or if its destination is unneeded.
6457 bool IsRedundant() const {
6458 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6459 }
6460
6461 // We clear both operands to indicate move that's been eliminated.
6462 void Eliminate() {
6463 source_ = destination_ = Location::NoLocation();
6464 }
6465
6466 bool IsEliminated() const {
6467 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6468 return source_.IsInvalid();
6469 }
6470
Alexey Frunze4dda3372015-06-01 18:31:49 -07006471 Primitive::Type GetType() const { return type_; }
6472
Nicolas Geoffray90218252015-04-15 11:56:51 +01006473 bool Is64BitMove() const {
6474 return Primitive::Is64BitType(type_);
6475 }
6476
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006477 HInstruction* GetInstruction() const { return instruction_; }
6478
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006479 private:
6480 Location source_;
6481 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006482 // The type this move is for.
6483 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006484 // The instruction this move is assocatied with. Null when this move is
6485 // for moving an input in the expected locations of user (including a phi user).
6486 // This is only used in debug mode, to ensure we do not connect interval siblings
6487 // in the same parallel move.
6488 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006489};
6490
Roland Levillainc9285912015-12-18 10:38:42 +00006491std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6492
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006493static constexpr size_t kDefaultNumberOfMoves = 4;
6494
Vladimir Markofcb503c2016-05-18 12:48:17 +01006495class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006496 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006497 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006498 : HTemplateInstruction(SideEffects::None(), dex_pc),
6499 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6500 moves_.reserve(kDefaultNumberOfMoves);
6501 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006502
Nicolas Geoffray90218252015-04-15 11:56:51 +01006503 void AddMove(Location source,
6504 Location destination,
6505 Primitive::Type type,
6506 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006507 DCHECK(source.IsValid());
6508 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006509 if (kIsDebugBuild) {
6510 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006511 for (const MoveOperands& move : moves_) {
6512 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006513 // Special case the situation where the move is for the spill slot
6514 // of the instruction.
6515 if ((GetPrevious() == instruction)
6516 || ((GetPrevious() == nullptr)
6517 && instruction->IsPhi()
6518 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006519 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006520 << "Doing parallel moves for the same instruction.";
6521 } else {
6522 DCHECK(false) << "Doing parallel moves for the same instruction.";
6523 }
6524 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006525 }
6526 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006527 for (const MoveOperands& move : moves_) {
6528 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006529 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006530 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006531 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006532 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006533 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006534 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006535 }
6536
Vladimir Marko225b6462015-09-28 12:17:40 +01006537 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006538 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006539 }
6540
Vladimir Marko225b6462015-09-28 12:17:40 +01006541 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006542
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006543 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006544
6545 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006546 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006547
6548 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6549};
6550
Mark Mendell0616ae02015-04-17 12:49:27 -04006551} // namespace art
6552
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006553#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6554#include "nodes_shared.h"
6555#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006556#ifdef ART_ENABLE_CODEGEN_arm
6557#include "nodes_arm.h"
6558#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006559#ifdef ART_ENABLE_CODEGEN_arm64
6560#include "nodes_arm64.h"
6561#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006562#ifdef ART_ENABLE_CODEGEN_mips
6563#include "nodes_mips.h"
6564#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006565#ifdef ART_ENABLE_CODEGEN_x86
6566#include "nodes_x86.h"
6567#endif
6568
6569namespace art {
6570
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006571class HGraphVisitor : public ValueObject {
6572 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006573 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6574 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006575
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006576 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006577 virtual void VisitBasicBlock(HBasicBlock* block);
6578
Roland Levillain633021e2014-10-01 14:12:25 +01006579 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006580 void VisitInsertionOrder();
6581
Roland Levillain633021e2014-10-01 14:12:25 +01006582 // Visit the graph following dominator tree reverse post-order.
6583 void VisitReversePostOrder();
6584
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006585 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006586
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006587 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006588#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006589 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6590
6591 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6592
6593#undef DECLARE_VISIT_INSTRUCTION
6594
6595 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006596 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006597
6598 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6599};
6600
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006601class HGraphDelegateVisitor : public HGraphVisitor {
6602 public:
6603 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6604 virtual ~HGraphDelegateVisitor() {}
6605
6606 // Visit functions that delegate to to super class.
6607#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006608 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006609
6610 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6611
6612#undef DECLARE_VISIT_INSTRUCTION
6613
6614 private:
6615 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6616};
6617
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006618class HInsertionOrderIterator : public ValueObject {
6619 public:
6620 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6621
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006622 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006623 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006624 void Advance() { ++index_; }
6625
6626 private:
6627 const HGraph& graph_;
6628 size_t index_;
6629
6630 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6631};
6632
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006633class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006634 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006635 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6636 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006637 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006638 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006639
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006640 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6641 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006642 void Advance() { ++index_; }
6643
6644 private:
6645 const HGraph& graph_;
6646 size_t index_;
6647
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006648 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006649};
6650
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006651class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006652 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006653 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006654 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006655 // 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
6659 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006660 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
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(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006668};
6669
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006670// Iterator over the blocks that art part of the loop. Includes blocks part
6671// of an inner loop. The order in which the blocks are iterated is on their
6672// block id.
6673class HBlocksInLoopIterator : public ValueObject {
6674 public:
6675 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6676 : blocks_in_loop_(info.GetBlocks()),
6677 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6678 index_(0) {
6679 if (!blocks_in_loop_.IsBitSet(index_)) {
6680 Advance();
6681 }
6682 }
6683
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006684 bool Done() const { return index_ == blocks_.size(); }
6685 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006686 void Advance() {
6687 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006688 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006689 if (blocks_in_loop_.IsBitSet(index_)) {
6690 break;
6691 }
6692 }
6693 }
6694
6695 private:
6696 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006697 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006698 size_t index_;
6699
6700 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6701};
6702
Mingyao Yang3584bce2015-05-19 16:01:59 -07006703// Iterator over the blocks that art part of the loop. Includes blocks part
6704// of an inner loop. The order in which the blocks are iterated is reverse
6705// post order.
6706class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6707 public:
6708 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6709 : blocks_in_loop_(info.GetBlocks()),
6710 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6711 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006712 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006713 Advance();
6714 }
6715 }
6716
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006717 bool Done() const { return index_ == blocks_.size(); }
6718 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006719 void Advance() {
6720 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006721 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6722 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006723 break;
6724 }
6725 }
6726 }
6727
6728 private:
6729 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006730 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006731 size_t index_;
6732
6733 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6734};
6735
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006736inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006737 if (constant->IsIntConstant()) {
6738 return constant->AsIntConstant()->GetValue();
6739 } else if (constant->IsLongConstant()) {
6740 return constant->AsLongConstant()->GetValue();
6741 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006742 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006743 return 0;
6744 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006745}
6746
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006747#define INSTRUCTION_TYPE_CHECK(type, super) \
6748 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6749 inline const H##type* HInstruction::As##type() const { \
6750 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6751 } \
6752 inline H##type* HInstruction::As##type() { \
6753 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6754 }
6755
6756 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6757#undef INSTRUCTION_TYPE_CHECK
6758
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006759// Create space in `blocks` for adding `number_of_new_blocks` entries
6760// starting at location `at`. Blocks after `at` are moved accordingly.
6761inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6762 size_t number_of_new_blocks,
6763 size_t after) {
6764 DCHECK_LT(after, blocks->size());
6765 size_t old_size = blocks->size();
6766 size_t new_size = old_size + number_of_new_blocks;
6767 blocks->resize(new_size);
6768 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6769}
6770
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006771} // namespace art
6772
6773#endif // ART_COMPILER_OPTIMIZING_NODES_H_