xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AArch64/AArch64RegisterInfo.cpp (revision 8bcb0991864975618c09697b1aca10683346d9f0)
10b57cec5SDimitry Andric //===- AArch64RegisterInfo.cpp - AArch64 Register Information -------------===//
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 contains the AArch64 implementation of the TargetRegisterInfo
100b57cec5SDimitry Andric // class.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "AArch64RegisterInfo.h"
150b57cec5SDimitry Andric #include "AArch64FrameLowering.h"
160b57cec5SDimitry Andric #include "AArch64InstrInfo.h"
170b57cec5SDimitry Andric #include "AArch64MachineFunctionInfo.h"
18*8bcb0991SDimitry Andric #include "AArch64StackOffset.h"
190b57cec5SDimitry Andric #include "AArch64Subtarget.h"
200b57cec5SDimitry Andric #include "MCTargetDesc/AArch64AddressingModes.h"
210b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
220b57cec5SDimitry Andric #include "llvm/ADT/Triple.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
28*8bcb0991SDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
29*8bcb0991SDimitry Andric #include "llvm/IR/Function.h"
30*8bcb0991SDimitry Andric #include "llvm/Support/raw_ostream.h"
310b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric using namespace llvm;
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric #define GET_REGINFO_TARGET_DESC
360b57cec5SDimitry Andric #include "AArch64GenRegisterInfo.inc"
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric AArch64RegisterInfo::AArch64RegisterInfo(const Triple &TT)
390b57cec5SDimitry Andric     : AArch64GenRegisterInfo(AArch64::LR), TT(TT) {
400b57cec5SDimitry Andric   AArch64_MC::initLLVMToCVRegMapping(this);
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric const MCPhysReg *
440b57cec5SDimitry Andric AArch64RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
450b57cec5SDimitry Andric   assert(MF && "Invalid MachineFunction pointer.");
460b57cec5SDimitry Andric   if (MF->getSubtarget<AArch64Subtarget>().isTargetWindows())
470b57cec5SDimitry Andric     return CSR_Win_AArch64_AAPCS_SaveList;
480b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::GHC)
490b57cec5SDimitry Andric     // GHC set of callee saved regs is empty as all those regs are
500b57cec5SDimitry Andric     // used for passing STG regs around
510b57cec5SDimitry Andric     return CSR_AArch64_NoRegs_SaveList;
520b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AnyReg)
530b57cec5SDimitry Andric     return CSR_AArch64_AllRegs_SaveList;
540b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AArch64_VectorCall)
550b57cec5SDimitry Andric     return CSR_AArch64_AAVPCS_SaveList;
560b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS)
570b57cec5SDimitry Andric     return MF->getInfo<AArch64FunctionInfo>()->isSplitCSR() ?
580b57cec5SDimitry Andric            CSR_AArch64_CXX_TLS_Darwin_PE_SaveList :
590b57cec5SDimitry Andric            CSR_AArch64_CXX_TLS_Darwin_SaveList;
600b57cec5SDimitry Andric   if (MF->getSubtarget<AArch64Subtarget>().getTargetLowering()
610b57cec5SDimitry Andric           ->supportSwiftError() &&
620b57cec5SDimitry Andric       MF->getFunction().getAttributes().hasAttrSomewhere(
630b57cec5SDimitry Andric           Attribute::SwiftError))
640b57cec5SDimitry Andric     return CSR_AArch64_AAPCS_SwiftError_SaveList;
650b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::PreserveMost)
660b57cec5SDimitry Andric     return CSR_AArch64_RT_MostRegs_SaveList;
67*8bcb0991SDimitry Andric   if (MF->getSubtarget<AArch64Subtarget>().isTargetDarwin())
68*8bcb0991SDimitry Andric     return CSR_Darwin_AArch64_AAPCS_SaveList;
690b57cec5SDimitry Andric   return CSR_AArch64_AAPCS_SaveList;
700b57cec5SDimitry Andric }
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric const MCPhysReg *AArch64RegisterInfo::getCalleeSavedRegsViaCopy(
730b57cec5SDimitry Andric     const MachineFunction *MF) const {
740b57cec5SDimitry Andric   assert(MF && "Invalid MachineFunction pointer.");
750b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS &&
760b57cec5SDimitry Andric       MF->getInfo<AArch64FunctionInfo>()->isSplitCSR())
770b57cec5SDimitry Andric     return CSR_AArch64_CXX_TLS_Darwin_ViaCopy_SaveList;
780b57cec5SDimitry Andric   return nullptr;
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric void AArch64RegisterInfo::UpdateCustomCalleeSavedRegs(
820b57cec5SDimitry Andric     MachineFunction &MF) const {
830b57cec5SDimitry Andric   const MCPhysReg *CSRs = getCalleeSavedRegs(&MF);
840b57cec5SDimitry Andric   SmallVector<MCPhysReg, 32> UpdatedCSRs;
850b57cec5SDimitry Andric   for (const MCPhysReg *I = CSRs; *I; ++I)
860b57cec5SDimitry Andric     UpdatedCSRs.push_back(*I);
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric   for (size_t i = 0; i < AArch64::GPR64commonRegClass.getNumRegs(); ++i) {
890b57cec5SDimitry Andric     if (MF.getSubtarget<AArch64Subtarget>().isXRegCustomCalleeSaved(i)) {
900b57cec5SDimitry Andric       UpdatedCSRs.push_back(AArch64::GPR64commonRegClass.getRegister(i));
910b57cec5SDimitry Andric     }
920b57cec5SDimitry Andric   }
930b57cec5SDimitry Andric   // Register lists are zero-terminated.
940b57cec5SDimitry Andric   UpdatedCSRs.push_back(0);
950b57cec5SDimitry Andric   MF.getRegInfo().setCalleeSavedRegs(UpdatedCSRs);
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric const TargetRegisterClass *
990b57cec5SDimitry Andric AArch64RegisterInfo::getSubClassWithSubReg(const TargetRegisterClass *RC,
1000b57cec5SDimitry Andric                                        unsigned Idx) const {
1010b57cec5SDimitry Andric   // edge case for GPR/FPR register classes
1020b57cec5SDimitry Andric   if (RC == &AArch64::GPR32allRegClass && Idx == AArch64::hsub)
1030b57cec5SDimitry Andric     return &AArch64::FPR32RegClass;
1040b57cec5SDimitry Andric   else if (RC == &AArch64::GPR64allRegClass && Idx == AArch64::hsub)
1050b57cec5SDimitry Andric     return &AArch64::FPR64RegClass;
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric   // Forward to TableGen's default version.
1080b57cec5SDimitry Andric   return AArch64GenRegisterInfo::getSubClassWithSubReg(RC, Idx);
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric const uint32_t *
1120b57cec5SDimitry Andric AArch64RegisterInfo::getCallPreservedMask(const MachineFunction &MF,
1130b57cec5SDimitry Andric                                           CallingConv::ID CC) const {
1140b57cec5SDimitry Andric   bool SCS = MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack);
1150b57cec5SDimitry Andric   if (CC == CallingConv::GHC)
1160b57cec5SDimitry Andric     // This is academic because all GHC calls are (supposed to be) tail calls
1170b57cec5SDimitry Andric     return SCS ? CSR_AArch64_NoRegs_SCS_RegMask : CSR_AArch64_NoRegs_RegMask;
1180b57cec5SDimitry Andric   if (CC == CallingConv::AnyReg)
1190b57cec5SDimitry Andric     return SCS ? CSR_AArch64_AllRegs_SCS_RegMask : CSR_AArch64_AllRegs_RegMask;
1200b57cec5SDimitry Andric   if (CC == CallingConv::CXX_FAST_TLS)
1210b57cec5SDimitry Andric     return SCS ? CSR_AArch64_CXX_TLS_Darwin_SCS_RegMask
1220b57cec5SDimitry Andric                : CSR_AArch64_CXX_TLS_Darwin_RegMask;
1230b57cec5SDimitry Andric   if (CC == CallingConv::AArch64_VectorCall)
1240b57cec5SDimitry Andric     return SCS ? CSR_AArch64_AAVPCS_SCS_RegMask : CSR_AArch64_AAVPCS_RegMask;
125*8bcb0991SDimitry Andric   if (CC == CallingConv::AArch64_SVE_VectorCall)
126*8bcb0991SDimitry Andric     return CSR_AArch64_SVE_AAPCS_RegMask;
1270b57cec5SDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>().getTargetLowering()
1280b57cec5SDimitry Andric           ->supportSwiftError() &&
1290b57cec5SDimitry Andric       MF.getFunction().getAttributes().hasAttrSomewhere(Attribute::SwiftError))
1300b57cec5SDimitry Andric     return SCS ? CSR_AArch64_AAPCS_SwiftError_SCS_RegMask
1310b57cec5SDimitry Andric                : CSR_AArch64_AAPCS_SwiftError_RegMask;
1320b57cec5SDimitry Andric   if (CC == CallingConv::PreserveMost)
1330b57cec5SDimitry Andric     return SCS ? CSR_AArch64_RT_MostRegs_SCS_RegMask
1340b57cec5SDimitry Andric                : CSR_AArch64_RT_MostRegs_RegMask;
1350b57cec5SDimitry Andric   else
1360b57cec5SDimitry Andric     return SCS ? CSR_AArch64_AAPCS_SCS_RegMask : CSR_AArch64_AAPCS_RegMask;
1370b57cec5SDimitry Andric }
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric const uint32_t *AArch64RegisterInfo::getTLSCallPreservedMask() const {
1400b57cec5SDimitry Andric   if (TT.isOSDarwin())
1410b57cec5SDimitry Andric     return CSR_AArch64_TLS_Darwin_RegMask;
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric   assert(TT.isOSBinFormatELF() && "Invalid target");
1440b57cec5SDimitry Andric   return CSR_AArch64_TLS_ELF_RegMask;
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric void AArch64RegisterInfo::UpdateCustomCallPreservedMask(MachineFunction &MF,
1480b57cec5SDimitry Andric                                                  const uint32_t **Mask) const {
1490b57cec5SDimitry Andric   uint32_t *UpdatedMask = MF.allocateRegMask();
1500b57cec5SDimitry Andric   unsigned RegMaskSize = MachineOperand::getRegMaskSize(getNumRegs());
1510b57cec5SDimitry Andric   memcpy(UpdatedMask, *Mask, sizeof(UpdatedMask[0]) * RegMaskSize);
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric   for (size_t i = 0; i < AArch64::GPR64commonRegClass.getNumRegs(); ++i) {
1540b57cec5SDimitry Andric     if (MF.getSubtarget<AArch64Subtarget>().isXRegCustomCalleeSaved(i)) {
1550b57cec5SDimitry Andric       for (MCSubRegIterator SubReg(AArch64::GPR64commonRegClass.getRegister(i),
1560b57cec5SDimitry Andric                                    this, true);
1570b57cec5SDimitry Andric            SubReg.isValid(); ++SubReg) {
1580b57cec5SDimitry Andric         // See TargetRegisterInfo::getCallPreservedMask for how to interpret the
1590b57cec5SDimitry Andric         // register mask.
1600b57cec5SDimitry Andric         UpdatedMask[*SubReg / 32] |= 1u << (*SubReg % 32);
1610b57cec5SDimitry Andric       }
1620b57cec5SDimitry Andric     }
1630b57cec5SDimitry Andric   }
1640b57cec5SDimitry Andric   *Mask = UpdatedMask;
1650b57cec5SDimitry Andric }
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric const uint32_t *AArch64RegisterInfo::getNoPreservedMask() const {
1680b57cec5SDimitry Andric   return CSR_AArch64_NoRegs_RegMask;
1690b57cec5SDimitry Andric }
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric const uint32_t *
1720b57cec5SDimitry Andric AArch64RegisterInfo::getThisReturnPreservedMask(const MachineFunction &MF,
1730b57cec5SDimitry Andric                                                 CallingConv::ID CC) const {
1740b57cec5SDimitry Andric   // This should return a register mask that is the same as that returned by
1750b57cec5SDimitry Andric   // getCallPreservedMask but that additionally preserves the register used for
1760b57cec5SDimitry Andric   // the first i64 argument (which must also be the register used to return a
1770b57cec5SDimitry Andric   // single i64 return value)
1780b57cec5SDimitry Andric   //
1790b57cec5SDimitry Andric   // In case that the calling convention does not use the same register for
1800b57cec5SDimitry Andric   // both, the function should return NULL (does not currently apply)
1810b57cec5SDimitry Andric   assert(CC != CallingConv::GHC && "should not be GHC calling convention.");
1820b57cec5SDimitry Andric   return CSR_AArch64_AAPCS_ThisReturn_RegMask;
1830b57cec5SDimitry Andric }
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric const uint32_t *AArch64RegisterInfo::getWindowsStackProbePreservedMask() const {
1860b57cec5SDimitry Andric   return CSR_AArch64_StackProbe_Windows_RegMask;
1870b57cec5SDimitry Andric }
1880b57cec5SDimitry Andric 
1890b57cec5SDimitry Andric BitVector
1900b57cec5SDimitry Andric AArch64RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
1910b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric   // FIXME: avoid re-calculating this every time.
1940b57cec5SDimitry Andric   BitVector Reserved(getNumRegs());
1950b57cec5SDimitry Andric   markSuperRegs(Reserved, AArch64::WSP);
1960b57cec5SDimitry Andric   markSuperRegs(Reserved, AArch64::WZR);
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric   if (TFI->hasFP(MF) || TT.isOSDarwin())
1990b57cec5SDimitry Andric     markSuperRegs(Reserved, AArch64::W29);
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric   for (size_t i = 0; i < AArch64::GPR32commonRegClass.getNumRegs(); ++i) {
2020b57cec5SDimitry Andric     if (MF.getSubtarget<AArch64Subtarget>().isXRegisterReserved(i))
2030b57cec5SDimitry Andric       markSuperRegs(Reserved, AArch64::GPR32commonRegClass.getRegister(i));
2040b57cec5SDimitry Andric   }
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric   if (hasBasePointer(MF))
2070b57cec5SDimitry Andric     markSuperRegs(Reserved, AArch64::W19);
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric   // SLH uses register W16/X16 as the taint register.
2100b57cec5SDimitry Andric   if (MF.getFunction().hasFnAttribute(Attribute::SpeculativeLoadHardening))
2110b57cec5SDimitry Andric     markSuperRegs(Reserved, AArch64::W16);
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric   assert(checkAllSuperRegsMarked(Reserved));
2140b57cec5SDimitry Andric   return Reserved;
2150b57cec5SDimitry Andric }
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric bool AArch64RegisterInfo::isReservedReg(const MachineFunction &MF,
2180b57cec5SDimitry Andric                                       unsigned Reg) const {
2190b57cec5SDimitry Andric   return getReservedRegs(MF)[Reg];
2200b57cec5SDimitry Andric }
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric bool AArch64RegisterInfo::isAnyArgRegReserved(const MachineFunction &MF) const {
2230b57cec5SDimitry Andric   return std::any_of(std::begin(*AArch64::GPR64argRegClass.MC),
2240b57cec5SDimitry Andric                      std::end(*AArch64::GPR64argRegClass.MC),
2250b57cec5SDimitry Andric                      [this, &MF](MCPhysReg r){return isReservedReg(MF, r);});
2260b57cec5SDimitry Andric }
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric void AArch64RegisterInfo::emitReservedArgRegCallError(
2290b57cec5SDimitry Andric     const MachineFunction &MF) const {
2300b57cec5SDimitry Andric   const Function &F = MF.getFunction();
2310b57cec5SDimitry Andric   F.getContext().diagnose(DiagnosticInfoUnsupported{F, "AArch64 doesn't support"
2320b57cec5SDimitry Andric     " function calls if any of the argument registers is reserved."});
2330b57cec5SDimitry Andric }
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric bool AArch64RegisterInfo::isAsmClobberable(const MachineFunction &MF,
2360b57cec5SDimitry Andric                                           unsigned PhysReg) const {
2370b57cec5SDimitry Andric   return !isReservedReg(MF, PhysReg);
2380b57cec5SDimitry Andric }
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric bool AArch64RegisterInfo::isConstantPhysReg(unsigned PhysReg) const {
2410b57cec5SDimitry Andric   return PhysReg == AArch64::WZR || PhysReg == AArch64::XZR;
2420b57cec5SDimitry Andric }
2430b57cec5SDimitry Andric 
2440b57cec5SDimitry Andric const TargetRegisterClass *
2450b57cec5SDimitry Andric AArch64RegisterInfo::getPointerRegClass(const MachineFunction &MF,
2460b57cec5SDimitry Andric                                       unsigned Kind) const {
2470b57cec5SDimitry Andric   return &AArch64::GPR64spRegClass;
2480b57cec5SDimitry Andric }
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric const TargetRegisterClass *
2510b57cec5SDimitry Andric AArch64RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
2520b57cec5SDimitry Andric   if (RC == &AArch64::CCRRegClass)
2530b57cec5SDimitry Andric     return &AArch64::GPR64RegClass; // Only MSR & MRS copy NZCV.
2540b57cec5SDimitry Andric   return RC;
2550b57cec5SDimitry Andric }
2560b57cec5SDimitry Andric 
2570b57cec5SDimitry Andric unsigned AArch64RegisterInfo::getBaseRegister() const { return AArch64::X19; }
2580b57cec5SDimitry Andric 
2590b57cec5SDimitry Andric bool AArch64RegisterInfo::hasBasePointer(const MachineFunction &MF) const {
2600b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric   // In the presence of variable sized objects or funclets, if the fixed stack
2630b57cec5SDimitry Andric   // size is large enough that referencing from the FP won't result in things
2640b57cec5SDimitry Andric   // being in range relatively often, we can use a base pointer to allow access
2650b57cec5SDimitry Andric   // from the other direction like the SP normally works.
2660b57cec5SDimitry Andric   //
2670b57cec5SDimitry Andric   // Furthermore, if both variable sized objects are present, and the
2680b57cec5SDimitry Andric   // stack needs to be dynamically re-aligned, the base pointer is the only
2690b57cec5SDimitry Andric   // reliable way to reference the locals.
2700b57cec5SDimitry Andric   if (MFI.hasVarSizedObjects() || MF.hasEHFunclets()) {
2710b57cec5SDimitry Andric     if (needsStackRealignment(MF))
2720b57cec5SDimitry Andric       return true;
2730b57cec5SDimitry Andric     // Conservatively estimate whether the negative offset from the frame
2740b57cec5SDimitry Andric     // pointer will be sufficient to reach. If a function has a smallish
2750b57cec5SDimitry Andric     // frame, it's less likely to have lots of spills and callee saved
2760b57cec5SDimitry Andric     // space, so it's all more likely to be within range of the frame pointer.
2770b57cec5SDimitry Andric     // If it's wrong, we'll materialize the constant and still get to the
2780b57cec5SDimitry Andric     // object; it's just suboptimal. Negative offsets use the unscaled
2790b57cec5SDimitry Andric     // load/store instructions, which have a 9-bit signed immediate.
2800b57cec5SDimitry Andric     return MFI.getLocalFrameSize() >= 256;
2810b57cec5SDimitry Andric   }
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric   return false;
2840b57cec5SDimitry Andric }
2850b57cec5SDimitry Andric 
2860b57cec5SDimitry Andric Register
2870b57cec5SDimitry Andric AArch64RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
2880b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
2890b57cec5SDimitry Andric   return TFI->hasFP(MF) ? AArch64::FP : AArch64::SP;
2900b57cec5SDimitry Andric }
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric bool AArch64RegisterInfo::requiresRegisterScavenging(
2930b57cec5SDimitry Andric     const MachineFunction &MF) const {
2940b57cec5SDimitry Andric   return true;
2950b57cec5SDimitry Andric }
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric bool AArch64RegisterInfo::requiresVirtualBaseRegisters(
2980b57cec5SDimitry Andric     const MachineFunction &MF) const {
2990b57cec5SDimitry Andric   return true;
3000b57cec5SDimitry Andric }
3010b57cec5SDimitry Andric 
3020b57cec5SDimitry Andric bool
3030b57cec5SDimitry Andric AArch64RegisterInfo::useFPForScavengingIndex(const MachineFunction &MF) const {
3040b57cec5SDimitry Andric   // This function indicates whether the emergency spillslot should be placed
3050b57cec5SDimitry Andric   // close to the beginning of the stackframe (closer to FP) or the end
3060b57cec5SDimitry Andric   // (closer to SP).
3070b57cec5SDimitry Andric   //
3080b57cec5SDimitry Andric   // The beginning works most reliably if we have a frame pointer.
3090b57cec5SDimitry Andric   const AArch64FrameLowering &TFI = *getFrameLowering(MF);
3100b57cec5SDimitry Andric   return TFI.hasFP(MF);
3110b57cec5SDimitry Andric }
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric bool AArch64RegisterInfo::requiresFrameIndexScavenging(
3140b57cec5SDimitry Andric     const MachineFunction &MF) const {
3150b57cec5SDimitry Andric   return true;
3160b57cec5SDimitry Andric }
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric bool
3190b57cec5SDimitry Andric AArch64RegisterInfo::cannotEliminateFrame(const MachineFunction &MF) const {
3200b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
3210b57cec5SDimitry Andric   if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI.adjustsStack())
3220b57cec5SDimitry Andric     return true;
3230b57cec5SDimitry Andric   return MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken();
3240b57cec5SDimitry Andric }
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric /// needsFrameBaseReg - Returns true if the instruction's frame index
3270b57cec5SDimitry Andric /// reference would be better served by a base register other than FP
3280b57cec5SDimitry Andric /// or SP. Used by LocalStackFrameAllocation to determine which frame index
3290b57cec5SDimitry Andric /// references it should create new base registers for.
3300b57cec5SDimitry Andric bool AArch64RegisterInfo::needsFrameBaseReg(MachineInstr *MI,
3310b57cec5SDimitry Andric                                             int64_t Offset) const {
3320b57cec5SDimitry Andric   for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i)
3330b57cec5SDimitry Andric     assert(i < MI->getNumOperands() &&
3340b57cec5SDimitry Andric            "Instr doesn't have FrameIndex operand!");
3350b57cec5SDimitry Andric 
3360b57cec5SDimitry Andric   // It's the load/store FI references that cause issues, as it can be difficult
3370b57cec5SDimitry Andric   // to materialize the offset if it won't fit in the literal field. Estimate
3380b57cec5SDimitry Andric   // based on the size of the local frame and some conservative assumptions
3390b57cec5SDimitry Andric   // about the rest of the stack frame (note, this is pre-regalloc, so
3400b57cec5SDimitry Andric   // we don't know everything for certain yet) whether this offset is likely
3410b57cec5SDimitry Andric   // to be out of range of the immediate. Return true if so.
3420b57cec5SDimitry Andric 
3430b57cec5SDimitry Andric   // We only generate virtual base registers for loads and stores, so
3440b57cec5SDimitry Andric   // return false for everything else.
3450b57cec5SDimitry Andric   if (!MI->mayLoad() && !MI->mayStore())
3460b57cec5SDimitry Andric     return false;
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric   // Without a virtual base register, if the function has variable sized
3490b57cec5SDimitry Andric   // objects, all fixed-size local references will be via the frame pointer,
3500b57cec5SDimitry Andric   // Approximate the offset and see if it's legal for the instruction.
3510b57cec5SDimitry Andric   // Note that the incoming offset is based on the SP value at function entry,
3520b57cec5SDimitry Andric   // so it'll be negative.
3530b57cec5SDimitry Andric   MachineFunction &MF = *MI->getParent()->getParent();
3540b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
3550b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric   // Estimate an offset from the frame pointer.
3580b57cec5SDimitry Andric   // Conservatively assume all GPR callee-saved registers get pushed.
3590b57cec5SDimitry Andric   // FP, LR, X19-X28, D8-D15. 64-bits each.
3600b57cec5SDimitry Andric   int64_t FPOffset = Offset - 16 * 20;
3610b57cec5SDimitry Andric   // Estimate an offset from the stack pointer.
3620b57cec5SDimitry Andric   // The incoming offset is relating to the SP at the start of the function,
3630b57cec5SDimitry Andric   // but when we access the local it'll be relative to the SP after local
3640b57cec5SDimitry Andric   // allocation, so adjust our SP-relative offset by that allocation size.
3650b57cec5SDimitry Andric   Offset += MFI.getLocalFrameSize();
3660b57cec5SDimitry Andric   // Assume that we'll have at least some spill slots allocated.
3670b57cec5SDimitry Andric   // FIXME: This is a total SWAG number. We should run some statistics
3680b57cec5SDimitry Andric   //        and pick a real one.
3690b57cec5SDimitry Andric   Offset += 128; // 128 bytes of spill slots
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric   // If there is a frame pointer, try using it.
3720b57cec5SDimitry Andric   // The FP is only available if there is no dynamic realignment. We
3730b57cec5SDimitry Andric   // don't know for sure yet whether we'll need that, so we guess based
3740b57cec5SDimitry Andric   // on whether there are any local variables that would trigger it.
3750b57cec5SDimitry Andric   if (TFI->hasFP(MF) && isFrameOffsetLegal(MI, AArch64::FP, FPOffset))
3760b57cec5SDimitry Andric     return false;
3770b57cec5SDimitry Andric 
3780b57cec5SDimitry Andric   // If we can reference via the stack pointer or base pointer, try that.
3790b57cec5SDimitry Andric   // FIXME: This (and the code that resolves the references) can be improved
3800b57cec5SDimitry Andric   //        to only disallow SP relative references in the live range of
3810b57cec5SDimitry Andric   //        the VLA(s). In practice, it's unclear how much difference that
3820b57cec5SDimitry Andric   //        would make, but it may be worth doing.
3830b57cec5SDimitry Andric   if (isFrameOffsetLegal(MI, AArch64::SP, Offset))
3840b57cec5SDimitry Andric     return false;
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric   // The offset likely isn't legal; we want to allocate a virtual base register.
3870b57cec5SDimitry Andric   return true;
3880b57cec5SDimitry Andric }
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric bool AArch64RegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
3910b57cec5SDimitry Andric                                              unsigned BaseReg,
3920b57cec5SDimitry Andric                                              int64_t Offset) const {
3930b57cec5SDimitry Andric   assert(Offset <= INT_MAX && "Offset too big to fit in int.");
3940b57cec5SDimitry Andric   assert(MI && "Unable to get the legal offset for nil instruction.");
395*8bcb0991SDimitry Andric   StackOffset SaveOffset(Offset, MVT::i8);
3960b57cec5SDimitry Andric   return isAArch64FrameOffsetLegal(*MI, SaveOffset) & AArch64FrameOffsetIsLegal;
3970b57cec5SDimitry Andric }
3980b57cec5SDimitry Andric 
3990b57cec5SDimitry Andric /// Insert defining instruction(s) for BaseReg to be a pointer to FrameIdx
4000b57cec5SDimitry Andric /// at the beginning of the basic block.
4010b57cec5SDimitry Andric void AArch64RegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB,
4020b57cec5SDimitry Andric                                                        unsigned BaseReg,
4030b57cec5SDimitry Andric                                                        int FrameIdx,
4040b57cec5SDimitry Andric                                                        int64_t Offset) const {
4050b57cec5SDimitry Andric   MachineBasicBlock::iterator Ins = MBB->begin();
4060b57cec5SDimitry Andric   DebugLoc DL; // Defaults to "unknown"
4070b57cec5SDimitry Andric   if (Ins != MBB->end())
4080b57cec5SDimitry Andric     DL = Ins->getDebugLoc();
4090b57cec5SDimitry Andric   const MachineFunction &MF = *MBB->getParent();
4100b57cec5SDimitry Andric   const AArch64InstrInfo *TII =
4110b57cec5SDimitry Andric       MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
4120b57cec5SDimitry Andric   const MCInstrDesc &MCID = TII->get(AArch64::ADDXri);
4130b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
4140b57cec5SDimitry Andric   MRI.constrainRegClass(BaseReg, TII->getRegClass(MCID, 0, this, MF));
4150b57cec5SDimitry Andric   unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0);
4160b57cec5SDimitry Andric 
4170b57cec5SDimitry Andric   BuildMI(*MBB, Ins, DL, MCID, BaseReg)
4180b57cec5SDimitry Andric       .addFrameIndex(FrameIdx)
4190b57cec5SDimitry Andric       .addImm(Offset)
4200b57cec5SDimitry Andric       .addImm(Shifter);
4210b57cec5SDimitry Andric }
4220b57cec5SDimitry Andric 
4230b57cec5SDimitry Andric void AArch64RegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg,
4240b57cec5SDimitry Andric                                             int64_t Offset) const {
425*8bcb0991SDimitry Andric   // ARM doesn't need the general 64-bit offsets
426*8bcb0991SDimitry Andric   StackOffset Off(Offset, MVT::i8);
427*8bcb0991SDimitry Andric 
4280b57cec5SDimitry Andric   unsigned i = 0;
4290b57cec5SDimitry Andric 
4300b57cec5SDimitry Andric   while (!MI.getOperand(i).isFI()) {
4310b57cec5SDimitry Andric     ++i;
4320b57cec5SDimitry Andric     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
4330b57cec5SDimitry Andric   }
4340b57cec5SDimitry Andric   const MachineFunction *MF = MI.getParent()->getParent();
4350b57cec5SDimitry Andric   const AArch64InstrInfo *TII =
4360b57cec5SDimitry Andric       MF->getSubtarget<AArch64Subtarget>().getInstrInfo();
4370b57cec5SDimitry Andric   bool Done = rewriteAArch64FrameIndex(MI, i, BaseReg, Off, TII);
4380b57cec5SDimitry Andric   assert(Done && "Unable to resolve frame index!");
4390b57cec5SDimitry Andric   (void)Done;
4400b57cec5SDimitry Andric }
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric void AArch64RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
4430b57cec5SDimitry Andric                                               int SPAdj, unsigned FIOperandNum,
4440b57cec5SDimitry Andric                                               RegScavenger *RS) const {
4450b57cec5SDimitry Andric   assert(SPAdj == 0 && "Unexpected");
4460b57cec5SDimitry Andric 
4470b57cec5SDimitry Andric   MachineInstr &MI = *II;
4480b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
4490b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
450*8bcb0991SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
4510b57cec5SDimitry Andric   const AArch64InstrInfo *TII =
4520b57cec5SDimitry Andric       MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
4530b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
4540b57cec5SDimitry Andric 
4550b57cec5SDimitry Andric   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
456*8bcb0991SDimitry Andric   bool Tagged =
457*8bcb0991SDimitry Andric       MI.getOperand(FIOperandNum).getTargetFlags() & AArch64II::MO_TAGGED;
4580b57cec5SDimitry Andric   unsigned FrameReg;
4590b57cec5SDimitry Andric 
4600b57cec5SDimitry Andric   // Special handling of dbg_value, stackmap and patchpoint instructions.
4610b57cec5SDimitry Andric   if (MI.isDebugValue() || MI.getOpcode() == TargetOpcode::STACKMAP ||
4620b57cec5SDimitry Andric       MI.getOpcode() == TargetOpcode::PATCHPOINT) {
463*8bcb0991SDimitry Andric     StackOffset Offset =
464*8bcb0991SDimitry Andric         TFI->resolveFrameIndexReference(MF, FrameIndex, FrameReg,
4650b57cec5SDimitry Andric                                         /*PreferFP=*/true,
4660b57cec5SDimitry Andric                                         /*ForSimm=*/false);
467*8bcb0991SDimitry Andric     Offset += StackOffset(MI.getOperand(FIOperandNum + 1).getImm(), MVT::i8);
4680b57cec5SDimitry Andric     MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false /*isDef*/);
469*8bcb0991SDimitry Andric     MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset.getBytes());
4700b57cec5SDimitry Andric     return;
4710b57cec5SDimitry Andric   }
4720b57cec5SDimitry Andric 
4730b57cec5SDimitry Andric   if (MI.getOpcode() == TargetOpcode::LOCAL_ESCAPE) {
4740b57cec5SDimitry Andric     MachineOperand &FI = MI.getOperand(FIOperandNum);
475*8bcb0991SDimitry Andric     int Offset = TFI->getNonLocalFrameIndexReference(MF, FrameIndex);
4760b57cec5SDimitry Andric     FI.ChangeToImmediate(Offset);
4770b57cec5SDimitry Andric     return;
4780b57cec5SDimitry Andric   }
4790b57cec5SDimitry Andric 
480*8bcb0991SDimitry Andric   StackOffset Offset;
4810b57cec5SDimitry Andric   if (MI.getOpcode() == AArch64::TAGPstack) {
4820b57cec5SDimitry Andric     // TAGPstack must use the virtual frame register in its 3rd operand.
4830b57cec5SDimitry Andric     const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
4840b57cec5SDimitry Andric     FrameReg = MI.getOperand(3).getReg();
485*8bcb0991SDimitry Andric     Offset = {MFI.getObjectOffset(FrameIndex) +
486*8bcb0991SDimitry Andric                   AFI->getTaggedBasePointerOffset(),
487*8bcb0991SDimitry Andric               MVT::i8};
488*8bcb0991SDimitry Andric   } else if (Tagged) {
489*8bcb0991SDimitry Andric     StackOffset SPOffset = {
490*8bcb0991SDimitry Andric         MFI.getObjectOffset(FrameIndex) + (int64_t)MFI.getStackSize(), MVT::i8};
491*8bcb0991SDimitry Andric     if (MFI.hasVarSizedObjects() ||
492*8bcb0991SDimitry Andric         isAArch64FrameOffsetLegal(MI, SPOffset, nullptr, nullptr, nullptr) !=
493*8bcb0991SDimitry Andric             (AArch64FrameOffsetCanUpdate | AArch64FrameOffsetIsLegal)) {
494*8bcb0991SDimitry Andric       // Can't update to SP + offset in place. Precalculate the tagged pointer
495*8bcb0991SDimitry Andric       // in a scratch register.
496*8bcb0991SDimitry Andric       Offset = TFI->resolveFrameIndexReference(
497*8bcb0991SDimitry Andric           MF, FrameIndex, FrameReg, /*PreferFP=*/false, /*ForSimm=*/true);
498*8bcb0991SDimitry Andric       Register ScratchReg =
499*8bcb0991SDimitry Andric           MF.getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass);
500*8bcb0991SDimitry Andric       emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset,
501*8bcb0991SDimitry Andric                       TII);
502*8bcb0991SDimitry Andric       BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(AArch64::LDG), ScratchReg)
503*8bcb0991SDimitry Andric           .addReg(ScratchReg)
504*8bcb0991SDimitry Andric           .addReg(ScratchReg)
505*8bcb0991SDimitry Andric           .addImm(0);
506*8bcb0991SDimitry Andric       MI.getOperand(FIOperandNum)
507*8bcb0991SDimitry Andric           .ChangeToRegister(ScratchReg, false, false, true);
508*8bcb0991SDimitry Andric       return;
509*8bcb0991SDimitry Andric     }
510*8bcb0991SDimitry Andric     FrameReg = AArch64::SP;
511*8bcb0991SDimitry Andric     Offset = {MFI.getObjectOffset(FrameIndex) + (int64_t)MFI.getStackSize(),
512*8bcb0991SDimitry Andric               MVT::i8};
5130b57cec5SDimitry Andric   } else {
5140b57cec5SDimitry Andric     Offset = TFI->resolveFrameIndexReference(
5150b57cec5SDimitry Andric         MF, FrameIndex, FrameReg, /*PreferFP=*/false, /*ForSimm=*/true);
5160b57cec5SDimitry Andric   }
5170b57cec5SDimitry Andric 
5180b57cec5SDimitry Andric   // Modify MI as necessary to handle as much of 'Offset' as possible
5190b57cec5SDimitry Andric   if (rewriteAArch64FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII))
5200b57cec5SDimitry Andric     return;
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric   assert((!RS || !RS->isScavengingFrameIndex(FrameIndex)) &&
5230b57cec5SDimitry Andric          "Emergency spill slot is out of reach");
5240b57cec5SDimitry Andric 
5250b57cec5SDimitry Andric   // If we get here, the immediate doesn't fit into the instruction.  We folded
5260b57cec5SDimitry Andric   // as much as possible above.  Handle the rest, providing a register that is
5270b57cec5SDimitry Andric   // SP+LargeImm.
528*8bcb0991SDimitry Andric   Register ScratchReg =
5290b57cec5SDimitry Andric       MF.getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass);
5300b57cec5SDimitry Andric   emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset, TII);
5310b57cec5SDimitry Andric   MI.getOperand(FIOperandNum).ChangeToRegister(ScratchReg, false, false, true);
5320b57cec5SDimitry Andric }
5330b57cec5SDimitry Andric 
5340b57cec5SDimitry Andric unsigned AArch64RegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
5350b57cec5SDimitry Andric                                                   MachineFunction &MF) const {
5360b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
5370b57cec5SDimitry Andric 
5380b57cec5SDimitry Andric   switch (RC->getID()) {
5390b57cec5SDimitry Andric   default:
5400b57cec5SDimitry Andric     return 0;
5410b57cec5SDimitry Andric   case AArch64::GPR32RegClassID:
5420b57cec5SDimitry Andric   case AArch64::GPR32spRegClassID:
5430b57cec5SDimitry Andric   case AArch64::GPR32allRegClassID:
5440b57cec5SDimitry Andric   case AArch64::GPR64spRegClassID:
5450b57cec5SDimitry Andric   case AArch64::GPR64allRegClassID:
5460b57cec5SDimitry Andric   case AArch64::GPR64RegClassID:
5470b57cec5SDimitry Andric   case AArch64::GPR32commonRegClassID:
5480b57cec5SDimitry Andric   case AArch64::GPR64commonRegClassID:
5490b57cec5SDimitry Andric     return 32 - 1                                   // XZR/SP
5500b57cec5SDimitry Andric               - (TFI->hasFP(MF) || TT.isOSDarwin()) // FP
5510b57cec5SDimitry Andric               - MF.getSubtarget<AArch64Subtarget>().getNumXRegisterReserved()
5520b57cec5SDimitry Andric               - hasBasePointer(MF);  // X19
5530b57cec5SDimitry Andric   case AArch64::FPR8RegClassID:
5540b57cec5SDimitry Andric   case AArch64::FPR16RegClassID:
5550b57cec5SDimitry Andric   case AArch64::FPR32RegClassID:
5560b57cec5SDimitry Andric   case AArch64::FPR64RegClassID:
5570b57cec5SDimitry Andric   case AArch64::FPR128RegClassID:
5580b57cec5SDimitry Andric     return 32;
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric   case AArch64::DDRegClassID:
5610b57cec5SDimitry Andric   case AArch64::DDDRegClassID:
5620b57cec5SDimitry Andric   case AArch64::DDDDRegClassID:
5630b57cec5SDimitry Andric   case AArch64::QQRegClassID:
5640b57cec5SDimitry Andric   case AArch64::QQQRegClassID:
5650b57cec5SDimitry Andric   case AArch64::QQQQRegClassID:
5660b57cec5SDimitry Andric     return 32;
5670b57cec5SDimitry Andric 
5680b57cec5SDimitry Andric   case AArch64::FPR128_loRegClassID:
5690b57cec5SDimitry Andric     return 16;
5700b57cec5SDimitry Andric   }
5710b57cec5SDimitry Andric }
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric unsigned AArch64RegisterInfo::getLocalAddressRegister(
5740b57cec5SDimitry Andric   const MachineFunction &MF) const {
5750b57cec5SDimitry Andric   const auto &MFI = MF.getFrameInfo();
5760b57cec5SDimitry Andric   if (!MF.hasEHFunclets() && !MFI.hasVarSizedObjects())
5770b57cec5SDimitry Andric     return AArch64::SP;
5780b57cec5SDimitry Andric   else if (needsStackRealignment(MF))
5790b57cec5SDimitry Andric     return getBaseRegister();
5800b57cec5SDimitry Andric   return getFrameRegister(MF);
5810b57cec5SDimitry Andric }
582