blob: 7d74e47a0a8129247370075c9a9009d4f362fd82 [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
David Sehrc757dec2016-11-04 15:48:34 -0700772 bool HasExitEdge() const;
773
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000774 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100775 // Internal recursive implementation of `Populate`.
776 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100777 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100778
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000779 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100780 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000781 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100782 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100783 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100784 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000785
786 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
787};
788
David Brazdilec16f792015-08-19 15:04:01 +0100789// Stores try/catch information for basic blocks.
790// Note that HGraph is constructed so that catch blocks cannot simultaneously
791// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100792class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100793 public:
794 // Try block information constructor.
795 explicit TryCatchInformation(const HTryBoundary& try_entry)
796 : try_entry_(&try_entry),
797 catch_dex_file_(nullptr),
798 catch_type_index_(DexFile::kDexNoIndex16) {
799 DCHECK(try_entry_ != nullptr);
800 }
801
802 // Catch block information constructor.
803 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
804 : try_entry_(nullptr),
805 catch_dex_file_(&dex_file),
806 catch_type_index_(catch_type_index) {}
807
808 bool IsTryBlock() const { return try_entry_ != nullptr; }
809
810 const HTryBoundary& GetTryEntry() const {
811 DCHECK(IsTryBlock());
812 return *try_entry_;
813 }
814
815 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
816
817 bool IsCatchAllTypeIndex() const {
818 DCHECK(IsCatchBlock());
819 return catch_type_index_ == DexFile::kDexNoIndex16;
820 }
821
822 uint16_t GetCatchTypeIndex() const {
823 DCHECK(IsCatchBlock());
824 return catch_type_index_;
825 }
826
827 const DexFile& GetCatchDexFile() const {
828 DCHECK(IsCatchBlock());
829 return *catch_dex_file_;
830 }
831
832 private:
833 // One of possibly several TryBoundary instructions entering the block's try.
834 // Only set for try blocks.
835 const HTryBoundary* try_entry_;
836
837 // Exception type information. Only set for catch blocks.
838 const DexFile* catch_dex_file_;
839 const uint16_t catch_type_index_;
840};
841
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100842static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100843static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100844
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000845// A block in a method. Contains the list of instructions represented
846// as a double linked list. Each block knows its predecessors and
847// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100848
Vladimir Markof9f64412015-09-02 14:05:49 +0100849class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000850 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700851 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000852 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000853 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
854 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000855 loop_information_(nullptr),
856 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000857 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100858 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100859 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100860 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000861 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000862 try_catch_information_(nullptr) {
863 predecessors_.reserve(kDefaultNumberOfPredecessors);
864 successors_.reserve(kDefaultNumberOfSuccessors);
865 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
866 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000867
Vladimir Marko60584552015-09-03 13:35:12 +0000868 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100869 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000870 }
871
Vladimir Marko60584552015-09-03 13:35:12 +0000872 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100873 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000874 }
875
David Brazdild26a4112015-11-10 11:07:31 +0000876 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
877 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
878
Vladimir Marko60584552015-09-03 13:35:12 +0000879 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
880 return ContainsElement(successors_, block, start_from);
881 }
882
883 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100884 return dominated_blocks_;
885 }
886
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100887 bool IsEntryBlock() const {
888 return graph_->GetEntryBlock() == this;
889 }
890
891 bool IsExitBlock() const {
892 return graph_->GetExitBlock() == this;
893 }
894
David Brazdil46e2a392015-03-16 17:31:52 +0000895 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000896 bool IsSingleTryBoundary() const;
897
898 // Returns true if this block emits nothing but a jump.
899 bool IsSingleJump() const {
900 HLoopInformation* loop_info = GetLoopInformation();
901 return (IsSingleGoto() || IsSingleTryBoundary())
902 // Back edges generate a suspend check.
903 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
904 }
David Brazdil46e2a392015-03-16 17:31:52 +0000905
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000906 void AddBackEdge(HBasicBlock* back_edge) {
907 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000908 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000909 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100910 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000911 loop_information_->AddBackEdge(back_edge);
912 }
913
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000914 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000915 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000916
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100917 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000918 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600919 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000920
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000921 HBasicBlock* GetDominator() const { return dominator_; }
922 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000923 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
924
925 void RemoveDominatedBlock(HBasicBlock* block) {
926 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100927 }
Vladimir Marko60584552015-09-03 13:35:12 +0000928
929 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
930 ReplaceElement(dominated_blocks_, existing, new_block);
931 }
932
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100933 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000934
935 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100936 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000937 }
938
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100939 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
940 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100941 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100942 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100943 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
944 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000945
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000946 HInstruction* GetFirstInstructionDisregardMoves() const;
947
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000948 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000949 successors_.push_back(block);
950 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000951 }
952
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100953 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
954 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100955 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000956 new_block->predecessors_.push_back(this);
957 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000958 }
959
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000960 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
961 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000962 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000963 new_block->successors_.push_back(this);
964 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000965 }
966
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100967 // Insert `this` between `predecessor` and `successor. This method
968 // preserves the indicies, and will update the first edge found between
969 // `predecessor` and `successor`.
970 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
971 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100972 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000973 successor->predecessors_[predecessor_index] = this;
974 predecessor->successors_[successor_index] = this;
975 successors_.push_back(successor);
976 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100977 }
978
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100979 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000980 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100981 }
982
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000983 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000984 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000985 }
986
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100987 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000988 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100989 }
990
991 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000992 predecessors_.push_back(block);
993 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100994 }
995
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100996 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000997 DCHECK_EQ(predecessors_.size(), 2u);
998 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100999 }
1000
David Brazdil769c9e52015-04-27 13:54:09 +01001001 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001002 DCHECK_EQ(successors_.size(), 2u);
1003 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001004 }
1005
David Brazdilfc6a86a2015-06-26 10:33:45 +00001006 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001007 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001008 }
1009
David Brazdilfc6a86a2015-06-26 10:33:45 +00001010 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001011 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001012 }
1013
David Brazdilfc6a86a2015-06-26 10:33:45 +00001014 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001015 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001016 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001017 }
1018
1019 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001020 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001021 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001022 }
1023
1024 // Returns whether the first occurrence of `predecessor` in the list of
1025 // predecessors is at index `idx`.
1026 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001027 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001028 return GetPredecessorIndexOf(predecessor) == idx;
1029 }
1030
David Brazdild7558da2015-09-22 13:04:14 +01001031 // Create a new block between this block and its predecessors. The new block
1032 // is added to the graph, all predecessor edges are relinked to it and an edge
1033 // is created to `this`. Returns the new empty block. Reverse post order or
1034 // loop and try/catch information are not updated.
1035 HBasicBlock* CreateImmediateDominator();
1036
David Brazdilfc6a86a2015-06-26 10:33:45 +00001037 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001038 // created, latter block. Note that this method will add the block to the
1039 // graph, create a Goto at the end of the former block and will create an edge
1040 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001041 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001042 HBasicBlock* SplitBefore(HInstruction* cursor);
1043
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001044 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001045 // created block. Note that this method just updates raw block information,
1046 // like predecessors, successors, dominators, and instruction list. It does not
1047 // update the graph, reverse post order, loop information, nor make sure the
1048 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001049 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1050
1051 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1052 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001053
1054 // Merge `other` at the end of `this`. Successors and dominated blocks of
1055 // `other` are changed to be successors and dominated blocks of `this`. Note
1056 // that this method does not update the graph, reverse post order, loop
1057 // information, nor make sure the blocks are consistent (for example ending
1058 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001059 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001060
1061 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1062 // of `this` are moved to `other`.
1063 // Note that this method does not update the graph, reverse post order, loop
1064 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001065 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001066 void ReplaceWith(HBasicBlock* other);
1067
David Brazdil2d7352b2015-04-20 14:52:42 +01001068 // Merge `other` at the end of `this`. This method updates loops, reverse post
1069 // order, links to predecessors, successors, dominators and deletes the block
1070 // from the graph. The two blocks must be successive, i.e. `this` the only
1071 // predecessor of `other` and vice versa.
1072 void MergeWith(HBasicBlock* other);
1073
1074 // Disconnects `this` from all its predecessors, successors and dominator,
1075 // removes it from all loops it is included in and eventually from the graph.
1076 // The block must not dominate any other block. Predecessors and successors
1077 // are safely updated.
1078 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001079
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001080 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001081 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001082 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001083 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001084 // Replace instruction `initial` with `replacement` within this block.
1085 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1086 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001087 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001088 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001089 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1090 // instruction list. With 'ensure_safety' set to true, it verifies that the
1091 // instruction is not in use and removes it from the use lists of its inputs.
1092 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1093 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001094 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001095
1096 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001097 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001098 }
1099
Roland Levillain6b879dd2014-09-22 17:13:44 +01001100 bool IsLoopPreHeaderFirstPredecessor() const {
1101 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001102 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001103 }
1104
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001105 bool IsFirstPredecessorBackEdge() const {
1106 DCHECK(IsLoopHeader());
1107 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1108 }
1109
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001110 HLoopInformation* GetLoopInformation() const {
1111 return loop_information_;
1112 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001113
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001114 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001115 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001116 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001117 void SetInLoop(HLoopInformation* info) {
1118 if (IsLoopHeader()) {
1119 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001120 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001121 loop_information_ = info;
1122 } else if (loop_information_->Contains(*info->GetHeader())) {
1123 // Block is currently part of an outer loop. Make it part of this inner loop.
1124 // Note that a non loop header having a loop information means this loop information
1125 // has already been populated
1126 loop_information_ = info;
1127 } else {
1128 // Block is part of an inner loop. Do not update the loop information.
1129 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1130 // at this point, because this method is being called while populating `info`.
1131 }
1132 }
1133
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001134 // Raw update of the loop information.
1135 void SetLoopInformation(HLoopInformation* info) {
1136 loop_information_ = info;
1137 }
1138
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001139 bool IsInLoop() const { return loop_information_ != nullptr; }
1140
David Brazdilec16f792015-08-19 15:04:01 +01001141 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1142
1143 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1144 try_catch_information_ = try_catch_information;
1145 }
1146
1147 bool IsTryBlock() const {
1148 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1149 }
1150
1151 bool IsCatchBlock() const {
1152 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1153 }
David Brazdilffee3d32015-07-06 11:48:53 +01001154
1155 // Returns the try entry that this block's successors should have. They will
1156 // be in the same try, unless the block ends in a try boundary. In that case,
1157 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001158 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001159
David Brazdild7558da2015-09-22 13:04:14 +01001160 bool HasThrowingInstructions() const;
1161
David Brazdila4b8c212015-05-07 09:59:30 +01001162 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001163 bool Dominates(HBasicBlock* block) const;
1164
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001165 size_t GetLifetimeStart() const { return lifetime_start_; }
1166 size_t GetLifetimeEnd() const { return lifetime_end_; }
1167
1168 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1169 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1170
David Brazdil8d5b8b22015-03-24 10:51:52 +00001171 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001172 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001173 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001174 bool HasSinglePhi() const;
1175
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001176 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001177 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001178 ArenaVector<HBasicBlock*> predecessors_;
1179 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001180 HInstructionList instructions_;
1181 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001182 HLoopInformation* loop_information_;
1183 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001184 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001185 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001186 // The dex program counter of the first instruction of this block.
1187 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001188 size_t lifetime_start_;
1189 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001190 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001191
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001192 friend class HGraph;
1193 friend class HInstruction;
1194
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001195 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1196};
1197
David Brazdilb2bd1c52015-03-25 11:17:37 +00001198// Iterates over the LoopInformation of all loops which contain 'block'
1199// from the innermost to the outermost.
1200class HLoopInformationOutwardIterator : public ValueObject {
1201 public:
1202 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1203 : current_(block.GetLoopInformation()) {}
1204
1205 bool Done() const { return current_ == nullptr; }
1206
1207 void Advance() {
1208 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001209 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001210 }
1211
1212 HLoopInformation* Current() const {
1213 DCHECK(!Done());
1214 return current_;
1215 }
1216
1217 private:
1218 HLoopInformation* current_;
1219
1220 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1221};
1222
Alexandre Ramesef20f712015-06-09 10:29:30 +01001223#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001224 M(Above, Condition) \
1225 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001226 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001227 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001228 M(ArrayGet, Instruction) \
1229 M(ArrayLength, Instruction) \
1230 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001231 M(Below, Condition) \
1232 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001233 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001234 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001235 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001236 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001237 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001238 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001239 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001240 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001241 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001242 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001243 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001244 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001245 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001246 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001247 M(Exit, Instruction) \
1248 M(FloatConstant, Constant) \
1249 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001250 M(GreaterThan, Condition) \
1251 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001252 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001253 M(InstanceFieldGet, Instruction) \
1254 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001255 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001256 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001257 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001258 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001259 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001260 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001261 M(LessThan, Condition) \
1262 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001263 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001264 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001265 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001266 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001267 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001268 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001269 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001270 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001271 M(Neg, UnaryOperation) \
1272 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001273 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001274 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001275 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001276 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001277 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001278 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001279 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001280 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001281 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001282 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001283 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001284 M(Return, Instruction) \
1285 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001286 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001287 M(Shl, BinaryOperation) \
1288 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001289 M(StaticFieldGet, Instruction) \
1290 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001291 M(UnresolvedInstanceFieldGet, Instruction) \
1292 M(UnresolvedInstanceFieldSet, Instruction) \
1293 M(UnresolvedStaticFieldGet, Instruction) \
1294 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001295 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001296 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001297 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001298 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001299 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001300 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001301 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001302 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001303
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001304/*
1305 * Instructions, shared across several (not all) architectures.
1306 */
1307#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1308#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1309#else
1310#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001311 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001312 M(MultiplyAccumulate, Instruction) \
1313 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001314#endif
1315
Vladimir Markob4536b72015-11-24 13:45:23 +00001316#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001317#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001318#else
1319#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1320 M(ArmDexCacheArraysBase, Instruction)
1321#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001322
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001323#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001324#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001325#else
1326#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001327 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001328#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001329
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001330#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001331#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001332#else
1333#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1334 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001335 M(MipsDexCacheArraysBase, Instruction) \
1336 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001337#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001338
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001339#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1340
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001341#ifndef ART_ENABLE_CODEGEN_x86
1342#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1343#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001344#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1345 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001346 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001347 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001348 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001349#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001350
1351#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1352
1353#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1354 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001355 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001356 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1357 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001358 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001359 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001360 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1361 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1362
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001363#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1364 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001365 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001366 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001367 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001368 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001369
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001370#define FOR_EACH_INSTRUCTION(M) \
1371 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1372 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1373
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001374#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001375FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1376#undef FORWARD_DECLARATION
1377
Vladimir Marko372f10e2016-05-17 16:30:10 +01001378#define DECLARE_INSTRUCTION(type) \
1379 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1380 const char* DebugName() const OVERRIDE { return #type; } \
1381 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1382 return other->Is##type(); \
1383 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001384 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001385
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001386#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1387 bool Is##type() const { return As##type() != nullptr; } \
1388 const H##type* As##type() const { return this; } \
1389 H##type* As##type() { return this; }
1390
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001391template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001392class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001393 public:
David Brazdiled596192015-01-23 10:39:45 +00001394 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001395 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001396 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001397
Vladimir Marko46817b82016-03-29 12:21:58 +01001398 // Hook for the IntrusiveForwardList<>.
1399 // TODO: Hide this better.
1400 IntrusiveForwardListHook hook;
1401
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001402 private:
David Brazdiled596192015-01-23 10:39:45 +00001403 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001404 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001405
1406 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001407 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001408
Vladimir Marko46817b82016-03-29 12:21:58 +01001409 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001410
1411 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1412};
1413
David Brazdiled596192015-01-23 10:39:45 +00001414template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001415using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001416
David Brazdil1abb4192015-02-17 18:33:36 +00001417// This class is used by HEnvironment and HInstruction classes to record the
1418// instructions they use and pointers to the corresponding HUseListNodes kept
1419// by the used instructions.
1420template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001421class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001422 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001423 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1424 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001425
Vladimir Marko46817b82016-03-29 12:21:58 +01001426 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1427 : HUserRecord(old_record.instruction_, before_use_node) {}
1428 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1429 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001430 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001431 }
1432
1433 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001434 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1435 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001436
1437 private:
1438 // Instruction used by the user.
1439 HInstruction* instruction_;
1440
Vladimir Marko46817b82016-03-29 12:21:58 +01001441 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1442 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001443};
1444
Vladimir Markoe9004912016-06-16 16:50:52 +01001445// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1446// This is used for HInstruction::GetInputs() to return a container wrapper providing
1447// HInstruction* values even though the underlying container has HUserRecord<>s.
1448struct HInputExtractor {
1449 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1450 return record.GetInstruction();
1451 }
1452 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1453 return record.GetInstruction();
1454 }
1455};
1456
1457using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1458using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1459
Aart Bik854a02b2015-07-14 16:07:00 -07001460/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001461 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001462 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001463 * For write/read dependences on fields/arrays, the dependence analysis uses
1464 * type disambiguation (e.g. a float field write cannot modify the value of an
1465 * integer field read) and the access type (e.g. a reference array write cannot
1466 * modify the value of a reference field read [although it may modify the
1467 * reference fetch prior to reading the field, which is represented by its own
1468 * write/read dependence]). The analysis makes conservative points-to
1469 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1470 * the same, and any reference read depends on any reference read without
1471 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001472 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001473 * The internal representation uses 38-bit and is described in the table below.
1474 * The first line indicates the side effect, and for field/array accesses the
1475 * second line indicates the type of the access (in the order of the
1476 * Primitive::Type enum).
1477 * The two numbered lines below indicate the bit position in the bitfield (read
1478 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001479 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001480 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1481 * +-------------+---------+---------+--------------+---------+---------+
1482 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1483 * | 3 |333333322|222222221| 1 |111111110|000000000|
1484 * | 7 |654321098|765432109| 8 |765432109|876543210|
1485 *
1486 * Note that, to ease the implementation, 'changes' bits are least significant
1487 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001488 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001489class SideEffects : public ValueObject {
1490 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001491 SideEffects() : flags_(0) {}
1492
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001493 static SideEffects None() {
1494 return SideEffects(0);
1495 }
1496
1497 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001498 return SideEffects(kAllChangeBits | kAllDependOnBits);
1499 }
1500
1501 static SideEffects AllChanges() {
1502 return SideEffects(kAllChangeBits);
1503 }
1504
1505 static SideEffects AllDependencies() {
1506 return SideEffects(kAllDependOnBits);
1507 }
1508
1509 static SideEffects AllExceptGCDependency() {
1510 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1511 }
1512
1513 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001514 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001515 }
1516
Aart Bik34c3ba92015-07-20 14:08:59 -07001517 static SideEffects AllWrites() {
1518 return SideEffects(kAllWrites);
1519 }
1520
1521 static SideEffects AllReads() {
1522 return SideEffects(kAllReads);
1523 }
1524
1525 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1526 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001527 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001528 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001529 }
1530
Aart Bik854a02b2015-07-14 16:07:00 -07001531 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001532 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001533 }
1534
Aart Bik34c3ba92015-07-20 14:08:59 -07001535 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1536 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001537 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001538 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001539 }
1540
1541 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001542 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001543 }
1544
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001545 static SideEffects CanTriggerGC() {
1546 return SideEffects(1ULL << kCanTriggerGCBit);
1547 }
1548
1549 static SideEffects DependsOnGC() {
1550 return SideEffects(1ULL << kDependsOnGCBit);
1551 }
1552
Aart Bik854a02b2015-07-14 16:07:00 -07001553 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001554 SideEffects Union(SideEffects other) const {
1555 return SideEffects(flags_ | other.flags_);
1556 }
1557
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001558 SideEffects Exclusion(SideEffects other) const {
1559 return SideEffects(flags_ & ~other.flags_);
1560 }
1561
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001562 void Add(SideEffects other) {
1563 flags_ |= other.flags_;
1564 }
1565
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001566 bool Includes(SideEffects other) const {
1567 return (other.flags_ & flags_) == other.flags_;
1568 }
1569
1570 bool HasSideEffects() const {
1571 return (flags_ & kAllChangeBits);
1572 }
1573
1574 bool HasDependencies() const {
1575 return (flags_ & kAllDependOnBits);
1576 }
1577
1578 // Returns true if there are no side effects or dependencies.
1579 bool DoesNothing() const {
1580 return flags_ == 0;
1581 }
1582
Aart Bik854a02b2015-07-14 16:07:00 -07001583 // Returns true if something is written.
1584 bool DoesAnyWrite() const {
1585 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001586 }
1587
Aart Bik854a02b2015-07-14 16:07:00 -07001588 // Returns true if something is read.
1589 bool DoesAnyRead() const {
1590 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001591 }
1592
Aart Bik854a02b2015-07-14 16:07:00 -07001593 // Returns true if potentially everything is written and read
1594 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001595 bool DoesAllReadWrite() const {
1596 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1597 }
1598
Aart Bik854a02b2015-07-14 16:07:00 -07001599 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001600 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001601 }
1602
Roland Levillain0d5a2812015-11-13 10:07:31 +00001603 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001604 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001605 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1606 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001607 }
1608
1609 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001610 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001611 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001612 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001613 for (int s = kLastBit; s >= 0; s--) {
1614 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1615 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1616 // This is a bit for the GC side effect.
1617 if (current_bit_is_set) {
1618 flags += "GC";
1619 }
Aart Bik854a02b2015-07-14 16:07:00 -07001620 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001621 } else {
1622 // This is a bit for the array/field analysis.
1623 // The underscore character stands for the 'can trigger GC' bit.
1624 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1625 if (current_bit_is_set) {
1626 flags += kDebug[s];
1627 }
1628 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1629 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1630 flags += "|";
1631 }
1632 }
Aart Bik854a02b2015-07-14 16:07:00 -07001633 }
1634 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001635 }
1636
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001637 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001638
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001639 private:
1640 static constexpr int kFieldArrayAnalysisBits = 9;
1641
1642 static constexpr int kFieldWriteOffset = 0;
1643 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1644 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1645 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1646
1647 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1648
1649 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1650 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1651 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1652 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1653
1654 static constexpr int kLastBit = kDependsOnGCBit;
1655 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1656
1657 // Aliases.
1658
1659 static_assert(kChangeBits == kDependOnBits,
1660 "the 'change' bits should match the 'depend on' bits.");
1661
1662 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1663 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1664 static constexpr uint64_t kAllWrites =
1665 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1666 static constexpr uint64_t kAllReads =
1667 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001668
Aart Bik854a02b2015-07-14 16:07:00 -07001669 // Translates type to bit flag.
1670 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1671 CHECK_NE(type, Primitive::kPrimVoid);
1672 const uint64_t one = 1;
1673 const int shift = type; // 0-based consecutive enum
1674 DCHECK_LE(kFieldWriteOffset, shift);
1675 DCHECK_LT(shift, kArrayWriteOffset);
1676 return one << (type + offset);
1677 }
1678
1679 // Private constructor on direct flags value.
1680 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1681
1682 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001683};
1684
David Brazdiled596192015-01-23 10:39:45 +00001685// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001686class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001687 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001688 HEnvironment(ArenaAllocator* arena,
1689 size_t number_of_vregs,
1690 const DexFile& dex_file,
1691 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001692 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001693 InvokeType invoke_type,
1694 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001695 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1696 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001697 parent_(nullptr),
1698 dex_file_(dex_file),
1699 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001700 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001701 invoke_type_(invoke_type),
1702 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001703 }
1704
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001705 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001706 : HEnvironment(arena,
1707 to_copy.Size(),
1708 to_copy.GetDexFile(),
1709 to_copy.GetMethodIdx(),
1710 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001711 to_copy.GetInvokeType(),
1712 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001713
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001714 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001715 if (parent_ != nullptr) {
1716 parent_->SetAndCopyParentChain(allocator, parent);
1717 } else {
1718 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1719 parent_->CopyFrom(parent);
1720 if (parent->GetParent() != nullptr) {
1721 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1722 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001723 }
David Brazdiled596192015-01-23 10:39:45 +00001724 }
1725
Vladimir Marko71bf8092015-09-15 15:33:14 +01001726 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001727 void CopyFrom(HEnvironment* environment);
1728
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001729 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1730 // input to the loop phi instead. This is for inserting instructions that
1731 // require an environment (like HDeoptimization) in the loop pre-header.
1732 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001733
1734 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001735 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001736 }
1737
1738 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001739 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001740 }
1741
David Brazdil1abb4192015-02-17 18:33:36 +00001742 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001743
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001744 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001745
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001746 HEnvironment* GetParent() const { return parent_; }
1747
1748 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001749 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001750 }
1751
1752 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001753 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001754 }
1755
1756 uint32_t GetDexPc() const {
1757 return dex_pc_;
1758 }
1759
1760 uint32_t GetMethodIdx() const {
1761 return method_idx_;
1762 }
1763
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001764 InvokeType GetInvokeType() const {
1765 return invoke_type_;
1766 }
1767
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001768 const DexFile& GetDexFile() const {
1769 return dex_file_;
1770 }
1771
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001772 HInstruction* GetHolder() const {
1773 return holder_;
1774 }
1775
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001776
1777 bool IsFromInlinedInvoke() const {
1778 return GetParent() != nullptr;
1779 }
1780
David Brazdiled596192015-01-23 10:39:45 +00001781 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001782 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1783 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001784 HEnvironment* parent_;
1785 const DexFile& dex_file_;
1786 const uint32_t method_idx_;
1787 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001788 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001789
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001790 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001791 HInstruction* const holder_;
1792
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001793 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001794
1795 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1796};
1797
Vladimir Markof9f64412015-09-02 14:05:49 +01001798class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001799 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001800 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001801 : previous_(nullptr),
1802 next_(nullptr),
1803 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001804 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001805 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001806 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001807 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001808 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001809 locations_(nullptr),
1810 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001811 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001812 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001813 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1814 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1815 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001816
Dave Allison20dfc792014-06-16 20:44:29 -07001817 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001818
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001819#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001820 enum InstructionKind {
1821 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1822 };
1823#undef DECLARE_KIND
1824
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001825 HInstruction* GetNext() const { return next_; }
1826 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001827
Calin Juravle77520bc2015-01-12 18:45:46 +00001828 HInstruction* GetNextDisregardingMoves() const;
1829 HInstruction* GetPreviousDisregardingMoves() const;
1830
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001831 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001832 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001833 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001834 bool IsInBlock() const { return block_ != nullptr; }
1835 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001836 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1837 bool IsIrreducibleLoopHeaderPhi() const {
1838 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1839 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001840
Vladimir Marko372f10e2016-05-17 16:30:10 +01001841 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1842
1843 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1844 // One virtual method is enough, just const_cast<> and then re-add the const.
1845 return ArrayRef<const HUserRecord<HInstruction*>>(
1846 const_cast<HInstruction*>(this)->GetInputRecords());
1847 }
1848
Vladimir Markoe9004912016-06-16 16:50:52 +01001849 HInputsRef GetInputs() {
1850 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001851 }
1852
Vladimir Markoe9004912016-06-16 16:50:52 +01001853 HConstInputsRef GetInputs() const {
1854 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001855 }
1856
1857 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001858 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001859
Aart Bik2767f4b2016-10-28 15:03:53 -07001860 bool HasInput(HInstruction* input) const {
1861 for (const HInstruction* i : GetInputs()) {
1862 if (i == input) {
1863 return true;
1864 }
1865 }
1866 return false;
1867 }
1868
Vladimir Marko372f10e2016-05-17 16:30:10 +01001869 void SetRawInputAt(size_t index, HInstruction* input) {
1870 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1871 }
1872
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001873 virtual void Accept(HGraphVisitor* visitor) = 0;
1874 virtual const char* DebugName() const = 0;
1875
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001876 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1877
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001878 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001879
1880 uint32_t GetDexPc() const { return dex_pc_; }
1881
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001882 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001883
Roland Levillaine161a2a2014-10-03 12:45:18 +01001884 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001885 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001886
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001887 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001888 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001889
Calin Juravle10e244f2015-01-26 18:54:32 +00001890 // Does not apply for all instructions, but having this at top level greatly
1891 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001892 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001893 virtual bool CanBeNull() const {
1894 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1895 return true;
1896 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001897
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001898 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001899 return false;
1900 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001901
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001902 virtual bool IsActualObject() const {
1903 return GetType() == Primitive::kPrimNot;
1904 }
1905
Calin Juravle2e768302015-07-28 14:41:11 +00001906 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001907
Calin Juravle61d544b2015-02-23 16:46:57 +00001908 ReferenceTypeInfo GetReferenceTypeInfo() const {
1909 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001910 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001911 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001912 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001913
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001914 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001915 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001916 // Note: fixup_end remains valid across push_front().
1917 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1918 HUseListNode<HInstruction*>* new_node =
1919 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1920 uses_.push_front(*new_node);
1921 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001922 }
1923
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001924 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001925 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001926 // Note: env_fixup_end remains valid across push_front().
1927 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1928 HUseListNode<HEnvironment*>* new_node =
1929 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1930 env_uses_.push_front(*new_node);
1931 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001932 }
1933
David Brazdil1abb4192015-02-17 18:33:36 +00001934 void RemoveAsUserOfInput(size_t input) {
1935 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001936 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1937 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1938 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001939 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001940
Vladimir Marko372f10e2016-05-17 16:30:10 +01001941 void RemoveAsUserOfAllInputs() {
1942 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1943 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1944 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1945 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1946 }
1947 }
1948
David Brazdil1abb4192015-02-17 18:33:36 +00001949 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1950 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001951
Vladimir Marko46817b82016-03-29 12:21:58 +01001952 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1953 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1954 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001955 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001956 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001957 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001958
Aart Bikcc42be02016-10-20 16:14:16 -07001959 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07001960 return
1961 !HasSideEffects() &&
1962 !CanThrow() &&
1963 !IsSuspendCheck() &&
1964 !IsControlFlow() &&
1965 !IsNativeDebugInfo() &&
1966 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07001967 // If we added an explicit barrier then we should keep it.
1968 !IsMemoryBarrier();
1969 }
1970
Aart Bikcc42be02016-10-20 16:14:16 -07001971 bool IsDeadAndRemovable() const {
1972 return IsRemovable() && !HasUses();
1973 }
1974
Roland Levillain6c82d402014-10-13 16:10:27 +01001975 // Does this instruction strictly dominate `other_instruction`?
1976 // Returns false if this instruction and `other_instruction` are the same.
1977 // Aborts if this instruction and `other_instruction` are both phis.
1978 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001979
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001980 int GetId() const { return id_; }
1981 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001982
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001983 int GetSsaIndex() const { return ssa_index_; }
1984 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1985 bool HasSsaIndex() const { return ssa_index_ != -1; }
1986
1987 bool HasEnvironment() const { return environment_ != nullptr; }
1988 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001989 // Set the `environment_` field. Raw because this method does not
1990 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001991 void SetRawEnvironment(HEnvironment* environment) {
1992 DCHECK(environment_ == nullptr);
1993 DCHECK_EQ(environment->GetHolder(), this);
1994 environment_ = environment;
1995 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001996
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001997 void InsertRawEnvironment(HEnvironment* environment) {
1998 DCHECK(environment_ != nullptr);
1999 DCHECK_EQ(environment->GetHolder(), this);
2000 DCHECK(environment->GetParent() == nullptr);
2001 environment->parent_ = environment_;
2002 environment_ = environment;
2003 }
2004
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002005 void RemoveEnvironment();
2006
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002007 // Set the environment of this instruction, copying it from `environment`. While
2008 // copying, the uses lists are being updated.
2009 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002010 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002011 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002012 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002013 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002014 if (environment->GetParent() != nullptr) {
2015 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2016 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002017 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002018
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002019 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2020 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002021 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002022 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002023 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002024 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002025 if (environment->GetParent() != nullptr) {
2026 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2027 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002028 }
2029
Nicolas Geoffray39468442014-09-02 15:17:15 +01002030 // Returns the number of entries in the environment. Typically, that is the
2031 // number of dex registers in a method. It could be more in case of inlining.
2032 size_t EnvironmentSize() const;
2033
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002034 LocationSummary* GetLocations() const { return locations_; }
2035 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002036
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002037 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002038 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002039
Alexandre Rames188d4312015-04-09 18:30:21 +01002040 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2041 // uses of this instruction by `other` are *not* updated.
2042 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2043 ReplaceWith(other);
2044 other->ReplaceInput(this, use_index);
2045 }
2046
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002047 // Move `this` instruction before `cursor`.
2048 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002049
Vladimir Markofb337ea2015-11-25 15:25:10 +00002050 // Move `this` before its first user and out of any loops. If there is no
2051 // out-of-loop user that dominates all other users, move the instruction
2052 // to the end of the out-of-loop common dominator of the user's blocks.
2053 //
2054 // This can be used only on non-throwing instructions with no side effects that
2055 // have at least one use but no environment uses.
2056 void MoveBeforeFirstUserAndOutOfLoops();
2057
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002058#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002059 bool Is##type() const; \
2060 const H##type* As##type() const; \
2061 H##type* As##type();
2062
2063 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2064#undef INSTRUCTION_TYPE_CHECK
2065
2066#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002067 bool Is##type() const { return (As##type() != nullptr); } \
2068 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002069 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002070 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002071#undef INSTRUCTION_TYPE_CHECK
2072
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002073 // Returns whether the instruction can be moved within the graph.
2074 virtual bool CanBeMoved() const { return false; }
2075
2076 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002077 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002078 return false;
2079 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002080
2081 // Returns whether any data encoded in the two instructions is equal.
2082 // This method does not look at the inputs. Both instructions must be
2083 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002084 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002085 return false;
2086 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002087
2088 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002089 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002090 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002091 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002092
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002093 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2094 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2095 // the virtual function because the __attribute__((__pure__)) doesn't really
2096 // apply the strong requirement for virtual functions, preventing optimizations.
2097 InstructionKind GetKind() const PURE;
2098 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002099
2100 virtual size_t ComputeHashCode() const {
2101 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002102 for (const HInstruction* input : GetInputs()) {
2103 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002104 }
2105 return result;
2106 }
2107
2108 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002109 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002110 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002111
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002112 size_t GetLifetimePosition() const { return lifetime_position_; }
2113 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2114 LiveInterval* GetLiveInterval() const { return live_interval_; }
2115 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2116 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2117
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002118 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2119
2120 // Returns whether the code generation of the instruction will require to have access
2121 // to the current method. Such instructions are:
2122 // (1): Instructions that require an environment, as calling the runtime requires
2123 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002124 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2125 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002126 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002127 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002128 }
2129
Vladimir Markodc151b22015-10-15 18:02:30 +01002130 // Returns whether the code generation of the instruction will require to have access
2131 // to the dex cache of the current method's declaring class via the current method.
2132 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002133
Mark Mendellc4701932015-04-10 13:18:51 -04002134 // Does this instruction have any use in an environment before
2135 // control flow hits 'other'?
2136 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2137
2138 // Remove all references to environment uses of this instruction.
2139 // The caller must ensure that this is safe to do.
2140 void RemoveEnvironmentUsers();
2141
Vladimir Markoa1de9182016-02-25 11:37:38 +00002142 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2143 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002144
David Brazdil1abb4192015-02-17 18:33:36 +00002145 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002146 // If set, the machine code for this instruction is assumed to be generated by
2147 // its users. Used by liveness analysis to compute use positions accordingly.
2148 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2149 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2150 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2151 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2152
Vladimir Marko372f10e2016-05-17 16:30:10 +01002153 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2154 return GetInputRecords()[i];
2155 }
2156
2157 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2158 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2159 input_records[index] = input;
2160 }
David Brazdil1abb4192015-02-17 18:33:36 +00002161
Vladimir Markoa1de9182016-02-25 11:37:38 +00002162 uint32_t GetPackedFields() const {
2163 return packed_fields_;
2164 }
2165
2166 template <size_t flag>
2167 bool GetPackedFlag() const {
2168 return (packed_fields_ & (1u << flag)) != 0u;
2169 }
2170
2171 template <size_t flag>
2172 void SetPackedFlag(bool value = true) {
2173 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2174 }
2175
2176 template <typename BitFieldType>
2177 typename BitFieldType::value_type GetPackedField() const {
2178 return BitFieldType::Decode(packed_fields_);
2179 }
2180
2181 template <typename BitFieldType>
2182 void SetPackedField(typename BitFieldType::value_type value) {
2183 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2184 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2185 }
2186
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002187 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002188 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2189 auto before_use_node = uses_.before_begin();
2190 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2191 HInstruction* user = use_node->GetUser();
2192 size_t input_index = use_node->GetIndex();
2193 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2194 before_use_node = use_node;
2195 }
2196 }
2197
2198 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2199 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2200 if (next != uses_.end()) {
2201 HInstruction* next_user = next->GetUser();
2202 size_t next_index = next->GetIndex();
2203 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2204 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2205 }
2206 }
2207
2208 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2209 auto before_env_use_node = env_uses_.before_begin();
2210 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2211 HEnvironment* user = env_use_node->GetUser();
2212 size_t input_index = env_use_node->GetIndex();
2213 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2214 before_env_use_node = env_use_node;
2215 }
2216 }
2217
2218 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2219 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2220 if (next != env_uses_.end()) {
2221 HEnvironment* next_user = next->GetUser();
2222 size_t next_index = next->GetIndex();
2223 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2224 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2225 }
2226 }
David Brazdil1abb4192015-02-17 18:33:36 +00002227
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002228 HInstruction* previous_;
2229 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002230 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002231 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002232
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002233 // An instruction gets an id when it is added to the graph.
2234 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002235 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002236 int id_;
2237
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002238 // When doing liveness analysis, instructions that have uses get an SSA index.
2239 int ssa_index_;
2240
Vladimir Markoa1de9182016-02-25 11:37:38 +00002241 // Packed fields.
2242 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002243
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002244 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002245 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002246
2247 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002248 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002249
Nicolas Geoffray39468442014-09-02 15:17:15 +01002250 // The environment associated with this instruction. Not null if the instruction
2251 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002252 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002253
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002254 // Set by the code generator.
2255 LocationSummary* locations_;
2256
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002257 // Set by the liveness analysis.
2258 LiveInterval* live_interval_;
2259
2260 // Set by the liveness analysis, this is the position in a linear
2261 // order of blocks where this instruction's live interval start.
2262 size_t lifetime_position_;
2263
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002264 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002265
Vladimir Markoa1de9182016-02-25 11:37:38 +00002266 // The reference handle part of the reference type info.
2267 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002268 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002269 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002270
David Brazdil1abb4192015-02-17 18:33:36 +00002271 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002272 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002273 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002274 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002275 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002276
2277 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2278};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002279std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002280
2281class HInstructionIterator : public ValueObject {
2282 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002283 explicit HInstructionIterator(const HInstructionList& instructions)
2284 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002285 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002286 }
2287
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002288 bool Done() const { return instruction_ == nullptr; }
2289 HInstruction* Current() const { return instruction_; }
2290 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002291 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002292 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002293 }
2294
2295 private:
2296 HInstruction* instruction_;
2297 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002298
2299 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002300};
2301
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002302class HBackwardInstructionIterator : public ValueObject {
2303 public:
2304 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2305 : instruction_(instructions.last_instruction_) {
2306 next_ = Done() ? nullptr : instruction_->GetPrevious();
2307 }
2308
2309 bool Done() const { return instruction_ == nullptr; }
2310 HInstruction* Current() const { return instruction_; }
2311 void Advance() {
2312 instruction_ = next_;
2313 next_ = Done() ? nullptr : instruction_->GetPrevious();
2314 }
2315
2316 private:
2317 HInstruction* instruction_;
2318 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002319
2320 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002321};
2322
Vladimir Markof9f64412015-09-02 14:05:49 +01002323template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002324class HTemplateInstruction: public HInstruction {
2325 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002326 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002327 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002328 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002329
Vladimir Marko372f10e2016-05-17 16:30:10 +01002330 using HInstruction::GetInputRecords; // Keep the const version visible.
2331 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2332 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002333 }
2334
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002335 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002336 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002337
2338 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002339};
2340
Vladimir Markof9f64412015-09-02 14:05:49 +01002341// HTemplateInstruction specialization for N=0.
2342template<>
2343class HTemplateInstruction<0>: public HInstruction {
2344 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002345 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002346 : HInstruction(side_effects, dex_pc) {}
2347
Vladimir Markof9f64412015-09-02 14:05:49 +01002348 virtual ~HTemplateInstruction() {}
2349
Vladimir Marko372f10e2016-05-17 16:30:10 +01002350 using HInstruction::GetInputRecords; // Keep the const version visible.
2351 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2352 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002353 }
2354
2355 private:
2356 friend class SsaBuilder;
2357};
2358
Dave Allison20dfc792014-06-16 20:44:29 -07002359template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002360class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002361 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002362 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002363 : HTemplateInstruction<N>(side_effects, dex_pc) {
2364 this->template SetPackedField<TypeField>(type);
2365 }
Dave Allison20dfc792014-06-16 20:44:29 -07002366 virtual ~HExpression() {}
2367
Vladimir Markoa1de9182016-02-25 11:37:38 +00002368 Primitive::Type GetType() const OVERRIDE {
2369 return TypeField::Decode(this->GetPackedFields());
2370 }
Dave Allison20dfc792014-06-16 20:44:29 -07002371
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002372 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002373 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2374 static constexpr size_t kFieldTypeSize =
2375 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2376 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2377 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2378 "Too many packed fields.");
2379 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002380};
2381
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002382// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2383// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002384class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002385 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002386 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2387 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002388
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002389 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002390
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002391 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002392
2393 private:
2394 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2395};
2396
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002397// Represents dex's RETURN opcodes. A HReturn is a control flow
2398// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002399class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002400 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002401 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2402 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002403 SetRawInputAt(0, value);
2404 }
2405
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002406 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002407
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002408 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002409
2410 private:
2411 DISALLOW_COPY_AND_ASSIGN(HReturn);
2412};
2413
Vladimir Markofcb503c2016-05-18 12:48:17 +01002414class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002415 public:
2416 HPhi(ArenaAllocator* arena,
2417 uint32_t reg_number,
2418 size_t number_of_inputs,
2419 Primitive::Type type,
2420 uint32_t dex_pc = kNoDexPc)
2421 : HInstruction(SideEffects::None(), dex_pc),
2422 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2423 reg_number_(reg_number) {
2424 SetPackedField<TypeField>(ToPhiType(type));
2425 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2426 // Phis are constructed live and marked dead if conflicting or unused.
2427 // Individual steps of SsaBuilder should assume that if a phi has been
2428 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2429 SetPackedFlag<kFlagIsLive>(true);
2430 SetPackedFlag<kFlagCanBeNull>(true);
2431 }
2432
2433 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2434 static Primitive::Type ToPhiType(Primitive::Type type) {
2435 return Primitive::PrimitiveKind(type);
2436 }
2437
2438 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2439
Vladimir Marko372f10e2016-05-17 16:30:10 +01002440 using HInstruction::GetInputRecords; // Keep the const version visible.
2441 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2442 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2443 }
David Brazdildee58d62016-04-07 09:54:26 +00002444
2445 void AddInput(HInstruction* input);
2446 void RemoveInputAt(size_t index);
2447
2448 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2449 void SetType(Primitive::Type new_type) {
2450 // Make sure that only valid type changes occur. The following are allowed:
2451 // (1) int -> float/ref (primitive type propagation),
2452 // (2) long -> double (primitive type propagation).
2453 DCHECK(GetType() == new_type ||
2454 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2455 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2456 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2457 SetPackedField<TypeField>(new_type);
2458 }
2459
2460 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2461 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2462
2463 uint32_t GetRegNumber() const { return reg_number_; }
2464
2465 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2466 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2467 bool IsDead() const { return !IsLive(); }
2468 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2469
Vladimir Markoe9004912016-06-16 16:50:52 +01002470 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002471 return other != nullptr
2472 && other->IsPhi()
2473 && other->AsPhi()->GetBlock() == GetBlock()
2474 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2475 }
2476
2477 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2478 // An equivalent phi is a phi having the same dex register and type.
2479 // It assumes that phis with the same dex register are adjacent.
2480 HPhi* GetNextEquivalentPhiWithSameType() {
2481 HInstruction* next = GetNext();
2482 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2483 if (next->GetType() == GetType()) {
2484 return next->AsPhi();
2485 }
2486 next = next->GetNext();
2487 }
2488 return nullptr;
2489 }
2490
2491 DECLARE_INSTRUCTION(Phi);
2492
David Brazdildee58d62016-04-07 09:54:26 +00002493 private:
2494 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2495 static constexpr size_t kFieldTypeSize =
2496 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2497 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2498 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2499 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2500 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2501 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2502
Vladimir Marko372f10e2016-05-17 16:30:10 +01002503 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002504 const uint32_t reg_number_;
2505
2506 DISALLOW_COPY_AND_ASSIGN(HPhi);
2507};
2508
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002509// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002510// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002511// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002512class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002513 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002514 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002515
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002516 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002517
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002518 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002519
2520 private:
2521 DISALLOW_COPY_AND_ASSIGN(HExit);
2522};
2523
2524// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002525class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002526 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002527 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002528
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002529 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002530
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002531 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002532 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002533 }
2534
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002535 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002536
2537 private:
2538 DISALLOW_COPY_AND_ASSIGN(HGoto);
2539};
2540
Roland Levillain9867bc72015-08-05 10:21:34 +01002541class HConstant : public HExpression<0> {
2542 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002543 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2544 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002545
2546 bool CanBeMoved() const OVERRIDE { return true; }
2547
Roland Levillain1a653882016-03-18 18:05:57 +00002548 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002549 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002550 // Is this constant 0 in the arithmetic sense?
2551 virtual bool IsArithmeticZero() const { return false; }
2552 // Is this constant a 0-bit pattern?
2553 virtual bool IsZeroBitPattern() const { return false; }
2554 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002555 virtual bool IsOne() const { return false; }
2556
David Brazdil77a48ae2015-09-15 12:34:04 +00002557 virtual uint64_t GetValueAsUint64() const = 0;
2558
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002559 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002560
2561 private:
2562 DISALLOW_COPY_AND_ASSIGN(HConstant);
2563};
2564
Vladimir Markofcb503c2016-05-18 12:48:17 +01002565class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002566 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002567 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002568 return true;
2569 }
2570
David Brazdil77a48ae2015-09-15 12:34:04 +00002571 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2572
Roland Levillain9867bc72015-08-05 10:21:34 +01002573 size_t ComputeHashCode() const OVERRIDE { return 0; }
2574
Roland Levillain1a653882016-03-18 18:05:57 +00002575 // The null constant representation is a 0-bit pattern.
2576 virtual bool IsZeroBitPattern() const { return true; }
2577
Roland Levillain9867bc72015-08-05 10:21:34 +01002578 DECLARE_INSTRUCTION(NullConstant);
2579
2580 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002581 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002582
2583 friend class HGraph;
2584 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2585};
2586
2587// Constants of the type int. Those can be from Dex instructions, or
2588// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002589class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002590 public:
2591 int32_t GetValue() const { return value_; }
2592
David Brazdil9f389d42015-10-01 14:32:56 +01002593 uint64_t GetValueAsUint64() const OVERRIDE {
2594 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2595 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002596
Vladimir Marko372f10e2016-05-17 16:30:10 +01002597 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002598 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002599 return other->AsIntConstant()->value_ == value_;
2600 }
2601
2602 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2603
2604 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002605 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2606 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002607 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2608
Roland Levillain1a653882016-03-18 18:05:57 +00002609 // Integer constants are used to encode Boolean values as well,
2610 // where 1 means true and 0 means false.
2611 bool IsTrue() const { return GetValue() == 1; }
2612 bool IsFalse() const { return GetValue() == 0; }
2613
Roland Levillain9867bc72015-08-05 10:21:34 +01002614 DECLARE_INSTRUCTION(IntConstant);
2615
2616 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002617 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2618 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2619 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2620 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002621
2622 const int32_t value_;
2623
2624 friend class HGraph;
2625 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2626 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2627 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2628};
2629
Vladimir Markofcb503c2016-05-18 12:48:17 +01002630class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002631 public:
2632 int64_t GetValue() const { return value_; }
2633
David Brazdil77a48ae2015-09-15 12:34:04 +00002634 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2635
Vladimir Marko372f10e2016-05-17 16:30:10 +01002636 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002637 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002638 return other->AsLongConstant()->value_ == value_;
2639 }
2640
2641 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2642
2643 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002644 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2645 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002646 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2647
2648 DECLARE_INSTRUCTION(LongConstant);
2649
2650 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002651 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2652 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002653
2654 const int64_t value_;
2655
2656 friend class HGraph;
2657 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2658};
Dave Allison20dfc792014-06-16 20:44:29 -07002659
Vladimir Markofcb503c2016-05-18 12:48:17 +01002660class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002661 public:
2662 float GetValue() const { return value_; }
2663
2664 uint64_t GetValueAsUint64() const OVERRIDE {
2665 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2666 }
2667
Vladimir Marko372f10e2016-05-17 16:30:10 +01002668 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002669 DCHECK(other->IsFloatConstant()) << other->DebugName();
2670 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2671 }
2672
2673 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2674
2675 bool IsMinusOne() const OVERRIDE {
2676 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2677 }
Roland Levillain1a653882016-03-18 18:05:57 +00002678 bool IsArithmeticZero() const OVERRIDE {
2679 return std::fpclassify(value_) == FP_ZERO;
2680 }
2681 bool IsArithmeticPositiveZero() const {
2682 return IsArithmeticZero() && !std::signbit(value_);
2683 }
2684 bool IsArithmeticNegativeZero() const {
2685 return IsArithmeticZero() && std::signbit(value_);
2686 }
2687 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002688 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002689 }
2690 bool IsOne() const OVERRIDE {
2691 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2692 }
2693 bool IsNaN() const {
2694 return std::isnan(value_);
2695 }
2696
2697 DECLARE_INSTRUCTION(FloatConstant);
2698
2699 private:
2700 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2701 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2702 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2703 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2704
2705 const float value_;
2706
2707 // Only the SsaBuilder and HGraph can create floating-point constants.
2708 friend class SsaBuilder;
2709 friend class HGraph;
2710 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2711};
2712
Vladimir Markofcb503c2016-05-18 12:48:17 +01002713class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002714 public:
2715 double GetValue() const { return value_; }
2716
2717 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2718
Vladimir Marko372f10e2016-05-17 16:30:10 +01002719 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002720 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2721 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2722 }
2723
2724 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2725
2726 bool IsMinusOne() const OVERRIDE {
2727 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2728 }
Roland Levillain1a653882016-03-18 18:05:57 +00002729 bool IsArithmeticZero() const OVERRIDE {
2730 return std::fpclassify(value_) == FP_ZERO;
2731 }
2732 bool IsArithmeticPositiveZero() const {
2733 return IsArithmeticZero() && !std::signbit(value_);
2734 }
2735 bool IsArithmeticNegativeZero() const {
2736 return IsArithmeticZero() && std::signbit(value_);
2737 }
2738 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002739 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002740 }
2741 bool IsOne() const OVERRIDE {
2742 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2743 }
2744 bool IsNaN() const {
2745 return std::isnan(value_);
2746 }
2747
2748 DECLARE_INSTRUCTION(DoubleConstant);
2749
2750 private:
2751 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2752 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2753 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2754 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2755
2756 const double value_;
2757
2758 // Only the SsaBuilder and HGraph can create floating-point constants.
2759 friend class SsaBuilder;
2760 friend class HGraph;
2761 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2762};
2763
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002764// Conditional branch. A block ending with an HIf instruction must have
2765// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002766class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002767 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002768 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2769 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002770 SetRawInputAt(0, input);
2771 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002772
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002773 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002774
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002775 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002776 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002777 }
2778
2779 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002780 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002781 }
2782
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002783 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002784
2785 private:
2786 DISALLOW_COPY_AND_ASSIGN(HIf);
2787};
2788
David Brazdilfc6a86a2015-06-26 10:33:45 +00002789
2790// Abstract instruction which marks the beginning and/or end of a try block and
2791// links it to the respective exception handlers. Behaves the same as a Goto in
2792// non-exceptional control flow.
2793// Normal-flow successor is stored at index zero, exception handlers under
2794// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002795class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002796 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002797 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002798 kEntry,
2799 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002800 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002801 };
2802
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002803 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002804 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2805 SetPackedField<BoundaryKindField>(kind);
2806 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002807
2808 bool IsControlFlow() const OVERRIDE { return true; }
2809
2810 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002811 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002812
David Brazdild26a4112015-11-10 11:07:31 +00002813 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2814 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2815 }
2816
David Brazdilfc6a86a2015-06-26 10:33:45 +00002817 // Returns whether `handler` is among its exception handlers (non-zero index
2818 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002819 bool HasExceptionHandler(const HBasicBlock& handler) const {
2820 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002821 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002822 }
2823
2824 // If not present already, adds `handler` to its block's list of exception
2825 // handlers.
2826 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002827 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002828 GetBlock()->AddSuccessor(handler);
2829 }
2830 }
2831
Vladimir Markoa1de9182016-02-25 11:37:38 +00002832 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2833 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002834
David Brazdilffee3d32015-07-06 11:48:53 +01002835 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2836
David Brazdilfc6a86a2015-06-26 10:33:45 +00002837 DECLARE_INSTRUCTION(TryBoundary);
2838
2839 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002840 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2841 static constexpr size_t kFieldBoundaryKindSize =
2842 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2843 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2844 kFieldBoundaryKind + kFieldBoundaryKindSize;
2845 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2846 "Too many packed fields.");
2847 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002848
2849 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2850};
2851
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002852// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002853class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002854 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002855 // We set CanTriggerGC to prevent any intermediate address to be live
2856 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002857 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002858 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002859 SetRawInputAt(0, cond);
2860 }
2861
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002862 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002863 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002864 return true;
2865 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002866 bool NeedsEnvironment() const OVERRIDE { return true; }
2867 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002868
2869 DECLARE_INSTRUCTION(Deoptimize);
2870
2871 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002872 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2873};
2874
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002875// Represents the ArtMethod that was passed as a first argument to
2876// the method. It is used by instructions that depend on it, like
2877// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002878class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002879 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002880 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2881 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002882
2883 DECLARE_INSTRUCTION(CurrentMethod);
2884
2885 private:
2886 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2887};
2888
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002889// Fetches an ArtMethod from the virtual table or the interface method table
2890// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002891class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002892 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002893 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002894 kVTable,
2895 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002896 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002897 };
2898 HClassTableGet(HInstruction* cls,
2899 Primitive::Type type,
2900 TableKind kind,
2901 size_t index,
2902 uint32_t dex_pc)
2903 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002904 index_(index) {
2905 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002906 SetRawInputAt(0, cls);
2907 }
2908
2909 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002910 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002911 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002912 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002913 }
2914
Vladimir Markoa1de9182016-02-25 11:37:38 +00002915 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002916 size_t GetIndex() const { return index_; }
2917
2918 DECLARE_INSTRUCTION(ClassTableGet);
2919
2920 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002921 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2922 static constexpr size_t kFieldTableKindSize =
2923 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2924 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2925 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2926 "Too many packed fields.");
2927 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2928
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002929 // The index of the ArtMethod in the table.
2930 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002931
2932 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2933};
2934
Mark Mendellfe57faa2015-09-18 09:26:15 -04002935// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2936// have one successor for each entry in the switch table, and the final successor
2937// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002938class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002939 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002940 HPackedSwitch(int32_t start_value,
2941 uint32_t num_entries,
2942 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002943 uint32_t dex_pc = kNoDexPc)
2944 : HTemplateInstruction(SideEffects::None(), dex_pc),
2945 start_value_(start_value),
2946 num_entries_(num_entries) {
2947 SetRawInputAt(0, input);
2948 }
2949
2950 bool IsControlFlow() const OVERRIDE { return true; }
2951
2952 int32_t GetStartValue() const { return start_value_; }
2953
Vladimir Marko211c2112015-09-24 16:52:33 +01002954 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002955
2956 HBasicBlock* GetDefaultBlock() const {
2957 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002958 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002959 }
2960 DECLARE_INSTRUCTION(PackedSwitch);
2961
2962 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002963 const int32_t start_value_;
2964 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002965
2966 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2967};
2968
Roland Levillain88cb1752014-10-20 16:36:47 +01002969class HUnaryOperation : public HExpression<1> {
2970 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002971 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2972 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002973 SetRawInputAt(0, input);
2974 }
2975
2976 HInstruction* GetInput() const { return InputAt(0); }
2977 Primitive::Type GetResultType() const { return GetType(); }
2978
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002979 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002980 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002981 return true;
2982 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002983
Roland Levillain31dd3d62016-02-16 12:21:02 +00002984 // Try to statically evaluate `this` and return a HConstant
2985 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002986 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002987 HConstant* TryStaticEvaluation() const;
2988
2989 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002990 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2991 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002992 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2993 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002994
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002995 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002996
2997 private:
2998 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2999};
3000
Dave Allison20dfc792014-06-16 20:44:29 -07003001class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003002 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003003 HBinaryOperation(Primitive::Type result_type,
3004 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003005 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003006 SideEffects side_effects = SideEffects::None(),
3007 uint32_t dex_pc = kNoDexPc)
3008 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003009 SetRawInputAt(0, left);
3010 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003011 }
3012
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003013 HInstruction* GetLeft() const { return InputAt(0); }
3014 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003015 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003016
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003017 virtual bool IsCommutative() const { return false; }
3018
3019 // Put constant on the right.
3020 // Returns whether order is changed.
3021 bool OrderInputsWithConstantOnTheRight() {
3022 HInstruction* left = InputAt(0);
3023 HInstruction* right = InputAt(1);
3024 if (left->IsConstant() && !right->IsConstant()) {
3025 ReplaceInput(right, 0);
3026 ReplaceInput(left, 1);
3027 return true;
3028 }
3029 return false;
3030 }
3031
3032 // Order inputs by instruction id, but favor constant on the right side.
3033 // This helps GVN for commutative ops.
3034 void OrderInputs() {
3035 DCHECK(IsCommutative());
3036 HInstruction* left = InputAt(0);
3037 HInstruction* right = InputAt(1);
3038 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3039 return;
3040 }
3041 if (OrderInputsWithConstantOnTheRight()) {
3042 return;
3043 }
3044 // Order according to instruction id.
3045 if (left->GetId() > right->GetId()) {
3046 ReplaceInput(right, 0);
3047 ReplaceInput(left, 1);
3048 }
3049 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003050
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003051 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003052 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003053 return true;
3054 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003055
Roland Levillain31dd3d62016-02-16 12:21:02 +00003056 // Try to statically evaluate `this` and return a HConstant
3057 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003058 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003059 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003060
3061 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003062 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3063 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003064 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3065 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003066 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003067 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3068 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003069 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3070 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003071 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3072 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003073 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003074 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3075 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003076
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003077 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003078 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003079 HConstant* GetConstantRight() const;
3080
3081 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003082 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003083 HInstruction* GetLeastConstantLeft() const;
3084
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003085 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003086
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003087 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003088 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3089};
3090
Mark Mendellc4701932015-04-10 13:18:51 -04003091// The comparison bias applies for floating point operations and indicates how NaN
3092// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003093enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003094 kNoBias, // bias is not applicable (i.e. for long operation)
3095 kGtBias, // return 1 for NaN comparisons
3096 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003097 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003098};
3099
Roland Levillain31dd3d62016-02-16 12:21:02 +00003100std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3101
Dave Allison20dfc792014-06-16 20:44:29 -07003102class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003103 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003104 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003105 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3106 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3107 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003108
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003109 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003110 // `instruction`, and disregard moves in between.
3111 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003112
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003113 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003114
3115 virtual IfCondition GetCondition() const = 0;
3116
Mark Mendellc4701932015-04-10 13:18:51 -04003117 virtual IfCondition GetOppositeCondition() const = 0;
3118
Vladimir Markoa1de9182016-02-25 11:37:38 +00003119 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003120 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3121
Vladimir Markoa1de9182016-02-25 11:37:38 +00003122 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3123 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003124
Vladimir Marko372f10e2016-05-17 16:30:10 +01003125 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003126 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003127 }
3128
Roland Levillain4fa13f62015-07-06 18:11:54 +01003129 bool IsFPConditionTrueIfNaN() 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 == kCondNE) {
3133 return true;
3134 } else if (if_cond == kCondEQ) {
3135 return false;
3136 }
3137 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003138 }
3139
3140 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003141 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003142 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003143 if (if_cond == kCondEQ) {
3144 return true;
3145 } else if (if_cond == kCondNE) {
3146 return false;
3147 }
3148 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003149 }
3150
Roland Levillain31dd3d62016-02-16 12:21:02 +00003151 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003152 // Needed if we merge a HCompare into a HCondition.
3153 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3154 static constexpr size_t kFieldComparisonBiasSize =
3155 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3156 static constexpr size_t kNumberOfConditionPackedBits =
3157 kFieldComparisonBias + kFieldComparisonBiasSize;
3158 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3159 using ComparisonBiasField =
3160 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3161
Roland Levillain31dd3d62016-02-16 12:21:02 +00003162 template <typename T>
3163 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3164
3165 template <typename T>
3166 int32_t CompareFP(T x, T y) const {
3167 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3168 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3169 // Handle the bias.
3170 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3171 }
3172
3173 // Return an integer constant containing the result of a condition evaluated at compile time.
3174 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3175 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3176 }
3177
Dave Allison20dfc792014-06-16 20:44:29 -07003178 private:
3179 DISALLOW_COPY_AND_ASSIGN(HCondition);
3180};
3181
3182// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003183class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003184 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003185 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3186 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003187
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003188 bool IsCommutative() const OVERRIDE { return true; }
3189
Vladimir Marko9e23df52015-11-10 17:14:35 +00003190 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3191 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003192 return MakeConstantCondition(true, GetDexPc());
3193 }
3194 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3195 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3196 }
3197 // In the following Evaluate methods, a HCompare instruction has
3198 // been merged into this HEqual instruction; evaluate it as
3199 // `Compare(x, y) == 0`.
3200 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3201 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3202 GetDexPc());
3203 }
3204 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3205 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3206 }
3207 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3208 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003209 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003210
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003211 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003212
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003213 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003214 return kCondEQ;
3215 }
3216
Mark Mendellc4701932015-04-10 13:18:51 -04003217 IfCondition GetOppositeCondition() const OVERRIDE {
3218 return kCondNE;
3219 }
3220
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003221 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003222 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003223
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003224 DISALLOW_COPY_AND_ASSIGN(HEqual);
3225};
3226
Vladimir Markofcb503c2016-05-18 12:48:17 +01003227class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003228 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003229 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3230 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003231
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003232 bool IsCommutative() const OVERRIDE { return true; }
3233
Vladimir Marko9e23df52015-11-10 17:14:35 +00003234 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3235 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003236 return MakeConstantCondition(false, GetDexPc());
3237 }
3238 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3239 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3240 }
3241 // In the following Evaluate methods, a HCompare instruction has
3242 // been merged into this HNotEqual instruction; evaluate it as
3243 // `Compare(x, y) != 0`.
3244 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3245 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3246 }
3247 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3248 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3249 }
3250 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3251 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003252 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003253
Dave Allison20dfc792014-06-16 20:44:29 -07003254 DECLARE_INSTRUCTION(NotEqual);
3255
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003256 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003257 return kCondNE;
3258 }
3259
Mark Mendellc4701932015-04-10 13:18:51 -04003260 IfCondition GetOppositeCondition() const OVERRIDE {
3261 return kCondEQ;
3262 }
3263
Dave Allison20dfc792014-06-16 20:44:29 -07003264 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003265 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003266
Dave Allison20dfc792014-06-16 20:44:29 -07003267 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3268};
3269
Vladimir Markofcb503c2016-05-18 12:48:17 +01003270class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003271 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003272 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3273 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003274
Roland Levillain9867bc72015-08-05 10:21:34 +01003275 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003276 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003277 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003278 // In the following Evaluate methods, a HCompare instruction has
3279 // been merged into this HLessThan instruction; evaluate it as
3280 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003281 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003282 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3283 }
3284 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3285 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3286 }
3287 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3288 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003289 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003290
Dave Allison20dfc792014-06-16 20:44:29 -07003291 DECLARE_INSTRUCTION(LessThan);
3292
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003293 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003294 return kCondLT;
3295 }
3296
Mark Mendellc4701932015-04-10 13:18:51 -04003297 IfCondition GetOppositeCondition() const OVERRIDE {
3298 return kCondGE;
3299 }
3300
Dave Allison20dfc792014-06-16 20:44:29 -07003301 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003302 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003303
Dave Allison20dfc792014-06-16 20:44:29 -07003304 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3305};
3306
Vladimir Markofcb503c2016-05-18 12:48:17 +01003307class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003308 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003309 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3310 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003311
Roland Levillain9867bc72015-08-05 10:21:34 +01003312 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003313 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003314 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003315 // In the following Evaluate methods, a HCompare instruction has
3316 // been merged into this HLessThanOrEqual instruction; evaluate it as
3317 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003318 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003319 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3320 }
3321 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3322 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3323 }
3324 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3325 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003326 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003327
Dave Allison20dfc792014-06-16 20:44:29 -07003328 DECLARE_INSTRUCTION(LessThanOrEqual);
3329
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003330 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003331 return kCondLE;
3332 }
3333
Mark Mendellc4701932015-04-10 13:18:51 -04003334 IfCondition GetOppositeCondition() const OVERRIDE {
3335 return kCondGT;
3336 }
3337
Dave Allison20dfc792014-06-16 20:44:29 -07003338 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003339 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003340
Dave Allison20dfc792014-06-16 20:44:29 -07003341 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3342};
3343
Vladimir Markofcb503c2016-05-18 12:48:17 +01003344class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003345 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003346 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3347 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003348
Roland Levillain9867bc72015-08-05 10:21:34 +01003349 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003350 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003351 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003352 // In the following Evaluate methods, a HCompare instruction has
3353 // been merged into this HGreaterThan instruction; evaluate it as
3354 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003355 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003356 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3357 }
3358 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3359 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3360 }
3361 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3362 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003363 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003364
Dave Allison20dfc792014-06-16 20:44:29 -07003365 DECLARE_INSTRUCTION(GreaterThan);
3366
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003367 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003368 return kCondGT;
3369 }
3370
Mark Mendellc4701932015-04-10 13:18:51 -04003371 IfCondition GetOppositeCondition() const OVERRIDE {
3372 return kCondLE;
3373 }
3374
Dave Allison20dfc792014-06-16 20:44:29 -07003375 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003376 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003377
Dave Allison20dfc792014-06-16 20:44:29 -07003378 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3379};
3380
Vladimir Markofcb503c2016-05-18 12:48:17 +01003381class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003382 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003383 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3384 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003385
Roland Levillain9867bc72015-08-05 10:21:34 +01003386 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003387 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003388 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003389 // In the following Evaluate methods, a HCompare instruction has
3390 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3391 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003392 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003393 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3394 }
3395 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3396 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3397 }
3398 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3399 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003400 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003401
Dave Allison20dfc792014-06-16 20:44:29 -07003402 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3403
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003404 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003405 return kCondGE;
3406 }
3407
Mark Mendellc4701932015-04-10 13:18:51 -04003408 IfCondition GetOppositeCondition() const OVERRIDE {
3409 return kCondLT;
3410 }
3411
Dave Allison20dfc792014-06-16 20:44:29 -07003412 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003413 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003414
Dave Allison20dfc792014-06-16 20:44:29 -07003415 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3416};
3417
Vladimir Markofcb503c2016-05-18 12:48:17 +01003418class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003419 public:
3420 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3421 : HCondition(first, second, dex_pc) {}
3422
3423 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003424 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003425 }
3426 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003427 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3428 }
3429 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3430 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3431 LOG(FATAL) << DebugName() << " is not defined for float values";
3432 UNREACHABLE();
3433 }
3434 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3435 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3436 LOG(FATAL) << DebugName() << " is not defined for double values";
3437 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003438 }
3439
3440 DECLARE_INSTRUCTION(Below);
3441
3442 IfCondition GetCondition() const OVERRIDE {
3443 return kCondB;
3444 }
3445
3446 IfCondition GetOppositeCondition() const OVERRIDE {
3447 return kCondAE;
3448 }
3449
3450 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003451 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003452 return MakeUnsigned(x) < MakeUnsigned(y);
3453 }
Aart Bike9f37602015-10-09 11:15:55 -07003454
3455 DISALLOW_COPY_AND_ASSIGN(HBelow);
3456};
3457
Vladimir Markofcb503c2016-05-18 12:48:17 +01003458class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003459 public:
3460 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3461 : HCondition(first, second, dex_pc) {}
3462
3463 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003464 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003465 }
3466 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003467 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3468 }
3469 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3470 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3471 LOG(FATAL) << DebugName() << " is not defined for float values";
3472 UNREACHABLE();
3473 }
3474 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3475 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3476 LOG(FATAL) << DebugName() << " is not defined for double values";
3477 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003478 }
3479
3480 DECLARE_INSTRUCTION(BelowOrEqual);
3481
3482 IfCondition GetCondition() const OVERRIDE {
3483 return kCondBE;
3484 }
3485
3486 IfCondition GetOppositeCondition() const OVERRIDE {
3487 return kCondA;
3488 }
3489
3490 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003491 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003492 return MakeUnsigned(x) <= MakeUnsigned(y);
3493 }
Aart Bike9f37602015-10-09 11:15:55 -07003494
3495 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3496};
3497
Vladimir Markofcb503c2016-05-18 12:48:17 +01003498class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003499 public:
3500 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3501 : HCondition(first, second, dex_pc) {}
3502
3503 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003504 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003505 }
3506 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003507 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3508 }
3509 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3510 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3511 LOG(FATAL) << DebugName() << " is not defined for float values";
3512 UNREACHABLE();
3513 }
3514 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3515 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3516 LOG(FATAL) << DebugName() << " is not defined for double values";
3517 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003518 }
3519
3520 DECLARE_INSTRUCTION(Above);
3521
3522 IfCondition GetCondition() const OVERRIDE {
3523 return kCondA;
3524 }
3525
3526 IfCondition GetOppositeCondition() const OVERRIDE {
3527 return kCondBE;
3528 }
3529
3530 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003531 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003532 return MakeUnsigned(x) > MakeUnsigned(y);
3533 }
Aart Bike9f37602015-10-09 11:15:55 -07003534
3535 DISALLOW_COPY_AND_ASSIGN(HAbove);
3536};
3537
Vladimir Markofcb503c2016-05-18 12:48:17 +01003538class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003539 public:
3540 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3541 : HCondition(first, second, dex_pc) {}
3542
3543 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003544 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003545 }
3546 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003547 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3548 }
3549 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3550 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3551 LOG(FATAL) << DebugName() << " is not defined for float values";
3552 UNREACHABLE();
3553 }
3554 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3555 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3556 LOG(FATAL) << DebugName() << " is not defined for double values";
3557 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003558 }
3559
3560 DECLARE_INSTRUCTION(AboveOrEqual);
3561
3562 IfCondition GetCondition() const OVERRIDE {
3563 return kCondAE;
3564 }
3565
3566 IfCondition GetOppositeCondition() const OVERRIDE {
3567 return kCondB;
3568 }
3569
3570 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003571 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003572 return MakeUnsigned(x) >= MakeUnsigned(y);
3573 }
Aart Bike9f37602015-10-09 11:15:55 -07003574
3575 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3576};
Dave Allison20dfc792014-06-16 20:44:29 -07003577
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003578// Instruction to check how two inputs compare to each other.
3579// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003580class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003581 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003582 // Note that `comparison_type` is the type of comparison performed
3583 // between the comparison's inputs, not the type of the instantiated
3584 // HCompare instruction (which is always Primitive::kPrimInt).
3585 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003586 HInstruction* first,
3587 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003588 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003589 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003590 : HBinaryOperation(Primitive::kPrimInt,
3591 first,
3592 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003593 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003594 dex_pc) {
3595 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003596 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3597 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003598 }
3599
Roland Levillain9867bc72015-08-05 10:21:34 +01003600 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003601 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3602
3603 template <typename T>
3604 int32_t ComputeFP(T x, T y) const {
3605 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3606 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3607 // Handle the bias.
3608 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3609 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003610
Roland Levillain9867bc72015-08-05 10:21:34 +01003611 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003612 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3613 // reach this code path when processing a freshly built HIR
3614 // graph. However HCompare integer instructions can be synthesized
3615 // by the instruction simplifier to implement IntegerCompare and
3616 // IntegerSignum intrinsics, so we have to handle this case.
3617 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003618 }
3619 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003620 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3621 }
3622 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3623 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3624 }
3625 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3626 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003627 }
3628
Vladimir Marko372f10e2016-05-17 16:30:10 +01003629 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003630 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003631 }
3632
Vladimir Markoa1de9182016-02-25 11:37:38 +00003633 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003634
Roland Levillain31dd3d62016-02-16 12:21:02 +00003635 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003636 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003637 bool IsGtBias() const {
3638 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003639 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003640 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003641
Roland Levillain1693a1f2016-03-15 14:57:31 +00003642 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3643 // Comparisons do not require a runtime call in any back end.
3644 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003645 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003646
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003647 DECLARE_INSTRUCTION(Compare);
3648
Roland Levillain31dd3d62016-02-16 12:21:02 +00003649 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003650 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3651 static constexpr size_t kFieldComparisonBiasSize =
3652 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3653 static constexpr size_t kNumberOfComparePackedBits =
3654 kFieldComparisonBias + kFieldComparisonBiasSize;
3655 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3656 using ComparisonBiasField =
3657 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3658
Roland Levillain31dd3d62016-02-16 12:21:02 +00003659 // Return an integer constant containing the result of a comparison evaluated at compile time.
3660 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3661 DCHECK(value == -1 || value == 0 || value == 1) << value;
3662 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3663 }
3664
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003665 private:
3666 DISALLOW_COPY_AND_ASSIGN(HCompare);
3667};
3668
Vladimir Markofcb503c2016-05-18 12:48:17 +01003669class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003670 public:
3671 HNewInstance(HInstruction* cls,
3672 HCurrentMethod* current_method,
3673 uint32_t dex_pc,
3674 uint16_t type_index,
3675 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003676 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003677 bool finalizable,
3678 QuickEntrypointEnum entrypoint)
3679 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3680 type_index_(type_index),
3681 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003682 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003683 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003684 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003685 SetRawInputAt(0, cls);
3686 SetRawInputAt(1, current_method);
3687 }
3688
3689 uint16_t GetTypeIndex() const { return type_index_; }
3690 const DexFile& GetDexFile() const { return dex_file_; }
3691
3692 // Calls runtime so needs an environment.
3693 bool NeedsEnvironment() const OVERRIDE { return true; }
3694
Mingyao Yang062157f2016-03-02 10:15:36 -08003695 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3696 bool CanThrow() const OVERRIDE { return true; }
3697
3698 // Needs to call into runtime to make sure it's instantiable/accessible.
3699 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003700
Vladimir Markoa1de9182016-02-25 11:37:38 +00003701 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003702
3703 bool CanBeNull() const OVERRIDE { return false; }
3704
3705 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3706
3707 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3708 entrypoint_ = entrypoint;
3709 }
3710
3711 bool IsStringAlloc() const;
3712
3713 DECLARE_INSTRUCTION(NewInstance);
3714
3715 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003716 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3717 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003718 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3719 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3720 "Too many packed fields.");
3721
David Brazdil6de19382016-01-08 17:37:10 +00003722 const uint16_t type_index_;
3723 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003724 QuickEntrypointEnum entrypoint_;
3725
3726 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3727};
3728
Agi Csaki05f20562015-08-19 14:58:14 -07003729enum IntrinsicNeedsEnvironmentOrCache {
3730 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3731 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003732};
3733
Aart Bik5d75afe2015-12-14 11:57:01 -08003734enum IntrinsicSideEffects {
3735 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3736 kReadSideEffects, // Intrinsic may read heap memory.
3737 kWriteSideEffects, // Intrinsic may write heap memory.
3738 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3739};
3740
3741enum IntrinsicExceptions {
3742 kNoThrow, // Intrinsic does not throw any exceptions.
3743 kCanThrow // Intrinsic may throw exceptions.
3744};
3745
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003746class HInvoke : public HInstruction {
3747 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003748 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003749
Vladimir Marko372f10e2016-05-17 16:30:10 +01003750 using HInstruction::GetInputRecords; // Keep the const version visible.
3751 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3752 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3753 }
3754
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003755 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003756 SetRawInputAt(index, argument);
3757 }
3758
Roland Levillain3e3d7332015-04-28 11:00:54 +01003759 // Return the number of arguments. This number can be lower than
3760 // the number of inputs returned by InputCount(), as some invoke
3761 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3762 // inputs at the end of their list of inputs.
3763 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3764
Vladimir Markoa1de9182016-02-25 11:37:38 +00003765 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003766
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003767 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003768 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003769
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003770 InvokeType GetInvokeType() const {
3771 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003772 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003773
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003774 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003775 return intrinsic_;
3776 }
3777
Aart Bik5d75afe2015-12-14 11:57:01 -08003778 void SetIntrinsic(Intrinsics intrinsic,
3779 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3780 IntrinsicSideEffects side_effects,
3781 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003782
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003783 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003784 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003785 }
3786
Vladimir Markoa1de9182016-02-25 11:37:38 +00003787 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003788
3789 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3790
Vladimir Marko372f10e2016-05-17 16:30:10 +01003791 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003792 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3793 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003794
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003795 uint32_t* GetIntrinsicOptimizations() {
3796 return &intrinsic_optimizations_;
3797 }
3798
3799 const uint32_t* GetIntrinsicOptimizations() const {
3800 return &intrinsic_optimizations_;
3801 }
3802
3803 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3804
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003805 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3806
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003807 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003808
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003809 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003810 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3811 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003812 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3813 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003814 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003815 static constexpr size_t kFieldReturnTypeSize =
3816 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3817 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3818 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3819 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003820 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003821 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3822
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003823 HInvoke(ArenaAllocator* arena,
3824 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003825 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003826 Primitive::Type return_type,
3827 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003828 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003829 ArtMethod* resolved_method,
3830 InvokeType invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003831 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003832 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003833 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003834 resolved_method_(resolved_method),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003835 inputs_(number_of_arguments + number_of_other_inputs,
3836 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003837 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003838 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003839 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003840 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003841 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003842 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003843 }
3844
Vladimir Markoa1de9182016-02-25 11:37:38 +00003845 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003846
Roland Levillain3e3d7332015-04-28 11:00:54 +01003847 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003848 ArtMethod* const resolved_method_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003849 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003850 const uint32_t dex_method_index_;
3851 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003852
3853 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3854 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003855
3856 private:
3857 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3858};
3859
Vladimir Markofcb503c2016-05-18 12:48:17 +01003860class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003861 public:
3862 HInvokeUnresolved(ArenaAllocator* arena,
3863 uint32_t number_of_arguments,
3864 Primitive::Type return_type,
3865 uint32_t dex_pc,
3866 uint32_t dex_method_index,
3867 InvokeType invoke_type)
3868 : HInvoke(arena,
3869 number_of_arguments,
3870 0u /* number_of_other_inputs */,
3871 return_type,
3872 dex_pc,
3873 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003874 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003875 invoke_type) {
3876 }
3877
3878 DECLARE_INSTRUCTION(InvokeUnresolved);
3879
3880 private:
3881 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3882};
3883
Vladimir Markofcb503c2016-05-18 12:48:17 +01003884class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003885 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003886 // Requirements of this method call regarding the class
3887 // initialization (clinit) check of its declaring class.
3888 enum class ClinitCheckRequirement {
3889 kNone, // Class already initialized.
3890 kExplicit, // Static call having explicit clinit check as last input.
3891 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003892 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003893 };
3894
Vladimir Marko58155012015-08-19 12:49:41 +00003895 // Determines how to load the target ArtMethod*.
3896 enum class MethodLoadKind {
3897 // Use a String init ArtMethod* loaded from Thread entrypoints.
3898 kStringInit,
3899
3900 // Use the method's own ArtMethod* loaded by the register allocator.
3901 kRecursive,
3902
3903 // Use ArtMethod* at a known address, embed the direct address in the code.
3904 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3905 kDirectAddress,
3906
3907 // Use ArtMethod* at an address that will be known at link time, embed the direct
3908 // address in the code. If the image is relocatable, emit .patch_oat entry.
3909 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3910 // the image relocatable or not.
3911 kDirectAddressWithFixup,
3912
Vladimir Markoa1de9182016-02-25 11:37:38 +00003913 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003914 // Used when we need to use the dex cache, for example for invoke-static that
3915 // may cause class initialization (the entry may point to a resolution method),
3916 // and we know that we can access the dex cache arrays using a PC-relative load.
3917 kDexCachePcRelative,
3918
3919 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3920 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3921 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3922 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3923 kDexCacheViaMethod,
3924 };
3925
3926 // Determines the location of the code pointer.
3927 enum class CodePtrLocation {
3928 // Recursive call, use local PC-relative call instruction.
3929 kCallSelf,
3930
3931 // Use PC-relative call instruction patched at link time.
3932 // Used for calls within an oat file, boot->boot or app->app.
3933 kCallPCRelative,
3934
3935 // Call to a known target address, embed the direct address in code.
3936 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3937 kCallDirect,
3938
3939 // Call to a target address that will be known at link time, embed the direct
3940 // address in code. If the image is relocatable, emit .patch_oat entry.
3941 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3942 // the image relocatable or not.
3943 kCallDirectWithFixup,
3944
3945 // Use code pointer from the ArtMethod*.
3946 // Used when we don't know the target code. This is also the last-resort-kind used when
3947 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3948 kCallArtMethod,
3949 };
3950
3951 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003952 MethodLoadKind method_load_kind;
3953 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003954 // The method load data holds
3955 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3956 // Note that there are multiple string init methods, each having its own offset.
3957 // - the method address for kDirectAddress
3958 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003959 uint64_t method_load_data;
3960 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003961 };
3962
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003963 HInvokeStaticOrDirect(ArenaAllocator* arena,
3964 uint32_t number_of_arguments,
3965 Primitive::Type return_type,
3966 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003967 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003968 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00003969 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003970 InvokeType invoke_type,
3971 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003972 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003973 : HInvoke(arena,
3974 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003975 // There is potentially one extra argument for the HCurrentMethod node, and
3976 // potentially one other if the clinit check is explicit, and potentially
3977 // one other if the method is a string factory.
3978 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003979 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003980 return_type,
3981 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003982 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003983 resolved_method,
3984 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003985 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003986 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003987 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3988 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003989
Vladimir Markodc151b22015-10-15 18:02:30 +01003990 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003991 bool had_current_method_input = HasCurrentMethodInput();
3992 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3993
3994 // Using the current method is the default and once we find a better
3995 // method load kind, we should not go back to using the current method.
3996 DCHECK(had_current_method_input || !needs_current_method_input);
3997
3998 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003999 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4000 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004001 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004002 dispatch_info_ = dispatch_info;
4003 }
4004
Vladimir Markoc53c0792015-11-19 15:48:33 +00004005 void AddSpecialInput(HInstruction* input) {
4006 // We allow only one special input.
4007 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4008 DCHECK(InputCount() == GetSpecialInputIndex() ||
4009 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4010 InsertInputAt(GetSpecialInputIndex(), input);
4011 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004012
Vladimir Marko372f10e2016-05-17 16:30:10 +01004013 using HInstruction::GetInputRecords; // Keep the const version visible.
4014 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4015 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4016 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4017 DCHECK(!input_records.empty());
4018 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4019 HInstruction* last_input = input_records.back().GetInstruction();
4020 // Note: `last_input` may be null during arguments setup.
4021 if (last_input != nullptr) {
4022 // `last_input` is the last input of a static invoke marked as having
4023 // an explicit clinit check. It must either be:
4024 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4025 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4026 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4027 }
4028 }
4029 return input_records;
4030 }
4031
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004032 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004033 // We access the method via the dex cache so we can't do an implicit null check.
4034 // TODO: for intrinsics we can generate implicit null checks.
4035 return false;
4036 }
4037
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004038 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004039 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004040 }
4041
Vladimir Markoc53c0792015-11-19 15:48:33 +00004042 // Get the index of the special input, if any.
4043 //
David Brazdil6de19382016-01-08 17:37:10 +00004044 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4045 // method pointer; otherwise there may be one platform-specific special input,
4046 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004047 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004048 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004049
Vladimir Marko58155012015-08-19 12:49:41 +00004050 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4051 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4052 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004053 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004054 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004055 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004056 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004057 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4058 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004059 bool HasCurrentMethodInput() const {
4060 // This function can be called only after the invoke has been fully initialized by the builder.
4061 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004062 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004063 return true;
4064 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004065 DCHECK(InputCount() == GetSpecialInputIndex() ||
4066 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004067 return false;
4068 }
4069 }
Vladimir Marko58155012015-08-19 12:49:41 +00004070 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
Vladimir Marko58155012015-08-19 12:49:41 +00004071
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004072 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004073 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004074 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004075 }
4076
4077 uint64_t GetMethodAddress() const {
4078 DCHECK(HasMethodAddress());
4079 return dispatch_info_.method_load_data;
4080 }
4081
4082 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004083 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004084 return dispatch_info_.method_load_data;
4085 }
4086
4087 uint64_t GetDirectCodePtr() const {
4088 DCHECK(HasDirectCodePtr());
4089 return dispatch_info_.direct_code_ptr;
4090 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004091
Vladimir Markoa1de9182016-02-25 11:37:38 +00004092 ClinitCheckRequirement GetClinitCheckRequirement() const {
4093 return GetPackedField<ClinitCheckRequirementField>();
4094 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004095
Roland Levillain4c0eb422015-04-24 16:43:49 +01004096 // Is this instruction a call to a static method?
4097 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004098 return GetInvokeType() == kStatic;
4099 }
4100
4101 MethodReference GetTargetMethod() const {
4102 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004103 }
4104
Vladimir Markofbb184a2015-11-13 14:47:00 +00004105 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4106 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4107 // instruction; only relevant for static calls with explicit clinit check.
4108 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004109 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004110 size_t last_input_index = inputs_.size() - 1u;
4111 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004112 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004113 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004114 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004115 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004116 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004117 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004118 }
4119
4120 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004121 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004122 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004123 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004124 }
4125
4126 // Is this a call to a static method whose declaring class has an
4127 // implicit intialization check requirement?
4128 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004129 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004130 }
4131
Vladimir Markob554b5a2015-11-06 12:57:55 +00004132 // Does this method load kind need the current method as an input?
4133 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4134 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4135 }
4136
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004137 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004138
Roland Levillain4c0eb422015-04-24 16:43:49 +01004139 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004140 void InsertInputAt(size_t index, HInstruction* input);
4141 void RemoveInputAt(size_t index);
4142
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004143 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004144 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004145 static constexpr size_t kFieldClinitCheckRequirementSize =
4146 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4147 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4148 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4149 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4150 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004151 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4152 kFieldClinitCheckRequirement,
4153 kFieldClinitCheckRequirementSize>;
4154
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004155 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004156 MethodReference target_method_;
4157 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004158
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004159 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004160};
Vladimir Markof64242a2015-12-01 14:58:23 +00004161std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004162std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004163
Vladimir Markofcb503c2016-05-18 12:48:17 +01004164class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004165 public:
4166 HInvokeVirtual(ArenaAllocator* arena,
4167 uint32_t number_of_arguments,
4168 Primitive::Type return_type,
4169 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004170 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004171 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004172 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004173 : HInvoke(arena,
4174 number_of_arguments,
4175 0u,
4176 return_type,
4177 dex_pc,
4178 dex_method_index,
4179 resolved_method,
4180 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004181 vtable_index_(vtable_index) {}
4182
Calin Juravle641547a2015-04-21 22:08:51 +01004183 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004184 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004185 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004186 }
4187
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004188 uint32_t GetVTableIndex() const { return vtable_index_; }
4189
4190 DECLARE_INSTRUCTION(InvokeVirtual);
4191
4192 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004193 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004194 const uint32_t vtable_index_;
4195
4196 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4197};
4198
Vladimir Markofcb503c2016-05-18 12:48:17 +01004199class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004200 public:
4201 HInvokeInterface(ArenaAllocator* arena,
4202 uint32_t number_of_arguments,
4203 Primitive::Type return_type,
4204 uint32_t dex_pc,
4205 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004206 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004207 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004208 : HInvoke(arena,
4209 number_of_arguments,
4210 0u,
4211 return_type,
4212 dex_pc,
4213 dex_method_index,
4214 resolved_method,
4215 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004216 imt_index_(imt_index) {}
4217
Calin Juravle641547a2015-04-21 22:08:51 +01004218 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004219 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004220 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004221 }
4222
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004223 uint32_t GetImtIndex() const { return imt_index_; }
4224 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4225
4226 DECLARE_INSTRUCTION(InvokeInterface);
4227
4228 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004229 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004230 const uint32_t imt_index_;
4231
4232 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4233};
4234
Vladimir Markofcb503c2016-05-18 12:48:17 +01004235class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004236 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004237 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004238 : HUnaryOperation(result_type, input, dex_pc) {
4239 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4240 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004241
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004242 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004243
4244 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004245 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004246 }
4247 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004248 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004249 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004250 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4251 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4252 }
4253 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4254 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4255 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004256
Roland Levillain88cb1752014-10-20 16:36:47 +01004257 DECLARE_INSTRUCTION(Neg);
4258
4259 private:
4260 DISALLOW_COPY_AND_ASSIGN(HNeg);
4261};
4262
Vladimir Markofcb503c2016-05-18 12:48:17 +01004263class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004264 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004265 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004266 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004267 uint32_t dex_pc,
4268 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004269 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004270 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004271 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004272 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004273 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004274 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004275 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004276 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004277 }
4278
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004279 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004280 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004281
4282 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004283 bool NeedsEnvironment() const OVERRIDE { return true; }
4284
Mingyao Yang0c365e62015-03-31 15:09:29 -07004285 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4286 bool CanThrow() const OVERRIDE { return true; }
4287
Calin Juravle10e244f2015-01-26 18:54:32 +00004288 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004289
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004290 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4291
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004292 DECLARE_INSTRUCTION(NewArray);
4293
4294 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004295 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004296 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004297 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004298
4299 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4300};
4301
Vladimir Markofcb503c2016-05-18 12:48:17 +01004302class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004303 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004304 HAdd(Primitive::Type result_type,
4305 HInstruction* left,
4306 HInstruction* right,
4307 uint32_t dex_pc = kNoDexPc)
4308 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004309
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004310 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004311
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004312 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004313
4314 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004315 return GetBlock()->GetGraph()->GetIntConstant(
4316 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004317 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004318 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004319 return GetBlock()->GetGraph()->GetLongConstant(
4320 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004321 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004322 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4323 return GetBlock()->GetGraph()->GetFloatConstant(
4324 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4325 }
4326 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4327 return GetBlock()->GetGraph()->GetDoubleConstant(
4328 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4329 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004330
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004331 DECLARE_INSTRUCTION(Add);
4332
4333 private:
4334 DISALLOW_COPY_AND_ASSIGN(HAdd);
4335};
4336
Vladimir Markofcb503c2016-05-18 12:48:17 +01004337class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004338 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004339 HSub(Primitive::Type result_type,
4340 HInstruction* left,
4341 HInstruction* right,
4342 uint32_t dex_pc = kNoDexPc)
4343 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004344
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004345 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004346
4347 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004348 return GetBlock()->GetGraph()->GetIntConstant(
4349 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004350 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004351 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004352 return GetBlock()->GetGraph()->GetLongConstant(
4353 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004354 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004355 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4356 return GetBlock()->GetGraph()->GetFloatConstant(
4357 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4358 }
4359 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4360 return GetBlock()->GetGraph()->GetDoubleConstant(
4361 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4362 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004363
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004364 DECLARE_INSTRUCTION(Sub);
4365
4366 private:
4367 DISALLOW_COPY_AND_ASSIGN(HSub);
4368};
4369
Vladimir Markofcb503c2016-05-18 12:48:17 +01004370class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004371 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004372 HMul(Primitive::Type result_type,
4373 HInstruction* left,
4374 HInstruction* right,
4375 uint32_t dex_pc = kNoDexPc)
4376 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004377
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004378 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004379
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004380 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004381
4382 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004383 return GetBlock()->GetGraph()->GetIntConstant(
4384 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004385 }
4386 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004387 return GetBlock()->GetGraph()->GetLongConstant(
4388 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004389 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004390 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4391 return GetBlock()->GetGraph()->GetFloatConstant(
4392 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4393 }
4394 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4395 return GetBlock()->GetGraph()->GetDoubleConstant(
4396 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4397 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004398
4399 DECLARE_INSTRUCTION(Mul);
4400
4401 private:
4402 DISALLOW_COPY_AND_ASSIGN(HMul);
4403};
4404
Vladimir Markofcb503c2016-05-18 12:48:17 +01004405class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004406 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004407 HDiv(Primitive::Type result_type,
4408 HInstruction* left,
4409 HInstruction* right,
4410 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004411 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004412
Roland Levillain9867bc72015-08-05 10:21:34 +01004413 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004414 T ComputeIntegral(T x, T y) const {
4415 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004416 // Our graph structure ensures we never have 0 for `y` during
4417 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004418 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004419 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004420 return (y == -1) ? -x : x / y;
4421 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004422
Roland Levillain31dd3d62016-02-16 12:21:02 +00004423 template <typename T>
4424 T ComputeFP(T x, T y) const {
4425 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4426 return x / y;
4427 }
4428
Roland Levillain9867bc72015-08-05 10:21:34 +01004429 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004430 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004431 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004432 }
4433 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004434 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004435 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4436 }
4437 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4438 return GetBlock()->GetGraph()->GetFloatConstant(
4439 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4440 }
4441 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4442 return GetBlock()->GetGraph()->GetDoubleConstant(
4443 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004444 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004445
4446 DECLARE_INSTRUCTION(Div);
4447
4448 private:
4449 DISALLOW_COPY_AND_ASSIGN(HDiv);
4450};
4451
Vladimir Markofcb503c2016-05-18 12:48:17 +01004452class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004453 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004454 HRem(Primitive::Type result_type,
4455 HInstruction* left,
4456 HInstruction* right,
4457 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004458 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004459
Roland Levillain9867bc72015-08-05 10:21:34 +01004460 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004461 T ComputeIntegral(T x, T y) const {
4462 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004463 // Our graph structure ensures we never have 0 for `y` during
4464 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004465 DCHECK_NE(y, 0);
4466 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4467 return (y == -1) ? 0 : x % y;
4468 }
4469
Roland Levillain31dd3d62016-02-16 12:21:02 +00004470 template <typename T>
4471 T ComputeFP(T x, T y) const {
4472 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4473 return std::fmod(x, y);
4474 }
4475
Roland Levillain9867bc72015-08-05 10:21:34 +01004476 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004477 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004478 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004479 }
4480 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004481 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004482 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004483 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004484 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4485 return GetBlock()->GetGraph()->GetFloatConstant(
4486 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4487 }
4488 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4489 return GetBlock()->GetGraph()->GetDoubleConstant(
4490 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4491 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004492
4493 DECLARE_INSTRUCTION(Rem);
4494
4495 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004496 DISALLOW_COPY_AND_ASSIGN(HRem);
4497};
4498
Vladimir Markofcb503c2016-05-18 12:48:17 +01004499class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004500 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004501 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4502 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004503 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004504 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004505 SetRawInputAt(0, value);
4506 }
4507
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004508 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4509
Calin Juravled0d48522014-11-04 16:40:20 +00004510 bool CanBeMoved() const OVERRIDE { return true; }
4511
Vladimir Marko372f10e2016-05-17 16:30:10 +01004512 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004513 return true;
4514 }
4515
4516 bool NeedsEnvironment() const OVERRIDE { return true; }
4517 bool CanThrow() const OVERRIDE { return true; }
4518
Calin Juravled0d48522014-11-04 16:40:20 +00004519 DECLARE_INSTRUCTION(DivZeroCheck);
4520
4521 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004522 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4523};
4524
Vladimir Markofcb503c2016-05-18 12:48:17 +01004525class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004526 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004527 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004528 HInstruction* value,
4529 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004530 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004531 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4532 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4533 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004534 }
4535
Roland Levillain5b5b9312016-03-22 14:57:31 +00004536 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004537 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004538 return value << (distance & max_shift_distance);
4539 }
4540
4541 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004542 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004543 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004544 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004545 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004546 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004547 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004548 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004549 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4550 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4551 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4552 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004553 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004554 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4555 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004556 LOG(FATAL) << DebugName() << " is not defined for float values";
4557 UNREACHABLE();
4558 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004559 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4560 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004561 LOG(FATAL) << DebugName() << " is not defined for double values";
4562 UNREACHABLE();
4563 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004564
4565 DECLARE_INSTRUCTION(Shl);
4566
4567 private:
4568 DISALLOW_COPY_AND_ASSIGN(HShl);
4569};
4570
Vladimir Markofcb503c2016-05-18 12:48:17 +01004571class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004572 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004573 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004574 HInstruction* value,
4575 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004576 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004577 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4578 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4579 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004580 }
4581
Roland Levillain5b5b9312016-03-22 14:57:31 +00004582 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004583 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004584 return value >> (distance & max_shift_distance);
4585 }
4586
4587 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004588 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004589 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004590 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004591 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004592 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004593 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004594 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004595 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4596 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4597 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4598 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004599 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004600 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4601 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004602 LOG(FATAL) << DebugName() << " is not defined for float values";
4603 UNREACHABLE();
4604 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004605 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4606 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004607 LOG(FATAL) << DebugName() << " is not defined for double values";
4608 UNREACHABLE();
4609 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004610
4611 DECLARE_INSTRUCTION(Shr);
4612
4613 private:
4614 DISALLOW_COPY_AND_ASSIGN(HShr);
4615};
4616
Vladimir Markofcb503c2016-05-18 12:48:17 +01004617class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004618 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004619 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004620 HInstruction* value,
4621 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004622 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004623 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4624 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4625 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004626 }
4627
Roland Levillain5b5b9312016-03-22 14:57:31 +00004628 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004629 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004630 typedef typename std::make_unsigned<T>::type V;
4631 V ux = static_cast<V>(value);
4632 return static_cast<T>(ux >> (distance & max_shift_distance));
4633 }
4634
4635 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004636 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004637 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004638 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004639 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004640 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004641 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004642 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004643 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4644 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4645 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4646 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004647 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004648 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4649 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004650 LOG(FATAL) << DebugName() << " is not defined for float values";
4651 UNREACHABLE();
4652 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004653 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4654 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004655 LOG(FATAL) << DebugName() << " is not defined for double values";
4656 UNREACHABLE();
4657 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004658
4659 DECLARE_INSTRUCTION(UShr);
4660
4661 private:
4662 DISALLOW_COPY_AND_ASSIGN(HUShr);
4663};
4664
Vladimir Markofcb503c2016-05-18 12:48:17 +01004665class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004666 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004667 HAnd(Primitive::Type result_type,
4668 HInstruction* left,
4669 HInstruction* right,
4670 uint32_t dex_pc = kNoDexPc)
4671 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004672
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004673 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004674
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004675 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004676
4677 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004678 return GetBlock()->GetGraph()->GetIntConstant(
4679 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004680 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004681 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004682 return GetBlock()->GetGraph()->GetLongConstant(
4683 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004684 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004685 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4686 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4687 LOG(FATAL) << DebugName() << " is not defined for float values";
4688 UNREACHABLE();
4689 }
4690 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4691 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4692 LOG(FATAL) << DebugName() << " is not defined for double values";
4693 UNREACHABLE();
4694 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004695
4696 DECLARE_INSTRUCTION(And);
4697
4698 private:
4699 DISALLOW_COPY_AND_ASSIGN(HAnd);
4700};
4701
Vladimir Markofcb503c2016-05-18 12:48:17 +01004702class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004703 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004704 HOr(Primitive::Type result_type,
4705 HInstruction* left,
4706 HInstruction* right,
4707 uint32_t dex_pc = kNoDexPc)
4708 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004709
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004710 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004711
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004712 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004713
4714 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004715 return GetBlock()->GetGraph()->GetIntConstant(
4716 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004717 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004718 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004719 return GetBlock()->GetGraph()->GetLongConstant(
4720 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004721 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004722 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4723 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4724 LOG(FATAL) << DebugName() << " is not defined for float values";
4725 UNREACHABLE();
4726 }
4727 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4728 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4729 LOG(FATAL) << DebugName() << " is not defined for double values";
4730 UNREACHABLE();
4731 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004732
4733 DECLARE_INSTRUCTION(Or);
4734
4735 private:
4736 DISALLOW_COPY_AND_ASSIGN(HOr);
4737};
4738
Vladimir Markofcb503c2016-05-18 12:48:17 +01004739class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004740 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004741 HXor(Primitive::Type result_type,
4742 HInstruction* left,
4743 HInstruction* right,
4744 uint32_t dex_pc = kNoDexPc)
4745 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004746
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004747 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004748
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004749 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004750
4751 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004752 return GetBlock()->GetGraph()->GetIntConstant(
4753 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004754 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004755 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004756 return GetBlock()->GetGraph()->GetLongConstant(
4757 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004758 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004759 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4760 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4761 LOG(FATAL) << DebugName() << " is not defined for float values";
4762 UNREACHABLE();
4763 }
4764 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4765 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4766 LOG(FATAL) << DebugName() << " is not defined for double values";
4767 UNREACHABLE();
4768 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004769
4770 DECLARE_INSTRUCTION(Xor);
4771
4772 private:
4773 DISALLOW_COPY_AND_ASSIGN(HXor);
4774};
4775
Vladimir Markofcb503c2016-05-18 12:48:17 +01004776class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004777 public:
4778 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004779 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004780 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4781 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004782 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004783
Roland Levillain5b5b9312016-03-22 14:57:31 +00004784 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004785 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004786 typedef typename std::make_unsigned<T>::type V;
4787 V ux = static_cast<V>(value);
4788 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004789 return static_cast<T>(ux);
4790 } else {
4791 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004792 return static_cast<T>(ux >> (distance & max_shift_value)) |
4793 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004794 }
4795 }
4796
Roland Levillain5b5b9312016-03-22 14:57:31 +00004797 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004798 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004799 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004800 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004801 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004802 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004803 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004804 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004805 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4806 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4807 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4808 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004809 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004810 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4811 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004812 LOG(FATAL) << DebugName() << " is not defined for float values";
4813 UNREACHABLE();
4814 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004815 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4816 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004817 LOG(FATAL) << DebugName() << " is not defined for double values";
4818 UNREACHABLE();
4819 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004820
4821 DECLARE_INSTRUCTION(Ror);
4822
4823 private:
4824 DISALLOW_COPY_AND_ASSIGN(HRor);
4825};
4826
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004827// The value of a parameter in this method. Its location depends on
4828// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004829class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004830 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004831 HParameterValue(const DexFile& dex_file,
4832 uint16_t type_index,
4833 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004834 Primitive::Type parameter_type,
4835 bool is_this = false)
4836 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004837 dex_file_(dex_file),
4838 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004839 index_(index) {
4840 SetPackedFlag<kFlagIsThis>(is_this);
4841 SetPackedFlag<kFlagCanBeNull>(!is_this);
4842 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004843
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004844 const DexFile& GetDexFile() const { return dex_file_; }
4845 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004846 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004847 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004848
Vladimir Markoa1de9182016-02-25 11:37:38 +00004849 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4850 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004851
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004852 DECLARE_INSTRUCTION(ParameterValue);
4853
4854 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004855 // Whether or not the parameter value corresponds to 'this' argument.
4856 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4857 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4858 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4859 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4860 "Too many packed fields.");
4861
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004862 const DexFile& dex_file_;
4863 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004864 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004865 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004866 const uint8_t index_;
4867
4868 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4869};
4870
Vladimir Markofcb503c2016-05-18 12:48:17 +01004871class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004872 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004873 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4874 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004875
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004876 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004877 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004878 return true;
4879 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004880
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004881 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004882
4883 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004884 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004885 }
4886 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004887 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004888 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004889 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4890 LOG(FATAL) << DebugName() << " is not defined for float values";
4891 UNREACHABLE();
4892 }
4893 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4894 LOG(FATAL) << DebugName() << " is not defined for double values";
4895 UNREACHABLE();
4896 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004897
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004898 DECLARE_INSTRUCTION(Not);
4899
4900 private:
4901 DISALLOW_COPY_AND_ASSIGN(HNot);
4902};
4903
Vladimir Markofcb503c2016-05-18 12:48:17 +01004904class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004905 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004906 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4907 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004908
4909 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004910 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004911 return true;
4912 }
4913
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004914 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004915 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004916 return !x;
4917 }
4918
Roland Levillain9867bc72015-08-05 10:21:34 +01004919 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004920 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004921 }
4922 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4923 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004924 UNREACHABLE();
4925 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004926 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4927 LOG(FATAL) << DebugName() << " is not defined for float values";
4928 UNREACHABLE();
4929 }
4930 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4931 LOG(FATAL) << DebugName() << " is not defined for double values";
4932 UNREACHABLE();
4933 }
David Brazdil66d126e2015-04-03 16:02:44 +01004934
4935 DECLARE_INSTRUCTION(BooleanNot);
4936
4937 private:
4938 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4939};
4940
Vladimir Markofcb503c2016-05-18 12:48:17 +01004941class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004942 public:
4943 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004944 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004945 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004946 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004947 // Invariant: We should never generate a conversion to a Boolean value.
4948 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004949 }
4950
4951 HInstruction* GetInput() const { return InputAt(0); }
4952 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4953 Primitive::Type GetResultType() const { return GetType(); }
4954
4955 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004956 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4957 return true;
4958 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004959
Mark Mendelle82549b2015-05-06 10:55:34 -04004960 // Try to statically evaluate the conversion and return a HConstant
4961 // containing the result. If the input cannot be converted, return nullptr.
4962 HConstant* TryStaticEvaluation() const;
4963
Roland Levillaindff1f282014-11-05 14:15:05 +00004964 DECLARE_INSTRUCTION(TypeConversion);
4965
4966 private:
4967 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4968};
4969
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004970static constexpr uint32_t kNoRegNumber = -1;
4971
Vladimir Markofcb503c2016-05-18 12:48:17 +01004972class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004973 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004974 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4975 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004976 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004977 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004978 SetRawInputAt(0, value);
4979 }
4980
Calin Juravle10e244f2015-01-26 18:54:32 +00004981 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004982 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004983 return true;
4984 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004985
Calin Juravle10e244f2015-01-26 18:54:32 +00004986 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004987
Calin Juravle10e244f2015-01-26 18:54:32 +00004988 bool CanThrow() const OVERRIDE { return true; }
4989
4990 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004991
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004992
4993 DECLARE_INSTRUCTION(NullCheck);
4994
4995 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004996 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4997};
4998
4999class FieldInfo : public ValueObject {
5000 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005001 FieldInfo(MemberOffset field_offset,
5002 Primitive::Type field_type,
5003 bool is_volatile,
5004 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005005 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005006 const DexFile& dex_file,
5007 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005008 : field_offset_(field_offset),
5009 field_type_(field_type),
5010 is_volatile_(is_volatile),
5011 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005012 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005013 dex_file_(dex_file),
5014 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005015
5016 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005017 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005018 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005019 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005020 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005021 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005022 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005023
5024 private:
5025 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005026 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005027 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005028 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005029 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005030 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005031 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005032};
5033
Vladimir Markofcb503c2016-05-18 12:48:17 +01005034class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005035 public:
5036 HInstanceFieldGet(HInstruction* value,
5037 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005038 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005039 bool is_volatile,
5040 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005041 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005042 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005043 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005044 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005045 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005046 field_info_(field_offset,
5047 field_type,
5048 is_volatile,
5049 field_idx,
5050 declaring_class_def_index,
5051 dex_file,
5052 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005053 SetRawInputAt(0, value);
5054 }
5055
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005056 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005057
Vladimir Marko372f10e2016-05-17 16:30:10 +01005058 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5059 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005060 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005061 }
5062
Calin Juravle641547a2015-04-21 22:08:51 +01005063 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005064 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005065 }
5066
5067 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005068 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5069 }
5070
Calin Juravle52c48962014-12-16 17:02:57 +00005071 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005072 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005073 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005074 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005075
5076 DECLARE_INSTRUCTION(InstanceFieldGet);
5077
5078 private:
5079 const FieldInfo field_info_;
5080
5081 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5082};
5083
Vladimir Markofcb503c2016-05-18 12:48:17 +01005084class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005085 public:
5086 HInstanceFieldSet(HInstruction* object,
5087 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005088 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005089 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005090 bool is_volatile,
5091 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005092 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005093 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005094 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005095 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005096 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005097 field_info_(field_offset,
5098 field_type,
5099 is_volatile,
5100 field_idx,
5101 declaring_class_def_index,
5102 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005103 dex_cache) {
5104 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005105 SetRawInputAt(0, object);
5106 SetRawInputAt(1, value);
5107 }
5108
Calin Juravle641547a2015-04-21 22:08:51 +01005109 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005110 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005111 }
5112
Calin Juravle52c48962014-12-16 17:02:57 +00005113 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005114 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005115 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005116 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005117 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005118 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5119 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005120
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005121 DECLARE_INSTRUCTION(InstanceFieldSet);
5122
5123 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005124 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5125 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5126 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5127 "Too many packed fields.");
5128
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005129 const FieldInfo field_info_;
5130
5131 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5132};
5133
Vladimir Markofcb503c2016-05-18 12:48:17 +01005134class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005135 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005136 HArrayGet(HInstruction* array,
5137 HInstruction* index,
5138 Primitive::Type type,
5139 uint32_t dex_pc,
5140 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005141 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005142 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005143 SetRawInputAt(0, array);
5144 SetRawInputAt(1, index);
5145 }
5146
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005147 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005148 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005149 return true;
5150 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005151 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005152 // TODO: We can be smarter here.
5153 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5154 // which generates the implicit null check. There are cases when these can be removed
5155 // to produce better code. If we ever add optimizations to do so we should allow an
5156 // implicit check here (as long as the address falls in the first page).
5157 return false;
5158 }
5159
David Brazdil4833f5a2015-12-16 10:37:39 +00005160 bool IsEquivalentOf(HArrayGet* other) const {
5161 bool result = (GetDexPc() == other->GetDexPc());
5162 if (kIsDebugBuild && result) {
5163 DCHECK_EQ(GetBlock(), other->GetBlock());
5164 DCHECK_EQ(GetArray(), other->GetArray());
5165 DCHECK_EQ(GetIndex(), other->GetIndex());
5166 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005167 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005168 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005169 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5170 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005171 }
5172 }
5173 return result;
5174 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005175
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005176 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5177
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005178 HInstruction* GetArray() const { return InputAt(0); }
5179 HInstruction* GetIndex() const { return InputAt(1); }
5180
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005181 DECLARE_INSTRUCTION(ArrayGet);
5182
5183 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005184 // We treat a String as an array, creating the HArrayGet from String.charAt()
5185 // intrinsic in the instruction simplifier. We can always determine whether
5186 // a particular HArrayGet is actually a String.charAt() by looking at the type
5187 // of the input but that requires holding the mutator lock, so we prefer to use
5188 // a flag, so that code generators don't need to do the locking.
5189 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5190 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5191 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5192 "Too many packed fields.");
5193
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005194 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5195};
5196
Vladimir Markofcb503c2016-05-18 12:48:17 +01005197class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005198 public:
5199 HArraySet(HInstruction* array,
5200 HInstruction* index,
5201 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005202 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005203 uint32_t dex_pc)
5204 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005205 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5206 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5207 SetPackedFlag<kFlagValueCanBeNull>(true);
5208 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005209 SetRawInputAt(0, array);
5210 SetRawInputAt(1, index);
5211 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005212 // Make a best guess now, may be refined during SSA building.
5213 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005214 }
5215
Calin Juravle77520bc2015-01-12 18:45:46 +00005216 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005217 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005218 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005219 }
5220
Mingyao Yang81014cb2015-06-02 03:16:27 -07005221 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005222 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005223
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005224 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005225 // TODO: Same as for ArrayGet.
5226 return false;
5227 }
5228
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005229 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005230 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005231 }
5232
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005233 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005234 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005235 }
5236
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005237 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005238 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005239 }
5240
Vladimir Markoa1de9182016-02-25 11:37:38 +00005241 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5242 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5243 bool StaticTypeOfArrayIsObjectArray() const {
5244 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5245 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005246
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005247 HInstruction* GetArray() const { return InputAt(0); }
5248 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005249 HInstruction* GetValue() const { return InputAt(2); }
5250
5251 Primitive::Type GetComponentType() const {
5252 // The Dex format does not type floating point index operations. Since the
5253 // `expected_component_type_` is set during building and can therefore not
5254 // be correct, we also check what is the value type. If it is a floating
5255 // point type, we must use that type.
5256 Primitive::Type value_type = GetValue()->GetType();
5257 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5258 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005259 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005260 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005261
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005262 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005263 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005264 }
5265
Aart Bik18b36ab2016-04-13 16:41:35 -07005266 void ComputeSideEffects() {
5267 Primitive::Type type = GetComponentType();
5268 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5269 SideEffectsForArchRuntimeCalls(type)));
5270 }
5271
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005272 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5273 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5274 }
5275
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005276 DECLARE_INSTRUCTION(ArraySet);
5277
5278 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005279 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5280 static constexpr size_t kFieldExpectedComponentTypeSize =
5281 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5282 static constexpr size_t kFlagNeedsTypeCheck =
5283 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5284 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005285 // Cached information for the reference_type_info_ so that codegen
5286 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005287 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5288 static constexpr size_t kNumberOfArraySetPackedBits =
5289 kFlagStaticTypeOfArrayIsObjectArray + 1;
5290 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5291 using ExpectedComponentTypeField =
5292 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005293
5294 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5295};
5296
Vladimir Markofcb503c2016-05-18 12:48:17 +01005297class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005298 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005299 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005300 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005301 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005302 // Note that arrays do not change length, so the instruction does not
5303 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005304 SetRawInputAt(0, array);
5305 }
5306
Calin Juravle77520bc2015-01-12 18:45:46 +00005307 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005308 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005309 return true;
5310 }
Calin Juravle641547a2015-04-21 22:08:51 +01005311 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5312 return obj == InputAt(0);
5313 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005314
Vladimir Markodce016e2016-04-28 13:10:02 +01005315 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5316
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005317 DECLARE_INSTRUCTION(ArrayLength);
5318
5319 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005320 // We treat a String as an array, creating the HArrayLength from String.length()
5321 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5322 // determine whether a particular HArrayLength is actually a String.length() by
5323 // looking at the type of the input but that requires holding the mutator lock, so
5324 // we prefer to use a flag, so that code generators don't need to do the locking.
5325 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5326 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5327 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5328 "Too many packed fields.");
5329
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005330 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5331};
5332
Vladimir Markofcb503c2016-05-18 12:48:17 +01005333class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005334 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005335 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5336 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005337 HBoundsCheck(HInstruction* index,
5338 HInstruction* length,
5339 uint32_t dex_pc,
5340 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5341 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5342 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005343 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005344 SetRawInputAt(0, index);
5345 SetRawInputAt(1, length);
5346 }
5347
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005348 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005349 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005350 return true;
5351 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005352
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005353 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005354
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005355 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005356
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005357 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5358 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5359
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005360 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005361
5362 DECLARE_INSTRUCTION(BoundsCheck);
5363
5364 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005365 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5366 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5367 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5368 // so we need to create an HEnvironment which will be translated to an InlineInfo
5369 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5370 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5371 // from the invoke as we don't want to look again at the dex bytecode.
5372 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5373
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005374 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5375};
5376
Vladimir Markofcb503c2016-05-18 12:48:17 +01005377class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005378 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005379 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005380 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005381
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005382 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005383 return true;
5384 }
5385
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005386 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5387 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005388
5389 DECLARE_INSTRUCTION(SuspendCheck);
5390
5391 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005392 // Only used for code generation, in order to share the same slow path between back edges
5393 // of a same loop.
5394 SlowPathCode* slow_path_;
5395
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005396 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5397};
5398
David Srbecky0cf44932015-12-09 14:09:59 +00005399// Pseudo-instruction which provides the native debugger with mapping information.
5400// It ensures that we can generate line number and local variables at this point.
5401class HNativeDebugInfo : public HTemplateInstruction<0> {
5402 public:
5403 explicit HNativeDebugInfo(uint32_t dex_pc)
5404 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5405
5406 bool NeedsEnvironment() const OVERRIDE {
5407 return true;
5408 }
5409
5410 DECLARE_INSTRUCTION(NativeDebugInfo);
5411
5412 private:
5413 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5414};
5415
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005416/**
5417 * Instruction to load a Class object.
5418 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005419class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005420 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005421 // Determines how to load the Class.
5422 enum class LoadKind {
5423 // Use the Class* from the method's own ArtMethod*.
5424 kReferrersClass,
5425
5426 // Use boot image Class* address that will be known at link time.
5427 // Used for boot image classes referenced by boot image code in non-PIC mode.
5428 kBootImageLinkTimeAddress,
5429
5430 // Use PC-relative boot image Class* address that will be known at link time.
5431 // Used for boot image classes referenced by boot image code in PIC mode.
5432 kBootImageLinkTimePcRelative,
5433
5434 // Use a known boot image Class* address, embedded in the code by the codegen.
5435 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5436 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5437 // GetIncludePatchInformation().
5438 kBootImageAddress,
5439
5440 // Load from the resolved types array at an absolute address.
5441 // Used for classes outside the boot image referenced by JIT-compiled code.
5442 kDexCacheAddress,
5443
5444 // Load from resolved types array in the dex cache using a PC-relative load.
5445 // Used for classes outside boot image when we know that we can access
5446 // the dex cache arrays using a PC-relative load.
5447 kDexCachePcRelative,
5448
5449 // Load from resolved types array accessed through the class loaded from
5450 // the compiled method's own ArtMethod*. This is the default access type when
5451 // all other types are unavailable.
5452 kDexCacheViaMethod,
5453
5454 kLast = kDexCacheViaMethod
5455 };
5456
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005457 HLoadClass(HCurrentMethod* current_method,
5458 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005459 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005460 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005461 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005462 bool needs_access_check,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005463 bool is_in_dex_cache,
5464 bool is_in_boot_image)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005465 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5466 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005467 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005468 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005469 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005470 // Referrers class should not need access check. We never inline unverified
5471 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005472 DCHECK(!is_referrers_class || !needs_access_check);
5473
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005474 SetPackedField<LoadKindField>(
5475 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005476 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5477 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005478 SetPackedFlag<kFlagIsInBootImage>(is_in_boot_image);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005479 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005480 }
5481
5482 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5483 DCHECK(HasAddress(load_kind));
5484 load_data_.address = address;
5485 SetLoadKindInternal(load_kind);
5486 }
5487
5488 void SetLoadKindWithTypeReference(LoadKind load_kind,
5489 const DexFile& dex_file,
5490 uint32_t type_index) {
5491 DCHECK(HasTypeReference(load_kind));
5492 DCHECK(IsSameDexFile(dex_file_, dex_file));
5493 DCHECK_EQ(type_index_, type_index);
5494 SetLoadKindInternal(load_kind);
5495 }
5496
5497 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5498 const DexFile& dex_file,
5499 uint32_t element_index) {
5500 DCHECK(HasDexCacheReference(load_kind));
5501 DCHECK(IsSameDexFile(dex_file_, dex_file));
5502 load_data_.dex_cache_element_index = element_index;
5503 SetLoadKindInternal(load_kind);
5504 }
5505
5506 LoadKind GetLoadKind() const {
5507 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005508 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005509
5510 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005511
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005512 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005513
5514 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5515
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005516 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005517
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005518 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005519 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005520 }
5521
Calin Juravle0ba218d2015-05-19 18:46:01 +01005522 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005523 // The entrypoint the code generator is going to call does not do
5524 // clinit of the class.
5525 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005526 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005527 }
5528
5529 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005530 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005531 (!IsReferrersClass() && !IsInDexCache()) ||
5532 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005533 }
5534
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005535
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005536 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005537 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005538 }
5539
Calin Juravleacf735c2015-02-12 15:25:22 +00005540 ReferenceTypeInfo GetLoadedClassRTI() {
5541 return loaded_class_rti_;
5542 }
5543
5544 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5545 // Make sure we only set exact types (the loaded class should never be merged).
5546 DCHECK(rti.IsExact());
5547 loaded_class_rti_ = rti;
5548 }
5549
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005550 uint32_t GetTypeIndex() const { return type_index_; }
5551 const DexFile& GetDexFile() const { return dex_file_; }
5552
5553 uint32_t GetDexCacheElementOffset() const;
5554
5555 uint64_t GetAddress() const {
5556 DCHECK(HasAddress(GetLoadKind()));
5557 return load_data_.address;
5558 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005559
Vladimir Markoa1de9182016-02-25 11:37:38 +00005560 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005561
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005562 static SideEffects SideEffectsForArchRuntimeCalls() {
5563 return SideEffects::CanTriggerGC();
5564 }
5565
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005566 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005567 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5568 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005569 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005570 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005571
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005572 void MarkInDexCache() {
5573 SetPackedFlag<kFlagIsInDexCache>(true);
5574 DCHECK(!NeedsEnvironment());
5575 RemoveEnvironment();
5576 SetSideEffects(SideEffects::None());
5577 }
5578
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005579 void MarkInBootImage() {
5580 SetPackedFlag<kFlagIsInBootImage>(true);
5581 }
5582
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005583 void AddSpecialInput(HInstruction* special_input);
5584
5585 using HInstruction::GetInputRecords; // Keep the const version visible.
5586 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5587 return ArrayRef<HUserRecord<HInstruction*>>(
5588 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5589 }
5590
5591 Primitive::Type GetType() const OVERRIDE {
5592 return Primitive::kPrimNot;
5593 }
5594
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005595 DECLARE_INSTRUCTION(LoadClass);
5596
5597 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005598 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005599 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005600 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005601 // Whether this instruction must generate the initialization check.
5602 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005603 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005604 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5605 static constexpr size_t kFieldLoadKindSize =
5606 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5607 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005608 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005609 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5610
5611 static bool HasTypeReference(LoadKind load_kind) {
5612 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5613 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5614 load_kind == LoadKind::kDexCacheViaMethod ||
5615 load_kind == LoadKind::kReferrersClass;
5616 }
5617
5618 static bool HasAddress(LoadKind load_kind) {
5619 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5620 }
5621
5622 static bool HasDexCacheReference(LoadKind load_kind) {
5623 return load_kind == LoadKind::kDexCachePcRelative;
5624 }
5625
5626 void SetLoadKindInternal(LoadKind load_kind);
5627
5628 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5629 // For other load kinds it's empty or possibly some architecture-specific instruction
5630 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5631 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005632
5633 const uint16_t type_index_;
5634 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005635
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005636 union {
5637 uint32_t dex_cache_element_index; // Only for dex cache reference.
5638 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5639 } load_data_;
5640
Calin Juravleacf735c2015-02-12 15:25:22 +00005641 ReferenceTypeInfo loaded_class_rti_;
5642
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005643 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5644};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005645std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5646
5647// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5648inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5649 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5650 return load_data_.dex_cache_element_index;
5651}
5652
5653// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5654inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005655 // The special input is used for PC-relative loads on some architectures,
5656 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005657 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005658 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5659 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5660 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005661 DCHECK(special_input_.GetInstruction() == nullptr);
5662 special_input_ = HUserRecord<HInstruction*>(special_input);
5663 special_input->AddUseAt(this, 0);
5664}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005665
Vladimir Marko372f10e2016-05-17 16:30:10 +01005666class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005667 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005668 // Determines how to load the String.
5669 enum class LoadKind {
5670 // Use boot image String* address that will be known at link time.
5671 // Used for boot image strings referenced by boot image code in non-PIC mode.
5672 kBootImageLinkTimeAddress,
5673
5674 // Use PC-relative boot image String* address that will be known at link time.
5675 // Used for boot image strings referenced by boot image code in PIC mode.
5676 kBootImageLinkTimePcRelative,
5677
5678 // Use a known boot image String* address, embedded in the code by the codegen.
5679 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5680 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5681 // GetIncludePatchInformation().
5682 kBootImageAddress,
5683
Vladimir Markoaad75c62016-10-03 08:46:48 +00005684 // Load from an entry in the .bss section using a PC-relative load.
5685 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5686 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005687
5688 // Load from resolved strings array accessed through the class loaded from
5689 // the compiled method's own ArtMethod*. This is the default access type when
5690 // all other types are unavailable.
5691 kDexCacheViaMethod,
5692
Nicolas Geoffrayac3ebc32016-10-05 13:13:50 +01005693 // Load from the root table associated with the JIT compiled method.
5694 kJitTableAddress,
5695
5696 kLast = kJitTableAddress,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005697 };
5698
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005699 HLoadString(HCurrentMethod* current_method,
5700 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005701 const DexFile& dex_file,
5702 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005703 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5704 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005705 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005706 SetPackedFlag<kFlagIsInDexCache>(false);
5707 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005708 load_data_.dex_file_ = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005709 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005710
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005711 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5712 DCHECK(HasAddress(load_kind));
5713 load_data_.address = address;
5714 SetLoadKindInternal(load_kind);
5715 }
5716
5717 void SetLoadKindWithStringReference(LoadKind load_kind,
5718 const DexFile& dex_file,
5719 uint32_t string_index) {
5720 DCHECK(HasStringReference(load_kind));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005721 load_data_.dex_file_ = &dex_file;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005722 string_index_ = string_index;
5723 SetLoadKindInternal(load_kind);
5724 }
5725
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005726 LoadKind GetLoadKind() const {
5727 return GetPackedField<LoadKindField>();
5728 }
5729
5730 const DexFile& GetDexFile() const;
5731
5732 uint32_t GetStringIndex() const {
5733 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5734 return string_index_;
5735 }
5736
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005737 uint64_t GetAddress() const {
5738 DCHECK(HasAddress(GetLoadKind()));
5739 return load_data_.address;
5740 }
5741
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005742 bool CanBeMoved() const OVERRIDE { return true; }
5743
Vladimir Marko372f10e2016-05-17 16:30:10 +01005744 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005745
5746 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5747
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005748 // Will call the runtime if we need to load the string through
5749 // the dex cache and the string is not guaranteed to be there yet.
5750 bool NeedsEnvironment() const OVERRIDE {
5751 LoadKind load_kind = GetLoadKind();
5752 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5753 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffrayac3ebc32016-10-05 13:13:50 +01005754 load_kind == LoadKind::kBootImageAddress ||
5755 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005756 return false;
5757 }
5758 return !IsInDexCache();
5759 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005760
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005761 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5762 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5763 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005764
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005765 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005766 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005767
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005768 static SideEffects SideEffectsForArchRuntimeCalls() {
5769 return SideEffects::CanTriggerGC();
5770 }
5771
Vladimir Markoa1de9182016-02-25 11:37:38 +00005772 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5773
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005774 void MarkInDexCache() {
5775 SetPackedFlag<kFlagIsInDexCache>(true);
5776 DCHECK(!NeedsEnvironment());
5777 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005778 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005779 }
5780
Vladimir Marko372f10e2016-05-17 16:30:10 +01005781 void AddSpecialInput(HInstruction* special_input);
5782
5783 using HInstruction::GetInputRecords; // Keep the const version visible.
5784 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5785 return ArrayRef<HUserRecord<HInstruction*>>(
5786 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005787 }
5788
Vladimir Marko372f10e2016-05-17 16:30:10 +01005789 Primitive::Type GetType() const OVERRIDE {
5790 return Primitive::kPrimNot;
5791 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005792
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005793 DECLARE_INSTRUCTION(LoadString);
5794
5795 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005796 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005797 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5798 static constexpr size_t kFieldLoadKindSize =
5799 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5800 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005801 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005802 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005803
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005804 static bool HasStringReference(LoadKind load_kind) {
5805 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5806 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005807 load_kind == LoadKind::kBssEntry ||
Nicolas Geoffrayac3ebc32016-10-05 13:13:50 +01005808 load_kind == LoadKind::kDexCacheViaMethod ||
5809 load_kind == LoadKind::kJitTableAddress;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005810 }
5811
5812 static bool HasAddress(LoadKind load_kind) {
Vladimir Marko54d6a202016-11-09 12:46:38 +00005813 return load_kind == LoadKind::kBootImageAddress;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005814 }
5815
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005816 void SetLoadKindInternal(LoadKind load_kind);
5817
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005818 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5819 // For other load kinds it's empty or possibly some architecture-specific instruction
5820 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005821 HUserRecord<HInstruction*> special_input_;
5822
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005823 // String index serves also as the hash code and it's also needed for slow-paths,
5824 // so it must not be overwritten with other load data.
5825 uint32_t string_index_;
5826
5827 union {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005828 const DexFile* dex_file_; // For string reference.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005829 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5830 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005831
5832 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5833};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005834std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5835
5836// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5837inline const DexFile& HLoadString::GetDexFile() const {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005838 DCHECK(HasStringReference(GetLoadKind())) << GetLoadKind();
5839 return *load_data_.dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005840}
5841
5842// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5843inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005844 // The special input is used for PC-relative loads on some architectures,
5845 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005846 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005847 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005848 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5849 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005850 // HLoadString::GetInputRecords() returns an empty array at this point,
5851 // so use the GetInputRecords() from the base class to set the input record.
5852 DCHECK(special_input_.GetInstruction() == nullptr);
5853 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005854 special_input->AddUseAt(this, 0);
5855}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005856
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005857/**
5858 * Performs an initialization check on its Class object input.
5859 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005860class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005861 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005862 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005863 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005864 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005865 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5866 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005867 SetRawInputAt(0, constant);
5868 }
5869
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005870 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005871 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005872 return true;
5873 }
5874
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005875 bool NeedsEnvironment() const OVERRIDE {
5876 // May call runtime to initialize the class.
5877 return true;
5878 }
5879
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005880 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005881
5882 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5883
5884 DECLARE_INSTRUCTION(ClinitCheck);
5885
5886 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005887 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5888};
5889
Vladimir Markofcb503c2016-05-18 12:48:17 +01005890class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005891 public:
5892 HStaticFieldGet(HInstruction* cls,
5893 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005894 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005895 bool is_volatile,
5896 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005897 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005898 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005899 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005900 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005901 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005902 field_info_(field_offset,
5903 field_type,
5904 is_volatile,
5905 field_idx,
5906 declaring_class_def_index,
5907 dex_file,
5908 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005909 SetRawInputAt(0, cls);
5910 }
5911
Calin Juravle52c48962014-12-16 17:02:57 +00005912
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005913 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005914
Vladimir Marko372f10e2016-05-17 16:30:10 +01005915 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5916 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005917 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005918 }
5919
5920 size_t ComputeHashCode() const OVERRIDE {
5921 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5922 }
5923
Calin Juravle52c48962014-12-16 17:02:57 +00005924 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005925 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5926 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005927 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005928
5929 DECLARE_INSTRUCTION(StaticFieldGet);
5930
5931 private:
5932 const FieldInfo field_info_;
5933
5934 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5935};
5936
Vladimir Markofcb503c2016-05-18 12:48:17 +01005937class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005938 public:
5939 HStaticFieldSet(HInstruction* cls,
5940 HInstruction* value,
5941 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005942 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005943 bool is_volatile,
5944 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005945 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005946 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005947 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005948 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005949 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005950 field_info_(field_offset,
5951 field_type,
5952 is_volatile,
5953 field_idx,
5954 declaring_class_def_index,
5955 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005956 dex_cache) {
5957 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005958 SetRawInputAt(0, cls);
5959 SetRawInputAt(1, value);
5960 }
5961
Calin Juravle52c48962014-12-16 17:02:57 +00005962 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005963 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5964 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005965 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005966
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005967 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005968 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5969 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005970
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005971 DECLARE_INSTRUCTION(StaticFieldSet);
5972
5973 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005974 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5975 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5976 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5977 "Too many packed fields.");
5978
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005979 const FieldInfo field_info_;
5980
5981 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5982};
5983
Vladimir Markofcb503c2016-05-18 12:48:17 +01005984class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005985 public:
5986 HUnresolvedInstanceFieldGet(HInstruction* obj,
5987 Primitive::Type field_type,
5988 uint32_t field_index,
5989 uint32_t dex_pc)
5990 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5991 field_index_(field_index) {
5992 SetRawInputAt(0, obj);
5993 }
5994
5995 bool NeedsEnvironment() const OVERRIDE { return true; }
5996 bool CanThrow() const OVERRIDE { return true; }
5997
5998 Primitive::Type GetFieldType() const { return GetType(); }
5999 uint32_t GetFieldIndex() const { return field_index_; }
6000
6001 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6002
6003 private:
6004 const uint32_t field_index_;
6005
6006 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6007};
6008
Vladimir Markofcb503c2016-05-18 12:48:17 +01006009class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006010 public:
6011 HUnresolvedInstanceFieldSet(HInstruction* obj,
6012 HInstruction* value,
6013 Primitive::Type field_type,
6014 uint32_t field_index,
6015 uint32_t dex_pc)
6016 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006017 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006018 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006019 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006020 SetRawInputAt(0, obj);
6021 SetRawInputAt(1, value);
6022 }
6023
6024 bool NeedsEnvironment() const OVERRIDE { return true; }
6025 bool CanThrow() const OVERRIDE { return true; }
6026
Vladimir Markoa1de9182016-02-25 11:37:38 +00006027 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006028 uint32_t GetFieldIndex() const { return field_index_; }
6029
6030 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6031
6032 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006033 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6034 static constexpr size_t kFieldFieldTypeSize =
6035 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6036 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6037 kFieldFieldType + kFieldFieldTypeSize;
6038 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6039 "Too many packed fields.");
6040 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6041
Calin Juravlee460d1d2015-09-29 04:52:17 +01006042 const uint32_t field_index_;
6043
6044 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6045};
6046
Vladimir Markofcb503c2016-05-18 12:48:17 +01006047class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006048 public:
6049 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6050 uint32_t field_index,
6051 uint32_t dex_pc)
6052 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6053 field_index_(field_index) {
6054 }
6055
6056 bool NeedsEnvironment() const OVERRIDE { return true; }
6057 bool CanThrow() const OVERRIDE { return true; }
6058
6059 Primitive::Type GetFieldType() const { return GetType(); }
6060 uint32_t GetFieldIndex() const { return field_index_; }
6061
6062 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6063
6064 private:
6065 const uint32_t field_index_;
6066
6067 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6068};
6069
Vladimir Markofcb503c2016-05-18 12:48:17 +01006070class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006071 public:
6072 HUnresolvedStaticFieldSet(HInstruction* value,
6073 Primitive::Type field_type,
6074 uint32_t field_index,
6075 uint32_t dex_pc)
6076 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006077 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006078 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006079 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006080 SetRawInputAt(0, value);
6081 }
6082
6083 bool NeedsEnvironment() const OVERRIDE { return true; }
6084 bool CanThrow() const OVERRIDE { return true; }
6085
Vladimir Markoa1de9182016-02-25 11:37:38 +00006086 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006087 uint32_t GetFieldIndex() const { return field_index_; }
6088
6089 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6090
6091 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006092 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6093 static constexpr size_t kFieldFieldTypeSize =
6094 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6095 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6096 kFieldFieldType + kFieldFieldTypeSize;
6097 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6098 "Too many packed fields.");
6099 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6100
Calin Juravlee460d1d2015-09-29 04:52:17 +01006101 const uint32_t field_index_;
6102
6103 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6104};
6105
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006106// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006107class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006108 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006109 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6110 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006111
David Brazdilbbd733e2015-08-18 17:48:17 +01006112 bool CanBeNull() const OVERRIDE { return false; }
6113
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006114 DECLARE_INSTRUCTION(LoadException);
6115
6116 private:
6117 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6118};
6119
David Brazdilcb1c0552015-08-04 16:22:25 +01006120// Implicit part of move-exception which clears thread-local exception storage.
6121// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006122class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006123 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006124 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6125 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006126
6127 DECLARE_INSTRUCTION(ClearException);
6128
6129 private:
6130 DISALLOW_COPY_AND_ASSIGN(HClearException);
6131};
6132
Vladimir Markofcb503c2016-05-18 12:48:17 +01006133class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006134 public:
6135 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006136 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006137 SetRawInputAt(0, exception);
6138 }
6139
6140 bool IsControlFlow() const OVERRIDE { return true; }
6141
6142 bool NeedsEnvironment() const OVERRIDE { return true; }
6143
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006144 bool CanThrow() const OVERRIDE { return true; }
6145
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006146
6147 DECLARE_INSTRUCTION(Throw);
6148
6149 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006150 DISALLOW_COPY_AND_ASSIGN(HThrow);
6151};
6152
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006153/**
6154 * Implementation strategies for the code generator of a HInstanceOf
6155 * or `HCheckCast`.
6156 */
6157enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006158 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006159 kExactCheck, // Can do a single class compare.
6160 kClassHierarchyCheck, // Can just walk the super class chain.
6161 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6162 kInterfaceCheck, // No optimization yet when checking against an interface.
6163 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006164 kArrayCheck, // No optimization yet when checking against a generic array.
6165 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006166};
6167
Roland Levillain86503782016-02-11 19:07:30 +00006168std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6169
Vladimir Markofcb503c2016-05-18 12:48:17 +01006170class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006171 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006172 HInstanceOf(HInstruction* object,
6173 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006174 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006175 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006176 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006177 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006178 dex_pc) {
6179 SetPackedField<TypeCheckKindField>(check_kind);
6180 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006181 SetRawInputAt(0, object);
6182 SetRawInputAt(1, constant);
6183 }
6184
6185 bool CanBeMoved() const OVERRIDE { return true; }
6186
Vladimir Marko372f10e2016-05-17 16:30:10 +01006187 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006188 return true;
6189 }
6190
6191 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006192 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006193 }
6194
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006195 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006196 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6197 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6198 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6199 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006200
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006201 static bool CanCallRuntime(TypeCheckKind check_kind) {
6202 // Mips currently does runtime calls for any other checks.
6203 return check_kind != TypeCheckKind::kExactCheck;
6204 }
6205
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006206 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006207 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006208 }
6209
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006210 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006211
6212 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006213 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6214 static constexpr size_t kFieldTypeCheckKindSize =
6215 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6216 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6217 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6218 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6219 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006220
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006221 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6222};
6223
Vladimir Markofcb503c2016-05-18 12:48:17 +01006224class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006225 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006226 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006227 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006228 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6229 SetPackedFlag<kFlagUpperCanBeNull>(true);
6230 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006231 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006232 SetRawInputAt(0, input);
6233 }
6234
David Brazdilf5552582015-12-27 13:36:12 +00006235 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006236 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006237 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006238 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006239
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006240 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006241 DCHECK(GetUpperCanBeNull() || !can_be_null);
6242 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006243 }
6244
Vladimir Markoa1de9182016-02-25 11:37:38 +00006245 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006246
Calin Juravleb1498f62015-02-16 13:13:29 +00006247 DECLARE_INSTRUCTION(BoundType);
6248
6249 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006250 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6251 // is false then CanBeNull() cannot be true).
6252 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6253 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6254 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6255 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6256
Calin Juravleb1498f62015-02-16 13:13:29 +00006257 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006258 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6259 // It is used to bound the type in cases like:
6260 // if (x instanceof ClassX) {
6261 // // uper_bound_ will be ClassX
6262 // }
David Brazdilf5552582015-12-27 13:36:12 +00006263 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006264
6265 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6266};
6267
Vladimir Markofcb503c2016-05-18 12:48:17 +01006268class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006269 public:
6270 HCheckCast(HInstruction* object,
6271 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006272 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006273 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006274 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6275 SetPackedField<TypeCheckKindField>(check_kind);
6276 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006277 SetRawInputAt(0, object);
6278 SetRawInputAt(1, constant);
6279 }
6280
6281 bool CanBeMoved() const OVERRIDE { return true; }
6282
Vladimir Marko372f10e2016-05-17 16:30:10 +01006283 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006284 return true;
6285 }
6286
6287 bool NeedsEnvironment() const OVERRIDE {
6288 // Instruction may throw a CheckCastError.
6289 return true;
6290 }
6291
6292 bool CanThrow() const OVERRIDE { return true; }
6293
Vladimir Markoa1de9182016-02-25 11:37:38 +00006294 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6295 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6296 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6297 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006298
6299 DECLARE_INSTRUCTION(CheckCast);
6300
6301 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006302 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6303 static constexpr size_t kFieldTypeCheckKindSize =
6304 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6305 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6306 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6307 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6308 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006309
6310 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006311};
6312
Andreas Gampe26de38b2016-07-27 17:53:11 -07006313/**
6314 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6315 * @details We define the combined barrier types that are actually required
6316 * by the Java Memory Model, rather than using exactly the terminology from
6317 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6318 * primitives. Note that the JSR-133 cookbook generally does not deal with
6319 * store atomicity issues, and the recipes there are not always entirely sufficient.
6320 * The current recipe is as follows:
6321 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6322 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6323 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6324 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6325 * class has final fields.
6326 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6327 * store-to-memory instructions. Only generated together with non-temporal stores.
6328 */
6329enum MemBarrierKind {
6330 kAnyStore,
6331 kLoadAny,
6332 kStoreStore,
6333 kAnyAny,
6334 kNTStoreStore,
6335 kLastBarrierKind = kNTStoreStore
6336};
6337std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6338
Vladimir Markofcb503c2016-05-18 12:48:17 +01006339class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006340 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006341 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006342 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006343 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6344 SetPackedField<BarrierKindField>(barrier_kind);
6345 }
Calin Juravle27df7582015-04-17 19:12:31 +01006346
Vladimir Markoa1de9182016-02-25 11:37:38 +00006347 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006348
6349 DECLARE_INSTRUCTION(MemoryBarrier);
6350
6351 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006352 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6353 static constexpr size_t kFieldBarrierKindSize =
6354 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6355 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6356 kFieldBarrierKind + kFieldBarrierKindSize;
6357 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6358 "Too many packed fields.");
6359 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006360
6361 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6362};
6363
Vladimir Markofcb503c2016-05-18 12:48:17 +01006364class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006365 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006366 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006367 kEnter,
6368 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006369 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006370 };
6371
6372 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006373 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006374 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6375 dex_pc) {
6376 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006377 SetRawInputAt(0, object);
6378 }
6379
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006380 // Instruction may go into runtime, so we need an environment.
6381 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006382
6383 bool CanThrow() const OVERRIDE {
6384 // Verifier guarantees that monitor-exit cannot throw.
6385 // This is important because it allows the HGraphBuilder to remove
6386 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6387 return IsEnter();
6388 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006389
Vladimir Markoa1de9182016-02-25 11:37:38 +00006390 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6391 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006392
6393 DECLARE_INSTRUCTION(MonitorOperation);
6394
Calin Juravle52c48962014-12-16 17:02:57 +00006395 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006396 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6397 static constexpr size_t kFieldOperationKindSize =
6398 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6399 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6400 kFieldOperationKind + kFieldOperationKindSize;
6401 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6402 "Too many packed fields.");
6403 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006404
6405 private:
6406 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6407};
6408
Vladimir Markofcb503c2016-05-18 12:48:17 +01006409class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006410 public:
6411 HSelect(HInstruction* condition,
6412 HInstruction* true_value,
6413 HInstruction* false_value,
6414 uint32_t dex_pc)
6415 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6416 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6417
6418 // First input must be `true_value` or `false_value` to allow codegens to
6419 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6420 // that architectures which implement HSelect as a conditional move also
6421 // will not need to invert the condition.
6422 SetRawInputAt(0, false_value);
6423 SetRawInputAt(1, true_value);
6424 SetRawInputAt(2, condition);
6425 }
6426
6427 HInstruction* GetFalseValue() const { return InputAt(0); }
6428 HInstruction* GetTrueValue() const { return InputAt(1); }
6429 HInstruction* GetCondition() const { return InputAt(2); }
6430
6431 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006432 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6433 return true;
6434 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006435
6436 bool CanBeNull() const OVERRIDE {
6437 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6438 }
6439
6440 DECLARE_INSTRUCTION(Select);
6441
6442 private:
6443 DISALLOW_COPY_AND_ASSIGN(HSelect);
6444};
6445
Vladimir Markof9f64412015-09-02 14:05:49 +01006446class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006447 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006448 MoveOperands(Location source,
6449 Location destination,
6450 Primitive::Type type,
6451 HInstruction* instruction)
6452 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006453
6454 Location GetSource() const { return source_; }
6455 Location GetDestination() const { return destination_; }
6456
6457 void SetSource(Location value) { source_ = value; }
6458 void SetDestination(Location value) { destination_ = value; }
6459
6460 // The parallel move resolver marks moves as "in-progress" by clearing the
6461 // destination (but not the source).
6462 Location MarkPending() {
6463 DCHECK(!IsPending());
6464 Location dest = destination_;
6465 destination_ = Location::NoLocation();
6466 return dest;
6467 }
6468
6469 void ClearPending(Location dest) {
6470 DCHECK(IsPending());
6471 destination_ = dest;
6472 }
6473
6474 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006475 DCHECK(source_.IsValid() || destination_.IsInvalid());
6476 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006477 }
6478
6479 // True if this blocks a move from the given location.
6480 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006481 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006482 }
6483
6484 // A move is redundant if it's been eliminated, if its source and
6485 // destination are the same, or if its destination is unneeded.
6486 bool IsRedundant() const {
6487 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6488 }
6489
6490 // We clear both operands to indicate move that's been eliminated.
6491 void Eliminate() {
6492 source_ = destination_ = Location::NoLocation();
6493 }
6494
6495 bool IsEliminated() const {
6496 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6497 return source_.IsInvalid();
6498 }
6499
Alexey Frunze4dda3372015-06-01 18:31:49 -07006500 Primitive::Type GetType() const { return type_; }
6501
Nicolas Geoffray90218252015-04-15 11:56:51 +01006502 bool Is64BitMove() const {
6503 return Primitive::Is64BitType(type_);
6504 }
6505
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006506 HInstruction* GetInstruction() const { return instruction_; }
6507
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006508 private:
6509 Location source_;
6510 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006511 // The type this move is for.
6512 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006513 // The instruction this move is assocatied with. Null when this move is
6514 // for moving an input in the expected locations of user (including a phi user).
6515 // This is only used in debug mode, to ensure we do not connect interval siblings
6516 // in the same parallel move.
6517 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006518};
6519
Roland Levillainc9285912015-12-18 10:38:42 +00006520std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6521
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006522static constexpr size_t kDefaultNumberOfMoves = 4;
6523
Vladimir Markofcb503c2016-05-18 12:48:17 +01006524class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006525 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006526 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006527 : HTemplateInstruction(SideEffects::None(), dex_pc),
6528 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6529 moves_.reserve(kDefaultNumberOfMoves);
6530 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006531
Nicolas Geoffray90218252015-04-15 11:56:51 +01006532 void AddMove(Location source,
6533 Location destination,
6534 Primitive::Type type,
6535 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006536 DCHECK(source.IsValid());
6537 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006538 if (kIsDebugBuild) {
6539 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006540 for (const MoveOperands& move : moves_) {
6541 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006542 // Special case the situation where the move is for the spill slot
6543 // of the instruction.
6544 if ((GetPrevious() == instruction)
6545 || ((GetPrevious() == nullptr)
6546 && instruction->IsPhi()
6547 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006548 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006549 << "Doing parallel moves for the same instruction.";
6550 } else {
6551 DCHECK(false) << "Doing parallel moves for the same instruction.";
6552 }
6553 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006554 }
6555 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006556 for (const MoveOperands& move : moves_) {
6557 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006558 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006559 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006560 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006561 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006562 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006563 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006564 }
6565
Vladimir Marko225b6462015-09-28 12:17:40 +01006566 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006567 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006568 }
6569
Vladimir Marko225b6462015-09-28 12:17:40 +01006570 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006571
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006572 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006573
6574 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006575 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006576
6577 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6578};
6579
Mark Mendell0616ae02015-04-17 12:49:27 -04006580} // namespace art
6581
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006582#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6583#include "nodes_shared.h"
6584#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006585#ifdef ART_ENABLE_CODEGEN_arm
6586#include "nodes_arm.h"
6587#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006588#ifdef ART_ENABLE_CODEGEN_arm64
6589#include "nodes_arm64.h"
6590#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006591#ifdef ART_ENABLE_CODEGEN_mips
6592#include "nodes_mips.h"
6593#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006594#ifdef ART_ENABLE_CODEGEN_x86
6595#include "nodes_x86.h"
6596#endif
6597
6598namespace art {
6599
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006600class HGraphVisitor : public ValueObject {
6601 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006602 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6603 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006604
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006605 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006606 virtual void VisitBasicBlock(HBasicBlock* block);
6607
Roland Levillain633021e2014-10-01 14:12:25 +01006608 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006609 void VisitInsertionOrder();
6610
Roland Levillain633021e2014-10-01 14:12:25 +01006611 // Visit the graph following dominator tree reverse post-order.
6612 void VisitReversePostOrder();
6613
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006614 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006615
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006616 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006617#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006618 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6619
6620 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6621
6622#undef DECLARE_VISIT_INSTRUCTION
6623
6624 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006625 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006626
6627 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6628};
6629
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006630class HGraphDelegateVisitor : public HGraphVisitor {
6631 public:
6632 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6633 virtual ~HGraphDelegateVisitor() {}
6634
6635 // Visit functions that delegate to to super class.
6636#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006637 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006638
6639 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6640
6641#undef DECLARE_VISIT_INSTRUCTION
6642
6643 private:
6644 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6645};
6646
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006647// Iterator over the blocks that art part of the loop. Includes blocks part
6648// of an inner loop. The order in which the blocks are iterated is on their
6649// block id.
6650class HBlocksInLoopIterator : public ValueObject {
6651 public:
6652 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6653 : blocks_in_loop_(info.GetBlocks()),
6654 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6655 index_(0) {
6656 if (!blocks_in_loop_.IsBitSet(index_)) {
6657 Advance();
6658 }
6659 }
6660
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006661 bool Done() const { return index_ == blocks_.size(); }
6662 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006663 void Advance() {
6664 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006665 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006666 if (blocks_in_loop_.IsBitSet(index_)) {
6667 break;
6668 }
6669 }
6670 }
6671
6672 private:
6673 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006674 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006675 size_t index_;
6676
6677 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6678};
6679
Mingyao Yang3584bce2015-05-19 16:01:59 -07006680// Iterator over the blocks that art part of the loop. Includes blocks part
6681// of an inner loop. The order in which the blocks are iterated is reverse
6682// post order.
6683class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6684 public:
6685 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6686 : blocks_in_loop_(info.GetBlocks()),
6687 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6688 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006689 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006690 Advance();
6691 }
6692 }
6693
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006694 bool Done() const { return index_ == blocks_.size(); }
6695 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006696 void Advance() {
6697 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006698 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6699 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006700 break;
6701 }
6702 }
6703 }
6704
6705 private:
6706 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006707 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006708 size_t index_;
6709
6710 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6711};
6712
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006713inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006714 if (constant->IsIntConstant()) {
6715 return constant->AsIntConstant()->GetValue();
6716 } else if (constant->IsLongConstant()) {
6717 return constant->AsLongConstant()->GetValue();
6718 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006719 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006720 return 0;
6721 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006722}
6723
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006724#define INSTRUCTION_TYPE_CHECK(type, super) \
6725 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6726 inline const H##type* HInstruction::As##type() const { \
6727 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6728 } \
6729 inline H##type* HInstruction::As##type() { \
6730 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6731 }
6732
6733 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6734#undef INSTRUCTION_TYPE_CHECK
6735
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006736// Create space in `blocks` for adding `number_of_new_blocks` entries
6737// starting at location `at`. Blocks after `at` are moved accordingly.
6738inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6739 size_t number_of_new_blocks,
6740 size_t after) {
6741 DCHECK_LT(after, blocks->size());
6742 size_t old_size = blocks->size();
6743 size_t new_size = old_size + number_of_new_blocks;
6744 blocks->resize(new_size);
6745 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6746}
6747
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006748} // namespace art
6749
6750#endif // ART_COMPILER_OPTIMIZING_NODES_H_