1//===--- AArch64Subtarget.h - Define Subtarget for the AArch64 -*- 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 declares the AArch64 specific subclass of TargetSubtarget.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TARGET_AARCH64_AARCH64SUBTARGET_H
14#define LLVM_LIB_TARGET_AARCH64_AARCH64SUBTARGET_H
15
16#include "AArch64FrameLowering.h"
17#include "AArch64ISelLowering.h"
18#include "AArch64InstrInfo.h"
19#include "AArch64PointerAuth.h"
20#include "AArch64RegisterInfo.h"
21#include "AArch64SelectionDAGInfo.h"
22#include "llvm/CodeGen/GlobalISel/CallLowering.h"
23#include "llvm/CodeGen/GlobalISel/InlineAsmLowering.h"
24#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
25#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
26#include "llvm/CodeGen/RegisterBankInfo.h"
27#include "llvm/CodeGen/TargetSubtargetInfo.h"
28#include "llvm/IR/DataLayout.h"
29#include "llvm/TargetParser/Triple.h"
30#include <optional>
31
32namespace llvm::AArch64 {
33// Mode for selecting how to insert frame record info into the stack ring
34// buffer.
35enum class StackTaggingRecordStackHistoryMode {
36 // Do not record frame record info.
37 None,
38
39 // Insert instructions into the prologue for storing into the stack ring
40 // buffer directly.
41 Instr,
42};
43
44enum class UncheckedLdStMode { Never, Safe, Always };
45} // namespace llvm::AArch64
46
47#define OPTIONS_STRUCT_DECL
48#include "AArch64Options.inc"
49
50#define GET_SUBTARGETINFO_HEADER
51#include "AArch64GenSubtargetInfo.inc"
52
53namespace llvm {
54class GlobalValue;
55class StringRef;
56
57class AArch64Subtarget final : public AArch64GenSubtargetInfo {
58 const AArch64Options &CLOpts;
59
60public:
61 enum ARMProcFamilyEnum : uint8_t {
62 Generic,
63#define ARM_PROCESSOR_FAMILY(ENUM) ENUM,
64#include "llvm/TargetParser/AArch64TargetParserDef.inc"
65#undef ARM_PROCESSOR_FAMILY
66 };
67
68protected:
69 /// ARMProcFamily - ARM processor family: Cortex-A53, Cortex-A57, and others.
70 ARMProcFamilyEnum ARMProcFamily = Generic;
71
72 // Enable 64-bit vectorization in SLP.
73 unsigned MinVectorRegisterBitWidth = 64;
74
75// Bool members corresponding to the SubtargetFeatures defined in tablegen
76#define GET_SUBTARGETINFO_MACRO(ATTRIBUTE, DEFAULT, GETTER) \
77 bool ATTRIBUTE = DEFAULT;
78#include "AArch64GenSubtargetInfo.inc"
79
80 unsigned EpilogueVectorizationMinVF = 16;
81 uint8_t MaxInterleaveFactor = 2;
82 uint8_t VectorInsertExtractBaseCost = 2;
83 uint16_t CacheLineSize = 64;
84 // Default scatter/gather overhead.
85 unsigned ScatterOverhead = 10;
86 unsigned GatherOverhead = 10;
87 uint16_t PrefetchDistance = 0;
88 uint16_t MinPrefetchStride = 1;
89 unsigned MaxPrefetchIterationsAhead = UINT_MAX;
90 Align PrefFunctionAlignment;
91 Align PrefLoopAlignment;
92 unsigned MaxBytesForLoopAlignment = 0;
93 unsigned MinimumJumpTableEntries = 4;
94 unsigned MaxJumpTableSize = 0;
95 unsigned FixedLoadLatency = 0;
96
97 // ReserveXRegister[i] - X#i is not available as a general purpose register.
98 BitVector ReserveXRegister;
99
100 // ReserveXRegisterForRA[i] - X#i is not available for register allocator.
101 BitVector ReserveXRegisterForRA;
102
103 // CustomCallUsedXRegister[i] - X#i call saved.
104 BitVector CustomCallSavedXRegs;
105
106 bool IsLittle;
107
108 bool IsStreaming;
109 bool IsStreamingCompatible;
110 unsigned MinSVEVectorSizeInBits;
111 unsigned MaxSVEVectorSizeInBits;
112 bool EnableSRLTSubregToRegMitigation;
113 unsigned VScaleForTuning = 1;
114 TailFoldingOpts DefaultSVETFOpts = TailFoldingOpts::Disabled;
115
116 bool EnableSubregLiveness;
117
118 /// TargetTriple - What processor and OS we're targeting.
119 Triple TargetTriple;
120
121 AArch64FrameLowering FrameLowering;
122 AArch64InstrInfo InstrInfo;
123 AArch64SelectionDAGInfo TSInfo;
124 AArch64TargetLowering TLInfo;
125
126 /// GlobalISel related APIs.
127 std::unique_ptr<CallLowering> CallLoweringInfo;
128 std::unique_ptr<InlineAsmLowering> InlineAsmLoweringInfo;
129 std::unique_ptr<InstructionSelector> InstSelector;
130 std::unique_ptr<LegalizerInfo> Legalizer;
131 std::unique_ptr<RegisterBankInfo> RegBankInfo;
132
133private:
134 /// initializeSubtargetDependencies - Initializes using CPUString and the
135 /// passed in feature string so that we can use initializer lists for
136 /// subtarget initialization.
137 AArch64Subtarget &initializeSubtargetDependencies(StringRef FS,
138 StringRef CPUString,
139 StringRef TuneCPUString,
140 bool HasMinSize);
141
142 /// Initialize properties based on the selected processor family.
143 void initializeProperties(bool HasMinSize);
144
145public:
146 /// This constructor initializes the data members to match that
147 /// of the specified triple.
148 AArch64Subtarget(const Triple &TT, StringRef CPU, StringRef TuneCPU,
149 StringRef FS, const TargetMachine &TM, bool LittleEndian,
150 unsigned MinSVEVectorSizeInBitsOverride = 0,
151 unsigned MaxSVEVectorSizeInBitsOverride = 0,
152 bool IsStreaming = false, bool IsStreamingCompatible = false,
153 bool HasMinSize = false,
154 bool EnableSRLTSubregToRegMitigation = false);
155
156 const AArch64Options &getCLOpts() const { return CLOpts; }
157
158// Getters for SubtargetFeatures defined in tablegen
159#define GET_SUBTARGETINFO_MACRO(ATTRIBUTE, DEFAULT, GETTER) \
160 bool GETTER() const { return ATTRIBUTE; }
161#include "AArch64GenSubtargetInfo.inc"
162
163 const AArch64SelectionDAGInfo *getSelectionDAGInfo() const override {
164 return &TSInfo;
165 }
166 const AArch64FrameLowering *getFrameLowering() const override {
167 return &FrameLowering;
168 }
169 const AArch64TargetLowering *getTargetLowering() const override {
170 return &TLInfo;
171 }
172 const AArch64InstrInfo *getInstrInfo() const override { return &InstrInfo; }
173 const AArch64RegisterInfo *getRegisterInfo() const override {
174 return &getInstrInfo()->getRegisterInfo();
175 }
176 const CallLowering *getCallLowering() const override;
177 const InlineAsmLowering *getInlineAsmLowering() const override;
178 InstructionSelector *getInstructionSelector() const override;
179 const LegalizerInfo *getLegalizerInfo() const override;
180 const RegisterBankInfo *getRegBankInfo() const override;
181 const Triple &getTargetTriple() const { return TargetTriple; }
182 bool enableMachineScheduler() const override { return true; }
183 bool enablePostRAScheduler() const override { return usePostRAScheduler(); }
184 bool enableSubRegLiveness() const override { return EnableSubregLiveness; }
185
186 bool enableMachinePipeliner() const override;
187 bool useDFAforSMS() const override { return false; }
188
189 /// Returns ARM processor family.
190 /// Avoid this function! CPU specifics should be kept local to this class
191 /// and preferably modeled with SubtargetFeatures or properties in
192 /// initializeProperties().
193 ARMProcFamilyEnum getProcFamily() const {
194 return ARMProcFamily;
195 }
196
197 /// Returns true if the processor is an Apple M-series or aligned A-series
198 /// (A14 or newer).
199 bool isAppleMLike() const {
200 switch (ARMProcFamily) {
201 case AppleA14:
202 case AppleA15:
203 case AppleA16:
204 case AppleA17:
205 case AppleM4:
206 case AppleM5:
207 case AppleM6:
208 return true;
209 default:
210 return false;
211 }
212 }
213
214 bool isXRaySupported() const override { return true; }
215
216 /// Returns true if the function has a streaming body.
217 bool isStreaming() const { return IsStreaming; }
218
219 /// Returns true if the function has a streaming-compatible body.
220 bool isStreamingCompatible() const { return IsStreamingCompatible; }
221
222 /// Returns the size of memory region that if accessed by both the CPU and
223 /// the SME unit could result in a hazard. 0 = disabled.
224 unsigned getStreamingHazardSize() const {
225 return CLOpts.streaming_hazard_size.value_or(
226 u: !hasSMEFA64() && hasSME() && hasSVE() ? 1024 : 0);
227 }
228
229 /// Returns true if the target has NEON and the function at runtime is known
230 /// to have NEON enabled (e.g. the function is known not to be in streaming-SVE
231 /// mode, which disables NEON instructions).
232 bool isNeonAvailable() const {
233 return hasNEON() &&
234 (hasSMEFA64() || (!isStreaming() && !isStreamingCompatible()));
235 }
236
237 /// Returns true if the target has SVE and can use the full range of SVE
238 /// instructions, for example because it knows the function is known not to be
239 /// in streaming-SVE mode or when the target has FEAT_FA64 enabled.
240 bool isSVEAvailable() const {
241 return hasSVE() &&
242 (hasSMEFA64() || (!isStreaming() && !isStreamingCompatible()));
243 }
244
245 /// Returns true if the target has access to the streaming-compatible subset
246 /// of SVE instructions.
247 bool isStreamingSVEAvailable() const { return hasSME() && isStreaming(); }
248
249 /// Returns true if the target has access to either the full range of SVE
250 /// instructions, or the streaming-compatible subset of SVE instructions.
251 bool isSVEorStreamingSVEAvailable() const {
252 return hasSVE() || isStreamingSVEAvailable();
253 }
254
255 /// Returns true if the target has access to either the full range of SVE
256 /// instructions, or the streaming-compatible subset of SVE instructions
257 /// available to SME2.
258 bool isNonStreamingSVEorSME2Available() const {
259 return isSVEAvailable() || (isSVEorStreamingSVEAvailable() && hasSME2());
260 }
261
262 /// Returns true if SVE bit-permute instructions (bext/bdep/bgrp) can be
263 /// emitted.
264 bool isSVEBitPermAvailable() const {
265 return hasSVEBitPerm() &&
266 (isSVEAvailable() ||
267 (isSVEorStreamingSVEAvailable() && hasSSVE_BitPerm()));
268 }
269
270 unsigned getMinVectorRegisterBitWidth() const {
271 // Don't assume any minimum vector size when PSTATE.SM may not be 0, because
272 // we don't yet support streaming-compatible codegen support that we trust
273 // is safe for functions that may be executed in streaming-SVE mode.
274 // By returning '0' here, we disable vectorization.
275 if (!isSVEAvailable() && !isNeonAvailable())
276 return 0;
277 return MinVectorRegisterBitWidth;
278 }
279
280 bool isXRegisterReserved(size_t i) const { return ReserveXRegister[i]; }
281 bool isXRegisterReservedForRA(size_t i) const { return ReserveXRegisterForRA[i]; }
282 unsigned getNumXRegisterReserved() const {
283 BitVector AllReservedX(AArch64::GPR64commonRegClass.getNumRegs());
284 AllReservedX |= ReserveXRegister;
285 AllReservedX |= ReserveXRegisterForRA;
286 return AllReservedX.count();
287 }
288 bool isLRReservedForRA() const { return ReserveLRForRA; }
289 bool isXRegCustomCalleeSaved(size_t i) const {
290 return CustomCallSavedXRegs[i];
291 }
292 bool hasCustomCallingConv() const { return CustomCallSavedXRegs.any(); }
293
294 /// Return true if the CPU supports any kind of instruction fusion.
295 bool hasFusion() const {
296 return hasArithmeticBccFusion() || hasArithmeticCbzFusion() ||
297 hasFuseAES() || hasFuseArithmeticLogic() || hasFuseCmpCSel() ||
298 hasFuseFCmpFCSel() || hasFuseCmpCSet() || hasFuseAdrpAdd() ||
299 hasFuseLiterals() || hasFuseAppleSMECompute() || hasFuseFMinFMax();
300 }
301
302 /// Return true if the subtarget fuses this pair of move immediate
303 /// instructions.
304 bool fusesMOVImmPair(unsigned FirstOpc, unsigned FirstShift,
305 unsigned SecondOpc, unsigned SecondShift) const;
306
307 /// Return true if the subtarget fuses this pair of move immediate
308 /// instructions. The 1st instruction is a wildcard when it is nullptr, which
309 /// tells whether the 2nd one can be fused at all.
310 bool fusesMOVImmPair(const MachineInstr *FirstMI,
311 const MachineInstr &SecondMI) const;
312
313 unsigned getEpilogueVectorizationMinVF() const {
314 return EpilogueVectorizationMinVF;
315 }
316 unsigned getMaxInterleaveFactor() const { return MaxInterleaveFactor; }
317 unsigned getVectorInsertExtractBaseCost() const;
318 unsigned getCacheLineSize() const override { return CacheLineSize; }
319 unsigned getScatterOverhead() const { return ScatterOverhead; }
320 unsigned getGatherOverhead() const { return GatherOverhead; }
321 unsigned getPrefetchDistance() const override { return PrefetchDistance; }
322 unsigned getMinPrefetchStride(unsigned NumMemAccesses,
323 unsigned NumStridedMemAccesses,
324 unsigned NumPrefetches,
325 bool HasCall) const override {
326 return MinPrefetchStride;
327 }
328 unsigned getMaxPrefetchIterationsAhead() const override {
329 return MaxPrefetchIterationsAhead;
330 }
331 Align getPrefFunctionAlignment() const {
332 return PrefFunctionAlignment;
333 }
334 Align getPrefLoopAlignment() const { return PrefLoopAlignment; }
335
336 unsigned getMaxBytesForLoopAlignment() const {
337 return MaxBytesForLoopAlignment;
338 }
339
340 unsigned getMaximumJumpTableSize() const { return MaxJumpTableSize; }
341 unsigned getMinimumJumpTableEntries() const {
342 return MinimumJumpTableEntries;
343 }
344
345 unsigned getFixedLoadLatency() const { return FixedLoadLatency; }
346
347 /// CPU has TBI (top byte of addresses is ignored during HW address
348 /// translation) and OS enables it.
349 bool supportsAddressTopByteIgnored() const;
350
351 bool isLittleEndian() const { return IsLittle; }
352
353 bool isTargetDarwin() const { return TargetTriple.isOSDarwin(); }
354 bool isTargetIOS() const { return TargetTriple.isiOS(); }
355 bool isTargetLinux() const { return TargetTriple.isOSLinux(); }
356 bool isTargetWindows() const { return TargetTriple.isOSWindows(); }
357 bool isTargetUEFI() const { return TargetTriple.isUEFI(); }
358 /// UEFI follows the Windows ARM64 ABI and produces PE/COFF images, so most
359 /// of the Windows specific ABI handling (calling convention, callee saved
360 /// registers, frame chain, stack probing and unwind info) applies to it.
361 bool isTargetWindowsOrUEFI() const {
362 return TargetTriple.isOSWindowsOrUEFI();
363 }
364 bool isTargetAndroid() const { return TargetTriple.isAndroid(); }
365 bool isTargetFuchsia() const { return TargetTriple.isOSFuchsia(); }
366 bool isWindowsArm64EC() const { return TargetTriple.isWindowsArm64EC(); }
367 bool isLFI() const { return TargetTriple.isLFI(); }
368
369 bool isTargetCOFF() const { return TargetTriple.isOSBinFormatCOFF(); }
370 bool isTargetELF() const { return TargetTriple.isOSBinFormatELF(); }
371 bool isTargetMachO() const { return TargetTriple.isOSBinFormatMachO(); }
372
373 bool isTargetILP32() const {
374 return TargetTriple.isArch32Bit() ||
375 TargetTriple.getEnvironment() == Triple::GNUILP32;
376 }
377
378 bool useAA() const override;
379
380 bool useSmallAddressing() const {
381 switch (TLInfo.getTargetMachine().getCodeModel()) {
382 case CodeModel::Kernel:
383 // Kernel is currently allowed only for Fuchsia targets,
384 // where it is the same as Small for almost all purposes.
385 case CodeModel::Small:
386 return true;
387 default:
388 return false;
389 }
390 }
391
392 /// Returns whether the operating system makes it safer to store sensitive
393 /// values in x16 and x17 as opposed to other registers.
394 bool isX16X17Safer() const;
395
396 /// ParseSubtargetFeatures - Parses features string setting specified
397 /// subtarget options. Definition of function is auto generated by tblgen.
398 void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS);
399
400 /// ClassifyGlobalReference - Find the target operand flags that describe
401 /// how a global value should be referenced for the current subtarget.
402 unsigned ClassifyGlobalReference(const GlobalValue *GV,
403 const TargetMachine &TM) const;
404
405 unsigned classifyGlobalFunctionReference(const GlobalValue *GV,
406 const TargetMachine &TM) const;
407
408 /// This function is design to compatible with the function def in other
409 /// targets and escape build error about the virtual function def in base
410 /// class TargetSubtargetInfo. Updeate me if AArch64 target need to use it.
411 unsigned char
412 classifyGlobalFunctionReference(const GlobalValue *GV) const override {
413 return 0;
414 }
415
416 void overrideSchedPolicy(MachineSchedPolicy &Policy,
417 const SchedRegion &Region) const override;
418
419 void adjustSchedDependency(SUnit *Def, int DefOpIdx, SUnit *Use, int UseOpIdx,
420 SDep &Dep,
421 const TargetSchedModel *SchedModel) const override;
422
423 bool enableEarlyIfConversion() const override;
424
425 std::unique_ptr<PBQPRAConstraint> getCustomPBQPConstraints() const override;
426
427 bool isCallingConvWin64(CallingConv::ID CC, bool IsVarArg) const {
428 switch (CC) {
429 case CallingConv::C:
430 case CallingConv::Fast:
431 case CallingConv::Swift:
432 case CallingConv::SwiftTail:
433 return isTargetWindowsOrUEFI();
434 case CallingConv::PreserveNone:
435 return IsVarArg && isTargetWindowsOrUEFI();
436 case CallingConv::Win64:
437 return true;
438 default:
439 return false;
440 }
441 }
442
443 /// Return whether FrameLowering should always set the "extended frame
444 /// present" bit in FP, or set it based on a symbol in the runtime.
445 bool swiftAsyncContextIsDynamicallySet() const {
446 // Older OS versions (particularly system unwinders) are confused by the
447 // Swift extended frame, so when building code that might be run on them we
448 // must dynamically query the concurrency library to determine whether
449 // extended frames should be flagged as present.
450 const Triple &TT = getTargetTriple();
451
452 unsigned Major = TT.getOSVersion().getMajor();
453 switch(TT.getOS()) {
454 default:
455 return false;
456 case Triple::IOS:
457 case Triple::TvOS:
458 return Major < 15;
459 case Triple::WatchOS:
460 return Major < 8;
461 case Triple::MacOSX:
462 case Triple::Darwin:
463 return Major < 12;
464 }
465 }
466
467 void mirFileLoaded(MachineFunction &MF) const override;
468
469 // Return the known range for the bit length of SVE data registers. A value
470 // of 0 means nothing is known about that particular limit beyond what's
471 // implied by the architecture.
472 unsigned getMaxSVEVectorSizeInBits() const {
473 assert(isSVEorStreamingSVEAvailable() &&
474 "Tried to get SVE vector length without SVE support!");
475 return MaxSVEVectorSizeInBits;
476 }
477
478 unsigned getMinSVEVectorSizeInBits() const {
479 assert(isSVEorStreamingSVEAvailable() &&
480 "Tried to get SVE vector length without SVE support!");
481 return MinSVEVectorSizeInBits;
482 }
483
484 // Return the known bit length of SVE data registers. A value of 0 means the
485 // length is unknown beyond what's implied by the architecture.
486 unsigned getSVEVectorSizeInBits() const {
487 assert(isSVEorStreamingSVEAvailable() &&
488 "Tried to get SVE vector length without SVE support!");
489 if (MinSVEVectorSizeInBits == MaxSVEVectorSizeInBits)
490 return MaxSVEVectorSizeInBits;
491 return 0;
492 }
493
494 // Return the known bit length of SVE predicate registers. A value of 0 means
495 // the length is unknown beyond what's implied by the architecture.
496 unsigned getSVEPredicateSizeInBits() const {
497 return getSVEVectorSizeInBits() / 8;
498 }
499
500 bool useSVEForFixedLengthVectors() const {
501 if (!isSVEorStreamingSVEAvailable())
502 return false;
503
504 // Prefer NEON unless larger SVE registers are available.
505 return !isNeonAvailable() || getMinSVEVectorSizeInBits() >= 256;
506 }
507
508 bool useSVEForFixedLengthVectors(EVT VT) const {
509 if (!useSVEForFixedLengthVectors() || !VT.isFixedLengthVector())
510 return false;
511 return VT.getFixedSizeInBits() > AArch64::SVEBitsPerBlock ||
512 !isNeonAvailable();
513 }
514
515 unsigned getVScaleForTuning() const { return VScaleForTuning; }
516
517 TailFoldingOpts getSVETailFoldingDefaultOpts() const {
518 return DefaultSVETFOpts;
519 }
520
521 /// Returns true to use the addvl/inc/dec instructions, as opposed to separate
522 /// add + cnt instructions.
523 bool useScalarIncVL() const;
524
525 bool enableSRLTSubregToRegMitigation() const {
526 return EnableSRLTSubregToRegMitigation;
527 }
528
529 /// Choose a method of checking LR before performing a tail call.
530 AArch64PAuth::AuthCheckMethod
531 getAuthenticatedLRCheckMethod(const MachineFunction &MF) const;
532
533 /// Compute the integer discriminator for a given BlockAddress constant, if
534 /// blockaddress signing is enabled, or std::nullopt otherwise.
535 /// Blockaddress signing is controlled by the function attribute
536 /// "ptrauth-indirect-gotos" on the parent function.
537 /// Note that this assumes the discriminator is independent of the indirect
538 /// goto branch site itself, i.e., it's the same for all BlockAddresses in
539 /// a function.
540 std::optional<uint16_t>
541 getPtrAuthBlockAddressDiscriminatorIfEnabled(const Function &ParentFn) const;
542
543 bool enableAggressiveInterleaving() const { return AggressiveInterleaving; }
544};
545} // End llvm namespace
546
547#endif
548