blob: 397abded27ec213d5f95e3c2c06fdc4ead0d48c5 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko60584552015-09-03 13:35:12 +000028#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010029#include "base/transform_array_ref.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010030#include "dex_file.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000031#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000032#include "handle.h"
33#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000034#include "invoke_type.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010035#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010036#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000037#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000038#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010039#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070040#include "primitive.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010041#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042
43namespace art {
44
David Brazdil1abb4192015-02-17 18:33:36 +000045class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010047class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010049class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000051class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000053class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000054class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000055class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000056class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000057class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010058class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010059class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010060class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010061class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000062class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010063class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000064class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000065
Mathieu Chartier736b5602015-09-02 14:54:11 -070066namespace mirror {
67class DexCache;
68} // namespace mirror
69
Nicolas Geoffray818f2102014-02-18 16:43:35 +000070static const int kDefaultNumberOfBlocks = 8;
71static const int kDefaultNumberOfSuccessors = 2;
72static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010073static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010074static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000075static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000076
Roland Levillain5b5b9312016-03-22 14:57:31 +000077// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
78static constexpr int32_t kMaxIntShiftDistance = 0x1f;
79// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
80static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000081
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010082static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070083static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010084
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010085static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
86
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060087static constexpr uint32_t kNoDexPc = -1;
88
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010089inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
90 // For the purposes of the compiler, the dex files must actually be the same object
91 // if we want to safely treat them as the same. This is especially important for JIT
92 // as custom class loaders can open the same underlying file (or memory) multiple
93 // times and provide different class resolution but no two class loaders should ever
94 // use the same DexFile object - doing so is an unsupported hack that can lead to
95 // all sorts of weird failures.
96 return &lhs == &rhs;
97}
98
Dave Allison20dfc792014-06-16 20:44:29 -070099enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700100 // All types.
101 kCondEQ, // ==
102 kCondNE, // !=
103 // Signed integers and floating-point numbers.
104 kCondLT, // <
105 kCondLE, // <=
106 kCondGT, // >
107 kCondGE, // >=
108 // Unsigned integers.
109 kCondB, // <
110 kCondBE, // <=
111 kCondA, // >
112 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100113 // First and last aliases.
114 kCondFirst = kCondEQ,
115 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700116};
117
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000118enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000119 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000120 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000121 kAnalysisFailThrowCatchLoop,
122 kAnalysisFailAmbiguousArrayOp,
123 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000124};
125
Vladimir Markof9f64412015-09-02 14:05:49 +0100126class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100127 public:
128 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
129
130 void AddInstruction(HInstruction* instruction);
131 void RemoveInstruction(HInstruction* instruction);
132
David Brazdilc3d743f2015-04-22 13:40:50 +0100133 // Insert `instruction` before/after an existing instruction `cursor`.
134 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
135 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
136
Roland Levillain6b469232014-09-25 10:10:38 +0100137 // Return true if this list contains `instruction`.
138 bool Contains(HInstruction* instruction) const;
139
Roland Levillainccc07a92014-09-16 14:48:16 +0100140 // Return true if `instruction1` is found before `instruction2` in
141 // this instruction list and false otherwise. Abort if none
142 // of these instructions is found.
143 bool FoundBefore(const HInstruction* instruction1,
144 const HInstruction* instruction2) const;
145
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000146 bool IsEmpty() const { return first_instruction_ == nullptr; }
147 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
148
149 // Update the block of all instructions to be `block`.
150 void SetBlockOfInstructions(HBasicBlock* block) const;
151
152 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000153 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000154 void Add(const HInstructionList& instruction_list);
155
David Brazdil2d7352b2015-04-20 14:52:42 +0100156 // Return the number of instructions in the list. This is an expensive operation.
157 size_t CountSize() const;
158
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100159 private:
160 HInstruction* first_instruction_;
161 HInstruction* last_instruction_;
162
163 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000164 friend class HGraph;
165 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100166 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100167 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100168
169 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
170};
171
David Brazdil4833f5a2015-12-16 10:37:39 +0000172class ReferenceTypeInfo : ValueObject {
173 public:
174 typedef Handle<mirror::Class> TypeHandle;
175
Vladimir Markoa1de9182016-02-25 11:37:38 +0000176 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
177
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700178 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100179 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
180 }
181
Vladimir Markoa1de9182016-02-25 11:37:38 +0000182 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000183 return ReferenceTypeInfo(type_handle, is_exact);
184 }
185
186 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
187
Vladimir Markof39745e2016-01-26 12:16:55 +0000188 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000189 return handle.GetReference() != nullptr;
190 }
191
Vladimir Marko456307a2016-04-19 14:12:13 +0000192 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000193 return IsValidHandle(type_handle_);
194 }
195
196 bool IsExact() const { return is_exact_; }
197
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700198 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000199 DCHECK(IsValid());
200 return GetTypeHandle()->IsObjectClass();
201 }
202
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700203 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000204 DCHECK(IsValid());
205 return GetTypeHandle()->IsStringClass();
206 }
207
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700208 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000209 DCHECK(IsValid());
210 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
211 }
212
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700213 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000214 DCHECK(IsValid());
215 return GetTypeHandle()->IsInterface();
216 }
217
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700218 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000219 DCHECK(IsValid());
220 return GetTypeHandle()->IsArrayClass();
221 }
222
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700223 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000224 DCHECK(IsValid());
225 return GetTypeHandle()->IsPrimitiveArray();
226 }
227
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700228 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000229 DCHECK(IsValid());
230 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
231 }
232
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700233 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000234 DCHECK(IsValid());
235 if (!IsExact()) return false;
236 if (!IsArrayClass()) return false;
237 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
238 }
239
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000241 DCHECK(IsValid());
242 if (!IsExact()) return false;
243 if (!IsArrayClass()) return false;
244 if (!rti.IsArrayClass()) return false;
245 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
246 rti.GetTypeHandle()->GetComponentType());
247 }
248
249 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
250
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700251 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000252 DCHECK(IsValid());
253 DCHECK(rti.IsValid());
254 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
255 }
256
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700257 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000258 DCHECK(IsValid());
259 DCHECK(rti.IsValid());
260 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
261 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
262 }
263
264 // Returns true if the type information provide the same amount of details.
265 // Note that it does not mean that the instructions have the same actual type
266 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700267 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000268 if (!IsValid() && !rti.IsValid()) {
269 // Invalid types are equal.
270 return true;
271 }
272 if (!IsValid() || !rti.IsValid()) {
273 // One is valid, the other not.
274 return false;
275 }
276 return IsExact() == rti.IsExact()
277 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
278 }
279
280 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000281 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
282 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
283 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000284
285 // The class of the object.
286 TypeHandle type_handle_;
287 // Whether or not the type is exact or a superclass of the actual type.
288 // Whether or not we have any information about this type.
289 bool is_exact_;
290};
291
292std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
293
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000294// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100295class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000296 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100297 HGraph(ArenaAllocator* arena,
298 const DexFile& dex_file,
299 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100300 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700301 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100302 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100303 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000304 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100305 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000306 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100307 blocks_(arena->Adapter(kArenaAllocBlockList)),
308 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
309 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700310 entry_block_(nullptr),
311 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100312 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100313 number_of_vregs_(0),
314 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000315 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400316 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000317 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000318 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000319 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000320 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100321 dex_file_(dex_file),
322 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100323 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100324 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100325 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700326 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000327 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100328 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
329 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
330 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
331 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000332 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000333 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
334 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100335 blocks_.reserve(kDefaultNumberOfBlocks);
336 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000337
David Brazdilbadd8262016-02-02 16:28:56 +0000338 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
339 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
340
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000341 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100342 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
343
David Brazdil69ba7b72015-06-23 18:27:30 +0100344 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000345 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100346
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000347 HBasicBlock* GetEntryBlock() const { return entry_block_; }
348 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100349 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000350
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000351 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
352 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000353
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000354 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100355
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100356 void ComputeDominanceInformation();
357 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000358 void ClearLoopInformation();
359 void FindBackEdges(ArenaBitVector* visited);
360 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100361 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100362 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000363
David Brazdil4833f5a2015-12-16 10:37:39 +0000364 // Analyze all natural loops in this graph. Returns a code specifying that it
365 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000366 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000367 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100368
Aart Bik20e9db62016-09-14 10:52:13 -0700369 // Computes the linear order (should be called before using HLinearOrderIterator).
370 // Linearizes the graph such that:
371 // (1): a block is always after its dominator,
372 // (2): blocks of loops are contiguous.
373 // This creates a natural and efficient ordering when visualizing live ranges.
374 void Linearize();
375
David Brazdilffee3d32015-07-06 11:48:53 +0100376 // Iterate over blocks to compute try block membership. Needs reverse post
377 // order and loop information.
378 void ComputeTryBlockInformation();
379
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000380 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000381 // Returns the instruction to replace the invoke expression or null if the
382 // invoke is for a void method. Note that the caller is responsible for replacing
383 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000384 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000385
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000386 // Update the loop and try membership of `block`, which was spawned from `reference`.
387 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
388 // should be the new back edge.
389 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
390 HBasicBlock* reference,
391 bool replace_if_back_edge);
392
Mingyao Yang3584bce2015-05-19 16:01:59 -0700393 // Need to add a couple of blocks to test if the loop body is entered and
394 // put deoptimization instructions, etc.
395 void TransformLoopHeaderForBCE(HBasicBlock* header);
396
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000397 // Removes `block` from the graph. Assumes `block` has been disconnected from
398 // other blocks and has no instructions or phis.
399 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000400
David Brazdilfc6a86a2015-06-26 10:33:45 +0000401 // Splits the edge between `block` and `successor` while preserving the
402 // indices in the predecessor/successor lists. If there are multiple edges
403 // between the blocks, the lowest indices are used.
404 // Returns the new block which is empty and has the same dex pc as `successor`.
405 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
406
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100407 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
408 void SimplifyLoop(HBasicBlock* header);
409
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000410 int32_t GetNextInstructionId() {
411 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000412 return current_instruction_id_++;
413 }
414
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000415 int32_t GetCurrentInstructionId() const {
416 return current_instruction_id_;
417 }
418
419 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000420 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000421 current_instruction_id_ = id;
422 }
423
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100424 uint16_t GetMaximumNumberOfOutVRegs() const {
425 return maximum_number_of_out_vregs_;
426 }
427
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000428 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
429 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100430 }
431
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100432 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
433 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
434 }
435
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000436 void UpdateTemporariesVRegSlots(size_t slots) {
437 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100438 }
439
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000440 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100441 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000442 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100443 }
444
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100445 void SetNumberOfVRegs(uint16_t number_of_vregs) {
446 number_of_vregs_ = number_of_vregs;
447 }
448
449 uint16_t GetNumberOfVRegs() const {
450 return number_of_vregs_;
451 }
452
453 void SetNumberOfInVRegs(uint16_t value) {
454 number_of_in_vregs_ = value;
455 }
456
David Brazdildee58d62016-04-07 09:54:26 +0000457 uint16_t GetNumberOfInVRegs() const {
458 return number_of_in_vregs_;
459 }
460
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100461 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100462 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100463 return number_of_vregs_ - number_of_in_vregs_;
464 }
465
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100466 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100467 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100468 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100469
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100470 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100471 return linear_order_;
472 }
473
Mark Mendell1152c922015-04-24 17:06:35 -0400474 bool HasBoundsChecks() const {
475 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800476 }
477
Mark Mendell1152c922015-04-24 17:06:35 -0400478 void SetHasBoundsChecks(bool value) {
479 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800480 }
481
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100482 bool ShouldGenerateConstructorBarrier() const {
483 return should_generate_constructor_barrier_;
484 }
485
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000486 bool IsDebuggable() const { return debuggable_; }
487
David Brazdil8d5b8b22015-03-24 10:51:52 +0000488 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000489 // already, it is created and inserted into the graph. This method is only for
490 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600491 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000492
493 // TODO: This is problematic for the consistency of reference type propagation
494 // because it can be created anytime after the pass and thus it will be left
495 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600496 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000497
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600498 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
499 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000500 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600501 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
502 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000503 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600504 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
505 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000506 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600507 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
508 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000509 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000510
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100511 HCurrentMethod* GetCurrentMethod();
512
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100513 const DexFile& GetDexFile() const {
514 return dex_file_;
515 }
516
517 uint32_t GetMethodIdx() const {
518 return method_idx_;
519 }
520
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100521 InvokeType GetInvokeType() const {
522 return invoke_type_;
523 }
524
Mark Mendellc4701932015-04-10 13:18:51 -0400525 InstructionSet GetInstructionSet() const {
526 return instruction_set_;
527 }
528
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000529 bool IsCompilingOsr() const { return osr_; }
530
David Brazdil77a48ae2015-09-15 12:34:04 +0000531 bool HasTryCatch() const { return has_try_catch_; }
532 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100533
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000534 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
535 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
536
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100537 ArtMethod* GetArtMethod() const { return art_method_; }
538 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
539
Mark Mendellf6529172015-11-17 11:16:56 -0500540 // Returns an instruction with the opposite boolean value from 'cond'.
541 // The instruction has been inserted into the graph, either as a constant, or
542 // before cursor.
543 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
544
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000545 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
546
David Brazdil2d7352b2015-04-20 14:52:42 +0100547 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000548 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100549 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000550
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000551 template <class InstructionType, typename ValueType>
552 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600553 ArenaSafeMap<ValueType, InstructionType*>* cache,
554 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000555 // Try to find an existing constant of the given value.
556 InstructionType* constant = nullptr;
557 auto cached_constant = cache->find(value);
558 if (cached_constant != cache->end()) {
559 constant = cached_constant->second;
560 }
561
562 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100563 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000564 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600565 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000566 cache->Overwrite(value, constant);
567 InsertConstant(constant);
568 }
569 return constant;
570 }
571
David Brazdil8d5b8b22015-03-24 10:51:52 +0000572 void InsertConstant(HConstant* instruction);
573
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000574 // Cache a float constant into the graph. This method should only be
575 // called by the SsaBuilder when creating "equivalent" instructions.
576 void CacheFloatConstant(HFloatConstant* constant);
577
578 // See CacheFloatConstant comment.
579 void CacheDoubleConstant(HDoubleConstant* constant);
580
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000581 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000582
583 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100584 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000585
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100586 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100587 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000588
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100589 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100590 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100591
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000592 HBasicBlock* entry_block_;
593 HBasicBlock* exit_block_;
594
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100595 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100596 uint16_t maximum_number_of_out_vregs_;
597
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100598 // The number of virtual registers in this method. Contains the parameters.
599 uint16_t number_of_vregs_;
600
601 // The number of virtual registers used by parameters of this method.
602 uint16_t number_of_in_vregs_;
603
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000604 // Number of vreg size slots that the temporaries use (used in baseline compiler).
605 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100606
Mark Mendell1152c922015-04-24 17:06:35 -0400607 // Has bounds checks. We can totally skip BCE if it's false.
608 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800609
David Brazdil77a48ae2015-09-15 12:34:04 +0000610 // Flag whether there are any try/catch blocks in the graph. We will skip
611 // try/catch-related passes if false.
612 bool has_try_catch_;
613
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000614 // Flag whether there are any irreducible loops in the graph.
615 bool has_irreducible_loops_;
616
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000617 // Indicates whether the graph should be compiled in a way that
618 // ensures full debuggability. If false, we can apply more
619 // aggressive optimizations that may limit the level of debugging.
620 const bool debuggable_;
621
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000622 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000623 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000624
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100625 // The dex file from which the method is from.
626 const DexFile& dex_file_;
627
628 // The method index in the dex file.
629 const uint32_t method_idx_;
630
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100631 // If inlined, this encodes how the callee is being invoked.
632 const InvokeType invoke_type_;
633
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100634 // Whether the graph has been transformed to SSA form. Only used
635 // in debug mode to ensure we are not using properties only valid
636 // for non-SSA form (like the number of temporaries).
637 bool in_ssa_form_;
638
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100639 const bool should_generate_constructor_barrier_;
640
Mathieu Chartiere401d142015-04-22 13:56:20 -0700641 const InstructionSet instruction_set_;
642
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000643 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000644 HNullConstant* cached_null_constant_;
645 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000646 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000647 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000648 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000649
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100650 HCurrentMethod* cached_current_method_;
651
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100652 // The ArtMethod this graph is for. Note that for AOT, it may be null,
653 // for example for methods whose declaring class could not be resolved
654 // (such as when the superclass could not be found).
655 ArtMethod* art_method_;
656
David Brazdil4833f5a2015-12-16 10:37:39 +0000657 // Keep the RTI of inexact Object to avoid having to pass stack handle
658 // collection pointer to passes which may create NullConstant.
659 ReferenceTypeInfo inexact_object_rti_;
660
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000661 // Whether we are compiling this graph for on stack replacement: this will
662 // make all loops seen as irreducible and emit special stack maps to mark
663 // compiled code entries which the interpreter can directly jump to.
664 const bool osr_;
665
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000666 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100667 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000668 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000669 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000670 DISALLOW_COPY_AND_ASSIGN(HGraph);
671};
672
Vladimir Markof9f64412015-09-02 14:05:49 +0100673class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000674 public:
675 HLoopInformation(HBasicBlock* header, HGraph* graph)
676 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100677 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000678 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100679 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100680 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100681 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000682 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100683 back_edges_.reserve(kDefaultNumberOfBackEdges);
684 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100685
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000686 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100687 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000688
689 void Dump(std::ostream& os);
690
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100691 HBasicBlock* GetHeader() const {
692 return header_;
693 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000694
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000695 void SetHeader(HBasicBlock* block) {
696 header_ = block;
697 }
698
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100699 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
700 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
701 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
702
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000703 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100704 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000705 }
706
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100707 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100708 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100709 }
710
David Brazdil46e2a392015-03-16 17:31:52 +0000711 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100712 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100713 }
714
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000715 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100716 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000717 }
718
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100719 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100720
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100721 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100722 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100723 }
724
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100725 // Returns the lifetime position of the back edge that has the
726 // greatest lifetime position.
727 size_t GetLifetimeEnd() const;
728
729 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100730 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100731 }
732
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000733 // Finds blocks that are part of this loop.
734 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100735
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100736 // Returns whether this loop information contains `block`.
737 // Note that this loop information *must* be populated before entering this function.
738 bool Contains(const HBasicBlock& block) const;
739
740 // Returns whether this loop information is an inner loop of `other`.
741 // Note that `other` *must* be populated before entering this function.
742 bool IsIn(const HLoopInformation& other) const;
743
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800744 // Returns true if instruction is not defined within this loop.
745 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700746
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100747 const ArenaBitVector& GetBlocks() const { return blocks_; }
748
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000749 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000750 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000751
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000752 void ClearAllBlocks() {
753 blocks_.ClearAllBits();
754 }
755
David Brazdil3f4a5222016-05-06 12:46:21 +0100756 bool HasBackEdgeNotDominatedByHeader() const;
757
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100758 bool IsPopulated() const {
759 return blocks_.GetHighestBitSet() != -1;
760 }
761
Anton Shaminf89381f2016-05-16 16:44:13 +0600762 bool DominatesAllBackEdges(HBasicBlock* block);
763
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000764 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100765 // Internal recursive implementation of `Populate`.
766 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100767 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100768
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000769 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100770 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000771 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100772 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100773 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100774 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000775
776 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
777};
778
David Brazdilec16f792015-08-19 15:04:01 +0100779// Stores try/catch information for basic blocks.
780// Note that HGraph is constructed so that catch blocks cannot simultaneously
781// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100782class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100783 public:
784 // Try block information constructor.
785 explicit TryCatchInformation(const HTryBoundary& try_entry)
786 : try_entry_(&try_entry),
787 catch_dex_file_(nullptr),
788 catch_type_index_(DexFile::kDexNoIndex16) {
789 DCHECK(try_entry_ != nullptr);
790 }
791
792 // Catch block information constructor.
793 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
794 : try_entry_(nullptr),
795 catch_dex_file_(&dex_file),
796 catch_type_index_(catch_type_index) {}
797
798 bool IsTryBlock() const { return try_entry_ != nullptr; }
799
800 const HTryBoundary& GetTryEntry() const {
801 DCHECK(IsTryBlock());
802 return *try_entry_;
803 }
804
805 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
806
807 bool IsCatchAllTypeIndex() const {
808 DCHECK(IsCatchBlock());
809 return catch_type_index_ == DexFile::kDexNoIndex16;
810 }
811
812 uint16_t GetCatchTypeIndex() const {
813 DCHECK(IsCatchBlock());
814 return catch_type_index_;
815 }
816
817 const DexFile& GetCatchDexFile() const {
818 DCHECK(IsCatchBlock());
819 return *catch_dex_file_;
820 }
821
822 private:
823 // One of possibly several TryBoundary instructions entering the block's try.
824 // Only set for try blocks.
825 const HTryBoundary* try_entry_;
826
827 // Exception type information. Only set for catch blocks.
828 const DexFile* catch_dex_file_;
829 const uint16_t catch_type_index_;
830};
831
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100832static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100833static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100834
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000835// A block in a method. Contains the list of instructions represented
836// as a double linked list. Each block knows its predecessors and
837// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100838
Vladimir Markof9f64412015-09-02 14:05:49 +0100839class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000840 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700841 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000842 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000843 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
844 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000845 loop_information_(nullptr),
846 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000847 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100848 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100849 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100850 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000851 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000852 try_catch_information_(nullptr) {
853 predecessors_.reserve(kDefaultNumberOfPredecessors);
854 successors_.reserve(kDefaultNumberOfSuccessors);
855 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
856 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000857
Vladimir Marko60584552015-09-03 13:35:12 +0000858 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100859 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000860 }
861
Vladimir Marko60584552015-09-03 13:35:12 +0000862 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100863 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000864 }
865
David Brazdild26a4112015-11-10 11:07:31 +0000866 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
867 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
868
Vladimir Marko60584552015-09-03 13:35:12 +0000869 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
870 return ContainsElement(successors_, block, start_from);
871 }
872
873 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100874 return dominated_blocks_;
875 }
876
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100877 bool IsEntryBlock() const {
878 return graph_->GetEntryBlock() == this;
879 }
880
881 bool IsExitBlock() const {
882 return graph_->GetExitBlock() == this;
883 }
884
David Brazdil46e2a392015-03-16 17:31:52 +0000885 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000886 bool IsSingleTryBoundary() const;
887
888 // Returns true if this block emits nothing but a jump.
889 bool IsSingleJump() const {
890 HLoopInformation* loop_info = GetLoopInformation();
891 return (IsSingleGoto() || IsSingleTryBoundary())
892 // Back edges generate a suspend check.
893 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
894 }
David Brazdil46e2a392015-03-16 17:31:52 +0000895
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000896 void AddBackEdge(HBasicBlock* back_edge) {
897 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000898 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000899 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100900 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000901 loop_information_->AddBackEdge(back_edge);
902 }
903
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000904 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000905 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000906
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100907 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000908 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600909 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000910
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000911 HBasicBlock* GetDominator() const { return dominator_; }
912 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000913 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
914
915 void RemoveDominatedBlock(HBasicBlock* block) {
916 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100917 }
Vladimir Marko60584552015-09-03 13:35:12 +0000918
919 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
920 ReplaceElement(dominated_blocks_, existing, new_block);
921 }
922
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100923 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000924
925 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100926 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000927 }
928
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100929 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
930 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100931 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100932 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100933 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
934 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000935
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000936 HInstruction* GetFirstInstructionDisregardMoves() const;
937
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000938 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000939 successors_.push_back(block);
940 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000941 }
942
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100943 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
944 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100945 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000946 new_block->predecessors_.push_back(this);
947 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000948 }
949
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000950 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
951 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000952 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000953 new_block->successors_.push_back(this);
954 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000955 }
956
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100957 // Insert `this` between `predecessor` and `successor. This method
958 // preserves the indicies, and will update the first edge found between
959 // `predecessor` and `successor`.
960 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
961 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100962 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000963 successor->predecessors_[predecessor_index] = this;
964 predecessor->successors_[successor_index] = this;
965 successors_.push_back(successor);
966 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100967 }
968
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100969 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000970 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100971 }
972
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000973 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000974 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000975 }
976
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100977 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000978 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100979 }
980
981 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000982 predecessors_.push_back(block);
983 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100984 }
985
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100986 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000987 DCHECK_EQ(predecessors_.size(), 2u);
988 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100989 }
990
David Brazdil769c9e52015-04-27 13:54:09 +0100991 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000992 DCHECK_EQ(successors_.size(), 2u);
993 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100994 }
995
David Brazdilfc6a86a2015-06-26 10:33:45 +0000996 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000997 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100998 }
999
David Brazdilfc6a86a2015-06-26 10:33:45 +00001000 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001001 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001002 }
1003
David Brazdilfc6a86a2015-06-26 10:33:45 +00001004 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001005 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001006 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001007 }
1008
1009 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001010 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001011 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001012 }
1013
1014 // Returns whether the first occurrence of `predecessor` in the list of
1015 // predecessors is at index `idx`.
1016 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001017 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001018 return GetPredecessorIndexOf(predecessor) == idx;
1019 }
1020
David Brazdild7558da2015-09-22 13:04:14 +01001021 // Create a new block between this block and its predecessors. The new block
1022 // is added to the graph, all predecessor edges are relinked to it and an edge
1023 // is created to `this`. Returns the new empty block. Reverse post order or
1024 // loop and try/catch information are not updated.
1025 HBasicBlock* CreateImmediateDominator();
1026
David Brazdilfc6a86a2015-06-26 10:33:45 +00001027 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001028 // created, latter block. Note that this method will add the block to the
1029 // graph, create a Goto at the end of the former block and will create an edge
1030 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001031 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001032 HBasicBlock* SplitBefore(HInstruction* cursor);
1033
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001034 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001035 // created block. Note that this method just updates raw block information,
1036 // like predecessors, successors, dominators, and instruction list. It does not
1037 // update the graph, reverse post order, loop information, nor make sure the
1038 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001039 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1040
1041 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1042 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001043
1044 // Merge `other` at the end of `this`. Successors and dominated blocks of
1045 // `other` are changed to be successors and dominated blocks of `this`. Note
1046 // that this method does not update the graph, reverse post order, loop
1047 // information, nor make sure the blocks are consistent (for example ending
1048 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001049 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001050
1051 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1052 // of `this` are moved to `other`.
1053 // Note that this method does not update the graph, reverse post order, loop
1054 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001055 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001056 void ReplaceWith(HBasicBlock* other);
1057
David Brazdil2d7352b2015-04-20 14:52:42 +01001058 // Merge `other` at the end of `this`. This method updates loops, reverse post
1059 // order, links to predecessors, successors, dominators and deletes the block
1060 // from the graph. The two blocks must be successive, i.e. `this` the only
1061 // predecessor of `other` and vice versa.
1062 void MergeWith(HBasicBlock* other);
1063
1064 // Disconnects `this` from all its predecessors, successors and dominator,
1065 // removes it from all loops it is included in and eventually from the graph.
1066 // The block must not dominate any other block. Predecessors and successors
1067 // are safely updated.
1068 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001069
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001070 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001071 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001072 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001073 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001074 // Replace instruction `initial` with `replacement` within this block.
1075 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1076 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001077 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001078 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001079 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1080 // instruction list. With 'ensure_safety' set to true, it verifies that the
1081 // instruction is not in use and removes it from the use lists of its inputs.
1082 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1083 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001084 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001085
1086 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001087 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001088 }
1089
Roland Levillain6b879dd2014-09-22 17:13:44 +01001090 bool IsLoopPreHeaderFirstPredecessor() const {
1091 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001092 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001093 }
1094
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001095 bool IsFirstPredecessorBackEdge() const {
1096 DCHECK(IsLoopHeader());
1097 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1098 }
1099
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001100 HLoopInformation* GetLoopInformation() const {
1101 return loop_information_;
1102 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001103
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001104 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001105 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001106 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001107 void SetInLoop(HLoopInformation* info) {
1108 if (IsLoopHeader()) {
1109 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001110 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001111 loop_information_ = info;
1112 } else if (loop_information_->Contains(*info->GetHeader())) {
1113 // Block is currently part of an outer loop. Make it part of this inner loop.
1114 // Note that a non loop header having a loop information means this loop information
1115 // has already been populated
1116 loop_information_ = info;
1117 } else {
1118 // Block is part of an inner loop. Do not update the loop information.
1119 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1120 // at this point, because this method is being called while populating `info`.
1121 }
1122 }
1123
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001124 // Raw update of the loop information.
1125 void SetLoopInformation(HLoopInformation* info) {
1126 loop_information_ = info;
1127 }
1128
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001129 bool IsInLoop() const { return loop_information_ != nullptr; }
1130
David Brazdilec16f792015-08-19 15:04:01 +01001131 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1132
1133 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1134 try_catch_information_ = try_catch_information;
1135 }
1136
1137 bool IsTryBlock() const {
1138 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1139 }
1140
1141 bool IsCatchBlock() const {
1142 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1143 }
David Brazdilffee3d32015-07-06 11:48:53 +01001144
1145 // Returns the try entry that this block's successors should have. They will
1146 // be in the same try, unless the block ends in a try boundary. In that case,
1147 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001148 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001149
David Brazdild7558da2015-09-22 13:04:14 +01001150 bool HasThrowingInstructions() const;
1151
David Brazdila4b8c212015-05-07 09:59:30 +01001152 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001153 bool Dominates(HBasicBlock* block) const;
1154
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001155 size_t GetLifetimeStart() const { return lifetime_start_; }
1156 size_t GetLifetimeEnd() const { return lifetime_end_; }
1157
1158 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1159 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1160
David Brazdil8d5b8b22015-03-24 10:51:52 +00001161 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001162 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001163 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001164 bool HasSinglePhi() const;
1165
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001166 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001167 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001168 ArenaVector<HBasicBlock*> predecessors_;
1169 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001170 HInstructionList instructions_;
1171 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001172 HLoopInformation* loop_information_;
1173 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001174 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001175 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001176 // The dex program counter of the first instruction of this block.
1177 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001178 size_t lifetime_start_;
1179 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001180 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001181
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001182 friend class HGraph;
1183 friend class HInstruction;
1184
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001185 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1186};
1187
David Brazdilb2bd1c52015-03-25 11:17:37 +00001188// Iterates over the LoopInformation of all loops which contain 'block'
1189// from the innermost to the outermost.
1190class HLoopInformationOutwardIterator : public ValueObject {
1191 public:
1192 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1193 : current_(block.GetLoopInformation()) {}
1194
1195 bool Done() const { return current_ == nullptr; }
1196
1197 void Advance() {
1198 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001199 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001200 }
1201
1202 HLoopInformation* Current() const {
1203 DCHECK(!Done());
1204 return current_;
1205 }
1206
1207 private:
1208 HLoopInformation* current_;
1209
1210 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1211};
1212
Alexandre Ramesef20f712015-06-09 10:29:30 +01001213#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001214 M(Above, Condition) \
1215 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001216 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001217 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001218 M(ArrayGet, Instruction) \
1219 M(ArrayLength, Instruction) \
1220 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001221 M(Below, Condition) \
1222 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001223 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001224 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001225 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001226 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001227 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001228 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001229 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001230 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001231 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001232 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001233 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001234 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001235 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001236 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001237 M(Exit, Instruction) \
1238 M(FloatConstant, Constant) \
1239 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001240 M(GreaterThan, Condition) \
1241 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001242 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001243 M(InstanceFieldGet, Instruction) \
1244 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001245 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001246 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001247 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001248 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001249 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001250 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001251 M(LessThan, Condition) \
1252 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001253 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001254 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001255 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001256 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001257 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001258 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001259 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001260 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001261 M(Neg, UnaryOperation) \
1262 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001263 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001264 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001265 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001266 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001267 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001268 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001269 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001270 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001271 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001272 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001273 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001274 M(Return, Instruction) \
1275 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001276 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001277 M(Shl, BinaryOperation) \
1278 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001279 M(StaticFieldGet, Instruction) \
1280 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001281 M(UnresolvedInstanceFieldGet, Instruction) \
1282 M(UnresolvedInstanceFieldSet, Instruction) \
1283 M(UnresolvedStaticFieldGet, Instruction) \
1284 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001285 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001286 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001287 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001288 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001289 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001290 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001291 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001292 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001293
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001294/*
1295 * Instructions, shared across several (not all) architectures.
1296 */
1297#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1298#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1299#else
1300#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001301 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001302 M(MultiplyAccumulate, Instruction) \
1303 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001304#endif
1305
Vladimir Markob4536b72015-11-24 13:45:23 +00001306#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001307#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001308#else
1309#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1310 M(ArmDexCacheArraysBase, Instruction)
1311#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001312
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001313#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001314#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001315#else
1316#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001317 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001318#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001319
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001320#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001321#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001322#else
1323#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1324 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001325 M(MipsDexCacheArraysBase, Instruction) \
1326 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001327#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001328
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001329#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1330
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001331#ifndef ART_ENABLE_CODEGEN_x86
1332#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1333#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001334#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1335 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001336 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001337 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001338 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001339#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001340
1341#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1342
1343#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1344 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001345 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001346 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1347 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001348 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001349 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001350 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1351 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1352
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001353#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1354 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001355 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001356 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001357 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001358 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001359
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001360#define FOR_EACH_INSTRUCTION(M) \
1361 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1362 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1363
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001364#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001365FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1366#undef FORWARD_DECLARATION
1367
Vladimir Marko372f10e2016-05-17 16:30:10 +01001368#define DECLARE_INSTRUCTION(type) \
1369 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1370 const char* DebugName() const OVERRIDE { return #type; } \
1371 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1372 return other->Is##type(); \
1373 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001374 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001375
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001376#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1377 bool Is##type() const { return As##type() != nullptr; } \
1378 const H##type* As##type() const { return this; } \
1379 H##type* As##type() { return this; }
1380
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001381template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001382class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001383 public:
David Brazdiled596192015-01-23 10:39:45 +00001384 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001385 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001386 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001387
Vladimir Marko46817b82016-03-29 12:21:58 +01001388 // Hook for the IntrusiveForwardList<>.
1389 // TODO: Hide this better.
1390 IntrusiveForwardListHook hook;
1391
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001392 private:
David Brazdiled596192015-01-23 10:39:45 +00001393 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001394 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001395
1396 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001397 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001398
Vladimir Marko46817b82016-03-29 12:21:58 +01001399 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001400
1401 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1402};
1403
David Brazdiled596192015-01-23 10:39:45 +00001404template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001405using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001406
David Brazdil1abb4192015-02-17 18:33:36 +00001407// This class is used by HEnvironment and HInstruction classes to record the
1408// instructions they use and pointers to the corresponding HUseListNodes kept
1409// by the used instructions.
1410template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001411class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001412 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001413 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1414 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001415
Vladimir Marko46817b82016-03-29 12:21:58 +01001416 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1417 : HUserRecord(old_record.instruction_, before_use_node) {}
1418 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1419 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001420 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001421 }
1422
1423 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001424 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1425 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001426
1427 private:
1428 // Instruction used by the user.
1429 HInstruction* instruction_;
1430
Vladimir Marko46817b82016-03-29 12:21:58 +01001431 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1432 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001433};
1434
Vladimir Markoe9004912016-06-16 16:50:52 +01001435// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1436// This is used for HInstruction::GetInputs() to return a container wrapper providing
1437// HInstruction* values even though the underlying container has HUserRecord<>s.
1438struct HInputExtractor {
1439 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1440 return record.GetInstruction();
1441 }
1442 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1443 return record.GetInstruction();
1444 }
1445};
1446
1447using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1448using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1449
Aart Bik854a02b2015-07-14 16:07:00 -07001450/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001451 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001452 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001453 * For write/read dependences on fields/arrays, the dependence analysis uses
1454 * type disambiguation (e.g. a float field write cannot modify the value of an
1455 * integer field read) and the access type (e.g. a reference array write cannot
1456 * modify the value of a reference field read [although it may modify the
1457 * reference fetch prior to reading the field, which is represented by its own
1458 * write/read dependence]). The analysis makes conservative points-to
1459 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1460 * the same, and any reference read depends on any reference read without
1461 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001462 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001463 * The internal representation uses 38-bit and is described in the table below.
1464 * The first line indicates the side effect, and for field/array accesses the
1465 * second line indicates the type of the access (in the order of the
1466 * Primitive::Type enum).
1467 * The two numbered lines below indicate the bit position in the bitfield (read
1468 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001469 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001470 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1471 * +-------------+---------+---------+--------------+---------+---------+
1472 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1473 * | 3 |333333322|222222221| 1 |111111110|000000000|
1474 * | 7 |654321098|765432109| 8 |765432109|876543210|
1475 *
1476 * Note that, to ease the implementation, 'changes' bits are least significant
1477 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001478 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001479class SideEffects : public ValueObject {
1480 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001481 SideEffects() : flags_(0) {}
1482
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001483 static SideEffects None() {
1484 return SideEffects(0);
1485 }
1486
1487 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001488 return SideEffects(kAllChangeBits | kAllDependOnBits);
1489 }
1490
1491 static SideEffects AllChanges() {
1492 return SideEffects(kAllChangeBits);
1493 }
1494
1495 static SideEffects AllDependencies() {
1496 return SideEffects(kAllDependOnBits);
1497 }
1498
1499 static SideEffects AllExceptGCDependency() {
1500 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1501 }
1502
1503 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001504 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001505 }
1506
Aart Bik34c3ba92015-07-20 14:08:59 -07001507 static SideEffects AllWrites() {
1508 return SideEffects(kAllWrites);
1509 }
1510
1511 static SideEffects AllReads() {
1512 return SideEffects(kAllReads);
1513 }
1514
1515 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1516 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001517 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001518 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001519 }
1520
Aart Bik854a02b2015-07-14 16:07:00 -07001521 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001522 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001523 }
1524
Aart Bik34c3ba92015-07-20 14:08:59 -07001525 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1526 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001527 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001528 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001529 }
1530
1531 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001532 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001533 }
1534
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001535 static SideEffects CanTriggerGC() {
1536 return SideEffects(1ULL << kCanTriggerGCBit);
1537 }
1538
1539 static SideEffects DependsOnGC() {
1540 return SideEffects(1ULL << kDependsOnGCBit);
1541 }
1542
Aart Bik854a02b2015-07-14 16:07:00 -07001543 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001544 SideEffects Union(SideEffects other) const {
1545 return SideEffects(flags_ | other.flags_);
1546 }
1547
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001548 SideEffects Exclusion(SideEffects other) const {
1549 return SideEffects(flags_ & ~other.flags_);
1550 }
1551
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001552 void Add(SideEffects other) {
1553 flags_ |= other.flags_;
1554 }
1555
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001556 bool Includes(SideEffects other) const {
1557 return (other.flags_ & flags_) == other.flags_;
1558 }
1559
1560 bool HasSideEffects() const {
1561 return (flags_ & kAllChangeBits);
1562 }
1563
1564 bool HasDependencies() const {
1565 return (flags_ & kAllDependOnBits);
1566 }
1567
1568 // Returns true if there are no side effects or dependencies.
1569 bool DoesNothing() const {
1570 return flags_ == 0;
1571 }
1572
Aart Bik854a02b2015-07-14 16:07:00 -07001573 // Returns true if something is written.
1574 bool DoesAnyWrite() const {
1575 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001576 }
1577
Aart Bik854a02b2015-07-14 16:07:00 -07001578 // Returns true if something is read.
1579 bool DoesAnyRead() const {
1580 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001581 }
1582
Aart Bik854a02b2015-07-14 16:07:00 -07001583 // Returns true if potentially everything is written and read
1584 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001585 bool DoesAllReadWrite() const {
1586 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1587 }
1588
Aart Bik854a02b2015-07-14 16:07:00 -07001589 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001590 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001591 }
1592
Roland Levillain0d5a2812015-11-13 10:07:31 +00001593 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001594 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001595 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1596 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001597 }
1598
1599 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001600 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001601 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001602 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001603 for (int s = kLastBit; s >= 0; s--) {
1604 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1605 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1606 // This is a bit for the GC side effect.
1607 if (current_bit_is_set) {
1608 flags += "GC";
1609 }
Aart Bik854a02b2015-07-14 16:07:00 -07001610 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001611 } else {
1612 // This is a bit for the array/field analysis.
1613 // The underscore character stands for the 'can trigger GC' bit.
1614 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1615 if (current_bit_is_set) {
1616 flags += kDebug[s];
1617 }
1618 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1619 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1620 flags += "|";
1621 }
1622 }
Aart Bik854a02b2015-07-14 16:07:00 -07001623 }
1624 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001625 }
1626
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001627 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001628
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001629 private:
1630 static constexpr int kFieldArrayAnalysisBits = 9;
1631
1632 static constexpr int kFieldWriteOffset = 0;
1633 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1634 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1635 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1636
1637 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1638
1639 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1640 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1641 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1642 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1643
1644 static constexpr int kLastBit = kDependsOnGCBit;
1645 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1646
1647 // Aliases.
1648
1649 static_assert(kChangeBits == kDependOnBits,
1650 "the 'change' bits should match the 'depend on' bits.");
1651
1652 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1653 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1654 static constexpr uint64_t kAllWrites =
1655 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1656 static constexpr uint64_t kAllReads =
1657 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001658
Aart Bik854a02b2015-07-14 16:07:00 -07001659 // Translates type to bit flag.
1660 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1661 CHECK_NE(type, Primitive::kPrimVoid);
1662 const uint64_t one = 1;
1663 const int shift = type; // 0-based consecutive enum
1664 DCHECK_LE(kFieldWriteOffset, shift);
1665 DCHECK_LT(shift, kArrayWriteOffset);
1666 return one << (type + offset);
1667 }
1668
1669 // Private constructor on direct flags value.
1670 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1671
1672 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001673};
1674
David Brazdiled596192015-01-23 10:39:45 +00001675// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001676class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001677 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001678 HEnvironment(ArenaAllocator* arena,
1679 size_t number_of_vregs,
1680 const DexFile& dex_file,
1681 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001682 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001683 InvokeType invoke_type,
1684 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001685 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1686 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001687 parent_(nullptr),
1688 dex_file_(dex_file),
1689 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001690 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001691 invoke_type_(invoke_type),
1692 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001693 }
1694
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001695 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001696 : HEnvironment(arena,
1697 to_copy.Size(),
1698 to_copy.GetDexFile(),
1699 to_copy.GetMethodIdx(),
1700 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001701 to_copy.GetInvokeType(),
1702 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001703
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001704 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001705 if (parent_ != nullptr) {
1706 parent_->SetAndCopyParentChain(allocator, parent);
1707 } else {
1708 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1709 parent_->CopyFrom(parent);
1710 if (parent->GetParent() != nullptr) {
1711 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1712 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001713 }
David Brazdiled596192015-01-23 10:39:45 +00001714 }
1715
Vladimir Marko71bf8092015-09-15 15:33:14 +01001716 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001717 void CopyFrom(HEnvironment* environment);
1718
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001719 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1720 // input to the loop phi instead. This is for inserting instructions that
1721 // require an environment (like HDeoptimization) in the loop pre-header.
1722 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001723
1724 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001725 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001726 }
1727
1728 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001729 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001730 }
1731
David Brazdil1abb4192015-02-17 18:33:36 +00001732 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001733
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001734 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001735
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001736 HEnvironment* GetParent() const { return parent_; }
1737
1738 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001739 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001740 }
1741
1742 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001743 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001744 }
1745
1746 uint32_t GetDexPc() const {
1747 return dex_pc_;
1748 }
1749
1750 uint32_t GetMethodIdx() const {
1751 return method_idx_;
1752 }
1753
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001754 InvokeType GetInvokeType() const {
1755 return invoke_type_;
1756 }
1757
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001758 const DexFile& GetDexFile() const {
1759 return dex_file_;
1760 }
1761
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001762 HInstruction* GetHolder() const {
1763 return holder_;
1764 }
1765
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001766
1767 bool IsFromInlinedInvoke() const {
1768 return GetParent() != nullptr;
1769 }
1770
David Brazdiled596192015-01-23 10:39:45 +00001771 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001772 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1773 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001774 HEnvironment* parent_;
1775 const DexFile& dex_file_;
1776 const uint32_t method_idx_;
1777 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001778 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001779
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001780 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001781 HInstruction* const holder_;
1782
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001783 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001784
1785 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1786};
1787
Vladimir Markof9f64412015-09-02 14:05:49 +01001788class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001789 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001790 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001791 : previous_(nullptr),
1792 next_(nullptr),
1793 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001794 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001795 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001796 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001797 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001798 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001799 locations_(nullptr),
1800 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001801 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001802 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001803 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1804 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1805 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001806
Dave Allison20dfc792014-06-16 20:44:29 -07001807 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001808
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001809#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001810 enum InstructionKind {
1811 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1812 };
1813#undef DECLARE_KIND
1814
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001815 HInstruction* GetNext() const { return next_; }
1816 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001817
Calin Juravle77520bc2015-01-12 18:45:46 +00001818 HInstruction* GetNextDisregardingMoves() const;
1819 HInstruction* GetPreviousDisregardingMoves() const;
1820
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001821 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001822 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001823 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001824 bool IsInBlock() const { return block_ != nullptr; }
1825 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001826 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1827 bool IsIrreducibleLoopHeaderPhi() const {
1828 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1829 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001830
Vladimir Marko372f10e2016-05-17 16:30:10 +01001831 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1832
1833 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1834 // One virtual method is enough, just const_cast<> and then re-add the const.
1835 return ArrayRef<const HUserRecord<HInstruction*>>(
1836 const_cast<HInstruction*>(this)->GetInputRecords());
1837 }
1838
Vladimir Markoe9004912016-06-16 16:50:52 +01001839 HInputsRef GetInputs() {
1840 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001841 }
1842
Vladimir Markoe9004912016-06-16 16:50:52 +01001843 HConstInputsRef GetInputs() const {
1844 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001845 }
1846
1847 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001848 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001849
Vladimir Marko372f10e2016-05-17 16:30:10 +01001850 void SetRawInputAt(size_t index, HInstruction* input) {
1851 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1852 }
1853
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001854 virtual void Accept(HGraphVisitor* visitor) = 0;
1855 virtual const char* DebugName() const = 0;
1856
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001857 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1858
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001859 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001860
1861 uint32_t GetDexPc() const { return dex_pc_; }
1862
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001863 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001864
Roland Levillaine161a2a2014-10-03 12:45:18 +01001865 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001866 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001867
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001868 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001869 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001870
Calin Juravle10e244f2015-01-26 18:54:32 +00001871 // Does not apply for all instructions, but having this at top level greatly
1872 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001873 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001874 virtual bool CanBeNull() const {
1875 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1876 return true;
1877 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001878
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001879 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001880 return false;
1881 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001882
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001883 virtual bool IsActualObject() const {
1884 return GetType() == Primitive::kPrimNot;
1885 }
1886
Calin Juravle2e768302015-07-28 14:41:11 +00001887 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001888
Calin Juravle61d544b2015-02-23 16:46:57 +00001889 ReferenceTypeInfo GetReferenceTypeInfo() const {
1890 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001891 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001892 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001893 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001894
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001895 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001896 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001897 // Note: fixup_end remains valid across push_front().
1898 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1899 HUseListNode<HInstruction*>* new_node =
1900 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1901 uses_.push_front(*new_node);
1902 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001903 }
1904
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001905 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001906 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001907 // Note: env_fixup_end remains valid across push_front().
1908 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1909 HUseListNode<HEnvironment*>* new_node =
1910 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1911 env_uses_.push_front(*new_node);
1912 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001913 }
1914
David Brazdil1abb4192015-02-17 18:33:36 +00001915 void RemoveAsUserOfInput(size_t input) {
1916 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001917 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1918 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1919 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001920 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001921
Vladimir Marko372f10e2016-05-17 16:30:10 +01001922 void RemoveAsUserOfAllInputs() {
1923 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1924 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1925 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1926 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1927 }
1928 }
1929
David Brazdil1abb4192015-02-17 18:33:36 +00001930 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1931 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001932
Vladimir Marko46817b82016-03-29 12:21:58 +01001933 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1934 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1935 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001936 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001937 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001938 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001939
Roland Levillain6c82d402014-10-13 16:10:27 +01001940 // Does this instruction strictly dominate `other_instruction`?
1941 // Returns false if this instruction and `other_instruction` are the same.
1942 // Aborts if this instruction and `other_instruction` are both phis.
1943 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001944
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001945 int GetId() const { return id_; }
1946 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001947
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001948 int GetSsaIndex() const { return ssa_index_; }
1949 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1950 bool HasSsaIndex() const { return ssa_index_ != -1; }
1951
1952 bool HasEnvironment() const { return environment_ != nullptr; }
1953 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001954 // Set the `environment_` field. Raw because this method does not
1955 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001956 void SetRawEnvironment(HEnvironment* environment) {
1957 DCHECK(environment_ == nullptr);
1958 DCHECK_EQ(environment->GetHolder(), this);
1959 environment_ = environment;
1960 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001961
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001962 void InsertRawEnvironment(HEnvironment* environment) {
1963 DCHECK(environment_ != nullptr);
1964 DCHECK_EQ(environment->GetHolder(), this);
1965 DCHECK(environment->GetParent() == nullptr);
1966 environment->parent_ = environment_;
1967 environment_ = environment;
1968 }
1969
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001970 void RemoveEnvironment();
1971
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001972 // Set the environment of this instruction, copying it from `environment`. While
1973 // copying, the uses lists are being updated.
1974 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001975 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001976 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001977 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001978 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001979 if (environment->GetParent() != nullptr) {
1980 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1981 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001982 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001983
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001984 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1985 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001986 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001987 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001988 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001989 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001990 if (environment->GetParent() != nullptr) {
1991 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1992 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001993 }
1994
Nicolas Geoffray39468442014-09-02 15:17:15 +01001995 // Returns the number of entries in the environment. Typically, that is the
1996 // number of dex registers in a method. It could be more in case of inlining.
1997 size_t EnvironmentSize() const;
1998
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001999 LocationSummary* GetLocations() const { return locations_; }
2000 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002001
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002002 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002003 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002004
Alexandre Rames188d4312015-04-09 18:30:21 +01002005 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2006 // uses of this instruction by `other` are *not* updated.
2007 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2008 ReplaceWith(other);
2009 other->ReplaceInput(this, use_index);
2010 }
2011
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002012 // Move `this` instruction before `cursor`.
2013 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002014
Vladimir Markofb337ea2015-11-25 15:25:10 +00002015 // Move `this` before its first user and out of any loops. If there is no
2016 // out-of-loop user that dominates all other users, move the instruction
2017 // to the end of the out-of-loop common dominator of the user's blocks.
2018 //
2019 // This can be used only on non-throwing instructions with no side effects that
2020 // have at least one use but no environment uses.
2021 void MoveBeforeFirstUserAndOutOfLoops();
2022
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002023#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002024 bool Is##type() const; \
2025 const H##type* As##type() const; \
2026 H##type* As##type();
2027
2028 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2029#undef INSTRUCTION_TYPE_CHECK
2030
2031#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002032 bool Is##type() const { return (As##type() != nullptr); } \
2033 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002034 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002035 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002036#undef INSTRUCTION_TYPE_CHECK
2037
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002038 // Returns whether the instruction can be moved within the graph.
2039 virtual bool CanBeMoved() const { return false; }
2040
2041 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002042 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002043 return false;
2044 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002045
2046 // Returns whether any data encoded in the two instructions is equal.
2047 // This method does not look at the inputs. Both instructions must be
2048 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002049 virtual bool InstructionDataEquals(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 two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002054 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002055 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002056 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002057
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002058 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2059 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2060 // the virtual function because the __attribute__((__pure__)) doesn't really
2061 // apply the strong requirement for virtual functions, preventing optimizations.
2062 InstructionKind GetKind() const PURE;
2063 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002064
2065 virtual size_t ComputeHashCode() const {
2066 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002067 for (const HInstruction* input : GetInputs()) {
2068 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002069 }
2070 return result;
2071 }
2072
2073 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002074 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002075 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002076
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002077 size_t GetLifetimePosition() const { return lifetime_position_; }
2078 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2079 LiveInterval* GetLiveInterval() const { return live_interval_; }
2080 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2081 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2082
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002083 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2084
2085 // Returns whether the code generation of the instruction will require to have access
2086 // to the current method. Such instructions are:
2087 // (1): Instructions that require an environment, as calling the runtime requires
2088 // to walk the stack and have the current method stored at a specific stack address.
2089 // (2): Object literals like classes and strings, that are loaded from the dex cache
2090 // fields of the current method.
2091 bool NeedsCurrentMethod() const {
2092 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2093 }
2094
Vladimir Markodc151b22015-10-15 18:02:30 +01002095 // Returns whether the code generation of the instruction will require to have access
2096 // to the dex cache of the current method's declaring class via the current method.
2097 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002098
Mark Mendellc4701932015-04-10 13:18:51 -04002099 // Does this instruction have any use in an environment before
2100 // control flow hits 'other'?
2101 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2102
2103 // Remove all references to environment uses of this instruction.
2104 // The caller must ensure that this is safe to do.
2105 void RemoveEnvironmentUsers();
2106
Vladimir Markoa1de9182016-02-25 11:37:38 +00002107 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2108 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002109
David Brazdil1abb4192015-02-17 18:33:36 +00002110 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002111 // If set, the machine code for this instruction is assumed to be generated by
2112 // its users. Used by liveness analysis to compute use positions accordingly.
2113 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2114 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2115 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2116 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2117
Vladimir Marko372f10e2016-05-17 16:30:10 +01002118 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2119 return GetInputRecords()[i];
2120 }
2121
2122 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2123 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2124 input_records[index] = input;
2125 }
David Brazdil1abb4192015-02-17 18:33:36 +00002126
Vladimir Markoa1de9182016-02-25 11:37:38 +00002127 uint32_t GetPackedFields() const {
2128 return packed_fields_;
2129 }
2130
2131 template <size_t flag>
2132 bool GetPackedFlag() const {
2133 return (packed_fields_ & (1u << flag)) != 0u;
2134 }
2135
2136 template <size_t flag>
2137 void SetPackedFlag(bool value = true) {
2138 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2139 }
2140
2141 template <typename BitFieldType>
2142 typename BitFieldType::value_type GetPackedField() const {
2143 return BitFieldType::Decode(packed_fields_);
2144 }
2145
2146 template <typename BitFieldType>
2147 void SetPackedField(typename BitFieldType::value_type value) {
2148 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2149 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2150 }
2151
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002152 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002153 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2154 auto before_use_node = uses_.before_begin();
2155 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2156 HInstruction* user = use_node->GetUser();
2157 size_t input_index = use_node->GetIndex();
2158 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2159 before_use_node = use_node;
2160 }
2161 }
2162
2163 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2164 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2165 if (next != uses_.end()) {
2166 HInstruction* next_user = next->GetUser();
2167 size_t next_index = next->GetIndex();
2168 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2169 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2170 }
2171 }
2172
2173 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2174 auto before_env_use_node = env_uses_.before_begin();
2175 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2176 HEnvironment* user = env_use_node->GetUser();
2177 size_t input_index = env_use_node->GetIndex();
2178 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2179 before_env_use_node = env_use_node;
2180 }
2181 }
2182
2183 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2184 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2185 if (next != env_uses_.end()) {
2186 HEnvironment* next_user = next->GetUser();
2187 size_t next_index = next->GetIndex();
2188 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2189 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2190 }
2191 }
David Brazdil1abb4192015-02-17 18:33:36 +00002192
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002193 HInstruction* previous_;
2194 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002195 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002196 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002197
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002198 // An instruction gets an id when it is added to the graph.
2199 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002200 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002201 int id_;
2202
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002203 // When doing liveness analysis, instructions that have uses get an SSA index.
2204 int ssa_index_;
2205
Vladimir Markoa1de9182016-02-25 11:37:38 +00002206 // Packed fields.
2207 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002208
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002209 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002210 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002211
2212 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002213 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002214
Nicolas Geoffray39468442014-09-02 15:17:15 +01002215 // The environment associated with this instruction. Not null if the instruction
2216 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002217 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002218
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002219 // Set by the code generator.
2220 LocationSummary* locations_;
2221
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002222 // Set by the liveness analysis.
2223 LiveInterval* live_interval_;
2224
2225 // Set by the liveness analysis, this is the position in a linear
2226 // order of blocks where this instruction's live interval start.
2227 size_t lifetime_position_;
2228
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002229 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002230
Vladimir Markoa1de9182016-02-25 11:37:38 +00002231 // The reference handle part of the reference type info.
2232 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002233 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002234 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002235
David Brazdil1abb4192015-02-17 18:33:36 +00002236 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002237 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002238 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002239 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002240 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002241
2242 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2243};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002244std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002245
2246class HInstructionIterator : public ValueObject {
2247 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002248 explicit HInstructionIterator(const HInstructionList& instructions)
2249 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002250 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002251 }
2252
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002253 bool Done() const { return instruction_ == nullptr; }
2254 HInstruction* Current() const { return instruction_; }
2255 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002256 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002257 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002258 }
2259
2260 private:
2261 HInstruction* instruction_;
2262 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002263
2264 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002265};
2266
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002267class HBackwardInstructionIterator : public ValueObject {
2268 public:
2269 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2270 : instruction_(instructions.last_instruction_) {
2271 next_ = Done() ? nullptr : instruction_->GetPrevious();
2272 }
2273
2274 bool Done() const { return instruction_ == nullptr; }
2275 HInstruction* Current() const { return instruction_; }
2276 void Advance() {
2277 instruction_ = next_;
2278 next_ = Done() ? nullptr : instruction_->GetPrevious();
2279 }
2280
2281 private:
2282 HInstruction* instruction_;
2283 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002284
2285 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002286};
2287
Vladimir Markof9f64412015-09-02 14:05:49 +01002288template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002289class HTemplateInstruction: public HInstruction {
2290 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002291 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002292 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002293 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002294
Vladimir Marko372f10e2016-05-17 16:30:10 +01002295 using HInstruction::GetInputRecords; // Keep the const version visible.
2296 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2297 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002298 }
2299
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002300 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002301 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002302
2303 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002304};
2305
Vladimir Markof9f64412015-09-02 14:05:49 +01002306// HTemplateInstruction specialization for N=0.
2307template<>
2308class HTemplateInstruction<0>: public HInstruction {
2309 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002310 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002311 : HInstruction(side_effects, dex_pc) {}
2312
Vladimir Markof9f64412015-09-02 14:05:49 +01002313 virtual ~HTemplateInstruction() {}
2314
Vladimir Marko372f10e2016-05-17 16:30:10 +01002315 using HInstruction::GetInputRecords; // Keep the const version visible.
2316 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2317 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002318 }
2319
2320 private:
2321 friend class SsaBuilder;
2322};
2323
Dave Allison20dfc792014-06-16 20:44:29 -07002324template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002325class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002326 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002327 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002328 : HTemplateInstruction<N>(side_effects, dex_pc) {
2329 this->template SetPackedField<TypeField>(type);
2330 }
Dave Allison20dfc792014-06-16 20:44:29 -07002331 virtual ~HExpression() {}
2332
Vladimir Markoa1de9182016-02-25 11:37:38 +00002333 Primitive::Type GetType() const OVERRIDE {
2334 return TypeField::Decode(this->GetPackedFields());
2335 }
Dave Allison20dfc792014-06-16 20:44:29 -07002336
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002337 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002338 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2339 static constexpr size_t kFieldTypeSize =
2340 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2341 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2342 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2343 "Too many packed fields.");
2344 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002345};
2346
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002347// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2348// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002349class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002350 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002351 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2352 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002353
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002354 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002355
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002356 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002357
2358 private:
2359 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2360};
2361
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002362// Represents dex's RETURN opcodes. A HReturn is a control flow
2363// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002364class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002365 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002366 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2367 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002368 SetRawInputAt(0, value);
2369 }
2370
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002371 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002372
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002373 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002374
2375 private:
2376 DISALLOW_COPY_AND_ASSIGN(HReturn);
2377};
2378
Vladimir Markofcb503c2016-05-18 12:48:17 +01002379class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002380 public:
2381 HPhi(ArenaAllocator* arena,
2382 uint32_t reg_number,
2383 size_t number_of_inputs,
2384 Primitive::Type type,
2385 uint32_t dex_pc = kNoDexPc)
2386 : HInstruction(SideEffects::None(), dex_pc),
2387 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2388 reg_number_(reg_number) {
2389 SetPackedField<TypeField>(ToPhiType(type));
2390 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2391 // Phis are constructed live and marked dead if conflicting or unused.
2392 // Individual steps of SsaBuilder should assume that if a phi has been
2393 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2394 SetPackedFlag<kFlagIsLive>(true);
2395 SetPackedFlag<kFlagCanBeNull>(true);
2396 }
2397
2398 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2399 static Primitive::Type ToPhiType(Primitive::Type type) {
2400 return Primitive::PrimitiveKind(type);
2401 }
2402
2403 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2404
Vladimir Marko372f10e2016-05-17 16:30:10 +01002405 using HInstruction::GetInputRecords; // Keep the const version visible.
2406 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2407 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2408 }
David Brazdildee58d62016-04-07 09:54:26 +00002409
2410 void AddInput(HInstruction* input);
2411 void RemoveInputAt(size_t index);
2412
2413 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2414 void SetType(Primitive::Type new_type) {
2415 // Make sure that only valid type changes occur. The following are allowed:
2416 // (1) int -> float/ref (primitive type propagation),
2417 // (2) long -> double (primitive type propagation).
2418 DCHECK(GetType() == new_type ||
2419 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2420 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2421 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2422 SetPackedField<TypeField>(new_type);
2423 }
2424
2425 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2426 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2427
2428 uint32_t GetRegNumber() const { return reg_number_; }
2429
2430 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2431 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2432 bool IsDead() const { return !IsLive(); }
2433 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2434
Vladimir Markoe9004912016-06-16 16:50:52 +01002435 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002436 return other != nullptr
2437 && other->IsPhi()
2438 && other->AsPhi()->GetBlock() == GetBlock()
2439 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2440 }
2441
2442 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2443 // An equivalent phi is a phi having the same dex register and type.
2444 // It assumes that phis with the same dex register are adjacent.
2445 HPhi* GetNextEquivalentPhiWithSameType() {
2446 HInstruction* next = GetNext();
2447 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2448 if (next->GetType() == GetType()) {
2449 return next->AsPhi();
2450 }
2451 next = next->GetNext();
2452 }
2453 return nullptr;
2454 }
2455
2456 DECLARE_INSTRUCTION(Phi);
2457
David Brazdildee58d62016-04-07 09:54:26 +00002458 private:
2459 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2460 static constexpr size_t kFieldTypeSize =
2461 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2462 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2463 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2464 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2465 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2466 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2467
Vladimir Marko372f10e2016-05-17 16:30:10 +01002468 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002469 const uint32_t reg_number_;
2470
2471 DISALLOW_COPY_AND_ASSIGN(HPhi);
2472};
2473
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002474// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002475// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002476// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002477class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002478 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002479 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002480
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002481 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002482
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002483 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002484
2485 private:
2486 DISALLOW_COPY_AND_ASSIGN(HExit);
2487};
2488
2489// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002490class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002491 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002492 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002493
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002494 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002495
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002496 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002497 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002498 }
2499
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002500 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002501
2502 private:
2503 DISALLOW_COPY_AND_ASSIGN(HGoto);
2504};
2505
Roland Levillain9867bc72015-08-05 10:21:34 +01002506class HConstant : public HExpression<0> {
2507 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002508 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2509 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002510
2511 bool CanBeMoved() const OVERRIDE { return true; }
2512
Roland Levillain1a653882016-03-18 18:05:57 +00002513 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002514 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002515 // Is this constant 0 in the arithmetic sense?
2516 virtual bool IsArithmeticZero() const { return false; }
2517 // Is this constant a 0-bit pattern?
2518 virtual bool IsZeroBitPattern() const { return false; }
2519 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002520 virtual bool IsOne() const { return false; }
2521
David Brazdil77a48ae2015-09-15 12:34:04 +00002522 virtual uint64_t GetValueAsUint64() const = 0;
2523
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002524 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002525
2526 private:
2527 DISALLOW_COPY_AND_ASSIGN(HConstant);
2528};
2529
Vladimir Markofcb503c2016-05-18 12:48:17 +01002530class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002531 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002532 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002533 return true;
2534 }
2535
David Brazdil77a48ae2015-09-15 12:34:04 +00002536 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2537
Roland Levillain9867bc72015-08-05 10:21:34 +01002538 size_t ComputeHashCode() const OVERRIDE { return 0; }
2539
Roland Levillain1a653882016-03-18 18:05:57 +00002540 // The null constant representation is a 0-bit pattern.
2541 virtual bool IsZeroBitPattern() const { return true; }
2542
Roland Levillain9867bc72015-08-05 10:21:34 +01002543 DECLARE_INSTRUCTION(NullConstant);
2544
2545 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002546 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002547
2548 friend class HGraph;
2549 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2550};
2551
2552// Constants of the type int. Those can be from Dex instructions, or
2553// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002554class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002555 public:
2556 int32_t GetValue() const { return value_; }
2557
David Brazdil9f389d42015-10-01 14:32:56 +01002558 uint64_t GetValueAsUint64() const OVERRIDE {
2559 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2560 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002561
Vladimir Marko372f10e2016-05-17 16:30:10 +01002562 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002563 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002564 return other->AsIntConstant()->value_ == value_;
2565 }
2566
2567 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2568
2569 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002570 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2571 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002572 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2573
Roland Levillain1a653882016-03-18 18:05:57 +00002574 // Integer constants are used to encode Boolean values as well,
2575 // where 1 means true and 0 means false.
2576 bool IsTrue() const { return GetValue() == 1; }
2577 bool IsFalse() const { return GetValue() == 0; }
2578
Roland Levillain9867bc72015-08-05 10:21:34 +01002579 DECLARE_INSTRUCTION(IntConstant);
2580
2581 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002582 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2583 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2584 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2585 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002586
2587 const int32_t value_;
2588
2589 friend class HGraph;
2590 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2591 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2592 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2593};
2594
Vladimir Markofcb503c2016-05-18 12:48:17 +01002595class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002596 public:
2597 int64_t GetValue() const { return value_; }
2598
David Brazdil77a48ae2015-09-15 12:34:04 +00002599 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2600
Vladimir Marko372f10e2016-05-17 16:30:10 +01002601 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002602 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002603 return other->AsLongConstant()->value_ == value_;
2604 }
2605
2606 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2607
2608 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002609 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2610 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002611 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2612
2613 DECLARE_INSTRUCTION(LongConstant);
2614
2615 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002616 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2617 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002618
2619 const int64_t value_;
2620
2621 friend class HGraph;
2622 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2623};
Dave Allison20dfc792014-06-16 20:44:29 -07002624
Vladimir Markofcb503c2016-05-18 12:48:17 +01002625class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002626 public:
2627 float GetValue() const { return value_; }
2628
2629 uint64_t GetValueAsUint64() const OVERRIDE {
2630 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2631 }
2632
Vladimir Marko372f10e2016-05-17 16:30:10 +01002633 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002634 DCHECK(other->IsFloatConstant()) << other->DebugName();
2635 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2636 }
2637
2638 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2639
2640 bool IsMinusOne() const OVERRIDE {
2641 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2642 }
Roland Levillain1a653882016-03-18 18:05:57 +00002643 bool IsArithmeticZero() const OVERRIDE {
2644 return std::fpclassify(value_) == FP_ZERO;
2645 }
2646 bool IsArithmeticPositiveZero() const {
2647 return IsArithmeticZero() && !std::signbit(value_);
2648 }
2649 bool IsArithmeticNegativeZero() const {
2650 return IsArithmeticZero() && std::signbit(value_);
2651 }
2652 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002653 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002654 }
2655 bool IsOne() const OVERRIDE {
2656 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2657 }
2658 bool IsNaN() const {
2659 return std::isnan(value_);
2660 }
2661
2662 DECLARE_INSTRUCTION(FloatConstant);
2663
2664 private:
2665 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2666 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2667 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2668 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2669
2670 const float value_;
2671
2672 // Only the SsaBuilder and HGraph can create floating-point constants.
2673 friend class SsaBuilder;
2674 friend class HGraph;
2675 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2676};
2677
Vladimir Markofcb503c2016-05-18 12:48:17 +01002678class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002679 public:
2680 double GetValue() const { return value_; }
2681
2682 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2683
Vladimir Marko372f10e2016-05-17 16:30:10 +01002684 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002685 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2686 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2687 }
2688
2689 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2690
2691 bool IsMinusOne() const OVERRIDE {
2692 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2693 }
Roland Levillain1a653882016-03-18 18:05:57 +00002694 bool IsArithmeticZero() const OVERRIDE {
2695 return std::fpclassify(value_) == FP_ZERO;
2696 }
2697 bool IsArithmeticPositiveZero() const {
2698 return IsArithmeticZero() && !std::signbit(value_);
2699 }
2700 bool IsArithmeticNegativeZero() const {
2701 return IsArithmeticZero() && std::signbit(value_);
2702 }
2703 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002704 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002705 }
2706 bool IsOne() const OVERRIDE {
2707 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2708 }
2709 bool IsNaN() const {
2710 return std::isnan(value_);
2711 }
2712
2713 DECLARE_INSTRUCTION(DoubleConstant);
2714
2715 private:
2716 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2717 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2718 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2719 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2720
2721 const double value_;
2722
2723 // Only the SsaBuilder and HGraph can create floating-point constants.
2724 friend class SsaBuilder;
2725 friend class HGraph;
2726 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2727};
2728
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002729// Conditional branch. A block ending with an HIf instruction must have
2730// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002731class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002732 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002733 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2734 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002735 SetRawInputAt(0, input);
2736 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002737
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002738 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002739
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002740 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002741 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002742 }
2743
2744 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002745 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002746 }
2747
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002748 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002749
2750 private:
2751 DISALLOW_COPY_AND_ASSIGN(HIf);
2752};
2753
David Brazdilfc6a86a2015-06-26 10:33:45 +00002754
2755// Abstract instruction which marks the beginning and/or end of a try block and
2756// links it to the respective exception handlers. Behaves the same as a Goto in
2757// non-exceptional control flow.
2758// Normal-flow successor is stored at index zero, exception handlers under
2759// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002760class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002761 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002762 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002763 kEntry,
2764 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002765 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002766 };
2767
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002768 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002769 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2770 SetPackedField<BoundaryKindField>(kind);
2771 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002772
2773 bool IsControlFlow() const OVERRIDE { return true; }
2774
2775 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002776 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002777
David Brazdild26a4112015-11-10 11:07:31 +00002778 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2779 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2780 }
2781
David Brazdilfc6a86a2015-06-26 10:33:45 +00002782 // Returns whether `handler` is among its exception handlers (non-zero index
2783 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002784 bool HasExceptionHandler(const HBasicBlock& handler) const {
2785 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002786 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002787 }
2788
2789 // If not present already, adds `handler` to its block's list of exception
2790 // handlers.
2791 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002792 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002793 GetBlock()->AddSuccessor(handler);
2794 }
2795 }
2796
Vladimir Markoa1de9182016-02-25 11:37:38 +00002797 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2798 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002799
David Brazdilffee3d32015-07-06 11:48:53 +01002800 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2801
David Brazdilfc6a86a2015-06-26 10:33:45 +00002802 DECLARE_INSTRUCTION(TryBoundary);
2803
2804 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002805 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2806 static constexpr size_t kFieldBoundaryKindSize =
2807 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2808 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2809 kFieldBoundaryKind + kFieldBoundaryKindSize;
2810 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2811 "Too many packed fields.");
2812 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002813
2814 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2815};
2816
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002817// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002818class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002819 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002820 // We set CanTriggerGC to prevent any intermediate address to be live
2821 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002822 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002823 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002824 SetRawInputAt(0, cond);
2825 }
2826
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002827 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002828 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002829 return true;
2830 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002831 bool NeedsEnvironment() const OVERRIDE { return true; }
2832 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002833
2834 DECLARE_INSTRUCTION(Deoptimize);
2835
2836 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002837 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2838};
2839
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002840// Represents the ArtMethod that was passed as a first argument to
2841// the method. It is used by instructions that depend on it, like
2842// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002843class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002844 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002845 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2846 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002847
2848 DECLARE_INSTRUCTION(CurrentMethod);
2849
2850 private:
2851 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2852};
2853
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002854// Fetches an ArtMethod from the virtual table or the interface method table
2855// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002856class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002857 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002858 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002859 kVTable,
2860 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002861 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002862 };
2863 HClassTableGet(HInstruction* cls,
2864 Primitive::Type type,
2865 TableKind kind,
2866 size_t index,
2867 uint32_t dex_pc)
2868 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002869 index_(index) {
2870 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002871 SetRawInputAt(0, cls);
2872 }
2873
2874 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002875 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002876 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002877 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002878 }
2879
Vladimir Markoa1de9182016-02-25 11:37:38 +00002880 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002881 size_t GetIndex() const { return index_; }
2882
2883 DECLARE_INSTRUCTION(ClassTableGet);
2884
2885 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002886 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2887 static constexpr size_t kFieldTableKindSize =
2888 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2889 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2890 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2891 "Too many packed fields.");
2892 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2893
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002894 // The index of the ArtMethod in the table.
2895 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002896
2897 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2898};
2899
Mark Mendellfe57faa2015-09-18 09:26:15 -04002900// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2901// have one successor for each entry in the switch table, and the final successor
2902// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002903class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002904 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002905 HPackedSwitch(int32_t start_value,
2906 uint32_t num_entries,
2907 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002908 uint32_t dex_pc = kNoDexPc)
2909 : HTemplateInstruction(SideEffects::None(), dex_pc),
2910 start_value_(start_value),
2911 num_entries_(num_entries) {
2912 SetRawInputAt(0, input);
2913 }
2914
2915 bool IsControlFlow() const OVERRIDE { return true; }
2916
2917 int32_t GetStartValue() const { return start_value_; }
2918
Vladimir Marko211c2112015-09-24 16:52:33 +01002919 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002920
2921 HBasicBlock* GetDefaultBlock() const {
2922 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002923 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002924 }
2925 DECLARE_INSTRUCTION(PackedSwitch);
2926
2927 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002928 const int32_t start_value_;
2929 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002930
2931 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2932};
2933
Roland Levillain88cb1752014-10-20 16:36:47 +01002934class HUnaryOperation : public HExpression<1> {
2935 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002936 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2937 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002938 SetRawInputAt(0, input);
2939 }
2940
2941 HInstruction* GetInput() const { return InputAt(0); }
2942 Primitive::Type GetResultType() const { return GetType(); }
2943
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002944 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002945 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002946 return true;
2947 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002948
Roland Levillain31dd3d62016-02-16 12:21:02 +00002949 // Try to statically evaluate `this` and return a HConstant
2950 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002951 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002952 HConstant* TryStaticEvaluation() const;
2953
2954 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002955 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2956 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002957 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2958 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002959
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002960 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002961
2962 private:
2963 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2964};
2965
Dave Allison20dfc792014-06-16 20:44:29 -07002966class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002967 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002968 HBinaryOperation(Primitive::Type result_type,
2969 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002970 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002971 SideEffects side_effects = SideEffects::None(),
2972 uint32_t dex_pc = kNoDexPc)
2973 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002974 SetRawInputAt(0, left);
2975 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002976 }
2977
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002978 HInstruction* GetLeft() const { return InputAt(0); }
2979 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002980 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002981
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002982 virtual bool IsCommutative() const { return false; }
2983
2984 // Put constant on the right.
2985 // Returns whether order is changed.
2986 bool OrderInputsWithConstantOnTheRight() {
2987 HInstruction* left = InputAt(0);
2988 HInstruction* right = InputAt(1);
2989 if (left->IsConstant() && !right->IsConstant()) {
2990 ReplaceInput(right, 0);
2991 ReplaceInput(left, 1);
2992 return true;
2993 }
2994 return false;
2995 }
2996
2997 // Order inputs by instruction id, but favor constant on the right side.
2998 // This helps GVN for commutative ops.
2999 void OrderInputs() {
3000 DCHECK(IsCommutative());
3001 HInstruction* left = InputAt(0);
3002 HInstruction* right = InputAt(1);
3003 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3004 return;
3005 }
3006 if (OrderInputsWithConstantOnTheRight()) {
3007 return;
3008 }
3009 // Order according to instruction id.
3010 if (left->GetId() > right->GetId()) {
3011 ReplaceInput(right, 0);
3012 ReplaceInput(left, 1);
3013 }
3014 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003015
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003016 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003017 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003018 return true;
3019 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003020
Roland Levillain31dd3d62016-02-16 12:21:02 +00003021 // Try to statically evaluate `this` and return a HConstant
3022 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003023 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003024 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003025
3026 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003027 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3028 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003029 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3030 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003031 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003032 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3033 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003034 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3035 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003036 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3037 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003038 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003039 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3040 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003041
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003042 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003043 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003044 HConstant* GetConstantRight() const;
3045
3046 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003047 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003048 HInstruction* GetLeastConstantLeft() const;
3049
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003050 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003051
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003052 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003053 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3054};
3055
Mark Mendellc4701932015-04-10 13:18:51 -04003056// The comparison bias applies for floating point operations and indicates how NaN
3057// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003058enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003059 kNoBias, // bias is not applicable (i.e. for long operation)
3060 kGtBias, // return 1 for NaN comparisons
3061 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003062 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003063};
3064
Roland Levillain31dd3d62016-02-16 12:21:02 +00003065std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3066
Dave Allison20dfc792014-06-16 20:44:29 -07003067class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003068 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003069 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003070 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3071 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3072 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003073
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003074 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003075 // `instruction`, and disregard moves in between.
3076 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003077
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003078 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003079
3080 virtual IfCondition GetCondition() const = 0;
3081
Mark Mendellc4701932015-04-10 13:18:51 -04003082 virtual IfCondition GetOppositeCondition() const = 0;
3083
Vladimir Markoa1de9182016-02-25 11:37:38 +00003084 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003085 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3086
Vladimir Markoa1de9182016-02-25 11:37:38 +00003087 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3088 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003089
Vladimir Marko372f10e2016-05-17 16:30:10 +01003090 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003091 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003092 }
3093
Roland Levillain4fa13f62015-07-06 18:11:54 +01003094 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003095 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003096 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003097 if (if_cond == kCondNE) {
3098 return true;
3099 } else if (if_cond == kCondEQ) {
3100 return false;
3101 }
3102 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003103 }
3104
3105 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003106 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003107 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003108 if (if_cond == kCondEQ) {
3109 return true;
3110 } else if (if_cond == kCondNE) {
3111 return false;
3112 }
3113 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003114 }
3115
Roland Levillain31dd3d62016-02-16 12:21:02 +00003116 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003117 // Needed if we merge a HCompare into a HCondition.
3118 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3119 static constexpr size_t kFieldComparisonBiasSize =
3120 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3121 static constexpr size_t kNumberOfConditionPackedBits =
3122 kFieldComparisonBias + kFieldComparisonBiasSize;
3123 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3124 using ComparisonBiasField =
3125 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3126
Roland Levillain31dd3d62016-02-16 12:21:02 +00003127 template <typename T>
3128 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3129
3130 template <typename T>
3131 int32_t CompareFP(T x, T y) const {
3132 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3133 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3134 // Handle the bias.
3135 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3136 }
3137
3138 // Return an integer constant containing the result of a condition evaluated at compile time.
3139 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3140 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3141 }
3142
Dave Allison20dfc792014-06-16 20:44:29 -07003143 private:
3144 DISALLOW_COPY_AND_ASSIGN(HCondition);
3145};
3146
3147// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003148class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003149 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003150 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3151 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003152
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003153 bool IsCommutative() const OVERRIDE { return true; }
3154
Vladimir Marko9e23df52015-11-10 17:14:35 +00003155 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3156 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003157 return MakeConstantCondition(true, GetDexPc());
3158 }
3159 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3160 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3161 }
3162 // In the following Evaluate methods, a HCompare instruction has
3163 // been merged into this HEqual instruction; evaluate it as
3164 // `Compare(x, y) == 0`.
3165 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3166 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3167 GetDexPc());
3168 }
3169 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3170 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3171 }
3172 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3173 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003174 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003175
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003176 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003177
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003178 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003179 return kCondEQ;
3180 }
3181
Mark Mendellc4701932015-04-10 13:18:51 -04003182 IfCondition GetOppositeCondition() const OVERRIDE {
3183 return kCondNE;
3184 }
3185
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003186 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003187 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003188
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003189 DISALLOW_COPY_AND_ASSIGN(HEqual);
3190};
3191
Vladimir Markofcb503c2016-05-18 12:48:17 +01003192class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003193 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003194 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3195 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003196
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003197 bool IsCommutative() const OVERRIDE { return true; }
3198
Vladimir Marko9e23df52015-11-10 17:14:35 +00003199 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3200 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003201 return MakeConstantCondition(false, GetDexPc());
3202 }
3203 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3204 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3205 }
3206 // In the following Evaluate methods, a HCompare instruction has
3207 // been merged into this HNotEqual instruction; evaluate it as
3208 // `Compare(x, y) != 0`.
3209 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3210 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3211 }
3212 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3213 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3214 }
3215 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3216 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003217 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003218
Dave Allison20dfc792014-06-16 20:44:29 -07003219 DECLARE_INSTRUCTION(NotEqual);
3220
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003221 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003222 return kCondNE;
3223 }
3224
Mark Mendellc4701932015-04-10 13:18:51 -04003225 IfCondition GetOppositeCondition() const OVERRIDE {
3226 return kCondEQ;
3227 }
3228
Dave Allison20dfc792014-06-16 20:44:29 -07003229 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003230 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003231
Dave Allison20dfc792014-06-16 20:44:29 -07003232 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3233};
3234
Vladimir Markofcb503c2016-05-18 12:48:17 +01003235class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003236 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003237 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3238 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003239
Roland Levillain9867bc72015-08-05 10:21:34 +01003240 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003241 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003242 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003243 // In the following Evaluate methods, a HCompare instruction has
3244 // been merged into this HLessThan instruction; evaluate it as
3245 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003246 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003247 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3248 }
3249 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3250 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3251 }
3252 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3253 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003254 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003255
Dave Allison20dfc792014-06-16 20:44:29 -07003256 DECLARE_INSTRUCTION(LessThan);
3257
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003258 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003259 return kCondLT;
3260 }
3261
Mark Mendellc4701932015-04-10 13:18:51 -04003262 IfCondition GetOppositeCondition() const OVERRIDE {
3263 return kCondGE;
3264 }
3265
Dave Allison20dfc792014-06-16 20:44:29 -07003266 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003267 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003268
Dave Allison20dfc792014-06-16 20:44:29 -07003269 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3270};
3271
Vladimir Markofcb503c2016-05-18 12:48:17 +01003272class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003273 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003274 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3275 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003276
Roland Levillain9867bc72015-08-05 10:21:34 +01003277 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003278 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003279 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003280 // In the following Evaluate methods, a HCompare instruction has
3281 // been merged into this HLessThanOrEqual instruction; evaluate it as
3282 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003283 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003284 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3285 }
3286 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3287 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3288 }
3289 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3290 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003291 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003292
Dave Allison20dfc792014-06-16 20:44:29 -07003293 DECLARE_INSTRUCTION(LessThanOrEqual);
3294
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003295 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003296 return kCondLE;
3297 }
3298
Mark Mendellc4701932015-04-10 13:18:51 -04003299 IfCondition GetOppositeCondition() const OVERRIDE {
3300 return kCondGT;
3301 }
3302
Dave Allison20dfc792014-06-16 20:44:29 -07003303 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003304 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003305
Dave Allison20dfc792014-06-16 20:44:29 -07003306 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3307};
3308
Vladimir Markofcb503c2016-05-18 12:48:17 +01003309class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003310 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003311 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3312 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003313
Roland Levillain9867bc72015-08-05 10:21:34 +01003314 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003315 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003316 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003317 // In the following Evaluate methods, a HCompare instruction has
3318 // been merged into this HGreaterThan instruction; evaluate it as
3319 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003320 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003321 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3322 }
3323 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3324 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3325 }
3326 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3327 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003328 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003329
Dave Allison20dfc792014-06-16 20:44:29 -07003330 DECLARE_INSTRUCTION(GreaterThan);
3331
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003332 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003333 return kCondGT;
3334 }
3335
Mark Mendellc4701932015-04-10 13:18:51 -04003336 IfCondition GetOppositeCondition() const OVERRIDE {
3337 return kCondLE;
3338 }
3339
Dave Allison20dfc792014-06-16 20:44:29 -07003340 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003341 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003342
Dave Allison20dfc792014-06-16 20:44:29 -07003343 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3344};
3345
Vladimir Markofcb503c2016-05-18 12:48:17 +01003346class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003347 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003348 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3349 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003350
Roland Levillain9867bc72015-08-05 10:21:34 +01003351 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003352 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003353 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003354 // In the following Evaluate methods, a HCompare instruction has
3355 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3356 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003357 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003358 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3359 }
3360 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3361 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3362 }
3363 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3364 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003365 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003366
Dave Allison20dfc792014-06-16 20:44:29 -07003367 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3368
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003369 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003370 return kCondGE;
3371 }
3372
Mark Mendellc4701932015-04-10 13:18:51 -04003373 IfCondition GetOppositeCondition() const OVERRIDE {
3374 return kCondLT;
3375 }
3376
Dave Allison20dfc792014-06-16 20:44:29 -07003377 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003378 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003379
Dave Allison20dfc792014-06-16 20:44:29 -07003380 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3381};
3382
Vladimir Markofcb503c2016-05-18 12:48:17 +01003383class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003384 public:
3385 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3386 : HCondition(first, second, dex_pc) {}
3387
3388 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003389 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003390 }
3391 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003392 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3393 }
3394 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3395 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3396 LOG(FATAL) << DebugName() << " is not defined for float values";
3397 UNREACHABLE();
3398 }
3399 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3400 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3401 LOG(FATAL) << DebugName() << " is not defined for double values";
3402 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003403 }
3404
3405 DECLARE_INSTRUCTION(Below);
3406
3407 IfCondition GetCondition() const OVERRIDE {
3408 return kCondB;
3409 }
3410
3411 IfCondition GetOppositeCondition() const OVERRIDE {
3412 return kCondAE;
3413 }
3414
3415 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003416 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003417 return MakeUnsigned(x) < MakeUnsigned(y);
3418 }
Aart Bike9f37602015-10-09 11:15:55 -07003419
3420 DISALLOW_COPY_AND_ASSIGN(HBelow);
3421};
3422
Vladimir Markofcb503c2016-05-18 12:48:17 +01003423class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003424 public:
3425 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3426 : HCondition(first, second, dex_pc) {}
3427
3428 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003429 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003430 }
3431 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003432 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3433 }
3434 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3435 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3436 LOG(FATAL) << DebugName() << " is not defined for float values";
3437 UNREACHABLE();
3438 }
3439 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3440 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3441 LOG(FATAL) << DebugName() << " is not defined for double values";
3442 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003443 }
3444
3445 DECLARE_INSTRUCTION(BelowOrEqual);
3446
3447 IfCondition GetCondition() const OVERRIDE {
3448 return kCondBE;
3449 }
3450
3451 IfCondition GetOppositeCondition() const OVERRIDE {
3452 return kCondA;
3453 }
3454
3455 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003456 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003457 return MakeUnsigned(x) <= MakeUnsigned(y);
3458 }
Aart Bike9f37602015-10-09 11:15:55 -07003459
3460 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3461};
3462
Vladimir Markofcb503c2016-05-18 12:48:17 +01003463class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003464 public:
3465 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3466 : HCondition(first, second, dex_pc) {}
3467
3468 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003469 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003470 }
3471 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003472 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3473 }
3474 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3475 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3476 LOG(FATAL) << DebugName() << " is not defined for float values";
3477 UNREACHABLE();
3478 }
3479 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3480 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3481 LOG(FATAL) << DebugName() << " is not defined for double values";
3482 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003483 }
3484
3485 DECLARE_INSTRUCTION(Above);
3486
3487 IfCondition GetCondition() const OVERRIDE {
3488 return kCondA;
3489 }
3490
3491 IfCondition GetOppositeCondition() const OVERRIDE {
3492 return kCondBE;
3493 }
3494
3495 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003496 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003497 return MakeUnsigned(x) > MakeUnsigned(y);
3498 }
Aart Bike9f37602015-10-09 11:15:55 -07003499
3500 DISALLOW_COPY_AND_ASSIGN(HAbove);
3501};
3502
Vladimir Markofcb503c2016-05-18 12:48:17 +01003503class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003504 public:
3505 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3506 : HCondition(first, second, dex_pc) {}
3507
3508 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003509 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003510 }
3511 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003512 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3513 }
3514 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3515 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3516 LOG(FATAL) << DebugName() << " is not defined for float values";
3517 UNREACHABLE();
3518 }
3519 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3520 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3521 LOG(FATAL) << DebugName() << " is not defined for double values";
3522 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003523 }
3524
3525 DECLARE_INSTRUCTION(AboveOrEqual);
3526
3527 IfCondition GetCondition() const OVERRIDE {
3528 return kCondAE;
3529 }
3530
3531 IfCondition GetOppositeCondition() const OVERRIDE {
3532 return kCondB;
3533 }
3534
3535 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003536 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003537 return MakeUnsigned(x) >= MakeUnsigned(y);
3538 }
Aart Bike9f37602015-10-09 11:15:55 -07003539
3540 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3541};
Dave Allison20dfc792014-06-16 20:44:29 -07003542
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003543// Instruction to check how two inputs compare to each other.
3544// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003545class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003546 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003547 // Note that `comparison_type` is the type of comparison performed
3548 // between the comparison's inputs, not the type of the instantiated
3549 // HCompare instruction (which is always Primitive::kPrimInt).
3550 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003551 HInstruction* first,
3552 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003553 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003554 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003555 : HBinaryOperation(Primitive::kPrimInt,
3556 first,
3557 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003558 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003559 dex_pc) {
3560 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003561 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3562 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003563 }
3564
Roland Levillain9867bc72015-08-05 10:21:34 +01003565 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003566 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3567
3568 template <typename T>
3569 int32_t ComputeFP(T x, T y) const {
3570 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3571 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3572 // Handle the bias.
3573 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3574 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003575
Roland Levillain9867bc72015-08-05 10:21:34 +01003576 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003577 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3578 // reach this code path when processing a freshly built HIR
3579 // graph. However HCompare integer instructions can be synthesized
3580 // by the instruction simplifier to implement IntegerCompare and
3581 // IntegerSignum intrinsics, so we have to handle this case.
3582 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003583 }
3584 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003585 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3586 }
3587 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3588 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3589 }
3590 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3591 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003592 }
3593
Vladimir Marko372f10e2016-05-17 16:30:10 +01003594 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003595 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003596 }
3597
Vladimir Markoa1de9182016-02-25 11:37:38 +00003598 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003599
Roland Levillain31dd3d62016-02-16 12:21:02 +00003600 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003601 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003602 bool IsGtBias() const {
3603 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003604 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003605 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003606
Roland Levillain1693a1f2016-03-15 14:57:31 +00003607 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3608 // Comparisons do not require a runtime call in any back end.
3609 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003610 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003611
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003612 DECLARE_INSTRUCTION(Compare);
3613
Roland Levillain31dd3d62016-02-16 12:21:02 +00003614 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003615 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3616 static constexpr size_t kFieldComparisonBiasSize =
3617 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3618 static constexpr size_t kNumberOfComparePackedBits =
3619 kFieldComparisonBias + kFieldComparisonBiasSize;
3620 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3621 using ComparisonBiasField =
3622 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3623
Roland Levillain31dd3d62016-02-16 12:21:02 +00003624 // Return an integer constant containing the result of a comparison evaluated at compile time.
3625 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3626 DCHECK(value == -1 || value == 0 || value == 1) << value;
3627 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3628 }
3629
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003630 private:
3631 DISALLOW_COPY_AND_ASSIGN(HCompare);
3632};
3633
Vladimir Markofcb503c2016-05-18 12:48:17 +01003634class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003635 public:
3636 HNewInstance(HInstruction* cls,
3637 HCurrentMethod* current_method,
3638 uint32_t dex_pc,
3639 uint16_t type_index,
3640 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003641 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003642 bool finalizable,
3643 QuickEntrypointEnum entrypoint)
3644 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3645 type_index_(type_index),
3646 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003647 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003648 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003649 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003650 SetRawInputAt(0, cls);
3651 SetRawInputAt(1, current_method);
3652 }
3653
3654 uint16_t GetTypeIndex() const { return type_index_; }
3655 const DexFile& GetDexFile() const { return dex_file_; }
3656
3657 // Calls runtime so needs an environment.
3658 bool NeedsEnvironment() const OVERRIDE { return true; }
3659
Mingyao Yang062157f2016-03-02 10:15:36 -08003660 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3661 bool CanThrow() const OVERRIDE { return true; }
3662
3663 // Needs to call into runtime to make sure it's instantiable/accessible.
3664 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003665
Vladimir Markoa1de9182016-02-25 11:37:38 +00003666 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003667
3668 bool CanBeNull() const OVERRIDE { return false; }
3669
3670 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3671
3672 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3673 entrypoint_ = entrypoint;
3674 }
3675
3676 bool IsStringAlloc() const;
3677
3678 DECLARE_INSTRUCTION(NewInstance);
3679
3680 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003681 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3682 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003683 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3684 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3685 "Too many packed fields.");
3686
David Brazdil6de19382016-01-08 17:37:10 +00003687 const uint16_t type_index_;
3688 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003689 QuickEntrypointEnum entrypoint_;
3690
3691 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3692};
3693
Agi Csaki05f20562015-08-19 14:58:14 -07003694enum IntrinsicNeedsEnvironmentOrCache {
3695 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3696 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003697};
3698
Aart Bik5d75afe2015-12-14 11:57:01 -08003699enum IntrinsicSideEffects {
3700 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3701 kReadSideEffects, // Intrinsic may read heap memory.
3702 kWriteSideEffects, // Intrinsic may write heap memory.
3703 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3704};
3705
3706enum IntrinsicExceptions {
3707 kNoThrow, // Intrinsic does not throw any exceptions.
3708 kCanThrow // Intrinsic may throw exceptions.
3709};
3710
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003711class HInvoke : public HInstruction {
3712 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003713 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003714
Vladimir Marko372f10e2016-05-17 16:30:10 +01003715 using HInstruction::GetInputRecords; // Keep the const version visible.
3716 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3717 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3718 }
3719
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003720 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003721 SetRawInputAt(index, argument);
3722 }
3723
Roland Levillain3e3d7332015-04-28 11:00:54 +01003724 // Return the number of arguments. This number can be lower than
3725 // the number of inputs returned by InputCount(), as some invoke
3726 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3727 // inputs at the end of their list of inputs.
3728 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3729
Vladimir Markoa1de9182016-02-25 11:37:38 +00003730 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003731
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003732 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003733 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003734
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003735 InvokeType GetInvokeType() const {
3736 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003737 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003738
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003739 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003740 return intrinsic_;
3741 }
3742
Aart Bik5d75afe2015-12-14 11:57:01 -08003743 void SetIntrinsic(Intrinsics intrinsic,
3744 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3745 IntrinsicSideEffects side_effects,
3746 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003747
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003748 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003749 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003750 }
3751
Vladimir Markoa1de9182016-02-25 11:37:38 +00003752 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003753
3754 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3755
Vladimir Marko372f10e2016-05-17 16:30:10 +01003756 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003757 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3758 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003759
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003760 uint32_t* GetIntrinsicOptimizations() {
3761 return &intrinsic_optimizations_;
3762 }
3763
3764 const uint32_t* GetIntrinsicOptimizations() const {
3765 return &intrinsic_optimizations_;
3766 }
3767
3768 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3769
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003770 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3771
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003772 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003773
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003774 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003775 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3776 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003777 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3778 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003779 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003780 static constexpr size_t kFieldReturnTypeSize =
3781 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3782 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3783 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3784 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003785 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003786 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3787
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003788 HInvoke(ArenaAllocator* arena,
3789 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003790 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003791 Primitive::Type return_type,
3792 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003793 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003794 ArtMethod* resolved_method,
3795 InvokeType invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003796 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003797 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003798 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003799 resolved_method_(resolved_method),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003800 inputs_(number_of_arguments + number_of_other_inputs,
3801 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003802 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003803 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003804 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003805 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003806 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003807 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003808 }
3809
Vladimir Markoa1de9182016-02-25 11:37:38 +00003810 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003811
Roland Levillain3e3d7332015-04-28 11:00:54 +01003812 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003813 ArtMethod* const resolved_method_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003814 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003815 const uint32_t dex_method_index_;
3816 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003817
3818 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3819 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003820
3821 private:
3822 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3823};
3824
Vladimir Markofcb503c2016-05-18 12:48:17 +01003825class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003826 public:
3827 HInvokeUnresolved(ArenaAllocator* arena,
3828 uint32_t number_of_arguments,
3829 Primitive::Type return_type,
3830 uint32_t dex_pc,
3831 uint32_t dex_method_index,
3832 InvokeType invoke_type)
3833 : HInvoke(arena,
3834 number_of_arguments,
3835 0u /* number_of_other_inputs */,
3836 return_type,
3837 dex_pc,
3838 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003839 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003840 invoke_type) {
3841 }
3842
3843 DECLARE_INSTRUCTION(InvokeUnresolved);
3844
3845 private:
3846 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3847};
3848
Vladimir Markofcb503c2016-05-18 12:48:17 +01003849class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003850 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003851 // Requirements of this method call regarding the class
3852 // initialization (clinit) check of its declaring class.
3853 enum class ClinitCheckRequirement {
3854 kNone, // Class already initialized.
3855 kExplicit, // Static call having explicit clinit check as last input.
3856 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003857 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003858 };
3859
Vladimir Marko58155012015-08-19 12:49:41 +00003860 // Determines how to load the target ArtMethod*.
3861 enum class MethodLoadKind {
3862 // Use a String init ArtMethod* loaded from Thread entrypoints.
3863 kStringInit,
3864
3865 // Use the method's own ArtMethod* loaded by the register allocator.
3866 kRecursive,
3867
3868 // Use ArtMethod* at a known address, embed the direct address in the code.
3869 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3870 kDirectAddress,
3871
3872 // Use ArtMethod* at an address that will be known at link time, embed the direct
3873 // address in the code. If the image is relocatable, emit .patch_oat entry.
3874 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3875 // the image relocatable or not.
3876 kDirectAddressWithFixup,
3877
Vladimir Markoa1de9182016-02-25 11:37:38 +00003878 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003879 // Used when we need to use the dex cache, for example for invoke-static that
3880 // may cause class initialization (the entry may point to a resolution method),
3881 // and we know that we can access the dex cache arrays using a PC-relative load.
3882 kDexCachePcRelative,
3883
3884 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3885 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3886 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3887 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3888 kDexCacheViaMethod,
3889 };
3890
3891 // Determines the location of the code pointer.
3892 enum class CodePtrLocation {
3893 // Recursive call, use local PC-relative call instruction.
3894 kCallSelf,
3895
3896 // Use PC-relative call instruction patched at link time.
3897 // Used for calls within an oat file, boot->boot or app->app.
3898 kCallPCRelative,
3899
3900 // Call to a known target address, embed the direct address in code.
3901 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3902 kCallDirect,
3903
3904 // Call to a target address that will be known at link time, embed the direct
3905 // address in code. If the image is relocatable, emit .patch_oat entry.
3906 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3907 // the image relocatable or not.
3908 kCallDirectWithFixup,
3909
3910 // Use code pointer from the ArtMethod*.
3911 // Used when we don't know the target code. This is also the last-resort-kind used when
3912 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3913 kCallArtMethod,
3914 };
3915
3916 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003917 MethodLoadKind method_load_kind;
3918 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003919 // The method load data holds
3920 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3921 // Note that there are multiple string init methods, each having its own offset.
3922 // - the method address for kDirectAddress
3923 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003924 uint64_t method_load_data;
3925 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003926 };
3927
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003928 HInvokeStaticOrDirect(ArenaAllocator* arena,
3929 uint32_t number_of_arguments,
3930 Primitive::Type return_type,
3931 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003932 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003933 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00003934 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003935 InvokeType invoke_type,
3936 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003937 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003938 : HInvoke(arena,
3939 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003940 // There is potentially one extra argument for the HCurrentMethod node, and
3941 // potentially one other if the clinit check is explicit, and potentially
3942 // one other if the method is a string factory.
3943 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003944 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003945 return_type,
3946 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003947 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003948 resolved_method,
3949 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003950 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003951 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003952 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3953 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003954
Vladimir Markodc151b22015-10-15 18:02:30 +01003955 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003956 bool had_current_method_input = HasCurrentMethodInput();
3957 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3958
3959 // Using the current method is the default and once we find a better
3960 // method load kind, we should not go back to using the current method.
3961 DCHECK(had_current_method_input || !needs_current_method_input);
3962
3963 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003964 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3965 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003966 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003967 dispatch_info_ = dispatch_info;
3968 }
3969
Vladimir Markoc53c0792015-11-19 15:48:33 +00003970 void AddSpecialInput(HInstruction* input) {
3971 // We allow only one special input.
3972 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3973 DCHECK(InputCount() == GetSpecialInputIndex() ||
3974 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3975 InsertInputAt(GetSpecialInputIndex(), input);
3976 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003977
Vladimir Marko372f10e2016-05-17 16:30:10 +01003978 using HInstruction::GetInputRecords; // Keep the const version visible.
3979 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3980 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
3981 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
3982 DCHECK(!input_records.empty());
3983 DCHECK_GT(input_records.size(), GetNumberOfArguments());
3984 HInstruction* last_input = input_records.back().GetInstruction();
3985 // Note: `last_input` may be null during arguments setup.
3986 if (last_input != nullptr) {
3987 // `last_input` is the last input of a static invoke marked as having
3988 // an explicit clinit check. It must either be:
3989 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3990 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3991 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
3992 }
3993 }
3994 return input_records;
3995 }
3996
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003997 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003998 // We access the method via the dex cache so we can't do an implicit null check.
3999 // TODO: for intrinsics we can generate implicit null checks.
4000 return false;
4001 }
4002
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004003 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004004 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004005 }
4006
Vladimir Markoc53c0792015-11-19 15:48:33 +00004007 // Get the index of the special input, if any.
4008 //
David Brazdil6de19382016-01-08 17:37:10 +00004009 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4010 // method pointer; otherwise there may be one platform-specific special input,
4011 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004012 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004013 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004014
Vladimir Marko58155012015-08-19 12:49:41 +00004015 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4016 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4017 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004018 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004019 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004020 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004021 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004022 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4023 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004024 bool HasCurrentMethodInput() const {
4025 // This function can be called only after the invoke has been fully initialized by the builder.
4026 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004027 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004028 return true;
4029 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004030 DCHECK(InputCount() == GetSpecialInputIndex() ||
4031 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004032 return false;
4033 }
4034 }
Vladimir Marko58155012015-08-19 12:49:41 +00004035 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
Vladimir Marko58155012015-08-19 12:49:41 +00004036
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004037 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004038 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004039 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004040 }
4041
4042 uint64_t GetMethodAddress() const {
4043 DCHECK(HasMethodAddress());
4044 return dispatch_info_.method_load_data;
4045 }
4046
4047 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004048 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004049 return dispatch_info_.method_load_data;
4050 }
4051
4052 uint64_t GetDirectCodePtr() const {
4053 DCHECK(HasDirectCodePtr());
4054 return dispatch_info_.direct_code_ptr;
4055 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004056
Vladimir Markoa1de9182016-02-25 11:37:38 +00004057 ClinitCheckRequirement GetClinitCheckRequirement() const {
4058 return GetPackedField<ClinitCheckRequirementField>();
4059 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004060
Roland Levillain4c0eb422015-04-24 16:43:49 +01004061 // Is this instruction a call to a static method?
4062 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004063 return GetInvokeType() == kStatic;
4064 }
4065
4066 MethodReference GetTargetMethod() const {
4067 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004068 }
4069
Vladimir Markofbb184a2015-11-13 14:47:00 +00004070 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4071 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4072 // instruction; only relevant for static calls with explicit clinit check.
4073 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004074 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004075 size_t last_input_index = inputs_.size() - 1u;
4076 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004077 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004078 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004079 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004080 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004081 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004082 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004083 }
4084
4085 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004086 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004087 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004088 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004089 }
4090
4091 // Is this a call to a static method whose declaring class has an
4092 // implicit intialization check requirement?
4093 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004094 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004095 }
4096
Vladimir Markob554b5a2015-11-06 12:57:55 +00004097 // Does this method load kind need the current method as an input?
4098 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4099 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4100 }
4101
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004102 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004103
Roland Levillain4c0eb422015-04-24 16:43:49 +01004104 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004105 void InsertInputAt(size_t index, HInstruction* input);
4106 void RemoveInputAt(size_t index);
4107
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004108 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004109 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004110 static constexpr size_t kFieldClinitCheckRequirementSize =
4111 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4112 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4113 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4114 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4115 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004116 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4117 kFieldClinitCheckRequirement,
4118 kFieldClinitCheckRequirementSize>;
4119
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004120 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004121 MethodReference target_method_;
4122 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004123
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004124 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004125};
Vladimir Markof64242a2015-12-01 14:58:23 +00004126std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004127std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004128
Vladimir Markofcb503c2016-05-18 12:48:17 +01004129class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004130 public:
4131 HInvokeVirtual(ArenaAllocator* arena,
4132 uint32_t number_of_arguments,
4133 Primitive::Type return_type,
4134 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004135 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004136 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004137 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004138 : HInvoke(arena,
4139 number_of_arguments,
4140 0u,
4141 return_type,
4142 dex_pc,
4143 dex_method_index,
4144 resolved_method,
4145 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004146 vtable_index_(vtable_index) {}
4147
Calin Juravle641547a2015-04-21 22:08:51 +01004148 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004149 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004150 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004151 }
4152
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004153 uint32_t GetVTableIndex() const { return vtable_index_; }
4154
4155 DECLARE_INSTRUCTION(InvokeVirtual);
4156
4157 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004158 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004159 const uint32_t vtable_index_;
4160
4161 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4162};
4163
Vladimir Markofcb503c2016-05-18 12:48:17 +01004164class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004165 public:
4166 HInvokeInterface(ArenaAllocator* arena,
4167 uint32_t number_of_arguments,
4168 Primitive::Type return_type,
4169 uint32_t dex_pc,
4170 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004171 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004172 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004173 : HInvoke(arena,
4174 number_of_arguments,
4175 0u,
4176 return_type,
4177 dex_pc,
4178 dex_method_index,
4179 resolved_method,
4180 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004181 imt_index_(imt_index) {}
4182
Calin Juravle641547a2015-04-21 22:08:51 +01004183 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004184 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004185 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004186 }
4187
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004188 uint32_t GetImtIndex() const { return imt_index_; }
4189 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4190
4191 DECLARE_INSTRUCTION(InvokeInterface);
4192
4193 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004194 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004195 const uint32_t imt_index_;
4196
4197 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4198};
4199
Vladimir Markofcb503c2016-05-18 12:48:17 +01004200class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004201 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004202 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004203 : HUnaryOperation(result_type, input, dex_pc) {
4204 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4205 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004206
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004207 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004208
4209 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004210 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004211 }
4212 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004213 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004214 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004215 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4216 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4217 }
4218 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4219 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4220 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004221
Roland Levillain88cb1752014-10-20 16:36:47 +01004222 DECLARE_INSTRUCTION(Neg);
4223
4224 private:
4225 DISALLOW_COPY_AND_ASSIGN(HNeg);
4226};
4227
Vladimir Markofcb503c2016-05-18 12:48:17 +01004228class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004229 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004230 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004231 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004232 uint32_t dex_pc,
4233 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004234 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004235 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004236 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004237 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004238 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004239 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004240 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004241 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004242 }
4243
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004244 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004245 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004246
4247 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004248 bool NeedsEnvironment() const OVERRIDE { return true; }
4249
Mingyao Yang0c365e62015-03-31 15:09:29 -07004250 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4251 bool CanThrow() const OVERRIDE { return true; }
4252
Calin Juravle10e244f2015-01-26 18:54:32 +00004253 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004254
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004255 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4256
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004257 DECLARE_INSTRUCTION(NewArray);
4258
4259 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004260 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004261 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004262 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004263
4264 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4265};
4266
Vladimir Markofcb503c2016-05-18 12:48:17 +01004267class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004268 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004269 HAdd(Primitive::Type result_type,
4270 HInstruction* left,
4271 HInstruction* right,
4272 uint32_t dex_pc = kNoDexPc)
4273 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004274
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004275 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004276
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004277 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004278
4279 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004280 return GetBlock()->GetGraph()->GetIntConstant(
4281 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004282 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004283 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004284 return GetBlock()->GetGraph()->GetLongConstant(
4285 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004286 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004287 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4288 return GetBlock()->GetGraph()->GetFloatConstant(
4289 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4290 }
4291 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4292 return GetBlock()->GetGraph()->GetDoubleConstant(
4293 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4294 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004295
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004296 DECLARE_INSTRUCTION(Add);
4297
4298 private:
4299 DISALLOW_COPY_AND_ASSIGN(HAdd);
4300};
4301
Vladimir Markofcb503c2016-05-18 12:48:17 +01004302class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004303 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004304 HSub(Primitive::Type result_type,
4305 HInstruction* left,
4306 HInstruction* right,
4307 uint32_t dex_pc = kNoDexPc)
4308 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004309
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004310 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004311
4312 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004313 return GetBlock()->GetGraph()->GetIntConstant(
4314 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004315 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004316 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004317 return GetBlock()->GetGraph()->GetLongConstant(
4318 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004319 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004320 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4321 return GetBlock()->GetGraph()->GetFloatConstant(
4322 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4323 }
4324 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4325 return GetBlock()->GetGraph()->GetDoubleConstant(
4326 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4327 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004328
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004329 DECLARE_INSTRUCTION(Sub);
4330
4331 private:
4332 DISALLOW_COPY_AND_ASSIGN(HSub);
4333};
4334
Vladimir Markofcb503c2016-05-18 12:48:17 +01004335class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004336 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004337 HMul(Primitive::Type result_type,
4338 HInstruction* left,
4339 HInstruction* right,
4340 uint32_t dex_pc = kNoDexPc)
4341 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004342
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004343 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004344
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004345 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004346
4347 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004348 return GetBlock()->GetGraph()->GetIntConstant(
4349 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004350 }
4351 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004352 return GetBlock()->GetGraph()->GetLongConstant(
4353 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004354 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004355 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4356 return GetBlock()->GetGraph()->GetFloatConstant(
4357 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4358 }
4359 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4360 return GetBlock()->GetGraph()->GetDoubleConstant(
4361 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4362 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004363
4364 DECLARE_INSTRUCTION(Mul);
4365
4366 private:
4367 DISALLOW_COPY_AND_ASSIGN(HMul);
4368};
4369
Vladimir Markofcb503c2016-05-18 12:48:17 +01004370class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004371 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004372 HDiv(Primitive::Type result_type,
4373 HInstruction* left,
4374 HInstruction* right,
4375 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004376 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004377
Roland Levillain9867bc72015-08-05 10:21:34 +01004378 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004379 T ComputeIntegral(T x, T y) const {
4380 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004381 // Our graph structure ensures we never have 0 for `y` during
4382 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004383 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004384 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004385 return (y == -1) ? -x : x / y;
4386 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004387
Roland Levillain31dd3d62016-02-16 12:21:02 +00004388 template <typename T>
4389 T ComputeFP(T x, T y) const {
4390 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4391 return x / y;
4392 }
4393
Roland Levillain9867bc72015-08-05 10:21:34 +01004394 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004395 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004396 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004397 }
4398 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004399 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004400 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4401 }
4402 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4403 return GetBlock()->GetGraph()->GetFloatConstant(
4404 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4405 }
4406 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4407 return GetBlock()->GetGraph()->GetDoubleConstant(
4408 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004409 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004410
4411 DECLARE_INSTRUCTION(Div);
4412
4413 private:
4414 DISALLOW_COPY_AND_ASSIGN(HDiv);
4415};
4416
Vladimir Markofcb503c2016-05-18 12:48:17 +01004417class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004418 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004419 HRem(Primitive::Type result_type,
4420 HInstruction* left,
4421 HInstruction* right,
4422 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004423 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004424
Roland Levillain9867bc72015-08-05 10:21:34 +01004425 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004426 T ComputeIntegral(T x, T y) const {
4427 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004428 // Our graph structure ensures we never have 0 for `y` during
4429 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004430 DCHECK_NE(y, 0);
4431 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4432 return (y == -1) ? 0 : x % y;
4433 }
4434
Roland Levillain31dd3d62016-02-16 12:21:02 +00004435 template <typename T>
4436 T ComputeFP(T x, T y) const {
4437 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4438 return std::fmod(x, y);
4439 }
4440
Roland Levillain9867bc72015-08-05 10:21:34 +01004441 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004442 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004443 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004444 }
4445 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004446 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004447 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004448 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004449 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4450 return GetBlock()->GetGraph()->GetFloatConstant(
4451 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4452 }
4453 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4454 return GetBlock()->GetGraph()->GetDoubleConstant(
4455 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4456 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004457
4458 DECLARE_INSTRUCTION(Rem);
4459
4460 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004461 DISALLOW_COPY_AND_ASSIGN(HRem);
4462};
4463
Vladimir Markofcb503c2016-05-18 12:48:17 +01004464class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004465 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004466 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4467 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004468 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004469 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004470 SetRawInputAt(0, value);
4471 }
4472
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004473 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4474
Calin Juravled0d48522014-11-04 16:40:20 +00004475 bool CanBeMoved() const OVERRIDE { return true; }
4476
Vladimir Marko372f10e2016-05-17 16:30:10 +01004477 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004478 return true;
4479 }
4480
4481 bool NeedsEnvironment() const OVERRIDE { return true; }
4482 bool CanThrow() const OVERRIDE { return true; }
4483
Calin Juravled0d48522014-11-04 16:40:20 +00004484 DECLARE_INSTRUCTION(DivZeroCheck);
4485
4486 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004487 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4488};
4489
Vladimir Markofcb503c2016-05-18 12:48:17 +01004490class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004491 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004492 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004493 HInstruction* value,
4494 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004495 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004496 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4497 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4498 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004499 }
4500
Roland Levillain5b5b9312016-03-22 14:57:31 +00004501 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004502 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004503 return value << (distance & max_shift_distance);
4504 }
4505
4506 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004507 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004508 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004509 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004510 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004511 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004512 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004513 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004514 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4515 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4516 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4517 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004518 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004519 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4520 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004521 LOG(FATAL) << DebugName() << " is not defined for float values";
4522 UNREACHABLE();
4523 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004524 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4525 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004526 LOG(FATAL) << DebugName() << " is not defined for double values";
4527 UNREACHABLE();
4528 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004529
4530 DECLARE_INSTRUCTION(Shl);
4531
4532 private:
4533 DISALLOW_COPY_AND_ASSIGN(HShl);
4534};
4535
Vladimir Markofcb503c2016-05-18 12:48:17 +01004536class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004537 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004538 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004539 HInstruction* value,
4540 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004541 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004542 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4543 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4544 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004545 }
4546
Roland Levillain5b5b9312016-03-22 14:57:31 +00004547 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004548 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004549 return value >> (distance & max_shift_distance);
4550 }
4551
4552 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004553 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004554 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004555 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004556 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004557 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004558 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004559 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004560 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4561 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4562 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4563 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004564 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004565 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4566 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004567 LOG(FATAL) << DebugName() << " is not defined for float values";
4568 UNREACHABLE();
4569 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004570 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4571 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004572 LOG(FATAL) << DebugName() << " is not defined for double values";
4573 UNREACHABLE();
4574 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004575
4576 DECLARE_INSTRUCTION(Shr);
4577
4578 private:
4579 DISALLOW_COPY_AND_ASSIGN(HShr);
4580};
4581
Vladimir Markofcb503c2016-05-18 12:48:17 +01004582class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004583 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004584 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004585 HInstruction* value,
4586 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004587 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004588 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4589 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4590 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004591 }
4592
Roland Levillain5b5b9312016-03-22 14:57:31 +00004593 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004594 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004595 typedef typename std::make_unsigned<T>::type V;
4596 V ux = static_cast<V>(value);
4597 return static_cast<T>(ux >> (distance & max_shift_distance));
4598 }
4599
4600 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004601 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004602 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004603 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004604 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004605 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004606 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004607 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004608 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4609 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4610 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4611 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004612 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004613 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4614 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004615 LOG(FATAL) << DebugName() << " is not defined for float values";
4616 UNREACHABLE();
4617 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004618 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4619 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004620 LOG(FATAL) << DebugName() << " is not defined for double values";
4621 UNREACHABLE();
4622 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004623
4624 DECLARE_INSTRUCTION(UShr);
4625
4626 private:
4627 DISALLOW_COPY_AND_ASSIGN(HUShr);
4628};
4629
Vladimir Markofcb503c2016-05-18 12:48:17 +01004630class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004631 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004632 HAnd(Primitive::Type result_type,
4633 HInstruction* left,
4634 HInstruction* right,
4635 uint32_t dex_pc = kNoDexPc)
4636 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004637
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004638 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004639
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004640 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004641
4642 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004643 return GetBlock()->GetGraph()->GetIntConstant(
4644 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004645 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004646 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004647 return GetBlock()->GetGraph()->GetLongConstant(
4648 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004649 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004650 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4651 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4652 LOG(FATAL) << DebugName() << " is not defined for float values";
4653 UNREACHABLE();
4654 }
4655 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4656 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4657 LOG(FATAL) << DebugName() << " is not defined for double values";
4658 UNREACHABLE();
4659 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004660
4661 DECLARE_INSTRUCTION(And);
4662
4663 private:
4664 DISALLOW_COPY_AND_ASSIGN(HAnd);
4665};
4666
Vladimir Markofcb503c2016-05-18 12:48:17 +01004667class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004668 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004669 HOr(Primitive::Type result_type,
4670 HInstruction* left,
4671 HInstruction* right,
4672 uint32_t dex_pc = kNoDexPc)
4673 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004674
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004675 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004676
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004677 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004678
4679 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004680 return GetBlock()->GetGraph()->GetIntConstant(
4681 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004682 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004683 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004684 return GetBlock()->GetGraph()->GetLongConstant(
4685 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004686 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004687 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4688 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4689 LOG(FATAL) << DebugName() << " is not defined for float values";
4690 UNREACHABLE();
4691 }
4692 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4693 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4694 LOG(FATAL) << DebugName() << " is not defined for double values";
4695 UNREACHABLE();
4696 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004697
4698 DECLARE_INSTRUCTION(Or);
4699
4700 private:
4701 DISALLOW_COPY_AND_ASSIGN(HOr);
4702};
4703
Vladimir Markofcb503c2016-05-18 12:48:17 +01004704class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004705 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004706 HXor(Primitive::Type result_type,
4707 HInstruction* left,
4708 HInstruction* right,
4709 uint32_t dex_pc = kNoDexPc)
4710 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004711
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004712 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004713
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004714 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004715
4716 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004717 return GetBlock()->GetGraph()->GetIntConstant(
4718 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004719 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004720 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004721 return GetBlock()->GetGraph()->GetLongConstant(
4722 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004723 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004724 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4725 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4726 LOG(FATAL) << DebugName() << " is not defined for float values";
4727 UNREACHABLE();
4728 }
4729 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4730 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4731 LOG(FATAL) << DebugName() << " is not defined for double values";
4732 UNREACHABLE();
4733 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004734
4735 DECLARE_INSTRUCTION(Xor);
4736
4737 private:
4738 DISALLOW_COPY_AND_ASSIGN(HXor);
4739};
4740
Vladimir Markofcb503c2016-05-18 12:48:17 +01004741class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004742 public:
4743 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004744 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004745 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4746 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004747 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004748
Roland Levillain5b5b9312016-03-22 14:57:31 +00004749 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004750 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004751 typedef typename std::make_unsigned<T>::type V;
4752 V ux = static_cast<V>(value);
4753 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004754 return static_cast<T>(ux);
4755 } else {
4756 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004757 return static_cast<T>(ux >> (distance & max_shift_value)) |
4758 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004759 }
4760 }
4761
Roland Levillain5b5b9312016-03-22 14:57:31 +00004762 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004763 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004764 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004765 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004766 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004767 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004768 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004769 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004770 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4771 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4772 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4773 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004774 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004775 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4776 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004777 LOG(FATAL) << DebugName() << " is not defined for float values";
4778 UNREACHABLE();
4779 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004780 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4781 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004782 LOG(FATAL) << DebugName() << " is not defined for double values";
4783 UNREACHABLE();
4784 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004785
4786 DECLARE_INSTRUCTION(Ror);
4787
4788 private:
4789 DISALLOW_COPY_AND_ASSIGN(HRor);
4790};
4791
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004792// The value of a parameter in this method. Its location depends on
4793// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004794class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004795 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004796 HParameterValue(const DexFile& dex_file,
4797 uint16_t type_index,
4798 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004799 Primitive::Type parameter_type,
4800 bool is_this = false)
4801 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004802 dex_file_(dex_file),
4803 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004804 index_(index) {
4805 SetPackedFlag<kFlagIsThis>(is_this);
4806 SetPackedFlag<kFlagCanBeNull>(!is_this);
4807 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004808
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004809 const DexFile& GetDexFile() const { return dex_file_; }
4810 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004811 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004812 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004813
Vladimir Markoa1de9182016-02-25 11:37:38 +00004814 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4815 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004816
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004817 DECLARE_INSTRUCTION(ParameterValue);
4818
4819 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004820 // Whether or not the parameter value corresponds to 'this' argument.
4821 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4822 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4823 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4824 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4825 "Too many packed fields.");
4826
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004827 const DexFile& dex_file_;
4828 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004829 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004830 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004831 const uint8_t index_;
4832
4833 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4834};
4835
Vladimir Markofcb503c2016-05-18 12:48:17 +01004836class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004837 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004838 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4839 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004840
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004841 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004842 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004843 return true;
4844 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004845
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004846 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004847
4848 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004849 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004850 }
4851 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004852 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004853 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004854 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4855 LOG(FATAL) << DebugName() << " is not defined for float values";
4856 UNREACHABLE();
4857 }
4858 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4859 LOG(FATAL) << DebugName() << " is not defined for double values";
4860 UNREACHABLE();
4861 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004862
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004863 DECLARE_INSTRUCTION(Not);
4864
4865 private:
4866 DISALLOW_COPY_AND_ASSIGN(HNot);
4867};
4868
Vladimir Markofcb503c2016-05-18 12:48:17 +01004869class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004870 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004871 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4872 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004873
4874 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004875 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004876 return true;
4877 }
4878
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004879 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004880 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004881 return !x;
4882 }
4883
Roland Levillain9867bc72015-08-05 10:21:34 +01004884 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004885 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004886 }
4887 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4888 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004889 UNREACHABLE();
4890 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004891 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4892 LOG(FATAL) << DebugName() << " is not defined for float values";
4893 UNREACHABLE();
4894 }
4895 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4896 LOG(FATAL) << DebugName() << " is not defined for double values";
4897 UNREACHABLE();
4898 }
David Brazdil66d126e2015-04-03 16:02:44 +01004899
4900 DECLARE_INSTRUCTION(BooleanNot);
4901
4902 private:
4903 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4904};
4905
Vladimir Markofcb503c2016-05-18 12:48:17 +01004906class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004907 public:
4908 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004909 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004910 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004911 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004912 // Invariant: We should never generate a conversion to a Boolean value.
4913 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004914 }
4915
4916 HInstruction* GetInput() const { return InputAt(0); }
4917 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4918 Primitive::Type GetResultType() const { return GetType(); }
4919
4920 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004921 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4922 return true;
4923 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004924
Mark Mendelle82549b2015-05-06 10:55:34 -04004925 // Try to statically evaluate the conversion and return a HConstant
4926 // containing the result. If the input cannot be converted, return nullptr.
4927 HConstant* TryStaticEvaluation() const;
4928
Roland Levillaindff1f282014-11-05 14:15:05 +00004929 DECLARE_INSTRUCTION(TypeConversion);
4930
4931 private:
4932 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4933};
4934
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004935static constexpr uint32_t kNoRegNumber = -1;
4936
Vladimir Markofcb503c2016-05-18 12:48:17 +01004937class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004938 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004939 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4940 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004941 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004942 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004943 SetRawInputAt(0, value);
4944 }
4945
Calin Juravle10e244f2015-01-26 18:54:32 +00004946 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004947 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004948 return true;
4949 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004950
Calin Juravle10e244f2015-01-26 18:54:32 +00004951 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004952
Calin Juravle10e244f2015-01-26 18:54:32 +00004953 bool CanThrow() const OVERRIDE { return true; }
4954
4955 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004956
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004957
4958 DECLARE_INSTRUCTION(NullCheck);
4959
4960 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004961 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4962};
4963
4964class FieldInfo : public ValueObject {
4965 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004966 FieldInfo(MemberOffset field_offset,
4967 Primitive::Type field_type,
4968 bool is_volatile,
4969 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004970 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004971 const DexFile& dex_file,
4972 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004973 : field_offset_(field_offset),
4974 field_type_(field_type),
4975 is_volatile_(is_volatile),
4976 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004977 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004978 dex_file_(dex_file),
4979 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004980
4981 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004982 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004983 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004984 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004985 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004986 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004987 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004988
4989 private:
4990 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004991 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004992 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004993 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004994 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004995 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004996 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004997};
4998
Vladimir Markofcb503c2016-05-18 12:48:17 +01004999class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005000 public:
5001 HInstanceFieldGet(HInstruction* value,
5002 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005003 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005004 bool is_volatile,
5005 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005006 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005007 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005008 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005009 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005010 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005011 field_info_(field_offset,
5012 field_type,
5013 is_volatile,
5014 field_idx,
5015 declaring_class_def_index,
5016 dex_file,
5017 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005018 SetRawInputAt(0, value);
5019 }
5020
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005021 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005022
Vladimir Marko372f10e2016-05-17 16:30:10 +01005023 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5024 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005025 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005026 }
5027
Calin Juravle641547a2015-04-21 22:08:51 +01005028 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005029 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005030 }
5031
5032 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005033 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5034 }
5035
Calin Juravle52c48962014-12-16 17:02:57 +00005036 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005037 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005038 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005039 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005040
5041 DECLARE_INSTRUCTION(InstanceFieldGet);
5042
5043 private:
5044 const FieldInfo field_info_;
5045
5046 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5047};
5048
Vladimir Markofcb503c2016-05-18 12:48:17 +01005049class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005050 public:
5051 HInstanceFieldSet(HInstruction* object,
5052 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005053 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005054 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005055 bool is_volatile,
5056 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005057 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005058 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005059 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005060 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005061 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005062 field_info_(field_offset,
5063 field_type,
5064 is_volatile,
5065 field_idx,
5066 declaring_class_def_index,
5067 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005068 dex_cache) {
5069 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005070 SetRawInputAt(0, object);
5071 SetRawInputAt(1, value);
5072 }
5073
Calin Juravle641547a2015-04-21 22:08:51 +01005074 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005075 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005076 }
5077
Calin Juravle52c48962014-12-16 17:02:57 +00005078 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005079 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005080 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005081 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005082 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005083 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5084 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005085
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005086 DECLARE_INSTRUCTION(InstanceFieldSet);
5087
5088 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005089 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5090 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5091 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5092 "Too many packed fields.");
5093
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005094 const FieldInfo field_info_;
5095
5096 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5097};
5098
Vladimir Markofcb503c2016-05-18 12:48:17 +01005099class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005100 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005101 HArrayGet(HInstruction* array,
5102 HInstruction* index,
5103 Primitive::Type type,
5104 uint32_t dex_pc,
5105 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005106 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005107 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005108 SetRawInputAt(0, array);
5109 SetRawInputAt(1, index);
5110 }
5111
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005112 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005113 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005114 return true;
5115 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005116 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005117 // TODO: We can be smarter here.
5118 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5119 // which generates the implicit null check. There are cases when these can be removed
5120 // to produce better code. If we ever add optimizations to do so we should allow an
5121 // implicit check here (as long as the address falls in the first page).
5122 return false;
5123 }
5124
David Brazdil4833f5a2015-12-16 10:37:39 +00005125 bool IsEquivalentOf(HArrayGet* other) const {
5126 bool result = (GetDexPc() == other->GetDexPc());
5127 if (kIsDebugBuild && result) {
5128 DCHECK_EQ(GetBlock(), other->GetBlock());
5129 DCHECK_EQ(GetArray(), other->GetArray());
5130 DCHECK_EQ(GetIndex(), other->GetIndex());
5131 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005132 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005133 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005134 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5135 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005136 }
5137 }
5138 return result;
5139 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005140
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005141 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5142
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005143 HInstruction* GetArray() const { return InputAt(0); }
5144 HInstruction* GetIndex() const { return InputAt(1); }
5145
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005146 DECLARE_INSTRUCTION(ArrayGet);
5147
5148 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005149 // We treat a String as an array, creating the HArrayGet from String.charAt()
5150 // intrinsic in the instruction simplifier. We can always determine whether
5151 // a particular HArrayGet is actually a String.charAt() by looking at the type
5152 // of the input but that requires holding the mutator lock, so we prefer to use
5153 // a flag, so that code generators don't need to do the locking.
5154 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5155 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5156 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5157 "Too many packed fields.");
5158
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005159 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5160};
5161
Vladimir Markofcb503c2016-05-18 12:48:17 +01005162class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005163 public:
5164 HArraySet(HInstruction* array,
5165 HInstruction* index,
5166 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005167 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005168 uint32_t dex_pc)
5169 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005170 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5171 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5172 SetPackedFlag<kFlagValueCanBeNull>(true);
5173 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005174 SetRawInputAt(0, array);
5175 SetRawInputAt(1, index);
5176 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005177 // Make a best guess now, may be refined during SSA building.
5178 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005179 }
5180
Calin Juravle77520bc2015-01-12 18:45:46 +00005181 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005182 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005183 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005184 }
5185
Mingyao Yang81014cb2015-06-02 03:16:27 -07005186 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005187 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005188
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005189 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005190 // TODO: Same as for ArrayGet.
5191 return false;
5192 }
5193
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005194 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005195 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005196 }
5197
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005198 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005199 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005200 }
5201
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005202 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005203 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005204 }
5205
Vladimir Markoa1de9182016-02-25 11:37:38 +00005206 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5207 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5208 bool StaticTypeOfArrayIsObjectArray() const {
5209 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5210 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005211
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005212 HInstruction* GetArray() const { return InputAt(0); }
5213 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005214 HInstruction* GetValue() const { return InputAt(2); }
5215
5216 Primitive::Type GetComponentType() const {
5217 // The Dex format does not type floating point index operations. Since the
5218 // `expected_component_type_` is set during building and can therefore not
5219 // be correct, we also check what is the value type. If it is a floating
5220 // point type, we must use that type.
5221 Primitive::Type value_type = GetValue()->GetType();
5222 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5223 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005224 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005225 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005226
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005227 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005228 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005229 }
5230
Aart Bik18b36ab2016-04-13 16:41:35 -07005231 void ComputeSideEffects() {
5232 Primitive::Type type = GetComponentType();
5233 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5234 SideEffectsForArchRuntimeCalls(type)));
5235 }
5236
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005237 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5238 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5239 }
5240
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005241 DECLARE_INSTRUCTION(ArraySet);
5242
5243 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005244 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5245 static constexpr size_t kFieldExpectedComponentTypeSize =
5246 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5247 static constexpr size_t kFlagNeedsTypeCheck =
5248 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5249 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005250 // Cached information for the reference_type_info_ so that codegen
5251 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005252 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5253 static constexpr size_t kNumberOfArraySetPackedBits =
5254 kFlagStaticTypeOfArrayIsObjectArray + 1;
5255 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5256 using ExpectedComponentTypeField =
5257 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005258
5259 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5260};
5261
Vladimir Markofcb503c2016-05-18 12:48:17 +01005262class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005263 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005264 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005265 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005266 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005267 // Note that arrays do not change length, so the instruction does not
5268 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005269 SetRawInputAt(0, array);
5270 }
5271
Calin Juravle77520bc2015-01-12 18:45:46 +00005272 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005273 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005274 return true;
5275 }
Calin Juravle641547a2015-04-21 22:08:51 +01005276 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5277 return obj == InputAt(0);
5278 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005279
Vladimir Markodce016e2016-04-28 13:10:02 +01005280 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5281
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005282 DECLARE_INSTRUCTION(ArrayLength);
5283
5284 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005285 // We treat a String as an array, creating the HArrayLength from String.length()
5286 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5287 // determine whether a particular HArrayLength is actually a String.length() by
5288 // looking at the type of the input but that requires holding the mutator lock, so
5289 // we prefer to use a flag, so that code generators don't need to do the locking.
5290 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5291 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5292 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5293 "Too many packed fields.");
5294
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005295 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5296};
5297
Vladimir Markofcb503c2016-05-18 12:48:17 +01005298class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005299 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005300 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5301 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005302 HBoundsCheck(HInstruction* index,
5303 HInstruction* length,
5304 uint32_t dex_pc,
5305 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5306 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5307 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005308 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005309 SetRawInputAt(0, index);
5310 SetRawInputAt(1, length);
5311 }
5312
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005313 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005314 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005315 return true;
5316 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005317
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005318 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005319
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005320 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005321
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005322 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5323 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5324
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005325 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005326
5327 DECLARE_INSTRUCTION(BoundsCheck);
5328
5329 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005330 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5331 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5332 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5333 // so we need to create an HEnvironment which will be translated to an InlineInfo
5334 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5335 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5336 // from the invoke as we don't want to look again at the dex bytecode.
5337 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5338
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005339 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5340};
5341
Vladimir Markofcb503c2016-05-18 12:48:17 +01005342class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005343 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005344 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005345 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005346
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005347 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005348 return true;
5349 }
5350
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005351 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5352 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005353
5354 DECLARE_INSTRUCTION(SuspendCheck);
5355
5356 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005357 // Only used for code generation, in order to share the same slow path between back edges
5358 // of a same loop.
5359 SlowPathCode* slow_path_;
5360
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005361 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5362};
5363
David Srbecky0cf44932015-12-09 14:09:59 +00005364// Pseudo-instruction which provides the native debugger with mapping information.
5365// It ensures that we can generate line number and local variables at this point.
5366class HNativeDebugInfo : public HTemplateInstruction<0> {
5367 public:
5368 explicit HNativeDebugInfo(uint32_t dex_pc)
5369 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5370
5371 bool NeedsEnvironment() const OVERRIDE {
5372 return true;
5373 }
5374
5375 DECLARE_INSTRUCTION(NativeDebugInfo);
5376
5377 private:
5378 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5379};
5380
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005381/**
5382 * Instruction to load a Class object.
5383 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005384class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005385 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005386 // Determines how to load the Class.
5387 enum class LoadKind {
5388 // Use the Class* from the method's own ArtMethod*.
5389 kReferrersClass,
5390
5391 // Use boot image Class* address that will be known at link time.
5392 // Used for boot image classes referenced by boot image code in non-PIC mode.
5393 kBootImageLinkTimeAddress,
5394
5395 // Use PC-relative boot image Class* address that will be known at link time.
5396 // Used for boot image classes referenced by boot image code in PIC mode.
5397 kBootImageLinkTimePcRelative,
5398
5399 // Use a known boot image Class* address, embedded in the code by the codegen.
5400 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5401 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5402 // GetIncludePatchInformation().
5403 kBootImageAddress,
5404
5405 // Load from the resolved types array at an absolute address.
5406 // Used for classes outside the boot image referenced by JIT-compiled code.
5407 kDexCacheAddress,
5408
5409 // Load from resolved types array in the dex cache using a PC-relative load.
5410 // Used for classes outside boot image when we know that we can access
5411 // the dex cache arrays using a PC-relative load.
5412 kDexCachePcRelative,
5413
5414 // Load from resolved types array accessed through the class loaded from
5415 // the compiled method's own ArtMethod*. This is the default access type when
5416 // all other types are unavailable.
5417 kDexCacheViaMethod,
5418
5419 kLast = kDexCacheViaMethod
5420 };
5421
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005422 HLoadClass(HCurrentMethod* current_method,
5423 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005424 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005425 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005426 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005427 bool needs_access_check,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005428 bool is_in_dex_cache,
5429 bool is_in_boot_image)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005430 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5431 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005432 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005433 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005434 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005435 // Referrers class should not need access check. We never inline unverified
5436 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005437 DCHECK(!is_referrers_class || !needs_access_check);
5438
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005439 SetPackedField<LoadKindField>(
5440 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005441 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5442 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005443 SetPackedFlag<kFlagIsInBootImage>(is_in_boot_image);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005444 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005445 }
5446
5447 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5448 DCHECK(HasAddress(load_kind));
5449 load_data_.address = address;
5450 SetLoadKindInternal(load_kind);
5451 }
5452
5453 void SetLoadKindWithTypeReference(LoadKind load_kind,
5454 const DexFile& dex_file,
5455 uint32_t type_index) {
5456 DCHECK(HasTypeReference(load_kind));
5457 DCHECK(IsSameDexFile(dex_file_, dex_file));
5458 DCHECK_EQ(type_index_, type_index);
5459 SetLoadKindInternal(load_kind);
5460 }
5461
5462 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5463 const DexFile& dex_file,
5464 uint32_t element_index) {
5465 DCHECK(HasDexCacheReference(load_kind));
5466 DCHECK(IsSameDexFile(dex_file_, dex_file));
5467 load_data_.dex_cache_element_index = element_index;
5468 SetLoadKindInternal(load_kind);
5469 }
5470
5471 LoadKind GetLoadKind() const {
5472 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005473 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005474
5475 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005476
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005477 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005478
5479 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5480
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005481 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005482
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005483 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005484 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005485 }
5486
Calin Juravle0ba218d2015-05-19 18:46:01 +01005487 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005488 // The entrypoint the code generator is going to call does not do
5489 // clinit of the class.
5490 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005491 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005492 }
5493
5494 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005495 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005496 (!IsReferrersClass() && !IsInDexCache()) ||
5497 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005498 }
5499
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005500
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005501 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005502 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005503 }
5504
Calin Juravleacf735c2015-02-12 15:25:22 +00005505 ReferenceTypeInfo GetLoadedClassRTI() {
5506 return loaded_class_rti_;
5507 }
5508
5509 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5510 // Make sure we only set exact types (the loaded class should never be merged).
5511 DCHECK(rti.IsExact());
5512 loaded_class_rti_ = rti;
5513 }
5514
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005515 uint32_t GetTypeIndex() const { return type_index_; }
5516 const DexFile& GetDexFile() const { return dex_file_; }
5517
5518 uint32_t GetDexCacheElementOffset() const;
5519
5520 uint64_t GetAddress() const {
5521 DCHECK(HasAddress(GetLoadKind()));
5522 return load_data_.address;
5523 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005524
Vladimir Markoa1de9182016-02-25 11:37:38 +00005525 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005526
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005527 static SideEffects SideEffectsForArchRuntimeCalls() {
5528 return SideEffects::CanTriggerGC();
5529 }
5530
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005531 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005532 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5533 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005534 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005535 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005536
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005537 void MarkInDexCache() {
5538 SetPackedFlag<kFlagIsInDexCache>(true);
5539 DCHECK(!NeedsEnvironment());
5540 RemoveEnvironment();
5541 SetSideEffects(SideEffects::None());
5542 }
5543
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005544 void MarkInBootImage() {
5545 SetPackedFlag<kFlagIsInBootImage>(true);
5546 }
5547
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005548 void AddSpecialInput(HInstruction* special_input);
5549
5550 using HInstruction::GetInputRecords; // Keep the const version visible.
5551 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5552 return ArrayRef<HUserRecord<HInstruction*>>(
5553 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5554 }
5555
5556 Primitive::Type GetType() const OVERRIDE {
5557 return Primitive::kPrimNot;
5558 }
5559
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005560 DECLARE_INSTRUCTION(LoadClass);
5561
5562 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005563 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005564 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005565 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005566 // Whether this instruction must generate the initialization check.
5567 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005568 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005569 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5570 static constexpr size_t kFieldLoadKindSize =
5571 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5572 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005573 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005574 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5575
5576 static bool HasTypeReference(LoadKind load_kind) {
5577 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5578 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5579 load_kind == LoadKind::kDexCacheViaMethod ||
5580 load_kind == LoadKind::kReferrersClass;
5581 }
5582
5583 static bool HasAddress(LoadKind load_kind) {
5584 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5585 }
5586
5587 static bool HasDexCacheReference(LoadKind load_kind) {
5588 return load_kind == LoadKind::kDexCachePcRelative;
5589 }
5590
5591 void SetLoadKindInternal(LoadKind load_kind);
5592
5593 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5594 // For other load kinds it's empty or possibly some architecture-specific instruction
5595 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5596 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005597
5598 const uint16_t type_index_;
5599 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005600
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005601 union {
5602 uint32_t dex_cache_element_index; // Only for dex cache reference.
5603 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5604 } load_data_;
5605
Calin Juravleacf735c2015-02-12 15:25:22 +00005606 ReferenceTypeInfo loaded_class_rti_;
5607
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005608 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5609};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005610std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5611
5612// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5613inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5614 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5615 return load_data_.dex_cache_element_index;
5616}
5617
5618// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5619inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005620 // The special input is used for PC-relative loads on some architectures,
5621 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005622 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005623 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5624 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5625 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005626 DCHECK(special_input_.GetInstruction() == nullptr);
5627 special_input_ = HUserRecord<HInstruction*>(special_input);
5628 special_input->AddUseAt(this, 0);
5629}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005630
Vladimir Marko372f10e2016-05-17 16:30:10 +01005631class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005632 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005633 // Determines how to load the String.
5634 enum class LoadKind {
5635 // Use boot image String* address that will be known at link time.
5636 // Used for boot image strings referenced by boot image code in non-PIC mode.
5637 kBootImageLinkTimeAddress,
5638
5639 // Use PC-relative boot image String* address that will be known at link time.
5640 // Used for boot image strings referenced by boot image code in PIC mode.
5641 kBootImageLinkTimePcRelative,
5642
5643 // Use a known boot image String* address, embedded in the code by the codegen.
5644 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5645 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5646 // GetIncludePatchInformation().
5647 kBootImageAddress,
5648
5649 // Load from the resolved strings array at an absolute address.
5650 // Used for strings outside the boot image referenced by JIT-compiled code.
5651 kDexCacheAddress,
5652
5653 // Load from resolved strings array in the dex cache using a PC-relative load.
5654 // Used for strings outside boot image when we know that we can access
5655 // the dex cache arrays using a PC-relative load.
5656 kDexCachePcRelative,
5657
5658 // Load from resolved strings array accessed through the class loaded from
5659 // the compiled method's own ArtMethod*. This is the default access type when
5660 // all other types are unavailable.
5661 kDexCacheViaMethod,
5662
5663 kLast = kDexCacheViaMethod
5664 };
5665
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005666 HLoadString(HCurrentMethod* current_method,
5667 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005668 const DexFile& dex_file,
5669 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005670 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5671 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005672 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005673 SetPackedFlag<kFlagIsInDexCache>(false);
5674 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5675 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005676 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005677
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005678 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5679 DCHECK(HasAddress(load_kind));
5680 load_data_.address = address;
5681 SetLoadKindInternal(load_kind);
5682 }
5683
5684 void SetLoadKindWithStringReference(LoadKind load_kind,
5685 const DexFile& dex_file,
5686 uint32_t string_index) {
5687 DCHECK(HasStringReference(load_kind));
5688 load_data_.ref.dex_file = &dex_file;
5689 string_index_ = string_index;
5690 SetLoadKindInternal(load_kind);
5691 }
5692
5693 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5694 const DexFile& dex_file,
5695 uint32_t element_index) {
5696 DCHECK(HasDexCacheReference(load_kind));
5697 load_data_.ref.dex_file = &dex_file;
5698 load_data_.ref.dex_cache_element_index = element_index;
5699 SetLoadKindInternal(load_kind);
5700 }
5701
5702 LoadKind GetLoadKind() const {
5703 return GetPackedField<LoadKindField>();
5704 }
5705
5706 const DexFile& GetDexFile() const;
5707
5708 uint32_t GetStringIndex() const {
5709 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5710 return string_index_;
5711 }
5712
5713 uint32_t GetDexCacheElementOffset() const;
5714
5715 uint64_t GetAddress() const {
5716 DCHECK(HasAddress(GetLoadKind()));
5717 return load_data_.address;
5718 }
5719
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005720 bool CanBeMoved() const OVERRIDE { return true; }
5721
Vladimir Marko372f10e2016-05-17 16:30:10 +01005722 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005723
5724 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5725
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005726 // Will call the runtime if we need to load the string through
5727 // the dex cache and the string is not guaranteed to be there yet.
5728 bool NeedsEnvironment() const OVERRIDE {
5729 LoadKind load_kind = GetLoadKind();
5730 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5731 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5732 load_kind == LoadKind::kBootImageAddress) {
5733 return false;
5734 }
5735 return !IsInDexCache();
5736 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005737
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005738 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5739 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5740 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005741
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005742 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005743 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005744
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005745 static SideEffects SideEffectsForArchRuntimeCalls() {
5746 return SideEffects::CanTriggerGC();
5747 }
5748
Vladimir Markoa1de9182016-02-25 11:37:38 +00005749 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5750
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005751 void MarkInDexCache() {
5752 SetPackedFlag<kFlagIsInDexCache>(true);
5753 DCHECK(!NeedsEnvironment());
5754 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005755 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005756 }
5757
Vladimir Marko372f10e2016-05-17 16:30:10 +01005758 void AddSpecialInput(HInstruction* special_input);
5759
5760 using HInstruction::GetInputRecords; // Keep the const version visible.
5761 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5762 return ArrayRef<HUserRecord<HInstruction*>>(
5763 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005764 }
5765
Vladimir Marko372f10e2016-05-17 16:30:10 +01005766 Primitive::Type GetType() const OVERRIDE {
5767 return Primitive::kPrimNot;
5768 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005769
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005770 DECLARE_INSTRUCTION(LoadString);
5771
5772 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005773 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005774 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5775 static constexpr size_t kFieldLoadKindSize =
5776 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5777 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005778 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005779 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005780
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005781 static bool HasStringReference(LoadKind load_kind) {
5782 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5783 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5784 load_kind == LoadKind::kDexCacheViaMethod;
5785 }
5786
5787 static bool HasAddress(LoadKind load_kind) {
5788 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5789 }
5790
5791 static bool HasDexCacheReference(LoadKind load_kind) {
5792 return load_kind == LoadKind::kDexCachePcRelative;
5793 }
5794
5795 void SetLoadKindInternal(LoadKind load_kind);
5796
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005797 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5798 // For other load kinds it's empty or possibly some architecture-specific instruction
5799 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005800 HUserRecord<HInstruction*> special_input_;
5801
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005802 // String index serves also as the hash code and it's also needed for slow-paths,
5803 // so it must not be overwritten with other load data.
5804 uint32_t string_index_;
5805
5806 union {
5807 struct {
5808 const DexFile* dex_file; // For string reference and dex cache reference.
5809 uint32_t dex_cache_element_index; // Only for dex cache reference.
5810 } ref;
5811 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5812 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005813
5814 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5815};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005816std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5817
5818// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5819inline const DexFile& HLoadString::GetDexFile() const {
5820 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5821 << GetLoadKind();
5822 return *load_data_.ref.dex_file;
5823}
5824
5825// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5826inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5827 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5828 return load_data_.ref.dex_cache_element_index;
5829}
5830
5831// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5832inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005833 // The special input is used for PC-relative loads on some architectures,
5834 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005835 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005836 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5837 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5838 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005839 // HLoadString::GetInputRecords() returns an empty array at this point,
5840 // so use the GetInputRecords() from the base class to set the input record.
5841 DCHECK(special_input_.GetInstruction() == nullptr);
5842 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005843 special_input->AddUseAt(this, 0);
5844}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005845
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005846/**
5847 * Performs an initialization check on its Class object input.
5848 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005849class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005850 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005851 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005852 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005853 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005854 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5855 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005856 SetRawInputAt(0, constant);
5857 }
5858
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005859 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005860 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005861 return true;
5862 }
5863
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005864 bool NeedsEnvironment() const OVERRIDE {
5865 // May call runtime to initialize the class.
5866 return true;
5867 }
5868
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005869 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005870
5871 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5872
5873 DECLARE_INSTRUCTION(ClinitCheck);
5874
5875 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005876 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5877};
5878
Vladimir Markofcb503c2016-05-18 12:48:17 +01005879class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005880 public:
5881 HStaticFieldGet(HInstruction* cls,
5882 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005883 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005884 bool is_volatile,
5885 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005886 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005887 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005888 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005889 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005890 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005891 field_info_(field_offset,
5892 field_type,
5893 is_volatile,
5894 field_idx,
5895 declaring_class_def_index,
5896 dex_file,
5897 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005898 SetRawInputAt(0, cls);
5899 }
5900
Calin Juravle52c48962014-12-16 17:02:57 +00005901
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005902 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005903
Vladimir Marko372f10e2016-05-17 16:30:10 +01005904 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5905 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005906 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005907 }
5908
5909 size_t ComputeHashCode() const OVERRIDE {
5910 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5911 }
5912
Calin Juravle52c48962014-12-16 17:02:57 +00005913 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005914 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5915 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005916 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005917
5918 DECLARE_INSTRUCTION(StaticFieldGet);
5919
5920 private:
5921 const FieldInfo field_info_;
5922
5923 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5924};
5925
Vladimir Markofcb503c2016-05-18 12:48:17 +01005926class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005927 public:
5928 HStaticFieldSet(HInstruction* cls,
5929 HInstruction* value,
5930 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005931 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005932 bool is_volatile,
5933 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005934 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005935 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005936 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005937 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005938 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005939 field_info_(field_offset,
5940 field_type,
5941 is_volatile,
5942 field_idx,
5943 declaring_class_def_index,
5944 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005945 dex_cache) {
5946 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005947 SetRawInputAt(0, cls);
5948 SetRawInputAt(1, value);
5949 }
5950
Calin Juravle52c48962014-12-16 17:02:57 +00005951 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005952 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5953 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005954 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005955
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005956 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005957 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5958 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005959
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005960 DECLARE_INSTRUCTION(StaticFieldSet);
5961
5962 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005963 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5964 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5965 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5966 "Too many packed fields.");
5967
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005968 const FieldInfo field_info_;
5969
5970 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5971};
5972
Vladimir Markofcb503c2016-05-18 12:48:17 +01005973class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005974 public:
5975 HUnresolvedInstanceFieldGet(HInstruction* obj,
5976 Primitive::Type field_type,
5977 uint32_t field_index,
5978 uint32_t dex_pc)
5979 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5980 field_index_(field_index) {
5981 SetRawInputAt(0, obj);
5982 }
5983
5984 bool NeedsEnvironment() const OVERRIDE { return true; }
5985 bool CanThrow() const OVERRIDE { return true; }
5986
5987 Primitive::Type GetFieldType() const { return GetType(); }
5988 uint32_t GetFieldIndex() const { return field_index_; }
5989
5990 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5991
5992 private:
5993 const uint32_t field_index_;
5994
5995 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5996};
5997
Vladimir Markofcb503c2016-05-18 12:48:17 +01005998class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005999 public:
6000 HUnresolvedInstanceFieldSet(HInstruction* obj,
6001 HInstruction* value,
6002 Primitive::Type field_type,
6003 uint32_t field_index,
6004 uint32_t dex_pc)
6005 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006006 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006007 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006008 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006009 SetRawInputAt(0, obj);
6010 SetRawInputAt(1, value);
6011 }
6012
6013 bool NeedsEnvironment() const OVERRIDE { return true; }
6014 bool CanThrow() const OVERRIDE { return true; }
6015
Vladimir Markoa1de9182016-02-25 11:37:38 +00006016 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006017 uint32_t GetFieldIndex() const { return field_index_; }
6018
6019 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6020
6021 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006022 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6023 static constexpr size_t kFieldFieldTypeSize =
6024 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6025 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6026 kFieldFieldType + kFieldFieldTypeSize;
6027 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6028 "Too many packed fields.");
6029 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6030
Calin Juravlee460d1d2015-09-29 04:52:17 +01006031 const uint32_t field_index_;
6032
6033 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6034};
6035
Vladimir Markofcb503c2016-05-18 12:48:17 +01006036class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006037 public:
6038 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6039 uint32_t field_index,
6040 uint32_t dex_pc)
6041 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6042 field_index_(field_index) {
6043 }
6044
6045 bool NeedsEnvironment() const OVERRIDE { return true; }
6046 bool CanThrow() const OVERRIDE { return true; }
6047
6048 Primitive::Type GetFieldType() const { return GetType(); }
6049 uint32_t GetFieldIndex() const { return field_index_; }
6050
6051 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6052
6053 private:
6054 const uint32_t field_index_;
6055
6056 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6057};
6058
Vladimir Markofcb503c2016-05-18 12:48:17 +01006059class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006060 public:
6061 HUnresolvedStaticFieldSet(HInstruction* value,
6062 Primitive::Type field_type,
6063 uint32_t field_index,
6064 uint32_t dex_pc)
6065 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006066 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006067 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006068 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006069 SetRawInputAt(0, value);
6070 }
6071
6072 bool NeedsEnvironment() const OVERRIDE { return true; }
6073 bool CanThrow() const OVERRIDE { return true; }
6074
Vladimir Markoa1de9182016-02-25 11:37:38 +00006075 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006076 uint32_t GetFieldIndex() const { return field_index_; }
6077
6078 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6079
6080 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006081 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6082 static constexpr size_t kFieldFieldTypeSize =
6083 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6084 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6085 kFieldFieldType + kFieldFieldTypeSize;
6086 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6087 "Too many packed fields.");
6088 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6089
Calin Juravlee460d1d2015-09-29 04:52:17 +01006090 const uint32_t field_index_;
6091
6092 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6093};
6094
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006095// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006096class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006097 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006098 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6099 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006100
David Brazdilbbd733e2015-08-18 17:48:17 +01006101 bool CanBeNull() const OVERRIDE { return false; }
6102
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006103 DECLARE_INSTRUCTION(LoadException);
6104
6105 private:
6106 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6107};
6108
David Brazdilcb1c0552015-08-04 16:22:25 +01006109// Implicit part of move-exception which clears thread-local exception storage.
6110// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006111class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006112 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006113 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6114 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006115
6116 DECLARE_INSTRUCTION(ClearException);
6117
6118 private:
6119 DISALLOW_COPY_AND_ASSIGN(HClearException);
6120};
6121
Vladimir Markofcb503c2016-05-18 12:48:17 +01006122class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006123 public:
6124 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006125 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006126 SetRawInputAt(0, exception);
6127 }
6128
6129 bool IsControlFlow() const OVERRIDE { return true; }
6130
6131 bool NeedsEnvironment() const OVERRIDE { return true; }
6132
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006133 bool CanThrow() const OVERRIDE { return true; }
6134
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006135
6136 DECLARE_INSTRUCTION(Throw);
6137
6138 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006139 DISALLOW_COPY_AND_ASSIGN(HThrow);
6140};
6141
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006142/**
6143 * Implementation strategies for the code generator of a HInstanceOf
6144 * or `HCheckCast`.
6145 */
6146enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006147 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006148 kExactCheck, // Can do a single class compare.
6149 kClassHierarchyCheck, // Can just walk the super class chain.
6150 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6151 kInterfaceCheck, // No optimization yet when checking against an interface.
6152 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006153 kArrayCheck, // No optimization yet when checking against a generic array.
6154 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006155};
6156
Roland Levillain86503782016-02-11 19:07:30 +00006157std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6158
Vladimir Markofcb503c2016-05-18 12:48:17 +01006159class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006160 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006161 HInstanceOf(HInstruction* object,
6162 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006163 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006164 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006165 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006166 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006167 dex_pc) {
6168 SetPackedField<TypeCheckKindField>(check_kind);
6169 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006170 SetRawInputAt(0, object);
6171 SetRawInputAt(1, constant);
6172 }
6173
6174 bool CanBeMoved() const OVERRIDE { return true; }
6175
Vladimir Marko372f10e2016-05-17 16:30:10 +01006176 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006177 return true;
6178 }
6179
6180 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006181 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006182 }
6183
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006184 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006185 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6186 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6187 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6188 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006189
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006190 static bool CanCallRuntime(TypeCheckKind check_kind) {
6191 // Mips currently does runtime calls for any other checks.
6192 return check_kind != TypeCheckKind::kExactCheck;
6193 }
6194
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006195 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006196 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006197 }
6198
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006199 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006200
6201 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006202 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6203 static constexpr size_t kFieldTypeCheckKindSize =
6204 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6205 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6206 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6207 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6208 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006209
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006210 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6211};
6212
Vladimir Markofcb503c2016-05-18 12:48:17 +01006213class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006214 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006215 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006216 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006217 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6218 SetPackedFlag<kFlagUpperCanBeNull>(true);
6219 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006220 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006221 SetRawInputAt(0, input);
6222 }
6223
David Brazdilf5552582015-12-27 13:36:12 +00006224 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006225 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006226 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006227 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006228
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006229 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006230 DCHECK(GetUpperCanBeNull() || !can_be_null);
6231 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006232 }
6233
Vladimir Markoa1de9182016-02-25 11:37:38 +00006234 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006235
Calin Juravleb1498f62015-02-16 13:13:29 +00006236 DECLARE_INSTRUCTION(BoundType);
6237
6238 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006239 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6240 // is false then CanBeNull() cannot be true).
6241 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6242 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6243 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6244 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6245
Calin Juravleb1498f62015-02-16 13:13:29 +00006246 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006247 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6248 // It is used to bound the type in cases like:
6249 // if (x instanceof ClassX) {
6250 // // uper_bound_ will be ClassX
6251 // }
David Brazdilf5552582015-12-27 13:36:12 +00006252 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006253
6254 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6255};
6256
Vladimir Markofcb503c2016-05-18 12:48:17 +01006257class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006258 public:
6259 HCheckCast(HInstruction* object,
6260 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006261 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006262 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006263 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6264 SetPackedField<TypeCheckKindField>(check_kind);
6265 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006266 SetRawInputAt(0, object);
6267 SetRawInputAt(1, constant);
6268 }
6269
6270 bool CanBeMoved() const OVERRIDE { return true; }
6271
Vladimir Marko372f10e2016-05-17 16:30:10 +01006272 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006273 return true;
6274 }
6275
6276 bool NeedsEnvironment() const OVERRIDE {
6277 // Instruction may throw a CheckCastError.
6278 return true;
6279 }
6280
6281 bool CanThrow() const OVERRIDE { return true; }
6282
Vladimir Markoa1de9182016-02-25 11:37:38 +00006283 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6284 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6285 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6286 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006287
6288 DECLARE_INSTRUCTION(CheckCast);
6289
6290 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006291 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6292 static constexpr size_t kFieldTypeCheckKindSize =
6293 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6294 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6295 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6296 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6297 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006298
6299 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006300};
6301
Andreas Gampe26de38b2016-07-27 17:53:11 -07006302/**
6303 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6304 * @details We define the combined barrier types that are actually required
6305 * by the Java Memory Model, rather than using exactly the terminology from
6306 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6307 * primitives. Note that the JSR-133 cookbook generally does not deal with
6308 * store atomicity issues, and the recipes there are not always entirely sufficient.
6309 * The current recipe is as follows:
6310 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6311 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6312 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6313 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6314 * class has final fields.
6315 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6316 * store-to-memory instructions. Only generated together with non-temporal stores.
6317 */
6318enum MemBarrierKind {
6319 kAnyStore,
6320 kLoadAny,
6321 kStoreStore,
6322 kAnyAny,
6323 kNTStoreStore,
6324 kLastBarrierKind = kNTStoreStore
6325};
6326std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6327
Vladimir Markofcb503c2016-05-18 12:48:17 +01006328class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006329 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006330 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006331 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006332 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6333 SetPackedField<BarrierKindField>(barrier_kind);
6334 }
Calin Juravle27df7582015-04-17 19:12:31 +01006335
Vladimir Markoa1de9182016-02-25 11:37:38 +00006336 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006337
6338 DECLARE_INSTRUCTION(MemoryBarrier);
6339
6340 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006341 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6342 static constexpr size_t kFieldBarrierKindSize =
6343 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6344 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6345 kFieldBarrierKind + kFieldBarrierKindSize;
6346 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6347 "Too many packed fields.");
6348 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006349
6350 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6351};
6352
Vladimir Markofcb503c2016-05-18 12:48:17 +01006353class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006354 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006355 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006356 kEnter,
6357 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006358 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006359 };
6360
6361 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006362 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006363 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6364 dex_pc) {
6365 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006366 SetRawInputAt(0, object);
6367 }
6368
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006369 // Instruction may go into runtime, so we need an environment.
6370 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006371
6372 bool CanThrow() const OVERRIDE {
6373 // Verifier guarantees that monitor-exit cannot throw.
6374 // This is important because it allows the HGraphBuilder to remove
6375 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6376 return IsEnter();
6377 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006378
Vladimir Markoa1de9182016-02-25 11:37:38 +00006379 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6380 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006381
6382 DECLARE_INSTRUCTION(MonitorOperation);
6383
Calin Juravle52c48962014-12-16 17:02:57 +00006384 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006385 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6386 static constexpr size_t kFieldOperationKindSize =
6387 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6388 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6389 kFieldOperationKind + kFieldOperationKindSize;
6390 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6391 "Too many packed fields.");
6392 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006393
6394 private:
6395 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6396};
6397
Vladimir Markofcb503c2016-05-18 12:48:17 +01006398class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006399 public:
6400 HSelect(HInstruction* condition,
6401 HInstruction* true_value,
6402 HInstruction* false_value,
6403 uint32_t dex_pc)
6404 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6405 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6406
6407 // First input must be `true_value` or `false_value` to allow codegens to
6408 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6409 // that architectures which implement HSelect as a conditional move also
6410 // will not need to invert the condition.
6411 SetRawInputAt(0, false_value);
6412 SetRawInputAt(1, true_value);
6413 SetRawInputAt(2, condition);
6414 }
6415
6416 HInstruction* GetFalseValue() const { return InputAt(0); }
6417 HInstruction* GetTrueValue() const { return InputAt(1); }
6418 HInstruction* GetCondition() const { return InputAt(2); }
6419
6420 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006421 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6422 return true;
6423 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006424
6425 bool CanBeNull() const OVERRIDE {
6426 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6427 }
6428
6429 DECLARE_INSTRUCTION(Select);
6430
6431 private:
6432 DISALLOW_COPY_AND_ASSIGN(HSelect);
6433};
6434
Vladimir Markof9f64412015-09-02 14:05:49 +01006435class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006436 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006437 MoveOperands(Location source,
6438 Location destination,
6439 Primitive::Type type,
6440 HInstruction* instruction)
6441 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006442
6443 Location GetSource() const { return source_; }
6444 Location GetDestination() const { return destination_; }
6445
6446 void SetSource(Location value) { source_ = value; }
6447 void SetDestination(Location value) { destination_ = value; }
6448
6449 // The parallel move resolver marks moves as "in-progress" by clearing the
6450 // destination (but not the source).
6451 Location MarkPending() {
6452 DCHECK(!IsPending());
6453 Location dest = destination_;
6454 destination_ = Location::NoLocation();
6455 return dest;
6456 }
6457
6458 void ClearPending(Location dest) {
6459 DCHECK(IsPending());
6460 destination_ = dest;
6461 }
6462
6463 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006464 DCHECK(source_.IsValid() || destination_.IsInvalid());
6465 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006466 }
6467
6468 // True if this blocks a move from the given location.
6469 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006470 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006471 }
6472
6473 // A move is redundant if it's been eliminated, if its source and
6474 // destination are the same, or if its destination is unneeded.
6475 bool IsRedundant() const {
6476 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6477 }
6478
6479 // We clear both operands to indicate move that's been eliminated.
6480 void Eliminate() {
6481 source_ = destination_ = Location::NoLocation();
6482 }
6483
6484 bool IsEliminated() const {
6485 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6486 return source_.IsInvalid();
6487 }
6488
Alexey Frunze4dda3372015-06-01 18:31:49 -07006489 Primitive::Type GetType() const { return type_; }
6490
Nicolas Geoffray90218252015-04-15 11:56:51 +01006491 bool Is64BitMove() const {
6492 return Primitive::Is64BitType(type_);
6493 }
6494
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006495 HInstruction* GetInstruction() const { return instruction_; }
6496
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006497 private:
6498 Location source_;
6499 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006500 // The type this move is for.
6501 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006502 // The instruction this move is assocatied with. Null when this move is
6503 // for moving an input in the expected locations of user (including a phi user).
6504 // This is only used in debug mode, to ensure we do not connect interval siblings
6505 // in the same parallel move.
6506 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006507};
6508
Roland Levillainc9285912015-12-18 10:38:42 +00006509std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6510
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006511static constexpr size_t kDefaultNumberOfMoves = 4;
6512
Vladimir Markofcb503c2016-05-18 12:48:17 +01006513class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006514 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006515 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006516 : HTemplateInstruction(SideEffects::None(), dex_pc),
6517 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6518 moves_.reserve(kDefaultNumberOfMoves);
6519 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006520
Nicolas Geoffray90218252015-04-15 11:56:51 +01006521 void AddMove(Location source,
6522 Location destination,
6523 Primitive::Type type,
6524 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006525 DCHECK(source.IsValid());
6526 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006527 if (kIsDebugBuild) {
6528 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006529 for (const MoveOperands& move : moves_) {
6530 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006531 // Special case the situation where the move is for the spill slot
6532 // of the instruction.
6533 if ((GetPrevious() == instruction)
6534 || ((GetPrevious() == nullptr)
6535 && instruction->IsPhi()
6536 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006537 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006538 << "Doing parallel moves for the same instruction.";
6539 } else {
6540 DCHECK(false) << "Doing parallel moves for the same instruction.";
6541 }
6542 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006543 }
6544 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006545 for (const MoveOperands& move : moves_) {
6546 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006547 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006548 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006549 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006550 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006551 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006552 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006553 }
6554
Vladimir Marko225b6462015-09-28 12:17:40 +01006555 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006556 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006557 }
6558
Vladimir Marko225b6462015-09-28 12:17:40 +01006559 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006560
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006561 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006562
6563 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006564 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006565
6566 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6567};
6568
Mark Mendell0616ae02015-04-17 12:49:27 -04006569} // namespace art
6570
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006571#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6572#include "nodes_shared.h"
6573#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006574#ifdef ART_ENABLE_CODEGEN_arm
6575#include "nodes_arm.h"
6576#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006577#ifdef ART_ENABLE_CODEGEN_arm64
6578#include "nodes_arm64.h"
6579#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006580#ifdef ART_ENABLE_CODEGEN_mips
6581#include "nodes_mips.h"
6582#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006583#ifdef ART_ENABLE_CODEGEN_x86
6584#include "nodes_x86.h"
6585#endif
6586
6587namespace art {
6588
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006589class HGraphVisitor : public ValueObject {
6590 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006591 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6592 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006593
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006594 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006595 virtual void VisitBasicBlock(HBasicBlock* block);
6596
Roland Levillain633021e2014-10-01 14:12:25 +01006597 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006598 void VisitInsertionOrder();
6599
Roland Levillain633021e2014-10-01 14:12:25 +01006600 // Visit the graph following dominator tree reverse post-order.
6601 void VisitReversePostOrder();
6602
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006603 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006604
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006605 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006606#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006607 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6608
6609 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6610
6611#undef DECLARE_VISIT_INSTRUCTION
6612
6613 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006614 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006615
6616 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6617};
6618
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006619class HGraphDelegateVisitor : public HGraphVisitor {
6620 public:
6621 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6622 virtual ~HGraphDelegateVisitor() {}
6623
6624 // Visit functions that delegate to to super class.
6625#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006626 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006627
6628 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6629
6630#undef DECLARE_VISIT_INSTRUCTION
6631
6632 private:
6633 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6634};
6635
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006636class HInsertionOrderIterator : public ValueObject {
6637 public:
6638 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6639
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006640 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006641 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006642 void Advance() { ++index_; }
6643
6644 private:
6645 const HGraph& graph_;
6646 size_t index_;
6647
6648 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6649};
6650
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006651class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006652 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006653 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6654 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006655 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006656 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006657
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006658 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6659 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006660 void Advance() { ++index_; }
6661
6662 private:
6663 const HGraph& graph_;
6664 size_t index_;
6665
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006666 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006667};
6668
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006669class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006670 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006671 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006672 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006673 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006674 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006675 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006676
6677 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006678 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006679 void Advance() { --index_; }
6680
6681 private:
6682 const HGraph& graph_;
6683 size_t index_;
6684
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006685 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006686};
6687
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006688class HLinearPostOrderIterator : public ValueObject {
6689 public:
6690 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006691 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006692
6693 bool Done() const { return index_ == 0; }
6694
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006695 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006696
6697 void Advance() {
6698 --index_;
6699 DCHECK_GE(index_, 0U);
6700 }
6701
6702 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006703 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006704 size_t index_;
6705
6706 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6707};
6708
6709class HLinearOrderIterator : public ValueObject {
6710 public:
6711 explicit HLinearOrderIterator(const HGraph& graph)
6712 : order_(graph.GetLinearOrder()), index_(0) {}
6713
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006714 bool Done() const { return index_ == order_.size(); }
6715 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006716 void Advance() { ++index_; }
6717
6718 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006719 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006720 size_t index_;
6721
6722 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6723};
6724
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006725// Iterator over the blocks that art part of the loop. Includes blocks part
6726// of an inner loop. The order in which the blocks are iterated is on their
6727// block id.
6728class HBlocksInLoopIterator : public ValueObject {
6729 public:
6730 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6731 : blocks_in_loop_(info.GetBlocks()),
6732 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6733 index_(0) {
6734 if (!blocks_in_loop_.IsBitSet(index_)) {
6735 Advance();
6736 }
6737 }
6738
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006739 bool Done() const { return index_ == blocks_.size(); }
6740 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006741 void Advance() {
6742 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006743 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006744 if (blocks_in_loop_.IsBitSet(index_)) {
6745 break;
6746 }
6747 }
6748 }
6749
6750 private:
6751 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006752 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006753 size_t index_;
6754
6755 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6756};
6757
Mingyao Yang3584bce2015-05-19 16:01:59 -07006758// Iterator over the blocks that art part of the loop. Includes blocks part
6759// of an inner loop. The order in which the blocks are iterated is reverse
6760// post order.
6761class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6762 public:
6763 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6764 : blocks_in_loop_(info.GetBlocks()),
6765 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6766 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006767 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006768 Advance();
6769 }
6770 }
6771
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006772 bool Done() const { return index_ == blocks_.size(); }
6773 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006774 void Advance() {
6775 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006776 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6777 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006778 break;
6779 }
6780 }
6781 }
6782
6783 private:
6784 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006785 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006786 size_t index_;
6787
6788 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6789};
6790
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006791inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006792 if (constant->IsIntConstant()) {
6793 return constant->AsIntConstant()->GetValue();
6794 } else if (constant->IsLongConstant()) {
6795 return constant->AsLongConstant()->GetValue();
6796 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006797 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006798 return 0;
6799 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006800}
6801
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006802#define INSTRUCTION_TYPE_CHECK(type, super) \
6803 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6804 inline const H##type* HInstruction::As##type() const { \
6805 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6806 } \
6807 inline H##type* HInstruction::As##type() { \
6808 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6809 }
6810
6811 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6812#undef INSTRUCTION_TYPE_CHECK
6813
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006814// Create space in `blocks` for adding `number_of_new_blocks` entries
6815// starting at location `at`. Blocks after `at` are moved accordingly.
6816inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6817 size_t number_of_new_blocks,
6818 size_t after) {
6819 DCHECK_LT(after, blocks->size());
6820 size_t old_size = blocks->size();
6821 size_t new_size = old_size + number_of_new_blocks;
6822 blocks->resize(new_size);
6823 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6824}
6825
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006826} // namespace art
6827
6828#endif // ART_COMPILER_OPTIMIZING_NODES_H_