1//===-- AArch64Subtarget.cpp - AArch64 Subtarget Information ----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the AArch64 specific subclass of TargetSubtarget.
10//
11//===----------------------------------------------------------------------===//
12
13#include "AArch64Subtarget.h"
14
15#include "AArch64.h"
16#include "AArch64InstrInfo.h"
17#include "AArch64PBQPRegAlloc.h"
18#include "AArch64TargetMachine.h"
19#include "GISel/AArch64CallLowering.h"
20#include "GISel/AArch64LegalizerInfo.h"
21#include "GISel/AArch64RegisterBankInfo.h"
22#include "MCTargetDesc/AArch64AddressingModes.h"
23#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineScheduler.h"
26#include "llvm/IR/GlobalValue.h"
27#include "llvm/Option/LibraryOptions.h"
28#include "llvm/Support/SipHash.h"
29#include "llvm/TargetParser/AArch64TargetParser.h"
30
31using namespace llvm;
32
33#define DEBUG_TYPE "aarch64-subtarget"
34
35#define GET_SUBTARGETINFO_CTOR
36#define GET_SUBTARGETINFO_TARGET_DESC
37#include "AArch64GenSubtargetInfo.inc"
38
39#define OPTIONS_STRUCT_DEFS
40#include "AArch64Options.inc"
41
42// Reserve a list of X# registers, so they are unavailable for register
43// allocator, but can still be used as ABI requests, such as passing arguments
44// to function call.
45static cl::list<std::string>
46ReservedRegsForRA("reserve-regs-for-regalloc", cl::desc("Reserve physical "
47 "registers, so they can't be used by register allocator. "
48 "Should only be used for testing register allocator."),
49 cl::CommaSeparated, cl::Hidden);
50
51unsigned AArch64Subtarget::getVectorInsertExtractBaseCost() const {
52 return CLOpts.insert_extract_base_cost.value_or(u: VectorInsertExtractBaseCost);
53}
54
55AArch64Subtarget &AArch64Subtarget::initializeSubtargetDependencies(
56 StringRef FS, StringRef CPUString, StringRef TuneCPUString,
57 bool HasMinSize) {
58 // Determine default and user-specified characteristics
59
60 if (CPUString.empty())
61 CPUString = "generic";
62
63 if (TuneCPUString.empty())
64 TuneCPUString = CPUString;
65
66 ParseSubtargetFeatures(CPU: CPUString, TuneCPU: TuneCPUString, FS);
67 initializeProperties(HasMinSize);
68
69 return *this;
70}
71
72void AArch64Subtarget::initializeProperties(bool HasMinSize) {
73 // Initialize CPU specific properties. We should add a tablegen feature for
74 // this in the future so we can specify it together with the subtarget
75 // features.
76 switch (ARMProcFamily) {
77 case Generic:
78 // Using TuneCPU=generic we avoid ldapur instructions to line up with the
79 // cpus that use the AvoidLDAPUR feature. We don't want this to be on
80 // forever, so it is enabled between armv8.4 and armv8.7/armv9.2.
81 if (hasV8_4aOps() && !hasV8_8aOps())
82 AvoidLDAPUR = true;
83 break;
84 case Carmel:
85 break;
86 case CortexA35:
87 case CortexA53:
88 case CortexA55:
89 case CortexR82:
90 case CortexR82AE:
91 PrefFunctionAlignment = Align(16);
92 PrefLoopAlignment = Align(16);
93 MaxBytesForLoopAlignment = 8;
94 break;
95 case CortexA57:
96 PrefFunctionAlignment = Align(16);
97 PrefLoopAlignment = Align(16);
98 MaxBytesForLoopAlignment = 8;
99 break;
100 case CortexA65:
101 PrefFunctionAlignment = Align(8);
102 break;
103 case CortexA72:
104 case CortexA73:
105 case CortexA75:
106 PrefFunctionAlignment = Align(16);
107 PrefLoopAlignment = Align(16);
108 MaxBytesForLoopAlignment = 8;
109 break;
110 case CortexA76:
111 case CortexA77:
112 case CortexA78:
113 case CortexA78AE:
114 case CortexA78C:
115 case CortexX1:
116 PrefFunctionAlignment = Align(16);
117 PrefLoopAlignment = Align(32);
118 MaxBytesForLoopAlignment = 16;
119 break;
120 case CortexA320:
121 case CortexA510:
122 case CortexA520:
123 case C1Nano:
124 PrefFunctionAlignment = Align(16);
125 VScaleForTuning = 1;
126 PrefLoopAlignment = Align(16);
127 MaxBytesForLoopAlignment = 8;
128 break;
129 case CortexA710:
130 case CortexA715:
131 case CortexA720:
132 case CortexA725:
133 case C1Pro:
134 case CortexX2:
135 case CortexX3:
136 case CortexX4:
137 case CortexX925:
138 case C1Premium:
139 case C1Ultra:
140 case C2Ultra:
141 PrefFunctionAlignment = Align(16);
142 VScaleForTuning = 1;
143 PrefLoopAlignment = Align(32);
144 MaxBytesForLoopAlignment = 16;
145 break;
146 case A64FX:
147 CacheLineSize = 256;
148 PrefFunctionAlignment = Align(8);
149 PrefLoopAlignment = Align(4);
150 PrefetchDistance = 128;
151 MinPrefetchStride = 1024;
152 MaxPrefetchIterationsAhead = 4;
153 VScaleForTuning = 4;
154 break;
155 case MONAKA:
156 VScaleForTuning = 2;
157 break;
158 case AppleA7:
159 case AppleA10:
160 case AppleA11:
161 case AppleA12:
162 case AppleA13:
163 case AppleA14:
164 case AppleA15:
165 case AppleA16:
166 case AppleA17:
167 case AppleM4:
168 case AppleM5:
169 case AppleM6:
170 PrefetchDistance = 280;
171 MinPrefetchStride = 2048;
172 MaxPrefetchIterationsAhead = 3;
173 break;
174 case ExynosM3:
175 MaxJumpTableSize = 20;
176 PrefFunctionAlignment = Align(32);
177 PrefLoopAlignment = Align(16);
178 break;
179 case Falkor:
180 // FIXME: remove this to enable 64-bit SLP if performance looks good.
181 MinVectorRegisterBitWidth = 128;
182 CacheLineSize = 128;
183 PrefetchDistance = 820;
184 MinPrefetchStride = 2048;
185 MaxPrefetchIterationsAhead = 8;
186 break;
187 case Kryo:
188 VectorInsertExtractBaseCost = 2;
189 CacheLineSize = 128;
190 PrefetchDistance = 740;
191 MinPrefetchStride = 1024;
192 MaxPrefetchIterationsAhead = 11;
193 // FIXME: remove this to enable 64-bit SLP if performance looks good.
194 MinVectorRegisterBitWidth = 128;
195 break;
196 case NeoverseE1:
197 PrefFunctionAlignment = Align(8);
198 break;
199 case NeoverseN1:
200 PrefFunctionAlignment = Align(16);
201 PrefLoopAlignment = Align(32);
202 MaxBytesForLoopAlignment = 16;
203 break;
204 case NeoverseV2:
205 case NeoverseV3:
206 EpilogueVectorizationMinVF = 8;
207 ScatterOverhead = 13;
208 [[fallthrough]];
209 case NeoverseN2:
210 case NeoverseN3:
211 case NeoverseV3AE:
212 PrefFunctionAlignment = Align(32);
213 PrefLoopAlignment = Align(32);
214 MaxBytesForLoopAlignment = 16;
215 VScaleForTuning = 1;
216 break;
217 case NeoverseV1:
218 PrefFunctionAlignment = Align(16);
219 PrefLoopAlignment = Align(32);
220 MaxBytesForLoopAlignment = 16;
221 VScaleForTuning = 2;
222 DefaultSVETFOpts = TailFoldingOpts::Simple;
223 break;
224 case Neoverse512TVB:
225 PrefFunctionAlignment = Align(16);
226 VScaleForTuning = 1;
227 break;
228 case Saphira:
229 // FIXME: remove this to enable 64-bit SLP if performance looks good.
230 MinVectorRegisterBitWidth = 128;
231 break;
232 case ThunderX2T99:
233 PrefFunctionAlignment = Align(8);
234 PrefLoopAlignment = Align(4);
235 PrefetchDistance = 128;
236 MinPrefetchStride = 1024;
237 MaxPrefetchIterationsAhead = 4;
238 // FIXME: remove this to enable 64-bit SLP if performance looks good.
239 MinVectorRegisterBitWidth = 128;
240 break;
241 case ThunderX:
242 case ThunderXT88:
243 case ThunderXT81:
244 case ThunderXT83:
245 CacheLineSize = 128;
246 PrefFunctionAlignment = Align(8);
247 PrefLoopAlignment = Align(4);
248 // FIXME: remove this to enable 64-bit SLP if performance looks good.
249 MinVectorRegisterBitWidth = 128;
250 break;
251 case TSV110:
252 PrefFunctionAlignment = Align(16);
253 PrefLoopAlignment = Align(4);
254 break;
255 case HIP12:
256 PrefFunctionAlignment = Align(16);
257 PrefLoopAlignment = Align(4);
258 VScaleForTuning = 2;
259 DefaultSVETFOpts = TailFoldingOpts::Simple;
260 break;
261 case ThunderX3T110:
262 PrefFunctionAlignment = Align(16);
263 PrefLoopAlignment = Align(4);
264 PrefetchDistance = 128;
265 MinPrefetchStride = 1024;
266 MaxPrefetchIterationsAhead = 4;
267 // FIXME: remove this to enable 64-bit SLP if performance looks good.
268 MinVectorRegisterBitWidth = 128;
269 break;
270 case Ampere1:
271 case Ampere1A:
272 case Ampere1B:
273 case Ampere1C:
274 PrefFunctionAlignment = Align(64);
275 PrefLoopAlignment = Align(64);
276 break;
277 case Oryon:
278 PrefFunctionAlignment = Align(16);
279 PrefetchDistance = 128;
280 MinPrefetchStride = 1024;
281 break;
282 case Olympus:
283 EpilogueVectorizationMinVF = 8;
284 ScatterOverhead = 13;
285 PrefFunctionAlignment = Align(16);
286 PrefLoopAlignment = Align(32);
287 MaxBytesForLoopAlignment = 16;
288 VScaleForTuning = 1;
289 break;
290 }
291
292 if (CLOpts.min_jump_table_entries || !HasMinSize)
293 MinimumJumpTableEntries = CLOpts.min_jump_table_entries.value_or(u: 10);
294 if (CLOpts.sve_vscale_for_tuning)
295 VScaleForTuning = *CLOpts.sve_vscale_for_tuning;
296}
297
298AArch64Subtarget::AArch64Subtarget(const Triple &TT, StringRef CPU,
299 StringRef TuneCPU, StringRef FS,
300 const TargetMachine &TM, bool LittleEndian,
301 unsigned MinSVEVectorSizeInBitsOverride,
302 unsigned MaxSVEVectorSizeInBitsOverride,
303 bool IsStreaming, bool IsStreamingCompatible,
304 bool HasMinSize,
305 bool EnableSRLTSubregToRegMitigation)
306 : AArch64GenSubtargetInfo(TT, CPU, TuneCPU, FS),
307 CLOpts(static_cast<const AArch64TargetMachine &>(TM).getCLOpts()),
308 ReserveXRegister(AArch64::GPR64commonRegClass.getNumRegs()),
309 ReserveXRegisterForRA(AArch64::GPR64commonRegClass.getNumRegs()),
310 CustomCallSavedXRegs(AArch64::GPR64commonRegClass.getNumRegs()),
311 IsLittle(LittleEndian), IsStreaming(IsStreaming),
312 IsStreamingCompatible(IsStreamingCompatible),
313 MinSVEVectorSizeInBits(MinSVEVectorSizeInBitsOverride),
314 MaxSVEVectorSizeInBits(MaxSVEVectorSizeInBitsOverride),
315 EnableSRLTSubregToRegMitigation(EnableSRLTSubregToRegMitigation),
316 // To benefit from SME2's strided-register multi-vector load/store
317 // instructions we'll need to enable subreg liveness. Our longer
318 // term aim is to make this the default, regardless of streaming
319 // mode, but there are still some outstanding issues, see:
320 // https://github.com/llvm/llvm-project/pull/174188
321 // and:
322 // https://github.com/llvm/llvm-project/pull/168353
323 EnableSubregLiveness(IsStreaming ||
324 CLOpts.enable_subreg_liveness_tracking),
325 TargetTriple(TT),
326 InstrInfo(initializeSubtargetDependencies(FS, CPUString: CPU, TuneCPUString: TuneCPU, HasMinSize)),
327 TLInfo(TM, *this) {
328 if (AArch64::isX18ReservedByDefault(TT))
329 ReserveXRegister.set(18);
330 if (isLFI()) {
331 ReserveXRegister.set(I: 25, E: 29);
332 ReserveLRForRA = true;
333 }
334
335 CallLoweringInfo.reset(p: new AArch64CallLowering(*getTargetLowering()));
336 InlineAsmLoweringInfo.reset(p: new InlineAsmLowering(getTargetLowering()));
337 Legalizer.reset(p: new AArch64LegalizerInfo(*this));
338
339 auto *RBI = new AArch64RegisterBankInfo(*getRegisterInfo());
340
341 // FIXME: At this point, we can't rely on Subtarget having RBI.
342 // It's awkward to mix passing RBI and the Subtarget; should we pass
343 // TII/TRI as well?
344 InstSelector.reset(p: createAArch64InstructionSelector(
345 *static_cast<const AArch64TargetMachine *>(&TM), *this, *RBI));
346
347 RegBankInfo.reset(p: RBI);
348
349 auto TRI = getRegisterInfo();
350 StringSet<> ReservedRegNames(llvm::from_range, ReservedRegsForRA);
351 for (unsigned i = 0; i < 29; ++i) {
352 if (ReservedRegNames.count(Key: TRI->getName(RegNo: AArch64::X0 + i)))
353 ReserveXRegisterForRA.set(i);
354 }
355 // X30 is named LR, so we can't use TRI->getName to check X30.
356 if (ReservedRegNames.count(Key: "X30") || ReservedRegNames.count(Key: "LR"))
357 ReserveXRegisterForRA.set(30);
358 // X29 is named FP, so we can't use TRI->getName to check X29.
359 if (ReservedRegNames.count(Key: "X29") || ReservedRegNames.count(Key: "FP"))
360 ReserveXRegisterForRA.set(29);
361}
362
363const CallLowering *AArch64Subtarget::getCallLowering() const {
364 return CallLoweringInfo.get();
365}
366
367const InlineAsmLowering *AArch64Subtarget::getInlineAsmLowering() const {
368 return InlineAsmLoweringInfo.get();
369}
370
371InstructionSelector *AArch64Subtarget::getInstructionSelector() const {
372 return InstSelector.get();
373}
374
375const LegalizerInfo *AArch64Subtarget::getLegalizerInfo() const {
376 return Legalizer.get();
377}
378
379const RegisterBankInfo *AArch64Subtarget::getRegBankInfo() const {
380 return RegBankInfo.get();
381}
382
383/// Find the target operand flags that describe how a global value should be
384/// referenced for the current subtarget.
385unsigned
386AArch64Subtarget::ClassifyGlobalReference(const GlobalValue *GV,
387 const TargetMachine &TM) const {
388 // MachO large model always goes via a GOT, simply to get a single 8-byte
389 // absolute relocation on all global addresses.
390 if (TM.getCodeModel() == CodeModel::Large && isTargetMachO())
391 return AArch64II::MO_GOT;
392
393 // All globals dynamically protected by MTE must have their address tags
394 // synthesized. This is done by having the loader stash the tag in the GOT
395 // entry. Force all tagged globals (even ones with internal linkage) through
396 // the GOT.
397 if (GV->isTagged())
398 return AArch64II::MO_GOT;
399
400 if (!TM.shouldAssumeDSOLocal(GV)) {
401 if (GV->hasDLLImportStorageClass()) {
402 return AArch64II::MO_GOT | AArch64II::MO_DLLIMPORT;
403 }
404 if (getTargetTriple().isOSWindowsOrUEFI())
405 return AArch64II::MO_GOT | AArch64II::MO_COFFSTUB;
406 return AArch64II::MO_GOT;
407 }
408
409 // The small code model's direct accesses use ADRP, which cannot
410 // necessarily produce the value 0 (if the code is above 4GB).
411 // Same for the tiny code model, where we have a pc relative LDR.
412 if ((useSmallAddressing() || TM.getCodeModel() == CodeModel::Tiny) &&
413 GV->hasExternalWeakLinkage())
414 return AArch64II::MO_GOT;
415
416 // References to tagged globals are marked with MO_NC | MO_TAGGED to indicate
417 // that their nominal addresses are tagged and outside of the code model. In
418 // AArch64ExpandPseudo::expandMI we emit an additional instruction to set the
419 // tag if necessary based on MO_TAGGED.
420 if (AllowTaggedGlobals && !isa<FunctionType>(Val: GV->getValueType()))
421 return AArch64II::MO_NC | AArch64II::MO_TAGGED;
422
423 return AArch64II::MO_NO_FLAG;
424}
425
426unsigned AArch64Subtarget::classifyGlobalFunctionReference(
427 const GlobalValue *GV, const TargetMachine &TM) const {
428 // MachO large model always goes via a GOT, because we don't have the
429 // relocations available to do anything else..
430 if (TM.getCodeModel() == CodeModel::Large && isTargetMachO() &&
431 !GV->hasInternalLinkage())
432 return AArch64II::MO_GOT;
433
434 // NonLazyBind goes via GOT unless we know it's available locally.
435 auto *F = dyn_cast<Function>(Val: GV);
436 if ((!isTargetMachO() || CLOpts.macho_enable_nonlazybind) && F &&
437 F->hasFnAttribute(Kind: Attribute::NonLazyBind) && !TM.shouldAssumeDSOLocal(GV))
438 return AArch64II::MO_GOT;
439
440 if (getTargetTriple().isOSWindowsOrUEFI()) {
441 if (isWindowsArm64EC() && GV->getValueType()->isFunctionTy()) {
442 if (GV->hasDLLImportStorageClass()) {
443 // On Arm64EC, if we're calling a symbol from the import table
444 // directly, use MO_ARM64EC_CALLMANGLE.
445 return AArch64II::MO_GOT | AArch64II::MO_DLLIMPORT |
446 AArch64II::MO_ARM64EC_CALLMANGLE;
447 }
448 if (GV->hasExternalLinkage()) {
449 // If we're calling a symbol directly, use the mangled form in the
450 // call instruction.
451 return AArch64II::MO_ARM64EC_CALLMANGLE;
452 }
453 }
454
455 // Use ClassifyGlobalReference for setting MO_DLLIMPORT/MO_COFFSTUB.
456 return ClassifyGlobalReference(GV, TM);
457 }
458
459 return AArch64II::MO_NO_FLAG;
460}
461
462void AArch64Subtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
463 const SchedRegion &Region) const {
464 // LNT run (at least on Cyclone) showed reasonably significant gains for
465 // bi-directional scheduling. 253.perlbmk.
466 Policy.OnlyTopDown = false;
467 Policy.OnlyBottomUp = false;
468 // Enabling or Disabling the latency heuristic is a close call: It seems to
469 // help nearly no benchmark on out-of-order architectures, on the other hand
470 // it regresses register pressure on a few benchmarking.
471 Policy.DisableLatencyHeuristic = DisableLatencySchedHeuristic;
472}
473
474void AArch64Subtarget::adjustSchedDependency(
475 SUnit *Def, int DefOpIdx, SUnit *Use, int UseOpIdx, SDep &Dep,
476 const TargetSchedModel *SchedModel) const {
477 if (!SchedModel || Dep.getKind() != SDep::Kind::Data || !Dep.getReg() ||
478 !Def->isInstr() || !Use->isInstr() ||
479 (Def->getInstr()->getOpcode() != TargetOpcode::BUNDLE &&
480 Use->getInstr()->getOpcode() != TargetOpcode::BUNDLE))
481 return;
482
483 // If the Def is a BUNDLE, find the last instruction in the bundle that defs
484 // the register.
485 const MachineInstr *DefMI = Def->getInstr();
486 if (DefMI->getOpcode() == TargetOpcode::BUNDLE) {
487 Register Reg = DefMI->getOperand(i: DefOpIdx).getReg();
488 for (const auto &Op : const_mi_bundle_ops(MI: *DefMI)) {
489 if (Op.isReg() && Op.isDef() && Op.getReg() == Reg) {
490 DefMI = Op.getParent();
491 DefOpIdx = Op.getOperandNo();
492 }
493 }
494 }
495
496 // If the Use is a BUNDLE, find the first instruction that uses the Reg.
497 const MachineInstr *UseMI = Use->getInstr();
498 if (UseMI->getOpcode() == TargetOpcode::BUNDLE) {
499 Register Reg = UseMI->getOperand(i: UseOpIdx).getReg();
500 for (const auto &Op : const_mi_bundle_ops(MI: *UseMI)) {
501 if (Op.isReg() && Op.isUse() && Op.getReg() == Reg) {
502 UseMI = Op.getParent();
503 UseOpIdx = Op.getOperandNo();
504 break;
505 }
506 }
507 }
508
509 Dep.setLatency(
510 SchedModel->computeOperandLatency(DefMI, DefOperIdx: DefOpIdx, UseMI, UseOperIdx: UseOpIdx));
511}
512
513bool AArch64Subtarget::enableEarlyIfConversion() const {
514 return CLOpts.early_ifcvt;
515}
516
517bool AArch64Subtarget::supportsAddressTopByteIgnored() const {
518 if (!CLOpts.use_tbi)
519 return false;
520
521 if (TargetTriple.isDriverKit())
522 return true;
523 if (TargetTriple.isiOS()) {
524 return TargetTriple.getiOSVersion() >= VersionTuple(8);
525 }
526
527 return false;
528}
529
530std::unique_ptr<PBQPRAConstraint>
531AArch64Subtarget::getCustomPBQPConstraints() const {
532 return balanceFPOps() ? std::make_unique<A57ChainingConstraint>() : nullptr;
533}
534
535void AArch64Subtarget::mirFileLoaded(MachineFunction &MF) const {
536 // We usually compute max call frame size after ISel. Do the computation now
537 // if the .mir file didn't specify it. Note that this will probably give you
538 // bogus values after PEI has eliminated the callframe setup/destroy pseudo
539 // instructions, specify explicitly if you need it to be correct.
540 MachineFrameInfo &MFI = MF.getFrameInfo();
541 if (!MFI.isMaxCallFrameSizeComputed())
542 MFI.computeMaxCallFrameSize(MF);
543}
544
545bool AArch64Subtarget::useAA() const { return CLOpts.use_aa; }
546
547bool AArch64Subtarget::useScalarIncVL() const {
548 // If SVE2 or SME is present (we are not SVE-1 only) and
549 // -sve-use-scalar-inc-vl is not otherwise set, enable it by default.
550 return valueOr(X: CLOpts.sve_use_scalar_inc_vl, Default: hasSVE2() || hasSME());
551}
552
553// If return address signing is enabled, tail calls are emitted as follows:
554//
555// ```
556// <authenticate LR>
557// <check LR>
558// TCRETURN ; the callee may sign and spill the LR in its prologue
559// ```
560//
561// LR may require explicit checking because if FEAT_FPAC is not implemented
562// and LR was tampered with, then `<authenticate LR>` will not generate an
563// exception on its own. Later, if the callee spills the signed LR value and
564// neither FEAT_PAuth2 nor FEAT_EPAC are implemented, the valid PAC replaces
565// the higher bits of LR thus hiding the authentication failure.
566AArch64PAuth::AuthCheckMethod AArch64Subtarget::getAuthenticatedLRCheckMethod(
567 const MachineFunction &MF) const {
568 // TODO: Check subtarget for the scheme. Present variant is a default for
569 // pauthtest ABI.
570 if (MF.getFunction().hasFnAttribute(Kind: "ptrauth-returns") &&
571 MF.getFunction().hasFnAttribute(Kind: "ptrauth-auth-traps"))
572 return AArch64PAuth::AuthCheckMethod::HighBitsNoTBI;
573 // At now, use None by default because checks may introduce an unexpected
574 // performance regression or incompatibility with execute-only mappings.
575 return CLOpts.authenticated_lr_check_method.value_or(
576 u: AArch64PAuth::AuthCheckMethod::None);
577}
578
579std::optional<uint16_t>
580AArch64Subtarget::getPtrAuthBlockAddressDiscriminatorIfEnabled(
581 const Function &ParentFn) const {
582 if (!ParentFn.hasFnAttribute(Kind: "ptrauth-indirect-gotos"))
583 return std::nullopt;
584 // We currently have one simple mechanism for all targets.
585 // This isn't ABI, so we can always do better in the future.
586 return getPointerAuthStableSipHash(
587 S: (Twine(ParentFn.getName()) + " blockaddress").str());
588}
589
590bool AArch64Subtarget::isX16X17Safer() const {
591 // The Darwin kernel implements special protections for x16 and x17 so we
592 // should prefer to use those registers on that platform.
593 return isTargetDarwin();
594}
595
596bool AArch64Subtarget::enableMachinePipeliner() const {
597 return getSchedModel().hasInstrSchedModel();
598}
599
600/// Returns a MOVK's shifter operand, or 0 otherwise.
601static unsigned getMOVKShiftImm(const MachineInstr &MI) {
602 unsigned Opc = MI.getOpcode();
603 if (Opc != AArch64::MOVKWi && Opc != AArch64::MOVKXi)
604 return 0;
605 return MI.getOperand(i: 3).getImm();
606}
607
608/// \p HasFirst is false when the 1st instruction is a wildcard.
609static bool fusesMOVImmPairImpl(const AArch64Subtarget &ST, bool HasFirst,
610 unsigned FirstOpc, unsigned FirstShift,
611 unsigned SecondOpc, unsigned SecondShift) {
612 assert(ST.hasFuseLiterals() && "the subtarget doesn't fuse move immediate");
613
614 // 32 bit immediate.
615 if ((!HasFirst || FirstOpc == AArch64::MOVZWi) &&
616 SecondOpc == AArch64::MOVKWi && SecondShift == 16)
617 return true;
618
619 // Lower half of 64 bit immediate.
620 if ((!HasFirst || FirstOpc == AArch64::MOVZXi) &&
621 SecondOpc == AArch64::MOVKXi && SecondShift == 16)
622 return true;
623
624 // Upper half of 64 bit immediate.
625 if ((!HasFirst || (FirstOpc == AArch64::MOVKXi && FirstShift == 32)) &&
626 SecondOpc == AArch64::MOVKXi && SecondShift == 48)
627 return true;
628
629 return false;
630}
631
632bool AArch64Subtarget::fusesMOVImmPair(unsigned FirstOpc, unsigned FirstShift,
633 unsigned SecondOpc,
634 unsigned SecondShift) const {
635 return fusesMOVImmPairImpl(ST: *this, /*HasFirst=*/true, FirstOpc, FirstShift,
636 SecondOpc, SecondShift);
637}
638
639bool AArch64Subtarget::fusesMOVImmPair(const MachineInstr *FirstMI,
640 const MachineInstr &SecondMI) const {
641 return fusesMOVImmPairImpl(ST: *this, HasFirst: FirstMI != nullptr,
642 FirstOpc: FirstMI ? FirstMI->getOpcode() : 0,
643 FirstShift: FirstMI ? getMOVKShiftImm(MI: *FirstMI) : 0,
644 SecondOpc: SecondMI.getOpcode(), SecondShift: getMOVKShiftImm(MI: SecondMI));
645}
646