xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AArch64/AArch64RegisterInfo.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
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"
180b57cec5SDimitry Andric #include "AArch64Subtarget.h"
190b57cec5SDimitry Andric #include "MCTargetDesc/AArch64AddressingModes.h"
200b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
210b57cec5SDimitry Andric #include "llvm/ADT/Triple.h"
22*81ad6265SDimitry Andric #include "llvm/BinaryFormat/Dwarf.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"
28e8d8bef9SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
298bcb0991SDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
308bcb0991SDimitry Andric #include "llvm/IR/Function.h"
318bcb0991SDimitry Andric #include "llvm/Support/raw_ostream.h"
320b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric using namespace llvm;
350b57cec5SDimitry Andric 
36*81ad6265SDimitry Andric #define GET_CC_REGISTER_LISTS
37*81ad6265SDimitry Andric #include "AArch64GenCallingConv.inc"
380b57cec5SDimitry Andric #define GET_REGINFO_TARGET_DESC
390b57cec5SDimitry Andric #include "AArch64GenRegisterInfo.inc"
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric AArch64RegisterInfo::AArch64RegisterInfo(const Triple &TT)
420b57cec5SDimitry Andric     : AArch64GenRegisterInfo(AArch64::LR), TT(TT) {
430b57cec5SDimitry Andric   AArch64_MC::initLLVMToCVRegMapping(this);
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric 
4675b4d546SDimitry Andric /// Return whether the register needs a CFI entry. Not all unwinders may know
4775b4d546SDimitry Andric /// about SVE registers, so we assume the lowest common denominator, i.e. the
4875b4d546SDimitry Andric /// callee-saves required by the base ABI. For the SVE registers z8-z15 only the
4975b4d546SDimitry Andric /// lower 64-bits (d8-d15) need to be saved. The lower 64-bits subreg is
5075b4d546SDimitry Andric /// returned in \p RegToUseForCFI.
5175b4d546SDimitry Andric bool AArch64RegisterInfo::regNeedsCFI(unsigned Reg,
5275b4d546SDimitry Andric                                       unsigned &RegToUseForCFI) const {
5375b4d546SDimitry Andric   if (AArch64::PPRRegClass.contains(Reg))
5475b4d546SDimitry Andric     return false;
5575b4d546SDimitry Andric 
5675b4d546SDimitry Andric   if (AArch64::ZPRRegClass.contains(Reg)) {
5775b4d546SDimitry Andric     RegToUseForCFI = getSubReg(Reg, AArch64::dsub);
5875b4d546SDimitry Andric     for (int I = 0; CSR_AArch64_AAPCS_SaveList[I]; ++I) {
5975b4d546SDimitry Andric       if (CSR_AArch64_AAPCS_SaveList[I] == RegToUseForCFI)
6075b4d546SDimitry Andric         return true;
6175b4d546SDimitry Andric     }
6275b4d546SDimitry Andric     return false;
6375b4d546SDimitry Andric   }
6475b4d546SDimitry Andric 
6575b4d546SDimitry Andric   RegToUseForCFI = Reg;
6675b4d546SDimitry Andric   return true;
6775b4d546SDimitry Andric }
6875b4d546SDimitry Andric 
690b57cec5SDimitry Andric const MCPhysReg *
700b57cec5SDimitry Andric AArch64RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
710b57cec5SDimitry Andric   assert(MF && "Invalid MachineFunction pointer.");
725ffd83dbSDimitry Andric 
730b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::GHC)
740b57cec5SDimitry Andric     // GHC set of callee saved regs is empty as all those regs are
750b57cec5SDimitry Andric     // used for passing STG regs around
760b57cec5SDimitry Andric     return CSR_AArch64_NoRegs_SaveList;
770b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AnyReg)
780b57cec5SDimitry Andric     return CSR_AArch64_AllRegs_SaveList;
795ffd83dbSDimitry Andric 
805ffd83dbSDimitry Andric   // Darwin has its own CSR_AArch64_AAPCS_SaveList, which means most CSR save
815ffd83dbSDimitry Andric   // lists depending on that will need to have their Darwin variant as well.
825ffd83dbSDimitry Andric   if (MF->getSubtarget<AArch64Subtarget>().isTargetDarwin())
835ffd83dbSDimitry Andric     return getDarwinCalleeSavedRegs(MF);
845ffd83dbSDimitry Andric 
855ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::CFGuard_Check)
865ffd83dbSDimitry Andric     return CSR_Win_AArch64_CFGuard_Check_SaveList;
875ffd83dbSDimitry Andric   if (MF->getSubtarget<AArch64Subtarget>().isTargetWindows())
885ffd83dbSDimitry Andric     return CSR_Win_AArch64_AAPCS_SaveList;
890b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AArch64_VectorCall)
900b57cec5SDimitry Andric     return CSR_AArch64_AAVPCS_SaveList;
91480093f4SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AArch64_SVE_VectorCall)
92480093f4SDimitry Andric     return CSR_AArch64_SVE_AAPCS_SaveList;
930b57cec5SDimitry Andric   if (MF->getSubtarget<AArch64Subtarget>().getTargetLowering()
940b57cec5SDimitry Andric           ->supportSwiftError() &&
950b57cec5SDimitry Andric       MF->getFunction().getAttributes().hasAttrSomewhere(
960b57cec5SDimitry Andric           Attribute::SwiftError))
970b57cec5SDimitry Andric     return CSR_AArch64_AAPCS_SwiftError_SaveList;
98fe6060f1SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::SwiftTail)
99fe6060f1SDimitry Andric     return CSR_AArch64_AAPCS_SwiftTail_SaveList;
1000b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::PreserveMost)
1010b57cec5SDimitry Andric     return CSR_AArch64_RT_MostRegs_SaveList;
1025ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::Win64)
1035ffd83dbSDimitry Andric     // This is for OSes other than Windows; Windows is a separate case further
1045ffd83dbSDimitry Andric     // above.
1055ffd83dbSDimitry Andric     return CSR_AArch64_AAPCS_X18_SaveList;
106*81ad6265SDimitry Andric   if (MF->getInfo<AArch64FunctionInfo>()->isSVECC())
107979e22ffSDimitry Andric     return CSR_AArch64_SVE_AAPCS_SaveList;
1080b57cec5SDimitry Andric   return CSR_AArch64_AAPCS_SaveList;
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
1115ffd83dbSDimitry Andric const MCPhysReg *
1125ffd83dbSDimitry Andric AArch64RegisterInfo::getDarwinCalleeSavedRegs(const MachineFunction *MF) const {
1135ffd83dbSDimitry Andric   assert(MF && "Invalid MachineFunction pointer.");
1145ffd83dbSDimitry Andric   assert(MF->getSubtarget<AArch64Subtarget>().isTargetDarwin() &&
1155ffd83dbSDimitry Andric          "Invalid subtarget for getDarwinCalleeSavedRegs");
1165ffd83dbSDimitry Andric 
1175ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::CFGuard_Check)
1185ffd83dbSDimitry Andric     report_fatal_error(
1195ffd83dbSDimitry Andric         "Calling convention CFGuard_Check is unsupported on Darwin.");
1205ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AArch64_VectorCall)
1215ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_AAVPCS_SaveList;
1225ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AArch64_SVE_VectorCall)
1235ffd83dbSDimitry Andric     report_fatal_error(
1245ffd83dbSDimitry Andric         "Calling convention SVE_VectorCall is unsupported on Darwin.");
1255ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS)
1265ffd83dbSDimitry Andric     return MF->getInfo<AArch64FunctionInfo>()->isSplitCSR()
1275ffd83dbSDimitry Andric                ? CSR_Darwin_AArch64_CXX_TLS_PE_SaveList
1285ffd83dbSDimitry Andric                : CSR_Darwin_AArch64_CXX_TLS_SaveList;
1295ffd83dbSDimitry Andric   if (MF->getSubtarget<AArch64Subtarget>().getTargetLowering()
1305ffd83dbSDimitry Andric           ->supportSwiftError() &&
1315ffd83dbSDimitry Andric       MF->getFunction().getAttributes().hasAttrSomewhere(
1325ffd83dbSDimitry Andric           Attribute::SwiftError))
1335ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_AAPCS_SwiftError_SaveList;
134fe6060f1SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::SwiftTail)
135fe6060f1SDimitry Andric     return CSR_Darwin_AArch64_AAPCS_SwiftTail_SaveList;
1365ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::PreserveMost)
1375ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_RT_MostRegs_SaveList;
1385ffd83dbSDimitry Andric   return CSR_Darwin_AArch64_AAPCS_SaveList;
1395ffd83dbSDimitry Andric }
1405ffd83dbSDimitry Andric 
1410b57cec5SDimitry Andric const MCPhysReg *AArch64RegisterInfo::getCalleeSavedRegsViaCopy(
1420b57cec5SDimitry Andric     const MachineFunction *MF) const {
1430b57cec5SDimitry Andric   assert(MF && "Invalid MachineFunction pointer.");
1440b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS &&
1450b57cec5SDimitry Andric       MF->getInfo<AArch64FunctionInfo>()->isSplitCSR())
1465ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_CXX_TLS_ViaCopy_SaveList;
1470b57cec5SDimitry Andric   return nullptr;
1480b57cec5SDimitry Andric }
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric void AArch64RegisterInfo::UpdateCustomCalleeSavedRegs(
1510b57cec5SDimitry Andric     MachineFunction &MF) const {
1520b57cec5SDimitry Andric   const MCPhysReg *CSRs = getCalleeSavedRegs(&MF);
1530b57cec5SDimitry Andric   SmallVector<MCPhysReg, 32> UpdatedCSRs;
1540b57cec5SDimitry Andric   for (const MCPhysReg *I = CSRs; *I; ++I)
1550b57cec5SDimitry Andric     UpdatedCSRs.push_back(*I);
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric   for (size_t i = 0; i < AArch64::GPR64commonRegClass.getNumRegs(); ++i) {
1580b57cec5SDimitry Andric     if (MF.getSubtarget<AArch64Subtarget>().isXRegCustomCalleeSaved(i)) {
1590b57cec5SDimitry Andric       UpdatedCSRs.push_back(AArch64::GPR64commonRegClass.getRegister(i));
1600b57cec5SDimitry Andric     }
1610b57cec5SDimitry Andric   }
1620b57cec5SDimitry Andric   // Register lists are zero-terminated.
1630b57cec5SDimitry Andric   UpdatedCSRs.push_back(0);
1640b57cec5SDimitry Andric   MF.getRegInfo().setCalleeSavedRegs(UpdatedCSRs);
1650b57cec5SDimitry Andric }
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric const TargetRegisterClass *
1680b57cec5SDimitry Andric AArch64RegisterInfo::getSubClassWithSubReg(const TargetRegisterClass *RC,
1690b57cec5SDimitry Andric                                        unsigned Idx) const {
1700b57cec5SDimitry Andric   // edge case for GPR/FPR register classes
1710b57cec5SDimitry Andric   if (RC == &AArch64::GPR32allRegClass && Idx == AArch64::hsub)
1720b57cec5SDimitry Andric     return &AArch64::FPR32RegClass;
1730b57cec5SDimitry Andric   else if (RC == &AArch64::GPR64allRegClass && Idx == AArch64::hsub)
1740b57cec5SDimitry Andric     return &AArch64::FPR64RegClass;
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric   // Forward to TableGen's default version.
1770b57cec5SDimitry Andric   return AArch64GenRegisterInfo::getSubClassWithSubReg(RC, Idx);
1780b57cec5SDimitry Andric }
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric const uint32_t *
1815ffd83dbSDimitry Andric AArch64RegisterInfo::getDarwinCallPreservedMask(const MachineFunction &MF,
1825ffd83dbSDimitry Andric                                                 CallingConv::ID CC) const {
1835ffd83dbSDimitry Andric   assert(MF.getSubtarget<AArch64Subtarget>().isTargetDarwin() &&
1845ffd83dbSDimitry Andric          "Invalid subtarget for getDarwinCallPreservedMask");
1855ffd83dbSDimitry Andric 
1865ffd83dbSDimitry Andric   if (CC == CallingConv::CXX_FAST_TLS)
1875ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_CXX_TLS_RegMask;
1885ffd83dbSDimitry Andric   if (CC == CallingConv::AArch64_VectorCall)
1895ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_AAVPCS_RegMask;
1905ffd83dbSDimitry Andric   if (CC == CallingConv::AArch64_SVE_VectorCall)
1915ffd83dbSDimitry Andric     report_fatal_error(
1925ffd83dbSDimitry Andric         "Calling convention SVE_VectorCall is unsupported on Darwin.");
1935ffd83dbSDimitry Andric   if (CC == CallingConv::CFGuard_Check)
1945ffd83dbSDimitry Andric     report_fatal_error(
1955ffd83dbSDimitry Andric         "Calling convention CFGuard_Check is unsupported on Darwin.");
1965ffd83dbSDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>()
1975ffd83dbSDimitry Andric           .getTargetLowering()
1985ffd83dbSDimitry Andric           ->supportSwiftError() &&
1995ffd83dbSDimitry Andric       MF.getFunction().getAttributes().hasAttrSomewhere(Attribute::SwiftError))
2005ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_AAPCS_SwiftError_RegMask;
201fe6060f1SDimitry Andric   if (CC == CallingConv::SwiftTail)
202fe6060f1SDimitry Andric     return CSR_Darwin_AArch64_AAPCS_SwiftTail_RegMask;
2035ffd83dbSDimitry Andric   if (CC == CallingConv::PreserveMost)
2045ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_RT_MostRegs_RegMask;
2055ffd83dbSDimitry Andric   return CSR_Darwin_AArch64_AAPCS_RegMask;
2065ffd83dbSDimitry Andric }
2075ffd83dbSDimitry Andric 
2085ffd83dbSDimitry Andric const uint32_t *
2090b57cec5SDimitry Andric AArch64RegisterInfo::getCallPreservedMask(const MachineFunction &MF,
2100b57cec5SDimitry Andric                                           CallingConv::ID CC) const {
2110b57cec5SDimitry Andric   bool SCS = MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack);
2120b57cec5SDimitry Andric   if (CC == CallingConv::GHC)
2130b57cec5SDimitry Andric     // This is academic because all GHC calls are (supposed to be) tail calls
2140b57cec5SDimitry Andric     return SCS ? CSR_AArch64_NoRegs_SCS_RegMask : CSR_AArch64_NoRegs_RegMask;
2150b57cec5SDimitry Andric   if (CC == CallingConv::AnyReg)
2160b57cec5SDimitry Andric     return SCS ? CSR_AArch64_AllRegs_SCS_RegMask : CSR_AArch64_AllRegs_RegMask;
2175ffd83dbSDimitry Andric 
2185ffd83dbSDimitry Andric   // All the following calling conventions are handled differently on Darwin.
2195ffd83dbSDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>().isTargetDarwin()) {
2205ffd83dbSDimitry Andric     if (SCS)
2215ffd83dbSDimitry Andric       report_fatal_error("ShadowCallStack attribute not supported on Darwin.");
2225ffd83dbSDimitry Andric     return getDarwinCallPreservedMask(MF, CC);
2235ffd83dbSDimitry Andric   }
2245ffd83dbSDimitry Andric 
2250b57cec5SDimitry Andric   if (CC == CallingConv::AArch64_VectorCall)
2260b57cec5SDimitry Andric     return SCS ? CSR_AArch64_AAVPCS_SCS_RegMask : CSR_AArch64_AAVPCS_RegMask;
2278bcb0991SDimitry Andric   if (CC == CallingConv::AArch64_SVE_VectorCall)
228480093f4SDimitry Andric     return SCS ? CSR_AArch64_SVE_AAPCS_SCS_RegMask
229480093f4SDimitry Andric                : CSR_AArch64_SVE_AAPCS_RegMask;
230480093f4SDimitry Andric   if (CC == CallingConv::CFGuard_Check)
231480093f4SDimitry Andric     return CSR_Win_AArch64_CFGuard_Check_RegMask;
2320b57cec5SDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>().getTargetLowering()
2330b57cec5SDimitry Andric           ->supportSwiftError() &&
2340b57cec5SDimitry Andric       MF.getFunction().getAttributes().hasAttrSomewhere(Attribute::SwiftError))
2350b57cec5SDimitry Andric     return SCS ? CSR_AArch64_AAPCS_SwiftError_SCS_RegMask
2360b57cec5SDimitry Andric                : CSR_AArch64_AAPCS_SwiftError_RegMask;
237fe6060f1SDimitry Andric   if (CC == CallingConv::SwiftTail) {
238fe6060f1SDimitry Andric     if (SCS)
239fe6060f1SDimitry Andric       report_fatal_error("ShadowCallStack attribute not supported with swifttail");
240fe6060f1SDimitry Andric     return CSR_AArch64_AAPCS_SwiftTail_RegMask;
241fe6060f1SDimitry Andric   }
2420b57cec5SDimitry Andric   if (CC == CallingConv::PreserveMost)
2430b57cec5SDimitry Andric     return SCS ? CSR_AArch64_RT_MostRegs_SCS_RegMask
2440b57cec5SDimitry Andric                : CSR_AArch64_RT_MostRegs_RegMask;
2450b57cec5SDimitry Andric   else
2460b57cec5SDimitry Andric     return SCS ? CSR_AArch64_AAPCS_SCS_RegMask : CSR_AArch64_AAPCS_RegMask;
2470b57cec5SDimitry Andric }
2480b57cec5SDimitry Andric 
249e8d8bef9SDimitry Andric const uint32_t *AArch64RegisterInfo::getCustomEHPadPreservedMask(
250e8d8bef9SDimitry Andric     const MachineFunction &MF) const {
251e8d8bef9SDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>().isTargetLinux())
252e8d8bef9SDimitry Andric     return CSR_AArch64_AAPCS_RegMask;
253e8d8bef9SDimitry Andric 
254e8d8bef9SDimitry Andric   return nullptr;
255e8d8bef9SDimitry Andric }
256e8d8bef9SDimitry Andric 
2570b57cec5SDimitry Andric const uint32_t *AArch64RegisterInfo::getTLSCallPreservedMask() const {
2580b57cec5SDimitry Andric   if (TT.isOSDarwin())
2595ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_TLS_RegMask;
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric   assert(TT.isOSBinFormatELF() && "Invalid target");
2620b57cec5SDimitry Andric   return CSR_AArch64_TLS_ELF_RegMask;
2630b57cec5SDimitry Andric }
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric void AArch64RegisterInfo::UpdateCustomCallPreservedMask(MachineFunction &MF,
2660b57cec5SDimitry Andric                                                  const uint32_t **Mask) const {
2670b57cec5SDimitry Andric   uint32_t *UpdatedMask = MF.allocateRegMask();
2680b57cec5SDimitry Andric   unsigned RegMaskSize = MachineOperand::getRegMaskSize(getNumRegs());
2690b57cec5SDimitry Andric   memcpy(UpdatedMask, *Mask, sizeof(UpdatedMask[0]) * RegMaskSize);
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric   for (size_t i = 0; i < AArch64::GPR64commonRegClass.getNumRegs(); ++i) {
2720b57cec5SDimitry Andric     if (MF.getSubtarget<AArch64Subtarget>().isXRegCustomCalleeSaved(i)) {
2730b57cec5SDimitry Andric       for (MCSubRegIterator SubReg(AArch64::GPR64commonRegClass.getRegister(i),
2740b57cec5SDimitry Andric                                    this, true);
2750b57cec5SDimitry Andric            SubReg.isValid(); ++SubReg) {
2760b57cec5SDimitry Andric         // See TargetRegisterInfo::getCallPreservedMask for how to interpret the
2770b57cec5SDimitry Andric         // register mask.
2780b57cec5SDimitry Andric         UpdatedMask[*SubReg / 32] |= 1u << (*SubReg % 32);
2790b57cec5SDimitry Andric       }
2800b57cec5SDimitry Andric     }
2810b57cec5SDimitry Andric   }
2820b57cec5SDimitry Andric   *Mask = UpdatedMask;
2830b57cec5SDimitry Andric }
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric const uint32_t *AArch64RegisterInfo::getNoPreservedMask() const {
2860b57cec5SDimitry Andric   return CSR_AArch64_NoRegs_RegMask;
2870b57cec5SDimitry Andric }
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric const uint32_t *
2900b57cec5SDimitry Andric AArch64RegisterInfo::getThisReturnPreservedMask(const MachineFunction &MF,
2910b57cec5SDimitry Andric                                                 CallingConv::ID CC) const {
2920b57cec5SDimitry Andric   // This should return a register mask that is the same as that returned by
2930b57cec5SDimitry Andric   // getCallPreservedMask but that additionally preserves the register used for
2940b57cec5SDimitry Andric   // the first i64 argument (which must also be the register used to return a
2950b57cec5SDimitry Andric   // single i64 return value)
2960b57cec5SDimitry Andric   //
2970b57cec5SDimitry Andric   // In case that the calling convention does not use the same register for
2980b57cec5SDimitry Andric   // both, the function should return NULL (does not currently apply)
2990b57cec5SDimitry Andric   assert(CC != CallingConv::GHC && "should not be GHC calling convention.");
3005ffd83dbSDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>().isTargetDarwin())
3015ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_AAPCS_ThisReturn_RegMask;
3020b57cec5SDimitry Andric   return CSR_AArch64_AAPCS_ThisReturn_RegMask;
3030b57cec5SDimitry Andric }
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric const uint32_t *AArch64RegisterInfo::getWindowsStackProbePreservedMask() const {
3060b57cec5SDimitry Andric   return CSR_AArch64_StackProbe_Windows_RegMask;
3070b57cec5SDimitry Andric }
3080b57cec5SDimitry Andric 
3090b57cec5SDimitry Andric BitVector
3100b57cec5SDimitry Andric AArch64RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
3110b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric   // FIXME: avoid re-calculating this every time.
3140b57cec5SDimitry Andric   BitVector Reserved(getNumRegs());
3150b57cec5SDimitry Andric   markSuperRegs(Reserved, AArch64::WSP);
3160b57cec5SDimitry Andric   markSuperRegs(Reserved, AArch64::WZR);
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric   if (TFI->hasFP(MF) || TT.isOSDarwin())
3190b57cec5SDimitry Andric     markSuperRegs(Reserved, AArch64::W29);
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric   for (size_t i = 0; i < AArch64::GPR32commonRegClass.getNumRegs(); ++i) {
3220b57cec5SDimitry Andric     if (MF.getSubtarget<AArch64Subtarget>().isXRegisterReserved(i))
3230b57cec5SDimitry Andric       markSuperRegs(Reserved, AArch64::GPR32commonRegClass.getRegister(i));
3240b57cec5SDimitry Andric   }
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric   if (hasBasePointer(MF))
3270b57cec5SDimitry Andric     markSuperRegs(Reserved, AArch64::W19);
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric   // SLH uses register W16/X16 as the taint register.
3300b57cec5SDimitry Andric   if (MF.getFunction().hasFnAttribute(Attribute::SpeculativeLoadHardening))
3310b57cec5SDimitry Andric     markSuperRegs(Reserved, AArch64::W16);
3320b57cec5SDimitry Andric 
333*81ad6265SDimitry Andric   // SME tiles are not allocatable.
334*81ad6265SDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>().hasSME()) {
335*81ad6265SDimitry Andric     for (MCSubRegIterator SubReg(AArch64::ZA, this, /*self=*/true);
336*81ad6265SDimitry Andric          SubReg.isValid(); ++SubReg)
337*81ad6265SDimitry Andric       Reserved.set(*SubReg);
338*81ad6265SDimitry Andric   }
339*81ad6265SDimitry Andric 
3400b57cec5SDimitry Andric   assert(checkAllSuperRegsMarked(Reserved));
3410b57cec5SDimitry Andric   return Reserved;
3420b57cec5SDimitry Andric }
3430b57cec5SDimitry Andric 
3440b57cec5SDimitry Andric bool AArch64RegisterInfo::isReservedReg(const MachineFunction &MF,
3455ffd83dbSDimitry Andric                                         MCRegister Reg) const {
3460b57cec5SDimitry Andric   return getReservedRegs(MF)[Reg];
3470b57cec5SDimitry Andric }
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric bool AArch64RegisterInfo::isAnyArgRegReserved(const MachineFunction &MF) const {
350e8d8bef9SDimitry Andric   return llvm::any_of(*AArch64::GPR64argRegClass.MC, [this, &MF](MCPhysReg r) {
351e8d8bef9SDimitry Andric     return isReservedReg(MF, r);
352e8d8bef9SDimitry Andric   });
3530b57cec5SDimitry Andric }
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric void AArch64RegisterInfo::emitReservedArgRegCallError(
3560b57cec5SDimitry Andric     const MachineFunction &MF) const {
3570b57cec5SDimitry Andric   const Function &F = MF.getFunction();
358e8d8bef9SDimitry Andric   F.getContext().diagnose(DiagnosticInfoUnsupported{F, ("AArch64 doesn't support"
359e8d8bef9SDimitry Andric     " function calls if any of the argument registers is reserved.")});
3600b57cec5SDimitry Andric }
3610b57cec5SDimitry Andric 
3620b57cec5SDimitry Andric bool AArch64RegisterInfo::isAsmClobberable(const MachineFunction &MF,
3635ffd83dbSDimitry Andric                                           MCRegister PhysReg) const {
3640b57cec5SDimitry Andric   return !isReservedReg(MF, PhysReg);
3650b57cec5SDimitry Andric }
3660b57cec5SDimitry Andric 
3675ffd83dbSDimitry Andric bool AArch64RegisterInfo::isConstantPhysReg(MCRegister PhysReg) const {
3680b57cec5SDimitry Andric   return PhysReg == AArch64::WZR || PhysReg == AArch64::XZR;
3690b57cec5SDimitry Andric }
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric const TargetRegisterClass *
3720b57cec5SDimitry Andric AArch64RegisterInfo::getPointerRegClass(const MachineFunction &MF,
3730b57cec5SDimitry Andric                                       unsigned Kind) const {
3740b57cec5SDimitry Andric   return &AArch64::GPR64spRegClass;
3750b57cec5SDimitry Andric }
3760b57cec5SDimitry Andric 
3770b57cec5SDimitry Andric const TargetRegisterClass *
3780b57cec5SDimitry Andric AArch64RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
3790b57cec5SDimitry Andric   if (RC == &AArch64::CCRRegClass)
3800b57cec5SDimitry Andric     return &AArch64::GPR64RegClass; // Only MSR & MRS copy NZCV.
3810b57cec5SDimitry Andric   return RC;
3820b57cec5SDimitry Andric }
3830b57cec5SDimitry Andric 
3840b57cec5SDimitry Andric unsigned AArch64RegisterInfo::getBaseRegister() const { return AArch64::X19; }
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric bool AArch64RegisterInfo::hasBasePointer(const MachineFunction &MF) const {
3870b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
3880b57cec5SDimitry Andric 
3890b57cec5SDimitry Andric   // In the presence of variable sized objects or funclets, if the fixed stack
3900b57cec5SDimitry Andric   // size is large enough that referencing from the FP won't result in things
3910b57cec5SDimitry Andric   // being in range relatively often, we can use a base pointer to allow access
3920b57cec5SDimitry Andric   // from the other direction like the SP normally works.
3930b57cec5SDimitry Andric   //
3940b57cec5SDimitry Andric   // Furthermore, if both variable sized objects are present, and the
3950b57cec5SDimitry Andric   // stack needs to be dynamically re-aligned, the base pointer is the only
3960b57cec5SDimitry Andric   // reliable way to reference the locals.
3970b57cec5SDimitry Andric   if (MFI.hasVarSizedObjects() || MF.hasEHFunclets()) {
398fe6060f1SDimitry Andric     if (hasStackRealignment(MF))
3990b57cec5SDimitry Andric       return true;
400979e22ffSDimitry Andric 
401979e22ffSDimitry Andric     if (MF.getSubtarget<AArch64Subtarget>().hasSVE()) {
402979e22ffSDimitry Andric       const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
403979e22ffSDimitry Andric       // Frames that have variable sized objects and scalable SVE objects,
404979e22ffSDimitry Andric       // should always use a basepointer.
405979e22ffSDimitry Andric       if (!AFI->hasCalculatedStackSizeSVE() || AFI->getStackSizeSVE())
406979e22ffSDimitry Andric         return true;
407979e22ffSDimitry Andric     }
408979e22ffSDimitry Andric 
4090b57cec5SDimitry Andric     // Conservatively estimate whether the negative offset from the frame
4100b57cec5SDimitry Andric     // pointer will be sufficient to reach. If a function has a smallish
4110b57cec5SDimitry Andric     // frame, it's less likely to have lots of spills and callee saved
4120b57cec5SDimitry Andric     // space, so it's all more likely to be within range of the frame pointer.
4130b57cec5SDimitry Andric     // If it's wrong, we'll materialize the constant and still get to the
4140b57cec5SDimitry Andric     // object; it's just suboptimal. Negative offsets use the unscaled
4150b57cec5SDimitry Andric     // load/store instructions, which have a 9-bit signed immediate.
4160b57cec5SDimitry Andric     return MFI.getLocalFrameSize() >= 256;
4170b57cec5SDimitry Andric   }
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric   return false;
4200b57cec5SDimitry Andric }
4210b57cec5SDimitry Andric 
422*81ad6265SDimitry Andric bool AArch64RegisterInfo::isArgumentRegister(const MachineFunction &MF,
423*81ad6265SDimitry Andric                                              MCRegister Reg) const {
424*81ad6265SDimitry Andric   CallingConv::ID CC = MF.getFunction().getCallingConv();
425*81ad6265SDimitry Andric   const AArch64Subtarget &STI = MF.getSubtarget<AArch64Subtarget>();
426*81ad6265SDimitry Andric   bool IsVarArg = STI.isCallingConvWin64(MF.getFunction().getCallingConv());
427*81ad6265SDimitry Andric 
428*81ad6265SDimitry Andric   auto HasReg = [](ArrayRef<MCRegister> RegList, MCRegister Reg) {
429*81ad6265SDimitry Andric     return llvm::any_of(RegList,
430*81ad6265SDimitry Andric                         [Reg](const MCRegister R) { return R == Reg; });
431*81ad6265SDimitry Andric   };
432*81ad6265SDimitry Andric 
433*81ad6265SDimitry Andric   switch (CC) {
434*81ad6265SDimitry Andric   default:
435*81ad6265SDimitry Andric     report_fatal_error("Unsupported calling convention.");
436*81ad6265SDimitry Andric   case CallingConv::WebKit_JS:
437*81ad6265SDimitry Andric     return HasReg(CC_AArch64_WebKit_JS_ArgRegs, Reg);
438*81ad6265SDimitry Andric   case CallingConv::GHC:
439*81ad6265SDimitry Andric     return HasReg(CC_AArch64_GHC_ArgRegs, Reg);
440*81ad6265SDimitry Andric   case CallingConv::C:
441*81ad6265SDimitry Andric   case CallingConv::Fast:
442*81ad6265SDimitry Andric   case CallingConv::PreserveMost:
443*81ad6265SDimitry Andric   case CallingConv::CXX_FAST_TLS:
444*81ad6265SDimitry Andric   case CallingConv::Swift:
445*81ad6265SDimitry Andric   case CallingConv::SwiftTail:
446*81ad6265SDimitry Andric   case CallingConv::Tail:
447*81ad6265SDimitry Andric     if (STI.isTargetWindows() && IsVarArg)
448*81ad6265SDimitry Andric       return HasReg(CC_AArch64_Win64_VarArg_ArgRegs, Reg);
449*81ad6265SDimitry Andric     if (!STI.isTargetDarwin()) {
450*81ad6265SDimitry Andric       switch (CC) {
451*81ad6265SDimitry Andric       default:
452*81ad6265SDimitry Andric         return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg);
453*81ad6265SDimitry Andric       case CallingConv::Swift:
454*81ad6265SDimitry Andric       case CallingConv::SwiftTail:
455*81ad6265SDimitry Andric         return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg) ||
456*81ad6265SDimitry Andric                HasReg(CC_AArch64_AAPCS_Swift_ArgRegs, Reg);
457*81ad6265SDimitry Andric       }
458*81ad6265SDimitry Andric     }
459*81ad6265SDimitry Andric     if (!IsVarArg) {
460*81ad6265SDimitry Andric       switch (CC) {
461*81ad6265SDimitry Andric       default:
462*81ad6265SDimitry Andric         return HasReg(CC_AArch64_DarwinPCS_ArgRegs, Reg);
463*81ad6265SDimitry Andric       case CallingConv::Swift:
464*81ad6265SDimitry Andric       case CallingConv::SwiftTail:
465*81ad6265SDimitry Andric         return HasReg(CC_AArch64_DarwinPCS_ArgRegs, Reg) ||
466*81ad6265SDimitry Andric                HasReg(CC_AArch64_DarwinPCS_Swift_ArgRegs, Reg);
467*81ad6265SDimitry Andric       }
468*81ad6265SDimitry Andric     }
469*81ad6265SDimitry Andric     if (STI.isTargetILP32())
470*81ad6265SDimitry Andric       return HasReg(CC_AArch64_DarwinPCS_ILP32_VarArg_ArgRegs, Reg);
471*81ad6265SDimitry Andric     return HasReg(CC_AArch64_DarwinPCS_VarArg_ArgRegs, Reg);
472*81ad6265SDimitry Andric   case CallingConv::Win64:
473*81ad6265SDimitry Andric     if (IsVarArg)
474*81ad6265SDimitry Andric       HasReg(CC_AArch64_Win64_VarArg_ArgRegs, Reg);
475*81ad6265SDimitry Andric     return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg);
476*81ad6265SDimitry Andric   case CallingConv::CFGuard_Check:
477*81ad6265SDimitry Andric     return HasReg(CC_AArch64_Win64_CFGuard_Check_ArgRegs, Reg);
478*81ad6265SDimitry Andric   case CallingConv::AArch64_VectorCall:
479*81ad6265SDimitry Andric   case CallingConv::AArch64_SVE_VectorCall:
480*81ad6265SDimitry Andric     return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg);
481*81ad6265SDimitry Andric   }
482*81ad6265SDimitry Andric }
483*81ad6265SDimitry Andric 
4840b57cec5SDimitry Andric Register
4850b57cec5SDimitry Andric AArch64RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
4860b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
4870b57cec5SDimitry Andric   return TFI->hasFP(MF) ? AArch64::FP : AArch64::SP;
4880b57cec5SDimitry Andric }
4890b57cec5SDimitry Andric 
4900b57cec5SDimitry Andric bool AArch64RegisterInfo::requiresRegisterScavenging(
4910b57cec5SDimitry Andric     const MachineFunction &MF) const {
4920b57cec5SDimitry Andric   return true;
4930b57cec5SDimitry Andric }
4940b57cec5SDimitry Andric 
4950b57cec5SDimitry Andric bool AArch64RegisterInfo::requiresVirtualBaseRegisters(
4960b57cec5SDimitry Andric     const MachineFunction &MF) const {
4970b57cec5SDimitry Andric   return true;
4980b57cec5SDimitry Andric }
4990b57cec5SDimitry Andric 
5000b57cec5SDimitry Andric bool
5010b57cec5SDimitry Andric AArch64RegisterInfo::useFPForScavengingIndex(const MachineFunction &MF) const {
5020b57cec5SDimitry Andric   // This function indicates whether the emergency spillslot should be placed
5030b57cec5SDimitry Andric   // close to the beginning of the stackframe (closer to FP) or the end
5040b57cec5SDimitry Andric   // (closer to SP).
5050b57cec5SDimitry Andric   //
5060b57cec5SDimitry Andric   // The beginning works most reliably if we have a frame pointer.
507979e22ffSDimitry Andric   // In the presence of any non-constant space between FP and locals,
508979e22ffSDimitry Andric   // (e.g. in case of stack realignment or a scalable SVE area), it is
509979e22ffSDimitry Andric   // better to use SP or BP.
5100b57cec5SDimitry Andric   const AArch64FrameLowering &TFI = *getFrameLowering(MF);
511979e22ffSDimitry Andric   const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
512979e22ffSDimitry Andric   assert((!MF.getSubtarget<AArch64Subtarget>().hasSVE() ||
513979e22ffSDimitry Andric           AFI->hasCalculatedStackSizeSVE()) &&
514979e22ffSDimitry Andric          "Expected SVE area to be calculated by this point");
515fe6060f1SDimitry Andric   return TFI.hasFP(MF) && !hasStackRealignment(MF) && !AFI->getStackSizeSVE();
5160b57cec5SDimitry Andric }
5170b57cec5SDimitry Andric 
5180b57cec5SDimitry Andric bool AArch64RegisterInfo::requiresFrameIndexScavenging(
5190b57cec5SDimitry Andric     const MachineFunction &MF) const {
5200b57cec5SDimitry Andric   return true;
5210b57cec5SDimitry Andric }
5220b57cec5SDimitry Andric 
5230b57cec5SDimitry Andric bool
5240b57cec5SDimitry Andric AArch64RegisterInfo::cannotEliminateFrame(const MachineFunction &MF) const {
5250b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
5260b57cec5SDimitry Andric   if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI.adjustsStack())
5270b57cec5SDimitry Andric     return true;
5280b57cec5SDimitry Andric   return MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken();
5290b57cec5SDimitry Andric }
5300b57cec5SDimitry Andric 
5310b57cec5SDimitry Andric /// needsFrameBaseReg - Returns true if the instruction's frame index
5320b57cec5SDimitry Andric /// reference would be better served by a base register other than FP
5330b57cec5SDimitry Andric /// or SP. Used by LocalStackFrameAllocation to determine which frame index
5340b57cec5SDimitry Andric /// references it should create new base registers for.
5350b57cec5SDimitry Andric bool AArch64RegisterInfo::needsFrameBaseReg(MachineInstr *MI,
5360b57cec5SDimitry Andric                                             int64_t Offset) const {
5370b57cec5SDimitry Andric   for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i)
5380b57cec5SDimitry Andric     assert(i < MI->getNumOperands() &&
5390b57cec5SDimitry Andric            "Instr doesn't have FrameIndex operand!");
5400b57cec5SDimitry Andric 
5410b57cec5SDimitry Andric   // It's the load/store FI references that cause issues, as it can be difficult
5420b57cec5SDimitry Andric   // to materialize the offset if it won't fit in the literal field. Estimate
5430b57cec5SDimitry Andric   // based on the size of the local frame and some conservative assumptions
5440b57cec5SDimitry Andric   // about the rest of the stack frame (note, this is pre-regalloc, so
5450b57cec5SDimitry Andric   // we don't know everything for certain yet) whether this offset is likely
5460b57cec5SDimitry Andric   // to be out of range of the immediate. Return true if so.
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric   // We only generate virtual base registers for loads and stores, so
5490b57cec5SDimitry Andric   // return false for everything else.
5500b57cec5SDimitry Andric   if (!MI->mayLoad() && !MI->mayStore())
5510b57cec5SDimitry Andric     return false;
5520b57cec5SDimitry Andric 
5530b57cec5SDimitry Andric   // Without a virtual base register, if the function has variable sized
5540b57cec5SDimitry Andric   // objects, all fixed-size local references will be via the frame pointer,
5550b57cec5SDimitry Andric   // Approximate the offset and see if it's legal for the instruction.
5560b57cec5SDimitry Andric   // Note that the incoming offset is based on the SP value at function entry,
5570b57cec5SDimitry Andric   // so it'll be negative.
5580b57cec5SDimitry Andric   MachineFunction &MF = *MI->getParent()->getParent();
5590b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
5600b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
5610b57cec5SDimitry Andric 
5620b57cec5SDimitry Andric   // Estimate an offset from the frame pointer.
5630b57cec5SDimitry Andric   // Conservatively assume all GPR callee-saved registers get pushed.
5640b57cec5SDimitry Andric   // FP, LR, X19-X28, D8-D15. 64-bits each.
5650b57cec5SDimitry Andric   int64_t FPOffset = Offset - 16 * 20;
5660b57cec5SDimitry Andric   // Estimate an offset from the stack pointer.
5670b57cec5SDimitry Andric   // The incoming offset is relating to the SP at the start of the function,
5680b57cec5SDimitry Andric   // but when we access the local it'll be relative to the SP after local
5690b57cec5SDimitry Andric   // allocation, so adjust our SP-relative offset by that allocation size.
5700b57cec5SDimitry Andric   Offset += MFI.getLocalFrameSize();
5710b57cec5SDimitry Andric   // Assume that we'll have at least some spill slots allocated.
5720b57cec5SDimitry Andric   // FIXME: This is a total SWAG number. We should run some statistics
5730b57cec5SDimitry Andric   //        and pick a real one.
5740b57cec5SDimitry Andric   Offset += 128; // 128 bytes of spill slots
5750b57cec5SDimitry Andric 
5760b57cec5SDimitry Andric   // If there is a frame pointer, try using it.
5770b57cec5SDimitry Andric   // The FP is only available if there is no dynamic realignment. We
5780b57cec5SDimitry Andric   // don't know for sure yet whether we'll need that, so we guess based
5790b57cec5SDimitry Andric   // on whether there are any local variables that would trigger it.
5800b57cec5SDimitry Andric   if (TFI->hasFP(MF) && isFrameOffsetLegal(MI, AArch64::FP, FPOffset))
5810b57cec5SDimitry Andric     return false;
5820b57cec5SDimitry Andric 
5830b57cec5SDimitry Andric   // If we can reference via the stack pointer or base pointer, try that.
5840b57cec5SDimitry Andric   // FIXME: This (and the code that resolves the references) can be improved
5850b57cec5SDimitry Andric   //        to only disallow SP relative references in the live range of
5860b57cec5SDimitry Andric   //        the VLA(s). In practice, it's unclear how much difference that
5870b57cec5SDimitry Andric   //        would make, but it may be worth doing.
5880b57cec5SDimitry Andric   if (isFrameOffsetLegal(MI, AArch64::SP, Offset))
5890b57cec5SDimitry Andric     return false;
5900b57cec5SDimitry Andric 
5915ffd83dbSDimitry Andric   // If even offset 0 is illegal, we don't want a virtual base register.
5925ffd83dbSDimitry Andric   if (!isFrameOffsetLegal(MI, AArch64::SP, 0))
5935ffd83dbSDimitry Andric     return false;
5945ffd83dbSDimitry Andric 
5950b57cec5SDimitry Andric   // The offset likely isn't legal; we want to allocate a virtual base register.
5960b57cec5SDimitry Andric   return true;
5970b57cec5SDimitry Andric }
5980b57cec5SDimitry Andric 
5990b57cec5SDimitry Andric bool AArch64RegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
6005ffd83dbSDimitry Andric                                              Register BaseReg,
6010b57cec5SDimitry Andric                                              int64_t Offset) const {
6020b57cec5SDimitry Andric   assert(MI && "Unable to get the legal offset for nil instruction.");
603e8d8bef9SDimitry Andric   StackOffset SaveOffset = StackOffset::getFixed(Offset);
6040b57cec5SDimitry Andric   return isAArch64FrameOffsetLegal(*MI, SaveOffset) & AArch64FrameOffsetIsLegal;
6050b57cec5SDimitry Andric }
6060b57cec5SDimitry Andric 
6070b57cec5SDimitry Andric /// Insert defining instruction(s) for BaseReg to be a pointer to FrameIdx
6080b57cec5SDimitry Andric /// at the beginning of the basic block.
609e8d8bef9SDimitry Andric Register
610e8d8bef9SDimitry Andric AArch64RegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB,
6110b57cec5SDimitry Andric                                                   int FrameIdx,
6120b57cec5SDimitry Andric                                                   int64_t Offset) const {
6130b57cec5SDimitry Andric   MachineBasicBlock::iterator Ins = MBB->begin();
6140b57cec5SDimitry Andric   DebugLoc DL; // Defaults to "unknown"
6150b57cec5SDimitry Andric   if (Ins != MBB->end())
6160b57cec5SDimitry Andric     DL = Ins->getDebugLoc();
6170b57cec5SDimitry Andric   const MachineFunction &MF = *MBB->getParent();
6180b57cec5SDimitry Andric   const AArch64InstrInfo *TII =
6190b57cec5SDimitry Andric       MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
6200b57cec5SDimitry Andric   const MCInstrDesc &MCID = TII->get(AArch64::ADDXri);
6210b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
622e8d8bef9SDimitry Andric   Register BaseReg = MRI.createVirtualRegister(&AArch64::GPR64spRegClass);
6230b57cec5SDimitry Andric   MRI.constrainRegClass(BaseReg, TII->getRegClass(MCID, 0, this, MF));
6240b57cec5SDimitry Andric   unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0);
6250b57cec5SDimitry Andric 
6260b57cec5SDimitry Andric   BuildMI(*MBB, Ins, DL, MCID, BaseReg)
6270b57cec5SDimitry Andric       .addFrameIndex(FrameIdx)
6280b57cec5SDimitry Andric       .addImm(Offset)
6290b57cec5SDimitry Andric       .addImm(Shifter);
630e8d8bef9SDimitry Andric 
631e8d8bef9SDimitry Andric   return BaseReg;
6320b57cec5SDimitry Andric }
6330b57cec5SDimitry Andric 
6345ffd83dbSDimitry Andric void AArch64RegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg,
6350b57cec5SDimitry Andric                                             int64_t Offset) const {
6368bcb0991SDimitry Andric   // ARM doesn't need the general 64-bit offsets
637e8d8bef9SDimitry Andric   StackOffset Off = StackOffset::getFixed(Offset);
6388bcb0991SDimitry Andric 
6390b57cec5SDimitry Andric   unsigned i = 0;
6400b57cec5SDimitry Andric   while (!MI.getOperand(i).isFI()) {
6410b57cec5SDimitry Andric     ++i;
6420b57cec5SDimitry Andric     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
6430b57cec5SDimitry Andric   }
644e8d8bef9SDimitry Andric 
6450b57cec5SDimitry Andric   const MachineFunction *MF = MI.getParent()->getParent();
6460b57cec5SDimitry Andric   const AArch64InstrInfo *TII =
6470b57cec5SDimitry Andric       MF->getSubtarget<AArch64Subtarget>().getInstrInfo();
6480b57cec5SDimitry Andric   bool Done = rewriteAArch64FrameIndex(MI, i, BaseReg, Off, TII);
6490b57cec5SDimitry Andric   assert(Done && "Unable to resolve frame index!");
6500b57cec5SDimitry Andric   (void)Done;
6510b57cec5SDimitry Andric }
6520b57cec5SDimitry Andric 
6535ffd83dbSDimitry Andric // Create a scratch register for the frame index elimination in an instruction.
6545ffd83dbSDimitry Andric // This function has special handling of stack tagging loop pseudos, in which
655*81ad6265SDimitry Andric // case it can also change the instruction opcode.
6565ffd83dbSDimitry Andric static Register
657*81ad6265SDimitry Andric createScratchRegisterForInstruction(MachineInstr &MI, unsigned FIOperandNum,
6585ffd83dbSDimitry Andric                                     const AArch64InstrInfo *TII) {
6595ffd83dbSDimitry Andric   // ST*Gloop have a reserved scratch register in operand 1. Use it, and also
6605ffd83dbSDimitry Andric   // replace the instruction with the writeback variant because it will now
6615ffd83dbSDimitry Andric   // satisfy the operand constraints for it.
662*81ad6265SDimitry Andric   Register ScratchReg;
663*81ad6265SDimitry Andric   if (MI.getOpcode() == AArch64::STGloop ||
664*81ad6265SDimitry Andric       MI.getOpcode() == AArch64::STZGloop) {
665*81ad6265SDimitry Andric     assert(FIOperandNum == 3 &&
666*81ad6265SDimitry Andric            "Wrong frame index operand for STGloop/STZGloop");
667*81ad6265SDimitry Andric     unsigned Op = MI.getOpcode() == AArch64::STGloop ? AArch64::STGloop_wback
668*81ad6265SDimitry Andric                                                      : AArch64::STZGloop_wback;
669*81ad6265SDimitry Andric     ScratchReg = MI.getOperand(1).getReg();
670*81ad6265SDimitry Andric     MI.getOperand(3).ChangeToRegister(ScratchReg, false, false, true);
671*81ad6265SDimitry Andric     MI.setDesc(TII->get(Op));
672*81ad6265SDimitry Andric     MI.tieOperands(1, 3);
6735ffd83dbSDimitry Andric   } else {
674*81ad6265SDimitry Andric     ScratchReg =
675*81ad6265SDimitry Andric         MI.getMF()->getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass);
676*81ad6265SDimitry Andric     MI.getOperand(FIOperandNum)
677*81ad6265SDimitry Andric         .ChangeToRegister(ScratchReg, false, false, true);
6785ffd83dbSDimitry Andric   }
679*81ad6265SDimitry Andric   return ScratchReg;
6805ffd83dbSDimitry Andric }
6815ffd83dbSDimitry Andric 
682e8d8bef9SDimitry Andric void AArch64RegisterInfo::getOffsetOpcodes(
683e8d8bef9SDimitry Andric     const StackOffset &Offset, SmallVectorImpl<uint64_t> &Ops) const {
684e8d8bef9SDimitry Andric   // The smallest scalable element supported by scaled SVE addressing
685e8d8bef9SDimitry Andric   // modes are predicates, which are 2 scalable bytes in size. So the scalable
686e8d8bef9SDimitry Andric   // byte offset must always be a multiple of 2.
687e8d8bef9SDimitry Andric   assert(Offset.getScalable() % 2 == 0 && "Invalid frame offset");
688e8d8bef9SDimitry Andric 
689e8d8bef9SDimitry Andric   // Add fixed-sized offset using existing DIExpression interface.
690e8d8bef9SDimitry Andric   DIExpression::appendOffset(Ops, Offset.getFixed());
691e8d8bef9SDimitry Andric 
692e8d8bef9SDimitry Andric   unsigned VG = getDwarfRegNum(AArch64::VG, true);
693e8d8bef9SDimitry Andric   int64_t VGSized = Offset.getScalable() / 2;
694e8d8bef9SDimitry Andric   if (VGSized > 0) {
695e8d8bef9SDimitry Andric     Ops.push_back(dwarf::DW_OP_constu);
696e8d8bef9SDimitry Andric     Ops.push_back(VGSized);
697e8d8bef9SDimitry Andric     Ops.append({dwarf::DW_OP_bregx, VG, 0ULL});
698e8d8bef9SDimitry Andric     Ops.push_back(dwarf::DW_OP_mul);
699e8d8bef9SDimitry Andric     Ops.push_back(dwarf::DW_OP_plus);
700e8d8bef9SDimitry Andric   } else if (VGSized < 0) {
701e8d8bef9SDimitry Andric     Ops.push_back(dwarf::DW_OP_constu);
702e8d8bef9SDimitry Andric     Ops.push_back(-VGSized);
703e8d8bef9SDimitry Andric     Ops.append({dwarf::DW_OP_bregx, VG, 0ULL});
704e8d8bef9SDimitry Andric     Ops.push_back(dwarf::DW_OP_mul);
705e8d8bef9SDimitry Andric     Ops.push_back(dwarf::DW_OP_minus);
706e8d8bef9SDimitry Andric   }
707e8d8bef9SDimitry Andric }
708e8d8bef9SDimitry Andric 
7090b57cec5SDimitry Andric void AArch64RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
7100b57cec5SDimitry Andric                                               int SPAdj, unsigned FIOperandNum,
7110b57cec5SDimitry Andric                                               RegScavenger *RS) const {
7120b57cec5SDimitry Andric   assert(SPAdj == 0 && "Unexpected");
7130b57cec5SDimitry Andric 
7140b57cec5SDimitry Andric   MachineInstr &MI = *II;
7150b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
7160b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
7178bcb0991SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
7180b57cec5SDimitry Andric   const AArch64InstrInfo *TII =
7190b57cec5SDimitry Andric       MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
7200b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
7210b57cec5SDimitry Andric   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
7228bcb0991SDimitry Andric   bool Tagged =
7238bcb0991SDimitry Andric       MI.getOperand(FIOperandNum).getTargetFlags() & AArch64II::MO_TAGGED;
7245ffd83dbSDimitry Andric   Register FrameReg;
7250b57cec5SDimitry Andric 
726e8d8bef9SDimitry Andric   // Special handling of dbg_value, stackmap patchpoint statepoint instructions.
727e8d8bef9SDimitry Andric   if (MI.getOpcode() == TargetOpcode::STACKMAP ||
728e8d8bef9SDimitry Andric       MI.getOpcode() == TargetOpcode::PATCHPOINT ||
729e8d8bef9SDimitry Andric       MI.getOpcode() == TargetOpcode::STATEPOINT) {
7308bcb0991SDimitry Andric     StackOffset Offset =
7318bcb0991SDimitry Andric         TFI->resolveFrameIndexReference(MF, FrameIndex, FrameReg,
7320b57cec5SDimitry Andric                                         /*PreferFP=*/true,
7330b57cec5SDimitry Andric                                         /*ForSimm=*/false);
734e8d8bef9SDimitry Andric     Offset += StackOffset::getFixed(MI.getOperand(FIOperandNum + 1).getImm());
7350b57cec5SDimitry Andric     MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false /*isDef*/);
736e8d8bef9SDimitry Andric     MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset.getFixed());
7370b57cec5SDimitry Andric     return;
7380b57cec5SDimitry Andric   }
7390b57cec5SDimitry Andric 
7400b57cec5SDimitry Andric   if (MI.getOpcode() == TargetOpcode::LOCAL_ESCAPE) {
7410b57cec5SDimitry Andric     MachineOperand &FI = MI.getOperand(FIOperandNum);
742e8d8bef9SDimitry Andric     StackOffset Offset = TFI->getNonLocalFrameIndexReference(MF, FrameIndex);
743e8d8bef9SDimitry Andric     assert(!Offset.getScalable() &&
744e8d8bef9SDimitry Andric            "Frame offsets with a scalable component are not supported");
745e8d8bef9SDimitry Andric     FI.ChangeToImmediate(Offset.getFixed());
7460b57cec5SDimitry Andric     return;
7470b57cec5SDimitry Andric   }
7480b57cec5SDimitry Andric 
7498bcb0991SDimitry Andric   StackOffset Offset;
7500b57cec5SDimitry Andric   if (MI.getOpcode() == AArch64::TAGPstack) {
7510b57cec5SDimitry Andric     // TAGPstack must use the virtual frame register in its 3rd operand.
7520b57cec5SDimitry Andric     const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
7530b57cec5SDimitry Andric     FrameReg = MI.getOperand(3).getReg();
754e8d8bef9SDimitry Andric     Offset = StackOffset::getFixed(MFI.getObjectOffset(FrameIndex) +
755e8d8bef9SDimitry Andric                                       AFI->getTaggedBasePointerOffset());
7568bcb0991SDimitry Andric   } else if (Tagged) {
757e8d8bef9SDimitry Andric     StackOffset SPOffset = StackOffset::getFixed(
758e8d8bef9SDimitry Andric         MFI.getObjectOffset(FrameIndex) + (int64_t)MFI.getStackSize());
7598bcb0991SDimitry Andric     if (MFI.hasVarSizedObjects() ||
7608bcb0991SDimitry Andric         isAArch64FrameOffsetLegal(MI, SPOffset, nullptr, nullptr, nullptr) !=
7618bcb0991SDimitry Andric             (AArch64FrameOffsetCanUpdate | AArch64FrameOffsetIsLegal)) {
7628bcb0991SDimitry Andric       // Can't update to SP + offset in place. Precalculate the tagged pointer
7638bcb0991SDimitry Andric       // in a scratch register.
7648bcb0991SDimitry Andric       Offset = TFI->resolveFrameIndexReference(
7658bcb0991SDimitry Andric           MF, FrameIndex, FrameReg, /*PreferFP=*/false, /*ForSimm=*/true);
7668bcb0991SDimitry Andric       Register ScratchReg =
7678bcb0991SDimitry Andric           MF.getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass);
7688bcb0991SDimitry Andric       emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset,
7698bcb0991SDimitry Andric                       TII);
7708bcb0991SDimitry Andric       BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(AArch64::LDG), ScratchReg)
7718bcb0991SDimitry Andric           .addReg(ScratchReg)
7728bcb0991SDimitry Andric           .addReg(ScratchReg)
7738bcb0991SDimitry Andric           .addImm(0);
7748bcb0991SDimitry Andric       MI.getOperand(FIOperandNum)
7758bcb0991SDimitry Andric           .ChangeToRegister(ScratchReg, false, false, true);
7768bcb0991SDimitry Andric       return;
7778bcb0991SDimitry Andric     }
7788bcb0991SDimitry Andric     FrameReg = AArch64::SP;
779e8d8bef9SDimitry Andric     Offset = StackOffset::getFixed(MFI.getObjectOffset(FrameIndex) +
780e8d8bef9SDimitry Andric                                    (int64_t)MFI.getStackSize());
7810b57cec5SDimitry Andric   } else {
7820b57cec5SDimitry Andric     Offset = TFI->resolveFrameIndexReference(
7830b57cec5SDimitry Andric         MF, FrameIndex, FrameReg, /*PreferFP=*/false, /*ForSimm=*/true);
7840b57cec5SDimitry Andric   }
7850b57cec5SDimitry Andric 
7860b57cec5SDimitry Andric   // Modify MI as necessary to handle as much of 'Offset' as possible
7870b57cec5SDimitry Andric   if (rewriteAArch64FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII))
7880b57cec5SDimitry Andric     return;
7890b57cec5SDimitry Andric 
7900b57cec5SDimitry Andric   assert((!RS || !RS->isScavengingFrameIndex(FrameIndex)) &&
7910b57cec5SDimitry Andric          "Emergency spill slot is out of reach");
7920b57cec5SDimitry Andric 
7930b57cec5SDimitry Andric   // If we get here, the immediate doesn't fit into the instruction.  We folded
7940b57cec5SDimitry Andric   // as much as possible above.  Handle the rest, providing a register that is
7950b57cec5SDimitry Andric   // SP+LargeImm.
796*81ad6265SDimitry Andric   Register ScratchReg =
797*81ad6265SDimitry Andric       createScratchRegisterForInstruction(MI, FIOperandNum, TII);
7980b57cec5SDimitry Andric   emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset, TII);
7990b57cec5SDimitry Andric }
8000b57cec5SDimitry Andric 
8010b57cec5SDimitry Andric unsigned AArch64RegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
8020b57cec5SDimitry Andric                                                   MachineFunction &MF) const {
8030b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
8040b57cec5SDimitry Andric 
8050b57cec5SDimitry Andric   switch (RC->getID()) {
8060b57cec5SDimitry Andric   default:
8070b57cec5SDimitry Andric     return 0;
8080b57cec5SDimitry Andric   case AArch64::GPR32RegClassID:
8090b57cec5SDimitry Andric   case AArch64::GPR32spRegClassID:
8100b57cec5SDimitry Andric   case AArch64::GPR32allRegClassID:
8110b57cec5SDimitry Andric   case AArch64::GPR64spRegClassID:
8120b57cec5SDimitry Andric   case AArch64::GPR64allRegClassID:
8130b57cec5SDimitry Andric   case AArch64::GPR64RegClassID:
8140b57cec5SDimitry Andric   case AArch64::GPR32commonRegClassID:
8150b57cec5SDimitry Andric   case AArch64::GPR64commonRegClassID:
8160b57cec5SDimitry Andric     return 32 - 1                                   // XZR/SP
8170b57cec5SDimitry Andric               - (TFI->hasFP(MF) || TT.isOSDarwin()) // FP
8180b57cec5SDimitry Andric               - MF.getSubtarget<AArch64Subtarget>().getNumXRegisterReserved()
8190b57cec5SDimitry Andric               - hasBasePointer(MF);  // X19
8200b57cec5SDimitry Andric   case AArch64::FPR8RegClassID:
8210b57cec5SDimitry Andric   case AArch64::FPR16RegClassID:
8220b57cec5SDimitry Andric   case AArch64::FPR32RegClassID:
8230b57cec5SDimitry Andric   case AArch64::FPR64RegClassID:
8240b57cec5SDimitry Andric   case AArch64::FPR128RegClassID:
8250b57cec5SDimitry Andric     return 32;
8260b57cec5SDimitry Andric 
827fe6060f1SDimitry Andric   case AArch64::MatrixIndexGPR32_12_15RegClassID:
828fe6060f1SDimitry Andric     return 4;
829fe6060f1SDimitry Andric 
8300b57cec5SDimitry Andric   case AArch64::DDRegClassID:
8310b57cec5SDimitry Andric   case AArch64::DDDRegClassID:
8320b57cec5SDimitry Andric   case AArch64::DDDDRegClassID:
8330b57cec5SDimitry Andric   case AArch64::QQRegClassID:
8340b57cec5SDimitry Andric   case AArch64::QQQRegClassID:
8350b57cec5SDimitry Andric   case AArch64::QQQQRegClassID:
8360b57cec5SDimitry Andric     return 32;
8370b57cec5SDimitry Andric 
8380b57cec5SDimitry Andric   case AArch64::FPR128_loRegClassID:
8395ffd83dbSDimitry Andric   case AArch64::FPR64_loRegClassID:
8405ffd83dbSDimitry Andric   case AArch64::FPR16_loRegClassID:
8410b57cec5SDimitry Andric     return 16;
8420b57cec5SDimitry Andric   }
8430b57cec5SDimitry Andric }
8440b57cec5SDimitry Andric 
8450b57cec5SDimitry Andric unsigned AArch64RegisterInfo::getLocalAddressRegister(
8460b57cec5SDimitry Andric   const MachineFunction &MF) const {
8470b57cec5SDimitry Andric   const auto &MFI = MF.getFrameInfo();
8480b57cec5SDimitry Andric   if (!MF.hasEHFunclets() && !MFI.hasVarSizedObjects())
8490b57cec5SDimitry Andric     return AArch64::SP;
850fe6060f1SDimitry Andric   else if (hasStackRealignment(MF))
8510b57cec5SDimitry Andric     return getBaseRegister();
8520b57cec5SDimitry Andric   return getFrameRegister(MF);
8530b57cec5SDimitry Andric }
854e8d8bef9SDimitry Andric 
855e8d8bef9SDimitry Andric /// SrcRC and DstRC will be morphed into NewRC if this returns true
856e8d8bef9SDimitry Andric bool AArch64RegisterInfo::shouldCoalesce(
857e8d8bef9SDimitry Andric     MachineInstr *MI, const TargetRegisterClass *SrcRC, unsigned SubReg,
858e8d8bef9SDimitry Andric     const TargetRegisterClass *DstRC, unsigned DstSubReg,
859e8d8bef9SDimitry Andric     const TargetRegisterClass *NewRC, LiveIntervals &LIS) const {
860e8d8bef9SDimitry Andric   if (MI->isCopy() &&
861e8d8bef9SDimitry Andric       ((DstRC->getID() == AArch64::GPR64RegClassID) ||
862e8d8bef9SDimitry Andric        (DstRC->getID() == AArch64::GPR64commonRegClassID)) &&
863e8d8bef9SDimitry Andric       MI->getOperand(0).getSubReg() && MI->getOperand(1).getSubReg())
864e8d8bef9SDimitry Andric     // Do not coalesce in the case of a 32-bit subregister copy
865e8d8bef9SDimitry Andric     // which implements a 32 to 64 bit zero extension
866e8d8bef9SDimitry Andric     // which relies on the upper 32 bits being zeroed.
867e8d8bef9SDimitry Andric     return false;
868e8d8bef9SDimitry Andric   return true;
869e8d8bef9SDimitry Andric }
870