blob: 149a71d1b92c3bd893b3e34eb6879810ea5bb153 [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"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010028#include "dex_file.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000038#include "utils/array_ref.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010039#include "utils/intrusive_forward_list.h"
Vladimir Marko372f10e2016-05-17 16:30:10 +010040#include "utils/transform_array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000041
42namespace art {
43
David Brazdil1abb4192015-02-17 18:33:36 +000044class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010046class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010048class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000050class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000051class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000053class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000054class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000055class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000056class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010057class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010058class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010059class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010060class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000061class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010062class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000063class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000064
Mathieu Chartier736b5602015-09-02 14:54:11 -070065namespace mirror {
66class DexCache;
67} // namespace mirror
68
Nicolas Geoffray818f2102014-02-18 16:43:35 +000069static const int kDefaultNumberOfBlocks = 8;
70static const int kDefaultNumberOfSuccessors = 2;
71static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010072static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010073static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000074static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000075
Roland Levillain5b5b9312016-03-22 14:57:31 +000076// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
77static constexpr int32_t kMaxIntShiftDistance = 0x1f;
78// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
79static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000080
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010081static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070082static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010083
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010084static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
85
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060086static constexpr uint32_t kNoDexPc = -1;
87
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010088inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
89 // For the purposes of the compiler, the dex files must actually be the same object
90 // if we want to safely treat them as the same. This is especially important for JIT
91 // as custom class loaders can open the same underlying file (or memory) multiple
92 // times and provide different class resolution but no two class loaders should ever
93 // use the same DexFile object - doing so is an unsupported hack that can lead to
94 // all sorts of weird failures.
95 return &lhs == &rhs;
96}
97
Dave Allison20dfc792014-06-16 20:44:29 -070098enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070099 // All types.
100 kCondEQ, // ==
101 kCondNE, // !=
102 // Signed integers and floating-point numbers.
103 kCondLT, // <
104 kCondLE, // <=
105 kCondGT, // >
106 kCondGE, // >=
107 // Unsigned integers.
108 kCondB, // <
109 kCondBE, // <=
110 kCondA, // >
111 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -0700112};
113
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000114enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000115 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000116 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000117 kAnalysisFailThrowCatchLoop,
118 kAnalysisFailAmbiguousArrayOp,
119 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000120};
121
Vladimir Markof9f64412015-09-02 14:05:49 +0100122class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100123 public:
124 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
125
126 void AddInstruction(HInstruction* instruction);
127 void RemoveInstruction(HInstruction* instruction);
128
David Brazdilc3d743f2015-04-22 13:40:50 +0100129 // Insert `instruction` before/after an existing instruction `cursor`.
130 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
131 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
132
Roland Levillain6b469232014-09-25 10:10:38 +0100133 // Return true if this list contains `instruction`.
134 bool Contains(HInstruction* instruction) const;
135
Roland Levillainccc07a92014-09-16 14:48:16 +0100136 // Return true if `instruction1` is found before `instruction2` in
137 // this instruction list and false otherwise. Abort if none
138 // of these instructions is found.
139 bool FoundBefore(const HInstruction* instruction1,
140 const HInstruction* instruction2) const;
141
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000142 bool IsEmpty() const { return first_instruction_ == nullptr; }
143 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
144
145 // Update the block of all instructions to be `block`.
146 void SetBlockOfInstructions(HBasicBlock* block) const;
147
148 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000149 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000150 void Add(const HInstructionList& instruction_list);
151
David Brazdil2d7352b2015-04-20 14:52:42 +0100152 // Return the number of instructions in the list. This is an expensive operation.
153 size_t CountSize() const;
154
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100155 private:
156 HInstruction* first_instruction_;
157 HInstruction* last_instruction_;
158
159 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 friend class HGraph;
161 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100162 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100163 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100164
165 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
166};
167
David Brazdil4833f5a2015-12-16 10:37:39 +0000168class ReferenceTypeInfo : ValueObject {
169 public:
170 typedef Handle<mirror::Class> TypeHandle;
171
Vladimir Markoa1de9182016-02-25 11:37:38 +0000172 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
173
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700174 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100175 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
176 }
177
Vladimir Markoa1de9182016-02-25 11:37:38 +0000178 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000179 return ReferenceTypeInfo(type_handle, is_exact);
180 }
181
182 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
183
Vladimir Markof39745e2016-01-26 12:16:55 +0000184 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000185 return handle.GetReference() != nullptr;
186 }
187
Vladimir Marko456307a2016-04-19 14:12:13 +0000188 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000189 return IsValidHandle(type_handle_);
190 }
191
192 bool IsExact() const { return is_exact_; }
193
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700194 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000195 DCHECK(IsValid());
196 return GetTypeHandle()->IsObjectClass();
197 }
198
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700199 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000200 DCHECK(IsValid());
201 return GetTypeHandle()->IsStringClass();
202 }
203
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700204 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000205 DCHECK(IsValid());
206 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
207 }
208
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700209 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000210 DCHECK(IsValid());
211 return GetTypeHandle()->IsInterface();
212 }
213
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700214 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000215 DCHECK(IsValid());
216 return GetTypeHandle()->IsArrayClass();
217 }
218
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000220 DCHECK(IsValid());
221 return GetTypeHandle()->IsPrimitiveArray();
222 }
223
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700224 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000225 DCHECK(IsValid());
226 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
227 }
228
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700229 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000230 DCHECK(IsValid());
231 if (!IsExact()) return false;
232 if (!IsArrayClass()) return false;
233 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
234 }
235
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000237 DCHECK(IsValid());
238 if (!IsExact()) return false;
239 if (!IsArrayClass()) return false;
240 if (!rti.IsArrayClass()) return false;
241 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
242 rti.GetTypeHandle()->GetComponentType());
243 }
244
245 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
246
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700247 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000248 DCHECK(IsValid());
249 DCHECK(rti.IsValid());
250 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
251 }
252
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700253 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000254 DCHECK(IsValid());
255 DCHECK(rti.IsValid());
256 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
257 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
258 }
259
260 // Returns true if the type information provide the same amount of details.
261 // Note that it does not mean that the instructions have the same actual type
262 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700263 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000264 if (!IsValid() && !rti.IsValid()) {
265 // Invalid types are equal.
266 return true;
267 }
268 if (!IsValid() || !rti.IsValid()) {
269 // One is valid, the other not.
270 return false;
271 }
272 return IsExact() == rti.IsExact()
273 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
274 }
275
276 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000277 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
278 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
279 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000280
281 // The class of the object.
282 TypeHandle type_handle_;
283 // Whether or not the type is exact or a superclass of the actual type.
284 // Whether or not we have any information about this type.
285 bool is_exact_;
286};
287
288std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
289
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000290// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100291class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000292 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100293 HGraph(ArenaAllocator* arena,
294 const DexFile& dex_file,
295 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100296 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700297 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100298 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100299 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000300 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100301 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000302 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100303 blocks_(arena->Adapter(kArenaAllocBlockList)),
304 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
305 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700306 entry_block_(nullptr),
307 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100308 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100309 number_of_vregs_(0),
310 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000311 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400312 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000313 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000314 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000315 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000316 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 dex_file_(dex_file),
318 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100319 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100320 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100321 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700322 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000323 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100324 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
325 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
326 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
327 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000328 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000329 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
330 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100331 blocks_.reserve(kDefaultNumberOfBlocks);
332 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000333
David Brazdilbadd8262016-02-02 16:28:56 +0000334 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
335 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
336
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000337 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100338 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
339
David Brazdil69ba7b72015-06-23 18:27:30 +0100340 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000341 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100342
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000343 HBasicBlock* GetEntryBlock() const { return entry_block_; }
344 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100345 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000346
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000347 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
348 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000349
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000350 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100351
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100352 void ComputeDominanceInformation();
353 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000354 void ClearLoopInformation();
355 void FindBackEdges(ArenaBitVector* visited);
356 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100357 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100358 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000359
David Brazdil4833f5a2015-12-16 10:37:39 +0000360 // Analyze all natural loops in this graph. Returns a code specifying that it
361 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000362 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000363 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100364
David Brazdilffee3d32015-07-06 11:48:53 +0100365 // Iterate over blocks to compute try block membership. Needs reverse post
366 // order and loop information.
367 void ComputeTryBlockInformation();
368
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000369 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000370 // Returns the instruction to replace the invoke expression or null if the
371 // invoke is for a void method. Note that the caller is responsible for replacing
372 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000373 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000374
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000375 // Update the loop and try membership of `block`, which was spawned from `reference`.
376 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
377 // should be the new back edge.
378 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
379 HBasicBlock* reference,
380 bool replace_if_back_edge);
381
Mingyao Yang3584bce2015-05-19 16:01:59 -0700382 // Need to add a couple of blocks to test if the loop body is entered and
383 // put deoptimization instructions, etc.
384 void TransformLoopHeaderForBCE(HBasicBlock* header);
385
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000386 // Removes `block` from the graph. Assumes `block` has been disconnected from
387 // other blocks and has no instructions or phis.
388 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000389
David Brazdilfc6a86a2015-06-26 10:33:45 +0000390 // Splits the edge between `block` and `successor` while preserving the
391 // indices in the predecessor/successor lists. If there are multiple edges
392 // between the blocks, the lowest indices are used.
393 // Returns the new block which is empty and has the same dex pc as `successor`.
394 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
395
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100396 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
397 void SimplifyLoop(HBasicBlock* header);
398
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000399 int32_t GetNextInstructionId() {
400 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000401 return current_instruction_id_++;
402 }
403
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000404 int32_t GetCurrentInstructionId() const {
405 return current_instruction_id_;
406 }
407
408 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000409 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000410 current_instruction_id_ = id;
411 }
412
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100413 uint16_t GetMaximumNumberOfOutVRegs() const {
414 return maximum_number_of_out_vregs_;
415 }
416
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000417 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
418 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100419 }
420
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100421 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
422 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
423 }
424
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000425 void UpdateTemporariesVRegSlots(size_t slots) {
426 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100427 }
428
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000429 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100430 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000431 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100432 }
433
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100434 void SetNumberOfVRegs(uint16_t number_of_vregs) {
435 number_of_vregs_ = number_of_vregs;
436 }
437
438 uint16_t GetNumberOfVRegs() const {
439 return number_of_vregs_;
440 }
441
442 void SetNumberOfInVRegs(uint16_t value) {
443 number_of_in_vregs_ = value;
444 }
445
David Brazdildee58d62016-04-07 09:54:26 +0000446 uint16_t GetNumberOfInVRegs() const {
447 return number_of_in_vregs_;
448 }
449
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100450 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100451 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100452 return number_of_vregs_ - number_of_in_vregs_;
453 }
454
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100455 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100456 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100457 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100458
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100459 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100460 return linear_order_;
461 }
462
Mark Mendell1152c922015-04-24 17:06:35 -0400463 bool HasBoundsChecks() const {
464 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800465 }
466
Mark Mendell1152c922015-04-24 17:06:35 -0400467 void SetHasBoundsChecks(bool value) {
468 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800469 }
470
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100471 bool ShouldGenerateConstructorBarrier() const {
472 return should_generate_constructor_barrier_;
473 }
474
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000475 bool IsDebuggable() const { return debuggable_; }
476
David Brazdil8d5b8b22015-03-24 10:51:52 +0000477 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000478 // already, it is created and inserted into the graph. This method is only for
479 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600480 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000481
482 // TODO: This is problematic for the consistency of reference type propagation
483 // because it can be created anytime after the pass and thus it will be left
484 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600485 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000486
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600487 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
488 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000489 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600490 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
491 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000492 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600493 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
494 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000495 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600496 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
497 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000498 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000499
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100500 HCurrentMethod* GetCurrentMethod();
501
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100502 const DexFile& GetDexFile() const {
503 return dex_file_;
504 }
505
506 uint32_t GetMethodIdx() const {
507 return method_idx_;
508 }
509
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100510 InvokeType GetInvokeType() const {
511 return invoke_type_;
512 }
513
Mark Mendellc4701932015-04-10 13:18:51 -0400514 InstructionSet GetInstructionSet() const {
515 return instruction_set_;
516 }
517
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000518 bool IsCompilingOsr() const { return osr_; }
519
David Brazdil77a48ae2015-09-15 12:34:04 +0000520 bool HasTryCatch() const { return has_try_catch_; }
521 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100522
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000523 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
524 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
525
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100526 ArtMethod* GetArtMethod() const { return art_method_; }
527 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
528
Mark Mendellf6529172015-11-17 11:16:56 -0500529 // Returns an instruction with the opposite boolean value from 'cond'.
530 // The instruction has been inserted into the graph, either as a constant, or
531 // before cursor.
532 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
533
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000534 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
535
David Brazdil2d7352b2015-04-20 14:52:42 +0100536 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000537 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100538 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000539
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000540 template <class InstructionType, typename ValueType>
541 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600542 ArenaSafeMap<ValueType, InstructionType*>* cache,
543 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000544 // Try to find an existing constant of the given value.
545 InstructionType* constant = nullptr;
546 auto cached_constant = cache->find(value);
547 if (cached_constant != cache->end()) {
548 constant = cached_constant->second;
549 }
550
551 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100552 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000553 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600554 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000555 cache->Overwrite(value, constant);
556 InsertConstant(constant);
557 }
558 return constant;
559 }
560
David Brazdil8d5b8b22015-03-24 10:51:52 +0000561 void InsertConstant(HConstant* instruction);
562
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000563 // Cache a float constant into the graph. This method should only be
564 // called by the SsaBuilder when creating "equivalent" instructions.
565 void CacheFloatConstant(HFloatConstant* constant);
566
567 // See CacheFloatConstant comment.
568 void CacheDoubleConstant(HDoubleConstant* constant);
569
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000570 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000571
572 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100573 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000574
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100575 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100576 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000577
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100578 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100579 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100580
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000581 HBasicBlock* entry_block_;
582 HBasicBlock* exit_block_;
583
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100584 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100585 uint16_t maximum_number_of_out_vregs_;
586
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100587 // The number of virtual registers in this method. Contains the parameters.
588 uint16_t number_of_vregs_;
589
590 // The number of virtual registers used by parameters of this method.
591 uint16_t number_of_in_vregs_;
592
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000593 // Number of vreg size slots that the temporaries use (used in baseline compiler).
594 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100595
Mark Mendell1152c922015-04-24 17:06:35 -0400596 // Has bounds checks. We can totally skip BCE if it's false.
597 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800598
David Brazdil77a48ae2015-09-15 12:34:04 +0000599 // Flag whether there are any try/catch blocks in the graph. We will skip
600 // try/catch-related passes if false.
601 bool has_try_catch_;
602
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000603 // Flag whether there are any irreducible loops in the graph.
604 bool has_irreducible_loops_;
605
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000606 // Indicates whether the graph should be compiled in a way that
607 // ensures full debuggability. If false, we can apply more
608 // aggressive optimizations that may limit the level of debugging.
609 const bool debuggable_;
610
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000611 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000612 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000613
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100614 // The dex file from which the method is from.
615 const DexFile& dex_file_;
616
617 // The method index in the dex file.
618 const uint32_t method_idx_;
619
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100620 // If inlined, this encodes how the callee is being invoked.
621 const InvokeType invoke_type_;
622
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100623 // Whether the graph has been transformed to SSA form. Only used
624 // in debug mode to ensure we are not using properties only valid
625 // for non-SSA form (like the number of temporaries).
626 bool in_ssa_form_;
627
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100628 const bool should_generate_constructor_barrier_;
629
Mathieu Chartiere401d142015-04-22 13:56:20 -0700630 const InstructionSet instruction_set_;
631
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000632 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000633 HNullConstant* cached_null_constant_;
634 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000635 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000636 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000637 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000638
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100639 HCurrentMethod* cached_current_method_;
640
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100641 // The ArtMethod this graph is for. Note that for AOT, it may be null,
642 // for example for methods whose declaring class could not be resolved
643 // (such as when the superclass could not be found).
644 ArtMethod* art_method_;
645
David Brazdil4833f5a2015-12-16 10:37:39 +0000646 // Keep the RTI of inexact Object to avoid having to pass stack handle
647 // collection pointer to passes which may create NullConstant.
648 ReferenceTypeInfo inexact_object_rti_;
649
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000650 // Whether we are compiling this graph for on stack replacement: this will
651 // make all loops seen as irreducible and emit special stack maps to mark
652 // compiled code entries which the interpreter can directly jump to.
653 const bool osr_;
654
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000655 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100656 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000657 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000658 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000659 DISALLOW_COPY_AND_ASSIGN(HGraph);
660};
661
Vladimir Markof9f64412015-09-02 14:05:49 +0100662class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000663 public:
664 HLoopInformation(HBasicBlock* header, HGraph* graph)
665 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100666 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000667 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100668 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100669 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100670 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000671 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100672 back_edges_.reserve(kDefaultNumberOfBackEdges);
673 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100674
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000675 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100676 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000677
678 void Dump(std::ostream& os);
679
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100680 HBasicBlock* GetHeader() const {
681 return header_;
682 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000683
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000684 void SetHeader(HBasicBlock* block) {
685 header_ = block;
686 }
687
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100688 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
689 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
690 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
691
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000692 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100693 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000694 }
695
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100696 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100697 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100698 }
699
David Brazdil46e2a392015-03-16 17:31:52 +0000700 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100701 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100702 }
703
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000704 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100705 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000706 }
707
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100708 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100709
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100710 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100711 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100712 }
713
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100714 // Returns the lifetime position of the back edge that has the
715 // greatest lifetime position.
716 size_t GetLifetimeEnd() const;
717
718 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100719 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100720 }
721
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000722 // Finds blocks that are part of this loop.
723 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100724
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100725 // Returns whether this loop information contains `block`.
726 // Note that this loop information *must* be populated before entering this function.
727 bool Contains(const HBasicBlock& block) const;
728
729 // Returns whether this loop information is an inner loop of `other`.
730 // Note that `other` *must* be populated before entering this function.
731 bool IsIn(const HLoopInformation& other) const;
732
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800733 // Returns true if instruction is not defined within this loop.
734 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700735
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100736 const ArenaBitVector& GetBlocks() const { return blocks_; }
737
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000738 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000739 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000740
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000741 void ClearAllBlocks() {
742 blocks_.ClearAllBits();
743 }
744
David Brazdil3f4a5222016-05-06 12:46:21 +0100745 bool HasBackEdgeNotDominatedByHeader() const;
746
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100747 bool IsPopulated() const {
748 return blocks_.GetHighestBitSet() != -1;
749 }
750
Anton Shaminf89381f2016-05-16 16:44:13 +0600751 bool DominatesAllBackEdges(HBasicBlock* block);
752
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000753 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100754 // Internal recursive implementation of `Populate`.
755 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100756 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100757
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000758 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100759 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000760 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100761 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100762 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100763 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000764
765 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
766};
767
David Brazdilec16f792015-08-19 15:04:01 +0100768// Stores try/catch information for basic blocks.
769// Note that HGraph is constructed so that catch blocks cannot simultaneously
770// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100771class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100772 public:
773 // Try block information constructor.
774 explicit TryCatchInformation(const HTryBoundary& try_entry)
775 : try_entry_(&try_entry),
776 catch_dex_file_(nullptr),
777 catch_type_index_(DexFile::kDexNoIndex16) {
778 DCHECK(try_entry_ != nullptr);
779 }
780
781 // Catch block information constructor.
782 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
783 : try_entry_(nullptr),
784 catch_dex_file_(&dex_file),
785 catch_type_index_(catch_type_index) {}
786
787 bool IsTryBlock() const { return try_entry_ != nullptr; }
788
789 const HTryBoundary& GetTryEntry() const {
790 DCHECK(IsTryBlock());
791 return *try_entry_;
792 }
793
794 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
795
796 bool IsCatchAllTypeIndex() const {
797 DCHECK(IsCatchBlock());
798 return catch_type_index_ == DexFile::kDexNoIndex16;
799 }
800
801 uint16_t GetCatchTypeIndex() const {
802 DCHECK(IsCatchBlock());
803 return catch_type_index_;
804 }
805
806 const DexFile& GetCatchDexFile() const {
807 DCHECK(IsCatchBlock());
808 return *catch_dex_file_;
809 }
810
811 private:
812 // One of possibly several TryBoundary instructions entering the block's try.
813 // Only set for try blocks.
814 const HTryBoundary* try_entry_;
815
816 // Exception type information. Only set for catch blocks.
817 const DexFile* catch_dex_file_;
818 const uint16_t catch_type_index_;
819};
820
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100821static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100822static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100823
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000824// A block in a method. Contains the list of instructions represented
825// as a double linked list. Each block knows its predecessors and
826// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100827
Vladimir Markof9f64412015-09-02 14:05:49 +0100828class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000829 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600830 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000831 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000832 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
833 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000834 loop_information_(nullptr),
835 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000836 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100837 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100838 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100839 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000840 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000841 try_catch_information_(nullptr) {
842 predecessors_.reserve(kDefaultNumberOfPredecessors);
843 successors_.reserve(kDefaultNumberOfSuccessors);
844 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
845 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000846
Vladimir Marko60584552015-09-03 13:35:12 +0000847 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100848 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000849 }
850
Vladimir Marko60584552015-09-03 13:35:12 +0000851 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100852 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000853 }
854
David Brazdild26a4112015-11-10 11:07:31 +0000855 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
856 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
857
Vladimir Marko60584552015-09-03 13:35:12 +0000858 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
859 return ContainsElement(successors_, block, start_from);
860 }
861
862 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100863 return dominated_blocks_;
864 }
865
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100866 bool IsEntryBlock() const {
867 return graph_->GetEntryBlock() == this;
868 }
869
870 bool IsExitBlock() const {
871 return graph_->GetExitBlock() == this;
872 }
873
David Brazdil46e2a392015-03-16 17:31:52 +0000874 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000875 bool IsSingleTryBoundary() const;
876
877 // Returns true if this block emits nothing but a jump.
878 bool IsSingleJump() const {
879 HLoopInformation* loop_info = GetLoopInformation();
880 return (IsSingleGoto() || IsSingleTryBoundary())
881 // Back edges generate a suspend check.
882 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
883 }
David Brazdil46e2a392015-03-16 17:31:52 +0000884
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000885 void AddBackEdge(HBasicBlock* back_edge) {
886 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000887 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000888 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100889 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000890 loop_information_->AddBackEdge(back_edge);
891 }
892
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000893 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000894 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000895
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100896 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000897 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600898 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000899
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000900 HBasicBlock* GetDominator() const { return dominator_; }
901 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000902 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
903
904 void RemoveDominatedBlock(HBasicBlock* block) {
905 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100906 }
Vladimir Marko60584552015-09-03 13:35:12 +0000907
908 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
909 ReplaceElement(dominated_blocks_, existing, new_block);
910 }
911
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100912 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000913
914 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100915 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000916 }
917
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100918 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
919 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100920 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100921 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100922 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
923 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000924
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000925 HInstruction* GetFirstInstructionDisregardMoves() const;
926
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000927 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000928 successors_.push_back(block);
929 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000930 }
931
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100932 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
933 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100934 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000935 new_block->predecessors_.push_back(this);
936 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000937 }
938
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000939 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
940 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000941 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000942 new_block->successors_.push_back(this);
943 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000944 }
945
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100946 // Insert `this` between `predecessor` and `successor. This method
947 // preserves the indicies, and will update the first edge found between
948 // `predecessor` and `successor`.
949 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
950 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100951 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000952 successor->predecessors_[predecessor_index] = this;
953 predecessor->successors_[successor_index] = this;
954 successors_.push_back(successor);
955 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100956 }
957
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100958 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000959 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100960 }
961
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000962 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000963 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000964 }
965
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100966 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000967 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100968 }
969
970 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000971 predecessors_.push_back(block);
972 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100973 }
974
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100975 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000976 DCHECK_EQ(predecessors_.size(), 2u);
977 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100978 }
979
David Brazdil769c9e52015-04-27 13:54:09 +0100980 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000981 DCHECK_EQ(successors_.size(), 2u);
982 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100983 }
984
David Brazdilfc6a86a2015-06-26 10:33:45 +0000985 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000986 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100987 }
988
David Brazdilfc6a86a2015-06-26 10:33:45 +0000989 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000990 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100991 }
992
David Brazdilfc6a86a2015-06-26 10:33:45 +0000993 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000994 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100995 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000996 }
997
998 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000999 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001000 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001001 }
1002
1003 // Returns whether the first occurrence of `predecessor` in the list of
1004 // predecessors is at index `idx`.
1005 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001006 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001007 return GetPredecessorIndexOf(predecessor) == idx;
1008 }
1009
David Brazdild7558da2015-09-22 13:04:14 +01001010 // Create a new block between this block and its predecessors. The new block
1011 // is added to the graph, all predecessor edges are relinked to it and an edge
1012 // is created to `this`. Returns the new empty block. Reverse post order or
1013 // loop and try/catch information are not updated.
1014 HBasicBlock* CreateImmediateDominator();
1015
David Brazdilfc6a86a2015-06-26 10:33:45 +00001016 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001017 // created, latter block. Note that this method will add the block to the
1018 // graph, create a Goto at the end of the former block and will create an edge
1019 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001020 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001021 HBasicBlock* SplitBefore(HInstruction* cursor);
1022
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001023 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001024 // created block. Note that this method just updates raw block information,
1025 // like predecessors, successors, dominators, and instruction list. It does not
1026 // update the graph, reverse post order, loop information, nor make sure the
1027 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001028 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1029
1030 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1031 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001032
1033 // Merge `other` at the end of `this`. Successors and dominated blocks of
1034 // `other` are changed to be successors and dominated blocks of `this`. Note
1035 // that this method does not update the graph, reverse post order, loop
1036 // information, nor make sure the blocks are consistent (for example ending
1037 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001038 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001039
1040 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1041 // of `this` are moved to `other`.
1042 // Note that this method does not update the graph, reverse post order, loop
1043 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001044 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001045 void ReplaceWith(HBasicBlock* other);
1046
David Brazdil2d7352b2015-04-20 14:52:42 +01001047 // Merge `other` at the end of `this`. This method updates loops, reverse post
1048 // order, links to predecessors, successors, dominators and deletes the block
1049 // from the graph. The two blocks must be successive, i.e. `this` the only
1050 // predecessor of `other` and vice versa.
1051 void MergeWith(HBasicBlock* other);
1052
1053 // Disconnects `this` from all its predecessors, successors and dominator,
1054 // removes it from all loops it is included in and eventually from the graph.
1055 // The block must not dominate any other block. Predecessors and successors
1056 // are safely updated.
1057 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001058
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001059 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001060 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001061 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001062 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001063 // Replace instruction `initial` with `replacement` within this block.
1064 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1065 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001066 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001067 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001068 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1069 // instruction list. With 'ensure_safety' set to true, it verifies that the
1070 // instruction is not in use and removes it from the use lists of its inputs.
1071 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1072 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001073 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001074
1075 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001076 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001077 }
1078
Roland Levillain6b879dd2014-09-22 17:13:44 +01001079 bool IsLoopPreHeaderFirstPredecessor() const {
1080 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001081 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001082 }
1083
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001084 bool IsFirstPredecessorBackEdge() const {
1085 DCHECK(IsLoopHeader());
1086 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1087 }
1088
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001089 HLoopInformation* GetLoopInformation() const {
1090 return loop_information_;
1091 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001092
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001093 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001094 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001095 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001096 void SetInLoop(HLoopInformation* info) {
1097 if (IsLoopHeader()) {
1098 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001099 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001100 loop_information_ = info;
1101 } else if (loop_information_->Contains(*info->GetHeader())) {
1102 // Block is currently part of an outer loop. Make it part of this inner loop.
1103 // Note that a non loop header having a loop information means this loop information
1104 // has already been populated
1105 loop_information_ = info;
1106 } else {
1107 // Block is part of an inner loop. Do not update the loop information.
1108 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1109 // at this point, because this method is being called while populating `info`.
1110 }
1111 }
1112
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001113 // Raw update of the loop information.
1114 void SetLoopInformation(HLoopInformation* info) {
1115 loop_information_ = info;
1116 }
1117
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001118 bool IsInLoop() const { return loop_information_ != nullptr; }
1119
David Brazdilec16f792015-08-19 15:04:01 +01001120 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1121
1122 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1123 try_catch_information_ = try_catch_information;
1124 }
1125
1126 bool IsTryBlock() const {
1127 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1128 }
1129
1130 bool IsCatchBlock() const {
1131 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1132 }
David Brazdilffee3d32015-07-06 11:48:53 +01001133
1134 // Returns the try entry that this block's successors should have. They will
1135 // be in the same try, unless the block ends in a try boundary. In that case,
1136 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001137 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001138
David Brazdild7558da2015-09-22 13:04:14 +01001139 bool HasThrowingInstructions() const;
1140
David Brazdila4b8c212015-05-07 09:59:30 +01001141 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001142 bool Dominates(HBasicBlock* block) const;
1143
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001144 size_t GetLifetimeStart() const { return lifetime_start_; }
1145 size_t GetLifetimeEnd() const { return lifetime_end_; }
1146
1147 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1148 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1149
David Brazdil8d5b8b22015-03-24 10:51:52 +00001150 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001151 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001152 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001153 bool HasSinglePhi() const;
1154
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001155 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001156 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001157 ArenaVector<HBasicBlock*> predecessors_;
1158 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001159 HInstructionList instructions_;
1160 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001161 HLoopInformation* loop_information_;
1162 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001163 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001164 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001165 // The dex program counter of the first instruction of this block.
1166 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001167 size_t lifetime_start_;
1168 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001169 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001170
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001171 friend class HGraph;
1172 friend class HInstruction;
1173
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001174 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1175};
1176
David Brazdilb2bd1c52015-03-25 11:17:37 +00001177// Iterates over the LoopInformation of all loops which contain 'block'
1178// from the innermost to the outermost.
1179class HLoopInformationOutwardIterator : public ValueObject {
1180 public:
1181 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1182 : current_(block.GetLoopInformation()) {}
1183
1184 bool Done() const { return current_ == nullptr; }
1185
1186 void Advance() {
1187 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001188 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001189 }
1190
1191 HLoopInformation* Current() const {
1192 DCHECK(!Done());
1193 return current_;
1194 }
1195
1196 private:
1197 HLoopInformation* current_;
1198
1199 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1200};
1201
Alexandre Ramesef20f712015-06-09 10:29:30 +01001202#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001203 M(Above, Condition) \
1204 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001205 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001206 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001207 M(ArrayGet, Instruction) \
1208 M(ArrayLength, Instruction) \
1209 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001210 M(Below, Condition) \
1211 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001212 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001213 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001214 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001215 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001216 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001217 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001218 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001219 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001220 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001221 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001222 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001223 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001224 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001225 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001226 M(Exit, Instruction) \
1227 M(FloatConstant, Constant) \
1228 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001229 M(GreaterThan, Condition) \
1230 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001231 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001232 M(InstanceFieldGet, Instruction) \
1233 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001234 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001235 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001236 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001237 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001238 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001239 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001240 M(LessThan, Condition) \
1241 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001242 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001243 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001244 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001245 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001246 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001247 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001248 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001249 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001250 M(Neg, UnaryOperation) \
1251 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001252 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001253 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001254 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001255 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001256 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001257 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001258 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001259 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001260 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001261 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001262 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001263 M(Return, Instruction) \
1264 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001265 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001266 M(Shl, BinaryOperation) \
1267 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001268 M(StaticFieldGet, Instruction) \
1269 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001270 M(UnresolvedInstanceFieldGet, Instruction) \
1271 M(UnresolvedInstanceFieldSet, Instruction) \
1272 M(UnresolvedStaticFieldGet, Instruction) \
1273 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001274 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001275 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001276 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001277 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001278 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001279 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001280 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001281 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001282
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001283/*
1284 * Instructions, shared across several (not all) architectures.
1285 */
1286#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1287#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1288#else
1289#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001290 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001291 M(MultiplyAccumulate, Instruction) \
1292 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001293#endif
1294
Vladimir Markob4536b72015-11-24 13:45:23 +00001295#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001296#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001297#else
1298#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1299 M(ArmDexCacheArraysBase, Instruction)
1300#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001301
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001302#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001303#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001304#else
1305#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001306 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001307#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001308
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001309#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001310#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001311#else
1312#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1313 M(MipsComputeBaseMethodAddress, Instruction) \
1314 M(MipsDexCacheArraysBase, Instruction)
1315#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001316
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001317#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1318
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001319#ifndef ART_ENABLE_CODEGEN_x86
1320#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1321#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001322#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1323 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001324 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001325 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001326 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001327#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001328
1329#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1330
1331#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1332 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001333 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001334 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1335 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001336 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001337 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001338 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1339 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1340
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001341#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1342 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001343 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001344 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001345 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001346 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001347
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001348#define FOR_EACH_INSTRUCTION(M) \
1349 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1350 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1351
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001352#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001353FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1354#undef FORWARD_DECLARATION
1355
Vladimir Marko372f10e2016-05-17 16:30:10 +01001356#define DECLARE_INSTRUCTION(type) \
1357 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1358 const char* DebugName() const OVERRIDE { return #type; } \
1359 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1360 return other->Is##type(); \
1361 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001362 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001363
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001364#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1365 bool Is##type() const { return As##type() != nullptr; } \
1366 const H##type* As##type() const { return this; } \
1367 H##type* As##type() { return this; }
1368
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001369template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001370class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001371 public:
David Brazdiled596192015-01-23 10:39:45 +00001372 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001373 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001374 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001375
Vladimir Marko46817b82016-03-29 12:21:58 +01001376 // Hook for the IntrusiveForwardList<>.
1377 // TODO: Hide this better.
1378 IntrusiveForwardListHook hook;
1379
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001380 private:
David Brazdiled596192015-01-23 10:39:45 +00001381 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001382 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001383
1384 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001385 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001386
Vladimir Marko46817b82016-03-29 12:21:58 +01001387 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001388
1389 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1390};
1391
David Brazdiled596192015-01-23 10:39:45 +00001392template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001393using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001394
David Brazdil1abb4192015-02-17 18:33:36 +00001395// This class is used by HEnvironment and HInstruction classes to record the
1396// instructions they use and pointers to the corresponding HUseListNodes kept
1397// by the used instructions.
1398template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001399class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001400 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001401 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1402 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001403
Vladimir Marko46817b82016-03-29 12:21:58 +01001404 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1405 : HUserRecord(old_record.instruction_, before_use_node) {}
1406 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1407 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001408 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001409 }
1410
1411 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001412 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1413 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001414
1415 private:
1416 // Instruction used by the user.
1417 HInstruction* instruction_;
1418
Vladimir Marko46817b82016-03-29 12:21:58 +01001419 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1420 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001421};
1422
Vladimir Markoe9004912016-06-16 16:50:52 +01001423// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1424// This is used for HInstruction::GetInputs() to return a container wrapper providing
1425// HInstruction* values even though the underlying container has HUserRecord<>s.
1426struct HInputExtractor {
1427 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1428 return record.GetInstruction();
1429 }
1430 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1431 return record.GetInstruction();
1432 }
1433};
1434
1435using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1436using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1437
Aart Bik854a02b2015-07-14 16:07:00 -07001438/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001439 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001440 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001441 * For write/read dependences on fields/arrays, the dependence analysis uses
1442 * type disambiguation (e.g. a float field write cannot modify the value of an
1443 * integer field read) and the access type (e.g. a reference array write cannot
1444 * modify the value of a reference field read [although it may modify the
1445 * reference fetch prior to reading the field, which is represented by its own
1446 * write/read dependence]). The analysis makes conservative points-to
1447 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1448 * the same, and any reference read depends on any reference read without
1449 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001450 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001451 * The internal representation uses 38-bit and is described in the table below.
1452 * The first line indicates the side effect, and for field/array accesses the
1453 * second line indicates the type of the access (in the order of the
1454 * Primitive::Type enum).
1455 * The two numbered lines below indicate the bit position in the bitfield (read
1456 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001457 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001458 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1459 * +-------------+---------+---------+--------------+---------+---------+
1460 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1461 * | 3 |333333322|222222221| 1 |111111110|000000000|
1462 * | 7 |654321098|765432109| 8 |765432109|876543210|
1463 *
1464 * Note that, to ease the implementation, 'changes' bits are least significant
1465 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001466 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001467class SideEffects : public ValueObject {
1468 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001469 SideEffects() : flags_(0) {}
1470
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001471 static SideEffects None() {
1472 return SideEffects(0);
1473 }
1474
1475 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001476 return SideEffects(kAllChangeBits | kAllDependOnBits);
1477 }
1478
1479 static SideEffects AllChanges() {
1480 return SideEffects(kAllChangeBits);
1481 }
1482
1483 static SideEffects AllDependencies() {
1484 return SideEffects(kAllDependOnBits);
1485 }
1486
1487 static SideEffects AllExceptGCDependency() {
1488 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1489 }
1490
1491 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001492 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001493 }
1494
Aart Bik34c3ba92015-07-20 14:08:59 -07001495 static SideEffects AllWrites() {
1496 return SideEffects(kAllWrites);
1497 }
1498
1499 static SideEffects AllReads() {
1500 return SideEffects(kAllReads);
1501 }
1502
1503 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1504 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001505 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001506 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001507 }
1508
Aart Bik854a02b2015-07-14 16:07:00 -07001509 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001510 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001511 }
1512
Aart Bik34c3ba92015-07-20 14:08:59 -07001513 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1514 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001515 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001516 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001517 }
1518
1519 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001520 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001521 }
1522
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001523 static SideEffects CanTriggerGC() {
1524 return SideEffects(1ULL << kCanTriggerGCBit);
1525 }
1526
1527 static SideEffects DependsOnGC() {
1528 return SideEffects(1ULL << kDependsOnGCBit);
1529 }
1530
Aart Bik854a02b2015-07-14 16:07:00 -07001531 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001532 SideEffects Union(SideEffects other) const {
1533 return SideEffects(flags_ | other.flags_);
1534 }
1535
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001536 SideEffects Exclusion(SideEffects other) const {
1537 return SideEffects(flags_ & ~other.flags_);
1538 }
1539
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001540 void Add(SideEffects other) {
1541 flags_ |= other.flags_;
1542 }
1543
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001544 bool Includes(SideEffects other) const {
1545 return (other.flags_ & flags_) == other.flags_;
1546 }
1547
1548 bool HasSideEffects() const {
1549 return (flags_ & kAllChangeBits);
1550 }
1551
1552 bool HasDependencies() const {
1553 return (flags_ & kAllDependOnBits);
1554 }
1555
1556 // Returns true if there are no side effects or dependencies.
1557 bool DoesNothing() const {
1558 return flags_ == 0;
1559 }
1560
Aart Bik854a02b2015-07-14 16:07:00 -07001561 // Returns true if something is written.
1562 bool DoesAnyWrite() const {
1563 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001564 }
1565
Aart Bik854a02b2015-07-14 16:07:00 -07001566 // Returns true if something is read.
1567 bool DoesAnyRead() const {
1568 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001569 }
1570
Aart Bik854a02b2015-07-14 16:07:00 -07001571 // Returns true if potentially everything is written and read
1572 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001573 bool DoesAllReadWrite() const {
1574 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1575 }
1576
Aart Bik854a02b2015-07-14 16:07:00 -07001577 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001578 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001579 }
1580
Roland Levillain0d5a2812015-11-13 10:07:31 +00001581 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001582 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001583 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1584 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001585 }
1586
1587 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001588 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001589 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001590 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001591 for (int s = kLastBit; s >= 0; s--) {
1592 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1593 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1594 // This is a bit for the GC side effect.
1595 if (current_bit_is_set) {
1596 flags += "GC";
1597 }
Aart Bik854a02b2015-07-14 16:07:00 -07001598 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001599 } else {
1600 // This is a bit for the array/field analysis.
1601 // The underscore character stands for the 'can trigger GC' bit.
1602 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1603 if (current_bit_is_set) {
1604 flags += kDebug[s];
1605 }
1606 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1607 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1608 flags += "|";
1609 }
1610 }
Aart Bik854a02b2015-07-14 16:07:00 -07001611 }
1612 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001613 }
1614
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001615 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001616
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001617 private:
1618 static constexpr int kFieldArrayAnalysisBits = 9;
1619
1620 static constexpr int kFieldWriteOffset = 0;
1621 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1622 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1623 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1624
1625 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1626
1627 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1628 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1629 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1630 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1631
1632 static constexpr int kLastBit = kDependsOnGCBit;
1633 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1634
1635 // Aliases.
1636
1637 static_assert(kChangeBits == kDependOnBits,
1638 "the 'change' bits should match the 'depend on' bits.");
1639
1640 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1641 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1642 static constexpr uint64_t kAllWrites =
1643 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1644 static constexpr uint64_t kAllReads =
1645 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001646
Aart Bik854a02b2015-07-14 16:07:00 -07001647 // Translates type to bit flag.
1648 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1649 CHECK_NE(type, Primitive::kPrimVoid);
1650 const uint64_t one = 1;
1651 const int shift = type; // 0-based consecutive enum
1652 DCHECK_LE(kFieldWriteOffset, shift);
1653 DCHECK_LT(shift, kArrayWriteOffset);
1654 return one << (type + offset);
1655 }
1656
1657 // Private constructor on direct flags value.
1658 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1659
1660 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001661};
1662
David Brazdiled596192015-01-23 10:39:45 +00001663// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001664class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001665 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001666 HEnvironment(ArenaAllocator* arena,
1667 size_t number_of_vregs,
1668 const DexFile& dex_file,
1669 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001670 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001671 InvokeType invoke_type,
1672 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001673 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1674 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001675 parent_(nullptr),
1676 dex_file_(dex_file),
1677 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001678 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001679 invoke_type_(invoke_type),
1680 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001681 }
1682
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001683 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001684 : HEnvironment(arena,
1685 to_copy.Size(),
1686 to_copy.GetDexFile(),
1687 to_copy.GetMethodIdx(),
1688 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001689 to_copy.GetInvokeType(),
1690 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001691
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001692 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001693 if (parent_ != nullptr) {
1694 parent_->SetAndCopyParentChain(allocator, parent);
1695 } else {
1696 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1697 parent_->CopyFrom(parent);
1698 if (parent->GetParent() != nullptr) {
1699 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1700 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001701 }
David Brazdiled596192015-01-23 10:39:45 +00001702 }
1703
Vladimir Marko71bf8092015-09-15 15:33:14 +01001704 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001705 void CopyFrom(HEnvironment* environment);
1706
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001707 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1708 // input to the loop phi instead. This is for inserting instructions that
1709 // require an environment (like HDeoptimization) in the loop pre-header.
1710 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001711
1712 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001713 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001714 }
1715
1716 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001717 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001718 }
1719
David Brazdil1abb4192015-02-17 18:33:36 +00001720 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001721
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001722 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001723
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001724 HEnvironment* GetParent() const { return parent_; }
1725
1726 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001727 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001728 }
1729
1730 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001731 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001732 }
1733
1734 uint32_t GetDexPc() const {
1735 return dex_pc_;
1736 }
1737
1738 uint32_t GetMethodIdx() const {
1739 return method_idx_;
1740 }
1741
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001742 InvokeType GetInvokeType() const {
1743 return invoke_type_;
1744 }
1745
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001746 const DexFile& GetDexFile() const {
1747 return dex_file_;
1748 }
1749
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001750 HInstruction* GetHolder() const {
1751 return holder_;
1752 }
1753
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001754
1755 bool IsFromInlinedInvoke() const {
1756 return GetParent() != nullptr;
1757 }
1758
David Brazdiled596192015-01-23 10:39:45 +00001759 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001760 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1761 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001762 HEnvironment* parent_;
1763 const DexFile& dex_file_;
1764 const uint32_t method_idx_;
1765 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001766 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001767
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001768 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001769 HInstruction* const holder_;
1770
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001771 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001772
1773 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1774};
1775
Vladimir Markof9f64412015-09-02 14:05:49 +01001776class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001777 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001778 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001779 : previous_(nullptr),
1780 next_(nullptr),
1781 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001782 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001783 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001784 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001785 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001786 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001787 locations_(nullptr),
1788 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001789 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001790 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001791 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1792 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1793 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001794
Dave Allison20dfc792014-06-16 20:44:29 -07001795 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001796
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001797#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001798 enum InstructionKind {
1799 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1800 };
1801#undef DECLARE_KIND
1802
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001803 HInstruction* GetNext() const { return next_; }
1804 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001805
Calin Juravle77520bc2015-01-12 18:45:46 +00001806 HInstruction* GetNextDisregardingMoves() const;
1807 HInstruction* GetPreviousDisregardingMoves() const;
1808
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001809 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001810 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001811 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001812 bool IsInBlock() const { return block_ != nullptr; }
1813 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001814 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1815 bool IsIrreducibleLoopHeaderPhi() const {
1816 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1817 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001818
Vladimir Marko372f10e2016-05-17 16:30:10 +01001819 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1820
1821 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1822 // One virtual method is enough, just const_cast<> and then re-add the const.
1823 return ArrayRef<const HUserRecord<HInstruction*>>(
1824 const_cast<HInstruction*>(this)->GetInputRecords());
1825 }
1826
Vladimir Markoe9004912016-06-16 16:50:52 +01001827 HInputsRef GetInputs() {
1828 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001829 }
1830
Vladimir Markoe9004912016-06-16 16:50:52 +01001831 HConstInputsRef GetInputs() const {
1832 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001833 }
1834
1835 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001836 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001837
Vladimir Marko372f10e2016-05-17 16:30:10 +01001838 void SetRawInputAt(size_t index, HInstruction* input) {
1839 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1840 }
1841
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001842 virtual void Accept(HGraphVisitor* visitor) = 0;
1843 virtual const char* DebugName() const = 0;
1844
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001845 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1846
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001847 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001848
1849 uint32_t GetDexPc() const { return dex_pc_; }
1850
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001851 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001852
Roland Levillaine161a2a2014-10-03 12:45:18 +01001853 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001854 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001855
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001856 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001857 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001858
Calin Juravle10e244f2015-01-26 18:54:32 +00001859 // Does not apply for all instructions, but having this at top level greatly
1860 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001861 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001862 virtual bool CanBeNull() const {
1863 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1864 return true;
1865 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001866
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001867 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001868 return false;
1869 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001870
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001871 virtual bool IsActualObject() const {
1872 return GetType() == Primitive::kPrimNot;
1873 }
1874
Calin Juravle2e768302015-07-28 14:41:11 +00001875 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001876
Calin Juravle61d544b2015-02-23 16:46:57 +00001877 ReferenceTypeInfo GetReferenceTypeInfo() const {
1878 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001879 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001880 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001881 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001882
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001883 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001884 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001885 // Note: fixup_end remains valid across push_front().
1886 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1887 HUseListNode<HInstruction*>* new_node =
1888 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1889 uses_.push_front(*new_node);
1890 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001891 }
1892
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001893 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001894 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001895 // Note: env_fixup_end remains valid across push_front().
1896 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1897 HUseListNode<HEnvironment*>* new_node =
1898 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1899 env_uses_.push_front(*new_node);
1900 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001901 }
1902
David Brazdil1abb4192015-02-17 18:33:36 +00001903 void RemoveAsUserOfInput(size_t input) {
1904 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001905 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1906 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1907 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001908 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001909
Vladimir Marko372f10e2016-05-17 16:30:10 +01001910 void RemoveAsUserOfAllInputs() {
1911 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1912 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1913 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1914 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1915 }
1916 }
1917
David Brazdil1abb4192015-02-17 18:33:36 +00001918 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1919 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001920
Vladimir Marko46817b82016-03-29 12:21:58 +01001921 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1922 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1923 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001924 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001925 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001926 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001927
Roland Levillain6c82d402014-10-13 16:10:27 +01001928 // Does this instruction strictly dominate `other_instruction`?
1929 // Returns false if this instruction and `other_instruction` are the same.
1930 // Aborts if this instruction and `other_instruction` are both phis.
1931 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001932
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001933 int GetId() const { return id_; }
1934 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001935
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001936 int GetSsaIndex() const { return ssa_index_; }
1937 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1938 bool HasSsaIndex() const { return ssa_index_ != -1; }
1939
1940 bool HasEnvironment() const { return environment_ != nullptr; }
1941 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001942 // Set the `environment_` field. Raw because this method does not
1943 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001944 void SetRawEnvironment(HEnvironment* environment) {
1945 DCHECK(environment_ == nullptr);
1946 DCHECK_EQ(environment->GetHolder(), this);
1947 environment_ = environment;
1948 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001949
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001950 void InsertRawEnvironment(HEnvironment* environment) {
1951 DCHECK(environment_ != nullptr);
1952 DCHECK_EQ(environment->GetHolder(), this);
1953 DCHECK(environment->GetParent() == nullptr);
1954 environment->parent_ = environment_;
1955 environment_ = environment;
1956 }
1957
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001958 void RemoveEnvironment();
1959
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001960 // Set the environment of this instruction, copying it from `environment`. While
1961 // copying, the uses lists are being updated.
1962 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001963 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001964 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001965 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001966 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001967 if (environment->GetParent() != nullptr) {
1968 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1969 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001970 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001971
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001972 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1973 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001974 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001975 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001976 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001977 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001978 if (environment->GetParent() != nullptr) {
1979 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1980 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001981 }
1982
Nicolas Geoffray39468442014-09-02 15:17:15 +01001983 // Returns the number of entries in the environment. Typically, that is the
1984 // number of dex registers in a method. It could be more in case of inlining.
1985 size_t EnvironmentSize() const;
1986
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001987 LocationSummary* GetLocations() const { return locations_; }
1988 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001989
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001990 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001991 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001992
Alexandre Rames188d4312015-04-09 18:30:21 +01001993 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1994 // uses of this instruction by `other` are *not* updated.
1995 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1996 ReplaceWith(other);
1997 other->ReplaceInput(this, use_index);
1998 }
1999
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002000 // Move `this` instruction before `cursor`.
2001 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002002
Vladimir Markofb337ea2015-11-25 15:25:10 +00002003 // Move `this` before its first user and out of any loops. If there is no
2004 // out-of-loop user that dominates all other users, move the instruction
2005 // to the end of the out-of-loop common dominator of the user's blocks.
2006 //
2007 // This can be used only on non-throwing instructions with no side effects that
2008 // have at least one use but no environment uses.
2009 void MoveBeforeFirstUserAndOutOfLoops();
2010
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002011#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002012 bool Is##type() const; \
2013 const H##type* As##type() const; \
2014 H##type* As##type();
2015
2016 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2017#undef INSTRUCTION_TYPE_CHECK
2018
2019#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002020 bool Is##type() const { return (As##type() != nullptr); } \
2021 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002022 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002023 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002024#undef INSTRUCTION_TYPE_CHECK
2025
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002026 // Returns whether the instruction can be moved within the graph.
2027 virtual bool CanBeMoved() const { return false; }
2028
2029 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002030 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002031 return false;
2032 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002033
2034 // Returns whether any data encoded in the two instructions is equal.
2035 // This method does not look at the inputs. Both instructions must be
2036 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002037 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002038 return false;
2039 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002040
2041 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002042 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002043 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002044 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002045
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002046 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2047 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2048 // the virtual function because the __attribute__((__pure__)) doesn't really
2049 // apply the strong requirement for virtual functions, preventing optimizations.
2050 InstructionKind GetKind() const PURE;
2051 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002052
2053 virtual size_t ComputeHashCode() const {
2054 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002055 for (const HInstruction* input : GetInputs()) {
2056 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002057 }
2058 return result;
2059 }
2060
2061 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002062 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002063 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002064
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002065 size_t GetLifetimePosition() const { return lifetime_position_; }
2066 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2067 LiveInterval* GetLiveInterval() const { return live_interval_; }
2068 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2069 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2070
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002071 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2072
2073 // Returns whether the code generation of the instruction will require to have access
2074 // to the current method. Such instructions are:
2075 // (1): Instructions that require an environment, as calling the runtime requires
2076 // to walk the stack and have the current method stored at a specific stack address.
2077 // (2): Object literals like classes and strings, that are loaded from the dex cache
2078 // fields of the current method.
2079 bool NeedsCurrentMethod() const {
2080 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2081 }
2082
Vladimir Markodc151b22015-10-15 18:02:30 +01002083 // Returns whether the code generation of the instruction will require to have access
2084 // to the dex cache of the current method's declaring class via the current method.
2085 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002086
Mark Mendellc4701932015-04-10 13:18:51 -04002087 // Does this instruction have any use in an environment before
2088 // control flow hits 'other'?
2089 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2090
2091 // Remove all references to environment uses of this instruction.
2092 // The caller must ensure that this is safe to do.
2093 void RemoveEnvironmentUsers();
2094
Vladimir Markoa1de9182016-02-25 11:37:38 +00002095 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2096 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002097
David Brazdil1abb4192015-02-17 18:33:36 +00002098 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002099 // If set, the machine code for this instruction is assumed to be generated by
2100 // its users. Used by liveness analysis to compute use positions accordingly.
2101 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2102 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2103 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2104 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2105
Vladimir Marko372f10e2016-05-17 16:30:10 +01002106 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2107 return GetInputRecords()[i];
2108 }
2109
2110 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2111 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2112 input_records[index] = input;
2113 }
David Brazdil1abb4192015-02-17 18:33:36 +00002114
Vladimir Markoa1de9182016-02-25 11:37:38 +00002115 uint32_t GetPackedFields() const {
2116 return packed_fields_;
2117 }
2118
2119 template <size_t flag>
2120 bool GetPackedFlag() const {
2121 return (packed_fields_ & (1u << flag)) != 0u;
2122 }
2123
2124 template <size_t flag>
2125 void SetPackedFlag(bool value = true) {
2126 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2127 }
2128
2129 template <typename BitFieldType>
2130 typename BitFieldType::value_type GetPackedField() const {
2131 return BitFieldType::Decode(packed_fields_);
2132 }
2133
2134 template <typename BitFieldType>
2135 void SetPackedField(typename BitFieldType::value_type value) {
2136 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2137 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2138 }
2139
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002140 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002141 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2142 auto before_use_node = uses_.before_begin();
2143 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2144 HInstruction* user = use_node->GetUser();
2145 size_t input_index = use_node->GetIndex();
2146 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2147 before_use_node = use_node;
2148 }
2149 }
2150
2151 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2152 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2153 if (next != uses_.end()) {
2154 HInstruction* next_user = next->GetUser();
2155 size_t next_index = next->GetIndex();
2156 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2157 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2158 }
2159 }
2160
2161 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2162 auto before_env_use_node = env_uses_.before_begin();
2163 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2164 HEnvironment* user = env_use_node->GetUser();
2165 size_t input_index = env_use_node->GetIndex();
2166 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2167 before_env_use_node = env_use_node;
2168 }
2169 }
2170
2171 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2172 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2173 if (next != env_uses_.end()) {
2174 HEnvironment* next_user = next->GetUser();
2175 size_t next_index = next->GetIndex();
2176 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2177 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2178 }
2179 }
David Brazdil1abb4192015-02-17 18:33:36 +00002180
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002181 HInstruction* previous_;
2182 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002183 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002184 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002185
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002186 // An instruction gets an id when it is added to the graph.
2187 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002188 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002189 int id_;
2190
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002191 // When doing liveness analysis, instructions that have uses get an SSA index.
2192 int ssa_index_;
2193
Vladimir Markoa1de9182016-02-25 11:37:38 +00002194 // Packed fields.
2195 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002196
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002197 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002198 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002199
2200 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002201 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002202
Nicolas Geoffray39468442014-09-02 15:17:15 +01002203 // The environment associated with this instruction. Not null if the instruction
2204 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002205 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002206
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002207 // Set by the code generator.
2208 LocationSummary* locations_;
2209
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002210 // Set by the liveness analysis.
2211 LiveInterval* live_interval_;
2212
2213 // Set by the liveness analysis, this is the position in a linear
2214 // order of blocks where this instruction's live interval start.
2215 size_t lifetime_position_;
2216
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002217 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002218
Vladimir Markoa1de9182016-02-25 11:37:38 +00002219 // The reference handle part of the reference type info.
2220 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002221 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002222 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002223
David Brazdil1abb4192015-02-17 18:33:36 +00002224 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002225 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002226 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002227 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002228 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002229
2230 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2231};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002232std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002233
2234class HInstructionIterator : public ValueObject {
2235 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002236 explicit HInstructionIterator(const HInstructionList& instructions)
2237 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002238 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002239 }
2240
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002241 bool Done() const { return instruction_ == nullptr; }
2242 HInstruction* Current() const { return instruction_; }
2243 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002244 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002245 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002246 }
2247
2248 private:
2249 HInstruction* instruction_;
2250 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002251
2252 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002253};
2254
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002255class HBackwardInstructionIterator : public ValueObject {
2256 public:
2257 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2258 : instruction_(instructions.last_instruction_) {
2259 next_ = Done() ? nullptr : instruction_->GetPrevious();
2260 }
2261
2262 bool Done() const { return instruction_ == nullptr; }
2263 HInstruction* Current() const { return instruction_; }
2264 void Advance() {
2265 instruction_ = next_;
2266 next_ = Done() ? nullptr : instruction_->GetPrevious();
2267 }
2268
2269 private:
2270 HInstruction* instruction_;
2271 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002272
2273 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002274};
2275
Vladimir Markof9f64412015-09-02 14:05:49 +01002276template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002277class HTemplateInstruction: public HInstruction {
2278 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002279 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002280 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002281 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002282
Vladimir Marko372f10e2016-05-17 16:30:10 +01002283 using HInstruction::GetInputRecords; // Keep the const version visible.
2284 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2285 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002286 }
2287
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002288 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002289 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002290
2291 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002292};
2293
Vladimir Markof9f64412015-09-02 14:05:49 +01002294// HTemplateInstruction specialization for N=0.
2295template<>
2296class HTemplateInstruction<0>: public HInstruction {
2297 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002298 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002299 : HInstruction(side_effects, dex_pc) {}
2300
Vladimir Markof9f64412015-09-02 14:05:49 +01002301 virtual ~HTemplateInstruction() {}
2302
Vladimir Marko372f10e2016-05-17 16:30:10 +01002303 using HInstruction::GetInputRecords; // Keep the const version visible.
2304 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2305 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002306 }
2307
2308 private:
2309 friend class SsaBuilder;
2310};
2311
Dave Allison20dfc792014-06-16 20:44:29 -07002312template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002313class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002314 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002315 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002316 : HTemplateInstruction<N>(side_effects, dex_pc) {
2317 this->template SetPackedField<TypeField>(type);
2318 }
Dave Allison20dfc792014-06-16 20:44:29 -07002319 virtual ~HExpression() {}
2320
Vladimir Markoa1de9182016-02-25 11:37:38 +00002321 Primitive::Type GetType() const OVERRIDE {
2322 return TypeField::Decode(this->GetPackedFields());
2323 }
Dave Allison20dfc792014-06-16 20:44:29 -07002324
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002325 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002326 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2327 static constexpr size_t kFieldTypeSize =
2328 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2329 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2330 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2331 "Too many packed fields.");
2332 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002333};
2334
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002335// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2336// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002337class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002338 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002339 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2340 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002341
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002342 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002343
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002344 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002345
2346 private:
2347 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2348};
2349
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002350// Represents dex's RETURN opcodes. A HReturn is a control flow
2351// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002352class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002353 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002354 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2355 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002356 SetRawInputAt(0, value);
2357 }
2358
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002359 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002360
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002361 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002362
2363 private:
2364 DISALLOW_COPY_AND_ASSIGN(HReturn);
2365};
2366
Vladimir Markofcb503c2016-05-18 12:48:17 +01002367class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002368 public:
2369 HPhi(ArenaAllocator* arena,
2370 uint32_t reg_number,
2371 size_t number_of_inputs,
2372 Primitive::Type type,
2373 uint32_t dex_pc = kNoDexPc)
2374 : HInstruction(SideEffects::None(), dex_pc),
2375 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2376 reg_number_(reg_number) {
2377 SetPackedField<TypeField>(ToPhiType(type));
2378 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2379 // Phis are constructed live and marked dead if conflicting or unused.
2380 // Individual steps of SsaBuilder should assume that if a phi has been
2381 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2382 SetPackedFlag<kFlagIsLive>(true);
2383 SetPackedFlag<kFlagCanBeNull>(true);
2384 }
2385
2386 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2387 static Primitive::Type ToPhiType(Primitive::Type type) {
2388 return Primitive::PrimitiveKind(type);
2389 }
2390
2391 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2392
Vladimir Marko372f10e2016-05-17 16:30:10 +01002393 using HInstruction::GetInputRecords; // Keep the const version visible.
2394 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2395 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2396 }
David Brazdildee58d62016-04-07 09:54:26 +00002397
2398 void AddInput(HInstruction* input);
2399 void RemoveInputAt(size_t index);
2400
2401 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2402 void SetType(Primitive::Type new_type) {
2403 // Make sure that only valid type changes occur. The following are allowed:
2404 // (1) int -> float/ref (primitive type propagation),
2405 // (2) long -> double (primitive type propagation).
2406 DCHECK(GetType() == new_type ||
2407 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2408 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2409 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2410 SetPackedField<TypeField>(new_type);
2411 }
2412
2413 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2414 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2415
2416 uint32_t GetRegNumber() const { return reg_number_; }
2417
2418 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2419 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2420 bool IsDead() const { return !IsLive(); }
2421 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2422
Vladimir Markoe9004912016-06-16 16:50:52 +01002423 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002424 return other != nullptr
2425 && other->IsPhi()
2426 && other->AsPhi()->GetBlock() == GetBlock()
2427 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2428 }
2429
2430 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2431 // An equivalent phi is a phi having the same dex register and type.
2432 // It assumes that phis with the same dex register are adjacent.
2433 HPhi* GetNextEquivalentPhiWithSameType() {
2434 HInstruction* next = GetNext();
2435 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2436 if (next->GetType() == GetType()) {
2437 return next->AsPhi();
2438 }
2439 next = next->GetNext();
2440 }
2441 return nullptr;
2442 }
2443
2444 DECLARE_INSTRUCTION(Phi);
2445
David Brazdildee58d62016-04-07 09:54:26 +00002446 private:
2447 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2448 static constexpr size_t kFieldTypeSize =
2449 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2450 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2451 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2452 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2453 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2454 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2455
Vladimir Marko372f10e2016-05-17 16:30:10 +01002456 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002457 const uint32_t reg_number_;
2458
2459 DISALLOW_COPY_AND_ASSIGN(HPhi);
2460};
2461
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002462// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002463// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002464// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002465class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002466 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002467 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002468
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002469 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002470
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002471 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002472
2473 private:
2474 DISALLOW_COPY_AND_ASSIGN(HExit);
2475};
2476
2477// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002478class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002479 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002480 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002481
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002482 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002483
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002484 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002485 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002486 }
2487
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002488 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002489
2490 private:
2491 DISALLOW_COPY_AND_ASSIGN(HGoto);
2492};
2493
Roland Levillain9867bc72015-08-05 10:21:34 +01002494class HConstant : public HExpression<0> {
2495 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002496 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2497 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002498
2499 bool CanBeMoved() const OVERRIDE { return true; }
2500
Roland Levillain1a653882016-03-18 18:05:57 +00002501 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002502 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002503 // Is this constant 0 in the arithmetic sense?
2504 virtual bool IsArithmeticZero() const { return false; }
2505 // Is this constant a 0-bit pattern?
2506 virtual bool IsZeroBitPattern() const { return false; }
2507 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002508 virtual bool IsOne() const { return false; }
2509
David Brazdil77a48ae2015-09-15 12:34:04 +00002510 virtual uint64_t GetValueAsUint64() const = 0;
2511
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002512 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002513
2514 private:
2515 DISALLOW_COPY_AND_ASSIGN(HConstant);
2516};
2517
Vladimir Markofcb503c2016-05-18 12:48:17 +01002518class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002519 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002520 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002521 return true;
2522 }
2523
David Brazdil77a48ae2015-09-15 12:34:04 +00002524 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2525
Roland Levillain9867bc72015-08-05 10:21:34 +01002526 size_t ComputeHashCode() const OVERRIDE { return 0; }
2527
Roland Levillain1a653882016-03-18 18:05:57 +00002528 // The null constant representation is a 0-bit pattern.
2529 virtual bool IsZeroBitPattern() const { return true; }
2530
Roland Levillain9867bc72015-08-05 10:21:34 +01002531 DECLARE_INSTRUCTION(NullConstant);
2532
2533 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002534 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002535
2536 friend class HGraph;
2537 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2538};
2539
2540// Constants of the type int. Those can be from Dex instructions, or
2541// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002542class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002543 public:
2544 int32_t GetValue() const { return value_; }
2545
David Brazdil9f389d42015-10-01 14:32:56 +01002546 uint64_t GetValueAsUint64() const OVERRIDE {
2547 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2548 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002549
Vladimir Marko372f10e2016-05-17 16:30:10 +01002550 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002551 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002552 return other->AsIntConstant()->value_ == value_;
2553 }
2554
2555 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2556
2557 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002558 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2559 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002560 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2561
Roland Levillain1a653882016-03-18 18:05:57 +00002562 // Integer constants are used to encode Boolean values as well,
2563 // where 1 means true and 0 means false.
2564 bool IsTrue() const { return GetValue() == 1; }
2565 bool IsFalse() const { return GetValue() == 0; }
2566
Roland Levillain9867bc72015-08-05 10:21:34 +01002567 DECLARE_INSTRUCTION(IntConstant);
2568
2569 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002570 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2571 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2572 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2573 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002574
2575 const int32_t value_;
2576
2577 friend class HGraph;
2578 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2579 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2580 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2581};
2582
Vladimir Markofcb503c2016-05-18 12:48:17 +01002583class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002584 public:
2585 int64_t GetValue() const { return value_; }
2586
David Brazdil77a48ae2015-09-15 12:34:04 +00002587 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2588
Vladimir Marko372f10e2016-05-17 16:30:10 +01002589 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002590 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002591 return other->AsLongConstant()->value_ == value_;
2592 }
2593
2594 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2595
2596 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002597 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2598 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002599 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2600
2601 DECLARE_INSTRUCTION(LongConstant);
2602
2603 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002604 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2605 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002606
2607 const int64_t value_;
2608
2609 friend class HGraph;
2610 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2611};
Dave Allison20dfc792014-06-16 20:44:29 -07002612
Vladimir Markofcb503c2016-05-18 12:48:17 +01002613class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002614 public:
2615 float GetValue() const { return value_; }
2616
2617 uint64_t GetValueAsUint64() const OVERRIDE {
2618 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2619 }
2620
Vladimir Marko372f10e2016-05-17 16:30:10 +01002621 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002622 DCHECK(other->IsFloatConstant()) << other->DebugName();
2623 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2624 }
2625
2626 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2627
2628 bool IsMinusOne() const OVERRIDE {
2629 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2630 }
Roland Levillain1a653882016-03-18 18:05:57 +00002631 bool IsArithmeticZero() const OVERRIDE {
2632 return std::fpclassify(value_) == FP_ZERO;
2633 }
2634 bool IsArithmeticPositiveZero() const {
2635 return IsArithmeticZero() && !std::signbit(value_);
2636 }
2637 bool IsArithmeticNegativeZero() const {
2638 return IsArithmeticZero() && std::signbit(value_);
2639 }
2640 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002641 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002642 }
2643 bool IsOne() const OVERRIDE {
2644 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2645 }
2646 bool IsNaN() const {
2647 return std::isnan(value_);
2648 }
2649
2650 DECLARE_INSTRUCTION(FloatConstant);
2651
2652 private:
2653 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2654 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2655 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2656 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2657
2658 const float value_;
2659
2660 // Only the SsaBuilder and HGraph can create floating-point constants.
2661 friend class SsaBuilder;
2662 friend class HGraph;
2663 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2664};
2665
Vladimir Markofcb503c2016-05-18 12:48:17 +01002666class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002667 public:
2668 double GetValue() const { return value_; }
2669
2670 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2671
Vladimir Marko372f10e2016-05-17 16:30:10 +01002672 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002673 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2674 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2675 }
2676
2677 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2678
2679 bool IsMinusOne() const OVERRIDE {
2680 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2681 }
Roland Levillain1a653882016-03-18 18:05:57 +00002682 bool IsArithmeticZero() const OVERRIDE {
2683 return std::fpclassify(value_) == FP_ZERO;
2684 }
2685 bool IsArithmeticPositiveZero() const {
2686 return IsArithmeticZero() && !std::signbit(value_);
2687 }
2688 bool IsArithmeticNegativeZero() const {
2689 return IsArithmeticZero() && std::signbit(value_);
2690 }
2691 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002692 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002693 }
2694 bool IsOne() const OVERRIDE {
2695 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2696 }
2697 bool IsNaN() const {
2698 return std::isnan(value_);
2699 }
2700
2701 DECLARE_INSTRUCTION(DoubleConstant);
2702
2703 private:
2704 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2705 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2706 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2707 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2708
2709 const double value_;
2710
2711 // Only the SsaBuilder and HGraph can create floating-point constants.
2712 friend class SsaBuilder;
2713 friend class HGraph;
2714 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2715};
2716
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002717// Conditional branch. A block ending with an HIf instruction must have
2718// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002719class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002720 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002721 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2722 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002723 SetRawInputAt(0, input);
2724 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002725
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002726 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002727
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002728 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002729 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002730 }
2731
2732 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002733 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002734 }
2735
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002736 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002737
2738 private:
2739 DISALLOW_COPY_AND_ASSIGN(HIf);
2740};
2741
David Brazdilfc6a86a2015-06-26 10:33:45 +00002742
2743// Abstract instruction which marks the beginning and/or end of a try block and
2744// links it to the respective exception handlers. Behaves the same as a Goto in
2745// non-exceptional control flow.
2746// Normal-flow successor is stored at index zero, exception handlers under
2747// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002748class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002749 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002750 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002751 kEntry,
2752 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002753 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002754 };
2755
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002756 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002757 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2758 SetPackedField<BoundaryKindField>(kind);
2759 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002760
2761 bool IsControlFlow() const OVERRIDE { return true; }
2762
2763 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002764 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002765
David Brazdild26a4112015-11-10 11:07:31 +00002766 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2767 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2768 }
2769
David Brazdilfc6a86a2015-06-26 10:33:45 +00002770 // Returns whether `handler` is among its exception handlers (non-zero index
2771 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002772 bool HasExceptionHandler(const HBasicBlock& handler) const {
2773 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002774 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002775 }
2776
2777 // If not present already, adds `handler` to its block's list of exception
2778 // handlers.
2779 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002780 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002781 GetBlock()->AddSuccessor(handler);
2782 }
2783 }
2784
Vladimir Markoa1de9182016-02-25 11:37:38 +00002785 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2786 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002787
David Brazdilffee3d32015-07-06 11:48:53 +01002788 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2789
David Brazdilfc6a86a2015-06-26 10:33:45 +00002790 DECLARE_INSTRUCTION(TryBoundary);
2791
2792 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002793 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2794 static constexpr size_t kFieldBoundaryKindSize =
2795 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2796 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2797 kFieldBoundaryKind + kFieldBoundaryKindSize;
2798 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2799 "Too many packed fields.");
2800 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002801
2802 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2803};
2804
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002805// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002806class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002807 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002808 // We set CanTriggerGC to prevent any intermediate address to be live
2809 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002810 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002811 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002812 SetRawInputAt(0, cond);
2813 }
2814
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002815 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002816 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002817 return true;
2818 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002819 bool NeedsEnvironment() const OVERRIDE { return true; }
2820 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002821
2822 DECLARE_INSTRUCTION(Deoptimize);
2823
2824 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002825 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2826};
2827
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002828// Represents the ArtMethod that was passed as a first argument to
2829// the method. It is used by instructions that depend on it, like
2830// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002831class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002832 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002833 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2834 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002835
2836 DECLARE_INSTRUCTION(CurrentMethod);
2837
2838 private:
2839 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2840};
2841
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002842// Fetches an ArtMethod from the virtual table or the interface method table
2843// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002844class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002845 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002846 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002847 kVTable,
2848 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002849 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002850 };
2851 HClassTableGet(HInstruction* cls,
2852 Primitive::Type type,
2853 TableKind kind,
2854 size_t index,
2855 uint32_t dex_pc)
2856 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002857 index_(index) {
2858 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002859 SetRawInputAt(0, cls);
2860 }
2861
2862 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002863 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002864 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002865 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002866 }
2867
Vladimir Markoa1de9182016-02-25 11:37:38 +00002868 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002869 size_t GetIndex() const { return index_; }
2870
2871 DECLARE_INSTRUCTION(ClassTableGet);
2872
2873 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002874 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2875 static constexpr size_t kFieldTableKindSize =
2876 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2877 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2878 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2879 "Too many packed fields.");
2880 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2881
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002882 // The index of the ArtMethod in the table.
2883 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002884
2885 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2886};
2887
Mark Mendellfe57faa2015-09-18 09:26:15 -04002888// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2889// have one successor for each entry in the switch table, and the final successor
2890// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002891class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002892 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002893 HPackedSwitch(int32_t start_value,
2894 uint32_t num_entries,
2895 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002896 uint32_t dex_pc = kNoDexPc)
2897 : HTemplateInstruction(SideEffects::None(), dex_pc),
2898 start_value_(start_value),
2899 num_entries_(num_entries) {
2900 SetRawInputAt(0, input);
2901 }
2902
2903 bool IsControlFlow() const OVERRIDE { return true; }
2904
2905 int32_t GetStartValue() const { return start_value_; }
2906
Vladimir Marko211c2112015-09-24 16:52:33 +01002907 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002908
2909 HBasicBlock* GetDefaultBlock() const {
2910 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002911 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002912 }
2913 DECLARE_INSTRUCTION(PackedSwitch);
2914
2915 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002916 const int32_t start_value_;
2917 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002918
2919 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2920};
2921
Roland Levillain88cb1752014-10-20 16:36:47 +01002922class HUnaryOperation : public HExpression<1> {
2923 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002924 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2925 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002926 SetRawInputAt(0, input);
2927 }
2928
2929 HInstruction* GetInput() const { return InputAt(0); }
2930 Primitive::Type GetResultType() const { return GetType(); }
2931
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002932 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002933 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002934 return true;
2935 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002936
Roland Levillain31dd3d62016-02-16 12:21:02 +00002937 // Try to statically evaluate `this` and return a HConstant
2938 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002939 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002940 HConstant* TryStaticEvaluation() const;
2941
2942 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002943 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2944 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002945 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2946 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002947
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002948 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002949
2950 private:
2951 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2952};
2953
Dave Allison20dfc792014-06-16 20:44:29 -07002954class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002955 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002956 HBinaryOperation(Primitive::Type result_type,
2957 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002958 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002959 SideEffects side_effects = SideEffects::None(),
2960 uint32_t dex_pc = kNoDexPc)
2961 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002962 SetRawInputAt(0, left);
2963 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002964 }
2965
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002966 HInstruction* GetLeft() const { return InputAt(0); }
2967 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002968 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002969
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002970 virtual bool IsCommutative() const { return false; }
2971
2972 // Put constant on the right.
2973 // Returns whether order is changed.
2974 bool OrderInputsWithConstantOnTheRight() {
2975 HInstruction* left = InputAt(0);
2976 HInstruction* right = InputAt(1);
2977 if (left->IsConstant() && !right->IsConstant()) {
2978 ReplaceInput(right, 0);
2979 ReplaceInput(left, 1);
2980 return true;
2981 }
2982 return false;
2983 }
2984
2985 // Order inputs by instruction id, but favor constant on the right side.
2986 // This helps GVN for commutative ops.
2987 void OrderInputs() {
2988 DCHECK(IsCommutative());
2989 HInstruction* left = InputAt(0);
2990 HInstruction* right = InputAt(1);
2991 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2992 return;
2993 }
2994 if (OrderInputsWithConstantOnTheRight()) {
2995 return;
2996 }
2997 // Order according to instruction id.
2998 if (left->GetId() > right->GetId()) {
2999 ReplaceInput(right, 0);
3000 ReplaceInput(left, 1);
3001 }
3002 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003003
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003004 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003005 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003006 return true;
3007 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003008
Roland Levillain31dd3d62016-02-16 12:21:02 +00003009 // Try to statically evaluate `this` and return a HConstant
3010 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003011 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003012 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003013
3014 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003015 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3016 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003017 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3018 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003019 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003020 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3021 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003022 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3023 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003024 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3025 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003026 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003027 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3028 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003029
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003030 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003031 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003032 HConstant* GetConstantRight() const;
3033
3034 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003035 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003036 HInstruction* GetLeastConstantLeft() const;
3037
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003038 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003039
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003040 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003041 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3042};
3043
Mark Mendellc4701932015-04-10 13:18:51 -04003044// The comparison bias applies for floating point operations and indicates how NaN
3045// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003046enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003047 kNoBias, // bias is not applicable (i.e. for long operation)
3048 kGtBias, // return 1 for NaN comparisons
3049 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003050 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003051};
3052
Roland Levillain31dd3d62016-02-16 12:21:02 +00003053std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3054
Dave Allison20dfc792014-06-16 20:44:29 -07003055class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003056 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003057 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003058 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3059 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3060 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003061
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003062 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003063 // `instruction`, and disregard moves in between.
3064 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003065
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003066 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003067
3068 virtual IfCondition GetCondition() const = 0;
3069
Mark Mendellc4701932015-04-10 13:18:51 -04003070 virtual IfCondition GetOppositeCondition() const = 0;
3071
Vladimir Markoa1de9182016-02-25 11:37:38 +00003072 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003073 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3074
Vladimir Markoa1de9182016-02-25 11:37:38 +00003075 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3076 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003077
Vladimir Marko372f10e2016-05-17 16:30:10 +01003078 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003079 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003080 }
3081
Roland Levillain4fa13f62015-07-06 18:11:54 +01003082 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003083 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003084 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003085 if (if_cond == kCondNE) {
3086 return true;
3087 } else if (if_cond == kCondEQ) {
3088 return false;
3089 }
3090 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003091 }
3092
3093 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003094 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003095 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003096 if (if_cond == kCondEQ) {
3097 return true;
3098 } else if (if_cond == kCondNE) {
3099 return false;
3100 }
3101 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003102 }
3103
Roland Levillain31dd3d62016-02-16 12:21:02 +00003104 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003105 // Needed if we merge a HCompare into a HCondition.
3106 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3107 static constexpr size_t kFieldComparisonBiasSize =
3108 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3109 static constexpr size_t kNumberOfConditionPackedBits =
3110 kFieldComparisonBias + kFieldComparisonBiasSize;
3111 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3112 using ComparisonBiasField =
3113 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3114
Roland Levillain31dd3d62016-02-16 12:21:02 +00003115 template <typename T>
3116 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3117
3118 template <typename T>
3119 int32_t CompareFP(T x, T y) const {
3120 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3121 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3122 // Handle the bias.
3123 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3124 }
3125
3126 // Return an integer constant containing the result of a condition evaluated at compile time.
3127 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3128 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3129 }
3130
Dave Allison20dfc792014-06-16 20:44:29 -07003131 private:
3132 DISALLOW_COPY_AND_ASSIGN(HCondition);
3133};
3134
3135// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003136class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003137 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003138 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3139 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003140
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003141 bool IsCommutative() const OVERRIDE { return true; }
3142
Vladimir Marko9e23df52015-11-10 17:14:35 +00003143 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3144 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003145 return MakeConstantCondition(true, GetDexPc());
3146 }
3147 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3148 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3149 }
3150 // In the following Evaluate methods, a HCompare instruction has
3151 // been merged into this HEqual instruction; evaluate it as
3152 // `Compare(x, y) == 0`.
3153 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3154 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3155 GetDexPc());
3156 }
3157 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3158 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3159 }
3160 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3161 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003162 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003163
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003164 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003165
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003166 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003167 return kCondEQ;
3168 }
3169
Mark Mendellc4701932015-04-10 13:18:51 -04003170 IfCondition GetOppositeCondition() const OVERRIDE {
3171 return kCondNE;
3172 }
3173
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003174 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003175 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003176
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003177 DISALLOW_COPY_AND_ASSIGN(HEqual);
3178};
3179
Vladimir Markofcb503c2016-05-18 12:48:17 +01003180class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003181 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003182 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3183 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003184
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003185 bool IsCommutative() const OVERRIDE { return true; }
3186
Vladimir Marko9e23df52015-11-10 17:14:35 +00003187 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3188 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003189 return MakeConstantCondition(false, GetDexPc());
3190 }
3191 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3192 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3193 }
3194 // In the following Evaluate methods, a HCompare instruction has
3195 // been merged into this HNotEqual instruction; evaluate it as
3196 // `Compare(x, y) != 0`.
3197 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3198 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3199 }
3200 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3201 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3202 }
3203 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3204 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003205 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003206
Dave Allison20dfc792014-06-16 20:44:29 -07003207 DECLARE_INSTRUCTION(NotEqual);
3208
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003209 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003210 return kCondNE;
3211 }
3212
Mark Mendellc4701932015-04-10 13:18:51 -04003213 IfCondition GetOppositeCondition() const OVERRIDE {
3214 return kCondEQ;
3215 }
3216
Dave Allison20dfc792014-06-16 20:44:29 -07003217 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003218 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003219
Dave Allison20dfc792014-06-16 20:44:29 -07003220 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3221};
3222
Vladimir Markofcb503c2016-05-18 12:48:17 +01003223class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003224 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003225 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3226 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003227
Roland Levillain9867bc72015-08-05 10:21:34 +01003228 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003229 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003230 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003231 // In the following Evaluate methods, a HCompare instruction has
3232 // been merged into this HLessThan instruction; evaluate it as
3233 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003234 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003235 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3236 }
3237 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3238 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3239 }
3240 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3241 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003242 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003243
Dave Allison20dfc792014-06-16 20:44:29 -07003244 DECLARE_INSTRUCTION(LessThan);
3245
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003246 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003247 return kCondLT;
3248 }
3249
Mark Mendellc4701932015-04-10 13:18:51 -04003250 IfCondition GetOppositeCondition() const OVERRIDE {
3251 return kCondGE;
3252 }
3253
Dave Allison20dfc792014-06-16 20:44:29 -07003254 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003255 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003256
Dave Allison20dfc792014-06-16 20:44:29 -07003257 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3258};
3259
Vladimir Markofcb503c2016-05-18 12:48:17 +01003260class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003261 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003262 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3263 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003264
Roland Levillain9867bc72015-08-05 10:21:34 +01003265 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003266 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003267 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003268 // In the following Evaluate methods, a HCompare instruction has
3269 // been merged into this HLessThanOrEqual instruction; evaluate it as
3270 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003271 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003272 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3273 }
3274 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3275 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3276 }
3277 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3278 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003279 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003280
Dave Allison20dfc792014-06-16 20:44:29 -07003281 DECLARE_INSTRUCTION(LessThanOrEqual);
3282
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003283 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003284 return kCondLE;
3285 }
3286
Mark Mendellc4701932015-04-10 13:18:51 -04003287 IfCondition GetOppositeCondition() const OVERRIDE {
3288 return kCondGT;
3289 }
3290
Dave Allison20dfc792014-06-16 20:44:29 -07003291 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003292 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003293
Dave Allison20dfc792014-06-16 20:44:29 -07003294 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3295};
3296
Vladimir Markofcb503c2016-05-18 12:48:17 +01003297class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003298 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003299 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3300 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003301
Roland Levillain9867bc72015-08-05 10:21:34 +01003302 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003303 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003304 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003305 // In the following Evaluate methods, a HCompare instruction has
3306 // been merged into this HGreaterThan instruction; evaluate it as
3307 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003308 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003309 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3310 }
3311 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3312 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3313 }
3314 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3315 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003316 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003317
Dave Allison20dfc792014-06-16 20:44:29 -07003318 DECLARE_INSTRUCTION(GreaterThan);
3319
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003320 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003321 return kCondGT;
3322 }
3323
Mark Mendellc4701932015-04-10 13:18:51 -04003324 IfCondition GetOppositeCondition() const OVERRIDE {
3325 return kCondLE;
3326 }
3327
Dave Allison20dfc792014-06-16 20:44:29 -07003328 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003329 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003330
Dave Allison20dfc792014-06-16 20:44:29 -07003331 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3332};
3333
Vladimir Markofcb503c2016-05-18 12:48:17 +01003334class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003335 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003336 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3337 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003338
Roland Levillain9867bc72015-08-05 10:21:34 +01003339 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003340 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003341 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003342 // In the following Evaluate methods, a HCompare instruction has
3343 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3344 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003345 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003346 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3347 }
3348 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3349 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3350 }
3351 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3352 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003353 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003354
Dave Allison20dfc792014-06-16 20:44:29 -07003355 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3356
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003357 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003358 return kCondGE;
3359 }
3360
Mark Mendellc4701932015-04-10 13:18:51 -04003361 IfCondition GetOppositeCondition() const OVERRIDE {
3362 return kCondLT;
3363 }
3364
Dave Allison20dfc792014-06-16 20:44:29 -07003365 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003366 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003367
Dave Allison20dfc792014-06-16 20:44:29 -07003368 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3369};
3370
Vladimir Markofcb503c2016-05-18 12:48:17 +01003371class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003372 public:
3373 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3374 : HCondition(first, second, dex_pc) {}
3375
3376 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003377 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003378 }
3379 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003380 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3381 }
3382 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3383 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3384 LOG(FATAL) << DebugName() << " is not defined for float values";
3385 UNREACHABLE();
3386 }
3387 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3388 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3389 LOG(FATAL) << DebugName() << " is not defined for double values";
3390 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003391 }
3392
3393 DECLARE_INSTRUCTION(Below);
3394
3395 IfCondition GetCondition() const OVERRIDE {
3396 return kCondB;
3397 }
3398
3399 IfCondition GetOppositeCondition() const OVERRIDE {
3400 return kCondAE;
3401 }
3402
3403 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003404 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003405 return MakeUnsigned(x) < MakeUnsigned(y);
3406 }
Aart Bike9f37602015-10-09 11:15:55 -07003407
3408 DISALLOW_COPY_AND_ASSIGN(HBelow);
3409};
3410
Vladimir Markofcb503c2016-05-18 12:48:17 +01003411class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003412 public:
3413 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3414 : HCondition(first, second, dex_pc) {}
3415
3416 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003417 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003418 }
3419 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003420 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3421 }
3422 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3423 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3424 LOG(FATAL) << DebugName() << " is not defined for float values";
3425 UNREACHABLE();
3426 }
3427 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3428 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3429 LOG(FATAL) << DebugName() << " is not defined for double values";
3430 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003431 }
3432
3433 DECLARE_INSTRUCTION(BelowOrEqual);
3434
3435 IfCondition GetCondition() const OVERRIDE {
3436 return kCondBE;
3437 }
3438
3439 IfCondition GetOppositeCondition() const OVERRIDE {
3440 return kCondA;
3441 }
3442
3443 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003444 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003445 return MakeUnsigned(x) <= MakeUnsigned(y);
3446 }
Aart Bike9f37602015-10-09 11:15:55 -07003447
3448 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3449};
3450
Vladimir Markofcb503c2016-05-18 12:48:17 +01003451class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003452 public:
3453 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3454 : HCondition(first, second, dex_pc) {}
3455
3456 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003457 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003458 }
3459 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003460 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3461 }
3462 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3463 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3464 LOG(FATAL) << DebugName() << " is not defined for float values";
3465 UNREACHABLE();
3466 }
3467 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3468 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3469 LOG(FATAL) << DebugName() << " is not defined for double values";
3470 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003471 }
3472
3473 DECLARE_INSTRUCTION(Above);
3474
3475 IfCondition GetCondition() const OVERRIDE {
3476 return kCondA;
3477 }
3478
3479 IfCondition GetOppositeCondition() const OVERRIDE {
3480 return kCondBE;
3481 }
3482
3483 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003484 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003485 return MakeUnsigned(x) > MakeUnsigned(y);
3486 }
Aart Bike9f37602015-10-09 11:15:55 -07003487
3488 DISALLOW_COPY_AND_ASSIGN(HAbove);
3489};
3490
Vladimir Markofcb503c2016-05-18 12:48:17 +01003491class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003492 public:
3493 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3494 : HCondition(first, second, dex_pc) {}
3495
3496 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003497 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003498 }
3499 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003500 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3501 }
3502 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3503 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3504 LOG(FATAL) << DebugName() << " is not defined for float values";
3505 UNREACHABLE();
3506 }
3507 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3508 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3509 LOG(FATAL) << DebugName() << " is not defined for double values";
3510 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003511 }
3512
3513 DECLARE_INSTRUCTION(AboveOrEqual);
3514
3515 IfCondition GetCondition() const OVERRIDE {
3516 return kCondAE;
3517 }
3518
3519 IfCondition GetOppositeCondition() const OVERRIDE {
3520 return kCondB;
3521 }
3522
3523 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003524 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003525 return MakeUnsigned(x) >= MakeUnsigned(y);
3526 }
Aart Bike9f37602015-10-09 11:15:55 -07003527
3528 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3529};
Dave Allison20dfc792014-06-16 20:44:29 -07003530
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003531// Instruction to check how two inputs compare to each other.
3532// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003533class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003534 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003535 // Note that `comparison_type` is the type of comparison performed
3536 // between the comparison's inputs, not the type of the instantiated
3537 // HCompare instruction (which is always Primitive::kPrimInt).
3538 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003539 HInstruction* first,
3540 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003541 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003542 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003543 : HBinaryOperation(Primitive::kPrimInt,
3544 first,
3545 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003546 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003547 dex_pc) {
3548 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003549 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3550 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003551 }
3552
Roland Levillain9867bc72015-08-05 10:21:34 +01003553 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003554 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3555
3556 template <typename T>
3557 int32_t ComputeFP(T x, T y) const {
3558 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3559 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3560 // Handle the bias.
3561 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3562 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003563
Roland Levillain9867bc72015-08-05 10:21:34 +01003564 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003565 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3566 // reach this code path when processing a freshly built HIR
3567 // graph. However HCompare integer instructions can be synthesized
3568 // by the instruction simplifier to implement IntegerCompare and
3569 // IntegerSignum intrinsics, so we have to handle this case.
3570 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003571 }
3572 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003573 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3574 }
3575 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3576 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3577 }
3578 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3579 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003580 }
3581
Vladimir Marko372f10e2016-05-17 16:30:10 +01003582 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003583 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003584 }
3585
Vladimir Markoa1de9182016-02-25 11:37:38 +00003586 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003587
Roland Levillain31dd3d62016-02-16 12:21:02 +00003588 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003589 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003590 bool IsGtBias() const {
3591 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003592 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003593 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003594
Roland Levillain1693a1f2016-03-15 14:57:31 +00003595 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3596 // Comparisons do not require a runtime call in any back end.
3597 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003598 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003599
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003600 DECLARE_INSTRUCTION(Compare);
3601
Roland Levillain31dd3d62016-02-16 12:21:02 +00003602 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003603 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3604 static constexpr size_t kFieldComparisonBiasSize =
3605 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3606 static constexpr size_t kNumberOfComparePackedBits =
3607 kFieldComparisonBias + kFieldComparisonBiasSize;
3608 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3609 using ComparisonBiasField =
3610 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3611
Roland Levillain31dd3d62016-02-16 12:21:02 +00003612 // Return an integer constant containing the result of a comparison evaluated at compile time.
3613 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3614 DCHECK(value == -1 || value == 0 || value == 1) << value;
3615 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3616 }
3617
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003618 private:
3619 DISALLOW_COPY_AND_ASSIGN(HCompare);
3620};
3621
Vladimir Markofcb503c2016-05-18 12:48:17 +01003622class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003623 public:
3624 HNewInstance(HInstruction* cls,
3625 HCurrentMethod* current_method,
3626 uint32_t dex_pc,
3627 uint16_t type_index,
3628 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003629 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003630 bool finalizable,
3631 QuickEntrypointEnum entrypoint)
3632 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3633 type_index_(type_index),
3634 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003635 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003636 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003637 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003638 SetRawInputAt(0, cls);
3639 SetRawInputAt(1, current_method);
3640 }
3641
3642 uint16_t GetTypeIndex() const { return type_index_; }
3643 const DexFile& GetDexFile() const { return dex_file_; }
3644
3645 // Calls runtime so needs an environment.
3646 bool NeedsEnvironment() const OVERRIDE { return true; }
3647
Mingyao Yang062157f2016-03-02 10:15:36 -08003648 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3649 bool CanThrow() const OVERRIDE { return true; }
3650
3651 // Needs to call into runtime to make sure it's instantiable/accessible.
3652 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003653
Vladimir Markoa1de9182016-02-25 11:37:38 +00003654 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003655
3656 bool CanBeNull() const OVERRIDE { return false; }
3657
3658 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3659
3660 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3661 entrypoint_ = entrypoint;
3662 }
3663
3664 bool IsStringAlloc() const;
3665
3666 DECLARE_INSTRUCTION(NewInstance);
3667
3668 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003669 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3670 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003671 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3672 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3673 "Too many packed fields.");
3674
David Brazdil6de19382016-01-08 17:37:10 +00003675 const uint16_t type_index_;
3676 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003677 QuickEntrypointEnum entrypoint_;
3678
3679 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3680};
3681
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003682enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003683#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3684 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003685#include "intrinsics_list.h"
3686 kNone,
3687 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3688#undef INTRINSICS_LIST
3689#undef OPTIMIZING_INTRINSICS
3690};
3691std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3692
Agi Csaki05f20562015-08-19 14:58:14 -07003693enum IntrinsicNeedsEnvironmentOrCache {
3694 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3695 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003696};
3697
Aart Bik5d75afe2015-12-14 11:57:01 -08003698enum IntrinsicSideEffects {
3699 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3700 kReadSideEffects, // Intrinsic may read heap memory.
3701 kWriteSideEffects, // Intrinsic may write heap memory.
3702 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3703};
3704
3705enum IntrinsicExceptions {
3706 kNoThrow, // Intrinsic does not throw any exceptions.
3707 kCanThrow // Intrinsic may throw exceptions.
3708};
3709
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003710class HInvoke : public HInstruction {
3711 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003712 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003713
Vladimir Marko372f10e2016-05-17 16:30:10 +01003714 using HInstruction::GetInputRecords; // Keep the const version visible.
3715 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3716 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3717 }
3718
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003719 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003720 SetRawInputAt(index, argument);
3721 }
3722
Roland Levillain3e3d7332015-04-28 11:00:54 +01003723 // Return the number of arguments. This number can be lower than
3724 // the number of inputs returned by InputCount(), as some invoke
3725 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3726 // inputs at the end of their list of inputs.
3727 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3728
Vladimir Markoa1de9182016-02-25 11:37:38 +00003729 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003730
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003731 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003732 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003733
Vladimir Markoa1de9182016-02-25 11:37:38 +00003734 InvokeType GetOriginalInvokeType() const {
3735 return GetPackedField<OriginalInvokeTypeField>();
3736 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003737
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003738 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003739 return intrinsic_;
3740 }
3741
Aart Bik5d75afe2015-12-14 11:57:01 -08003742 void SetIntrinsic(Intrinsics intrinsic,
3743 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3744 IntrinsicSideEffects side_effects,
3745 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003746
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003747 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003748 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003749 }
3750
Vladimir Markoa1de9182016-02-25 11:37:38 +00003751 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003752
3753 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3754
Vladimir Marko372f10e2016-05-17 16:30:10 +01003755 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003756 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3757 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003758
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003759 uint32_t* GetIntrinsicOptimizations() {
3760 return &intrinsic_optimizations_;
3761 }
3762
3763 const uint32_t* GetIntrinsicOptimizations() const {
3764 return &intrinsic_optimizations_;
3765 }
3766
3767 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3768
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003769 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003770
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003771 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003772 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3773 static constexpr size_t kFieldOriginalInvokeTypeSize =
3774 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3775 static constexpr size_t kFieldReturnType =
3776 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3777 static constexpr size_t kFieldReturnTypeSize =
3778 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3779 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3780 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3781 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3782 using OriginalInvokeTypeField =
3783 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3784 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3785
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003786 HInvoke(ArenaAllocator* arena,
3787 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003788 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003789 Primitive::Type return_type,
3790 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003791 uint32_t dex_method_index,
3792 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003793 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003794 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003795 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003796 inputs_(number_of_arguments + number_of_other_inputs,
3797 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003798 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003799 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003800 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003801 SetPackedField<ReturnTypeField>(return_type);
3802 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3803 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003804 }
3805
Vladimir Markoa1de9182016-02-25 11:37:38 +00003806 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003807
Roland Levillain3e3d7332015-04-28 11:00:54 +01003808 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003809 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003810 const uint32_t dex_method_index_;
3811 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003812
3813 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3814 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003815
3816 private:
3817 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3818};
3819
Vladimir Markofcb503c2016-05-18 12:48:17 +01003820class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003821 public:
3822 HInvokeUnresolved(ArenaAllocator* arena,
3823 uint32_t number_of_arguments,
3824 Primitive::Type return_type,
3825 uint32_t dex_pc,
3826 uint32_t dex_method_index,
3827 InvokeType invoke_type)
3828 : HInvoke(arena,
3829 number_of_arguments,
3830 0u /* number_of_other_inputs */,
3831 return_type,
3832 dex_pc,
3833 dex_method_index,
3834 invoke_type) {
3835 }
3836
3837 DECLARE_INSTRUCTION(InvokeUnresolved);
3838
3839 private:
3840 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3841};
3842
Vladimir Markofcb503c2016-05-18 12:48:17 +01003843class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003844 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003845 // Requirements of this method call regarding the class
3846 // initialization (clinit) check of its declaring class.
3847 enum class ClinitCheckRequirement {
3848 kNone, // Class already initialized.
3849 kExplicit, // Static call having explicit clinit check as last input.
3850 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003851 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003852 };
3853
Vladimir Marko58155012015-08-19 12:49:41 +00003854 // Determines how to load the target ArtMethod*.
3855 enum class MethodLoadKind {
3856 // Use a String init ArtMethod* loaded from Thread entrypoints.
3857 kStringInit,
3858
3859 // Use the method's own ArtMethod* loaded by the register allocator.
3860 kRecursive,
3861
3862 // Use ArtMethod* at a known address, embed the direct address in the code.
3863 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3864 kDirectAddress,
3865
3866 // Use ArtMethod* at an address that will be known at link time, embed the direct
3867 // address in the code. If the image is relocatable, emit .patch_oat entry.
3868 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3869 // the image relocatable or not.
3870 kDirectAddressWithFixup,
3871
Vladimir Markoa1de9182016-02-25 11:37:38 +00003872 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003873 // Used when we need to use the dex cache, for example for invoke-static that
3874 // may cause class initialization (the entry may point to a resolution method),
3875 // and we know that we can access the dex cache arrays using a PC-relative load.
3876 kDexCachePcRelative,
3877
3878 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3879 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3880 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3881 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3882 kDexCacheViaMethod,
3883 };
3884
3885 // Determines the location of the code pointer.
3886 enum class CodePtrLocation {
3887 // Recursive call, use local PC-relative call instruction.
3888 kCallSelf,
3889
3890 // Use PC-relative call instruction patched at link time.
3891 // Used for calls within an oat file, boot->boot or app->app.
3892 kCallPCRelative,
3893
3894 // Call to a known target address, embed the direct address in code.
3895 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3896 kCallDirect,
3897
3898 // Call to a target address that will be known at link time, embed the direct
3899 // address in code. If the image is relocatable, emit .patch_oat entry.
3900 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3901 // the image relocatable or not.
3902 kCallDirectWithFixup,
3903
3904 // Use code pointer from the ArtMethod*.
3905 // Used when we don't know the target code. This is also the last-resort-kind used when
3906 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3907 kCallArtMethod,
3908 };
3909
3910 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003911 MethodLoadKind method_load_kind;
3912 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003913 // The method load data holds
3914 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3915 // Note that there are multiple string init methods, each having its own offset.
3916 // - the method address for kDirectAddress
3917 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003918 uint64_t method_load_data;
3919 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003920 };
3921
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003922 HInvokeStaticOrDirect(ArenaAllocator* arena,
3923 uint32_t number_of_arguments,
3924 Primitive::Type return_type,
3925 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003926 uint32_t method_index,
3927 MethodReference target_method,
3928 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003929 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003930 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003931 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003932 : HInvoke(arena,
3933 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003934 // There is potentially one extra argument for the HCurrentMethod node, and
3935 // potentially one other if the clinit check is explicit, and potentially
3936 // one other if the method is a string factory.
3937 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003938 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003939 return_type,
3940 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003941 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003942 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003943 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003944 dispatch_info_(dispatch_info) {
3945 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3946 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3947 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003948
Vladimir Markodc151b22015-10-15 18:02:30 +01003949 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003950 bool had_current_method_input = HasCurrentMethodInput();
3951 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3952
3953 // Using the current method is the default and once we find a better
3954 // method load kind, we should not go back to using the current method.
3955 DCHECK(had_current_method_input || !needs_current_method_input);
3956
3957 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003958 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3959 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003960 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003961 dispatch_info_ = dispatch_info;
3962 }
3963
Vladimir Markoc53c0792015-11-19 15:48:33 +00003964 void AddSpecialInput(HInstruction* input) {
3965 // We allow only one special input.
3966 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3967 DCHECK(InputCount() == GetSpecialInputIndex() ||
3968 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3969 InsertInputAt(GetSpecialInputIndex(), input);
3970 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003971
Vladimir Marko372f10e2016-05-17 16:30:10 +01003972 using HInstruction::GetInputRecords; // Keep the const version visible.
3973 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3974 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
3975 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
3976 DCHECK(!input_records.empty());
3977 DCHECK_GT(input_records.size(), GetNumberOfArguments());
3978 HInstruction* last_input = input_records.back().GetInstruction();
3979 // Note: `last_input` may be null during arguments setup.
3980 if (last_input != nullptr) {
3981 // `last_input` is the last input of a static invoke marked as having
3982 // an explicit clinit check. It must either be:
3983 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3984 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3985 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
3986 }
3987 }
3988 return input_records;
3989 }
3990
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003991 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003992 // We access the method via the dex cache so we can't do an implicit null check.
3993 // TODO: for intrinsics we can generate implicit null checks.
3994 return false;
3995 }
3996
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003997 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003998 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003999 }
4000
Vladimir Markoc53c0792015-11-19 15:48:33 +00004001 // Get the index of the special input, if any.
4002 //
David Brazdil6de19382016-01-08 17:37:10 +00004003 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4004 // method pointer; otherwise there may be one platform-specific special input,
4005 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004006 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004007 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004008
Vladimir Markoa1de9182016-02-25 11:37:38 +00004009 InvokeType GetOptimizedInvokeType() const {
4010 return GetPackedField<OptimizedInvokeTypeField>();
4011 }
4012
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004013 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004014 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004015 }
4016
Vladimir Marko58155012015-08-19 12:49:41 +00004017 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4018 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4019 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004020 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004021 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004022 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004023 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004024 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4025 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004026 bool HasCurrentMethodInput() const {
4027 // This function can be called only after the invoke has been fully initialized by the builder.
4028 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004029 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004030 return true;
4031 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004032 DCHECK(InputCount() == GetSpecialInputIndex() ||
4033 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004034 return false;
4035 }
4036 }
Vladimir Marko58155012015-08-19 12:49:41 +00004037 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4038 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004039 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004040
4041 int32_t GetStringInitOffset() const {
4042 DCHECK(IsStringInit());
4043 return dispatch_info_.method_load_data;
4044 }
4045
4046 uint64_t GetMethodAddress() const {
4047 DCHECK(HasMethodAddress());
4048 return dispatch_info_.method_load_data;
4049 }
4050
4051 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004052 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004053 return dispatch_info_.method_load_data;
4054 }
4055
4056 uint64_t GetDirectCodePtr() const {
4057 DCHECK(HasDirectCodePtr());
4058 return dispatch_info_.direct_code_ptr;
4059 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004060
Vladimir Markoa1de9182016-02-25 11:37:38 +00004061 ClinitCheckRequirement GetClinitCheckRequirement() const {
4062 return GetPackedField<ClinitCheckRequirementField>();
4063 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004064
Roland Levillain4c0eb422015-04-24 16:43:49 +01004065 // Is this instruction a call to a static method?
4066 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004067 return GetOriginalInvokeType() == kStatic;
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:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004109 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4110 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4111 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4112 static constexpr size_t kFieldClinitCheckRequirement =
4113 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4114 static constexpr size_t kFieldClinitCheckRequirementSize =
4115 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4116 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4117 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4118 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4119 "Too many packed fields.");
4120 using OptimizedInvokeTypeField =
4121 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4122 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4123 kFieldClinitCheckRequirement,
4124 kFieldClinitCheckRequirementSize>;
4125
Vladimir Marko58155012015-08-19 12:49:41 +00004126 // The target method may refer to different dex file or method index than the original
4127 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4128 // in derived class to increase visibility.
4129 MethodReference target_method_;
4130 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004131
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004132 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004133};
Vladimir Markof64242a2015-12-01 14:58:23 +00004134std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004135std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004136
Vladimir Markofcb503c2016-05-18 12:48:17 +01004137class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004138 public:
4139 HInvokeVirtual(ArenaAllocator* arena,
4140 uint32_t number_of_arguments,
4141 Primitive::Type return_type,
4142 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004143 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004144 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004145 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, 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:
4158 const uint32_t vtable_index_;
4159
4160 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4161};
4162
Vladimir Markofcb503c2016-05-18 12:48:17 +01004163class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004164 public:
4165 HInvokeInterface(ArenaAllocator* arena,
4166 uint32_t number_of_arguments,
4167 Primitive::Type return_type,
4168 uint32_t dex_pc,
4169 uint32_t dex_method_index,
4170 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004171 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004172 imt_index_(imt_index) {}
4173
Calin Juravle641547a2015-04-21 22:08:51 +01004174 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004175 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004176 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004177 }
4178
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004179 uint32_t GetImtIndex() const { return imt_index_; }
4180 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4181
4182 DECLARE_INSTRUCTION(InvokeInterface);
4183
4184 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004185 const uint32_t imt_index_;
4186
4187 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4188};
4189
Vladimir Markofcb503c2016-05-18 12:48:17 +01004190class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004191 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004192 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004193 : HUnaryOperation(result_type, input, dex_pc) {
4194 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4195 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004196
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004197 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004198
4199 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004200 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004201 }
4202 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004203 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004204 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004205 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4206 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4207 }
4208 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4209 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4210 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004211
Roland Levillain88cb1752014-10-20 16:36:47 +01004212 DECLARE_INSTRUCTION(Neg);
4213
4214 private:
4215 DISALLOW_COPY_AND_ASSIGN(HNeg);
4216};
4217
Vladimir Markofcb503c2016-05-18 12:48:17 +01004218class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004219 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004220 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004221 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004222 uint32_t dex_pc,
4223 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004224 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004225 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004226 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004227 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004228 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004229 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004230 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004231 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004232 }
4233
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004234 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004235 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004236
4237 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004238 bool NeedsEnvironment() const OVERRIDE { return true; }
4239
Mingyao Yang0c365e62015-03-31 15:09:29 -07004240 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4241 bool CanThrow() const OVERRIDE { return true; }
4242
Calin Juravle10e244f2015-01-26 18:54:32 +00004243 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004244
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004245 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4246
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004247 DECLARE_INSTRUCTION(NewArray);
4248
4249 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004250 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004251 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004252 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004253
4254 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4255};
4256
Vladimir Markofcb503c2016-05-18 12:48:17 +01004257class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004258 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004259 HAdd(Primitive::Type result_type,
4260 HInstruction* left,
4261 HInstruction* right,
4262 uint32_t dex_pc = kNoDexPc)
4263 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004264
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004265 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004266
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004267 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004268
4269 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004270 return GetBlock()->GetGraph()->GetIntConstant(
4271 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004272 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004273 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004274 return GetBlock()->GetGraph()->GetLongConstant(
4275 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004276 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004277 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4278 return GetBlock()->GetGraph()->GetFloatConstant(
4279 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4280 }
4281 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4282 return GetBlock()->GetGraph()->GetDoubleConstant(
4283 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4284 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004285
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004286 DECLARE_INSTRUCTION(Add);
4287
4288 private:
4289 DISALLOW_COPY_AND_ASSIGN(HAdd);
4290};
4291
Vladimir Markofcb503c2016-05-18 12:48:17 +01004292class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004293 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004294 HSub(Primitive::Type result_type,
4295 HInstruction* left,
4296 HInstruction* right,
4297 uint32_t dex_pc = kNoDexPc)
4298 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004299
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004300 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004301
4302 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004303 return GetBlock()->GetGraph()->GetIntConstant(
4304 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004305 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004306 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004307 return GetBlock()->GetGraph()->GetLongConstant(
4308 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004309 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004310 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4311 return GetBlock()->GetGraph()->GetFloatConstant(
4312 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4313 }
4314 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4315 return GetBlock()->GetGraph()->GetDoubleConstant(
4316 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4317 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004318
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004319 DECLARE_INSTRUCTION(Sub);
4320
4321 private:
4322 DISALLOW_COPY_AND_ASSIGN(HSub);
4323};
4324
Vladimir Markofcb503c2016-05-18 12:48:17 +01004325class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004326 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004327 HMul(Primitive::Type result_type,
4328 HInstruction* left,
4329 HInstruction* right,
4330 uint32_t dex_pc = kNoDexPc)
4331 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004332
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004333 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004334
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004335 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004336
4337 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004338 return GetBlock()->GetGraph()->GetIntConstant(
4339 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004340 }
4341 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004342 return GetBlock()->GetGraph()->GetLongConstant(
4343 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004344 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004345 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4346 return GetBlock()->GetGraph()->GetFloatConstant(
4347 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4348 }
4349 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4350 return GetBlock()->GetGraph()->GetDoubleConstant(
4351 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4352 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004353
4354 DECLARE_INSTRUCTION(Mul);
4355
4356 private:
4357 DISALLOW_COPY_AND_ASSIGN(HMul);
4358};
4359
Vladimir Markofcb503c2016-05-18 12:48:17 +01004360class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004361 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004362 HDiv(Primitive::Type result_type,
4363 HInstruction* left,
4364 HInstruction* right,
4365 uint32_t dex_pc)
4366 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004367
Roland Levillain9867bc72015-08-05 10:21:34 +01004368 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004369 T ComputeIntegral(T x, T y) const {
4370 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004371 // Our graph structure ensures we never have 0 for `y` during
4372 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004373 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004374 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004375 return (y == -1) ? -x : x / y;
4376 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004377
Roland Levillain31dd3d62016-02-16 12:21:02 +00004378 template <typename T>
4379 T ComputeFP(T x, T y) const {
4380 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4381 return x / y;
4382 }
4383
Roland Levillain9867bc72015-08-05 10:21:34 +01004384 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004385 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004386 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004387 }
4388 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004389 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004390 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4391 }
4392 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4393 return GetBlock()->GetGraph()->GetFloatConstant(
4394 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4395 }
4396 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4397 return GetBlock()->GetGraph()->GetDoubleConstant(
4398 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004399 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004400
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004401 static SideEffects SideEffectsForArchRuntimeCalls() {
4402 // The generated code can use a runtime call.
4403 return SideEffects::CanTriggerGC();
4404 }
4405
Calin Juravle7c4954d2014-10-28 16:57:40 +00004406 DECLARE_INSTRUCTION(Div);
4407
4408 private:
4409 DISALLOW_COPY_AND_ASSIGN(HDiv);
4410};
4411
Vladimir Markofcb503c2016-05-18 12:48:17 +01004412class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004413 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004414 HRem(Primitive::Type result_type,
4415 HInstruction* left,
4416 HInstruction* right,
4417 uint32_t dex_pc)
4418 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004419
Roland Levillain9867bc72015-08-05 10:21:34 +01004420 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004421 T ComputeIntegral(T x, T y) const {
4422 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004423 // Our graph structure ensures we never have 0 for `y` during
4424 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004425 DCHECK_NE(y, 0);
4426 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4427 return (y == -1) ? 0 : x % y;
4428 }
4429
Roland Levillain31dd3d62016-02-16 12:21:02 +00004430 template <typename T>
4431 T ComputeFP(T x, T y) const {
4432 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4433 return std::fmod(x, y);
4434 }
4435
Roland Levillain9867bc72015-08-05 10:21:34 +01004436 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004437 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004438 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004439 }
4440 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004441 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004442 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004443 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004444 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4445 return GetBlock()->GetGraph()->GetFloatConstant(
4446 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4447 }
4448 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4449 return GetBlock()->GetGraph()->GetDoubleConstant(
4450 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4451 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004452
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004453 static SideEffects SideEffectsForArchRuntimeCalls() {
4454 return SideEffects::CanTriggerGC();
4455 }
4456
Calin Juravlebacfec32014-11-14 15:54:36 +00004457 DECLARE_INSTRUCTION(Rem);
4458
4459 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004460 DISALLOW_COPY_AND_ASSIGN(HRem);
4461};
4462
Vladimir Markofcb503c2016-05-18 12:48:17 +01004463class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004464 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004465 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4466 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004467 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004468 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004469 SetRawInputAt(0, value);
4470 }
4471
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004472 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4473
Calin Juravled0d48522014-11-04 16:40:20 +00004474 bool CanBeMoved() const OVERRIDE { return true; }
4475
Vladimir Marko372f10e2016-05-17 16:30:10 +01004476 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004477 return true;
4478 }
4479
4480 bool NeedsEnvironment() const OVERRIDE { return true; }
4481 bool CanThrow() const OVERRIDE { return true; }
4482
Calin Juravled0d48522014-11-04 16:40:20 +00004483 DECLARE_INSTRUCTION(DivZeroCheck);
4484
4485 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004486 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4487};
4488
Vladimir Markofcb503c2016-05-18 12:48:17 +01004489class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004490 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004491 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004492 HInstruction* value,
4493 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004494 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004495 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4496 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4497 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004498 }
4499
Roland Levillain5b5b9312016-03-22 14:57:31 +00004500 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004501 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004502 return value << (distance & max_shift_distance);
4503 }
4504
4505 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004506 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004507 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004508 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004509 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004510 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004511 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004512 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004513 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4514 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4515 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4516 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004517 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004518 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4519 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004520 LOG(FATAL) << DebugName() << " is not defined for float values";
4521 UNREACHABLE();
4522 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004523 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4524 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004525 LOG(FATAL) << DebugName() << " is not defined for double values";
4526 UNREACHABLE();
4527 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004528
4529 DECLARE_INSTRUCTION(Shl);
4530
4531 private:
4532 DISALLOW_COPY_AND_ASSIGN(HShl);
4533};
4534
Vladimir Markofcb503c2016-05-18 12:48:17 +01004535class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004536 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004537 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004538 HInstruction* value,
4539 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004540 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004541 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4542 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4543 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004544 }
4545
Roland Levillain5b5b9312016-03-22 14:57:31 +00004546 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004547 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004548 return value >> (distance & max_shift_distance);
4549 }
4550
4551 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004552 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004553 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004554 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004555 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004556 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004557 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004558 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004559 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4560 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4561 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4562 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004563 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004564 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4565 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004566 LOG(FATAL) << DebugName() << " is not defined for float values";
4567 UNREACHABLE();
4568 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004569 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4570 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004571 LOG(FATAL) << DebugName() << " is not defined for double values";
4572 UNREACHABLE();
4573 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004574
4575 DECLARE_INSTRUCTION(Shr);
4576
4577 private:
4578 DISALLOW_COPY_AND_ASSIGN(HShr);
4579};
4580
Vladimir Markofcb503c2016-05-18 12:48:17 +01004581class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004582 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004583 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004584 HInstruction* value,
4585 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004586 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004587 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4588 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4589 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004590 }
4591
Roland Levillain5b5b9312016-03-22 14:57:31 +00004592 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004593 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004594 typedef typename std::make_unsigned<T>::type V;
4595 V ux = static_cast<V>(value);
4596 return static_cast<T>(ux >> (distance & max_shift_distance));
4597 }
4598
4599 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004600 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004601 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004602 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004603 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004604 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004605 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004606 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004607 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4608 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4609 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4610 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004611 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004612 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4613 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004614 LOG(FATAL) << DebugName() << " is not defined for float values";
4615 UNREACHABLE();
4616 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004617 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4618 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004619 LOG(FATAL) << DebugName() << " is not defined for double values";
4620 UNREACHABLE();
4621 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004622
4623 DECLARE_INSTRUCTION(UShr);
4624
4625 private:
4626 DISALLOW_COPY_AND_ASSIGN(HUShr);
4627};
4628
Vladimir Markofcb503c2016-05-18 12:48:17 +01004629class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004630 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004631 HAnd(Primitive::Type result_type,
4632 HInstruction* left,
4633 HInstruction* right,
4634 uint32_t dex_pc = kNoDexPc)
4635 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004636
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004637 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004638
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004639 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004640
4641 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004642 return GetBlock()->GetGraph()->GetIntConstant(
4643 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004644 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004645 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004646 return GetBlock()->GetGraph()->GetLongConstant(
4647 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004648 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004649 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4650 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4651 LOG(FATAL) << DebugName() << " is not defined for float values";
4652 UNREACHABLE();
4653 }
4654 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4655 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4656 LOG(FATAL) << DebugName() << " is not defined for double values";
4657 UNREACHABLE();
4658 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004659
4660 DECLARE_INSTRUCTION(And);
4661
4662 private:
4663 DISALLOW_COPY_AND_ASSIGN(HAnd);
4664};
4665
Vladimir Markofcb503c2016-05-18 12:48:17 +01004666class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004667 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004668 HOr(Primitive::Type result_type,
4669 HInstruction* left,
4670 HInstruction* right,
4671 uint32_t dex_pc = kNoDexPc)
4672 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004673
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004674 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004675
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004676 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004677
4678 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004679 return GetBlock()->GetGraph()->GetIntConstant(
4680 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004681 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004682 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004683 return GetBlock()->GetGraph()->GetLongConstant(
4684 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004685 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004686 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4687 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4688 LOG(FATAL) << DebugName() << " is not defined for float values";
4689 UNREACHABLE();
4690 }
4691 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4692 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4693 LOG(FATAL) << DebugName() << " is not defined for double values";
4694 UNREACHABLE();
4695 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004696
4697 DECLARE_INSTRUCTION(Or);
4698
4699 private:
4700 DISALLOW_COPY_AND_ASSIGN(HOr);
4701};
4702
Vladimir Markofcb503c2016-05-18 12:48:17 +01004703class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004704 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004705 HXor(Primitive::Type result_type,
4706 HInstruction* left,
4707 HInstruction* right,
4708 uint32_t dex_pc = kNoDexPc)
4709 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004710
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004711 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004712
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004713 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004714
4715 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004716 return GetBlock()->GetGraph()->GetIntConstant(
4717 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004718 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004719 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004720 return GetBlock()->GetGraph()->GetLongConstant(
4721 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004722 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004723 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4724 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4725 LOG(FATAL) << DebugName() << " is not defined for float values";
4726 UNREACHABLE();
4727 }
4728 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4729 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4730 LOG(FATAL) << DebugName() << " is not defined for double values";
4731 UNREACHABLE();
4732 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004733
4734 DECLARE_INSTRUCTION(Xor);
4735
4736 private:
4737 DISALLOW_COPY_AND_ASSIGN(HXor);
4738};
4739
Vladimir Markofcb503c2016-05-18 12:48:17 +01004740class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004741 public:
4742 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004743 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004744 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4745 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004746 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004747
Roland Levillain5b5b9312016-03-22 14:57:31 +00004748 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004749 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004750 typedef typename std::make_unsigned<T>::type V;
4751 V ux = static_cast<V>(value);
4752 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004753 return static_cast<T>(ux);
4754 } else {
4755 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004756 return static_cast<T>(ux >> (distance & max_shift_value)) |
4757 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004758 }
4759 }
4760
Roland Levillain5b5b9312016-03-22 14:57:31 +00004761 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004762 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004763 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004764 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004765 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004766 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004767 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004768 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004769 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4770 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4771 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4772 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004773 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004774 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4775 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004776 LOG(FATAL) << DebugName() << " is not defined for float values";
4777 UNREACHABLE();
4778 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004779 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4780 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004781 LOG(FATAL) << DebugName() << " is not defined for double values";
4782 UNREACHABLE();
4783 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004784
4785 DECLARE_INSTRUCTION(Ror);
4786
4787 private:
4788 DISALLOW_COPY_AND_ASSIGN(HRor);
4789};
4790
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004791// The value of a parameter in this method. Its location depends on
4792// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004793class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004794 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004795 HParameterValue(const DexFile& dex_file,
4796 uint16_t type_index,
4797 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004798 Primitive::Type parameter_type,
4799 bool is_this = false)
4800 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004801 dex_file_(dex_file),
4802 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004803 index_(index) {
4804 SetPackedFlag<kFlagIsThis>(is_this);
4805 SetPackedFlag<kFlagCanBeNull>(!is_this);
4806 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004807
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004808 const DexFile& GetDexFile() const { return dex_file_; }
4809 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004810 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004811 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004812
Vladimir Markoa1de9182016-02-25 11:37:38 +00004813 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4814 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004815
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004816 DECLARE_INSTRUCTION(ParameterValue);
4817
4818 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004819 // Whether or not the parameter value corresponds to 'this' argument.
4820 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4821 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4822 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4823 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4824 "Too many packed fields.");
4825
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004826 const DexFile& dex_file_;
4827 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004828 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004829 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004830 const uint8_t index_;
4831
4832 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4833};
4834
Vladimir Markofcb503c2016-05-18 12:48:17 +01004835class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004836 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004837 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4838 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004839
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004840 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004841 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004842 return true;
4843 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004844
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004845 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004846
4847 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004848 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004849 }
4850 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004851 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004852 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004853 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4854 LOG(FATAL) << DebugName() << " is not defined for float values";
4855 UNREACHABLE();
4856 }
4857 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4858 LOG(FATAL) << DebugName() << " is not defined for double values";
4859 UNREACHABLE();
4860 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004861
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004862 DECLARE_INSTRUCTION(Not);
4863
4864 private:
4865 DISALLOW_COPY_AND_ASSIGN(HNot);
4866};
4867
Vladimir Markofcb503c2016-05-18 12:48:17 +01004868class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004869 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004870 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4871 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004872
4873 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004874 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004875 return true;
4876 }
4877
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004878 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004879 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004880 return !x;
4881 }
4882
Roland Levillain9867bc72015-08-05 10:21:34 +01004883 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004884 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004885 }
4886 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4887 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004888 UNREACHABLE();
4889 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004890 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4891 LOG(FATAL) << DebugName() << " is not defined for float values";
4892 UNREACHABLE();
4893 }
4894 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4895 LOG(FATAL) << DebugName() << " is not defined for double values";
4896 UNREACHABLE();
4897 }
David Brazdil66d126e2015-04-03 16:02:44 +01004898
4899 DECLARE_INSTRUCTION(BooleanNot);
4900
4901 private:
4902 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4903};
4904
Vladimir Markofcb503c2016-05-18 12:48:17 +01004905class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004906 public:
4907 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004908 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004909 : HExpression(result_type,
4910 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4911 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004912 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004913 // Invariant: We should never generate a conversion to a Boolean value.
4914 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004915 }
4916
4917 HInstruction* GetInput() const { return InputAt(0); }
4918 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4919 Primitive::Type GetResultType() const { return GetType(); }
4920
4921 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004922 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4923 return true;
4924 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004925
Mark Mendelle82549b2015-05-06 10:55:34 -04004926 // Try to statically evaluate the conversion and return a HConstant
4927 // containing the result. If the input cannot be converted, return nullptr.
4928 HConstant* TryStaticEvaluation() const;
4929
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004930 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4931 Primitive::Type result_type) {
4932 // Some architectures may not require the 'GC' side effects, but at this point
4933 // in the compilation process we do not know what architecture we will
4934 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004935 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4936 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004937 return SideEffects::CanTriggerGC();
4938 }
4939 return SideEffects::None();
4940 }
4941
Roland Levillaindff1f282014-11-05 14:15:05 +00004942 DECLARE_INSTRUCTION(TypeConversion);
4943
4944 private:
4945 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4946};
4947
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004948static constexpr uint32_t kNoRegNumber = -1;
4949
Vladimir Markofcb503c2016-05-18 12:48:17 +01004950class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004951 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004952 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4953 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004954 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004955 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004956 SetRawInputAt(0, value);
4957 }
4958
Calin Juravle10e244f2015-01-26 18:54:32 +00004959 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004960 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004961 return true;
4962 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004963
Calin Juravle10e244f2015-01-26 18:54:32 +00004964 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004965
Calin Juravle10e244f2015-01-26 18:54:32 +00004966 bool CanThrow() const OVERRIDE { return true; }
4967
4968 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004969
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004970
4971 DECLARE_INSTRUCTION(NullCheck);
4972
4973 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004974 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4975};
4976
4977class FieldInfo : public ValueObject {
4978 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004979 FieldInfo(MemberOffset field_offset,
4980 Primitive::Type field_type,
4981 bool is_volatile,
4982 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004983 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004984 const DexFile& dex_file,
4985 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004986 : field_offset_(field_offset),
4987 field_type_(field_type),
4988 is_volatile_(is_volatile),
4989 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004990 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004991 dex_file_(dex_file),
4992 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004993
4994 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004995 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004996 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004997 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004998 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004999 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005000 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005001
5002 private:
5003 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005004 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005005 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005006 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005007 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005008 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005009 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005010};
5011
Vladimir Markofcb503c2016-05-18 12:48:17 +01005012class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005013 public:
5014 HInstanceFieldGet(HInstruction* value,
5015 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005016 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005017 bool is_volatile,
5018 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005019 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005020 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005021 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005022 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005023 : HExpression(field_type,
Serban Constantinescufca16662016-07-14 09:21:59 +01005024 SideEffectsForArchRuntimeCalls(field_type, is_volatile),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005025 dex_pc),
5026 field_info_(field_offset,
5027 field_type,
5028 is_volatile,
5029 field_idx,
5030 declaring_class_def_index,
5031 dex_file,
5032 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005033 SetRawInputAt(0, value);
5034 }
5035
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005036 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005037
Vladimir Marko372f10e2016-05-17 16:30:10 +01005038 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5039 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005040 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005041 }
5042
Calin Juravle641547a2015-04-21 22:08:51 +01005043 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005044 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005045 }
5046
5047 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005048 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5049 }
5050
Calin Juravle52c48962014-12-16 17:02:57 +00005051 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005052 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005053 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005054 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005055
Serban Constantinescufca16662016-07-14 09:21:59 +01005056 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type field_type, bool is_volatile) {
5057 SideEffects side_effects = SideEffects::FieldReadOfType(field_type, is_volatile);
5058
5059 // MIPS delegates volatile kPrimLong and kPrimDouble loads to a runtime helper.
5060 if (Primitive::Is64BitType(field_type)) {
5061 side_effects.Add(SideEffects::CanTriggerGC());
5062 }
5063 return side_effects;
5064 }
5065
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005066 DECLARE_INSTRUCTION(InstanceFieldGet);
5067
5068 private:
5069 const FieldInfo field_info_;
5070
5071 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5072};
5073
Vladimir Markofcb503c2016-05-18 12:48:17 +01005074class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005075 public:
5076 HInstanceFieldSet(HInstruction* object,
5077 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005078 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005079 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005080 bool is_volatile,
5081 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005082 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005083 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005084 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005085 uint32_t dex_pc)
Serban Constantinescufca16662016-07-14 09:21:59 +01005086 : HTemplateInstruction(SideEffectsForArchRuntimeCalls(field_type, is_volatile),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005087 dex_pc),
5088 field_info_(field_offset,
5089 field_type,
5090 is_volatile,
5091 field_idx,
5092 declaring_class_def_index,
5093 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005094 dex_cache) {
5095 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005096 SetRawInputAt(0, object);
5097 SetRawInputAt(1, value);
5098 }
5099
Calin Juravle641547a2015-04-21 22:08:51 +01005100 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005101 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005102 }
5103
Calin Juravle52c48962014-12-16 17:02:57 +00005104 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005105 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005106 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005107 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005108 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005109 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5110 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005111
Serban Constantinescufca16662016-07-14 09:21:59 +01005112 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type field_type, bool is_volatile) {
5113 SideEffects side_effects = SideEffects::FieldWriteOfType(field_type, is_volatile);
5114
5115 // MIPS delegates volatile kPrimLong and kPrimDouble stores to a runtime helper.
5116 if (Primitive::Is64BitType(field_type)) {
5117 side_effects.Add(SideEffects::CanTriggerGC());
5118 }
5119 return side_effects;
5120 }
5121
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005122 DECLARE_INSTRUCTION(InstanceFieldSet);
5123
5124 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005125 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5126 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5127 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5128 "Too many packed fields.");
5129
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005130 const FieldInfo field_info_;
5131
5132 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5133};
5134
Vladimir Markofcb503c2016-05-18 12:48:17 +01005135class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005136 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005137 HArrayGet(HInstruction* array,
5138 HInstruction* index,
5139 Primitive::Type type,
5140 uint32_t dex_pc,
5141 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005142 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005143 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005144 SetRawInputAt(0, array);
5145 SetRawInputAt(1, index);
5146 }
5147
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005148 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005149 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005150 return true;
5151 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005152 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005153 // TODO: We can be smarter here.
5154 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5155 // which generates the implicit null check. There are cases when these can be removed
5156 // to produce better code. If we ever add optimizations to do so we should allow an
5157 // implicit check here (as long as the address falls in the first page).
5158 return false;
5159 }
5160
David Brazdil4833f5a2015-12-16 10:37:39 +00005161 bool IsEquivalentOf(HArrayGet* other) const {
5162 bool result = (GetDexPc() == other->GetDexPc());
5163 if (kIsDebugBuild && result) {
5164 DCHECK_EQ(GetBlock(), other->GetBlock());
5165 DCHECK_EQ(GetArray(), other->GetArray());
5166 DCHECK_EQ(GetIndex(), other->GetIndex());
5167 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005168 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005169 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005170 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5171 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005172 }
5173 }
5174 return result;
5175 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005176
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005177 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5178
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005179 HInstruction* GetArray() const { return InputAt(0); }
5180 HInstruction* GetIndex() const { return InputAt(1); }
5181
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005182 DECLARE_INSTRUCTION(ArrayGet);
5183
5184 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005185 // We treat a String as an array, creating the HArrayGet from String.charAt()
5186 // intrinsic in the instruction simplifier. We can always determine whether
5187 // a particular HArrayGet is actually a String.charAt() by looking at the type
5188 // of the input but that requires holding the mutator lock, so we prefer to use
5189 // a flag, so that code generators don't need to do the locking.
5190 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5191 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5192 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5193 "Too many packed fields.");
5194
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005195 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5196};
5197
Vladimir Markofcb503c2016-05-18 12:48:17 +01005198class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005199 public:
5200 HArraySet(HInstruction* array,
5201 HInstruction* index,
5202 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005203 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005204 uint32_t dex_pc)
5205 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005206 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5207 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5208 SetPackedFlag<kFlagValueCanBeNull>(true);
5209 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005210 SetRawInputAt(0, array);
5211 SetRawInputAt(1, index);
5212 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005213 // Make a best guess now, may be refined during SSA building.
5214 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005215 }
5216
Calin Juravle77520bc2015-01-12 18:45:46 +00005217 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005218 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005219 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005220 }
5221
Mingyao Yang81014cb2015-06-02 03:16:27 -07005222 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005223 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005224
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005225 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005226 // TODO: Same as for ArrayGet.
5227 return false;
5228 }
5229
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005230 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005231 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005232 }
5233
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005234 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005235 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005236 }
5237
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005238 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005239 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005240 }
5241
Vladimir Markoa1de9182016-02-25 11:37:38 +00005242 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5243 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5244 bool StaticTypeOfArrayIsObjectArray() const {
5245 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5246 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005247
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005248 HInstruction* GetArray() const { return InputAt(0); }
5249 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005250 HInstruction* GetValue() const { return InputAt(2); }
5251
5252 Primitive::Type GetComponentType() const {
5253 // The Dex format does not type floating point index operations. Since the
5254 // `expected_component_type_` is set during building and can therefore not
5255 // be correct, we also check what is the value type. If it is a floating
5256 // point type, we must use that type.
5257 Primitive::Type value_type = GetValue()->GetType();
5258 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5259 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005260 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005261 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005262
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005263 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005264 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005265 }
5266
Aart Bik18b36ab2016-04-13 16:41:35 -07005267 void ComputeSideEffects() {
5268 Primitive::Type type = GetComponentType();
5269 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5270 SideEffectsForArchRuntimeCalls(type)));
5271 }
5272
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005273 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5274 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5275 }
5276
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005277 DECLARE_INSTRUCTION(ArraySet);
5278
5279 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005280 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5281 static constexpr size_t kFieldExpectedComponentTypeSize =
5282 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5283 static constexpr size_t kFlagNeedsTypeCheck =
5284 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5285 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005286 // Cached information for the reference_type_info_ so that codegen
5287 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005288 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5289 static constexpr size_t kNumberOfArraySetPackedBits =
5290 kFlagStaticTypeOfArrayIsObjectArray + 1;
5291 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5292 using ExpectedComponentTypeField =
5293 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005294
5295 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5296};
5297
Vladimir Markofcb503c2016-05-18 12:48:17 +01005298class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005299 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005300 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005301 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005302 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005303 // Note that arrays do not change length, so the instruction does not
5304 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005305 SetRawInputAt(0, array);
5306 }
5307
Calin Juravle77520bc2015-01-12 18:45:46 +00005308 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005309 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005310 return true;
5311 }
Calin Juravle641547a2015-04-21 22:08:51 +01005312 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5313 return obj == InputAt(0);
5314 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005315
Vladimir Markodce016e2016-04-28 13:10:02 +01005316 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5317
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005318 DECLARE_INSTRUCTION(ArrayLength);
5319
5320 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005321 // We treat a String as an array, creating the HArrayLength from String.length()
5322 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5323 // determine whether a particular HArrayLength is actually a String.length() by
5324 // looking at the type of the input but that requires holding the mutator lock, so
5325 // we prefer to use a flag, so that code generators don't need to do the locking.
5326 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5327 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5328 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5329 "Too many packed fields.");
5330
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005331 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5332};
5333
Vladimir Markofcb503c2016-05-18 12:48:17 +01005334class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005335 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005336 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5337 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005338 HBoundsCheck(HInstruction* index,
5339 HInstruction* length,
5340 uint32_t dex_pc,
5341 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5342 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5343 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005344 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005345 SetRawInputAt(0, index);
5346 SetRawInputAt(1, length);
5347 }
5348
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005349 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005350 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005351 return true;
5352 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005353
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005354 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005355
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005356 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005357
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005358 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5359 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5360
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005361 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005362
5363 DECLARE_INSTRUCTION(BoundsCheck);
5364
5365 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005366 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5367 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5368 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5369 // so we need to create an HEnvironment which will be translated to an InlineInfo
5370 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5371 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5372 // from the invoke as we don't want to look again at the dex bytecode.
5373 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5374
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005375 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5376};
5377
Vladimir Markofcb503c2016-05-18 12:48:17 +01005378class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005379 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005380 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005381 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005382
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005383 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005384 return true;
5385 }
5386
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005387 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5388 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005389
5390 DECLARE_INSTRUCTION(SuspendCheck);
5391
5392 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005393 // Only used for code generation, in order to share the same slow path between back edges
5394 // of a same loop.
5395 SlowPathCode* slow_path_;
5396
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005397 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5398};
5399
David Srbecky0cf44932015-12-09 14:09:59 +00005400// Pseudo-instruction which provides the native debugger with mapping information.
5401// It ensures that we can generate line number and local variables at this point.
5402class HNativeDebugInfo : public HTemplateInstruction<0> {
5403 public:
5404 explicit HNativeDebugInfo(uint32_t dex_pc)
5405 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5406
5407 bool NeedsEnvironment() const OVERRIDE {
5408 return true;
5409 }
5410
5411 DECLARE_INSTRUCTION(NativeDebugInfo);
5412
5413 private:
5414 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5415};
5416
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005417/**
5418 * Instruction to load a Class object.
5419 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005420class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005421 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005422 // Determines how to load the Class.
5423 enum class LoadKind {
5424 // Use the Class* from the method's own ArtMethod*.
5425 kReferrersClass,
5426
5427 // Use boot image Class* address that will be known at link time.
5428 // Used for boot image classes referenced by boot image code in non-PIC mode.
5429 kBootImageLinkTimeAddress,
5430
5431 // Use PC-relative boot image Class* address that will be known at link time.
5432 // Used for boot image classes referenced by boot image code in PIC mode.
5433 kBootImageLinkTimePcRelative,
5434
5435 // Use a known boot image Class* address, embedded in the code by the codegen.
5436 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5437 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5438 // GetIncludePatchInformation().
5439 kBootImageAddress,
5440
5441 // Load from the resolved types array at an absolute address.
5442 // Used for classes outside the boot image referenced by JIT-compiled code.
5443 kDexCacheAddress,
5444
5445 // Load from resolved types array in the dex cache using a PC-relative load.
5446 // Used for classes outside boot image when we know that we can access
5447 // the dex cache arrays using a PC-relative load.
5448 kDexCachePcRelative,
5449
5450 // Load from resolved types array accessed through the class loaded from
5451 // the compiled method's own ArtMethod*. This is the default access type when
5452 // all other types are unavailable.
5453 kDexCacheViaMethod,
5454
5455 kLast = kDexCacheViaMethod
5456 };
5457
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005458 HLoadClass(HCurrentMethod* current_method,
5459 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005460 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005461 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005462 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005463 bool needs_access_check,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005464 bool is_in_dex_cache,
5465 bool is_in_boot_image)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005466 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5467 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005468 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005469 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005470 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005471 // Referrers class should not need access check. We never inline unverified
5472 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005473 DCHECK(!is_referrers_class || !needs_access_check);
5474
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005475 SetPackedField<LoadKindField>(
5476 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005477 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5478 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005479 SetPackedFlag<kFlagIsInBootImage>(is_in_boot_image);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005480 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005481 }
5482
5483 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5484 DCHECK(HasAddress(load_kind));
5485 load_data_.address = address;
5486 SetLoadKindInternal(load_kind);
5487 }
5488
5489 void SetLoadKindWithTypeReference(LoadKind load_kind,
5490 const DexFile& dex_file,
5491 uint32_t type_index) {
5492 DCHECK(HasTypeReference(load_kind));
5493 DCHECK(IsSameDexFile(dex_file_, dex_file));
5494 DCHECK_EQ(type_index_, type_index);
5495 SetLoadKindInternal(load_kind);
5496 }
5497
5498 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5499 const DexFile& dex_file,
5500 uint32_t element_index) {
5501 DCHECK(HasDexCacheReference(load_kind));
5502 DCHECK(IsSameDexFile(dex_file_, dex_file));
5503 load_data_.dex_cache_element_index = element_index;
5504 SetLoadKindInternal(load_kind);
5505 }
5506
5507 LoadKind GetLoadKind() const {
5508 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005509 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005510
5511 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005512
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005513 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005514
5515 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5516
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005517 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005518
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005519 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005520 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005521 }
5522
Calin Juravle0ba218d2015-05-19 18:46:01 +01005523 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005524 // The entrypoint the code generator is going to call does not do
5525 // clinit of the class.
5526 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005527 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005528 }
5529
5530 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005531 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005532 (!IsReferrersClass() && !IsInDexCache()) ||
5533 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005534 }
5535
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005536
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005537 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005538 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005539 }
5540
Calin Juravleacf735c2015-02-12 15:25:22 +00005541 ReferenceTypeInfo GetLoadedClassRTI() {
5542 return loaded_class_rti_;
5543 }
5544
5545 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5546 // Make sure we only set exact types (the loaded class should never be merged).
5547 DCHECK(rti.IsExact());
5548 loaded_class_rti_ = rti;
5549 }
5550
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005551 uint32_t GetTypeIndex() const { return type_index_; }
5552 const DexFile& GetDexFile() const { return dex_file_; }
5553
5554 uint32_t GetDexCacheElementOffset() const;
5555
5556 uint64_t GetAddress() const {
5557 DCHECK(HasAddress(GetLoadKind()));
5558 return load_data_.address;
5559 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005560
Vladimir Markoa1de9182016-02-25 11:37:38 +00005561 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005562
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005563 static SideEffects SideEffectsForArchRuntimeCalls() {
5564 return SideEffects::CanTriggerGC();
5565 }
5566
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005567 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005568 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5569 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005570 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005571 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005572
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005573 void MarkInDexCache() {
5574 SetPackedFlag<kFlagIsInDexCache>(true);
5575 DCHECK(!NeedsEnvironment());
5576 RemoveEnvironment();
5577 SetSideEffects(SideEffects::None());
5578 }
5579
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005580 void MarkInBootImage() {
5581 SetPackedFlag<kFlagIsInBootImage>(true);
5582 }
5583
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005584 void AddSpecialInput(HInstruction* special_input);
5585
5586 using HInstruction::GetInputRecords; // Keep the const version visible.
5587 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5588 return ArrayRef<HUserRecord<HInstruction*>>(
5589 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5590 }
5591
5592 Primitive::Type GetType() const OVERRIDE {
5593 return Primitive::kPrimNot;
5594 }
5595
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005596 DECLARE_INSTRUCTION(LoadClass);
5597
5598 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005599 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005600 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005601 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005602 // Whether this instruction must generate the initialization check.
5603 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005604 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005605 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5606 static constexpr size_t kFieldLoadKindSize =
5607 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5608 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005609 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005610 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5611
5612 static bool HasTypeReference(LoadKind load_kind) {
5613 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5614 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5615 load_kind == LoadKind::kDexCacheViaMethod ||
5616 load_kind == LoadKind::kReferrersClass;
5617 }
5618
5619 static bool HasAddress(LoadKind load_kind) {
5620 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5621 }
5622
5623 static bool HasDexCacheReference(LoadKind load_kind) {
5624 return load_kind == LoadKind::kDexCachePcRelative;
5625 }
5626
5627 void SetLoadKindInternal(LoadKind load_kind);
5628
5629 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5630 // For other load kinds it's empty or possibly some architecture-specific instruction
5631 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5632 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005633
5634 const uint16_t type_index_;
5635 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005636
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005637 union {
5638 uint32_t dex_cache_element_index; // Only for dex cache reference.
5639 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5640 } load_data_;
5641
Calin Juravleacf735c2015-02-12 15:25:22 +00005642 ReferenceTypeInfo loaded_class_rti_;
5643
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005644 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5645};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005646std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5647
5648// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5649inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5650 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5651 return load_data_.dex_cache_element_index;
5652}
5653
5654// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5655inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005656 // The special input is used for PC-relative loads on some architectures,
5657 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005658 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005659 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5660 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5661 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005662 DCHECK(special_input_.GetInstruction() == nullptr);
5663 special_input_ = HUserRecord<HInstruction*>(special_input);
5664 special_input->AddUseAt(this, 0);
5665}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005666
Vladimir Marko372f10e2016-05-17 16:30:10 +01005667class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005668 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005669 // Determines how to load the String.
5670 enum class LoadKind {
5671 // Use boot image String* address that will be known at link time.
5672 // Used for boot image strings referenced by boot image code in non-PIC mode.
5673 kBootImageLinkTimeAddress,
5674
5675 // Use PC-relative boot image String* address that will be known at link time.
5676 // Used for boot image strings referenced by boot image code in PIC mode.
5677 kBootImageLinkTimePcRelative,
5678
5679 // Use a known boot image String* address, embedded in the code by the codegen.
5680 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5681 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5682 // GetIncludePatchInformation().
5683 kBootImageAddress,
5684
5685 // Load from the resolved strings array at an absolute address.
5686 // Used for strings outside the boot image referenced by JIT-compiled code.
5687 kDexCacheAddress,
5688
5689 // Load from resolved strings array in the dex cache using a PC-relative load.
5690 // Used for strings outside boot image when we know that we can access
5691 // the dex cache arrays using a PC-relative load.
5692 kDexCachePcRelative,
5693
5694 // Load from resolved strings array accessed through the class loaded from
5695 // the compiled method's own ArtMethod*. This is the default access type when
5696 // all other types are unavailable.
5697 kDexCacheViaMethod,
5698
5699 kLast = kDexCacheViaMethod
5700 };
5701
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005702 HLoadString(HCurrentMethod* current_method,
5703 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005704 const DexFile& dex_file,
5705 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005706 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5707 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005708 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005709 SetPackedFlag<kFlagIsInDexCache>(false);
5710 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5711 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005712 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005713
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005714 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5715 DCHECK(HasAddress(load_kind));
5716 load_data_.address = address;
5717 SetLoadKindInternal(load_kind);
5718 }
5719
5720 void SetLoadKindWithStringReference(LoadKind load_kind,
5721 const DexFile& dex_file,
5722 uint32_t string_index) {
5723 DCHECK(HasStringReference(load_kind));
5724 load_data_.ref.dex_file = &dex_file;
5725 string_index_ = string_index;
5726 SetLoadKindInternal(load_kind);
5727 }
5728
5729 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5730 const DexFile& dex_file,
5731 uint32_t element_index) {
5732 DCHECK(HasDexCacheReference(load_kind));
5733 load_data_.ref.dex_file = &dex_file;
5734 load_data_.ref.dex_cache_element_index = element_index;
5735 SetLoadKindInternal(load_kind);
5736 }
5737
5738 LoadKind GetLoadKind() const {
5739 return GetPackedField<LoadKindField>();
5740 }
5741
5742 const DexFile& GetDexFile() const;
5743
5744 uint32_t GetStringIndex() const {
5745 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5746 return string_index_;
5747 }
5748
5749 uint32_t GetDexCacheElementOffset() const;
5750
5751 uint64_t GetAddress() const {
5752 DCHECK(HasAddress(GetLoadKind()));
5753 return load_data_.address;
5754 }
5755
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005756 bool CanBeMoved() const OVERRIDE { return true; }
5757
Vladimir Marko372f10e2016-05-17 16:30:10 +01005758 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005759
5760 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5761
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005762 // Will call the runtime if we need to load the string through
5763 // the dex cache and the string is not guaranteed to be there yet.
5764 bool NeedsEnvironment() const OVERRIDE {
5765 LoadKind load_kind = GetLoadKind();
5766 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5767 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5768 load_kind == LoadKind::kBootImageAddress) {
5769 return false;
5770 }
5771 return !IsInDexCache();
5772 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005773
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005774 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5775 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5776 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005777
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005778 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005779 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005780
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005781 static SideEffects SideEffectsForArchRuntimeCalls() {
5782 return SideEffects::CanTriggerGC();
5783 }
5784
Vladimir Markoa1de9182016-02-25 11:37:38 +00005785 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5786
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005787 void MarkInDexCache() {
5788 SetPackedFlag<kFlagIsInDexCache>(true);
5789 DCHECK(!NeedsEnvironment());
5790 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005791 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005792 }
5793
Vladimir Marko372f10e2016-05-17 16:30:10 +01005794 void AddSpecialInput(HInstruction* special_input);
5795
5796 using HInstruction::GetInputRecords; // Keep the const version visible.
5797 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5798 return ArrayRef<HUserRecord<HInstruction*>>(
5799 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005800 }
5801
Vladimir Marko372f10e2016-05-17 16:30:10 +01005802 Primitive::Type GetType() const OVERRIDE {
5803 return Primitive::kPrimNot;
5804 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005805
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005806 DECLARE_INSTRUCTION(LoadString);
5807
5808 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005809 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005810 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5811 static constexpr size_t kFieldLoadKindSize =
5812 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5813 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005814 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005815 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005816
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005817 static bool HasStringReference(LoadKind load_kind) {
5818 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5819 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5820 load_kind == LoadKind::kDexCacheViaMethod;
5821 }
5822
5823 static bool HasAddress(LoadKind load_kind) {
5824 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5825 }
5826
5827 static bool HasDexCacheReference(LoadKind load_kind) {
5828 return load_kind == LoadKind::kDexCachePcRelative;
5829 }
5830
5831 void SetLoadKindInternal(LoadKind load_kind);
5832
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005833 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5834 // For other load kinds it's empty or possibly some architecture-specific instruction
5835 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005836 HUserRecord<HInstruction*> special_input_;
5837
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005838 // String index serves also as the hash code and it's also needed for slow-paths,
5839 // so it must not be overwritten with other load data.
5840 uint32_t string_index_;
5841
5842 union {
5843 struct {
5844 const DexFile* dex_file; // For string reference and dex cache reference.
5845 uint32_t dex_cache_element_index; // Only for dex cache reference.
5846 } ref;
5847 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5848 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005849
5850 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5851};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005852std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5853
5854// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5855inline const DexFile& HLoadString::GetDexFile() const {
5856 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5857 << GetLoadKind();
5858 return *load_data_.ref.dex_file;
5859}
5860
5861// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5862inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5863 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5864 return load_data_.ref.dex_cache_element_index;
5865}
5866
5867// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5868inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005869 // The special input is used for PC-relative loads on some architectures,
5870 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005871 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005872 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5873 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5874 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005875 // HLoadString::GetInputRecords() returns an empty array at this point,
5876 // so use the GetInputRecords() from the base class to set the input record.
5877 DCHECK(special_input_.GetInstruction() == nullptr);
5878 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005879 special_input->AddUseAt(this, 0);
5880}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005881
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005882/**
5883 * Performs an initialization check on its Class object input.
5884 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005885class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005886 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005887 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005888 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005889 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005890 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5891 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005892 SetRawInputAt(0, constant);
5893 }
5894
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005895 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005896 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005897 return true;
5898 }
5899
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005900 bool NeedsEnvironment() const OVERRIDE {
5901 // May call runtime to initialize the class.
5902 return true;
5903 }
5904
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005905 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005906
5907 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5908
5909 DECLARE_INSTRUCTION(ClinitCheck);
5910
5911 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005912 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5913};
5914
Vladimir Markofcb503c2016-05-18 12:48:17 +01005915class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005916 public:
5917 HStaticFieldGet(HInstruction* cls,
5918 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005919 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005920 bool is_volatile,
5921 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005922 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005923 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005924 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005925 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005926 : HExpression(field_type,
Serban Constantinescufca16662016-07-14 09:21:59 +01005927 SideEffectsForArchRuntimeCalls(field_type, is_volatile),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005928 dex_pc),
5929 field_info_(field_offset,
5930 field_type,
5931 is_volatile,
5932 field_idx,
5933 declaring_class_def_index,
5934 dex_file,
5935 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005936 SetRawInputAt(0, cls);
5937 }
5938
Calin Juravle52c48962014-12-16 17:02:57 +00005939
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005940 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005941
Vladimir Marko372f10e2016-05-17 16:30:10 +01005942 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5943 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005944 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005945 }
5946
5947 size_t ComputeHashCode() const OVERRIDE {
5948 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
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
Serban Constantinescufca16662016-07-14 09:21:59 +01005956 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type field_type, bool is_volatile) {
5957 SideEffects side_effects = SideEffects::FieldReadOfType(field_type, is_volatile);
5958
5959 // MIPS delegates volatile kPrimLong and kPrimDouble loads to a runtime helper.
5960 if (Primitive::Is64BitType(field_type)) {
5961 side_effects.Add(SideEffects::CanTriggerGC());
5962 }
5963 return side_effects;
5964 }
5965
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005966 DECLARE_INSTRUCTION(StaticFieldGet);
5967
5968 private:
5969 const FieldInfo field_info_;
5970
5971 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5972};
5973
Vladimir Markofcb503c2016-05-18 12:48:17 +01005974class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005975 public:
5976 HStaticFieldSet(HInstruction* cls,
5977 HInstruction* value,
5978 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005979 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005980 bool is_volatile,
5981 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005982 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005983 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005984 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005985 uint32_t dex_pc)
Serban Constantinescufca16662016-07-14 09:21:59 +01005986 : HTemplateInstruction(SideEffectsForArchRuntimeCalls(field_type, is_volatile),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005987 dex_pc),
5988 field_info_(field_offset,
5989 field_type,
5990 is_volatile,
5991 field_idx,
5992 declaring_class_def_index,
5993 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005994 dex_cache) {
5995 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005996 SetRawInputAt(0, cls);
5997 SetRawInputAt(1, value);
5998 }
5999
Calin Juravle52c48962014-12-16 17:02:57 +00006000 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006001 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6002 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006003 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006004
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006005 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006006 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6007 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006008
Serban Constantinescufca16662016-07-14 09:21:59 +01006009 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type field_type, bool is_volatile) {
6010 SideEffects side_effects = SideEffects::FieldWriteOfType(field_type, is_volatile);
6011
6012 // MIPS delegates volatile kPrimLong and kPrimDouble stores to a runtime helper.
6013 if (Primitive::Is64BitType(field_type)) {
6014 side_effects.Add(SideEffects::CanTriggerGC());
6015 }
6016 return side_effects;
6017 }
6018
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006019 DECLARE_INSTRUCTION(StaticFieldSet);
6020
6021 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006022 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6023 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6024 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6025 "Too many packed fields.");
6026
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006027 const FieldInfo field_info_;
6028
6029 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6030};
6031
Vladimir Markofcb503c2016-05-18 12:48:17 +01006032class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006033 public:
6034 HUnresolvedInstanceFieldGet(HInstruction* obj,
6035 Primitive::Type field_type,
6036 uint32_t field_index,
6037 uint32_t dex_pc)
6038 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6039 field_index_(field_index) {
6040 SetRawInputAt(0, obj);
6041 }
6042
6043 bool NeedsEnvironment() const OVERRIDE { return true; }
6044 bool CanThrow() const OVERRIDE { return true; }
6045
6046 Primitive::Type GetFieldType() const { return GetType(); }
6047 uint32_t GetFieldIndex() const { return field_index_; }
6048
6049 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6050
6051 private:
6052 const uint32_t field_index_;
6053
6054 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6055};
6056
Vladimir Markofcb503c2016-05-18 12:48:17 +01006057class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006058 public:
6059 HUnresolvedInstanceFieldSet(HInstruction* obj,
6060 HInstruction* value,
6061 Primitive::Type field_type,
6062 uint32_t field_index,
6063 uint32_t dex_pc)
6064 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006065 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006066 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006067 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006068 SetRawInputAt(0, obj);
6069 SetRawInputAt(1, 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(UnresolvedInstanceFieldSet);
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(HUnresolvedInstanceFieldSet);
6093};
6094
Vladimir Markofcb503c2016-05-18 12:48:17 +01006095class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006096 public:
6097 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6098 uint32_t field_index,
6099 uint32_t dex_pc)
6100 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6101 field_index_(field_index) {
6102 }
6103
6104 bool NeedsEnvironment() const OVERRIDE { return true; }
6105 bool CanThrow() const OVERRIDE { return true; }
6106
6107 Primitive::Type GetFieldType() const { return GetType(); }
6108 uint32_t GetFieldIndex() const { return field_index_; }
6109
6110 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6111
6112 private:
6113 const uint32_t field_index_;
6114
6115 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6116};
6117
Vladimir Markofcb503c2016-05-18 12:48:17 +01006118class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006119 public:
6120 HUnresolvedStaticFieldSet(HInstruction* value,
6121 Primitive::Type field_type,
6122 uint32_t field_index,
6123 uint32_t dex_pc)
6124 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006125 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006126 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006127 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006128 SetRawInputAt(0, value);
6129 }
6130
6131 bool NeedsEnvironment() const OVERRIDE { return true; }
6132 bool CanThrow() const OVERRIDE { return true; }
6133
Vladimir Markoa1de9182016-02-25 11:37:38 +00006134 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006135 uint32_t GetFieldIndex() const { return field_index_; }
6136
6137 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6138
6139 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006140 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6141 static constexpr size_t kFieldFieldTypeSize =
6142 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6143 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6144 kFieldFieldType + kFieldFieldTypeSize;
6145 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6146 "Too many packed fields.");
6147 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6148
Calin Juravlee460d1d2015-09-29 04:52:17 +01006149 const uint32_t field_index_;
6150
6151 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6152};
6153
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006154// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006155class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006156 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006157 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6158 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006159
David Brazdilbbd733e2015-08-18 17:48:17 +01006160 bool CanBeNull() const OVERRIDE { return false; }
6161
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006162 DECLARE_INSTRUCTION(LoadException);
6163
6164 private:
6165 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6166};
6167
David Brazdilcb1c0552015-08-04 16:22:25 +01006168// Implicit part of move-exception which clears thread-local exception storage.
6169// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006170class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006171 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006172 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6173 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006174
6175 DECLARE_INSTRUCTION(ClearException);
6176
6177 private:
6178 DISALLOW_COPY_AND_ASSIGN(HClearException);
6179};
6180
Vladimir Markofcb503c2016-05-18 12:48:17 +01006181class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006182 public:
6183 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006184 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006185 SetRawInputAt(0, exception);
6186 }
6187
6188 bool IsControlFlow() const OVERRIDE { return true; }
6189
6190 bool NeedsEnvironment() const OVERRIDE { return true; }
6191
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006192 bool CanThrow() const OVERRIDE { return true; }
6193
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006194
6195 DECLARE_INSTRUCTION(Throw);
6196
6197 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006198 DISALLOW_COPY_AND_ASSIGN(HThrow);
6199};
6200
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006201/**
6202 * Implementation strategies for the code generator of a HInstanceOf
6203 * or `HCheckCast`.
6204 */
6205enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006206 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006207 kExactCheck, // Can do a single class compare.
6208 kClassHierarchyCheck, // Can just walk the super class chain.
6209 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6210 kInterfaceCheck, // No optimization yet when checking against an interface.
6211 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006212 kArrayCheck, // No optimization yet when checking against a generic array.
6213 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006214};
6215
Roland Levillain86503782016-02-11 19:07:30 +00006216std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6217
Vladimir Markofcb503c2016-05-18 12:48:17 +01006218class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006219 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006220 HInstanceOf(HInstruction* object,
6221 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006222 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006223 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006224 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006225 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006226 dex_pc) {
6227 SetPackedField<TypeCheckKindField>(check_kind);
6228 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006229 SetRawInputAt(0, object);
6230 SetRawInputAt(1, constant);
6231 }
6232
6233 bool CanBeMoved() const OVERRIDE { return true; }
6234
Vladimir Marko372f10e2016-05-17 16:30:10 +01006235 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006236 return true;
6237 }
6238
6239 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006240 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006241 }
6242
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006243 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006244 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6245 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6246 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6247 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006248
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006249 static bool CanCallRuntime(TypeCheckKind check_kind) {
6250 // Mips currently does runtime calls for any other checks.
6251 return check_kind != TypeCheckKind::kExactCheck;
6252 }
6253
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006254 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006255 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006256 }
6257
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006258 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006259
6260 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006261 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6262 static constexpr size_t kFieldTypeCheckKindSize =
6263 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6264 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6265 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6266 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6267 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006268
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006269 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6270};
6271
Vladimir Markofcb503c2016-05-18 12:48:17 +01006272class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006273 public:
David Brazdilf5552582015-12-27 13:36:12 +00006274 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006275 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006276 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6277 SetPackedFlag<kFlagUpperCanBeNull>(true);
6278 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006279 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006280 SetRawInputAt(0, input);
6281 }
6282
David Brazdilf5552582015-12-27 13:36:12 +00006283 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006284 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006285 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006286 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006287
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006288 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006289 DCHECK(GetUpperCanBeNull() || !can_be_null);
6290 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006291 }
6292
Vladimir Markoa1de9182016-02-25 11:37:38 +00006293 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006294
Calin Juravleb1498f62015-02-16 13:13:29 +00006295 DECLARE_INSTRUCTION(BoundType);
6296
6297 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006298 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6299 // is false then CanBeNull() cannot be true).
6300 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6301 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6302 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6303 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6304
Calin Juravleb1498f62015-02-16 13:13:29 +00006305 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006306 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6307 // It is used to bound the type in cases like:
6308 // if (x instanceof ClassX) {
6309 // // uper_bound_ will be ClassX
6310 // }
David Brazdilf5552582015-12-27 13:36:12 +00006311 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006312
6313 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6314};
6315
Vladimir Markofcb503c2016-05-18 12:48:17 +01006316class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006317 public:
6318 HCheckCast(HInstruction* object,
6319 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006320 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006321 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006322 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6323 SetPackedField<TypeCheckKindField>(check_kind);
6324 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006325 SetRawInputAt(0, object);
6326 SetRawInputAt(1, constant);
6327 }
6328
6329 bool CanBeMoved() const OVERRIDE { return true; }
6330
Vladimir Marko372f10e2016-05-17 16:30:10 +01006331 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006332 return true;
6333 }
6334
6335 bool NeedsEnvironment() const OVERRIDE {
6336 // Instruction may throw a CheckCastError.
6337 return true;
6338 }
6339
6340 bool CanThrow() const OVERRIDE { return true; }
6341
Vladimir Markoa1de9182016-02-25 11:37:38 +00006342 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6343 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6344 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6345 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006346
6347 DECLARE_INSTRUCTION(CheckCast);
6348
6349 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006350 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6351 static constexpr size_t kFieldTypeCheckKindSize =
6352 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6353 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6354 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6355 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6356 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006357
6358 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006359};
6360
Andreas Gampe26de38b2016-07-27 17:53:11 -07006361/**
6362 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6363 * @details We define the combined barrier types that are actually required
6364 * by the Java Memory Model, rather than using exactly the terminology from
6365 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6366 * primitives. Note that the JSR-133 cookbook generally does not deal with
6367 * store atomicity issues, and the recipes there are not always entirely sufficient.
6368 * The current recipe is as follows:
6369 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6370 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6371 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6372 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6373 * class has final fields.
6374 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6375 * store-to-memory instructions. Only generated together with non-temporal stores.
6376 */
6377enum MemBarrierKind {
6378 kAnyStore,
6379 kLoadAny,
6380 kStoreStore,
6381 kAnyAny,
6382 kNTStoreStore,
6383 kLastBarrierKind = kNTStoreStore
6384};
6385std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6386
Vladimir Markofcb503c2016-05-18 12:48:17 +01006387class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006388 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006389 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006390 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006391 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6392 SetPackedField<BarrierKindField>(barrier_kind);
6393 }
Calin Juravle27df7582015-04-17 19:12:31 +01006394
Vladimir Markoa1de9182016-02-25 11:37:38 +00006395 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006396
6397 DECLARE_INSTRUCTION(MemoryBarrier);
6398
6399 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006400 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6401 static constexpr size_t kFieldBarrierKindSize =
6402 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6403 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6404 kFieldBarrierKind + kFieldBarrierKindSize;
6405 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6406 "Too many packed fields.");
6407 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006408
6409 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6410};
6411
Vladimir Markofcb503c2016-05-18 12:48:17 +01006412class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006413 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006414 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006415 kEnter,
6416 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006417 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006418 };
6419
6420 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006421 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006422 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6423 dex_pc) {
6424 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006425 SetRawInputAt(0, object);
6426 }
6427
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006428 // Instruction may go into runtime, so we need an environment.
6429 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006430
6431 bool CanThrow() const OVERRIDE {
6432 // Verifier guarantees that monitor-exit cannot throw.
6433 // This is important because it allows the HGraphBuilder to remove
6434 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6435 return IsEnter();
6436 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006437
Vladimir Markoa1de9182016-02-25 11:37:38 +00006438 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6439 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006440
6441 DECLARE_INSTRUCTION(MonitorOperation);
6442
Calin Juravle52c48962014-12-16 17:02:57 +00006443 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006444 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6445 static constexpr size_t kFieldOperationKindSize =
6446 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6447 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6448 kFieldOperationKind + kFieldOperationKindSize;
6449 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6450 "Too many packed fields.");
6451 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006452
6453 private:
6454 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6455};
6456
Vladimir Markofcb503c2016-05-18 12:48:17 +01006457class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006458 public:
6459 HSelect(HInstruction* condition,
6460 HInstruction* true_value,
6461 HInstruction* false_value,
6462 uint32_t dex_pc)
6463 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6464 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6465
6466 // First input must be `true_value` or `false_value` to allow codegens to
6467 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6468 // that architectures which implement HSelect as a conditional move also
6469 // will not need to invert the condition.
6470 SetRawInputAt(0, false_value);
6471 SetRawInputAt(1, true_value);
6472 SetRawInputAt(2, condition);
6473 }
6474
6475 HInstruction* GetFalseValue() const { return InputAt(0); }
6476 HInstruction* GetTrueValue() const { return InputAt(1); }
6477 HInstruction* GetCondition() const { return InputAt(2); }
6478
6479 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006480 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6481 return true;
6482 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006483
6484 bool CanBeNull() const OVERRIDE {
6485 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6486 }
6487
6488 DECLARE_INSTRUCTION(Select);
6489
6490 private:
6491 DISALLOW_COPY_AND_ASSIGN(HSelect);
6492};
6493
Vladimir Markof9f64412015-09-02 14:05:49 +01006494class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006495 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006496 MoveOperands(Location source,
6497 Location destination,
6498 Primitive::Type type,
6499 HInstruction* instruction)
6500 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006501
6502 Location GetSource() const { return source_; }
6503 Location GetDestination() const { return destination_; }
6504
6505 void SetSource(Location value) { source_ = value; }
6506 void SetDestination(Location value) { destination_ = value; }
6507
6508 // The parallel move resolver marks moves as "in-progress" by clearing the
6509 // destination (but not the source).
6510 Location MarkPending() {
6511 DCHECK(!IsPending());
6512 Location dest = destination_;
6513 destination_ = Location::NoLocation();
6514 return dest;
6515 }
6516
6517 void ClearPending(Location dest) {
6518 DCHECK(IsPending());
6519 destination_ = dest;
6520 }
6521
6522 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006523 DCHECK(source_.IsValid() || destination_.IsInvalid());
6524 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006525 }
6526
6527 // True if this blocks a move from the given location.
6528 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006529 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006530 }
6531
6532 // A move is redundant if it's been eliminated, if its source and
6533 // destination are the same, or if its destination is unneeded.
6534 bool IsRedundant() const {
6535 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6536 }
6537
6538 // We clear both operands to indicate move that's been eliminated.
6539 void Eliminate() {
6540 source_ = destination_ = Location::NoLocation();
6541 }
6542
6543 bool IsEliminated() const {
6544 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6545 return source_.IsInvalid();
6546 }
6547
Alexey Frunze4dda3372015-06-01 18:31:49 -07006548 Primitive::Type GetType() const { return type_; }
6549
Nicolas Geoffray90218252015-04-15 11:56:51 +01006550 bool Is64BitMove() const {
6551 return Primitive::Is64BitType(type_);
6552 }
6553
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006554 HInstruction* GetInstruction() const { return instruction_; }
6555
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006556 private:
6557 Location source_;
6558 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006559 // The type this move is for.
6560 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006561 // The instruction this move is assocatied with. Null when this move is
6562 // for moving an input in the expected locations of user (including a phi user).
6563 // This is only used in debug mode, to ensure we do not connect interval siblings
6564 // in the same parallel move.
6565 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006566};
6567
Roland Levillainc9285912015-12-18 10:38:42 +00006568std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6569
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006570static constexpr size_t kDefaultNumberOfMoves = 4;
6571
Vladimir Markofcb503c2016-05-18 12:48:17 +01006572class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006573 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006574 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006575 : HTemplateInstruction(SideEffects::None(), dex_pc),
6576 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6577 moves_.reserve(kDefaultNumberOfMoves);
6578 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006579
Nicolas Geoffray90218252015-04-15 11:56:51 +01006580 void AddMove(Location source,
6581 Location destination,
6582 Primitive::Type type,
6583 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006584 DCHECK(source.IsValid());
6585 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006586 if (kIsDebugBuild) {
6587 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006588 for (const MoveOperands& move : moves_) {
6589 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006590 // Special case the situation where the move is for the spill slot
6591 // of the instruction.
6592 if ((GetPrevious() == instruction)
6593 || ((GetPrevious() == nullptr)
6594 && instruction->IsPhi()
6595 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006596 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006597 << "Doing parallel moves for the same instruction.";
6598 } else {
6599 DCHECK(false) << "Doing parallel moves for the same instruction.";
6600 }
6601 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006602 }
6603 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006604 for (const MoveOperands& move : moves_) {
6605 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006606 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006607 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006608 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006609 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006610 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006611 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006612 }
6613
Vladimir Marko225b6462015-09-28 12:17:40 +01006614 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006615 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006616 }
6617
Vladimir Marko225b6462015-09-28 12:17:40 +01006618 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006619
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006620 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006621
6622 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006623 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006624
6625 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6626};
6627
Mark Mendell0616ae02015-04-17 12:49:27 -04006628} // namespace art
6629
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006630#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6631#include "nodes_shared.h"
6632#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006633#ifdef ART_ENABLE_CODEGEN_arm
6634#include "nodes_arm.h"
6635#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006636#ifdef ART_ENABLE_CODEGEN_arm64
6637#include "nodes_arm64.h"
6638#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006639#ifdef ART_ENABLE_CODEGEN_mips
6640#include "nodes_mips.h"
6641#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006642#ifdef ART_ENABLE_CODEGEN_x86
6643#include "nodes_x86.h"
6644#endif
6645
6646namespace art {
6647
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006648class HGraphVisitor : public ValueObject {
6649 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006650 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6651 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006652
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006653 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006654 virtual void VisitBasicBlock(HBasicBlock* block);
6655
Roland Levillain633021e2014-10-01 14:12:25 +01006656 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006657 void VisitInsertionOrder();
6658
Roland Levillain633021e2014-10-01 14:12:25 +01006659 // Visit the graph following dominator tree reverse post-order.
6660 void VisitReversePostOrder();
6661
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006662 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006663
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006664 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006665#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006666 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6667
6668 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6669
6670#undef DECLARE_VISIT_INSTRUCTION
6671
6672 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006673 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006674
6675 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6676};
6677
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006678class HGraphDelegateVisitor : public HGraphVisitor {
6679 public:
6680 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6681 virtual ~HGraphDelegateVisitor() {}
6682
6683 // Visit functions that delegate to to super class.
6684#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006685 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006686
6687 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6688
6689#undef DECLARE_VISIT_INSTRUCTION
6690
6691 private:
6692 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6693};
6694
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006695class HInsertionOrderIterator : public ValueObject {
6696 public:
6697 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6698
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006699 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006700 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006701 void Advance() { ++index_; }
6702
6703 private:
6704 const HGraph& graph_;
6705 size_t index_;
6706
6707 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6708};
6709
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006710class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006711 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006712 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6713 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006714 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006715 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006716
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006717 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6718 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006719 void Advance() { ++index_; }
6720
6721 private:
6722 const HGraph& graph_;
6723 size_t index_;
6724
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006725 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006726};
6727
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006728class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006729 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006730 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006731 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006732 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006733 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006734 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006735
6736 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006737 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006738 void Advance() { --index_; }
6739
6740 private:
6741 const HGraph& graph_;
6742 size_t index_;
6743
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006744 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006745};
6746
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006747class HLinearPostOrderIterator : public ValueObject {
6748 public:
6749 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006750 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006751
6752 bool Done() const { return index_ == 0; }
6753
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006754 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006755
6756 void Advance() {
6757 --index_;
6758 DCHECK_GE(index_, 0U);
6759 }
6760
6761 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006762 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006763 size_t index_;
6764
6765 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6766};
6767
6768class HLinearOrderIterator : public ValueObject {
6769 public:
6770 explicit HLinearOrderIterator(const HGraph& graph)
6771 : order_(graph.GetLinearOrder()), index_(0) {}
6772
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006773 bool Done() const { return index_ == order_.size(); }
6774 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006775 void Advance() { ++index_; }
6776
6777 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006778 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006779 size_t index_;
6780
6781 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6782};
6783
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006784// Iterator over the blocks that art part of the loop. Includes blocks part
6785// of an inner loop. The order in which the blocks are iterated is on their
6786// block id.
6787class HBlocksInLoopIterator : public ValueObject {
6788 public:
6789 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6790 : blocks_in_loop_(info.GetBlocks()),
6791 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6792 index_(0) {
6793 if (!blocks_in_loop_.IsBitSet(index_)) {
6794 Advance();
6795 }
6796 }
6797
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006798 bool Done() const { return index_ == blocks_.size(); }
6799 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006800 void Advance() {
6801 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006802 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006803 if (blocks_in_loop_.IsBitSet(index_)) {
6804 break;
6805 }
6806 }
6807 }
6808
6809 private:
6810 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006811 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006812 size_t index_;
6813
6814 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6815};
6816
Mingyao Yang3584bce2015-05-19 16:01:59 -07006817// Iterator over the blocks that art part of the loop. Includes blocks part
6818// of an inner loop. The order in which the blocks are iterated is reverse
6819// post order.
6820class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6821 public:
6822 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6823 : blocks_in_loop_(info.GetBlocks()),
6824 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6825 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006826 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006827 Advance();
6828 }
6829 }
6830
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006831 bool Done() const { return index_ == blocks_.size(); }
6832 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006833 void Advance() {
6834 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006835 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6836 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006837 break;
6838 }
6839 }
6840 }
6841
6842 private:
6843 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006844 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006845 size_t index_;
6846
6847 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6848};
6849
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006850inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006851 if (constant->IsIntConstant()) {
6852 return constant->AsIntConstant()->GetValue();
6853 } else if (constant->IsLongConstant()) {
6854 return constant->AsLongConstant()->GetValue();
6855 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006856 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006857 return 0;
6858 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006859}
6860
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006861#define INSTRUCTION_TYPE_CHECK(type, super) \
6862 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6863 inline const H##type* HInstruction::As##type() const { \
6864 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6865 } \
6866 inline H##type* HInstruction::As##type() { \
6867 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6868 }
6869
6870 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6871#undef INSTRUCTION_TYPE_CHECK
6872
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006873// Create space in `blocks` for adding `number_of_new_blocks` entries
6874// starting at location `at`. Blocks after `at` are moved accordingly.
6875inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6876 size_t number_of_new_blocks,
6877 size_t after) {
6878 DCHECK_LT(after, blocks->size());
6879 size_t old_size = blocks->size();
6880 size_t new_size = old_size + number_of_new_blocks;
6881 blocks->resize(new_size);
6882 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6883}
6884
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006885} // namespace art
6886
6887#endif // ART_COMPILER_OPTIMIZING_NODES_H_