blob: 6a45149509e286a48e86e661347ad44f607830f0 [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
Vladimir Marko372f10e2016-05-17 16:30:10 +01001858 void SetRawInputAt(size_t index, HInstruction* input) {
1859 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1860 }
1861
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001862 virtual void Accept(HGraphVisitor* visitor) = 0;
1863 virtual const char* DebugName() const = 0;
1864
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001865 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1866
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001867 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001868
1869 uint32_t GetDexPc() const { return dex_pc_; }
1870
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001871 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001872
Roland Levillaine161a2a2014-10-03 12:45:18 +01001873 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001874 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001875
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001876 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001877 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001878
Calin Juravle10e244f2015-01-26 18:54:32 +00001879 // Does not apply for all instructions, but having this at top level greatly
1880 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001881 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001882 virtual bool CanBeNull() const {
1883 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1884 return true;
1885 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001886
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001887 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001888 return false;
1889 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001890
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001891 virtual bool IsActualObject() const {
1892 return GetType() == Primitive::kPrimNot;
1893 }
1894
Calin Juravle2e768302015-07-28 14:41:11 +00001895 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001896
Calin Juravle61d544b2015-02-23 16:46:57 +00001897 ReferenceTypeInfo GetReferenceTypeInfo() const {
1898 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001899 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001900 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001901 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001902
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001903 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001904 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001905 // Note: fixup_end remains valid across push_front().
1906 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1907 HUseListNode<HInstruction*>* new_node =
1908 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1909 uses_.push_front(*new_node);
1910 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001911 }
1912
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001913 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001914 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001915 // Note: env_fixup_end remains valid across push_front().
1916 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1917 HUseListNode<HEnvironment*>* new_node =
1918 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1919 env_uses_.push_front(*new_node);
1920 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001921 }
1922
David Brazdil1abb4192015-02-17 18:33:36 +00001923 void RemoveAsUserOfInput(size_t input) {
1924 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001925 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1926 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1927 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001928 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001929
Vladimir Marko372f10e2016-05-17 16:30:10 +01001930 void RemoveAsUserOfAllInputs() {
1931 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1932 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1933 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1934 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1935 }
1936 }
1937
David Brazdil1abb4192015-02-17 18:33:36 +00001938 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1939 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001940
Vladimir Marko46817b82016-03-29 12:21:58 +01001941 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1942 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1943 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001944 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001945 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001946 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001947
Aart Bikcc42be02016-10-20 16:14:16 -07001948 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07001949 return
1950 !HasSideEffects() &&
1951 !CanThrow() &&
1952 !IsSuspendCheck() &&
1953 !IsControlFlow() &&
1954 !IsNativeDebugInfo() &&
1955 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07001956 // If we added an explicit barrier then we should keep it.
1957 !IsMemoryBarrier();
1958 }
1959
Aart Bikcc42be02016-10-20 16:14:16 -07001960 bool IsDeadAndRemovable() const {
1961 return IsRemovable() && !HasUses();
1962 }
1963
Roland Levillain6c82d402014-10-13 16:10:27 +01001964 // Does this instruction strictly dominate `other_instruction`?
1965 // Returns false if this instruction and `other_instruction` are the same.
1966 // Aborts if this instruction and `other_instruction` are both phis.
1967 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001968
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001969 int GetId() const { return id_; }
1970 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001971
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001972 int GetSsaIndex() const { return ssa_index_; }
1973 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1974 bool HasSsaIndex() const { return ssa_index_ != -1; }
1975
1976 bool HasEnvironment() const { return environment_ != nullptr; }
1977 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001978 // Set the `environment_` field. Raw because this method does not
1979 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001980 void SetRawEnvironment(HEnvironment* environment) {
1981 DCHECK(environment_ == nullptr);
1982 DCHECK_EQ(environment->GetHolder(), this);
1983 environment_ = environment;
1984 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001985
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001986 void InsertRawEnvironment(HEnvironment* environment) {
1987 DCHECK(environment_ != nullptr);
1988 DCHECK_EQ(environment->GetHolder(), this);
1989 DCHECK(environment->GetParent() == nullptr);
1990 environment->parent_ = environment_;
1991 environment_ = environment;
1992 }
1993
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001994 void RemoveEnvironment();
1995
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001996 // Set the environment of this instruction, copying it from `environment`. While
1997 // copying, the uses lists are being updated.
1998 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001999 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002000 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002001 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002002 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002003 if (environment->GetParent() != nullptr) {
2004 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2005 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002006 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002007
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002008 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2009 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002010 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002011 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002012 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002013 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002014 if (environment->GetParent() != nullptr) {
2015 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2016 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002017 }
2018
Nicolas Geoffray39468442014-09-02 15:17:15 +01002019 // Returns the number of entries in the environment. Typically, that is the
2020 // number of dex registers in a method. It could be more in case of inlining.
2021 size_t EnvironmentSize() const;
2022
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002023 LocationSummary* GetLocations() const { return locations_; }
2024 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002025
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002026 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002027 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002028
Alexandre Rames188d4312015-04-09 18:30:21 +01002029 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2030 // uses of this instruction by `other` are *not* updated.
2031 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2032 ReplaceWith(other);
2033 other->ReplaceInput(this, use_index);
2034 }
2035
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002036 // Move `this` instruction before `cursor`.
2037 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002038
Vladimir Markofb337ea2015-11-25 15:25:10 +00002039 // Move `this` before its first user and out of any loops. If there is no
2040 // out-of-loop user that dominates all other users, move the instruction
2041 // to the end of the out-of-loop common dominator of the user's blocks.
2042 //
2043 // This can be used only on non-throwing instructions with no side effects that
2044 // have at least one use but no environment uses.
2045 void MoveBeforeFirstUserAndOutOfLoops();
2046
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002047#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002048 bool Is##type() const; \
2049 const H##type* As##type() const; \
2050 H##type* As##type();
2051
2052 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2053#undef INSTRUCTION_TYPE_CHECK
2054
2055#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002056 bool Is##type() const { return (As##type() != nullptr); } \
2057 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002058 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002059 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002060#undef INSTRUCTION_TYPE_CHECK
2061
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002062 // Returns whether the instruction can be moved within the graph.
2063 virtual bool CanBeMoved() const { return false; }
2064
2065 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002066 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002067 return false;
2068 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002069
2070 // Returns whether any data encoded in the two instructions is equal.
2071 // This method does not look at the inputs. Both instructions must be
2072 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002073 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002074 return false;
2075 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002076
2077 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002078 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002079 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002080 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002081
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002082 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2083 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2084 // the virtual function because the __attribute__((__pure__)) doesn't really
2085 // apply the strong requirement for virtual functions, preventing optimizations.
2086 InstructionKind GetKind() const PURE;
2087 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002088
2089 virtual size_t ComputeHashCode() const {
2090 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002091 for (const HInstruction* input : GetInputs()) {
2092 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002093 }
2094 return result;
2095 }
2096
2097 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002098 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002099 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002100
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002101 size_t GetLifetimePosition() const { return lifetime_position_; }
2102 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2103 LiveInterval* GetLiveInterval() const { return live_interval_; }
2104 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2105 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2106
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002107 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2108
2109 // Returns whether the code generation of the instruction will require to have access
2110 // to the current method. Such instructions are:
2111 // (1): Instructions that require an environment, as calling the runtime requires
2112 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002113 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2114 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002115 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002116 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002117 }
2118
Vladimir Markodc151b22015-10-15 18:02:30 +01002119 // Returns whether the code generation of the instruction will require to have access
2120 // to the dex cache of the current method's declaring class via the current method.
2121 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002122
Mark Mendellc4701932015-04-10 13:18:51 -04002123 // Does this instruction have any use in an environment before
2124 // control flow hits 'other'?
2125 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2126
2127 // Remove all references to environment uses of this instruction.
2128 // The caller must ensure that this is safe to do.
2129 void RemoveEnvironmentUsers();
2130
Vladimir Markoa1de9182016-02-25 11:37:38 +00002131 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2132 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002133
David Brazdil1abb4192015-02-17 18:33:36 +00002134 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002135 // If set, the machine code for this instruction is assumed to be generated by
2136 // its users. Used by liveness analysis to compute use positions accordingly.
2137 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2138 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2139 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2140 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2141
Vladimir Marko372f10e2016-05-17 16:30:10 +01002142 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2143 return GetInputRecords()[i];
2144 }
2145
2146 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2147 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2148 input_records[index] = input;
2149 }
David Brazdil1abb4192015-02-17 18:33:36 +00002150
Vladimir Markoa1de9182016-02-25 11:37:38 +00002151 uint32_t GetPackedFields() const {
2152 return packed_fields_;
2153 }
2154
2155 template <size_t flag>
2156 bool GetPackedFlag() const {
2157 return (packed_fields_ & (1u << flag)) != 0u;
2158 }
2159
2160 template <size_t flag>
2161 void SetPackedFlag(bool value = true) {
2162 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2163 }
2164
2165 template <typename BitFieldType>
2166 typename BitFieldType::value_type GetPackedField() const {
2167 return BitFieldType::Decode(packed_fields_);
2168 }
2169
2170 template <typename BitFieldType>
2171 void SetPackedField(typename BitFieldType::value_type value) {
2172 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2173 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2174 }
2175
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002176 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002177 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2178 auto before_use_node = uses_.before_begin();
2179 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2180 HInstruction* user = use_node->GetUser();
2181 size_t input_index = use_node->GetIndex();
2182 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2183 before_use_node = use_node;
2184 }
2185 }
2186
2187 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2188 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2189 if (next != uses_.end()) {
2190 HInstruction* next_user = next->GetUser();
2191 size_t next_index = next->GetIndex();
2192 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2193 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2194 }
2195 }
2196
2197 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2198 auto before_env_use_node = env_uses_.before_begin();
2199 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2200 HEnvironment* user = env_use_node->GetUser();
2201 size_t input_index = env_use_node->GetIndex();
2202 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2203 before_env_use_node = env_use_node;
2204 }
2205 }
2206
2207 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2208 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2209 if (next != env_uses_.end()) {
2210 HEnvironment* next_user = next->GetUser();
2211 size_t next_index = next->GetIndex();
2212 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2213 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2214 }
2215 }
David Brazdil1abb4192015-02-17 18:33:36 +00002216
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002217 HInstruction* previous_;
2218 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002219 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002220 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002221
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002222 // An instruction gets an id when it is added to the graph.
2223 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002224 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002225 int id_;
2226
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002227 // When doing liveness analysis, instructions that have uses get an SSA index.
2228 int ssa_index_;
2229
Vladimir Markoa1de9182016-02-25 11:37:38 +00002230 // Packed fields.
2231 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002232
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002233 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002234 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002235
2236 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002237 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002238
Nicolas Geoffray39468442014-09-02 15:17:15 +01002239 // The environment associated with this instruction. Not null if the instruction
2240 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002241 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002242
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002243 // Set by the code generator.
2244 LocationSummary* locations_;
2245
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002246 // Set by the liveness analysis.
2247 LiveInterval* live_interval_;
2248
2249 // Set by the liveness analysis, this is the position in a linear
2250 // order of blocks where this instruction's live interval start.
2251 size_t lifetime_position_;
2252
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002253 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002254
Vladimir Markoa1de9182016-02-25 11:37:38 +00002255 // The reference handle part of the reference type info.
2256 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002257 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002258 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002259
David Brazdil1abb4192015-02-17 18:33:36 +00002260 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002261 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002262 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002263 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002264 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002265
2266 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2267};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002268std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002269
2270class HInstructionIterator : public ValueObject {
2271 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002272 explicit HInstructionIterator(const HInstructionList& instructions)
2273 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002274 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002275 }
2276
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002277 bool Done() const { return instruction_ == nullptr; }
2278 HInstruction* Current() const { return instruction_; }
2279 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002280 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002281 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002282 }
2283
2284 private:
2285 HInstruction* instruction_;
2286 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002287
2288 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002289};
2290
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002291class HBackwardInstructionIterator : public ValueObject {
2292 public:
2293 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2294 : instruction_(instructions.last_instruction_) {
2295 next_ = Done() ? nullptr : instruction_->GetPrevious();
2296 }
2297
2298 bool Done() const { return instruction_ == nullptr; }
2299 HInstruction* Current() const { return instruction_; }
2300 void Advance() {
2301 instruction_ = next_;
2302 next_ = Done() ? nullptr : instruction_->GetPrevious();
2303 }
2304
2305 private:
2306 HInstruction* instruction_;
2307 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002308
2309 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002310};
2311
Vladimir Markof9f64412015-09-02 14:05:49 +01002312template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002313class HTemplateInstruction: public HInstruction {
2314 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002315 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002316 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002317 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002318
Vladimir Marko372f10e2016-05-17 16:30:10 +01002319 using HInstruction::GetInputRecords; // Keep the const version visible.
2320 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2321 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002322 }
2323
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002324 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002325 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002326
2327 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002328};
2329
Vladimir Markof9f64412015-09-02 14:05:49 +01002330// HTemplateInstruction specialization for N=0.
2331template<>
2332class HTemplateInstruction<0>: public HInstruction {
2333 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002334 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002335 : HInstruction(side_effects, dex_pc) {}
2336
Vladimir Markof9f64412015-09-02 14:05:49 +01002337 virtual ~HTemplateInstruction() {}
2338
Vladimir Marko372f10e2016-05-17 16:30:10 +01002339 using HInstruction::GetInputRecords; // Keep the const version visible.
2340 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2341 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002342 }
2343
2344 private:
2345 friend class SsaBuilder;
2346};
2347
Dave Allison20dfc792014-06-16 20:44:29 -07002348template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002349class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002350 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002351 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002352 : HTemplateInstruction<N>(side_effects, dex_pc) {
2353 this->template SetPackedField<TypeField>(type);
2354 }
Dave Allison20dfc792014-06-16 20:44:29 -07002355 virtual ~HExpression() {}
2356
Vladimir Markoa1de9182016-02-25 11:37:38 +00002357 Primitive::Type GetType() const OVERRIDE {
2358 return TypeField::Decode(this->GetPackedFields());
2359 }
Dave Allison20dfc792014-06-16 20:44:29 -07002360
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002361 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002362 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2363 static constexpr size_t kFieldTypeSize =
2364 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2365 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2366 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2367 "Too many packed fields.");
2368 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002369};
2370
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002371// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2372// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002373class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002374 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002375 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2376 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002377
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002378 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002379
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002380 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002381
2382 private:
2383 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2384};
2385
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002386// Represents dex's RETURN opcodes. A HReturn is a control flow
2387// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002388class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002389 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002390 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2391 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002392 SetRawInputAt(0, value);
2393 }
2394
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002395 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002396
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002397 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002398
2399 private:
2400 DISALLOW_COPY_AND_ASSIGN(HReturn);
2401};
2402
Vladimir Markofcb503c2016-05-18 12:48:17 +01002403class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002404 public:
2405 HPhi(ArenaAllocator* arena,
2406 uint32_t reg_number,
2407 size_t number_of_inputs,
2408 Primitive::Type type,
2409 uint32_t dex_pc = kNoDexPc)
2410 : HInstruction(SideEffects::None(), dex_pc),
2411 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2412 reg_number_(reg_number) {
2413 SetPackedField<TypeField>(ToPhiType(type));
2414 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2415 // Phis are constructed live and marked dead if conflicting or unused.
2416 // Individual steps of SsaBuilder should assume that if a phi has been
2417 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2418 SetPackedFlag<kFlagIsLive>(true);
2419 SetPackedFlag<kFlagCanBeNull>(true);
2420 }
2421
2422 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2423 static Primitive::Type ToPhiType(Primitive::Type type) {
2424 return Primitive::PrimitiveKind(type);
2425 }
2426
2427 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2428
Vladimir Marko372f10e2016-05-17 16:30:10 +01002429 using HInstruction::GetInputRecords; // Keep the const version visible.
2430 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2431 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2432 }
David Brazdildee58d62016-04-07 09:54:26 +00002433
2434 void AddInput(HInstruction* input);
2435 void RemoveInputAt(size_t index);
2436
2437 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2438 void SetType(Primitive::Type new_type) {
2439 // Make sure that only valid type changes occur. The following are allowed:
2440 // (1) int -> float/ref (primitive type propagation),
2441 // (2) long -> double (primitive type propagation).
2442 DCHECK(GetType() == new_type ||
2443 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2444 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2445 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2446 SetPackedField<TypeField>(new_type);
2447 }
2448
2449 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2450 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2451
2452 uint32_t GetRegNumber() const { return reg_number_; }
2453
2454 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2455 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2456 bool IsDead() const { return !IsLive(); }
2457 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2458
Vladimir Markoe9004912016-06-16 16:50:52 +01002459 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002460 return other != nullptr
2461 && other->IsPhi()
2462 && other->AsPhi()->GetBlock() == GetBlock()
2463 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2464 }
2465
2466 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2467 // An equivalent phi is a phi having the same dex register and type.
2468 // It assumes that phis with the same dex register are adjacent.
2469 HPhi* GetNextEquivalentPhiWithSameType() {
2470 HInstruction* next = GetNext();
2471 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2472 if (next->GetType() == GetType()) {
2473 return next->AsPhi();
2474 }
2475 next = next->GetNext();
2476 }
2477 return nullptr;
2478 }
2479
2480 DECLARE_INSTRUCTION(Phi);
2481
David Brazdildee58d62016-04-07 09:54:26 +00002482 private:
2483 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2484 static constexpr size_t kFieldTypeSize =
2485 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2486 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2487 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2488 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2489 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2490 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2491
Vladimir Marko372f10e2016-05-17 16:30:10 +01002492 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002493 const uint32_t reg_number_;
2494
2495 DISALLOW_COPY_AND_ASSIGN(HPhi);
2496};
2497
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002498// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002499// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002500// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002501class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002502 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002503 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002504
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002505 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002506
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002507 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002508
2509 private:
2510 DISALLOW_COPY_AND_ASSIGN(HExit);
2511};
2512
2513// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002514class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002515 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002516 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002517
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002518 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002519
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002520 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002521 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002522 }
2523
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002524 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002525
2526 private:
2527 DISALLOW_COPY_AND_ASSIGN(HGoto);
2528};
2529
Roland Levillain9867bc72015-08-05 10:21:34 +01002530class HConstant : public HExpression<0> {
2531 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002532 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2533 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002534
2535 bool CanBeMoved() const OVERRIDE { return true; }
2536
Roland Levillain1a653882016-03-18 18:05:57 +00002537 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002538 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002539 // Is this constant 0 in the arithmetic sense?
2540 virtual bool IsArithmeticZero() const { return false; }
2541 // Is this constant a 0-bit pattern?
2542 virtual bool IsZeroBitPattern() const { return false; }
2543 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002544 virtual bool IsOne() const { return false; }
2545
David Brazdil77a48ae2015-09-15 12:34:04 +00002546 virtual uint64_t GetValueAsUint64() const = 0;
2547
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002548 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002549
2550 private:
2551 DISALLOW_COPY_AND_ASSIGN(HConstant);
2552};
2553
Vladimir Markofcb503c2016-05-18 12:48:17 +01002554class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002555 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002556 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002557 return true;
2558 }
2559
David Brazdil77a48ae2015-09-15 12:34:04 +00002560 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2561
Roland Levillain9867bc72015-08-05 10:21:34 +01002562 size_t ComputeHashCode() const OVERRIDE { return 0; }
2563
Roland Levillain1a653882016-03-18 18:05:57 +00002564 // The null constant representation is a 0-bit pattern.
2565 virtual bool IsZeroBitPattern() const { return true; }
2566
Roland Levillain9867bc72015-08-05 10:21:34 +01002567 DECLARE_INSTRUCTION(NullConstant);
2568
2569 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002570 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002571
2572 friend class HGraph;
2573 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2574};
2575
2576// Constants of the type int. Those can be from Dex instructions, or
2577// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002578class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002579 public:
2580 int32_t GetValue() const { return value_; }
2581
David Brazdil9f389d42015-10-01 14:32:56 +01002582 uint64_t GetValueAsUint64() const OVERRIDE {
2583 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2584 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002585
Vladimir Marko372f10e2016-05-17 16:30:10 +01002586 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002587 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002588 return other->AsIntConstant()->value_ == value_;
2589 }
2590
2591 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2592
2593 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002594 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2595 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002596 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2597
Roland Levillain1a653882016-03-18 18:05:57 +00002598 // Integer constants are used to encode Boolean values as well,
2599 // where 1 means true and 0 means false.
2600 bool IsTrue() const { return GetValue() == 1; }
2601 bool IsFalse() const { return GetValue() == 0; }
2602
Roland Levillain9867bc72015-08-05 10:21:34 +01002603 DECLARE_INSTRUCTION(IntConstant);
2604
2605 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002606 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2607 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2608 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2609 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002610
2611 const int32_t value_;
2612
2613 friend class HGraph;
2614 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2615 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2616 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2617};
2618
Vladimir Markofcb503c2016-05-18 12:48:17 +01002619class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002620 public:
2621 int64_t GetValue() const { return value_; }
2622
David Brazdil77a48ae2015-09-15 12:34:04 +00002623 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2624
Vladimir Marko372f10e2016-05-17 16:30:10 +01002625 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002626 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002627 return other->AsLongConstant()->value_ == value_;
2628 }
2629
2630 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2631
2632 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002633 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2634 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002635 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2636
2637 DECLARE_INSTRUCTION(LongConstant);
2638
2639 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002640 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2641 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002642
2643 const int64_t value_;
2644
2645 friend class HGraph;
2646 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2647};
Dave Allison20dfc792014-06-16 20:44:29 -07002648
Vladimir Markofcb503c2016-05-18 12:48:17 +01002649class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002650 public:
2651 float GetValue() const { return value_; }
2652
2653 uint64_t GetValueAsUint64() const OVERRIDE {
2654 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2655 }
2656
Vladimir Marko372f10e2016-05-17 16:30:10 +01002657 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002658 DCHECK(other->IsFloatConstant()) << other->DebugName();
2659 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2660 }
2661
2662 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2663
2664 bool IsMinusOne() const OVERRIDE {
2665 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2666 }
Roland Levillain1a653882016-03-18 18:05:57 +00002667 bool IsArithmeticZero() const OVERRIDE {
2668 return std::fpclassify(value_) == FP_ZERO;
2669 }
2670 bool IsArithmeticPositiveZero() const {
2671 return IsArithmeticZero() && !std::signbit(value_);
2672 }
2673 bool IsArithmeticNegativeZero() const {
2674 return IsArithmeticZero() && std::signbit(value_);
2675 }
2676 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002677 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002678 }
2679 bool IsOne() const OVERRIDE {
2680 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2681 }
2682 bool IsNaN() const {
2683 return std::isnan(value_);
2684 }
2685
2686 DECLARE_INSTRUCTION(FloatConstant);
2687
2688 private:
2689 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2690 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2691 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2692 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2693
2694 const float value_;
2695
2696 // Only the SsaBuilder and HGraph can create floating-point constants.
2697 friend class SsaBuilder;
2698 friend class HGraph;
2699 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2700};
2701
Vladimir Markofcb503c2016-05-18 12:48:17 +01002702class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002703 public:
2704 double GetValue() const { return value_; }
2705
2706 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2707
Vladimir Marko372f10e2016-05-17 16:30:10 +01002708 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002709 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2710 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2711 }
2712
2713 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2714
2715 bool IsMinusOne() const OVERRIDE {
2716 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2717 }
Roland Levillain1a653882016-03-18 18:05:57 +00002718 bool IsArithmeticZero() const OVERRIDE {
2719 return std::fpclassify(value_) == FP_ZERO;
2720 }
2721 bool IsArithmeticPositiveZero() const {
2722 return IsArithmeticZero() && !std::signbit(value_);
2723 }
2724 bool IsArithmeticNegativeZero() const {
2725 return IsArithmeticZero() && std::signbit(value_);
2726 }
2727 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002728 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002729 }
2730 bool IsOne() const OVERRIDE {
2731 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2732 }
2733 bool IsNaN() const {
2734 return std::isnan(value_);
2735 }
2736
2737 DECLARE_INSTRUCTION(DoubleConstant);
2738
2739 private:
2740 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2741 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2742 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2743 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2744
2745 const double value_;
2746
2747 // Only the SsaBuilder and HGraph can create floating-point constants.
2748 friend class SsaBuilder;
2749 friend class HGraph;
2750 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2751};
2752
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002753// Conditional branch. A block ending with an HIf instruction must have
2754// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002755class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002756 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002757 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2758 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002759 SetRawInputAt(0, input);
2760 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002761
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002762 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002763
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002764 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002765 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002766 }
2767
2768 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002769 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002770 }
2771
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002772 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002773
2774 private:
2775 DISALLOW_COPY_AND_ASSIGN(HIf);
2776};
2777
David Brazdilfc6a86a2015-06-26 10:33:45 +00002778
2779// Abstract instruction which marks the beginning and/or end of a try block and
2780// links it to the respective exception handlers. Behaves the same as a Goto in
2781// non-exceptional control flow.
2782// Normal-flow successor is stored at index zero, exception handlers under
2783// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002784class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002785 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002786 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002787 kEntry,
2788 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002789 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002790 };
2791
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002792 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002793 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2794 SetPackedField<BoundaryKindField>(kind);
2795 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002796
2797 bool IsControlFlow() const OVERRIDE { return true; }
2798
2799 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002800 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002801
David Brazdild26a4112015-11-10 11:07:31 +00002802 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2803 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2804 }
2805
David Brazdilfc6a86a2015-06-26 10:33:45 +00002806 // Returns whether `handler` is among its exception handlers (non-zero index
2807 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002808 bool HasExceptionHandler(const HBasicBlock& handler) const {
2809 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002810 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002811 }
2812
2813 // If not present already, adds `handler` to its block's list of exception
2814 // handlers.
2815 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002816 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002817 GetBlock()->AddSuccessor(handler);
2818 }
2819 }
2820
Vladimir Markoa1de9182016-02-25 11:37:38 +00002821 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2822 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002823
David Brazdilffee3d32015-07-06 11:48:53 +01002824 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2825
David Brazdilfc6a86a2015-06-26 10:33:45 +00002826 DECLARE_INSTRUCTION(TryBoundary);
2827
2828 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002829 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2830 static constexpr size_t kFieldBoundaryKindSize =
2831 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2832 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2833 kFieldBoundaryKind + kFieldBoundaryKindSize;
2834 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2835 "Too many packed fields.");
2836 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002837
2838 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2839};
2840
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002841// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002842class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002843 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002844 // We set CanTriggerGC to prevent any intermediate address to be live
2845 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002846 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002847 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002848 SetRawInputAt(0, cond);
2849 }
2850
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002851 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002852 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002853 return true;
2854 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002855 bool NeedsEnvironment() const OVERRIDE { return true; }
2856 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002857
2858 DECLARE_INSTRUCTION(Deoptimize);
2859
2860 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002861 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2862};
2863
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002864// Represents the ArtMethod that was passed as a first argument to
2865// the method. It is used by instructions that depend on it, like
2866// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002867class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002868 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002869 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2870 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002871
2872 DECLARE_INSTRUCTION(CurrentMethod);
2873
2874 private:
2875 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2876};
2877
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002878// Fetches an ArtMethod from the virtual table or the interface method table
2879// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002880class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002881 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002882 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002883 kVTable,
2884 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002885 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002886 };
2887 HClassTableGet(HInstruction* cls,
2888 Primitive::Type type,
2889 TableKind kind,
2890 size_t index,
2891 uint32_t dex_pc)
2892 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002893 index_(index) {
2894 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002895 SetRawInputAt(0, cls);
2896 }
2897
2898 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002899 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002900 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002901 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002902 }
2903
Vladimir Markoa1de9182016-02-25 11:37:38 +00002904 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002905 size_t GetIndex() const { return index_; }
2906
2907 DECLARE_INSTRUCTION(ClassTableGet);
2908
2909 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002910 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2911 static constexpr size_t kFieldTableKindSize =
2912 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2913 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2914 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2915 "Too many packed fields.");
2916 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2917
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002918 // The index of the ArtMethod in the table.
2919 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002920
2921 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2922};
2923
Mark Mendellfe57faa2015-09-18 09:26:15 -04002924// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2925// have one successor for each entry in the switch table, and the final successor
2926// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002927class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002928 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002929 HPackedSwitch(int32_t start_value,
2930 uint32_t num_entries,
2931 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002932 uint32_t dex_pc = kNoDexPc)
2933 : HTemplateInstruction(SideEffects::None(), dex_pc),
2934 start_value_(start_value),
2935 num_entries_(num_entries) {
2936 SetRawInputAt(0, input);
2937 }
2938
2939 bool IsControlFlow() const OVERRIDE { return true; }
2940
2941 int32_t GetStartValue() const { return start_value_; }
2942
Vladimir Marko211c2112015-09-24 16:52:33 +01002943 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002944
2945 HBasicBlock* GetDefaultBlock() const {
2946 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002947 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002948 }
2949 DECLARE_INSTRUCTION(PackedSwitch);
2950
2951 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002952 const int32_t start_value_;
2953 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002954
2955 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2956};
2957
Roland Levillain88cb1752014-10-20 16:36:47 +01002958class HUnaryOperation : public HExpression<1> {
2959 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002960 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2961 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002962 SetRawInputAt(0, input);
2963 }
2964
2965 HInstruction* GetInput() const { return InputAt(0); }
2966 Primitive::Type GetResultType() const { return GetType(); }
2967
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002968 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002969 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002970 return true;
2971 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002972
Roland Levillain31dd3d62016-02-16 12:21:02 +00002973 // Try to statically evaluate `this` and return a HConstant
2974 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002975 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002976 HConstant* TryStaticEvaluation() const;
2977
2978 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002979 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2980 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002981 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2982 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002983
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002984 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002985
2986 private:
2987 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2988};
2989
Dave Allison20dfc792014-06-16 20:44:29 -07002990class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002991 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002992 HBinaryOperation(Primitive::Type result_type,
2993 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002994 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002995 SideEffects side_effects = SideEffects::None(),
2996 uint32_t dex_pc = kNoDexPc)
2997 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002998 SetRawInputAt(0, left);
2999 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003000 }
3001
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003002 HInstruction* GetLeft() const { return InputAt(0); }
3003 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003004 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003005
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003006 virtual bool IsCommutative() const { return false; }
3007
3008 // Put constant on the right.
3009 // Returns whether order is changed.
3010 bool OrderInputsWithConstantOnTheRight() {
3011 HInstruction* left = InputAt(0);
3012 HInstruction* right = InputAt(1);
3013 if (left->IsConstant() && !right->IsConstant()) {
3014 ReplaceInput(right, 0);
3015 ReplaceInput(left, 1);
3016 return true;
3017 }
3018 return false;
3019 }
3020
3021 // Order inputs by instruction id, but favor constant on the right side.
3022 // This helps GVN for commutative ops.
3023 void OrderInputs() {
3024 DCHECK(IsCommutative());
3025 HInstruction* left = InputAt(0);
3026 HInstruction* right = InputAt(1);
3027 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3028 return;
3029 }
3030 if (OrderInputsWithConstantOnTheRight()) {
3031 return;
3032 }
3033 // Order according to instruction id.
3034 if (left->GetId() > right->GetId()) {
3035 ReplaceInput(right, 0);
3036 ReplaceInput(left, 1);
3037 }
3038 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003039
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003040 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003041 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003042 return true;
3043 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003044
Roland Levillain31dd3d62016-02-16 12:21:02 +00003045 // Try to statically evaluate `this` and return a HConstant
3046 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003047 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003048 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003049
3050 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003051 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3052 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003053 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3054 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003055 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003056 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3057 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003058 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3059 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003060 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3061 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003062 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003063 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3064 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003065
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003066 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003067 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003068 HConstant* GetConstantRight() const;
3069
3070 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003071 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003072 HInstruction* GetLeastConstantLeft() const;
3073
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003074 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003075
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003076 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003077 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3078};
3079
Mark Mendellc4701932015-04-10 13:18:51 -04003080// The comparison bias applies for floating point operations and indicates how NaN
3081// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003082enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003083 kNoBias, // bias is not applicable (i.e. for long operation)
3084 kGtBias, // return 1 for NaN comparisons
3085 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003086 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003087};
3088
Roland Levillain31dd3d62016-02-16 12:21:02 +00003089std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3090
Dave Allison20dfc792014-06-16 20:44:29 -07003091class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003092 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003093 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003094 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3095 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3096 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003097
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003098 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003099 // `instruction`, and disregard moves in between.
3100 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003101
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003102 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003103
3104 virtual IfCondition GetCondition() const = 0;
3105
Mark Mendellc4701932015-04-10 13:18:51 -04003106 virtual IfCondition GetOppositeCondition() const = 0;
3107
Vladimir Markoa1de9182016-02-25 11:37:38 +00003108 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003109 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3110
Vladimir Markoa1de9182016-02-25 11:37:38 +00003111 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3112 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003113
Vladimir Marko372f10e2016-05-17 16:30:10 +01003114 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003115 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003116 }
3117
Roland Levillain4fa13f62015-07-06 18:11:54 +01003118 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003119 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003120 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003121 if (if_cond == kCondNE) {
3122 return true;
3123 } else if (if_cond == kCondEQ) {
3124 return false;
3125 }
3126 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003127 }
3128
3129 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003130 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003131 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003132 if (if_cond == kCondEQ) {
3133 return true;
3134 } else if (if_cond == kCondNE) {
3135 return false;
3136 }
3137 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003138 }
3139
Roland Levillain31dd3d62016-02-16 12:21:02 +00003140 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003141 // Needed if we merge a HCompare into a HCondition.
3142 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3143 static constexpr size_t kFieldComparisonBiasSize =
3144 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3145 static constexpr size_t kNumberOfConditionPackedBits =
3146 kFieldComparisonBias + kFieldComparisonBiasSize;
3147 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3148 using ComparisonBiasField =
3149 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3150
Roland Levillain31dd3d62016-02-16 12:21:02 +00003151 template <typename T>
3152 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3153
3154 template <typename T>
3155 int32_t CompareFP(T x, T y) const {
3156 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3157 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3158 // Handle the bias.
3159 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3160 }
3161
3162 // Return an integer constant containing the result of a condition evaluated at compile time.
3163 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3164 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3165 }
3166
Dave Allison20dfc792014-06-16 20:44:29 -07003167 private:
3168 DISALLOW_COPY_AND_ASSIGN(HCondition);
3169};
3170
3171// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003172class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003173 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003174 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3175 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003176
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003177 bool IsCommutative() const OVERRIDE { return true; }
3178
Vladimir Marko9e23df52015-11-10 17:14:35 +00003179 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3180 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003181 return MakeConstantCondition(true, GetDexPc());
3182 }
3183 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3184 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3185 }
3186 // In the following Evaluate methods, a HCompare instruction has
3187 // been merged into this HEqual instruction; evaluate it as
3188 // `Compare(x, y) == 0`.
3189 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3190 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3191 GetDexPc());
3192 }
3193 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3194 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3195 }
3196 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3197 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003198 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003199
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003200 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003201
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003202 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003203 return kCondEQ;
3204 }
3205
Mark Mendellc4701932015-04-10 13:18:51 -04003206 IfCondition GetOppositeCondition() const OVERRIDE {
3207 return kCondNE;
3208 }
3209
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003210 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003211 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003212
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003213 DISALLOW_COPY_AND_ASSIGN(HEqual);
3214};
3215
Vladimir Markofcb503c2016-05-18 12:48:17 +01003216class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003217 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003218 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3219 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003220
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003221 bool IsCommutative() const OVERRIDE { return true; }
3222
Vladimir Marko9e23df52015-11-10 17:14:35 +00003223 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3224 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003225 return MakeConstantCondition(false, GetDexPc());
3226 }
3227 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3228 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3229 }
3230 // In the following Evaluate methods, a HCompare instruction has
3231 // been merged into this HNotEqual instruction; evaluate it as
3232 // `Compare(x, y) != 0`.
3233 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3234 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3235 }
3236 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3237 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3238 }
3239 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3240 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003241 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003242
Dave Allison20dfc792014-06-16 20:44:29 -07003243 DECLARE_INSTRUCTION(NotEqual);
3244
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003245 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003246 return kCondNE;
3247 }
3248
Mark Mendellc4701932015-04-10 13:18:51 -04003249 IfCondition GetOppositeCondition() const OVERRIDE {
3250 return kCondEQ;
3251 }
3252
Dave Allison20dfc792014-06-16 20:44:29 -07003253 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003254 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003255
Dave Allison20dfc792014-06-16 20:44:29 -07003256 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3257};
3258
Vladimir Markofcb503c2016-05-18 12:48:17 +01003259class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003260 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003261 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3262 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003263
Roland Levillain9867bc72015-08-05 10:21:34 +01003264 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003265 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003266 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003267 // In the following Evaluate methods, a HCompare instruction has
3268 // been merged into this HLessThan instruction; evaluate it as
3269 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003270 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003271 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3272 }
3273 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3274 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3275 }
3276 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3277 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003278 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003279
Dave Allison20dfc792014-06-16 20:44:29 -07003280 DECLARE_INSTRUCTION(LessThan);
3281
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003282 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003283 return kCondLT;
3284 }
3285
Mark Mendellc4701932015-04-10 13:18:51 -04003286 IfCondition GetOppositeCondition() const OVERRIDE {
3287 return kCondGE;
3288 }
3289
Dave Allison20dfc792014-06-16 20:44:29 -07003290 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003291 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003292
Dave Allison20dfc792014-06-16 20:44:29 -07003293 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3294};
3295
Vladimir Markofcb503c2016-05-18 12:48:17 +01003296class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003297 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003298 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3299 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003300
Roland Levillain9867bc72015-08-05 10:21:34 +01003301 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003302 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003303 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003304 // In the following Evaluate methods, a HCompare instruction has
3305 // been merged into this HLessThanOrEqual instruction; evaluate it as
3306 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003307 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003308 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3309 }
3310 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3311 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3312 }
3313 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3314 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003315 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003316
Dave Allison20dfc792014-06-16 20:44:29 -07003317 DECLARE_INSTRUCTION(LessThanOrEqual);
3318
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003319 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003320 return kCondLE;
3321 }
3322
Mark Mendellc4701932015-04-10 13:18:51 -04003323 IfCondition GetOppositeCondition() const OVERRIDE {
3324 return kCondGT;
3325 }
3326
Dave Allison20dfc792014-06-16 20:44:29 -07003327 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003328 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003329
Dave Allison20dfc792014-06-16 20:44:29 -07003330 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3331};
3332
Vladimir Markofcb503c2016-05-18 12:48:17 +01003333class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003334 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003335 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3336 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003337
Roland Levillain9867bc72015-08-05 10:21:34 +01003338 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003339 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003340 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003341 // In the following Evaluate methods, a HCompare instruction has
3342 // been merged into this HGreaterThan instruction; evaluate it as
3343 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003344 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003345 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3346 }
3347 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3348 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3349 }
3350 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3351 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003352 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003353
Dave Allison20dfc792014-06-16 20:44:29 -07003354 DECLARE_INSTRUCTION(GreaterThan);
3355
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003356 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003357 return kCondGT;
3358 }
3359
Mark Mendellc4701932015-04-10 13:18:51 -04003360 IfCondition GetOppositeCondition() const OVERRIDE {
3361 return kCondLE;
3362 }
3363
Dave Allison20dfc792014-06-16 20:44:29 -07003364 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003365 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003366
Dave Allison20dfc792014-06-16 20:44:29 -07003367 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3368};
3369
Vladimir Markofcb503c2016-05-18 12:48:17 +01003370class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003371 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003372 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3373 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003374
Roland Levillain9867bc72015-08-05 10:21:34 +01003375 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003376 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003377 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003378 // In the following Evaluate methods, a HCompare instruction has
3379 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3380 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003381 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003382 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3383 }
3384 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3385 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3386 }
3387 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3388 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003389 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003390
Dave Allison20dfc792014-06-16 20:44:29 -07003391 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3392
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003393 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003394 return kCondGE;
3395 }
3396
Mark Mendellc4701932015-04-10 13:18:51 -04003397 IfCondition GetOppositeCondition() const OVERRIDE {
3398 return kCondLT;
3399 }
3400
Dave Allison20dfc792014-06-16 20:44:29 -07003401 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003402 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003403
Dave Allison20dfc792014-06-16 20:44:29 -07003404 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3405};
3406
Vladimir Markofcb503c2016-05-18 12:48:17 +01003407class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003408 public:
3409 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3410 : HCondition(first, second, dex_pc) {}
3411
3412 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003413 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003414 }
3415 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003416 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3417 }
3418 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3419 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3420 LOG(FATAL) << DebugName() << " is not defined for float values";
3421 UNREACHABLE();
3422 }
3423 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3424 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3425 LOG(FATAL) << DebugName() << " is not defined for double values";
3426 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003427 }
3428
3429 DECLARE_INSTRUCTION(Below);
3430
3431 IfCondition GetCondition() const OVERRIDE {
3432 return kCondB;
3433 }
3434
3435 IfCondition GetOppositeCondition() const OVERRIDE {
3436 return kCondAE;
3437 }
3438
3439 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003440 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003441 return MakeUnsigned(x) < MakeUnsigned(y);
3442 }
Aart Bike9f37602015-10-09 11:15:55 -07003443
3444 DISALLOW_COPY_AND_ASSIGN(HBelow);
3445};
3446
Vladimir Markofcb503c2016-05-18 12:48:17 +01003447class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003448 public:
3449 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3450 : HCondition(first, second, dex_pc) {}
3451
3452 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003453 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003454 }
3455 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003456 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3457 }
3458 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3459 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3460 LOG(FATAL) << DebugName() << " is not defined for float values";
3461 UNREACHABLE();
3462 }
3463 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3464 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3465 LOG(FATAL) << DebugName() << " is not defined for double values";
3466 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003467 }
3468
3469 DECLARE_INSTRUCTION(BelowOrEqual);
3470
3471 IfCondition GetCondition() const OVERRIDE {
3472 return kCondBE;
3473 }
3474
3475 IfCondition GetOppositeCondition() const OVERRIDE {
3476 return kCondA;
3477 }
3478
3479 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003480 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003481 return MakeUnsigned(x) <= MakeUnsigned(y);
3482 }
Aart Bike9f37602015-10-09 11:15:55 -07003483
3484 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3485};
3486
Vladimir Markofcb503c2016-05-18 12:48:17 +01003487class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003488 public:
3489 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3490 : HCondition(first, second, dex_pc) {}
3491
3492 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003493 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003494 }
3495 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003496 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3497 }
3498 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3499 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3500 LOG(FATAL) << DebugName() << " is not defined for float values";
3501 UNREACHABLE();
3502 }
3503 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3504 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3505 LOG(FATAL) << DebugName() << " is not defined for double values";
3506 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003507 }
3508
3509 DECLARE_INSTRUCTION(Above);
3510
3511 IfCondition GetCondition() const OVERRIDE {
3512 return kCondA;
3513 }
3514
3515 IfCondition GetOppositeCondition() const OVERRIDE {
3516 return kCondBE;
3517 }
3518
3519 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003520 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003521 return MakeUnsigned(x) > MakeUnsigned(y);
3522 }
Aart Bike9f37602015-10-09 11:15:55 -07003523
3524 DISALLOW_COPY_AND_ASSIGN(HAbove);
3525};
3526
Vladimir Markofcb503c2016-05-18 12:48:17 +01003527class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003528 public:
3529 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3530 : HCondition(first, second, dex_pc) {}
3531
3532 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003533 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003534 }
3535 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003536 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3537 }
3538 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3539 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3540 LOG(FATAL) << DebugName() << " is not defined for float values";
3541 UNREACHABLE();
3542 }
3543 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3544 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3545 LOG(FATAL) << DebugName() << " is not defined for double values";
3546 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003547 }
3548
3549 DECLARE_INSTRUCTION(AboveOrEqual);
3550
3551 IfCondition GetCondition() const OVERRIDE {
3552 return kCondAE;
3553 }
3554
3555 IfCondition GetOppositeCondition() const OVERRIDE {
3556 return kCondB;
3557 }
3558
3559 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003560 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003561 return MakeUnsigned(x) >= MakeUnsigned(y);
3562 }
Aart Bike9f37602015-10-09 11:15:55 -07003563
3564 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3565};
Dave Allison20dfc792014-06-16 20:44:29 -07003566
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003567// Instruction to check how two inputs compare to each other.
3568// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003569class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003570 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003571 // Note that `comparison_type` is the type of comparison performed
3572 // between the comparison's inputs, not the type of the instantiated
3573 // HCompare instruction (which is always Primitive::kPrimInt).
3574 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003575 HInstruction* first,
3576 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003577 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003578 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003579 : HBinaryOperation(Primitive::kPrimInt,
3580 first,
3581 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003582 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003583 dex_pc) {
3584 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003585 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3586 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003587 }
3588
Roland Levillain9867bc72015-08-05 10:21:34 +01003589 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003590 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3591
3592 template <typename T>
3593 int32_t ComputeFP(T x, T y) const {
3594 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3595 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3596 // Handle the bias.
3597 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3598 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003599
Roland Levillain9867bc72015-08-05 10:21:34 +01003600 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003601 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3602 // reach this code path when processing a freshly built HIR
3603 // graph. However HCompare integer instructions can be synthesized
3604 // by the instruction simplifier to implement IntegerCompare and
3605 // IntegerSignum intrinsics, so we have to handle this case.
3606 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003607 }
3608 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003609 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3610 }
3611 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3612 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3613 }
3614 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3615 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003616 }
3617
Vladimir Marko372f10e2016-05-17 16:30:10 +01003618 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003619 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003620 }
3621
Vladimir Markoa1de9182016-02-25 11:37:38 +00003622 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003623
Roland Levillain31dd3d62016-02-16 12:21:02 +00003624 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003625 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003626 bool IsGtBias() const {
3627 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003628 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003629 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003630
Roland Levillain1693a1f2016-03-15 14:57:31 +00003631 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3632 // Comparisons do not require a runtime call in any back end.
3633 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003634 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003635
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003636 DECLARE_INSTRUCTION(Compare);
3637
Roland Levillain31dd3d62016-02-16 12:21:02 +00003638 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003639 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3640 static constexpr size_t kFieldComparisonBiasSize =
3641 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3642 static constexpr size_t kNumberOfComparePackedBits =
3643 kFieldComparisonBias + kFieldComparisonBiasSize;
3644 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3645 using ComparisonBiasField =
3646 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3647
Roland Levillain31dd3d62016-02-16 12:21:02 +00003648 // Return an integer constant containing the result of a comparison evaluated at compile time.
3649 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3650 DCHECK(value == -1 || value == 0 || value == 1) << value;
3651 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3652 }
3653
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003654 private:
3655 DISALLOW_COPY_AND_ASSIGN(HCompare);
3656};
3657
Vladimir Markofcb503c2016-05-18 12:48:17 +01003658class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003659 public:
3660 HNewInstance(HInstruction* cls,
3661 HCurrentMethod* current_method,
3662 uint32_t dex_pc,
3663 uint16_t type_index,
3664 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003665 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003666 bool finalizable,
3667 QuickEntrypointEnum entrypoint)
3668 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3669 type_index_(type_index),
3670 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003671 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003672 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003673 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003674 SetRawInputAt(0, cls);
3675 SetRawInputAt(1, current_method);
3676 }
3677
3678 uint16_t GetTypeIndex() const { return type_index_; }
3679 const DexFile& GetDexFile() const { return dex_file_; }
3680
3681 // Calls runtime so needs an environment.
3682 bool NeedsEnvironment() const OVERRIDE { return true; }
3683
Mingyao Yang062157f2016-03-02 10:15:36 -08003684 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3685 bool CanThrow() const OVERRIDE { return true; }
3686
3687 // Needs to call into runtime to make sure it's instantiable/accessible.
3688 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003689
Vladimir Markoa1de9182016-02-25 11:37:38 +00003690 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003691
3692 bool CanBeNull() const OVERRIDE { return false; }
3693
3694 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3695
3696 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3697 entrypoint_ = entrypoint;
3698 }
3699
3700 bool IsStringAlloc() const;
3701
3702 DECLARE_INSTRUCTION(NewInstance);
3703
3704 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003705 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3706 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003707 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3708 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3709 "Too many packed fields.");
3710
David Brazdil6de19382016-01-08 17:37:10 +00003711 const uint16_t type_index_;
3712 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003713 QuickEntrypointEnum entrypoint_;
3714
3715 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3716};
3717
Agi Csaki05f20562015-08-19 14:58:14 -07003718enum IntrinsicNeedsEnvironmentOrCache {
3719 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3720 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003721};
3722
Aart Bik5d75afe2015-12-14 11:57:01 -08003723enum IntrinsicSideEffects {
3724 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3725 kReadSideEffects, // Intrinsic may read heap memory.
3726 kWriteSideEffects, // Intrinsic may write heap memory.
3727 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3728};
3729
3730enum IntrinsicExceptions {
3731 kNoThrow, // Intrinsic does not throw any exceptions.
3732 kCanThrow // Intrinsic may throw exceptions.
3733};
3734
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003735class HInvoke : public HInstruction {
3736 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003737 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003738
Vladimir Marko372f10e2016-05-17 16:30:10 +01003739 using HInstruction::GetInputRecords; // Keep the const version visible.
3740 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3741 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3742 }
3743
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003744 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003745 SetRawInputAt(index, argument);
3746 }
3747
Roland Levillain3e3d7332015-04-28 11:00:54 +01003748 // Return the number of arguments. This number can be lower than
3749 // the number of inputs returned by InputCount(), as some invoke
3750 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3751 // inputs at the end of their list of inputs.
3752 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3753
Vladimir Markoa1de9182016-02-25 11:37:38 +00003754 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003755
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003756 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003757 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003758
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003759 InvokeType GetInvokeType() const {
3760 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003761 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003762
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003763 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003764 return intrinsic_;
3765 }
3766
Aart Bik5d75afe2015-12-14 11:57:01 -08003767 void SetIntrinsic(Intrinsics intrinsic,
3768 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3769 IntrinsicSideEffects side_effects,
3770 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003771
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003772 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003773 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003774 }
3775
Vladimir Markoa1de9182016-02-25 11:37:38 +00003776 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003777
3778 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3779
Vladimir Marko372f10e2016-05-17 16:30:10 +01003780 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003781 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3782 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003783
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003784 uint32_t* GetIntrinsicOptimizations() {
3785 return &intrinsic_optimizations_;
3786 }
3787
3788 const uint32_t* GetIntrinsicOptimizations() const {
3789 return &intrinsic_optimizations_;
3790 }
3791
3792 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3793
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003794 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3795
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003796 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003797
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003798 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003799 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3800 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003801 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3802 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003803 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003804 static constexpr size_t kFieldReturnTypeSize =
3805 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3806 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3807 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3808 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003809 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003810 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3811
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003812 HInvoke(ArenaAllocator* arena,
3813 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003814 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003815 Primitive::Type return_type,
3816 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003817 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003818 ArtMethod* resolved_method,
3819 InvokeType invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003820 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003821 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003822 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003823 resolved_method_(resolved_method),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003824 inputs_(number_of_arguments + number_of_other_inputs,
3825 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003826 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003827 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003828 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003829 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003830 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003831 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003832 }
3833
Vladimir Markoa1de9182016-02-25 11:37:38 +00003834 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003835
Roland Levillain3e3d7332015-04-28 11:00:54 +01003836 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003837 ArtMethod* const resolved_method_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003838 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003839 const uint32_t dex_method_index_;
3840 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003841
3842 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3843 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003844
3845 private:
3846 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3847};
3848
Vladimir Markofcb503c2016-05-18 12:48:17 +01003849class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003850 public:
3851 HInvokeUnresolved(ArenaAllocator* arena,
3852 uint32_t number_of_arguments,
3853 Primitive::Type return_type,
3854 uint32_t dex_pc,
3855 uint32_t dex_method_index,
3856 InvokeType invoke_type)
3857 : HInvoke(arena,
3858 number_of_arguments,
3859 0u /* number_of_other_inputs */,
3860 return_type,
3861 dex_pc,
3862 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003863 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003864 invoke_type) {
3865 }
3866
3867 DECLARE_INSTRUCTION(InvokeUnresolved);
3868
3869 private:
3870 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3871};
3872
Vladimir Markofcb503c2016-05-18 12:48:17 +01003873class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003874 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003875 // Requirements of this method call regarding the class
3876 // initialization (clinit) check of its declaring class.
3877 enum class ClinitCheckRequirement {
3878 kNone, // Class already initialized.
3879 kExplicit, // Static call having explicit clinit check as last input.
3880 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003881 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003882 };
3883
Vladimir Marko58155012015-08-19 12:49:41 +00003884 // Determines how to load the target ArtMethod*.
3885 enum class MethodLoadKind {
3886 // Use a String init ArtMethod* loaded from Thread entrypoints.
3887 kStringInit,
3888
3889 // Use the method's own ArtMethod* loaded by the register allocator.
3890 kRecursive,
3891
3892 // Use ArtMethod* at a known address, embed the direct address in the code.
3893 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3894 kDirectAddress,
3895
3896 // Use ArtMethod* at an address that will be known at link time, embed the direct
3897 // address in the code. If the image is relocatable, emit .patch_oat entry.
3898 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3899 // the image relocatable or not.
3900 kDirectAddressWithFixup,
3901
Vladimir Markoa1de9182016-02-25 11:37:38 +00003902 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003903 // Used when we need to use the dex cache, for example for invoke-static that
3904 // may cause class initialization (the entry may point to a resolution method),
3905 // and we know that we can access the dex cache arrays using a PC-relative load.
3906 kDexCachePcRelative,
3907
3908 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3909 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3910 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3911 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3912 kDexCacheViaMethod,
3913 };
3914
3915 // Determines the location of the code pointer.
3916 enum class CodePtrLocation {
3917 // Recursive call, use local PC-relative call instruction.
3918 kCallSelf,
3919
3920 // Use PC-relative call instruction patched at link time.
3921 // Used for calls within an oat file, boot->boot or app->app.
3922 kCallPCRelative,
3923
3924 // Call to a known target address, embed the direct address in code.
3925 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3926 kCallDirect,
3927
3928 // Call to a target address that will be known at link time, embed the direct
3929 // address in code. If the image is relocatable, emit .patch_oat entry.
3930 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3931 // the image relocatable or not.
3932 kCallDirectWithFixup,
3933
3934 // Use code pointer from the ArtMethod*.
3935 // Used when we don't know the target code. This is also the last-resort-kind used when
3936 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3937 kCallArtMethod,
3938 };
3939
3940 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003941 MethodLoadKind method_load_kind;
3942 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003943 // The method load data holds
3944 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3945 // Note that there are multiple string init methods, each having its own offset.
3946 // - the method address for kDirectAddress
3947 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003948 uint64_t method_load_data;
3949 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003950 };
3951
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003952 HInvokeStaticOrDirect(ArenaAllocator* arena,
3953 uint32_t number_of_arguments,
3954 Primitive::Type return_type,
3955 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003956 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003957 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00003958 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003959 InvokeType invoke_type,
3960 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003961 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003962 : HInvoke(arena,
3963 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003964 // There is potentially one extra argument for the HCurrentMethod node, and
3965 // potentially one other if the clinit check is explicit, and potentially
3966 // one other if the method is a string factory.
3967 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003968 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003969 return_type,
3970 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003971 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003972 resolved_method,
3973 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003974 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003975 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003976 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3977 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003978
Vladimir Markodc151b22015-10-15 18:02:30 +01003979 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003980 bool had_current_method_input = HasCurrentMethodInput();
3981 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3982
3983 // Using the current method is the default and once we find a better
3984 // method load kind, we should not go back to using the current method.
3985 DCHECK(had_current_method_input || !needs_current_method_input);
3986
3987 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003988 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3989 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003990 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003991 dispatch_info_ = dispatch_info;
3992 }
3993
Vladimir Markoc53c0792015-11-19 15:48:33 +00003994 void AddSpecialInput(HInstruction* input) {
3995 // We allow only one special input.
3996 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3997 DCHECK(InputCount() == GetSpecialInputIndex() ||
3998 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3999 InsertInputAt(GetSpecialInputIndex(), input);
4000 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004001
Vladimir Marko372f10e2016-05-17 16:30:10 +01004002 using HInstruction::GetInputRecords; // Keep the const version visible.
4003 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4004 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4005 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4006 DCHECK(!input_records.empty());
4007 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4008 HInstruction* last_input = input_records.back().GetInstruction();
4009 // Note: `last_input` may be null during arguments setup.
4010 if (last_input != nullptr) {
4011 // `last_input` is the last input of a static invoke marked as having
4012 // an explicit clinit check. It must either be:
4013 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4014 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4015 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4016 }
4017 }
4018 return input_records;
4019 }
4020
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004021 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004022 // We access the method via the dex cache so we can't do an implicit null check.
4023 // TODO: for intrinsics we can generate implicit null checks.
4024 return false;
4025 }
4026
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004027 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004028 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004029 }
4030
Vladimir Markoc53c0792015-11-19 15:48:33 +00004031 // Get the index of the special input, if any.
4032 //
David Brazdil6de19382016-01-08 17:37:10 +00004033 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4034 // method pointer; otherwise there may be one platform-specific special input,
4035 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004036 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004037 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004038
Vladimir Marko58155012015-08-19 12:49:41 +00004039 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4040 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4041 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004042 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004043 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004044 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004045 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004046 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4047 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004048 bool HasCurrentMethodInput() const {
4049 // This function can be called only after the invoke has been fully initialized by the builder.
4050 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004051 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004052 return true;
4053 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004054 DCHECK(InputCount() == GetSpecialInputIndex() ||
4055 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004056 return false;
4057 }
4058 }
Vladimir Marko58155012015-08-19 12:49:41 +00004059 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
Vladimir Marko58155012015-08-19 12:49:41 +00004060
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004061 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004062 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004063 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004064 }
4065
4066 uint64_t GetMethodAddress() const {
4067 DCHECK(HasMethodAddress());
4068 return dispatch_info_.method_load_data;
4069 }
4070
4071 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004072 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004073 return dispatch_info_.method_load_data;
4074 }
4075
4076 uint64_t GetDirectCodePtr() const {
4077 DCHECK(HasDirectCodePtr());
4078 return dispatch_info_.direct_code_ptr;
4079 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004080
Vladimir Markoa1de9182016-02-25 11:37:38 +00004081 ClinitCheckRequirement GetClinitCheckRequirement() const {
4082 return GetPackedField<ClinitCheckRequirementField>();
4083 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004084
Roland Levillain4c0eb422015-04-24 16:43:49 +01004085 // Is this instruction a call to a static method?
4086 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004087 return GetInvokeType() == kStatic;
4088 }
4089
4090 MethodReference GetTargetMethod() const {
4091 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004092 }
4093
Vladimir Markofbb184a2015-11-13 14:47:00 +00004094 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4095 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4096 // instruction; only relevant for static calls with explicit clinit check.
4097 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004098 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004099 size_t last_input_index = inputs_.size() - 1u;
4100 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004101 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004102 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004103 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004104 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004105 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004106 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004107 }
4108
4109 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004110 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004111 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004112 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004113 }
4114
4115 // Is this a call to a static method whose declaring class has an
4116 // implicit intialization check requirement?
4117 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004118 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004119 }
4120
Vladimir Markob554b5a2015-11-06 12:57:55 +00004121 // Does this method load kind need the current method as an input?
4122 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4123 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4124 }
4125
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004126 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004127
Roland Levillain4c0eb422015-04-24 16:43:49 +01004128 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004129 void InsertInputAt(size_t index, HInstruction* input);
4130 void RemoveInputAt(size_t index);
4131
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004132 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004133 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004134 static constexpr size_t kFieldClinitCheckRequirementSize =
4135 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4136 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4137 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4138 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4139 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004140 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4141 kFieldClinitCheckRequirement,
4142 kFieldClinitCheckRequirementSize>;
4143
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004144 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004145 MethodReference target_method_;
4146 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004147
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004148 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004149};
Vladimir Markof64242a2015-12-01 14:58:23 +00004150std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004151std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004152
Vladimir Markofcb503c2016-05-18 12:48:17 +01004153class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004154 public:
4155 HInvokeVirtual(ArenaAllocator* arena,
4156 uint32_t number_of_arguments,
4157 Primitive::Type return_type,
4158 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004159 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004160 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004161 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004162 : HInvoke(arena,
4163 number_of_arguments,
4164 0u,
4165 return_type,
4166 dex_pc,
4167 dex_method_index,
4168 resolved_method,
4169 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004170 vtable_index_(vtable_index) {}
4171
Calin Juravle641547a2015-04-21 22:08:51 +01004172 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004173 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004174 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004175 }
4176
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004177 uint32_t GetVTableIndex() const { return vtable_index_; }
4178
4179 DECLARE_INSTRUCTION(InvokeVirtual);
4180
4181 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004182 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004183 const uint32_t vtable_index_;
4184
4185 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4186};
4187
Vladimir Markofcb503c2016-05-18 12:48:17 +01004188class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004189 public:
4190 HInvokeInterface(ArenaAllocator* arena,
4191 uint32_t number_of_arguments,
4192 Primitive::Type return_type,
4193 uint32_t dex_pc,
4194 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004195 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004196 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004197 : HInvoke(arena,
4198 number_of_arguments,
4199 0u,
4200 return_type,
4201 dex_pc,
4202 dex_method_index,
4203 resolved_method,
4204 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004205 imt_index_(imt_index) {}
4206
Calin Juravle641547a2015-04-21 22:08:51 +01004207 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004208 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004209 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004210 }
4211
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004212 uint32_t GetImtIndex() const { return imt_index_; }
4213 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4214
4215 DECLARE_INSTRUCTION(InvokeInterface);
4216
4217 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004218 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004219 const uint32_t imt_index_;
4220
4221 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4222};
4223
Vladimir Markofcb503c2016-05-18 12:48:17 +01004224class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004225 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004226 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004227 : HUnaryOperation(result_type, input, dex_pc) {
4228 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4229 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004230
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004231 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004232
4233 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004234 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004235 }
4236 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004237 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004238 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004239 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4240 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4241 }
4242 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4243 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4244 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004245
Roland Levillain88cb1752014-10-20 16:36:47 +01004246 DECLARE_INSTRUCTION(Neg);
4247
4248 private:
4249 DISALLOW_COPY_AND_ASSIGN(HNeg);
4250};
4251
Vladimir Markofcb503c2016-05-18 12:48:17 +01004252class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004253 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004254 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004255 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004256 uint32_t dex_pc,
4257 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004258 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004259 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004260 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004261 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004262 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004263 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004264 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004265 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004266 }
4267
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004268 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004269 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004270
4271 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004272 bool NeedsEnvironment() const OVERRIDE { return true; }
4273
Mingyao Yang0c365e62015-03-31 15:09:29 -07004274 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4275 bool CanThrow() const OVERRIDE { return true; }
4276
Calin Juravle10e244f2015-01-26 18:54:32 +00004277 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004278
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004279 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4280
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004281 DECLARE_INSTRUCTION(NewArray);
4282
4283 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004284 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004285 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004286 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004287
4288 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4289};
4290
Vladimir Markofcb503c2016-05-18 12:48:17 +01004291class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004292 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004293 HAdd(Primitive::Type result_type,
4294 HInstruction* left,
4295 HInstruction* right,
4296 uint32_t dex_pc = kNoDexPc)
4297 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004298
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004299 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004300
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004301 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004302
4303 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004304 return GetBlock()->GetGraph()->GetIntConstant(
4305 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004306 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004307 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004308 return GetBlock()->GetGraph()->GetLongConstant(
4309 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004310 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004311 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4312 return GetBlock()->GetGraph()->GetFloatConstant(
4313 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4314 }
4315 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4316 return GetBlock()->GetGraph()->GetDoubleConstant(
4317 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4318 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004319
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004320 DECLARE_INSTRUCTION(Add);
4321
4322 private:
4323 DISALLOW_COPY_AND_ASSIGN(HAdd);
4324};
4325
Vladimir Markofcb503c2016-05-18 12:48:17 +01004326class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004327 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004328 HSub(Primitive::Type result_type,
4329 HInstruction* left,
4330 HInstruction* right,
4331 uint32_t dex_pc = kNoDexPc)
4332 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004333
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004334 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004335
4336 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004337 return GetBlock()->GetGraph()->GetIntConstant(
4338 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004339 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004340 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004341 return GetBlock()->GetGraph()->GetLongConstant(
4342 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004343 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004344 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4345 return GetBlock()->GetGraph()->GetFloatConstant(
4346 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4347 }
4348 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4349 return GetBlock()->GetGraph()->GetDoubleConstant(
4350 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4351 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004352
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004353 DECLARE_INSTRUCTION(Sub);
4354
4355 private:
4356 DISALLOW_COPY_AND_ASSIGN(HSub);
4357};
4358
Vladimir Markofcb503c2016-05-18 12:48:17 +01004359class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004360 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004361 HMul(Primitive::Type result_type,
4362 HInstruction* left,
4363 HInstruction* right,
4364 uint32_t dex_pc = kNoDexPc)
4365 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004366
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004367 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004368
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004369 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004370
4371 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004372 return GetBlock()->GetGraph()->GetIntConstant(
4373 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004374 }
4375 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004376 return GetBlock()->GetGraph()->GetLongConstant(
4377 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004378 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004379 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4380 return GetBlock()->GetGraph()->GetFloatConstant(
4381 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4382 }
4383 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4384 return GetBlock()->GetGraph()->GetDoubleConstant(
4385 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4386 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004387
4388 DECLARE_INSTRUCTION(Mul);
4389
4390 private:
4391 DISALLOW_COPY_AND_ASSIGN(HMul);
4392};
4393
Vladimir Markofcb503c2016-05-18 12:48:17 +01004394class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004395 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004396 HDiv(Primitive::Type result_type,
4397 HInstruction* left,
4398 HInstruction* right,
4399 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004400 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004401
Roland Levillain9867bc72015-08-05 10:21:34 +01004402 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004403 T ComputeIntegral(T x, T y) const {
4404 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004405 // Our graph structure ensures we never have 0 for `y` during
4406 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004407 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004408 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004409 return (y == -1) ? -x : x / y;
4410 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004411
Roland Levillain31dd3d62016-02-16 12:21:02 +00004412 template <typename T>
4413 T ComputeFP(T x, T y) const {
4414 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4415 return x / y;
4416 }
4417
Roland Levillain9867bc72015-08-05 10:21:34 +01004418 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004419 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004420 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004421 }
4422 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004423 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004424 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4425 }
4426 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4427 return GetBlock()->GetGraph()->GetFloatConstant(
4428 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4429 }
4430 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4431 return GetBlock()->GetGraph()->GetDoubleConstant(
4432 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004433 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004434
4435 DECLARE_INSTRUCTION(Div);
4436
4437 private:
4438 DISALLOW_COPY_AND_ASSIGN(HDiv);
4439};
4440
Vladimir Markofcb503c2016-05-18 12:48:17 +01004441class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004442 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004443 HRem(Primitive::Type result_type,
4444 HInstruction* left,
4445 HInstruction* right,
4446 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004447 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004448
Roland Levillain9867bc72015-08-05 10:21:34 +01004449 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004450 T ComputeIntegral(T x, T y) const {
4451 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004452 // Our graph structure ensures we never have 0 for `y` during
4453 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004454 DCHECK_NE(y, 0);
4455 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4456 return (y == -1) ? 0 : x % y;
4457 }
4458
Roland Levillain31dd3d62016-02-16 12:21:02 +00004459 template <typename T>
4460 T ComputeFP(T x, T y) const {
4461 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4462 return std::fmod(x, y);
4463 }
4464
Roland Levillain9867bc72015-08-05 10:21:34 +01004465 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004466 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004467 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004468 }
4469 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004470 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004471 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004472 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004473 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4474 return GetBlock()->GetGraph()->GetFloatConstant(
4475 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4476 }
4477 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4478 return GetBlock()->GetGraph()->GetDoubleConstant(
4479 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4480 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004481
4482 DECLARE_INSTRUCTION(Rem);
4483
4484 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004485 DISALLOW_COPY_AND_ASSIGN(HRem);
4486};
4487
Vladimir Markofcb503c2016-05-18 12:48:17 +01004488class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004489 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004490 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4491 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004492 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004493 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004494 SetRawInputAt(0, value);
4495 }
4496
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004497 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4498
Calin Juravled0d48522014-11-04 16:40:20 +00004499 bool CanBeMoved() const OVERRIDE { return true; }
4500
Vladimir Marko372f10e2016-05-17 16:30:10 +01004501 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004502 return true;
4503 }
4504
4505 bool NeedsEnvironment() const OVERRIDE { return true; }
4506 bool CanThrow() const OVERRIDE { return true; }
4507
Calin Juravled0d48522014-11-04 16:40:20 +00004508 DECLARE_INSTRUCTION(DivZeroCheck);
4509
4510 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004511 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4512};
4513
Vladimir Markofcb503c2016-05-18 12:48:17 +01004514class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004515 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004516 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004517 HInstruction* value,
4518 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004519 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004520 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4521 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4522 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004523 }
4524
Roland Levillain5b5b9312016-03-22 14:57:31 +00004525 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004526 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004527 return value << (distance & max_shift_distance);
4528 }
4529
4530 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004531 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004532 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004533 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004534 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004535 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004536 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004537 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004538 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4539 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4540 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4541 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004542 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004543 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4544 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004545 LOG(FATAL) << DebugName() << " is not defined for float values";
4546 UNREACHABLE();
4547 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004548 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4549 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004550 LOG(FATAL) << DebugName() << " is not defined for double values";
4551 UNREACHABLE();
4552 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004553
4554 DECLARE_INSTRUCTION(Shl);
4555
4556 private:
4557 DISALLOW_COPY_AND_ASSIGN(HShl);
4558};
4559
Vladimir Markofcb503c2016-05-18 12:48:17 +01004560class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004561 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004562 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004563 HInstruction* value,
4564 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004565 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004566 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4567 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4568 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004569 }
4570
Roland Levillain5b5b9312016-03-22 14:57:31 +00004571 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004572 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004573 return value >> (distance & max_shift_distance);
4574 }
4575
4576 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004577 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004578 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004579 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004580 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004581 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004582 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004583 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004584 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4585 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4586 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4587 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004588 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004589 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4590 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004591 LOG(FATAL) << DebugName() << " is not defined for float values";
4592 UNREACHABLE();
4593 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004594 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4595 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004596 LOG(FATAL) << DebugName() << " is not defined for double values";
4597 UNREACHABLE();
4598 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004599
4600 DECLARE_INSTRUCTION(Shr);
4601
4602 private:
4603 DISALLOW_COPY_AND_ASSIGN(HShr);
4604};
4605
Vladimir Markofcb503c2016-05-18 12:48:17 +01004606class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004607 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004608 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004609 HInstruction* value,
4610 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004611 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004612 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4613 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4614 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004615 }
4616
Roland Levillain5b5b9312016-03-22 14:57:31 +00004617 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004618 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004619 typedef typename std::make_unsigned<T>::type V;
4620 V ux = static_cast<V>(value);
4621 return static_cast<T>(ux >> (distance & max_shift_distance));
4622 }
4623
4624 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004625 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004626 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004627 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004628 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004629 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004630 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004631 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004632 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4633 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4634 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4635 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004636 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004637 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4638 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004639 LOG(FATAL) << DebugName() << " is not defined for float values";
4640 UNREACHABLE();
4641 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004642 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4643 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004644 LOG(FATAL) << DebugName() << " is not defined for double values";
4645 UNREACHABLE();
4646 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004647
4648 DECLARE_INSTRUCTION(UShr);
4649
4650 private:
4651 DISALLOW_COPY_AND_ASSIGN(HUShr);
4652};
4653
Vladimir Markofcb503c2016-05-18 12:48:17 +01004654class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004655 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004656 HAnd(Primitive::Type result_type,
4657 HInstruction* left,
4658 HInstruction* right,
4659 uint32_t dex_pc = kNoDexPc)
4660 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004661
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004662 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004663
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004664 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004665
4666 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004667 return GetBlock()->GetGraph()->GetIntConstant(
4668 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004669 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004670 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004671 return GetBlock()->GetGraph()->GetLongConstant(
4672 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004673 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004674 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4675 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4676 LOG(FATAL) << DebugName() << " is not defined for float values";
4677 UNREACHABLE();
4678 }
4679 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4680 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4681 LOG(FATAL) << DebugName() << " is not defined for double values";
4682 UNREACHABLE();
4683 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004684
4685 DECLARE_INSTRUCTION(And);
4686
4687 private:
4688 DISALLOW_COPY_AND_ASSIGN(HAnd);
4689};
4690
Vladimir Markofcb503c2016-05-18 12:48:17 +01004691class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004692 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004693 HOr(Primitive::Type result_type,
4694 HInstruction* left,
4695 HInstruction* right,
4696 uint32_t dex_pc = kNoDexPc)
4697 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004698
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004699 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004700
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004701 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004702
4703 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004704 return GetBlock()->GetGraph()->GetIntConstant(
4705 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004706 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004707 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004708 return GetBlock()->GetGraph()->GetLongConstant(
4709 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004710 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004711 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4712 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4713 LOG(FATAL) << DebugName() << " is not defined for float values";
4714 UNREACHABLE();
4715 }
4716 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4717 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4718 LOG(FATAL) << DebugName() << " is not defined for double values";
4719 UNREACHABLE();
4720 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004721
4722 DECLARE_INSTRUCTION(Or);
4723
4724 private:
4725 DISALLOW_COPY_AND_ASSIGN(HOr);
4726};
4727
Vladimir Markofcb503c2016-05-18 12:48:17 +01004728class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004729 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004730 HXor(Primitive::Type result_type,
4731 HInstruction* left,
4732 HInstruction* right,
4733 uint32_t dex_pc = kNoDexPc)
4734 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004735
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004736 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004737
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004738 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004739
4740 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004741 return GetBlock()->GetGraph()->GetIntConstant(
4742 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004743 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004744 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004745 return GetBlock()->GetGraph()->GetLongConstant(
4746 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004747 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004748 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4749 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4750 LOG(FATAL) << DebugName() << " is not defined for float values";
4751 UNREACHABLE();
4752 }
4753 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4754 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4755 LOG(FATAL) << DebugName() << " is not defined for double values";
4756 UNREACHABLE();
4757 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004758
4759 DECLARE_INSTRUCTION(Xor);
4760
4761 private:
4762 DISALLOW_COPY_AND_ASSIGN(HXor);
4763};
4764
Vladimir Markofcb503c2016-05-18 12:48:17 +01004765class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004766 public:
4767 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004768 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004769 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4770 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004771 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004772
Roland Levillain5b5b9312016-03-22 14:57:31 +00004773 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004774 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004775 typedef typename std::make_unsigned<T>::type V;
4776 V ux = static_cast<V>(value);
4777 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004778 return static_cast<T>(ux);
4779 } else {
4780 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004781 return static_cast<T>(ux >> (distance & max_shift_value)) |
4782 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004783 }
4784 }
4785
Roland Levillain5b5b9312016-03-22 14:57:31 +00004786 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004787 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004788 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004789 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004790 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004791 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004792 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004793 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004794 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4795 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4796 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4797 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004798 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004799 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4800 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004801 LOG(FATAL) << DebugName() << " is not defined for float values";
4802 UNREACHABLE();
4803 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004804 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4805 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004806 LOG(FATAL) << DebugName() << " is not defined for double values";
4807 UNREACHABLE();
4808 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004809
4810 DECLARE_INSTRUCTION(Ror);
4811
4812 private:
4813 DISALLOW_COPY_AND_ASSIGN(HRor);
4814};
4815
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004816// The value of a parameter in this method. Its location depends on
4817// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004818class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004819 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004820 HParameterValue(const DexFile& dex_file,
4821 uint16_t type_index,
4822 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004823 Primitive::Type parameter_type,
4824 bool is_this = false)
4825 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004826 dex_file_(dex_file),
4827 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004828 index_(index) {
4829 SetPackedFlag<kFlagIsThis>(is_this);
4830 SetPackedFlag<kFlagCanBeNull>(!is_this);
4831 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004832
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004833 const DexFile& GetDexFile() const { return dex_file_; }
4834 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004835 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004836 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004837
Vladimir Markoa1de9182016-02-25 11:37:38 +00004838 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4839 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004840
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004841 DECLARE_INSTRUCTION(ParameterValue);
4842
4843 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004844 // Whether or not the parameter value corresponds to 'this' argument.
4845 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4846 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4847 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4848 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4849 "Too many packed fields.");
4850
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004851 const DexFile& dex_file_;
4852 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004853 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004854 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004855 const uint8_t index_;
4856
4857 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4858};
4859
Vladimir Markofcb503c2016-05-18 12:48:17 +01004860class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004861 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004862 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4863 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004864
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004865 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004866 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004867 return true;
4868 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004869
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004870 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004871
4872 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004873 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004874 }
4875 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004876 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004877 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004878 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4879 LOG(FATAL) << DebugName() << " is not defined for float values";
4880 UNREACHABLE();
4881 }
4882 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4883 LOG(FATAL) << DebugName() << " is not defined for double values";
4884 UNREACHABLE();
4885 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004886
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004887 DECLARE_INSTRUCTION(Not);
4888
4889 private:
4890 DISALLOW_COPY_AND_ASSIGN(HNot);
4891};
4892
Vladimir Markofcb503c2016-05-18 12:48:17 +01004893class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004894 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004895 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4896 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004897
4898 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004899 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004900 return true;
4901 }
4902
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004903 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004904 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004905 return !x;
4906 }
4907
Roland Levillain9867bc72015-08-05 10:21:34 +01004908 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004909 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004910 }
4911 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4912 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004913 UNREACHABLE();
4914 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004915 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4916 LOG(FATAL) << DebugName() << " is not defined for float values";
4917 UNREACHABLE();
4918 }
4919 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4920 LOG(FATAL) << DebugName() << " is not defined for double values";
4921 UNREACHABLE();
4922 }
David Brazdil66d126e2015-04-03 16:02:44 +01004923
4924 DECLARE_INSTRUCTION(BooleanNot);
4925
4926 private:
4927 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4928};
4929
Vladimir Markofcb503c2016-05-18 12:48:17 +01004930class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004931 public:
4932 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004933 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004934 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004935 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004936 // Invariant: We should never generate a conversion to a Boolean value.
4937 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004938 }
4939
4940 HInstruction* GetInput() const { return InputAt(0); }
4941 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4942 Primitive::Type GetResultType() const { return GetType(); }
4943
4944 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004945 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4946 return true;
4947 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004948
Mark Mendelle82549b2015-05-06 10:55:34 -04004949 // Try to statically evaluate the conversion and return a HConstant
4950 // containing the result. If the input cannot be converted, return nullptr.
4951 HConstant* TryStaticEvaluation() const;
4952
Roland Levillaindff1f282014-11-05 14:15:05 +00004953 DECLARE_INSTRUCTION(TypeConversion);
4954
4955 private:
4956 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4957};
4958
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004959static constexpr uint32_t kNoRegNumber = -1;
4960
Vladimir Markofcb503c2016-05-18 12:48:17 +01004961class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004962 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004963 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4964 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004965 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004966 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004967 SetRawInputAt(0, value);
4968 }
4969
Calin Juravle10e244f2015-01-26 18:54:32 +00004970 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004971 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004972 return true;
4973 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004974
Calin Juravle10e244f2015-01-26 18:54:32 +00004975 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004976
Calin Juravle10e244f2015-01-26 18:54:32 +00004977 bool CanThrow() const OVERRIDE { return true; }
4978
4979 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004980
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004981
4982 DECLARE_INSTRUCTION(NullCheck);
4983
4984 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004985 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4986};
4987
4988class FieldInfo : public ValueObject {
4989 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004990 FieldInfo(MemberOffset field_offset,
4991 Primitive::Type field_type,
4992 bool is_volatile,
4993 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004994 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004995 const DexFile& dex_file,
4996 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004997 : field_offset_(field_offset),
4998 field_type_(field_type),
4999 is_volatile_(is_volatile),
5000 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005001 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005002 dex_file_(dex_file),
5003 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005004
5005 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005006 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005007 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005008 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005009 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005010 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005011 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005012
5013 private:
5014 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005015 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005016 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005017 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005018 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005019 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005020 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005021};
5022
Vladimir Markofcb503c2016-05-18 12:48:17 +01005023class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005024 public:
5025 HInstanceFieldGet(HInstruction* value,
5026 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005027 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005028 bool is_volatile,
5029 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005030 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005031 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005032 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005033 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005034 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005035 field_info_(field_offset,
5036 field_type,
5037 is_volatile,
5038 field_idx,
5039 declaring_class_def_index,
5040 dex_file,
5041 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005042 SetRawInputAt(0, value);
5043 }
5044
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005045 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005046
Vladimir Marko372f10e2016-05-17 16:30:10 +01005047 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5048 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005049 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005050 }
5051
Calin Juravle641547a2015-04-21 22:08:51 +01005052 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005053 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005054 }
5055
5056 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005057 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5058 }
5059
Calin Juravle52c48962014-12-16 17:02:57 +00005060 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005061 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005062 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005063 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005064
5065 DECLARE_INSTRUCTION(InstanceFieldGet);
5066
5067 private:
5068 const FieldInfo field_info_;
5069
5070 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5071};
5072
Vladimir Markofcb503c2016-05-18 12:48:17 +01005073class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005074 public:
5075 HInstanceFieldSet(HInstruction* object,
5076 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005077 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005078 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005079 bool is_volatile,
5080 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005081 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005082 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005083 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005084 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005085 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005086 field_info_(field_offset,
5087 field_type,
5088 is_volatile,
5089 field_idx,
5090 declaring_class_def_index,
5091 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005092 dex_cache) {
5093 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005094 SetRawInputAt(0, object);
5095 SetRawInputAt(1, value);
5096 }
5097
Calin Juravle641547a2015-04-21 22:08:51 +01005098 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005099 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005100 }
5101
Calin Juravle52c48962014-12-16 17:02:57 +00005102 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005103 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005104 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005105 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005106 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005107 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5108 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005109
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005110 DECLARE_INSTRUCTION(InstanceFieldSet);
5111
5112 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005113 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5114 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5115 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5116 "Too many packed fields.");
5117
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005118 const FieldInfo field_info_;
5119
5120 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5121};
5122
Vladimir Markofcb503c2016-05-18 12:48:17 +01005123class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005124 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005125 HArrayGet(HInstruction* array,
5126 HInstruction* index,
5127 Primitive::Type type,
5128 uint32_t dex_pc,
5129 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005130 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005131 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005132 SetRawInputAt(0, array);
5133 SetRawInputAt(1, index);
5134 }
5135
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005136 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005137 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005138 return true;
5139 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005140 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005141 // TODO: We can be smarter here.
5142 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5143 // which generates the implicit null check. There are cases when these can be removed
5144 // to produce better code. If we ever add optimizations to do so we should allow an
5145 // implicit check here (as long as the address falls in the first page).
5146 return false;
5147 }
5148
David Brazdil4833f5a2015-12-16 10:37:39 +00005149 bool IsEquivalentOf(HArrayGet* other) const {
5150 bool result = (GetDexPc() == other->GetDexPc());
5151 if (kIsDebugBuild && result) {
5152 DCHECK_EQ(GetBlock(), other->GetBlock());
5153 DCHECK_EQ(GetArray(), other->GetArray());
5154 DCHECK_EQ(GetIndex(), other->GetIndex());
5155 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005156 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005157 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005158 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5159 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005160 }
5161 }
5162 return result;
5163 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005164
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005165 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5166
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005167 HInstruction* GetArray() const { return InputAt(0); }
5168 HInstruction* GetIndex() const { return InputAt(1); }
5169
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005170 DECLARE_INSTRUCTION(ArrayGet);
5171
5172 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005173 // We treat a String as an array, creating the HArrayGet from String.charAt()
5174 // intrinsic in the instruction simplifier. We can always determine whether
5175 // a particular HArrayGet is actually a String.charAt() by looking at the type
5176 // of the input but that requires holding the mutator lock, so we prefer to use
5177 // a flag, so that code generators don't need to do the locking.
5178 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5179 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5180 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5181 "Too many packed fields.");
5182
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005183 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5184};
5185
Vladimir Markofcb503c2016-05-18 12:48:17 +01005186class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005187 public:
5188 HArraySet(HInstruction* array,
5189 HInstruction* index,
5190 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005191 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005192 uint32_t dex_pc)
5193 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005194 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5195 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5196 SetPackedFlag<kFlagValueCanBeNull>(true);
5197 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005198 SetRawInputAt(0, array);
5199 SetRawInputAt(1, index);
5200 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005201 // Make a best guess now, may be refined during SSA building.
5202 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005203 }
5204
Calin Juravle77520bc2015-01-12 18:45:46 +00005205 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005206 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005207 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005208 }
5209
Mingyao Yang81014cb2015-06-02 03:16:27 -07005210 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005211 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005212
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005213 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005214 // TODO: Same as for ArrayGet.
5215 return false;
5216 }
5217
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005218 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005219 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005220 }
5221
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005222 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005223 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005224 }
5225
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005226 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005227 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005228 }
5229
Vladimir Markoa1de9182016-02-25 11:37:38 +00005230 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5231 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5232 bool StaticTypeOfArrayIsObjectArray() const {
5233 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5234 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005235
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005236 HInstruction* GetArray() const { return InputAt(0); }
5237 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005238 HInstruction* GetValue() const { return InputAt(2); }
5239
5240 Primitive::Type GetComponentType() const {
5241 // The Dex format does not type floating point index operations. Since the
5242 // `expected_component_type_` is set during building and can therefore not
5243 // be correct, we also check what is the value type. If it is a floating
5244 // point type, we must use that type.
5245 Primitive::Type value_type = GetValue()->GetType();
5246 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5247 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005248 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005249 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005250
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005251 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005252 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005253 }
5254
Aart Bik18b36ab2016-04-13 16:41:35 -07005255 void ComputeSideEffects() {
5256 Primitive::Type type = GetComponentType();
5257 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5258 SideEffectsForArchRuntimeCalls(type)));
5259 }
5260
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005261 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5262 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5263 }
5264
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005265 DECLARE_INSTRUCTION(ArraySet);
5266
5267 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005268 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5269 static constexpr size_t kFieldExpectedComponentTypeSize =
5270 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5271 static constexpr size_t kFlagNeedsTypeCheck =
5272 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5273 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005274 // Cached information for the reference_type_info_ so that codegen
5275 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005276 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5277 static constexpr size_t kNumberOfArraySetPackedBits =
5278 kFlagStaticTypeOfArrayIsObjectArray + 1;
5279 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5280 using ExpectedComponentTypeField =
5281 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005282
5283 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5284};
5285
Vladimir Markofcb503c2016-05-18 12:48:17 +01005286class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005287 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005288 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005289 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005290 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005291 // Note that arrays do not change length, so the instruction does not
5292 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005293 SetRawInputAt(0, array);
5294 }
5295
Calin Juravle77520bc2015-01-12 18:45:46 +00005296 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005297 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005298 return true;
5299 }
Calin Juravle641547a2015-04-21 22:08:51 +01005300 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5301 return obj == InputAt(0);
5302 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005303
Vladimir Markodce016e2016-04-28 13:10:02 +01005304 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5305
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005306 DECLARE_INSTRUCTION(ArrayLength);
5307
5308 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005309 // We treat a String as an array, creating the HArrayLength from String.length()
5310 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5311 // determine whether a particular HArrayLength is actually a String.length() by
5312 // looking at the type of the input but that requires holding the mutator lock, so
5313 // we prefer to use a flag, so that code generators don't need to do the locking.
5314 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5315 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5316 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5317 "Too many packed fields.");
5318
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005319 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5320};
5321
Vladimir Markofcb503c2016-05-18 12:48:17 +01005322class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005323 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005324 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5325 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005326 HBoundsCheck(HInstruction* index,
5327 HInstruction* length,
5328 uint32_t dex_pc,
5329 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5330 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5331 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005332 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005333 SetRawInputAt(0, index);
5334 SetRawInputAt(1, length);
5335 }
5336
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005337 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005338 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005339 return true;
5340 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005341
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005342 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005343
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005344 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005345
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005346 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5347 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5348
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005349 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005350
5351 DECLARE_INSTRUCTION(BoundsCheck);
5352
5353 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005354 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5355 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5356 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5357 // so we need to create an HEnvironment which will be translated to an InlineInfo
5358 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5359 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5360 // from the invoke as we don't want to look again at the dex bytecode.
5361 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5362
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005363 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5364};
5365
Vladimir Markofcb503c2016-05-18 12:48:17 +01005366class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005367 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005368 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005369 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005370
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005371 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005372 return true;
5373 }
5374
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005375 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5376 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005377
5378 DECLARE_INSTRUCTION(SuspendCheck);
5379
5380 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005381 // Only used for code generation, in order to share the same slow path between back edges
5382 // of a same loop.
5383 SlowPathCode* slow_path_;
5384
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005385 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5386};
5387
David Srbecky0cf44932015-12-09 14:09:59 +00005388// Pseudo-instruction which provides the native debugger with mapping information.
5389// It ensures that we can generate line number and local variables at this point.
5390class HNativeDebugInfo : public HTemplateInstruction<0> {
5391 public:
5392 explicit HNativeDebugInfo(uint32_t dex_pc)
5393 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5394
5395 bool NeedsEnvironment() const OVERRIDE {
5396 return true;
5397 }
5398
5399 DECLARE_INSTRUCTION(NativeDebugInfo);
5400
5401 private:
5402 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5403};
5404
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005405/**
5406 * Instruction to load a Class object.
5407 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005408class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005409 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005410 // Determines how to load the Class.
5411 enum class LoadKind {
5412 // Use the Class* from the method's own ArtMethod*.
5413 kReferrersClass,
5414
5415 // Use boot image Class* address that will be known at link time.
5416 // Used for boot image classes referenced by boot image code in non-PIC mode.
5417 kBootImageLinkTimeAddress,
5418
5419 // Use PC-relative boot image Class* address that will be known at link time.
5420 // Used for boot image classes referenced by boot image code in PIC mode.
5421 kBootImageLinkTimePcRelative,
5422
5423 // Use a known boot image Class* address, embedded in the code by the codegen.
5424 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5425 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5426 // GetIncludePatchInformation().
5427 kBootImageAddress,
5428
5429 // Load from the resolved types array at an absolute address.
5430 // Used for classes outside the boot image referenced by JIT-compiled code.
5431 kDexCacheAddress,
5432
5433 // Load from resolved types array in the dex cache using a PC-relative load.
5434 // Used for classes outside boot image when we know that we can access
5435 // the dex cache arrays using a PC-relative load.
5436 kDexCachePcRelative,
5437
5438 // Load from resolved types array accessed through the class loaded from
5439 // the compiled method's own ArtMethod*. This is the default access type when
5440 // all other types are unavailable.
5441 kDexCacheViaMethod,
5442
5443 kLast = kDexCacheViaMethod
5444 };
5445
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005446 HLoadClass(HCurrentMethod* current_method,
5447 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005448 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005449 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005450 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005451 bool needs_access_check,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005452 bool is_in_dex_cache,
5453 bool is_in_boot_image)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005454 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5455 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005456 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005457 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005458 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005459 // Referrers class should not need access check. We never inline unverified
5460 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005461 DCHECK(!is_referrers_class || !needs_access_check);
5462
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005463 SetPackedField<LoadKindField>(
5464 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005465 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5466 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005467 SetPackedFlag<kFlagIsInBootImage>(is_in_boot_image);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005468 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005469 }
5470
5471 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5472 DCHECK(HasAddress(load_kind));
5473 load_data_.address = address;
5474 SetLoadKindInternal(load_kind);
5475 }
5476
5477 void SetLoadKindWithTypeReference(LoadKind load_kind,
5478 const DexFile& dex_file,
5479 uint32_t type_index) {
5480 DCHECK(HasTypeReference(load_kind));
5481 DCHECK(IsSameDexFile(dex_file_, dex_file));
5482 DCHECK_EQ(type_index_, type_index);
5483 SetLoadKindInternal(load_kind);
5484 }
5485
5486 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5487 const DexFile& dex_file,
5488 uint32_t element_index) {
5489 DCHECK(HasDexCacheReference(load_kind));
5490 DCHECK(IsSameDexFile(dex_file_, dex_file));
5491 load_data_.dex_cache_element_index = element_index;
5492 SetLoadKindInternal(load_kind);
5493 }
5494
5495 LoadKind GetLoadKind() const {
5496 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005497 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005498
5499 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005500
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005501 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005502
5503 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5504
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005505 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005506
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005507 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005508 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005509 }
5510
Calin Juravle0ba218d2015-05-19 18:46:01 +01005511 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005512 // The entrypoint the code generator is going to call does not do
5513 // clinit of the class.
5514 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005515 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005516 }
5517
5518 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005519 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005520 (!IsReferrersClass() && !IsInDexCache()) ||
5521 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005522 }
5523
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005524
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005525 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005526 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005527 }
5528
Calin Juravleacf735c2015-02-12 15:25:22 +00005529 ReferenceTypeInfo GetLoadedClassRTI() {
5530 return loaded_class_rti_;
5531 }
5532
5533 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5534 // Make sure we only set exact types (the loaded class should never be merged).
5535 DCHECK(rti.IsExact());
5536 loaded_class_rti_ = rti;
5537 }
5538
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005539 uint32_t GetTypeIndex() const { return type_index_; }
5540 const DexFile& GetDexFile() const { return dex_file_; }
5541
5542 uint32_t GetDexCacheElementOffset() const;
5543
5544 uint64_t GetAddress() const {
5545 DCHECK(HasAddress(GetLoadKind()));
5546 return load_data_.address;
5547 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005548
Vladimir Markoa1de9182016-02-25 11:37:38 +00005549 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005550
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005551 static SideEffects SideEffectsForArchRuntimeCalls() {
5552 return SideEffects::CanTriggerGC();
5553 }
5554
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005555 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005556 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5557 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005558 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005559 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005560
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005561 void MarkInDexCache() {
5562 SetPackedFlag<kFlagIsInDexCache>(true);
5563 DCHECK(!NeedsEnvironment());
5564 RemoveEnvironment();
5565 SetSideEffects(SideEffects::None());
5566 }
5567
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005568 void MarkInBootImage() {
5569 SetPackedFlag<kFlagIsInBootImage>(true);
5570 }
5571
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005572 void AddSpecialInput(HInstruction* special_input);
5573
5574 using HInstruction::GetInputRecords; // Keep the const version visible.
5575 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5576 return ArrayRef<HUserRecord<HInstruction*>>(
5577 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5578 }
5579
5580 Primitive::Type GetType() const OVERRIDE {
5581 return Primitive::kPrimNot;
5582 }
5583
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005584 DECLARE_INSTRUCTION(LoadClass);
5585
5586 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005587 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005588 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005589 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005590 // Whether this instruction must generate the initialization check.
5591 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005592 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005593 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5594 static constexpr size_t kFieldLoadKindSize =
5595 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5596 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005597 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005598 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5599
5600 static bool HasTypeReference(LoadKind load_kind) {
5601 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5602 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5603 load_kind == LoadKind::kDexCacheViaMethod ||
5604 load_kind == LoadKind::kReferrersClass;
5605 }
5606
5607 static bool HasAddress(LoadKind load_kind) {
5608 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5609 }
5610
5611 static bool HasDexCacheReference(LoadKind load_kind) {
5612 return load_kind == LoadKind::kDexCachePcRelative;
5613 }
5614
5615 void SetLoadKindInternal(LoadKind load_kind);
5616
5617 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5618 // For other load kinds it's empty or possibly some architecture-specific instruction
5619 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5620 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005621
5622 const uint16_t type_index_;
5623 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005624
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005625 union {
5626 uint32_t dex_cache_element_index; // Only for dex cache reference.
5627 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5628 } load_data_;
5629
Calin Juravleacf735c2015-02-12 15:25:22 +00005630 ReferenceTypeInfo loaded_class_rti_;
5631
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005632 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5633};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005634std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5635
5636// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5637inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5638 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5639 return load_data_.dex_cache_element_index;
5640}
5641
5642// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5643inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005644 // The special input is used for PC-relative loads on some architectures,
5645 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005646 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005647 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5648 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5649 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005650 DCHECK(special_input_.GetInstruction() == nullptr);
5651 special_input_ = HUserRecord<HInstruction*>(special_input);
5652 special_input->AddUseAt(this, 0);
5653}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005654
Vladimir Marko372f10e2016-05-17 16:30:10 +01005655class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005656 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005657 // Determines how to load the String.
5658 enum class LoadKind {
5659 // Use boot image String* address that will be known at link time.
5660 // Used for boot image strings referenced by boot image code in non-PIC mode.
5661 kBootImageLinkTimeAddress,
5662
5663 // Use PC-relative boot image String* address that will be known at link time.
5664 // Used for boot image strings referenced by boot image code in PIC mode.
5665 kBootImageLinkTimePcRelative,
5666
5667 // Use a known boot image String* address, embedded in the code by the codegen.
5668 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5669 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5670 // GetIncludePatchInformation().
5671 kBootImageAddress,
5672
5673 // Load from the resolved strings array at an absolute address.
5674 // Used for strings outside the boot image referenced by JIT-compiled code.
5675 kDexCacheAddress,
5676
Vladimir Markoaad75c62016-10-03 08:46:48 +00005677 // Load from an entry in the .bss section using a PC-relative load.
5678 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5679 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005680
5681 // Load from resolved strings array accessed through the class loaded from
5682 // the compiled method's own ArtMethod*. This is the default access type when
5683 // all other types are unavailable.
5684 kDexCacheViaMethod,
5685
5686 kLast = kDexCacheViaMethod
5687 };
5688
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005689 HLoadString(HCurrentMethod* current_method,
5690 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005691 const DexFile& dex_file,
5692 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005693 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5694 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005695 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005696 SetPackedFlag<kFlagIsInDexCache>(false);
5697 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005698 load_data_.dex_file_ = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005699 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005700
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005701 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5702 DCHECK(HasAddress(load_kind));
5703 load_data_.address = address;
5704 SetLoadKindInternal(load_kind);
5705 }
5706
5707 void SetLoadKindWithStringReference(LoadKind load_kind,
5708 const DexFile& dex_file,
5709 uint32_t string_index) {
5710 DCHECK(HasStringReference(load_kind));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005711 load_data_.dex_file_ = &dex_file;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005712 string_index_ = string_index;
5713 SetLoadKindInternal(load_kind);
5714 }
5715
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005716 LoadKind GetLoadKind() const {
5717 return GetPackedField<LoadKindField>();
5718 }
5719
5720 const DexFile& GetDexFile() const;
5721
5722 uint32_t GetStringIndex() const {
5723 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5724 return string_index_;
5725 }
5726
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005727 uint64_t GetAddress() const {
5728 DCHECK(HasAddress(GetLoadKind()));
5729 return load_data_.address;
5730 }
5731
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005732 bool CanBeMoved() const OVERRIDE { return true; }
5733
Vladimir Marko372f10e2016-05-17 16:30:10 +01005734 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005735
5736 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5737
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005738 // Will call the runtime if we need to load the string through
5739 // the dex cache and the string is not guaranteed to be there yet.
5740 bool NeedsEnvironment() const OVERRIDE {
5741 LoadKind load_kind = GetLoadKind();
5742 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5743 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5744 load_kind == LoadKind::kBootImageAddress) {
5745 return false;
5746 }
5747 return !IsInDexCache();
5748 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005749
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005750 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5751 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5752 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005753
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005754 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005755 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005756
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005757 static SideEffects SideEffectsForArchRuntimeCalls() {
5758 return SideEffects::CanTriggerGC();
5759 }
5760
Vladimir Markoa1de9182016-02-25 11:37:38 +00005761 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5762
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005763 void MarkInDexCache() {
5764 SetPackedFlag<kFlagIsInDexCache>(true);
5765 DCHECK(!NeedsEnvironment());
5766 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005767 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005768 }
5769
Vladimir Marko372f10e2016-05-17 16:30:10 +01005770 void AddSpecialInput(HInstruction* special_input);
5771
5772 using HInstruction::GetInputRecords; // Keep the const version visible.
5773 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5774 return ArrayRef<HUserRecord<HInstruction*>>(
5775 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005776 }
5777
Vladimir Marko372f10e2016-05-17 16:30:10 +01005778 Primitive::Type GetType() const OVERRIDE {
5779 return Primitive::kPrimNot;
5780 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005781
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005782 DECLARE_INSTRUCTION(LoadString);
5783
5784 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005785 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005786 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5787 static constexpr size_t kFieldLoadKindSize =
5788 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5789 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005790 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005791 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005792
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005793 static bool HasStringReference(LoadKind load_kind) {
5794 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5795 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005796 load_kind == LoadKind::kBssEntry ||
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005797 load_kind == LoadKind::kDexCacheViaMethod;
5798 }
5799
5800 static bool HasAddress(LoadKind load_kind) {
5801 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5802 }
5803
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005804 void SetLoadKindInternal(LoadKind load_kind);
5805
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005806 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5807 // For other load kinds it's empty or possibly some architecture-specific instruction
5808 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005809 HUserRecord<HInstruction*> special_input_;
5810
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005811 // String index serves also as the hash code and it's also needed for slow-paths,
5812 // so it must not be overwritten with other load data.
5813 uint32_t string_index_;
5814
5815 union {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005816 const DexFile* dex_file_; // For string reference.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005817 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5818 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005819
5820 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5821};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005822std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5823
5824// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5825inline const DexFile& HLoadString::GetDexFile() const {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005826 DCHECK(HasStringReference(GetLoadKind())) << GetLoadKind();
5827 return *load_data_.dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005828}
5829
5830// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5831inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005832 // The special input is used for PC-relative loads on some architectures,
5833 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005834 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005835 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005836 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5837 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005838 // HLoadString::GetInputRecords() returns an empty array at this point,
5839 // so use the GetInputRecords() from the base class to set the input record.
5840 DCHECK(special_input_.GetInstruction() == nullptr);
5841 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005842 special_input->AddUseAt(this, 0);
5843}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005844
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005845/**
5846 * Performs an initialization check on its Class object input.
5847 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005848class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005849 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005850 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005851 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005852 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005853 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5854 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005855 SetRawInputAt(0, constant);
5856 }
5857
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005858 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005859 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005860 return true;
5861 }
5862
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005863 bool NeedsEnvironment() const OVERRIDE {
5864 // May call runtime to initialize the class.
5865 return true;
5866 }
5867
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005868 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005869
5870 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5871
5872 DECLARE_INSTRUCTION(ClinitCheck);
5873
5874 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005875 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5876};
5877
Vladimir Markofcb503c2016-05-18 12:48:17 +01005878class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005879 public:
5880 HStaticFieldGet(HInstruction* cls,
5881 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005882 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005883 bool is_volatile,
5884 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005885 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005886 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005887 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005888 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005889 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005890 field_info_(field_offset,
5891 field_type,
5892 is_volatile,
5893 field_idx,
5894 declaring_class_def_index,
5895 dex_file,
5896 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005897 SetRawInputAt(0, cls);
5898 }
5899
Calin Juravle52c48962014-12-16 17:02:57 +00005900
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005901 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005902
Vladimir Marko372f10e2016-05-17 16:30:10 +01005903 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5904 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005905 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005906 }
5907
5908 size_t ComputeHashCode() const OVERRIDE {
5909 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5910 }
5911
Calin Juravle52c48962014-12-16 17:02:57 +00005912 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005913 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5914 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005915 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005916
5917 DECLARE_INSTRUCTION(StaticFieldGet);
5918
5919 private:
5920 const FieldInfo field_info_;
5921
5922 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5923};
5924
Vladimir Markofcb503c2016-05-18 12:48:17 +01005925class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005926 public:
5927 HStaticFieldSet(HInstruction* cls,
5928 HInstruction* value,
5929 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005930 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005931 bool is_volatile,
5932 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005933 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005934 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005935 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005936 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005937 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005938 field_info_(field_offset,
5939 field_type,
5940 is_volatile,
5941 field_idx,
5942 declaring_class_def_index,
5943 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005944 dex_cache) {
5945 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005946 SetRawInputAt(0, cls);
5947 SetRawInputAt(1, value);
5948 }
5949
Calin Juravle52c48962014-12-16 17:02:57 +00005950 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005951 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5952 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005953 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005954
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005955 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005956 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5957 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005958
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005959 DECLARE_INSTRUCTION(StaticFieldSet);
5960
5961 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005962 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5963 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5964 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5965 "Too many packed fields.");
5966
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005967 const FieldInfo field_info_;
5968
5969 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5970};
5971
Vladimir Markofcb503c2016-05-18 12:48:17 +01005972class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005973 public:
5974 HUnresolvedInstanceFieldGet(HInstruction* obj,
5975 Primitive::Type field_type,
5976 uint32_t field_index,
5977 uint32_t dex_pc)
5978 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5979 field_index_(field_index) {
5980 SetRawInputAt(0, obj);
5981 }
5982
5983 bool NeedsEnvironment() const OVERRIDE { return true; }
5984 bool CanThrow() const OVERRIDE { return true; }
5985
5986 Primitive::Type GetFieldType() const { return GetType(); }
5987 uint32_t GetFieldIndex() const { return field_index_; }
5988
5989 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5990
5991 private:
5992 const uint32_t field_index_;
5993
5994 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5995};
5996
Vladimir Markofcb503c2016-05-18 12:48:17 +01005997class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005998 public:
5999 HUnresolvedInstanceFieldSet(HInstruction* obj,
6000 HInstruction* value,
6001 Primitive::Type field_type,
6002 uint32_t field_index,
6003 uint32_t dex_pc)
6004 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006005 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006006 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006007 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006008 SetRawInputAt(0, obj);
6009 SetRawInputAt(1, value);
6010 }
6011
6012 bool NeedsEnvironment() const OVERRIDE { return true; }
6013 bool CanThrow() const OVERRIDE { return true; }
6014
Vladimir Markoa1de9182016-02-25 11:37:38 +00006015 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006016 uint32_t GetFieldIndex() const { return field_index_; }
6017
6018 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6019
6020 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006021 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6022 static constexpr size_t kFieldFieldTypeSize =
6023 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6024 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6025 kFieldFieldType + kFieldFieldTypeSize;
6026 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6027 "Too many packed fields.");
6028 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6029
Calin Juravlee460d1d2015-09-29 04:52:17 +01006030 const uint32_t field_index_;
6031
6032 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6033};
6034
Vladimir Markofcb503c2016-05-18 12:48:17 +01006035class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006036 public:
6037 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6038 uint32_t field_index,
6039 uint32_t dex_pc)
6040 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6041 field_index_(field_index) {
6042 }
6043
6044 bool NeedsEnvironment() const OVERRIDE { return true; }
6045 bool CanThrow() const OVERRIDE { return true; }
6046
6047 Primitive::Type GetFieldType() const { return GetType(); }
6048 uint32_t GetFieldIndex() const { return field_index_; }
6049
6050 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6051
6052 private:
6053 const uint32_t field_index_;
6054
6055 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6056};
6057
Vladimir Markofcb503c2016-05-18 12:48:17 +01006058class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006059 public:
6060 HUnresolvedStaticFieldSet(HInstruction* value,
6061 Primitive::Type field_type,
6062 uint32_t field_index,
6063 uint32_t dex_pc)
6064 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006065 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006066 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006067 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006068 SetRawInputAt(0, value);
6069 }
6070
6071 bool NeedsEnvironment() const OVERRIDE { return true; }
6072 bool CanThrow() const OVERRIDE { return true; }
6073
Vladimir Markoa1de9182016-02-25 11:37:38 +00006074 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006075 uint32_t GetFieldIndex() const { return field_index_; }
6076
6077 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6078
6079 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006080 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6081 static constexpr size_t kFieldFieldTypeSize =
6082 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6083 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6084 kFieldFieldType + kFieldFieldTypeSize;
6085 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6086 "Too many packed fields.");
6087 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6088
Calin Juravlee460d1d2015-09-29 04:52:17 +01006089 const uint32_t field_index_;
6090
6091 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6092};
6093
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006094// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006095class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006096 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006097 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6098 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006099
David Brazdilbbd733e2015-08-18 17:48:17 +01006100 bool CanBeNull() const OVERRIDE { return false; }
6101
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006102 DECLARE_INSTRUCTION(LoadException);
6103
6104 private:
6105 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6106};
6107
David Brazdilcb1c0552015-08-04 16:22:25 +01006108// Implicit part of move-exception which clears thread-local exception storage.
6109// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006110class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006111 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006112 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6113 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006114
6115 DECLARE_INSTRUCTION(ClearException);
6116
6117 private:
6118 DISALLOW_COPY_AND_ASSIGN(HClearException);
6119};
6120
Vladimir Markofcb503c2016-05-18 12:48:17 +01006121class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006122 public:
6123 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006124 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006125 SetRawInputAt(0, exception);
6126 }
6127
6128 bool IsControlFlow() const OVERRIDE { return true; }
6129
6130 bool NeedsEnvironment() const OVERRIDE { return true; }
6131
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006132 bool CanThrow() const OVERRIDE { return true; }
6133
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006134
6135 DECLARE_INSTRUCTION(Throw);
6136
6137 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006138 DISALLOW_COPY_AND_ASSIGN(HThrow);
6139};
6140
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006141/**
6142 * Implementation strategies for the code generator of a HInstanceOf
6143 * or `HCheckCast`.
6144 */
6145enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006146 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006147 kExactCheck, // Can do a single class compare.
6148 kClassHierarchyCheck, // Can just walk the super class chain.
6149 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6150 kInterfaceCheck, // No optimization yet when checking against an interface.
6151 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006152 kArrayCheck, // No optimization yet when checking against a generic array.
6153 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006154};
6155
Roland Levillain86503782016-02-11 19:07:30 +00006156std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6157
Vladimir Markofcb503c2016-05-18 12:48:17 +01006158class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006159 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006160 HInstanceOf(HInstruction* object,
6161 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006162 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006163 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006164 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006165 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006166 dex_pc) {
6167 SetPackedField<TypeCheckKindField>(check_kind);
6168 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006169 SetRawInputAt(0, object);
6170 SetRawInputAt(1, constant);
6171 }
6172
6173 bool CanBeMoved() const OVERRIDE { return true; }
6174
Vladimir Marko372f10e2016-05-17 16:30:10 +01006175 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006176 return true;
6177 }
6178
6179 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006180 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006181 }
6182
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006183 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006184 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6185 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6186 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6187 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006188
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006189 static bool CanCallRuntime(TypeCheckKind check_kind) {
6190 // Mips currently does runtime calls for any other checks.
6191 return check_kind != TypeCheckKind::kExactCheck;
6192 }
6193
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006194 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006195 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006196 }
6197
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006198 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006199
6200 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006201 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6202 static constexpr size_t kFieldTypeCheckKindSize =
6203 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6204 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6205 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6206 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6207 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006208
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006209 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6210};
6211
Vladimir Markofcb503c2016-05-18 12:48:17 +01006212class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006213 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006214 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006215 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006216 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6217 SetPackedFlag<kFlagUpperCanBeNull>(true);
6218 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006219 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006220 SetRawInputAt(0, input);
6221 }
6222
David Brazdilf5552582015-12-27 13:36:12 +00006223 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006224 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006225 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006226 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006227
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006228 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006229 DCHECK(GetUpperCanBeNull() || !can_be_null);
6230 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006231 }
6232
Vladimir Markoa1de9182016-02-25 11:37:38 +00006233 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006234
Calin Juravleb1498f62015-02-16 13:13:29 +00006235 DECLARE_INSTRUCTION(BoundType);
6236
6237 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006238 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6239 // is false then CanBeNull() cannot be true).
6240 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6241 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6242 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6243 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6244
Calin Juravleb1498f62015-02-16 13:13:29 +00006245 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006246 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6247 // It is used to bound the type in cases like:
6248 // if (x instanceof ClassX) {
6249 // // uper_bound_ will be ClassX
6250 // }
David Brazdilf5552582015-12-27 13:36:12 +00006251 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006252
6253 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6254};
6255
Vladimir Markofcb503c2016-05-18 12:48:17 +01006256class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006257 public:
6258 HCheckCast(HInstruction* object,
6259 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006260 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006261 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006262 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6263 SetPackedField<TypeCheckKindField>(check_kind);
6264 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006265 SetRawInputAt(0, object);
6266 SetRawInputAt(1, constant);
6267 }
6268
6269 bool CanBeMoved() const OVERRIDE { return true; }
6270
Vladimir Marko372f10e2016-05-17 16:30:10 +01006271 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006272 return true;
6273 }
6274
6275 bool NeedsEnvironment() const OVERRIDE {
6276 // Instruction may throw a CheckCastError.
6277 return true;
6278 }
6279
6280 bool CanThrow() const OVERRIDE { return true; }
6281
Vladimir Markoa1de9182016-02-25 11:37:38 +00006282 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6283 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6284 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6285 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006286
6287 DECLARE_INSTRUCTION(CheckCast);
6288
6289 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006290 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6291 static constexpr size_t kFieldTypeCheckKindSize =
6292 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6293 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6294 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6295 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6296 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006297
6298 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006299};
6300
Andreas Gampe26de38b2016-07-27 17:53:11 -07006301/**
6302 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6303 * @details We define the combined barrier types that are actually required
6304 * by the Java Memory Model, rather than using exactly the terminology from
6305 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6306 * primitives. Note that the JSR-133 cookbook generally does not deal with
6307 * store atomicity issues, and the recipes there are not always entirely sufficient.
6308 * The current recipe is as follows:
6309 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6310 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6311 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6312 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6313 * class has final fields.
6314 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6315 * store-to-memory instructions. Only generated together with non-temporal stores.
6316 */
6317enum MemBarrierKind {
6318 kAnyStore,
6319 kLoadAny,
6320 kStoreStore,
6321 kAnyAny,
6322 kNTStoreStore,
6323 kLastBarrierKind = kNTStoreStore
6324};
6325std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6326
Vladimir Markofcb503c2016-05-18 12:48:17 +01006327class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006328 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006329 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006330 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006331 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6332 SetPackedField<BarrierKindField>(barrier_kind);
6333 }
Calin Juravle27df7582015-04-17 19:12:31 +01006334
Vladimir Markoa1de9182016-02-25 11:37:38 +00006335 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006336
6337 DECLARE_INSTRUCTION(MemoryBarrier);
6338
6339 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006340 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6341 static constexpr size_t kFieldBarrierKindSize =
6342 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6343 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6344 kFieldBarrierKind + kFieldBarrierKindSize;
6345 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6346 "Too many packed fields.");
6347 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006348
6349 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6350};
6351
Vladimir Markofcb503c2016-05-18 12:48:17 +01006352class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006353 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006354 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006355 kEnter,
6356 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006357 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006358 };
6359
6360 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006361 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006362 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6363 dex_pc) {
6364 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006365 SetRawInputAt(0, object);
6366 }
6367
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006368 // Instruction may go into runtime, so we need an environment.
6369 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006370
6371 bool CanThrow() const OVERRIDE {
6372 // Verifier guarantees that monitor-exit cannot throw.
6373 // This is important because it allows the HGraphBuilder to remove
6374 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6375 return IsEnter();
6376 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006377
Vladimir Markoa1de9182016-02-25 11:37:38 +00006378 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6379 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006380
6381 DECLARE_INSTRUCTION(MonitorOperation);
6382
Calin Juravle52c48962014-12-16 17:02:57 +00006383 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006384 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6385 static constexpr size_t kFieldOperationKindSize =
6386 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6387 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6388 kFieldOperationKind + kFieldOperationKindSize;
6389 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6390 "Too many packed fields.");
6391 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006392
6393 private:
6394 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6395};
6396
Vladimir Markofcb503c2016-05-18 12:48:17 +01006397class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006398 public:
6399 HSelect(HInstruction* condition,
6400 HInstruction* true_value,
6401 HInstruction* false_value,
6402 uint32_t dex_pc)
6403 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6404 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6405
6406 // First input must be `true_value` or `false_value` to allow codegens to
6407 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6408 // that architectures which implement HSelect as a conditional move also
6409 // will not need to invert the condition.
6410 SetRawInputAt(0, false_value);
6411 SetRawInputAt(1, true_value);
6412 SetRawInputAt(2, condition);
6413 }
6414
6415 HInstruction* GetFalseValue() const { return InputAt(0); }
6416 HInstruction* GetTrueValue() const { return InputAt(1); }
6417 HInstruction* GetCondition() const { return InputAt(2); }
6418
6419 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006420 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6421 return true;
6422 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006423
6424 bool CanBeNull() const OVERRIDE {
6425 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6426 }
6427
6428 DECLARE_INSTRUCTION(Select);
6429
6430 private:
6431 DISALLOW_COPY_AND_ASSIGN(HSelect);
6432};
6433
Vladimir Markof9f64412015-09-02 14:05:49 +01006434class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006435 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006436 MoveOperands(Location source,
6437 Location destination,
6438 Primitive::Type type,
6439 HInstruction* instruction)
6440 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006441
6442 Location GetSource() const { return source_; }
6443 Location GetDestination() const { return destination_; }
6444
6445 void SetSource(Location value) { source_ = value; }
6446 void SetDestination(Location value) { destination_ = value; }
6447
6448 // The parallel move resolver marks moves as "in-progress" by clearing the
6449 // destination (but not the source).
6450 Location MarkPending() {
6451 DCHECK(!IsPending());
6452 Location dest = destination_;
6453 destination_ = Location::NoLocation();
6454 return dest;
6455 }
6456
6457 void ClearPending(Location dest) {
6458 DCHECK(IsPending());
6459 destination_ = dest;
6460 }
6461
6462 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006463 DCHECK(source_.IsValid() || destination_.IsInvalid());
6464 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006465 }
6466
6467 // True if this blocks a move from the given location.
6468 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006469 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006470 }
6471
6472 // A move is redundant if it's been eliminated, if its source and
6473 // destination are the same, or if its destination is unneeded.
6474 bool IsRedundant() const {
6475 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6476 }
6477
6478 // We clear both operands to indicate move that's been eliminated.
6479 void Eliminate() {
6480 source_ = destination_ = Location::NoLocation();
6481 }
6482
6483 bool IsEliminated() const {
6484 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6485 return source_.IsInvalid();
6486 }
6487
Alexey Frunze4dda3372015-06-01 18:31:49 -07006488 Primitive::Type GetType() const { return type_; }
6489
Nicolas Geoffray90218252015-04-15 11:56:51 +01006490 bool Is64BitMove() const {
6491 return Primitive::Is64BitType(type_);
6492 }
6493
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006494 HInstruction* GetInstruction() const { return instruction_; }
6495
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006496 private:
6497 Location source_;
6498 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006499 // The type this move is for.
6500 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006501 // The instruction this move is assocatied with. Null when this move is
6502 // for moving an input in the expected locations of user (including a phi user).
6503 // This is only used in debug mode, to ensure we do not connect interval siblings
6504 // in the same parallel move.
6505 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006506};
6507
Roland Levillainc9285912015-12-18 10:38:42 +00006508std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6509
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006510static constexpr size_t kDefaultNumberOfMoves = 4;
6511
Vladimir Markofcb503c2016-05-18 12:48:17 +01006512class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006513 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006514 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006515 : HTemplateInstruction(SideEffects::None(), dex_pc),
6516 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6517 moves_.reserve(kDefaultNumberOfMoves);
6518 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006519
Nicolas Geoffray90218252015-04-15 11:56:51 +01006520 void AddMove(Location source,
6521 Location destination,
6522 Primitive::Type type,
6523 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006524 DCHECK(source.IsValid());
6525 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006526 if (kIsDebugBuild) {
6527 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006528 for (const MoveOperands& move : moves_) {
6529 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006530 // Special case the situation where the move is for the spill slot
6531 // of the instruction.
6532 if ((GetPrevious() == instruction)
6533 || ((GetPrevious() == nullptr)
6534 && instruction->IsPhi()
6535 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006536 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006537 << "Doing parallel moves for the same instruction.";
6538 } else {
6539 DCHECK(false) << "Doing parallel moves for the same instruction.";
6540 }
6541 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006542 }
6543 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006544 for (const MoveOperands& move : moves_) {
6545 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006546 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006547 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006548 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006549 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006550 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006551 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006552 }
6553
Vladimir Marko225b6462015-09-28 12:17:40 +01006554 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006555 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006556 }
6557
Vladimir Marko225b6462015-09-28 12:17:40 +01006558 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006559
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006560 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006561
6562 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006563 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006564
6565 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6566};
6567
Mark Mendell0616ae02015-04-17 12:49:27 -04006568} // namespace art
6569
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006570#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6571#include "nodes_shared.h"
6572#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006573#ifdef ART_ENABLE_CODEGEN_arm
6574#include "nodes_arm.h"
6575#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006576#ifdef ART_ENABLE_CODEGEN_arm64
6577#include "nodes_arm64.h"
6578#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006579#ifdef ART_ENABLE_CODEGEN_mips
6580#include "nodes_mips.h"
6581#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006582#ifdef ART_ENABLE_CODEGEN_x86
6583#include "nodes_x86.h"
6584#endif
6585
6586namespace art {
6587
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006588class HGraphVisitor : public ValueObject {
6589 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006590 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6591 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006592
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006593 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006594 virtual void VisitBasicBlock(HBasicBlock* block);
6595
Roland Levillain633021e2014-10-01 14:12:25 +01006596 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006597 void VisitInsertionOrder();
6598
Roland Levillain633021e2014-10-01 14:12:25 +01006599 // Visit the graph following dominator tree reverse post-order.
6600 void VisitReversePostOrder();
6601
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006602 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006603
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006604 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006605#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006606 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6607
6608 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6609
6610#undef DECLARE_VISIT_INSTRUCTION
6611
6612 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006613 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006614
6615 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6616};
6617
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006618class HGraphDelegateVisitor : public HGraphVisitor {
6619 public:
6620 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6621 virtual ~HGraphDelegateVisitor() {}
6622
6623 // Visit functions that delegate to to super class.
6624#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006625 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006626
6627 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6628
6629#undef DECLARE_VISIT_INSTRUCTION
6630
6631 private:
6632 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6633};
6634
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006635// Iterator over the blocks that art part of the loop. Includes blocks part
6636// of an inner loop. The order in which the blocks are iterated is on their
6637// block id.
6638class HBlocksInLoopIterator : public ValueObject {
6639 public:
6640 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6641 : blocks_in_loop_(info.GetBlocks()),
6642 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6643 index_(0) {
6644 if (!blocks_in_loop_.IsBitSet(index_)) {
6645 Advance();
6646 }
6647 }
6648
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006649 bool Done() const { return index_ == blocks_.size(); }
6650 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006651 void Advance() {
6652 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006653 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006654 if (blocks_in_loop_.IsBitSet(index_)) {
6655 break;
6656 }
6657 }
6658 }
6659
6660 private:
6661 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006662 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006663 size_t index_;
6664
6665 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6666};
6667
Mingyao Yang3584bce2015-05-19 16:01:59 -07006668// Iterator over the blocks that art part of the loop. Includes blocks part
6669// of an inner loop. The order in which the blocks are iterated is reverse
6670// post order.
6671class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6672 public:
6673 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6674 : blocks_in_loop_(info.GetBlocks()),
6675 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6676 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006677 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006678 Advance();
6679 }
6680 }
6681
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006682 bool Done() const { return index_ == blocks_.size(); }
6683 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006684 void Advance() {
6685 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006686 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6687 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006688 break;
6689 }
6690 }
6691 }
6692
6693 private:
6694 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006695 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006696 size_t index_;
6697
6698 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6699};
6700
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006701inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006702 if (constant->IsIntConstant()) {
6703 return constant->AsIntConstant()->GetValue();
6704 } else if (constant->IsLongConstant()) {
6705 return constant->AsLongConstant()->GetValue();
6706 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006707 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006708 return 0;
6709 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006710}
6711
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006712#define INSTRUCTION_TYPE_CHECK(type, super) \
6713 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6714 inline const H##type* HInstruction::As##type() const { \
6715 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6716 } \
6717 inline H##type* HInstruction::As##type() { \
6718 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6719 }
6720
6721 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6722#undef INSTRUCTION_TYPE_CHECK
6723
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006724// Create space in `blocks` for adding `number_of_new_blocks` entries
6725// starting at location `at`. Blocks after `at` are moved accordingly.
6726inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6727 size_t number_of_new_blocks,
6728 size_t after) {
6729 DCHECK_LT(after, blocks->size());
6730 size_t old_size = blocks->size();
6731 size_t new_size = old_size + number_of_new_blocks;
6732 blocks->resize(new_size);
6733 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6734}
6735
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006736} // namespace art
6737
6738#endif // ART_COMPILER_OPTIMIZING_NODES_H_