xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AArch64/AArch64RegisterInfo.cpp (revision 979e22ff1ac2a50acbf94e28576a058db89003b5)
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"
188bcb0991SDimitry Andric #include "AArch64StackOffset.h"
190b57cec5SDimitry Andric #include "AArch64Subtarget.h"
200b57cec5SDimitry Andric #include "MCTargetDesc/AArch64AddressingModes.h"
210b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
220b57cec5SDimitry Andric #include "llvm/ADT/Triple.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
288bcb0991SDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
298bcb0991SDimitry Andric #include "llvm/IR/Function.h"
308bcb0991SDimitry Andric #include "llvm/Support/raw_ostream.h"
310b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric using namespace llvm;
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric #define GET_REGINFO_TARGET_DESC
360b57cec5SDimitry Andric #include "AArch64GenRegisterInfo.inc"
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric AArch64RegisterInfo::AArch64RegisterInfo(const Triple &TT)
390b57cec5SDimitry Andric     : AArch64GenRegisterInfo(AArch64::LR), TT(TT) {
400b57cec5SDimitry Andric   AArch64_MC::initLLVMToCVRegMapping(this);
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric 
43*979e22ffSDimitry Andric static bool hasSVEArgsOrReturn(const MachineFunction *MF) {
44*979e22ffSDimitry Andric   const Function &F = MF->getFunction();
45*979e22ffSDimitry Andric   return isa<ScalableVectorType>(F.getReturnType()) ||
46*979e22ffSDimitry Andric          any_of(F.args(), [](const Argument &Arg) {
47*979e22ffSDimitry Andric            return isa<ScalableVectorType>(Arg.getType());
48*979e22ffSDimitry Andric          });
49*979e22ffSDimitry Andric }
50*979e22ffSDimitry Andric 
510b57cec5SDimitry Andric const MCPhysReg *
520b57cec5SDimitry Andric AArch64RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
530b57cec5SDimitry Andric   assert(MF && "Invalid MachineFunction pointer.");
545ffd83dbSDimitry Andric 
550b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::GHC)
560b57cec5SDimitry Andric     // GHC set of callee saved regs is empty as all those regs are
570b57cec5SDimitry Andric     // used for passing STG regs around
580b57cec5SDimitry Andric     return CSR_AArch64_NoRegs_SaveList;
590b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AnyReg)
600b57cec5SDimitry Andric     return CSR_AArch64_AllRegs_SaveList;
615ffd83dbSDimitry Andric 
625ffd83dbSDimitry Andric   // Darwin has its own CSR_AArch64_AAPCS_SaveList, which means most CSR save
635ffd83dbSDimitry Andric   // lists depending on that will need to have their Darwin variant as well.
645ffd83dbSDimitry Andric   if (MF->getSubtarget<AArch64Subtarget>().isTargetDarwin())
655ffd83dbSDimitry Andric     return getDarwinCalleeSavedRegs(MF);
665ffd83dbSDimitry Andric 
675ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::CFGuard_Check)
685ffd83dbSDimitry Andric     return CSR_Win_AArch64_CFGuard_Check_SaveList;
695ffd83dbSDimitry Andric   if (MF->getSubtarget<AArch64Subtarget>().isTargetWindows())
705ffd83dbSDimitry Andric     return CSR_Win_AArch64_AAPCS_SaveList;
710b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AArch64_VectorCall)
720b57cec5SDimitry Andric     return CSR_AArch64_AAVPCS_SaveList;
73480093f4SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AArch64_SVE_VectorCall)
74480093f4SDimitry Andric     return CSR_AArch64_SVE_AAPCS_SaveList;
750b57cec5SDimitry Andric   if (MF->getSubtarget<AArch64Subtarget>().getTargetLowering()
760b57cec5SDimitry Andric           ->supportSwiftError() &&
770b57cec5SDimitry Andric       MF->getFunction().getAttributes().hasAttrSomewhere(
780b57cec5SDimitry Andric           Attribute::SwiftError))
790b57cec5SDimitry Andric     return CSR_AArch64_AAPCS_SwiftError_SaveList;
800b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::PreserveMost)
810b57cec5SDimitry Andric     return CSR_AArch64_RT_MostRegs_SaveList;
825ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::Win64)
835ffd83dbSDimitry Andric     // This is for OSes other than Windows; Windows is a separate case further
845ffd83dbSDimitry Andric     // above.
855ffd83dbSDimitry Andric     return CSR_AArch64_AAPCS_X18_SaveList;
86*979e22ffSDimitry Andric   if (hasSVEArgsOrReturn(MF))
87*979e22ffSDimitry Andric     return CSR_AArch64_SVE_AAPCS_SaveList;
880b57cec5SDimitry Andric   return CSR_AArch64_AAPCS_SaveList;
890b57cec5SDimitry Andric }
900b57cec5SDimitry Andric 
915ffd83dbSDimitry Andric const MCPhysReg *
925ffd83dbSDimitry Andric AArch64RegisterInfo::getDarwinCalleeSavedRegs(const MachineFunction *MF) const {
935ffd83dbSDimitry Andric   assert(MF && "Invalid MachineFunction pointer.");
945ffd83dbSDimitry Andric   assert(MF->getSubtarget<AArch64Subtarget>().isTargetDarwin() &&
955ffd83dbSDimitry Andric          "Invalid subtarget for getDarwinCalleeSavedRegs");
965ffd83dbSDimitry Andric 
975ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::CFGuard_Check)
985ffd83dbSDimitry Andric     report_fatal_error(
995ffd83dbSDimitry Andric         "Calling convention CFGuard_Check is unsupported on Darwin.");
1005ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AArch64_VectorCall)
1015ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_AAVPCS_SaveList;
1025ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AArch64_SVE_VectorCall)
1035ffd83dbSDimitry Andric     report_fatal_error(
1045ffd83dbSDimitry Andric         "Calling convention SVE_VectorCall is unsupported on Darwin.");
1055ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS)
1065ffd83dbSDimitry Andric     return MF->getInfo<AArch64FunctionInfo>()->isSplitCSR()
1075ffd83dbSDimitry Andric                ? CSR_Darwin_AArch64_CXX_TLS_PE_SaveList
1085ffd83dbSDimitry Andric                : CSR_Darwin_AArch64_CXX_TLS_SaveList;
1095ffd83dbSDimitry Andric   if (MF->getSubtarget<AArch64Subtarget>().getTargetLowering()
1105ffd83dbSDimitry Andric           ->supportSwiftError() &&
1115ffd83dbSDimitry Andric       MF->getFunction().getAttributes().hasAttrSomewhere(
1125ffd83dbSDimitry Andric           Attribute::SwiftError))
1135ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_AAPCS_SwiftError_SaveList;
1145ffd83dbSDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::PreserveMost)
1155ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_RT_MostRegs_SaveList;
1165ffd83dbSDimitry Andric   return CSR_Darwin_AArch64_AAPCS_SaveList;
1175ffd83dbSDimitry Andric }
1185ffd83dbSDimitry Andric 
1190b57cec5SDimitry Andric const MCPhysReg *AArch64RegisterInfo::getCalleeSavedRegsViaCopy(
1200b57cec5SDimitry Andric     const MachineFunction *MF) const {
1210b57cec5SDimitry Andric   assert(MF && "Invalid MachineFunction pointer.");
1220b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS &&
1230b57cec5SDimitry Andric       MF->getInfo<AArch64FunctionInfo>()->isSplitCSR())
1245ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_CXX_TLS_ViaCopy_SaveList;
1250b57cec5SDimitry Andric   return nullptr;
1260b57cec5SDimitry Andric }
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric void AArch64RegisterInfo::UpdateCustomCalleeSavedRegs(
1290b57cec5SDimitry Andric     MachineFunction &MF) const {
1300b57cec5SDimitry Andric   const MCPhysReg *CSRs = getCalleeSavedRegs(&MF);
1310b57cec5SDimitry Andric   SmallVector<MCPhysReg, 32> UpdatedCSRs;
1320b57cec5SDimitry Andric   for (const MCPhysReg *I = CSRs; *I; ++I)
1330b57cec5SDimitry Andric     UpdatedCSRs.push_back(*I);
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric   for (size_t i = 0; i < AArch64::GPR64commonRegClass.getNumRegs(); ++i) {
1360b57cec5SDimitry Andric     if (MF.getSubtarget<AArch64Subtarget>().isXRegCustomCalleeSaved(i)) {
1370b57cec5SDimitry Andric       UpdatedCSRs.push_back(AArch64::GPR64commonRegClass.getRegister(i));
1380b57cec5SDimitry Andric     }
1390b57cec5SDimitry Andric   }
1400b57cec5SDimitry Andric   // Register lists are zero-terminated.
1410b57cec5SDimitry Andric   UpdatedCSRs.push_back(0);
1420b57cec5SDimitry Andric   MF.getRegInfo().setCalleeSavedRegs(UpdatedCSRs);
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric const TargetRegisterClass *
1460b57cec5SDimitry Andric AArch64RegisterInfo::getSubClassWithSubReg(const TargetRegisterClass *RC,
1470b57cec5SDimitry Andric                                        unsigned Idx) const {
1480b57cec5SDimitry Andric   // edge case for GPR/FPR register classes
1490b57cec5SDimitry Andric   if (RC == &AArch64::GPR32allRegClass && Idx == AArch64::hsub)
1500b57cec5SDimitry Andric     return &AArch64::FPR32RegClass;
1510b57cec5SDimitry Andric   else if (RC == &AArch64::GPR64allRegClass && Idx == AArch64::hsub)
1520b57cec5SDimitry Andric     return &AArch64::FPR64RegClass;
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   // Forward to TableGen's default version.
1550b57cec5SDimitry Andric   return AArch64GenRegisterInfo::getSubClassWithSubReg(RC, Idx);
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric const uint32_t *
1595ffd83dbSDimitry Andric AArch64RegisterInfo::getDarwinCallPreservedMask(const MachineFunction &MF,
1605ffd83dbSDimitry Andric                                                 CallingConv::ID CC) const {
1615ffd83dbSDimitry Andric   assert(MF.getSubtarget<AArch64Subtarget>().isTargetDarwin() &&
1625ffd83dbSDimitry Andric          "Invalid subtarget for getDarwinCallPreservedMask");
1635ffd83dbSDimitry Andric 
1645ffd83dbSDimitry Andric   if (CC == CallingConv::CXX_FAST_TLS)
1655ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_CXX_TLS_RegMask;
1665ffd83dbSDimitry Andric   if (CC == CallingConv::AArch64_VectorCall)
1675ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_AAVPCS_RegMask;
1685ffd83dbSDimitry Andric   if (CC == CallingConv::AArch64_SVE_VectorCall)
1695ffd83dbSDimitry Andric     report_fatal_error(
1705ffd83dbSDimitry Andric         "Calling convention SVE_VectorCall is unsupported on Darwin.");
1715ffd83dbSDimitry Andric   if (CC == CallingConv::CFGuard_Check)
1725ffd83dbSDimitry Andric     report_fatal_error(
1735ffd83dbSDimitry Andric         "Calling convention CFGuard_Check is unsupported on Darwin.");
1745ffd83dbSDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>()
1755ffd83dbSDimitry Andric           .getTargetLowering()
1765ffd83dbSDimitry Andric           ->supportSwiftError() &&
1775ffd83dbSDimitry Andric       MF.getFunction().getAttributes().hasAttrSomewhere(Attribute::SwiftError))
1785ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_AAPCS_SwiftError_RegMask;
1795ffd83dbSDimitry Andric   if (CC == CallingConv::PreserveMost)
1805ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_RT_MostRegs_RegMask;
1815ffd83dbSDimitry Andric   return CSR_Darwin_AArch64_AAPCS_RegMask;
1825ffd83dbSDimitry Andric }
1835ffd83dbSDimitry Andric 
1845ffd83dbSDimitry Andric const uint32_t *
1850b57cec5SDimitry Andric AArch64RegisterInfo::getCallPreservedMask(const MachineFunction &MF,
1860b57cec5SDimitry Andric                                           CallingConv::ID CC) const {
1870b57cec5SDimitry Andric   bool SCS = MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack);
1880b57cec5SDimitry Andric   if (CC == CallingConv::GHC)
1890b57cec5SDimitry Andric     // This is academic because all GHC calls are (supposed to be) tail calls
1900b57cec5SDimitry Andric     return SCS ? CSR_AArch64_NoRegs_SCS_RegMask : CSR_AArch64_NoRegs_RegMask;
1910b57cec5SDimitry Andric   if (CC == CallingConv::AnyReg)
1920b57cec5SDimitry Andric     return SCS ? CSR_AArch64_AllRegs_SCS_RegMask : CSR_AArch64_AllRegs_RegMask;
1935ffd83dbSDimitry Andric 
1945ffd83dbSDimitry Andric   // All the following calling conventions are handled differently on Darwin.
1955ffd83dbSDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>().isTargetDarwin()) {
1965ffd83dbSDimitry Andric     if (SCS)
1975ffd83dbSDimitry Andric       report_fatal_error("ShadowCallStack attribute not supported on Darwin.");
1985ffd83dbSDimitry Andric     return getDarwinCallPreservedMask(MF, CC);
1995ffd83dbSDimitry Andric   }
2005ffd83dbSDimitry Andric 
2010b57cec5SDimitry Andric   if (CC == CallingConv::AArch64_VectorCall)
2020b57cec5SDimitry Andric     return SCS ? CSR_AArch64_AAVPCS_SCS_RegMask : CSR_AArch64_AAVPCS_RegMask;
2038bcb0991SDimitry Andric   if (CC == CallingConv::AArch64_SVE_VectorCall)
204480093f4SDimitry Andric     return SCS ? CSR_AArch64_SVE_AAPCS_SCS_RegMask
205480093f4SDimitry Andric                : CSR_AArch64_SVE_AAPCS_RegMask;
206480093f4SDimitry Andric   if (CC == CallingConv::CFGuard_Check)
207480093f4SDimitry Andric     return CSR_Win_AArch64_CFGuard_Check_RegMask;
2080b57cec5SDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>().getTargetLowering()
2090b57cec5SDimitry Andric           ->supportSwiftError() &&
2100b57cec5SDimitry Andric       MF.getFunction().getAttributes().hasAttrSomewhere(Attribute::SwiftError))
2110b57cec5SDimitry Andric     return SCS ? CSR_AArch64_AAPCS_SwiftError_SCS_RegMask
2120b57cec5SDimitry Andric                : CSR_AArch64_AAPCS_SwiftError_RegMask;
2130b57cec5SDimitry Andric   if (CC == CallingConv::PreserveMost)
2140b57cec5SDimitry Andric     return SCS ? CSR_AArch64_RT_MostRegs_SCS_RegMask
2150b57cec5SDimitry Andric                : CSR_AArch64_RT_MostRegs_RegMask;
2160b57cec5SDimitry Andric   else
2170b57cec5SDimitry Andric     return SCS ? CSR_AArch64_AAPCS_SCS_RegMask : CSR_AArch64_AAPCS_RegMask;
2180b57cec5SDimitry Andric }
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric const uint32_t *AArch64RegisterInfo::getTLSCallPreservedMask() const {
2210b57cec5SDimitry Andric   if (TT.isOSDarwin())
2225ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_TLS_RegMask;
2230b57cec5SDimitry Andric 
2240b57cec5SDimitry Andric   assert(TT.isOSBinFormatELF() && "Invalid target");
2250b57cec5SDimitry Andric   return CSR_AArch64_TLS_ELF_RegMask;
2260b57cec5SDimitry Andric }
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric void AArch64RegisterInfo::UpdateCustomCallPreservedMask(MachineFunction &MF,
2290b57cec5SDimitry Andric                                                  const uint32_t **Mask) const {
2300b57cec5SDimitry Andric   uint32_t *UpdatedMask = MF.allocateRegMask();
2310b57cec5SDimitry Andric   unsigned RegMaskSize = MachineOperand::getRegMaskSize(getNumRegs());
2320b57cec5SDimitry Andric   memcpy(UpdatedMask, *Mask, sizeof(UpdatedMask[0]) * RegMaskSize);
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric   for (size_t i = 0; i < AArch64::GPR64commonRegClass.getNumRegs(); ++i) {
2350b57cec5SDimitry Andric     if (MF.getSubtarget<AArch64Subtarget>().isXRegCustomCalleeSaved(i)) {
2360b57cec5SDimitry Andric       for (MCSubRegIterator SubReg(AArch64::GPR64commonRegClass.getRegister(i),
2370b57cec5SDimitry Andric                                    this, true);
2380b57cec5SDimitry Andric            SubReg.isValid(); ++SubReg) {
2390b57cec5SDimitry Andric         // See TargetRegisterInfo::getCallPreservedMask for how to interpret the
2400b57cec5SDimitry Andric         // register mask.
2410b57cec5SDimitry Andric         UpdatedMask[*SubReg / 32] |= 1u << (*SubReg % 32);
2420b57cec5SDimitry Andric       }
2430b57cec5SDimitry Andric     }
2440b57cec5SDimitry Andric   }
2450b57cec5SDimitry Andric   *Mask = UpdatedMask;
2460b57cec5SDimitry Andric }
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric const uint32_t *AArch64RegisterInfo::getNoPreservedMask() const {
2490b57cec5SDimitry Andric   return CSR_AArch64_NoRegs_RegMask;
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric const uint32_t *
2530b57cec5SDimitry Andric AArch64RegisterInfo::getThisReturnPreservedMask(const MachineFunction &MF,
2540b57cec5SDimitry Andric                                                 CallingConv::ID CC) const {
2550b57cec5SDimitry Andric   // This should return a register mask that is the same as that returned by
2560b57cec5SDimitry Andric   // getCallPreservedMask but that additionally preserves the register used for
2570b57cec5SDimitry Andric   // the first i64 argument (which must also be the register used to return a
2580b57cec5SDimitry Andric   // single i64 return value)
2590b57cec5SDimitry Andric   //
2600b57cec5SDimitry Andric   // In case that the calling convention does not use the same register for
2610b57cec5SDimitry Andric   // both, the function should return NULL (does not currently apply)
2620b57cec5SDimitry Andric   assert(CC != CallingConv::GHC && "should not be GHC calling convention.");
2635ffd83dbSDimitry Andric   if (MF.getSubtarget<AArch64Subtarget>().isTargetDarwin())
2645ffd83dbSDimitry Andric     return CSR_Darwin_AArch64_AAPCS_ThisReturn_RegMask;
2650b57cec5SDimitry Andric   return CSR_AArch64_AAPCS_ThisReturn_RegMask;
2660b57cec5SDimitry Andric }
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric const uint32_t *AArch64RegisterInfo::getWindowsStackProbePreservedMask() const {
2690b57cec5SDimitry Andric   return CSR_AArch64_StackProbe_Windows_RegMask;
2700b57cec5SDimitry Andric }
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric BitVector
2730b57cec5SDimitry Andric AArch64RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
2740b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric   // FIXME: avoid re-calculating this every time.
2770b57cec5SDimitry Andric   BitVector Reserved(getNumRegs());
2780b57cec5SDimitry Andric   markSuperRegs(Reserved, AArch64::WSP);
2790b57cec5SDimitry Andric   markSuperRegs(Reserved, AArch64::WZR);
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric   if (TFI->hasFP(MF) || TT.isOSDarwin())
2820b57cec5SDimitry Andric     markSuperRegs(Reserved, AArch64::W29);
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric   for (size_t i = 0; i < AArch64::GPR32commonRegClass.getNumRegs(); ++i) {
2850b57cec5SDimitry Andric     if (MF.getSubtarget<AArch64Subtarget>().isXRegisterReserved(i))
2860b57cec5SDimitry Andric       markSuperRegs(Reserved, AArch64::GPR32commonRegClass.getRegister(i));
2870b57cec5SDimitry Andric   }
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric   if (hasBasePointer(MF))
2900b57cec5SDimitry Andric     markSuperRegs(Reserved, AArch64::W19);
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric   // SLH uses register W16/X16 as the taint register.
2930b57cec5SDimitry Andric   if (MF.getFunction().hasFnAttribute(Attribute::SpeculativeLoadHardening))
2940b57cec5SDimitry Andric     markSuperRegs(Reserved, AArch64::W16);
2950b57cec5SDimitry Andric 
2960b57cec5SDimitry Andric   assert(checkAllSuperRegsMarked(Reserved));
2970b57cec5SDimitry Andric   return Reserved;
2980b57cec5SDimitry Andric }
2990b57cec5SDimitry Andric 
3000b57cec5SDimitry Andric bool AArch64RegisterInfo::isReservedReg(const MachineFunction &MF,
3015ffd83dbSDimitry Andric                                         MCRegister Reg) const {
3020b57cec5SDimitry Andric   return getReservedRegs(MF)[Reg];
3030b57cec5SDimitry Andric }
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric bool AArch64RegisterInfo::isAnyArgRegReserved(const MachineFunction &MF) const {
3060b57cec5SDimitry Andric   return std::any_of(std::begin(*AArch64::GPR64argRegClass.MC),
3070b57cec5SDimitry Andric                      std::end(*AArch64::GPR64argRegClass.MC),
3080b57cec5SDimitry Andric                      [this, &MF](MCPhysReg r){return isReservedReg(MF, r);});
3090b57cec5SDimitry Andric }
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric void AArch64RegisterInfo::emitReservedArgRegCallError(
3120b57cec5SDimitry Andric     const MachineFunction &MF) const {
3130b57cec5SDimitry Andric   const Function &F = MF.getFunction();
3140b57cec5SDimitry Andric   F.getContext().diagnose(DiagnosticInfoUnsupported{F, "AArch64 doesn't support"
3150b57cec5SDimitry Andric     " function calls if any of the argument registers is reserved."});
3160b57cec5SDimitry Andric }
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric bool AArch64RegisterInfo::isAsmClobberable(const MachineFunction &MF,
3195ffd83dbSDimitry Andric                                           MCRegister PhysReg) const {
3200b57cec5SDimitry Andric   return !isReservedReg(MF, PhysReg);
3210b57cec5SDimitry Andric }
3220b57cec5SDimitry Andric 
3235ffd83dbSDimitry Andric bool AArch64RegisterInfo::isConstantPhysReg(MCRegister PhysReg) const {
3240b57cec5SDimitry Andric   return PhysReg == AArch64::WZR || PhysReg == AArch64::XZR;
3250b57cec5SDimitry Andric }
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric const TargetRegisterClass *
3280b57cec5SDimitry Andric AArch64RegisterInfo::getPointerRegClass(const MachineFunction &MF,
3290b57cec5SDimitry Andric                                       unsigned Kind) const {
3300b57cec5SDimitry Andric   return &AArch64::GPR64spRegClass;
3310b57cec5SDimitry Andric }
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric const TargetRegisterClass *
3340b57cec5SDimitry Andric AArch64RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
3350b57cec5SDimitry Andric   if (RC == &AArch64::CCRRegClass)
3360b57cec5SDimitry Andric     return &AArch64::GPR64RegClass; // Only MSR & MRS copy NZCV.
3370b57cec5SDimitry Andric   return RC;
3380b57cec5SDimitry Andric }
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric unsigned AArch64RegisterInfo::getBaseRegister() const { return AArch64::X19; }
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric bool AArch64RegisterInfo::hasBasePointer(const MachineFunction &MF) const {
3430b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
3440b57cec5SDimitry Andric 
3450b57cec5SDimitry Andric   // In the presence of variable sized objects or funclets, if the fixed stack
3460b57cec5SDimitry Andric   // size is large enough that referencing from the FP won't result in things
3470b57cec5SDimitry Andric   // being in range relatively often, we can use a base pointer to allow access
3480b57cec5SDimitry Andric   // from the other direction like the SP normally works.
3490b57cec5SDimitry Andric   //
3500b57cec5SDimitry Andric   // Furthermore, if both variable sized objects are present, and the
3510b57cec5SDimitry Andric   // stack needs to be dynamically re-aligned, the base pointer is the only
3520b57cec5SDimitry Andric   // reliable way to reference the locals.
3530b57cec5SDimitry Andric   if (MFI.hasVarSizedObjects() || MF.hasEHFunclets()) {
3540b57cec5SDimitry Andric     if (needsStackRealignment(MF))
3550b57cec5SDimitry Andric       return true;
356*979e22ffSDimitry Andric 
357*979e22ffSDimitry Andric     if (MF.getSubtarget<AArch64Subtarget>().hasSVE()) {
358*979e22ffSDimitry Andric       const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
359*979e22ffSDimitry Andric       // Frames that have variable sized objects and scalable SVE objects,
360*979e22ffSDimitry Andric       // should always use a basepointer.
361*979e22ffSDimitry Andric       if (!AFI->hasCalculatedStackSizeSVE() || AFI->getStackSizeSVE())
362*979e22ffSDimitry Andric         return true;
363*979e22ffSDimitry Andric     }
364*979e22ffSDimitry Andric 
3650b57cec5SDimitry Andric     // Conservatively estimate whether the negative offset from the frame
3660b57cec5SDimitry Andric     // pointer will be sufficient to reach. If a function has a smallish
3670b57cec5SDimitry Andric     // frame, it's less likely to have lots of spills and callee saved
3680b57cec5SDimitry Andric     // space, so it's all more likely to be within range of the frame pointer.
3690b57cec5SDimitry Andric     // If it's wrong, we'll materialize the constant and still get to the
3700b57cec5SDimitry Andric     // object; it's just suboptimal. Negative offsets use the unscaled
3710b57cec5SDimitry Andric     // load/store instructions, which have a 9-bit signed immediate.
3720b57cec5SDimitry Andric     return MFI.getLocalFrameSize() >= 256;
3730b57cec5SDimitry Andric   }
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric   return false;
3760b57cec5SDimitry Andric }
3770b57cec5SDimitry Andric 
3780b57cec5SDimitry Andric Register
3790b57cec5SDimitry Andric AArch64RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
3800b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
3810b57cec5SDimitry Andric   return TFI->hasFP(MF) ? AArch64::FP : AArch64::SP;
3820b57cec5SDimitry Andric }
3830b57cec5SDimitry Andric 
3840b57cec5SDimitry Andric bool AArch64RegisterInfo::requiresRegisterScavenging(
3850b57cec5SDimitry Andric     const MachineFunction &MF) const {
3860b57cec5SDimitry Andric   return true;
3870b57cec5SDimitry Andric }
3880b57cec5SDimitry Andric 
3890b57cec5SDimitry Andric bool AArch64RegisterInfo::requiresVirtualBaseRegisters(
3900b57cec5SDimitry Andric     const MachineFunction &MF) const {
3910b57cec5SDimitry Andric   return true;
3920b57cec5SDimitry Andric }
3930b57cec5SDimitry Andric 
3940b57cec5SDimitry Andric bool
3950b57cec5SDimitry Andric AArch64RegisterInfo::useFPForScavengingIndex(const MachineFunction &MF) const {
3960b57cec5SDimitry Andric   // This function indicates whether the emergency spillslot should be placed
3970b57cec5SDimitry Andric   // close to the beginning of the stackframe (closer to FP) or the end
3980b57cec5SDimitry Andric   // (closer to SP).
3990b57cec5SDimitry Andric   //
4000b57cec5SDimitry Andric   // The beginning works most reliably if we have a frame pointer.
401*979e22ffSDimitry Andric   // In the presence of any non-constant space between FP and locals,
402*979e22ffSDimitry Andric   // (e.g. in case of stack realignment or a scalable SVE area), it is
403*979e22ffSDimitry Andric   // better to use SP or BP.
4040b57cec5SDimitry Andric   const AArch64FrameLowering &TFI = *getFrameLowering(MF);
405*979e22ffSDimitry Andric   const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
406*979e22ffSDimitry Andric   assert((!MF.getSubtarget<AArch64Subtarget>().hasSVE() ||
407*979e22ffSDimitry Andric           AFI->hasCalculatedStackSizeSVE()) &&
408*979e22ffSDimitry Andric          "Expected SVE area to be calculated by this point");
409*979e22ffSDimitry Andric   return TFI.hasFP(MF) && !needsStackRealignment(MF) && !AFI->getStackSizeSVE();
4100b57cec5SDimitry Andric }
4110b57cec5SDimitry Andric 
4120b57cec5SDimitry Andric bool AArch64RegisterInfo::requiresFrameIndexScavenging(
4130b57cec5SDimitry Andric     const MachineFunction &MF) const {
4140b57cec5SDimitry Andric   return true;
4150b57cec5SDimitry Andric }
4160b57cec5SDimitry Andric 
4170b57cec5SDimitry Andric bool
4180b57cec5SDimitry Andric AArch64RegisterInfo::cannotEliminateFrame(const MachineFunction &MF) const {
4190b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
4200b57cec5SDimitry Andric   if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI.adjustsStack())
4210b57cec5SDimitry Andric     return true;
4220b57cec5SDimitry Andric   return MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken();
4230b57cec5SDimitry Andric }
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric /// needsFrameBaseReg - Returns true if the instruction's frame index
4260b57cec5SDimitry Andric /// reference would be better served by a base register other than FP
4270b57cec5SDimitry Andric /// or SP. Used by LocalStackFrameAllocation to determine which frame index
4280b57cec5SDimitry Andric /// references it should create new base registers for.
4290b57cec5SDimitry Andric bool AArch64RegisterInfo::needsFrameBaseReg(MachineInstr *MI,
4300b57cec5SDimitry Andric                                             int64_t Offset) const {
4310b57cec5SDimitry Andric   for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i)
4320b57cec5SDimitry Andric     assert(i < MI->getNumOperands() &&
4330b57cec5SDimitry Andric            "Instr doesn't have FrameIndex operand!");
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric   // It's the load/store FI references that cause issues, as it can be difficult
4360b57cec5SDimitry Andric   // to materialize the offset if it won't fit in the literal field. Estimate
4370b57cec5SDimitry Andric   // based on the size of the local frame and some conservative assumptions
4380b57cec5SDimitry Andric   // about the rest of the stack frame (note, this is pre-regalloc, so
4390b57cec5SDimitry Andric   // we don't know everything for certain yet) whether this offset is likely
4400b57cec5SDimitry Andric   // to be out of range of the immediate. Return true if so.
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric   // We only generate virtual base registers for loads and stores, so
4430b57cec5SDimitry Andric   // return false for everything else.
4440b57cec5SDimitry Andric   if (!MI->mayLoad() && !MI->mayStore())
4450b57cec5SDimitry Andric     return false;
4460b57cec5SDimitry Andric 
4470b57cec5SDimitry Andric   // Without a virtual base register, if the function has variable sized
4480b57cec5SDimitry Andric   // objects, all fixed-size local references will be via the frame pointer,
4490b57cec5SDimitry Andric   // Approximate the offset and see if it's legal for the instruction.
4500b57cec5SDimitry Andric   // Note that the incoming offset is based on the SP value at function entry,
4510b57cec5SDimitry Andric   // so it'll be negative.
4520b57cec5SDimitry Andric   MachineFunction &MF = *MI->getParent()->getParent();
4530b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
4540b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
4550b57cec5SDimitry Andric 
4560b57cec5SDimitry Andric   // Estimate an offset from the frame pointer.
4570b57cec5SDimitry Andric   // Conservatively assume all GPR callee-saved registers get pushed.
4580b57cec5SDimitry Andric   // FP, LR, X19-X28, D8-D15. 64-bits each.
4590b57cec5SDimitry Andric   int64_t FPOffset = Offset - 16 * 20;
4600b57cec5SDimitry Andric   // Estimate an offset from the stack pointer.
4610b57cec5SDimitry Andric   // The incoming offset is relating to the SP at the start of the function,
4620b57cec5SDimitry Andric   // but when we access the local it'll be relative to the SP after local
4630b57cec5SDimitry Andric   // allocation, so adjust our SP-relative offset by that allocation size.
4640b57cec5SDimitry Andric   Offset += MFI.getLocalFrameSize();
4650b57cec5SDimitry Andric   // Assume that we'll have at least some spill slots allocated.
4660b57cec5SDimitry Andric   // FIXME: This is a total SWAG number. We should run some statistics
4670b57cec5SDimitry Andric   //        and pick a real one.
4680b57cec5SDimitry Andric   Offset += 128; // 128 bytes of spill slots
4690b57cec5SDimitry Andric 
4700b57cec5SDimitry Andric   // If there is a frame pointer, try using it.
4710b57cec5SDimitry Andric   // The FP is only available if there is no dynamic realignment. We
4720b57cec5SDimitry Andric   // don't know for sure yet whether we'll need that, so we guess based
4730b57cec5SDimitry Andric   // on whether there are any local variables that would trigger it.
4740b57cec5SDimitry Andric   if (TFI->hasFP(MF) && isFrameOffsetLegal(MI, AArch64::FP, FPOffset))
4750b57cec5SDimitry Andric     return false;
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric   // If we can reference via the stack pointer or base pointer, try that.
4780b57cec5SDimitry Andric   // FIXME: This (and the code that resolves the references) can be improved
4790b57cec5SDimitry Andric   //        to only disallow SP relative references in the live range of
4800b57cec5SDimitry Andric   //        the VLA(s). In practice, it's unclear how much difference that
4810b57cec5SDimitry Andric   //        would make, but it may be worth doing.
4820b57cec5SDimitry Andric   if (isFrameOffsetLegal(MI, AArch64::SP, Offset))
4830b57cec5SDimitry Andric     return false;
4840b57cec5SDimitry Andric 
4855ffd83dbSDimitry Andric   // If even offset 0 is illegal, we don't want a virtual base register.
4865ffd83dbSDimitry Andric   if (!isFrameOffsetLegal(MI, AArch64::SP, 0))
4875ffd83dbSDimitry Andric     return false;
4885ffd83dbSDimitry Andric 
4890b57cec5SDimitry Andric   // The offset likely isn't legal; we want to allocate a virtual base register.
4900b57cec5SDimitry Andric   return true;
4910b57cec5SDimitry Andric }
4920b57cec5SDimitry Andric 
4930b57cec5SDimitry Andric bool AArch64RegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
4945ffd83dbSDimitry Andric                                              Register BaseReg,
4950b57cec5SDimitry Andric                                              int64_t Offset) const {
4960b57cec5SDimitry Andric   assert(MI && "Unable to get the legal offset for nil instruction.");
4978bcb0991SDimitry Andric   StackOffset SaveOffset(Offset, MVT::i8);
4980b57cec5SDimitry Andric   return isAArch64FrameOffsetLegal(*MI, SaveOffset) & AArch64FrameOffsetIsLegal;
4990b57cec5SDimitry Andric }
5000b57cec5SDimitry Andric 
5010b57cec5SDimitry Andric /// Insert defining instruction(s) for BaseReg to be a pointer to FrameIdx
5020b57cec5SDimitry Andric /// at the beginning of the basic block.
5030b57cec5SDimitry Andric void AArch64RegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB,
5045ffd83dbSDimitry Andric                                                        Register BaseReg,
5050b57cec5SDimitry Andric                                                        int FrameIdx,
5060b57cec5SDimitry Andric                                                        int64_t Offset) const {
5070b57cec5SDimitry Andric   MachineBasicBlock::iterator Ins = MBB->begin();
5080b57cec5SDimitry Andric   DebugLoc DL; // Defaults to "unknown"
5090b57cec5SDimitry Andric   if (Ins != MBB->end())
5100b57cec5SDimitry Andric     DL = Ins->getDebugLoc();
5110b57cec5SDimitry Andric   const MachineFunction &MF = *MBB->getParent();
5120b57cec5SDimitry Andric   const AArch64InstrInfo *TII =
5130b57cec5SDimitry Andric       MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
5140b57cec5SDimitry Andric   const MCInstrDesc &MCID = TII->get(AArch64::ADDXri);
5150b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
5160b57cec5SDimitry Andric   MRI.constrainRegClass(BaseReg, TII->getRegClass(MCID, 0, this, MF));
5170b57cec5SDimitry Andric   unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0);
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric   BuildMI(*MBB, Ins, DL, MCID, BaseReg)
5200b57cec5SDimitry Andric       .addFrameIndex(FrameIdx)
5210b57cec5SDimitry Andric       .addImm(Offset)
5220b57cec5SDimitry Andric       .addImm(Shifter);
5230b57cec5SDimitry Andric }
5240b57cec5SDimitry Andric 
5255ffd83dbSDimitry Andric void AArch64RegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg,
5260b57cec5SDimitry Andric                                             int64_t Offset) const {
5278bcb0991SDimitry Andric   // ARM doesn't need the general 64-bit offsets
5288bcb0991SDimitry Andric   StackOffset Off(Offset, MVT::i8);
5298bcb0991SDimitry Andric 
5300b57cec5SDimitry Andric   unsigned i = 0;
5310b57cec5SDimitry Andric 
5320b57cec5SDimitry Andric   while (!MI.getOperand(i).isFI()) {
5330b57cec5SDimitry Andric     ++i;
5340b57cec5SDimitry Andric     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
5350b57cec5SDimitry Andric   }
5360b57cec5SDimitry Andric   const MachineFunction *MF = MI.getParent()->getParent();
5370b57cec5SDimitry Andric   const AArch64InstrInfo *TII =
5380b57cec5SDimitry Andric       MF->getSubtarget<AArch64Subtarget>().getInstrInfo();
5390b57cec5SDimitry Andric   bool Done = rewriteAArch64FrameIndex(MI, i, BaseReg, Off, TII);
5400b57cec5SDimitry Andric   assert(Done && "Unable to resolve frame index!");
5410b57cec5SDimitry Andric   (void)Done;
5420b57cec5SDimitry Andric }
5430b57cec5SDimitry Andric 
5445ffd83dbSDimitry Andric // Create a scratch register for the frame index elimination in an instruction.
5455ffd83dbSDimitry Andric // This function has special handling of stack tagging loop pseudos, in which
5465ffd83dbSDimitry Andric // case it can also change the instruction opcode (but not the operands).
5475ffd83dbSDimitry Andric static Register
5485ffd83dbSDimitry Andric createScratchRegisterForInstruction(MachineInstr &MI,
5495ffd83dbSDimitry Andric                                     const AArch64InstrInfo *TII) {
5505ffd83dbSDimitry Andric   // ST*Gloop have a reserved scratch register in operand 1. Use it, and also
5515ffd83dbSDimitry Andric   // replace the instruction with the writeback variant because it will now
5525ffd83dbSDimitry Andric   // satisfy the operand constraints for it.
5535ffd83dbSDimitry Andric   if (MI.getOpcode() == AArch64::STGloop) {
5545ffd83dbSDimitry Andric     MI.setDesc(TII->get(AArch64::STGloop_wback));
5555ffd83dbSDimitry Andric     return MI.getOperand(1).getReg();
5565ffd83dbSDimitry Andric   } else if (MI.getOpcode() == AArch64::STZGloop) {
5575ffd83dbSDimitry Andric     MI.setDesc(TII->get(AArch64::STZGloop_wback));
5585ffd83dbSDimitry Andric     return MI.getOperand(1).getReg();
5595ffd83dbSDimitry Andric   } else {
5605ffd83dbSDimitry Andric     return MI.getMF()->getRegInfo().createVirtualRegister(
5615ffd83dbSDimitry Andric         &AArch64::GPR64RegClass);
5625ffd83dbSDimitry Andric   }
5635ffd83dbSDimitry Andric }
5645ffd83dbSDimitry Andric 
5650b57cec5SDimitry Andric void AArch64RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
5660b57cec5SDimitry Andric                                               int SPAdj, unsigned FIOperandNum,
5670b57cec5SDimitry Andric                                               RegScavenger *RS) const {
5680b57cec5SDimitry Andric   assert(SPAdj == 0 && "Unexpected");
5690b57cec5SDimitry Andric 
5700b57cec5SDimitry Andric   MachineInstr &MI = *II;
5710b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
5720b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
5738bcb0991SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
5740b57cec5SDimitry Andric   const AArch64InstrInfo *TII =
5750b57cec5SDimitry Andric       MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
5760b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
5770b57cec5SDimitry Andric 
5780b57cec5SDimitry Andric   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
5798bcb0991SDimitry Andric   bool Tagged =
5808bcb0991SDimitry Andric       MI.getOperand(FIOperandNum).getTargetFlags() & AArch64II::MO_TAGGED;
5815ffd83dbSDimitry Andric   Register FrameReg;
5820b57cec5SDimitry Andric 
5830b57cec5SDimitry Andric   // Special handling of dbg_value, stackmap and patchpoint instructions.
5840b57cec5SDimitry Andric   if (MI.isDebugValue() || MI.getOpcode() == TargetOpcode::STACKMAP ||
5850b57cec5SDimitry Andric       MI.getOpcode() == TargetOpcode::PATCHPOINT) {
5868bcb0991SDimitry Andric     StackOffset Offset =
5878bcb0991SDimitry Andric         TFI->resolveFrameIndexReference(MF, FrameIndex, FrameReg,
5880b57cec5SDimitry Andric                                         /*PreferFP=*/true,
5890b57cec5SDimitry Andric                                         /*ForSimm=*/false);
5908bcb0991SDimitry Andric     Offset += StackOffset(MI.getOperand(FIOperandNum + 1).getImm(), MVT::i8);
5910b57cec5SDimitry Andric     MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false /*isDef*/);
5928bcb0991SDimitry Andric     MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset.getBytes());
5930b57cec5SDimitry Andric     return;
5940b57cec5SDimitry Andric   }
5950b57cec5SDimitry Andric 
5960b57cec5SDimitry Andric   if (MI.getOpcode() == TargetOpcode::LOCAL_ESCAPE) {
5970b57cec5SDimitry Andric     MachineOperand &FI = MI.getOperand(FIOperandNum);
5988bcb0991SDimitry Andric     int Offset = TFI->getNonLocalFrameIndexReference(MF, FrameIndex);
5990b57cec5SDimitry Andric     FI.ChangeToImmediate(Offset);
6000b57cec5SDimitry Andric     return;
6010b57cec5SDimitry Andric   }
6020b57cec5SDimitry Andric 
6038bcb0991SDimitry Andric   StackOffset Offset;
6040b57cec5SDimitry Andric   if (MI.getOpcode() == AArch64::TAGPstack) {
6050b57cec5SDimitry Andric     // TAGPstack must use the virtual frame register in its 3rd operand.
6060b57cec5SDimitry Andric     const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
6070b57cec5SDimitry Andric     FrameReg = MI.getOperand(3).getReg();
6088bcb0991SDimitry Andric     Offset = {MFI.getObjectOffset(FrameIndex) +
6098bcb0991SDimitry Andric                   AFI->getTaggedBasePointerOffset(),
6108bcb0991SDimitry Andric               MVT::i8};
6118bcb0991SDimitry Andric   } else if (Tagged) {
6128bcb0991SDimitry Andric     StackOffset SPOffset = {
6138bcb0991SDimitry Andric         MFI.getObjectOffset(FrameIndex) + (int64_t)MFI.getStackSize(), MVT::i8};
6148bcb0991SDimitry Andric     if (MFI.hasVarSizedObjects() ||
6158bcb0991SDimitry Andric         isAArch64FrameOffsetLegal(MI, SPOffset, nullptr, nullptr, nullptr) !=
6168bcb0991SDimitry Andric             (AArch64FrameOffsetCanUpdate | AArch64FrameOffsetIsLegal)) {
6178bcb0991SDimitry Andric       // Can't update to SP + offset in place. Precalculate the tagged pointer
6188bcb0991SDimitry Andric       // in a scratch register.
6198bcb0991SDimitry Andric       Offset = TFI->resolveFrameIndexReference(
6208bcb0991SDimitry Andric           MF, FrameIndex, FrameReg, /*PreferFP=*/false, /*ForSimm=*/true);
6218bcb0991SDimitry Andric       Register ScratchReg =
6228bcb0991SDimitry Andric           MF.getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass);
6238bcb0991SDimitry Andric       emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset,
6248bcb0991SDimitry Andric                       TII);
6258bcb0991SDimitry Andric       BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(AArch64::LDG), ScratchReg)
6268bcb0991SDimitry Andric           .addReg(ScratchReg)
6278bcb0991SDimitry Andric           .addReg(ScratchReg)
6288bcb0991SDimitry Andric           .addImm(0);
6298bcb0991SDimitry Andric       MI.getOperand(FIOperandNum)
6308bcb0991SDimitry Andric           .ChangeToRegister(ScratchReg, false, false, true);
6318bcb0991SDimitry Andric       return;
6328bcb0991SDimitry Andric     }
6338bcb0991SDimitry Andric     FrameReg = AArch64::SP;
6348bcb0991SDimitry Andric     Offset = {MFI.getObjectOffset(FrameIndex) + (int64_t)MFI.getStackSize(),
6358bcb0991SDimitry Andric               MVT::i8};
6360b57cec5SDimitry Andric   } else {
6370b57cec5SDimitry Andric     Offset = TFI->resolveFrameIndexReference(
6380b57cec5SDimitry Andric         MF, FrameIndex, FrameReg, /*PreferFP=*/false, /*ForSimm=*/true);
6390b57cec5SDimitry Andric   }
6400b57cec5SDimitry Andric 
6410b57cec5SDimitry Andric   // Modify MI as necessary to handle as much of 'Offset' as possible
6420b57cec5SDimitry Andric   if (rewriteAArch64FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII))
6430b57cec5SDimitry Andric     return;
6440b57cec5SDimitry Andric 
6450b57cec5SDimitry Andric   assert((!RS || !RS->isScavengingFrameIndex(FrameIndex)) &&
6460b57cec5SDimitry Andric          "Emergency spill slot is out of reach");
6470b57cec5SDimitry Andric 
6480b57cec5SDimitry Andric   // If we get here, the immediate doesn't fit into the instruction.  We folded
6490b57cec5SDimitry Andric   // as much as possible above.  Handle the rest, providing a register that is
6500b57cec5SDimitry Andric   // SP+LargeImm.
6515ffd83dbSDimitry Andric   Register ScratchReg = createScratchRegisterForInstruction(MI, TII);
6520b57cec5SDimitry Andric   emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset, TII);
6530b57cec5SDimitry Andric   MI.getOperand(FIOperandNum).ChangeToRegister(ScratchReg, false, false, true);
6540b57cec5SDimitry Andric }
6550b57cec5SDimitry Andric 
6560b57cec5SDimitry Andric unsigned AArch64RegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
6570b57cec5SDimitry Andric                                                   MachineFunction &MF) const {
6580b57cec5SDimitry Andric   const AArch64FrameLowering *TFI = getFrameLowering(MF);
6590b57cec5SDimitry Andric 
6600b57cec5SDimitry Andric   switch (RC->getID()) {
6610b57cec5SDimitry Andric   default:
6620b57cec5SDimitry Andric     return 0;
6630b57cec5SDimitry Andric   case AArch64::GPR32RegClassID:
6640b57cec5SDimitry Andric   case AArch64::GPR32spRegClassID:
6650b57cec5SDimitry Andric   case AArch64::GPR32allRegClassID:
6660b57cec5SDimitry Andric   case AArch64::GPR64spRegClassID:
6670b57cec5SDimitry Andric   case AArch64::GPR64allRegClassID:
6680b57cec5SDimitry Andric   case AArch64::GPR64RegClassID:
6690b57cec5SDimitry Andric   case AArch64::GPR32commonRegClassID:
6700b57cec5SDimitry Andric   case AArch64::GPR64commonRegClassID:
6710b57cec5SDimitry Andric     return 32 - 1                                   // XZR/SP
6720b57cec5SDimitry Andric               - (TFI->hasFP(MF) || TT.isOSDarwin()) // FP
6730b57cec5SDimitry Andric               - MF.getSubtarget<AArch64Subtarget>().getNumXRegisterReserved()
6740b57cec5SDimitry Andric               - hasBasePointer(MF);  // X19
6750b57cec5SDimitry Andric   case AArch64::FPR8RegClassID:
6760b57cec5SDimitry Andric   case AArch64::FPR16RegClassID:
6770b57cec5SDimitry Andric   case AArch64::FPR32RegClassID:
6780b57cec5SDimitry Andric   case AArch64::FPR64RegClassID:
6790b57cec5SDimitry Andric   case AArch64::FPR128RegClassID:
6800b57cec5SDimitry Andric     return 32;
6810b57cec5SDimitry Andric 
6820b57cec5SDimitry Andric   case AArch64::DDRegClassID:
6830b57cec5SDimitry Andric   case AArch64::DDDRegClassID:
6840b57cec5SDimitry Andric   case AArch64::DDDDRegClassID:
6850b57cec5SDimitry Andric   case AArch64::QQRegClassID:
6860b57cec5SDimitry Andric   case AArch64::QQQRegClassID:
6870b57cec5SDimitry Andric   case AArch64::QQQQRegClassID:
6880b57cec5SDimitry Andric     return 32;
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric   case AArch64::FPR128_loRegClassID:
6915ffd83dbSDimitry Andric   case AArch64::FPR64_loRegClassID:
6925ffd83dbSDimitry Andric   case AArch64::FPR16_loRegClassID:
6930b57cec5SDimitry Andric     return 16;
6940b57cec5SDimitry Andric   }
6950b57cec5SDimitry Andric }
6960b57cec5SDimitry Andric 
6970b57cec5SDimitry Andric unsigned AArch64RegisterInfo::getLocalAddressRegister(
6980b57cec5SDimitry Andric   const MachineFunction &MF) const {
6990b57cec5SDimitry Andric   const auto &MFI = MF.getFrameInfo();
7000b57cec5SDimitry Andric   if (!MF.hasEHFunclets() && !MFI.hasVarSizedObjects())
7010b57cec5SDimitry Andric     return AArch64::SP;
7020b57cec5SDimitry Andric   else if (needsStackRealignment(MF))
7030b57cec5SDimitry Andric     return getBaseRegister();
7040b57cec5SDimitry Andric   return getFrameRegister(MF);
7050b57cec5SDimitry Andric }
706