xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AArch64/AArch64Subtarget.cpp (revision 8bcb0991864975618c09697b1aca10683346d9f0)
10b57cec5SDimitry Andric //===-- AArch64Subtarget.cpp - AArch64 Subtarget Information ----*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the AArch64 specific subclass of TargetSubtarget.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "AArch64Subtarget.h"
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "AArch64.h"
160b57cec5SDimitry Andric #include "AArch64CallLowering.h"
170b57cec5SDimitry Andric #include "AArch64InstrInfo.h"
180b57cec5SDimitry Andric #include "AArch64LegalizerInfo.h"
190b57cec5SDimitry Andric #include "AArch64PBQPRegAlloc.h"
200b57cec5SDimitry Andric #include "AArch64RegisterBankInfo.h"
210b57cec5SDimitry Andric #include "AArch64TargetMachine.h"
220b57cec5SDimitry Andric #include "MCTargetDesc/AArch64AddressingModes.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineScheduler.h"
250b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h"
260b57cec5SDimitry Andric #include "llvm/Support/TargetParser.h"
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric using namespace llvm;
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric #define DEBUG_TYPE "aarch64-subtarget"
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric #define GET_SUBTARGETINFO_CTOR
330b57cec5SDimitry Andric #define GET_SUBTARGETINFO_TARGET_DESC
340b57cec5SDimitry Andric #include "AArch64GenSubtargetInfo.inc"
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric static cl::opt<bool>
370b57cec5SDimitry Andric EnableEarlyIfConvert("aarch64-early-ifcvt", cl::desc("Enable the early if "
380b57cec5SDimitry Andric                      "converter pass"), cl::init(true), cl::Hidden);
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric // If OS supports TBI, use this flag to enable it.
410b57cec5SDimitry Andric static cl::opt<bool>
420b57cec5SDimitry Andric UseAddressTopByteIgnored("aarch64-use-tbi", cl::desc("Assume that top byte of "
430b57cec5SDimitry Andric                          "an address is ignored"), cl::init(false), cl::Hidden);
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric static cl::opt<bool>
460b57cec5SDimitry Andric     UseNonLazyBind("aarch64-enable-nonlazybind",
470b57cec5SDimitry Andric                    cl::desc("Call nonlazybind functions via direct GOT load"),
480b57cec5SDimitry Andric                    cl::init(false), cl::Hidden);
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric AArch64Subtarget &
510b57cec5SDimitry Andric AArch64Subtarget::initializeSubtargetDependencies(StringRef FS,
520b57cec5SDimitry Andric                                                   StringRef CPUString) {
530b57cec5SDimitry Andric   // Determine default and user-specified characteristics
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   if (CPUString.empty())
560b57cec5SDimitry Andric     CPUString = "generic";
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   ParseSubtargetFeatures(CPUString, FS);
590b57cec5SDimitry Andric   initializeProperties();
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   return *this;
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric void AArch64Subtarget::initializeProperties() {
650b57cec5SDimitry Andric   // Initialize CPU specific properties. We should add a tablegen feature for
660b57cec5SDimitry Andric   // this in the future so we can specify it together with the subtarget
670b57cec5SDimitry Andric   // features.
680b57cec5SDimitry Andric   switch (ARMProcFamily) {
690b57cec5SDimitry Andric   case Others:
700b57cec5SDimitry Andric     break;
710b57cec5SDimitry Andric   case CortexA35:
720b57cec5SDimitry Andric     break;
730b57cec5SDimitry Andric   case CortexA53:
74*8bcb0991SDimitry Andric     PrefFunctionLogAlignment = 3;
750b57cec5SDimitry Andric     break;
760b57cec5SDimitry Andric   case CortexA55:
770b57cec5SDimitry Andric     break;
780b57cec5SDimitry Andric   case CortexA57:
790b57cec5SDimitry Andric     MaxInterleaveFactor = 4;
80*8bcb0991SDimitry Andric     PrefFunctionLogAlignment = 4;
81*8bcb0991SDimitry Andric     break;
82*8bcb0991SDimitry Andric   case CortexA65:
83*8bcb0991SDimitry Andric     PrefFunctionLogAlignment = 3;
840b57cec5SDimitry Andric     break;
850b57cec5SDimitry Andric   case CortexA72:
860b57cec5SDimitry Andric   case CortexA73:
870b57cec5SDimitry Andric   case CortexA75:
880b57cec5SDimitry Andric   case CortexA76:
89*8bcb0991SDimitry Andric     PrefFunctionLogAlignment = 4;
900b57cec5SDimitry Andric     break;
910b57cec5SDimitry Andric   case Cyclone:
920b57cec5SDimitry Andric     CacheLineSize = 64;
930b57cec5SDimitry Andric     PrefetchDistance = 280;
940b57cec5SDimitry Andric     MinPrefetchStride = 2048;
950b57cec5SDimitry Andric     MaxPrefetchIterationsAhead = 3;
960b57cec5SDimitry Andric     break;
970b57cec5SDimitry Andric   case ExynosM1:
980b57cec5SDimitry Andric     MaxInterleaveFactor = 4;
990b57cec5SDimitry Andric     MaxJumpTableSize = 8;
100*8bcb0991SDimitry Andric     PrefFunctionLogAlignment = 4;
101*8bcb0991SDimitry Andric     PrefLoopLogAlignment = 3;
1020b57cec5SDimitry Andric     break;
1030b57cec5SDimitry Andric   case ExynosM3:
1040b57cec5SDimitry Andric     MaxInterleaveFactor = 4;
1050b57cec5SDimitry Andric     MaxJumpTableSize = 20;
106*8bcb0991SDimitry Andric     PrefFunctionLogAlignment = 5;
107*8bcb0991SDimitry Andric     PrefLoopLogAlignment = 4;
1080b57cec5SDimitry Andric     break;
1090b57cec5SDimitry Andric   case Falkor:
1100b57cec5SDimitry Andric     MaxInterleaveFactor = 4;
1110b57cec5SDimitry Andric     // FIXME: remove this to enable 64-bit SLP if performance looks good.
1120b57cec5SDimitry Andric     MinVectorRegisterBitWidth = 128;
1130b57cec5SDimitry Andric     CacheLineSize = 128;
1140b57cec5SDimitry Andric     PrefetchDistance = 820;
1150b57cec5SDimitry Andric     MinPrefetchStride = 2048;
1160b57cec5SDimitry Andric     MaxPrefetchIterationsAhead = 8;
1170b57cec5SDimitry Andric     break;
1180b57cec5SDimitry Andric   case Kryo:
1190b57cec5SDimitry Andric     MaxInterleaveFactor = 4;
1200b57cec5SDimitry Andric     VectorInsertExtractBaseCost = 2;
1210b57cec5SDimitry Andric     CacheLineSize = 128;
1220b57cec5SDimitry Andric     PrefetchDistance = 740;
1230b57cec5SDimitry Andric     MinPrefetchStride = 1024;
1240b57cec5SDimitry Andric     MaxPrefetchIterationsAhead = 11;
1250b57cec5SDimitry Andric     // FIXME: remove this to enable 64-bit SLP if performance looks good.
1260b57cec5SDimitry Andric     MinVectorRegisterBitWidth = 128;
1270b57cec5SDimitry Andric     break;
128*8bcb0991SDimitry Andric   case NeoverseE1:
129*8bcb0991SDimitry Andric     PrefFunctionLogAlignment = 3;
130*8bcb0991SDimitry Andric     break;
131*8bcb0991SDimitry Andric   case NeoverseN1:
132*8bcb0991SDimitry Andric     PrefFunctionLogAlignment = 4;
133*8bcb0991SDimitry Andric     break;
1340b57cec5SDimitry Andric   case Saphira:
1350b57cec5SDimitry Andric     MaxInterleaveFactor = 4;
1360b57cec5SDimitry Andric     // FIXME: remove this to enable 64-bit SLP if performance looks good.
1370b57cec5SDimitry Andric     MinVectorRegisterBitWidth = 128;
1380b57cec5SDimitry Andric     break;
1390b57cec5SDimitry Andric   case ThunderX2T99:
1400b57cec5SDimitry Andric     CacheLineSize = 64;
141*8bcb0991SDimitry Andric     PrefFunctionLogAlignment = 3;
142*8bcb0991SDimitry Andric     PrefLoopLogAlignment = 2;
1430b57cec5SDimitry Andric     MaxInterleaveFactor = 4;
1440b57cec5SDimitry Andric     PrefetchDistance = 128;
1450b57cec5SDimitry Andric     MinPrefetchStride = 1024;
1460b57cec5SDimitry Andric     MaxPrefetchIterationsAhead = 4;
1470b57cec5SDimitry Andric     // FIXME: remove this to enable 64-bit SLP if performance looks good.
1480b57cec5SDimitry Andric     MinVectorRegisterBitWidth = 128;
1490b57cec5SDimitry Andric     break;
1500b57cec5SDimitry Andric   case ThunderX:
1510b57cec5SDimitry Andric   case ThunderXT88:
1520b57cec5SDimitry Andric   case ThunderXT81:
1530b57cec5SDimitry Andric   case ThunderXT83:
1540b57cec5SDimitry Andric     CacheLineSize = 128;
155*8bcb0991SDimitry Andric     PrefFunctionLogAlignment = 3;
156*8bcb0991SDimitry Andric     PrefLoopLogAlignment = 2;
1570b57cec5SDimitry Andric     // FIXME: remove this to enable 64-bit SLP if performance looks good.
1580b57cec5SDimitry Andric     MinVectorRegisterBitWidth = 128;
1590b57cec5SDimitry Andric     break;
1600b57cec5SDimitry Andric   case TSV110:
1610b57cec5SDimitry Andric     CacheLineSize = 64;
162*8bcb0991SDimitry Andric     PrefFunctionLogAlignment = 4;
163*8bcb0991SDimitry Andric     PrefLoopLogAlignment = 2;
1640b57cec5SDimitry Andric     break;
1650b57cec5SDimitry Andric   }
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric AArch64Subtarget::AArch64Subtarget(const Triple &TT, const std::string &CPU,
1690b57cec5SDimitry Andric                                    const std::string &FS,
1700b57cec5SDimitry Andric                                    const TargetMachine &TM, bool LittleEndian)
1710b57cec5SDimitry Andric     : AArch64GenSubtargetInfo(TT, CPU, FS),
1720b57cec5SDimitry Andric       ReserveXRegister(AArch64::GPR64commonRegClass.getNumRegs()),
1730b57cec5SDimitry Andric       CustomCallSavedXRegs(AArch64::GPR64commonRegClass.getNumRegs()),
1740b57cec5SDimitry Andric       IsLittle(LittleEndian),
1750b57cec5SDimitry Andric       TargetTriple(TT), FrameLowering(),
1760b57cec5SDimitry Andric       InstrInfo(initializeSubtargetDependencies(FS, CPU)), TSInfo(),
1770b57cec5SDimitry Andric       TLInfo(TM, *this) {
1780b57cec5SDimitry Andric   if (AArch64::isX18ReservedByDefault(TT))
1790b57cec5SDimitry Andric     ReserveXRegister.set(18);
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric   CallLoweringInfo.reset(new AArch64CallLowering(*getTargetLowering()));
1820b57cec5SDimitry Andric   Legalizer.reset(new AArch64LegalizerInfo(*this));
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric   auto *RBI = new AArch64RegisterBankInfo(*getRegisterInfo());
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric   // FIXME: At this point, we can't rely on Subtarget having RBI.
1870b57cec5SDimitry Andric   // It's awkward to mix passing RBI and the Subtarget; should we pass
1880b57cec5SDimitry Andric   // TII/TRI as well?
1890b57cec5SDimitry Andric   InstSelector.reset(createAArch64InstructionSelector(
1900b57cec5SDimitry Andric       *static_cast<const AArch64TargetMachine *>(&TM), *this, *RBI));
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric   RegBankInfo.reset(RBI);
1930b57cec5SDimitry Andric }
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric const CallLowering *AArch64Subtarget::getCallLowering() const {
1960b57cec5SDimitry Andric   return CallLoweringInfo.get();
1970b57cec5SDimitry Andric }
1980b57cec5SDimitry Andric 
199*8bcb0991SDimitry Andric InstructionSelector *AArch64Subtarget::getInstructionSelector() const {
2000b57cec5SDimitry Andric   return InstSelector.get();
2010b57cec5SDimitry Andric }
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric const LegalizerInfo *AArch64Subtarget::getLegalizerInfo() const {
2040b57cec5SDimitry Andric   return Legalizer.get();
2050b57cec5SDimitry Andric }
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric const RegisterBankInfo *AArch64Subtarget::getRegBankInfo() const {
2080b57cec5SDimitry Andric   return RegBankInfo.get();
2090b57cec5SDimitry Andric }
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric /// Find the target operand flags that describe how a global value should be
2120b57cec5SDimitry Andric /// referenced for the current subtarget.
213*8bcb0991SDimitry Andric unsigned
2140b57cec5SDimitry Andric AArch64Subtarget::ClassifyGlobalReference(const GlobalValue *GV,
2150b57cec5SDimitry Andric                                           const TargetMachine &TM) const {
2160b57cec5SDimitry Andric   // MachO large model always goes via a GOT, simply to get a single 8-byte
2170b57cec5SDimitry Andric   // absolute relocation on all global addresses.
2180b57cec5SDimitry Andric   if (TM.getCodeModel() == CodeModel::Large && isTargetMachO())
2190b57cec5SDimitry Andric     return AArch64II::MO_GOT;
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric   if (!TM.shouldAssumeDSOLocal(*GV->getParent(), GV)) {
2220b57cec5SDimitry Andric     if (GV->hasDLLImportStorageClass())
2230b57cec5SDimitry Andric       return AArch64II::MO_GOT | AArch64II::MO_DLLIMPORT;
2240b57cec5SDimitry Andric     if (getTargetTriple().isOSWindows())
2250b57cec5SDimitry Andric       return AArch64II::MO_GOT | AArch64II::MO_COFFSTUB;
2260b57cec5SDimitry Andric     return AArch64II::MO_GOT;
2270b57cec5SDimitry Andric   }
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric   // The small code model's direct accesses use ADRP, which cannot
2300b57cec5SDimitry Andric   // necessarily produce the value 0 (if the code is above 4GB).
2310b57cec5SDimitry Andric   // Same for the tiny code model, where we have a pc relative LDR.
2320b57cec5SDimitry Andric   if ((useSmallAddressing() || TM.getCodeModel() == CodeModel::Tiny) &&
2330b57cec5SDimitry Andric       GV->hasExternalWeakLinkage())
2340b57cec5SDimitry Andric     return AArch64II::MO_GOT;
2350b57cec5SDimitry Andric 
236*8bcb0991SDimitry Andric   // References to tagged globals are marked with MO_NC | MO_TAGGED to indicate
237*8bcb0991SDimitry Andric   // that their nominal addresses are tagged and outside of the code model. In
238*8bcb0991SDimitry Andric   // AArch64ExpandPseudo::expandMI we emit an additional instruction to set the
239*8bcb0991SDimitry Andric   // tag if necessary based on MO_TAGGED.
240*8bcb0991SDimitry Andric   if (AllowTaggedGlobals && !isa<FunctionType>(GV->getValueType()))
241*8bcb0991SDimitry Andric     return AArch64II::MO_NC | AArch64II::MO_TAGGED;
242*8bcb0991SDimitry Andric 
2430b57cec5SDimitry Andric   return AArch64II::MO_NO_FLAG;
2440b57cec5SDimitry Andric }
2450b57cec5SDimitry Andric 
246*8bcb0991SDimitry Andric unsigned AArch64Subtarget::classifyGlobalFunctionReference(
2470b57cec5SDimitry Andric     const GlobalValue *GV, const TargetMachine &TM) const {
2480b57cec5SDimitry Andric   // MachO large model always goes via a GOT, because we don't have the
2490b57cec5SDimitry Andric   // relocations available to do anything else..
2500b57cec5SDimitry Andric   if (TM.getCodeModel() == CodeModel::Large && isTargetMachO() &&
2510b57cec5SDimitry Andric       !GV->hasInternalLinkage())
2520b57cec5SDimitry Andric     return AArch64II::MO_GOT;
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric   // NonLazyBind goes via GOT unless we know it's available locally.
2550b57cec5SDimitry Andric   auto *F = dyn_cast<Function>(GV);
2560b57cec5SDimitry Andric   if (UseNonLazyBind && F && F->hasFnAttribute(Attribute::NonLazyBind) &&
2570b57cec5SDimitry Andric       !TM.shouldAssumeDSOLocal(*GV->getParent(), GV))
2580b57cec5SDimitry Andric     return AArch64II::MO_GOT;
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric   return AArch64II::MO_NO_FLAG;
2610b57cec5SDimitry Andric }
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric void AArch64Subtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
2640b57cec5SDimitry Andric                                            unsigned NumRegionInstrs) const {
2650b57cec5SDimitry Andric   // LNT run (at least on Cyclone) showed reasonably significant gains for
2660b57cec5SDimitry Andric   // bi-directional scheduling. 253.perlbmk.
2670b57cec5SDimitry Andric   Policy.OnlyTopDown = false;
2680b57cec5SDimitry Andric   Policy.OnlyBottomUp = false;
2690b57cec5SDimitry Andric   // Enabling or Disabling the latency heuristic is a close call: It seems to
2700b57cec5SDimitry Andric   // help nearly no benchmark on out-of-order architectures, on the other hand
2710b57cec5SDimitry Andric   // it regresses register pressure on a few benchmarking.
2720b57cec5SDimitry Andric   Policy.DisableLatencyHeuristic = DisableLatencySchedHeuristic;
2730b57cec5SDimitry Andric }
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric bool AArch64Subtarget::enableEarlyIfConversion() const {
2760b57cec5SDimitry Andric   return EnableEarlyIfConvert;
2770b57cec5SDimitry Andric }
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric bool AArch64Subtarget::supportsAddressTopByteIgnored() const {
2800b57cec5SDimitry Andric   if (!UseAddressTopByteIgnored)
2810b57cec5SDimitry Andric     return false;
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric   if (TargetTriple.isiOS()) {
2840b57cec5SDimitry Andric     unsigned Major, Minor, Micro;
2850b57cec5SDimitry Andric     TargetTriple.getiOSVersion(Major, Minor, Micro);
2860b57cec5SDimitry Andric     return Major >= 8;
2870b57cec5SDimitry Andric   }
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric   return false;
2900b57cec5SDimitry Andric }
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric std::unique_ptr<PBQPRAConstraint>
2930b57cec5SDimitry Andric AArch64Subtarget::getCustomPBQPConstraints() const {
294*8bcb0991SDimitry Andric   return balanceFPOps() ? std::make_unique<A57ChainingConstraint>() : nullptr;
2950b57cec5SDimitry Andric }
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric void AArch64Subtarget::mirFileLoaded(MachineFunction &MF) const {
2980b57cec5SDimitry Andric   // We usually compute max call frame size after ISel. Do the computation now
2990b57cec5SDimitry Andric   // if the .mir file didn't specify it. Note that this will probably give you
3000b57cec5SDimitry Andric   // bogus values after PEI has eliminated the callframe setup/destroy pseudo
3010b57cec5SDimitry Andric   // instructions, specify explicitly if you need it to be correct.
3020b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
3030b57cec5SDimitry Andric   if (!MFI.isMaxCallFrameSizeComputed())
3040b57cec5SDimitry Andric     MFI.computeMaxCallFrameSize(MF);
3050b57cec5SDimitry Andric }
306