xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AArch64/AArch64RegisterInfo.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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"
20*bdd1243dSDimitry Andric #include "MCTargetDesc/AArch64InstPrinter.h"
210b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
220b57cec5SDimitry Andric #include "llvm/ADT/Triple.h"
2381ad6265SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
29e8d8bef9SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
308bcb0991SDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
318bcb0991SDimitry Andric #include "llvm/IR/Function.h"
328bcb0991SDimitry Andric #include "llvm/Support/raw_ostream.h"
330b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric using namespace llvm;
360b57cec5SDimitry Andric 
3781ad6265SDimitry Andric #define GET_CC_REGISTER_LISTS
3881ad6265SDimitry Andric #include "AArch64GenCallingConv.inc"
390b57cec5SDimitry Andric #define GET_REGINFO_TARGET_DESC
400b57cec5SDimitry Andric #include "AArch64GenRegisterInfo.inc"
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric AArch64RegisterInfo::AArch64RegisterInfo(const Triple &TT)
430b57cec5SDimitry Andric     : AArch64GenRegisterInfo(AArch64::LR), TT(TT) {
440b57cec5SDimitry Andric   AArch64_MC::initLLVMToCVRegMapping(this);
450b57cec5SDimitry Andric }
460b57cec5SDimitry Andric 
4775b4d546SDimitry Andric /// Return whether the register needs a CFI entry. Not all unwinders may know
4875b4d546SDimitry Andric /// about SVE registers, so we assume the lowest common denominator, i.e. the
4975b4d546SDimitry Andric /// callee-saves required by the base ABI. For the SVE registers z8-z15 only the
5075b4d546SDimitry Andric /// lower 64-bits (d8-d15) need to be saved. The lower 64-bits subreg is
5175b4d546SDimitry Andric /// returned in \p RegToUseForCFI.
5275b4d546SDimitry Andric bool AArch64RegisterInfo::regNeedsCFI(unsigned Reg,
5375b4d546SDimitry Andric                                       unsigned &RegToUseForCFI) const {
5475b4d546SDimitry Andric   if (AArch64::PPRRegClass.contains(Reg))
5575b4d546SDimitry Andric     return false;
5675b4d546SDimitry Andric 
5775b4d546SDimitry Andric   if (AArch64::ZPRRegClass.contains(Reg)) {
5875b4d546SDimitry Andric     RegToUseForCFI = getSubReg(Reg, AArch64::dsub);
5975b4d546SDimitry Andric     for (int I = 0; CSR_AArch64_AAPCS_SaveList[I]; ++I) {
6075b4d546SDimitry Andric       if (CSR_AArch64_AAPCS_SaveList[I] == RegToUseForCFI)
6175b4d546SDimitry Andric         return true;
6275b4d546SDimitry Andric     }
6375b4d546SDimitry Andric     return false;
6475b4d546SDimitry Andric   }
6575b4d546SDimitry Andric 
6675b4d546SDimitry Andric   RegToUseForCFI = Reg;
6775b4d546SDimitry Andric   return true;
6875b4d546SDimitry Andric }
6975b4d546SDimitry Andric 
700b57cec5SDimitry Andric const MCPhysReg *
710b57cec5SDimitry Andric AArch64RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
720b57cec5SDimitry Andric   assert(MF && "Invalid MachineFunction pointer.");
735ffd83dbSDimitry Andric 
740b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::GHC)
750b57cec5SDimitry Andric     // GHC set of callee saved regs is empty as all those regs are
760b57cec5SDimitry Andric     // used for passing STG regs around
770b57cec5SDimitry Andric     return CSR_AArch64_NoRegs_SaveList;
780b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AnyReg)
790b57cec5SDimitry Andric     return CSR_AArch64_AllRegs_SaveList;
805ffd83dbSDimitry Andric 
815ffd83dbSDimitry Andric   // Darwin has its own CSR_AArch64_AAPCS_SaveList, which means most CSR save
825ffd83dbSDimitry Andric   // lists depending on that will need to have their Darwin variant as well.
835ffd83dbSDimitry Andric   if (MF->getSubtarget<AArch64Subtarget>().isTargetDarwin())
845ffd83dbSDimitry Andric     return getDarwinCalleeSavedRegs(MF);
855ffd83dbSDimitry Andric 
865ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::CFGuard_Check)
875ffd83dbSDimitry Andric     return CSR_Win_AArch64_CFGuard_Check_SaveList;
885ffd83dbSDimitry Andric   if (MF->getSubtarget<AArch64Subtarget>().isTargetWindows())
895ffd83dbSDimitry Andric     return CSR_Win_AArch64_AAPCS_SaveList;
900b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AArch64_VectorCall)
910b57cec5SDimitry Andric     return CSR_AArch64_AAVPCS_SaveList;
92480093f4SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AArch64_SVE_VectorCall)
93480093f4SDimitry Andric     return CSR_AArch64_SVE_AAPCS_SaveList;
94*bdd1243dSDimitry Andric   if (MF->getFunction().getCallingConv() ==
95*bdd1243dSDimitry Andric           CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0)
96*bdd1243dSDimitry Andric     report_fatal_error(
97*bdd1243dSDimitry Andric         "Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0 is "
98*bdd1243dSDimitry Andric         "only supported to improve calls to SME ACLE save/restore/disable-za "
99*bdd1243dSDimitry Andric         "functions, and is not intended to be used beyond that scope.");
100*bdd1243dSDimitry Andric   if (MF->getFunction().getCallingConv() ==
101*bdd1243dSDimitry Andric           CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2)
102*bdd1243dSDimitry Andric     report_fatal_error(
103*bdd1243dSDimitry Andric         "Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2 is "
104*bdd1243dSDimitry Andric         "only supported to improve calls to SME ACLE __arm_sme_state "
105*bdd1243dSDimitry Andric         "and is not intended to be used beyond that scope.");
1060b57cec5SDimitry Andric   if (MF->getSubtarget<AArch64Subtarget>().getTargetLowering()
1070b57cec5SDimitry Andric           ->supportSwiftError() &&
1080b57cec5SDimitry Andric       MF->getFunction().getAttributes().hasAttrSomewhere(
1090b57cec5SDimitry Andric           Attribute::SwiftError))
1100b57cec5SDimitry Andric     return CSR_AArch64_AAPCS_SwiftError_SaveList;
111fe6060f1SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::SwiftTail)
112fe6060f1SDimitry Andric     return CSR_AArch64_AAPCS_SwiftTail_SaveList;
1130b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::PreserveMost)
1140b57cec5SDimitry Andric     return CSR_AArch64_RT_MostRegs_SaveList;
1155ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::Win64)
1165ffd83dbSDimitry Andric     // This is for OSes other than Windows; Windows is a separate case further
1175ffd83dbSDimitry Andric     // above.
1185ffd83dbSDimitry Andric     return CSR_AArch64_AAPCS_X18_SaveList;
11981ad6265SDimitry Andric   if (MF->getInfo<AArch64FunctionInfo>()->isSVECC())
120979e22ffSDimitry Andric     return CSR_AArch64_SVE_AAPCS_SaveList;
1210b57cec5SDimitry Andric   return CSR_AArch64_AAPCS_SaveList;
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric 
1245ffd83dbSDimitry Andric const MCPhysReg *
1255ffd83dbSDimitry Andric AArch64RegisterInfo::getDarwinCalleeSavedRegs(const MachineFunction *MF) const {
1265ffd83dbSDimitry Andric   assert(MF && "Invalid MachineFunction pointer.");
1275ffd83dbSDimitry Andric   assert(MF->getSubtarget<AArch64Subtarget>().isTargetDarwin() &&
1285ffd83dbSDimitry Andric          "Invalid subtarget for getDarwinCalleeSavedRegs");
1295ffd83dbSDimitry Andric 
1305ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::CFGuard_Check)
1315ffd83dbSDimitry Andric     report_fatal_error(
1325ffd83dbSDimitry Andric         "Calling convention CFGuard_Check is unsupported on Darwin.");
1335ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AArch64_VectorCall)
1345ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_AAVPCS_SaveList;
1355ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AArch64_SVE_VectorCall)
1365ffd83dbSDimitry Andric     report_fatal_error(
1375ffd83dbSDimitry Andric         "Calling convention SVE_VectorCall is unsupported on Darwin.");
138*bdd1243dSDimitry Andric   if (MF->getFunction().getCallingConv() ==
139*bdd1243dSDimitry Andric           CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0)
140*bdd1243dSDimitry Andric     report_fatal_error(
141*bdd1243dSDimitry Andric         "Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0 is "
142*bdd1243dSDimitry Andric         "only supported to improve calls to SME ACLE save/restore/disable-za "
143*bdd1243dSDimitry Andric         "functions, and is not intended to be used beyond that scope.");
144*bdd1243dSDimitry Andric   if (MF->getFunction().getCallingConv() ==
145*bdd1243dSDimitry Andric           CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2)
146*bdd1243dSDimitry Andric     report_fatal_error(
147*bdd1243dSDimitry Andric         "Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2 is "
148*bdd1243dSDimitry Andric         "only supported to improve calls to SME ACLE __arm_sme_state "
149*bdd1243dSDimitry Andric         "and is not intended to be used beyond that scope.");
1505ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS)
1515ffd83dbSDimitry Andric     return MF->getInfo<AArch64FunctionInfo>()->isSplitCSR()
1525ffd83dbSDimitry Andric                ? CSR_Darwin_AArch64_CXX_TLS_PE_SaveList
1535ffd83dbSDimitry Andric                : CSR_Darwin_AArch64_CXX_TLS_SaveList;
1545ffd83dbSDimitry Andric   if (MF->getSubtarget<AArch64Subtarget>().getTargetLowering()
1555ffd83dbSDimitry Andric           ->supportSwiftError() &&
1565ffd83dbSDimitry Andric       MF->getFunction().getAttributes().hasAttrSomewhere(
1575ffd83dbSDimitry Andric           Attribute::SwiftError))
1585ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_AAPCS_SwiftError_SaveList;
159fe6060f1SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::SwiftTail)
160fe6060f1SDimitry Andric     return CSR_Darwin_AArch64_AAPCS_SwiftTail_SaveList;
1615ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::PreserveMost)
1625ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_RT_MostRegs_SaveList;
163*bdd1243dSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::Win64)
164*bdd1243dSDimitry Andric     return CSR_Darwin_AArch64_AAPCS_Win64_SaveList;
1655ffd83dbSDimitry Andric   return CSR_Darwin_AArch64_AAPCS_SaveList;
1665ffd83dbSDimitry Andric }
1675ffd83dbSDimitry Andric 
1680b57cec5SDimitry Andric const MCPhysReg *AArch64RegisterInfo::getCalleeSavedRegsViaCopy(
1690b57cec5SDimitry Andric     const MachineFunction *MF) const {
1700b57cec5SDimitry Andric   assert(MF && "Invalid MachineFunction pointer.");
1710b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS &&
1720b57cec5SDimitry Andric       MF->getInfo<AArch64FunctionInfo>()->isSplitCSR())
1735ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_CXX_TLS_ViaCopy_SaveList;
1740b57cec5SDimitry Andric   return nullptr;
1750b57cec5SDimitry Andric }
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric void AArch64RegisterInfo::UpdateCustomCalleeSavedRegs(
1780b57cec5SDimitry Andric     MachineFunction &MF) const {
1790b57cec5SDimitry Andric   const MCPhysReg *CSRs = getCalleeSavedRegs(&MF);
1800b57cec5SDimitry Andric   SmallVector<MCPhysReg, 32> UpdatedCSRs;
1810b57cec5SDimitry Andric   for (const MCPhysReg *I = CSRs; *I; ++I)
1820b57cec5SDimitry Andric     UpdatedCSRs.push_back(*I);
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric   for (size_t i = 0; i < AArch64::GPR64commonRegClass.getNumRegs(); ++i) {
1850b57cec5SDimitry Andric     if (MF.getSubtarget<AArch64Subtarget>().isXRegCustomCalleeSaved(i)) {
1860b57cec5SDimitry Andric       UpdatedCSRs.push_back(AArch64::GPR64commonRegClass.getRegister(i));
1870b57cec5SDimitry Andric     }
1880b57cec5SDimitry Andric   }
1890b57cec5SDimitry Andric   // Register lists are zero-terminated.
1900b57cec5SDimitry Andric   UpdatedCSRs.push_back(0);
1910b57cec5SDimitry Andric   MF.getRegInfo().setCalleeSavedRegs(UpdatedCSRs);
1920b57cec5SDimitry Andric }
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric const TargetRegisterClass *
1950b57cec5SDimitry Andric AArch64RegisterInfo::getSubClassWithSubReg(const TargetRegisterClass *RC,
1960b57cec5SDimitry Andric                                        unsigned Idx) const {
1970b57cec5SDimitry Andric   // edge case for GPR/FPR register classes
1980b57cec5SDimitry Andric   if (RC == &AArch64::GPR32allRegClass && Idx == AArch64::hsub)
1990b57cec5SDimitry Andric     return &AArch64::FPR32RegClass;
2000b57cec5SDimitry Andric   else if (RC == &AArch64::GPR64allRegClass && Idx == AArch64::hsub)
2010b57cec5SDimitry Andric     return &AArch64::FPR64RegClass;
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric   // Forward to TableGen's default version.
2040b57cec5SDimitry Andric   return AArch64GenRegisterInfo::getSubClassWithSubReg(RC, Idx);
2050b57cec5SDimitry Andric }
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric const uint32_t *
2085ffd83dbSDimitry Andric AArch64RegisterInfo::getDarwinCallPreservedMask(const MachineFunction &MF,
2095ffd83dbSDimitry Andric                                                 CallingConv::ID CC) const {
2105ffd83dbSDimitry Andric   assert(MF.getSubtarget<AArch64Subtarget>().isTargetDarwin() &&
2115ffd83dbSDimitry Andric          "Invalid subtarget for getDarwinCallPreservedMask");
2125ffd83dbSDimitry Andric 
2135ffd83dbSDimitry Andric   if (CC == CallingConv::CXX_FAST_TLS)
2145ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_CXX_TLS_RegMask;
2155ffd83dbSDimitry Andric   if (CC == CallingConv::AArch64_VectorCall)
2165ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_AAVPCS_RegMask;
2175ffd83dbSDimitry Andric   if (CC == CallingConv::AArch64_SVE_VectorCall)
2185ffd83dbSDimitry Andric     report_fatal_error(
2195ffd83dbSDimitry Andric         "Calling convention SVE_VectorCall is unsupported on Darwin.");
220*bdd1243dSDimitry Andric   if (CC == CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0)
221*bdd1243dSDimitry Andric     report_fatal_error(
222*bdd1243dSDimitry Andric         "Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0 is "
223*bdd1243dSDimitry Andric         "unsupported on Darwin.");
224*bdd1243dSDimitry Andric   if (CC == CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2)
225*bdd1243dSDimitry Andric     report_fatal_error(
226*bdd1243dSDimitry Andric         "Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2 is "
227*bdd1243dSDimitry Andric         "unsupported on Darwin.");
2285ffd83dbSDimitry Andric   if (CC == CallingConv::CFGuard_Check)
2295ffd83dbSDimitry Andric     report_fatal_error(
2305ffd83dbSDimitry Andric         "Calling convention CFGuard_Check is unsupported on Darwin.");
2315ffd83dbSDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>()
2325ffd83dbSDimitry Andric           .getTargetLowering()
2335ffd83dbSDimitry Andric           ->supportSwiftError() &&
2345ffd83dbSDimitry Andric       MF.getFunction().getAttributes().hasAttrSomewhere(Attribute::SwiftError))
2355ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_AAPCS_SwiftError_RegMask;
236fe6060f1SDimitry Andric   if (CC == CallingConv::SwiftTail)
237fe6060f1SDimitry Andric     return CSR_Darwin_AArch64_AAPCS_SwiftTail_RegMask;
2385ffd83dbSDimitry Andric   if (CC == CallingConv::PreserveMost)
2395ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_RT_MostRegs_RegMask;
2405ffd83dbSDimitry Andric   return CSR_Darwin_AArch64_AAPCS_RegMask;
2415ffd83dbSDimitry Andric }
2425ffd83dbSDimitry Andric 
2435ffd83dbSDimitry Andric const uint32_t *
2440b57cec5SDimitry Andric AArch64RegisterInfo::getCallPreservedMask(const MachineFunction &MF,
2450b57cec5SDimitry Andric                                           CallingConv::ID CC) const {
2460b57cec5SDimitry Andric   bool SCS = MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack);
2470b57cec5SDimitry Andric   if (CC == CallingConv::GHC)
2480b57cec5SDimitry Andric     // This is academic because all GHC calls are (supposed to be) tail calls
2490b57cec5SDimitry Andric     return SCS ? CSR_AArch64_NoRegs_SCS_RegMask : CSR_AArch64_NoRegs_RegMask;
2500b57cec5SDimitry Andric   if (CC == CallingConv::AnyReg)
2510b57cec5SDimitry Andric     return SCS ? CSR_AArch64_AllRegs_SCS_RegMask : CSR_AArch64_AllRegs_RegMask;
2525ffd83dbSDimitry Andric 
2535ffd83dbSDimitry Andric   // All the following calling conventions are handled differently on Darwin.
2545ffd83dbSDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>().isTargetDarwin()) {
2555ffd83dbSDimitry Andric     if (SCS)
2565ffd83dbSDimitry Andric       report_fatal_error("ShadowCallStack attribute not supported on Darwin.");
2575ffd83dbSDimitry Andric     return getDarwinCallPreservedMask(MF, CC);
2585ffd83dbSDimitry Andric   }
2595ffd83dbSDimitry Andric 
2600b57cec5SDimitry Andric   if (CC == CallingConv::AArch64_VectorCall)
2610b57cec5SDimitry Andric     return SCS ? CSR_AArch64_AAVPCS_SCS_RegMask : CSR_AArch64_AAVPCS_RegMask;
2628bcb0991SDimitry Andric   if (CC == CallingConv::AArch64_SVE_VectorCall)
263480093f4SDimitry Andric     return SCS ? CSR_AArch64_SVE_AAPCS_SCS_RegMask
264480093f4SDimitry Andric                : CSR_AArch64_SVE_AAPCS_RegMask;
265*bdd1243dSDimitry Andric   if (CC == CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0)
266*bdd1243dSDimitry Andric     return CSR_AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0_RegMask;
267*bdd1243dSDimitry Andric   if (CC == CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2)
268*bdd1243dSDimitry Andric     return CSR_AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2_RegMask;
269480093f4SDimitry Andric   if (CC == CallingConv::CFGuard_Check)
270480093f4SDimitry Andric     return CSR_Win_AArch64_CFGuard_Check_RegMask;
2710b57cec5SDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>().getTargetLowering()
2720b57cec5SDimitry Andric           ->supportSwiftError() &&
2730b57cec5SDimitry Andric       MF.getFunction().getAttributes().hasAttrSomewhere(Attribute::SwiftError))
2740b57cec5SDimitry Andric     return SCS ? CSR_AArch64_AAPCS_SwiftError_SCS_RegMask
2750b57cec5SDimitry Andric                : CSR_AArch64_AAPCS_SwiftError_RegMask;
276fe6060f1SDimitry Andric   if (CC == CallingConv::SwiftTail) {
277fe6060f1SDimitry Andric     if (SCS)
278fe6060f1SDimitry Andric       report_fatal_error("ShadowCallStack attribute not supported with swifttail");
279fe6060f1SDimitry Andric     return CSR_AArch64_AAPCS_SwiftTail_RegMask;
280fe6060f1SDimitry Andric   }
2810b57cec5SDimitry Andric   if (CC == CallingConv::PreserveMost)
2820b57cec5SDimitry Andric     return SCS ? CSR_AArch64_RT_MostRegs_SCS_RegMask
2830b57cec5SDimitry Andric                : CSR_AArch64_RT_MostRegs_RegMask;
2840b57cec5SDimitry Andric   else
2850b57cec5SDimitry Andric     return SCS ? CSR_AArch64_AAPCS_SCS_RegMask : CSR_AArch64_AAPCS_RegMask;
2860b57cec5SDimitry Andric }
2870b57cec5SDimitry Andric 
288e8d8bef9SDimitry Andric const uint32_t *AArch64RegisterInfo::getCustomEHPadPreservedMask(
289e8d8bef9SDimitry Andric     const MachineFunction &MF) const {
290e8d8bef9SDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>().isTargetLinux())
291e8d8bef9SDimitry Andric     return CSR_AArch64_AAPCS_RegMask;
292e8d8bef9SDimitry Andric 
293e8d8bef9SDimitry Andric   return nullptr;
294e8d8bef9SDimitry Andric }
295e8d8bef9SDimitry Andric 
2960b57cec5SDimitry Andric const uint32_t *AArch64RegisterInfo::getTLSCallPreservedMask() const {
2970b57cec5SDimitry Andric   if (TT.isOSDarwin())
2985ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_TLS_RegMask;
2990b57cec5SDimitry Andric 
3000b57cec5SDimitry Andric   assert(TT.isOSBinFormatELF() && "Invalid target");
3010b57cec5SDimitry Andric   return CSR_AArch64_TLS_ELF_RegMask;
3020b57cec5SDimitry Andric }
3030b57cec5SDimitry Andric 
3040b57cec5SDimitry Andric void AArch64RegisterInfo::UpdateCustomCallPreservedMask(MachineFunction &MF,
3050b57cec5SDimitry Andric                                                  const uint32_t **Mask) const {
3060b57cec5SDimitry Andric   uint32_t *UpdatedMask = MF.allocateRegMask();
3070b57cec5SDimitry Andric   unsigned RegMaskSize = MachineOperand::getRegMaskSize(getNumRegs());
3080b57cec5SDimitry Andric   memcpy(UpdatedMask, *Mask, sizeof(UpdatedMask[0]) * RegMaskSize);
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric   for (size_t i = 0; i < AArch64::GPR64commonRegClass.getNumRegs(); ++i) {
3110b57cec5SDimitry Andric     if (MF.getSubtarget<AArch64Subtarget>().isXRegCustomCalleeSaved(i)) {
3120b57cec5SDimitry Andric       for (MCSubRegIterator SubReg(AArch64::GPR64commonRegClass.getRegister(i),
3130b57cec5SDimitry Andric                                    this, true);
3140b57cec5SDimitry Andric            SubReg.isValid(); ++SubReg) {
3150b57cec5SDimitry Andric         // See TargetRegisterInfo::getCallPreservedMask for how to interpret the
3160b57cec5SDimitry Andric         // register mask.
3170b57cec5SDimitry Andric         UpdatedMask[*SubReg / 32] |= 1u << (*SubReg % 32);
3180b57cec5SDimitry Andric       }
3190b57cec5SDimitry Andric     }
3200b57cec5SDimitry Andric   }
3210b57cec5SDimitry Andric   *Mask = UpdatedMask;
3220b57cec5SDimitry Andric }
3230b57cec5SDimitry Andric 
324*bdd1243dSDimitry Andric const uint32_t *AArch64RegisterInfo::getSMStartStopCallPreservedMask() const {
325*bdd1243dSDimitry Andric   return CSR_AArch64_SMStartStop_RegMask;
326*bdd1243dSDimitry Andric }
327*bdd1243dSDimitry Andric 
328*bdd1243dSDimitry Andric const uint32_t *
329*bdd1243dSDimitry Andric AArch64RegisterInfo::SMEABISupportRoutinesCallPreservedMaskFromX0() const {
330*bdd1243dSDimitry Andric   return CSR_AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0_RegMask;
331*bdd1243dSDimitry Andric }
332*bdd1243dSDimitry Andric 
3330b57cec5SDimitry Andric const uint32_t *AArch64RegisterInfo::getNoPreservedMask() const {
3340b57cec5SDimitry Andric   return CSR_AArch64_NoRegs_RegMask;
3350b57cec5SDimitry Andric }
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric const uint32_t *
3380b57cec5SDimitry Andric AArch64RegisterInfo::getThisReturnPreservedMask(const MachineFunction &MF,
3390b57cec5SDimitry Andric                                                 CallingConv::ID CC) const {
3400b57cec5SDimitry Andric   // This should return a register mask that is the same as that returned by
3410b57cec5SDimitry Andric   // getCallPreservedMask but that additionally preserves the register used for
3420b57cec5SDimitry Andric   // the first i64 argument (which must also be the register used to return a
3430b57cec5SDimitry Andric   // single i64 return value)
3440b57cec5SDimitry Andric   //
3450b57cec5SDimitry Andric   // In case that the calling convention does not use the same register for
3460b57cec5SDimitry Andric   // both, the function should return NULL (does not currently apply)
3470b57cec5SDimitry Andric   assert(CC != CallingConv::GHC && "should not be GHC calling convention.");
3485ffd83dbSDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>().isTargetDarwin())
3495ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_AAPCS_ThisReturn_RegMask;
3500b57cec5SDimitry Andric   return CSR_AArch64_AAPCS_ThisReturn_RegMask;
3510b57cec5SDimitry Andric }
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric const uint32_t *AArch64RegisterInfo::getWindowsStackProbePreservedMask() const {
3540b57cec5SDimitry Andric   return CSR_AArch64_StackProbe_Windows_RegMask;
3550b57cec5SDimitry Andric }
3560b57cec5SDimitry Andric 
357*bdd1243dSDimitry Andric std::optional<std::string>
358*bdd1243dSDimitry Andric AArch64RegisterInfo::explainReservedReg(const MachineFunction &MF,
359*bdd1243dSDimitry Andric                                         MCRegister PhysReg) const {
360*bdd1243dSDimitry Andric   if (hasBasePointer(MF) && MCRegisterInfo::regsOverlap(PhysReg, AArch64::X19))
361*bdd1243dSDimitry Andric     return std::string("X19 is used as the frame base pointer register.");
362*bdd1243dSDimitry Andric 
363*bdd1243dSDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>().isWindowsArm64EC()) {
364*bdd1243dSDimitry Andric     bool warn = false;
365*bdd1243dSDimitry Andric     if (MCRegisterInfo::regsOverlap(PhysReg, AArch64::X13) ||
366*bdd1243dSDimitry Andric         MCRegisterInfo::regsOverlap(PhysReg, AArch64::X14) ||
367*bdd1243dSDimitry Andric         MCRegisterInfo::regsOverlap(PhysReg, AArch64::X23) ||
368*bdd1243dSDimitry Andric         MCRegisterInfo::regsOverlap(PhysReg, AArch64::X24) ||
369*bdd1243dSDimitry Andric         MCRegisterInfo::regsOverlap(PhysReg, AArch64::X28))
370*bdd1243dSDimitry Andric       warn = true;
371*bdd1243dSDimitry Andric 
372*bdd1243dSDimitry Andric     for (unsigned i = AArch64::B16; i <= AArch64::B31; ++i)
373*bdd1243dSDimitry Andric       if (MCRegisterInfo::regsOverlap(PhysReg, i))
374*bdd1243dSDimitry Andric         warn = true;
375*bdd1243dSDimitry Andric 
376*bdd1243dSDimitry Andric     if (warn)
377*bdd1243dSDimitry Andric       return std::string(AArch64InstPrinter::getRegisterName(PhysReg)) +
378*bdd1243dSDimitry Andric              " is clobbered by asynchronous signals when using Arm64EC.";
379*bdd1243dSDimitry Andric   }
380*bdd1243dSDimitry Andric 
381*bdd1243dSDimitry Andric   return {};
382*bdd1243dSDimitry Andric }
383*bdd1243dSDimitry Andric 
3840b57cec5SDimitry Andric BitVector
385*bdd1243dSDimitry Andric AArch64RegisterInfo::getStrictlyReservedRegs(const MachineFunction &MF) const {
3860b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric   // FIXME: avoid re-calculating this every time.
3890b57cec5SDimitry Andric   BitVector Reserved(getNumRegs());
3900b57cec5SDimitry Andric   markSuperRegs(Reserved, AArch64::WSP);
3910b57cec5SDimitry Andric   markSuperRegs(Reserved, AArch64::WZR);
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric   if (TFI->hasFP(MF) || TT.isOSDarwin())
3940b57cec5SDimitry Andric     markSuperRegs(Reserved, AArch64::W29);
3950b57cec5SDimitry Andric 
396*bdd1243dSDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>().isWindowsArm64EC()) {
397*bdd1243dSDimitry Andric     // x13, x14, x23, x24, x28, and v16-v31 are clobbered by asynchronous
398*bdd1243dSDimitry Andric     // signals, so we can't ever use them.
399*bdd1243dSDimitry Andric     markSuperRegs(Reserved, AArch64::W13);
400*bdd1243dSDimitry Andric     markSuperRegs(Reserved, AArch64::W14);
401*bdd1243dSDimitry Andric     markSuperRegs(Reserved, AArch64::W23);
402*bdd1243dSDimitry Andric     markSuperRegs(Reserved, AArch64::W24);
403*bdd1243dSDimitry Andric     markSuperRegs(Reserved, AArch64::W28);
404*bdd1243dSDimitry Andric     for (unsigned i = AArch64::B16; i <= AArch64::B31; ++i)
405*bdd1243dSDimitry Andric       markSuperRegs(Reserved, i);
406*bdd1243dSDimitry Andric   }
407*bdd1243dSDimitry Andric 
4080b57cec5SDimitry Andric   for (size_t i = 0; i < AArch64::GPR32commonRegClass.getNumRegs(); ++i) {
4090b57cec5SDimitry Andric     if (MF.getSubtarget<AArch64Subtarget>().isXRegisterReserved(i))
4100b57cec5SDimitry Andric       markSuperRegs(Reserved, AArch64::GPR32commonRegClass.getRegister(i));
4110b57cec5SDimitry Andric   }
4120b57cec5SDimitry Andric 
4130b57cec5SDimitry Andric   if (hasBasePointer(MF))
4140b57cec5SDimitry Andric     markSuperRegs(Reserved, AArch64::W19);
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric   // SLH uses register W16/X16 as the taint register.
4170b57cec5SDimitry Andric   if (MF.getFunction().hasFnAttribute(Attribute::SpeculativeLoadHardening))
4180b57cec5SDimitry Andric     markSuperRegs(Reserved, AArch64::W16);
4190b57cec5SDimitry Andric 
42081ad6265SDimitry Andric   // SME tiles are not allocatable.
42181ad6265SDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>().hasSME()) {
42281ad6265SDimitry Andric     for (MCSubRegIterator SubReg(AArch64::ZA, this, /*self=*/true);
42381ad6265SDimitry Andric          SubReg.isValid(); ++SubReg)
42481ad6265SDimitry Andric       Reserved.set(*SubReg);
42581ad6265SDimitry Andric   }
42681ad6265SDimitry Andric 
427*bdd1243dSDimitry Andric   markSuperRegs(Reserved, AArch64::FPCR);
428*bdd1243dSDimitry Andric 
429*bdd1243dSDimitry Andric   assert(checkAllSuperRegsMarked(Reserved));
430*bdd1243dSDimitry Andric   return Reserved;
431*bdd1243dSDimitry Andric }
432*bdd1243dSDimitry Andric 
433*bdd1243dSDimitry Andric BitVector
434*bdd1243dSDimitry Andric AArch64RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
435*bdd1243dSDimitry Andric   BitVector Reserved = getStrictlyReservedRegs(MF);
436*bdd1243dSDimitry Andric 
437*bdd1243dSDimitry Andric   for (size_t i = 0; i < AArch64::GPR32commonRegClass.getNumRegs(); ++i) {
438*bdd1243dSDimitry Andric     if (MF.getSubtarget<AArch64Subtarget>().isXRegisterReservedForRA(i))
439*bdd1243dSDimitry Andric       markSuperRegs(Reserved, AArch64::GPR32commonRegClass.getRegister(i));
440*bdd1243dSDimitry Andric   }
441*bdd1243dSDimitry Andric 
4420b57cec5SDimitry Andric   assert(checkAllSuperRegsMarked(Reserved));
4430b57cec5SDimitry Andric   return Reserved;
4440b57cec5SDimitry Andric }
4450b57cec5SDimitry Andric 
4460b57cec5SDimitry Andric bool AArch64RegisterInfo::isReservedReg(const MachineFunction &MF,
4475ffd83dbSDimitry Andric                                         MCRegister Reg) const {
4480b57cec5SDimitry Andric   return getReservedRegs(MF)[Reg];
4490b57cec5SDimitry Andric }
4500b57cec5SDimitry Andric 
451*bdd1243dSDimitry Andric bool AArch64RegisterInfo::isStrictlyReservedReg(const MachineFunction &MF,
452*bdd1243dSDimitry Andric                                                 MCRegister Reg) const {
453*bdd1243dSDimitry Andric   return getStrictlyReservedRegs(MF)[Reg];
454*bdd1243dSDimitry Andric }
455*bdd1243dSDimitry Andric 
4560b57cec5SDimitry Andric bool AArch64RegisterInfo::isAnyArgRegReserved(const MachineFunction &MF) const {
457e8d8bef9SDimitry Andric   return llvm::any_of(*AArch64::GPR64argRegClass.MC, [this, &MF](MCPhysReg r) {
458*bdd1243dSDimitry Andric     return isStrictlyReservedReg(MF, r);
459e8d8bef9SDimitry Andric   });
4600b57cec5SDimitry Andric }
4610b57cec5SDimitry Andric 
4620b57cec5SDimitry Andric void AArch64RegisterInfo::emitReservedArgRegCallError(
4630b57cec5SDimitry Andric     const MachineFunction &MF) const {
4640b57cec5SDimitry Andric   const Function &F = MF.getFunction();
465e8d8bef9SDimitry Andric   F.getContext().diagnose(DiagnosticInfoUnsupported{F, ("AArch64 doesn't support"
466e8d8bef9SDimitry Andric     " function calls if any of the argument registers is reserved.")});
4670b57cec5SDimitry Andric }
4680b57cec5SDimitry Andric 
4690b57cec5SDimitry Andric bool AArch64RegisterInfo::isAsmClobberable(const MachineFunction &MF,
4705ffd83dbSDimitry Andric                                           MCRegister PhysReg) const {
471*bdd1243dSDimitry Andric   // SLH uses register X16 as the taint register but it will fallback to a different
472*bdd1243dSDimitry Andric   // method if the user clobbers it. So X16 is not reserved for inline asm but is
473*bdd1243dSDimitry Andric   // for normal codegen.
474*bdd1243dSDimitry Andric   if (MF.getFunction().hasFnAttribute(Attribute::SpeculativeLoadHardening) &&
475*bdd1243dSDimitry Andric         MCRegisterInfo::regsOverlap(PhysReg, AArch64::X16))
476*bdd1243dSDimitry Andric     return true;
4770b57cec5SDimitry Andric 
478*bdd1243dSDimitry Andric   return !isReservedReg(MF, PhysReg);
4790b57cec5SDimitry Andric }
4800b57cec5SDimitry Andric 
4810b57cec5SDimitry Andric const TargetRegisterClass *
4820b57cec5SDimitry Andric AArch64RegisterInfo::getPointerRegClass(const MachineFunction &MF,
4830b57cec5SDimitry Andric                                       unsigned Kind) const {
4840b57cec5SDimitry Andric   return &AArch64::GPR64spRegClass;
4850b57cec5SDimitry Andric }
4860b57cec5SDimitry Andric 
4870b57cec5SDimitry Andric const TargetRegisterClass *
4880b57cec5SDimitry Andric AArch64RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
4890b57cec5SDimitry Andric   if (RC == &AArch64::CCRRegClass)
4900b57cec5SDimitry Andric     return &AArch64::GPR64RegClass; // Only MSR & MRS copy NZCV.
4910b57cec5SDimitry Andric   return RC;
4920b57cec5SDimitry Andric }
4930b57cec5SDimitry Andric 
4940b57cec5SDimitry Andric unsigned AArch64RegisterInfo::getBaseRegister() const { return AArch64::X19; }
4950b57cec5SDimitry Andric 
4960b57cec5SDimitry Andric bool AArch64RegisterInfo::hasBasePointer(const MachineFunction &MF) const {
4970b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
4980b57cec5SDimitry Andric 
4990b57cec5SDimitry Andric   // In the presence of variable sized objects or funclets, if the fixed stack
5000b57cec5SDimitry Andric   // size is large enough that referencing from the FP won't result in things
5010b57cec5SDimitry Andric   // being in range relatively often, we can use a base pointer to allow access
5020b57cec5SDimitry Andric   // from the other direction like the SP normally works.
5030b57cec5SDimitry Andric   //
5040b57cec5SDimitry Andric   // Furthermore, if both variable sized objects are present, and the
5050b57cec5SDimitry Andric   // stack needs to be dynamically re-aligned, the base pointer is the only
5060b57cec5SDimitry Andric   // reliable way to reference the locals.
5070b57cec5SDimitry Andric   if (MFI.hasVarSizedObjects() || MF.hasEHFunclets()) {
508fe6060f1SDimitry Andric     if (hasStackRealignment(MF))
5090b57cec5SDimitry Andric       return true;
510979e22ffSDimitry Andric 
511979e22ffSDimitry Andric     if (MF.getSubtarget<AArch64Subtarget>().hasSVE()) {
512979e22ffSDimitry Andric       const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
513979e22ffSDimitry Andric       // Frames that have variable sized objects and scalable SVE objects,
514979e22ffSDimitry Andric       // should always use a basepointer.
515979e22ffSDimitry Andric       if (!AFI->hasCalculatedStackSizeSVE() || AFI->getStackSizeSVE())
516979e22ffSDimitry Andric         return true;
517979e22ffSDimitry Andric     }
518979e22ffSDimitry Andric 
5190b57cec5SDimitry Andric     // Conservatively estimate whether the negative offset from the frame
5200b57cec5SDimitry Andric     // pointer will be sufficient to reach. If a function has a smallish
5210b57cec5SDimitry Andric     // frame, it's less likely to have lots of spills and callee saved
5220b57cec5SDimitry Andric     // space, so it's all more likely to be within range of the frame pointer.
5230b57cec5SDimitry Andric     // If it's wrong, we'll materialize the constant and still get to the
5240b57cec5SDimitry Andric     // object; it's just suboptimal. Negative offsets use the unscaled
5250b57cec5SDimitry Andric     // load/store instructions, which have a 9-bit signed immediate.
5260b57cec5SDimitry Andric     return MFI.getLocalFrameSize() >= 256;
5270b57cec5SDimitry Andric   }
5280b57cec5SDimitry Andric 
5290b57cec5SDimitry Andric   return false;
5300b57cec5SDimitry Andric }
5310b57cec5SDimitry Andric 
53281ad6265SDimitry Andric bool AArch64RegisterInfo::isArgumentRegister(const MachineFunction &MF,
53381ad6265SDimitry Andric                                              MCRegister Reg) const {
53481ad6265SDimitry Andric   CallingConv::ID CC = MF.getFunction().getCallingConv();
53581ad6265SDimitry Andric   const AArch64Subtarget &STI = MF.getSubtarget<AArch64Subtarget>();
53681ad6265SDimitry Andric   bool IsVarArg = STI.isCallingConvWin64(MF.getFunction().getCallingConv());
53781ad6265SDimitry Andric 
53881ad6265SDimitry Andric   auto HasReg = [](ArrayRef<MCRegister> RegList, MCRegister Reg) {
539*bdd1243dSDimitry Andric     return llvm::is_contained(RegList, Reg);
54081ad6265SDimitry Andric   };
54181ad6265SDimitry Andric 
54281ad6265SDimitry Andric   switch (CC) {
54381ad6265SDimitry Andric   default:
54481ad6265SDimitry Andric     report_fatal_error("Unsupported calling convention.");
54581ad6265SDimitry Andric   case CallingConv::WebKit_JS:
54681ad6265SDimitry Andric     return HasReg(CC_AArch64_WebKit_JS_ArgRegs, Reg);
54781ad6265SDimitry Andric   case CallingConv::GHC:
54881ad6265SDimitry Andric     return HasReg(CC_AArch64_GHC_ArgRegs, Reg);
54981ad6265SDimitry Andric   case CallingConv::C:
55081ad6265SDimitry Andric   case CallingConv::Fast:
55181ad6265SDimitry Andric   case CallingConv::PreserveMost:
55281ad6265SDimitry Andric   case CallingConv::CXX_FAST_TLS:
55381ad6265SDimitry Andric   case CallingConv::Swift:
55481ad6265SDimitry Andric   case CallingConv::SwiftTail:
55581ad6265SDimitry Andric   case CallingConv::Tail:
55681ad6265SDimitry Andric     if (STI.isTargetWindows() && IsVarArg)
55781ad6265SDimitry Andric       return HasReg(CC_AArch64_Win64_VarArg_ArgRegs, Reg);
55881ad6265SDimitry Andric     if (!STI.isTargetDarwin()) {
55981ad6265SDimitry Andric       switch (CC) {
56081ad6265SDimitry Andric       default:
56181ad6265SDimitry Andric         return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg);
56281ad6265SDimitry Andric       case CallingConv::Swift:
56381ad6265SDimitry Andric       case CallingConv::SwiftTail:
56481ad6265SDimitry Andric         return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg) ||
56581ad6265SDimitry Andric                HasReg(CC_AArch64_AAPCS_Swift_ArgRegs, Reg);
56681ad6265SDimitry Andric       }
56781ad6265SDimitry Andric     }
56881ad6265SDimitry Andric     if (!IsVarArg) {
56981ad6265SDimitry Andric       switch (CC) {
57081ad6265SDimitry Andric       default:
57181ad6265SDimitry Andric         return HasReg(CC_AArch64_DarwinPCS_ArgRegs, Reg);
57281ad6265SDimitry Andric       case CallingConv::Swift:
57381ad6265SDimitry Andric       case CallingConv::SwiftTail:
57481ad6265SDimitry Andric         return HasReg(CC_AArch64_DarwinPCS_ArgRegs, Reg) ||
57581ad6265SDimitry Andric                HasReg(CC_AArch64_DarwinPCS_Swift_ArgRegs, Reg);
57681ad6265SDimitry Andric       }
57781ad6265SDimitry Andric     }
57881ad6265SDimitry Andric     if (STI.isTargetILP32())
57981ad6265SDimitry Andric       return HasReg(CC_AArch64_DarwinPCS_ILP32_VarArg_ArgRegs, Reg);
58081ad6265SDimitry Andric     return HasReg(CC_AArch64_DarwinPCS_VarArg_ArgRegs, Reg);
58181ad6265SDimitry Andric   case CallingConv::Win64:
58281ad6265SDimitry Andric     if (IsVarArg)
58381ad6265SDimitry Andric       HasReg(CC_AArch64_Win64_VarArg_ArgRegs, Reg);
58481ad6265SDimitry Andric     return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg);
58581ad6265SDimitry Andric   case CallingConv::CFGuard_Check:
58681ad6265SDimitry Andric     return HasReg(CC_AArch64_Win64_CFGuard_Check_ArgRegs, Reg);
58781ad6265SDimitry Andric   case CallingConv::AArch64_VectorCall:
58881ad6265SDimitry Andric   case CallingConv::AArch64_SVE_VectorCall:
589*bdd1243dSDimitry Andric   case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0:
590*bdd1243dSDimitry Andric   case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2:
59181ad6265SDimitry Andric     return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg);
59281ad6265SDimitry Andric   }
59381ad6265SDimitry Andric }
59481ad6265SDimitry Andric 
5950b57cec5SDimitry Andric Register
5960b57cec5SDimitry Andric AArch64RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
5970b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
5980b57cec5SDimitry Andric   return TFI->hasFP(MF) ? AArch64::FP : AArch64::SP;
5990b57cec5SDimitry Andric }
6000b57cec5SDimitry Andric 
6010b57cec5SDimitry Andric bool AArch64RegisterInfo::requiresRegisterScavenging(
6020b57cec5SDimitry Andric     const MachineFunction &MF) const {
6030b57cec5SDimitry Andric   return true;
6040b57cec5SDimitry Andric }
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric bool AArch64RegisterInfo::requiresVirtualBaseRegisters(
6070b57cec5SDimitry Andric     const MachineFunction &MF) const {
6080b57cec5SDimitry Andric   return true;
6090b57cec5SDimitry Andric }
6100b57cec5SDimitry Andric 
6110b57cec5SDimitry Andric bool
6120b57cec5SDimitry Andric AArch64RegisterInfo::useFPForScavengingIndex(const MachineFunction &MF) const {
6130b57cec5SDimitry Andric   // This function indicates whether the emergency spillslot should be placed
6140b57cec5SDimitry Andric   // close to the beginning of the stackframe (closer to FP) or the end
6150b57cec5SDimitry Andric   // (closer to SP).
6160b57cec5SDimitry Andric   //
6170b57cec5SDimitry Andric   // The beginning works most reliably if we have a frame pointer.
618979e22ffSDimitry Andric   // In the presence of any non-constant space between FP and locals,
619979e22ffSDimitry Andric   // (e.g. in case of stack realignment or a scalable SVE area), it is
620979e22ffSDimitry Andric   // better to use SP or BP.
6210b57cec5SDimitry Andric   const AArch64FrameLowering &TFI = *getFrameLowering(MF);
622979e22ffSDimitry Andric   const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
623979e22ffSDimitry Andric   assert((!MF.getSubtarget<AArch64Subtarget>().hasSVE() ||
624979e22ffSDimitry Andric           AFI->hasCalculatedStackSizeSVE()) &&
625979e22ffSDimitry Andric          "Expected SVE area to be calculated by this point");
626fe6060f1SDimitry Andric   return TFI.hasFP(MF) && !hasStackRealignment(MF) && !AFI->getStackSizeSVE();
6270b57cec5SDimitry Andric }
6280b57cec5SDimitry Andric 
6290b57cec5SDimitry Andric bool AArch64RegisterInfo::requiresFrameIndexScavenging(
6300b57cec5SDimitry Andric     const MachineFunction &MF) const {
6310b57cec5SDimitry Andric   return true;
6320b57cec5SDimitry Andric }
6330b57cec5SDimitry Andric 
6340b57cec5SDimitry Andric bool
6350b57cec5SDimitry Andric AArch64RegisterInfo::cannotEliminateFrame(const MachineFunction &MF) const {
6360b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
6370b57cec5SDimitry Andric   if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI.adjustsStack())
6380b57cec5SDimitry Andric     return true;
6390b57cec5SDimitry Andric   return MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken();
6400b57cec5SDimitry Andric }
6410b57cec5SDimitry Andric 
6420b57cec5SDimitry Andric /// needsFrameBaseReg - Returns true if the instruction's frame index
6430b57cec5SDimitry Andric /// reference would be better served by a base register other than FP
6440b57cec5SDimitry Andric /// or SP. Used by LocalStackFrameAllocation to determine which frame index
6450b57cec5SDimitry Andric /// references it should create new base registers for.
6460b57cec5SDimitry Andric bool AArch64RegisterInfo::needsFrameBaseReg(MachineInstr *MI,
6470b57cec5SDimitry Andric                                             int64_t Offset) const {
6480b57cec5SDimitry Andric   for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i)
6490b57cec5SDimitry Andric     assert(i < MI->getNumOperands() &&
6500b57cec5SDimitry Andric            "Instr doesn't have FrameIndex operand!");
6510b57cec5SDimitry Andric 
6520b57cec5SDimitry Andric   // It's the load/store FI references that cause issues, as it can be difficult
6530b57cec5SDimitry Andric   // to materialize the offset if it won't fit in the literal field. Estimate
6540b57cec5SDimitry Andric   // based on the size of the local frame and some conservative assumptions
6550b57cec5SDimitry Andric   // about the rest of the stack frame (note, this is pre-regalloc, so
6560b57cec5SDimitry Andric   // we don't know everything for certain yet) whether this offset is likely
6570b57cec5SDimitry Andric   // to be out of range of the immediate. Return true if so.
6580b57cec5SDimitry Andric 
6590b57cec5SDimitry Andric   // We only generate virtual base registers for loads and stores, so
6600b57cec5SDimitry Andric   // return false for everything else.
6610b57cec5SDimitry Andric   if (!MI->mayLoad() && !MI->mayStore())
6620b57cec5SDimitry Andric     return false;
6630b57cec5SDimitry Andric 
6640b57cec5SDimitry Andric   // Without a virtual base register, if the function has variable sized
6650b57cec5SDimitry Andric   // objects, all fixed-size local references will be via the frame pointer,
6660b57cec5SDimitry Andric   // Approximate the offset and see if it's legal for the instruction.
6670b57cec5SDimitry Andric   // Note that the incoming offset is based on the SP value at function entry,
6680b57cec5SDimitry Andric   // so it'll be negative.
6690b57cec5SDimitry Andric   MachineFunction &MF = *MI->getParent()->getParent();
6700b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
6710b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
6720b57cec5SDimitry Andric 
6730b57cec5SDimitry Andric   // Estimate an offset from the frame pointer.
6740b57cec5SDimitry Andric   // Conservatively assume all GPR callee-saved registers get pushed.
6750b57cec5SDimitry Andric   // FP, LR, X19-X28, D8-D15. 64-bits each.
6760b57cec5SDimitry Andric   int64_t FPOffset = Offset - 16 * 20;
6770b57cec5SDimitry Andric   // Estimate an offset from the stack pointer.
6780b57cec5SDimitry Andric   // The incoming offset is relating to the SP at the start of the function,
6790b57cec5SDimitry Andric   // but when we access the local it'll be relative to the SP after local
6800b57cec5SDimitry Andric   // allocation, so adjust our SP-relative offset by that allocation size.
6810b57cec5SDimitry Andric   Offset += MFI.getLocalFrameSize();
6820b57cec5SDimitry Andric   // Assume that we'll have at least some spill slots allocated.
6830b57cec5SDimitry Andric   // FIXME: This is a total SWAG number. We should run some statistics
6840b57cec5SDimitry Andric   //        and pick a real one.
6850b57cec5SDimitry Andric   Offset += 128; // 128 bytes of spill slots
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric   // If there is a frame pointer, try using it.
6880b57cec5SDimitry Andric   // The FP is only available if there is no dynamic realignment. We
6890b57cec5SDimitry Andric   // don't know for sure yet whether we'll need that, so we guess based
6900b57cec5SDimitry Andric   // on whether there are any local variables that would trigger it.
6910b57cec5SDimitry Andric   if (TFI->hasFP(MF) && isFrameOffsetLegal(MI, AArch64::FP, FPOffset))
6920b57cec5SDimitry Andric     return false;
6930b57cec5SDimitry Andric 
6940b57cec5SDimitry Andric   // If we can reference via the stack pointer or base pointer, try that.
6950b57cec5SDimitry Andric   // FIXME: This (and the code that resolves the references) can be improved
6960b57cec5SDimitry Andric   //        to only disallow SP relative references in the live range of
6970b57cec5SDimitry Andric   //        the VLA(s). In practice, it's unclear how much difference that
6980b57cec5SDimitry Andric   //        would make, but it may be worth doing.
6990b57cec5SDimitry Andric   if (isFrameOffsetLegal(MI, AArch64::SP, Offset))
7000b57cec5SDimitry Andric     return false;
7010b57cec5SDimitry Andric 
7025ffd83dbSDimitry Andric   // If even offset 0 is illegal, we don't want a virtual base register.
7035ffd83dbSDimitry Andric   if (!isFrameOffsetLegal(MI, AArch64::SP, 0))
7045ffd83dbSDimitry Andric     return false;
7055ffd83dbSDimitry Andric 
7060b57cec5SDimitry Andric   // The offset likely isn't legal; we want to allocate a virtual base register.
7070b57cec5SDimitry Andric   return true;
7080b57cec5SDimitry Andric }
7090b57cec5SDimitry Andric 
7100b57cec5SDimitry Andric bool AArch64RegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
7115ffd83dbSDimitry Andric                                              Register BaseReg,
7120b57cec5SDimitry Andric                                              int64_t Offset) const {
7130b57cec5SDimitry Andric   assert(MI && "Unable to get the legal offset for nil instruction.");
714e8d8bef9SDimitry Andric   StackOffset SaveOffset = StackOffset::getFixed(Offset);
7150b57cec5SDimitry Andric   return isAArch64FrameOffsetLegal(*MI, SaveOffset) & AArch64FrameOffsetIsLegal;
7160b57cec5SDimitry Andric }
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric /// Insert defining instruction(s) for BaseReg to be a pointer to FrameIdx
7190b57cec5SDimitry Andric /// at the beginning of the basic block.
720e8d8bef9SDimitry Andric Register
721e8d8bef9SDimitry Andric AArch64RegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB,
7220b57cec5SDimitry Andric                                                   int FrameIdx,
7230b57cec5SDimitry Andric                                                   int64_t Offset) const {
7240b57cec5SDimitry Andric   MachineBasicBlock::iterator Ins = MBB->begin();
7250b57cec5SDimitry Andric   DebugLoc DL; // Defaults to "unknown"
7260b57cec5SDimitry Andric   if (Ins != MBB->end())
7270b57cec5SDimitry Andric     DL = Ins->getDebugLoc();
7280b57cec5SDimitry Andric   const MachineFunction &MF = *MBB->getParent();
7290b57cec5SDimitry Andric   const AArch64InstrInfo *TII =
7300b57cec5SDimitry Andric       MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
7310b57cec5SDimitry Andric   const MCInstrDesc &MCID = TII->get(AArch64::ADDXri);
7320b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
733e8d8bef9SDimitry Andric   Register BaseReg = MRI.createVirtualRegister(&AArch64::GPR64spRegClass);
7340b57cec5SDimitry Andric   MRI.constrainRegClass(BaseReg, TII->getRegClass(MCID, 0, this, MF));
7350b57cec5SDimitry Andric   unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0);
7360b57cec5SDimitry Andric 
7370b57cec5SDimitry Andric   BuildMI(*MBB, Ins, DL, MCID, BaseReg)
7380b57cec5SDimitry Andric       .addFrameIndex(FrameIdx)
7390b57cec5SDimitry Andric       .addImm(Offset)
7400b57cec5SDimitry Andric       .addImm(Shifter);
741e8d8bef9SDimitry Andric 
742e8d8bef9SDimitry Andric   return BaseReg;
7430b57cec5SDimitry Andric }
7440b57cec5SDimitry Andric 
7455ffd83dbSDimitry Andric void AArch64RegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg,
7460b57cec5SDimitry Andric                                             int64_t Offset) const {
7478bcb0991SDimitry Andric   // ARM doesn't need the general 64-bit offsets
748e8d8bef9SDimitry Andric   StackOffset Off = StackOffset::getFixed(Offset);
7498bcb0991SDimitry Andric 
7500b57cec5SDimitry Andric   unsigned i = 0;
7510b57cec5SDimitry Andric   while (!MI.getOperand(i).isFI()) {
7520b57cec5SDimitry Andric     ++i;
7530b57cec5SDimitry Andric     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
7540b57cec5SDimitry Andric   }
755e8d8bef9SDimitry Andric 
7560b57cec5SDimitry Andric   const MachineFunction *MF = MI.getParent()->getParent();
7570b57cec5SDimitry Andric   const AArch64InstrInfo *TII =
7580b57cec5SDimitry Andric       MF->getSubtarget<AArch64Subtarget>().getInstrInfo();
7590b57cec5SDimitry Andric   bool Done = rewriteAArch64FrameIndex(MI, i, BaseReg, Off, TII);
7600b57cec5SDimitry Andric   assert(Done && "Unable to resolve frame index!");
7610b57cec5SDimitry Andric   (void)Done;
7620b57cec5SDimitry Andric }
7630b57cec5SDimitry Andric 
7645ffd83dbSDimitry Andric // Create a scratch register for the frame index elimination in an instruction.
7655ffd83dbSDimitry Andric // This function has special handling of stack tagging loop pseudos, in which
76681ad6265SDimitry Andric // case it can also change the instruction opcode.
7675ffd83dbSDimitry Andric static Register
76881ad6265SDimitry Andric createScratchRegisterForInstruction(MachineInstr &MI, unsigned FIOperandNum,
7695ffd83dbSDimitry Andric                                     const AArch64InstrInfo *TII) {
7705ffd83dbSDimitry Andric   // ST*Gloop have a reserved scratch register in operand 1. Use it, and also
7715ffd83dbSDimitry Andric   // replace the instruction with the writeback variant because it will now
7725ffd83dbSDimitry Andric   // satisfy the operand constraints for it.
77381ad6265SDimitry Andric   Register ScratchReg;
77481ad6265SDimitry Andric   if (MI.getOpcode() == AArch64::STGloop ||
77581ad6265SDimitry Andric       MI.getOpcode() == AArch64::STZGloop) {
77681ad6265SDimitry Andric     assert(FIOperandNum == 3 &&
77781ad6265SDimitry Andric            "Wrong frame index operand for STGloop/STZGloop");
77881ad6265SDimitry Andric     unsigned Op = MI.getOpcode() == AArch64::STGloop ? AArch64::STGloop_wback
77981ad6265SDimitry Andric                                                      : AArch64::STZGloop_wback;
78081ad6265SDimitry Andric     ScratchReg = MI.getOperand(1).getReg();
78181ad6265SDimitry Andric     MI.getOperand(3).ChangeToRegister(ScratchReg, false, false, true);
78281ad6265SDimitry Andric     MI.setDesc(TII->get(Op));
78381ad6265SDimitry Andric     MI.tieOperands(1, 3);
7845ffd83dbSDimitry Andric   } else {
78581ad6265SDimitry Andric     ScratchReg =
78681ad6265SDimitry Andric         MI.getMF()->getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass);
78781ad6265SDimitry Andric     MI.getOperand(FIOperandNum)
78881ad6265SDimitry Andric         .ChangeToRegister(ScratchReg, false, false, true);
7895ffd83dbSDimitry Andric   }
79081ad6265SDimitry Andric   return ScratchReg;
7915ffd83dbSDimitry Andric }
7925ffd83dbSDimitry Andric 
793e8d8bef9SDimitry Andric void AArch64RegisterInfo::getOffsetOpcodes(
794e8d8bef9SDimitry Andric     const StackOffset &Offset, SmallVectorImpl<uint64_t> &Ops) const {
795e8d8bef9SDimitry Andric   // The smallest scalable element supported by scaled SVE addressing
796e8d8bef9SDimitry Andric   // modes are predicates, which are 2 scalable bytes in size. So the scalable
797e8d8bef9SDimitry Andric   // byte offset must always be a multiple of 2.
798e8d8bef9SDimitry Andric   assert(Offset.getScalable() % 2 == 0 && "Invalid frame offset");
799e8d8bef9SDimitry Andric 
800e8d8bef9SDimitry Andric   // Add fixed-sized offset using existing DIExpression interface.
801e8d8bef9SDimitry Andric   DIExpression::appendOffset(Ops, Offset.getFixed());
802e8d8bef9SDimitry Andric 
803e8d8bef9SDimitry Andric   unsigned VG = getDwarfRegNum(AArch64::VG, true);
804e8d8bef9SDimitry Andric   int64_t VGSized = Offset.getScalable() / 2;
805e8d8bef9SDimitry Andric   if (VGSized > 0) {
806e8d8bef9SDimitry Andric     Ops.push_back(dwarf::DW_OP_constu);
807e8d8bef9SDimitry Andric     Ops.push_back(VGSized);
808e8d8bef9SDimitry Andric     Ops.append({dwarf::DW_OP_bregx, VG, 0ULL});
809e8d8bef9SDimitry Andric     Ops.push_back(dwarf::DW_OP_mul);
810e8d8bef9SDimitry Andric     Ops.push_back(dwarf::DW_OP_plus);
811e8d8bef9SDimitry Andric   } else if (VGSized < 0) {
812e8d8bef9SDimitry Andric     Ops.push_back(dwarf::DW_OP_constu);
813e8d8bef9SDimitry Andric     Ops.push_back(-VGSized);
814e8d8bef9SDimitry Andric     Ops.append({dwarf::DW_OP_bregx, VG, 0ULL});
815e8d8bef9SDimitry Andric     Ops.push_back(dwarf::DW_OP_mul);
816e8d8bef9SDimitry Andric     Ops.push_back(dwarf::DW_OP_minus);
817e8d8bef9SDimitry Andric   }
818e8d8bef9SDimitry Andric }
819e8d8bef9SDimitry Andric 
820*bdd1243dSDimitry Andric bool AArch64RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
8210b57cec5SDimitry Andric                                               int SPAdj, unsigned FIOperandNum,
8220b57cec5SDimitry Andric                                               RegScavenger *RS) const {
8230b57cec5SDimitry Andric   assert(SPAdj == 0 && "Unexpected");
8240b57cec5SDimitry Andric 
8250b57cec5SDimitry Andric   MachineInstr &MI = *II;
8260b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
8270b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
8288bcb0991SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
8290b57cec5SDimitry Andric   const AArch64InstrInfo *TII =
8300b57cec5SDimitry Andric       MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
8310b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
8320b57cec5SDimitry Andric   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
8338bcb0991SDimitry Andric   bool Tagged =
8348bcb0991SDimitry Andric       MI.getOperand(FIOperandNum).getTargetFlags() & AArch64II::MO_TAGGED;
8355ffd83dbSDimitry Andric   Register FrameReg;
8360b57cec5SDimitry Andric 
837e8d8bef9SDimitry Andric   // Special handling of dbg_value, stackmap patchpoint statepoint instructions.
838e8d8bef9SDimitry Andric   if (MI.getOpcode() == TargetOpcode::STACKMAP ||
839e8d8bef9SDimitry Andric       MI.getOpcode() == TargetOpcode::PATCHPOINT ||
840e8d8bef9SDimitry Andric       MI.getOpcode() == TargetOpcode::STATEPOINT) {
8418bcb0991SDimitry Andric     StackOffset Offset =
8428bcb0991SDimitry Andric         TFI->resolveFrameIndexReference(MF, FrameIndex, FrameReg,
8430b57cec5SDimitry Andric                                         /*PreferFP=*/true,
8440b57cec5SDimitry Andric                                         /*ForSimm=*/false);
845e8d8bef9SDimitry Andric     Offset += StackOffset::getFixed(MI.getOperand(FIOperandNum + 1).getImm());
8460b57cec5SDimitry Andric     MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false /*isDef*/);
847e8d8bef9SDimitry Andric     MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset.getFixed());
848*bdd1243dSDimitry Andric     return false;
8490b57cec5SDimitry Andric   }
8500b57cec5SDimitry Andric 
8510b57cec5SDimitry Andric   if (MI.getOpcode() == TargetOpcode::LOCAL_ESCAPE) {
8520b57cec5SDimitry Andric     MachineOperand &FI = MI.getOperand(FIOperandNum);
853e8d8bef9SDimitry Andric     StackOffset Offset = TFI->getNonLocalFrameIndexReference(MF, FrameIndex);
854e8d8bef9SDimitry Andric     assert(!Offset.getScalable() &&
855e8d8bef9SDimitry Andric            "Frame offsets with a scalable component are not supported");
856e8d8bef9SDimitry Andric     FI.ChangeToImmediate(Offset.getFixed());
857*bdd1243dSDimitry Andric     return false;
8580b57cec5SDimitry Andric   }
8590b57cec5SDimitry Andric 
8608bcb0991SDimitry Andric   StackOffset Offset;
8610b57cec5SDimitry Andric   if (MI.getOpcode() == AArch64::TAGPstack) {
8620b57cec5SDimitry Andric     // TAGPstack must use the virtual frame register in its 3rd operand.
8630b57cec5SDimitry Andric     const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
8640b57cec5SDimitry Andric     FrameReg = MI.getOperand(3).getReg();
865e8d8bef9SDimitry Andric     Offset = StackOffset::getFixed(MFI.getObjectOffset(FrameIndex) +
866e8d8bef9SDimitry Andric                                       AFI->getTaggedBasePointerOffset());
8678bcb0991SDimitry Andric   } else if (Tagged) {
868e8d8bef9SDimitry Andric     StackOffset SPOffset = StackOffset::getFixed(
869e8d8bef9SDimitry Andric         MFI.getObjectOffset(FrameIndex) + (int64_t)MFI.getStackSize());
8708bcb0991SDimitry Andric     if (MFI.hasVarSizedObjects() ||
8718bcb0991SDimitry Andric         isAArch64FrameOffsetLegal(MI, SPOffset, nullptr, nullptr, nullptr) !=
8728bcb0991SDimitry Andric             (AArch64FrameOffsetCanUpdate | AArch64FrameOffsetIsLegal)) {
8738bcb0991SDimitry Andric       // Can't update to SP + offset in place. Precalculate the tagged pointer
8748bcb0991SDimitry Andric       // in a scratch register.
8758bcb0991SDimitry Andric       Offset = TFI->resolveFrameIndexReference(
8768bcb0991SDimitry Andric           MF, FrameIndex, FrameReg, /*PreferFP=*/false, /*ForSimm=*/true);
8778bcb0991SDimitry Andric       Register ScratchReg =
8788bcb0991SDimitry Andric           MF.getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass);
8798bcb0991SDimitry Andric       emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset,
8808bcb0991SDimitry Andric                       TII);
8818bcb0991SDimitry Andric       BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(AArch64::LDG), ScratchReg)
8828bcb0991SDimitry Andric           .addReg(ScratchReg)
8838bcb0991SDimitry Andric           .addReg(ScratchReg)
8848bcb0991SDimitry Andric           .addImm(0);
8858bcb0991SDimitry Andric       MI.getOperand(FIOperandNum)
8868bcb0991SDimitry Andric           .ChangeToRegister(ScratchReg, false, false, true);
887*bdd1243dSDimitry Andric       return false;
8888bcb0991SDimitry Andric     }
8898bcb0991SDimitry Andric     FrameReg = AArch64::SP;
890e8d8bef9SDimitry Andric     Offset = StackOffset::getFixed(MFI.getObjectOffset(FrameIndex) +
891e8d8bef9SDimitry Andric                                    (int64_t)MFI.getStackSize());
8920b57cec5SDimitry Andric   } else {
8930b57cec5SDimitry Andric     Offset = TFI->resolveFrameIndexReference(
8940b57cec5SDimitry Andric         MF, FrameIndex, FrameReg, /*PreferFP=*/false, /*ForSimm=*/true);
8950b57cec5SDimitry Andric   }
8960b57cec5SDimitry Andric 
8970b57cec5SDimitry Andric   // Modify MI as necessary to handle as much of 'Offset' as possible
8980b57cec5SDimitry Andric   if (rewriteAArch64FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII))
899*bdd1243dSDimitry Andric     return true;
9000b57cec5SDimitry Andric 
9010b57cec5SDimitry Andric   assert((!RS || !RS->isScavengingFrameIndex(FrameIndex)) &&
9020b57cec5SDimitry Andric          "Emergency spill slot is out of reach");
9030b57cec5SDimitry Andric 
9040b57cec5SDimitry Andric   // If we get here, the immediate doesn't fit into the instruction.  We folded
9050b57cec5SDimitry Andric   // as much as possible above.  Handle the rest, providing a register that is
9060b57cec5SDimitry Andric   // SP+LargeImm.
90781ad6265SDimitry Andric   Register ScratchReg =
90881ad6265SDimitry Andric       createScratchRegisterForInstruction(MI, FIOperandNum, TII);
9090b57cec5SDimitry Andric   emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset, TII);
910*bdd1243dSDimitry Andric   return false;
9110b57cec5SDimitry Andric }
9120b57cec5SDimitry Andric 
9130b57cec5SDimitry Andric unsigned AArch64RegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
9140b57cec5SDimitry Andric                                                   MachineFunction &MF) const {
9150b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
9160b57cec5SDimitry Andric 
9170b57cec5SDimitry Andric   switch (RC->getID()) {
9180b57cec5SDimitry Andric   default:
9190b57cec5SDimitry Andric     return 0;
9200b57cec5SDimitry Andric   case AArch64::GPR32RegClassID:
9210b57cec5SDimitry Andric   case AArch64::GPR32spRegClassID:
9220b57cec5SDimitry Andric   case AArch64::GPR32allRegClassID:
9230b57cec5SDimitry Andric   case AArch64::GPR64spRegClassID:
9240b57cec5SDimitry Andric   case AArch64::GPR64allRegClassID:
9250b57cec5SDimitry Andric   case AArch64::GPR64RegClassID:
9260b57cec5SDimitry Andric   case AArch64::GPR32commonRegClassID:
9270b57cec5SDimitry Andric   case AArch64::GPR64commonRegClassID:
9280b57cec5SDimitry Andric     return 32 - 1                                   // XZR/SP
9290b57cec5SDimitry Andric               - (TFI->hasFP(MF) || TT.isOSDarwin()) // FP
9300b57cec5SDimitry Andric               - MF.getSubtarget<AArch64Subtarget>().getNumXRegisterReserved()
9310b57cec5SDimitry Andric               - hasBasePointer(MF);  // X19
9320b57cec5SDimitry Andric   case AArch64::FPR8RegClassID:
9330b57cec5SDimitry Andric   case AArch64::FPR16RegClassID:
9340b57cec5SDimitry Andric   case AArch64::FPR32RegClassID:
9350b57cec5SDimitry Andric   case AArch64::FPR64RegClassID:
9360b57cec5SDimitry Andric   case AArch64::FPR128RegClassID:
9370b57cec5SDimitry Andric     return 32;
9380b57cec5SDimitry Andric 
939*bdd1243dSDimitry Andric   case AArch64::MatrixIndexGPR32_8_11RegClassID:
940fe6060f1SDimitry Andric   case AArch64::MatrixIndexGPR32_12_15RegClassID:
941fe6060f1SDimitry Andric     return 4;
942fe6060f1SDimitry Andric 
9430b57cec5SDimitry Andric   case AArch64::DDRegClassID:
9440b57cec5SDimitry Andric   case AArch64::DDDRegClassID:
9450b57cec5SDimitry Andric   case AArch64::DDDDRegClassID:
9460b57cec5SDimitry Andric   case AArch64::QQRegClassID:
9470b57cec5SDimitry Andric   case AArch64::QQQRegClassID:
9480b57cec5SDimitry Andric   case AArch64::QQQQRegClassID:
9490b57cec5SDimitry Andric     return 32;
9500b57cec5SDimitry Andric 
9510b57cec5SDimitry Andric   case AArch64::FPR128_loRegClassID:
9525ffd83dbSDimitry Andric   case AArch64::FPR64_loRegClassID:
9535ffd83dbSDimitry Andric   case AArch64::FPR16_loRegClassID:
9540b57cec5SDimitry Andric     return 16;
9550b57cec5SDimitry Andric   }
9560b57cec5SDimitry Andric }
9570b57cec5SDimitry Andric 
9580b57cec5SDimitry Andric unsigned AArch64RegisterInfo::getLocalAddressRegister(
9590b57cec5SDimitry Andric   const MachineFunction &MF) const {
9600b57cec5SDimitry Andric   const auto &MFI = MF.getFrameInfo();
9610b57cec5SDimitry Andric   if (!MF.hasEHFunclets() && !MFI.hasVarSizedObjects())
9620b57cec5SDimitry Andric     return AArch64::SP;
963fe6060f1SDimitry Andric   else if (hasStackRealignment(MF))
9640b57cec5SDimitry Andric     return getBaseRegister();
9650b57cec5SDimitry Andric   return getFrameRegister(MF);
9660b57cec5SDimitry Andric }
967e8d8bef9SDimitry Andric 
968e8d8bef9SDimitry Andric /// SrcRC and DstRC will be morphed into NewRC if this returns true
969e8d8bef9SDimitry Andric bool AArch64RegisterInfo::shouldCoalesce(
970e8d8bef9SDimitry Andric     MachineInstr *MI, const TargetRegisterClass *SrcRC, unsigned SubReg,
971e8d8bef9SDimitry Andric     const TargetRegisterClass *DstRC, unsigned DstSubReg,
972e8d8bef9SDimitry Andric     const TargetRegisterClass *NewRC, LiveIntervals &LIS) const {
973e8d8bef9SDimitry Andric   if (MI->isCopy() &&
974e8d8bef9SDimitry Andric       ((DstRC->getID() == AArch64::GPR64RegClassID) ||
975e8d8bef9SDimitry Andric        (DstRC->getID() == AArch64::GPR64commonRegClassID)) &&
976e8d8bef9SDimitry Andric       MI->getOperand(0).getSubReg() && MI->getOperand(1).getSubReg())
977e8d8bef9SDimitry Andric     // Do not coalesce in the case of a 32-bit subregister copy
978e8d8bef9SDimitry Andric     // which implements a 32 to 64 bit zero extension
979e8d8bef9SDimitry Andric     // which relies on the upper 32 bits being zeroed.
980e8d8bef9SDimitry Andric     return false;
981e8d8bef9SDimitry Andric   return true;
982e8d8bef9SDimitry Andric }
983