blob: 40928585f6e81410843188d0f4ef4f823388c902 [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
Alex Light3a73ffb2021-01-25 14:11:05 +000019#include "aarch64/assembler-aarch64.h"
20#include "aarch64/registers-aarch64.h"
Vladimir Markof4f2daa2017-03-20 18:26:59 +000021#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000022#include "arch/arm64/instruction_set_features_arm64.h"
Vladimir Marko86c87522020-05-11 16:55:55 +010023#include "arch/arm64/jni_frame_arm64.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000024#include "art_method-inl.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070025#include "base/bit_utils.h"
26#include "base/bit_utils_iterator.h"
Vladimir Marko98873af2020-12-16 12:10:03 +000027#include "class_root-inl.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010028#include "class_table.h"
Zheng Xuc6667102015-05-15 16:08:45 +080029#include "code_generator_utils.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010030#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080031#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010032#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010033#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070034#include "heap_poisoning.h"
Nicolas Geoffray4313ccb2020-08-26 17:01:15 +010035#include "interpreter/mterp/nterp.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080036#include "intrinsics.h"
37#include "intrinsics_arm64.h"
Ulya Trafimovichec696e52022-01-26 10:21:32 +000038#include "intrinsics_utils.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010039#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070040#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010041#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070042#include "mirror/class-inl.h"
Vladimir Marko2d98dc22020-10-01 11:21:37 +000043#include "mirror/var_handle.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000044#include "offsets.h"
Alex Light3a73ffb2021-01-25 14:11:05 +000045#include "optimizing/common_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010046#include "thread.h"
47#include "utils/arm64/assembler_arm64.h"
48#include "utils/assembler.h"
49#include "utils/stack_checks.h"
50
Scott Wakeling97c72b72016-06-24 16:19:36 +010051using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000052using vixl::ExactAssemblyScope;
53using vixl::CodeBufferCheckScope;
54using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010055
56#ifdef __
57#error "ARM64 Codegen VIXL macro-assembler macro already defined."
58#endif
59
VladimĂ­r Marko434d9682022-11-04 14:04:17 +000060namespace art HIDDEN {
Alexandre Rames5319def2014-10-23 10:03:10 +010061
Roland Levillain22ccc3a2015-11-24 13:10:05 +000062template<class MirrorType>
63class GcRoot;
64
Alexandre Rames5319def2014-10-23 10:03:10 +010065namespace arm64 {
66
Alexandre Ramesbe919d92016-08-23 18:33:36 +010067using helpers::ARM64EncodableConstantOrRegister;
68using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::CPURegisterFrom;
70using helpers::DRegisterFrom;
71using helpers::FPRegisterFrom;
72using helpers::HeapOperand;
73using helpers::HeapOperandFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010074using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080075using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080076using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010077using helpers::InputRegisterAt;
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +010078using helpers::Int64FromLocation;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010079using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080080using helpers::LocationFrom;
81using helpers::OperandFromMemOperand;
82using helpers::OutputCPURegister;
83using helpers::OutputFPRegister;
84using helpers::OutputRegister;
85using helpers::RegisterFrom;
86using helpers::StackOperandFrom;
87using helpers::VIXLRegCodeFromART;
88using helpers::WRegisterFrom;
89using helpers::XRegisterFrom;
90
Vladimir Markof3e0ee22015-12-17 15:23:13 +000091// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080092// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
93// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000094static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010095
Vladimir Markof4f2daa2017-03-20 18:26:59 +000096// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
97// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
Vladimir Marko008e09f32018-08-06 15:42:43 +010098// For the Baker read barrier implementation using link-time generated thunks we need to split
Vladimir Markof4f2daa2017-03-20 18:26:59 +000099// the offset explicitly.
100constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
101
Alexandre Rames5319def2014-10-23 10:03:10 +0100102inline Condition ARM64Condition(IfCondition cond) {
103 switch (cond) {
104 case kCondEQ: return eq;
105 case kCondNE: return ne;
106 case kCondLT: return lt;
107 case kCondLE: return le;
108 case kCondGT: return gt;
109 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700110 case kCondB: return lo;
111 case kCondBE: return ls;
112 case kCondA: return hi;
113 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100114 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000115 LOG(FATAL) << "Unreachable";
116 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100117}
118
Vladimir Markod6e069b2016-01-18 11:11:01 +0000119inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
120 // The ARM64 condition codes can express all the necessary branches, see the
121 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
122 // There is no dex instruction or HIR that would need the missing conditions
123 // "equal or unordered" or "not equal".
124 switch (cond) {
125 case kCondEQ: return eq;
126 case kCondNE: return ne /* unordered */;
127 case kCondLT: return gt_bias ? cc : lt /* unordered */;
128 case kCondLE: return gt_bias ? ls : le /* unordered */;
129 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
130 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
131 default:
132 LOG(FATAL) << "UNREACHABLE";
133 UNREACHABLE();
134 }
135}
136
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100137Location ARM64ReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000138 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
139 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
140 // but we use the exact registers for clarity.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100141 if (return_type == DataType::Type::kFloat32) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000142 return LocationFrom(s0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100143 } else if (return_type == DataType::Type::kFloat64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000144 return LocationFrom(d0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100145 } else if (return_type == DataType::Type::kInt64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000146 return LocationFrom(x0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100147 } else if (return_type == DataType::Type::kVoid) {
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100148 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000149 } else {
150 return LocationFrom(w0);
151 }
152}
153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100154Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000155 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100156}
157
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100158static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
159 InvokeRuntimeCallingConvention calling_convention;
160 RegisterSet caller_saves = RegisterSet::Empty();
161 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
162 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
163 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
164 DataType::Type::kReference).GetCode());
165 return caller_saves;
166}
167
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100168// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
169#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700170#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100171
Zheng Xuda403092015-04-24 17:35:39 +0800172void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800173 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Andreas Gampe3db70682018-12-26 15:12:03 -0800174 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100175 for (uint32_t i : LowToHighBits(core_spills)) {
176 // If the register holds an object, update the stack mask.
177 if (locations->RegisterContainsObject(i)) {
178 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800179 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100180 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
181 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
182 saved_core_stack_offsets_[i] = stack_offset;
183 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800184 }
185
Artem Serovc8150b52019-07-31 18:28:00 +0100186 const size_t fp_reg_size = codegen->GetSlowPathFPWidth();
Andreas Gampe3db70682018-12-26 15:12:03 -0800187 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100188 for (uint32_t i : LowToHighBits(fp_spills)) {
189 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
190 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
191 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serov9df37b92019-07-23 16:41:54 +0100192 stack_offset += fp_reg_size;
Zheng Xuda403092015-04-24 17:35:39 +0800193 }
194
Artem Serov55ab7e82020-04-27 21:02:28 +0100195 InstructionCodeGeneratorARM64* visitor =
196 down_cast<CodeGeneratorARM64*>(codegen)->GetInstructionCodeGeneratorArm64();
197 visitor->SaveLiveRegistersHelper(locations, codegen->GetFirstRegisterSlotInSlowPath());
Zheng Xuda403092015-04-24 17:35:39 +0800198}
199
200void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Artem Serov55ab7e82020-04-27 21:02:28 +0100201 InstructionCodeGeneratorARM64* visitor =
202 down_cast<CodeGeneratorARM64*>(codegen)->GetInstructionCodeGeneratorArm64();
203 visitor->RestoreLiveRegistersHelper(locations, codegen->GetFirstRegisterSlotInSlowPath());
Zheng Xuda403092015-04-24 17:35:39 +0800204}
205
Alexandre Rames5319def2014-10-23 10:03:10 +0100206class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
207 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000208 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100209
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100210 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100211 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000212 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100213
Alexandre Rames5319def2014-10-23 10:03:10 +0100214 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000215 if (instruction_->CanThrowIntoCatchBlock()) {
216 // Live registers will be restored in the catch block if caught.
217 SaveLiveRegisters(codegen, instruction_->GetLocations());
218 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000219 // We're moving two locations to locations that could overlap, so we need a parallel
220 // move resolver.
221 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100222 codegen->EmitParallelMoves(locations->InAt(0),
223 LocationFrom(calling_convention.GetRegisterAt(0)),
224 DataType::Type::kInt32,
225 locations->InAt(1),
226 LocationFrom(calling_convention.GetRegisterAt(1)),
227 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000228 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
229 ? kQuickThrowStringBounds
230 : kQuickThrowArrayBounds;
231 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100232 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800233 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100234 }
235
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100236 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100237
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100238 const char* GetDescription() const override { return "BoundsCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100239
Alexandre Rames5319def2014-10-23 10:03:10 +0100240 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100241 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
242};
243
Alexandre Rames67555f72014-11-18 10:55:16 +0000244class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
245 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000246 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000247
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100248 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000249 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
250 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000251 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800252 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000253 }
254
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100255 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100256
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100257 const char* GetDescription() const override { return "DivZeroCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100258
Alexandre Rames67555f72014-11-18 10:55:16 +0000259 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000260 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
261};
262
263class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
264 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100265 LoadClassSlowPathARM64(HLoadClass* cls, HInstruction* at)
266 : SlowPathCodeARM64(at), cls_(cls) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000267 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100268 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 }
270
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100271 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000272 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000273 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100274 const uint32_t dex_pc = instruction_->GetDexPc();
275 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
276 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Alexandre Rames67555f72014-11-18 10:55:16 +0000277
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100278 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames67555f72014-11-18 10:55:16 +0000279 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000280 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000281
Vladimir Markof3c52b42017-11-17 17:32:12 +0000282 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100283 if (must_resolve_type) {
Santiago Aboy Solanesa0232ad2021-11-08 17:00:06 +0000284 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm64_codegen->GetGraph()->GetDexFile()) ||
Santiago Aboy Solanes69a87e32022-03-08 16:43:54 +0000285 arm64_codegen->GetCompilerOptions().WithinOatFile(&cls_->GetDexFile()) ||
286 ContainsElement(Runtime::Current()->GetClassLinker()->GetBootClassPath(),
287 &cls_->GetDexFile()));
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100288 dex::TypeIndex type_index = cls_->GetTypeIndex();
289 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Vladimir Marko8f63f102020-09-28 12:10:28 +0100290 if (cls_->NeedsAccessCheck()) {
291 CheckEntrypointTypes<kQuickResolveTypeAndVerifyAccess, void*, uint32_t>();
292 arm64_codegen->InvokeRuntime(kQuickResolveTypeAndVerifyAccess, instruction_, dex_pc, this);
293 } else {
294 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
295 arm64_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
296 }
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100297 // If we also must_do_clinit, the resolved type is now in the correct register.
298 } else {
299 DCHECK(must_do_clinit);
300 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
301 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)),
302 source,
303 cls_->GetType());
304 }
305 if (must_do_clinit) {
306 arm64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
307 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800308 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000309
310 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000311 if (out.IsValid()) {
312 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100313 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000314 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000315 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000316 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000317 __ B(GetExitLabel());
318 }
319
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100320 const char* GetDescription() const override { return "LoadClassSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100321
Alexandre Rames67555f72014-11-18 10:55:16 +0000322 private:
323 // The class this slow path will load.
324 HLoadClass* const cls_;
325
Alexandre Rames67555f72014-11-18 10:55:16 +0000326 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
327};
328
Vladimir Markoaad75c62016-10-03 08:46:48 +0000329class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
330 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000331 explicit LoadStringSlowPathARM64(HLoadString* instruction)
332 : SlowPathCodeARM64(instruction) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000333
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100334 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000335 LocationSummary* locations = instruction_->GetLocations();
336 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
337 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
338
339 __ Bind(GetEntryLabel());
340 SaveLiveRegisters(codegen, locations);
341
Vladimir Markof3c52b42017-11-17 17:32:12 +0000342 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000343 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
344 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000345 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
346 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100347 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000348 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
349
350 RestoreLiveRegisters(codegen, locations);
351
Vladimir Markoaad75c62016-10-03 08:46:48 +0000352 __ B(GetExitLabel());
353 }
354
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100355 const char* GetDescription() const override { return "LoadStringSlowPathARM64"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000356
357 private:
358 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
359};
360
Alexandre Rames5319def2014-10-23 10:03:10 +0100361class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
362 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000363 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100364
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100365 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000366 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100367 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000368 if (instruction_->CanThrowIntoCatchBlock()) {
369 // Live registers will be restored in the catch block if caught.
370 SaveLiveRegisters(codegen, instruction_->GetLocations());
371 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000372 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
373 instruction_,
374 instruction_->GetDexPc(),
375 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800376 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100377 }
378
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100379 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100380
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100381 const char* GetDescription() const override { return "NullCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100382
Alexandre Rames5319def2014-10-23 10:03:10 +0100383 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100384 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
385};
386
387class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
388 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100389 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000390 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100391
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100392 void EmitNativeCode(CodeGenerator* codegen) override {
Artem Serov7957d952017-04-04 15:44:09 +0100393 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000394 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100395 __ Bind(GetEntryLabel());
Artem Serov1a719e42019-07-18 14:24:55 +0100396 SaveLiveRegisters(codegen, locations); // Only saves live vector regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000397 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800398 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov1a719e42019-07-18 14:24:55 +0100399 RestoreLiveRegisters(codegen, locations); // Only restores live vector regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000400 if (successor_ == nullptr) {
401 __ B(GetReturnLabel());
402 } else {
403 __ B(arm64_codegen->GetLabelOf(successor_));
404 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100405 }
406
Scott Wakeling97c72b72016-06-24 16:19:36 +0100407 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100408 DCHECK(successor_ == nullptr);
409 return &return_label_;
410 }
411
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100412 HBasicBlock* GetSuccessor() const {
413 return successor_;
414 }
415
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100416 const char* GetDescription() const override { return "SuspendCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100417
Alexandre Rames5319def2014-10-23 10:03:10 +0100418 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100419 // If not null, the block to branch to after the suspend check.
420 HBasicBlock* const successor_;
421
422 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100423 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100424
425 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
426};
427
Alexandre Rames67555f72014-11-18 10:55:16 +0000428class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
429 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000430 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000431 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000432
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100433 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000434 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800435
Alexandre Rames3e69f162014-12-10 10:36:50 +0000436 DCHECK(instruction_->IsCheckCast()
437 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
438 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100439 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000440
Alexandre Rames67555f72014-11-18 10:55:16 +0000441 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000442
Vladimir Marko87584542017-12-12 17:47:52 +0000443 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000444 SaveLiveRegisters(codegen, locations);
445 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000446
447 // We're moving two locations to locations that could overlap, so we need a parallel
448 // move resolver.
449 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800450 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800451 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100452 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800453 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800454 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100455 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000456 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000457 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800458 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100459 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000460 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
461 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
462 } else {
463 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800464 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
465 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000466 }
467
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000468 if (!is_fatal_) {
469 RestoreLiveRegisters(codegen, locations);
470 __ B(GetExitLabel());
471 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000472 }
473
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100474 const char* GetDescription() const override { return "TypeCheckSlowPathARM64"; }
475 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100476
Alexandre Rames67555f72014-11-18 10:55:16 +0000477 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000478 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000479
Alexandre Rames67555f72014-11-18 10:55:16 +0000480 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
481};
482
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700483class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
484 public:
Aart Bik42249c32016-01-07 15:33:50 -0800485 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000486 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700487
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100488 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bik42249c32016-01-07 15:33:50 -0800489 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700490 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100491 LocationSummary* locations = instruction_->GetLocations();
492 SaveLiveRegisters(codegen, locations);
493 InvokeRuntimeCallingConvention calling_convention;
494 __ Mov(calling_convention.GetRegisterAt(0),
495 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000496 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100497 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700498 }
499
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100500 const char* GetDescription() const override { return "DeoptimizationSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100501
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700502 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700503 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
504};
505
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100506class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
507 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000508 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100509
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100510 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100511 LocationSummary* locations = instruction_->GetLocations();
512 __ Bind(GetEntryLabel());
513 SaveLiveRegisters(codegen, locations);
514
515 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100516 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100517 parallel_move.AddMove(
518 locations->InAt(0),
519 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100520 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100521 nullptr);
522 parallel_move.AddMove(
523 locations->InAt(1),
524 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100525 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100526 nullptr);
527 parallel_move.AddMove(
528 locations->InAt(2),
529 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100530 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100531 nullptr);
532 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
533
534 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000535 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100536 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
537 RestoreLiveRegisters(codegen, locations);
538 __ B(GetExitLabel());
539 }
540
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100541 const char* GetDescription() const override { return "ArraySetSlowPathARM64"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100542
543 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100544 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
545};
546
Zheng Xu3927c8b2015-11-18 17:46:25 +0800547void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
548 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000549 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800550
551 // We are about to use the assembler to place literals directly. Make sure we have enough
552 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000553 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
554 num_entries * sizeof(int32_t),
555 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800556
557 __ Bind(&table_start_);
558 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
559 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100560 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800561 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100562 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800563 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
564 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
565 Literal<int32_t> literal(jump_offset);
566 __ place(&literal);
567 }
568}
569
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000570// Slow path generating a read barrier for a heap reference.
571class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
572 public:
573 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
574 Location out,
575 Location ref,
576 Location obj,
577 uint32_t offset,
578 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000579 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000580 out_(out),
581 ref_(ref),
582 obj_(obj),
583 offset_(offset),
584 index_(index) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +0000585 DCHECK(gUseReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000586 // If `obj` is equal to `out` or `ref`, it means the initial object
587 // has been overwritten by (or after) the heap object reference load
588 // to be instrumented, e.g.:
589 //
590 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000591 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000592 //
593 // In that case, we have lost the information about the original
594 // object, and the emitted read barrier cannot work properly.
595 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
596 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
597 }
598
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100599 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000600 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
601 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100602 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000603 DCHECK(locations->CanCall());
604 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100605 DCHECK(instruction_->IsInstanceFieldGet() ||
Alex Light3a73ffb2021-01-25 14:11:05 +0000606 instruction_->IsPredicatedInstanceFieldGet() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100607 instruction_->IsStaticFieldGet() ||
608 instruction_->IsArrayGet() ||
609 instruction_->IsInstanceOf() ||
610 instruction_->IsCheckCast() ||
Vladimir Markoa41ea272020-09-07 15:24:36 +0000611 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000612 << "Unexpected instruction in read barrier for heap reference slow path: "
613 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000614 // The read barrier instrumentation of object ArrayGet
615 // instructions does not support the HIntermediateAddress
616 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000617 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100618 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000619
620 __ Bind(GetEntryLabel());
621
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000622 SaveLiveRegisters(codegen, locations);
623
624 // We may have to change the index's value, but as `index_` is a
625 // constant member (like other "inputs" of this slow path),
626 // introduce a copy of it, `index`.
627 Location index = index_;
628 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100629 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000630 if (instruction_->IsArrayGet()) {
631 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100632 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000633 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
634 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
635 // We are about to change the value of `index_reg` (see the
636 // calls to vixl::MacroAssembler::Lsl and
637 // vixl::MacroAssembler::Mov below), but it has
638 // not been saved by the previous call to
639 // art::SlowPathCode::SaveLiveRegisters, as it is a
640 // callee-save register --
641 // art::SlowPathCode::SaveLiveRegisters does not consider
642 // callee-save registers, as it has been designed with the
643 // assumption that callee-save registers are supposed to be
644 // handled by the called function. So, as a callee-save
645 // register, `index_reg` _would_ eventually be saved onto
646 // the stack, but it would be too late: we would have
647 // changed its value earlier. Therefore, we manually save
648 // it here into another freely available register,
649 // `free_reg`, chosen of course among the caller-save
650 // registers (as a callee-save `free_reg` register would
651 // exhibit the same problem).
652 //
653 // Note we could have requested a temporary register from
654 // the register allocator instead; but we prefer not to, as
655 // this is a slow path, and we know we can find a
656 // caller-save register that is available.
657 Register free_reg = FindAvailableCallerSaveRegister(codegen);
658 __ Mov(free_reg.W(), index_reg);
659 index_reg = free_reg;
660 index = LocationFrom(index_reg);
661 } else {
662 // The initial register stored in `index_` has already been
663 // saved in the call to art::SlowPathCode::SaveLiveRegisters
664 // (as it is not a callee-save register), so we can freely
665 // use it.
666 }
667 // Shifting the index value contained in `index_reg` by the scale
668 // factor (2) cannot overflow in practice, as the runtime is
669 // unable to allocate object arrays with a size larger than
670 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100671 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000672 static_assert(
673 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
674 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
675 __ Add(index_reg, index_reg, Operand(offset_));
676 } else {
Ulya Trafimovichec696e52022-01-26 10:21:32 +0000677 // In the case of the following intrinsics `index_` is not shifted by a scale factor of 2
678 // (as in the case of ArrayGet), as it is actually an offset to an object field within an
679 // object.
Roland Levillain3d312422016-06-23 13:53:42 +0100680 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000681 DCHECK(instruction_->GetLocations()->Intrinsified());
Ulya Trafimovichec696e52022-01-26 10:21:32 +0000682 HInvoke* invoke = instruction_->AsInvoke();
683 DCHECK(IsUnsafeGetObject(invoke) ||
684 IsVarHandleGet(invoke) ||
685 IsUnsafeCASObject(invoke) ||
686 IsVarHandleCASFamily(invoke)) << invoke->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100687 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100688 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000689 }
690 }
691
692 // We're moving two or three locations to locations that could
693 // overlap, so we need a parallel move resolver.
694 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100695 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000696 parallel_move.AddMove(ref_,
697 LocationFrom(calling_convention.GetRegisterAt(0)),
698 type,
699 nullptr);
700 parallel_move.AddMove(obj_,
701 LocationFrom(calling_convention.GetRegisterAt(1)),
702 type,
703 nullptr);
704 if (index.IsValid()) {
705 parallel_move.AddMove(index,
706 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100707 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000708 nullptr);
709 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
710 } else {
711 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
712 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
713 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000714 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000715 instruction_,
716 instruction_->GetDexPc(),
717 this);
718 CheckEntrypointTypes<
719 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
720 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
721
722 RestoreLiveRegisters(codegen, locations);
723
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000724 __ B(GetExitLabel());
725 }
726
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100727 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000728
729 private:
730 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100731 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
732 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000733 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
734 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
735 return Register(VIXLRegCodeFromART(i), kXRegSize);
736 }
737 }
738 // We shall never fail to find a free caller-save register, as
739 // there are more than two core caller-save registers on ARM64
740 // (meaning it is possible to find one which is different from
741 // `ref` and `obj`).
742 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
743 LOG(FATAL) << "Could not find a free register";
744 UNREACHABLE();
745 }
746
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000747 const Location out_;
748 const Location ref_;
749 const Location obj_;
750 const uint32_t offset_;
751 // An additional location containing an index to an array.
752 // Only used for HArrayGet and the UnsafeGetObject &
753 // UnsafeGetObjectVolatile intrinsics.
754 const Location index_;
755
756 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
757};
758
759// Slow path generating a read barrier for a GC root.
760class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
761 public:
762 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000763 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +0000764 DCHECK(gUseReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +0000765 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000766
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100767 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000768 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100769 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000770 DCHECK(locations->CanCall());
771 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Vladimir Markoa41ea272020-09-07 15:24:36 +0000772 DCHECK(instruction_->IsLoadClass() ||
773 instruction_->IsLoadString() ||
774 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000775 << "Unexpected instruction in read barrier for GC root slow path: "
776 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000777
778 __ Bind(GetEntryLabel());
779 SaveLiveRegisters(codegen, locations);
780
781 InvokeRuntimeCallingConvention calling_convention;
782 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
783 // The argument of the ReadBarrierForRootSlow is not a managed
784 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
785 // thus we need a 64-bit move here, and we cannot use
786 //
787 // arm64_codegen->MoveLocation(
788 // LocationFrom(calling_convention.GetRegisterAt(0)),
789 // root_,
790 // type);
791 //
792 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100793 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000794 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000795 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000796 instruction_,
797 instruction_->GetDexPc(),
798 this);
799 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
800 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
801
802 RestoreLiveRegisters(codegen, locations);
803 __ B(GetExitLabel());
804 }
805
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100806 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000807
808 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000809 const Location out_;
810 const Location root_;
811
812 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
813};
814
Mythri Alle5097f832021-11-02 14:52:30 +0000815class MethodEntryExitHooksSlowPathARM64 : public SlowPathCodeARM64 {
816 public:
817 explicit MethodEntryExitHooksSlowPathARM64(HInstruction* instruction)
818 : SlowPathCodeARM64(instruction) {}
819
820 void EmitNativeCode(CodeGenerator* codegen) override {
821 LocationSummary* locations = instruction_->GetLocations();
822 QuickEntrypointEnum entry_point =
823 (instruction_->IsMethodEntryHook()) ? kQuickMethodEntryHook : kQuickMethodExitHook;
824 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
825 __ Bind(GetEntryLabel());
826 SaveLiveRegisters(codegen, locations);
Mythri Allebab6beb2022-10-21 13:28:05 +0000827 if (instruction_->IsMethodExitHook()) {
828 __ Mov(vixl::aarch64::x4, arm64_codegen->GetFrameSize());
829 }
Mythri Alle5097f832021-11-02 14:52:30 +0000830 arm64_codegen->InvokeRuntime(entry_point, instruction_, instruction_->GetDexPc(), this);
831 RestoreLiveRegisters(codegen, locations);
832 __ B(GetExitLabel());
833 }
834
835 const char* GetDescription() const override {
836 return "MethodEntryExitHooksSlowPath";
837 }
838
839 private:
840 DISALLOW_COPY_AND_ASSIGN(MethodEntryExitHooksSlowPathARM64);
841};
842
Nicolas Geoffray9e598902021-11-19 14:53:07 +0000843class CompileOptimizedSlowPathARM64 : public SlowPathCodeARM64 {
844 public:
845 CompileOptimizedSlowPathARM64() : SlowPathCodeARM64(/* instruction= */ nullptr) {}
846
847 void EmitNativeCode(CodeGenerator* codegen) override {
848 uint32_t entrypoint_offset =
849 GetThreadOffset<kArm64PointerSize>(kQuickCompileOptimized).Int32Value();
850 __ Bind(GetEntryLabel());
851 __ Ldr(lr, MemOperand(tr, entrypoint_offset));
852 // Note: we don't record the call here (and therefore don't generate a stack
853 // map), as the entrypoint should never be suspended.
854 __ Blr(lr);
855 __ B(GetExitLabel());
856 }
857
858 const char* GetDescription() const override {
859 return "CompileOptimizedSlowPath";
860 }
861
862 private:
863 DISALLOW_COPY_AND_ASSIGN(CompileOptimizedSlowPathARM64);
864};
865
Alexandre Rames5319def2014-10-23 10:03:10 +0100866#undef __
867
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100868Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100869 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100870 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100871 LOG(FATAL) << "Unreachable type " << type;
872 }
873
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100874 if (DataType::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100875 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
876 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100877 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000878 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000879 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
880 } else {
881 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100882 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
883 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100884 }
885
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000886 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100887 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100888 return next_location;
889}
890
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100891Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100892 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100893}
894
Vladimir Marko86c87522020-05-11 16:55:55 +0100895Location CriticalNativeCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
896 DCHECK_NE(type, DataType::Type::kReference);
897
898 Location location = Location::NoLocation();
899 if (DataType::IsFloatingPointType(type)) {
900 if (fpr_index_ < kParameterFPRegistersLength) {
901 location = LocationFrom(kParameterFPRegisters[fpr_index_]);
902 ++fpr_index_;
903 }
904 } else {
905 // Native ABI uses the same registers as managed, except that the method register x0
906 // is a normal argument.
907 if (gpr_index_ < 1u + kParameterCoreRegistersLength) {
908 location = LocationFrom(gpr_index_ == 0u ? x0 : kParameterCoreRegisters[gpr_index_ - 1u]);
909 ++gpr_index_;
910 }
911 }
912 if (location.IsInvalid()) {
913 if (DataType::Is64BitType(type)) {
914 location = Location::DoubleStackSlot(stack_offset_);
915 } else {
916 location = Location::StackSlot(stack_offset_);
917 }
918 stack_offset_ += kFramePointerSize;
919
920 if (for_register_allocation_) {
921 location = Location::Any();
922 }
923 }
924 return location;
925}
926
927Location CriticalNativeCallingConventionVisitorARM64::GetReturnLocation(DataType::Type type) const {
928 // We perform conversion to the managed ABI return register after the call if needed.
929 InvokeDexCallingConventionVisitorARM64 dex_calling_convention;
930 return dex_calling_convention.GetReturnLocation(type);
931}
932
933Location CriticalNativeCallingConventionVisitorARM64::GetMethodLocation() const {
934 // Pass the method in the hidden argument x15.
935 return Location::RegisterLocation(x15.GetCode());
936}
937
Serban Constantinescu579885a2015-02-22 20:51:33 +0000938CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100939 const CompilerOptions& compiler_options,
940 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100941 : CodeGenerator(graph,
942 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000943 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000944 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100945 callee_saved_core_registers.GetList(),
946 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100947 compiler_options,
948 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100949 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
950 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov1a719e42019-07-18 14:24:55 +0100951 location_builder_neon_(graph, this),
952 instruction_visitor_neon_(graph, this),
953 location_builder_sve_(graph, this),
954 instruction_visitor_sve_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100955 move_resolver_(graph->GetAllocator(), this),
Artem Serovaa6f4832018-11-21 18:57:54 +0000956 assembler_(graph->GetAllocator(),
957 compiler_options.GetInstructionSetFeatures()->AsArm64InstructionSetFeatures()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000958 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100959 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000960 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100961 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +0100962 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
963 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000964 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100965 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeb9eb002020-10-02 13:54:19 +0100966 boot_image_jni_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +0100967 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100968 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100969 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100970 uint32_literals_(std::less<uint32_t>(),
971 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
972 uint64_literals_(std::less<uint64_t>(),
973 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000974 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100975 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000976 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +0000977 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
978 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
979 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000980 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000981 AddAllocatedRegister(LocationFrom(lr));
Artem Serov1a719e42019-07-18 14:24:55 +0100982
983 bool use_sve = ShouldUseSVE();
984 if (use_sve) {
985 location_builder_ = &location_builder_sve_;
986 instruction_visitor_ = &instruction_visitor_sve_;
987 } else {
988 location_builder_ = &location_builder_neon_;
989 instruction_visitor_ = &instruction_visitor_neon_;
990 }
991}
992
993bool CodeGeneratorARM64::ShouldUseSVE() const {
Artem Serov8ba4de12019-12-04 21:10:23 +0000994 return GetInstructionSetFeatures().HasSVE();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000995}
Alexandre Rames5319def2014-10-23 10:03:10 +0100996
Artem Serov55ab7e82020-04-27 21:02:28 +0100997size_t CodeGeneratorARM64::GetSIMDRegisterWidth() const {
998 return SupportsPredicatedSIMD()
999 ? GetInstructionSetFeatures().GetSVEVectorLength() / kBitsPerByte
1000 : vixl::aarch64::kQRegSizeInBytes;
1001}
1002
Alexandre Rames67555f72014-11-18 10:55:16 +00001003#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001004
Zheng Xu3927c8b2015-11-18 17:46:25 +08001005void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001006 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001007 jump_table->EmitTable(this);
1008 }
1009}
1010
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001011void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001012 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +00001013
1014 // Emit JIT baker read barrier slow paths.
Vladimir Marko695348f2020-05-19 14:42:02 +01001015 DCHECK(GetCompilerOptions().IsJitCompiler() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +00001016 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
1017 uint32_t encoded_data = entry.first;
1018 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
1019 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -08001020 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00001021 }
1022
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001023 // Ensure we emit the literal pool.
1024 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001025
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001026 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001027
1028 // Verify Baker read barrier linker patches.
1029 if (kIsDebugBuild) {
1030 ArrayRef<const uint8_t> code = allocator->GetMemory();
1031 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1032 DCHECK(info.label.IsBound());
1033 uint32_t literal_offset = info.label.GetLocation();
1034 DCHECK_ALIGNED(literal_offset, 4u);
1035
1036 auto GetInsn = [&code](uint32_t offset) {
1037 DCHECK_ALIGNED(offset, 4u);
1038 return
1039 (static_cast<uint32_t>(code[offset + 0]) << 0) +
1040 (static_cast<uint32_t>(code[offset + 1]) << 8) +
1041 (static_cast<uint32_t>(code[offset + 2]) << 16)+
1042 (static_cast<uint32_t>(code[offset + 3]) << 24);
1043 };
1044
1045 const uint32_t encoded_data = info.custom_data;
1046 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1047 // Check that the next instruction matches the expected LDR.
1048 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01001049 case BakerReadBarrierKind::kField:
1050 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00001051 DCHECK_GE(code.size() - literal_offset, 8u);
1052 uint32_t next_insn = GetInsn(literal_offset + 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001053 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1054 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko0ecac682018-08-07 10:40:38 +01001055 if (kind == BakerReadBarrierKind::kField) {
1056 // LDR (immediate) with correct base_reg.
1057 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
1058 } else {
1059 DCHECK(kind == BakerReadBarrierKind::kAcquire);
1060 // LDAR with correct base_reg.
1061 CHECK_EQ(next_insn & 0xffffffe0u, 0x88dffc00u | (base_reg << 5));
1062 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001063 break;
1064 }
1065 case BakerReadBarrierKind::kArray: {
1066 DCHECK_GE(code.size() - literal_offset, 8u);
1067 uint32_t next_insn = GetInsn(literal_offset + 4u);
1068 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1069 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1070 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1071 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1072 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1073 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1074 break;
1075 }
1076 case BakerReadBarrierKind::kGcRoot: {
1077 DCHECK_GE(literal_offset, 4u);
1078 uint32_t prev_insn = GetInsn(literal_offset - 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001079 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko94796f82018-08-08 15:15:33 +01001080 // Usually LDR (immediate) with correct root_reg but
Vladimir Markoc8178f52020-11-24 10:38:16 +00001081 // we may have a "MOV marked, old_value" for intrinsic CAS.
Vladimir Marko94796f82018-08-08 15:15:33 +01001082 if ((prev_insn & 0xffe0ffff) != (0x2a0003e0 | root_reg)) { // MOV?
1083 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg); // LDR?
1084 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001085 break;
1086 }
1087 default:
1088 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1089 UNREACHABLE();
1090 }
1091 }
1092 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001093}
1094
Zheng Xuad4450e2015-04-17 18:48:56 +08001095void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1096 // Note: There are 6 kinds of moves:
1097 // 1. constant -> GPR/FPR (non-cycle)
1098 // 2. constant -> stack (non-cycle)
1099 // 3. GPR/FPR -> GPR/FPR
1100 // 4. GPR/FPR -> stack
1101 // 5. stack -> GPR/FPR
1102 // 6. stack -> stack (non-cycle)
1103 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1104 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1105 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1106 // dependency.
1107 vixl_temps_.Open(GetVIXLAssembler());
1108}
1109
1110void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1111 vixl_temps_.Close();
1112}
1113
1114Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001115 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1116 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1117 || kind == Location::kSIMDStackSlot);
1118 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1119 ? Location::kFpuRegister
1120 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001121 Location scratch = GetScratchLocation(kind);
1122 if (!scratch.Equals(Location::NoLocation())) {
1123 return scratch;
1124 }
1125 // Allocate from VIXL temp registers.
1126 if (kind == Location::kRegister) {
1127 scratch = LocationFrom(vixl_temps_.AcquireX());
1128 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001129 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serov1a719e42019-07-18 14:24:55 +01001130 scratch = codegen_->GetGraph()->HasSIMD()
1131 ? codegen_->GetInstructionCodeGeneratorArm64()->AllocateSIMDScratchLocation(&vixl_temps_)
1132 : LocationFrom(vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001133 }
1134 AddScratchLocation(scratch);
1135 return scratch;
1136}
1137
1138void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1139 if (loc.IsRegister()) {
1140 vixl_temps_.Release(XRegisterFrom(loc));
1141 } else {
1142 DCHECK(loc.IsFpuRegister());
Artem Serov1a719e42019-07-18 14:24:55 +01001143 if (codegen_->GetGraph()->HasSIMD()) {
1144 codegen_->GetInstructionCodeGeneratorArm64()->FreeSIMDScratchLocation(loc, &vixl_temps_);
1145 } else {
1146 vixl_temps_.Release(DRegisterFrom(loc));
1147 }
Zheng Xuad4450e2015-04-17 18:48:56 +08001148 }
1149 RemoveScratchLocation(loc);
1150}
1151
Alexandre Rames3e69f162014-12-10 10:36:50 +00001152void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001153 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001154 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001155}
1156
Mythri Alle5097f832021-11-02 14:52:30 +00001157void LocationsBuilderARM64::VisitMethodExitHook(HMethodExitHook* method_hook) {
1158 LocationSummary* locations = new (GetGraph()->GetAllocator())
1159 LocationSummary(method_hook, LocationSummary::kCallOnSlowPath);
1160 DataType::Type return_type = method_hook->InputAt(0)->GetType();
1161 locations->SetInAt(0, ARM64ReturnLocation(return_type));
1162}
1163
1164void InstructionCodeGeneratorARM64::GenerateMethodEntryExitHook(HInstruction* instruction) {
1165 MacroAssembler* masm = GetVIXLAssembler();
1166 UseScratchRegisterScope temps(masm);
1167 Register temp = temps.AcquireX();
1168 Register value = temps.AcquireW();
1169
1170 SlowPathCodeARM64* slow_path =
1171 new (codegen_->GetScopedAllocator()) MethodEntryExitHooksSlowPathARM64(instruction);
1172 codegen_->AddSlowPath(slow_path);
1173
1174 uint64_t address = reinterpret_cast64<uint64_t>(Runtime::Current()->GetInstrumentation());
Mythri Alle5b263ae2022-10-21 15:07:44 +00001175 MemberOffset offset = instruction->IsMethodExitHook() ?
1176 instrumentation::Instrumentation::NeedsExitHooksOffset() :
1177 instrumentation::Instrumentation::HaveMethodEntryListenersOffset();
1178 __ Mov(temp, address + offset.Int32Value());
Mythri Alle9575c122021-11-12 12:04:41 +00001179 __ Ldrb(value, MemOperand(temp, 0));
Mythri Alle5097f832021-11-02 14:52:30 +00001180 __ Cbnz(value, slow_path->GetEntryLabel());
1181 __ Bind(slow_path->GetExitLabel());
1182}
1183
1184void InstructionCodeGeneratorARM64::VisitMethodExitHook(HMethodExitHook* instruction) {
1185 DCHECK(codegen_->GetCompilerOptions().IsJitCompiler() && GetGraph()->IsDebuggable());
1186 DCHECK(codegen_->RequiresCurrentMethod());
1187 GenerateMethodEntryExitHook(instruction);
1188}
1189
1190void LocationsBuilderARM64::VisitMethodEntryHook(HMethodEntryHook* method_hook) {
1191 new (GetGraph()->GetAllocator()) LocationSummary(method_hook, LocationSummary::kCallOnSlowPath);
1192}
1193
1194void InstructionCodeGeneratorARM64::VisitMethodEntryHook(HMethodEntryHook* instruction) {
1195 DCHECK(codegen_->GetCompilerOptions().IsJitCompiler() && GetGraph()->IsDebuggable());
1196 DCHECK(codegen_->RequiresCurrentMethod());
1197 GenerateMethodEntryExitHook(instruction);
1198}
1199
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001200void CodeGeneratorARM64::MaybeIncrementHotness(bool is_frame_entry) {
1201 MacroAssembler* masm = GetVIXLAssembler();
1202 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1203 UseScratchRegisterScope temps(masm);
1204 Register counter = temps.AcquireX();
1205 Register method = is_frame_entry ? kArtMethodRegister : temps.AcquireX();
1206 if (!is_frame_entry) {
1207 __ Ldr(method, MemOperand(sp, 0));
1208 }
1209 __ Ldrh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
Nicolas Geoffray61673dc2021-11-06 13:58:31 +00001210 vixl::aarch64::Label done;
1211 DCHECK_EQ(0u, interpreter::kNterpHotnessValue);
1212 __ Cbz(counter, &done);
1213 __ Add(counter, counter, -1);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001214 __ Strh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
Nicolas Geoffray61673dc2021-11-06 13:58:31 +00001215 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001216 }
1217
1218 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray9e598902021-11-19 14:53:07 +00001219 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) CompileOptimizedSlowPathARM64();
1220 AddSlowPath(slow_path);
1221 ProfilingInfo* info = GetGraph()->GetProfilingInfo();
1222 DCHECK(info != nullptr);
1223 DCHECK(!HasEmptyFrame());
1224 uint64_t address = reinterpret_cast64<uint64_t>(info);
1225 vixl::aarch64::Label done;
1226 UseScratchRegisterScope temps(masm);
1227 Register temp = temps.AcquireX();
1228 Register counter = temps.AcquireW();
1229 __ Ldr(temp, DeduplicateUint64Literal(address));
1230 __ Ldrh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1231 __ Cbz(counter, slow_path->GetEntryLabel());
1232 __ Add(counter, counter, -1);
1233 __ Strh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1234 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001235 }
1236}
1237
Alexandre Rames5319def2014-10-23 10:03:10 +01001238void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001239 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffrayab3f8d22022-07-23 15:49:51 +00001240
1241 // Check if we need to generate the clinit check. We will jump to the
1242 // resolution stub if the class is not initialized and the executing thread is
1243 // not the thread initializing it.
1244 // We do this before constructing the frame to get the correct stack trace if
1245 // an exception is thrown.
1246 if (GetCompilerOptions().ShouldCompileWithClinitCheck(GetGraph()->GetArtMethod())) {
1247 UseScratchRegisterScope temps(masm);
1248 vixl::aarch64::Label resolution;
Nicolas Geoffrayf8537902022-09-13 15:22:29 +01001249 vixl::aarch64::Label memory_barrier;
Nicolas Geoffrayab3f8d22022-07-23 15:49:51 +00001250
1251 Register temp1 = temps.AcquireW();
1252 Register temp2 = temps.AcquireW();
1253
1254 // Check if we're visibly initialized.
1255
1256 // We don't emit a read barrier here to save on code size. We rely on the
1257 // resolution trampoline to do a suspend check before re-entering this code.
1258 __ Ldr(temp1, MemOperand(kArtMethodRegister, ArtMethod::DeclaringClassOffset().Int32Value()));
1259 __ Ldrb(temp2, HeapOperand(temp1, status_byte_offset));
1260 __ Cmp(temp2, shifted_visibly_initialized_value);
1261 __ B(hs, &frame_entry_label_);
1262
Nicolas Geoffrayf8537902022-09-13 15:22:29 +01001263 // Check if we're initialized and jump to code that does a memory barrier if
1264 // so.
1265 __ Cmp(temp2, shifted_initialized_value);
1266 __ B(hs, &memory_barrier);
1267
Nicolas Geoffrayab3f8d22022-07-23 15:49:51 +00001268 // Check if we're initializing and the thread initializing is the one
1269 // executing the code.
1270 __ Cmp(temp2, shifted_initializing_value);
1271 __ B(lo, &resolution);
1272
1273 __ Ldr(temp1, HeapOperand(temp1, mirror::Class::ClinitThreadIdOffset().Int32Value()));
1274 __ Ldr(temp2, MemOperand(tr, Thread::TidOffset<kArm64PointerSize>().Int32Value()));
1275 __ Cmp(temp1, temp2);
1276 __ B(eq, &frame_entry_label_);
1277 __ Bind(&resolution);
1278
1279 // Jump to the resolution stub.
1280 ThreadOffset64 entrypoint_offset =
1281 GetThreadOffset<kArm64PointerSize>(kQuickQuickResolutionTrampoline);
1282 __ Ldr(temp1.X(), MemOperand(tr, entrypoint_offset.Int32Value()));
1283 __ Br(temp1.X());
Nicolas Geoffrayf8537902022-09-13 15:22:29 +01001284
1285 __ Bind(&memory_barrier);
1286 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Nicolas Geoffrayab3f8d22022-07-23 15:49:51 +00001287 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001288 __ Bind(&frame_entry_label_);
1289
Vladimir Marko33bff252017-11-01 14:35:42 +00001290 bool do_overflow_check =
1291 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001292 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001293 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001294 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001295 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001296 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001297 {
1298 // Ensure that between load and RecordPcInfo there are no pools emitted.
1299 ExactAssemblyScope eas(GetVIXLAssembler(),
1300 kInstructionSize,
1301 CodeBufferCheckScope::kExactSize);
1302 __ ldr(wzr, MemOperand(temp, 0));
1303 RecordPcInfo(nullptr, 0);
1304 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001305 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001306
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001307 if (!HasEmptyFrame()) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001308 // Stack layout:
1309 // sp[frame_size - 8] : lr.
1310 // ... : other preserved core registers.
1311 // ... : other preserved fp registers.
1312 // ... : reserved frame space.
1313 // sp[0] : current method.
Vladimir Marko1a225a72019-07-05 13:37:42 +01001314 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1315 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1316 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1317 DCHECK(!preserved_core_registers.IsEmpty());
1318 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1319 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001320
Vladimir Marko1a225a72019-07-05 13:37:42 +01001321 // Save the current method if we need it, or if using STP reduces code
1322 // size. Note that we do not do this in HCurrentMethod, as the
1323 // instruction might have been removed in the SSA graph.
1324 CPURegister lowest_spill;
1325 if (core_spills_offset == kXRegSizeInBytes) {
1326 // If there is no gap between the method and the lowest core spill, use
1327 // aligned STP pre-index to store both. Max difference is 512. We do
1328 // that to reduce code size even if we do not have to save the method.
1329 DCHECK_LE(frame_size, 512); // 32 core registers are only 256 bytes.
1330 lowest_spill = preserved_core_registers.PopLowestIndex();
1331 __ Stp(kArtMethodRegister, lowest_spill, MemOperand(sp, -frame_size, PreIndex));
1332 } else if (RequiresCurrentMethod()) {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001333 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001334 } else {
1335 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001336 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001337 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Vladimir Marko1a225a72019-07-05 13:37:42 +01001338 if (lowest_spill.IsValid()) {
1339 GetAssembler()->cfi().RelOffset(DWARFReg(lowest_spill), core_spills_offset);
1340 core_spills_offset += kXRegSizeInBytes;
1341 }
1342 GetAssembler()->SpillRegisters(preserved_core_registers, core_spills_offset);
1343 GetAssembler()->SpillRegisters(preserved_fp_registers, fp_spills_offset);
Mingyao Yang063fc772016-08-02 11:02:54 -07001344
1345 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1346 // Initialize should_deoptimize flag to 0.
1347 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1348 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1349 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001350 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001351 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08001352 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001353}
1354
1355void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001356 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001357 if (!HasEmptyFrame()) {
Vladimir Marko1a225a72019-07-05 13:37:42 +01001358 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1359 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1360 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1361 DCHECK(!preserved_core_registers.IsEmpty());
1362 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1363 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
1364
1365 CPURegister lowest_spill;
1366 if (core_spills_offset == kXRegSizeInBytes) {
1367 // If there is no gap between the method and the lowest core spill, use
1368 // aligned LDP pre-index to pop both. Max difference is 504. We do
1369 // that to reduce code size even though the loaded method is unused.
1370 DCHECK_LE(frame_size, 504); // 32 core registers are only 256 bytes.
1371 lowest_spill = preserved_core_registers.PopLowestIndex();
1372 core_spills_offset += kXRegSizeInBytes;
1373 }
1374 GetAssembler()->UnspillRegisters(preserved_fp_registers, fp_spills_offset);
1375 GetAssembler()->UnspillRegisters(preserved_core_registers, core_spills_offset);
1376 if (lowest_spill.IsValid()) {
1377 __ Ldp(xzr, lowest_spill, MemOperand(sp, frame_size, PostIndex));
1378 GetAssembler()->cfi().Restore(DWARFReg(lowest_spill));
1379 } else {
1380 __ Drop(frame_size);
1381 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001382 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001383 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001384 __ Ret();
1385 GetAssembler()->cfi().RestoreState();
1386 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001387}
1388
Scott Wakeling97c72b72016-06-24 16:19:36 +01001389CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001390 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001391 return CPURegList(CPURegister::kRegister, kXRegSize,
1392 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001393}
1394
Scott Wakeling97c72b72016-06-24 16:19:36 +01001395CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001396 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1397 GetNumberOfFloatingPointRegisters()));
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001398 return CPURegList(CPURegister::kVRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001399 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001400}
1401
Alexandre Rames5319def2014-10-23 10:03:10 +01001402void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1403 __ Bind(GetLabelOf(block));
1404}
1405
Calin Juravle175dc732015-08-25 15:42:32 +01001406void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1407 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001408 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001409}
1410
Calin Juravlee460d1d2015-09-29 04:52:17 +01001411void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1412 if (location.IsRegister()) {
1413 locations->AddTemp(location);
1414 } else {
1415 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1416 }
1417}
1418
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001419void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001420 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001421 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001422 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001423 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001424 if (value_can_be_null) {
1425 __ Cbz(value, &done);
1426 }
Roland Levillainc73f0522018-08-14 15:16:50 +01001427 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07001428 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01001429 // Calculate the offset (in the card table) of the card corresponding to
1430 // `object`.
Alexandre Rames5319def2014-10-23 10:03:10 +01001431 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Roland Levillainc73f0522018-08-14 15:16:50 +01001432 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
1433 // `object`'s card.
1434 //
1435 // Register `card` contains the address of the card table. Note that the card
1436 // table's base is biased during its creation so that it always starts at an
1437 // address whose least-significant byte is equal to `kCardDirty` (see
1438 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
1439 // below writes the `kCardDirty` (byte) value into the `object`'s card
1440 // (located at `card + object >> kCardShift`).
1441 //
1442 // This dual use of the value in register `card` (1. to calculate the location
1443 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
1444 // (no need to explicitly load `kCardDirty` as an immediate value).
Serban Constantinescu02164b32014-11-13 14:05:07 +00001445 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001446 if (value_can_be_null) {
1447 __ Bind(&done);
1448 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001449}
1450
David Brazdil58282f42016-01-14 12:45:10 +00001451void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001452 // Blocked core registers:
1453 // lr : Runtime reserved.
1454 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001455 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001456 // ip1 : VIXL core temp.
1457 // ip0 : VIXL core temp.
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001458 // x18 : Platform register.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001459 //
1460 // Blocked fp registers:
1461 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001462 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1463 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001464 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001465 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001466 }
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001467 blocked_core_registers_[X18] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001468
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001469 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001470 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001471 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001472 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001473
David Brazdil58282f42016-01-14 12:45:10 +00001474 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001475 // Stubs do not save callee-save floating point registers. If the graph
1476 // is debuggable, we need to deal with these registers differently. For
1477 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001478 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1479 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001480 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001481 }
1482 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001483}
1484
Alexandre Rames3e69f162014-12-10 10:36:50 +00001485size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1486 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1487 __ Str(reg, MemOperand(sp, stack_index));
1488 return kArm64WordSize;
1489}
1490
1491size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1492 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1493 __ Ldr(reg, MemOperand(sp, stack_index));
1494 return kArm64WordSize;
1495}
1496
Artem Serov9df37b92019-07-23 16:41:54 +01001497size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1498 uint32_t reg_id ATTRIBUTE_UNUSED) {
1499 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1500 << "use SaveRestoreLiveRegistersHelper";
1501 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001502}
1503
Artem Serov9df37b92019-07-23 16:41:54 +01001504size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1505 uint32_t reg_id ATTRIBUTE_UNUSED) {
1506 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1507 << "use SaveRestoreLiveRegistersHelper";
1508 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001509}
1510
Alexandre Rames5319def2014-10-23 10:03:10 +01001511void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001512 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001513}
1514
1515void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001516 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001517}
1518
Vladimir Markoa0431112018-06-25 09:32:54 +01001519const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1520 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1521}
1522
Alexandre Rames67555f72014-11-18 10:55:16 +00001523void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001524 if (constant->IsIntConstant()) {
1525 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1526 } else if (constant->IsLongConstant()) {
1527 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1528 } else if (constant->IsNullConstant()) {
1529 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001530 } else if (constant->IsFloatConstant()) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001531 __ Fmov(VRegister(destination), constant->AsFloatConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001532 } else {
1533 DCHECK(constant->IsDoubleConstant());
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001534 __ Fmov(VRegister(destination), constant->AsDoubleConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001535 }
1536}
1537
Alexandre Rames3e69f162014-12-10 10:36:50 +00001538
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001539static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001540 DCHECK(constant.IsConstant());
1541 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001542 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001543 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001544 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1545 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1546 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1547 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001548}
1549
Roland Levillain952b2352017-05-03 19:49:14 +01001550// Allocate a scratch register from the VIXL pool, querying first
1551// the floating-point register pool, and then the core register
1552// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001553// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1554// using a different allocation strategy.
1555static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1556 vixl::aarch64::UseScratchRegisterScope* temps,
1557 int size_in_bits) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001558 return masm->GetScratchVRegisterList()->IsEmpty()
Roland Levillain558dea12017-01-27 19:40:44 +00001559 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1560 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1561}
1562
Calin Juravlee460d1d2015-09-29 04:52:17 +01001563void CodeGeneratorARM64::MoveLocation(Location destination,
1564 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001565 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001566 if (source.Equals(destination)) {
1567 return;
1568 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001569
1570 // A valid move can always be inferred from the destination and source
1571 // locations. When moving from and to a register, the argument type can be
1572 // used to generate 32bit instead of 64bit moves. In debug mode we also
1573 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001574 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001575
1576 if (destination.IsRegister() || destination.IsFpuRegister()) {
1577 if (unspecified_type) {
1578 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1579 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001580 (src_cst != nullptr && (src_cst->IsIntConstant()
1581 || src_cst->IsFloatConstant()
1582 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001583 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001584 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001585 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001586 // If the source is a double stack slot or a 64bit constant, a 64bit
1587 // type is appropriate. Else the source is a register, and since the
1588 // type has not been specified, we chose a 64bit type to force a 64bit
1589 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001590 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001591 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001592 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001593 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1594 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001595 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001596 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1597 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1598 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001599 } else if (source.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001600 GetInstructionCodeGeneratorArm64()->LoadSIMDRegFromStack(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001601 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001602 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001603 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001604 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001605 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001606 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001607 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001608 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001609 DataType::Type source_type = DataType::Is64BitType(dst_type)
1610 ? DataType::Type::kInt64
1611 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001612 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1613 }
1614 } else {
1615 DCHECK(source.IsFpuRegister());
1616 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001617 DataType::Type source_type = DataType::Is64BitType(dst_type)
1618 ? DataType::Type::kFloat64
1619 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001620 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1621 } else {
1622 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001623 if (GetGraph()->HasSIMD()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001624 GetInstructionCodeGeneratorArm64()->MoveSIMDRegToSIMDReg(destination, source);
Artem Serovd4bccf12017-04-03 18:47:32 +01001625 } else {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001626 __ Fmov(VRegister(dst), FPRegisterFrom(source, dst_type));
Artem Serovd4bccf12017-04-03 18:47:32 +01001627 }
1628 }
1629 }
1630 } else if (destination.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001631 GetInstructionCodeGeneratorArm64()->MoveToSIMDStackSlot(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001632 } else { // The destination is not a register. It must be a stack slot.
1633 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1634 if (source.IsRegister() || source.IsFpuRegister()) {
1635 if (unspecified_type) {
1636 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001637 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001638 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001639 dst_type =
1640 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001641 }
1642 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001643 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1644 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001645 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001646 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001647 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1648 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001649 UseScratchRegisterScope temps(GetVIXLAssembler());
1650 HConstant* src_cst = source.GetConstant();
1651 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001652 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001653 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1654 ? Register(xzr)
1655 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001656 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001657 if (src_cst->IsIntConstant()) {
1658 temp = temps.AcquireW();
1659 } else if (src_cst->IsLongConstant()) {
1660 temp = temps.AcquireX();
1661 } else if (src_cst->IsFloatConstant()) {
1662 temp = temps.AcquireS();
1663 } else {
1664 DCHECK(src_cst->IsDoubleConstant());
1665 temp = temps.AcquireD();
1666 }
1667 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001668 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001669 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001670 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001671 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001672 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001673 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001674 // Use any scratch register (a core or a floating-point one)
1675 // from VIXL scratch register pools as a temporary.
1676 //
1677 // We used to only use the FP scratch register pool, but in some
1678 // rare cases the only register from this pool (D31) would
1679 // already be used (e.g. within a ParallelMove instruction, when
1680 // a move is blocked by a another move requiring a scratch FP
1681 // register, which would reserve D31). To prevent this issue, we
1682 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001683 //
1684 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001685 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001686 // use AcquireFPOrCoreCPURegisterOfSize instead of
1687 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1688 // allocates core scratch registers first.
1689 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1690 GetVIXLAssembler(),
1691 &temps,
1692 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001693 __ Ldr(temp, StackOperandFrom(source));
1694 __ Str(temp, StackOperandFrom(destination));
1695 }
1696 }
1697}
1698
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001699void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001700 CPURegister dst,
1701 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001702 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001703 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001704 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001705 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001706 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001707 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001708 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001709 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001710 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001711 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001712 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001713 case DataType::Type::kInt16:
1714 __ Ldrsh(Register(dst), src);
1715 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001716 case DataType::Type::kInt32:
1717 case DataType::Type::kReference:
1718 case DataType::Type::kInt64:
1719 case DataType::Type::kFloat32:
1720 case DataType::Type::kFloat64:
1721 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001722 __ Ldr(dst, src);
1723 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001724 case DataType::Type::kUint32:
1725 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001726 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001727 LOG(FATAL) << "Unreachable type " << type;
1728 }
1729}
1730
Calin Juravle77520bc2015-01-12 18:45:46 +00001731void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Vladimir Marko98873af2020-12-16 12:10:03 +00001732 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001733 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001734 const MemOperand& src,
1735 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001736 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001737 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001738 Register temp_base = temps.AcquireX();
1739
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001740 DCHECK(!src.IsPreIndex());
1741 DCHECK(!src.IsPostIndex());
1742
1743 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001744 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001745 {
1746 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1747 MemOperand base = MemOperand(temp_base);
1748 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001749 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001750 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001751 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001752 {
1753 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1754 __ ldarb(Register(dst), base);
1755 if (needs_null_check) {
1756 MaybeRecordImplicitNullCheck(instruction);
1757 }
1758 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001759 if (type == DataType::Type::kInt8) {
1760 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001761 }
1762 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001763 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001764 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001765 {
1766 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1767 __ ldarh(Register(dst), base);
1768 if (needs_null_check) {
1769 MaybeRecordImplicitNullCheck(instruction);
1770 }
1771 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001772 if (type == DataType::Type::kInt16) {
1773 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1774 }
Artem Serov914d7a82017-02-07 14:33:49 +00001775 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001776 case DataType::Type::kInt32:
1777 case DataType::Type::kReference:
1778 case DataType::Type::kInt64:
1779 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001780 {
1781 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1782 __ ldar(Register(dst), base);
1783 if (needs_null_check) {
1784 MaybeRecordImplicitNullCheck(instruction);
1785 }
1786 }
1787 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001788 case DataType::Type::kFloat32:
1789 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001790 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001791 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001792
Artem Serov914d7a82017-02-07 14:33:49 +00001793 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1794 {
1795 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1796 __ ldar(temp, base);
1797 if (needs_null_check) {
1798 MaybeRecordImplicitNullCheck(instruction);
1799 }
1800 }
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001801 __ Fmov(VRegister(dst), temp);
Artem Serov914d7a82017-02-07 14:33:49 +00001802 break;
Roland Levillain44015862016-01-22 11:47:17 +00001803 }
Aart Bik66c158e2018-01-31 12:55:04 -08001804 case DataType::Type::kUint32:
1805 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001806 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001807 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001808 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001809 }
1810}
1811
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001812void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001813 CPURegister src,
1814 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001815 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001816 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001817 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001818 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001819 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001820 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001821 case DataType::Type::kUint16:
1822 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001823 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001824 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001825 case DataType::Type::kInt32:
1826 case DataType::Type::kReference:
1827 case DataType::Type::kInt64:
1828 case DataType::Type::kFloat32:
1829 case DataType::Type::kFloat64:
1830 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001831 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001832 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001833 case DataType::Type::kUint32:
1834 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001835 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001836 LOG(FATAL) << "Unreachable type " << type;
1837 }
1838}
1839
Artem Serov914d7a82017-02-07 14:33:49 +00001840void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001841 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001842 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001843 const MemOperand& dst,
1844 bool needs_null_check) {
1845 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001846 UseScratchRegisterScope temps(GetVIXLAssembler());
1847 Register temp_base = temps.AcquireX();
1848
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001849 DCHECK(!dst.IsPreIndex());
1850 DCHECK(!dst.IsPostIndex());
1851
1852 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001853 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001854 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001855 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001856 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001857 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001858 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001859 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001860 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001861 {
1862 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1863 __ stlrb(Register(src), base);
1864 if (needs_null_check) {
1865 MaybeRecordImplicitNullCheck(instruction);
1866 }
1867 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001868 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001869 case DataType::Type::kUint16:
1870 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001871 {
1872 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1873 __ stlrh(Register(src), base);
1874 if (needs_null_check) {
1875 MaybeRecordImplicitNullCheck(instruction);
1876 }
1877 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001878 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001879 case DataType::Type::kInt32:
1880 case DataType::Type::kReference:
1881 case DataType::Type::kInt64:
1882 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001883 {
1884 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1885 __ stlr(Register(src), base);
1886 if (needs_null_check) {
1887 MaybeRecordImplicitNullCheck(instruction);
1888 }
1889 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001890 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001891 case DataType::Type::kFloat32:
1892 case DataType::Type::kFloat64: {
1893 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001894 Register temp_src;
1895 if (src.IsZero()) {
1896 // The zero register is used to avoid synthesizing zero constants.
1897 temp_src = Register(src);
1898 } else {
1899 DCHECK(src.IsFPRegister());
1900 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001901 __ Fmov(temp_src, VRegister(src));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001902 }
Artem Serov914d7a82017-02-07 14:33:49 +00001903 {
1904 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1905 __ stlr(temp_src, base);
1906 if (needs_null_check) {
1907 MaybeRecordImplicitNullCheck(instruction);
1908 }
1909 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001910 break;
1911 }
Aart Bik66c158e2018-01-31 12:55:04 -08001912 case DataType::Type::kUint32:
1913 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001914 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001915 LOG(FATAL) << "Unreachable type " << type;
1916 }
1917}
1918
Calin Juravle175dc732015-08-25 15:42:32 +01001919void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1920 HInstruction* instruction,
1921 uint32_t dex_pc,
1922 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001923 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001924
Vladimir Markof6675082019-05-17 12:05:28 +01001925 ThreadOffset64 entrypoint_offset = GetThreadOffset<kArm64PointerSize>(entrypoint);
1926 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
1927 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
1928 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Marko695348f2020-05-19 14:42:02 +01001929 if (slow_path == nullptr || GetCompilerOptions().IsJitCompiler()) {
Vladimir Markof6675082019-05-17 12:05:28 +01001930 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00001931 // Ensure the pc position is recorded immediately after the `blr` instruction.
1932 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1933 __ blr(lr);
1934 if (EntrypointRequiresStackMap(entrypoint)) {
1935 RecordPcInfo(instruction, dex_pc, slow_path);
1936 }
Vladimir Markof6675082019-05-17 12:05:28 +01001937 } else {
1938 // Ensure the pc position is recorded immediately after the `bl` instruction.
1939 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1940 EmitEntrypointThunkCall(entrypoint_offset);
1941 if (EntrypointRequiresStackMap(entrypoint)) {
1942 RecordPcInfo(instruction, dex_pc, slow_path);
1943 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001944 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001945}
1946
Roland Levillaindec8f632016-07-22 17:10:06 +01001947void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1948 HInstruction* instruction,
1949 SlowPathCode* slow_path) {
1950 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001951 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1952 __ Blr(lr);
1953}
1954
Alexandre Rames67555f72014-11-18 10:55:16 +00001955void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001956 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001957 UseScratchRegisterScope temps(GetVIXLAssembler());
1958 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001959
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001960 // CMP (immediate) is limited to imm12 or imm12<<12, so we would need to materialize
1961 // the constant 0xf0000000 for comparison with the full 32-bit field. To reduce the code
1962 // size, load only the high byte of the field and compare with 0xf0.
1963 // Note: The same code size could be achieved with LDR+MNV(asr #24)+CBNZ but benchmarks
1964 // show that this pattern is slower (tested on little cores).
1965 __ Ldrb(temp, HeapOperand(class_reg, status_byte_offset));
1966 __ Cmp(temp, shifted_visibly_initialized_value);
1967 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001968 __ Bind(slow_path->GetExitLabel());
1969}
Alexandre Rames5319def2014-10-23 10:03:10 +01001970
Vladimir Marko175e7862018-03-27 09:03:13 +00001971void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
1972 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
1973 uint32_t path_to_root = check->GetBitstringPathToRoot();
1974 uint32_t mask = check->GetBitstringMask();
1975 DCHECK(IsPowerOfTwo(mask + 1));
1976 size_t mask_bits = WhichPowerOf2(mask + 1);
1977
1978 if (mask_bits == 16u) {
1979 // Load only the bitstring part of the status word.
1980 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1981 } else {
1982 // /* uint32_t */ temp = temp->status_
1983 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1984 // Extract the bitstring bits.
1985 __ Ubfx(temp, temp, 0, mask_bits);
1986 }
1987 // Compare the bitstring bits to `path_to_root`.
1988 __ Cmp(temp, path_to_root);
1989}
1990
Roland Levillain44015862016-01-22 11:47:17 +00001991void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001992 BarrierType type = BarrierAll;
1993
1994 switch (kind) {
1995 case MemBarrierKind::kAnyAny:
1996 case MemBarrierKind::kAnyStore: {
1997 type = BarrierAll;
1998 break;
1999 }
2000 case MemBarrierKind::kLoadAny: {
2001 type = BarrierReads;
2002 break;
2003 }
2004 case MemBarrierKind::kStoreStore: {
2005 type = BarrierWrites;
2006 break;
2007 }
2008 default:
2009 LOG(FATAL) << "Unexpected memory barrier " << kind;
2010 }
2011 __ Dmb(InnerShareable, type);
2012}
2013
Vladimir Marko7039c112018-04-05 17:59:39 +01002014bool CodeGeneratorARM64::CanUseImplicitSuspendCheck() const {
2015 // Use implicit suspend checks if requested in compiler options unless there are SIMD
2016 // instructions in the graph. The implicit suspend check saves all FP registers as
2017 // 64-bit (in line with the calling convention) but SIMD instructions can use 128-bit
2018 // registers, so they need to be saved in an explicit slow path.
2019 return GetCompilerOptions().GetImplicitSuspendChecks() && !GetGraph()->HasSIMD();
2020}
2021
Serban Constantinescu02164b32014-11-13 14:05:07 +00002022void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2023 HBasicBlock* successor) {
Stelios Ioannouc54cc7c2021-07-09 17:06:03 +01002024 if (instruction->IsNoOp()) {
2025 if (successor != nullptr) {
2026 __ B(codegen_->GetLabelOf(successor));
2027 }
2028 return;
2029 }
2030
Vladimir Marko7039c112018-04-05 17:59:39 +01002031 if (codegen_->CanUseImplicitSuspendCheck()) {
2032 __ Ldr(kImplicitSuspendCheckRegister, MemOperand(kImplicitSuspendCheckRegister));
2033 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Vladimir Markoe45883e2022-01-11 12:38:35 +00002034 if (successor != nullptr) {
2035 __ B(codegen_->GetLabelOf(successor));
2036 }
Vladimir Marko7039c112018-04-05 17:59:39 +01002037 return;
2038 }
2039
Serban Constantinescu02164b32014-11-13 14:05:07 +00002040 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002041 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2042 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002043 slow_path =
2044 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002045 instruction->SetSlowPath(slow_path);
2046 codegen_->AddSlowPath(slow_path);
2047 if (successor != nullptr) {
2048 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002049 }
2050 } else {
2051 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2052 }
2053
Serban Constantinescu02164b32014-11-13 14:05:07 +00002054 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2055 Register temp = temps.AcquireW();
2056
Vladimir Markoddf4fd32021-11-22 16:31:57 +00002057 __ Ldr(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Vladimir Marko254a8582021-11-29 14:08:37 +00002058 __ Tst(temp, Thread::SuspendOrCheckpointRequestFlags());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002059 if (successor == nullptr) {
Vladimir Marko254a8582021-11-29 14:08:37 +00002060 __ B(ne, slow_path->GetEntryLabel());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002061 __ Bind(slow_path->GetReturnLabel());
2062 } else {
Vladimir Marko254a8582021-11-29 14:08:37 +00002063 __ B(eq, codegen_->GetLabelOf(successor));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002064 __ B(slow_path->GetEntryLabel());
2065 // slow_path will return to GetLabelOf(successor).
2066 }
2067}
2068
Alexandre Rames5319def2014-10-23 10:03:10 +01002069InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2070 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002071 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002072 assembler_(codegen->GetAssembler()),
2073 codegen_(codegen) {}
2074
Alexandre Rames67555f72014-11-18 10:55:16 +00002075void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002076 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002077 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002078 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002079 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002080 case DataType::Type::kInt32:
2081 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01002082 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002083 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002084 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002085 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002086
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002087 case DataType::Type::kFloat32:
2088 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002089 locations->SetInAt(0, Location::RequiresFpuRegister());
2090 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002091 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002092 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002093
Alexandre Rames5319def2014-10-23 10:03:10 +01002094 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002095 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002096 }
2097}
2098
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002099void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2100 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00002101 DCHECK(instruction->IsInstanceFieldGet() ||
2102 instruction->IsStaticFieldGet() ||
2103 instruction->IsPredicatedInstanceFieldGet());
2104
2105 bool is_predicated = instruction->IsPredicatedInstanceFieldGet();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002106
2107 bool object_field_get_with_read_barrier =
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00002108 gUseReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002109 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002110 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2111 object_field_get_with_read_barrier
2112 ? LocationSummary::kCallOnSlowPath
2113 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002114 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002115 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko0ecac682018-08-07 10:40:38 +01002116 // We need a temporary register for the read barrier load in
2117 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2118 // only if the field is volatile or the offset is too big.
2119 if (field_info.IsVolatile() ||
2120 field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2121 locations->AddTemp(FixedTempLocation());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002122 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002123 }
Alex Light3a73ffb2021-01-25 14:11:05 +00002124 // Input for object receiver.
2125 locations->SetInAt(is_predicated ? 1 : 0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002126 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alex Light3a73ffb2021-01-25 14:11:05 +00002127 if (is_predicated) {
2128 locations->SetInAt(0, Location::RequiresFpuRegister());
2129 locations->SetOut(Location::SameAsFirstInput());
2130 } else {
2131 locations->SetOut(Location::RequiresFpuRegister());
2132 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002133 } else {
Alex Light3a73ffb2021-01-25 14:11:05 +00002134 if (is_predicated) {
2135 locations->SetInAt(0, Location::RequiresRegister());
2136 locations->SetOut(Location::SameAsFirstInput());
2137 } else {
2138 // The output overlaps for an object field get when read barriers
2139 // are enabled: we do not want the load to overwrite the object's
2140 // location, as we need it to emit the read barrier.
2141 locations->SetOut(Location::RequiresRegister(),
2142 object_field_get_with_read_barrier ? Location::kOutputOverlap
2143 : Location::kNoOutputOverlap);
2144 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002145 }
2146}
2147
2148void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2149 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00002150 DCHECK(instruction->IsInstanceFieldGet() ||
2151 instruction->IsStaticFieldGet() ||
2152 instruction->IsPredicatedInstanceFieldGet());
2153 bool is_predicated = instruction->IsPredicatedInstanceFieldGet();
Roland Levillain44015862016-01-22 11:47:17 +00002154 LocationSummary* locations = instruction->GetLocations();
Alex Light3a73ffb2021-01-25 14:11:05 +00002155 uint32_t receiver_input = is_predicated ? 1 : 0;
2156 Location base_loc = locations->InAt(receiver_input);
Roland Levillain44015862016-01-22 11:47:17 +00002157 Location out = locations->Out();
2158 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002159 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2160 DataType::Type load_type = instruction->GetType();
Alex Light3a73ffb2021-01-25 14:11:05 +00002161 MemOperand field =
2162 HeapOperand(InputRegisterAt(instruction, receiver_input), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002163
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00002164 if (gUseReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002165 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002166 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002167 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002168 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002169 Location maybe_temp =
2170 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002171 // Note that potential implicit null checks are handled in this
2172 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2173 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2174 instruction,
2175 out,
2176 base,
2177 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002178 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002179 /* needs_null_check= */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002180 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002181 } else {
2182 // General case.
2183 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002184 // Note that a potential implicit null check is handled in this
2185 // CodeGeneratorARM64::LoadAcquire call.
2186 // NB: LoadAcquire will record the pc info if needed.
Vladimir Marko98873af2020-12-16 12:10:03 +00002187 codegen_->LoadAcquire(instruction,
2188 load_type,
2189 OutputCPURegister(instruction),
2190 field,
2191 /* needs_null_check= */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002192 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002193 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2194 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002195 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002196 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002197 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002198 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002199 // If read barriers are enabled, emit read barriers other than
2200 // Baker's using a slow path (and also unpoison the loaded
2201 // reference, if heap poisoning is enabled).
2202 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2203 }
Roland Levillain4d027112015-07-01 15:41:14 +01002204 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002205}
2206
2207void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2208 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002209 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002210 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002211 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2212 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002213 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002214 locations->SetInAt(1, Location::RequiresFpuRegister());
2215 } else {
2216 locations->SetInAt(1, Location::RequiresRegister());
2217 }
2218}
2219
2220void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002221 const FieldInfo& field_info,
2222 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002223 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alex Light3a73ffb2021-01-25 14:11:05 +00002224 bool is_predicated =
2225 instruction->IsInstanceFieldSet() && instruction->AsInstanceFieldSet()->GetIsPredicatedSet();
Alexandre Rames09a99962015-04-15 11:47:56 +01002226
2227 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002228 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002229 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002230 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002231 DataType::Type field_type = field_info.GetFieldType();
Alex Light3a73ffb2021-01-25 14:11:05 +00002232 std::optional<vixl::aarch64::Label> pred_is_null;
2233 if (is_predicated) {
2234 pred_is_null.emplace();
2235 __ Cbz(obj, &*pred_is_null);
2236 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002237
Roland Levillain4d027112015-07-01 15:41:14 +01002238 {
2239 // We use a block to end the scratch scope before the write barrier, thus
2240 // freeing the temporary registers so they can be used in `MarkGCCard`.
2241 UseScratchRegisterScope temps(GetVIXLAssembler());
2242
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002243 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002244 DCHECK(value.IsW());
2245 Register temp = temps.AcquireW();
2246 __ Mov(temp, value.W());
2247 GetAssembler()->PoisonHeapReference(temp.W());
2248 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002249 }
Roland Levillain4d027112015-07-01 15:41:14 +01002250
2251 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002252 codegen_->StoreRelease(
Andreas Gampe3db70682018-12-26 15:12:03 -08002253 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check= */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002254 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002255 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2256 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002257 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2258 codegen_->MaybeRecordImplicitNullCheck(instruction);
2259 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002260 }
2261
2262 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002263 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002264 }
Alex Light3a73ffb2021-01-25 14:11:05 +00002265
2266 if (is_predicated) {
2267 __ Bind(&*pred_is_null);
2268 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002269}
2270
Alexandre Rames67555f72014-11-18 10:55:16 +00002271void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002272 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002273
2274 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002275 case DataType::Type::kInt32:
2276 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002277 Register dst = OutputRegister(instr);
2278 Register lhs = InputRegisterAt(instr, 0);
2279 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002280 if (instr->IsAdd()) {
2281 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002282 } else if (instr->IsAnd()) {
2283 __ And(dst, lhs, rhs);
2284 } else if (instr->IsOr()) {
2285 __ Orr(dst, lhs, rhs);
2286 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002287 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002288 } else if (instr->IsRor()) {
2289 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002290 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002291 __ Ror(dst, lhs, shift);
2292 } else {
2293 // Ensure shift distance is in the same size register as the result. If
2294 // we are rotating a long and the shift comes in a w register originally,
2295 // we don't need to sxtw for use as an x since the shift distances are
2296 // all & reg_bits - 1.
2297 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2298 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002299 } else if (instr->IsMin() || instr->IsMax()) {
2300 __ Cmp(lhs, rhs);
2301 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002302 } else {
2303 DCHECK(instr->IsXor());
2304 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002305 }
2306 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002307 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002308 case DataType::Type::kFloat32:
2309 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002310 VRegister dst = OutputFPRegister(instr);
2311 VRegister lhs = InputFPRegisterAt(instr, 0);
2312 VRegister rhs = InputFPRegisterAt(instr, 1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002313 if (instr->IsAdd()) {
2314 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002315 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002316 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002317 } else if (instr->IsMin()) {
2318 __ Fmin(dst, lhs, rhs);
2319 } else if (instr->IsMax()) {
2320 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002321 } else {
2322 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002323 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002324 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002325 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002326 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002327 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002328 }
2329}
2330
Serban Constantinescu02164b32014-11-13 14:05:07 +00002331void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2332 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2333
Vladimir Markoca6fff82017-10-03 14:49:14 +01002334 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002335 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002336 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002337 case DataType::Type::kInt32:
2338 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002339 locations->SetInAt(0, Location::RequiresRegister());
2340 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002341 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002342 break;
2343 }
2344 default:
2345 LOG(FATAL) << "Unexpected shift type " << type;
2346 }
2347}
2348
2349void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2350 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2351
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002352 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002353 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002354 case DataType::Type::kInt32:
2355 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002356 Register dst = OutputRegister(instr);
2357 Register lhs = InputRegisterAt(instr, 0);
2358 Operand rhs = InputOperandAt(instr, 1);
2359 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002360 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002361 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002362 if (instr->IsShl()) {
2363 __ Lsl(dst, lhs, shift_value);
2364 } else if (instr->IsShr()) {
2365 __ Asr(dst, lhs, shift_value);
2366 } else {
2367 __ Lsr(dst, lhs, shift_value);
2368 }
2369 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002370 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002371
2372 if (instr->IsShl()) {
2373 __ Lsl(dst, lhs, rhs_reg);
2374 } else if (instr->IsShr()) {
2375 __ Asr(dst, lhs, rhs_reg);
2376 } else {
2377 __ Lsr(dst, lhs, rhs_reg);
2378 }
2379 }
2380 break;
2381 }
2382 default:
2383 LOG(FATAL) << "Unexpected shift operation type " << type;
2384 }
2385}
2386
Alexandre Rames5319def2014-10-23 10:03:10 +01002387void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002388 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002389}
2390
2391void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002392 HandleBinaryOp(instruction);
2393}
2394
2395void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2396 HandleBinaryOp(instruction);
2397}
2398
2399void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2400 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002401}
2402
Artem Serov7fc63502016-02-09 17:15:29 +00002403void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002404 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002405 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002406 locations->SetInAt(0, Location::RequiresRegister());
2407 // There is no immediate variant of negated bitwise instructions in AArch64.
2408 locations->SetInAt(1, Location::RequiresRegister());
2409 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2410}
2411
Artem Serov7fc63502016-02-09 17:15:29 +00002412void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002413 Register dst = OutputRegister(instr);
2414 Register lhs = InputRegisterAt(instr, 0);
2415 Register rhs = InputRegisterAt(instr, 1);
2416
2417 switch (instr->GetOpKind()) {
2418 case HInstruction::kAnd:
2419 __ Bic(dst, lhs, rhs);
2420 break;
2421 case HInstruction::kOr:
2422 __ Orn(dst, lhs, rhs);
2423 break;
2424 case HInstruction::kXor:
2425 __ Eon(dst, lhs, rhs);
2426 break;
2427 default:
2428 LOG(FATAL) << "Unreachable";
2429 }
2430}
2431
Anton Kirilov74234da2017-01-13 14:42:47 +00002432void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2433 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002434 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2435 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002436 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002437 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002438 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2439 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2440 } else {
2441 locations->SetInAt(0, Location::RequiresRegister());
2442 }
2443 locations->SetInAt(1, Location::RequiresRegister());
2444 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2445}
2446
Anton Kirilov74234da2017-01-13 14:42:47 +00002447void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2448 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002449 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002450 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002451 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002452 Register out = OutputRegister(instruction);
2453 Register left;
2454 if (kind != HInstruction::kNeg) {
2455 left = InputRegisterAt(instruction, 0);
2456 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002457 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002458 // shifter operand operation, the IR generating `right_reg` (input to the type
2459 // conversion) can have a different type from the current instruction's type,
2460 // so we manually indicate the type.
2461 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002462 Operand right_operand(0);
2463
Anton Kirilov74234da2017-01-13 14:42:47 +00002464 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2465 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002466 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2467 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002468 right_operand = Operand(right_reg,
2469 helpers::ShiftFromOpKind(op_kind),
2470 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002471 }
2472
2473 // Logical binary operations do not support extension operations in the
2474 // operand. Note that VIXL would still manage if it was passed by generating
2475 // the extension as a separate instruction.
2476 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
Santiago Aboy Solanes872ec722022-02-18 14:10:25 +00002477 DCHECK_IMPLIES(right_operand.IsExtendedRegister(),
2478 kind != HInstruction::kAnd && kind != HInstruction::kOr &&
2479 kind != HInstruction::kXor && kind != HInstruction::kNeg);
Alexandre Rames8626b742015-11-25 16:28:08 +00002480 switch (kind) {
2481 case HInstruction::kAdd:
2482 __ Add(out, left, right_operand);
2483 break;
2484 case HInstruction::kAnd:
2485 __ And(out, left, right_operand);
2486 break;
2487 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002488 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002489 __ Neg(out, right_operand);
2490 break;
2491 case HInstruction::kOr:
2492 __ Orr(out, left, right_operand);
2493 break;
2494 case HInstruction::kSub:
2495 __ Sub(out, left, right_operand);
2496 break;
2497 case HInstruction::kXor:
2498 __ Eor(out, left, right_operand);
2499 break;
2500 default:
2501 LOG(FATAL) << "Unexpected operation kind: " << kind;
2502 UNREACHABLE();
2503 }
2504}
2505
Artem Serov328429f2016-07-06 16:23:04 +01002506void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002507 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002508 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002509 locations->SetInAt(0, Location::RequiresRegister());
2510 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002511 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002512}
2513
Roland Levillain19c54192016-11-04 13:44:09 +00002514void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002515 __ Add(OutputRegister(instruction),
2516 InputRegisterAt(instruction, 0),
2517 Operand(InputOperandAt(instruction, 1)));
2518}
2519
Artem Serove1811ed2017-04-27 16:50:47 +01002520void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2521 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002522 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002523
2524 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2525
2526 locations->SetInAt(0, Location::RequiresRegister());
2527 // For byte case we don't need to shift the index variable so we can encode the data offset into
2528 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2529 // data offset constant generation out of the loop and reduce the critical path length in the
2530 // loop.
2531 locations->SetInAt(1, shift->GetValue() == 0
2532 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2533 : Location::RequiresRegister());
2534 locations->SetInAt(2, Location::ConstantLocation(shift));
2535 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2536}
2537
2538void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2539 HIntermediateAddressIndex* instruction) {
2540 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002541 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002542 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2543
2544 if (shift == 0) {
2545 __ Add(OutputRegister(instruction), index_reg, offset);
2546 } else {
2547 Register offset_reg = InputRegisterAt(instruction, 1);
2548 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2549 }
2550}
2551
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002552void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002553 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002554 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002555 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2556 if (instr->GetOpKind() == HInstruction::kSub &&
2557 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002558 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002559 // Don't allocate register for Mneg instruction.
2560 } else {
2561 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2562 Location::RequiresRegister());
2563 }
2564 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2565 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002566 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2567}
2568
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002569void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002570 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002571 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2572 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002573
2574 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2575 // This fixup should be carried out for all multiply-accumulate instructions:
2576 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002577 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002578 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2579 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Florian Mayer6f572b72022-01-10 13:03:57 -08002580 ptrdiff_t off = masm->GetCursorOffset();
2581 if (off >= static_cast<ptrdiff_t>(kInstructionSize) &&
2582 masm->GetInstructionAt(off - static_cast<ptrdiff_t>(kInstructionSize))->IsLoadOrStore()) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002583 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002584 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002585 __ nop();
2586 }
2587 }
2588
2589 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002590 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002591 __ Madd(res, mul_left, mul_right, accumulator);
2592 } else {
2593 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002594 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002595 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002596 __ Mneg(res, mul_left, mul_right);
2597 } else {
2598 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2599 __ Msub(res, mul_left, mul_right, accumulator);
2600 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002601 }
2602}
2603
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002604void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002605 bool object_array_get_with_read_barrier =
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00002606 gUseReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002607 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002608 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2609 object_array_get_with_read_barrier
2610 ? LocationSummary::kCallOnSlowPath
2611 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002612 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002613 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002614 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002615 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002616 // We need a temporary register for the read barrier load in
2617 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2618 // only if the offset is too big.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002619 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2620 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002621 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002622 if (offset >= kReferenceLoadMinFarOffset) {
2623 locations->AddTemp(FixedTempLocation());
2624 }
Artem Serov0806f582018-10-11 20:14:20 +01002625 } else if (!instruction->GetArray()->IsIntermediateAddress()) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01002626 // We need a non-scratch temporary for the array data pointer in
Artem Serov0806f582018-10-11 20:14:20 +01002627 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier() for the case with no
2628 // intermediate address.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002629 locations->AddTemp(Location::RequiresRegister());
2630 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002631 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002632 locations->SetInAt(0, Location::RequiresRegister());
2633 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002634 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002635 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2636 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002637 // The output overlaps in the case of an object array get with
2638 // read barriers enabled: we do not want the move to overwrite the
2639 // array's location, as we need it to emit the read barrier.
2640 locations->SetOut(
2641 Location::RequiresRegister(),
2642 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002643 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002644}
2645
2646void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002647 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002648 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002649 LocationSummary* locations = instruction->GetLocations();
2650 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002651 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002652 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002653 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2654 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002655 MacroAssembler* masm = GetVIXLAssembler();
2656 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002657
Artem Serov0806f582018-10-11 20:14:20 +01002658 // The non-Baker read barrier instrumentation of object ArrayGet instructions
Roland Levillain19c54192016-11-04 13:44:09 +00002659 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002660 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002661 instruction->GetArray()->IsIntermediateAddress() &&
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00002662 gUseReadBarrier &&
Artem Serov0806f582018-10-11 20:14:20 +01002663 !kUseBakerReadBarrier));
Roland Levillain19c54192016-11-04 13:44:09 +00002664
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00002665 if (type == DataType::Type::kReference && gUseReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002666 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002667 // Note that a potential implicit null check is handled in the
2668 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002669 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002670 if (index.IsConstant()) {
Artem Serov0806f582018-10-11 20:14:20 +01002671 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002672 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002673 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002674 Location maybe_temp =
2675 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2676 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2677 out,
2678 obj.W(),
2679 offset,
2680 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002681 /* needs_null_check= */ false,
2682 /* use_load_acquire= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002683 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002684 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08002685 instruction, out, obj.W(), offset, index, /* needs_null_check= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002686 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002687 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002688 // General case.
2689 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002690 Register length;
2691 if (maybe_compressed_char_at) {
2692 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2693 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002694 {
2695 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2696 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2697
2698 if (instruction->GetArray()->IsIntermediateAddress()) {
2699 DCHECK_LT(count_offset, offset);
2700 int64_t adjusted_offset =
2701 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2702 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2703 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2704 } else {
2705 __ Ldr(length, HeapOperand(obj, count_offset));
2706 }
2707 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002708 }
jessicahandojo05765752016-09-09 19:01:32 -07002709 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002710 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002711 if (maybe_compressed_char_at) {
2712 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002713 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2714 "Expecting 0=compressed, 1=uncompressed");
2715 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002716 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002717 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002718 __ B(&done);
2719 __ Bind(&uncompressed_load);
2720 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002721 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002722 __ Bind(&done);
2723 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002724 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002725 source = HeapOperand(obj, offset);
2726 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002727 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002728 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002729 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002730 // We do not need to compute the intermediate address from the array: the
2731 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002732 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002733 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002734 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2735 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
Roland Levillain44015862016-01-22 11:47:17 +00002736 }
2737 temp = obj;
2738 } else {
2739 __ Add(temp, obj, offset);
2740 }
jessicahandojo05765752016-09-09 19:01:32 -07002741 if (maybe_compressed_char_at) {
2742 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002743 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2744 "Expecting 0=compressed, 1=uncompressed");
2745 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002746 __ Ldrb(Register(OutputCPURegister(instruction)),
2747 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2748 __ B(&done);
2749 __ Bind(&uncompressed_load);
2750 __ Ldrh(Register(OutputCPURegister(instruction)),
2751 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2752 __ Bind(&done);
2753 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002754 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002755 }
Roland Levillain44015862016-01-22 11:47:17 +00002756 }
jessicahandojo05765752016-09-09 19:01:32 -07002757 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002758 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2759 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002760 codegen_->Load(type, OutputCPURegister(instruction), source);
2761 codegen_->MaybeRecordImplicitNullCheck(instruction);
2762 }
Roland Levillain44015862016-01-22 11:47:17 +00002763
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002764 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002765 static_assert(
2766 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2767 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2768 Location obj_loc = locations->InAt(0);
2769 if (index.IsConstant()) {
2770 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2771 } else {
2772 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2773 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002774 }
Roland Levillain4d027112015-07-01 15:41:14 +01002775 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002776}
2777
Alexandre Rames5319def2014-10-23 10:03:10 +01002778void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002779 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002780 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002781 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002782}
2783
2784void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002785 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002786 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002787 {
2788 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2789 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2790 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2791 codegen_->MaybeRecordImplicitNullCheck(instruction);
2792 }
jessicahandojo05765752016-09-09 19:01:32 -07002793 // Mask out compression flag from String's array length.
2794 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002795 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002796 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002797}
2798
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002799void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002800 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002801
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002802 bool needs_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002803 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002804 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002805 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002806 locations->SetInAt(0, Location::RequiresRegister());
2807 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002808 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2809 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002810 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002811 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002812 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002813 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002814 }
2815}
2816
2817void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002818 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002819 LocationSummary* locations = instruction->GetLocations();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002820 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002821 bool needs_write_barrier =
2822 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002823
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002824 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002825 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002826 CPURegister source = value;
2827 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002828 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002829 MemOperand destination = HeapOperand(array);
2830 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002831
2832 if (!needs_write_barrier) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002833 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002834 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002835 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002836 destination = HeapOperand(array, offset);
2837 } else {
2838 UseScratchRegisterScope temps(masm);
2839 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002840 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002841 // We do not need to compute the intermediate address from the array: the
2842 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002843 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002844 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002845 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2846 DCHECK(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002847 }
2848 temp = array;
2849 } else {
2850 __ Add(temp, array, offset);
2851 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002852 destination = HeapOperand(temp,
2853 XRegisterFrom(index),
2854 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002855 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002856 }
Artem Serov914d7a82017-02-07 14:33:49 +00002857 {
2858 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2859 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2860 codegen_->Store(value_type, value, destination);
2861 codegen_->MaybeRecordImplicitNullCheck(instruction);
2862 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002863 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002864 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002865
2866 bool can_value_be_null = instruction->GetValueCanBeNull();
2867 vixl::aarch64::Label do_store;
2868 if (can_value_be_null) {
2869 __ Cbz(Register(value), &do_store);
2870 }
2871
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002872 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002873 if (needs_type_check) {
2874 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
2875 codegen_->AddSlowPath(slow_path);
2876
2877 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2878 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2879 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2880
Alexandre Rames97833a02015-04-16 15:07:12 +01002881 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002882 Register temp = temps.AcquireSameSizeAs(array);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002883 Register temp2 = temps.AcquireSameSizeAs(array);
2884
2885 // Note that when Baker read barriers are enabled, the type
2886 // checks are performed without read barriers. This is fine,
2887 // even in the case where a class object is in the from-space
2888 // after the flip, as a comparison involving such a type would
2889 // not produce a false positive; it may of course produce a
2890 // false negative, in which case we would take the ArraySet
2891 // slow path.
2892
2893 // /* HeapReference<Class> */ temp = array->klass_
2894 {
2895 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2896 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2897 __ Ldr(temp, HeapOperand(array, class_offset));
2898 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames97833a02015-04-16 15:07:12 +01002899 }
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002900 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Alexandre Rames97833a02015-04-16 15:07:12 +01002901
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002902 // /* HeapReference<Class> */ temp = temp->component_type_
2903 __ Ldr(temp, HeapOperand(temp, component_offset));
2904 // /* HeapReference<Class> */ temp2 = value->klass_
2905 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2906 // If heap poisoning is enabled, no need to unpoison `temp`
2907 // nor `temp2`, as we are comparing two poisoned references.
2908 __ Cmp(temp, temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002909
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002910 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2911 vixl::aarch64::Label do_put;
2912 __ B(eq, &do_put);
2913 // If heap poisoning is enabled, the `temp` reference has
2914 // not been unpoisoned yet; unpoison it now.
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002915 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002916
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002917 // /* HeapReference<Class> */ temp = temp->super_class_
2918 __ Ldr(temp, HeapOperand(temp, super_offset));
2919 // If heap poisoning is enabled, no need to unpoison
2920 // `temp`, as we are comparing against null below.
2921 __ Cbnz(temp, slow_path->GetEntryLabel());
2922 __ Bind(&do_put);
Vladimir Markod1ef8732017-04-18 13:55:13 +01002923 } else {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002924 __ B(ne, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002925 }
2926 }
2927
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002928 codegen_->MarkGCCard(array, value.W(), /* value_can_be_null= */ false);
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002929
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002930 if (can_value_be_null) {
2931 DCHECK(do_store.IsLinked());
2932 __ Bind(&do_store);
2933 }
2934
2935 UseScratchRegisterScope temps(masm);
2936 if (kPoisonHeapReferences) {
2937 Register temp_source = temps.AcquireSameSizeAs(array);
2938 DCHECK(value.IsW());
2939 __ Mov(temp_source, value.W());
2940 GetAssembler()->PoisonHeapReference(temp_source);
2941 source = temp_source;
2942 }
2943
2944 if (index.IsConstant()) {
2945 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
2946 destination = HeapOperand(array, offset);
2947 } else {
2948 Register temp_base = temps.AcquireSameSizeAs(array);
2949 __ Add(temp_base, array, offset);
2950 destination = HeapOperand(temp_base,
2951 XRegisterFrom(index),
2952 LSL,
2953 DataType::SizeShift(value_type));
2954 }
2955
2956 {
2957 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2958 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2959 __ Str(source, destination);
2960
2961 if (can_value_be_null || !needs_type_check) {
2962 codegen_->MaybeRecordImplicitNullCheck(instruction);
2963 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002964 }
2965
2966 if (slow_path != nullptr) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002967 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002968 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002969 }
2970}
2971
Alexandre Rames67555f72014-11-18 10:55:16 +00002972void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002973 RegisterSet caller_saves = RegisterSet::Empty();
2974 InvokeRuntimeCallingConvention calling_convention;
2975 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2976 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2977 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002978
2979 // If both index and length are constant, we can check the bounds statically and
2980 // generate code accordingly. We want to make sure we generate constant locations
2981 // in that case, regardless of whether they are encodable in the comparison or not.
2982 HInstruction* index = instruction->InputAt(0);
2983 HInstruction* length = instruction->InputAt(1);
2984 bool both_const = index->IsConstant() && length->IsConstant();
2985 locations->SetInAt(0, both_const
2986 ? Location::ConstantLocation(index->AsConstant())
2987 : ARM64EncodableConstantOrRegister(index, instruction));
2988 locations->SetInAt(1, both_const
2989 ? Location::ConstantLocation(length->AsConstant())
2990 : ARM64EncodableConstantOrRegister(length, instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002991}
2992
2993void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Georgia Kouvelibe530852019-01-17 10:46:41 +00002994 LocationSummary* locations = instruction->GetLocations();
2995 Location index_loc = locations->InAt(0);
2996 Location length_loc = locations->InAt(1);
2997
2998 int cmp_first_input = 0;
2999 int cmp_second_input = 1;
3000 Condition cond = hs;
3001
3002 if (index_loc.IsConstant()) {
3003 int64_t index = Int64FromLocation(index_loc);
3004 if (length_loc.IsConstant()) {
3005 int64_t length = Int64FromLocation(length_loc);
3006 if (index < 0 || index >= length) {
3007 BoundsCheckSlowPathARM64* slow_path =
3008 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
3009 codegen_->AddSlowPath(slow_path);
3010 __ B(slow_path->GetEntryLabel());
3011 } else {
3012 // BCE will remove the bounds check if we are guaranteed to pass.
3013 // However, some optimization after BCE may have generated this, and we should not
3014 // generate a bounds check if it is a valid range.
3015 }
3016 return;
3017 }
3018 // Only the index is constant: change the order of the operands and commute the condition
3019 // so we can use an immediate constant for the index (only the second input to a cmp
3020 // instruction can be an immediate).
3021 cmp_first_input = 1;
3022 cmp_second_input = 0;
3023 cond = ls;
3024 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003025 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003026 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Georgia Kouvelibe530852019-01-17 10:46:41 +00003027 __ Cmp(InputRegisterAt(instruction, cmp_first_input),
3028 InputOperandAt(instruction, cmp_second_input));
Alexandre Rames67555f72014-11-18 10:55:16 +00003029 codegen_->AddSlowPath(slow_path);
Georgia Kouvelibe530852019-01-17 10:46:41 +00003030 __ B(slow_path->GetEntryLabel(), cond);
Alexandre Rames67555f72014-11-18 10:55:16 +00003031}
3032
Alexandre Rames67555f72014-11-18 10:55:16 +00003033void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3034 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003035 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00003036 locations->SetInAt(0, Location::RequiresRegister());
3037 if (check->HasUses()) {
3038 locations->SetOut(Location::SameAsFirstInput());
3039 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01003040 // Rely on the type initialization to save everything we need.
3041 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00003042}
3043
3044void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3045 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01003046 SlowPathCodeARM64* slow_path =
3047 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00003048 codegen_->AddSlowPath(slow_path);
3049 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3050}
3051
Roland Levillain1a653882016-03-18 18:05:57 +00003052static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3053 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3054 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3055}
3056
3057void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01003058 VRegister lhs_reg = InputFPRegisterAt(instruction, 0);
Roland Levillain1a653882016-03-18 18:05:57 +00003059 Location rhs_loc = instruction->GetLocations()->InAt(1);
3060 if (rhs_loc.IsConstant()) {
3061 // 0.0 is the only immediate that can be encoded directly in
3062 // an FCMP instruction.
3063 //
3064 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3065 // specify that in a floating-point comparison, positive zero
3066 // and negative zero are considered equal, so we can use the
3067 // literal 0.0 for both cases here.
3068 //
3069 // Note however that some methods (Float.equal, Float.compare,
3070 // Float.compareTo, Double.equal, Double.compare,
3071 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3072 // StrictMath.min) consider 0.0 to be (strictly) greater than
3073 // -0.0. So if we ever translate calls to these methods into a
3074 // HCompare instruction, we must handle the -0.0 case with
3075 // care here.
3076 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3077 __ Fcmp(lhs_reg, 0.0);
3078 } else {
3079 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3080 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003081}
3082
Serban Constantinescu02164b32014-11-13 14:05:07 +00003083void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003084 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003085 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003086 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003087 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003088 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003089 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003090 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003091 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003092 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003093 case DataType::Type::kInt32:
3094 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003095 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003096 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003097 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3098 break;
3099 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003100 case DataType::Type::kFloat32:
3101 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003102 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003103 locations->SetInAt(1,
3104 IsFloatingPointZeroConstant(compare->InputAt(1))
3105 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3106 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003107 locations->SetOut(Location::RequiresRegister());
3108 break;
3109 }
3110 default:
3111 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3112 }
3113}
3114
3115void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003116 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00003117
3118 // 0 if: left == right
3119 // 1 if: left > right
3120 // -1 if: left < right
3121 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003122 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003123 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003124 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003125 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003126 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003127 case DataType::Type::kInt32:
3128 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003129 Register result = OutputRegister(compare);
3130 Register left = InputRegisterAt(compare, 0);
3131 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003132 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003133 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3134 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003135 break;
3136 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003137 case DataType::Type::kFloat32:
3138 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003139 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003140 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003141 __ Cset(result, ne);
3142 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003143 break;
3144 }
3145 default:
3146 LOG(FATAL) << "Unimplemented compare type " << in_type;
3147 }
3148}
3149
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003150void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003151 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003152
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003153 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003154 locations->SetInAt(0, Location::RequiresFpuRegister());
3155 locations->SetInAt(1,
3156 IsFloatingPointZeroConstant(instruction->InputAt(1))
3157 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3158 : Location::RequiresFpuRegister());
3159 } else {
3160 // Integer cases.
3161 locations->SetInAt(0, Location::RequiresRegister());
3162 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3163 }
3164
David Brazdilb3e773e2016-01-26 11:28:37 +00003165 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003166 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003167 }
3168}
3169
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003170void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003171 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003172 return;
3173 }
3174
3175 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003176 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003177 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003178
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003179 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003180 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003181 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003182 } else {
3183 // Integer cases.
3184 Register lhs = InputRegisterAt(instruction, 0);
3185 Operand rhs = InputOperandAt(instruction, 1);
3186 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003187 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003188 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003189}
3190
3191#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3192 M(Equal) \
3193 M(NotEqual) \
3194 M(LessThan) \
3195 M(LessThanOrEqual) \
3196 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003197 M(GreaterThanOrEqual) \
3198 M(Below) \
3199 M(BelowOrEqual) \
3200 M(Above) \
3201 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003202#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003203void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3204void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003205FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003206#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003207#undef FOR_EACH_CONDITION_INSTRUCTION
3208
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003209void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003210 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003211 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003212 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3213
3214 Register out = OutputRegister(instruction);
3215 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003216
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003217 Register final_dividend;
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003218 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003219 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
3220 // NOTE: The generated code for HDiv correctly works for the INT32_MIN/INT64_MIN dividends:
3221 // imm == 2
3222 // add out, dividend(0x80000000), dividend(0x80000000), lsr #31 => out = 0x80000001
3223 // asr out, out(0x80000001), #1 => out = 0xc0000000
3224 // This is the same as 'asr out, 0x80000000, #1'
3225 //
3226 // imm > 2
3227 // add temp, dividend(0x80000000), imm - 1 => temp = 0b10..01..1, where the number
3228 // of the rightmost 1s is ctz_imm.
3229 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3230 // csel out, temp(0b10..01..1), dividend(0x80000000), lt => out = 0b10..01..1
3231 // asr out, out(0b10..01..1), #ctz_imm => out = 0b1..10..0, where the number of the
3232 // leftmost 1s is ctz_imm + 1.
3233 // This is the same as 'asr out, dividend(0x80000000), #ctz_imm'.
3234 //
3235 // imm == INT32_MIN
3236 // add tmp, dividend(0x80000000), #0x7fffffff => tmp = -1
3237 // cmp dividend(0x80000000), 0 => N = 1, V = 0 (lt is true)
3238 // csel out, temp(-1), dividend(0x80000000), lt => out = -1
3239 // neg out, out(-1), asr #31 => out = 1
3240 // This is the same as 'neg out, dividend(0x80000000), asr #31'.
3241 final_dividend = dividend;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003242 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003243 if (abs_imm == 2) {
3244 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
3245 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
3246 } else {
3247 UseScratchRegisterScope temps(GetVIXLAssembler());
3248 Register temp = temps.AcquireSameSizeAs(out);
3249 __ Add(temp, dividend, abs_imm - 1);
3250 __ Cmp(dividend, 0);
3251 __ Csel(out, temp, dividend, lt);
3252 }
3253 final_dividend = out;
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003254 }
3255
Zheng Xuc6667102015-05-15 16:08:45 +08003256 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003257 if (imm > 0) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003258 __ Asr(out, final_dividend, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003259 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01003260 __ Neg(out, Operand(final_dividend, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003261 }
3262}
3263
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003264// Return true if the magic number was modified by subtracting 2^32(Int32 div) or 2^64(Int64 div).
3265// So dividend needs to be added.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003266static inline bool NeedToAddDividend(int64_t magic_number, int64_t divisor) {
3267 return divisor > 0 && magic_number < 0;
3268}
3269
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003270// Return true if the magic number was modified by adding 2^32(Int32 div) or 2^64(Int64 div).
3271// So dividend needs to be subtracted.
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003272static inline bool NeedToSubDividend(int64_t magic_number, int64_t divisor) {
3273 return divisor < 0 && magic_number > 0;
3274}
3275
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003276// Generate code which increments the value in register 'in' by 1 if the value is negative.
3277// It is done with 'add out, in, in, lsr #31 or #63'.
3278// If the value is a result of an operation setting the N flag, CINC MI can be used
3279// instead of ADD. 'use_cond_inc' controls this.
3280void InstructionCodeGeneratorARM64::GenerateIncrementNegativeByOne(
3281 Register out,
3282 Register in,
3283 bool use_cond_inc) {
3284 if (use_cond_inc) {
3285 __ Cinc(out, in, mi);
3286 } else {
3287 __ Add(out, in, Operand(in, LSR, in.GetSizeInBits() - 1));
3288 }
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003289}
3290
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003291// Helper to generate code producing the result of HRem with a constant divisor.
3292void InstructionCodeGeneratorARM64::GenerateResultRemWithAnyConstant(
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003293 Register out,
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003294 Register dividend,
3295 Register quotient,
3296 int64_t divisor,
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003297 UseScratchRegisterScope* temps_scope) {
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003298 Register temp_imm = temps_scope->AcquireSameSizeAs(out);
3299 __ Mov(temp_imm, divisor);
3300 __ Msub(out, quotient, temp_imm, dividend);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003301}
3302
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003303// Helper to generate code for HDiv/HRem instructions when a dividend is non-negative and
3304// a divisor is a positive constant, not power of 2.
3305void InstructionCodeGeneratorARM64::GenerateInt64UnsignedDivRemWithAnyPositiveConstant(
3306 HBinaryOperation* instruction) {
3307 DCHECK(instruction->IsDiv() || instruction->IsRem());
3308 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
3309
3310 LocationSummary* locations = instruction->GetLocations();
3311 Location second = locations->InAt(1);
3312 DCHECK(second.IsConstant());
3313
3314 Register out = OutputRegister(instruction);
3315 Register dividend = InputRegisterAt(instruction, 0);
3316 int64_t imm = Int64FromConstant(second.GetConstant());
3317 DCHECK_GT(imm, 0);
3318
3319 int64_t magic;
3320 int shift;
3321 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
3322
3323 UseScratchRegisterScope temps(GetVIXLAssembler());
3324 Register temp = temps.AcquireSameSizeAs(out);
3325
3326 auto generate_unsigned_div_code = [this, magic, shift](Register out,
3327 Register dividend,
3328 Register temp) {
3329 // temp = get_high(dividend * magic)
3330 __ Mov(temp, magic);
3331 if (magic > 0 && shift == 0) {
3332 __ Smulh(out, dividend, temp);
3333 } else {
3334 __ Smulh(temp, dividend, temp);
3335 if (magic < 0) {
3336 // The negative magic means that the multiplier m is greater than INT64_MAX.
3337 // In such a case shift is never 0. See the proof in
3338 // InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant.
3339 __ Add(temp, temp, dividend);
3340 }
3341 DCHECK_NE(shift, 0);
3342 __ Lsr(out, temp, shift);
3343 }
3344 };
3345
3346 if (instruction->IsDiv()) {
3347 generate_unsigned_div_code(out, dividend, temp);
3348 } else {
3349 generate_unsigned_div_code(temp, dividend, temp);
3350 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3351 }
3352}
3353
3354// Helper to generate code for HDiv/HRem instructions for any dividend and a constant divisor
3355// (not power of 2).
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003356void InstructionCodeGeneratorARM64::GenerateInt64DivRemWithAnyConstant(
3357 HBinaryOperation* instruction) {
Zheng Xuc6667102015-05-15 16:08:45 +08003358 DCHECK(instruction->IsDiv() || instruction->IsRem());
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003359 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003360
3361 LocationSummary* locations = instruction->GetLocations();
3362 Location second = locations->InAt(1);
3363 DCHECK(second.IsConstant());
3364
3365 Register out = OutputRegister(instruction);
3366 Register dividend = InputRegisterAt(instruction, 0);
3367 int64_t imm = Int64FromConstant(second.GetConstant());
3368
Zheng Xuc6667102015-05-15 16:08:45 +08003369 int64_t magic;
3370 int shift;
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003371 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003372
3373 UseScratchRegisterScope temps(GetVIXLAssembler());
3374 Register temp = temps.AcquireSameSizeAs(out);
3375
3376 // temp = get_high(dividend * magic)
3377 __ Mov(temp, magic);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003378 __ Smulh(temp, dividend, temp);
3379
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003380 // The multiplication result might need some corrections to be finalized.
3381 // The last correction is to increment by 1, if the result is negative.
3382 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3383 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3384 // However if one of the corrections is ADD or SUB, the sign can be detected
3385 // with ADDS/SUBS. They set the N flag if the result is negative.
3386 // This allows to use CINC MI which has latency 1.
3387 bool use_cond_inc = false;
3388
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003389 // Some combinations of magic_number and the divisor require to correct the result.
3390 // Check whether the correction is needed.
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003391 if (NeedToAddDividend(magic, imm)) {
3392 __ Adds(temp, temp, dividend);
3393 use_cond_inc = true;
3394 } else if (NeedToSubDividend(magic, imm)) {
3395 __ Subs(temp, temp, dividend);
3396 use_cond_inc = true;
3397 }
3398
3399 if (shift != 0) {
3400 __ Asr(temp, temp, shift);
3401 }
3402
3403 if (instruction->IsRem()) {
3404 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3405 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3406 } else {
3407 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3408 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003409}
3410
3411void InstructionCodeGeneratorARM64::GenerateInt32DivRemWithAnyConstant(
3412 HBinaryOperation* instruction) {
3413 DCHECK(instruction->IsDiv() || instruction->IsRem());
3414 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
3415
3416 LocationSummary* locations = instruction->GetLocations();
3417 Location second = locations->InAt(1);
3418 DCHECK(second.IsConstant());
3419
3420 Register out = OutputRegister(instruction);
3421 Register dividend = InputRegisterAt(instruction, 0);
3422 int64_t imm = Int64FromConstant(second.GetConstant());
3423
3424 int64_t magic;
3425 int shift;
3426 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
3427 UseScratchRegisterScope temps(GetVIXLAssembler());
3428 Register temp = temps.AcquireSameSizeAs(out);
3429
3430 // temp = get_high(dividend * magic)
3431 __ Mov(temp, magic);
3432 __ Smull(temp.X(), dividend, temp);
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003433
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003434 // The multiplication result might need some corrections to be finalized.
3435 // The last correction is to increment by 1, if the result is negative.
3436 // Currently it is done with 'add result, temp_result, temp_result, lsr #31 or #63'.
3437 // Such ADD usually has latency 2, e.g. on Cortex-A55.
3438 // However if one of the corrections is ADD or SUB, the sign can be detected
3439 // with ADDS/SUBS. They set the N flag if the result is negative.
3440 // This allows to use CINC MI which has latency 1.
3441 bool use_cond_inc = false;
3442
3443 // ADD/SUB correction is performed in the high 32 bits
3444 // as high 32 bits are ignored because type are kInt32.
3445 if (NeedToAddDividend(magic, imm)) {
3446 __ Adds(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3447 use_cond_inc = true;
3448 } else if (NeedToSubDividend(magic, imm)) {
3449 __ Subs(temp.X(), temp.X(), Operand(dividend.X(), LSL, 32));
3450 use_cond_inc = true;
Evgeny Astigeevich968db3c2020-05-07 12:44:10 +01003451 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003452
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003453 // Extract the result from the high 32 bits and apply the final right shift.
3454 DCHECK_LT(shift, 32);
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01003455 if (imm > 0 && HasNonNegativeInputAt(instruction, 0)) {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003456 // No need to adjust the result for a non-negative dividend and a positive divisor.
3457 if (instruction->IsDiv()) {
3458 __ Lsr(out.X(), temp.X(), 32 + shift);
3459 } else {
3460 __ Lsr(temp.X(), temp.X(), 32 + shift);
3461 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3462 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003463 } else {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01003464 __ Asr(temp.X(), temp.X(), 32 + shift);
3465
3466 if (instruction->IsRem()) {
3467 GenerateIncrementNegativeByOne(temp, temp, use_cond_inc);
3468 GenerateResultRemWithAnyConstant(out, dividend, temp, imm, &temps);
3469 } else {
3470 GenerateIncrementNegativeByOne(out, temp, use_cond_inc);
3471 }
Evgeny Astigeevich0ddb3382020-05-18 11:15:46 +01003472 }
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003473}
3474
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003475void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction,
3476 int64_t divisor) {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003477 DCHECK(instruction->IsDiv() || instruction->IsRem());
3478 if (instruction->GetResultType() == DataType::Type::kInt64) {
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003479 if (divisor > 0 && HasNonNegativeInputAt(instruction, 0)) {
3480 GenerateInt64UnsignedDivRemWithAnyPositiveConstant(instruction);
3481 } else {
3482 GenerateInt64DivRemWithAnyConstant(instruction);
3483 }
Zheng Xuc6667102015-05-15 16:08:45 +08003484 } else {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003485 GenerateInt32DivRemWithAnyConstant(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003486 }
3487}
3488
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003489void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003490 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003491
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003492 if (imm == 0) {
3493 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3494 return;
3495 }
Zheng Xuc6667102015-05-15 16:08:45 +08003496
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003497 if (IsPowerOfTwo(AbsOrMin(imm))) {
3498 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003499 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003500 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3501 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01003502 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003503 }
3504}
3505
3506void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3507 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3508 << instruction->GetResultType();
3509
3510 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3511 GenerateIntDivForConstDenom(instruction);
3512 } else {
3513 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003514 Register dividend = InputRegisterAt(instruction, 0);
3515 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003516 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003517 }
3518}
3519
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003520void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3521 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003522 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003523 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003524 case DataType::Type::kInt32:
3525 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003526 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003527 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003528 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3529 break;
3530
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003531 case DataType::Type::kFloat32:
3532 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003533 locations->SetInAt(0, Location::RequiresFpuRegister());
3534 locations->SetInAt(1, Location::RequiresFpuRegister());
3535 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3536 break;
3537
3538 default:
3539 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3540 }
3541}
3542
3543void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003544 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003545 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003546 case DataType::Type::kInt32:
3547 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003548 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003549 break;
3550
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003551 case DataType::Type::kFloat32:
3552 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003553 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3554 break;
3555
3556 default:
3557 LOG(FATAL) << "Unexpected div type " << type;
3558 }
3559}
3560
Alexandre Rames67555f72014-11-18 10:55:16 +00003561void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003562 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003563 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003564}
3565
3566void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3567 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003568 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003569 codegen_->AddSlowPath(slow_path);
3570 Location value = instruction->GetLocations()->InAt(0);
3571
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003572 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003573
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003574 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003575 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Elliott Hughesc1896c92018-11-29 11:33:18 -08003576 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003577 }
3578
Alexandre Rames67555f72014-11-18 10:55:16 +00003579 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003580 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003581 if (divisor == 0) {
3582 __ B(slow_path->GetEntryLabel());
3583 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003584 // A division by a non-null constant is valid. We don't need to perform
3585 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003586 }
3587 } else {
3588 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3589 }
3590}
3591
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003592void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3593 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003594 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003595 locations->SetOut(Location::ConstantLocation(constant));
3596}
3597
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003598void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3599 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003600 // Will be generated at use site.
3601}
3602
Alexandre Rames5319def2014-10-23 10:03:10 +01003603void LocationsBuilderARM64::VisitExit(HExit* exit) {
3604 exit->SetLocations(nullptr);
3605}
3606
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003607void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003608}
3609
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003610void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3611 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003612 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003613 locations->SetOut(Location::ConstantLocation(constant));
3614}
3615
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003616void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003617 // Will be generated at use site.
3618}
3619
David Brazdilfc6a86a2015-06-26 10:33:45 +00003620void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003621 if (successor->IsExitBlock()) {
3622 DCHECK(got->GetPrevious()->AlwaysThrows());
3623 return; // no code needed
3624 }
3625
Serban Constantinescu02164b32014-11-13 14:05:07 +00003626 HBasicBlock* block = got->GetBlock();
3627 HInstruction* previous = got->GetPrevious();
3628 HLoopInformation* info = block->GetLoopInformation();
3629
David Brazdil46e2a392015-03-16 17:31:52 +00003630 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00003631 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003632 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
Vladimir Markoe45883e2022-01-11 12:38:35 +00003633 return; // `GenerateSuspendCheck()` emitted the jump.
Serban Constantinescu02164b32014-11-13 14:05:07 +00003634 }
3635 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3636 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08003637 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003638 }
3639 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003640 __ B(codegen_->GetLabelOf(successor));
3641 }
3642}
3643
David Brazdilfc6a86a2015-06-26 10:33:45 +00003644void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3645 got->SetLocations(nullptr);
3646}
3647
3648void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3649 HandleGoto(got, got->GetSuccessor());
3650}
3651
3652void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3653 try_boundary->SetLocations(nullptr);
3654}
3655
3656void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3657 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3658 if (!successor->IsExitBlock()) {
3659 HandleGoto(try_boundary, successor);
3660 }
3661}
3662
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003663void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003664 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003665 vixl::aarch64::Label* true_target,
3666 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003667 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003668
David Brazdil0debae72015-11-12 18:37:00 +00003669 if (true_target == nullptr && false_target == nullptr) {
3670 // Nothing to do. The code always falls through.
3671 return;
3672 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003673 // Constant condition, statically compared against "true" (integer value 1).
3674 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003675 if (true_target != nullptr) {
3676 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003677 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003678 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003679 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003680 if (false_target != nullptr) {
3681 __ B(false_target);
3682 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003683 }
David Brazdil0debae72015-11-12 18:37:00 +00003684 return;
3685 }
3686
3687 // The following code generates these patterns:
3688 // (1) true_target == nullptr && false_target != nullptr
3689 // - opposite condition true => branch to false_target
3690 // (2) true_target != nullptr && false_target == nullptr
3691 // - condition true => branch to true_target
3692 // (3) true_target != nullptr && false_target != nullptr
3693 // - condition true => branch to true_target
3694 // - branch to false_target
3695 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003696 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003697 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003698 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003699 if (true_target == nullptr) {
3700 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3701 } else {
3702 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3703 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003704 } else {
3705 // The condition instruction has not been materialized, use its inputs as
3706 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003707 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003708
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003709 DataType::Type type = condition->InputAt(0)->GetType();
3710 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003711 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003712 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003713 IfCondition opposite_condition = condition->GetOppositeCondition();
3714 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003715 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003716 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003717 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003718 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003719 // Integer cases.
3720 Register lhs = InputRegisterAt(condition, 0);
3721 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003722
3723 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003724 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003725 if (true_target == nullptr) {
3726 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3727 non_fallthrough_target = false_target;
3728 } else {
3729 arm64_cond = ARM64Condition(condition->GetCondition());
3730 non_fallthrough_target = true_target;
3731 }
3732
Aart Bik086d27e2016-01-20 17:02:00 -08003733 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003734 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003735 switch (arm64_cond) {
3736 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003737 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003738 break;
3739 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003740 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003741 break;
3742 case lt:
3743 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003744 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003745 break;
3746 case ge:
3747 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003748 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003749 break;
3750 default:
3751 // Without the `static_cast` the compiler throws an error for
3752 // `-Werror=sign-promo`.
3753 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3754 }
3755 } else {
3756 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003757 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003758 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003759 }
3760 }
David Brazdil0debae72015-11-12 18:37:00 +00003761
3762 // If neither branch falls through (case 3), the conditional branch to `true_target`
3763 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3764 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003765 __ B(false_target);
3766 }
3767}
3768
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003769void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003770 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003771 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003772 locations->SetInAt(0, Location::RequiresRegister());
3773 }
3774}
3775
3776void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003777 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3778 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003779 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3780 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3781 true_target = nullptr;
3782 }
3783 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3784 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3785 false_target = nullptr;
3786 }
Andreas Gampe3db70682018-12-26 15:12:03 -08003787 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003788}
3789
3790void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003791 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003792 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003793 InvokeRuntimeCallingConvention calling_convention;
3794 RegisterSet caller_saves = RegisterSet::Empty();
3795 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3796 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003797 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003798 locations->SetInAt(0, Location::RequiresRegister());
3799 }
3800}
3801
3802void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003803 SlowPathCodeARM64* slow_path =
3804 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003805 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08003806 /* condition_input_index= */ 0,
David Brazdil0debae72015-11-12 18:37:00 +00003807 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08003808 /* false_target= */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003809}
3810
Mingyao Yang063fc772016-08-02 11:02:54 -07003811void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003812 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003813 LocationSummary(flag, LocationSummary::kNoCall);
3814 locations->SetOut(Location::RequiresRegister());
3815}
3816
3817void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3818 __ Ldr(OutputRegister(flag),
3819 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3820}
3821
David Brazdilc0b601b2016-02-08 14:20:45 +00003822static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3823 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003824 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003825}
3826
Alexandre Rames880f1192016-06-13 16:04:50 +01003827static inline Condition GetConditionForSelect(HCondition* condition) {
3828 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003829 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3830 : ARM64Condition(cond);
3831}
3832
David Brazdil74eb1b22015-12-14 11:44:01 +00003833void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003834 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003835 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003836 locations->SetInAt(0, Location::RequiresFpuRegister());
3837 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003838 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003839 } else {
3840 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3841 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3842 bool is_true_value_constant = cst_true_value != nullptr;
3843 bool is_false_value_constant = cst_false_value != nullptr;
3844 // Ask VIXL whether we should synthesize constants in registers.
3845 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3846 Operand true_op = is_true_value_constant ?
3847 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3848 Operand false_op = is_false_value_constant ?
3849 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3850 bool true_value_in_register = false;
3851 bool false_value_in_register = false;
3852 MacroAssembler::GetCselSynthesisInformation(
3853 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3854 true_value_in_register |= !is_true_value_constant;
3855 false_value_in_register |= !is_false_value_constant;
3856
3857 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3858 : Location::ConstantLocation(cst_true_value));
3859 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3860 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003861 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003862 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003863
David Brazdil74eb1b22015-12-14 11:44:01 +00003864 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3865 locations->SetInAt(2, Location::RequiresRegister());
3866 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003867}
3868
3869void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003870 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003871 Condition csel_cond;
3872
3873 if (IsBooleanValueOrMaterializedCondition(cond)) {
3874 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003875 // Use the condition flags set by the previous instruction.
3876 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003877 } else {
3878 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003879 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003880 }
3881 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003882 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003883 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003884 } else {
3885 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003886 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003887 }
3888
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003889 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003890 __ Fcsel(OutputFPRegister(select),
3891 InputFPRegisterAt(select, 1),
3892 InputFPRegisterAt(select, 0),
3893 csel_cond);
3894 } else {
3895 __ Csel(OutputRegister(select),
3896 InputOperandAt(select, 1),
3897 InputOperandAt(select, 0),
3898 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003899 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003900}
3901
Santiago Aboy Solanescaf9b652022-06-24 10:03:30 +01003902void LocationsBuilderARM64::VisitNop(HNop* nop) {
3903 new (GetGraph()->GetAllocator()) LocationSummary(nop);
David Srbecky0cf44932015-12-09 14:09:59 +00003904}
3905
Santiago Aboy Solanescaf9b652022-06-24 10:03:30 +01003906void InstructionCodeGeneratorARM64::VisitNop(HNop*) {
3907 // The environment recording already happened in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003908}
3909
Vladimir Markodec78172020-06-19 15:31:23 +01003910void CodeGeneratorARM64::IncreaseFrame(size_t adjustment) {
3911 __ Claim(adjustment);
3912 GetAssembler()->cfi().AdjustCFAOffset(adjustment);
3913}
3914
3915void CodeGeneratorARM64::DecreaseFrame(size_t adjustment) {
3916 __ Drop(adjustment);
3917 GetAssembler()->cfi().AdjustCFAOffset(-adjustment);
3918}
3919
David Srbeckyc7098ff2016-02-09 14:30:11 +00003920void CodeGeneratorARM64::GenerateNop() {
3921 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003922}
3923
Alex Light3a73ffb2021-01-25 14:11:05 +00003924void LocationsBuilderARM64::VisitPredicatedInstanceFieldGet(
3925 HPredicatedInstanceFieldGet* instruction) {
3926 HandleFieldGet(instruction, instruction->GetFieldInfo());
3927}
3928
Alexandre Rames5319def2014-10-23 10:03:10 +01003929void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003930 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003931}
3932
Alex Light3a73ffb2021-01-25 14:11:05 +00003933void InstructionCodeGeneratorARM64::VisitPredicatedInstanceFieldGet(
3934 HPredicatedInstanceFieldGet* instruction) {
3935 vixl::aarch64::Label finish;
3936 __ Cbz(InputRegisterAt(instruction, 1), &finish);
3937 HandleFieldGet(instruction, instruction->GetFieldInfo());
3938 __ Bind(&finish);
3939}
3940
Alexandre Rames5319def2014-10-23 10:03:10 +01003941void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003942 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003943}
3944
3945void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003946 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003947}
3948
3949void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003950 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003951}
3952
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003953// Temp is used for read barrier.
3954static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00003955 if (gUseReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003956 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003957 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3958 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3959 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3960 return 1;
3961 }
3962 return 0;
3963}
3964
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003965// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003966// interface pointer, one for loading the current interface.
3967// The other checks have one temp for loading the object's class.
3968static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3969 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3970 return 3;
3971 }
3972 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003973}
3974
Alexandre Rames67555f72014-11-18 10:55:16 +00003975void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003976 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003977 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003978 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003979 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003980 case TypeCheckKind::kExactCheck:
3981 case TypeCheckKind::kAbstractClassCheck:
3982 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003983 case TypeCheckKind::kArrayObjectCheck: {
3984 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3985 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3986 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003987 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003988 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003989 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003990 case TypeCheckKind::kUnresolvedCheck:
3991 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003992 call_kind = LocationSummary::kCallOnSlowPath;
3993 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003994 case TypeCheckKind::kBitstringCheck:
3995 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003996 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003997
Vladimir Markoca6fff82017-10-03 14:49:14 +01003998 LocationSummary* locations =
3999 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01004000 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004001 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004002 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004003 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004004 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4005 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4006 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4007 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4008 } else {
4009 locations->SetInAt(1, Location::RequiresRegister());
4010 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004011 // The "out" register is used as a temporary, so it overlaps with the inputs.
4012 // Note that TypeCheckSlowPathARM64 uses this register too.
4013 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004014 // Add temps if necessary for read barriers.
4015 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00004016}
4017
4018void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004019 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00004020 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004021 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004022 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004023 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4024 ? Register()
4025 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004026 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00004027 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004028 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
4029 DCHECK_LE(num_temps, 1u);
4030 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004031 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4032 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4033 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4034 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00004035
Scott Wakeling97c72b72016-06-24 16:19:36 +01004036 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004037 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004038
4039 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004040 // Avoid null check if we know `obj` is not null.
4041 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004042 __ Cbz(obj, &zero);
4043 }
4044
Roland Levillain44015862016-01-22 11:47:17 +00004045 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004046 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004047 ReadBarrierOption read_barrier_option =
4048 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004049 // /* HeapReference<Class> */ out = obj->klass_
4050 GenerateReferenceLoadTwoRegisters(instruction,
4051 out_loc,
4052 obj_loc,
4053 class_offset,
4054 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004055 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004056 __ Cmp(out, cls);
4057 __ Cset(out, eq);
4058 if (zero.IsLinked()) {
4059 __ B(&done);
4060 }
4061 break;
4062 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004063
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004064 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004065 ReadBarrierOption read_barrier_option =
4066 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004067 // /* HeapReference<Class> */ out = obj->klass_
4068 GenerateReferenceLoadTwoRegisters(instruction,
4069 out_loc,
4070 obj_loc,
4071 class_offset,
4072 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004073 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004074 // If the class is abstract, we eagerly fetch the super class of the
4075 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004076 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004077 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004078 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004079 GenerateReferenceLoadOneRegister(instruction,
4080 out_loc,
4081 super_offset,
4082 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004083 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004084 // If `out` is null, we use it for the result, and jump to `done`.
4085 __ Cbz(out, &done);
4086 __ Cmp(out, cls);
4087 __ B(ne, &loop);
4088 __ Mov(out, 1);
4089 if (zero.IsLinked()) {
4090 __ B(&done);
4091 }
4092 break;
4093 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004094
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004095 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004096 ReadBarrierOption read_barrier_option =
4097 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004098 // /* HeapReference<Class> */ out = obj->klass_
4099 GenerateReferenceLoadTwoRegisters(instruction,
4100 out_loc,
4101 obj_loc,
4102 class_offset,
4103 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004104 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004105 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004106 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004107 __ Bind(&loop);
4108 __ Cmp(out, cls);
4109 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004110 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004111 GenerateReferenceLoadOneRegister(instruction,
4112 out_loc,
4113 super_offset,
4114 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004115 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004116 __ Cbnz(out, &loop);
4117 // If `out` is null, we use it for the result, and jump to `done`.
4118 __ B(&done);
4119 __ Bind(&success);
4120 __ Mov(out, 1);
4121 if (zero.IsLinked()) {
4122 __ B(&done);
4123 }
4124 break;
4125 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004126
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004127 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004128 ReadBarrierOption read_barrier_option =
4129 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004130 // /* HeapReference<Class> */ out = obj->klass_
4131 GenerateReferenceLoadTwoRegisters(instruction,
4132 out_loc,
4133 obj_loc,
4134 class_offset,
4135 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004136 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004137 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004138 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004139 __ Cmp(out, cls);
4140 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004141 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004142 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004143 GenerateReferenceLoadOneRegister(instruction,
4144 out_loc,
4145 component_offset,
4146 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004147 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004148 // If `out` is null, we use it for the result, and jump to `done`.
4149 __ Cbz(out, &done);
4150 __ Ldrh(out, HeapOperand(out, primitive_offset));
4151 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4152 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004153 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004154 __ Mov(out, 1);
4155 __ B(&done);
4156 break;
4157 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004158
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004159 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004160 // No read barrier since the slow path will retry upon failure.
4161 // /* HeapReference<Class> */ out = obj->klass_
4162 GenerateReferenceLoadTwoRegisters(instruction,
4163 out_loc,
4164 obj_loc,
4165 class_offset,
4166 maybe_temp_loc,
4167 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004168 __ Cmp(out, cls);
4169 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004170 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004171 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004172 codegen_->AddSlowPath(slow_path);
4173 __ B(ne, slow_path->GetEntryLabel());
4174 __ Mov(out, 1);
4175 if (zero.IsLinked()) {
4176 __ B(&done);
4177 }
4178 break;
4179 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004180
Calin Juravle98893e12015-10-02 21:05:03 +01004181 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004182 case TypeCheckKind::kInterfaceCheck: {
4183 // Note that we indeed only call on slow path, but we always go
4184 // into the slow path for the unresolved and interface check
4185 // cases.
4186 //
4187 // We cannot directly call the InstanceofNonTrivial runtime
4188 // entry point without resorting to a type checking slow path
4189 // here (i.e. by calling InvokeRuntime directly), as it would
4190 // require to assign fixed registers for the inputs of this
4191 // HInstanceOf instruction (following the runtime calling
4192 // convention), which might be cluttered by the potential first
4193 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004194 //
4195 // TODO: Introduce a new runtime entry point taking the object
4196 // to test (instead of its class) as argument, and let it deal
4197 // with the read barrier issues. This will let us refactor this
4198 // case of the `switch` code as it was previously (with a direct
4199 // call to the runtime not using a type checking slow path).
4200 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004201 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004202 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08004203 instruction, /* is_fatal= */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004204 codegen_->AddSlowPath(slow_path);
4205 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004206 if (zero.IsLinked()) {
4207 __ B(&done);
4208 }
4209 break;
4210 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004211
4212 case TypeCheckKind::kBitstringCheck: {
4213 // /* HeapReference<Class> */ temp = obj->klass_
4214 GenerateReferenceLoadTwoRegisters(instruction,
4215 out_loc,
4216 obj_loc,
4217 class_offset,
4218 maybe_temp_loc,
4219 kWithoutReadBarrier);
4220
4221 GenerateBitstringTypeCheckCompare(instruction, out);
4222 __ Cset(out, eq);
4223 if (zero.IsLinked()) {
4224 __ B(&done);
4225 }
4226 break;
4227 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004228 }
4229
4230 if (zero.IsLinked()) {
4231 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004232 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004233 }
4234
4235 if (done.IsLinked()) {
4236 __ Bind(&done);
4237 }
4238
4239 if (slow_path != nullptr) {
4240 __ Bind(slow_path->GetExitLabel());
4241 }
4242}
4243
4244void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004245 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004246 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004247 LocationSummary* locations =
4248 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004249 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004250 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4251 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4252 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4253 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4254 } else {
4255 locations->SetInAt(1, Location::RequiresRegister());
4256 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004257 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4258 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004259}
4260
4261void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004262 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004263 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004264 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004265 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004266 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4267 ? Register()
4268 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004269 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4270 DCHECK_GE(num_temps, 1u);
4271 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004272 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004273 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4274 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004275 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004276 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4277 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4278 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4279 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4280 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4281 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4282 const uint32_t object_array_data_offset =
4283 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004284
Vladimir Marko87584542017-12-12 17:47:52 +00004285 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004286 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004287 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4288 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004289 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004290
Scott Wakeling97c72b72016-06-24 16:19:36 +01004291 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004292 // Avoid null check if we know obj is not null.
4293 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004294 __ Cbz(obj, &done);
4295 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004296
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004297 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004298 case TypeCheckKind::kExactCheck:
4299 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004300 // /* HeapReference<Class> */ temp = obj->klass_
4301 GenerateReferenceLoadTwoRegisters(instruction,
4302 temp_loc,
4303 obj_loc,
4304 class_offset,
4305 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004306 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004307
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004308 __ Cmp(temp, cls);
4309 // Jump to slow path for throwing the exception or doing a
4310 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004311 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004312 break;
4313 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004314
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004315 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004316 // /* HeapReference<Class> */ temp = obj->klass_
4317 GenerateReferenceLoadTwoRegisters(instruction,
4318 temp_loc,
4319 obj_loc,
4320 class_offset,
4321 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004322 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004323
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004324 // If the class is abstract, we eagerly fetch the super class of the
4325 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004326 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004327 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004328 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004329 GenerateReferenceLoadOneRegister(instruction,
4330 temp_loc,
4331 super_offset,
4332 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004333 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004334
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004335 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4336 // exception.
4337 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4338 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004339 __ Cmp(temp, cls);
4340 __ B(ne, &loop);
4341 break;
4342 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004343
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004344 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004345 // /* HeapReference<Class> */ temp = obj->klass_
4346 GenerateReferenceLoadTwoRegisters(instruction,
4347 temp_loc,
4348 obj_loc,
4349 class_offset,
4350 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004351 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004352
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004353 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004354 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004355 __ Bind(&loop);
4356 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004357 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004358
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004359 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004360 GenerateReferenceLoadOneRegister(instruction,
4361 temp_loc,
4362 super_offset,
4363 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004364 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004365
4366 // If the class reference currently in `temp` is not null, jump
4367 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004368 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004369 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004370 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004371 break;
4372 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004373
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004374 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004375 // /* HeapReference<Class> */ temp = obj->klass_
4376 GenerateReferenceLoadTwoRegisters(instruction,
4377 temp_loc,
4378 obj_loc,
4379 class_offset,
4380 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004381 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004382
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004383 // Do an exact check.
4384 __ Cmp(temp, cls);
4385 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004386
4387 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004388 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004389 GenerateReferenceLoadOneRegister(instruction,
4390 temp_loc,
4391 component_offset,
4392 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004393 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004394
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004395 // If the component type is null, jump to the slow path to throw the exception.
4396 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4397 // Otherwise, the object is indeed an array. Further check that this component type is not a
4398 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004399 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4400 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004401 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004402 break;
4403 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004404
Calin Juravle98893e12015-10-02 21:05:03 +01004405 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004406 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004407 //
4408 // We cannot directly call the CheckCast runtime entry point
4409 // without resorting to a type checking slow path here (i.e. by
4410 // calling InvokeRuntime directly), as it would require to
4411 // assign fixed registers for the inputs of this HInstanceOf
4412 // instruction (following the runtime calling convention), which
4413 // might be cluttered by the potential first read barrier
4414 // emission at the beginning of this method.
4415 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004416 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004417 case TypeCheckKind::kInterfaceCheck: {
4418 // /* HeapReference<Class> */ temp = obj->klass_
4419 GenerateReferenceLoadTwoRegisters(instruction,
4420 temp_loc,
4421 obj_loc,
4422 class_offset,
4423 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004424 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004425
4426 // /* HeapReference<Class> */ temp = temp->iftable_
4427 GenerateReferenceLoadTwoRegisters(instruction,
4428 temp_loc,
4429 temp_loc,
4430 iftable_offset,
4431 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004432 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004433 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004434 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004435 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004436 vixl::aarch64::Label start_loop;
4437 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004438 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004439 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4440 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004441 // Go to next interface.
4442 __ Add(temp, temp, 2 * kHeapReferenceSize);
4443 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004444 // Compare the classes and continue the loop if they do not match.
4445 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4446 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004447 break;
4448 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004449
4450 case TypeCheckKind::kBitstringCheck: {
4451 // /* HeapReference<Class> */ temp = obj->klass_
4452 GenerateReferenceLoadTwoRegisters(instruction,
4453 temp_loc,
4454 obj_loc,
4455 class_offset,
4456 maybe_temp2_loc,
4457 kWithoutReadBarrier);
4458
4459 GenerateBitstringTypeCheckCompare(instruction, temp);
4460 __ B(ne, type_check_slow_path->GetEntryLabel());
4461 break;
4462 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004463 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004464 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004465
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004466 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004467}
4468
Alexandre Rames5319def2014-10-23 10:03:10 +01004469void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004470 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004471 locations->SetOut(Location::ConstantLocation(constant));
4472}
4473
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004474void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004475 // Will be generated at use site.
4476}
4477
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004478void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004479 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004480 locations->SetOut(Location::ConstantLocation(constant));
4481}
4482
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004483void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004484 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004485}
4486
Calin Juravle175dc732015-08-25 15:42:32 +01004487void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4488 // The trampoline uses the same calling convention as dex calling conventions,
4489 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4490 // the method_idx.
4491 HandleInvoke(invoke);
4492}
4493
4494void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4495 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004496 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004497}
4498
Alexandre Rames5319def2014-10-23 10:03:10 +01004499void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004500 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004501 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004502}
4503
Alexandre Rames67555f72014-11-18 10:55:16 +00004504void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4505 HandleInvoke(invoke);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004506 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
4507 // We cannot request ip1 as it's blocked by the register allocator.
4508 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1, Location::Any());
4509 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004510}
4511
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004512void CodeGeneratorARM64::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
4513 Register klass) {
4514 DCHECK_EQ(klass.GetCode(), 0u);
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004515 // We know the destination of an intrinsic, so no need to record inline
4516 // caches.
4517 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00004518 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004519 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004520 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray9e598902021-11-19 14:53:07 +00004521 ProfilingInfo* info = GetGraph()->GetProfilingInfo();
4522 DCHECK(info != nullptr);
4523 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
4524 uint64_t address = reinterpret_cast64<uint64_t>(cache);
4525 vixl::aarch64::Label done;
4526 __ Mov(x8, address);
4527 __ Ldr(x9, MemOperand(x8, InlineCache::ClassesOffset().Int32Value()));
4528 // Fast path for a monomorphic cache.
4529 __ Cmp(klass, x9);
4530 __ B(eq, &done);
4531 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
4532 __ Bind(&done);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004533 }
4534}
4535
Alexandre Rames67555f72014-11-18 10:55:16 +00004536void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4537 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004538 LocationSummary* locations = invoke->GetLocations();
4539 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004540 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004541 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004542 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004543
Artem Serov914d7a82017-02-07 14:33:49 +00004544 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004545 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004546 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004547 {
4548 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4549 // /* HeapReference<Class> */ temp = temp->klass_
4550 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4551 codegen_->MaybeRecordImplicitNullCheck(invoke);
4552 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004553 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004554 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004555 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004556 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004557 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004558 }
Artem Serov914d7a82017-02-07 14:33:49 +00004559
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004560 // Instead of simply (possibly) unpoisoning `temp` here, we should
4561 // emit a read barrier for the previous class reference load.
4562 // However this is not required in practice, as this is an
4563 // intermediate/temporary reference and because the current
4564 // concurrent copying collector keeps the from-space memory
4565 // intact/accessible until the end of the marking phase (the
4566 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004567 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004568
4569 // If we're compiling baseline, update the inline cache.
4570 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
4571
4572 // The register ip1 is required to be used for the hidden argument in
4573 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
4574 MacroAssembler* masm = GetVIXLAssembler();
4575 UseScratchRegisterScope scratch_scope(masm);
4576 scratch_scope.Exclude(ip1);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004577 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
4578 Location interface_method = locations->InAt(invoke->GetNumberOfArguments() - 1);
4579 if (interface_method.IsStackSlot()) {
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004580 __ Ldr(ip1, StackOperandFrom(interface_method));
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004581 } else {
4582 __ Mov(ip1, XRegisterFrom(interface_method));
4583 }
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004584 // If the load kind is through a runtime call, we will pass the method we
4585 // fetch the IMT, which will either be a no-op if we don't hit the conflict
4586 // stub, or will make us always go through the trampoline when there is a
4587 // conflict.
4588 } else if (invoke->GetHiddenArgumentLoadKind() != MethodLoadKind::kRuntimeCall) {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004589 codegen_->LoadMethod(
4590 invoke->GetHiddenArgumentLoadKind(), Location::RegisterLocation(ip1.GetCode()), invoke);
4591 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004592
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004593 __ Ldr(temp,
4594 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4595 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004596 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004597 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004598 __ Ldr(temp, MemOperand(temp, method_offset));
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00004599 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRuntimeCall) {
4600 // We pass the method from the IMT in case of a conflict. This will ensure
4601 // we go into the runtime to resolve the actual method.
4602 __ Mov(ip1, temp);
4603 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004604 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004605 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004606
4607 {
4608 // Ensure the pc position is recorded immediately after the `blr` instruction.
4609 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4610
4611 // lr();
4612 __ blr(lr);
4613 DCHECK(!codegen_->IsLeafMethod());
4614 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4615 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004616
Andreas Gampe3db70682018-12-26 15:12:03 -08004617 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004618}
4619
4620void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004621 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004622 if (intrinsic.TryDispatch(invoke)) {
4623 return;
4624 }
4625
Alexandre Rames67555f72014-11-18 10:55:16 +00004626 HandleInvoke(invoke);
4627}
4628
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004629void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004630 // Explicit clinit checks triggered by static invokes must have been pruned by
4631 // art::PrepareForRegisterAllocation.
4632 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004633
Vladimir Markoca6fff82017-10-03 14:49:14 +01004634 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004635 if (intrinsic.TryDispatch(invoke)) {
4636 return;
4637 }
4638
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004639 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01004640 CriticalNativeCallingConventionVisitorARM64 calling_convention_visitor(
4641 /*for_register_allocation=*/ true);
4642 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4643 } else {
4644 HandleInvoke(invoke);
4645 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004646}
4647
Andreas Gampe878d58c2015-01-15 23:24:00 -08004648static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4649 if (invoke->GetLocations()->Intrinsified()) {
4650 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4651 intrinsic.Dispatch(invoke);
4652 return true;
4653 }
4654 return false;
4655}
4656
Vladimir Markodc151b22015-10-15 18:02:30 +01004657HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4658 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004659 ArtMethod* method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004660 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004661 return desired_dispatch_info;
4662}
4663
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004664void CodeGeneratorARM64::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
4665 switch (load_kind) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004666 case MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004667 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004668 // Add ADRP with its PC-relative method patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004669 vixl::aarch64::Label* adrp_label =
4670 NewBootImageMethodPatch(invoke->GetResolvedMethodReference());
Vladimir Marko65979462017-05-19 17:25:12 +01004671 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4672 // Add ADD with its PC-relative method patch.
4673 vixl::aarch64::Label* add_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004674 NewBootImageMethodPatch(invoke->GetResolvedMethodReference(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004675 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4676 break;
4677 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004678 case MethodLoadKind::kBootImageRelRo: {
Vladimir Markob066d432018-01-03 13:14:37 +00004679 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
Vladimir Marko9d31daa2022-04-14 10:48:44 +01004680 uint32_t boot_image_offset = GetBootImageOffset(invoke);
4681 LoadBootImageRelRoEntry(WRegisterFrom(temp), boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00004682 break;
4683 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004684 case MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004685 // Add ADRP with its PC-relative .bss entry patch.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004686 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(invoke->GetMethodReference());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004687 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004688 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004689 vixl::aarch64::Label* ldr_label =
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004690 NewMethodBssEntryPatch(invoke->GetMethodReference(), adrp_label);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004691 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004692 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004693 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004694 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004695 case MethodLoadKind::kJitDirectAddress: {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004696 // Load method address from literal pool.
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004697 __ Ldr(XRegisterFrom(temp),
4698 DeduplicateUint64Literal(reinterpret_cast<uint64_t>(invoke->GetResolvedMethod())));
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004699 break;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004700 }
4701 case MethodLoadKind::kRuntimeCall: {
4702 // Test situation, don't do anything.
4703 break;
4704 }
4705 default: {
4706 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
4707 UNREACHABLE();
4708 }
4709 }
4710}
4711
4712void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4713 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
4714 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
4715 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4716 switch (invoke->GetMethodLoadKind()) {
4717 case MethodLoadKind::kStringInit: {
4718 uint32_t offset =
4719 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4720 // temp = thread->string_init_entrypoint
4721 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
4722 break;
4723 }
4724 case MethodLoadKind::kRecursive: {
4725 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
4726 break;
4727 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004728 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004729 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4730 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004731 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004732 case MethodLoadKind::kBootImageLinkTimePcRelative:
4733 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
4734 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
4735 // Do not materialize the method pointer, load directly the entrypoint.
4736 // Add ADRP with its PC-relative JNI entrypoint patch.
4737 vixl::aarch64::Label* adrp_label =
4738 NewBootImageJniEntrypointPatch(invoke->GetResolvedMethodReference());
4739 EmitAdrpPlaceholder(adrp_label, lr);
4740 // Add the LDR with its PC-relative method patch.
4741 vixl::aarch64::Label* add_label =
4742 NewBootImageJniEntrypointPatch(invoke->GetResolvedMethodReference(), adrp_label);
4743 EmitLdrOffsetPlaceholder(add_label, lr, lr);
4744 break;
4745 }
4746 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004747 default: {
4748 LoadMethod(invoke->GetMethodLoadKind(), temp, invoke);
4749 break;
4750 }
Vladimir Marko58155012015-08-19 12:49:41 +00004751 }
4752
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004753 auto call_lr = [&]() {
4754 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4755 ExactAssemblyScope eas(GetVIXLAssembler(),
4756 kInstructionSize,
4757 CodeBufferCheckScope::kExactSize);
4758 // lr()
4759 __ blr(lr);
4760 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko86c87522020-05-11 16:55:55 +01004761 };
Vladimir Marko58155012015-08-19 12:49:41 +00004762 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004763 case CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004764 {
Nicolas Geoffray282795c2021-09-24 18:16:41 +01004765 DCHECK(!GetGraph()->HasShouldDeoptimizeFlag());
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004766 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4767 ExactAssemblyScope eas(GetVIXLAssembler(),
4768 kInstructionSize,
4769 CodeBufferCheckScope::kExactSize);
4770 __ bl(&frame_entry_label_);
4771 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4772 }
Vladimir Marko58155012015-08-19 12:49:41 +00004773 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004774 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01004775 size_t out_frame_size =
4776 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorARM64,
4777 kAapcs64StackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01004778 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004779 if (invoke->GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative) {
4780 call_lr();
4781 } else {
4782 // LR = callee_method->ptr_sized_fields_.data_; // EntryPointFromJni
4783 MemberOffset offset = ArtMethod::EntryPointFromJniOffset(kArm64PointerSize);
4784 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4785 // lr()
4786 call_lr();
4787 }
Vladimir Marko86c87522020-05-11 16:55:55 +01004788 // Zero-/sign-extend the result when needed due to native and managed ABI mismatch.
4789 switch (invoke->GetType()) {
4790 case DataType::Type::kBool:
4791 __ Ubfx(w0, w0, 0, 8);
4792 break;
4793 case DataType::Type::kInt8:
4794 __ Sbfx(w0, w0, 0, 8);
4795 break;
4796 case DataType::Type::kUint16:
4797 __ Ubfx(w0, w0, 0, 16);
4798 break;
4799 case DataType::Type::kInt16:
4800 __ Sbfx(w0, w0, 0, 16);
4801 break;
4802 case DataType::Type::kInt32:
4803 case DataType::Type::kInt64:
4804 case DataType::Type::kFloat32:
4805 case DataType::Type::kFloat64:
4806 case DataType::Type::kVoid:
4807 break;
4808 default:
4809 DCHECK(false) << invoke->GetType();
4810 break;
4811 }
4812 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01004813 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01004814 }
4815 break;
4816 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004817 case CodePtrLocation::kCallArtMethod: {
4818 // LR = callee_method->ptr_sized_fields_.entry_point_from_quick_compiled_code_;
4819 MemberOffset offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
4820 __ Ldr(lr, MemOperand(XRegisterFrom(callee_method), offset.Int32Value()));
4821 // lr()
4822 call_lr();
Vladimir Marko58155012015-08-19 12:49:41 +00004823 break;
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004824 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004825 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004826
Andreas Gampe878d58c2015-01-15 23:24:00 -08004827 DCHECK(!IsLeafMethod());
4828}
4829
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004830void CodeGeneratorARM64::GenerateVirtualCall(
4831 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004832 // Use the calling convention instead of the location of the receiver, as
4833 // intrinsics may have put the receiver in a different register. In the intrinsics
4834 // slow path, the arguments have been moved to the right place, so here we are
4835 // guaranteed that the receiver is the first register of the calling convention.
4836 InvokeDexCallingConvention calling_convention;
4837 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004838 Register temp = XRegisterFrom(temp_in);
4839 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4840 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4841 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004842 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004843
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004844 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004845
4846 {
4847 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4848 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4849 // /* HeapReference<Class> */ temp = receiver->klass_
4850 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4851 MaybeRecordImplicitNullCheck(invoke);
4852 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004853 // Instead of simply (possibly) unpoisoning `temp` here, we should
4854 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004855 // intermediate/temporary reference and because the current
4856 // concurrent copying collector keeps the from-space memory
4857 // intact/accessible until the end of the marking phase (the
4858 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004859 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004860
4861 // If we're compiling baseline, update the inline cache.
4862 MaybeGenerateInlineCacheCheck(invoke, temp);
4863
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004864 // temp = temp->GetMethodAt(method_offset);
4865 __ Ldr(temp, MemOperand(temp, method_offset));
4866 // lr = temp->GetEntryPoint();
4867 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004868 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004869 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004870 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4871 // lr();
4872 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004873 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004874 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004875}
4876
Vladimir Marko9922f002020-06-08 15:05:15 +01004877void CodeGeneratorARM64::MoveFromReturnRegister(Location trg, DataType::Type type) {
4878 if (!trg.IsValid()) {
4879 DCHECK(type == DataType::Type::kVoid);
4880 return;
4881 }
4882
4883 DCHECK_NE(type, DataType::Type::kVoid);
4884
4885 if (DataType::IsIntegralType(type) || type == DataType::Type::kReference) {
4886 Register trg_reg = RegisterFrom(trg, type);
4887 Register res_reg = RegisterFrom(ARM64ReturnLocation(type), type);
4888 __ Mov(trg_reg, res_reg, kDiscardForSameWReg);
4889 } else {
4890 VRegister trg_reg = FPRegisterFrom(trg, type);
4891 VRegister res_reg = FPRegisterFrom(ARM64ReturnLocation(type), type);
4892 __ Fmov(trg_reg, res_reg);
4893 }
4894}
4895
Orion Hodsonac141392017-01-13 11:53:47 +00004896void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004897 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
4898 if (intrinsic.TryDispatch(invoke)) {
4899 return;
4900 }
Orion Hodsonac141392017-01-13 11:53:47 +00004901 HandleInvoke(invoke);
4902}
4903
4904void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00004905 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4906 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
4907 return;
4908 }
Orion Hodsonac141392017-01-13 11:53:47 +00004909 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004910 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004911}
4912
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004913void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4914 HandleInvoke(invoke);
4915}
4916
4917void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4918 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004919 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004920}
4921
Vladimir Marko6fd16062018-06-26 11:02:04 +01004922vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4923 uint32_t intrinsic_data,
4924 vixl::aarch64::Label* adrp_label) {
4925 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004926 /* dex_file= */ nullptr, intrinsic_data, adrp_label, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004927}
4928
Vladimir Markob066d432018-01-03 13:14:37 +00004929vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4930 uint32_t boot_image_offset,
4931 vixl::aarch64::Label* adrp_label) {
4932 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004933 /* dex_file= */ nullptr, boot_image_offset, adrp_label, &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00004934}
4935
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004936vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004937 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004938 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004939 return NewPcRelativePatch(
4940 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004941}
4942
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004943vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4944 MethodReference target_method,
4945 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004946 return NewPcRelativePatch(
4947 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004948}
4949
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004950vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004951 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004952 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004953 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004954 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004955}
4956
Vladimir Marko1998cd02017-01-13 13:02:58 +00004957vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
Vladimir Marko8f63f102020-09-28 12:10:28 +01004958 HLoadClass* load_class,
Vladimir Marko1998cd02017-01-13 13:02:58 +00004959 vixl::aarch64::Label* adrp_label) {
Vladimir Marko8f63f102020-09-28 12:10:28 +01004960 const DexFile& dex_file = load_class->GetDexFile();
4961 dex::TypeIndex type_index = load_class->GetTypeIndex();
4962 ArenaDeque<PcRelativePatchInfo>* patches = nullptr;
4963 switch (load_class->GetLoadKind()) {
4964 case HLoadClass::LoadKind::kBssEntry:
4965 patches = &type_bss_entry_patches_;
4966 break;
4967 case HLoadClass::LoadKind::kBssEntryPublic:
4968 patches = &public_type_bss_entry_patches_;
4969 break;
4970 case HLoadClass::LoadKind::kBssEntryPackage:
4971 patches = &package_type_bss_entry_patches_;
4972 break;
4973 default:
4974 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
4975 UNREACHABLE();
4976 }
4977 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004978}
4979
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004980vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004981 const DexFile& dex_file,
4982 dex::StringIndex string_index,
4983 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004984 return NewPcRelativePatch(
4985 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004986}
4987
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004988vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4989 const DexFile& dex_file,
4990 dex::StringIndex string_index,
4991 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004992 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004993}
4994
Vladimir Markoeb9eb002020-10-02 13:54:19 +01004995vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageJniEntrypointPatch(
4996 MethodReference target_method,
4997 vixl::aarch64::Label* adrp_label) {
4998 return NewPcRelativePatch(
4999 target_method.dex_file, target_method.index, adrp_label, &boot_image_jni_entrypoint_patches_);
5000}
5001
Vladimir Markof6675082019-05-17 12:05:28 +01005002void CodeGeneratorARM64::EmitEntrypointThunkCall(ThreadOffset64 entrypoint_offset) {
5003 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01005004 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markof6675082019-05-17 12:05:28 +01005005 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
5006 vixl::aarch64::Label* bl_label = &call_entrypoint_patches_.back().label;
5007 __ bind(bl_label);
5008 __ bl(static_cast<int64_t>(0)); // Placeholder, patched at link-time.
5009}
5010
Vladimir Marko966b46f2018-08-03 10:20:19 +00005011void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
Vladimir Marko94796f82018-08-08 15:15:33 +01005012 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01005013 if (GetCompilerOptions().IsJitCompiler()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00005014 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
5015 vixl::aarch64::Label* slow_path_entry = &it->second.label;
5016 __ cbnz(mr, slow_path_entry);
5017 } else {
5018 baker_read_barrier_patches_.emplace_back(custom_data);
5019 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
5020 __ bind(cbnz_label);
5021 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
5022 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005023}
5024
Scott Wakeling97c72b72016-06-24 16:19:36 +01005025vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005026 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005027 uint32_t offset_or_index,
5028 vixl::aarch64::Label* adrp_label,
5029 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005030 // Add a patch entry and return the label.
5031 patches->emplace_back(dex_file, offset_or_index);
5032 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01005033 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005034 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
5035 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
5036 return label;
5037}
5038
Scott Wakeling97c72b72016-06-24 16:19:36 +01005039vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
5040 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005041 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005042}
5043
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005044vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005045 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005046 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005047 return jit_string_patches_.GetOrCreate(
5048 StringReference(&dex_file, string_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08005049 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005050}
5051
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005052vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005053 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005054 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005055 return jit_class_patches_.GetOrCreate(
5056 TypeReference(&dex_file, type_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08005057 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005058}
5059
Vladimir Markoaad75c62016-10-03 08:46:48 +00005060void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
5061 vixl::aarch64::Register reg) {
5062 DCHECK(reg.IsX());
5063 SingleEmissionCheckScope guard(GetVIXLAssembler());
5064 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005065 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005066}
5067
5068void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
5069 vixl::aarch64::Register out,
5070 vixl::aarch64::Register base) {
5071 DCHECK(out.IsX());
5072 DCHECK(base.IsX());
5073 SingleEmissionCheckScope guard(GetVIXLAssembler());
5074 __ Bind(fixup_label);
5075 __ add(out, base, Operand(/* offset placeholder */ 0));
5076}
5077
5078void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
5079 vixl::aarch64::Register out,
5080 vixl::aarch64::Register base) {
5081 DCHECK(base.IsX());
5082 SingleEmissionCheckScope guard(GetVIXLAssembler());
5083 __ Bind(fixup_label);
5084 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
5085}
5086
Vladimir Marko9d31daa2022-04-14 10:48:44 +01005087void CodeGeneratorARM64::LoadBootImageRelRoEntry(vixl::aarch64::Register reg,
5088 uint32_t boot_image_offset) {
5089 DCHECK(reg.IsW());
5090 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5091 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
5092 EmitAdrpPlaceholder(adrp_label, reg.X());
5093 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
5094 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
5095 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
5096}
5097
Vladimir Markoeebb8212018-06-05 14:57:24 +01005098void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005099 uint32_t boot_image_reference) {
5100 if (GetCompilerOptions().IsBootImage()) {
5101 // Add ADRP with its PC-relative type patch.
5102 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
5103 EmitAdrpPlaceholder(adrp_label, reg.X());
5104 // Add ADD with its PC-relative type patch.
5105 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
5106 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005107 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Marko9d31daa2022-04-14 10:48:44 +01005108 LoadBootImageRelRoEntry(reg, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005109 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01005110 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005111 gc::Heap* heap = Runtime::Current()->GetHeap();
5112 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005113 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005114 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
5115 }
5116}
5117
Vladimir Marko98873af2020-12-16 12:10:03 +00005118void CodeGeneratorARM64::LoadTypeForBootImageIntrinsic(vixl::aarch64::Register reg,
5119 TypeReference target_type) {
5120 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Vladimir Marko9d31daa2022-04-14 10:48:44 +01005121 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko98873af2020-12-16 12:10:03 +00005122 // Add ADRP with its PC-relative type patch.
5123 vixl::aarch64::Label* adrp_label =
5124 NewBootImageTypePatch(*target_type.dex_file, target_type.TypeIndex());
5125 EmitAdrpPlaceholder(adrp_label, reg.X());
5126 // Add ADD with its PC-relative type patch.
5127 vixl::aarch64::Label* add_label =
5128 NewBootImageTypePatch(*target_type.dex_file, target_type.TypeIndex(), adrp_label);
5129 EmitAddPlaceholder(add_label, reg.X(), reg.X());
5130}
5131
Vladimir Markode91ca92020-10-27 13:41:40 +00005132void CodeGeneratorARM64::LoadIntrinsicDeclaringClass(vixl::aarch64::Register reg, HInvoke* invoke) {
5133 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005134 if (GetCompilerOptions().IsBootImage()) {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005135 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01005136 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
Vladimir Marko98873af2020-12-16 12:10:03 +00005137 LoadTypeForBootImageIntrinsic(reg, TypeReference(target_method.dex_file, type_idx));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005138 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00005139 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
5140 LoadBootImageAddress(reg, boot_image_offset);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005141 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01005142}
5143
Vladimir Marko98873af2020-12-16 12:10:03 +00005144void CodeGeneratorARM64::LoadClassRootForIntrinsic(vixl::aarch64::Register reg,
5145 ClassRoot class_root) {
5146 if (GetCompilerOptions().IsBootImage()) {
5147 ScopedObjectAccess soa(Thread::Current());
5148 ObjPtr<mirror::Class> klass = GetClassRoot(class_root);
5149 TypeReference target_type(&klass->GetDexFile(), klass->GetDexTypeIndex());
5150 LoadTypeForBootImageIntrinsic(reg, target_type);
5151 } else {
5152 uint32_t boot_image_offset = GetBootImageOffset(class_root);
5153 LoadBootImageAddress(reg, boot_image_offset);
5154 }
5155}
5156
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005157template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005158inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
5159 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005160 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005161 for (const PcRelativePatchInfo& info : infos) {
5162 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005163 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00005164 info.pc_insn_label->GetLocation(),
5165 info.offset_or_index));
5166 }
5167}
5168
Vladimir Marko6fd16062018-06-26 11:02:04 +01005169template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5170linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5171 const DexFile* target_dex_file,
5172 uint32_t pc_insn_offset,
5173 uint32_t boot_image_offset) {
5174 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5175 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005176}
5177
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005178void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005179 DCHECK(linker_patches->empty());
5180 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005181 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005182 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005183 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005184 type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01005185 public_type_bss_entry_patches_.size() +
5186 package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005187 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005188 string_bss_entry_patches_.size() +
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005189 boot_image_jni_entrypoint_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005190 boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01005191 call_entrypoint_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005192 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00005193 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005194 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005195 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005196 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005197 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005198 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005199 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005200 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01005201 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005202 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005203 DCHECK(boot_image_type_patches_.empty());
5204 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005205 }
5206 if (GetCompilerOptions().IsBootImage()) {
5207 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5208 boot_image_other_patches_, linker_patches);
5209 } else {
5210 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5211 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005212 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005213 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5214 method_bss_entry_patches_, linker_patches);
5215 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5216 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005217 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
5218 public_type_bss_entry_patches_, linker_patches);
5219 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
5220 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005221 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5222 string_bss_entry_patches_, linker_patches);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005223 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeJniEntrypointPatch>(
5224 boot_image_jni_entrypoint_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01005225 for (const PatchInfo<vixl::aarch64::Label>& info : call_entrypoint_patches_) {
5226 DCHECK(info.target_dex_file == nullptr);
5227 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
5228 info.label.GetLocation(), info.offset_or_index));
5229 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005230 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005231 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
5232 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005233 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00005234 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005235}
5236
Vladimir Markoca1e0382018-04-11 09:58:41 +00005237bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01005238 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
5239 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00005240 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
5241}
5242
5243void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
5244 /*out*/ ArenaVector<uint8_t>* code,
5245 /*out*/ std::string* debug_name) {
5246 Arm64Assembler assembler(GetGraph()->GetAllocator());
5247 switch (patch.GetType()) {
5248 case linker::LinkerPatch::Type::kCallRelative: {
5249 // The thunk just uses the entry point in the ArtMethod. This works even for calls
5250 // to the generic JNI and interpreter trampolines.
5251 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5252 kArm64PointerSize).Int32Value());
5253 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
David Srbecky889da942021-04-30 13:03:14 +01005254 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00005255 *debug_name = "MethodCallThunk";
5256 }
5257 break;
5258 }
Vladimir Markof6675082019-05-17 12:05:28 +01005259 case linker::LinkerPatch::Type::kCallEntrypoint: {
5260 Offset offset(patch.EntrypointOffset());
5261 assembler.JumpTo(ManagedRegister(arm64::TR), offset, ManagedRegister(arm64::IP0));
David Srbecky889da942021-04-30 13:03:14 +01005262 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markof6675082019-05-17 12:05:28 +01005263 *debug_name = "EntrypointCallThunk_" + std::to_string(offset.Uint32Value());
5264 }
5265 break;
5266 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00005267 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
5268 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
5269 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
5270 break;
5271 }
5272 default:
5273 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
5274 UNREACHABLE();
5275 }
5276
5277 // Ensure we emit the literal pool if any.
5278 assembler.FinalizeCode();
5279 code->resize(assembler.CodeSize());
5280 MemoryRegion code_region(code->data(), code->size());
5281 assembler.FinalizeInstructions(code_region);
5282}
5283
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005284vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
5285 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005286 value,
5287 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
5288}
5289
Scott Wakeling97c72b72016-06-24 16:19:36 +01005290vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005291 return uint64_literals_.GetOrCreate(
5292 value,
5293 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00005294}
5295
Andreas Gampe878d58c2015-01-15 23:24:00 -08005296void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005297 // Explicit clinit checks triggered by static invokes must have been pruned by
5298 // art::PrepareForRegisterAllocation.
5299 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01005300
Andreas Gampe878d58c2015-01-15 23:24:00 -08005301 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005302 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005303 return;
5304 }
5305
Vladimir Marko86c87522020-05-11 16:55:55 +01005306 LocationSummary* locations = invoke->GetLocations();
5307 codegen_->GenerateStaticOrDirectCall(
5308 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005309
Andreas Gampe3db70682018-12-26 15:12:03 -08005310 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005311}
5312
5313void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08005314 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08005315 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005316 return;
5317 }
5318
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005319 {
5320 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
5321 // are no pools emitted.
5322 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
5323 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
5324 DCHECK(!codegen_->IsLeafMethod());
5325 }
5326
Andreas Gampe3db70682018-12-26 15:12:03 -08005327 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005328}
5329
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005330HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
5331 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005332 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005333 case HLoadClass::LoadKind::kInvalid:
5334 LOG(FATAL) << "UNREACHABLE";
5335 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005336 case HLoadClass::LoadKind::kReferrersClass:
5337 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005338 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005339 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005340 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01005341 case HLoadClass::LoadKind::kBssEntryPublic:
5342 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01005343 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005344 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005345 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005346 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005347 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005348 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005349 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005350 break;
5351 }
5352 return desired_class_load_kind;
5353}
5354
Alexandre Rames67555f72014-11-18 10:55:16 +00005355void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005356 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005357 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005358 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005359 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005360 cls,
5361 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005362 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005363 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005364 return;
5365 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005366 DCHECK_EQ(cls->NeedsAccessCheck(),
5367 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5368 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005369
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005370 const bool requires_read_barrier = gUseReadBarrier && !cls->IsInBootImage();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005371 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005372 ? LocationSummary::kCallOnSlowPath
5373 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005374 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005375 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005376 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005377 }
5378
Vladimir Marko41559982017-01-06 14:04:23 +00005379 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005380 locations->SetInAt(0, Location::RequiresRegister());
5381 }
5382 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005383 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005384 if (!gUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005385 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005386 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005387 } else {
5388 // For non-Baker read barrier we have a temp-clobbering call.
5389 }
5390 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005391}
5392
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005393// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5394// move.
5395void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005396 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005397 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005398 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08005399 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01005400 return;
5401 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005402 DCHECK_EQ(cls->NeedsAccessCheck(),
5403 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
5404 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Calin Juravle580b6092015-10-06 17:35:58 +01005405
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005406 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01005407 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00005408
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005409 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5410 ? kWithoutReadBarrier
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005411 : gCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005412 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005413 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005414 case HLoadClass::LoadKind::kReferrersClass: {
5415 DCHECK(!cls->CanCallRuntime());
5416 DCHECK(!cls->MustGenerateClinitCheck());
5417 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5418 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00005419 codegen_->GenerateGcRootFieldLoad(cls,
5420 out_loc,
5421 current_method,
5422 ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005423 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005424 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005425 break;
5426 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005427 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005428 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5429 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005430 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005431 // Add ADRP with its PC-relative type patch.
5432 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08005433 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005434 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005435 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005436 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005437 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005438 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005439 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005440 break;
5441 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005442 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005443 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markode91ca92020-10-27 13:41:40 +00005444 uint32_t boot_image_offset = CodeGenerator::GetBootImageOffset(cls);
Vladimir Marko9d31daa2022-04-14 10:48:44 +01005445 codegen_->LoadBootImageRelRoEntry(out.W(), boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005446 break;
5447 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01005448 case HLoadClass::LoadKind::kBssEntry:
5449 case HLoadClass::LoadKind::kBssEntryPublic:
5450 case HLoadClass::LoadKind::kBssEntryPackage: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005451 // Add ADRP with its PC-relative Class .bss entry patch.
Vladimir Markof3c52b42017-11-17 17:32:12 +00005452 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005453 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(cls);
Vladimir Markof3c52b42017-11-17 17:32:12 +00005454 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005455 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko8f63f102020-09-28 12:10:28 +01005456 vixl::aarch64::Label* ldr_label = codegen_->NewBssEntryTypePatch(cls, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005457 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005458 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005459 codegen_->GenerateGcRootFieldLoad(cls,
5460 out_loc,
5461 temp,
5462 /* offset placeholder */ 0u,
5463 ldr_label,
5464 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005465 generate_null_check = true;
5466 break;
5467 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005468 case HLoadClass::LoadKind::kJitBootImageAddress: {
5469 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5470 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
5471 DCHECK_NE(address, 0u);
5472 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5473 break;
5474 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005475 case HLoadClass::LoadKind::kJitTableAddress: {
5476 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5477 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005478 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005479 codegen_->GenerateGcRootFieldLoad(cls,
5480 out_loc,
5481 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005482 /* offset= */ 0,
5483 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005484 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005485 break;
5486 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005487 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005488 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005489 LOG(FATAL) << "UNREACHABLE";
5490 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005491 }
5492
Vladimir Markoea4c1262017-02-06 19:59:33 +00005493 bool do_clinit = cls->MustGenerateClinitCheck();
5494 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005495 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01005496 SlowPathCodeARM64* slow_path =
5497 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005498 codegen_->AddSlowPath(slow_path);
5499 if (generate_null_check) {
5500 __ Cbz(out, slow_path->GetEntryLabel());
5501 }
5502 if (cls->MustGenerateClinitCheck()) {
5503 GenerateClassInitializationCheck(slow_path, out);
5504 } else {
5505 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005506 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005507 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005508 }
5509}
5510
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005511void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5512 InvokeRuntimeCallingConvention calling_convention;
5513 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5514 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5515}
5516
5517void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5518 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5519}
5520
Orion Hodson18259d72018-04-12 11:18:23 +01005521void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5522 InvokeRuntimeCallingConvention calling_convention;
5523 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5524 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5525}
5526
5527void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5528 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5529}
5530
David Brazdilcb1c0552015-08-04 16:22:25 +01005531static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005532 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005533}
5534
Alexandre Rames67555f72014-11-18 10:55:16 +00005535void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5536 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005537 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005538 locations->SetOut(Location::RequiresRegister());
5539}
5540
5541void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005542 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5543}
5544
5545void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005546 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005547}
5548
5549void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5550 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005551}
5552
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005553HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5554 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005555 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005556 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005557 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005558 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01005559 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005560 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005561 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005562 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01005563 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005564 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005565 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005566 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005567 }
5568 return desired_string_load_kind;
5569}
5570
Alexandre Rames67555f72014-11-18 10:55:16 +00005571void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005572 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005573 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005574 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005575 InvokeRuntimeCallingConvention calling_convention;
5576 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5577 } else {
5578 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005579 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005580 if (!gUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005581 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005582 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005583 } else {
5584 // For non-Baker read barrier we have a temp-clobbering call.
5585 }
5586 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005587 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005588}
5589
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005590// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5591// move.
5592void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005593 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005594 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005595
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005596 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005597 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005598 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5599 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005600 // Add ADRP with its PC-relative String patch.
5601 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005602 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005603 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005604 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005605 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005606 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005607 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005608 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005609 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005610 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005611 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005612 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markode91ca92020-10-27 13:41:40 +00005613 uint32_t boot_image_offset = CodeGenerator::GetBootImageOffset(load);
Vladimir Marko9d31daa2022-04-14 10:48:44 +01005614 codegen_->LoadBootImageRelRoEntry(out.W(), boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005615 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005616 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005617 case HLoadString::LoadKind::kBssEntry: {
5618 // Add ADRP with its PC-relative String .bss entry patch.
5619 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005620 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005621 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005622 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005623 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005624 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005625 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005626 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005627 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005628 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005629 codegen_->GenerateGcRootFieldLoad(load,
5630 out_loc,
5631 temp,
5632 /* offset placeholder */ 0u,
5633 ldr_label,
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005634 gCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005635 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005636 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005637 codegen_->AddSlowPath(slow_path);
5638 __ Cbz(out.X(), slow_path->GetEntryLabel());
5639 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08005640 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005641 return;
5642 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005643 case HLoadString::LoadKind::kJitBootImageAddress: {
5644 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
5645 DCHECK_NE(address, 0u);
5646 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5647 return;
5648 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005649 case HLoadString::LoadKind::kJitTableAddress: {
5650 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005651 load->GetStringIndex(),
5652 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005653 codegen_->GenerateGcRootFieldLoad(load,
5654 out_loc,
5655 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005656 /* offset= */ 0,
5657 /* fixup_label= */ nullptr,
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005658 gCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005659 return;
5660 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005661 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005662 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005663 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005664
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005665 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005666 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005667 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005668 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005669 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5670 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005671 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005672}
5673
Alexandre Rames5319def2014-10-23 10:03:10 +01005674void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005675 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005676 locations->SetOut(Location::ConstantLocation(constant));
5677}
5678
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005679void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005680 // Will be generated at use site.
5681}
5682
Alexandre Rames67555f72014-11-18 10:55:16 +00005683void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005684 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5685 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005686 InvokeRuntimeCallingConvention calling_convention;
5687 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5688}
5689
5690void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005691 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005692 instruction,
5693 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005694 if (instruction->IsEnter()) {
5695 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5696 } else {
5697 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5698 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005699 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005700}
5701
Alexandre Rames42d641b2014-10-27 14:00:51 +00005702void LocationsBuilderARM64::VisitMul(HMul* mul) {
5703 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005704 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005705 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005706 case DataType::Type::kInt32:
5707 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005708 locations->SetInAt(0, Location::RequiresRegister());
5709 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005710 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005711 break;
5712
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005713 case DataType::Type::kFloat32:
5714 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005715 locations->SetInAt(0, Location::RequiresFpuRegister());
5716 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005717 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005718 break;
5719
5720 default:
5721 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5722 }
5723}
5724
5725void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5726 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005727 case DataType::Type::kInt32:
5728 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005729 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5730 break;
5731
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005732 case DataType::Type::kFloat32:
5733 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005734 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005735 break;
5736
5737 default:
5738 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5739 }
5740}
5741
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005742void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5743 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005744 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005745 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005746 case DataType::Type::kInt32:
5747 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005748 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005749 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005750 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005751
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005752 case DataType::Type::kFloat32:
5753 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005754 locations->SetInAt(0, Location::RequiresFpuRegister());
5755 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005756 break;
5757
5758 default:
5759 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5760 }
5761}
5762
5763void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5764 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005765 case DataType::Type::kInt32:
5766 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005767 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5768 break;
5769
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005770 case DataType::Type::kFloat32:
5771 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005772 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005773 break;
5774
5775 default:
5776 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5777 }
5778}
5779
5780void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005781 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5782 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005783 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005784 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005785 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5786 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005787}
5788
5789void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005790 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5791 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005792 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005793 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005794 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005795}
5796
Alexandre Rames5319def2014-10-23 10:03:10 +01005797void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005798 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5799 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005800 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005801 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005802 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005803}
5804
5805void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005806 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5807 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005808 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005809}
5810
5811void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005812 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005813 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005814 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005815}
5816
5817void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005818 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005819 case DataType::Type::kInt32:
5820 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005821 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005822 break;
5823
5824 default:
5825 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5826 }
5827}
5828
David Brazdil66d126e2015-04-03 16:02:44 +01005829void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005830 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005831 locations->SetInAt(0, Location::RequiresRegister());
5832 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5833}
5834
5835void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005836 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005837}
5838
Alexandre Rames5319def2014-10-23 10:03:10 +01005839void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005840 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5841 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005842}
5843
Calin Juravle2ae48182016-03-16 14:05:09 +00005844void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5845 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005846 return;
5847 }
Artem Serov914d7a82017-02-07 14:33:49 +00005848 {
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005849 // Ensure that between load and RecordPcInfo there are no pools emitted.
Artem Serov914d7a82017-02-07 14:33:49 +00005850 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5851 Location obj = instruction->GetLocations()->InAt(0);
5852 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5853 RecordPcInfo(instruction, instruction->GetDexPc());
5854 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005855}
5856
Calin Juravle2ae48182016-03-16 14:05:09 +00005857void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005858 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005859 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005860
5861 LocationSummary* locations = instruction->GetLocations();
5862 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005863
5864 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005865}
5866
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005867void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005868 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005869}
5870
Alexandre Rames67555f72014-11-18 10:55:16 +00005871void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5872 HandleBinaryOp(instruction);
5873}
5874
5875void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5876 HandleBinaryOp(instruction);
5877}
5878
Alexandre Rames3e69f162014-12-10 10:36:50 +00005879void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5880 LOG(FATAL) << "Unreachable";
5881}
5882
5883void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005884 if (instruction->GetNext()->IsSuspendCheck() &&
5885 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5886 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5887 // The back edge will generate the suspend check.
5888 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5889 }
5890
Alexandre Rames3e69f162014-12-10 10:36:50 +00005891 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5892}
5893
Alexandre Rames5319def2014-10-23 10:03:10 +01005894void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005895 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005896 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5897 if (location.IsStackSlot()) {
5898 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5899 } else if (location.IsDoubleStackSlot()) {
5900 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5901 }
5902 locations->SetOut(location);
5903}
5904
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005905void InstructionCodeGeneratorARM64::VisitParameterValue(
5906 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005907 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005908}
5909
5910void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5911 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005912 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005913 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005914}
5915
5916void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5917 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5918 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005919}
5920
5921void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005922 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005923 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005924 locations->SetInAt(i, Location::Any());
5925 }
5926 locations->SetOut(Location::Any());
5927}
5928
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005929void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005930 LOG(FATAL) << "Unreachable";
5931}
5932
Serban Constantinescu02164b32014-11-13 14:05:07 +00005933void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005934 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005935 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005936 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005937 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005938 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005939
5940 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005941 case DataType::Type::kInt32:
5942 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005943 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005944 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005945 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5946 break;
5947
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005948 case DataType::Type::kFloat32:
5949 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005950 InvokeRuntimeCallingConvention calling_convention;
5951 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5952 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5953 locations->SetOut(calling_convention.GetReturnLocation(type));
5954
5955 break;
5956 }
5957
Serban Constantinescu02164b32014-11-13 14:05:07 +00005958 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005959 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005960 }
5961}
5962
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005963void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005964 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005965 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5966 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5967
5968 Register out = OutputRegister(instruction);
5969 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005970
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01005971 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005972 // No need to adjust the result for non-negative dividends or the INT32_MIN/INT64_MIN dividends.
5973 // NOTE: The generated code for HRem correctly works for the INT32_MIN/INT64_MIN dividends.
5974 // INT*_MIN % imm must be 0 for any imm of power 2. 'and' works only with bits
5975 // 0..30 (Int32 case)/0..62 (Int64 case) of a dividend. For INT32_MIN/INT64_MIN they are zeros.
5976 // So 'and' always produces zero.
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005977 __ And(out, dividend, abs_imm - 1);
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01005978 } else {
5979 if (abs_imm == 2) {
5980 __ Cmp(dividend, 0);
5981 __ And(out, dividend, 1);
5982 __ Csneg(out, out, out, ge);
5983 } else {
5984 UseScratchRegisterScope temps(GetVIXLAssembler());
5985 Register temp = temps.AcquireSameSizeAs(out);
5986
5987 __ Negs(temp, dividend);
5988 __ And(out, dividend, abs_imm - 1);
5989 __ And(temp, temp, abs_imm - 1);
5990 __ Csneg(out, out, temp, mi);
5991 }
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005992 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005993}
5994
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005995void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005996 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005997
5998 if (imm == 0) {
5999 // Do not generate anything.
6000 // DivZeroCheck would prevent any code to be executed.
6001 return;
6002 }
6003
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01006004 if (IsPowerOfTwo(AbsOrMin(imm))) {
6005 // Cases imm == -1 or imm == 1 are handled in constant folding by
6006 // InstructionWithAbsorbingInputSimplifier.
6007 // If the cases have survided till code generation they are handled in
6008 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
6009 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01006010 GenerateIntRemForPower2Denom(instruction);
6011 } else {
6012 DCHECK(imm < -2 || imm > 2) << imm;
Evgeny Astigeevichc679fe32020-09-14 14:02:40 +01006013 GenerateDivRemWithAnyConstant(instruction, imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01006014 }
6015}
6016
6017void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
6018 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
6019 << instruction->GetResultType();
6020
6021 if (instruction->GetLocations()->InAt(1).IsConstant()) {
6022 GenerateIntRemForConstDenom(instruction);
6023 } else {
6024 Register out = OutputRegister(instruction);
6025 Register dividend = InputRegisterAt(instruction, 0);
6026 Register divisor = InputRegisterAt(instruction, 1);
6027 UseScratchRegisterScope temps(GetVIXLAssembler());
6028 Register temp = temps.AcquireSameSizeAs(out);
6029 __ Sdiv(temp, dividend, divisor);
6030 __ Msub(out, temp, divisor, dividend);
6031 }
6032}
6033
Serban Constantinescu02164b32014-11-13 14:05:07 +00006034void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006035 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00006036
Serban Constantinescu02164b32014-11-13 14:05:07 +00006037 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006038 case DataType::Type::kInt32:
6039 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01006040 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006041 break;
6042 }
6043
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006044 case DataType::Type::kFloat32:
6045 case DataType::Type::kFloat64: {
6046 QuickEntrypointEnum entrypoint =
6047 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00006048 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006049 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006050 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6051 } else {
6052 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6053 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00006054 break;
6055 }
6056
Serban Constantinescu02164b32014-11-13 14:05:07 +00006057 default:
6058 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00006059 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00006060 }
6061}
6062
Aart Bik1f8d51b2018-02-15 10:42:37 -08006063void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01006064 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006065}
6066
Aart Bik1f8d51b2018-02-15 10:42:37 -08006067void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01006068 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006069}
6070
6071void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01006072 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006073}
6074
6075void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01006076 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006077}
6078
Aart Bik3dad3412018-02-28 12:01:46 -08006079void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
6080 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
6081 switch (abs->GetResultType()) {
6082 case DataType::Type::kInt32:
6083 case DataType::Type::kInt64:
6084 locations->SetInAt(0, Location::RequiresRegister());
6085 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6086 break;
6087 case DataType::Type::kFloat32:
6088 case DataType::Type::kFloat64:
6089 locations->SetInAt(0, Location::RequiresFpuRegister());
6090 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6091 break;
6092 default:
6093 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
6094 }
6095}
6096
6097void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
6098 switch (abs->GetResultType()) {
6099 case DataType::Type::kInt32:
6100 case DataType::Type::kInt64: {
6101 Register in_reg = InputRegisterAt(abs, 0);
6102 Register out_reg = OutputRegister(abs);
6103 __ Cmp(in_reg, Operand(0));
6104 __ Cneg(out_reg, in_reg, lt);
6105 break;
6106 }
6107 case DataType::Type::kFloat32:
6108 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01006109 VRegister in_reg = InputFPRegisterAt(abs, 0);
6110 VRegister out_reg = OutputFPRegister(abs);
Aart Bik3dad3412018-02-28 12:01:46 -08006111 __ Fabs(out_reg, in_reg);
6112 break;
6113 }
6114 default:
6115 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
6116 }
6117}
6118
Igor Murashkind01745e2017-04-05 16:40:31 -07006119void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6120 constructor_fence->SetLocations(nullptr);
6121}
6122
6123void InstructionCodeGeneratorARM64::VisitConstructorFence(
6124 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6125 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6126}
6127
Calin Juravle27df7582015-04-17 19:12:31 +01006128void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6129 memory_barrier->SetLocations(nullptr);
6130}
6131
6132void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006133 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01006134}
6135
Alexandre Rames5319def2014-10-23 10:03:10 +01006136void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006137 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006138 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00006139 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01006140}
6141
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00006142void InstructionCodeGeneratorARM64::VisitReturn(HReturn* ret) {
6143 if (GetGraph()->IsCompilingOsr()) {
6144 // To simplify callers of an OSR method, we put the return value in both
6145 // floating point and core register.
6146 switch (ret->InputAt(0)->GetType()) {
6147 case DataType::Type::kFloat32:
6148 __ Fmov(w0, s0);
6149 break;
6150 case DataType::Type::kFloat64:
6151 __ Fmov(x0, d0);
6152 break;
6153 default:
6154 break;
6155 }
6156 }
Alexandre Rames5319def2014-10-23 10:03:10 +01006157 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01006158}
6159
6160void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
6161 instruction->SetLocations(nullptr);
6162}
6163
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006164void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01006165 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01006166}
6167
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006168void LocationsBuilderARM64::VisitRor(HRor* ror) {
6169 HandleBinaryOp(ror);
6170}
6171
6172void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
6173 HandleBinaryOp(ror);
6174}
6175
Serban Constantinescu02164b32014-11-13 14:05:07 +00006176void LocationsBuilderARM64::VisitShl(HShl* shl) {
6177 HandleShift(shl);
6178}
6179
6180void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
6181 HandleShift(shl);
6182}
6183
6184void LocationsBuilderARM64::VisitShr(HShr* shr) {
6185 HandleShift(shr);
6186}
6187
6188void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
6189 HandleShift(shr);
6190}
6191
Alexandre Rames5319def2014-10-23 10:03:10 +01006192void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006193 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006194}
6195
6196void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006197 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006198}
6199
Alexandre Rames67555f72014-11-18 10:55:16 +00006200void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006201 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00006202}
6203
6204void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01006205 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00006206}
6207
6208void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01006209 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01006210}
6211
Alexandre Rames67555f72014-11-18 10:55:16 +00006212void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006213 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01006214}
6215
Vladimir Marko552a1342017-10-31 10:56:47 +00006216void LocationsBuilderARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6217 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(x0));
6218}
6219
6220void InstructionCodeGeneratorARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6221 __ Mov(w0, instruction->GetFormat()->GetValue());
6222 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6223}
6224
Calin Juravlee460d1d2015-09-29 04:52:17 +01006225void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
6226 HUnresolvedInstanceFieldGet* instruction) {
6227 FieldAccessCallingConventionARM64 calling_convention;
6228 codegen_->CreateUnresolvedFieldLocationSummary(
6229 instruction, instruction->GetFieldType(), calling_convention);
6230}
6231
6232void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
6233 HUnresolvedInstanceFieldGet* instruction) {
6234 FieldAccessCallingConventionARM64 calling_convention;
6235 codegen_->GenerateUnresolvedFieldAccess(instruction,
6236 instruction->GetFieldType(),
6237 instruction->GetFieldIndex(),
6238 instruction->GetDexPc(),
6239 calling_convention);
6240}
6241
6242void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
6243 HUnresolvedInstanceFieldSet* instruction) {
6244 FieldAccessCallingConventionARM64 calling_convention;
6245 codegen_->CreateUnresolvedFieldLocationSummary(
6246 instruction, instruction->GetFieldType(), calling_convention);
6247}
6248
6249void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
6250 HUnresolvedInstanceFieldSet* instruction) {
6251 FieldAccessCallingConventionARM64 calling_convention;
6252 codegen_->GenerateUnresolvedFieldAccess(instruction,
6253 instruction->GetFieldType(),
6254 instruction->GetFieldIndex(),
6255 instruction->GetDexPc(),
6256 calling_convention);
6257}
6258
6259void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
6260 HUnresolvedStaticFieldGet* instruction) {
6261 FieldAccessCallingConventionARM64 calling_convention;
6262 codegen_->CreateUnresolvedFieldLocationSummary(
6263 instruction, instruction->GetFieldType(), calling_convention);
6264}
6265
6266void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
6267 HUnresolvedStaticFieldGet* instruction) {
6268 FieldAccessCallingConventionARM64 calling_convention;
6269 codegen_->GenerateUnresolvedFieldAccess(instruction,
6270 instruction->GetFieldType(),
6271 instruction->GetFieldIndex(),
6272 instruction->GetDexPc(),
6273 calling_convention);
6274}
6275
6276void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
6277 HUnresolvedStaticFieldSet* instruction) {
6278 FieldAccessCallingConventionARM64 calling_convention;
6279 codegen_->CreateUnresolvedFieldLocationSummary(
6280 instruction, instruction->GetFieldType(), calling_convention);
6281}
6282
6283void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
6284 HUnresolvedStaticFieldSet* instruction) {
6285 FieldAccessCallingConventionARM64 calling_convention;
6286 codegen_->GenerateUnresolvedFieldAccess(instruction,
6287 instruction->GetFieldType(),
6288 instruction->GetFieldIndex(),
6289 instruction->GetDexPc(),
6290 calling_convention);
6291}
6292
Alexandre Rames5319def2014-10-23 10:03:10 +01006293void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006294 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6295 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01006296 // In suspend check slow path, usually there are no caller-save registers at all.
6297 // If SIMD instructions are present, however, we force spilling all live SIMD
6298 // registers in full width (since the runtime only saves/restores lower part).
6299 locations->SetCustomSlowPathCallerSaves(
6300 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01006301}
6302
6303void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006304 HBasicBlock* block = instruction->GetBlock();
6305 if (block->GetLoopInformation() != nullptr) {
6306 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6307 // The back edge will generate the suspend check.
6308 return;
6309 }
6310 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6311 // The goto will generate the suspend check.
6312 return;
6313 }
6314 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08006315 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01006316}
6317
Alexandre Rames67555f72014-11-18 10:55:16 +00006318void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006319 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6320 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00006321 InvokeRuntimeCallingConvention calling_convention;
6322 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6323}
6324
6325void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00006326 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08006327 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00006328}
6329
6330void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
6331 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006332 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006333 DataType::Type input_type = conversion->GetInputType();
6334 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006335 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6336 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006337 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6338 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006339 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6340 }
6341
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006342 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006343 locations->SetInAt(0, Location::RequiresFpuRegister());
6344 } else {
6345 locations->SetInAt(0, Location::RequiresRegister());
6346 }
6347
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006348 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006349 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6350 } else {
6351 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6352 }
6353}
6354
6355void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006356 DataType::Type result_type = conversion->GetResultType();
6357 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00006358
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006359 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6360 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006361
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006362 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
6363 int result_size = DataType::Size(result_type);
6364 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00006365 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006366 Register output = OutputRegister(conversion);
6367 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006368 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01006369 // 'int' values are used directly as W registers, discarding the top
6370 // bits, so we don't need to sign-extend and can just perform a move.
6371 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
6372 // top 32 bits of the target register. We theoretically could leave those
6373 // bits unchanged, but we would have to make sure that no code uses a
6374 // 32bit input value as a 64bit value assuming that the top 32 bits are
6375 // zero.
6376 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006377 } else if (DataType::IsUnsignedType(result_type) ||
6378 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
6379 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006380 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00006381 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006382 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006383 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006384 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006385 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6386 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006387 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006388 } else if (DataType::IsFloatingPointType(result_type) &&
6389 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006390 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
6391 } else {
6392 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6393 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006394 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00006395}
Alexandre Rames67555f72014-11-18 10:55:16 +00006396
Serban Constantinescu02164b32014-11-13 14:05:07 +00006397void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
6398 HandleShift(ushr);
6399}
6400
6401void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
6402 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00006403}
6404
6405void LocationsBuilderARM64::VisitXor(HXor* instruction) {
6406 HandleBinaryOp(instruction);
6407}
6408
6409void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
6410 HandleBinaryOp(instruction);
6411}
6412
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006413void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006414 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006415 LOG(FATAL) << "Unreachable";
6416}
6417
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006418void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006419 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006420 LOG(FATAL) << "Unreachable";
6421}
6422
Mark Mendellfe57faa2015-09-18 09:26:15 -04006423// Simple implementation of packed switch - generate cascaded compare/jumps.
6424void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6425 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006426 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006427 locations->SetInAt(0, Location::RequiresRegister());
6428}
6429
6430void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6431 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08006432 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006433 Register value_reg = InputRegisterAt(switch_instr, 0);
6434 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6435
Zheng Xu3927c8b2015-11-18 17:46:25 +08006436 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01006437 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08006438 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
6439 // make sure we don't emit it if the target may run out of range.
6440 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
6441 // ranges and emit the tables only as required.
6442 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04006443
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006444 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08006445 // Current instruction id is an upper bound of the number of HIRs in the graph.
6446 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
6447 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006448 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6449 Register temp = temps.AcquireW();
6450 __ Subs(temp, value_reg, Operand(lower_bound));
6451
Zheng Xu3927c8b2015-11-18 17:46:25 +08006452 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006453 // Jump to successors[0] if value == lower_bound.
6454 __ B(eq, codegen_->GetLabelOf(successors[0]));
6455 int32_t last_index = 0;
6456 for (; num_entries - last_index > 2; last_index += 2) {
6457 __ Subs(temp, temp, Operand(2));
6458 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6459 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
6460 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6461 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
6462 }
6463 if (num_entries - last_index == 2) {
6464 // The last missing case_value.
6465 __ Cmp(temp, Operand(1));
6466 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08006467 }
6468
6469 // And the default for any other value.
6470 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6471 __ B(codegen_->GetLabelOf(default_block));
6472 }
6473 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01006474 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08006475
6476 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6477
6478 // Below instructions should use at most one blocked register. Since there are two blocked
6479 // registers, we are free to block one.
6480 Register temp_w = temps.AcquireW();
6481 Register index;
6482 // Remove the bias.
6483 if (lower_bound != 0) {
6484 index = temp_w;
6485 __ Sub(index, value_reg, Operand(lower_bound));
6486 } else {
6487 index = value_reg;
6488 }
6489
6490 // Jump to default block if index is out of the range.
6491 __ Cmp(index, Operand(num_entries));
6492 __ B(hs, codegen_->GetLabelOf(default_block));
6493
6494 // In current VIXL implementation, it won't require any blocked registers to encode the
6495 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
6496 // register pressure.
6497 Register table_base = temps.AcquireX();
6498 // Load jump offset from the table.
6499 __ Adr(table_base, jump_table->GetTableStartLabel());
6500 Register jump_offset = temp_w;
6501 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
6502
6503 // Jump to target block by branching to table_base(pc related) + offset.
6504 Register target_address = table_base;
6505 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
6506 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006507 }
6508}
6509
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006510void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
6511 HInstruction* instruction,
6512 Location out,
6513 uint32_t offset,
6514 Location maybe_temp,
6515 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006516 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006517 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006518 if (read_barrier_option == kWithReadBarrier) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006519 CHECK(gUseReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006520 if (kUseBakerReadBarrier) {
6521 // Load with fast path based Baker's read barrier.
6522 // /* HeapReference<Object> */ out = *(out + offset)
6523 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6524 out,
6525 out_reg,
6526 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006527 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006528 /* needs_null_check= */ false,
6529 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006530 } else {
6531 // Load with slow path based read barrier.
6532 // Save the value of `out` into `maybe_temp` before overwriting it
6533 // in the following move operation, as we will need it for the
6534 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006535 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00006536 __ Mov(temp_reg, out_reg);
6537 // /* HeapReference<Object> */ out = *(out + offset)
6538 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6539 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6540 }
6541 } else {
6542 // Plain load with no read barrier.
6543 // /* HeapReference<Object> */ out = *(out + offset)
6544 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6545 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6546 }
6547}
6548
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006549void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6550 HInstruction* instruction,
6551 Location out,
6552 Location obj,
6553 uint32_t offset,
6554 Location maybe_temp,
6555 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006556 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006557 Register out_reg = RegisterFrom(out, type);
6558 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006559 if (read_barrier_option == kWithReadBarrier) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006560 CHECK(gUseReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006561 if (kUseBakerReadBarrier) {
6562 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006563 // /* HeapReference<Object> */ out = *(obj + offset)
6564 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6565 out,
6566 obj_reg,
6567 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006568 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006569 /* needs_null_check= */ false,
6570 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006571 } else {
6572 // Load with slow path based read barrier.
6573 // /* HeapReference<Object> */ out = *(obj + offset)
6574 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6575 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6576 }
6577 } else {
6578 // Plain load with no read barrier.
6579 // /* HeapReference<Object> */ out = *(obj + offset)
6580 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6581 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6582 }
6583}
6584
Vladimir Markoca1e0382018-04-11 09:58:41 +00006585void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006586 HInstruction* instruction,
6587 Location root,
6588 Register obj,
6589 uint32_t offset,
6590 vixl::aarch64::Label* fixup_label,
6591 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006592 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006593 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006594 if (read_barrier_option == kWithReadBarrier) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006595 DCHECK(gUseReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006596 if (kUseBakerReadBarrier) {
6597 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006598 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00006599
Vladimir Marko008e09f32018-08-06 15:42:43 +01006600 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6601 // the Marking Register) to decide whether we need to enter
6602 // the slow path to mark the GC root.
6603 //
6604 // We use shared thunks for the slow path; shared within the method
6605 // for JIT, across methods for AOT. That thunk checks the reference
6606 // and jumps to the entrypoint if needed.
6607 //
6608 // lr = &return_address;
6609 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6610 // if (mr) { // Thread::Current()->GetIsGcMarking()
6611 // goto gc_root_thunk<root_reg>(lr)
6612 // }
6613 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00006614
Vladimir Marko008e09f32018-08-06 15:42:43 +01006615 UseScratchRegisterScope temps(GetVIXLAssembler());
6616 DCHECK(temps.IsAvailable(ip0));
6617 DCHECK(temps.IsAvailable(ip1));
6618 temps.Exclude(ip0, ip1);
6619 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillain44015862016-01-22 11:47:17 +00006620
Vladimir Marko008e09f32018-08-06 15:42:43 +01006621 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6622 vixl::aarch64::Label return_address;
6623 __ adr(lr, &return_address);
6624 if (fixup_label != nullptr) {
6625 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006626 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006627 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
Vladimir Marko94796f82018-08-08 15:15:33 +01006628 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Marko008e09f32018-08-06 15:42:43 +01006629 __ ldr(root_reg, MemOperand(obj.X(), offset));
6630 EmitBakerReadBarrierCbnz(custom_data);
6631 __ bind(&return_address);
Roland Levillain44015862016-01-22 11:47:17 +00006632 } else {
6633 // GC root loaded through a slow path for read barriers other
6634 // than Baker's.
6635 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006636 if (fixup_label == nullptr) {
6637 __ Add(root_reg.X(), obj.X(), offset);
6638 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006639 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006640 }
Roland Levillain44015862016-01-22 11:47:17 +00006641 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006642 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006643 }
6644 } else {
6645 // Plain GC root load with no read barrier.
6646 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006647 if (fixup_label == nullptr) {
6648 __ Ldr(root_reg, MemOperand(obj, offset));
6649 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006650 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006651 }
Roland Levillain44015862016-01-22 11:47:17 +00006652 // Note that GC roots are not affected by heap poisoning, thus we
6653 // do not have to unpoison `root_reg` here.
6654 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006655 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006656}
6657
Vladimir Markoc8178f52020-11-24 10:38:16 +00006658void CodeGeneratorARM64::GenerateIntrinsicCasMoveWithBakerReadBarrier(
6659 vixl::aarch64::Register marked_old_value,
Vladimir Marko94796f82018-08-08 15:15:33 +01006660 vixl::aarch64::Register old_value) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006661 DCHECK(gUseReadBarrier);
Vladimir Marko94796f82018-08-08 15:15:33 +01006662 DCHECK(kUseBakerReadBarrier);
6663
6664 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
Vladimir Markoc8178f52020-11-24 10:38:16 +00006665 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(marked_old_value.GetCode());
Vladimir Marko94796f82018-08-08 15:15:33 +01006666
6667 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6668 vixl::aarch64::Label return_address;
6669 __ adr(lr, &return_address);
6670 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6671 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Markoc8178f52020-11-24 10:38:16 +00006672 __ mov(marked_old_value, old_value);
Vladimir Marko94796f82018-08-08 15:15:33 +01006673 EmitBakerReadBarrierCbnz(custom_data);
6674 __ bind(&return_address);
6675}
6676
Roland Levillain44015862016-01-22 11:47:17 +00006677void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6678 Location ref,
Vladimir Marko248141f2018-08-10 10:40:07 +01006679 vixl::aarch64::Register obj,
6680 const vixl::aarch64::MemOperand& src,
Roland Levillain44015862016-01-22 11:47:17 +00006681 bool needs_null_check,
6682 bool use_load_acquire) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006683 DCHECK(gUseReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006684 DCHECK(kUseBakerReadBarrier);
6685
Vladimir Marko0ecac682018-08-07 10:40:38 +01006686 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6687 // Marking Register) to decide whether we need to enter the slow
6688 // path to mark the reference. Then, in the slow path, check the
6689 // gray bit in the lock word of the reference's holder (`obj`) to
6690 // decide whether to mark `ref` or not.
6691 //
6692 // We use shared thunks for the slow path; shared within the method
6693 // for JIT, across methods for AOT. That thunk checks the holder
6694 // and jumps to the entrypoint if needed. If the holder is not gray,
6695 // it creates a fake dependency and returns to the LDR instruction.
6696 //
6697 // lr = &gray_return_address;
6698 // if (mr) { // Thread::Current()->GetIsGcMarking()
6699 // goto field_thunk<holder_reg, base_reg, use_load_acquire>(lr)
6700 // }
6701 // not_gray_return_address:
6702 // // Original reference load. If the offset is too large to fit
6703 // // into LDR, we use an adjusted base register here.
6704 // HeapReference<mirror::Object> reference = *(obj+offset);
6705 // gray_return_address:
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006706
Vladimir Marko248141f2018-08-10 10:40:07 +01006707 DCHECK(src.GetAddrMode() == vixl::aarch64::Offset);
6708 DCHECK_ALIGNED(src.GetOffset(), sizeof(mirror::HeapReference<mirror::Object>));
6709
6710 UseScratchRegisterScope temps(GetVIXLAssembler());
6711 DCHECK(temps.IsAvailable(ip0));
6712 DCHECK(temps.IsAvailable(ip1));
6713 temps.Exclude(ip0, ip1);
6714 uint32_t custom_data = use_load_acquire
6715 ? EncodeBakerReadBarrierAcquireData(src.GetBaseRegister().GetCode(), obj.GetCode())
6716 : EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode());
6717
6718 {
6719 ExactAssemblyScope guard(GetVIXLAssembler(),
6720 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6721 vixl::aarch64::Label return_address;
6722 __ adr(lr, &return_address);
6723 EmitBakerReadBarrierCbnz(custom_data);
6724 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6725 "Field LDR must be 1 instruction (4B) before the return address label; "
6726 " 2 instructions (8B) for heap poisoning.");
6727 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
6728 if (use_load_acquire) {
6729 DCHECK_EQ(src.GetOffset(), 0);
6730 __ ldar(ref_reg, src);
6731 } else {
6732 __ ldr(ref_reg, src);
6733 }
6734 if (needs_null_check) {
6735 MaybeRecordImplicitNullCheck(instruction);
6736 }
6737 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6738 // macro instructions disallowed in ExactAssemblyScope.
6739 if (kPoisonHeapReferences) {
6740 __ neg(ref_reg, Operand(ref_reg));
6741 }
6742 __ bind(&return_address);
6743 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006744 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Vladimir Marko248141f2018-08-10 10:40:07 +01006745}
6746
6747void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6748 Location ref,
6749 Register obj,
6750 uint32_t offset,
6751 Location maybe_temp,
6752 bool needs_null_check,
6753 bool use_load_acquire) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006754 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6755 Register base = obj;
6756 if (use_load_acquire) {
6757 DCHECK(maybe_temp.IsRegister());
6758 base = WRegisterFrom(maybe_temp);
6759 __ Add(base, obj, offset);
6760 offset = 0u;
6761 } else if (offset >= kReferenceLoadMinFarOffset) {
6762 DCHECK(maybe_temp.IsRegister());
6763 base = WRegisterFrom(maybe_temp);
6764 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6765 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6766 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006767 }
Vladimir Marko248141f2018-08-10 10:40:07 +01006768 MemOperand src(base.X(), offset);
6769 GenerateFieldLoadWithBakerReadBarrier(
6770 instruction, ref, obj, src, needs_null_check, use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006771}
6772
Artem Serov0806f582018-10-11 20:14:20 +01006773void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HArrayGet* instruction,
6774 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006775 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006776 uint32_t data_offset,
6777 Location index,
Roland Levillain44015862016-01-22 11:47:17 +00006778 bool needs_null_check) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006779 DCHECK(gUseReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006780 DCHECK(kUseBakerReadBarrier);
6781
Vladimir Marko66d691d2017-04-07 17:53:39 +01006782 static_assert(
6783 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6784 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006785 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006786
Vladimir Marko008e09f32018-08-06 15:42:43 +01006787 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6788 // Marking Register) to decide whether we need to enter the slow
6789 // path to mark the reference. Then, in the slow path, check the
6790 // gray bit in the lock word of the reference's holder (`obj`) to
6791 // decide whether to mark `ref` or not.
6792 //
6793 // We use shared thunks for the slow path; shared within the method
6794 // for JIT, across methods for AOT. That thunk checks the holder
6795 // and jumps to the entrypoint if needed. If the holder is not gray,
6796 // it creates a fake dependency and returns to the LDR instruction.
6797 //
6798 // lr = &gray_return_address;
6799 // if (mr) { // Thread::Current()->GetIsGcMarking()
6800 // goto array_thunk<base_reg>(lr)
6801 // }
6802 // not_gray_return_address:
6803 // // Original reference load. If the offset is too large to fit
6804 // // into LDR, we use an adjusted base register here.
6805 // HeapReference<mirror::Object> reference = data[index];
6806 // gray_return_address:
Vladimir Marko66d691d2017-04-07 17:53:39 +01006807
Vladimir Marko008e09f32018-08-06 15:42:43 +01006808 DCHECK(index.IsValid());
6809 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6810 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006811
Vladimir Marko008e09f32018-08-06 15:42:43 +01006812 UseScratchRegisterScope temps(GetVIXLAssembler());
6813 DCHECK(temps.IsAvailable(ip0));
6814 DCHECK(temps.IsAvailable(ip1));
6815 temps.Exclude(ip0, ip1);
Artem Serov0806f582018-10-11 20:14:20 +01006816
6817 Register temp;
6818 if (instruction->GetArray()->IsIntermediateAddress()) {
6819 // We do not need to compute the intermediate address from the array: the
6820 // input instruction has done it already. See the comment in
6821 // `TryExtractArrayAccessAddress()`.
6822 if (kIsDebugBuild) {
6823 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
6824 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6825 }
6826 temp = obj;
6827 } else {
6828 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
6829 __ Add(temp.X(), obj.X(), Operand(data_offset));
6830 }
6831
Vladimir Marko008e09f32018-08-06 15:42:43 +01006832 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006833
Vladimir Marko008e09f32018-08-06 15:42:43 +01006834 {
6835 ExactAssemblyScope guard(GetVIXLAssembler(),
6836 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6837 vixl::aarch64::Label return_address;
6838 __ adr(lr, &return_address);
6839 EmitBakerReadBarrierCbnz(custom_data);
6840 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6841 "Array LDR must be 1 instruction (4B) before the return address label; "
6842 " 2 instructions (8B) for heap poisoning.");
6843 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6844 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6845 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6846 // macro instructions disallowed in ExactAssemblyScope.
6847 if (kPoisonHeapReferences) {
6848 __ neg(ref_reg, Operand(ref_reg));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006849 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006850 __ bind(&return_address);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006851 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006852 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Roland Levillain44015862016-01-22 11:47:17 +00006853}
6854
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006855void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6856 // The following condition is a compile-time one, so it does not have a run-time cost.
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006857 if (kIsDebugBuild && gUseReadBarrier && kUseBakerReadBarrier) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006858 // The following condition is a run-time one; it is executed after the
6859 // previous compile-time test, to avoid penalizing non-debug builds.
6860 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6861 UseScratchRegisterScope temps(GetVIXLAssembler());
6862 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6863 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6864 }
6865 }
6866}
6867
Vladimir Marko1bff99f2020-11-02 15:07:33 +00006868SlowPathCodeARM64* CodeGeneratorARM64::AddReadBarrierSlowPath(HInstruction* instruction,
6869 Location out,
6870 Location ref,
6871 Location obj,
6872 uint32_t offset,
6873 Location index) {
6874 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
6875 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6876 AddSlowPath(slow_path);
6877 return slow_path;
6878}
6879
Roland Levillain44015862016-01-22 11:47:17 +00006880void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6881 Location out,
6882 Location ref,
6883 Location obj,
6884 uint32_t offset,
6885 Location index) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006886 DCHECK(gUseReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006887
Roland Levillain44015862016-01-22 11:47:17 +00006888 // Insert a slow path based read barrier *after* the reference load.
6889 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006890 // If heap poisoning is enabled, the unpoisoning of the loaded
6891 // reference will be carried out by the runtime within the slow
6892 // path.
6893 //
6894 // Note that `ref` currently does not get unpoisoned (when heap
6895 // poisoning is enabled), which is alright as the `ref` argument is
6896 // not used by the artReadBarrierSlow entry point.
6897 //
6898 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko1bff99f2020-11-02 15:07:33 +00006899 SlowPathCodeARM64* slow_path = AddReadBarrierSlowPath(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006900
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006901 __ B(slow_path->GetEntryLabel());
6902 __ Bind(slow_path->GetExitLabel());
6903}
6904
Roland Levillain44015862016-01-22 11:47:17 +00006905void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6906 Location out,
6907 Location ref,
6908 Location obj,
6909 uint32_t offset,
6910 Location index) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006911 if (gUseReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006912 // Baker's read barriers shall be handled by the fast path
6913 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6914 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006915 // If heap poisoning is enabled, unpoisoning will be taken care of
6916 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006917 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006918 } else if (kPoisonHeapReferences) {
6919 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6920 }
6921}
6922
Roland Levillain44015862016-01-22 11:47:17 +00006923void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6924 Location out,
6925 Location root) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006926 DCHECK(gUseReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006927
Roland Levillain44015862016-01-22 11:47:17 +00006928 // Insert a slow path based read barrier *after* the GC root load.
6929 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006930 // Note that GC roots are not affected by heap poisoning, so we do
6931 // not need to do anything special for this here.
6932 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006933 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006934 AddSlowPath(slow_path);
6935
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006936 __ B(slow_path->GetEntryLabel());
6937 __ Bind(slow_path->GetExitLabel());
6938}
6939
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006940void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6941 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006942 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006943 locations->SetInAt(0, Location::RequiresRegister());
6944 locations->SetOut(Location::RequiresRegister());
6945}
6946
6947void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6948 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006949 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006950 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006951 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006952 __ Ldr(XRegisterFrom(locations->Out()),
6953 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006954 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006955 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006956 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006957 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6958 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006959 __ Ldr(XRegisterFrom(locations->Out()),
6960 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006961 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006962}
6963
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006964static void PatchJitRootUse(uint8_t* code,
6965 const uint8_t* roots_data,
6966 vixl::aarch64::Literal<uint32_t>* literal,
6967 uint64_t index_in_table) {
6968 uint32_t literal_offset = literal->GetOffset();
6969 uintptr_t address =
6970 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6971 uint8_t* data = code + literal_offset;
6972 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6973}
6974
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006975void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6976 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006977 const StringReference& string_reference = entry.first;
6978 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006979 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006980 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006981 }
6982 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006983 const TypeReference& type_reference = entry.first;
6984 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006985 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006986 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006987 }
6988}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006989
Artem Serov8ba4de12019-12-04 21:10:23 +00006990MemOperand InstructionCodeGeneratorARM64::VecNEONAddress(
Artem Serov1a719e42019-07-18 14:24:55 +01006991 HVecMemoryOperation* instruction,
6992 UseScratchRegisterScope* temps_scope,
6993 size_t size,
6994 bool is_string_char_at,
6995 /*out*/ Register* scratch) {
6996 LocationSummary* locations = instruction->GetLocations();
6997 Register base = InputRegisterAt(instruction, 0);
6998
6999 if (instruction->InputAt(1)->IsIntermediateAddressIndex()) {
7000 DCHECK(!is_string_char_at);
7001 return MemOperand(base.X(), InputRegisterAt(instruction, 1).X());
7002 }
7003
7004 Location index = locations->InAt(1);
7005 uint32_t offset = is_string_char_at
7006 ? mirror::String::ValueOffset().Uint32Value()
7007 : mirror::Array::DataOffset(size).Uint32Value();
7008 size_t shift = ComponentSizeShiftWidth(size);
7009
7010 // HIntermediateAddress optimization is only applied for scalar ArrayGet and ArraySet.
7011 DCHECK(!instruction->InputAt(0)->IsIntermediateAddress());
7012
7013 if (index.IsConstant()) {
7014 offset += Int64FromLocation(index) << shift;
7015 return HeapOperand(base, offset);
7016 } else {
7017 *scratch = temps_scope->AcquireSameSizeAs(base);
7018 __ Add(*scratch, base, Operand(WRegisterFrom(index), LSL, shift));
7019 return HeapOperand(*scratch, offset);
7020 }
7021}
7022
Artem Serov8ba4de12019-12-04 21:10:23 +00007023SVEMemOperand InstructionCodeGeneratorARM64::VecSVEAddress(
7024 HVecMemoryOperation* instruction,
7025 UseScratchRegisterScope* temps_scope,
7026 size_t size,
7027 bool is_string_char_at,
7028 /*out*/ Register* scratch) {
7029 LocationSummary* locations = instruction->GetLocations();
7030 Register base = InputRegisterAt(instruction, 0);
7031 Location index = locations->InAt(1);
7032
Artem Serov8ba4de12019-12-04 21:10:23 +00007033 DCHECK(!instruction->InputAt(1)->IsIntermediateAddressIndex());
Artem Serov8ba4de12019-12-04 21:10:23 +00007034 DCHECK(!index.IsConstant());
7035
7036 uint32_t offset = is_string_char_at
7037 ? mirror::String::ValueOffset().Uint32Value()
7038 : mirror::Array::DataOffset(size).Uint32Value();
7039 size_t shift = ComponentSizeShiftWidth(size);
7040
Raphael Gault0700b692020-09-30 08:33:10 +00007041 if (instruction->InputAt(0)->IsIntermediateAddress()) {
7042 return SVEMemOperand(base.X(), XRegisterFrom(index), LSL, shift);
7043 }
7044
Artem Serov8ba4de12019-12-04 21:10:23 +00007045 *scratch = temps_scope->AcquireSameSizeAs(base);
7046 __ Add(*scratch, base, offset);
7047 return SVEMemOperand(scratch->X(), XRegisterFrom(index), LSL, shift);
7048}
7049
Alexandre Rames67555f72014-11-18 10:55:16 +00007050#undef __
7051#undef QUICK_ENTRY_POINT
7052
Vladimir Markoca1e0382018-04-11 09:58:41 +00007053#define __ assembler.GetVIXLAssembler()->
7054
7055static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
7056 vixl::aarch64::Register base_reg,
7057 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01007058 vixl::aarch64::Label* slow_path,
7059 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00007060 // Load the lock word containing the rb_state.
7061 __ Ldr(ip0.W(), lock_word);
7062 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01007063 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00007064 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7065 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
7066 static_assert(
7067 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
7068 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01007069 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
7070 if (throw_npe != nullptr) {
7071 __ Bind(throw_npe);
7072 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00007073 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
7074 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
7075 "Field LDR must be 1 instruction (4B) before the return address label; "
7076 " 2 instructions (8B) for heap poisoning.");
7077 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
7078 // Introduce a dependency on the lock_word including rb_state,
7079 // to prevent load-load reordering, and without using
7080 // a memory barrier (which would be more expensive).
7081 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
7082 __ Br(lr); // And return back to the function.
7083 // Note: The fake dependency is unnecessary for the slow path.
7084}
7085
7086// Load the read barrier introspection entrypoint in register `entrypoint`.
7087static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
7088 vixl::aarch64::Register entrypoint) {
7089 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
7090 DCHECK_EQ(ip0.GetCode(), 16u);
7091 const int32_t entry_point_offset =
7092 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
7093 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
7094}
7095
7096void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
7097 uint32_t encoded_data,
7098 /*out*/ std::string* debug_name) {
7099 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
7100 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01007101 case BakerReadBarrierKind::kField:
7102 case BakerReadBarrierKind::kAcquire: {
Artem Serova07de552020-11-01 22:42:43 +00007103 Register base_reg =
7104 vixl::aarch64::XRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007105 CheckValidReg(base_reg.GetCode());
Artem Serova07de552020-11-01 22:42:43 +00007106 Register holder_reg =
7107 vixl::aarch64::XRegister(BakerReadBarrierSecondRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007108 CheckValidReg(holder_reg.GetCode());
7109 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
7110 temps.Exclude(ip0, ip1);
Roland Levillain988c3912019-09-25 19:33:35 +01007111 // In the case of a field load (with relaxed semantic), if `base_reg` differs from
7112 // `holder_reg`, the offset was too large and we must have emitted (during the construction
7113 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
7114 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
7115 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
7116 // not necessarily do that check before going to the thunk.
7117 //
7118 // In the case of a field load with load-acquire semantics (where `base_reg` always differs
7119 // from `holder_reg`), we also need an explicit null check when implicit null checks are
7120 // allowed, as we do not emit one before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01007121 vixl::aarch64::Label throw_npe_label;
7122 vixl::aarch64::Label* throw_npe = nullptr;
Roland Levillain988c3912019-09-25 19:33:35 +01007123 if (GetCompilerOptions().GetImplicitNullChecks() &&
7124 (holder_reg.Is(base_reg) || (kind == BakerReadBarrierKind::kAcquire))) {
Vladimir Marko7a695052018-04-12 10:26:50 +01007125 throw_npe = &throw_npe_label;
7126 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007127 }
Vladimir Marko7a695052018-04-12 10:26:50 +01007128 // Check if the holder is gray and, if not, add fake dependency to the base register
7129 // and return to the LDR instruction to load the reference. Otherwise, use introspection
7130 // to load the reference and call the entrypoint that performs further checks on the
7131 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00007132 vixl::aarch64::Label slow_path;
7133 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01007134 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007135 __ Bind(&slow_path);
Vladimir Marko0ecac682018-08-07 10:40:38 +01007136 if (kind == BakerReadBarrierKind::kField) {
7137 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
7138 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
7139 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7140 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
7141 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
7142 } else {
7143 DCHECK(kind == BakerReadBarrierKind::kAcquire);
7144 DCHECK(!base_reg.Is(holder_reg));
7145 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7146 __ Ldar(ip0.W(), MemOperand(base_reg));
7147 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00007148 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
7149 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00007150 break;
7151 }
7152 case BakerReadBarrierKind::kArray: {
Artem Serova07de552020-11-01 22:42:43 +00007153 Register base_reg =
7154 vixl::aarch64::XRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007155 CheckValidReg(base_reg.GetCode());
7156 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7157 BakerReadBarrierSecondRegField::Decode(encoded_data));
7158 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
7159 temps.Exclude(ip0, ip1);
7160 vixl::aarch64::Label slow_path;
7161 int32_t data_offset =
7162 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
7163 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
7164 DCHECK_LT(lock_word.GetOffset(), 0);
7165 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
7166 __ Bind(&slow_path);
7167 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
7168 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
7169 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7170 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
7171 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
7172 // a switch case target based on the index register.
7173 __ Mov(ip0, base_reg); // Move the base register to ip0.
7174 __ Br(ip1); // Jump to the entrypoint's array switch case.
7175 break;
7176 }
7177 case BakerReadBarrierKind::kGcRoot: {
7178 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
7179 // and it does not have a forwarding address), call the correct introspection entrypoint;
7180 // otherwise return the reference (or the extracted forwarding address).
7181 // There is no gray bit check for GC roots.
Artem Serova07de552020-11-01 22:42:43 +00007182 Register root_reg =
7183 vixl::aarch64::WRegister(BakerReadBarrierFirstRegField::Decode(encoded_data));
Vladimir Markoca1e0382018-04-11 09:58:41 +00007184 CheckValidReg(root_reg.GetCode());
7185 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7186 BakerReadBarrierSecondRegField::Decode(encoded_data));
7187 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
7188 temps.Exclude(ip0, ip1);
7189 vixl::aarch64::Label return_label, not_marked, forwarding_address;
7190 __ Cbz(root_reg, &return_label);
7191 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
7192 __ Ldr(ip0.W(), lock_word);
7193 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
7194 __ Bind(&return_label);
7195 __ Br(lr);
7196 __ Bind(&not_marked);
7197 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
7198 __ B(&forwarding_address, mi);
7199 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
7200 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
7201 // art_quick_read_barrier_mark_introspection_gc_roots.
7202 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
7203 __ Mov(ip0.W(), root_reg);
7204 __ Br(ip1);
7205 __ Bind(&forwarding_address);
7206 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
7207 __ Br(lr);
7208 break;
7209 }
7210 default:
7211 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
7212 UNREACHABLE();
7213 }
7214
Vladimir Marko966b46f2018-08-03 10:20:19 +00007215 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +01007216 // so JIT should pass null as `debug_name`.
Santiago Aboy Solanes872ec722022-02-18 14:10:25 +00007217 DCHECK_IMPLIES(GetCompilerOptions().IsJitCompiler(), debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00007218 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00007219 std::ostringstream oss;
7220 oss << "BakerReadBarrierThunk";
7221 switch (kind) {
7222 case BakerReadBarrierKind::kField:
7223 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7224 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7225 break;
Vladimir Marko0ecac682018-08-07 10:40:38 +01007226 case BakerReadBarrierKind::kAcquire:
7227 oss << "Acquire_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7228 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7229 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00007230 case BakerReadBarrierKind::kArray:
7231 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7232 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7233 BakerReadBarrierSecondRegField::Decode(encoded_data));
7234 break;
7235 case BakerReadBarrierKind::kGcRoot:
7236 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7237 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7238 BakerReadBarrierSecondRegField::Decode(encoded_data));
7239 break;
7240 }
7241 *debug_name = oss.str();
7242 }
7243}
7244
7245#undef __
7246
Alexandre Rames5319def2014-10-23 10:03:10 +01007247} // namespace arm64
7248} // namespace art