blob: 0e449e3b05d6969fd78c3f34d5c9413d9e44486c [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
Vladimir Marko60584552015-09-03 13:35:12 +000029#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010030#include "base/transform_array_ref.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010031#include "dex_file.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000032#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000033#include "handle.h"
34#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000035#include "invoke_type.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010036#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010037#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000038#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000039#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010040#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070041#include "primitive.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010042#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043
44namespace art {
45
David Brazdil1abb4192015-02-17 18:33:36 +000046class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000047class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010050class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000051class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000052class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000054class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000055class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000056class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000057class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000058class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010059class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010060class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010061class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010062class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000063class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010064class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000065class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000066
Mathieu Chartier736b5602015-09-02 14:54:11 -070067namespace mirror {
68class DexCache;
69} // namespace mirror
70
Nicolas Geoffray818f2102014-02-18 16:43:35 +000071static const int kDefaultNumberOfBlocks = 8;
72static const int kDefaultNumberOfSuccessors = 2;
73static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010074static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010075static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000076static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000077
Roland Levillain5b5b9312016-03-22 14:57:31 +000078// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
79static constexpr int32_t kMaxIntShiftDistance = 0x1f;
80// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
81static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000082
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010083static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070084static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010085
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010086static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
87
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060088static constexpr uint32_t kNoDexPc = -1;
89
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010090inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
91 // For the purposes of the compiler, the dex files must actually be the same object
92 // if we want to safely treat them as the same. This is especially important for JIT
93 // as custom class loaders can open the same underlying file (or memory) multiple
94 // times and provide different class resolution but no two class loaders should ever
95 // use the same DexFile object - doing so is an unsupported hack that can lead to
96 // all sorts of weird failures.
97 return &lhs == &rhs;
98}
99
Dave Allison20dfc792014-06-16 20:44:29 -0700100enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700101 // All types.
102 kCondEQ, // ==
103 kCondNE, // !=
104 // Signed integers and floating-point numbers.
105 kCondLT, // <
106 kCondLE, // <=
107 kCondGT, // >
108 kCondGE, // >=
109 // Unsigned integers.
110 kCondB, // <
111 kCondBE, // <=
112 kCondA, // >
113 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100114 // First and last aliases.
115 kCondFirst = kCondEQ,
116 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700117};
118
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000119enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000120 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000121 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000122 kAnalysisFailThrowCatchLoop,
123 kAnalysisFailAmbiguousArrayOp,
124 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000125};
126
Vladimir Markof9f64412015-09-02 14:05:49 +0100127class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100128 public:
129 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
130
131 void AddInstruction(HInstruction* instruction);
132 void RemoveInstruction(HInstruction* instruction);
133
David Brazdilc3d743f2015-04-22 13:40:50 +0100134 // Insert `instruction` before/after an existing instruction `cursor`.
135 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
136 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
137
Roland Levillain6b469232014-09-25 10:10:38 +0100138 // Return true if this list contains `instruction`.
139 bool Contains(HInstruction* instruction) const;
140
Roland Levillainccc07a92014-09-16 14:48:16 +0100141 // Return true if `instruction1` is found before `instruction2` in
142 // this instruction list and false otherwise. Abort if none
143 // of these instructions is found.
144 bool FoundBefore(const HInstruction* instruction1,
145 const HInstruction* instruction2) const;
146
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000147 bool IsEmpty() const { return first_instruction_ == nullptr; }
148 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
149
150 // Update the block of all instructions to be `block`.
151 void SetBlockOfInstructions(HBasicBlock* block) const;
152
153 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000154 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000155 void Add(const HInstructionList& instruction_list);
156
David Brazdil2d7352b2015-04-20 14:52:42 +0100157 // Return the number of instructions in the list. This is an expensive operation.
158 size_t CountSize() const;
159
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100160 private:
161 HInstruction* first_instruction_;
162 HInstruction* last_instruction_;
163
164 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000165 friend class HGraph;
166 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100167 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100168 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100169
170 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
171};
172
David Brazdil4833f5a2015-12-16 10:37:39 +0000173class ReferenceTypeInfo : ValueObject {
174 public:
175 typedef Handle<mirror::Class> TypeHandle;
176
Vladimir Markoa1de9182016-02-25 11:37:38 +0000177 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
178
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700179 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100180 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
181 }
182
Vladimir Markoa1de9182016-02-25 11:37:38 +0000183 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000184 return ReferenceTypeInfo(type_handle, is_exact);
185 }
186
187 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
188
Vladimir Markof39745e2016-01-26 12:16:55 +0000189 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000190 return handle.GetReference() != nullptr;
191 }
192
Vladimir Marko456307a2016-04-19 14:12:13 +0000193 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000194 return IsValidHandle(type_handle_);
195 }
196
197 bool IsExact() const { return is_exact_; }
198
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700199 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000200 DCHECK(IsValid());
201 return GetTypeHandle()->IsObjectClass();
202 }
203
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700204 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000205 DCHECK(IsValid());
206 return GetTypeHandle()->IsStringClass();
207 }
208
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700209 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000210 DCHECK(IsValid());
211 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
212 }
213
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700214 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000215 DCHECK(IsValid());
216 return GetTypeHandle()->IsInterface();
217 }
218
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000220 DCHECK(IsValid());
221 return GetTypeHandle()->IsArrayClass();
222 }
223
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700224 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000225 DCHECK(IsValid());
226 return GetTypeHandle()->IsPrimitiveArray();
227 }
228
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700229 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000230 DCHECK(IsValid());
231 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
232 }
233
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700234 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000235 DCHECK(IsValid());
236 if (!IsExact()) return false;
237 if (!IsArrayClass()) return false;
238 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
239 }
240
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700241 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 DCHECK(IsValid());
243 if (!IsExact()) return false;
244 if (!IsArrayClass()) return false;
245 if (!rti.IsArrayClass()) return false;
246 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
247 rti.GetTypeHandle()->GetComponentType());
248 }
249
250 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
251
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700252 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000253 DCHECK(IsValid());
254 DCHECK(rti.IsValid());
255 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
256 }
257
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700258 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000259 DCHECK(IsValid());
260 DCHECK(rti.IsValid());
261 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
262 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
263 }
264
265 // Returns true if the type information provide the same amount of details.
266 // Note that it does not mean that the instructions have the same actual type
267 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700268 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000269 if (!IsValid() && !rti.IsValid()) {
270 // Invalid types are equal.
271 return true;
272 }
273 if (!IsValid() || !rti.IsValid()) {
274 // One is valid, the other not.
275 return false;
276 }
277 return IsExact() == rti.IsExact()
278 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
279 }
280
281 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000282 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
283 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
284 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000285
286 // The class of the object.
287 TypeHandle type_handle_;
288 // Whether or not the type is exact or a superclass of the actual type.
289 // Whether or not we have any information about this type.
290 bool is_exact_;
291};
292
293std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
294
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000295// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100296class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000297 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100298 HGraph(ArenaAllocator* arena,
299 const DexFile& dex_file,
300 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100301 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700302 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100303 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100304 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000305 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100306 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100308 blocks_(arena->Adapter(kArenaAllocBlockList)),
309 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
310 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700311 entry_block_(nullptr),
312 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100313 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100314 number_of_vregs_(0),
315 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000316 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400317 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000318 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000319 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000320 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000321 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100322 dex_file_(dex_file),
323 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100324 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100325 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100326 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700327 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000328 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100329 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
330 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
331 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
332 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000333 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000334 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
335 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100336 blocks_.reserve(kDefaultNumberOfBlocks);
337 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000338
David Brazdilbadd8262016-02-02 16:28:56 +0000339 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700340 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000341
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000342 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100343 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
344
David Brazdil69ba7b72015-06-23 18:27:30 +0100345 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000346 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100347
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000348 HBasicBlock* GetEntryBlock() const { return entry_block_; }
349 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100350 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000351
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000352 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
353 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000354
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000355 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100356
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100357 void ComputeDominanceInformation();
358 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000359 void ClearLoopInformation();
360 void FindBackEdges(ArenaBitVector* visited);
361 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100362 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100363 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000364
David Brazdil4833f5a2015-12-16 10:37:39 +0000365 // Analyze all natural loops in this graph. Returns a code specifying that it
366 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000367 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000368 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100369
David Brazdilffee3d32015-07-06 11:48:53 +0100370 // Iterate over blocks to compute try block membership. Needs reverse post
371 // order and loop information.
372 void ComputeTryBlockInformation();
373
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000374 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000375 // Returns the instruction to replace the invoke expression or null if the
376 // invoke is for a void method. Note that the caller is responsible for replacing
377 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000378 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000379
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000380 // Update the loop and try membership of `block`, which was spawned from `reference`.
381 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
382 // should be the new back edge.
383 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
384 HBasicBlock* reference,
385 bool replace_if_back_edge);
386
Mingyao Yang3584bce2015-05-19 16:01:59 -0700387 // Need to add a couple of blocks to test if the loop body is entered and
388 // put deoptimization instructions, etc.
389 void TransformLoopHeaderForBCE(HBasicBlock* header);
390
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000391 // Removes `block` from the graph. Assumes `block` has been disconnected from
392 // other blocks and has no instructions or phis.
393 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000394
David Brazdilfc6a86a2015-06-26 10:33:45 +0000395 // Splits the edge between `block` and `successor` while preserving the
396 // indices in the predecessor/successor lists. If there are multiple edges
397 // between the blocks, the lowest indices are used.
398 // Returns the new block which is empty and has the same dex pc as `successor`.
399 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
400
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100401 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
402 void SimplifyLoop(HBasicBlock* header);
403
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000404 int32_t GetNextInstructionId() {
405 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000406 return current_instruction_id_++;
407 }
408
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000409 int32_t GetCurrentInstructionId() const {
410 return current_instruction_id_;
411 }
412
413 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000414 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000415 current_instruction_id_ = id;
416 }
417
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100418 uint16_t GetMaximumNumberOfOutVRegs() const {
419 return maximum_number_of_out_vregs_;
420 }
421
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000422 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
423 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100424 }
425
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100426 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
427 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
428 }
429
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000430 void UpdateTemporariesVRegSlots(size_t slots) {
431 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100432 }
433
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000434 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100435 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000436 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100437 }
438
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100439 void SetNumberOfVRegs(uint16_t number_of_vregs) {
440 number_of_vregs_ = number_of_vregs;
441 }
442
443 uint16_t GetNumberOfVRegs() const {
444 return number_of_vregs_;
445 }
446
447 void SetNumberOfInVRegs(uint16_t value) {
448 number_of_in_vregs_ = value;
449 }
450
David Brazdildee58d62016-04-07 09:54:26 +0000451 uint16_t GetNumberOfInVRegs() const {
452 return number_of_in_vregs_;
453 }
454
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100455 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100456 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100457 return number_of_vregs_ - number_of_in_vregs_;
458 }
459
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100460 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100461 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100462 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100463
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100464 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
465 DCHECK(GetReversePostOrder()[0] == entry_block_);
466 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
467 }
468
469 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
470 return ReverseRange(GetReversePostOrder());
471 }
472
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100473 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100474 return linear_order_;
475 }
476
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100477 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
478 return ReverseRange(GetLinearOrder());
479 }
480
Mark Mendell1152c922015-04-24 17:06:35 -0400481 bool HasBoundsChecks() const {
482 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800483 }
484
Mark Mendell1152c922015-04-24 17:06:35 -0400485 void SetHasBoundsChecks(bool value) {
486 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800487 }
488
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100489 bool ShouldGenerateConstructorBarrier() const {
490 return should_generate_constructor_barrier_;
491 }
492
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000493 bool IsDebuggable() const { return debuggable_; }
494
David Brazdil8d5b8b22015-03-24 10:51:52 +0000495 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000496 // already, it is created and inserted into the graph. This method is only for
497 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600498 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000499
500 // TODO: This is problematic for the consistency of reference type propagation
501 // because it can be created anytime after the pass and thus it will be left
502 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600503 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000504
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600505 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
506 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000507 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600508 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
509 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000510 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600511 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
512 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000513 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600514 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
515 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000516 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000517
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100518 HCurrentMethod* GetCurrentMethod();
519
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100520 const DexFile& GetDexFile() const {
521 return dex_file_;
522 }
523
524 uint32_t GetMethodIdx() const {
525 return method_idx_;
526 }
527
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100528 InvokeType GetInvokeType() const {
529 return invoke_type_;
530 }
531
Mark Mendellc4701932015-04-10 13:18:51 -0400532 InstructionSet GetInstructionSet() const {
533 return instruction_set_;
534 }
535
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000536 bool IsCompilingOsr() const { return osr_; }
537
David Brazdil77a48ae2015-09-15 12:34:04 +0000538 bool HasTryCatch() const { return has_try_catch_; }
539 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100540
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000541 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
542 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
543
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100544 ArtMethod* GetArtMethod() const { return art_method_; }
545 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
546
Mark Mendellf6529172015-11-17 11:16:56 -0500547 // Returns an instruction with the opposite boolean value from 'cond'.
548 // The instruction has been inserted into the graph, either as a constant, or
549 // before cursor.
550 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
551
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000552 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
553
David Brazdil2d7352b2015-04-20 14:52:42 +0100554 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000555 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100556 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000557
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000558 template <class InstructionType, typename ValueType>
559 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600560 ArenaSafeMap<ValueType, InstructionType*>* cache,
561 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000562 // Try to find an existing constant of the given value.
563 InstructionType* constant = nullptr;
564 auto cached_constant = cache->find(value);
565 if (cached_constant != cache->end()) {
566 constant = cached_constant->second;
567 }
568
569 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100570 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000571 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600572 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000573 cache->Overwrite(value, constant);
574 InsertConstant(constant);
575 }
576 return constant;
577 }
578
David Brazdil8d5b8b22015-03-24 10:51:52 +0000579 void InsertConstant(HConstant* instruction);
580
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000581 // Cache a float constant into the graph. This method should only be
582 // called by the SsaBuilder when creating "equivalent" instructions.
583 void CacheFloatConstant(HFloatConstant* constant);
584
585 // See CacheFloatConstant comment.
586 void CacheDoubleConstant(HDoubleConstant* constant);
587
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000588 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000589
590 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100591 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000592
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100593 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100594 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000595
Aart Bik281c6812016-08-26 11:31:48 -0700596 // List of blocks to perform a linear order tree traversal. Unlike the reverse
597 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100598 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100599
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000600 HBasicBlock* entry_block_;
601 HBasicBlock* exit_block_;
602
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100603 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100604 uint16_t maximum_number_of_out_vregs_;
605
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100606 // The number of virtual registers in this method. Contains the parameters.
607 uint16_t number_of_vregs_;
608
609 // The number of virtual registers used by parameters of this method.
610 uint16_t number_of_in_vregs_;
611
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000612 // Number of vreg size slots that the temporaries use (used in baseline compiler).
613 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100614
Mark Mendell1152c922015-04-24 17:06:35 -0400615 // Has bounds checks. We can totally skip BCE if it's false.
616 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800617
David Brazdil77a48ae2015-09-15 12:34:04 +0000618 // Flag whether there are any try/catch blocks in the graph. We will skip
619 // try/catch-related passes if false.
620 bool has_try_catch_;
621
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000622 // Flag whether there are any irreducible loops in the graph.
623 bool has_irreducible_loops_;
624
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000625 // Indicates whether the graph should be compiled in a way that
626 // ensures full debuggability. If false, we can apply more
627 // aggressive optimizations that may limit the level of debugging.
628 const bool debuggable_;
629
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000630 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000631 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000632
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100633 // The dex file from which the method is from.
634 const DexFile& dex_file_;
635
636 // The method index in the dex file.
637 const uint32_t method_idx_;
638
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100639 // If inlined, this encodes how the callee is being invoked.
640 const InvokeType invoke_type_;
641
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100642 // Whether the graph has been transformed to SSA form. Only used
643 // in debug mode to ensure we are not using properties only valid
644 // for non-SSA form (like the number of temporaries).
645 bool in_ssa_form_;
646
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100647 const bool should_generate_constructor_barrier_;
648
Mathieu Chartiere401d142015-04-22 13:56:20 -0700649 const InstructionSet instruction_set_;
650
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000651 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000652 HNullConstant* cached_null_constant_;
653 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000654 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000655 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000656 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000657
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100658 HCurrentMethod* cached_current_method_;
659
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100660 // The ArtMethod this graph is for. Note that for AOT, it may be null,
661 // for example for methods whose declaring class could not be resolved
662 // (such as when the superclass could not be found).
663 ArtMethod* art_method_;
664
David Brazdil4833f5a2015-12-16 10:37:39 +0000665 // Keep the RTI of inexact Object to avoid having to pass stack handle
666 // collection pointer to passes which may create NullConstant.
667 ReferenceTypeInfo inexact_object_rti_;
668
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000669 // Whether we are compiling this graph for on stack replacement: this will
670 // make all loops seen as irreducible and emit special stack maps to mark
671 // compiled code entries which the interpreter can directly jump to.
672 const bool osr_;
673
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000674 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100675 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000676 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000677 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000678 DISALLOW_COPY_AND_ASSIGN(HGraph);
679};
680
Vladimir Markof9f64412015-09-02 14:05:49 +0100681class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000682 public:
683 HLoopInformation(HBasicBlock* header, HGraph* graph)
684 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100685 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000686 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100687 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100688 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100689 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000690 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100691 back_edges_.reserve(kDefaultNumberOfBackEdges);
692 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100693
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000694 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100695 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000696
697 void Dump(std::ostream& os);
698
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100699 HBasicBlock* GetHeader() const {
700 return header_;
701 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000702
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000703 void SetHeader(HBasicBlock* block) {
704 header_ = block;
705 }
706
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100707 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
708 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
709 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
710
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000711 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100712 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000713 }
714
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100715 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100716 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100717 }
718
David Brazdil46e2a392015-03-16 17:31:52 +0000719 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100720 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100721 }
722
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000723 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100724 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000725 }
726
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100727 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100728
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100729 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100730 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100731 }
732
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100733 // Returns the lifetime position of the back edge that has the
734 // greatest lifetime position.
735 size_t GetLifetimeEnd() const;
736
737 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100738 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100739 }
740
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000741 // Finds blocks that are part of this loop.
742 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100743
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100744 // Returns whether this loop information contains `block`.
745 // Note that this loop information *must* be populated before entering this function.
746 bool Contains(const HBasicBlock& block) const;
747
748 // Returns whether this loop information is an inner loop of `other`.
749 // Note that `other` *must* be populated before entering this function.
750 bool IsIn(const HLoopInformation& other) const;
751
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800752 // Returns true if instruction is not defined within this loop.
753 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700754
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100755 const ArenaBitVector& GetBlocks() const { return blocks_; }
756
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000757 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000758 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000759
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000760 void ClearAllBlocks() {
761 blocks_.ClearAllBits();
762 }
763
David Brazdil3f4a5222016-05-06 12:46:21 +0100764 bool HasBackEdgeNotDominatedByHeader() const;
765
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100766 bool IsPopulated() const {
767 return blocks_.GetHighestBitSet() != -1;
768 }
769
Anton Shaminf89381f2016-05-16 16:44:13 +0600770 bool DominatesAllBackEdges(HBasicBlock* block);
771
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000772 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100773 // Internal recursive implementation of `Populate`.
774 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100775 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100776
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000777 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100778 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000779 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100780 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100781 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100782 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000783
784 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
785};
786
David Brazdilec16f792015-08-19 15:04:01 +0100787// Stores try/catch information for basic blocks.
788// Note that HGraph is constructed so that catch blocks cannot simultaneously
789// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100790class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100791 public:
792 // Try block information constructor.
793 explicit TryCatchInformation(const HTryBoundary& try_entry)
794 : try_entry_(&try_entry),
795 catch_dex_file_(nullptr),
796 catch_type_index_(DexFile::kDexNoIndex16) {
797 DCHECK(try_entry_ != nullptr);
798 }
799
800 // Catch block information constructor.
801 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
802 : try_entry_(nullptr),
803 catch_dex_file_(&dex_file),
804 catch_type_index_(catch_type_index) {}
805
806 bool IsTryBlock() const { return try_entry_ != nullptr; }
807
808 const HTryBoundary& GetTryEntry() const {
809 DCHECK(IsTryBlock());
810 return *try_entry_;
811 }
812
813 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
814
815 bool IsCatchAllTypeIndex() const {
816 DCHECK(IsCatchBlock());
817 return catch_type_index_ == DexFile::kDexNoIndex16;
818 }
819
820 uint16_t GetCatchTypeIndex() const {
821 DCHECK(IsCatchBlock());
822 return catch_type_index_;
823 }
824
825 const DexFile& GetCatchDexFile() const {
826 DCHECK(IsCatchBlock());
827 return *catch_dex_file_;
828 }
829
830 private:
831 // One of possibly several TryBoundary instructions entering the block's try.
832 // Only set for try blocks.
833 const HTryBoundary* try_entry_;
834
835 // Exception type information. Only set for catch blocks.
836 const DexFile* catch_dex_file_;
837 const uint16_t catch_type_index_;
838};
839
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100840static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100841static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100842
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000843// A block in a method. Contains the list of instructions represented
844// as a double linked list. Each block knows its predecessors and
845// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100846
Vladimir Markof9f64412015-09-02 14:05:49 +0100847class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000848 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700849 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000850 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000851 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
852 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000853 loop_information_(nullptr),
854 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000855 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100856 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100857 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100858 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000859 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000860 try_catch_information_(nullptr) {
861 predecessors_.reserve(kDefaultNumberOfPredecessors);
862 successors_.reserve(kDefaultNumberOfSuccessors);
863 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
864 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000865
Vladimir Marko60584552015-09-03 13:35:12 +0000866 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100867 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000868 }
869
Vladimir Marko60584552015-09-03 13:35:12 +0000870 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100871 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000872 }
873
David Brazdild26a4112015-11-10 11:07:31 +0000874 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
875 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
876
Vladimir Marko60584552015-09-03 13:35:12 +0000877 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
878 return ContainsElement(successors_, block, start_from);
879 }
880
881 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100882 return dominated_blocks_;
883 }
884
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100885 bool IsEntryBlock() const {
886 return graph_->GetEntryBlock() == this;
887 }
888
889 bool IsExitBlock() const {
890 return graph_->GetExitBlock() == this;
891 }
892
David Brazdil46e2a392015-03-16 17:31:52 +0000893 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000894 bool IsSingleTryBoundary() const;
895
896 // Returns true if this block emits nothing but a jump.
897 bool IsSingleJump() const {
898 HLoopInformation* loop_info = GetLoopInformation();
899 return (IsSingleGoto() || IsSingleTryBoundary())
900 // Back edges generate a suspend check.
901 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
902 }
David Brazdil46e2a392015-03-16 17:31:52 +0000903
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000904 void AddBackEdge(HBasicBlock* back_edge) {
905 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000906 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000907 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100908 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000909 loop_information_->AddBackEdge(back_edge);
910 }
911
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000912 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000913 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000914
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100915 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000916 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600917 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000918
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000919 HBasicBlock* GetDominator() const { return dominator_; }
920 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000921 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
922
923 void RemoveDominatedBlock(HBasicBlock* block) {
924 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100925 }
Vladimir Marko60584552015-09-03 13:35:12 +0000926
927 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
928 ReplaceElement(dominated_blocks_, existing, new_block);
929 }
930
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100931 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000932
933 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100934 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000935 }
936
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100937 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
938 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100939 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100940 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100941 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
942 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000943
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000944 HInstruction* GetFirstInstructionDisregardMoves() const;
945
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000946 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000947 successors_.push_back(block);
948 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000949 }
950
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100951 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
952 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100953 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000954 new_block->predecessors_.push_back(this);
955 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000956 }
957
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000958 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
959 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000960 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000961 new_block->successors_.push_back(this);
962 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000963 }
964
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100965 // Insert `this` between `predecessor` and `successor. This method
966 // preserves the indicies, and will update the first edge found between
967 // `predecessor` and `successor`.
968 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
969 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100970 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000971 successor->predecessors_[predecessor_index] = this;
972 predecessor->successors_[successor_index] = this;
973 successors_.push_back(successor);
974 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100975 }
976
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100977 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000978 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100979 }
980
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000981 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000982 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000983 }
984
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100985 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000986 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100987 }
988
989 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000990 predecessors_.push_back(block);
991 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100992 }
993
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100994 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000995 DCHECK_EQ(predecessors_.size(), 2u);
996 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100997 }
998
David Brazdil769c9e52015-04-27 13:54:09 +0100999 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001000 DCHECK_EQ(successors_.size(), 2u);
1001 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001002 }
1003
David Brazdilfc6a86a2015-06-26 10:33:45 +00001004 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001005 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001006 }
1007
David Brazdilfc6a86a2015-06-26 10:33:45 +00001008 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001009 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001010 }
1011
David Brazdilfc6a86a2015-06-26 10:33:45 +00001012 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001013 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001014 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001015 }
1016
1017 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001018 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001019 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001020 }
1021
1022 // Returns whether the first occurrence of `predecessor` in the list of
1023 // predecessors is at index `idx`.
1024 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001025 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001026 return GetPredecessorIndexOf(predecessor) == idx;
1027 }
1028
David Brazdild7558da2015-09-22 13:04:14 +01001029 // Create a new block between this block and its predecessors. The new block
1030 // is added to the graph, all predecessor edges are relinked to it and an edge
1031 // is created to `this`. Returns the new empty block. Reverse post order or
1032 // loop and try/catch information are not updated.
1033 HBasicBlock* CreateImmediateDominator();
1034
David Brazdilfc6a86a2015-06-26 10:33:45 +00001035 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001036 // created, latter block. Note that this method will add the block to the
1037 // graph, create a Goto at the end of the former block and will create an edge
1038 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001039 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001040 HBasicBlock* SplitBefore(HInstruction* cursor);
1041
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001042 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001043 // created block. Note that this method just updates raw block information,
1044 // like predecessors, successors, dominators, and instruction list. It does not
1045 // update the graph, reverse post order, loop information, nor make sure the
1046 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001047 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1048
1049 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1050 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001051
1052 // Merge `other` at the end of `this`. Successors and dominated blocks of
1053 // `other` are changed to be successors and dominated blocks of `this`. Note
1054 // that this method does not update the graph, reverse post order, loop
1055 // information, nor make sure the blocks are consistent (for example ending
1056 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001057 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001058
1059 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1060 // of `this` are moved to `other`.
1061 // Note that this method does not update the graph, reverse post order, loop
1062 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001063 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001064 void ReplaceWith(HBasicBlock* other);
1065
David Brazdil2d7352b2015-04-20 14:52:42 +01001066 // Merge `other` at the end of `this`. This method updates loops, reverse post
1067 // order, links to predecessors, successors, dominators and deletes the block
1068 // from the graph. The two blocks must be successive, i.e. `this` the only
1069 // predecessor of `other` and vice versa.
1070 void MergeWith(HBasicBlock* other);
1071
1072 // Disconnects `this` from all its predecessors, successors and dominator,
1073 // removes it from all loops it is included in and eventually from the graph.
1074 // The block must not dominate any other block. Predecessors and successors
1075 // are safely updated.
1076 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001077
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001078 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001079 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001080 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001081 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001082 // Replace instruction `initial` with `replacement` within this block.
1083 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1084 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001085 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001086 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001087 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1088 // instruction list. With 'ensure_safety' set to true, it verifies that the
1089 // instruction is not in use and removes it from the use lists of its inputs.
1090 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1091 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001092 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001093
1094 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001095 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001096 }
1097
Roland Levillain6b879dd2014-09-22 17:13:44 +01001098 bool IsLoopPreHeaderFirstPredecessor() const {
1099 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001100 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001101 }
1102
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001103 bool IsFirstPredecessorBackEdge() const {
1104 DCHECK(IsLoopHeader());
1105 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1106 }
1107
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001108 HLoopInformation* GetLoopInformation() const {
1109 return loop_information_;
1110 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001111
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001112 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001113 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001114 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001115 void SetInLoop(HLoopInformation* info) {
1116 if (IsLoopHeader()) {
1117 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001118 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001119 loop_information_ = info;
1120 } else if (loop_information_->Contains(*info->GetHeader())) {
1121 // Block is currently part of an outer loop. Make it part of this inner loop.
1122 // Note that a non loop header having a loop information means this loop information
1123 // has already been populated
1124 loop_information_ = info;
1125 } else {
1126 // Block is part of an inner loop. Do not update the loop information.
1127 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1128 // at this point, because this method is being called while populating `info`.
1129 }
1130 }
1131
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001132 // Raw update of the loop information.
1133 void SetLoopInformation(HLoopInformation* info) {
1134 loop_information_ = info;
1135 }
1136
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001137 bool IsInLoop() const { return loop_information_ != nullptr; }
1138
David Brazdilec16f792015-08-19 15:04:01 +01001139 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1140
1141 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1142 try_catch_information_ = try_catch_information;
1143 }
1144
1145 bool IsTryBlock() const {
1146 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1147 }
1148
1149 bool IsCatchBlock() const {
1150 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1151 }
David Brazdilffee3d32015-07-06 11:48:53 +01001152
1153 // Returns the try entry that this block's successors should have. They will
1154 // be in the same try, unless the block ends in a try boundary. In that case,
1155 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001156 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001157
David Brazdild7558da2015-09-22 13:04:14 +01001158 bool HasThrowingInstructions() const;
1159
David Brazdila4b8c212015-05-07 09:59:30 +01001160 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001161 bool Dominates(HBasicBlock* block) const;
1162
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001163 size_t GetLifetimeStart() const { return lifetime_start_; }
1164 size_t GetLifetimeEnd() const { return lifetime_end_; }
1165
1166 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1167 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1168
David Brazdil8d5b8b22015-03-24 10:51:52 +00001169 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001170 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001171 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001172 bool HasSinglePhi() const;
1173
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001174 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001175 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001176 ArenaVector<HBasicBlock*> predecessors_;
1177 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001178 HInstructionList instructions_;
1179 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001180 HLoopInformation* loop_information_;
1181 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001182 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001183 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001184 // The dex program counter of the first instruction of this block.
1185 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001186 size_t lifetime_start_;
1187 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001188 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001189
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001190 friend class HGraph;
1191 friend class HInstruction;
1192
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001193 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1194};
1195
David Brazdilb2bd1c52015-03-25 11:17:37 +00001196// Iterates over the LoopInformation of all loops which contain 'block'
1197// from the innermost to the outermost.
1198class HLoopInformationOutwardIterator : public ValueObject {
1199 public:
1200 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1201 : current_(block.GetLoopInformation()) {}
1202
1203 bool Done() const { return current_ == nullptr; }
1204
1205 void Advance() {
1206 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001207 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001208 }
1209
1210 HLoopInformation* Current() const {
1211 DCHECK(!Done());
1212 return current_;
1213 }
1214
1215 private:
1216 HLoopInformation* current_;
1217
1218 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1219};
1220
Alexandre Ramesef20f712015-06-09 10:29:30 +01001221#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001222 M(Above, Condition) \
1223 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001224 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001225 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001226 M(ArrayGet, Instruction) \
1227 M(ArrayLength, Instruction) \
1228 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001229 M(Below, Condition) \
1230 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001231 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001232 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001233 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001234 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001235 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001236 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001237 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001238 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001239 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001240 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001241 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001242 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001243 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001244 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001245 M(Exit, Instruction) \
1246 M(FloatConstant, Constant) \
1247 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001248 M(GreaterThan, Condition) \
1249 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001250 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001251 M(InstanceFieldGet, Instruction) \
1252 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001253 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001254 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001255 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001256 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001257 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001258 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001259 M(LessThan, Condition) \
1260 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001261 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001262 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001263 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001264 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001265 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001266 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001267 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001268 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001269 M(Neg, UnaryOperation) \
1270 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001271 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001272 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001273 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001274 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001275 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001276 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001277 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001278 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001279 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001280 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001281 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001282 M(Return, Instruction) \
1283 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001284 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001285 M(Shl, BinaryOperation) \
1286 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001287 M(StaticFieldGet, Instruction) \
1288 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001289 M(UnresolvedInstanceFieldGet, Instruction) \
1290 M(UnresolvedInstanceFieldSet, Instruction) \
1291 M(UnresolvedStaticFieldGet, Instruction) \
1292 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001293 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001294 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001295 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001296 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001297 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001298 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001299 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001300 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001301
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001302/*
1303 * Instructions, shared across several (not all) architectures.
1304 */
1305#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1306#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1307#else
1308#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001309 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001310 M(MultiplyAccumulate, Instruction) \
1311 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001312#endif
1313
Vladimir Markob4536b72015-11-24 13:45:23 +00001314#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001315#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001316#else
1317#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1318 M(ArmDexCacheArraysBase, Instruction)
1319#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001320
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001321#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001322#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001323#else
1324#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001325 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001326#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001327
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001328#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001329#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001330#else
1331#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1332 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001333 M(MipsDexCacheArraysBase, Instruction) \
1334 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001335#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001336
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001337#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1338
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001339#ifndef ART_ENABLE_CODEGEN_x86
1340#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1341#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001342#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1343 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001344 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001345 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001346 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001347#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001348
1349#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1350
1351#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1352 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001353 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001354 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1355 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001356 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001357 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001358 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1359 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1360
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001361#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1362 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001363 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001364 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001365 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001366 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001367
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001368#define FOR_EACH_INSTRUCTION(M) \
1369 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1370 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1371
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001372#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001373FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1374#undef FORWARD_DECLARATION
1375
Vladimir Marko372f10e2016-05-17 16:30:10 +01001376#define DECLARE_INSTRUCTION(type) \
1377 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1378 const char* DebugName() const OVERRIDE { return #type; } \
1379 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1380 return other->Is##type(); \
1381 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001382 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001383
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001384#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1385 bool Is##type() const { return As##type() != nullptr; } \
1386 const H##type* As##type() const { return this; } \
1387 H##type* As##type() { return this; }
1388
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001389template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001390class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001391 public:
David Brazdiled596192015-01-23 10:39:45 +00001392 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001393 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001394 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001395
Vladimir Marko46817b82016-03-29 12:21:58 +01001396 // Hook for the IntrusiveForwardList<>.
1397 // TODO: Hide this better.
1398 IntrusiveForwardListHook hook;
1399
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001400 private:
David Brazdiled596192015-01-23 10:39:45 +00001401 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001402 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001403
1404 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001405 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001406
Vladimir Marko46817b82016-03-29 12:21:58 +01001407 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001408
1409 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1410};
1411
David Brazdiled596192015-01-23 10:39:45 +00001412template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001413using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001414
David Brazdil1abb4192015-02-17 18:33:36 +00001415// This class is used by HEnvironment and HInstruction classes to record the
1416// instructions they use and pointers to the corresponding HUseListNodes kept
1417// by the used instructions.
1418template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001419class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001420 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001421 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1422 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001423
Vladimir Marko46817b82016-03-29 12:21:58 +01001424 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1425 : HUserRecord(old_record.instruction_, before_use_node) {}
1426 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1427 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001428 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001429 }
1430
1431 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001432 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1433 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001434
1435 private:
1436 // Instruction used by the user.
1437 HInstruction* instruction_;
1438
Vladimir Marko46817b82016-03-29 12:21:58 +01001439 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1440 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001441};
1442
Vladimir Markoe9004912016-06-16 16:50:52 +01001443// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1444// This is used for HInstruction::GetInputs() to return a container wrapper providing
1445// HInstruction* values even though the underlying container has HUserRecord<>s.
1446struct HInputExtractor {
1447 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1448 return record.GetInstruction();
1449 }
1450 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1451 return record.GetInstruction();
1452 }
1453};
1454
1455using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1456using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1457
Aart Bik854a02b2015-07-14 16:07:00 -07001458/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001459 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001460 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001461 * For write/read dependences on fields/arrays, the dependence analysis uses
1462 * type disambiguation (e.g. a float field write cannot modify the value of an
1463 * integer field read) and the access type (e.g. a reference array write cannot
1464 * modify the value of a reference field read [although it may modify the
1465 * reference fetch prior to reading the field, which is represented by its own
1466 * write/read dependence]). The analysis makes conservative points-to
1467 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1468 * the same, and any reference read depends on any reference read without
1469 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001470 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001471 * The internal representation uses 38-bit and is described in the table below.
1472 * The first line indicates the side effect, and for field/array accesses the
1473 * second line indicates the type of the access (in the order of the
1474 * Primitive::Type enum).
1475 * The two numbered lines below indicate the bit position in the bitfield (read
1476 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001477 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001478 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1479 * +-------------+---------+---------+--------------+---------+---------+
1480 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1481 * | 3 |333333322|222222221| 1 |111111110|000000000|
1482 * | 7 |654321098|765432109| 8 |765432109|876543210|
1483 *
1484 * Note that, to ease the implementation, 'changes' bits are least significant
1485 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001486 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001487class SideEffects : public ValueObject {
1488 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001489 SideEffects() : flags_(0) {}
1490
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001491 static SideEffects None() {
1492 return SideEffects(0);
1493 }
1494
1495 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001496 return SideEffects(kAllChangeBits | kAllDependOnBits);
1497 }
1498
1499 static SideEffects AllChanges() {
1500 return SideEffects(kAllChangeBits);
1501 }
1502
1503 static SideEffects AllDependencies() {
1504 return SideEffects(kAllDependOnBits);
1505 }
1506
1507 static SideEffects AllExceptGCDependency() {
1508 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1509 }
1510
1511 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001512 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001513 }
1514
Aart Bik34c3ba92015-07-20 14:08:59 -07001515 static SideEffects AllWrites() {
1516 return SideEffects(kAllWrites);
1517 }
1518
1519 static SideEffects AllReads() {
1520 return SideEffects(kAllReads);
1521 }
1522
1523 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1524 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001525 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001526 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001527 }
1528
Aart Bik854a02b2015-07-14 16:07:00 -07001529 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001530 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001531 }
1532
Aart Bik34c3ba92015-07-20 14:08:59 -07001533 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1534 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001535 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001536 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001537 }
1538
1539 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001540 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001541 }
1542
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001543 static SideEffects CanTriggerGC() {
1544 return SideEffects(1ULL << kCanTriggerGCBit);
1545 }
1546
1547 static SideEffects DependsOnGC() {
1548 return SideEffects(1ULL << kDependsOnGCBit);
1549 }
1550
Aart Bik854a02b2015-07-14 16:07:00 -07001551 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001552 SideEffects Union(SideEffects other) const {
1553 return SideEffects(flags_ | other.flags_);
1554 }
1555
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001556 SideEffects Exclusion(SideEffects other) const {
1557 return SideEffects(flags_ & ~other.flags_);
1558 }
1559
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001560 void Add(SideEffects other) {
1561 flags_ |= other.flags_;
1562 }
1563
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001564 bool Includes(SideEffects other) const {
1565 return (other.flags_ & flags_) == other.flags_;
1566 }
1567
1568 bool HasSideEffects() const {
1569 return (flags_ & kAllChangeBits);
1570 }
1571
1572 bool HasDependencies() const {
1573 return (flags_ & kAllDependOnBits);
1574 }
1575
1576 // Returns true if there are no side effects or dependencies.
1577 bool DoesNothing() const {
1578 return flags_ == 0;
1579 }
1580
Aart Bik854a02b2015-07-14 16:07:00 -07001581 // Returns true if something is written.
1582 bool DoesAnyWrite() const {
1583 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001584 }
1585
Aart Bik854a02b2015-07-14 16:07:00 -07001586 // Returns true if something is read.
1587 bool DoesAnyRead() const {
1588 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001589 }
1590
Aart Bik854a02b2015-07-14 16:07:00 -07001591 // Returns true if potentially everything is written and read
1592 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001593 bool DoesAllReadWrite() const {
1594 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1595 }
1596
Aart Bik854a02b2015-07-14 16:07:00 -07001597 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001598 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001599 }
1600
Roland Levillain0d5a2812015-11-13 10:07:31 +00001601 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001602 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001603 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1604 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001605 }
1606
1607 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001608 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001609 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001610 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001611 for (int s = kLastBit; s >= 0; s--) {
1612 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1613 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1614 // This is a bit for the GC side effect.
1615 if (current_bit_is_set) {
1616 flags += "GC";
1617 }
Aart Bik854a02b2015-07-14 16:07:00 -07001618 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001619 } else {
1620 // This is a bit for the array/field analysis.
1621 // The underscore character stands for the 'can trigger GC' bit.
1622 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1623 if (current_bit_is_set) {
1624 flags += kDebug[s];
1625 }
1626 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1627 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1628 flags += "|";
1629 }
1630 }
Aart Bik854a02b2015-07-14 16:07:00 -07001631 }
1632 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001633 }
1634
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001635 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001636
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001637 private:
1638 static constexpr int kFieldArrayAnalysisBits = 9;
1639
1640 static constexpr int kFieldWriteOffset = 0;
1641 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1642 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1643 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1644
1645 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1646
1647 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1648 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1649 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1650 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1651
1652 static constexpr int kLastBit = kDependsOnGCBit;
1653 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1654
1655 // Aliases.
1656
1657 static_assert(kChangeBits == kDependOnBits,
1658 "the 'change' bits should match the 'depend on' bits.");
1659
1660 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1661 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1662 static constexpr uint64_t kAllWrites =
1663 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1664 static constexpr uint64_t kAllReads =
1665 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001666
Aart Bik854a02b2015-07-14 16:07:00 -07001667 // Translates type to bit flag.
1668 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1669 CHECK_NE(type, Primitive::kPrimVoid);
1670 const uint64_t one = 1;
1671 const int shift = type; // 0-based consecutive enum
1672 DCHECK_LE(kFieldWriteOffset, shift);
1673 DCHECK_LT(shift, kArrayWriteOffset);
1674 return one << (type + offset);
1675 }
1676
1677 // Private constructor on direct flags value.
1678 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1679
1680 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001681};
1682
David Brazdiled596192015-01-23 10:39:45 +00001683// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001684class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001685 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001686 HEnvironment(ArenaAllocator* arena,
1687 size_t number_of_vregs,
1688 const DexFile& dex_file,
1689 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001690 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001691 InvokeType invoke_type,
1692 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001693 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1694 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001695 parent_(nullptr),
1696 dex_file_(dex_file),
1697 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001698 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001699 invoke_type_(invoke_type),
1700 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001701 }
1702
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001703 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001704 : HEnvironment(arena,
1705 to_copy.Size(),
1706 to_copy.GetDexFile(),
1707 to_copy.GetMethodIdx(),
1708 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001709 to_copy.GetInvokeType(),
1710 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001711
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001712 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001713 if (parent_ != nullptr) {
1714 parent_->SetAndCopyParentChain(allocator, parent);
1715 } else {
1716 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1717 parent_->CopyFrom(parent);
1718 if (parent->GetParent() != nullptr) {
1719 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1720 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001721 }
David Brazdiled596192015-01-23 10:39:45 +00001722 }
1723
Vladimir Marko71bf8092015-09-15 15:33:14 +01001724 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001725 void CopyFrom(HEnvironment* environment);
1726
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001727 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1728 // input to the loop phi instead. This is for inserting instructions that
1729 // require an environment (like HDeoptimization) in the loop pre-header.
1730 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001731
1732 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001733 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001734 }
1735
1736 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001737 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001738 }
1739
David Brazdil1abb4192015-02-17 18:33:36 +00001740 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001741
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001742 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001743
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001744 HEnvironment* GetParent() const { return parent_; }
1745
1746 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001747 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001748 }
1749
1750 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001751 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001752 }
1753
1754 uint32_t GetDexPc() const {
1755 return dex_pc_;
1756 }
1757
1758 uint32_t GetMethodIdx() const {
1759 return method_idx_;
1760 }
1761
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001762 InvokeType GetInvokeType() const {
1763 return invoke_type_;
1764 }
1765
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001766 const DexFile& GetDexFile() const {
1767 return dex_file_;
1768 }
1769
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001770 HInstruction* GetHolder() const {
1771 return holder_;
1772 }
1773
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001774
1775 bool IsFromInlinedInvoke() const {
1776 return GetParent() != nullptr;
1777 }
1778
David Brazdiled596192015-01-23 10:39:45 +00001779 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001780 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1781 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001782 HEnvironment* parent_;
1783 const DexFile& dex_file_;
1784 const uint32_t method_idx_;
1785 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001786 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001787
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001788 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001789 HInstruction* const holder_;
1790
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001791 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001792
1793 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1794};
1795
Vladimir Markof9f64412015-09-02 14:05:49 +01001796class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001797 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001798 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001799 : previous_(nullptr),
1800 next_(nullptr),
1801 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001802 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001803 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001804 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001805 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001806 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001807 locations_(nullptr),
1808 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001809 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001810 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001811 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1812 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1813 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001814
Dave Allison20dfc792014-06-16 20:44:29 -07001815 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001816
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001817#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001818 enum InstructionKind {
1819 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1820 };
1821#undef DECLARE_KIND
1822
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001823 HInstruction* GetNext() const { return next_; }
1824 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001825
Calin Juravle77520bc2015-01-12 18:45:46 +00001826 HInstruction* GetNextDisregardingMoves() const;
1827 HInstruction* GetPreviousDisregardingMoves() const;
1828
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001829 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001830 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001831 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001832 bool IsInBlock() const { return block_ != nullptr; }
1833 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001834 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1835 bool IsIrreducibleLoopHeaderPhi() const {
1836 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1837 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001838
Vladimir Marko372f10e2016-05-17 16:30:10 +01001839 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1840
1841 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1842 // One virtual method is enough, just const_cast<> and then re-add the const.
1843 return ArrayRef<const HUserRecord<HInstruction*>>(
1844 const_cast<HInstruction*>(this)->GetInputRecords());
1845 }
1846
Vladimir Markoe9004912016-06-16 16:50:52 +01001847 HInputsRef GetInputs() {
1848 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001849 }
1850
Vladimir Markoe9004912016-06-16 16:50:52 +01001851 HConstInputsRef GetInputs() const {
1852 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001853 }
1854
1855 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001856 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001857
Aart Bik2767f4b2016-10-28 15:03:53 -07001858 bool HasInput(HInstruction* input) const {
1859 for (const HInstruction* i : GetInputs()) {
1860 if (i == input) {
1861 return true;
1862 }
1863 }
1864 return false;
1865 }
1866
Vladimir Marko372f10e2016-05-17 16:30:10 +01001867 void SetRawInputAt(size_t index, HInstruction* input) {
1868 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1869 }
1870
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001871 virtual void Accept(HGraphVisitor* visitor) = 0;
1872 virtual const char* DebugName() const = 0;
1873
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001874 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1875
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001876 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001877
1878 uint32_t GetDexPc() const { return dex_pc_; }
1879
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001880 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001881
Roland Levillaine161a2a2014-10-03 12:45:18 +01001882 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001883 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001884
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001885 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001886 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001887
Calin Juravle10e244f2015-01-26 18:54:32 +00001888 // Does not apply for all instructions, but having this at top level greatly
1889 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001890 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001891 virtual bool CanBeNull() const {
1892 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1893 return true;
1894 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001895
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001896 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001897 return false;
1898 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001899
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001900 virtual bool IsActualObject() const {
1901 return GetType() == Primitive::kPrimNot;
1902 }
1903
Calin Juravle2e768302015-07-28 14:41:11 +00001904 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001905
Calin Juravle61d544b2015-02-23 16:46:57 +00001906 ReferenceTypeInfo GetReferenceTypeInfo() const {
1907 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001908 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001909 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001910 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001911
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001912 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001913 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001914 // Note: fixup_end remains valid across push_front().
1915 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1916 HUseListNode<HInstruction*>* new_node =
1917 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1918 uses_.push_front(*new_node);
1919 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001920 }
1921
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001922 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001923 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001924 // Note: env_fixup_end remains valid across push_front().
1925 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1926 HUseListNode<HEnvironment*>* new_node =
1927 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1928 env_uses_.push_front(*new_node);
1929 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001930 }
1931
David Brazdil1abb4192015-02-17 18:33:36 +00001932 void RemoveAsUserOfInput(size_t input) {
1933 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001934 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1935 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1936 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001937 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001938
Vladimir Marko372f10e2016-05-17 16:30:10 +01001939 void RemoveAsUserOfAllInputs() {
1940 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1941 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1942 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1943 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1944 }
1945 }
1946
David Brazdil1abb4192015-02-17 18:33:36 +00001947 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1948 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001949
Vladimir Marko46817b82016-03-29 12:21:58 +01001950 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1951 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1952 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001953 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001954 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001955 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001956
Aart Bikcc42be02016-10-20 16:14:16 -07001957 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07001958 return
1959 !HasSideEffects() &&
1960 !CanThrow() &&
1961 !IsSuspendCheck() &&
1962 !IsControlFlow() &&
1963 !IsNativeDebugInfo() &&
1964 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07001965 // If we added an explicit barrier then we should keep it.
1966 !IsMemoryBarrier();
1967 }
1968
Aart Bikcc42be02016-10-20 16:14:16 -07001969 bool IsDeadAndRemovable() const {
1970 return IsRemovable() && !HasUses();
1971 }
1972
Roland Levillain6c82d402014-10-13 16:10:27 +01001973 // Does this instruction strictly dominate `other_instruction`?
1974 // Returns false if this instruction and `other_instruction` are the same.
1975 // Aborts if this instruction and `other_instruction` are both phis.
1976 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001977
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001978 int GetId() const { return id_; }
1979 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001980
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001981 int GetSsaIndex() const { return ssa_index_; }
1982 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1983 bool HasSsaIndex() const { return ssa_index_ != -1; }
1984
1985 bool HasEnvironment() const { return environment_ != nullptr; }
1986 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001987 // Set the `environment_` field. Raw because this method does not
1988 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001989 void SetRawEnvironment(HEnvironment* environment) {
1990 DCHECK(environment_ == nullptr);
1991 DCHECK_EQ(environment->GetHolder(), this);
1992 environment_ = environment;
1993 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001994
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001995 void InsertRawEnvironment(HEnvironment* environment) {
1996 DCHECK(environment_ != nullptr);
1997 DCHECK_EQ(environment->GetHolder(), this);
1998 DCHECK(environment->GetParent() == nullptr);
1999 environment->parent_ = environment_;
2000 environment_ = environment;
2001 }
2002
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002003 void RemoveEnvironment();
2004
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002005 // Set the environment of this instruction, copying it from `environment`. While
2006 // copying, the uses lists are being updated.
2007 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002008 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002009 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002010 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002011 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002012 if (environment->GetParent() != nullptr) {
2013 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2014 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002015 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002016
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002017 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2018 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002019 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002020 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002021 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002022 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002023 if (environment->GetParent() != nullptr) {
2024 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2025 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002026 }
2027
Nicolas Geoffray39468442014-09-02 15:17:15 +01002028 // Returns the number of entries in the environment. Typically, that is the
2029 // number of dex registers in a method. It could be more in case of inlining.
2030 size_t EnvironmentSize() const;
2031
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002032 LocationSummary* GetLocations() const { return locations_; }
2033 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002034
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002035 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002036 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002037
Alexandre Rames188d4312015-04-09 18:30:21 +01002038 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2039 // uses of this instruction by `other` are *not* updated.
2040 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2041 ReplaceWith(other);
2042 other->ReplaceInput(this, use_index);
2043 }
2044
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002045 // Move `this` instruction before `cursor`.
2046 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002047
Vladimir Markofb337ea2015-11-25 15:25:10 +00002048 // Move `this` before its first user and out of any loops. If there is no
2049 // out-of-loop user that dominates all other users, move the instruction
2050 // to the end of the out-of-loop common dominator of the user's blocks.
2051 //
2052 // This can be used only on non-throwing instructions with no side effects that
2053 // have at least one use but no environment uses.
2054 void MoveBeforeFirstUserAndOutOfLoops();
2055
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002056#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002057 bool Is##type() const; \
2058 const H##type* As##type() const; \
2059 H##type* As##type();
2060
2061 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2062#undef INSTRUCTION_TYPE_CHECK
2063
2064#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002065 bool Is##type() const { return (As##type() != nullptr); } \
2066 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002067 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002068 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002069#undef INSTRUCTION_TYPE_CHECK
2070
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002071 // Returns whether the instruction can be moved within the graph.
2072 virtual bool CanBeMoved() const { return false; }
2073
2074 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002075 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002076 return false;
2077 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002078
2079 // Returns whether any data encoded in the two instructions is equal.
2080 // This method does not look at the inputs. Both instructions must be
2081 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002082 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002083 return false;
2084 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002085
2086 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002087 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002088 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002089 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002090
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002091 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2092 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2093 // the virtual function because the __attribute__((__pure__)) doesn't really
2094 // apply the strong requirement for virtual functions, preventing optimizations.
2095 InstructionKind GetKind() const PURE;
2096 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002097
2098 virtual size_t ComputeHashCode() const {
2099 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002100 for (const HInstruction* input : GetInputs()) {
2101 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002102 }
2103 return result;
2104 }
2105
2106 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002107 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002108 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002109
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002110 size_t GetLifetimePosition() const { return lifetime_position_; }
2111 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2112 LiveInterval* GetLiveInterval() const { return live_interval_; }
2113 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2114 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2115
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002116 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2117
2118 // Returns whether the code generation of the instruction will require to have access
2119 // to the current method. Such instructions are:
2120 // (1): Instructions that require an environment, as calling the runtime requires
2121 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002122 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2123 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002124 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002125 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002126 }
2127
Vladimir Markodc151b22015-10-15 18:02:30 +01002128 // Returns whether the code generation of the instruction will require to have access
2129 // to the dex cache of the current method's declaring class via the current method.
2130 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002131
Mark Mendellc4701932015-04-10 13:18:51 -04002132 // Does this instruction have any use in an environment before
2133 // control flow hits 'other'?
2134 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2135
2136 // Remove all references to environment uses of this instruction.
2137 // The caller must ensure that this is safe to do.
2138 void RemoveEnvironmentUsers();
2139
Vladimir Markoa1de9182016-02-25 11:37:38 +00002140 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2141 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002142
David Brazdil1abb4192015-02-17 18:33:36 +00002143 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002144 // If set, the machine code for this instruction is assumed to be generated by
2145 // its users. Used by liveness analysis to compute use positions accordingly.
2146 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2147 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2148 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2149 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2150
Vladimir Marko372f10e2016-05-17 16:30:10 +01002151 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2152 return GetInputRecords()[i];
2153 }
2154
2155 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2156 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2157 input_records[index] = input;
2158 }
David Brazdil1abb4192015-02-17 18:33:36 +00002159
Vladimir Markoa1de9182016-02-25 11:37:38 +00002160 uint32_t GetPackedFields() const {
2161 return packed_fields_;
2162 }
2163
2164 template <size_t flag>
2165 bool GetPackedFlag() const {
2166 return (packed_fields_ & (1u << flag)) != 0u;
2167 }
2168
2169 template <size_t flag>
2170 void SetPackedFlag(bool value = true) {
2171 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2172 }
2173
2174 template <typename BitFieldType>
2175 typename BitFieldType::value_type GetPackedField() const {
2176 return BitFieldType::Decode(packed_fields_);
2177 }
2178
2179 template <typename BitFieldType>
2180 void SetPackedField(typename BitFieldType::value_type value) {
2181 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2182 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2183 }
2184
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002185 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002186 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2187 auto before_use_node = uses_.before_begin();
2188 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2189 HInstruction* user = use_node->GetUser();
2190 size_t input_index = use_node->GetIndex();
2191 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2192 before_use_node = use_node;
2193 }
2194 }
2195
2196 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2197 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2198 if (next != uses_.end()) {
2199 HInstruction* next_user = next->GetUser();
2200 size_t next_index = next->GetIndex();
2201 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2202 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2203 }
2204 }
2205
2206 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2207 auto before_env_use_node = env_uses_.before_begin();
2208 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2209 HEnvironment* user = env_use_node->GetUser();
2210 size_t input_index = env_use_node->GetIndex();
2211 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2212 before_env_use_node = env_use_node;
2213 }
2214 }
2215
2216 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2217 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2218 if (next != env_uses_.end()) {
2219 HEnvironment* next_user = next->GetUser();
2220 size_t next_index = next->GetIndex();
2221 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2222 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2223 }
2224 }
David Brazdil1abb4192015-02-17 18:33:36 +00002225
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002226 HInstruction* previous_;
2227 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002228 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002229 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002230
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002231 // An instruction gets an id when it is added to the graph.
2232 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002233 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002234 int id_;
2235
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002236 // When doing liveness analysis, instructions that have uses get an SSA index.
2237 int ssa_index_;
2238
Vladimir Markoa1de9182016-02-25 11:37:38 +00002239 // Packed fields.
2240 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002241
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002242 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002243 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002244
2245 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002246 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002247
Nicolas Geoffray39468442014-09-02 15:17:15 +01002248 // The environment associated with this instruction. Not null if the instruction
2249 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002250 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002251
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002252 // Set by the code generator.
2253 LocationSummary* locations_;
2254
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002255 // Set by the liveness analysis.
2256 LiveInterval* live_interval_;
2257
2258 // Set by the liveness analysis, this is the position in a linear
2259 // order of blocks where this instruction's live interval start.
2260 size_t lifetime_position_;
2261
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002262 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002263
Vladimir Markoa1de9182016-02-25 11:37:38 +00002264 // The reference handle part of the reference type info.
2265 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002266 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002267 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002268
David Brazdil1abb4192015-02-17 18:33:36 +00002269 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002270 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002271 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002272 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002273 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002274
2275 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2276};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002277std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002278
2279class HInstructionIterator : public ValueObject {
2280 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002281 explicit HInstructionIterator(const HInstructionList& instructions)
2282 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002283 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002284 }
2285
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002286 bool Done() const { return instruction_ == nullptr; }
2287 HInstruction* Current() const { return instruction_; }
2288 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002289 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002290 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002291 }
2292
2293 private:
2294 HInstruction* instruction_;
2295 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002296
2297 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002298};
2299
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002300class HBackwardInstructionIterator : public ValueObject {
2301 public:
2302 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2303 : instruction_(instructions.last_instruction_) {
2304 next_ = Done() ? nullptr : instruction_->GetPrevious();
2305 }
2306
2307 bool Done() const { return instruction_ == nullptr; }
2308 HInstruction* Current() const { return instruction_; }
2309 void Advance() {
2310 instruction_ = next_;
2311 next_ = Done() ? nullptr : instruction_->GetPrevious();
2312 }
2313
2314 private:
2315 HInstruction* instruction_;
2316 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002317
2318 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002319};
2320
Vladimir Markof9f64412015-09-02 14:05:49 +01002321template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002322class HTemplateInstruction: public HInstruction {
2323 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002324 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002325 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002326 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002327
Vladimir Marko372f10e2016-05-17 16:30:10 +01002328 using HInstruction::GetInputRecords; // Keep the const version visible.
2329 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2330 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002331 }
2332
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002333 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002334 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002335
2336 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002337};
2338
Vladimir Markof9f64412015-09-02 14:05:49 +01002339// HTemplateInstruction specialization for N=0.
2340template<>
2341class HTemplateInstruction<0>: public HInstruction {
2342 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002343 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002344 : HInstruction(side_effects, dex_pc) {}
2345
Vladimir Markof9f64412015-09-02 14:05:49 +01002346 virtual ~HTemplateInstruction() {}
2347
Vladimir Marko372f10e2016-05-17 16:30:10 +01002348 using HInstruction::GetInputRecords; // Keep the const version visible.
2349 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2350 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002351 }
2352
2353 private:
2354 friend class SsaBuilder;
2355};
2356
Dave Allison20dfc792014-06-16 20:44:29 -07002357template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002358class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002359 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002360 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002361 : HTemplateInstruction<N>(side_effects, dex_pc) {
2362 this->template SetPackedField<TypeField>(type);
2363 }
Dave Allison20dfc792014-06-16 20:44:29 -07002364 virtual ~HExpression() {}
2365
Vladimir Markoa1de9182016-02-25 11:37:38 +00002366 Primitive::Type GetType() const OVERRIDE {
2367 return TypeField::Decode(this->GetPackedFields());
2368 }
Dave Allison20dfc792014-06-16 20:44:29 -07002369
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002370 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002371 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2372 static constexpr size_t kFieldTypeSize =
2373 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2374 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2375 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2376 "Too many packed fields.");
2377 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002378};
2379
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002380// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2381// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002382class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002383 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002384 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2385 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002386
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002387 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002388
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002389 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002390
2391 private:
2392 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2393};
2394
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002395// Represents dex's RETURN opcodes. A HReturn is a control flow
2396// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002397class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002398 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002399 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2400 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002401 SetRawInputAt(0, value);
2402 }
2403
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002404 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002405
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002406 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002407
2408 private:
2409 DISALLOW_COPY_AND_ASSIGN(HReturn);
2410};
2411
Vladimir Markofcb503c2016-05-18 12:48:17 +01002412class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002413 public:
2414 HPhi(ArenaAllocator* arena,
2415 uint32_t reg_number,
2416 size_t number_of_inputs,
2417 Primitive::Type type,
2418 uint32_t dex_pc = kNoDexPc)
2419 : HInstruction(SideEffects::None(), dex_pc),
2420 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2421 reg_number_(reg_number) {
2422 SetPackedField<TypeField>(ToPhiType(type));
2423 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2424 // Phis are constructed live and marked dead if conflicting or unused.
2425 // Individual steps of SsaBuilder should assume that if a phi has been
2426 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2427 SetPackedFlag<kFlagIsLive>(true);
2428 SetPackedFlag<kFlagCanBeNull>(true);
2429 }
2430
2431 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2432 static Primitive::Type ToPhiType(Primitive::Type type) {
2433 return Primitive::PrimitiveKind(type);
2434 }
2435
2436 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2437
Vladimir Marko372f10e2016-05-17 16:30:10 +01002438 using HInstruction::GetInputRecords; // Keep the const version visible.
2439 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2440 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2441 }
David Brazdildee58d62016-04-07 09:54:26 +00002442
2443 void AddInput(HInstruction* input);
2444 void RemoveInputAt(size_t index);
2445
2446 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2447 void SetType(Primitive::Type new_type) {
2448 // Make sure that only valid type changes occur. The following are allowed:
2449 // (1) int -> float/ref (primitive type propagation),
2450 // (2) long -> double (primitive type propagation).
2451 DCHECK(GetType() == new_type ||
2452 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2453 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2454 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2455 SetPackedField<TypeField>(new_type);
2456 }
2457
2458 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2459 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2460
2461 uint32_t GetRegNumber() const { return reg_number_; }
2462
2463 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2464 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2465 bool IsDead() const { return !IsLive(); }
2466 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2467
Vladimir Markoe9004912016-06-16 16:50:52 +01002468 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002469 return other != nullptr
2470 && other->IsPhi()
2471 && other->AsPhi()->GetBlock() == GetBlock()
2472 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2473 }
2474
2475 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2476 // An equivalent phi is a phi having the same dex register and type.
2477 // It assumes that phis with the same dex register are adjacent.
2478 HPhi* GetNextEquivalentPhiWithSameType() {
2479 HInstruction* next = GetNext();
2480 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2481 if (next->GetType() == GetType()) {
2482 return next->AsPhi();
2483 }
2484 next = next->GetNext();
2485 }
2486 return nullptr;
2487 }
2488
2489 DECLARE_INSTRUCTION(Phi);
2490
David Brazdildee58d62016-04-07 09:54:26 +00002491 private:
2492 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2493 static constexpr size_t kFieldTypeSize =
2494 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2495 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2496 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2497 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2498 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2499 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2500
Vladimir Marko372f10e2016-05-17 16:30:10 +01002501 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002502 const uint32_t reg_number_;
2503
2504 DISALLOW_COPY_AND_ASSIGN(HPhi);
2505};
2506
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002507// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002508// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002509// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002510class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002511 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002512 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002513
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002514 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002515
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002516 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002517
2518 private:
2519 DISALLOW_COPY_AND_ASSIGN(HExit);
2520};
2521
2522// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002523class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002524 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002525 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002526
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002527 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002528
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002529 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002530 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002531 }
2532
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002533 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002534
2535 private:
2536 DISALLOW_COPY_AND_ASSIGN(HGoto);
2537};
2538
Roland Levillain9867bc72015-08-05 10:21:34 +01002539class HConstant : public HExpression<0> {
2540 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002541 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2542 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002543
2544 bool CanBeMoved() const OVERRIDE { return true; }
2545
Roland Levillain1a653882016-03-18 18:05:57 +00002546 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002547 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002548 // Is this constant 0 in the arithmetic sense?
2549 virtual bool IsArithmeticZero() const { return false; }
2550 // Is this constant a 0-bit pattern?
2551 virtual bool IsZeroBitPattern() const { return false; }
2552 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002553 virtual bool IsOne() const { return false; }
2554
David Brazdil77a48ae2015-09-15 12:34:04 +00002555 virtual uint64_t GetValueAsUint64() const = 0;
2556
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002557 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002558
2559 private:
2560 DISALLOW_COPY_AND_ASSIGN(HConstant);
2561};
2562
Vladimir Markofcb503c2016-05-18 12:48:17 +01002563class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002564 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002565 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002566 return true;
2567 }
2568
David Brazdil77a48ae2015-09-15 12:34:04 +00002569 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2570
Roland Levillain9867bc72015-08-05 10:21:34 +01002571 size_t ComputeHashCode() const OVERRIDE { return 0; }
2572
Roland Levillain1a653882016-03-18 18:05:57 +00002573 // The null constant representation is a 0-bit pattern.
2574 virtual bool IsZeroBitPattern() const { return true; }
2575
Roland Levillain9867bc72015-08-05 10:21:34 +01002576 DECLARE_INSTRUCTION(NullConstant);
2577
2578 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002579 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002580
2581 friend class HGraph;
2582 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2583};
2584
2585// Constants of the type int. Those can be from Dex instructions, or
2586// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002587class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002588 public:
2589 int32_t GetValue() const { return value_; }
2590
David Brazdil9f389d42015-10-01 14:32:56 +01002591 uint64_t GetValueAsUint64() const OVERRIDE {
2592 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2593 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002594
Vladimir Marko372f10e2016-05-17 16:30:10 +01002595 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002596 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002597 return other->AsIntConstant()->value_ == value_;
2598 }
2599
2600 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2601
2602 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002603 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2604 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002605 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2606
Roland Levillain1a653882016-03-18 18:05:57 +00002607 // Integer constants are used to encode Boolean values as well,
2608 // where 1 means true and 0 means false.
2609 bool IsTrue() const { return GetValue() == 1; }
2610 bool IsFalse() const { return GetValue() == 0; }
2611
Roland Levillain9867bc72015-08-05 10:21:34 +01002612 DECLARE_INSTRUCTION(IntConstant);
2613
2614 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002615 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2616 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2617 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2618 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002619
2620 const int32_t value_;
2621
2622 friend class HGraph;
2623 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2624 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2625 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2626};
2627
Vladimir Markofcb503c2016-05-18 12:48:17 +01002628class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002629 public:
2630 int64_t GetValue() const { return value_; }
2631
David Brazdil77a48ae2015-09-15 12:34:04 +00002632 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2633
Vladimir Marko372f10e2016-05-17 16:30:10 +01002634 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002635 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002636 return other->AsLongConstant()->value_ == value_;
2637 }
2638
2639 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2640
2641 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002642 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2643 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002644 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2645
2646 DECLARE_INSTRUCTION(LongConstant);
2647
2648 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002649 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2650 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002651
2652 const int64_t value_;
2653
2654 friend class HGraph;
2655 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2656};
Dave Allison20dfc792014-06-16 20:44:29 -07002657
Vladimir Markofcb503c2016-05-18 12:48:17 +01002658class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002659 public:
2660 float GetValue() const { return value_; }
2661
2662 uint64_t GetValueAsUint64() const OVERRIDE {
2663 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2664 }
2665
Vladimir Marko372f10e2016-05-17 16:30:10 +01002666 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002667 DCHECK(other->IsFloatConstant()) << other->DebugName();
2668 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2669 }
2670
2671 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2672
2673 bool IsMinusOne() const OVERRIDE {
2674 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2675 }
Roland Levillain1a653882016-03-18 18:05:57 +00002676 bool IsArithmeticZero() const OVERRIDE {
2677 return std::fpclassify(value_) == FP_ZERO;
2678 }
2679 bool IsArithmeticPositiveZero() const {
2680 return IsArithmeticZero() && !std::signbit(value_);
2681 }
2682 bool IsArithmeticNegativeZero() const {
2683 return IsArithmeticZero() && std::signbit(value_);
2684 }
2685 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002686 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002687 }
2688 bool IsOne() const OVERRIDE {
2689 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2690 }
2691 bool IsNaN() const {
2692 return std::isnan(value_);
2693 }
2694
2695 DECLARE_INSTRUCTION(FloatConstant);
2696
2697 private:
2698 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2699 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2700 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2701 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2702
2703 const float value_;
2704
2705 // Only the SsaBuilder and HGraph can create floating-point constants.
2706 friend class SsaBuilder;
2707 friend class HGraph;
2708 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2709};
2710
Vladimir Markofcb503c2016-05-18 12:48:17 +01002711class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002712 public:
2713 double GetValue() const { return value_; }
2714
2715 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2716
Vladimir Marko372f10e2016-05-17 16:30:10 +01002717 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002718 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2719 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2720 }
2721
2722 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2723
2724 bool IsMinusOne() const OVERRIDE {
2725 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2726 }
Roland Levillain1a653882016-03-18 18:05:57 +00002727 bool IsArithmeticZero() const OVERRIDE {
2728 return std::fpclassify(value_) == FP_ZERO;
2729 }
2730 bool IsArithmeticPositiveZero() const {
2731 return IsArithmeticZero() && !std::signbit(value_);
2732 }
2733 bool IsArithmeticNegativeZero() const {
2734 return IsArithmeticZero() && std::signbit(value_);
2735 }
2736 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002737 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002738 }
2739 bool IsOne() const OVERRIDE {
2740 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2741 }
2742 bool IsNaN() const {
2743 return std::isnan(value_);
2744 }
2745
2746 DECLARE_INSTRUCTION(DoubleConstant);
2747
2748 private:
2749 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2750 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2751 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2752 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2753
2754 const double value_;
2755
2756 // Only the SsaBuilder and HGraph can create floating-point constants.
2757 friend class SsaBuilder;
2758 friend class HGraph;
2759 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2760};
2761
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002762// Conditional branch. A block ending with an HIf instruction must have
2763// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002764class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002765 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002766 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2767 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002768 SetRawInputAt(0, input);
2769 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002770
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002771 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002772
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002773 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002774 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002775 }
2776
2777 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002778 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002779 }
2780
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002781 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002782
2783 private:
2784 DISALLOW_COPY_AND_ASSIGN(HIf);
2785};
2786
David Brazdilfc6a86a2015-06-26 10:33:45 +00002787
2788// Abstract instruction which marks the beginning and/or end of a try block and
2789// links it to the respective exception handlers. Behaves the same as a Goto in
2790// non-exceptional control flow.
2791// Normal-flow successor is stored at index zero, exception handlers under
2792// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002793class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002794 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002795 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002796 kEntry,
2797 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002798 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002799 };
2800
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002801 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002802 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2803 SetPackedField<BoundaryKindField>(kind);
2804 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002805
2806 bool IsControlFlow() const OVERRIDE { return true; }
2807
2808 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002809 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002810
David Brazdild26a4112015-11-10 11:07:31 +00002811 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2812 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2813 }
2814
David Brazdilfc6a86a2015-06-26 10:33:45 +00002815 // Returns whether `handler` is among its exception handlers (non-zero index
2816 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002817 bool HasExceptionHandler(const HBasicBlock& handler) const {
2818 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002819 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002820 }
2821
2822 // If not present already, adds `handler` to its block's list of exception
2823 // handlers.
2824 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002825 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002826 GetBlock()->AddSuccessor(handler);
2827 }
2828 }
2829
Vladimir Markoa1de9182016-02-25 11:37:38 +00002830 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2831 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002832
David Brazdilffee3d32015-07-06 11:48:53 +01002833 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2834
David Brazdilfc6a86a2015-06-26 10:33:45 +00002835 DECLARE_INSTRUCTION(TryBoundary);
2836
2837 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002838 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2839 static constexpr size_t kFieldBoundaryKindSize =
2840 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2841 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2842 kFieldBoundaryKind + kFieldBoundaryKindSize;
2843 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2844 "Too many packed fields.");
2845 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002846
2847 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2848};
2849
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002850// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002851class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002852 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002853 // We set CanTriggerGC to prevent any intermediate address to be live
2854 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002855 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002856 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002857 SetRawInputAt(0, cond);
2858 }
2859
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002860 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002861 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002862 return true;
2863 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002864 bool NeedsEnvironment() const OVERRIDE { return true; }
2865 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002866
2867 DECLARE_INSTRUCTION(Deoptimize);
2868
2869 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002870 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2871};
2872
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002873// Represents the ArtMethod that was passed as a first argument to
2874// the method. It is used by instructions that depend on it, like
2875// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002876class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002877 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002878 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2879 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002880
2881 DECLARE_INSTRUCTION(CurrentMethod);
2882
2883 private:
2884 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2885};
2886
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002887// Fetches an ArtMethod from the virtual table or the interface method table
2888// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002889class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002890 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002891 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002892 kVTable,
2893 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002894 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002895 };
2896 HClassTableGet(HInstruction* cls,
2897 Primitive::Type type,
2898 TableKind kind,
2899 size_t index,
2900 uint32_t dex_pc)
2901 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002902 index_(index) {
2903 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002904 SetRawInputAt(0, cls);
2905 }
2906
2907 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002908 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002909 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002910 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002911 }
2912
Vladimir Markoa1de9182016-02-25 11:37:38 +00002913 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002914 size_t GetIndex() const { return index_; }
2915
2916 DECLARE_INSTRUCTION(ClassTableGet);
2917
2918 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002919 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2920 static constexpr size_t kFieldTableKindSize =
2921 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2922 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2923 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2924 "Too many packed fields.");
2925 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2926
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002927 // The index of the ArtMethod in the table.
2928 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002929
2930 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2931};
2932
Mark Mendellfe57faa2015-09-18 09:26:15 -04002933// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2934// have one successor for each entry in the switch table, and the final successor
2935// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002936class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002937 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002938 HPackedSwitch(int32_t start_value,
2939 uint32_t num_entries,
2940 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002941 uint32_t dex_pc = kNoDexPc)
2942 : HTemplateInstruction(SideEffects::None(), dex_pc),
2943 start_value_(start_value),
2944 num_entries_(num_entries) {
2945 SetRawInputAt(0, input);
2946 }
2947
2948 bool IsControlFlow() const OVERRIDE { return true; }
2949
2950 int32_t GetStartValue() const { return start_value_; }
2951
Vladimir Marko211c2112015-09-24 16:52:33 +01002952 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002953
2954 HBasicBlock* GetDefaultBlock() const {
2955 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002956 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002957 }
2958 DECLARE_INSTRUCTION(PackedSwitch);
2959
2960 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002961 const int32_t start_value_;
2962 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002963
2964 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2965};
2966
Roland Levillain88cb1752014-10-20 16:36:47 +01002967class HUnaryOperation : public HExpression<1> {
2968 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002969 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2970 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002971 SetRawInputAt(0, input);
2972 }
2973
2974 HInstruction* GetInput() const { return InputAt(0); }
2975 Primitive::Type GetResultType() const { return GetType(); }
2976
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002977 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002978 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002979 return true;
2980 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002981
Roland Levillain31dd3d62016-02-16 12:21:02 +00002982 // Try to statically evaluate `this` and return a HConstant
2983 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002984 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002985 HConstant* TryStaticEvaluation() const;
2986
2987 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002988 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2989 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002990 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2991 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002992
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002993 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002994
2995 private:
2996 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2997};
2998
Dave Allison20dfc792014-06-16 20:44:29 -07002999class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003000 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003001 HBinaryOperation(Primitive::Type result_type,
3002 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003003 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003004 SideEffects side_effects = SideEffects::None(),
3005 uint32_t dex_pc = kNoDexPc)
3006 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003007 SetRawInputAt(0, left);
3008 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003009 }
3010
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003011 HInstruction* GetLeft() const { return InputAt(0); }
3012 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003013 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003014
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003015 virtual bool IsCommutative() const { return false; }
3016
3017 // Put constant on the right.
3018 // Returns whether order is changed.
3019 bool OrderInputsWithConstantOnTheRight() {
3020 HInstruction* left = InputAt(0);
3021 HInstruction* right = InputAt(1);
3022 if (left->IsConstant() && !right->IsConstant()) {
3023 ReplaceInput(right, 0);
3024 ReplaceInput(left, 1);
3025 return true;
3026 }
3027 return false;
3028 }
3029
3030 // Order inputs by instruction id, but favor constant on the right side.
3031 // This helps GVN for commutative ops.
3032 void OrderInputs() {
3033 DCHECK(IsCommutative());
3034 HInstruction* left = InputAt(0);
3035 HInstruction* right = InputAt(1);
3036 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3037 return;
3038 }
3039 if (OrderInputsWithConstantOnTheRight()) {
3040 return;
3041 }
3042 // Order according to instruction id.
3043 if (left->GetId() > right->GetId()) {
3044 ReplaceInput(right, 0);
3045 ReplaceInput(left, 1);
3046 }
3047 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003048
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003049 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003050 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003051 return true;
3052 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003053
Roland Levillain31dd3d62016-02-16 12:21:02 +00003054 // Try to statically evaluate `this` and return a HConstant
3055 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003056 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003057 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003058
3059 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003060 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3061 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003062 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3063 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003064 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003065 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3066 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003067 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3068 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003069 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3070 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003071 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003072 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3073 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003074
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003075 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003076 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003077 HConstant* GetConstantRight() const;
3078
3079 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003080 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003081 HInstruction* GetLeastConstantLeft() const;
3082
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003083 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003084
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003085 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003086 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3087};
3088
Mark Mendellc4701932015-04-10 13:18:51 -04003089// The comparison bias applies for floating point operations and indicates how NaN
3090// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003091enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003092 kNoBias, // bias is not applicable (i.e. for long operation)
3093 kGtBias, // return 1 for NaN comparisons
3094 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003095 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003096};
3097
Roland Levillain31dd3d62016-02-16 12:21:02 +00003098std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3099
Dave Allison20dfc792014-06-16 20:44:29 -07003100class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003101 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003102 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003103 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3104 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3105 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003106
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003107 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003108 // `instruction`, and disregard moves in between.
3109 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003110
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003111 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003112
3113 virtual IfCondition GetCondition() const = 0;
3114
Mark Mendellc4701932015-04-10 13:18:51 -04003115 virtual IfCondition GetOppositeCondition() const = 0;
3116
Vladimir Markoa1de9182016-02-25 11:37:38 +00003117 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003118 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3119
Vladimir Markoa1de9182016-02-25 11:37:38 +00003120 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3121 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003122
Vladimir Marko372f10e2016-05-17 16:30:10 +01003123 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003124 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003125 }
3126
Roland Levillain4fa13f62015-07-06 18:11:54 +01003127 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003128 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003129 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003130 if (if_cond == kCondNE) {
3131 return true;
3132 } else if (if_cond == kCondEQ) {
3133 return false;
3134 }
3135 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003136 }
3137
3138 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003139 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003140 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003141 if (if_cond == kCondEQ) {
3142 return true;
3143 } else if (if_cond == kCondNE) {
3144 return false;
3145 }
3146 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003147 }
3148
Roland Levillain31dd3d62016-02-16 12:21:02 +00003149 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003150 // Needed if we merge a HCompare into a HCondition.
3151 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3152 static constexpr size_t kFieldComparisonBiasSize =
3153 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3154 static constexpr size_t kNumberOfConditionPackedBits =
3155 kFieldComparisonBias + kFieldComparisonBiasSize;
3156 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3157 using ComparisonBiasField =
3158 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3159
Roland Levillain31dd3d62016-02-16 12:21:02 +00003160 template <typename T>
3161 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3162
3163 template <typename T>
3164 int32_t CompareFP(T x, T y) const {
3165 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3166 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3167 // Handle the bias.
3168 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3169 }
3170
3171 // Return an integer constant containing the result of a condition evaluated at compile time.
3172 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3173 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3174 }
3175
Dave Allison20dfc792014-06-16 20:44:29 -07003176 private:
3177 DISALLOW_COPY_AND_ASSIGN(HCondition);
3178};
3179
3180// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003181class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003182 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003183 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3184 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003185
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003186 bool IsCommutative() const OVERRIDE { return true; }
3187
Vladimir Marko9e23df52015-11-10 17:14:35 +00003188 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3189 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003190 return MakeConstantCondition(true, GetDexPc());
3191 }
3192 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3193 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3194 }
3195 // In the following Evaluate methods, a HCompare instruction has
3196 // been merged into this HEqual instruction; evaluate it as
3197 // `Compare(x, y) == 0`.
3198 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3199 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3200 GetDexPc());
3201 }
3202 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3203 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3204 }
3205 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3206 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003207 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003208
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003209 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003210
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003211 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003212 return kCondEQ;
3213 }
3214
Mark Mendellc4701932015-04-10 13:18:51 -04003215 IfCondition GetOppositeCondition() const OVERRIDE {
3216 return kCondNE;
3217 }
3218
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003219 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003220 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003221
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003222 DISALLOW_COPY_AND_ASSIGN(HEqual);
3223};
3224
Vladimir Markofcb503c2016-05-18 12:48:17 +01003225class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003226 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003227 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3228 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003229
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003230 bool IsCommutative() const OVERRIDE { return true; }
3231
Vladimir Marko9e23df52015-11-10 17:14:35 +00003232 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3233 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003234 return MakeConstantCondition(false, GetDexPc());
3235 }
3236 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3237 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3238 }
3239 // In the following Evaluate methods, a HCompare instruction has
3240 // been merged into this HNotEqual instruction; evaluate it as
3241 // `Compare(x, y) != 0`.
3242 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3243 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3244 }
3245 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3246 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3247 }
3248 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3249 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003250 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003251
Dave Allison20dfc792014-06-16 20:44:29 -07003252 DECLARE_INSTRUCTION(NotEqual);
3253
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003254 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003255 return kCondNE;
3256 }
3257
Mark Mendellc4701932015-04-10 13:18:51 -04003258 IfCondition GetOppositeCondition() const OVERRIDE {
3259 return kCondEQ;
3260 }
3261
Dave Allison20dfc792014-06-16 20:44:29 -07003262 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003263 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003264
Dave Allison20dfc792014-06-16 20:44:29 -07003265 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3266};
3267
Vladimir Markofcb503c2016-05-18 12:48:17 +01003268class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003269 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003270 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3271 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003272
Roland Levillain9867bc72015-08-05 10:21:34 +01003273 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003274 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003275 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003276 // In the following Evaluate methods, a HCompare instruction has
3277 // been merged into this HLessThan instruction; evaluate it as
3278 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003279 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003280 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3281 }
3282 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3283 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3284 }
3285 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3286 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003287 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003288
Dave Allison20dfc792014-06-16 20:44:29 -07003289 DECLARE_INSTRUCTION(LessThan);
3290
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003291 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003292 return kCondLT;
3293 }
3294
Mark Mendellc4701932015-04-10 13:18:51 -04003295 IfCondition GetOppositeCondition() const OVERRIDE {
3296 return kCondGE;
3297 }
3298
Dave Allison20dfc792014-06-16 20:44:29 -07003299 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003300 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003301
Dave Allison20dfc792014-06-16 20:44:29 -07003302 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3303};
3304
Vladimir Markofcb503c2016-05-18 12:48:17 +01003305class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003306 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003307 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3308 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003309
Roland Levillain9867bc72015-08-05 10:21:34 +01003310 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003311 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003312 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003313 // In the following Evaluate methods, a HCompare instruction has
3314 // been merged into this HLessThanOrEqual instruction; evaluate it as
3315 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003316 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003317 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3318 }
3319 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3320 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3321 }
3322 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3323 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003324 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003325
Dave Allison20dfc792014-06-16 20:44:29 -07003326 DECLARE_INSTRUCTION(LessThanOrEqual);
3327
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003328 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003329 return kCondLE;
3330 }
3331
Mark Mendellc4701932015-04-10 13:18:51 -04003332 IfCondition GetOppositeCondition() const OVERRIDE {
3333 return kCondGT;
3334 }
3335
Dave Allison20dfc792014-06-16 20:44:29 -07003336 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003337 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003338
Dave Allison20dfc792014-06-16 20:44:29 -07003339 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3340};
3341
Vladimir Markofcb503c2016-05-18 12:48:17 +01003342class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003343 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003344 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3345 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003346
Roland Levillain9867bc72015-08-05 10:21:34 +01003347 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003348 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003349 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003350 // In the following Evaluate methods, a HCompare instruction has
3351 // been merged into this HGreaterThan instruction; evaluate it as
3352 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003353 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003354 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3355 }
3356 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3357 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3358 }
3359 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3360 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003361 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003362
Dave Allison20dfc792014-06-16 20:44:29 -07003363 DECLARE_INSTRUCTION(GreaterThan);
3364
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003365 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003366 return kCondGT;
3367 }
3368
Mark Mendellc4701932015-04-10 13:18:51 -04003369 IfCondition GetOppositeCondition() const OVERRIDE {
3370 return kCondLE;
3371 }
3372
Dave Allison20dfc792014-06-16 20:44:29 -07003373 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003374 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003375
Dave Allison20dfc792014-06-16 20:44:29 -07003376 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3377};
3378
Vladimir Markofcb503c2016-05-18 12:48:17 +01003379class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003380 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003381 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3382 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003383
Roland Levillain9867bc72015-08-05 10:21:34 +01003384 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003385 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003386 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003387 // In the following Evaluate methods, a HCompare instruction has
3388 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3389 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003390 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003391 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3392 }
3393 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3394 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3395 }
3396 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3397 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003398 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003399
Dave Allison20dfc792014-06-16 20:44:29 -07003400 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3401
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003402 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003403 return kCondGE;
3404 }
3405
Mark Mendellc4701932015-04-10 13:18:51 -04003406 IfCondition GetOppositeCondition() const OVERRIDE {
3407 return kCondLT;
3408 }
3409
Dave Allison20dfc792014-06-16 20:44:29 -07003410 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003411 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003412
Dave Allison20dfc792014-06-16 20:44:29 -07003413 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3414};
3415
Vladimir Markofcb503c2016-05-18 12:48:17 +01003416class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003417 public:
3418 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3419 : HCondition(first, second, dex_pc) {}
3420
3421 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003422 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003423 }
3424 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003425 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3426 }
3427 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3428 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3429 LOG(FATAL) << DebugName() << " is not defined for float values";
3430 UNREACHABLE();
3431 }
3432 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3433 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3434 LOG(FATAL) << DebugName() << " is not defined for double values";
3435 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003436 }
3437
3438 DECLARE_INSTRUCTION(Below);
3439
3440 IfCondition GetCondition() const OVERRIDE {
3441 return kCondB;
3442 }
3443
3444 IfCondition GetOppositeCondition() const OVERRIDE {
3445 return kCondAE;
3446 }
3447
3448 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003449 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003450 return MakeUnsigned(x) < MakeUnsigned(y);
3451 }
Aart Bike9f37602015-10-09 11:15:55 -07003452
3453 DISALLOW_COPY_AND_ASSIGN(HBelow);
3454};
3455
Vladimir Markofcb503c2016-05-18 12:48:17 +01003456class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003457 public:
3458 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3459 : HCondition(first, second, dex_pc) {}
3460
3461 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003462 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003463 }
3464 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003465 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3466 }
3467 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3468 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3469 LOG(FATAL) << DebugName() << " is not defined for float values";
3470 UNREACHABLE();
3471 }
3472 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3473 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3474 LOG(FATAL) << DebugName() << " is not defined for double values";
3475 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003476 }
3477
3478 DECLARE_INSTRUCTION(BelowOrEqual);
3479
3480 IfCondition GetCondition() const OVERRIDE {
3481 return kCondBE;
3482 }
3483
3484 IfCondition GetOppositeCondition() const OVERRIDE {
3485 return kCondA;
3486 }
3487
3488 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003489 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003490 return MakeUnsigned(x) <= MakeUnsigned(y);
3491 }
Aart Bike9f37602015-10-09 11:15:55 -07003492
3493 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3494};
3495
Vladimir Markofcb503c2016-05-18 12:48:17 +01003496class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003497 public:
3498 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3499 : HCondition(first, second, dex_pc) {}
3500
3501 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003502 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003503 }
3504 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003505 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3506 }
3507 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3508 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3509 LOG(FATAL) << DebugName() << " is not defined for float values";
3510 UNREACHABLE();
3511 }
3512 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3513 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3514 LOG(FATAL) << DebugName() << " is not defined for double values";
3515 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003516 }
3517
3518 DECLARE_INSTRUCTION(Above);
3519
3520 IfCondition GetCondition() const OVERRIDE {
3521 return kCondA;
3522 }
3523
3524 IfCondition GetOppositeCondition() const OVERRIDE {
3525 return kCondBE;
3526 }
3527
3528 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003529 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003530 return MakeUnsigned(x) > MakeUnsigned(y);
3531 }
Aart Bike9f37602015-10-09 11:15:55 -07003532
3533 DISALLOW_COPY_AND_ASSIGN(HAbove);
3534};
3535
Vladimir Markofcb503c2016-05-18 12:48:17 +01003536class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003537 public:
3538 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3539 : HCondition(first, second, dex_pc) {}
3540
3541 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003542 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003543 }
3544 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003545 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3546 }
3547 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3548 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3549 LOG(FATAL) << DebugName() << " is not defined for float values";
3550 UNREACHABLE();
3551 }
3552 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3553 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3554 LOG(FATAL) << DebugName() << " is not defined for double values";
3555 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003556 }
3557
3558 DECLARE_INSTRUCTION(AboveOrEqual);
3559
3560 IfCondition GetCondition() const OVERRIDE {
3561 return kCondAE;
3562 }
3563
3564 IfCondition GetOppositeCondition() const OVERRIDE {
3565 return kCondB;
3566 }
3567
3568 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003569 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003570 return MakeUnsigned(x) >= MakeUnsigned(y);
3571 }
Aart Bike9f37602015-10-09 11:15:55 -07003572
3573 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3574};
Dave Allison20dfc792014-06-16 20:44:29 -07003575
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003576// Instruction to check how two inputs compare to each other.
3577// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003578class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003579 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003580 // Note that `comparison_type` is the type of comparison performed
3581 // between the comparison's inputs, not the type of the instantiated
3582 // HCompare instruction (which is always Primitive::kPrimInt).
3583 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003584 HInstruction* first,
3585 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003586 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003587 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003588 : HBinaryOperation(Primitive::kPrimInt,
3589 first,
3590 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003591 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003592 dex_pc) {
3593 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003594 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3595 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003596 }
3597
Roland Levillain9867bc72015-08-05 10:21:34 +01003598 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003599 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3600
3601 template <typename T>
3602 int32_t ComputeFP(T x, T y) const {
3603 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3604 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3605 // Handle the bias.
3606 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3607 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003608
Roland Levillain9867bc72015-08-05 10:21:34 +01003609 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003610 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3611 // reach this code path when processing a freshly built HIR
3612 // graph. However HCompare integer instructions can be synthesized
3613 // by the instruction simplifier to implement IntegerCompare and
3614 // IntegerSignum intrinsics, so we have to handle this case.
3615 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003616 }
3617 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003618 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3619 }
3620 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3621 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3622 }
3623 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3624 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003625 }
3626
Vladimir Marko372f10e2016-05-17 16:30:10 +01003627 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003628 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003629 }
3630
Vladimir Markoa1de9182016-02-25 11:37:38 +00003631 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003632
Roland Levillain31dd3d62016-02-16 12:21:02 +00003633 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003634 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003635 bool IsGtBias() const {
3636 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003637 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003638 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003639
Roland Levillain1693a1f2016-03-15 14:57:31 +00003640 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3641 // Comparisons do not require a runtime call in any back end.
3642 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003643 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003644
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003645 DECLARE_INSTRUCTION(Compare);
3646
Roland Levillain31dd3d62016-02-16 12:21:02 +00003647 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003648 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3649 static constexpr size_t kFieldComparisonBiasSize =
3650 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3651 static constexpr size_t kNumberOfComparePackedBits =
3652 kFieldComparisonBias + kFieldComparisonBiasSize;
3653 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3654 using ComparisonBiasField =
3655 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3656
Roland Levillain31dd3d62016-02-16 12:21:02 +00003657 // Return an integer constant containing the result of a comparison evaluated at compile time.
3658 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3659 DCHECK(value == -1 || value == 0 || value == 1) << value;
3660 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3661 }
3662
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003663 private:
3664 DISALLOW_COPY_AND_ASSIGN(HCompare);
3665};
3666
Vladimir Markofcb503c2016-05-18 12:48:17 +01003667class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003668 public:
3669 HNewInstance(HInstruction* cls,
3670 HCurrentMethod* current_method,
3671 uint32_t dex_pc,
3672 uint16_t type_index,
3673 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003674 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003675 bool finalizable,
3676 QuickEntrypointEnum entrypoint)
3677 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3678 type_index_(type_index),
3679 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003680 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003681 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003682 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003683 SetRawInputAt(0, cls);
3684 SetRawInputAt(1, current_method);
3685 }
3686
3687 uint16_t GetTypeIndex() const { return type_index_; }
3688 const DexFile& GetDexFile() const { return dex_file_; }
3689
3690 // Calls runtime so needs an environment.
3691 bool NeedsEnvironment() const OVERRIDE { return true; }
3692
Mingyao Yang062157f2016-03-02 10:15:36 -08003693 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3694 bool CanThrow() const OVERRIDE { return true; }
3695
3696 // Needs to call into runtime to make sure it's instantiable/accessible.
3697 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003698
Vladimir Markoa1de9182016-02-25 11:37:38 +00003699 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003700
3701 bool CanBeNull() const OVERRIDE { return false; }
3702
3703 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3704
3705 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3706 entrypoint_ = entrypoint;
3707 }
3708
3709 bool IsStringAlloc() const;
3710
3711 DECLARE_INSTRUCTION(NewInstance);
3712
3713 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003714 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3715 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003716 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3717 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3718 "Too many packed fields.");
3719
David Brazdil6de19382016-01-08 17:37:10 +00003720 const uint16_t type_index_;
3721 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003722 QuickEntrypointEnum entrypoint_;
3723
3724 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3725};
3726
Agi Csaki05f20562015-08-19 14:58:14 -07003727enum IntrinsicNeedsEnvironmentOrCache {
3728 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3729 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003730};
3731
Aart Bik5d75afe2015-12-14 11:57:01 -08003732enum IntrinsicSideEffects {
3733 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3734 kReadSideEffects, // Intrinsic may read heap memory.
3735 kWriteSideEffects, // Intrinsic may write heap memory.
3736 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3737};
3738
3739enum IntrinsicExceptions {
3740 kNoThrow, // Intrinsic does not throw any exceptions.
3741 kCanThrow // Intrinsic may throw exceptions.
3742};
3743
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003744class HInvoke : public HInstruction {
3745 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003746 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003747
Vladimir Marko372f10e2016-05-17 16:30:10 +01003748 using HInstruction::GetInputRecords; // Keep the const version visible.
3749 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3750 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3751 }
3752
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003753 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003754 SetRawInputAt(index, argument);
3755 }
3756
Roland Levillain3e3d7332015-04-28 11:00:54 +01003757 // Return the number of arguments. This number can be lower than
3758 // the number of inputs returned by InputCount(), as some invoke
3759 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3760 // inputs at the end of their list of inputs.
3761 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3762
Vladimir Markoa1de9182016-02-25 11:37:38 +00003763 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003764
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003765 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003766 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003767
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003768 InvokeType GetInvokeType() const {
3769 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003770 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003771
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003772 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003773 return intrinsic_;
3774 }
3775
Aart Bik5d75afe2015-12-14 11:57:01 -08003776 void SetIntrinsic(Intrinsics intrinsic,
3777 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3778 IntrinsicSideEffects side_effects,
3779 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003780
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003781 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003782 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003783 }
3784
Vladimir Markoa1de9182016-02-25 11:37:38 +00003785 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003786
3787 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3788
Vladimir Marko372f10e2016-05-17 16:30:10 +01003789 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003790 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3791 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003792
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003793 uint32_t* GetIntrinsicOptimizations() {
3794 return &intrinsic_optimizations_;
3795 }
3796
3797 const uint32_t* GetIntrinsicOptimizations() const {
3798 return &intrinsic_optimizations_;
3799 }
3800
3801 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3802
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003803 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3804
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003805 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003806
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003807 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003808 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3809 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003810 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3811 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003812 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003813 static constexpr size_t kFieldReturnTypeSize =
3814 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3815 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3816 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3817 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003818 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003819 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3820
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003821 HInvoke(ArenaAllocator* arena,
3822 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003823 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003824 Primitive::Type return_type,
3825 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003826 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003827 ArtMethod* resolved_method,
3828 InvokeType invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003829 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003830 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003831 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003832 resolved_method_(resolved_method),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003833 inputs_(number_of_arguments + number_of_other_inputs,
3834 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003835 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003836 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003837 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003838 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003839 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003840 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003841 }
3842
Vladimir Markoa1de9182016-02-25 11:37:38 +00003843 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003844
Roland Levillain3e3d7332015-04-28 11:00:54 +01003845 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003846 ArtMethod* const resolved_method_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003847 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003848 const uint32_t dex_method_index_;
3849 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003850
3851 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3852 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003853
3854 private:
3855 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3856};
3857
Vladimir Markofcb503c2016-05-18 12:48:17 +01003858class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003859 public:
3860 HInvokeUnresolved(ArenaAllocator* arena,
3861 uint32_t number_of_arguments,
3862 Primitive::Type return_type,
3863 uint32_t dex_pc,
3864 uint32_t dex_method_index,
3865 InvokeType invoke_type)
3866 : HInvoke(arena,
3867 number_of_arguments,
3868 0u /* number_of_other_inputs */,
3869 return_type,
3870 dex_pc,
3871 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003872 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003873 invoke_type) {
3874 }
3875
3876 DECLARE_INSTRUCTION(InvokeUnresolved);
3877
3878 private:
3879 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3880};
3881
Vladimir Markofcb503c2016-05-18 12:48:17 +01003882class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003883 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003884 // Requirements of this method call regarding the class
3885 // initialization (clinit) check of its declaring class.
3886 enum class ClinitCheckRequirement {
3887 kNone, // Class already initialized.
3888 kExplicit, // Static call having explicit clinit check as last input.
3889 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003890 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003891 };
3892
Vladimir Marko58155012015-08-19 12:49:41 +00003893 // Determines how to load the target ArtMethod*.
3894 enum class MethodLoadKind {
3895 // Use a String init ArtMethod* loaded from Thread entrypoints.
3896 kStringInit,
3897
3898 // Use the method's own ArtMethod* loaded by the register allocator.
3899 kRecursive,
3900
3901 // Use ArtMethod* at a known address, embed the direct address in the code.
3902 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3903 kDirectAddress,
3904
3905 // Use ArtMethod* at an address that will be known at link time, embed the direct
3906 // address in the code. If the image is relocatable, emit .patch_oat entry.
3907 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3908 // the image relocatable or not.
3909 kDirectAddressWithFixup,
3910
Vladimir Markoa1de9182016-02-25 11:37:38 +00003911 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003912 // Used when we need to use the dex cache, for example for invoke-static that
3913 // may cause class initialization (the entry may point to a resolution method),
3914 // and we know that we can access the dex cache arrays using a PC-relative load.
3915 kDexCachePcRelative,
3916
3917 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3918 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3919 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3920 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3921 kDexCacheViaMethod,
3922 };
3923
3924 // Determines the location of the code pointer.
3925 enum class CodePtrLocation {
3926 // Recursive call, use local PC-relative call instruction.
3927 kCallSelf,
3928
3929 // Use PC-relative call instruction patched at link time.
3930 // Used for calls within an oat file, boot->boot or app->app.
3931 kCallPCRelative,
3932
3933 // Call to a known target address, embed the direct address in code.
3934 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3935 kCallDirect,
3936
3937 // Call to a target address that will be known at link time, embed the direct
3938 // address in code. If the image is relocatable, emit .patch_oat entry.
3939 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3940 // the image relocatable or not.
3941 kCallDirectWithFixup,
3942
3943 // Use code pointer from the ArtMethod*.
3944 // Used when we don't know the target code. This is also the last-resort-kind used when
3945 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3946 kCallArtMethod,
3947 };
3948
3949 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003950 MethodLoadKind method_load_kind;
3951 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003952 // The method load data holds
3953 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3954 // Note that there are multiple string init methods, each having its own offset.
3955 // - the method address for kDirectAddress
3956 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003957 uint64_t method_load_data;
3958 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003959 };
3960
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003961 HInvokeStaticOrDirect(ArenaAllocator* arena,
3962 uint32_t number_of_arguments,
3963 Primitive::Type return_type,
3964 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003965 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003966 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00003967 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003968 InvokeType invoke_type,
3969 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003970 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003971 : HInvoke(arena,
3972 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003973 // There is potentially one extra argument for the HCurrentMethod node, and
3974 // potentially one other if the clinit check is explicit, and potentially
3975 // one other if the method is a string factory.
3976 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003977 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003978 return_type,
3979 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003980 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003981 resolved_method,
3982 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003983 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003984 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003985 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3986 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003987
Vladimir Markodc151b22015-10-15 18:02:30 +01003988 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003989 bool had_current_method_input = HasCurrentMethodInput();
3990 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3991
3992 // Using the current method is the default and once we find a better
3993 // method load kind, we should not go back to using the current method.
3994 DCHECK(had_current_method_input || !needs_current_method_input);
3995
3996 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003997 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3998 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003999 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004000 dispatch_info_ = dispatch_info;
4001 }
4002
Vladimir Markoc53c0792015-11-19 15:48:33 +00004003 void AddSpecialInput(HInstruction* input) {
4004 // We allow only one special input.
4005 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4006 DCHECK(InputCount() == GetSpecialInputIndex() ||
4007 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4008 InsertInputAt(GetSpecialInputIndex(), input);
4009 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004010
Vladimir Marko372f10e2016-05-17 16:30:10 +01004011 using HInstruction::GetInputRecords; // Keep the const version visible.
4012 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4013 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4014 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4015 DCHECK(!input_records.empty());
4016 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4017 HInstruction* last_input = input_records.back().GetInstruction();
4018 // Note: `last_input` may be null during arguments setup.
4019 if (last_input != nullptr) {
4020 // `last_input` is the last input of a static invoke marked as having
4021 // an explicit clinit check. It must either be:
4022 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4023 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4024 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4025 }
4026 }
4027 return input_records;
4028 }
4029
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004030 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004031 // We access the method via the dex cache so we can't do an implicit null check.
4032 // TODO: for intrinsics we can generate implicit null checks.
4033 return false;
4034 }
4035
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004036 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004037 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004038 }
4039
Vladimir Markoc53c0792015-11-19 15:48:33 +00004040 // Get the index of the special input, if any.
4041 //
David Brazdil6de19382016-01-08 17:37:10 +00004042 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4043 // method pointer; otherwise there may be one platform-specific special input,
4044 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004045 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004046 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004047
Vladimir Marko58155012015-08-19 12:49:41 +00004048 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4049 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4050 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004051 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004052 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004053 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004054 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004055 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4056 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004057 bool HasCurrentMethodInput() const {
4058 // This function can be called only after the invoke has been fully initialized by the builder.
4059 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004060 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004061 return true;
4062 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004063 DCHECK(InputCount() == GetSpecialInputIndex() ||
4064 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004065 return false;
4066 }
4067 }
Vladimir Marko58155012015-08-19 12:49:41 +00004068 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
Vladimir Marko58155012015-08-19 12:49:41 +00004069
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004070 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004071 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004072 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004073 }
4074
4075 uint64_t GetMethodAddress() const {
4076 DCHECK(HasMethodAddress());
4077 return dispatch_info_.method_load_data;
4078 }
4079
4080 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004081 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004082 return dispatch_info_.method_load_data;
4083 }
4084
4085 uint64_t GetDirectCodePtr() const {
4086 DCHECK(HasDirectCodePtr());
4087 return dispatch_info_.direct_code_ptr;
4088 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004089
Vladimir Markoa1de9182016-02-25 11:37:38 +00004090 ClinitCheckRequirement GetClinitCheckRequirement() const {
4091 return GetPackedField<ClinitCheckRequirementField>();
4092 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004093
Roland Levillain4c0eb422015-04-24 16:43:49 +01004094 // Is this instruction a call to a static method?
4095 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004096 return GetInvokeType() == kStatic;
4097 }
4098
4099 MethodReference GetTargetMethod() const {
4100 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004101 }
4102
Vladimir Markofbb184a2015-11-13 14:47:00 +00004103 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4104 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4105 // instruction; only relevant for static calls with explicit clinit check.
4106 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004107 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004108 size_t last_input_index = inputs_.size() - 1u;
4109 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004110 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004111 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004112 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004113 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004114 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004115 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004116 }
4117
4118 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004119 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004120 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004121 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004122 }
4123
4124 // Is this a call to a static method whose declaring class has an
4125 // implicit intialization check requirement?
4126 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004127 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004128 }
4129
Vladimir Markob554b5a2015-11-06 12:57:55 +00004130 // Does this method load kind need the current method as an input?
4131 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4132 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4133 }
4134
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004135 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004136
Roland Levillain4c0eb422015-04-24 16:43:49 +01004137 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004138 void InsertInputAt(size_t index, HInstruction* input);
4139 void RemoveInputAt(size_t index);
4140
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004141 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004142 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004143 static constexpr size_t kFieldClinitCheckRequirementSize =
4144 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4145 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4146 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4147 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4148 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004149 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4150 kFieldClinitCheckRequirement,
4151 kFieldClinitCheckRequirementSize>;
4152
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004153 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004154 MethodReference target_method_;
4155 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004156
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004157 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004158};
Vladimir Markof64242a2015-12-01 14:58:23 +00004159std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004160std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004161
Vladimir Markofcb503c2016-05-18 12:48:17 +01004162class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004163 public:
4164 HInvokeVirtual(ArenaAllocator* arena,
4165 uint32_t number_of_arguments,
4166 Primitive::Type return_type,
4167 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004168 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004169 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004170 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004171 : HInvoke(arena,
4172 number_of_arguments,
4173 0u,
4174 return_type,
4175 dex_pc,
4176 dex_method_index,
4177 resolved_method,
4178 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004179 vtable_index_(vtable_index) {}
4180
Calin Juravle641547a2015-04-21 22:08:51 +01004181 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004182 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004183 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004184 }
4185
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004186 uint32_t GetVTableIndex() const { return vtable_index_; }
4187
4188 DECLARE_INSTRUCTION(InvokeVirtual);
4189
4190 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004191 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004192 const uint32_t vtable_index_;
4193
4194 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4195};
4196
Vladimir Markofcb503c2016-05-18 12:48:17 +01004197class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004198 public:
4199 HInvokeInterface(ArenaAllocator* arena,
4200 uint32_t number_of_arguments,
4201 Primitive::Type return_type,
4202 uint32_t dex_pc,
4203 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004204 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004205 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004206 : HInvoke(arena,
4207 number_of_arguments,
4208 0u,
4209 return_type,
4210 dex_pc,
4211 dex_method_index,
4212 resolved_method,
4213 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004214 imt_index_(imt_index) {}
4215
Calin Juravle641547a2015-04-21 22:08:51 +01004216 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004217 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004218 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004219 }
4220
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004221 uint32_t GetImtIndex() const { return imt_index_; }
4222 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4223
4224 DECLARE_INSTRUCTION(InvokeInterface);
4225
4226 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004227 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004228 const uint32_t imt_index_;
4229
4230 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4231};
4232
Vladimir Markofcb503c2016-05-18 12:48:17 +01004233class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004234 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004235 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004236 : HUnaryOperation(result_type, input, dex_pc) {
4237 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4238 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004239
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004240 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004241
4242 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004243 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004244 }
4245 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004246 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004247 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004248 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4249 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4250 }
4251 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4252 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4253 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004254
Roland Levillain88cb1752014-10-20 16:36:47 +01004255 DECLARE_INSTRUCTION(Neg);
4256
4257 private:
4258 DISALLOW_COPY_AND_ASSIGN(HNeg);
4259};
4260
Vladimir Markofcb503c2016-05-18 12:48:17 +01004261class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004262 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004263 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004264 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004265 uint32_t dex_pc,
4266 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004267 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004268 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004269 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004270 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004271 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004272 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004273 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004274 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004275 }
4276
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004277 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004278 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004279
4280 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004281 bool NeedsEnvironment() const OVERRIDE { return true; }
4282
Mingyao Yang0c365e62015-03-31 15:09:29 -07004283 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4284 bool CanThrow() const OVERRIDE { return true; }
4285
Calin Juravle10e244f2015-01-26 18:54:32 +00004286 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004287
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004288 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4289
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004290 DECLARE_INSTRUCTION(NewArray);
4291
4292 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004293 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004294 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004295 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004296
4297 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4298};
4299
Vladimir Markofcb503c2016-05-18 12:48:17 +01004300class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004301 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004302 HAdd(Primitive::Type result_type,
4303 HInstruction* left,
4304 HInstruction* right,
4305 uint32_t dex_pc = kNoDexPc)
4306 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004307
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004308 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004309
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004310 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004311
4312 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004313 return GetBlock()->GetGraph()->GetIntConstant(
4314 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004315 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004316 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004317 return GetBlock()->GetGraph()->GetLongConstant(
4318 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004319 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004320 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4321 return GetBlock()->GetGraph()->GetFloatConstant(
4322 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4323 }
4324 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4325 return GetBlock()->GetGraph()->GetDoubleConstant(
4326 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4327 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004328
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004329 DECLARE_INSTRUCTION(Add);
4330
4331 private:
4332 DISALLOW_COPY_AND_ASSIGN(HAdd);
4333};
4334
Vladimir Markofcb503c2016-05-18 12:48:17 +01004335class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004336 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004337 HSub(Primitive::Type result_type,
4338 HInstruction* left,
4339 HInstruction* right,
4340 uint32_t dex_pc = kNoDexPc)
4341 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004342
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004343 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004344
4345 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004346 return GetBlock()->GetGraph()->GetIntConstant(
4347 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004348 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004349 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004350 return GetBlock()->GetGraph()->GetLongConstant(
4351 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004352 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004353 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4354 return GetBlock()->GetGraph()->GetFloatConstant(
4355 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4356 }
4357 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4358 return GetBlock()->GetGraph()->GetDoubleConstant(
4359 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4360 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004361
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004362 DECLARE_INSTRUCTION(Sub);
4363
4364 private:
4365 DISALLOW_COPY_AND_ASSIGN(HSub);
4366};
4367
Vladimir Markofcb503c2016-05-18 12:48:17 +01004368class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004369 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004370 HMul(Primitive::Type result_type,
4371 HInstruction* left,
4372 HInstruction* right,
4373 uint32_t dex_pc = kNoDexPc)
4374 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004375
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004376 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004377
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004378 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004379
4380 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004381 return GetBlock()->GetGraph()->GetIntConstant(
4382 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004383 }
4384 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004385 return GetBlock()->GetGraph()->GetLongConstant(
4386 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004387 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004388 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4389 return GetBlock()->GetGraph()->GetFloatConstant(
4390 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4391 }
4392 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4393 return GetBlock()->GetGraph()->GetDoubleConstant(
4394 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4395 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004396
4397 DECLARE_INSTRUCTION(Mul);
4398
4399 private:
4400 DISALLOW_COPY_AND_ASSIGN(HMul);
4401};
4402
Vladimir Markofcb503c2016-05-18 12:48:17 +01004403class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004404 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004405 HDiv(Primitive::Type result_type,
4406 HInstruction* left,
4407 HInstruction* right,
4408 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004409 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004410
Roland Levillain9867bc72015-08-05 10:21:34 +01004411 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004412 T ComputeIntegral(T x, T y) const {
4413 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004414 // Our graph structure ensures we never have 0 for `y` during
4415 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004416 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004417 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004418 return (y == -1) ? -x : x / y;
4419 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004420
Roland Levillain31dd3d62016-02-16 12:21:02 +00004421 template <typename T>
4422 T ComputeFP(T x, T y) const {
4423 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4424 return x / y;
4425 }
4426
Roland Levillain9867bc72015-08-05 10:21:34 +01004427 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004428 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004429 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004430 }
4431 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004432 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004433 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4434 }
4435 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4436 return GetBlock()->GetGraph()->GetFloatConstant(
4437 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4438 }
4439 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4440 return GetBlock()->GetGraph()->GetDoubleConstant(
4441 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004442 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004443
4444 DECLARE_INSTRUCTION(Div);
4445
4446 private:
4447 DISALLOW_COPY_AND_ASSIGN(HDiv);
4448};
4449
Vladimir Markofcb503c2016-05-18 12:48:17 +01004450class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004451 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004452 HRem(Primitive::Type result_type,
4453 HInstruction* left,
4454 HInstruction* right,
4455 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004456 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004457
Roland Levillain9867bc72015-08-05 10:21:34 +01004458 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004459 T ComputeIntegral(T x, T y) const {
4460 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004461 // Our graph structure ensures we never have 0 for `y` during
4462 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004463 DCHECK_NE(y, 0);
4464 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4465 return (y == -1) ? 0 : x % y;
4466 }
4467
Roland Levillain31dd3d62016-02-16 12:21:02 +00004468 template <typename T>
4469 T ComputeFP(T x, T y) const {
4470 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4471 return std::fmod(x, y);
4472 }
4473
Roland Levillain9867bc72015-08-05 10:21:34 +01004474 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004475 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004476 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004477 }
4478 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004479 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004480 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004481 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004482 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4483 return GetBlock()->GetGraph()->GetFloatConstant(
4484 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4485 }
4486 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4487 return GetBlock()->GetGraph()->GetDoubleConstant(
4488 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4489 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004490
4491 DECLARE_INSTRUCTION(Rem);
4492
4493 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004494 DISALLOW_COPY_AND_ASSIGN(HRem);
4495};
4496
Vladimir Markofcb503c2016-05-18 12:48:17 +01004497class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004498 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004499 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4500 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004501 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004502 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004503 SetRawInputAt(0, value);
4504 }
4505
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004506 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4507
Calin Juravled0d48522014-11-04 16:40:20 +00004508 bool CanBeMoved() const OVERRIDE { return true; }
4509
Vladimir Marko372f10e2016-05-17 16:30:10 +01004510 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004511 return true;
4512 }
4513
4514 bool NeedsEnvironment() const OVERRIDE { return true; }
4515 bool CanThrow() const OVERRIDE { return true; }
4516
Calin Juravled0d48522014-11-04 16:40:20 +00004517 DECLARE_INSTRUCTION(DivZeroCheck);
4518
4519 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004520 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4521};
4522
Vladimir Markofcb503c2016-05-18 12:48:17 +01004523class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004524 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004525 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004526 HInstruction* value,
4527 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004528 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004529 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4530 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4531 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004532 }
4533
Roland Levillain5b5b9312016-03-22 14:57:31 +00004534 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004535 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004536 return value << (distance & max_shift_distance);
4537 }
4538
4539 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004540 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004541 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004542 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004543 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004544 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004545 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004546 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004547 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4548 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4549 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4550 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004551 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004552 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4553 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004554 LOG(FATAL) << DebugName() << " is not defined for float values";
4555 UNREACHABLE();
4556 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004557 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4558 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004559 LOG(FATAL) << DebugName() << " is not defined for double values";
4560 UNREACHABLE();
4561 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004562
4563 DECLARE_INSTRUCTION(Shl);
4564
4565 private:
4566 DISALLOW_COPY_AND_ASSIGN(HShl);
4567};
4568
Vladimir Markofcb503c2016-05-18 12:48:17 +01004569class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004570 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004571 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004572 HInstruction* value,
4573 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004574 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004575 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4576 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4577 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004578 }
4579
Roland Levillain5b5b9312016-03-22 14:57:31 +00004580 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004581 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004582 return value >> (distance & max_shift_distance);
4583 }
4584
4585 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004586 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004587 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004588 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004589 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004590 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004591 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004592 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004593 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4594 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4595 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4596 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004597 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004598 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4599 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004600 LOG(FATAL) << DebugName() << " is not defined for float values";
4601 UNREACHABLE();
4602 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004603 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4604 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004605 LOG(FATAL) << DebugName() << " is not defined for double values";
4606 UNREACHABLE();
4607 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004608
4609 DECLARE_INSTRUCTION(Shr);
4610
4611 private:
4612 DISALLOW_COPY_AND_ASSIGN(HShr);
4613};
4614
Vladimir Markofcb503c2016-05-18 12:48:17 +01004615class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004616 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004617 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004618 HInstruction* value,
4619 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004620 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004621 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4622 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4623 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004624 }
4625
Roland Levillain5b5b9312016-03-22 14:57:31 +00004626 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004627 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004628 typedef typename std::make_unsigned<T>::type V;
4629 V ux = static_cast<V>(value);
4630 return static_cast<T>(ux >> (distance & max_shift_distance));
4631 }
4632
4633 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004634 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004635 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004636 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004637 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004638 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004639 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004640 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004641 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4642 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4643 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4644 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004645 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004646 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4647 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004648 LOG(FATAL) << DebugName() << " is not defined for float values";
4649 UNREACHABLE();
4650 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004651 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4652 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004653 LOG(FATAL) << DebugName() << " is not defined for double values";
4654 UNREACHABLE();
4655 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004656
4657 DECLARE_INSTRUCTION(UShr);
4658
4659 private:
4660 DISALLOW_COPY_AND_ASSIGN(HUShr);
4661};
4662
Vladimir Markofcb503c2016-05-18 12:48:17 +01004663class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004664 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004665 HAnd(Primitive::Type result_type,
4666 HInstruction* left,
4667 HInstruction* right,
4668 uint32_t dex_pc = kNoDexPc)
4669 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004670
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004671 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004672
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004673 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004674
4675 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004676 return GetBlock()->GetGraph()->GetIntConstant(
4677 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004678 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004679 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004680 return GetBlock()->GetGraph()->GetLongConstant(
4681 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004682 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004683 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4684 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4685 LOG(FATAL) << DebugName() << " is not defined for float values";
4686 UNREACHABLE();
4687 }
4688 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4689 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4690 LOG(FATAL) << DebugName() << " is not defined for double values";
4691 UNREACHABLE();
4692 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004693
4694 DECLARE_INSTRUCTION(And);
4695
4696 private:
4697 DISALLOW_COPY_AND_ASSIGN(HAnd);
4698};
4699
Vladimir Markofcb503c2016-05-18 12:48:17 +01004700class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004701 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004702 HOr(Primitive::Type result_type,
4703 HInstruction* left,
4704 HInstruction* right,
4705 uint32_t dex_pc = kNoDexPc)
4706 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004707
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004708 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004709
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004710 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004711
4712 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004713 return GetBlock()->GetGraph()->GetIntConstant(
4714 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004715 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004716 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004717 return GetBlock()->GetGraph()->GetLongConstant(
4718 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004719 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004720 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4721 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4722 LOG(FATAL) << DebugName() << " is not defined for float values";
4723 UNREACHABLE();
4724 }
4725 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4726 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4727 LOG(FATAL) << DebugName() << " is not defined for double values";
4728 UNREACHABLE();
4729 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004730
4731 DECLARE_INSTRUCTION(Or);
4732
4733 private:
4734 DISALLOW_COPY_AND_ASSIGN(HOr);
4735};
4736
Vladimir Markofcb503c2016-05-18 12:48:17 +01004737class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004738 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004739 HXor(Primitive::Type result_type,
4740 HInstruction* left,
4741 HInstruction* right,
4742 uint32_t dex_pc = kNoDexPc)
4743 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004744
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004745 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004746
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004747 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004748
4749 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004750 return GetBlock()->GetGraph()->GetIntConstant(
4751 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004752 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004753 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004754 return GetBlock()->GetGraph()->GetLongConstant(
4755 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004756 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004757 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4758 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4759 LOG(FATAL) << DebugName() << " is not defined for float values";
4760 UNREACHABLE();
4761 }
4762 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4763 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4764 LOG(FATAL) << DebugName() << " is not defined for double values";
4765 UNREACHABLE();
4766 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004767
4768 DECLARE_INSTRUCTION(Xor);
4769
4770 private:
4771 DISALLOW_COPY_AND_ASSIGN(HXor);
4772};
4773
Vladimir Markofcb503c2016-05-18 12:48:17 +01004774class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004775 public:
4776 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004777 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004778 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4779 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004780 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004781
Roland Levillain5b5b9312016-03-22 14:57:31 +00004782 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004783 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004784 typedef typename std::make_unsigned<T>::type V;
4785 V ux = static_cast<V>(value);
4786 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004787 return static_cast<T>(ux);
4788 } else {
4789 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004790 return static_cast<T>(ux >> (distance & max_shift_value)) |
4791 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004792 }
4793 }
4794
Roland Levillain5b5b9312016-03-22 14:57:31 +00004795 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004796 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004797 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004798 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004799 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004800 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004801 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004802 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004803 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4804 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4805 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4806 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004807 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004808 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4809 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004810 LOG(FATAL) << DebugName() << " is not defined for float values";
4811 UNREACHABLE();
4812 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004813 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4814 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004815 LOG(FATAL) << DebugName() << " is not defined for double values";
4816 UNREACHABLE();
4817 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004818
4819 DECLARE_INSTRUCTION(Ror);
4820
4821 private:
4822 DISALLOW_COPY_AND_ASSIGN(HRor);
4823};
4824
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004825// The value of a parameter in this method. Its location depends on
4826// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004827class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004828 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004829 HParameterValue(const DexFile& dex_file,
4830 uint16_t type_index,
4831 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004832 Primitive::Type parameter_type,
4833 bool is_this = false)
4834 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004835 dex_file_(dex_file),
4836 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004837 index_(index) {
4838 SetPackedFlag<kFlagIsThis>(is_this);
4839 SetPackedFlag<kFlagCanBeNull>(!is_this);
4840 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004841
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004842 const DexFile& GetDexFile() const { return dex_file_; }
4843 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004844 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004845 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004846
Vladimir Markoa1de9182016-02-25 11:37:38 +00004847 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4848 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004849
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004850 DECLARE_INSTRUCTION(ParameterValue);
4851
4852 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004853 // Whether or not the parameter value corresponds to 'this' argument.
4854 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4855 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4856 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4857 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4858 "Too many packed fields.");
4859
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004860 const DexFile& dex_file_;
4861 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004862 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004863 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004864 const uint8_t index_;
4865
4866 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4867};
4868
Vladimir Markofcb503c2016-05-18 12:48:17 +01004869class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004870 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004871 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4872 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004873
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004874 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004875 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004876 return true;
4877 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004878
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004879 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004880
4881 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004882 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004883 }
4884 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004885 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004886 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004887 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4888 LOG(FATAL) << DebugName() << " is not defined for float values";
4889 UNREACHABLE();
4890 }
4891 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4892 LOG(FATAL) << DebugName() << " is not defined for double values";
4893 UNREACHABLE();
4894 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004895
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004896 DECLARE_INSTRUCTION(Not);
4897
4898 private:
4899 DISALLOW_COPY_AND_ASSIGN(HNot);
4900};
4901
Vladimir Markofcb503c2016-05-18 12:48:17 +01004902class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004903 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004904 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4905 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004906
4907 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004908 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004909 return true;
4910 }
4911
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004912 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004913 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004914 return !x;
4915 }
4916
Roland Levillain9867bc72015-08-05 10:21:34 +01004917 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004918 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004919 }
4920 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4921 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004922 UNREACHABLE();
4923 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004924 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4925 LOG(FATAL) << DebugName() << " is not defined for float values";
4926 UNREACHABLE();
4927 }
4928 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4929 LOG(FATAL) << DebugName() << " is not defined for double values";
4930 UNREACHABLE();
4931 }
David Brazdil66d126e2015-04-03 16:02:44 +01004932
4933 DECLARE_INSTRUCTION(BooleanNot);
4934
4935 private:
4936 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4937};
4938
Vladimir Markofcb503c2016-05-18 12:48:17 +01004939class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004940 public:
4941 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004942 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004943 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004944 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004945 // Invariant: We should never generate a conversion to a Boolean value.
4946 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004947 }
4948
4949 HInstruction* GetInput() const { return InputAt(0); }
4950 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4951 Primitive::Type GetResultType() const { return GetType(); }
4952
4953 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004954 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4955 return true;
4956 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004957
Mark Mendelle82549b2015-05-06 10:55:34 -04004958 // Try to statically evaluate the conversion and return a HConstant
4959 // containing the result. If the input cannot be converted, return nullptr.
4960 HConstant* TryStaticEvaluation() const;
4961
Roland Levillaindff1f282014-11-05 14:15:05 +00004962 DECLARE_INSTRUCTION(TypeConversion);
4963
4964 private:
4965 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4966};
4967
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004968static constexpr uint32_t kNoRegNumber = -1;
4969
Vladimir Markofcb503c2016-05-18 12:48:17 +01004970class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004971 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004972 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4973 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004974 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004975 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004976 SetRawInputAt(0, value);
4977 }
4978
Calin Juravle10e244f2015-01-26 18:54:32 +00004979 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004980 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004981 return true;
4982 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004983
Calin Juravle10e244f2015-01-26 18:54:32 +00004984 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004985
Calin Juravle10e244f2015-01-26 18:54:32 +00004986 bool CanThrow() const OVERRIDE { return true; }
4987
4988 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004989
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004990
4991 DECLARE_INSTRUCTION(NullCheck);
4992
4993 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004994 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4995};
4996
4997class FieldInfo : public ValueObject {
4998 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004999 FieldInfo(MemberOffset field_offset,
5000 Primitive::Type field_type,
5001 bool is_volatile,
5002 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005003 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005004 const DexFile& dex_file,
5005 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005006 : field_offset_(field_offset),
5007 field_type_(field_type),
5008 is_volatile_(is_volatile),
5009 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005010 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005011 dex_file_(dex_file),
5012 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005013
5014 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005015 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005016 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005017 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005018 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005019 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005020 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005021
5022 private:
5023 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005024 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005025 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005026 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005027 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005028 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005029 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005030};
5031
Vladimir Markofcb503c2016-05-18 12:48:17 +01005032class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005033 public:
5034 HInstanceFieldGet(HInstruction* value,
5035 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005036 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005037 bool is_volatile,
5038 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005039 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005040 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005041 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005042 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005043 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005044 field_info_(field_offset,
5045 field_type,
5046 is_volatile,
5047 field_idx,
5048 declaring_class_def_index,
5049 dex_file,
5050 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005051 SetRawInputAt(0, value);
5052 }
5053
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005054 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005055
Vladimir Marko372f10e2016-05-17 16:30:10 +01005056 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5057 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005058 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005059 }
5060
Calin Juravle641547a2015-04-21 22:08:51 +01005061 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005062 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005063 }
5064
5065 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005066 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5067 }
5068
Calin Juravle52c48962014-12-16 17:02:57 +00005069 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005070 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005071 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005072 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005073
5074 DECLARE_INSTRUCTION(InstanceFieldGet);
5075
5076 private:
5077 const FieldInfo field_info_;
5078
5079 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5080};
5081
Vladimir Markofcb503c2016-05-18 12:48:17 +01005082class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005083 public:
5084 HInstanceFieldSet(HInstruction* object,
5085 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005086 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005087 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005088 bool is_volatile,
5089 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005090 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005091 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005092 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005093 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005094 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005095 field_info_(field_offset,
5096 field_type,
5097 is_volatile,
5098 field_idx,
5099 declaring_class_def_index,
5100 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005101 dex_cache) {
5102 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005103 SetRawInputAt(0, object);
5104 SetRawInputAt(1, value);
5105 }
5106
Calin Juravle641547a2015-04-21 22:08:51 +01005107 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005108 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005109 }
5110
Calin Juravle52c48962014-12-16 17:02:57 +00005111 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005112 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005113 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005114 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005115 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005116 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5117 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005118
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005119 DECLARE_INSTRUCTION(InstanceFieldSet);
5120
5121 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005122 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5123 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5124 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5125 "Too many packed fields.");
5126
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005127 const FieldInfo field_info_;
5128
5129 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5130};
5131
Vladimir Markofcb503c2016-05-18 12:48:17 +01005132class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005133 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005134 HArrayGet(HInstruction* array,
5135 HInstruction* index,
5136 Primitive::Type type,
5137 uint32_t dex_pc,
5138 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005139 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005140 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005141 SetRawInputAt(0, array);
5142 SetRawInputAt(1, index);
5143 }
5144
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005145 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005146 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005147 return true;
5148 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005149 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005150 // TODO: We can be smarter here.
5151 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5152 // which generates the implicit null check. There are cases when these can be removed
5153 // to produce better code. If we ever add optimizations to do so we should allow an
5154 // implicit check here (as long as the address falls in the first page).
5155 return false;
5156 }
5157
David Brazdil4833f5a2015-12-16 10:37:39 +00005158 bool IsEquivalentOf(HArrayGet* other) const {
5159 bool result = (GetDexPc() == other->GetDexPc());
5160 if (kIsDebugBuild && result) {
5161 DCHECK_EQ(GetBlock(), other->GetBlock());
5162 DCHECK_EQ(GetArray(), other->GetArray());
5163 DCHECK_EQ(GetIndex(), other->GetIndex());
5164 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005165 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005166 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005167 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5168 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005169 }
5170 }
5171 return result;
5172 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005173
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005174 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5175
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005176 HInstruction* GetArray() const { return InputAt(0); }
5177 HInstruction* GetIndex() const { return InputAt(1); }
5178
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005179 DECLARE_INSTRUCTION(ArrayGet);
5180
5181 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005182 // We treat a String as an array, creating the HArrayGet from String.charAt()
5183 // intrinsic in the instruction simplifier. We can always determine whether
5184 // a particular HArrayGet is actually a String.charAt() by looking at the type
5185 // of the input but that requires holding the mutator lock, so we prefer to use
5186 // a flag, so that code generators don't need to do the locking.
5187 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5188 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5189 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5190 "Too many packed fields.");
5191
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005192 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5193};
5194
Vladimir Markofcb503c2016-05-18 12:48:17 +01005195class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005196 public:
5197 HArraySet(HInstruction* array,
5198 HInstruction* index,
5199 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005200 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005201 uint32_t dex_pc)
5202 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005203 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5204 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5205 SetPackedFlag<kFlagValueCanBeNull>(true);
5206 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005207 SetRawInputAt(0, array);
5208 SetRawInputAt(1, index);
5209 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005210 // Make a best guess now, may be refined during SSA building.
5211 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005212 }
5213
Calin Juravle77520bc2015-01-12 18:45:46 +00005214 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005215 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005216 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005217 }
5218
Mingyao Yang81014cb2015-06-02 03:16:27 -07005219 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005220 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005221
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005222 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005223 // TODO: Same as for ArrayGet.
5224 return false;
5225 }
5226
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005227 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005228 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005229 }
5230
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005231 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005232 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005233 }
5234
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005235 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005236 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005237 }
5238
Vladimir Markoa1de9182016-02-25 11:37:38 +00005239 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5240 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5241 bool StaticTypeOfArrayIsObjectArray() const {
5242 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5243 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005244
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005245 HInstruction* GetArray() const { return InputAt(0); }
5246 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005247 HInstruction* GetValue() const { return InputAt(2); }
5248
5249 Primitive::Type GetComponentType() const {
5250 // The Dex format does not type floating point index operations. Since the
5251 // `expected_component_type_` is set during building and can therefore not
5252 // be correct, we also check what is the value type. If it is a floating
5253 // point type, we must use that type.
5254 Primitive::Type value_type = GetValue()->GetType();
5255 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5256 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005257 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005258 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005259
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005260 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005261 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005262 }
5263
Aart Bik18b36ab2016-04-13 16:41:35 -07005264 void ComputeSideEffects() {
5265 Primitive::Type type = GetComponentType();
5266 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5267 SideEffectsForArchRuntimeCalls(type)));
5268 }
5269
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005270 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5271 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5272 }
5273
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005274 DECLARE_INSTRUCTION(ArraySet);
5275
5276 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005277 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5278 static constexpr size_t kFieldExpectedComponentTypeSize =
5279 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5280 static constexpr size_t kFlagNeedsTypeCheck =
5281 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5282 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005283 // Cached information for the reference_type_info_ so that codegen
5284 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005285 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5286 static constexpr size_t kNumberOfArraySetPackedBits =
5287 kFlagStaticTypeOfArrayIsObjectArray + 1;
5288 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5289 using ExpectedComponentTypeField =
5290 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005291
5292 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5293};
5294
Vladimir Markofcb503c2016-05-18 12:48:17 +01005295class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005296 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005297 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005298 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005299 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005300 // Note that arrays do not change length, so the instruction does not
5301 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005302 SetRawInputAt(0, array);
5303 }
5304
Calin Juravle77520bc2015-01-12 18:45:46 +00005305 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005306 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005307 return true;
5308 }
Calin Juravle641547a2015-04-21 22:08:51 +01005309 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5310 return obj == InputAt(0);
5311 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005312
Vladimir Markodce016e2016-04-28 13:10:02 +01005313 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5314
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005315 DECLARE_INSTRUCTION(ArrayLength);
5316
5317 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005318 // We treat a String as an array, creating the HArrayLength from String.length()
5319 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5320 // determine whether a particular HArrayLength is actually a String.length() by
5321 // looking at the type of the input but that requires holding the mutator lock, so
5322 // we prefer to use a flag, so that code generators don't need to do the locking.
5323 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5324 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5325 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5326 "Too many packed fields.");
5327
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005328 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5329};
5330
Vladimir Markofcb503c2016-05-18 12:48:17 +01005331class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005332 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005333 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5334 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005335 HBoundsCheck(HInstruction* index,
5336 HInstruction* length,
5337 uint32_t dex_pc,
5338 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5339 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5340 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005341 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005342 SetRawInputAt(0, index);
5343 SetRawInputAt(1, length);
5344 }
5345
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005346 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005347 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005348 return true;
5349 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005350
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005351 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005352
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005353 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005354
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005355 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5356 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5357
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005358 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005359
5360 DECLARE_INSTRUCTION(BoundsCheck);
5361
5362 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005363 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5364 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5365 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5366 // so we need to create an HEnvironment which will be translated to an InlineInfo
5367 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5368 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5369 // from the invoke as we don't want to look again at the dex bytecode.
5370 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5371
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005372 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5373};
5374
Vladimir Markofcb503c2016-05-18 12:48:17 +01005375class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005376 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005377 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005378 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005379
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005380 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005381 return true;
5382 }
5383
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005384 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5385 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005386
5387 DECLARE_INSTRUCTION(SuspendCheck);
5388
5389 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005390 // Only used for code generation, in order to share the same slow path between back edges
5391 // of a same loop.
5392 SlowPathCode* slow_path_;
5393
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005394 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5395};
5396
David Srbecky0cf44932015-12-09 14:09:59 +00005397// Pseudo-instruction which provides the native debugger with mapping information.
5398// It ensures that we can generate line number and local variables at this point.
5399class HNativeDebugInfo : public HTemplateInstruction<0> {
5400 public:
5401 explicit HNativeDebugInfo(uint32_t dex_pc)
5402 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5403
5404 bool NeedsEnvironment() const OVERRIDE {
5405 return true;
5406 }
5407
5408 DECLARE_INSTRUCTION(NativeDebugInfo);
5409
5410 private:
5411 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5412};
5413
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005414/**
5415 * Instruction to load a Class object.
5416 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005417class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005418 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005419 // Determines how to load the Class.
5420 enum class LoadKind {
5421 // Use the Class* from the method's own ArtMethod*.
5422 kReferrersClass,
5423
5424 // Use boot image Class* address that will be known at link time.
5425 // Used for boot image classes referenced by boot image code in non-PIC mode.
5426 kBootImageLinkTimeAddress,
5427
5428 // Use PC-relative boot image Class* address that will be known at link time.
5429 // Used for boot image classes referenced by boot image code in PIC mode.
5430 kBootImageLinkTimePcRelative,
5431
5432 // Use a known boot image Class* address, embedded in the code by the codegen.
5433 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5434 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5435 // GetIncludePatchInformation().
5436 kBootImageAddress,
5437
5438 // Load from the resolved types array at an absolute address.
5439 // Used for classes outside the boot image referenced by JIT-compiled code.
5440 kDexCacheAddress,
5441
5442 // Load from resolved types array in the dex cache using a PC-relative load.
5443 // Used for classes outside boot image when we know that we can access
5444 // the dex cache arrays using a PC-relative load.
5445 kDexCachePcRelative,
5446
5447 // Load from resolved types array accessed through the class loaded from
5448 // the compiled method's own ArtMethod*. This is the default access type when
5449 // all other types are unavailable.
5450 kDexCacheViaMethod,
5451
5452 kLast = kDexCacheViaMethod
5453 };
5454
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005455 HLoadClass(HCurrentMethod* current_method,
5456 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005457 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005458 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005459 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005460 bool needs_access_check,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005461 bool is_in_dex_cache,
5462 bool is_in_boot_image)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005463 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5464 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005465 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005466 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005467 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005468 // Referrers class should not need access check. We never inline unverified
5469 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005470 DCHECK(!is_referrers_class || !needs_access_check);
5471
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005472 SetPackedField<LoadKindField>(
5473 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005474 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5475 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005476 SetPackedFlag<kFlagIsInBootImage>(is_in_boot_image);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005477 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005478 }
5479
5480 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5481 DCHECK(HasAddress(load_kind));
5482 load_data_.address = address;
5483 SetLoadKindInternal(load_kind);
5484 }
5485
5486 void SetLoadKindWithTypeReference(LoadKind load_kind,
5487 const DexFile& dex_file,
5488 uint32_t type_index) {
5489 DCHECK(HasTypeReference(load_kind));
5490 DCHECK(IsSameDexFile(dex_file_, dex_file));
5491 DCHECK_EQ(type_index_, type_index);
5492 SetLoadKindInternal(load_kind);
5493 }
5494
5495 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5496 const DexFile& dex_file,
5497 uint32_t element_index) {
5498 DCHECK(HasDexCacheReference(load_kind));
5499 DCHECK(IsSameDexFile(dex_file_, dex_file));
5500 load_data_.dex_cache_element_index = element_index;
5501 SetLoadKindInternal(load_kind);
5502 }
5503
5504 LoadKind GetLoadKind() const {
5505 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005506 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005507
5508 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005509
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005510 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005511
5512 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5513
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005514 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005515
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005516 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005517 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005518 }
5519
Calin Juravle0ba218d2015-05-19 18:46:01 +01005520 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005521 // The entrypoint the code generator is going to call does not do
5522 // clinit of the class.
5523 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005524 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005525 }
5526
5527 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005528 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005529 (!IsReferrersClass() && !IsInDexCache()) ||
5530 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005531 }
5532
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005533
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005534 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005535 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005536 }
5537
Calin Juravleacf735c2015-02-12 15:25:22 +00005538 ReferenceTypeInfo GetLoadedClassRTI() {
5539 return loaded_class_rti_;
5540 }
5541
5542 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5543 // Make sure we only set exact types (the loaded class should never be merged).
5544 DCHECK(rti.IsExact());
5545 loaded_class_rti_ = rti;
5546 }
5547
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005548 uint32_t GetTypeIndex() const { return type_index_; }
5549 const DexFile& GetDexFile() const { return dex_file_; }
5550
5551 uint32_t GetDexCacheElementOffset() const;
5552
5553 uint64_t GetAddress() const {
5554 DCHECK(HasAddress(GetLoadKind()));
5555 return load_data_.address;
5556 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005557
Vladimir Markoa1de9182016-02-25 11:37:38 +00005558 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005559
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005560 static SideEffects SideEffectsForArchRuntimeCalls() {
5561 return SideEffects::CanTriggerGC();
5562 }
5563
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005564 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005565 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5566 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005567 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005568 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005569
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005570 void MarkInDexCache() {
5571 SetPackedFlag<kFlagIsInDexCache>(true);
5572 DCHECK(!NeedsEnvironment());
5573 RemoveEnvironment();
5574 SetSideEffects(SideEffects::None());
5575 }
5576
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005577 void MarkInBootImage() {
5578 SetPackedFlag<kFlagIsInBootImage>(true);
5579 }
5580
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005581 void AddSpecialInput(HInstruction* special_input);
5582
5583 using HInstruction::GetInputRecords; // Keep the const version visible.
5584 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5585 return ArrayRef<HUserRecord<HInstruction*>>(
5586 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5587 }
5588
5589 Primitive::Type GetType() const OVERRIDE {
5590 return Primitive::kPrimNot;
5591 }
5592
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005593 DECLARE_INSTRUCTION(LoadClass);
5594
5595 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005596 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005597 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005598 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005599 // Whether this instruction must generate the initialization check.
5600 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005601 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005602 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5603 static constexpr size_t kFieldLoadKindSize =
5604 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5605 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005606 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005607 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5608
5609 static bool HasTypeReference(LoadKind load_kind) {
5610 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5611 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5612 load_kind == LoadKind::kDexCacheViaMethod ||
5613 load_kind == LoadKind::kReferrersClass;
5614 }
5615
5616 static bool HasAddress(LoadKind load_kind) {
5617 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5618 }
5619
5620 static bool HasDexCacheReference(LoadKind load_kind) {
5621 return load_kind == LoadKind::kDexCachePcRelative;
5622 }
5623
5624 void SetLoadKindInternal(LoadKind load_kind);
5625
5626 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5627 // For other load kinds it's empty or possibly some architecture-specific instruction
5628 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5629 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005630
5631 const uint16_t type_index_;
5632 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005633
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005634 union {
5635 uint32_t dex_cache_element_index; // Only for dex cache reference.
5636 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5637 } load_data_;
5638
Calin Juravleacf735c2015-02-12 15:25:22 +00005639 ReferenceTypeInfo loaded_class_rti_;
5640
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005641 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5642};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005643std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5644
5645// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5646inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5647 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5648 return load_data_.dex_cache_element_index;
5649}
5650
5651// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5652inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005653 // The special input is used for PC-relative loads on some architectures,
5654 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005655 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005656 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5657 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5658 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005659 DCHECK(special_input_.GetInstruction() == nullptr);
5660 special_input_ = HUserRecord<HInstruction*>(special_input);
5661 special_input->AddUseAt(this, 0);
5662}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005663
Vladimir Marko372f10e2016-05-17 16:30:10 +01005664class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005665 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005666 // Determines how to load the String.
5667 enum class LoadKind {
5668 // Use boot image String* address that will be known at link time.
5669 // Used for boot image strings referenced by boot image code in non-PIC mode.
5670 kBootImageLinkTimeAddress,
5671
5672 // Use PC-relative boot image String* address that will be known at link time.
5673 // Used for boot image strings referenced by boot image code in PIC mode.
5674 kBootImageLinkTimePcRelative,
5675
5676 // Use a known boot image String* address, embedded in the code by the codegen.
5677 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5678 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5679 // GetIncludePatchInformation().
5680 kBootImageAddress,
5681
Vladimir Markoaad75c62016-10-03 08:46:48 +00005682 // Load from an entry in the .bss section using a PC-relative load.
5683 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5684 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005685
5686 // Load from resolved strings array accessed through the class loaded from
5687 // the compiled method's own ArtMethod*. This is the default access type when
5688 // all other types are unavailable.
5689 kDexCacheViaMethod,
5690
Nicolas Geoffrayac3ebc32016-10-05 13:13:50 +01005691 // Load from the root table associated with the JIT compiled method.
5692 kJitTableAddress,
5693
5694 kLast = kJitTableAddress,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005695 };
5696
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005697 HLoadString(HCurrentMethod* current_method,
5698 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005699 const DexFile& dex_file,
5700 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005701 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5702 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005703 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005704 SetPackedFlag<kFlagIsInDexCache>(false);
5705 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005706 load_data_.dex_file_ = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005707 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005708
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005709 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5710 DCHECK(HasAddress(load_kind));
5711 load_data_.address = address;
5712 SetLoadKindInternal(load_kind);
5713 }
5714
5715 void SetLoadKindWithStringReference(LoadKind load_kind,
5716 const DexFile& dex_file,
5717 uint32_t string_index) {
5718 DCHECK(HasStringReference(load_kind));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005719 load_data_.dex_file_ = &dex_file;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005720 string_index_ = string_index;
5721 SetLoadKindInternal(load_kind);
5722 }
5723
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005724 LoadKind GetLoadKind() const {
5725 return GetPackedField<LoadKindField>();
5726 }
5727
5728 const DexFile& GetDexFile() const;
5729
5730 uint32_t GetStringIndex() const {
5731 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5732 return string_index_;
5733 }
5734
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005735 uint64_t GetAddress() const {
5736 DCHECK(HasAddress(GetLoadKind()));
5737 return load_data_.address;
5738 }
5739
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005740 bool CanBeMoved() const OVERRIDE { return true; }
5741
Vladimir Marko372f10e2016-05-17 16:30:10 +01005742 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005743
5744 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5745
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005746 // Will call the runtime if we need to load the string through
5747 // the dex cache and the string is not guaranteed to be there yet.
5748 bool NeedsEnvironment() const OVERRIDE {
5749 LoadKind load_kind = GetLoadKind();
5750 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5751 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffrayac3ebc32016-10-05 13:13:50 +01005752 load_kind == LoadKind::kBootImageAddress ||
5753 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005754 return false;
5755 }
5756 return !IsInDexCache();
5757 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005758
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005759 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5760 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5761 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005762
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005763 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005764 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005765
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005766 static SideEffects SideEffectsForArchRuntimeCalls() {
5767 return SideEffects::CanTriggerGC();
5768 }
5769
Vladimir Markoa1de9182016-02-25 11:37:38 +00005770 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5771
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005772 void MarkInDexCache() {
5773 SetPackedFlag<kFlagIsInDexCache>(true);
5774 DCHECK(!NeedsEnvironment());
5775 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005776 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005777 }
5778
Vladimir Marko372f10e2016-05-17 16:30:10 +01005779 void AddSpecialInput(HInstruction* special_input);
5780
5781 using HInstruction::GetInputRecords; // Keep the const version visible.
5782 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5783 return ArrayRef<HUserRecord<HInstruction*>>(
5784 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005785 }
5786
Vladimir Marko372f10e2016-05-17 16:30:10 +01005787 Primitive::Type GetType() const OVERRIDE {
5788 return Primitive::kPrimNot;
5789 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005790
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005791 DECLARE_INSTRUCTION(LoadString);
5792
5793 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005794 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005795 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5796 static constexpr size_t kFieldLoadKindSize =
5797 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5798 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005799 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005800 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005801
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005802 static bool HasStringReference(LoadKind load_kind) {
5803 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5804 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005805 load_kind == LoadKind::kBssEntry ||
Nicolas Geoffrayac3ebc32016-10-05 13:13:50 +01005806 load_kind == LoadKind::kDexCacheViaMethod ||
5807 load_kind == LoadKind::kJitTableAddress;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005808 }
5809
5810 static bool HasAddress(LoadKind load_kind) {
Vladimir Marko54d6a202016-11-09 12:46:38 +00005811 return load_kind == LoadKind::kBootImageAddress;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005812 }
5813
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005814 void SetLoadKindInternal(LoadKind load_kind);
5815
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005816 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5817 // For other load kinds it's empty or possibly some architecture-specific instruction
5818 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005819 HUserRecord<HInstruction*> special_input_;
5820
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005821 // String index serves also as the hash code and it's also needed for slow-paths,
5822 // so it must not be overwritten with other load data.
5823 uint32_t string_index_;
5824
5825 union {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005826 const DexFile* dex_file_; // For string reference.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005827 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5828 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005829
5830 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5831};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005832std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5833
5834// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5835inline const DexFile& HLoadString::GetDexFile() const {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005836 DCHECK(HasStringReference(GetLoadKind())) << GetLoadKind();
5837 return *load_data_.dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005838}
5839
5840// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5841inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005842 // The special input is used for PC-relative loads on some architectures,
5843 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005844 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005845 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005846 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5847 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005848 // HLoadString::GetInputRecords() returns an empty array at this point,
5849 // so use the GetInputRecords() from the base class to set the input record.
5850 DCHECK(special_input_.GetInstruction() == nullptr);
5851 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005852 special_input->AddUseAt(this, 0);
5853}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005854
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005855/**
5856 * Performs an initialization check on its Class object input.
5857 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005858class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005859 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005860 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005861 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005862 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005863 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5864 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005865 SetRawInputAt(0, constant);
5866 }
5867
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005868 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005869 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005870 return true;
5871 }
5872
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005873 bool NeedsEnvironment() const OVERRIDE {
5874 // May call runtime to initialize the class.
5875 return true;
5876 }
5877
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005878 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005879
5880 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5881
5882 DECLARE_INSTRUCTION(ClinitCheck);
5883
5884 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005885 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5886};
5887
Vladimir Markofcb503c2016-05-18 12:48:17 +01005888class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005889 public:
5890 HStaticFieldGet(HInstruction* cls,
5891 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005892 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005893 bool is_volatile,
5894 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005895 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005896 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005897 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005898 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005899 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005900 field_info_(field_offset,
5901 field_type,
5902 is_volatile,
5903 field_idx,
5904 declaring_class_def_index,
5905 dex_file,
5906 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005907 SetRawInputAt(0, cls);
5908 }
5909
Calin Juravle52c48962014-12-16 17:02:57 +00005910
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005911 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005912
Vladimir Marko372f10e2016-05-17 16:30:10 +01005913 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5914 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005915 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005916 }
5917
5918 size_t ComputeHashCode() const OVERRIDE {
5919 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5920 }
5921
Calin Juravle52c48962014-12-16 17:02:57 +00005922 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005923 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5924 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005925 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005926
5927 DECLARE_INSTRUCTION(StaticFieldGet);
5928
5929 private:
5930 const FieldInfo field_info_;
5931
5932 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5933};
5934
Vladimir Markofcb503c2016-05-18 12:48:17 +01005935class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005936 public:
5937 HStaticFieldSet(HInstruction* cls,
5938 HInstruction* value,
5939 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005940 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005941 bool is_volatile,
5942 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005943 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005944 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005945 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005946 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005947 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005948 field_info_(field_offset,
5949 field_type,
5950 is_volatile,
5951 field_idx,
5952 declaring_class_def_index,
5953 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005954 dex_cache) {
5955 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005956 SetRawInputAt(0, cls);
5957 SetRawInputAt(1, value);
5958 }
5959
Calin Juravle52c48962014-12-16 17:02:57 +00005960 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005961 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5962 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005963 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005964
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005965 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005966 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5967 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005968
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005969 DECLARE_INSTRUCTION(StaticFieldSet);
5970
5971 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005972 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5973 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5974 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5975 "Too many packed fields.");
5976
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005977 const FieldInfo field_info_;
5978
5979 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5980};
5981
Vladimir Markofcb503c2016-05-18 12:48:17 +01005982class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005983 public:
5984 HUnresolvedInstanceFieldGet(HInstruction* obj,
5985 Primitive::Type field_type,
5986 uint32_t field_index,
5987 uint32_t dex_pc)
5988 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5989 field_index_(field_index) {
5990 SetRawInputAt(0, obj);
5991 }
5992
5993 bool NeedsEnvironment() const OVERRIDE { return true; }
5994 bool CanThrow() const OVERRIDE { return true; }
5995
5996 Primitive::Type GetFieldType() const { return GetType(); }
5997 uint32_t GetFieldIndex() const { return field_index_; }
5998
5999 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6000
6001 private:
6002 const uint32_t field_index_;
6003
6004 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6005};
6006
Vladimir Markofcb503c2016-05-18 12:48:17 +01006007class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006008 public:
6009 HUnresolvedInstanceFieldSet(HInstruction* obj,
6010 HInstruction* value,
6011 Primitive::Type field_type,
6012 uint32_t field_index,
6013 uint32_t dex_pc)
6014 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006015 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006016 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006017 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006018 SetRawInputAt(0, obj);
6019 SetRawInputAt(1, value);
6020 }
6021
6022 bool NeedsEnvironment() const OVERRIDE { return true; }
6023 bool CanThrow() const OVERRIDE { return true; }
6024
Vladimir Markoa1de9182016-02-25 11:37:38 +00006025 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006026 uint32_t GetFieldIndex() const { return field_index_; }
6027
6028 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6029
6030 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006031 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6032 static constexpr size_t kFieldFieldTypeSize =
6033 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6034 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6035 kFieldFieldType + kFieldFieldTypeSize;
6036 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6037 "Too many packed fields.");
6038 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6039
Calin Juravlee460d1d2015-09-29 04:52:17 +01006040 const uint32_t field_index_;
6041
6042 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6043};
6044
Vladimir Markofcb503c2016-05-18 12:48:17 +01006045class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006046 public:
6047 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6048 uint32_t field_index,
6049 uint32_t dex_pc)
6050 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6051 field_index_(field_index) {
6052 }
6053
6054 bool NeedsEnvironment() const OVERRIDE { return true; }
6055 bool CanThrow() const OVERRIDE { return true; }
6056
6057 Primitive::Type GetFieldType() const { return GetType(); }
6058 uint32_t GetFieldIndex() const { return field_index_; }
6059
6060 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6061
6062 private:
6063 const uint32_t field_index_;
6064
6065 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6066};
6067
Vladimir Markofcb503c2016-05-18 12:48:17 +01006068class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006069 public:
6070 HUnresolvedStaticFieldSet(HInstruction* value,
6071 Primitive::Type field_type,
6072 uint32_t field_index,
6073 uint32_t dex_pc)
6074 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006075 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006076 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006077 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006078 SetRawInputAt(0, value);
6079 }
6080
6081 bool NeedsEnvironment() const OVERRIDE { return true; }
6082 bool CanThrow() const OVERRIDE { return true; }
6083
Vladimir Markoa1de9182016-02-25 11:37:38 +00006084 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006085 uint32_t GetFieldIndex() const { return field_index_; }
6086
6087 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6088
6089 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006090 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6091 static constexpr size_t kFieldFieldTypeSize =
6092 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6093 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6094 kFieldFieldType + kFieldFieldTypeSize;
6095 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6096 "Too many packed fields.");
6097 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6098
Calin Juravlee460d1d2015-09-29 04:52:17 +01006099 const uint32_t field_index_;
6100
6101 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6102};
6103
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006104// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006105class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006106 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006107 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6108 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006109
David Brazdilbbd733e2015-08-18 17:48:17 +01006110 bool CanBeNull() const OVERRIDE { return false; }
6111
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006112 DECLARE_INSTRUCTION(LoadException);
6113
6114 private:
6115 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6116};
6117
David Brazdilcb1c0552015-08-04 16:22:25 +01006118// Implicit part of move-exception which clears thread-local exception storage.
6119// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006120class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006121 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006122 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6123 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006124
6125 DECLARE_INSTRUCTION(ClearException);
6126
6127 private:
6128 DISALLOW_COPY_AND_ASSIGN(HClearException);
6129};
6130
Vladimir Markofcb503c2016-05-18 12:48:17 +01006131class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006132 public:
6133 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006134 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006135 SetRawInputAt(0, exception);
6136 }
6137
6138 bool IsControlFlow() const OVERRIDE { return true; }
6139
6140 bool NeedsEnvironment() const OVERRIDE { return true; }
6141
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006142 bool CanThrow() const OVERRIDE { return true; }
6143
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006144
6145 DECLARE_INSTRUCTION(Throw);
6146
6147 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006148 DISALLOW_COPY_AND_ASSIGN(HThrow);
6149};
6150
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006151/**
6152 * Implementation strategies for the code generator of a HInstanceOf
6153 * or `HCheckCast`.
6154 */
6155enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006156 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006157 kExactCheck, // Can do a single class compare.
6158 kClassHierarchyCheck, // Can just walk the super class chain.
6159 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6160 kInterfaceCheck, // No optimization yet when checking against an interface.
6161 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006162 kArrayCheck, // No optimization yet when checking against a generic array.
6163 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006164};
6165
Roland Levillain86503782016-02-11 19:07:30 +00006166std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6167
Vladimir Markofcb503c2016-05-18 12:48:17 +01006168class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006169 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006170 HInstanceOf(HInstruction* object,
6171 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006172 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006173 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006174 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006175 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006176 dex_pc) {
6177 SetPackedField<TypeCheckKindField>(check_kind);
6178 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006179 SetRawInputAt(0, object);
6180 SetRawInputAt(1, constant);
6181 }
6182
6183 bool CanBeMoved() const OVERRIDE { return true; }
6184
Vladimir Marko372f10e2016-05-17 16:30:10 +01006185 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006186 return true;
6187 }
6188
6189 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006190 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006191 }
6192
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006193 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006194 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6195 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6196 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6197 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006198
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006199 static bool CanCallRuntime(TypeCheckKind check_kind) {
6200 // Mips currently does runtime calls for any other checks.
6201 return check_kind != TypeCheckKind::kExactCheck;
6202 }
6203
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006204 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006205 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006206 }
6207
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006208 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006209
6210 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006211 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6212 static constexpr size_t kFieldTypeCheckKindSize =
6213 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6214 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6215 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6216 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6217 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006218
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006219 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6220};
6221
Vladimir Markofcb503c2016-05-18 12:48:17 +01006222class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006223 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006224 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006225 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006226 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6227 SetPackedFlag<kFlagUpperCanBeNull>(true);
6228 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006229 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006230 SetRawInputAt(0, input);
6231 }
6232
David Brazdilf5552582015-12-27 13:36:12 +00006233 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006234 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006235 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006236 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006237
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006238 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006239 DCHECK(GetUpperCanBeNull() || !can_be_null);
6240 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006241 }
6242
Vladimir Markoa1de9182016-02-25 11:37:38 +00006243 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006244
Calin Juravleb1498f62015-02-16 13:13:29 +00006245 DECLARE_INSTRUCTION(BoundType);
6246
6247 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006248 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6249 // is false then CanBeNull() cannot be true).
6250 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6251 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6252 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6253 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6254
Calin Juravleb1498f62015-02-16 13:13:29 +00006255 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006256 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6257 // It is used to bound the type in cases like:
6258 // if (x instanceof ClassX) {
6259 // // uper_bound_ will be ClassX
6260 // }
David Brazdilf5552582015-12-27 13:36:12 +00006261 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006262
6263 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6264};
6265
Vladimir Markofcb503c2016-05-18 12:48:17 +01006266class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006267 public:
6268 HCheckCast(HInstruction* object,
6269 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006270 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006271 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006272 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6273 SetPackedField<TypeCheckKindField>(check_kind);
6274 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006275 SetRawInputAt(0, object);
6276 SetRawInputAt(1, constant);
6277 }
6278
6279 bool CanBeMoved() const OVERRIDE { return true; }
6280
Vladimir Marko372f10e2016-05-17 16:30:10 +01006281 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006282 return true;
6283 }
6284
6285 bool NeedsEnvironment() const OVERRIDE {
6286 // Instruction may throw a CheckCastError.
6287 return true;
6288 }
6289
6290 bool CanThrow() const OVERRIDE { return true; }
6291
Vladimir Markoa1de9182016-02-25 11:37:38 +00006292 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6293 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6294 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6295 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006296
6297 DECLARE_INSTRUCTION(CheckCast);
6298
6299 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006300 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6301 static constexpr size_t kFieldTypeCheckKindSize =
6302 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6303 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6304 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6305 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6306 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006307
6308 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006309};
6310
Andreas Gampe26de38b2016-07-27 17:53:11 -07006311/**
6312 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6313 * @details We define the combined barrier types that are actually required
6314 * by the Java Memory Model, rather than using exactly the terminology from
6315 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6316 * primitives. Note that the JSR-133 cookbook generally does not deal with
6317 * store atomicity issues, and the recipes there are not always entirely sufficient.
6318 * The current recipe is as follows:
6319 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6320 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6321 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6322 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6323 * class has final fields.
6324 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6325 * store-to-memory instructions. Only generated together with non-temporal stores.
6326 */
6327enum MemBarrierKind {
6328 kAnyStore,
6329 kLoadAny,
6330 kStoreStore,
6331 kAnyAny,
6332 kNTStoreStore,
6333 kLastBarrierKind = kNTStoreStore
6334};
6335std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6336
Vladimir Markofcb503c2016-05-18 12:48:17 +01006337class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006338 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006339 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006340 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006341 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6342 SetPackedField<BarrierKindField>(barrier_kind);
6343 }
Calin Juravle27df7582015-04-17 19:12:31 +01006344
Vladimir Markoa1de9182016-02-25 11:37:38 +00006345 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006346
6347 DECLARE_INSTRUCTION(MemoryBarrier);
6348
6349 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006350 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6351 static constexpr size_t kFieldBarrierKindSize =
6352 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6353 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6354 kFieldBarrierKind + kFieldBarrierKindSize;
6355 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6356 "Too many packed fields.");
6357 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006358
6359 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6360};
6361
Vladimir Markofcb503c2016-05-18 12:48:17 +01006362class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006363 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006364 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006365 kEnter,
6366 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006367 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006368 };
6369
6370 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006371 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006372 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6373 dex_pc) {
6374 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006375 SetRawInputAt(0, object);
6376 }
6377
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006378 // Instruction may go into runtime, so we need an environment.
6379 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006380
6381 bool CanThrow() const OVERRIDE {
6382 // Verifier guarantees that monitor-exit cannot throw.
6383 // This is important because it allows the HGraphBuilder to remove
6384 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6385 return IsEnter();
6386 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006387
Vladimir Markoa1de9182016-02-25 11:37:38 +00006388 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6389 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006390
6391 DECLARE_INSTRUCTION(MonitorOperation);
6392
Calin Juravle52c48962014-12-16 17:02:57 +00006393 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006394 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6395 static constexpr size_t kFieldOperationKindSize =
6396 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6397 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6398 kFieldOperationKind + kFieldOperationKindSize;
6399 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6400 "Too many packed fields.");
6401 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006402
6403 private:
6404 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6405};
6406
Vladimir Markofcb503c2016-05-18 12:48:17 +01006407class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006408 public:
6409 HSelect(HInstruction* condition,
6410 HInstruction* true_value,
6411 HInstruction* false_value,
6412 uint32_t dex_pc)
6413 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6414 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6415
6416 // First input must be `true_value` or `false_value` to allow codegens to
6417 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6418 // that architectures which implement HSelect as a conditional move also
6419 // will not need to invert the condition.
6420 SetRawInputAt(0, false_value);
6421 SetRawInputAt(1, true_value);
6422 SetRawInputAt(2, condition);
6423 }
6424
6425 HInstruction* GetFalseValue() const { return InputAt(0); }
6426 HInstruction* GetTrueValue() const { return InputAt(1); }
6427 HInstruction* GetCondition() const { return InputAt(2); }
6428
6429 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006430 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6431 return true;
6432 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006433
6434 bool CanBeNull() const OVERRIDE {
6435 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6436 }
6437
6438 DECLARE_INSTRUCTION(Select);
6439
6440 private:
6441 DISALLOW_COPY_AND_ASSIGN(HSelect);
6442};
6443
Vladimir Markof9f64412015-09-02 14:05:49 +01006444class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006445 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006446 MoveOperands(Location source,
6447 Location destination,
6448 Primitive::Type type,
6449 HInstruction* instruction)
6450 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006451
6452 Location GetSource() const { return source_; }
6453 Location GetDestination() const { return destination_; }
6454
6455 void SetSource(Location value) { source_ = value; }
6456 void SetDestination(Location value) { destination_ = value; }
6457
6458 // The parallel move resolver marks moves as "in-progress" by clearing the
6459 // destination (but not the source).
6460 Location MarkPending() {
6461 DCHECK(!IsPending());
6462 Location dest = destination_;
6463 destination_ = Location::NoLocation();
6464 return dest;
6465 }
6466
6467 void ClearPending(Location dest) {
6468 DCHECK(IsPending());
6469 destination_ = dest;
6470 }
6471
6472 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006473 DCHECK(source_.IsValid() || destination_.IsInvalid());
6474 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006475 }
6476
6477 // True if this blocks a move from the given location.
6478 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006479 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006480 }
6481
6482 // A move is redundant if it's been eliminated, if its source and
6483 // destination are the same, or if its destination is unneeded.
6484 bool IsRedundant() const {
6485 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6486 }
6487
6488 // We clear both operands to indicate move that's been eliminated.
6489 void Eliminate() {
6490 source_ = destination_ = Location::NoLocation();
6491 }
6492
6493 bool IsEliminated() const {
6494 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6495 return source_.IsInvalid();
6496 }
6497
Alexey Frunze4dda3372015-06-01 18:31:49 -07006498 Primitive::Type GetType() const { return type_; }
6499
Nicolas Geoffray90218252015-04-15 11:56:51 +01006500 bool Is64BitMove() const {
6501 return Primitive::Is64BitType(type_);
6502 }
6503
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006504 HInstruction* GetInstruction() const { return instruction_; }
6505
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006506 private:
6507 Location source_;
6508 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006509 // The type this move is for.
6510 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006511 // The instruction this move is assocatied with. Null when this move is
6512 // for moving an input in the expected locations of user (including a phi user).
6513 // This is only used in debug mode, to ensure we do not connect interval siblings
6514 // in the same parallel move.
6515 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006516};
6517
Roland Levillainc9285912015-12-18 10:38:42 +00006518std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6519
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006520static constexpr size_t kDefaultNumberOfMoves = 4;
6521
Vladimir Markofcb503c2016-05-18 12:48:17 +01006522class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006523 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006524 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006525 : HTemplateInstruction(SideEffects::None(), dex_pc),
6526 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6527 moves_.reserve(kDefaultNumberOfMoves);
6528 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006529
Nicolas Geoffray90218252015-04-15 11:56:51 +01006530 void AddMove(Location source,
6531 Location destination,
6532 Primitive::Type type,
6533 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006534 DCHECK(source.IsValid());
6535 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006536 if (kIsDebugBuild) {
6537 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006538 for (const MoveOperands& move : moves_) {
6539 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006540 // Special case the situation where the move is for the spill slot
6541 // of the instruction.
6542 if ((GetPrevious() == instruction)
6543 || ((GetPrevious() == nullptr)
6544 && instruction->IsPhi()
6545 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006546 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006547 << "Doing parallel moves for the same instruction.";
6548 } else {
6549 DCHECK(false) << "Doing parallel moves for the same instruction.";
6550 }
6551 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006552 }
6553 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006554 for (const MoveOperands& move : moves_) {
6555 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006556 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006557 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006558 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006559 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006560 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006561 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006562 }
6563
Vladimir Marko225b6462015-09-28 12:17:40 +01006564 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006565 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006566 }
6567
Vladimir Marko225b6462015-09-28 12:17:40 +01006568 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006569
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006570 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006571
6572 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006573 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006574
6575 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6576};
6577
Mark Mendell0616ae02015-04-17 12:49:27 -04006578} // namespace art
6579
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006580#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6581#include "nodes_shared.h"
6582#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006583#ifdef ART_ENABLE_CODEGEN_arm
6584#include "nodes_arm.h"
6585#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006586#ifdef ART_ENABLE_CODEGEN_arm64
6587#include "nodes_arm64.h"
6588#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006589#ifdef ART_ENABLE_CODEGEN_mips
6590#include "nodes_mips.h"
6591#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006592#ifdef ART_ENABLE_CODEGEN_x86
6593#include "nodes_x86.h"
6594#endif
6595
6596namespace art {
6597
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006598class HGraphVisitor : public ValueObject {
6599 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006600 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6601 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006602
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006603 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006604 virtual void VisitBasicBlock(HBasicBlock* block);
6605
Roland Levillain633021e2014-10-01 14:12:25 +01006606 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006607 void VisitInsertionOrder();
6608
Roland Levillain633021e2014-10-01 14:12:25 +01006609 // Visit the graph following dominator tree reverse post-order.
6610 void VisitReversePostOrder();
6611
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006612 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006613
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006614 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006615#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006616 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6617
6618 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6619
6620#undef DECLARE_VISIT_INSTRUCTION
6621
6622 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006623 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006624
6625 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6626};
6627
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006628class HGraphDelegateVisitor : public HGraphVisitor {
6629 public:
6630 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6631 virtual ~HGraphDelegateVisitor() {}
6632
6633 // Visit functions that delegate to to super class.
6634#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006635 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006636
6637 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6638
6639#undef DECLARE_VISIT_INSTRUCTION
6640
6641 private:
6642 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6643};
6644
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006645// Iterator over the blocks that art part of the loop. Includes blocks part
6646// of an inner loop. The order in which the blocks are iterated is on their
6647// block id.
6648class HBlocksInLoopIterator : public ValueObject {
6649 public:
6650 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6651 : blocks_in_loop_(info.GetBlocks()),
6652 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6653 index_(0) {
6654 if (!blocks_in_loop_.IsBitSet(index_)) {
6655 Advance();
6656 }
6657 }
6658
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006659 bool Done() const { return index_ == blocks_.size(); }
6660 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006661 void Advance() {
6662 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006663 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006664 if (blocks_in_loop_.IsBitSet(index_)) {
6665 break;
6666 }
6667 }
6668 }
6669
6670 private:
6671 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006672 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006673 size_t index_;
6674
6675 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6676};
6677
Mingyao Yang3584bce2015-05-19 16:01:59 -07006678// Iterator over the blocks that art part of the loop. Includes blocks part
6679// of an inner loop. The order in which the blocks are iterated is reverse
6680// post order.
6681class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6682 public:
6683 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6684 : blocks_in_loop_(info.GetBlocks()),
6685 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6686 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006687 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006688 Advance();
6689 }
6690 }
6691
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006692 bool Done() const { return index_ == blocks_.size(); }
6693 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006694 void Advance() {
6695 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006696 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6697 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006698 break;
6699 }
6700 }
6701 }
6702
6703 private:
6704 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006705 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006706 size_t index_;
6707
6708 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6709};
6710
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006711inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006712 if (constant->IsIntConstant()) {
6713 return constant->AsIntConstant()->GetValue();
6714 } else if (constant->IsLongConstant()) {
6715 return constant->AsLongConstant()->GetValue();
6716 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006717 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006718 return 0;
6719 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006720}
6721
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006722#define INSTRUCTION_TYPE_CHECK(type, super) \
6723 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6724 inline const H##type* HInstruction::As##type() const { \
6725 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6726 } \
6727 inline H##type* HInstruction::As##type() { \
6728 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6729 }
6730
6731 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6732#undef INSTRUCTION_TYPE_CHECK
6733
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006734// Create space in `blocks` for adding `number_of_new_blocks` entries
6735// starting at location `at`. Blocks after `at` are moved accordingly.
6736inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6737 size_t number_of_new_blocks,
6738 size_t after) {
6739 DCHECK_LT(after, blocks->size());
6740 size_t old_size = blocks->size();
6741 size_t new_size = old_size + number_of_new_blocks;
6742 blocks->resize(new_size);
6743 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6744}
6745
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006746} // namespace art
6747
6748#endif // ART_COMPILER_OPTIMIZING_NODES_H_