blob: 6cd51ccaabe4052b239c6feb2a9f72e6d9e11208 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
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#include "code_generator_arm64.h"
18
Vladimir Markof4f2daa2017-03-20 18:26:59 +000019#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000020#include "arch/arm64/instruction_set_features_arm64.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000021#include "art_method-inl.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070022#include "base/bit_utils.h"
23#include "base/bit_utils_iterator.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010024#include "class_table.h"
Zheng Xuc6667102015-05-15 16:08:45 +080025#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000026#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010027#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080028#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010029#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010030#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070031#include "heap_poisoning.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080032#include "intrinsics.h"
33#include "intrinsics_arm64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010034#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010036#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070037#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000038#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010039#include "thread.h"
40#include "utils/arm64/assembler_arm64.h"
41#include "utils/assembler.h"
42#include "utils/stack_checks.h"
43
Scott Wakeling97c72b72016-06-24 16:19:36 +010044using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000045using vixl::ExactAssemblyScope;
46using vixl::CodeBufferCheckScope;
47using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010048
49#ifdef __
50#error "ARM64 Codegen VIXL macro-assembler macro already defined."
51#endif
52
Vladimir Marko0a516052019-10-14 13:00:44 +000053namespace art {
Alexandre Rames5319def2014-10-23 10:03:10 +010054
Roland Levillain22ccc3a2015-11-24 13:10:05 +000055template<class MirrorType>
56class GcRoot;
57
Alexandre Rames5319def2014-10-23 10:03:10 +010058namespace arm64 {
59
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::ARM64EncodableConstantOrRegister;
61using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080062using helpers::CPURegisterFrom;
63using helpers::DRegisterFrom;
64using helpers::FPRegisterFrom;
65using helpers::HeapOperand;
66using helpers::HeapOperandFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010067using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080068using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010070using helpers::InputRegisterAt;
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +010071using helpers::Int64FromLocation;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010072using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080073using helpers::LocationFrom;
74using helpers::OperandFromMemOperand;
75using helpers::OutputCPURegister;
76using helpers::OutputFPRegister;
77using helpers::OutputRegister;
78using helpers::RegisterFrom;
79using helpers::StackOperandFrom;
80using helpers::VIXLRegCodeFromART;
81using helpers::WRegisterFrom;
82using helpers::XRegisterFrom;
83
Vladimir Markof3e0ee22015-12-17 15:23:13 +000084// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080085// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
86// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000087static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010088
Vladimir Markof4f2daa2017-03-20 18:26:59 +000089// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
90// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
Vladimir Marko008e09f32018-08-06 15:42:43 +010091// For the Baker read barrier implementation using link-time generated thunks we need to split
Vladimir Markof4f2daa2017-03-20 18:26:59 +000092// the offset explicitly.
93constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
94
Vladimir Markof91fc122020-05-13 09:21:00 +010095ALWAYS_INLINE static inline bool UseJitCompilation() {
96 Runtime* runtime = Runtime::Current();
97 // Note: There may be no Runtime for gtests; gtests use debug builds.
98 return (!kIsDebugBuild || runtime != nullptr) && runtime->UseJitCompilation();
99}
100
Alexandre Rames5319def2014-10-23 10:03:10 +0100101inline Condition ARM64Condition(IfCondition cond) {
102 switch (cond) {
103 case kCondEQ: return eq;
104 case kCondNE: return ne;
105 case kCondLT: return lt;
106 case kCondLE: return le;
107 case kCondGT: return gt;
108 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700109 case kCondB: return lo;
110 case kCondBE: return ls;
111 case kCondA: return hi;
112 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100113 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000114 LOG(FATAL) << "Unreachable";
115 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100116}
117
Vladimir Markod6e069b2016-01-18 11:11:01 +0000118inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
119 // The ARM64 condition codes can express all the necessary branches, see the
120 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
121 // There is no dex instruction or HIR that would need the missing conditions
122 // "equal or unordered" or "not equal".
123 switch (cond) {
124 case kCondEQ: return eq;
125 case kCondNE: return ne /* unordered */;
126 case kCondLT: return gt_bias ? cc : lt /* unordered */;
127 case kCondLE: return gt_bias ? ls : le /* unordered */;
128 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
129 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
130 default:
131 LOG(FATAL) << "UNREACHABLE";
132 UNREACHABLE();
133 }
134}
135
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100136Location ARM64ReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000137 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
138 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
139 // but we use the exact registers for clarity.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100140 if (return_type == DataType::Type::kFloat32) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000141 return LocationFrom(s0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100142 } else if (return_type == DataType::Type::kFloat64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000143 return LocationFrom(d0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100144 } else if (return_type == DataType::Type::kInt64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000145 return LocationFrom(x0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100146 } else if (return_type == DataType::Type::kVoid) {
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100147 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000148 } else {
149 return LocationFrom(w0);
150 }
151}
152
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100153Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000154 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100155}
156
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100157static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
158 InvokeRuntimeCallingConvention calling_convention;
159 RegisterSet caller_saves = RegisterSet::Empty();
160 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
161 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
162 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
163 DataType::Type::kReference).GetCode());
164 return caller_saves;
165}
166
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100167// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
168#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700169#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100170
Zheng Xuda403092015-04-24 17:35:39 +0800171// Calculate memory accessing operand for save/restore live registers.
172static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100173 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800174 int64_t spill_offset,
175 bool is_save) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800176 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
177 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100178 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800179 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100180 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800181 codegen->GetNumberOfFloatingPointRegisters()));
182
Vladimir Marko804b03f2016-09-14 16:26:36 +0100183 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serovc8150b52019-07-31 18:28:00 +0100184 const unsigned v_reg_size_in_bits = codegen->GetSlowPathFPWidth() * 8;
Artem Serov1a719e42019-07-18 14:24:55 +0100185 DCHECK_LE(codegen->GetSIMDRegisterWidth(), kQRegSizeInBytes);
Artem Serovc8150b52019-07-31 18:28:00 +0100186 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size_in_bits, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800187
188 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
189 UseScratchRegisterScope temps(masm);
190
191 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100192 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
193 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800194 int64_t reg_size = kXRegSizeInBytes;
195 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
196 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100197 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800198 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
199 // If the offset does not fit in the instruction's immediate field, use an alternate register
200 // to compute the base address(float point registers spill base address).
201 Register new_base = temps.AcquireSameSizeAs(base);
202 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
203 base = new_base;
204 spill_offset = -core_spill_size;
205 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
206 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
207 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
208 }
209
210 if (is_save) {
211 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
212 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
213 } else {
214 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
215 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
216 }
217}
218
219void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800220 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Andreas Gampe3db70682018-12-26 15:12:03 -0800221 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100222 for (uint32_t i : LowToHighBits(core_spills)) {
223 // If the register holds an object, update the stack mask.
224 if (locations->RegisterContainsObject(i)) {
225 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800226 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100227 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
228 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
229 saved_core_stack_offsets_[i] = stack_offset;
230 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800231 }
232
Artem Serovc8150b52019-07-31 18:28:00 +0100233 const size_t fp_reg_size = codegen->GetSlowPathFPWidth();
Andreas Gampe3db70682018-12-26 15:12:03 -0800234 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100235 for (uint32_t i : LowToHighBits(fp_spills)) {
236 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
237 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
238 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serov9df37b92019-07-23 16:41:54 +0100239 stack_offset += fp_reg_size;
Zheng Xuda403092015-04-24 17:35:39 +0800240 }
241
Vladimir Marko804b03f2016-09-14 16:26:36 +0100242 SaveRestoreLiveRegistersHelper(codegen,
243 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800244 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ true);
Zheng Xuda403092015-04-24 17:35:39 +0800245}
246
247void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100248 SaveRestoreLiveRegistersHelper(codegen,
249 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800250 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ false);
Zheng Xuda403092015-04-24 17:35:39 +0800251}
252
Alexandre Rames5319def2014-10-23 10:03:10 +0100253class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
254 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000255 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100256
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100257 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100258 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000259 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100260
Alexandre Rames5319def2014-10-23 10:03:10 +0100261 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000262 if (instruction_->CanThrowIntoCatchBlock()) {
263 // Live registers will be restored in the catch block if caught.
264 SaveLiveRegisters(codegen, instruction_->GetLocations());
265 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000266 // We're moving two locations to locations that could overlap, so we need a parallel
267 // move resolver.
268 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100269 codegen->EmitParallelMoves(locations->InAt(0),
270 LocationFrom(calling_convention.GetRegisterAt(0)),
271 DataType::Type::kInt32,
272 locations->InAt(1),
273 LocationFrom(calling_convention.GetRegisterAt(1)),
274 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000275 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
276 ? kQuickThrowStringBounds
277 : kQuickThrowArrayBounds;
278 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100279 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800280 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100281 }
282
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100283 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100284
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100285 const char* GetDescription() const override { return "BoundsCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100286
Alexandre Rames5319def2014-10-23 10:03:10 +0100287 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100288 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
289};
290
Alexandre Rames67555f72014-11-18 10:55:16 +0000291class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
292 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000293 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000294
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100295 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000296 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
297 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000298 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800299 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000300 }
301
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100302 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100303
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100304 const char* GetDescription() const override { return "DivZeroCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100305
Alexandre Rames67555f72014-11-18 10:55:16 +0000306 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000307 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
308};
309
310class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
311 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100312 LoadClassSlowPathARM64(HLoadClass* cls, HInstruction* at)
313 : SlowPathCodeARM64(at), cls_(cls) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000314 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100315 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexandre Rames67555f72014-11-18 10:55:16 +0000316 }
317
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100318 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000319 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000320 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100321 const uint32_t dex_pc = instruction_->GetDexPc();
322 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
323 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Alexandre Rames67555f72014-11-18 10:55:16 +0000324
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100325 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames67555f72014-11-18 10:55:16 +0000326 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000327 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000328
Vladimir Markof3c52b42017-11-17 17:32:12 +0000329 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100330 if (must_resolve_type) {
331 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm64_codegen->GetGraph()->GetDexFile()));
332 dex::TypeIndex type_index = cls_->GetTypeIndex();
333 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Vladimir Marko9d479252018-07-24 11:35:20 +0100334 arm64_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
335 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100336 // If we also must_do_clinit, the resolved type is now in the correct register.
337 } else {
338 DCHECK(must_do_clinit);
339 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
340 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)),
341 source,
342 cls_->GetType());
343 }
344 if (must_do_clinit) {
345 arm64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
346 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800347 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000348
349 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000350 if (out.IsValid()) {
351 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100352 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000353 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000354 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000355 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 __ B(GetExitLabel());
357 }
358
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100359 const char* GetDescription() const override { return "LoadClassSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100360
Alexandre Rames67555f72014-11-18 10:55:16 +0000361 private:
362 // The class this slow path will load.
363 HLoadClass* const cls_;
364
Alexandre Rames67555f72014-11-18 10:55:16 +0000365 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
366};
367
Vladimir Markoaad75c62016-10-03 08:46:48 +0000368class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
369 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000370 explicit LoadStringSlowPathARM64(HLoadString* instruction)
371 : SlowPathCodeARM64(instruction) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000372
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100373 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000374 LocationSummary* locations = instruction_->GetLocations();
375 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
376 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
377
378 __ Bind(GetEntryLabel());
379 SaveLiveRegisters(codegen, locations);
380
Vladimir Markof3c52b42017-11-17 17:32:12 +0000381 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000382 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
383 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000384 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
385 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100386 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000387 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
388
389 RestoreLiveRegisters(codegen, locations);
390
Vladimir Markoaad75c62016-10-03 08:46:48 +0000391 __ B(GetExitLabel());
392 }
393
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100394 const char* GetDescription() const override { return "LoadStringSlowPathARM64"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000395
396 private:
397 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
398};
399
Alexandre Rames5319def2014-10-23 10:03:10 +0100400class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
401 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000402 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100403
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100404 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000405 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100406 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000407 if (instruction_->CanThrowIntoCatchBlock()) {
408 // Live registers will be restored in the catch block if caught.
409 SaveLiveRegisters(codegen, instruction_->GetLocations());
410 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000411 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
412 instruction_,
413 instruction_->GetDexPc(),
414 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800415 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100416 }
417
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100418 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100419
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100420 const char* GetDescription() const override { return "NullCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100421
Alexandre Rames5319def2014-10-23 10:03:10 +0100422 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100423 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
424};
425
426class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
427 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100428 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000429 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100430
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100431 void EmitNativeCode(CodeGenerator* codegen) override {
Artem Serov7957d952017-04-04 15:44:09 +0100432 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000433 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100434 __ Bind(GetEntryLabel());
Artem Serov1a719e42019-07-18 14:24:55 +0100435 SaveLiveRegisters(codegen, locations); // Only saves live vector regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000436 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800437 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov1a719e42019-07-18 14:24:55 +0100438 RestoreLiveRegisters(codegen, locations); // Only restores live vector regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000439 if (successor_ == nullptr) {
440 __ B(GetReturnLabel());
441 } else {
442 __ B(arm64_codegen->GetLabelOf(successor_));
443 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100444 }
445
Scott Wakeling97c72b72016-06-24 16:19:36 +0100446 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100447 DCHECK(successor_ == nullptr);
448 return &return_label_;
449 }
450
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100451 HBasicBlock* GetSuccessor() const {
452 return successor_;
453 }
454
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100455 const char* GetDescription() const override { return "SuspendCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100456
Alexandre Rames5319def2014-10-23 10:03:10 +0100457 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100458 // If not null, the block to branch to after the suspend check.
459 HBasicBlock* const successor_;
460
461 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100462 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100463
464 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
465};
466
Alexandre Rames67555f72014-11-18 10:55:16 +0000467class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
468 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000469 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000470 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000471
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100472 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000473 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800474
Alexandre Rames3e69f162014-12-10 10:36:50 +0000475 DCHECK(instruction_->IsCheckCast()
476 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
477 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100478 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000479
Alexandre Rames67555f72014-11-18 10:55:16 +0000480 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000481
Vladimir Marko87584542017-12-12 17:47:52 +0000482 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000483 SaveLiveRegisters(codegen, locations);
484 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000485
486 // We're moving two locations to locations that could overlap, so we need a parallel
487 // move resolver.
488 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800489 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800490 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100491 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800492 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800493 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100494 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000495 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000496 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800497 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100498 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000499 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
500 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
501 } else {
502 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800503 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
504 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000505 }
506
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000507 if (!is_fatal_) {
508 RestoreLiveRegisters(codegen, locations);
509 __ B(GetExitLabel());
510 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000511 }
512
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100513 const char* GetDescription() const override { return "TypeCheckSlowPathARM64"; }
514 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100515
Alexandre Rames67555f72014-11-18 10:55:16 +0000516 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000517 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000518
Alexandre Rames67555f72014-11-18 10:55:16 +0000519 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
520};
521
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700522class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
523 public:
Aart Bik42249c32016-01-07 15:33:50 -0800524 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000525 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700526
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100527 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bik42249c32016-01-07 15:33:50 -0800528 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700529 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100530 LocationSummary* locations = instruction_->GetLocations();
531 SaveLiveRegisters(codegen, locations);
532 InvokeRuntimeCallingConvention calling_convention;
533 __ Mov(calling_convention.GetRegisterAt(0),
534 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000535 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100536 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700537 }
538
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100539 const char* GetDescription() const override { return "DeoptimizationSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100540
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700541 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700542 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
543};
544
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100545class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
546 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000547 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100548
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100549 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100550 LocationSummary* locations = instruction_->GetLocations();
551 __ Bind(GetEntryLabel());
552 SaveLiveRegisters(codegen, locations);
553
554 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100555 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100556 parallel_move.AddMove(
557 locations->InAt(0),
558 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100559 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100560 nullptr);
561 parallel_move.AddMove(
562 locations->InAt(1),
563 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100564 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100565 nullptr);
566 parallel_move.AddMove(
567 locations->InAt(2),
568 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100569 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100570 nullptr);
571 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
572
573 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000574 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100575 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
576 RestoreLiveRegisters(codegen, locations);
577 __ B(GetExitLabel());
578 }
579
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100580 const char* GetDescription() const override { return "ArraySetSlowPathARM64"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100581
582 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100583 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
584};
585
Zheng Xu3927c8b2015-11-18 17:46:25 +0800586void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
587 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000588 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800589
590 // We are about to use the assembler to place literals directly. Make sure we have enough
591 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000592 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
593 num_entries * sizeof(int32_t),
594 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800595
596 __ Bind(&table_start_);
597 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
598 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100599 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800600 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100601 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800602 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
603 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
604 Literal<int32_t> literal(jump_offset);
605 __ place(&literal);
606 }
607}
608
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000609// Slow path generating a read barrier for a heap reference.
610class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
611 public:
612 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
613 Location out,
614 Location ref,
615 Location obj,
616 uint32_t offset,
617 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000618 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000619 out_(out),
620 ref_(ref),
621 obj_(obj),
622 offset_(offset),
623 index_(index) {
624 DCHECK(kEmitCompilerReadBarrier);
625 // If `obj` is equal to `out` or `ref`, it means the initial object
626 // has been overwritten by (or after) the heap object reference load
627 // to be instrumented, e.g.:
628 //
629 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000630 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000631 //
632 // In that case, we have lost the information about the original
633 // object, and the emitted read barrier cannot work properly.
634 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
635 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
636 }
637
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100638 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000639 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
640 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100641 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000642 DCHECK(locations->CanCall());
643 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100644 DCHECK(instruction_->IsInstanceFieldGet() ||
645 instruction_->IsStaticFieldGet() ||
646 instruction_->IsArrayGet() ||
647 instruction_->IsInstanceOf() ||
648 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700649 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000650 << "Unexpected instruction in read barrier for heap reference slow path: "
651 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000652 // The read barrier instrumentation of object ArrayGet
653 // instructions does not support the HIntermediateAddress
654 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000655 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100656 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000657
658 __ Bind(GetEntryLabel());
659
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000660 SaveLiveRegisters(codegen, locations);
661
662 // We may have to change the index's value, but as `index_` is a
663 // constant member (like other "inputs" of this slow path),
664 // introduce a copy of it, `index`.
665 Location index = index_;
666 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100667 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000668 if (instruction_->IsArrayGet()) {
669 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100670 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000671 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
672 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
673 // We are about to change the value of `index_reg` (see the
674 // calls to vixl::MacroAssembler::Lsl and
675 // vixl::MacroAssembler::Mov below), but it has
676 // not been saved by the previous call to
677 // art::SlowPathCode::SaveLiveRegisters, as it is a
678 // callee-save register --
679 // art::SlowPathCode::SaveLiveRegisters does not consider
680 // callee-save registers, as it has been designed with the
681 // assumption that callee-save registers are supposed to be
682 // handled by the called function. So, as a callee-save
683 // register, `index_reg` _would_ eventually be saved onto
684 // the stack, but it would be too late: we would have
685 // changed its value earlier. Therefore, we manually save
686 // it here into another freely available register,
687 // `free_reg`, chosen of course among the caller-save
688 // registers (as a callee-save `free_reg` register would
689 // exhibit the same problem).
690 //
691 // Note we could have requested a temporary register from
692 // the register allocator instead; but we prefer not to, as
693 // this is a slow path, and we know we can find a
694 // caller-save register that is available.
695 Register free_reg = FindAvailableCallerSaveRegister(codegen);
696 __ Mov(free_reg.W(), index_reg);
697 index_reg = free_reg;
698 index = LocationFrom(index_reg);
699 } else {
700 // The initial register stored in `index_` has already been
701 // saved in the call to art::SlowPathCode::SaveLiveRegisters
702 // (as it is not a callee-save register), so we can freely
703 // use it.
704 }
705 // Shifting the index value contained in `index_reg` by the scale
706 // factor (2) cannot overflow in practice, as the runtime is
707 // unable to allocate object arrays with a size larger than
708 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100709 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000710 static_assert(
711 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
712 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
713 __ Add(index_reg, index_reg, Operand(offset_));
714 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100715 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
716 // intrinsics, `index_` is not shifted by a scale factor of 2
717 // (as in the case of ArrayGet), as it is actually an offset
718 // to an object field within an object.
719 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000720 DCHECK(instruction_->GetLocations()->Intrinsified());
721 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
722 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
723 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100724 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100725 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000726 }
727 }
728
729 // We're moving two or three locations to locations that could
730 // overlap, so we need a parallel move resolver.
731 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100732 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000733 parallel_move.AddMove(ref_,
734 LocationFrom(calling_convention.GetRegisterAt(0)),
735 type,
736 nullptr);
737 parallel_move.AddMove(obj_,
738 LocationFrom(calling_convention.GetRegisterAt(1)),
739 type,
740 nullptr);
741 if (index.IsValid()) {
742 parallel_move.AddMove(index,
743 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100744 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000745 nullptr);
746 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
747 } else {
748 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
749 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
750 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000751 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000752 instruction_,
753 instruction_->GetDexPc(),
754 this);
755 CheckEntrypointTypes<
756 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
757 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
758
759 RestoreLiveRegisters(codegen, locations);
760
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000761 __ B(GetExitLabel());
762 }
763
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100764 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000765
766 private:
767 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100768 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
769 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000770 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
771 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
772 return Register(VIXLRegCodeFromART(i), kXRegSize);
773 }
774 }
775 // We shall never fail to find a free caller-save register, as
776 // there are more than two core caller-save registers on ARM64
777 // (meaning it is possible to find one which is different from
778 // `ref` and `obj`).
779 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
780 LOG(FATAL) << "Could not find a free register";
781 UNREACHABLE();
782 }
783
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000784 const Location out_;
785 const Location ref_;
786 const Location obj_;
787 const uint32_t offset_;
788 // An additional location containing an index to an array.
789 // Only used for HArrayGet and the UnsafeGetObject &
790 // UnsafeGetObjectVolatile intrinsics.
791 const Location index_;
792
793 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
794};
795
796// Slow path generating a read barrier for a GC root.
797class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
798 public:
799 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000800 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000801 DCHECK(kEmitCompilerReadBarrier);
802 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000803
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100804 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000805 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100806 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000807 DCHECK(locations->CanCall());
808 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000809 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
810 << "Unexpected instruction in read barrier for GC root slow path: "
811 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000812
813 __ Bind(GetEntryLabel());
814 SaveLiveRegisters(codegen, locations);
815
816 InvokeRuntimeCallingConvention calling_convention;
817 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
818 // The argument of the ReadBarrierForRootSlow is not a managed
819 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
820 // thus we need a 64-bit move here, and we cannot use
821 //
822 // arm64_codegen->MoveLocation(
823 // LocationFrom(calling_convention.GetRegisterAt(0)),
824 // root_,
825 // type);
826 //
827 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100828 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000829 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000830 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000831 instruction_,
832 instruction_->GetDexPc(),
833 this);
834 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
835 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
836
837 RestoreLiveRegisters(codegen, locations);
838 __ B(GetExitLabel());
839 }
840
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100841 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000842
843 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000844 const Location out_;
845 const Location root_;
846
847 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
848};
849
Alexandre Rames5319def2014-10-23 10:03:10 +0100850#undef __
851
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100852Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100853 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100854 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100855 LOG(FATAL) << "Unreachable type " << type;
856 }
857
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100858 if (DataType::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100859 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
860 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100861 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000862 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000863 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
864 } else {
865 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100866 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
867 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100868 }
869
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000870 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100871 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100872 return next_location;
873}
874
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100875Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100876 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100877}
878
Serban Constantinescu579885a2015-02-22 20:51:33 +0000879CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100880 const CompilerOptions& compiler_options,
881 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100882 : CodeGenerator(graph,
883 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000884 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000885 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100886 callee_saved_core_registers.GetList(),
887 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100888 compiler_options,
889 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100890 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
891 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov1a719e42019-07-18 14:24:55 +0100892 location_builder_neon_(graph, this),
893 instruction_visitor_neon_(graph, this),
894 location_builder_sve_(graph, this),
895 instruction_visitor_sve_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100896 move_resolver_(graph->GetAllocator(), this),
Artem Serovaa6f4832018-11-21 18:57:54 +0000897 assembler_(graph->GetAllocator(),
898 compiler_options.GetInstructionSetFeatures()->AsArm64InstructionSetFeatures()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000899 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100900 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000901 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100902 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000903 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100904 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +0100905 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100906 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100907 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100908 uint32_literals_(std::less<uint32_t>(),
909 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
910 uint64_literals_(std::less<uint64_t>(),
911 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000912 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100913 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000914 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +0000915 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
916 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
917 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000918 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000919 AddAllocatedRegister(LocationFrom(lr));
Artem Serov1a719e42019-07-18 14:24:55 +0100920
921 bool use_sve = ShouldUseSVE();
922 if (use_sve) {
923 location_builder_ = &location_builder_sve_;
924 instruction_visitor_ = &instruction_visitor_sve_;
925 } else {
926 location_builder_ = &location_builder_neon_;
927 instruction_visitor_ = &instruction_visitor_neon_;
928 }
929}
930
931bool CodeGeneratorARM64::ShouldUseSVE() const {
932 return kArm64AllowSVE && GetInstructionSetFeatures().HasSVE();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000933}
Alexandre Rames5319def2014-10-23 10:03:10 +0100934
Alexandre Rames67555f72014-11-18 10:55:16 +0000935#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100936
Zheng Xu3927c8b2015-11-18 17:46:25 +0800937void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100938 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800939 jump_table->EmitTable(this);
940 }
941}
942
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000943void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800944 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +0000945
946 // Emit JIT baker read barrier slow paths.
Vladimir Markof91fc122020-05-13 09:21:00 +0100947 DCHECK(UseJitCompilation() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +0000948 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
949 uint32_t encoded_data = entry.first;
950 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
951 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -0800952 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +0000953 }
954
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000955 // Ensure we emit the literal pool.
956 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000957
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000958 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +0000959
960 // Verify Baker read barrier linker patches.
961 if (kIsDebugBuild) {
962 ArrayRef<const uint8_t> code = allocator->GetMemory();
963 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
964 DCHECK(info.label.IsBound());
965 uint32_t literal_offset = info.label.GetLocation();
966 DCHECK_ALIGNED(literal_offset, 4u);
967
968 auto GetInsn = [&code](uint32_t offset) {
969 DCHECK_ALIGNED(offset, 4u);
970 return
971 (static_cast<uint32_t>(code[offset + 0]) << 0) +
972 (static_cast<uint32_t>(code[offset + 1]) << 8) +
973 (static_cast<uint32_t>(code[offset + 2]) << 16)+
974 (static_cast<uint32_t>(code[offset + 3]) << 24);
975 };
976
977 const uint32_t encoded_data = info.custom_data;
978 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
979 // Check that the next instruction matches the expected LDR.
980 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +0100981 case BakerReadBarrierKind::kField:
982 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +0000983 DCHECK_GE(code.size() - literal_offset, 8u);
984 uint32_t next_insn = GetInsn(literal_offset + 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +0000985 CheckValidReg(next_insn & 0x1fu); // Check destination register.
986 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko0ecac682018-08-07 10:40:38 +0100987 if (kind == BakerReadBarrierKind::kField) {
988 // LDR (immediate) with correct base_reg.
989 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
990 } else {
991 DCHECK(kind == BakerReadBarrierKind::kAcquire);
992 // LDAR with correct base_reg.
993 CHECK_EQ(next_insn & 0xffffffe0u, 0x88dffc00u | (base_reg << 5));
994 }
Vladimir Markoca1e0382018-04-11 09:58:41 +0000995 break;
996 }
997 case BakerReadBarrierKind::kArray: {
998 DCHECK_GE(code.size() - literal_offset, 8u);
999 uint32_t next_insn = GetInsn(literal_offset + 4u);
1000 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1001 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1002 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1003 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1004 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1005 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1006 break;
1007 }
1008 case BakerReadBarrierKind::kGcRoot: {
1009 DCHECK_GE(literal_offset, 4u);
1010 uint32_t prev_insn = GetInsn(literal_offset - 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001011 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko94796f82018-08-08 15:15:33 +01001012 // Usually LDR (immediate) with correct root_reg but
1013 // we may have a "MOV marked, old_value" for UnsafeCASObject.
1014 if ((prev_insn & 0xffe0ffff) != (0x2a0003e0 | root_reg)) { // MOV?
1015 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg); // LDR?
1016 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001017 break;
1018 }
1019 default:
1020 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1021 UNREACHABLE();
1022 }
1023 }
1024 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001025}
1026
Zheng Xuad4450e2015-04-17 18:48:56 +08001027void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1028 // Note: There are 6 kinds of moves:
1029 // 1. constant -> GPR/FPR (non-cycle)
1030 // 2. constant -> stack (non-cycle)
1031 // 3. GPR/FPR -> GPR/FPR
1032 // 4. GPR/FPR -> stack
1033 // 5. stack -> GPR/FPR
1034 // 6. stack -> stack (non-cycle)
1035 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1036 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1037 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1038 // dependency.
1039 vixl_temps_.Open(GetVIXLAssembler());
1040}
1041
1042void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1043 vixl_temps_.Close();
1044}
1045
1046Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001047 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1048 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1049 || kind == Location::kSIMDStackSlot);
1050 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1051 ? Location::kFpuRegister
1052 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001053 Location scratch = GetScratchLocation(kind);
1054 if (!scratch.Equals(Location::NoLocation())) {
1055 return scratch;
1056 }
1057 // Allocate from VIXL temp registers.
1058 if (kind == Location::kRegister) {
1059 scratch = LocationFrom(vixl_temps_.AcquireX());
1060 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001061 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serov1a719e42019-07-18 14:24:55 +01001062 scratch = codegen_->GetGraph()->HasSIMD()
1063 ? codegen_->GetInstructionCodeGeneratorArm64()->AllocateSIMDScratchLocation(&vixl_temps_)
1064 : LocationFrom(vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001065 }
1066 AddScratchLocation(scratch);
1067 return scratch;
1068}
1069
1070void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1071 if (loc.IsRegister()) {
1072 vixl_temps_.Release(XRegisterFrom(loc));
1073 } else {
1074 DCHECK(loc.IsFpuRegister());
Artem Serov1a719e42019-07-18 14:24:55 +01001075 if (codegen_->GetGraph()->HasSIMD()) {
1076 codegen_->GetInstructionCodeGeneratorArm64()->FreeSIMDScratchLocation(loc, &vixl_temps_);
1077 } else {
1078 vixl_temps_.Release(DRegisterFrom(loc));
1079 }
Zheng Xuad4450e2015-04-17 18:48:56 +08001080 }
1081 RemoveScratchLocation(loc);
1082}
1083
Alexandre Rames3e69f162014-12-10 10:36:50 +00001084void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001085 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001086 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001087}
1088
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001089void CodeGeneratorARM64::MaybeIncrementHotness(bool is_frame_entry) {
1090 MacroAssembler* masm = GetVIXLAssembler();
1091 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1092 UseScratchRegisterScope temps(masm);
1093 Register counter = temps.AcquireX();
1094 Register method = is_frame_entry ? kArtMethodRegister : temps.AcquireX();
1095 if (!is_frame_entry) {
1096 __ Ldr(method, MemOperand(sp, 0));
1097 }
1098 __ Ldrh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1099 __ Add(counter, counter, 1);
1100 // Subtract one if the counter would overflow.
1101 __ Sub(counter, counter, Operand(counter, LSR, 16));
1102 __ Strh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1103 }
1104
1105 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
1106 ScopedObjectAccess soa(Thread::Current());
1107 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001108 if (info != nullptr) {
Nicolas Geoffrayc1cd1332020-01-25 13:08:24 +00001109 uint64_t address = reinterpret_cast64<uint64_t>(info);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001110 vixl::aarch64::Label done;
1111 UseScratchRegisterScope temps(masm);
1112 Register temp = temps.AcquireX();
1113 Register counter = temps.AcquireW();
1114 __ Mov(temp, address);
1115 __ Ldrh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1116 __ Add(counter, counter, 1);
1117 __ Strh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1118 __ Tst(counter, 0xffff);
1119 __ B(ne, &done);
1120 if (is_frame_entry) {
1121 if (HasEmptyFrame()) {
1122 // The entyrpoint expects the method at the bottom of the stack. We
1123 // claim stack space necessary for alignment.
1124 __ Claim(kStackAlignment);
1125 __ Stp(kArtMethodRegister, lr, MemOperand(sp, 0));
1126 } else if (!RequiresCurrentMethod()) {
1127 __ Str(kArtMethodRegister, MemOperand(sp, 0));
1128 }
1129 } else {
1130 CHECK(RequiresCurrentMethod());
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001131 }
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001132 uint32_t entrypoint_offset =
1133 GetThreadOffset<kArm64PointerSize>(kQuickCompileOptimized).Int32Value();
1134 __ Ldr(lr, MemOperand(tr, entrypoint_offset));
1135 // Note: we don't record the call here (and therefore don't generate a stack
1136 // map), as the entrypoint should never be suspended.
1137 __ Blr(lr);
1138 if (HasEmptyFrame()) {
1139 CHECK(is_frame_entry);
1140 __ Ldr(lr, MemOperand(sp, 8));
1141 __ Drop(kStackAlignment);
1142 }
1143 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001144 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001145 }
1146}
1147
Alexandre Rames5319def2014-10-23 10:03:10 +01001148void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001149 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001150 __ Bind(&frame_entry_label_);
1151
Vladimir Marko33bff252017-11-01 14:35:42 +00001152 bool do_overflow_check =
1153 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001154 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001155 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001156 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001157 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001158 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001159 {
1160 // Ensure that between load and RecordPcInfo there are no pools emitted.
1161 ExactAssemblyScope eas(GetVIXLAssembler(),
1162 kInstructionSize,
1163 CodeBufferCheckScope::kExactSize);
1164 __ ldr(wzr, MemOperand(temp, 0));
1165 RecordPcInfo(nullptr, 0);
1166 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001167 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001168
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001169 if (!HasEmptyFrame()) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001170 // Stack layout:
1171 // sp[frame_size - 8] : lr.
1172 // ... : other preserved core registers.
1173 // ... : other preserved fp registers.
1174 // ... : reserved frame space.
1175 // sp[0] : current method.
Vladimir Marko1a225a72019-07-05 13:37:42 +01001176 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1177 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1178 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1179 DCHECK(!preserved_core_registers.IsEmpty());
1180 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1181 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001182
Vladimir Marko1a225a72019-07-05 13:37:42 +01001183 // Save the current method if we need it, or if using STP reduces code
1184 // size. Note that we do not do this in HCurrentMethod, as the
1185 // instruction might have been removed in the SSA graph.
1186 CPURegister lowest_spill;
1187 if (core_spills_offset == kXRegSizeInBytes) {
1188 // If there is no gap between the method and the lowest core spill, use
1189 // aligned STP pre-index to store both. Max difference is 512. We do
1190 // that to reduce code size even if we do not have to save the method.
1191 DCHECK_LE(frame_size, 512); // 32 core registers are only 256 bytes.
1192 lowest_spill = preserved_core_registers.PopLowestIndex();
1193 __ Stp(kArtMethodRegister, lowest_spill, MemOperand(sp, -frame_size, PreIndex));
1194 } else if (RequiresCurrentMethod()) {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001195 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001196 } else {
1197 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001198 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001199 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Vladimir Marko1a225a72019-07-05 13:37:42 +01001200 if (lowest_spill.IsValid()) {
1201 GetAssembler()->cfi().RelOffset(DWARFReg(lowest_spill), core_spills_offset);
1202 core_spills_offset += kXRegSizeInBytes;
1203 }
1204 GetAssembler()->SpillRegisters(preserved_core_registers, core_spills_offset);
1205 GetAssembler()->SpillRegisters(preserved_fp_registers, fp_spills_offset);
Mingyao Yang063fc772016-08-02 11:02:54 -07001206
1207 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1208 // Initialize should_deoptimize flag to 0.
1209 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1210 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1211 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001212 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001213 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08001214 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001215}
1216
1217void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001218 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001219 if (!HasEmptyFrame()) {
Vladimir Marko1a225a72019-07-05 13:37:42 +01001220 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1221 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1222 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1223 DCHECK(!preserved_core_registers.IsEmpty());
1224 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1225 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
1226
1227 CPURegister lowest_spill;
1228 if (core_spills_offset == kXRegSizeInBytes) {
1229 // If there is no gap between the method and the lowest core spill, use
1230 // aligned LDP pre-index to pop both. Max difference is 504. We do
1231 // that to reduce code size even though the loaded method is unused.
1232 DCHECK_LE(frame_size, 504); // 32 core registers are only 256 bytes.
1233 lowest_spill = preserved_core_registers.PopLowestIndex();
1234 core_spills_offset += kXRegSizeInBytes;
1235 }
1236 GetAssembler()->UnspillRegisters(preserved_fp_registers, fp_spills_offset);
1237 GetAssembler()->UnspillRegisters(preserved_core_registers, core_spills_offset);
1238 if (lowest_spill.IsValid()) {
1239 __ Ldp(xzr, lowest_spill, MemOperand(sp, frame_size, PostIndex));
1240 GetAssembler()->cfi().Restore(DWARFReg(lowest_spill));
1241 } else {
1242 __ Drop(frame_size);
1243 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001244 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001245 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001246 __ Ret();
1247 GetAssembler()->cfi().RestoreState();
1248 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001249}
1250
Scott Wakeling97c72b72016-06-24 16:19:36 +01001251CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001252 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001253 return CPURegList(CPURegister::kRegister, kXRegSize,
1254 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001255}
1256
Scott Wakeling97c72b72016-06-24 16:19:36 +01001257CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001258 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1259 GetNumberOfFloatingPointRegisters()));
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001260 return CPURegList(CPURegister::kVRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001261 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001262}
1263
Alexandre Rames5319def2014-10-23 10:03:10 +01001264void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1265 __ Bind(GetLabelOf(block));
1266}
1267
Calin Juravle175dc732015-08-25 15:42:32 +01001268void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1269 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001270 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001271}
1272
Calin Juravlee460d1d2015-09-29 04:52:17 +01001273void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1274 if (location.IsRegister()) {
1275 locations->AddTemp(location);
1276 } else {
1277 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1278 }
1279}
1280
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001281void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001282 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001283 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001284 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001285 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001286 if (value_can_be_null) {
1287 __ Cbz(value, &done);
1288 }
Roland Levillainc73f0522018-08-14 15:16:50 +01001289 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07001290 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01001291 // Calculate the offset (in the card table) of the card corresponding to
1292 // `object`.
Alexandre Rames5319def2014-10-23 10:03:10 +01001293 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Roland Levillainc73f0522018-08-14 15:16:50 +01001294 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
1295 // `object`'s card.
1296 //
1297 // Register `card` contains the address of the card table. Note that the card
1298 // table's base is biased during its creation so that it always starts at an
1299 // address whose least-significant byte is equal to `kCardDirty` (see
1300 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
1301 // below writes the `kCardDirty` (byte) value into the `object`'s card
1302 // (located at `card + object >> kCardShift`).
1303 //
1304 // This dual use of the value in register `card` (1. to calculate the location
1305 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
1306 // (no need to explicitly load `kCardDirty` as an immediate value).
Serban Constantinescu02164b32014-11-13 14:05:07 +00001307 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001308 if (value_can_be_null) {
1309 __ Bind(&done);
1310 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001311}
1312
David Brazdil58282f42016-01-14 12:45:10 +00001313void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001314 // Blocked core registers:
1315 // lr : Runtime reserved.
1316 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001317 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001318 // ip1 : VIXL core temp.
1319 // ip0 : VIXL core temp.
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001320 // x18 : Platform register.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001321 //
1322 // Blocked fp registers:
1323 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001324 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1325 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001326 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001327 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001328 }
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001329 blocked_core_registers_[X18] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001330
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001331 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001332 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001333 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001334 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001335
David Brazdil58282f42016-01-14 12:45:10 +00001336 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001337 // Stubs do not save callee-save floating point registers. If the graph
1338 // is debuggable, we need to deal with these registers differently. For
1339 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001340 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1341 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001342 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001343 }
1344 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001345}
1346
Alexandre Rames3e69f162014-12-10 10:36:50 +00001347size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1348 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1349 __ Str(reg, MemOperand(sp, stack_index));
1350 return kArm64WordSize;
1351}
1352
1353size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1354 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1355 __ Ldr(reg, MemOperand(sp, stack_index));
1356 return kArm64WordSize;
1357}
1358
Artem Serov9df37b92019-07-23 16:41:54 +01001359size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1360 uint32_t reg_id ATTRIBUTE_UNUSED) {
1361 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1362 << "use SaveRestoreLiveRegistersHelper";
1363 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001364}
1365
Artem Serov9df37b92019-07-23 16:41:54 +01001366size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1367 uint32_t reg_id ATTRIBUTE_UNUSED) {
1368 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1369 << "use SaveRestoreLiveRegistersHelper";
1370 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001371}
1372
Alexandre Rames5319def2014-10-23 10:03:10 +01001373void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001374 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001375}
1376
1377void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001378 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001379}
1380
Vladimir Markoa0431112018-06-25 09:32:54 +01001381const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1382 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1383}
1384
Alexandre Rames67555f72014-11-18 10:55:16 +00001385void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001386 if (constant->IsIntConstant()) {
1387 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1388 } else if (constant->IsLongConstant()) {
1389 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1390 } else if (constant->IsNullConstant()) {
1391 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001392 } else if (constant->IsFloatConstant()) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001393 __ Fmov(VRegister(destination), constant->AsFloatConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001394 } else {
1395 DCHECK(constant->IsDoubleConstant());
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001396 __ Fmov(VRegister(destination), constant->AsDoubleConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001397 }
1398}
1399
Alexandre Rames3e69f162014-12-10 10:36:50 +00001400
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001401static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001402 DCHECK(constant.IsConstant());
1403 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001404 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001405 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001406 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1407 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1408 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1409 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001410}
1411
Roland Levillain952b2352017-05-03 19:49:14 +01001412// Allocate a scratch register from the VIXL pool, querying first
1413// the floating-point register pool, and then the core register
1414// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001415// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1416// using a different allocation strategy.
1417static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1418 vixl::aarch64::UseScratchRegisterScope* temps,
1419 int size_in_bits) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001420 return masm->GetScratchVRegisterList()->IsEmpty()
Roland Levillain558dea12017-01-27 19:40:44 +00001421 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1422 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1423}
1424
Calin Juravlee460d1d2015-09-29 04:52:17 +01001425void CodeGeneratorARM64::MoveLocation(Location destination,
1426 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001427 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001428 if (source.Equals(destination)) {
1429 return;
1430 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001431
1432 // A valid move can always be inferred from the destination and source
1433 // locations. When moving from and to a register, the argument type can be
1434 // used to generate 32bit instead of 64bit moves. In debug mode we also
1435 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001436 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001437
1438 if (destination.IsRegister() || destination.IsFpuRegister()) {
1439 if (unspecified_type) {
1440 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1441 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001442 (src_cst != nullptr && (src_cst->IsIntConstant()
1443 || src_cst->IsFloatConstant()
1444 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001445 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001446 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001447 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001448 // If the source is a double stack slot or a 64bit constant, a 64bit
1449 // type is appropriate. Else the source is a register, and since the
1450 // type has not been specified, we chose a 64bit type to force a 64bit
1451 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001452 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001453 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001454 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001455 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1456 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001457 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001458 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1459 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1460 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001461 } else if (source.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001462 GetInstructionCodeGeneratorArm64()->LoadSIMDRegFromStack(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001463 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001464 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001465 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001466 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001467 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001468 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001469 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001470 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001471 DataType::Type source_type = DataType::Is64BitType(dst_type)
1472 ? DataType::Type::kInt64
1473 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001474 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1475 }
1476 } else {
1477 DCHECK(source.IsFpuRegister());
1478 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001479 DataType::Type source_type = DataType::Is64BitType(dst_type)
1480 ? DataType::Type::kFloat64
1481 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001482 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1483 } else {
1484 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001485 if (GetGraph()->HasSIMD()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001486 GetInstructionCodeGeneratorArm64()->MoveSIMDRegToSIMDReg(destination, source);
Artem Serovd4bccf12017-04-03 18:47:32 +01001487 } else {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001488 __ Fmov(VRegister(dst), FPRegisterFrom(source, dst_type));
Artem Serovd4bccf12017-04-03 18:47:32 +01001489 }
1490 }
1491 }
1492 } else if (destination.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001493 GetInstructionCodeGeneratorArm64()->MoveToSIMDStackSlot(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001494 } else { // The destination is not a register. It must be a stack slot.
1495 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1496 if (source.IsRegister() || source.IsFpuRegister()) {
1497 if (unspecified_type) {
1498 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001499 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001500 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001501 dst_type =
1502 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001503 }
1504 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001505 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1506 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001507 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001508 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001509 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1510 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001511 UseScratchRegisterScope temps(GetVIXLAssembler());
1512 HConstant* src_cst = source.GetConstant();
1513 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001514 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001515 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1516 ? Register(xzr)
1517 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001518 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001519 if (src_cst->IsIntConstant()) {
1520 temp = temps.AcquireW();
1521 } else if (src_cst->IsLongConstant()) {
1522 temp = temps.AcquireX();
1523 } else if (src_cst->IsFloatConstant()) {
1524 temp = temps.AcquireS();
1525 } else {
1526 DCHECK(src_cst->IsDoubleConstant());
1527 temp = temps.AcquireD();
1528 }
1529 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001530 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001531 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001532 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001533 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001534 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001535 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001536 // Use any scratch register (a core or a floating-point one)
1537 // from VIXL scratch register pools as a temporary.
1538 //
1539 // We used to only use the FP scratch register pool, but in some
1540 // rare cases the only register from this pool (D31) would
1541 // already be used (e.g. within a ParallelMove instruction, when
1542 // a move is blocked by a another move requiring a scratch FP
1543 // register, which would reserve D31). To prevent this issue, we
1544 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001545 //
1546 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001547 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001548 // use AcquireFPOrCoreCPURegisterOfSize instead of
1549 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1550 // allocates core scratch registers first.
1551 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1552 GetVIXLAssembler(),
1553 &temps,
1554 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001555 __ Ldr(temp, StackOperandFrom(source));
1556 __ Str(temp, StackOperandFrom(destination));
1557 }
1558 }
1559}
1560
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001561void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001562 CPURegister dst,
1563 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001564 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001565 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001566 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001567 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001568 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001569 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001570 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001571 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001572 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001573 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001574 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001575 case DataType::Type::kInt16:
1576 __ Ldrsh(Register(dst), src);
1577 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001578 case DataType::Type::kInt32:
1579 case DataType::Type::kReference:
1580 case DataType::Type::kInt64:
1581 case DataType::Type::kFloat32:
1582 case DataType::Type::kFloat64:
1583 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001584 __ Ldr(dst, src);
1585 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001586 case DataType::Type::kUint32:
1587 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001588 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001589 LOG(FATAL) << "Unreachable type " << type;
1590 }
1591}
1592
Calin Juravle77520bc2015-01-12 18:45:46 +00001593void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001594 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001595 const MemOperand& src,
1596 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001597 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001598 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001599 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001600 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001601
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001602 DCHECK(!src.IsPreIndex());
1603 DCHECK(!src.IsPostIndex());
1604
1605 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001606 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001607 {
1608 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1609 MemOperand base = MemOperand(temp_base);
1610 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001611 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001612 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001613 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001614 {
1615 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1616 __ ldarb(Register(dst), base);
1617 if (needs_null_check) {
1618 MaybeRecordImplicitNullCheck(instruction);
1619 }
1620 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001621 if (type == DataType::Type::kInt8) {
1622 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001623 }
1624 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001625 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001626 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001627 {
1628 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1629 __ ldarh(Register(dst), base);
1630 if (needs_null_check) {
1631 MaybeRecordImplicitNullCheck(instruction);
1632 }
1633 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001634 if (type == DataType::Type::kInt16) {
1635 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1636 }
Artem Serov914d7a82017-02-07 14:33:49 +00001637 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001638 case DataType::Type::kInt32:
1639 case DataType::Type::kReference:
1640 case DataType::Type::kInt64:
1641 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001642 {
1643 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1644 __ ldar(Register(dst), base);
1645 if (needs_null_check) {
1646 MaybeRecordImplicitNullCheck(instruction);
1647 }
1648 }
1649 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001650 case DataType::Type::kFloat32:
1651 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001652 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001653 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001654
Artem Serov914d7a82017-02-07 14:33:49 +00001655 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1656 {
1657 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1658 __ ldar(temp, base);
1659 if (needs_null_check) {
1660 MaybeRecordImplicitNullCheck(instruction);
1661 }
1662 }
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001663 __ Fmov(VRegister(dst), temp);
Artem Serov914d7a82017-02-07 14:33:49 +00001664 break;
Roland Levillain44015862016-01-22 11:47:17 +00001665 }
Aart Bik66c158e2018-01-31 12:55:04 -08001666 case DataType::Type::kUint32:
1667 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001668 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001669 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001670 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001671 }
1672}
1673
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001674void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001675 CPURegister src,
1676 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001677 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001678 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001679 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001680 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001681 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001682 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001683 case DataType::Type::kUint16:
1684 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001685 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001686 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001687 case DataType::Type::kInt32:
1688 case DataType::Type::kReference:
1689 case DataType::Type::kInt64:
1690 case DataType::Type::kFloat32:
1691 case DataType::Type::kFloat64:
1692 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001693 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001694 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001695 case DataType::Type::kUint32:
1696 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001697 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001698 LOG(FATAL) << "Unreachable type " << type;
1699 }
1700}
1701
Artem Serov914d7a82017-02-07 14:33:49 +00001702void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001703 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001704 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001705 const MemOperand& dst,
1706 bool needs_null_check) {
1707 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001708 UseScratchRegisterScope temps(GetVIXLAssembler());
1709 Register temp_base = temps.AcquireX();
1710
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001711 DCHECK(!dst.IsPreIndex());
1712 DCHECK(!dst.IsPostIndex());
1713
1714 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001715 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001716 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001717 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001718 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001719 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001720 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001721 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001722 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001723 {
1724 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1725 __ stlrb(Register(src), base);
1726 if (needs_null_check) {
1727 MaybeRecordImplicitNullCheck(instruction);
1728 }
1729 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001730 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001731 case DataType::Type::kUint16:
1732 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001733 {
1734 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1735 __ stlrh(Register(src), base);
1736 if (needs_null_check) {
1737 MaybeRecordImplicitNullCheck(instruction);
1738 }
1739 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001740 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001741 case DataType::Type::kInt32:
1742 case DataType::Type::kReference:
1743 case DataType::Type::kInt64:
1744 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001745 {
1746 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1747 __ stlr(Register(src), base);
1748 if (needs_null_check) {
1749 MaybeRecordImplicitNullCheck(instruction);
1750 }
1751 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001752 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001753 case DataType::Type::kFloat32:
1754 case DataType::Type::kFloat64: {
1755 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001756 Register temp_src;
1757 if (src.IsZero()) {
1758 // The zero register is used to avoid synthesizing zero constants.
1759 temp_src = Register(src);
1760 } else {
1761 DCHECK(src.IsFPRegister());
1762 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001763 __ Fmov(temp_src, VRegister(src));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001764 }
Artem Serov914d7a82017-02-07 14:33:49 +00001765 {
1766 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1767 __ stlr(temp_src, base);
1768 if (needs_null_check) {
1769 MaybeRecordImplicitNullCheck(instruction);
1770 }
1771 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001772 break;
1773 }
Aart Bik66c158e2018-01-31 12:55:04 -08001774 case DataType::Type::kUint32:
1775 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001776 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001777 LOG(FATAL) << "Unreachable type " << type;
1778 }
1779}
1780
Calin Juravle175dc732015-08-25 15:42:32 +01001781void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1782 HInstruction* instruction,
1783 uint32_t dex_pc,
1784 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001785 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001786
Vladimir Markof6675082019-05-17 12:05:28 +01001787 ThreadOffset64 entrypoint_offset = GetThreadOffset<kArm64PointerSize>(entrypoint);
1788 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
1789 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
1790 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Markof91fc122020-05-13 09:21:00 +01001791 if (slow_path == nullptr || UseJitCompilation()) {
Vladimir Markof6675082019-05-17 12:05:28 +01001792 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00001793 // Ensure the pc position is recorded immediately after the `blr` instruction.
1794 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1795 __ blr(lr);
1796 if (EntrypointRequiresStackMap(entrypoint)) {
1797 RecordPcInfo(instruction, dex_pc, slow_path);
1798 }
Vladimir Markof6675082019-05-17 12:05:28 +01001799 } else {
1800 // Ensure the pc position is recorded immediately after the `bl` instruction.
1801 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1802 EmitEntrypointThunkCall(entrypoint_offset);
1803 if (EntrypointRequiresStackMap(entrypoint)) {
1804 RecordPcInfo(instruction, dex_pc, slow_path);
1805 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001806 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001807}
1808
Roland Levillaindec8f632016-07-22 17:10:06 +01001809void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1810 HInstruction* instruction,
1811 SlowPathCode* slow_path) {
1812 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001813 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1814 __ Blr(lr);
1815}
1816
Alexandre Rames67555f72014-11-18 10:55:16 +00001817void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001818 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001819 UseScratchRegisterScope temps(GetVIXLAssembler());
1820 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00001821 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001822 const size_t status_byte_offset =
1823 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1824 constexpr uint32_t shifted_visibly_initialized_value =
1825 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001826
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001827 // CMP (immediate) is limited to imm12 or imm12<<12, so we would need to materialize
1828 // the constant 0xf0000000 for comparison with the full 32-bit field. To reduce the code
1829 // size, load only the high byte of the field and compare with 0xf0.
1830 // Note: The same code size could be achieved with LDR+MNV(asr #24)+CBNZ but benchmarks
1831 // show that this pattern is slower (tested on little cores).
1832 __ Ldrb(temp, HeapOperand(class_reg, status_byte_offset));
1833 __ Cmp(temp, shifted_visibly_initialized_value);
1834 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001835 __ Bind(slow_path->GetExitLabel());
1836}
Alexandre Rames5319def2014-10-23 10:03:10 +01001837
Vladimir Marko175e7862018-03-27 09:03:13 +00001838void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
1839 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
1840 uint32_t path_to_root = check->GetBitstringPathToRoot();
1841 uint32_t mask = check->GetBitstringMask();
1842 DCHECK(IsPowerOfTwo(mask + 1));
1843 size_t mask_bits = WhichPowerOf2(mask + 1);
1844
1845 if (mask_bits == 16u) {
1846 // Load only the bitstring part of the status word.
1847 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1848 } else {
1849 // /* uint32_t */ temp = temp->status_
1850 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1851 // Extract the bitstring bits.
1852 __ Ubfx(temp, temp, 0, mask_bits);
1853 }
1854 // Compare the bitstring bits to `path_to_root`.
1855 __ Cmp(temp, path_to_root);
1856}
1857
Roland Levillain44015862016-01-22 11:47:17 +00001858void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001859 BarrierType type = BarrierAll;
1860
1861 switch (kind) {
1862 case MemBarrierKind::kAnyAny:
1863 case MemBarrierKind::kAnyStore: {
1864 type = BarrierAll;
1865 break;
1866 }
1867 case MemBarrierKind::kLoadAny: {
1868 type = BarrierReads;
1869 break;
1870 }
1871 case MemBarrierKind::kStoreStore: {
1872 type = BarrierWrites;
1873 break;
1874 }
1875 default:
1876 LOG(FATAL) << "Unexpected memory barrier " << kind;
1877 }
1878 __ Dmb(InnerShareable, type);
1879}
1880
Serban Constantinescu02164b32014-11-13 14:05:07 +00001881void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1882 HBasicBlock* successor) {
1883 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001884 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1885 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001886 slow_path =
1887 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001888 instruction->SetSlowPath(slow_path);
1889 codegen_->AddSlowPath(slow_path);
1890 if (successor != nullptr) {
1891 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001892 }
1893 } else {
1894 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1895 }
1896
Serban Constantinescu02164b32014-11-13 14:05:07 +00001897 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1898 Register temp = temps.AcquireW();
1899
Andreas Gampe542451c2016-07-26 09:02:02 -07001900 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001901 if (successor == nullptr) {
1902 __ Cbnz(temp, slow_path->GetEntryLabel());
1903 __ Bind(slow_path->GetReturnLabel());
1904 } else {
1905 __ Cbz(temp, codegen_->GetLabelOf(successor));
1906 __ B(slow_path->GetEntryLabel());
1907 // slow_path will return to GetLabelOf(successor).
1908 }
1909}
1910
Alexandre Rames5319def2014-10-23 10:03:10 +01001911InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1912 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001913 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001914 assembler_(codegen->GetAssembler()),
1915 codegen_(codegen) {}
1916
Alexandre Rames67555f72014-11-18 10:55:16 +00001917void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001918 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001919 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001920 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001921 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001922 case DataType::Type::kInt32:
1923 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01001924 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001925 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001926 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001927 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001928
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001929 case DataType::Type::kFloat32:
1930 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001931 locations->SetInAt(0, Location::RequiresFpuRegister());
1932 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001933 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001934 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001935
Alexandre Rames5319def2014-10-23 10:03:10 +01001936 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001937 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001938 }
1939}
1940
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001941void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
1942 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001943 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1944
1945 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001946 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01001947 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001948 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
1949 object_field_get_with_read_barrier
1950 ? LocationSummary::kCallOnSlowPath
1951 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001952 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001953 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko0ecac682018-08-07 10:40:38 +01001954 // We need a temporary register for the read barrier load in
1955 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
1956 // only if the field is volatile or the offset is too big.
1957 if (field_info.IsVolatile() ||
1958 field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
1959 locations->AddTemp(FixedTempLocation());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001960 }
Vladimir Marko70e97462016-08-09 11:04:26 +01001961 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001962 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001963 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001964 locations->SetOut(Location::RequiresFpuRegister());
1965 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001966 // The output overlaps for an object field get when read barriers
1967 // are enabled: we do not want the load to overwrite the object's
1968 // location, as we need it to emit the read barrier.
1969 locations->SetOut(
1970 Location::RequiresRegister(),
1971 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001972 }
1973}
1974
1975void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1976 const FieldInfo& field_info) {
1977 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001978 LocationSummary* locations = instruction->GetLocations();
1979 Location base_loc = locations->InAt(0);
1980 Location out = locations->Out();
1981 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01001982 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
1983 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001984 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001985
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001986 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01001987 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00001988 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00001989 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001990 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001991 Location maybe_temp =
1992 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00001993 // Note that potential implicit null checks are handled in this
1994 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1995 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1996 instruction,
1997 out,
1998 base,
1999 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002000 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002001 /* needs_null_check= */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002002 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002003 } else {
2004 // General case.
2005 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002006 // Note that a potential implicit null check is handled in this
2007 // CodeGeneratorARM64::LoadAcquire call.
2008 // NB: LoadAcquire will record the pc info if needed.
2009 codegen_->LoadAcquire(
Andreas Gampe3db70682018-12-26 15:12:03 -08002010 instruction, OutputCPURegister(instruction), field, /* needs_null_check= */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002011 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002012 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2013 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002014 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002015 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002016 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002017 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002018 // If read barriers are enabled, emit read barriers other than
2019 // Baker's using a slow path (and also unpoison the loaded
2020 // reference, if heap poisoning is enabled).
2021 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2022 }
Roland Levillain4d027112015-07-01 15:41:14 +01002023 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002024}
2025
2026void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2027 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002028 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002029 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002030 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2031 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002032 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002033 locations->SetInAt(1, Location::RequiresFpuRegister());
2034 } else {
2035 locations->SetInAt(1, Location::RequiresRegister());
2036 }
2037}
2038
2039void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002040 const FieldInfo& field_info,
2041 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002042 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2043
2044 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002045 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002046 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002047 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002048 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002049
Roland Levillain4d027112015-07-01 15:41:14 +01002050 {
2051 // We use a block to end the scratch scope before the write barrier, thus
2052 // freeing the temporary registers so they can be used in `MarkGCCard`.
2053 UseScratchRegisterScope temps(GetVIXLAssembler());
2054
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002055 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002056 DCHECK(value.IsW());
2057 Register temp = temps.AcquireW();
2058 __ Mov(temp, value.W());
2059 GetAssembler()->PoisonHeapReference(temp.W());
2060 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002061 }
Roland Levillain4d027112015-07-01 15:41:14 +01002062
2063 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002064 codegen_->StoreRelease(
Andreas Gampe3db70682018-12-26 15:12:03 -08002065 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check= */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002066 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002067 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2068 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002069 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2070 codegen_->MaybeRecordImplicitNullCheck(instruction);
2071 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002072 }
2073
2074 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002075 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002076 }
2077}
2078
Alexandre Rames67555f72014-11-18 10:55:16 +00002079void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002080 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002081
2082 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002083 case DataType::Type::kInt32:
2084 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002085 Register dst = OutputRegister(instr);
2086 Register lhs = InputRegisterAt(instr, 0);
2087 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002088 if (instr->IsAdd()) {
2089 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002090 } else if (instr->IsAnd()) {
2091 __ And(dst, lhs, rhs);
2092 } else if (instr->IsOr()) {
2093 __ Orr(dst, lhs, rhs);
2094 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002095 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002096 } else if (instr->IsRor()) {
2097 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002098 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002099 __ Ror(dst, lhs, shift);
2100 } else {
2101 // Ensure shift distance is in the same size register as the result. If
2102 // we are rotating a long and the shift comes in a w register originally,
2103 // we don't need to sxtw for use as an x since the shift distances are
2104 // all & reg_bits - 1.
2105 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2106 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002107 } else if (instr->IsMin() || instr->IsMax()) {
2108 __ Cmp(lhs, rhs);
2109 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002110 } else {
2111 DCHECK(instr->IsXor());
2112 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002113 }
2114 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002115 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002116 case DataType::Type::kFloat32:
2117 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002118 VRegister dst = OutputFPRegister(instr);
2119 VRegister lhs = InputFPRegisterAt(instr, 0);
2120 VRegister rhs = InputFPRegisterAt(instr, 1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002121 if (instr->IsAdd()) {
2122 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002123 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002124 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002125 } else if (instr->IsMin()) {
2126 __ Fmin(dst, lhs, rhs);
2127 } else if (instr->IsMax()) {
2128 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002129 } else {
2130 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002131 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002132 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002133 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002134 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002135 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002136 }
2137}
2138
Serban Constantinescu02164b32014-11-13 14:05:07 +00002139void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2140 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2141
Vladimir Markoca6fff82017-10-03 14:49:14 +01002142 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002143 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002144 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002145 case DataType::Type::kInt32:
2146 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002147 locations->SetInAt(0, Location::RequiresRegister());
2148 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002149 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002150 break;
2151 }
2152 default:
2153 LOG(FATAL) << "Unexpected shift type " << type;
2154 }
2155}
2156
2157void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2158 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2159
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002160 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002161 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002162 case DataType::Type::kInt32:
2163 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002164 Register dst = OutputRegister(instr);
2165 Register lhs = InputRegisterAt(instr, 0);
2166 Operand rhs = InputOperandAt(instr, 1);
2167 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002168 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002169 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002170 if (instr->IsShl()) {
2171 __ Lsl(dst, lhs, shift_value);
2172 } else if (instr->IsShr()) {
2173 __ Asr(dst, lhs, shift_value);
2174 } else {
2175 __ Lsr(dst, lhs, shift_value);
2176 }
2177 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002178 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002179
2180 if (instr->IsShl()) {
2181 __ Lsl(dst, lhs, rhs_reg);
2182 } else if (instr->IsShr()) {
2183 __ Asr(dst, lhs, rhs_reg);
2184 } else {
2185 __ Lsr(dst, lhs, rhs_reg);
2186 }
2187 }
2188 break;
2189 }
2190 default:
2191 LOG(FATAL) << "Unexpected shift operation type " << type;
2192 }
2193}
2194
Alexandre Rames5319def2014-10-23 10:03:10 +01002195void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002196 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002197}
2198
2199void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002200 HandleBinaryOp(instruction);
2201}
2202
2203void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2204 HandleBinaryOp(instruction);
2205}
2206
2207void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2208 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002209}
2210
Artem Serov7fc63502016-02-09 17:15:29 +00002211void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002212 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002213 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002214 locations->SetInAt(0, Location::RequiresRegister());
2215 // There is no immediate variant of negated bitwise instructions in AArch64.
2216 locations->SetInAt(1, Location::RequiresRegister());
2217 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2218}
2219
Artem Serov7fc63502016-02-09 17:15:29 +00002220void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002221 Register dst = OutputRegister(instr);
2222 Register lhs = InputRegisterAt(instr, 0);
2223 Register rhs = InputRegisterAt(instr, 1);
2224
2225 switch (instr->GetOpKind()) {
2226 case HInstruction::kAnd:
2227 __ Bic(dst, lhs, rhs);
2228 break;
2229 case HInstruction::kOr:
2230 __ Orn(dst, lhs, rhs);
2231 break;
2232 case HInstruction::kXor:
2233 __ Eon(dst, lhs, rhs);
2234 break;
2235 default:
2236 LOG(FATAL) << "Unreachable";
2237 }
2238}
2239
Anton Kirilov74234da2017-01-13 14:42:47 +00002240void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2241 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002242 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2243 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002244 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002245 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002246 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2247 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2248 } else {
2249 locations->SetInAt(0, Location::RequiresRegister());
2250 }
2251 locations->SetInAt(1, Location::RequiresRegister());
2252 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2253}
2254
Anton Kirilov74234da2017-01-13 14:42:47 +00002255void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2256 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002257 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002258 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002259 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002260 Register out = OutputRegister(instruction);
2261 Register left;
2262 if (kind != HInstruction::kNeg) {
2263 left = InputRegisterAt(instruction, 0);
2264 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002265 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002266 // shifter operand operation, the IR generating `right_reg` (input to the type
2267 // conversion) can have a different type from the current instruction's type,
2268 // so we manually indicate the type.
2269 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002270 Operand right_operand(0);
2271
Anton Kirilov74234da2017-01-13 14:42:47 +00002272 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2273 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002274 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2275 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002276 right_operand = Operand(right_reg,
2277 helpers::ShiftFromOpKind(op_kind),
2278 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002279 }
2280
2281 // Logical binary operations do not support extension operations in the
2282 // operand. Note that VIXL would still manage if it was passed by generating
2283 // the extension as a separate instruction.
2284 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2285 DCHECK(!right_operand.IsExtendedRegister() ||
2286 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2287 kind != HInstruction::kNeg));
2288 switch (kind) {
2289 case HInstruction::kAdd:
2290 __ Add(out, left, right_operand);
2291 break;
2292 case HInstruction::kAnd:
2293 __ And(out, left, right_operand);
2294 break;
2295 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002296 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002297 __ Neg(out, right_operand);
2298 break;
2299 case HInstruction::kOr:
2300 __ Orr(out, left, right_operand);
2301 break;
2302 case HInstruction::kSub:
2303 __ Sub(out, left, right_operand);
2304 break;
2305 case HInstruction::kXor:
2306 __ Eor(out, left, right_operand);
2307 break;
2308 default:
2309 LOG(FATAL) << "Unexpected operation kind: " << kind;
2310 UNREACHABLE();
2311 }
2312}
2313
Artem Serov328429f2016-07-06 16:23:04 +01002314void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002315 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002316 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002317 locations->SetInAt(0, Location::RequiresRegister());
2318 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002319 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002320}
2321
Roland Levillain19c54192016-11-04 13:44:09 +00002322void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002323 __ Add(OutputRegister(instruction),
2324 InputRegisterAt(instruction, 0),
2325 Operand(InputOperandAt(instruction, 1)));
2326}
2327
Artem Serove1811ed2017-04-27 16:50:47 +01002328void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2329 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002330 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002331
2332 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2333
2334 locations->SetInAt(0, Location::RequiresRegister());
2335 // For byte case we don't need to shift the index variable so we can encode the data offset into
2336 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2337 // data offset constant generation out of the loop and reduce the critical path length in the
2338 // loop.
2339 locations->SetInAt(1, shift->GetValue() == 0
2340 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2341 : Location::RequiresRegister());
2342 locations->SetInAt(2, Location::ConstantLocation(shift));
2343 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2344}
2345
2346void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2347 HIntermediateAddressIndex* instruction) {
2348 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002349 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002350 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2351
2352 if (shift == 0) {
2353 __ Add(OutputRegister(instruction), index_reg, offset);
2354 } else {
2355 Register offset_reg = InputRegisterAt(instruction, 1);
2356 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2357 }
2358}
2359
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002360void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002361 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002362 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002363 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2364 if (instr->GetOpKind() == HInstruction::kSub &&
2365 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002366 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002367 // Don't allocate register for Mneg instruction.
2368 } else {
2369 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2370 Location::RequiresRegister());
2371 }
2372 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2373 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002374 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2375}
2376
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002377void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002378 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002379 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2380 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002381
2382 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2383 // This fixup should be carried out for all multiply-accumulate instructions:
2384 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002385 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002386 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2387 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002388 vixl::aarch64::Instruction* prev =
2389 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002390 if (prev->IsLoadOrStore()) {
2391 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002392 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002393 __ nop();
2394 }
2395 }
2396
2397 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002398 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002399 __ Madd(res, mul_left, mul_right, accumulator);
2400 } else {
2401 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002402 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002403 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002404 __ Mneg(res, mul_left, mul_right);
2405 } else {
2406 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2407 __ Msub(res, mul_left, mul_right, accumulator);
2408 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002409 }
2410}
2411
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002412void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002413 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002414 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002415 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002416 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2417 object_array_get_with_read_barrier
2418 ? LocationSummary::kCallOnSlowPath
2419 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002420 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002421 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002422 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002423 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002424 // We need a temporary register for the read barrier load in
2425 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2426 // only if the offset is too big.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002427 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2428 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002429 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002430 if (offset >= kReferenceLoadMinFarOffset) {
2431 locations->AddTemp(FixedTempLocation());
2432 }
Artem Serov0806f582018-10-11 20:14:20 +01002433 } else if (!instruction->GetArray()->IsIntermediateAddress()) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01002434 // We need a non-scratch temporary for the array data pointer in
Artem Serov0806f582018-10-11 20:14:20 +01002435 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier() for the case with no
2436 // intermediate address.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002437 locations->AddTemp(Location::RequiresRegister());
2438 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002439 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002440 locations->SetInAt(0, Location::RequiresRegister());
2441 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002442 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002443 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2444 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002445 // The output overlaps in the case of an object array get with
2446 // read barriers enabled: we do not want the move to overwrite the
2447 // array's location, as we need it to emit the read barrier.
2448 locations->SetOut(
2449 Location::RequiresRegister(),
2450 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002451 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002452}
2453
2454void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002455 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002456 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002457 LocationSummary* locations = instruction->GetLocations();
2458 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002459 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002460 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002461 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2462 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002463 MacroAssembler* masm = GetVIXLAssembler();
2464 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002465
Artem Serov0806f582018-10-11 20:14:20 +01002466 // The non-Baker read barrier instrumentation of object ArrayGet instructions
Roland Levillain19c54192016-11-04 13:44:09 +00002467 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002468 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002469 instruction->GetArray()->IsIntermediateAddress() &&
Artem Serov0806f582018-10-11 20:14:20 +01002470 kEmitCompilerReadBarrier &&
2471 !kUseBakerReadBarrier));
Roland Levillain19c54192016-11-04 13:44:09 +00002472
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002473 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002474 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002475 // Note that a potential implicit null check is handled in the
2476 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002477 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002478 if (index.IsConstant()) {
Artem Serov0806f582018-10-11 20:14:20 +01002479 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002480 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002481 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002482 Location maybe_temp =
2483 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2484 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2485 out,
2486 obj.W(),
2487 offset,
2488 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002489 /* needs_null_check= */ false,
2490 /* use_load_acquire= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002491 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002492 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08002493 instruction, out, obj.W(), offset, index, /* needs_null_check= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002494 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002495 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002496 // General case.
2497 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002498 Register length;
2499 if (maybe_compressed_char_at) {
2500 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2501 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002502 {
2503 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2504 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2505
2506 if (instruction->GetArray()->IsIntermediateAddress()) {
2507 DCHECK_LT(count_offset, offset);
2508 int64_t adjusted_offset =
2509 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2510 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2511 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2512 } else {
2513 __ Ldr(length, HeapOperand(obj, count_offset));
2514 }
2515 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002516 }
jessicahandojo05765752016-09-09 19:01:32 -07002517 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002518 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002519 if (maybe_compressed_char_at) {
2520 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002521 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2522 "Expecting 0=compressed, 1=uncompressed");
2523 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002524 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002525 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002526 __ B(&done);
2527 __ Bind(&uncompressed_load);
2528 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002529 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002530 __ Bind(&done);
2531 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002532 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002533 source = HeapOperand(obj, offset);
2534 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002535 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002536 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002537 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002538 // We do not need to compute the intermediate address from the array: the
2539 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002540 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002541 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002542 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2543 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
Roland Levillain44015862016-01-22 11:47:17 +00002544 }
2545 temp = obj;
2546 } else {
2547 __ Add(temp, obj, offset);
2548 }
jessicahandojo05765752016-09-09 19:01:32 -07002549 if (maybe_compressed_char_at) {
2550 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002551 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2552 "Expecting 0=compressed, 1=uncompressed");
2553 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002554 __ Ldrb(Register(OutputCPURegister(instruction)),
2555 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2556 __ B(&done);
2557 __ Bind(&uncompressed_load);
2558 __ Ldrh(Register(OutputCPURegister(instruction)),
2559 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2560 __ Bind(&done);
2561 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002562 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002563 }
Roland Levillain44015862016-01-22 11:47:17 +00002564 }
jessicahandojo05765752016-09-09 19:01:32 -07002565 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002566 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2567 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002568 codegen_->Load(type, OutputCPURegister(instruction), source);
2569 codegen_->MaybeRecordImplicitNullCheck(instruction);
2570 }
Roland Levillain44015862016-01-22 11:47:17 +00002571
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002572 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002573 static_assert(
2574 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2575 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2576 Location obj_loc = locations->InAt(0);
2577 if (index.IsConstant()) {
2578 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2579 } else {
2580 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2581 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002582 }
Roland Levillain4d027112015-07-01 15:41:14 +01002583 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002584}
2585
Alexandre Rames5319def2014-10-23 10:03:10 +01002586void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002587 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002588 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002589 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002590}
2591
2592void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002593 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002594 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002595 {
2596 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2597 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2598 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2599 codegen_->MaybeRecordImplicitNullCheck(instruction);
2600 }
jessicahandojo05765752016-09-09 19:01:32 -07002601 // Mask out compression flag from String's array length.
2602 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002603 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002604 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002605}
2606
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002607void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002608 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002609
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002610 bool needs_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002611 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002612 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002613 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002614 locations->SetInAt(0, Location::RequiresRegister());
2615 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002616 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2617 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002618 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002619 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002620 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002621 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002622 }
2623}
2624
2625void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002626 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002627 LocationSummary* locations = instruction->GetLocations();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002628 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002629 bool needs_write_barrier =
2630 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002631
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002632 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002633 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002634 CPURegister source = value;
2635 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002636 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002637 MemOperand destination = HeapOperand(array);
2638 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002639
2640 if (!needs_write_barrier) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002641 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002642 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002643 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002644 destination = HeapOperand(array, offset);
2645 } else {
2646 UseScratchRegisterScope temps(masm);
2647 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002648 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002649 // We do not need to compute the intermediate address from the array: the
2650 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002651 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002652 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002653 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2654 DCHECK(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002655 }
2656 temp = array;
2657 } else {
2658 __ Add(temp, array, offset);
2659 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002660 destination = HeapOperand(temp,
2661 XRegisterFrom(index),
2662 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002663 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002664 }
Artem Serov914d7a82017-02-07 14:33:49 +00002665 {
2666 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2667 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2668 codegen_->Store(value_type, value, destination);
2669 codegen_->MaybeRecordImplicitNullCheck(instruction);
2670 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002671 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002672 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002673
2674 bool can_value_be_null = instruction->GetValueCanBeNull();
2675 vixl::aarch64::Label do_store;
2676 if (can_value_be_null) {
2677 __ Cbz(Register(value), &do_store);
2678 }
2679
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002680 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002681 if (needs_type_check) {
2682 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
2683 codegen_->AddSlowPath(slow_path);
2684
2685 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2686 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2687 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2688
Alexandre Rames97833a02015-04-16 15:07:12 +01002689 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002690 Register temp = temps.AcquireSameSizeAs(array);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002691 Register temp2 = temps.AcquireSameSizeAs(array);
2692
2693 // Note that when Baker read barriers are enabled, the type
2694 // checks are performed without read barriers. This is fine,
2695 // even in the case where a class object is in the from-space
2696 // after the flip, as a comparison involving such a type would
2697 // not produce a false positive; it may of course produce a
2698 // false negative, in which case we would take the ArraySet
2699 // slow path.
2700
2701 // /* HeapReference<Class> */ temp = array->klass_
2702 {
2703 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2704 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2705 __ Ldr(temp, HeapOperand(array, class_offset));
2706 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames97833a02015-04-16 15:07:12 +01002707 }
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002708 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Alexandre Rames97833a02015-04-16 15:07:12 +01002709
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002710 // /* HeapReference<Class> */ temp = temp->component_type_
2711 __ Ldr(temp, HeapOperand(temp, component_offset));
2712 // /* HeapReference<Class> */ temp2 = value->klass_
2713 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2714 // If heap poisoning is enabled, no need to unpoison `temp`
2715 // nor `temp2`, as we are comparing two poisoned references.
2716 __ Cmp(temp, temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002717
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002718 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2719 vixl::aarch64::Label do_put;
2720 __ B(eq, &do_put);
2721 // If heap poisoning is enabled, the `temp` reference has
2722 // not been unpoisoned yet; unpoison it now.
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002723 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002724
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002725 // /* HeapReference<Class> */ temp = temp->super_class_
2726 __ Ldr(temp, HeapOperand(temp, super_offset));
2727 // If heap poisoning is enabled, no need to unpoison
2728 // `temp`, as we are comparing against null below.
2729 __ Cbnz(temp, slow_path->GetEntryLabel());
2730 __ Bind(&do_put);
Vladimir Markod1ef8732017-04-18 13:55:13 +01002731 } else {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002732 __ B(ne, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002733 }
2734 }
2735
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002736 codegen_->MarkGCCard(array, value.W(), /* value_can_be_null= */ false);
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002737
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002738 if (can_value_be_null) {
2739 DCHECK(do_store.IsLinked());
2740 __ Bind(&do_store);
2741 }
2742
2743 UseScratchRegisterScope temps(masm);
2744 if (kPoisonHeapReferences) {
2745 Register temp_source = temps.AcquireSameSizeAs(array);
2746 DCHECK(value.IsW());
2747 __ Mov(temp_source, value.W());
2748 GetAssembler()->PoisonHeapReference(temp_source);
2749 source = temp_source;
2750 }
2751
2752 if (index.IsConstant()) {
2753 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
2754 destination = HeapOperand(array, offset);
2755 } else {
2756 Register temp_base = temps.AcquireSameSizeAs(array);
2757 __ Add(temp_base, array, offset);
2758 destination = HeapOperand(temp_base,
2759 XRegisterFrom(index),
2760 LSL,
2761 DataType::SizeShift(value_type));
2762 }
2763
2764 {
2765 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2766 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2767 __ Str(source, destination);
2768
2769 if (can_value_be_null || !needs_type_check) {
2770 codegen_->MaybeRecordImplicitNullCheck(instruction);
2771 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002772 }
2773
2774 if (slow_path != nullptr) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002775 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002776 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002777 }
2778}
2779
Alexandre Rames67555f72014-11-18 10:55:16 +00002780void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002781 RegisterSet caller_saves = RegisterSet::Empty();
2782 InvokeRuntimeCallingConvention calling_convention;
2783 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2784 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2785 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002786
2787 // If both index and length are constant, we can check the bounds statically and
2788 // generate code accordingly. We want to make sure we generate constant locations
2789 // in that case, regardless of whether they are encodable in the comparison or not.
2790 HInstruction* index = instruction->InputAt(0);
2791 HInstruction* length = instruction->InputAt(1);
2792 bool both_const = index->IsConstant() && length->IsConstant();
2793 locations->SetInAt(0, both_const
2794 ? Location::ConstantLocation(index->AsConstant())
2795 : ARM64EncodableConstantOrRegister(index, instruction));
2796 locations->SetInAt(1, both_const
2797 ? Location::ConstantLocation(length->AsConstant())
2798 : ARM64EncodableConstantOrRegister(length, instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002799}
2800
2801void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Georgia Kouvelibe530852019-01-17 10:46:41 +00002802 LocationSummary* locations = instruction->GetLocations();
2803 Location index_loc = locations->InAt(0);
2804 Location length_loc = locations->InAt(1);
2805
2806 int cmp_first_input = 0;
2807 int cmp_second_input = 1;
2808 Condition cond = hs;
2809
2810 if (index_loc.IsConstant()) {
2811 int64_t index = Int64FromLocation(index_loc);
2812 if (length_loc.IsConstant()) {
2813 int64_t length = Int64FromLocation(length_loc);
2814 if (index < 0 || index >= length) {
2815 BoundsCheckSlowPathARM64* slow_path =
2816 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
2817 codegen_->AddSlowPath(slow_path);
2818 __ B(slow_path->GetEntryLabel());
2819 } else {
2820 // BCE will remove the bounds check if we are guaranteed to pass.
2821 // However, some optimization after BCE may have generated this, and we should not
2822 // generate a bounds check if it is a valid range.
2823 }
2824 return;
2825 }
2826 // Only the index is constant: change the order of the operands and commute the condition
2827 // so we can use an immediate constant for the index (only the second input to a cmp
2828 // instruction can be an immediate).
2829 cmp_first_input = 1;
2830 cmp_second_input = 0;
2831 cond = ls;
2832 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002833 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002834 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002835 __ Cmp(InputRegisterAt(instruction, cmp_first_input),
2836 InputOperandAt(instruction, cmp_second_input));
Alexandre Rames67555f72014-11-18 10:55:16 +00002837 codegen_->AddSlowPath(slow_path);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002838 __ B(slow_path->GetEntryLabel(), cond);
Alexandre Rames67555f72014-11-18 10:55:16 +00002839}
2840
Alexandre Rames67555f72014-11-18 10:55:16 +00002841void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2842 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002843 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00002844 locations->SetInAt(0, Location::RequiresRegister());
2845 if (check->HasUses()) {
2846 locations->SetOut(Location::SameAsFirstInput());
2847 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01002848 // Rely on the type initialization to save everything we need.
2849 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00002850}
2851
2852void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2853 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01002854 SlowPathCodeARM64* slow_path =
2855 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00002856 codegen_->AddSlowPath(slow_path);
2857 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2858}
2859
Roland Levillain1a653882016-03-18 18:05:57 +00002860static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2861 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2862 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2863}
2864
2865void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002866 VRegister lhs_reg = InputFPRegisterAt(instruction, 0);
Roland Levillain1a653882016-03-18 18:05:57 +00002867 Location rhs_loc = instruction->GetLocations()->InAt(1);
2868 if (rhs_loc.IsConstant()) {
2869 // 0.0 is the only immediate that can be encoded directly in
2870 // an FCMP instruction.
2871 //
2872 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2873 // specify that in a floating-point comparison, positive zero
2874 // and negative zero are considered equal, so we can use the
2875 // literal 0.0 for both cases here.
2876 //
2877 // Note however that some methods (Float.equal, Float.compare,
2878 // Float.compareTo, Double.equal, Double.compare,
2879 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2880 // StrictMath.min) consider 0.0 to be (strictly) greater than
2881 // -0.0. So if we ever translate calls to these methods into a
2882 // HCompare instruction, we must handle the -0.0 case with
2883 // care here.
2884 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2885 __ Fcmp(lhs_reg, 0.0);
2886 } else {
2887 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2888 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002889}
2890
Serban Constantinescu02164b32014-11-13 14:05:07 +00002891void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002892 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002893 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002894 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002895 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002896 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002897 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002898 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002899 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002900 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002901 case DataType::Type::kInt32:
2902 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002903 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002904 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002905 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2906 break;
2907 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002908 case DataType::Type::kFloat32:
2909 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002910 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002911 locations->SetInAt(1,
2912 IsFloatingPointZeroConstant(compare->InputAt(1))
2913 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2914 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002915 locations->SetOut(Location::RequiresRegister());
2916 break;
2917 }
2918 default:
2919 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2920 }
2921}
2922
2923void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002924 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002925
2926 // 0 if: left == right
2927 // 1 if: left > right
2928 // -1 if: left < right
2929 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002930 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002931 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002932 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002933 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002934 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002935 case DataType::Type::kInt32:
2936 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002937 Register result = OutputRegister(compare);
2938 Register left = InputRegisterAt(compare, 0);
2939 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002940 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002941 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2942 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002943 break;
2944 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002945 case DataType::Type::kFloat32:
2946 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002947 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002948 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002949 __ Cset(result, ne);
2950 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002951 break;
2952 }
2953 default:
2954 LOG(FATAL) << "Unimplemented compare type " << in_type;
2955 }
2956}
2957
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002958void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002959 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002960
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002961 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002962 locations->SetInAt(0, Location::RequiresFpuRegister());
2963 locations->SetInAt(1,
2964 IsFloatingPointZeroConstant(instruction->InputAt(1))
2965 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2966 : Location::RequiresFpuRegister());
2967 } else {
2968 // Integer cases.
2969 locations->SetInAt(0, Location::RequiresRegister());
2970 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2971 }
2972
David Brazdilb3e773e2016-01-26 11:28:37 +00002973 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002974 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002975 }
2976}
2977
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002978void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002979 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002980 return;
2981 }
2982
2983 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002984 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002985 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002986
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002987 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002988 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002989 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002990 } else {
2991 // Integer cases.
2992 Register lhs = InputRegisterAt(instruction, 0);
2993 Operand rhs = InputOperandAt(instruction, 1);
2994 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002995 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002996 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002997}
2998
2999#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3000 M(Equal) \
3001 M(NotEqual) \
3002 M(LessThan) \
3003 M(LessThanOrEqual) \
3004 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003005 M(GreaterThanOrEqual) \
3006 M(Below) \
3007 M(BelowOrEqual) \
3008 M(Above) \
3009 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003010#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003011void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3012void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003013FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003014#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003015#undef FOR_EACH_CONDITION_INSTRUCTION
3016
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003017void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003018 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003019 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003020 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3021
3022 Register out = OutputRegister(instruction);
3023 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003024
3025 if (abs_imm == 2) {
3026 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
3027 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
3028 } else {
3029 UseScratchRegisterScope temps(GetVIXLAssembler());
3030 Register temp = temps.AcquireSameSizeAs(out);
3031 __ Add(temp, dividend, abs_imm - 1);
3032 __ Cmp(dividend, 0);
3033 __ Csel(out, temp, dividend, lt);
3034 }
3035
Zheng Xuc6667102015-05-15 16:08:45 +08003036 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003037 if (imm > 0) {
3038 __ Asr(out, out, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003039 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003040 __ Neg(out, Operand(out, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003041 }
3042}
3043
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003044// Return true if the magic number was modified by subtracting 2^32. So dividend needs to be added.
3045static inline bool NeedToAddDividend(int64_t magic_number, int64_t divisor) {
3046 return divisor > 0 && magic_number < 0;
3047}
3048
3049// Return true if the magic number was modified by adding 2^32. So dividend needs to be subtracted.
3050static inline bool NeedToSubDividend(int64_t magic_number, int64_t divisor) {
3051 return divisor < 0 && magic_number > 0;
3052}
3053
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003054// Generate code which increments the value in register 'in' by 1 if the value is negative.
3055// It is done with 'add out, in, in, lsr #31 or #63'.
3056// If the value is a result of an operation setting the N flag, CINC MI can be used
3057// instead of ADD. 'use_cond_inc' controls this.
3058void InstructionCodeGeneratorARM64::GenerateIncrementNegativeByOne(
3059 Register out,
3060 Register in,
3061 bool use_cond_inc) {
3062 if (use_cond_inc) {
3063 __ Cinc(out, in, mi);
3064 } else {
3065 __ Add(out, in, Operand(in, LSR, in.GetSizeInBits() - 1));
3066 }
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003067}
3068
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003069// Helper to generate code producing the result of HRem with a constant divisor.
3070void InstructionCodeGeneratorARM64::GenerateResultRemWithAnyConstant(
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003071 Register out,
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003072 Register dividend,
3073 Register quotient,
3074 int64_t divisor,
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003075 UseScratchRegisterScope* temps_scope) {
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003076 // TODO: Strength reduction for msub.
3077 Register temp_imm = temps_scope->AcquireSameSizeAs(out);
3078 __ Mov(temp_imm, divisor);
3079 __ Msub(out, quotient, temp_imm, dividend);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003080}
3081
3082void InstructionCodeGeneratorARM64::GenerateInt64DivRemWithAnyConstant(
3083 HBinaryOperation* instruction) {
Zheng Xuc6667102015-05-15 16:08:45 +08003084 DCHECK(instruction->IsDiv() || instruction->IsRem());
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003085 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003086
3087 LocationSummary* locations = instruction->GetLocations();
3088 Location second = locations->InAt(1);
3089 DCHECK(second.IsConstant());
3090
3091 Register out = OutputRegister(instruction);
3092 Register dividend = InputRegisterAt(instruction, 0);
3093 int64_t imm = Int64FromConstant(second.GetConstant());
3094
Zheng Xuc6667102015-05-15 16:08:45 +08003095 int64_t magic;
3096 int shift;
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003097 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003098
3099 UseScratchRegisterScope temps(GetVIXLAssembler());
3100 Register temp = temps.AcquireSameSizeAs(out);
3101
3102 // temp = get_high(dividend * magic)
3103 __ Mov(temp, magic);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003104 __ Smulh(temp, dividend, temp);
3105
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003106 // The multiplication result might need some corrections to be finalized.
3107 // The last correction is to increment by 1, if the result is negative.
3108 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3109 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3110 // However if one of the corrections is ADD or SUB, the sign can be detected
3111 // with ADDS/SUBS. They set the N flag if the result is negative.
3112 // This allows to use CINC MI which has latency 1.
3113 bool use_cond_inc = false;
3114
3115 // As magic_number can be modified to fit into 32 bits, check whether the correction is needed.
3116 if (NeedToAddDividend(magic, imm)) {
3117 __ Adds(temp, temp, dividend);
3118 use_cond_inc = true;
3119 } else if (NeedToSubDividend(magic, imm)) {
3120 __ Subs(temp, temp, dividend);
3121 use_cond_inc = true;
3122 }
3123
3124 if (shift != 0) {
3125 __ Asr(temp, temp, shift);
3126 }
3127
3128 if (instruction->IsRem()) {
3129 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3130 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3131 } else {
3132 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3133 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003134}
3135
3136void InstructionCodeGeneratorARM64::GenerateInt32DivRemWithAnyConstant(
3137 HBinaryOperation* instruction) {
3138 DCHECK(instruction->IsDiv() || instruction->IsRem());
3139 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
3140
3141 LocationSummary* locations = instruction->GetLocations();
3142 Location second = locations->InAt(1);
3143 DCHECK(second.IsConstant());
3144
3145 Register out = OutputRegister(instruction);
3146 Register dividend = InputRegisterAt(instruction, 0);
3147 int64_t imm = Int64FromConstant(second.GetConstant());
3148
3149 int64_t magic;
3150 int shift;
3151 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
3152 UseScratchRegisterScope temps(GetVIXLAssembler());
3153 Register temp = temps.AcquireSameSizeAs(out);
3154
3155 // temp = get_high(dividend * magic)
3156 __ Mov(temp, magic);
3157 __ Smull(temp.X(), dividend, temp);
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003158
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003159 // The multiplication result might need some corrections to be finalized.
3160 // The last correction is to increment by 1, if the result is negative.
3161 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3162 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3163 // However if one of the corrections is ADD or SUB, the sign can be detected
3164 // with ADDS/SUBS. They set the N flag if the result is negative.
3165 // This allows to use CINC MI which has latency 1.
3166 bool use_cond_inc = false;
3167
3168 // ADD/SUB correction is performed in the high 32 bits
3169 // as high 32 bits are ignored because type are kInt32.
3170 if (NeedToAddDividend(magic, imm)) {
3171 __ Adds(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3172 use_cond_inc = true;
3173 } else if (NeedToSubDividend(magic, imm)) {
3174 __ Subs(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3175 use_cond_inc = true;
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003176 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003177
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003178 // Extract the result from the high 32 bits and apply the final right shift.
3179 DCHECK_LT(shift, 32);
3180 __ Asr(temp.X(), temp.X(), 32 + shift);
3181
3182 if (instruction->IsRem()) {
3183 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3184 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3185 } else {
3186 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3187 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003188}
3189
3190void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3191 DCHECK(instruction->IsDiv() || instruction->IsRem());
3192 if (instruction->GetResultType() == DataType::Type::kInt64) {
3193 GenerateInt64DivRemWithAnyConstant(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003194 } else {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003195 GenerateInt32DivRemWithAnyConstant(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003196 }
3197}
3198
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003199void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003200 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003201
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003202 if (imm == 0) {
3203 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3204 return;
3205 }
Zheng Xuc6667102015-05-15 16:08:45 +08003206
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003207 if (IsPowerOfTwo(AbsOrMin(imm))) {
3208 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003209 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003210 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3211 DCHECK(imm < -2 || imm > 2) << imm;
3212 GenerateDivRemWithAnyConstant(instruction);
3213 }
3214}
3215
3216void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3217 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3218 << instruction->GetResultType();
3219
3220 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3221 GenerateIntDivForConstDenom(instruction);
3222 } else {
3223 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003224 Register dividend = InputRegisterAt(instruction, 0);
3225 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003226 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003227 }
3228}
3229
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003230void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3231 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003232 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003233 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003234 case DataType::Type::kInt32:
3235 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003236 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003237 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003238 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3239 break;
3240
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003241 case DataType::Type::kFloat32:
3242 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003243 locations->SetInAt(0, Location::RequiresFpuRegister());
3244 locations->SetInAt(1, Location::RequiresFpuRegister());
3245 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3246 break;
3247
3248 default:
3249 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3250 }
3251}
3252
3253void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003254 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003255 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003256 case DataType::Type::kInt32:
3257 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003258 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003259 break;
3260
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003261 case DataType::Type::kFloat32:
3262 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003263 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3264 break;
3265
3266 default:
3267 LOG(FATAL) << "Unexpected div type " << type;
3268 }
3269}
3270
Alexandre Rames67555f72014-11-18 10:55:16 +00003271void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003272 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003273 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003274}
3275
3276void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3277 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003278 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003279 codegen_->AddSlowPath(slow_path);
3280 Location value = instruction->GetLocations()->InAt(0);
3281
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003282 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003283
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003284 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003285 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Elliott Hughesc1896c92018-11-29 11:33:18 -08003286 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003287 }
3288
Alexandre Rames67555f72014-11-18 10:55:16 +00003289 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003290 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003291 if (divisor == 0) {
3292 __ B(slow_path->GetEntryLabel());
3293 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003294 // A division by a non-null constant is valid. We don't need to perform
3295 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003296 }
3297 } else {
3298 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3299 }
3300}
3301
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003302void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3303 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003304 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003305 locations->SetOut(Location::ConstantLocation(constant));
3306}
3307
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003308void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3309 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003310 // Will be generated at use site.
3311}
3312
Alexandre Rames5319def2014-10-23 10:03:10 +01003313void LocationsBuilderARM64::VisitExit(HExit* exit) {
3314 exit->SetLocations(nullptr);
3315}
3316
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003317void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003318}
3319
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003320void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3321 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003322 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003323 locations->SetOut(Location::ConstantLocation(constant));
3324}
3325
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003326void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003327 // Will be generated at use site.
3328}
3329
David Brazdilfc6a86a2015-06-26 10:33:45 +00003330void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003331 if (successor->IsExitBlock()) {
3332 DCHECK(got->GetPrevious()->AlwaysThrows());
3333 return; // no code needed
3334 }
3335
Serban Constantinescu02164b32014-11-13 14:05:07 +00003336 HBasicBlock* block = got->GetBlock();
3337 HInstruction* previous = got->GetPrevious();
3338 HLoopInformation* info = block->GetLoopInformation();
3339
David Brazdil46e2a392015-03-16 17:31:52 +00003340 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00003341 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003342 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3343 return;
3344 }
3345 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3346 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08003347 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003348 }
3349 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003350 __ B(codegen_->GetLabelOf(successor));
3351 }
3352}
3353
David Brazdilfc6a86a2015-06-26 10:33:45 +00003354void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3355 got->SetLocations(nullptr);
3356}
3357
3358void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3359 HandleGoto(got, got->GetSuccessor());
3360}
3361
3362void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3363 try_boundary->SetLocations(nullptr);
3364}
3365
3366void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3367 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3368 if (!successor->IsExitBlock()) {
3369 HandleGoto(try_boundary, successor);
3370 }
3371}
3372
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003373void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003374 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003375 vixl::aarch64::Label* true_target,
3376 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003377 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003378
David Brazdil0debae72015-11-12 18:37:00 +00003379 if (true_target == nullptr && false_target == nullptr) {
3380 // Nothing to do. The code always falls through.
3381 return;
3382 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003383 // Constant condition, statically compared against "true" (integer value 1).
3384 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003385 if (true_target != nullptr) {
3386 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003387 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003388 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003389 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003390 if (false_target != nullptr) {
3391 __ B(false_target);
3392 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003393 }
David Brazdil0debae72015-11-12 18:37:00 +00003394 return;
3395 }
3396
3397 // The following code generates these patterns:
3398 // (1) true_target == nullptr && false_target != nullptr
3399 // - opposite condition true => branch to false_target
3400 // (2) true_target != nullptr && false_target == nullptr
3401 // - condition true => branch to true_target
3402 // (3) true_target != nullptr && false_target != nullptr
3403 // - condition true => branch to true_target
3404 // - branch to false_target
3405 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003406 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003407 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003408 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003409 if (true_target == nullptr) {
3410 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3411 } else {
3412 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3413 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003414 } else {
3415 // The condition instruction has not been materialized, use its inputs as
3416 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003417 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003418
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003419 DataType::Type type = condition->InputAt(0)->GetType();
3420 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003421 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003422 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003423 IfCondition opposite_condition = condition->GetOppositeCondition();
3424 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003425 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003426 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003427 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003428 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003429 // Integer cases.
3430 Register lhs = InputRegisterAt(condition, 0);
3431 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003432
3433 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003434 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003435 if (true_target == nullptr) {
3436 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3437 non_fallthrough_target = false_target;
3438 } else {
3439 arm64_cond = ARM64Condition(condition->GetCondition());
3440 non_fallthrough_target = true_target;
3441 }
3442
Aart Bik086d27e2016-01-20 17:02:00 -08003443 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003444 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003445 switch (arm64_cond) {
3446 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003447 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003448 break;
3449 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003450 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003451 break;
3452 case lt:
3453 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003454 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003455 break;
3456 case ge:
3457 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003458 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003459 break;
3460 default:
3461 // Without the `static_cast` the compiler throws an error for
3462 // `-Werror=sign-promo`.
3463 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3464 }
3465 } else {
3466 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003467 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003468 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003469 }
3470 }
David Brazdil0debae72015-11-12 18:37:00 +00003471
3472 // If neither branch falls through (case 3), the conditional branch to `true_target`
3473 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3474 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003475 __ B(false_target);
3476 }
3477}
3478
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003479void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003480 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003481 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003482 locations->SetInAt(0, Location::RequiresRegister());
3483 }
3484}
3485
3486void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003487 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3488 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003489 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3490 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3491 true_target = nullptr;
3492 }
3493 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3494 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3495 false_target = nullptr;
3496 }
Andreas Gampe3db70682018-12-26 15:12:03 -08003497 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003498}
3499
3500void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003501 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003502 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003503 InvokeRuntimeCallingConvention calling_convention;
3504 RegisterSet caller_saves = RegisterSet::Empty();
3505 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3506 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003507 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003508 locations->SetInAt(0, Location::RequiresRegister());
3509 }
3510}
3511
3512void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003513 SlowPathCodeARM64* slow_path =
3514 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003515 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08003516 /* condition_input_index= */ 0,
David Brazdil0debae72015-11-12 18:37:00 +00003517 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08003518 /* false_target= */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003519}
3520
Mingyao Yang063fc772016-08-02 11:02:54 -07003521void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003522 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003523 LocationSummary(flag, LocationSummary::kNoCall);
3524 locations->SetOut(Location::RequiresRegister());
3525}
3526
3527void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3528 __ Ldr(OutputRegister(flag),
3529 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3530}
3531
David Brazdilc0b601b2016-02-08 14:20:45 +00003532static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3533 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003534 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003535}
3536
Alexandre Rames880f1192016-06-13 16:04:50 +01003537static inline Condition GetConditionForSelect(HCondition* condition) {
3538 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003539 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3540 : ARM64Condition(cond);
3541}
3542
David Brazdil74eb1b22015-12-14 11:44:01 +00003543void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003544 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003545 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003546 locations->SetInAt(0, Location::RequiresFpuRegister());
3547 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003548 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003549 } else {
3550 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3551 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3552 bool is_true_value_constant = cst_true_value != nullptr;
3553 bool is_false_value_constant = cst_false_value != nullptr;
3554 // Ask VIXL whether we should synthesize constants in registers.
3555 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3556 Operand true_op = is_true_value_constant ?
3557 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3558 Operand false_op = is_false_value_constant ?
3559 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3560 bool true_value_in_register = false;
3561 bool false_value_in_register = false;
3562 MacroAssembler::GetCselSynthesisInformation(
3563 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3564 true_value_in_register |= !is_true_value_constant;
3565 false_value_in_register |= !is_false_value_constant;
3566
3567 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3568 : Location::ConstantLocation(cst_true_value));
3569 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3570 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003571 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003572 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003573
David Brazdil74eb1b22015-12-14 11:44:01 +00003574 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3575 locations->SetInAt(2, Location::RequiresRegister());
3576 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003577}
3578
3579void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003580 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003581 Condition csel_cond;
3582
3583 if (IsBooleanValueOrMaterializedCondition(cond)) {
3584 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003585 // Use the condition flags set by the previous instruction.
3586 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003587 } else {
3588 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003589 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003590 }
3591 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003592 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003593 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003594 } else {
3595 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003596 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003597 }
3598
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003599 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003600 __ Fcsel(OutputFPRegister(select),
3601 InputFPRegisterAt(select, 1),
3602 InputFPRegisterAt(select, 0),
3603 csel_cond);
3604 } else {
3605 __ Csel(OutputRegister(select),
3606 InputOperandAt(select, 1),
3607 InputOperandAt(select, 0),
3608 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003609 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003610}
3611
David Srbecky0cf44932015-12-09 14:09:59 +00003612void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003613 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003614}
3615
David Srbeckyd28f4a02016-03-14 17:14:24 +00003616void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3617 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003618}
3619
3620void CodeGeneratorARM64::GenerateNop() {
3621 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003622}
3623
Alexandre Rames5319def2014-10-23 10:03:10 +01003624void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003625 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003626}
3627
3628void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003629 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003630}
3631
3632void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003633 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003634}
3635
3636void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003637 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003638}
3639
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003640// Temp is used for read barrier.
3641static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3642 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003643 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003644 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3645 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3646 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3647 return 1;
3648 }
3649 return 0;
3650}
3651
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003652// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003653// interface pointer, one for loading the current interface.
3654// The other checks have one temp for loading the object's class.
3655static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3656 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3657 return 3;
3658 }
3659 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003660}
3661
Alexandre Rames67555f72014-11-18 10:55:16 +00003662void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003663 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003664 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003665 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003666 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003667 case TypeCheckKind::kExactCheck:
3668 case TypeCheckKind::kAbstractClassCheck:
3669 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003670 case TypeCheckKind::kArrayObjectCheck: {
3671 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3672 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3673 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003674 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003675 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003676 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003677 case TypeCheckKind::kUnresolvedCheck:
3678 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003679 call_kind = LocationSummary::kCallOnSlowPath;
3680 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003681 case TypeCheckKind::kBitstringCheck:
3682 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003683 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003684
Vladimir Markoca6fff82017-10-03 14:49:14 +01003685 LocationSummary* locations =
3686 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003687 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003688 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003689 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003690 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003691 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3692 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3693 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3694 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3695 } else {
3696 locations->SetInAt(1, Location::RequiresRegister());
3697 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003698 // The "out" register is used as a temporary, so it overlaps with the inputs.
3699 // Note that TypeCheckSlowPathARM64 uses this register too.
3700 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003701 // Add temps if necessary for read barriers.
3702 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003703}
3704
3705void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003706 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003707 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003708 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003709 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003710 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3711 ? Register()
3712 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003713 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003714 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003715 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3716 DCHECK_LE(num_temps, 1u);
3717 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003718 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3719 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3720 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3721 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003722
Scott Wakeling97c72b72016-06-24 16:19:36 +01003723 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003724 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003725
3726 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003727 // Avoid null check if we know `obj` is not null.
3728 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003729 __ Cbz(obj, &zero);
3730 }
3731
Roland Levillain44015862016-01-22 11:47:17 +00003732 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003733 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003734 ReadBarrierOption read_barrier_option =
3735 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003736 // /* HeapReference<Class> */ out = obj->klass_
3737 GenerateReferenceLoadTwoRegisters(instruction,
3738 out_loc,
3739 obj_loc,
3740 class_offset,
3741 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003742 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003743 __ Cmp(out, cls);
3744 __ Cset(out, eq);
3745 if (zero.IsLinked()) {
3746 __ B(&done);
3747 }
3748 break;
3749 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003750
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003751 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003752 ReadBarrierOption read_barrier_option =
3753 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003754 // /* HeapReference<Class> */ out = obj->klass_
3755 GenerateReferenceLoadTwoRegisters(instruction,
3756 out_loc,
3757 obj_loc,
3758 class_offset,
3759 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003760 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003761 // If the class is abstract, we eagerly fetch the super class of the
3762 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003763 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003764 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003765 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003766 GenerateReferenceLoadOneRegister(instruction,
3767 out_loc,
3768 super_offset,
3769 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003770 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003771 // If `out` is null, we use it for the result, and jump to `done`.
3772 __ Cbz(out, &done);
3773 __ Cmp(out, cls);
3774 __ B(ne, &loop);
3775 __ Mov(out, 1);
3776 if (zero.IsLinked()) {
3777 __ B(&done);
3778 }
3779 break;
3780 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003781
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003782 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003783 ReadBarrierOption read_barrier_option =
3784 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003785 // /* HeapReference<Class> */ out = obj->klass_
3786 GenerateReferenceLoadTwoRegisters(instruction,
3787 out_loc,
3788 obj_loc,
3789 class_offset,
3790 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003791 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003792 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003793 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003794 __ Bind(&loop);
3795 __ Cmp(out, cls);
3796 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003797 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003798 GenerateReferenceLoadOneRegister(instruction,
3799 out_loc,
3800 super_offset,
3801 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003802 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003803 __ Cbnz(out, &loop);
3804 // If `out` is null, we use it for the result, and jump to `done`.
3805 __ B(&done);
3806 __ Bind(&success);
3807 __ Mov(out, 1);
3808 if (zero.IsLinked()) {
3809 __ B(&done);
3810 }
3811 break;
3812 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003813
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003814 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003815 ReadBarrierOption read_barrier_option =
3816 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003817 // /* HeapReference<Class> */ out = obj->klass_
3818 GenerateReferenceLoadTwoRegisters(instruction,
3819 out_loc,
3820 obj_loc,
3821 class_offset,
3822 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003823 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003824 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003825 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003826 __ Cmp(out, cls);
3827 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003828 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003829 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003830 GenerateReferenceLoadOneRegister(instruction,
3831 out_loc,
3832 component_offset,
3833 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003834 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003835 // If `out` is null, we use it for the result, and jump to `done`.
3836 __ Cbz(out, &done);
3837 __ Ldrh(out, HeapOperand(out, primitive_offset));
3838 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3839 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003840 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003841 __ Mov(out, 1);
3842 __ B(&done);
3843 break;
3844 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003845
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003846 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003847 // No read barrier since the slow path will retry upon failure.
3848 // /* HeapReference<Class> */ out = obj->klass_
3849 GenerateReferenceLoadTwoRegisters(instruction,
3850 out_loc,
3851 obj_loc,
3852 class_offset,
3853 maybe_temp_loc,
3854 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003855 __ Cmp(out, cls);
3856 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01003857 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08003858 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003859 codegen_->AddSlowPath(slow_path);
3860 __ B(ne, slow_path->GetEntryLabel());
3861 __ Mov(out, 1);
3862 if (zero.IsLinked()) {
3863 __ B(&done);
3864 }
3865 break;
3866 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003867
Calin Juravle98893e12015-10-02 21:05:03 +01003868 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003869 case TypeCheckKind::kInterfaceCheck: {
3870 // Note that we indeed only call on slow path, but we always go
3871 // into the slow path for the unresolved and interface check
3872 // cases.
3873 //
3874 // We cannot directly call the InstanceofNonTrivial runtime
3875 // entry point without resorting to a type checking slow path
3876 // here (i.e. by calling InvokeRuntime directly), as it would
3877 // require to assign fixed registers for the inputs of this
3878 // HInstanceOf instruction (following the runtime calling
3879 // convention), which might be cluttered by the potential first
3880 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003881 //
3882 // TODO: Introduce a new runtime entry point taking the object
3883 // to test (instead of its class) as argument, and let it deal
3884 // with the read barrier issues. This will let us refactor this
3885 // case of the `switch` code as it was previously (with a direct
3886 // call to the runtime not using a type checking slow path).
3887 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003888 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01003889 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08003890 instruction, /* is_fatal= */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003891 codegen_->AddSlowPath(slow_path);
3892 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003893 if (zero.IsLinked()) {
3894 __ B(&done);
3895 }
3896 break;
3897 }
Vladimir Marko175e7862018-03-27 09:03:13 +00003898
3899 case TypeCheckKind::kBitstringCheck: {
3900 // /* HeapReference<Class> */ temp = obj->klass_
3901 GenerateReferenceLoadTwoRegisters(instruction,
3902 out_loc,
3903 obj_loc,
3904 class_offset,
3905 maybe_temp_loc,
3906 kWithoutReadBarrier);
3907
3908 GenerateBitstringTypeCheckCompare(instruction, out);
3909 __ Cset(out, eq);
3910 if (zero.IsLinked()) {
3911 __ B(&done);
3912 }
3913 break;
3914 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003915 }
3916
3917 if (zero.IsLinked()) {
3918 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003919 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003920 }
3921
3922 if (done.IsLinked()) {
3923 __ Bind(&done);
3924 }
3925
3926 if (slow_path != nullptr) {
3927 __ Bind(slow_path->GetExitLabel());
3928 }
3929}
3930
3931void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003932 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00003933 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003934 LocationSummary* locations =
3935 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003936 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003937 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3938 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3939 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3940 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3941 } else {
3942 locations->SetInAt(1, Location::RequiresRegister());
3943 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003944 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3945 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003946}
3947
3948void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003949 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003950 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003951 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003952 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003953 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3954 ? Register()
3955 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003956 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3957 DCHECK_GE(num_temps, 1u);
3958 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003959 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003960 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3961 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003962 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003963 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3964 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3965 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3966 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3967 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3968 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3969 const uint32_t object_array_data_offset =
3970 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003971
Vladimir Marko87584542017-12-12 17:47:52 +00003972 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003973 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003974 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
3975 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003976 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003977
Scott Wakeling97c72b72016-06-24 16:19:36 +01003978 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003979 // Avoid null check if we know obj is not null.
3980 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003981 __ Cbz(obj, &done);
3982 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003983
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003984 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003985 case TypeCheckKind::kExactCheck:
3986 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003987 // /* HeapReference<Class> */ temp = obj->klass_
3988 GenerateReferenceLoadTwoRegisters(instruction,
3989 temp_loc,
3990 obj_loc,
3991 class_offset,
3992 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003993 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003994
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003995 __ Cmp(temp, cls);
3996 // Jump to slow path for throwing the exception or doing a
3997 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003998 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003999 break;
4000 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004001
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004002 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004003 // /* HeapReference<Class> */ temp = obj->klass_
4004 GenerateReferenceLoadTwoRegisters(instruction,
4005 temp_loc,
4006 obj_loc,
4007 class_offset,
4008 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004009 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004010
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004011 // If the class is abstract, we eagerly fetch the super class of the
4012 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004013 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004014 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004015 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004016 GenerateReferenceLoadOneRegister(instruction,
4017 temp_loc,
4018 super_offset,
4019 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004020 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004021
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004022 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4023 // exception.
4024 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4025 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004026 __ Cmp(temp, cls);
4027 __ B(ne, &loop);
4028 break;
4029 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004030
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004031 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004032 // /* HeapReference<Class> */ temp = obj->klass_
4033 GenerateReferenceLoadTwoRegisters(instruction,
4034 temp_loc,
4035 obj_loc,
4036 class_offset,
4037 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004038 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004039
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004040 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004041 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004042 __ Bind(&loop);
4043 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004044 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004045
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004046 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004047 GenerateReferenceLoadOneRegister(instruction,
4048 temp_loc,
4049 super_offset,
4050 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004051 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004052
4053 // If the class reference currently in `temp` is not null, jump
4054 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004055 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004056 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004057 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004058 break;
4059 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004060
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004061 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004062 // /* HeapReference<Class> */ temp = obj->klass_
4063 GenerateReferenceLoadTwoRegisters(instruction,
4064 temp_loc,
4065 obj_loc,
4066 class_offset,
4067 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004068 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004069
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004070 // Do an exact check.
4071 __ Cmp(temp, cls);
4072 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004073
4074 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004075 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004076 GenerateReferenceLoadOneRegister(instruction,
4077 temp_loc,
4078 component_offset,
4079 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004080 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004081
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004082 // If the component type is null, jump to the slow path to throw the exception.
4083 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4084 // Otherwise, the object is indeed an array. Further check that this component type is not a
4085 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004086 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4087 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004088 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004089 break;
4090 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004091
Calin Juravle98893e12015-10-02 21:05:03 +01004092 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004093 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004094 //
4095 // We cannot directly call the CheckCast runtime entry point
4096 // without resorting to a type checking slow path here (i.e. by
4097 // calling InvokeRuntime directly), as it would require to
4098 // assign fixed registers for the inputs of this HInstanceOf
4099 // instruction (following the runtime calling convention), which
4100 // might be cluttered by the potential first read barrier
4101 // emission at the beginning of this method.
4102 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004103 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004104 case TypeCheckKind::kInterfaceCheck: {
4105 // /* HeapReference<Class> */ temp = obj->klass_
4106 GenerateReferenceLoadTwoRegisters(instruction,
4107 temp_loc,
4108 obj_loc,
4109 class_offset,
4110 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004111 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004112
4113 // /* HeapReference<Class> */ temp = temp->iftable_
4114 GenerateReferenceLoadTwoRegisters(instruction,
4115 temp_loc,
4116 temp_loc,
4117 iftable_offset,
4118 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004119 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004120 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004121 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004122 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004123 vixl::aarch64::Label start_loop;
4124 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004125 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004126 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4127 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004128 // Go to next interface.
4129 __ Add(temp, temp, 2 * kHeapReferenceSize);
4130 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004131 // Compare the classes and continue the loop if they do not match.
4132 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4133 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004134 break;
4135 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004136
4137 case TypeCheckKind::kBitstringCheck: {
4138 // /* HeapReference<Class> */ temp = obj->klass_
4139 GenerateReferenceLoadTwoRegisters(instruction,
4140 temp_loc,
4141 obj_loc,
4142 class_offset,
4143 maybe_temp2_loc,
4144 kWithoutReadBarrier);
4145
4146 GenerateBitstringTypeCheckCompare(instruction, temp);
4147 __ B(ne, type_check_slow_path->GetEntryLabel());
4148 break;
4149 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004150 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004151 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004152
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004153 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004154}
4155
Alexandre Rames5319def2014-10-23 10:03:10 +01004156void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004157 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004158 locations->SetOut(Location::ConstantLocation(constant));
4159}
4160
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004161void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004162 // Will be generated at use site.
4163}
4164
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004165void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004166 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004167 locations->SetOut(Location::ConstantLocation(constant));
4168}
4169
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004170void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004171 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004172}
4173
Calin Juravle175dc732015-08-25 15:42:32 +01004174void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4175 // The trampoline uses the same calling convention as dex calling conventions,
4176 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4177 // the method_idx.
4178 HandleInvoke(invoke);
4179}
4180
4181void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4182 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004183 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004184}
4185
Alexandre Rames5319def2014-10-23 10:03:10 +01004186void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004187 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004188 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004189}
4190
Alexandre Rames67555f72014-11-18 10:55:16 +00004191void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4192 HandleInvoke(invoke);
4193}
4194
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004195void CodeGeneratorARM64::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
4196 Register klass) {
4197 DCHECK_EQ(klass.GetCode(), 0u);
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004198 // We know the destination of an intrinsic, so no need to record inline
4199 // caches.
4200 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00004201 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004202 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004203 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
4204 ScopedObjectAccess soa(Thread::Current());
4205 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00004206 if (info != nullptr) {
4207 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
4208 uint64_t address = reinterpret_cast64<uint64_t>(cache);
4209 vixl::aarch64::Label done;
4210 __ Mov(x8, address);
4211 __ Ldr(x9, MemOperand(x8, InlineCache::ClassesOffset().Int32Value()));
4212 // Fast path for a monomorphic cache.
4213 __ Cmp(klass, x9);
4214 __ B(eq, &done);
4215 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
4216 __ Bind(&done);
4217 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004218 }
4219}
4220
Alexandre Rames67555f72014-11-18 10:55:16 +00004221void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4222 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004223 LocationSummary* locations = invoke->GetLocations();
4224 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004225 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004226 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004227 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004228
Artem Serov914d7a82017-02-07 14:33:49 +00004229 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004230 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004231 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004232 {
4233 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4234 // /* HeapReference<Class> */ temp = temp->klass_
4235 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4236 codegen_->MaybeRecordImplicitNullCheck(invoke);
4237 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004238 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004239 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004240 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004241 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004242 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004243 }
Artem Serov914d7a82017-02-07 14:33:49 +00004244
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004245 // Instead of simply (possibly) unpoisoning `temp` here, we should
4246 // emit a read barrier for the previous class reference load.
4247 // However this is not required in practice, as this is an
4248 // intermediate/temporary reference and because the current
4249 // concurrent copying collector keeps the from-space memory
4250 // intact/accessible until the end of the marking phase (the
4251 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004252 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004253
4254 // If we're compiling baseline, update the inline cache.
4255 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
4256
4257 // The register ip1 is required to be used for the hidden argument in
4258 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
4259 MacroAssembler* masm = GetVIXLAssembler();
4260 UseScratchRegisterScope scratch_scope(masm);
4261 scratch_scope.Exclude(ip1);
4262 __ Mov(ip1, invoke->GetDexMethodIndex());
4263
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004264 __ Ldr(temp,
4265 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4266 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004267 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004268 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004269 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004270 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004271 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004272
4273 {
4274 // Ensure the pc position is recorded immediately after the `blr` instruction.
4275 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4276
4277 // lr();
4278 __ blr(lr);
4279 DCHECK(!codegen_->IsLeafMethod());
4280 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4281 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004282
Andreas Gampe3db70682018-12-26 15:12:03 -08004283 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004284}
4285
4286void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004287 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004288 if (intrinsic.TryDispatch(invoke)) {
4289 return;
4290 }
4291
Alexandre Rames67555f72014-11-18 10:55:16 +00004292 HandleInvoke(invoke);
4293}
4294
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004295void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004296 // Explicit clinit checks triggered by static invokes must have been pruned by
4297 // art::PrepareForRegisterAllocation.
4298 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004299
Vladimir Markoca6fff82017-10-03 14:49:14 +01004300 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004301 if (intrinsic.TryDispatch(invoke)) {
4302 return;
4303 }
4304
Alexandre Rames67555f72014-11-18 10:55:16 +00004305 HandleInvoke(invoke);
4306}
4307
Andreas Gampe878d58c2015-01-15 23:24:00 -08004308static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4309 if (invoke->GetLocations()->Intrinsified()) {
4310 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4311 intrinsic.Dispatch(invoke);
4312 return true;
4313 }
4314 return false;
4315}
4316
Vladimir Markodc151b22015-10-15 18:02:30 +01004317HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4318 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004319 ArtMethod* method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004320 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004321 return desired_dispatch_info;
4322}
4323
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004324void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4325 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004326 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004327 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4328 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004329 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4330 uint32_t offset =
4331 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004332 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004333 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004334 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004335 }
Vladimir Marko58155012015-08-19 12:49:41 +00004336 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004337 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004338 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004339 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004340 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004341 // Add ADRP with its PC-relative method patch.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004342 vixl::aarch64::Label* adrp_label = NewBootImageMethodPatch(invoke->GetTargetMethod());
Vladimir Marko65979462017-05-19 17:25:12 +01004343 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4344 // Add ADD with its PC-relative method patch.
4345 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004346 NewBootImageMethodPatch(invoke->GetTargetMethod(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004347 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4348 break;
4349 }
Vladimir Markob066d432018-01-03 13:14:37 +00004350 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4351 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004352 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004353 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4354 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4355 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4356 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4357 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4358 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4359 break;
4360 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004361 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004362 // Add ADRP with its PC-relative .bss entry patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004363 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4364 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004365 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004366 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004367 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004368 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004369 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004370 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004371 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004372 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004373 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
4374 // Load method address from literal pool.
4375 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
4376 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004377 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4378 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4379 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004380 }
4381 }
4382
4383 switch (invoke->GetCodePtrLocation()) {
4384 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004385 {
4386 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4387 ExactAssemblyScope eas(GetVIXLAssembler(),
4388 kInstructionSize,
4389 CodeBufferCheckScope::kExactSize);
4390 __ bl(&frame_entry_label_);
4391 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4392 }
Vladimir Marko58155012015-08-19 12:49:41 +00004393 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004394 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4395 // LR = callee_method->entry_point_from_quick_compiled_code_;
4396 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004397 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004398 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004399 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004400 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004401 ExactAssemblyScope eas(GetVIXLAssembler(),
4402 kInstructionSize,
4403 CodeBufferCheckScope::kExactSize);
4404 // lr()
4405 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004406 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004407 }
Vladimir Marko58155012015-08-19 12:49:41 +00004408 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004409 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004410
Andreas Gampe878d58c2015-01-15 23:24:00 -08004411 DCHECK(!IsLeafMethod());
4412}
4413
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004414void CodeGeneratorARM64::GenerateVirtualCall(
4415 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004416 // Use the calling convention instead of the location of the receiver, as
4417 // intrinsics may have put the receiver in a different register. In the intrinsics
4418 // slow path, the arguments have been moved to the right place, so here we are
4419 // guaranteed that the receiver is the first register of the calling convention.
4420 InvokeDexCallingConvention calling_convention;
4421 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004422 Register temp = XRegisterFrom(temp_in);
4423 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4424 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4425 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004426 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004427
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004428 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004429
4430 {
4431 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4432 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4433 // /* HeapReference<Class> */ temp = receiver->klass_
4434 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4435 MaybeRecordImplicitNullCheck(invoke);
4436 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004437 // Instead of simply (possibly) unpoisoning `temp` here, we should
4438 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004439 // intermediate/temporary reference and because the current
4440 // concurrent copying collector keeps the from-space memory
4441 // intact/accessible until the end of the marking phase (the
4442 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004443 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004444
4445 // If we're compiling baseline, update the inline cache.
4446 MaybeGenerateInlineCacheCheck(invoke, temp);
4447
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004448 // temp = temp->GetMethodAt(method_offset);
4449 __ Ldr(temp, MemOperand(temp, method_offset));
4450 // lr = temp->GetEntryPoint();
4451 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004452 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004453 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004454 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4455 // lr();
4456 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004457 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004458 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004459}
4460
Orion Hodsonac141392017-01-13 11:53:47 +00004461void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4462 HandleInvoke(invoke);
4463}
4464
4465void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4466 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004467 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004468}
4469
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004470void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4471 HandleInvoke(invoke);
4472}
4473
4474void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4475 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004476 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004477}
4478
Vladimir Marko6fd16062018-06-26 11:02:04 +01004479vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4480 uint32_t intrinsic_data,
4481 vixl::aarch64::Label* adrp_label) {
4482 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004483 /* dex_file= */ nullptr, intrinsic_data, adrp_label, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004484}
4485
Vladimir Markob066d432018-01-03 13:14:37 +00004486vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4487 uint32_t boot_image_offset,
4488 vixl::aarch64::Label* adrp_label) {
4489 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004490 /* dex_file= */ nullptr, boot_image_offset, adrp_label, &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00004491}
4492
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004493vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004494 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004495 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004496 return NewPcRelativePatch(
4497 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004498}
4499
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004500vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4501 MethodReference target_method,
4502 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004503 return NewPcRelativePatch(
4504 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004505}
4506
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004507vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004508 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004509 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004510 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004511 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004512}
4513
Vladimir Marko1998cd02017-01-13 13:02:58 +00004514vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4515 const DexFile& dex_file,
4516 dex::TypeIndex type_index,
4517 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004518 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004519}
4520
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004521vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004522 const DexFile& dex_file,
4523 dex::StringIndex string_index,
4524 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004525 return NewPcRelativePatch(
4526 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004527}
4528
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004529vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4530 const DexFile& dex_file,
4531 dex::StringIndex string_index,
4532 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004533 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004534}
4535
Vladimir Markof6675082019-05-17 12:05:28 +01004536void CodeGeneratorARM64::EmitEntrypointThunkCall(ThreadOffset64 entrypoint_offset) {
4537 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Markof91fc122020-05-13 09:21:00 +01004538 DCHECK(!UseJitCompilation());
Vladimir Markof6675082019-05-17 12:05:28 +01004539 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
4540 vixl::aarch64::Label* bl_label = &call_entrypoint_patches_.back().label;
4541 __ bind(bl_label);
4542 __ bl(static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4543}
4544
Vladimir Marko966b46f2018-08-03 10:20:19 +00004545void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
Vladimir Marko94796f82018-08-08 15:15:33 +01004546 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Markof91fc122020-05-13 09:21:00 +01004547 if (UseJitCompilation()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00004548 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
4549 vixl::aarch64::Label* slow_path_entry = &it->second.label;
4550 __ cbnz(mr, slow_path_entry);
4551 } else {
4552 baker_read_barrier_patches_.emplace_back(custom_data);
4553 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
4554 __ bind(cbnz_label);
4555 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4556 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004557}
4558
Scott Wakeling97c72b72016-06-24 16:19:36 +01004559vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004560 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004561 uint32_t offset_or_index,
4562 vixl::aarch64::Label* adrp_label,
4563 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004564 // Add a patch entry and return the label.
4565 patches->emplace_back(dex_file, offset_or_index);
4566 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004567 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004568 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4569 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4570 return label;
4571}
4572
Scott Wakeling97c72b72016-06-24 16:19:36 +01004573vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4574 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004575 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004576}
4577
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004578vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004579 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004580 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004581 return jit_string_patches_.GetOrCreate(
4582 StringReference(&dex_file, string_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004583 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004584}
4585
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004586vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004587 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004588 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004589 return jit_class_patches_.GetOrCreate(
4590 TypeReference(&dex_file, type_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004591 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004592}
4593
Vladimir Markoaad75c62016-10-03 08:46:48 +00004594void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4595 vixl::aarch64::Register reg) {
4596 DCHECK(reg.IsX());
4597 SingleEmissionCheckScope guard(GetVIXLAssembler());
4598 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004599 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004600}
4601
4602void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4603 vixl::aarch64::Register out,
4604 vixl::aarch64::Register base) {
4605 DCHECK(out.IsX());
4606 DCHECK(base.IsX());
4607 SingleEmissionCheckScope guard(GetVIXLAssembler());
4608 __ Bind(fixup_label);
4609 __ add(out, base, Operand(/* offset placeholder */ 0));
4610}
4611
4612void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4613 vixl::aarch64::Register out,
4614 vixl::aarch64::Register base) {
4615 DCHECK(base.IsX());
4616 SingleEmissionCheckScope guard(GetVIXLAssembler());
4617 __ Bind(fixup_label);
4618 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4619}
4620
Vladimir Markoeebb8212018-06-05 14:57:24 +01004621void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004622 uint32_t boot_image_reference) {
4623 if (GetCompilerOptions().IsBootImage()) {
4624 // Add ADRP with its PC-relative type patch.
4625 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
4626 EmitAdrpPlaceholder(adrp_label, reg.X());
4627 // Add ADD with its PC-relative type patch.
4628 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
4629 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Markoa2da9b92018-10-10 14:21:55 +01004630 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004631 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004632 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004633 EmitAdrpPlaceholder(adrp_label, reg.X());
4634 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004635 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_reference, adrp_label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004636 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
4637 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004638 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01004639 gc::Heap* heap = Runtime::Current()->GetHeap();
4640 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004641 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01004642 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
4643 }
4644}
4645
Vladimir Marko6fd16062018-06-26 11:02:04 +01004646void CodeGeneratorARM64::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
4647 uint32_t boot_image_offset) {
4648 DCHECK(invoke->IsStatic());
4649 InvokeRuntimeCallingConvention calling_convention;
4650 Register argument = calling_convention.GetRegisterAt(0);
4651 if (GetCompilerOptions().IsBootImage()) {
4652 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
4653 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
4654 MethodReference target_method = invoke->GetTargetMethod();
4655 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
4656 // Add ADRP with its PC-relative type patch.
4657 vixl::aarch64::Label* adrp_label = NewBootImageTypePatch(*target_method.dex_file, type_idx);
4658 EmitAdrpPlaceholder(adrp_label, argument.X());
4659 // Add ADD with its PC-relative type patch.
4660 vixl::aarch64::Label* add_label =
4661 NewBootImageTypePatch(*target_method.dex_file, type_idx, adrp_label);
4662 EmitAddPlaceholder(add_label, argument.X(), argument.X());
4663 } else {
4664 LoadBootImageAddress(argument, boot_image_offset);
4665 }
4666 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
4667 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4668}
4669
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004670template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004671inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4672 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004673 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004674 for (const PcRelativePatchInfo& info : infos) {
4675 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004676 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004677 info.pc_insn_label->GetLocation(),
4678 info.offset_or_index));
4679 }
4680}
4681
Vladimir Marko6fd16062018-06-26 11:02:04 +01004682template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
4683linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
4684 const DexFile* target_dex_file,
4685 uint32_t pc_insn_offset,
4686 uint32_t boot_image_offset) {
4687 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
4688 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00004689}
4690
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004691void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004692 DCHECK(linker_patches->empty());
4693 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004694 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004695 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004696 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004697 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004698 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004699 string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01004700 boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01004701 call_entrypoint_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004702 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004703 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01004704 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004705 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004706 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004707 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004708 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004709 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004710 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004711 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01004712 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004713 DCHECK(boot_image_type_patches_.empty());
4714 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01004715 }
4716 if (GetCompilerOptions().IsBootImage()) {
4717 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
4718 boot_image_other_patches_, linker_patches);
4719 } else {
4720 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
4721 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004722 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004723 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4724 method_bss_entry_patches_, linker_patches);
4725 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4726 type_bss_entry_patches_, linker_patches);
4727 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4728 string_bss_entry_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01004729 for (const PatchInfo<vixl::aarch64::Label>& info : call_entrypoint_patches_) {
4730 DCHECK(info.target_dex_file == nullptr);
4731 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
4732 info.label.GetLocation(), info.offset_or_index));
4733 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004734 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004735 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
4736 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004737 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004738 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004739}
4740
Vladimir Markoca1e0382018-04-11 09:58:41 +00004741bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01004742 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
4743 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00004744 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
4745}
4746
4747void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
4748 /*out*/ ArenaVector<uint8_t>* code,
4749 /*out*/ std::string* debug_name) {
4750 Arm64Assembler assembler(GetGraph()->GetAllocator());
4751 switch (patch.GetType()) {
4752 case linker::LinkerPatch::Type::kCallRelative: {
4753 // The thunk just uses the entry point in the ArtMethod. This works even for calls
4754 // to the generic JNI and interpreter trampolines.
4755 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4756 kArm64PointerSize).Int32Value());
4757 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
4758 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4759 *debug_name = "MethodCallThunk";
4760 }
4761 break;
4762 }
Vladimir Markof6675082019-05-17 12:05:28 +01004763 case linker::LinkerPatch::Type::kCallEntrypoint: {
4764 Offset offset(patch.EntrypointOffset());
4765 assembler.JumpTo(ManagedRegister(arm64::TR), offset, ManagedRegister(arm64::IP0));
4766 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4767 *debug_name = "EntrypointCallThunk_" + std::to_string(offset.Uint32Value());
4768 }
4769 break;
4770 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00004771 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
4772 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
4773 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
4774 break;
4775 }
4776 default:
4777 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
4778 UNREACHABLE();
4779 }
4780
4781 // Ensure we emit the literal pool if any.
4782 assembler.FinalizeCode();
4783 code->resize(assembler.CodeSize());
4784 MemoryRegion code_region(code->data(), code->size());
4785 assembler.FinalizeInstructions(code_region);
4786}
4787
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004788vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4789 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004790 value,
4791 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4792}
4793
Scott Wakeling97c72b72016-06-24 16:19:36 +01004794vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004795 return uint64_literals_.GetOrCreate(
4796 value,
4797 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004798}
4799
Andreas Gampe878d58c2015-01-15 23:24:00 -08004800void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004801 // Explicit clinit checks triggered by static invokes must have been pruned by
4802 // art::PrepareForRegisterAllocation.
4803 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004804
Andreas Gampe878d58c2015-01-15 23:24:00 -08004805 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08004806 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004807 return;
4808 }
4809
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004810 {
4811 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4812 // are no pools emitted.
4813 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4814 LocationSummary* locations = invoke->GetLocations();
4815 codegen_->GenerateStaticOrDirectCall(
4816 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
4817 }
4818
Andreas Gampe3db70682018-12-26 15:12:03 -08004819 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004820}
4821
4822void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004823 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08004824 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004825 return;
4826 }
4827
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004828 {
4829 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4830 // are no pools emitted.
4831 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4832 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
4833 DCHECK(!codegen_->IsLeafMethod());
4834 }
4835
Andreas Gampe3db70682018-12-26 15:12:03 -08004836 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004837}
4838
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004839HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4840 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004841 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004842 case HLoadClass::LoadKind::kInvalid:
4843 LOG(FATAL) << "UNREACHABLE";
4844 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004845 case HLoadClass::LoadKind::kReferrersClass:
4846 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004847 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004848 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004849 case HLoadClass::LoadKind::kBssEntry:
4850 DCHECK(!Runtime::Current()->UseJitCompilation());
4851 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004852 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004853 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004854 DCHECK(Runtime::Current()->UseJitCompilation());
4855 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004856 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004857 break;
4858 }
4859 return desired_class_load_kind;
4860}
4861
Alexandre Rames67555f72014-11-18 10:55:16 +00004862void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004863 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004864 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004865 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004866 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004867 cls,
4868 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004869 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004870 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004871 return;
4872 }
Vladimir Marko41559982017-01-06 14:04:23 +00004873 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004874
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004875 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4876 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004877 ? LocationSummary::kCallOnSlowPath
4878 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004879 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004880 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004881 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004882 }
4883
Vladimir Marko41559982017-01-06 14:04:23 +00004884 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004885 locations->SetInAt(0, Location::RequiresRegister());
4886 }
4887 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004888 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4889 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4890 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01004891 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004892 } else {
4893 // For non-Baker read barrier we have a temp-clobbering call.
4894 }
4895 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004896}
4897
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004898// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4899// move.
4900void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004901 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004902 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00004903 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08004904 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01004905 return;
4906 }
Vladimir Marko41559982017-01-06 14:04:23 +00004907 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004908
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004909 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004910 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004911
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004912 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4913 ? kWithoutReadBarrier
4914 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004915 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004916 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004917 case HLoadClass::LoadKind::kReferrersClass: {
4918 DCHECK(!cls->CanCallRuntime());
4919 DCHECK(!cls->MustGenerateClinitCheck());
4920 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4921 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00004922 codegen_->GenerateGcRootFieldLoad(cls,
4923 out_loc,
4924 current_method,
4925 ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08004926 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00004927 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004928 break;
4929 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004930 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004931 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
4932 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004933 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004934 // Add ADRP with its PC-relative type patch.
4935 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004936 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004937 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004938 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004939 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004940 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004941 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004942 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004943 break;
4944 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004945 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004946 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004947 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
4948 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
4949 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004950 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004951 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004952 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004953 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004954 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004955 break;
4956 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004957 case HLoadClass::LoadKind::kBssEntry: {
4958 // Add ADRP with its PC-relative Class .bss entry patch.
4959 const DexFile& dex_file = cls->GetDexFile();
4960 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00004961 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
4962 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4963 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004964 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004965 vixl::aarch64::Label* ldr_label =
Vladimir Markof3c52b42017-11-17 17:32:12 +00004966 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004967 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004968 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00004969 codegen_->GenerateGcRootFieldLoad(cls,
4970 out_loc,
4971 temp,
4972 /* offset placeholder */ 0u,
4973 ldr_label,
4974 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004975 generate_null_check = true;
4976 break;
4977 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004978 case HLoadClass::LoadKind::kJitBootImageAddress: {
4979 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
4980 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
4981 DCHECK_NE(address, 0u);
4982 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
4983 break;
4984 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004985 case HLoadClass::LoadKind::kJitTableAddress: {
4986 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4987 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004988 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00004989 codegen_->GenerateGcRootFieldLoad(cls,
4990 out_loc,
4991 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08004992 /* offset= */ 0,
4993 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00004994 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004995 break;
4996 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004997 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004998 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004999 LOG(FATAL) << "UNREACHABLE";
5000 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005001 }
5002
Vladimir Markoea4c1262017-02-06 19:59:33 +00005003 bool do_clinit = cls->MustGenerateClinitCheck();
5004 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005005 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01005006 SlowPathCodeARM64* slow_path =
5007 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005008 codegen_->AddSlowPath(slow_path);
5009 if (generate_null_check) {
5010 __ Cbz(out, slow_path->GetEntryLabel());
5011 }
5012 if (cls->MustGenerateClinitCheck()) {
5013 GenerateClassInitializationCheck(slow_path, out);
5014 } else {
5015 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005016 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005017 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005018 }
5019}
5020
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005021void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5022 InvokeRuntimeCallingConvention calling_convention;
5023 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5024 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5025}
5026
5027void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5028 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5029}
5030
Orion Hodson18259d72018-04-12 11:18:23 +01005031void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5032 InvokeRuntimeCallingConvention calling_convention;
5033 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5034 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5035}
5036
5037void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5038 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5039}
5040
David Brazdilcb1c0552015-08-04 16:22:25 +01005041static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005042 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005043}
5044
Alexandre Rames67555f72014-11-18 10:55:16 +00005045void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5046 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005047 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005048 locations->SetOut(Location::RequiresRegister());
5049}
5050
5051void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005052 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5053}
5054
5055void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005056 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005057}
5058
5059void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5060 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005061}
5062
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005063HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5064 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005065 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005066 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005067 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005068 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005069 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005070 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005071 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005072 case HLoadString::LoadKind::kJitTableAddress:
5073 DCHECK(Runtime::Current()->UseJitCompilation());
5074 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005075 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005076 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005077 }
5078 return desired_string_load_kind;
5079}
5080
Alexandre Rames67555f72014-11-18 10:55:16 +00005081void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005082 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005083 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005084 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005085 InvokeRuntimeCallingConvention calling_convention;
5086 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5087 } else {
5088 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005089 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5090 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005091 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005092 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005093 } else {
5094 // For non-Baker read barrier we have a temp-clobbering call.
5095 }
5096 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005097 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005098}
5099
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005100// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5101// move.
5102void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005103 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005104 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005105
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005106 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005107 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005108 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5109 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005110 // Add ADRP with its PC-relative String patch.
5111 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005112 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005113 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005114 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005115 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005116 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005117 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005118 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005119 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005120 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005121 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005122 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005123 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5124 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
5125 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005126 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005127 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005128 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005129 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005130 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5131 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005132 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005133 case HLoadString::LoadKind::kBssEntry: {
5134 // Add ADRP with its PC-relative String .bss entry patch.
5135 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005136 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005137 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005138 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005139 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005140 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005141 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005142 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005143 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005144 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005145 codegen_->GenerateGcRootFieldLoad(load,
5146 out_loc,
5147 temp,
5148 /* offset placeholder */ 0u,
5149 ldr_label,
5150 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005151 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005152 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005153 codegen_->AddSlowPath(slow_path);
5154 __ Cbz(out.X(), slow_path->GetEntryLabel());
5155 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08005156 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005157 return;
5158 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005159 case HLoadString::LoadKind::kJitBootImageAddress: {
5160 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
5161 DCHECK_NE(address, 0u);
5162 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5163 return;
5164 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005165 case HLoadString::LoadKind::kJitTableAddress: {
5166 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005167 load->GetStringIndex(),
5168 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005169 codegen_->GenerateGcRootFieldLoad(load,
5170 out_loc,
5171 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005172 /* offset= */ 0,
5173 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005174 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005175 return;
5176 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005177 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005178 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005179 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005180
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005181 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005182 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005183 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005184 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005185 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5186 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005187 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005188}
5189
Alexandre Rames5319def2014-10-23 10:03:10 +01005190void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005191 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005192 locations->SetOut(Location::ConstantLocation(constant));
5193}
5194
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005195void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005196 // Will be generated at use site.
5197}
5198
Alexandre Rames67555f72014-11-18 10:55:16 +00005199void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005200 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5201 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005202 InvokeRuntimeCallingConvention calling_convention;
5203 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5204}
5205
5206void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005207 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005208 instruction,
5209 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005210 if (instruction->IsEnter()) {
5211 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5212 } else {
5213 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5214 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005215 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005216}
5217
Alexandre Rames42d641b2014-10-27 14:00:51 +00005218void LocationsBuilderARM64::VisitMul(HMul* mul) {
5219 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005220 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005221 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005222 case DataType::Type::kInt32:
5223 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005224 locations->SetInAt(0, Location::RequiresRegister());
5225 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005226 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005227 break;
5228
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005229 case DataType::Type::kFloat32:
5230 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005231 locations->SetInAt(0, Location::RequiresFpuRegister());
5232 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005233 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005234 break;
5235
5236 default:
5237 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5238 }
5239}
5240
5241void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5242 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005243 case DataType::Type::kInt32:
5244 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005245 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5246 break;
5247
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005248 case DataType::Type::kFloat32:
5249 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005250 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005251 break;
5252
5253 default:
5254 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5255 }
5256}
5257
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005258void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5259 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005260 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005261 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005262 case DataType::Type::kInt32:
5263 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005264 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005265 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005266 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005267
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005268 case DataType::Type::kFloat32:
5269 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005270 locations->SetInAt(0, Location::RequiresFpuRegister());
5271 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005272 break;
5273
5274 default:
5275 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5276 }
5277}
5278
5279void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5280 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005281 case DataType::Type::kInt32:
5282 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005283 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5284 break;
5285
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005286 case DataType::Type::kFloat32:
5287 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005288 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005289 break;
5290
5291 default:
5292 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5293 }
5294}
5295
5296void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005297 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5298 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005299 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005300 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005301 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5302 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005303}
5304
5305void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005306 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5307 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005308 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005309 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005310 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005311}
5312
Alexandre Rames5319def2014-10-23 10:03:10 +01005313void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005314 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5315 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005316 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005317 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005318 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005319}
5320
5321void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005322 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5323 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005324 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005325}
5326
5327void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005328 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005329 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005330 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005331}
5332
5333void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005334 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005335 case DataType::Type::kInt32:
5336 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005337 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005338 break;
5339
5340 default:
5341 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5342 }
5343}
5344
David Brazdil66d126e2015-04-03 16:02:44 +01005345void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005346 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005347 locations->SetInAt(0, Location::RequiresRegister());
5348 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5349}
5350
5351void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005352 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005353}
5354
Alexandre Rames5319def2014-10-23 10:03:10 +01005355void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005356 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5357 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005358}
5359
Calin Juravle2ae48182016-03-16 14:05:09 +00005360void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5361 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005362 return;
5363 }
Artem Serov914d7a82017-02-07 14:33:49 +00005364 {
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005365 // Ensure that between load and RecordPcInfo there are no pools emitted.
Artem Serov914d7a82017-02-07 14:33:49 +00005366 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5367 Location obj = instruction->GetLocations()->InAt(0);
5368 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5369 RecordPcInfo(instruction, instruction->GetDexPc());
5370 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005371}
5372
Calin Juravle2ae48182016-03-16 14:05:09 +00005373void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005374 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005375 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005376
5377 LocationSummary* locations = instruction->GetLocations();
5378 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005379
5380 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005381}
5382
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005383void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005384 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005385}
5386
Alexandre Rames67555f72014-11-18 10:55:16 +00005387void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5388 HandleBinaryOp(instruction);
5389}
5390
5391void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5392 HandleBinaryOp(instruction);
5393}
5394
Alexandre Rames3e69f162014-12-10 10:36:50 +00005395void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5396 LOG(FATAL) << "Unreachable";
5397}
5398
5399void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005400 if (instruction->GetNext()->IsSuspendCheck() &&
5401 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5402 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5403 // The back edge will generate the suspend check.
5404 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5405 }
5406
Alexandre Rames3e69f162014-12-10 10:36:50 +00005407 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5408}
5409
Alexandre Rames5319def2014-10-23 10:03:10 +01005410void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005411 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005412 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5413 if (location.IsStackSlot()) {
5414 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5415 } else if (location.IsDoubleStackSlot()) {
5416 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5417 }
5418 locations->SetOut(location);
5419}
5420
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005421void InstructionCodeGeneratorARM64::VisitParameterValue(
5422 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005423 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005424}
5425
5426void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5427 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005428 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005429 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005430}
5431
5432void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5433 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5434 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005435}
5436
5437void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005438 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005439 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005440 locations->SetInAt(i, Location::Any());
5441 }
5442 locations->SetOut(Location::Any());
5443}
5444
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005445void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005446 LOG(FATAL) << "Unreachable";
5447}
5448
Serban Constantinescu02164b32014-11-13 14:05:07 +00005449void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005450 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005451 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005452 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005453 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005454 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005455
5456 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005457 case DataType::Type::kInt32:
5458 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005459 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005460 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005461 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5462 break;
5463
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005464 case DataType::Type::kFloat32:
5465 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005466 InvokeRuntimeCallingConvention calling_convention;
5467 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5468 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5469 locations->SetOut(calling_convention.GetReturnLocation(type));
5470
5471 break;
5472 }
5473
Serban Constantinescu02164b32014-11-13 14:05:07 +00005474 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005475 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005476 }
5477}
5478
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005479void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005480 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005481 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5482 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5483
5484 Register out = OutputRegister(instruction);
5485 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005486
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005487 if (abs_imm == 2) {
5488 __ Cmp(dividend, 0);
5489 __ And(out, dividend, 1);
5490 __ Csneg(out, out, out, ge);
5491 } else {
5492 UseScratchRegisterScope temps(GetVIXLAssembler());
5493 Register temp = temps.AcquireSameSizeAs(out);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005494
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005495 __ Negs(temp, dividend);
5496 __ And(out, dividend, abs_imm - 1);
5497 __ And(temp, temp, abs_imm - 1);
5498 __ Csneg(out, out, temp, mi);
5499 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005500}
5501
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005502void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005503 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005504
5505 if (imm == 0) {
5506 // Do not generate anything.
5507 // DivZeroCheck would prevent any code to be executed.
5508 return;
5509 }
5510
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01005511 if (IsPowerOfTwo(AbsOrMin(imm))) {
5512 // Cases imm == -1 or imm == 1 are handled in constant folding by
5513 // InstructionWithAbsorbingInputSimplifier.
5514 // If the cases have survided till code generation they are handled in
5515 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
5516 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005517 GenerateIntRemForPower2Denom(instruction);
5518 } else {
5519 DCHECK(imm < -2 || imm > 2) << imm;
5520 GenerateDivRemWithAnyConstant(instruction);
5521 }
5522}
5523
5524void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
5525 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
5526 << instruction->GetResultType();
5527
5528 if (instruction->GetLocations()->InAt(1).IsConstant()) {
5529 GenerateIntRemForConstDenom(instruction);
5530 } else {
5531 Register out = OutputRegister(instruction);
5532 Register dividend = InputRegisterAt(instruction, 0);
5533 Register divisor = InputRegisterAt(instruction, 1);
5534 UseScratchRegisterScope temps(GetVIXLAssembler());
5535 Register temp = temps.AcquireSameSizeAs(out);
5536 __ Sdiv(temp, dividend, divisor);
5537 __ Msub(out, temp, divisor, dividend);
5538 }
5539}
5540
Serban Constantinescu02164b32014-11-13 14:05:07 +00005541void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005542 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005543
Serban Constantinescu02164b32014-11-13 14:05:07 +00005544 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005545 case DataType::Type::kInt32:
5546 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005547 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005548 break;
5549 }
5550
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005551 case DataType::Type::kFloat32:
5552 case DataType::Type::kFloat64: {
5553 QuickEntrypointEnum entrypoint =
5554 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005555 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005556 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005557 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5558 } else {
5559 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5560 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005561 break;
5562 }
5563
Serban Constantinescu02164b32014-11-13 14:05:07 +00005564 default:
5565 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005566 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005567 }
5568}
5569
Aart Bik1f8d51b2018-02-15 10:42:37 -08005570void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005571 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005572}
5573
Aart Bik1f8d51b2018-02-15 10:42:37 -08005574void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005575 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005576}
5577
5578void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005579 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005580}
5581
5582void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005583 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005584}
5585
Aart Bik3dad3412018-02-28 12:01:46 -08005586void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
5587 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5588 switch (abs->GetResultType()) {
5589 case DataType::Type::kInt32:
5590 case DataType::Type::kInt64:
5591 locations->SetInAt(0, Location::RequiresRegister());
5592 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5593 break;
5594 case DataType::Type::kFloat32:
5595 case DataType::Type::kFloat64:
5596 locations->SetInAt(0, Location::RequiresFpuRegister());
5597 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5598 break;
5599 default:
5600 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5601 }
5602}
5603
5604void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
5605 switch (abs->GetResultType()) {
5606 case DataType::Type::kInt32:
5607 case DataType::Type::kInt64: {
5608 Register in_reg = InputRegisterAt(abs, 0);
5609 Register out_reg = OutputRegister(abs);
5610 __ Cmp(in_reg, Operand(0));
5611 __ Cneg(out_reg, in_reg, lt);
5612 break;
5613 }
5614 case DataType::Type::kFloat32:
5615 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01005616 VRegister in_reg = InputFPRegisterAt(abs, 0);
5617 VRegister out_reg = OutputFPRegister(abs);
Aart Bik3dad3412018-02-28 12:01:46 -08005618 __ Fabs(out_reg, in_reg);
5619 break;
5620 }
5621 default:
5622 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5623 }
5624}
5625
Igor Murashkind01745e2017-04-05 16:40:31 -07005626void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5627 constructor_fence->SetLocations(nullptr);
5628}
5629
5630void InstructionCodeGeneratorARM64::VisitConstructorFence(
5631 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5632 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5633}
5634
Calin Juravle27df7582015-04-17 19:12:31 +01005635void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5636 memory_barrier->SetLocations(nullptr);
5637}
5638
5639void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005640 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005641}
5642
Alexandre Rames5319def2014-10-23 10:03:10 +01005643void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005644 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005645 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005646 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005647}
5648
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00005649void InstructionCodeGeneratorARM64::VisitReturn(HReturn* ret) {
5650 if (GetGraph()->IsCompilingOsr()) {
5651 // To simplify callers of an OSR method, we put the return value in both
5652 // floating point and core register.
5653 switch (ret->InputAt(0)->GetType()) {
5654 case DataType::Type::kFloat32:
5655 __ Fmov(w0, s0);
5656 break;
5657 case DataType::Type::kFloat64:
5658 __ Fmov(x0, d0);
5659 break;
5660 default:
5661 break;
5662 }
5663 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005664 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005665}
5666
5667void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5668 instruction->SetLocations(nullptr);
5669}
5670
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005671void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005672 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005673}
5674
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005675void LocationsBuilderARM64::VisitRor(HRor* ror) {
5676 HandleBinaryOp(ror);
5677}
5678
5679void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5680 HandleBinaryOp(ror);
5681}
5682
Serban Constantinescu02164b32014-11-13 14:05:07 +00005683void LocationsBuilderARM64::VisitShl(HShl* shl) {
5684 HandleShift(shl);
5685}
5686
5687void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5688 HandleShift(shl);
5689}
5690
5691void LocationsBuilderARM64::VisitShr(HShr* shr) {
5692 HandleShift(shr);
5693}
5694
5695void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5696 HandleShift(shr);
5697}
5698
Alexandre Rames5319def2014-10-23 10:03:10 +01005699void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005700 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005701}
5702
5703void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005704 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005705}
5706
Alexandre Rames67555f72014-11-18 10:55:16 +00005707void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005708 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005709}
5710
5711void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005712 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005713}
5714
5715void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005716 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005717}
5718
Alexandre Rames67555f72014-11-18 10:55:16 +00005719void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005720 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005721}
5722
Vladimir Marko552a1342017-10-31 10:56:47 +00005723void LocationsBuilderARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5724 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(x0));
5725}
5726
5727void InstructionCodeGeneratorARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5728 __ Mov(w0, instruction->GetFormat()->GetValue());
5729 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5730}
5731
Calin Juravlee460d1d2015-09-29 04:52:17 +01005732void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5733 HUnresolvedInstanceFieldGet* instruction) {
5734 FieldAccessCallingConventionARM64 calling_convention;
5735 codegen_->CreateUnresolvedFieldLocationSummary(
5736 instruction, instruction->GetFieldType(), calling_convention);
5737}
5738
5739void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5740 HUnresolvedInstanceFieldGet* instruction) {
5741 FieldAccessCallingConventionARM64 calling_convention;
5742 codegen_->GenerateUnresolvedFieldAccess(instruction,
5743 instruction->GetFieldType(),
5744 instruction->GetFieldIndex(),
5745 instruction->GetDexPc(),
5746 calling_convention);
5747}
5748
5749void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5750 HUnresolvedInstanceFieldSet* instruction) {
5751 FieldAccessCallingConventionARM64 calling_convention;
5752 codegen_->CreateUnresolvedFieldLocationSummary(
5753 instruction, instruction->GetFieldType(), calling_convention);
5754}
5755
5756void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5757 HUnresolvedInstanceFieldSet* instruction) {
5758 FieldAccessCallingConventionARM64 calling_convention;
5759 codegen_->GenerateUnresolvedFieldAccess(instruction,
5760 instruction->GetFieldType(),
5761 instruction->GetFieldIndex(),
5762 instruction->GetDexPc(),
5763 calling_convention);
5764}
5765
5766void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5767 HUnresolvedStaticFieldGet* instruction) {
5768 FieldAccessCallingConventionARM64 calling_convention;
5769 codegen_->CreateUnresolvedFieldLocationSummary(
5770 instruction, instruction->GetFieldType(), calling_convention);
5771}
5772
5773void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5774 HUnresolvedStaticFieldGet* instruction) {
5775 FieldAccessCallingConventionARM64 calling_convention;
5776 codegen_->GenerateUnresolvedFieldAccess(instruction,
5777 instruction->GetFieldType(),
5778 instruction->GetFieldIndex(),
5779 instruction->GetDexPc(),
5780 calling_convention);
5781}
5782
5783void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5784 HUnresolvedStaticFieldSet* instruction) {
5785 FieldAccessCallingConventionARM64 calling_convention;
5786 codegen_->CreateUnresolvedFieldLocationSummary(
5787 instruction, instruction->GetFieldType(), calling_convention);
5788}
5789
5790void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5791 HUnresolvedStaticFieldSet* instruction) {
5792 FieldAccessCallingConventionARM64 calling_convention;
5793 codegen_->GenerateUnresolvedFieldAccess(instruction,
5794 instruction->GetFieldType(),
5795 instruction->GetFieldIndex(),
5796 instruction->GetDexPc(),
5797 calling_convention);
5798}
5799
Alexandre Rames5319def2014-10-23 10:03:10 +01005800void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005801 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5802 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005803 // In suspend check slow path, usually there are no caller-save registers at all.
5804 // If SIMD instructions are present, however, we force spilling all live SIMD
5805 // registers in full width (since the runtime only saves/restores lower part).
5806 locations->SetCustomSlowPathCallerSaves(
5807 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005808}
5809
5810void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005811 HBasicBlock* block = instruction->GetBlock();
5812 if (block->GetLoopInformation() != nullptr) {
5813 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5814 // The back edge will generate the suspend check.
5815 return;
5816 }
5817 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5818 // The goto will generate the suspend check.
5819 return;
5820 }
5821 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08005822 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005823}
5824
Alexandre Rames67555f72014-11-18 10:55:16 +00005825void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005826 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5827 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005828 InvokeRuntimeCallingConvention calling_convention;
5829 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5830}
5831
5832void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005833 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005834 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005835}
5836
5837void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5838 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005839 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005840 DataType::Type input_type = conversion->GetInputType();
5841 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005842 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5843 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005844 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
5845 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005846 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5847 }
5848
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005849 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005850 locations->SetInAt(0, Location::RequiresFpuRegister());
5851 } else {
5852 locations->SetInAt(0, Location::RequiresRegister());
5853 }
5854
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005855 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005856 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5857 } else {
5858 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5859 }
5860}
5861
5862void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005863 DataType::Type result_type = conversion->GetResultType();
5864 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00005865
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005866 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5867 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005868
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005869 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
5870 int result_size = DataType::Size(result_type);
5871 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005872 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005873 Register output = OutputRegister(conversion);
5874 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005875 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005876 // 'int' values are used directly as W registers, discarding the top
5877 // bits, so we don't need to sign-extend and can just perform a move.
5878 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5879 // top 32 bits of the target register. We theoretically could leave those
5880 // bits unchanged, but we would have to make sure that no code uses a
5881 // 32bit input value as a 64bit value assuming that the top 32 bits are
5882 // zero.
5883 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005884 } else if (DataType::IsUnsignedType(result_type) ||
5885 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
5886 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005887 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005888 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005889 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005890 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005891 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005892 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
5893 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005894 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005895 } else if (DataType::IsFloatingPointType(result_type) &&
5896 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005897 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5898 } else {
5899 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5900 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005901 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005902}
Alexandre Rames67555f72014-11-18 10:55:16 +00005903
Serban Constantinescu02164b32014-11-13 14:05:07 +00005904void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5905 HandleShift(ushr);
5906}
5907
5908void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5909 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005910}
5911
5912void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5913 HandleBinaryOp(instruction);
5914}
5915
5916void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5917 HandleBinaryOp(instruction);
5918}
5919
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005920void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005921 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005922 LOG(FATAL) << "Unreachable";
5923}
5924
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005925void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005926 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005927 LOG(FATAL) << "Unreachable";
5928}
5929
Mark Mendellfe57faa2015-09-18 09:26:15 -04005930// Simple implementation of packed switch - generate cascaded compare/jumps.
5931void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5932 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005933 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005934 locations->SetInAt(0, Location::RequiresRegister());
5935}
5936
5937void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5938 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005939 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005940 Register value_reg = InputRegisterAt(switch_instr, 0);
5941 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5942
Zheng Xu3927c8b2015-11-18 17:46:25 +08005943 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005944 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005945 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5946 // make sure we don't emit it if the target may run out of range.
5947 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5948 // ranges and emit the tables only as required.
5949 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005950
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005951 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005952 // Current instruction id is an upper bound of the number of HIRs in the graph.
5953 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5954 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005955 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5956 Register temp = temps.AcquireW();
5957 __ Subs(temp, value_reg, Operand(lower_bound));
5958
Zheng Xu3927c8b2015-11-18 17:46:25 +08005959 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005960 // Jump to successors[0] if value == lower_bound.
5961 __ B(eq, codegen_->GetLabelOf(successors[0]));
5962 int32_t last_index = 0;
5963 for (; num_entries - last_index > 2; last_index += 2) {
5964 __ Subs(temp, temp, Operand(2));
5965 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5966 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5967 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5968 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5969 }
5970 if (num_entries - last_index == 2) {
5971 // The last missing case_value.
5972 __ Cmp(temp, Operand(1));
5973 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005974 }
5975
5976 // And the default for any other value.
5977 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5978 __ B(codegen_->GetLabelOf(default_block));
5979 }
5980 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005981 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005982
5983 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5984
5985 // Below instructions should use at most one blocked register. Since there are two blocked
5986 // registers, we are free to block one.
5987 Register temp_w = temps.AcquireW();
5988 Register index;
5989 // Remove the bias.
5990 if (lower_bound != 0) {
5991 index = temp_w;
5992 __ Sub(index, value_reg, Operand(lower_bound));
5993 } else {
5994 index = value_reg;
5995 }
5996
5997 // Jump to default block if index is out of the range.
5998 __ Cmp(index, Operand(num_entries));
5999 __ B(hs, codegen_->GetLabelOf(default_block));
6000
6001 // In current VIXL implementation, it won't require any blocked registers to encode the
6002 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
6003 // register pressure.
6004 Register table_base = temps.AcquireX();
6005 // Load jump offset from the table.
6006 __ Adr(table_base, jump_table->GetTableStartLabel());
6007 Register jump_offset = temp_w;
6008 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
6009
6010 // Jump to target block by branching to table_base(pc related) + offset.
6011 Register target_address = table_base;
6012 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
6013 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006014 }
6015}
6016
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006017void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
6018 HInstruction* instruction,
6019 Location out,
6020 uint32_t offset,
6021 Location maybe_temp,
6022 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006023 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006024 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006025 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006026 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006027 if (kUseBakerReadBarrier) {
6028 // Load with fast path based Baker's read barrier.
6029 // /* HeapReference<Object> */ out = *(out + offset)
6030 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6031 out,
6032 out_reg,
6033 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006034 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006035 /* needs_null_check= */ false,
6036 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006037 } else {
6038 // Load with slow path based read barrier.
6039 // Save the value of `out` into `maybe_temp` before overwriting it
6040 // in the following move operation, as we will need it for the
6041 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006042 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00006043 __ Mov(temp_reg, out_reg);
6044 // /* HeapReference<Object> */ out = *(out + offset)
6045 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6046 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6047 }
6048 } else {
6049 // Plain load with no read barrier.
6050 // /* HeapReference<Object> */ out = *(out + offset)
6051 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6052 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6053 }
6054}
6055
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006056void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6057 HInstruction* instruction,
6058 Location out,
6059 Location obj,
6060 uint32_t offset,
6061 Location maybe_temp,
6062 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006063 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006064 Register out_reg = RegisterFrom(out, type);
6065 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006066 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006067 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006068 if (kUseBakerReadBarrier) {
6069 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006070 // /* HeapReference<Object> */ out = *(obj + offset)
6071 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6072 out,
6073 obj_reg,
6074 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006075 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006076 /* needs_null_check= */ false,
6077 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006078 } else {
6079 // Load with slow path based read barrier.
6080 // /* HeapReference<Object> */ out = *(obj + offset)
6081 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6082 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6083 }
6084 } else {
6085 // Plain load with no read barrier.
6086 // /* HeapReference<Object> */ out = *(obj + offset)
6087 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6088 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6089 }
6090}
6091
Vladimir Markoca1e0382018-04-11 09:58:41 +00006092void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006093 HInstruction* instruction,
6094 Location root,
6095 Register obj,
6096 uint32_t offset,
6097 vixl::aarch64::Label* fixup_label,
6098 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006099 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006100 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006101 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006102 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006103 if (kUseBakerReadBarrier) {
6104 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006105 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00006106
Vladimir Marko008e09f32018-08-06 15:42:43 +01006107 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6108 // the Marking Register) to decide whether we need to enter
6109 // the slow path to mark the GC root.
6110 //
6111 // We use shared thunks for the slow path; shared within the method
6112 // for JIT, across methods for AOT. That thunk checks the reference
6113 // and jumps to the entrypoint if needed.
6114 //
6115 // lr = &return_address;
6116 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6117 // if (mr) { // Thread::Current()->GetIsGcMarking()
6118 // goto gc_root_thunk<root_reg>(lr)
6119 // }
6120 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00006121
Vladimir Marko008e09f32018-08-06 15:42:43 +01006122 UseScratchRegisterScope temps(GetVIXLAssembler());
6123 DCHECK(temps.IsAvailable(ip0));
6124 DCHECK(temps.IsAvailable(ip1));
6125 temps.Exclude(ip0, ip1);
6126 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillain44015862016-01-22 11:47:17 +00006127
Vladimir Marko008e09f32018-08-06 15:42:43 +01006128 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6129 vixl::aarch64::Label return_address;
6130 __ adr(lr, &return_address);
6131 if (fixup_label != nullptr) {
6132 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006133 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006134 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
Vladimir Marko94796f82018-08-08 15:15:33 +01006135 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Marko008e09f32018-08-06 15:42:43 +01006136 __ ldr(root_reg, MemOperand(obj.X(), offset));
6137 EmitBakerReadBarrierCbnz(custom_data);
6138 __ bind(&return_address);
Roland Levillain44015862016-01-22 11:47:17 +00006139 } else {
6140 // GC root loaded through a slow path for read barriers other
6141 // than Baker's.
6142 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006143 if (fixup_label == nullptr) {
6144 __ Add(root_reg.X(), obj.X(), offset);
6145 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006146 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006147 }
Roland Levillain44015862016-01-22 11:47:17 +00006148 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006149 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006150 }
6151 } else {
6152 // Plain GC root load with no read barrier.
6153 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006154 if (fixup_label == nullptr) {
6155 __ Ldr(root_reg, MemOperand(obj, offset));
6156 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006157 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006158 }
Roland Levillain44015862016-01-22 11:47:17 +00006159 // Note that GC roots are not affected by heap poisoning, thus we
6160 // do not have to unpoison `root_reg` here.
6161 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006162 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006163}
6164
Vladimir Marko94796f82018-08-08 15:15:33 +01006165void CodeGeneratorARM64::GenerateUnsafeCasOldValueMovWithBakerReadBarrier(
6166 vixl::aarch64::Register marked,
6167 vixl::aarch64::Register old_value) {
6168 DCHECK(kEmitCompilerReadBarrier);
6169 DCHECK(kUseBakerReadBarrier);
6170
6171 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
6172 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(marked.GetCode());
6173
6174 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6175 vixl::aarch64::Label return_address;
6176 __ adr(lr, &return_address);
6177 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6178 "GC root LDR must be 2 instructions (8B) before the return address label.");
6179 __ mov(marked, old_value);
6180 EmitBakerReadBarrierCbnz(custom_data);
6181 __ bind(&return_address);
6182}
6183
Roland Levillain44015862016-01-22 11:47:17 +00006184void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6185 Location ref,
Vladimir Marko248141f2018-08-10 10:40:07 +01006186 vixl::aarch64::Register obj,
6187 const vixl::aarch64::MemOperand& src,
Roland Levillain44015862016-01-22 11:47:17 +00006188 bool needs_null_check,
6189 bool use_load_acquire) {
6190 DCHECK(kEmitCompilerReadBarrier);
6191 DCHECK(kUseBakerReadBarrier);
6192
Vladimir Marko0ecac682018-08-07 10:40:38 +01006193 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6194 // Marking Register) to decide whether we need to enter the slow
6195 // path to mark the reference. Then, in the slow path, check the
6196 // gray bit in the lock word of the reference's holder (`obj`) to
6197 // decide whether to mark `ref` or not.
6198 //
6199 // We use shared thunks for the slow path; shared within the method
6200 // for JIT, across methods for AOT. That thunk checks the holder
6201 // and jumps to the entrypoint if needed. If the holder is not gray,
6202 // it creates a fake dependency and returns to the LDR instruction.
6203 //
6204 // lr = &gray_return_address;
6205 // if (mr) { // Thread::Current()->GetIsGcMarking()
6206 // goto field_thunk<holder_reg, base_reg, use_load_acquire>(lr)
6207 // }
6208 // not_gray_return_address:
6209 // // Original reference load. If the offset is too large to fit
6210 // // into LDR, we use an adjusted base register here.
6211 // HeapReference<mirror::Object> reference = *(obj+offset);
6212 // gray_return_address:
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006213
Vladimir Marko248141f2018-08-10 10:40:07 +01006214 DCHECK(src.GetAddrMode() == vixl::aarch64::Offset);
6215 DCHECK_ALIGNED(src.GetOffset(), sizeof(mirror::HeapReference<mirror::Object>));
6216
6217 UseScratchRegisterScope temps(GetVIXLAssembler());
6218 DCHECK(temps.IsAvailable(ip0));
6219 DCHECK(temps.IsAvailable(ip1));
6220 temps.Exclude(ip0, ip1);
6221 uint32_t custom_data = use_load_acquire
6222 ? EncodeBakerReadBarrierAcquireData(src.GetBaseRegister().GetCode(), obj.GetCode())
6223 : EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode());
6224
6225 {
6226 ExactAssemblyScope guard(GetVIXLAssembler(),
6227 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6228 vixl::aarch64::Label return_address;
6229 __ adr(lr, &return_address);
6230 EmitBakerReadBarrierCbnz(custom_data);
6231 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6232 "Field LDR must be 1 instruction (4B) before the return address label; "
6233 " 2 instructions (8B) for heap poisoning.");
6234 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
6235 if (use_load_acquire) {
6236 DCHECK_EQ(src.GetOffset(), 0);
6237 __ ldar(ref_reg, src);
6238 } else {
6239 __ ldr(ref_reg, src);
6240 }
6241 if (needs_null_check) {
6242 MaybeRecordImplicitNullCheck(instruction);
6243 }
6244 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6245 // macro instructions disallowed in ExactAssemblyScope.
6246 if (kPoisonHeapReferences) {
6247 __ neg(ref_reg, Operand(ref_reg));
6248 }
6249 __ bind(&return_address);
6250 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006251 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Vladimir Marko248141f2018-08-10 10:40:07 +01006252}
6253
6254void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6255 Location ref,
6256 Register obj,
6257 uint32_t offset,
6258 Location maybe_temp,
6259 bool needs_null_check,
6260 bool use_load_acquire) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006261 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6262 Register base = obj;
6263 if (use_load_acquire) {
6264 DCHECK(maybe_temp.IsRegister());
6265 base = WRegisterFrom(maybe_temp);
6266 __ Add(base, obj, offset);
6267 offset = 0u;
6268 } else if (offset >= kReferenceLoadMinFarOffset) {
6269 DCHECK(maybe_temp.IsRegister());
6270 base = WRegisterFrom(maybe_temp);
6271 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6272 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6273 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006274 }
Vladimir Marko248141f2018-08-10 10:40:07 +01006275 MemOperand src(base.X(), offset);
6276 GenerateFieldLoadWithBakerReadBarrier(
6277 instruction, ref, obj, src, needs_null_check, use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006278}
6279
Artem Serov0806f582018-10-11 20:14:20 +01006280void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HArrayGet* instruction,
6281 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006282 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006283 uint32_t data_offset,
6284 Location index,
Roland Levillain44015862016-01-22 11:47:17 +00006285 bool needs_null_check) {
6286 DCHECK(kEmitCompilerReadBarrier);
6287 DCHECK(kUseBakerReadBarrier);
6288
Vladimir Marko66d691d2017-04-07 17:53:39 +01006289 static_assert(
6290 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6291 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006292 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006293
Vladimir Marko008e09f32018-08-06 15:42:43 +01006294 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6295 // Marking Register) to decide whether we need to enter the slow
6296 // path to mark the reference. Then, in the slow path, check the
6297 // gray bit in the lock word of the reference's holder (`obj`) to
6298 // decide whether to mark `ref` or not.
6299 //
6300 // We use shared thunks for the slow path; shared within the method
6301 // for JIT, across methods for AOT. That thunk checks the holder
6302 // and jumps to the entrypoint if needed. If the holder is not gray,
6303 // it creates a fake dependency and returns to the LDR instruction.
6304 //
6305 // lr = &gray_return_address;
6306 // if (mr) { // Thread::Current()->GetIsGcMarking()
6307 // goto array_thunk<base_reg>(lr)
6308 // }
6309 // not_gray_return_address:
6310 // // Original reference load. If the offset is too large to fit
6311 // // into LDR, we use an adjusted base register here.
6312 // HeapReference<mirror::Object> reference = data[index];
6313 // gray_return_address:
Vladimir Marko66d691d2017-04-07 17:53:39 +01006314
Vladimir Marko008e09f32018-08-06 15:42:43 +01006315 DCHECK(index.IsValid());
6316 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6317 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006318
Vladimir Marko008e09f32018-08-06 15:42:43 +01006319 UseScratchRegisterScope temps(GetVIXLAssembler());
6320 DCHECK(temps.IsAvailable(ip0));
6321 DCHECK(temps.IsAvailable(ip1));
6322 temps.Exclude(ip0, ip1);
Artem Serov0806f582018-10-11 20:14:20 +01006323
6324 Register temp;
6325 if (instruction->GetArray()->IsIntermediateAddress()) {
6326 // We do not need to compute the intermediate address from the array: the
6327 // input instruction has done it already. See the comment in
6328 // `TryExtractArrayAccessAddress()`.
6329 if (kIsDebugBuild) {
6330 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
6331 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6332 }
6333 temp = obj;
6334 } else {
6335 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
6336 __ Add(temp.X(), obj.X(), Operand(data_offset));
6337 }
6338
Vladimir Marko008e09f32018-08-06 15:42:43 +01006339 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006340
Vladimir Marko008e09f32018-08-06 15:42:43 +01006341 {
6342 ExactAssemblyScope guard(GetVIXLAssembler(),
6343 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6344 vixl::aarch64::Label return_address;
6345 __ adr(lr, &return_address);
6346 EmitBakerReadBarrierCbnz(custom_data);
6347 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6348 "Array LDR must be 1 instruction (4B) before the return address label; "
6349 " 2 instructions (8B) for heap poisoning.");
6350 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6351 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6352 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6353 // macro instructions disallowed in ExactAssemblyScope.
6354 if (kPoisonHeapReferences) {
6355 __ neg(ref_reg, Operand(ref_reg));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006356 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006357 __ bind(&return_address);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006358 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006359 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Roland Levillain44015862016-01-22 11:47:17 +00006360}
6361
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006362void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6363 // The following condition is a compile-time one, so it does not have a run-time cost.
6364 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6365 // The following condition is a run-time one; it is executed after the
6366 // previous compile-time test, to avoid penalizing non-debug builds.
6367 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6368 UseScratchRegisterScope temps(GetVIXLAssembler());
6369 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6370 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6371 }
6372 }
6373}
6374
Roland Levillain44015862016-01-22 11:47:17 +00006375void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6376 Location out,
6377 Location ref,
6378 Location obj,
6379 uint32_t offset,
6380 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006381 DCHECK(kEmitCompilerReadBarrier);
6382
Roland Levillain44015862016-01-22 11:47:17 +00006383 // Insert a slow path based read barrier *after* the reference load.
6384 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006385 // If heap poisoning is enabled, the unpoisoning of the loaded
6386 // reference will be carried out by the runtime within the slow
6387 // path.
6388 //
6389 // Note that `ref` currently does not get unpoisoned (when heap
6390 // poisoning is enabled), which is alright as the `ref` argument is
6391 // not used by the artReadBarrierSlow entry point.
6392 //
6393 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006394 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006395 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6396 AddSlowPath(slow_path);
6397
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006398 __ B(slow_path->GetEntryLabel());
6399 __ Bind(slow_path->GetExitLabel());
6400}
6401
Roland Levillain44015862016-01-22 11:47:17 +00006402void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6403 Location out,
6404 Location ref,
6405 Location obj,
6406 uint32_t offset,
6407 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006408 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006409 // Baker's read barriers shall be handled by the fast path
6410 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6411 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006412 // If heap poisoning is enabled, unpoisoning will be taken care of
6413 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006414 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006415 } else if (kPoisonHeapReferences) {
6416 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6417 }
6418}
6419
Roland Levillain44015862016-01-22 11:47:17 +00006420void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6421 Location out,
6422 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006423 DCHECK(kEmitCompilerReadBarrier);
6424
Roland Levillain44015862016-01-22 11:47:17 +00006425 // Insert a slow path based read barrier *after* the GC root load.
6426 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006427 // Note that GC roots are not affected by heap poisoning, so we do
6428 // not need to do anything special for this here.
6429 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006430 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006431 AddSlowPath(slow_path);
6432
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006433 __ B(slow_path->GetEntryLabel());
6434 __ Bind(slow_path->GetExitLabel());
6435}
6436
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006437void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6438 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006439 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006440 locations->SetInAt(0, Location::RequiresRegister());
6441 locations->SetOut(Location::RequiresRegister());
6442}
6443
6444void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6445 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006446 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006447 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006448 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006449 __ Ldr(XRegisterFrom(locations->Out()),
6450 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006451 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006452 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006453 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006454 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6455 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006456 __ Ldr(XRegisterFrom(locations->Out()),
6457 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006458 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006459}
6460
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006461static void PatchJitRootUse(uint8_t* code,
6462 const uint8_t* roots_data,
6463 vixl::aarch64::Literal<uint32_t>* literal,
6464 uint64_t index_in_table) {
6465 uint32_t literal_offset = literal->GetOffset();
6466 uintptr_t address =
6467 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6468 uint8_t* data = code + literal_offset;
6469 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6470}
6471
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006472void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6473 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006474 const StringReference& string_reference = entry.first;
6475 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006476 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006477 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006478 }
6479 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006480 const TypeReference& type_reference = entry.first;
6481 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006482 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006483 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006484 }
6485}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006486
Artem Serov1a719e42019-07-18 14:24:55 +01006487MemOperand InstructionCodeGeneratorARM64::VecNeonAddress(
6488 HVecMemoryOperation* instruction,
6489 UseScratchRegisterScope* temps_scope,
6490 size_t size,
6491 bool is_string_char_at,
6492 /*out*/ Register* scratch) {
6493 LocationSummary* locations = instruction->GetLocations();
6494 Register base = InputRegisterAt(instruction, 0);
6495
6496 if (instruction->InputAt(1)->IsIntermediateAddressIndex()) {
6497 DCHECK(!is_string_char_at);
6498 return MemOperand(base.X(), InputRegisterAt(instruction, 1).X());
6499 }
6500
6501 Location index = locations->InAt(1);
6502 uint32_t offset = is_string_char_at
6503 ? mirror::String::ValueOffset().Uint32Value()
6504 : mirror::Array::DataOffset(size).Uint32Value();
6505 size_t shift = ComponentSizeShiftWidth(size);
6506
6507 // HIntermediateAddress optimization is only applied for scalar ArrayGet and ArraySet.
6508 DCHECK(!instruction->InputAt(0)->IsIntermediateAddress());
6509
6510 if (index.IsConstant()) {
6511 offset += Int64FromLocation(index) << shift;
6512 return HeapOperand(base, offset);
6513 } else {
6514 *scratch = temps_scope->AcquireSameSizeAs(base);
6515 __ Add(*scratch, base, Operand(WRegisterFrom(index), LSL, shift));
6516 return HeapOperand(*scratch, offset);
6517 }
6518}
6519
Alexandre Rames67555f72014-11-18 10:55:16 +00006520#undef __
6521#undef QUICK_ENTRY_POINT
6522
Vladimir Markoca1e0382018-04-11 09:58:41 +00006523#define __ assembler.GetVIXLAssembler()->
6524
6525static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
6526 vixl::aarch64::Register base_reg,
6527 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01006528 vixl::aarch64::Label* slow_path,
6529 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006530 // Load the lock word containing the rb_state.
6531 __ Ldr(ip0.W(), lock_word);
6532 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01006533 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00006534 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6535 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
6536 static_assert(
6537 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
6538 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01006539 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
6540 if (throw_npe != nullptr) {
6541 __ Bind(throw_npe);
6542 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006543 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
6544 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6545 "Field LDR must be 1 instruction (4B) before the return address label; "
6546 " 2 instructions (8B) for heap poisoning.");
6547 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6548 // Introduce a dependency on the lock_word including rb_state,
6549 // to prevent load-load reordering, and without using
6550 // a memory barrier (which would be more expensive).
6551 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
6552 __ Br(lr); // And return back to the function.
6553 // Note: The fake dependency is unnecessary for the slow path.
6554}
6555
6556// Load the read barrier introspection entrypoint in register `entrypoint`.
6557static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
6558 vixl::aarch64::Register entrypoint) {
6559 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6560 DCHECK_EQ(ip0.GetCode(), 16u);
6561 const int32_t entry_point_offset =
6562 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6563 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
6564}
6565
6566void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
6567 uint32_t encoded_data,
6568 /*out*/ std::string* debug_name) {
6569 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
6570 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006571 case BakerReadBarrierKind::kField:
6572 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006573 auto base_reg =
6574 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6575 CheckValidReg(base_reg.GetCode());
6576 auto holder_reg =
6577 Register::GetXRegFromCode(BakerReadBarrierSecondRegField::Decode(encoded_data));
6578 CheckValidReg(holder_reg.GetCode());
6579 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6580 temps.Exclude(ip0, ip1);
Roland Levillain988c3912019-09-25 19:33:35 +01006581 // In the case of a field load (with relaxed semantic), if `base_reg` differs from
6582 // `holder_reg`, the offset was too large and we must have emitted (during the construction
6583 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
6584 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
6585 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
6586 // not necessarily do that check before going to the thunk.
6587 //
6588 // In the case of a field load with load-acquire semantics (where `base_reg` always differs
6589 // from `holder_reg`), we also need an explicit null check when implicit null checks are
6590 // allowed, as we do not emit one before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01006591 vixl::aarch64::Label throw_npe_label;
6592 vixl::aarch64::Label* throw_npe = nullptr;
Roland Levillain988c3912019-09-25 19:33:35 +01006593 if (GetCompilerOptions().GetImplicitNullChecks() &&
6594 (holder_reg.Is(base_reg) || (kind == BakerReadBarrierKind::kAcquire))) {
Vladimir Marko7a695052018-04-12 10:26:50 +01006595 throw_npe = &throw_npe_label;
6596 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006597 }
Vladimir Marko7a695052018-04-12 10:26:50 +01006598 // Check if the holder is gray and, if not, add fake dependency to the base register
6599 // and return to the LDR instruction to load the reference. Otherwise, use introspection
6600 // to load the reference and call the entrypoint that performs further checks on the
6601 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006602 vixl::aarch64::Label slow_path;
6603 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01006604 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006605 __ Bind(&slow_path);
Vladimir Marko0ecac682018-08-07 10:40:38 +01006606 if (kind == BakerReadBarrierKind::kField) {
6607 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6608 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
6609 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6610 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
6611 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
6612 } else {
6613 DCHECK(kind == BakerReadBarrierKind::kAcquire);
6614 DCHECK(!base_reg.Is(holder_reg));
6615 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6616 __ Ldar(ip0.W(), MemOperand(base_reg));
6617 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006618 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
6619 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006620 break;
6621 }
6622 case BakerReadBarrierKind::kArray: {
6623 auto base_reg =
6624 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6625 CheckValidReg(base_reg.GetCode());
6626 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6627 BakerReadBarrierSecondRegField::Decode(encoded_data));
6628 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6629 temps.Exclude(ip0, ip1);
6630 vixl::aarch64::Label slow_path;
6631 int32_t data_offset =
6632 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
6633 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
6634 DCHECK_LT(lock_word.GetOffset(), 0);
6635 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
6636 __ Bind(&slow_path);
6637 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
6638 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
6639 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6640 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
6641 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
6642 // a switch case target based on the index register.
6643 __ Mov(ip0, base_reg); // Move the base register to ip0.
6644 __ Br(ip1); // Jump to the entrypoint's array switch case.
6645 break;
6646 }
6647 case BakerReadBarrierKind::kGcRoot: {
6648 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
6649 // and it does not have a forwarding address), call the correct introspection entrypoint;
6650 // otherwise return the reference (or the extracted forwarding address).
6651 // There is no gray bit check for GC roots.
6652 auto root_reg =
6653 Register::GetWRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6654 CheckValidReg(root_reg.GetCode());
6655 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6656 BakerReadBarrierSecondRegField::Decode(encoded_data));
6657 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6658 temps.Exclude(ip0, ip1);
6659 vixl::aarch64::Label return_label, not_marked, forwarding_address;
6660 __ Cbz(root_reg, &return_label);
6661 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
6662 __ Ldr(ip0.W(), lock_word);
6663 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
6664 __ Bind(&return_label);
6665 __ Br(lr);
6666 __ Bind(&not_marked);
6667 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
6668 __ B(&forwarding_address, mi);
6669 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6670 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
6671 // art_quick_read_barrier_mark_introspection_gc_roots.
6672 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
6673 __ Mov(ip0.W(), root_reg);
6674 __ Br(ip1);
6675 __ Bind(&forwarding_address);
6676 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
6677 __ Br(lr);
6678 break;
6679 }
6680 default:
6681 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
6682 UNREACHABLE();
6683 }
6684
Vladimir Marko966b46f2018-08-03 10:20:19 +00006685 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +01006686 // so JIT should pass null as `debug_name`.
6687 DCHECK(!UseJitCompilation() || debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00006688 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006689 std::ostringstream oss;
6690 oss << "BakerReadBarrierThunk";
6691 switch (kind) {
6692 case BakerReadBarrierKind::kField:
6693 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6694 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6695 break;
Vladimir Marko0ecac682018-08-07 10:40:38 +01006696 case BakerReadBarrierKind::kAcquire:
6697 oss << "Acquire_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6698 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6699 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00006700 case BakerReadBarrierKind::kArray:
6701 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6702 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6703 BakerReadBarrierSecondRegField::Decode(encoded_data));
6704 break;
6705 case BakerReadBarrierKind::kGcRoot:
6706 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6707 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6708 BakerReadBarrierSecondRegField::Decode(encoded_data));
6709 break;
6710 }
6711 *debug_name = oss.str();
6712 }
6713}
6714
6715#undef __
6716
Alexandre Rames5319def2014-10-23 10:03:10 +01006717} // namespace arm64
6718} // namespace art