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" 20bdd1243dSDimitry Andric #include "MCTargetDesc/AArch64InstPrinter.h" 210b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h" 2281ad6265SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h" 270b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 28e8d8bef9SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 298bcb0991SDimitry Andric #include "llvm/IR/DiagnosticInfo.h" 308bcb0991SDimitry Andric #include "llvm/IR/Function.h" 318bcb0991SDimitry Andric #include "llvm/Support/raw_ostream.h" 320b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h" 3306c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.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 817a6dacacSDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::ARM64EC_Thunk_X64) 827a6dacacSDimitry Andric return CSR_Win_AArch64_Arm64EC_Thunk_SaveList; 837a6dacacSDimitry Andric 845ffd83dbSDimitry Andric // Darwin has its own CSR_AArch64_AAPCS_SaveList, which means most CSR save 855ffd83dbSDimitry Andric // lists depending on that will need to have their Darwin variant as well. 865ffd83dbSDimitry Andric if (MF->getSubtarget<AArch64Subtarget>().isTargetDarwin()) 875ffd83dbSDimitry Andric return getDarwinCalleeSavedRegs(MF); 885ffd83dbSDimitry Andric 895ffd83dbSDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::CFGuard_Check) 905ffd83dbSDimitry Andric return CSR_Win_AArch64_CFGuard_Check_SaveList; 9106c3fb27SDimitry Andric if (MF->getSubtarget<AArch64Subtarget>().isTargetWindows()) { 9206c3fb27SDimitry Andric if (MF->getSubtarget<AArch64Subtarget>().getTargetLowering() 9306c3fb27SDimitry Andric ->supportSwiftError() && 9406c3fb27SDimitry Andric MF->getFunction().getAttributes().hasAttrSomewhere( 9506c3fb27SDimitry Andric Attribute::SwiftError)) 9606c3fb27SDimitry Andric return CSR_Win_AArch64_AAPCS_SwiftError_SaveList; 9706c3fb27SDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::SwiftTail) 9806c3fb27SDimitry Andric return CSR_Win_AArch64_AAPCS_SwiftTail_SaveList; 995ffd83dbSDimitry Andric return CSR_Win_AArch64_AAPCS_SaveList; 10006c3fb27SDimitry Andric } 1010b57cec5SDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::AArch64_VectorCall) 1020b57cec5SDimitry Andric return CSR_AArch64_AAVPCS_SaveList; 103480093f4SDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::AArch64_SVE_VectorCall) 104480093f4SDimitry Andric return CSR_AArch64_SVE_AAPCS_SaveList; 105bdd1243dSDimitry Andric if (MF->getFunction().getCallingConv() == 106bdd1243dSDimitry Andric CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0) 107bdd1243dSDimitry Andric report_fatal_error( 108bdd1243dSDimitry Andric "Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0 is " 109bdd1243dSDimitry Andric "only supported to improve calls to SME ACLE save/restore/disable-za " 110bdd1243dSDimitry Andric "functions, and is not intended to be used beyond that scope."); 111bdd1243dSDimitry Andric if (MF->getFunction().getCallingConv() == 112bdd1243dSDimitry Andric CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2) 113bdd1243dSDimitry Andric report_fatal_error( 114bdd1243dSDimitry Andric "Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2 is " 115bdd1243dSDimitry Andric "only supported to improve calls to SME ACLE __arm_sme_state " 116bdd1243dSDimitry Andric "and is not intended to be used beyond that scope."); 1170b57cec5SDimitry Andric if (MF->getSubtarget<AArch64Subtarget>().getTargetLowering() 1180b57cec5SDimitry Andric ->supportSwiftError() && 1190b57cec5SDimitry Andric MF->getFunction().getAttributes().hasAttrSomewhere( 1200b57cec5SDimitry Andric Attribute::SwiftError)) 1210b57cec5SDimitry Andric return CSR_AArch64_AAPCS_SwiftError_SaveList; 122fe6060f1SDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::SwiftTail) 123fe6060f1SDimitry Andric return CSR_AArch64_AAPCS_SwiftTail_SaveList; 1240b57cec5SDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::PreserveMost) 1250b57cec5SDimitry Andric return CSR_AArch64_RT_MostRegs_SaveList; 12606c3fb27SDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::PreserveAll) 12706c3fb27SDimitry Andric return CSR_AArch64_RT_AllRegs_SaveList; 1285ffd83dbSDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::Win64) 1295ffd83dbSDimitry Andric // This is for OSes other than Windows; Windows is a separate case further 1305ffd83dbSDimitry Andric // above. 1315ffd83dbSDimitry Andric return CSR_AArch64_AAPCS_X18_SaveList; 13281ad6265SDimitry Andric if (MF->getInfo<AArch64FunctionInfo>()->isSVECC()) 133979e22ffSDimitry Andric return CSR_AArch64_SVE_AAPCS_SaveList; 1340b57cec5SDimitry Andric return CSR_AArch64_AAPCS_SaveList; 1350b57cec5SDimitry Andric } 1360b57cec5SDimitry Andric 1375ffd83dbSDimitry Andric const MCPhysReg * 1385ffd83dbSDimitry Andric AArch64RegisterInfo::getDarwinCalleeSavedRegs(const MachineFunction *MF) const { 1395ffd83dbSDimitry Andric assert(MF && "Invalid MachineFunction pointer."); 1405ffd83dbSDimitry Andric assert(MF->getSubtarget<AArch64Subtarget>().isTargetDarwin() && 1415ffd83dbSDimitry Andric "Invalid subtarget for getDarwinCalleeSavedRegs"); 1425ffd83dbSDimitry Andric 1435ffd83dbSDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::CFGuard_Check) 1445ffd83dbSDimitry Andric report_fatal_error( 1455ffd83dbSDimitry Andric "Calling convention CFGuard_Check is unsupported on Darwin."); 1465ffd83dbSDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::AArch64_VectorCall) 1475ffd83dbSDimitry Andric return CSR_Darwin_AArch64_AAVPCS_SaveList; 1485ffd83dbSDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::AArch64_SVE_VectorCall) 1495ffd83dbSDimitry Andric report_fatal_error( 1505ffd83dbSDimitry Andric "Calling convention SVE_VectorCall is unsupported on Darwin."); 151bdd1243dSDimitry Andric if (MF->getFunction().getCallingConv() == 152bdd1243dSDimitry Andric CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0) 153bdd1243dSDimitry Andric report_fatal_error( 154bdd1243dSDimitry Andric "Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0 is " 155bdd1243dSDimitry Andric "only supported to improve calls to SME ACLE save/restore/disable-za " 156bdd1243dSDimitry Andric "functions, and is not intended to be used beyond that scope."); 157bdd1243dSDimitry Andric if (MF->getFunction().getCallingConv() == 158bdd1243dSDimitry Andric CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2) 159bdd1243dSDimitry Andric report_fatal_error( 160bdd1243dSDimitry Andric "Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2 is " 161bdd1243dSDimitry Andric "only supported to improve calls to SME ACLE __arm_sme_state " 162bdd1243dSDimitry Andric "and is not intended to be used beyond that scope."); 1635ffd83dbSDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS) 1645ffd83dbSDimitry Andric return MF->getInfo<AArch64FunctionInfo>()->isSplitCSR() 1655ffd83dbSDimitry Andric ? CSR_Darwin_AArch64_CXX_TLS_PE_SaveList 1665ffd83dbSDimitry Andric : CSR_Darwin_AArch64_CXX_TLS_SaveList; 1675ffd83dbSDimitry Andric if (MF->getSubtarget<AArch64Subtarget>().getTargetLowering() 1685ffd83dbSDimitry Andric ->supportSwiftError() && 1695ffd83dbSDimitry Andric MF->getFunction().getAttributes().hasAttrSomewhere( 1705ffd83dbSDimitry Andric Attribute::SwiftError)) 1715ffd83dbSDimitry Andric return CSR_Darwin_AArch64_AAPCS_SwiftError_SaveList; 172fe6060f1SDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::SwiftTail) 173fe6060f1SDimitry Andric return CSR_Darwin_AArch64_AAPCS_SwiftTail_SaveList; 1745ffd83dbSDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::PreserveMost) 1755ffd83dbSDimitry Andric return CSR_Darwin_AArch64_RT_MostRegs_SaveList; 17606c3fb27SDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::PreserveAll) 17706c3fb27SDimitry Andric return CSR_Darwin_AArch64_RT_AllRegs_SaveList; 178bdd1243dSDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::Win64) 179bdd1243dSDimitry Andric return CSR_Darwin_AArch64_AAPCS_Win64_SaveList; 1805ffd83dbSDimitry Andric return CSR_Darwin_AArch64_AAPCS_SaveList; 1815ffd83dbSDimitry Andric } 1825ffd83dbSDimitry Andric 1830b57cec5SDimitry Andric const MCPhysReg *AArch64RegisterInfo::getCalleeSavedRegsViaCopy( 1840b57cec5SDimitry Andric const MachineFunction *MF) const { 1850b57cec5SDimitry Andric assert(MF && "Invalid MachineFunction pointer."); 1860b57cec5SDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS && 1870b57cec5SDimitry Andric MF->getInfo<AArch64FunctionInfo>()->isSplitCSR()) 1885ffd83dbSDimitry Andric return CSR_Darwin_AArch64_CXX_TLS_ViaCopy_SaveList; 1890b57cec5SDimitry Andric return nullptr; 1900b57cec5SDimitry Andric } 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric void AArch64RegisterInfo::UpdateCustomCalleeSavedRegs( 1930b57cec5SDimitry Andric MachineFunction &MF) const { 1940b57cec5SDimitry Andric const MCPhysReg *CSRs = getCalleeSavedRegs(&MF); 1950b57cec5SDimitry Andric SmallVector<MCPhysReg, 32> UpdatedCSRs; 1960b57cec5SDimitry Andric for (const MCPhysReg *I = CSRs; *I; ++I) 1970b57cec5SDimitry Andric UpdatedCSRs.push_back(*I); 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric for (size_t i = 0; i < AArch64::GPR64commonRegClass.getNumRegs(); ++i) { 2000b57cec5SDimitry Andric if (MF.getSubtarget<AArch64Subtarget>().isXRegCustomCalleeSaved(i)) { 2010b57cec5SDimitry Andric UpdatedCSRs.push_back(AArch64::GPR64commonRegClass.getRegister(i)); 2020b57cec5SDimitry Andric } 2030b57cec5SDimitry Andric } 2040b57cec5SDimitry Andric // Register lists are zero-terminated. 2050b57cec5SDimitry Andric UpdatedCSRs.push_back(0); 2060b57cec5SDimitry Andric MF.getRegInfo().setCalleeSavedRegs(UpdatedCSRs); 2070b57cec5SDimitry Andric } 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric const TargetRegisterClass * 2100b57cec5SDimitry Andric AArch64RegisterInfo::getSubClassWithSubReg(const TargetRegisterClass *RC, 2110b57cec5SDimitry Andric unsigned Idx) const { 2120b57cec5SDimitry Andric // edge case for GPR/FPR register classes 2130b57cec5SDimitry Andric if (RC == &AArch64::GPR32allRegClass && Idx == AArch64::hsub) 2140b57cec5SDimitry Andric return &AArch64::FPR32RegClass; 2150b57cec5SDimitry Andric else if (RC == &AArch64::GPR64allRegClass && Idx == AArch64::hsub) 2160b57cec5SDimitry Andric return &AArch64::FPR64RegClass; 2170b57cec5SDimitry Andric 2180b57cec5SDimitry Andric // Forward to TableGen's default version. 2190b57cec5SDimitry Andric return AArch64GenRegisterInfo::getSubClassWithSubReg(RC, Idx); 2200b57cec5SDimitry Andric } 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric const uint32_t * 2235ffd83dbSDimitry Andric AArch64RegisterInfo::getDarwinCallPreservedMask(const MachineFunction &MF, 2245ffd83dbSDimitry Andric CallingConv::ID CC) const { 2255ffd83dbSDimitry Andric assert(MF.getSubtarget<AArch64Subtarget>().isTargetDarwin() && 2265ffd83dbSDimitry Andric "Invalid subtarget for getDarwinCallPreservedMask"); 2275ffd83dbSDimitry Andric 2285ffd83dbSDimitry Andric if (CC == CallingConv::CXX_FAST_TLS) 2295ffd83dbSDimitry Andric return CSR_Darwin_AArch64_CXX_TLS_RegMask; 2305ffd83dbSDimitry Andric if (CC == CallingConv::AArch64_VectorCall) 2315ffd83dbSDimitry Andric return CSR_Darwin_AArch64_AAVPCS_RegMask; 2325ffd83dbSDimitry Andric if (CC == CallingConv::AArch64_SVE_VectorCall) 2335ffd83dbSDimitry Andric report_fatal_error( 2345ffd83dbSDimitry Andric "Calling convention SVE_VectorCall is unsupported on Darwin."); 235bdd1243dSDimitry Andric if (CC == CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0) 236bdd1243dSDimitry Andric report_fatal_error( 237bdd1243dSDimitry Andric "Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0 is " 238bdd1243dSDimitry Andric "unsupported on Darwin."); 239bdd1243dSDimitry Andric if (CC == CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2) 240bdd1243dSDimitry Andric report_fatal_error( 241bdd1243dSDimitry Andric "Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2 is " 242bdd1243dSDimitry Andric "unsupported on Darwin."); 2435ffd83dbSDimitry Andric if (CC == CallingConv::CFGuard_Check) 2445ffd83dbSDimitry Andric report_fatal_error( 2455ffd83dbSDimitry Andric "Calling convention CFGuard_Check is unsupported on Darwin."); 2465ffd83dbSDimitry Andric if (MF.getSubtarget<AArch64Subtarget>() 2475ffd83dbSDimitry Andric .getTargetLowering() 2485ffd83dbSDimitry Andric ->supportSwiftError() && 2495ffd83dbSDimitry Andric MF.getFunction().getAttributes().hasAttrSomewhere(Attribute::SwiftError)) 2505ffd83dbSDimitry Andric return CSR_Darwin_AArch64_AAPCS_SwiftError_RegMask; 251fe6060f1SDimitry Andric if (CC == CallingConv::SwiftTail) 252fe6060f1SDimitry Andric return CSR_Darwin_AArch64_AAPCS_SwiftTail_RegMask; 2535ffd83dbSDimitry Andric if (CC == CallingConv::PreserveMost) 2545ffd83dbSDimitry Andric return CSR_Darwin_AArch64_RT_MostRegs_RegMask; 25506c3fb27SDimitry Andric if (CC == CallingConv::PreserveAll) 25606c3fb27SDimitry Andric return CSR_Darwin_AArch64_RT_AllRegs_RegMask; 2575ffd83dbSDimitry Andric return CSR_Darwin_AArch64_AAPCS_RegMask; 2585ffd83dbSDimitry Andric } 2595ffd83dbSDimitry Andric 2605ffd83dbSDimitry Andric const uint32_t * 2610b57cec5SDimitry Andric AArch64RegisterInfo::getCallPreservedMask(const MachineFunction &MF, 2620b57cec5SDimitry Andric CallingConv::ID CC) const { 2630b57cec5SDimitry Andric bool SCS = MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack); 2640b57cec5SDimitry Andric if (CC == CallingConv::GHC) 2650b57cec5SDimitry Andric // This is academic because all GHC calls are (supposed to be) tail calls 2660b57cec5SDimitry Andric return SCS ? CSR_AArch64_NoRegs_SCS_RegMask : CSR_AArch64_NoRegs_RegMask; 2670b57cec5SDimitry Andric if (CC == CallingConv::AnyReg) 2680b57cec5SDimitry Andric return SCS ? CSR_AArch64_AllRegs_SCS_RegMask : CSR_AArch64_AllRegs_RegMask; 2695ffd83dbSDimitry Andric 2705ffd83dbSDimitry Andric // All the following calling conventions are handled differently on Darwin. 2715ffd83dbSDimitry Andric if (MF.getSubtarget<AArch64Subtarget>().isTargetDarwin()) { 2725ffd83dbSDimitry Andric if (SCS) 2735ffd83dbSDimitry Andric report_fatal_error("ShadowCallStack attribute not supported on Darwin."); 2745ffd83dbSDimitry Andric return getDarwinCallPreservedMask(MF, CC); 2755ffd83dbSDimitry Andric } 2765ffd83dbSDimitry Andric 2770b57cec5SDimitry Andric if (CC == CallingConv::AArch64_VectorCall) 2780b57cec5SDimitry Andric return SCS ? CSR_AArch64_AAVPCS_SCS_RegMask : CSR_AArch64_AAVPCS_RegMask; 2798bcb0991SDimitry Andric if (CC == CallingConv::AArch64_SVE_VectorCall) 280480093f4SDimitry Andric return SCS ? CSR_AArch64_SVE_AAPCS_SCS_RegMask 281480093f4SDimitry Andric : CSR_AArch64_SVE_AAPCS_RegMask; 282bdd1243dSDimitry Andric if (CC == CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0) 283bdd1243dSDimitry Andric return CSR_AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0_RegMask; 284bdd1243dSDimitry Andric if (CC == CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2) 285bdd1243dSDimitry Andric return CSR_AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2_RegMask; 286480093f4SDimitry Andric if (CC == CallingConv::CFGuard_Check) 287480093f4SDimitry Andric return CSR_Win_AArch64_CFGuard_Check_RegMask; 2880b57cec5SDimitry Andric if (MF.getSubtarget<AArch64Subtarget>().getTargetLowering() 2890b57cec5SDimitry Andric ->supportSwiftError() && 2900b57cec5SDimitry Andric MF.getFunction().getAttributes().hasAttrSomewhere(Attribute::SwiftError)) 2910b57cec5SDimitry Andric return SCS ? CSR_AArch64_AAPCS_SwiftError_SCS_RegMask 2920b57cec5SDimitry Andric : CSR_AArch64_AAPCS_SwiftError_RegMask; 293fe6060f1SDimitry Andric if (CC == CallingConv::SwiftTail) { 294fe6060f1SDimitry Andric if (SCS) 295fe6060f1SDimitry Andric report_fatal_error("ShadowCallStack attribute not supported with swifttail"); 296fe6060f1SDimitry Andric return CSR_AArch64_AAPCS_SwiftTail_RegMask; 297fe6060f1SDimitry Andric } 2980b57cec5SDimitry Andric if (CC == CallingConv::PreserveMost) 2990b57cec5SDimitry Andric return SCS ? CSR_AArch64_RT_MostRegs_SCS_RegMask 3000b57cec5SDimitry Andric : CSR_AArch64_RT_MostRegs_RegMask; 30106c3fb27SDimitry Andric else if (CC == CallingConv::PreserveAll) 30206c3fb27SDimitry Andric return SCS ? CSR_AArch64_RT_AllRegs_SCS_RegMask 30306c3fb27SDimitry Andric : CSR_AArch64_RT_AllRegs_RegMask; 30406c3fb27SDimitry Andric 3050b57cec5SDimitry Andric else 3060b57cec5SDimitry Andric return SCS ? CSR_AArch64_AAPCS_SCS_RegMask : CSR_AArch64_AAPCS_RegMask; 3070b57cec5SDimitry Andric } 3080b57cec5SDimitry Andric 309e8d8bef9SDimitry Andric const uint32_t *AArch64RegisterInfo::getCustomEHPadPreservedMask( 310e8d8bef9SDimitry Andric const MachineFunction &MF) const { 311e8d8bef9SDimitry Andric if (MF.getSubtarget<AArch64Subtarget>().isTargetLinux()) 312e8d8bef9SDimitry Andric return CSR_AArch64_AAPCS_RegMask; 313e8d8bef9SDimitry Andric 314e8d8bef9SDimitry Andric return nullptr; 315e8d8bef9SDimitry Andric } 316e8d8bef9SDimitry Andric 3170b57cec5SDimitry Andric const uint32_t *AArch64RegisterInfo::getTLSCallPreservedMask() const { 3180b57cec5SDimitry Andric if (TT.isOSDarwin()) 3195ffd83dbSDimitry Andric return CSR_Darwin_AArch64_TLS_RegMask; 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric assert(TT.isOSBinFormatELF() && "Invalid target"); 3220b57cec5SDimitry Andric return CSR_AArch64_TLS_ELF_RegMask; 3230b57cec5SDimitry Andric } 3240b57cec5SDimitry Andric 3250b57cec5SDimitry Andric void AArch64RegisterInfo::UpdateCustomCallPreservedMask(MachineFunction &MF, 3260b57cec5SDimitry Andric const uint32_t **Mask) const { 3270b57cec5SDimitry Andric uint32_t *UpdatedMask = MF.allocateRegMask(); 3280b57cec5SDimitry Andric unsigned RegMaskSize = MachineOperand::getRegMaskSize(getNumRegs()); 3290b57cec5SDimitry Andric memcpy(UpdatedMask, *Mask, sizeof(UpdatedMask[0]) * RegMaskSize); 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric for (size_t i = 0; i < AArch64::GPR64commonRegClass.getNumRegs(); ++i) { 3320b57cec5SDimitry Andric if (MF.getSubtarget<AArch64Subtarget>().isXRegCustomCalleeSaved(i)) { 33306c3fb27SDimitry Andric for (MCPhysReg SubReg : 33406c3fb27SDimitry Andric subregs_inclusive(AArch64::GPR64commonRegClass.getRegister(i))) { 3350b57cec5SDimitry Andric // See TargetRegisterInfo::getCallPreservedMask for how to interpret the 3360b57cec5SDimitry Andric // register mask. 33706c3fb27SDimitry Andric UpdatedMask[SubReg / 32] |= 1u << (SubReg % 32); 3380b57cec5SDimitry Andric } 3390b57cec5SDimitry Andric } 3400b57cec5SDimitry Andric } 3410b57cec5SDimitry Andric *Mask = UpdatedMask; 3420b57cec5SDimitry Andric } 3430b57cec5SDimitry Andric 344bdd1243dSDimitry Andric const uint32_t *AArch64RegisterInfo::getSMStartStopCallPreservedMask() const { 345bdd1243dSDimitry Andric return CSR_AArch64_SMStartStop_RegMask; 346bdd1243dSDimitry Andric } 347bdd1243dSDimitry Andric 348bdd1243dSDimitry Andric const uint32_t * 349bdd1243dSDimitry Andric AArch64RegisterInfo::SMEABISupportRoutinesCallPreservedMaskFromX0() const { 350bdd1243dSDimitry Andric return CSR_AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0_RegMask; 351bdd1243dSDimitry Andric } 352bdd1243dSDimitry Andric 3530b57cec5SDimitry Andric const uint32_t *AArch64RegisterInfo::getNoPreservedMask() const { 3540b57cec5SDimitry Andric return CSR_AArch64_NoRegs_RegMask; 3550b57cec5SDimitry Andric } 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric const uint32_t * 3580b57cec5SDimitry Andric AArch64RegisterInfo::getThisReturnPreservedMask(const MachineFunction &MF, 3590b57cec5SDimitry Andric CallingConv::ID CC) const { 3600b57cec5SDimitry Andric // This should return a register mask that is the same as that returned by 3610b57cec5SDimitry Andric // getCallPreservedMask but that additionally preserves the register used for 3620b57cec5SDimitry Andric // the first i64 argument (which must also be the register used to return a 3630b57cec5SDimitry Andric // single i64 return value) 3640b57cec5SDimitry Andric // 3650b57cec5SDimitry Andric // In case that the calling convention does not use the same register for 3660b57cec5SDimitry Andric // both, the function should return NULL (does not currently apply) 3670b57cec5SDimitry Andric assert(CC != CallingConv::GHC && "should not be GHC calling convention."); 3685ffd83dbSDimitry Andric if (MF.getSubtarget<AArch64Subtarget>().isTargetDarwin()) 3695ffd83dbSDimitry Andric return CSR_Darwin_AArch64_AAPCS_ThisReturn_RegMask; 3700b57cec5SDimitry Andric return CSR_AArch64_AAPCS_ThisReturn_RegMask; 3710b57cec5SDimitry Andric } 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric const uint32_t *AArch64RegisterInfo::getWindowsStackProbePreservedMask() const { 3740b57cec5SDimitry Andric return CSR_AArch64_StackProbe_Windows_RegMask; 3750b57cec5SDimitry Andric } 3760b57cec5SDimitry Andric 377bdd1243dSDimitry Andric std::optional<std::string> 378bdd1243dSDimitry Andric AArch64RegisterInfo::explainReservedReg(const MachineFunction &MF, 379bdd1243dSDimitry Andric MCRegister PhysReg) const { 380bdd1243dSDimitry Andric if (hasBasePointer(MF) && MCRegisterInfo::regsOverlap(PhysReg, AArch64::X19)) 381bdd1243dSDimitry Andric return std::string("X19 is used as the frame base pointer register."); 382bdd1243dSDimitry Andric 383bdd1243dSDimitry Andric if (MF.getSubtarget<AArch64Subtarget>().isWindowsArm64EC()) { 384bdd1243dSDimitry Andric bool warn = false; 385bdd1243dSDimitry Andric if (MCRegisterInfo::regsOverlap(PhysReg, AArch64::X13) || 386bdd1243dSDimitry Andric MCRegisterInfo::regsOverlap(PhysReg, AArch64::X14) || 387bdd1243dSDimitry Andric MCRegisterInfo::regsOverlap(PhysReg, AArch64::X23) || 388bdd1243dSDimitry Andric MCRegisterInfo::regsOverlap(PhysReg, AArch64::X24) || 389bdd1243dSDimitry Andric MCRegisterInfo::regsOverlap(PhysReg, AArch64::X28)) 390bdd1243dSDimitry Andric warn = true; 391bdd1243dSDimitry Andric 392bdd1243dSDimitry Andric for (unsigned i = AArch64::B16; i <= AArch64::B31; ++i) 393bdd1243dSDimitry Andric if (MCRegisterInfo::regsOverlap(PhysReg, i)) 394bdd1243dSDimitry Andric warn = true; 395bdd1243dSDimitry Andric 396bdd1243dSDimitry Andric if (warn) 397bdd1243dSDimitry Andric return std::string(AArch64InstPrinter::getRegisterName(PhysReg)) + 398bdd1243dSDimitry Andric " is clobbered by asynchronous signals when using Arm64EC."; 399bdd1243dSDimitry Andric } 400bdd1243dSDimitry Andric 401bdd1243dSDimitry Andric return {}; 402bdd1243dSDimitry Andric } 403bdd1243dSDimitry Andric 4040b57cec5SDimitry Andric BitVector 405bdd1243dSDimitry Andric AArch64RegisterInfo::getStrictlyReservedRegs(const MachineFunction &MF) const { 4060b57cec5SDimitry Andric const AArch64FrameLowering *TFI = getFrameLowering(MF); 4070b57cec5SDimitry Andric 4080b57cec5SDimitry Andric // FIXME: avoid re-calculating this every time. 4090b57cec5SDimitry Andric BitVector Reserved(getNumRegs()); 4100b57cec5SDimitry Andric markSuperRegs(Reserved, AArch64::WSP); 4110b57cec5SDimitry Andric markSuperRegs(Reserved, AArch64::WZR); 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric if (TFI->hasFP(MF) || TT.isOSDarwin()) 4140b57cec5SDimitry Andric markSuperRegs(Reserved, AArch64::W29); 4150b57cec5SDimitry Andric 416bdd1243dSDimitry Andric if (MF.getSubtarget<AArch64Subtarget>().isWindowsArm64EC()) { 417bdd1243dSDimitry Andric // x13, x14, x23, x24, x28, and v16-v31 are clobbered by asynchronous 418bdd1243dSDimitry Andric // signals, so we can't ever use them. 419bdd1243dSDimitry Andric markSuperRegs(Reserved, AArch64::W13); 420bdd1243dSDimitry Andric markSuperRegs(Reserved, AArch64::W14); 421bdd1243dSDimitry Andric markSuperRegs(Reserved, AArch64::W23); 422bdd1243dSDimitry Andric markSuperRegs(Reserved, AArch64::W24); 423bdd1243dSDimitry Andric markSuperRegs(Reserved, AArch64::W28); 424bdd1243dSDimitry Andric for (unsigned i = AArch64::B16; i <= AArch64::B31; ++i) 425bdd1243dSDimitry Andric markSuperRegs(Reserved, i); 426bdd1243dSDimitry Andric } 427bdd1243dSDimitry Andric 4280b57cec5SDimitry Andric for (size_t i = 0; i < AArch64::GPR32commonRegClass.getNumRegs(); ++i) { 4290b57cec5SDimitry Andric if (MF.getSubtarget<AArch64Subtarget>().isXRegisterReserved(i)) 4300b57cec5SDimitry Andric markSuperRegs(Reserved, AArch64::GPR32commonRegClass.getRegister(i)); 4310b57cec5SDimitry Andric } 4320b57cec5SDimitry Andric 4330b57cec5SDimitry Andric if (hasBasePointer(MF)) 4340b57cec5SDimitry Andric markSuperRegs(Reserved, AArch64::W19); 4350b57cec5SDimitry Andric 4360b57cec5SDimitry Andric // SLH uses register W16/X16 as the taint register. 4370b57cec5SDimitry Andric if (MF.getFunction().hasFnAttribute(Attribute::SpeculativeLoadHardening)) 4380b57cec5SDimitry Andric markSuperRegs(Reserved, AArch64::W16); 4390b57cec5SDimitry Andric 44081ad6265SDimitry Andric // SME tiles are not allocatable. 44181ad6265SDimitry Andric if (MF.getSubtarget<AArch64Subtarget>().hasSME()) { 44206c3fb27SDimitry Andric for (MCPhysReg SubReg : subregs_inclusive(AArch64::ZA)) 44306c3fb27SDimitry Andric Reserved.set(SubReg); 44481ad6265SDimitry Andric } 44581ad6265SDimitry Andric 4465f757f3fSDimitry Andric if (MF.getSubtarget<AArch64Subtarget>().hasSME2()) { 4475f757f3fSDimitry Andric for (MCSubRegIterator SubReg(AArch64::ZT0, this, /*self=*/true); 4485f757f3fSDimitry Andric SubReg.isValid(); ++SubReg) 4495f757f3fSDimitry Andric Reserved.set(*SubReg); 4505f757f3fSDimitry Andric } 4515f757f3fSDimitry Andric 452bdd1243dSDimitry Andric markSuperRegs(Reserved, AArch64::FPCR); 453bdd1243dSDimitry Andric 4545f757f3fSDimitry Andric if (MF.getFunction().getCallingConv() == CallingConv::GRAAL) { 4555f757f3fSDimitry Andric markSuperRegs(Reserved, AArch64::X27); 4565f757f3fSDimitry Andric markSuperRegs(Reserved, AArch64::X28); 4575f757f3fSDimitry Andric markSuperRegs(Reserved, AArch64::W27); 4585f757f3fSDimitry Andric markSuperRegs(Reserved, AArch64::W28); 4595f757f3fSDimitry Andric } 4605f757f3fSDimitry Andric 461bdd1243dSDimitry Andric assert(checkAllSuperRegsMarked(Reserved)); 462bdd1243dSDimitry Andric return Reserved; 463bdd1243dSDimitry Andric } 464bdd1243dSDimitry Andric 465bdd1243dSDimitry Andric BitVector 466bdd1243dSDimitry Andric AArch64RegisterInfo::getReservedRegs(const MachineFunction &MF) const { 467bdd1243dSDimitry Andric BitVector Reserved = getStrictlyReservedRegs(MF); 468bdd1243dSDimitry Andric 469bdd1243dSDimitry Andric for (size_t i = 0; i < AArch64::GPR32commonRegClass.getNumRegs(); ++i) { 470bdd1243dSDimitry Andric if (MF.getSubtarget<AArch64Subtarget>().isXRegisterReservedForRA(i)) 471bdd1243dSDimitry Andric markSuperRegs(Reserved, AArch64::GPR32commonRegClass.getRegister(i)); 472bdd1243dSDimitry Andric } 473bdd1243dSDimitry Andric 4740b57cec5SDimitry Andric assert(checkAllSuperRegsMarked(Reserved)); 4750b57cec5SDimitry Andric return Reserved; 4760b57cec5SDimitry Andric } 4770b57cec5SDimitry Andric 4780b57cec5SDimitry Andric bool AArch64RegisterInfo::isReservedReg(const MachineFunction &MF, 4795ffd83dbSDimitry Andric MCRegister Reg) const { 4800b57cec5SDimitry Andric return getReservedRegs(MF)[Reg]; 4810b57cec5SDimitry Andric } 4820b57cec5SDimitry Andric 483bdd1243dSDimitry Andric bool AArch64RegisterInfo::isStrictlyReservedReg(const MachineFunction &MF, 484bdd1243dSDimitry Andric MCRegister Reg) const { 485bdd1243dSDimitry Andric return getStrictlyReservedRegs(MF)[Reg]; 486bdd1243dSDimitry Andric } 487bdd1243dSDimitry Andric 4880b57cec5SDimitry Andric bool AArch64RegisterInfo::isAnyArgRegReserved(const MachineFunction &MF) const { 489e8d8bef9SDimitry Andric return llvm::any_of(*AArch64::GPR64argRegClass.MC, [this, &MF](MCPhysReg r) { 490bdd1243dSDimitry Andric return isStrictlyReservedReg(MF, r); 491e8d8bef9SDimitry Andric }); 4920b57cec5SDimitry Andric } 4930b57cec5SDimitry Andric 4940b57cec5SDimitry Andric void AArch64RegisterInfo::emitReservedArgRegCallError( 4950b57cec5SDimitry Andric const MachineFunction &MF) const { 4960b57cec5SDimitry Andric const Function &F = MF.getFunction(); 497e8d8bef9SDimitry Andric F.getContext().diagnose(DiagnosticInfoUnsupported{F, ("AArch64 doesn't support" 498e8d8bef9SDimitry Andric " function calls if any of the argument registers is reserved.")}); 4990b57cec5SDimitry Andric } 5000b57cec5SDimitry Andric 5010b57cec5SDimitry Andric bool AArch64RegisterInfo::isAsmClobberable(const MachineFunction &MF, 5025ffd83dbSDimitry Andric MCRegister PhysReg) const { 503bdd1243dSDimitry Andric // SLH uses register X16 as the taint register but it will fallback to a different 504bdd1243dSDimitry Andric // method if the user clobbers it. So X16 is not reserved for inline asm but is 505bdd1243dSDimitry Andric // for normal codegen. 506bdd1243dSDimitry Andric if (MF.getFunction().hasFnAttribute(Attribute::SpeculativeLoadHardening) && 507bdd1243dSDimitry Andric MCRegisterInfo::regsOverlap(PhysReg, AArch64::X16)) 508bdd1243dSDimitry Andric return true; 5090b57cec5SDimitry Andric 510*74626c16SDimitry Andric // ZA/ZT0 registers are reserved but may be permitted in the clobber list. 511*74626c16SDimitry Andric if (PhysReg == AArch64::ZA || PhysReg == AArch64::ZT0) 512*74626c16SDimitry Andric return true; 513*74626c16SDimitry Andric 514bdd1243dSDimitry Andric return !isReservedReg(MF, PhysReg); 5150b57cec5SDimitry Andric } 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric const TargetRegisterClass * 5180b57cec5SDimitry Andric AArch64RegisterInfo::getPointerRegClass(const MachineFunction &MF, 5190b57cec5SDimitry Andric unsigned Kind) const { 5200b57cec5SDimitry Andric return &AArch64::GPR64spRegClass; 5210b57cec5SDimitry Andric } 5220b57cec5SDimitry Andric 5230b57cec5SDimitry Andric const TargetRegisterClass * 5240b57cec5SDimitry Andric AArch64RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const { 5250b57cec5SDimitry Andric if (RC == &AArch64::CCRRegClass) 5260b57cec5SDimitry Andric return &AArch64::GPR64RegClass; // Only MSR & MRS copy NZCV. 5270b57cec5SDimitry Andric return RC; 5280b57cec5SDimitry Andric } 5290b57cec5SDimitry Andric 5300b57cec5SDimitry Andric unsigned AArch64RegisterInfo::getBaseRegister() const { return AArch64::X19; } 5310b57cec5SDimitry Andric 5320b57cec5SDimitry Andric bool AArch64RegisterInfo::hasBasePointer(const MachineFunction &MF) const { 5330b57cec5SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo(); 5340b57cec5SDimitry Andric 5350b57cec5SDimitry Andric // In the presence of variable sized objects or funclets, if the fixed stack 5360b57cec5SDimitry Andric // size is large enough that referencing from the FP won't result in things 5370b57cec5SDimitry Andric // being in range relatively often, we can use a base pointer to allow access 5380b57cec5SDimitry Andric // from the other direction like the SP normally works. 5390b57cec5SDimitry Andric // 5400b57cec5SDimitry Andric // Furthermore, if both variable sized objects are present, and the 5410b57cec5SDimitry Andric // stack needs to be dynamically re-aligned, the base pointer is the only 5420b57cec5SDimitry Andric // reliable way to reference the locals. 5430b57cec5SDimitry Andric if (MFI.hasVarSizedObjects() || MF.hasEHFunclets()) { 544fe6060f1SDimitry Andric if (hasStackRealignment(MF)) 5450b57cec5SDimitry Andric return true; 546979e22ffSDimitry Andric 547979e22ffSDimitry Andric if (MF.getSubtarget<AArch64Subtarget>().hasSVE()) { 548979e22ffSDimitry Andric const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); 549979e22ffSDimitry Andric // Frames that have variable sized objects and scalable SVE objects, 550979e22ffSDimitry Andric // should always use a basepointer. 551979e22ffSDimitry Andric if (!AFI->hasCalculatedStackSizeSVE() || AFI->getStackSizeSVE()) 552979e22ffSDimitry Andric return true; 553979e22ffSDimitry Andric } 554979e22ffSDimitry Andric 5550b57cec5SDimitry Andric // Conservatively estimate whether the negative offset from the frame 5560b57cec5SDimitry Andric // pointer will be sufficient to reach. If a function has a smallish 5570b57cec5SDimitry Andric // frame, it's less likely to have lots of spills and callee saved 5580b57cec5SDimitry Andric // space, so it's all more likely to be within range of the frame pointer. 5590b57cec5SDimitry Andric // If it's wrong, we'll materialize the constant and still get to the 5600b57cec5SDimitry Andric // object; it's just suboptimal. Negative offsets use the unscaled 5610b57cec5SDimitry Andric // load/store instructions, which have a 9-bit signed immediate. 5620b57cec5SDimitry Andric return MFI.getLocalFrameSize() >= 256; 5630b57cec5SDimitry Andric } 5640b57cec5SDimitry Andric 5650b57cec5SDimitry Andric return false; 5660b57cec5SDimitry Andric } 5670b57cec5SDimitry Andric 56881ad6265SDimitry Andric bool AArch64RegisterInfo::isArgumentRegister(const MachineFunction &MF, 56981ad6265SDimitry Andric MCRegister Reg) const { 57081ad6265SDimitry Andric CallingConv::ID CC = MF.getFunction().getCallingConv(); 57181ad6265SDimitry Andric const AArch64Subtarget &STI = MF.getSubtarget<AArch64Subtarget>(); 57281ad6265SDimitry Andric bool IsVarArg = STI.isCallingConvWin64(MF.getFunction().getCallingConv()); 57381ad6265SDimitry Andric 57481ad6265SDimitry Andric auto HasReg = [](ArrayRef<MCRegister> RegList, MCRegister Reg) { 575bdd1243dSDimitry Andric return llvm::is_contained(RegList, Reg); 57681ad6265SDimitry Andric }; 57781ad6265SDimitry Andric 57881ad6265SDimitry Andric switch (CC) { 57981ad6265SDimitry Andric default: 58081ad6265SDimitry Andric report_fatal_error("Unsupported calling convention."); 58181ad6265SDimitry Andric case CallingConv::GHC: 58281ad6265SDimitry Andric return HasReg(CC_AArch64_GHC_ArgRegs, Reg); 58381ad6265SDimitry Andric case CallingConv::C: 58481ad6265SDimitry Andric case CallingConv::Fast: 58581ad6265SDimitry Andric case CallingConv::PreserveMost: 58606c3fb27SDimitry Andric case CallingConv::PreserveAll: 58781ad6265SDimitry Andric case CallingConv::CXX_FAST_TLS: 58881ad6265SDimitry Andric case CallingConv::Swift: 58981ad6265SDimitry Andric case CallingConv::SwiftTail: 59081ad6265SDimitry Andric case CallingConv::Tail: 5915f757f3fSDimitry Andric if (STI.isTargetWindows()) { 5925f757f3fSDimitry Andric if (IsVarArg) 59381ad6265SDimitry Andric return HasReg(CC_AArch64_Win64_VarArg_ArgRegs, Reg); 5945f757f3fSDimitry Andric switch (CC) { 5955f757f3fSDimitry Andric default: 5965f757f3fSDimitry Andric return HasReg(CC_AArch64_Win64PCS_ArgRegs, Reg); 5975f757f3fSDimitry Andric case CallingConv::Swift: 5985f757f3fSDimitry Andric case CallingConv::SwiftTail: 5995f757f3fSDimitry Andric return HasReg(CC_AArch64_Win64PCS_Swift_ArgRegs, Reg) || 6005f757f3fSDimitry Andric HasReg(CC_AArch64_Win64PCS_ArgRegs, Reg); 6015f757f3fSDimitry Andric } 6025f757f3fSDimitry Andric } 60381ad6265SDimitry Andric if (!STI.isTargetDarwin()) { 60481ad6265SDimitry Andric switch (CC) { 60581ad6265SDimitry Andric default: 60681ad6265SDimitry Andric return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg); 60781ad6265SDimitry Andric case CallingConv::Swift: 60881ad6265SDimitry Andric case CallingConv::SwiftTail: 60981ad6265SDimitry Andric return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg) || 61081ad6265SDimitry Andric HasReg(CC_AArch64_AAPCS_Swift_ArgRegs, Reg); 61181ad6265SDimitry Andric } 61281ad6265SDimitry Andric } 61381ad6265SDimitry Andric if (!IsVarArg) { 61481ad6265SDimitry Andric switch (CC) { 61581ad6265SDimitry Andric default: 61681ad6265SDimitry Andric return HasReg(CC_AArch64_DarwinPCS_ArgRegs, Reg); 61781ad6265SDimitry Andric case CallingConv::Swift: 61881ad6265SDimitry Andric case CallingConv::SwiftTail: 61981ad6265SDimitry Andric return HasReg(CC_AArch64_DarwinPCS_ArgRegs, Reg) || 62081ad6265SDimitry Andric HasReg(CC_AArch64_DarwinPCS_Swift_ArgRegs, Reg); 62181ad6265SDimitry Andric } 62281ad6265SDimitry Andric } 62381ad6265SDimitry Andric if (STI.isTargetILP32()) 62481ad6265SDimitry Andric return HasReg(CC_AArch64_DarwinPCS_ILP32_VarArg_ArgRegs, Reg); 62581ad6265SDimitry Andric return HasReg(CC_AArch64_DarwinPCS_VarArg_ArgRegs, Reg); 62681ad6265SDimitry Andric case CallingConv::Win64: 62781ad6265SDimitry Andric if (IsVarArg) 62881ad6265SDimitry Andric HasReg(CC_AArch64_Win64_VarArg_ArgRegs, Reg); 6295f757f3fSDimitry Andric return HasReg(CC_AArch64_Win64PCS_ArgRegs, Reg); 63081ad6265SDimitry Andric case CallingConv::CFGuard_Check: 63181ad6265SDimitry Andric return HasReg(CC_AArch64_Win64_CFGuard_Check_ArgRegs, Reg); 63281ad6265SDimitry Andric case CallingConv::AArch64_VectorCall: 63381ad6265SDimitry Andric case CallingConv::AArch64_SVE_VectorCall: 634bdd1243dSDimitry Andric case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0: 635bdd1243dSDimitry Andric case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2: 6365f757f3fSDimitry Andric if (STI.isTargetWindows()) 6375f757f3fSDimitry Andric return HasReg(CC_AArch64_Win64PCS_ArgRegs, Reg); 63881ad6265SDimitry Andric return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg); 63981ad6265SDimitry Andric } 64081ad6265SDimitry Andric } 64181ad6265SDimitry Andric 6420b57cec5SDimitry Andric Register 6430b57cec5SDimitry Andric AArch64RegisterInfo::getFrameRegister(const MachineFunction &MF) const { 6440b57cec5SDimitry Andric const AArch64FrameLowering *TFI = getFrameLowering(MF); 6450b57cec5SDimitry Andric return TFI->hasFP(MF) ? AArch64::FP : AArch64::SP; 6460b57cec5SDimitry Andric } 6470b57cec5SDimitry Andric 6480b57cec5SDimitry Andric bool AArch64RegisterInfo::requiresRegisterScavenging( 6490b57cec5SDimitry Andric const MachineFunction &MF) const { 6500b57cec5SDimitry Andric return true; 6510b57cec5SDimitry Andric } 6520b57cec5SDimitry Andric 6530b57cec5SDimitry Andric bool AArch64RegisterInfo::requiresVirtualBaseRegisters( 6540b57cec5SDimitry Andric const MachineFunction &MF) const { 6550b57cec5SDimitry Andric return true; 6560b57cec5SDimitry Andric } 6570b57cec5SDimitry Andric 6580b57cec5SDimitry Andric bool 6590b57cec5SDimitry Andric AArch64RegisterInfo::useFPForScavengingIndex(const MachineFunction &MF) const { 6600b57cec5SDimitry Andric // This function indicates whether the emergency spillslot should be placed 6610b57cec5SDimitry Andric // close to the beginning of the stackframe (closer to FP) or the end 6620b57cec5SDimitry Andric // (closer to SP). 6630b57cec5SDimitry Andric // 6640b57cec5SDimitry Andric // The beginning works most reliably if we have a frame pointer. 665979e22ffSDimitry Andric // In the presence of any non-constant space between FP and locals, 666979e22ffSDimitry Andric // (e.g. in case of stack realignment or a scalable SVE area), it is 667979e22ffSDimitry Andric // better to use SP or BP. 6680b57cec5SDimitry Andric const AArch64FrameLowering &TFI = *getFrameLowering(MF); 669979e22ffSDimitry Andric const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); 670979e22ffSDimitry Andric assert((!MF.getSubtarget<AArch64Subtarget>().hasSVE() || 671979e22ffSDimitry Andric AFI->hasCalculatedStackSizeSVE()) && 672979e22ffSDimitry Andric "Expected SVE area to be calculated by this point"); 673fe6060f1SDimitry Andric return TFI.hasFP(MF) && !hasStackRealignment(MF) && !AFI->getStackSizeSVE(); 6740b57cec5SDimitry Andric } 6750b57cec5SDimitry Andric 6760b57cec5SDimitry Andric bool AArch64RegisterInfo::requiresFrameIndexScavenging( 6770b57cec5SDimitry Andric const MachineFunction &MF) const { 6780b57cec5SDimitry Andric return true; 6790b57cec5SDimitry Andric } 6800b57cec5SDimitry Andric 6810b57cec5SDimitry Andric bool 6820b57cec5SDimitry Andric AArch64RegisterInfo::cannotEliminateFrame(const MachineFunction &MF) const { 6830b57cec5SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo(); 6840b57cec5SDimitry Andric if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI.adjustsStack()) 6850b57cec5SDimitry Andric return true; 6860b57cec5SDimitry Andric return MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken(); 6870b57cec5SDimitry Andric } 6880b57cec5SDimitry Andric 6890b57cec5SDimitry Andric /// needsFrameBaseReg - Returns true if the instruction's frame index 6900b57cec5SDimitry Andric /// reference would be better served by a base register other than FP 6910b57cec5SDimitry Andric /// or SP. Used by LocalStackFrameAllocation to determine which frame index 6920b57cec5SDimitry Andric /// references it should create new base registers for. 6930b57cec5SDimitry Andric bool AArch64RegisterInfo::needsFrameBaseReg(MachineInstr *MI, 6940b57cec5SDimitry Andric int64_t Offset) const { 6950b57cec5SDimitry Andric for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) 6960b57cec5SDimitry Andric assert(i < MI->getNumOperands() && 6970b57cec5SDimitry Andric "Instr doesn't have FrameIndex operand!"); 6980b57cec5SDimitry Andric 6990b57cec5SDimitry Andric // It's the load/store FI references that cause issues, as it can be difficult 7000b57cec5SDimitry Andric // to materialize the offset if it won't fit in the literal field. Estimate 7010b57cec5SDimitry Andric // based on the size of the local frame and some conservative assumptions 7020b57cec5SDimitry Andric // about the rest of the stack frame (note, this is pre-regalloc, so 7030b57cec5SDimitry Andric // we don't know everything for certain yet) whether this offset is likely 7040b57cec5SDimitry Andric // to be out of range of the immediate. Return true if so. 7050b57cec5SDimitry Andric 7060b57cec5SDimitry Andric // We only generate virtual base registers for loads and stores, so 7070b57cec5SDimitry Andric // return false for everything else. 7080b57cec5SDimitry Andric if (!MI->mayLoad() && !MI->mayStore()) 7090b57cec5SDimitry Andric return false; 7100b57cec5SDimitry Andric 7110b57cec5SDimitry Andric // Without a virtual base register, if the function has variable sized 7120b57cec5SDimitry Andric // objects, all fixed-size local references will be via the frame pointer, 7130b57cec5SDimitry Andric // Approximate the offset and see if it's legal for the instruction. 7140b57cec5SDimitry Andric // Note that the incoming offset is based on the SP value at function entry, 7150b57cec5SDimitry Andric // so it'll be negative. 7160b57cec5SDimitry Andric MachineFunction &MF = *MI->getParent()->getParent(); 7170b57cec5SDimitry Andric const AArch64FrameLowering *TFI = getFrameLowering(MF); 7180b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 7190b57cec5SDimitry Andric 7200b57cec5SDimitry Andric // Estimate an offset from the frame pointer. 7210b57cec5SDimitry Andric // Conservatively assume all GPR callee-saved registers get pushed. 7220b57cec5SDimitry Andric // FP, LR, X19-X28, D8-D15. 64-bits each. 7230b57cec5SDimitry Andric int64_t FPOffset = Offset - 16 * 20; 7240b57cec5SDimitry Andric // Estimate an offset from the stack pointer. 7250b57cec5SDimitry Andric // The incoming offset is relating to the SP at the start of the function, 7260b57cec5SDimitry Andric // but when we access the local it'll be relative to the SP after local 7270b57cec5SDimitry Andric // allocation, so adjust our SP-relative offset by that allocation size. 7280b57cec5SDimitry Andric Offset += MFI.getLocalFrameSize(); 7290b57cec5SDimitry Andric // Assume that we'll have at least some spill slots allocated. 7300b57cec5SDimitry Andric // FIXME: This is a total SWAG number. We should run some statistics 7310b57cec5SDimitry Andric // and pick a real one. 7320b57cec5SDimitry Andric Offset += 128; // 128 bytes of spill slots 7330b57cec5SDimitry Andric 7340b57cec5SDimitry Andric // If there is a frame pointer, try using it. 7350b57cec5SDimitry Andric // The FP is only available if there is no dynamic realignment. We 7360b57cec5SDimitry Andric // don't know for sure yet whether we'll need that, so we guess based 7370b57cec5SDimitry Andric // on whether there are any local variables that would trigger it. 7380b57cec5SDimitry Andric if (TFI->hasFP(MF) && isFrameOffsetLegal(MI, AArch64::FP, FPOffset)) 7390b57cec5SDimitry Andric return false; 7400b57cec5SDimitry Andric 7410b57cec5SDimitry Andric // If we can reference via the stack pointer or base pointer, try that. 7420b57cec5SDimitry Andric // FIXME: This (and the code that resolves the references) can be improved 7430b57cec5SDimitry Andric // to only disallow SP relative references in the live range of 7440b57cec5SDimitry Andric // the VLA(s). In practice, it's unclear how much difference that 7450b57cec5SDimitry Andric // would make, but it may be worth doing. 7460b57cec5SDimitry Andric if (isFrameOffsetLegal(MI, AArch64::SP, Offset)) 7470b57cec5SDimitry Andric return false; 7480b57cec5SDimitry Andric 7495ffd83dbSDimitry Andric // If even offset 0 is illegal, we don't want a virtual base register. 7505ffd83dbSDimitry Andric if (!isFrameOffsetLegal(MI, AArch64::SP, 0)) 7515ffd83dbSDimitry Andric return false; 7525ffd83dbSDimitry Andric 7530b57cec5SDimitry Andric // The offset likely isn't legal; we want to allocate a virtual base register. 7540b57cec5SDimitry Andric return true; 7550b57cec5SDimitry Andric } 7560b57cec5SDimitry Andric 7570b57cec5SDimitry Andric bool AArch64RegisterInfo::isFrameOffsetLegal(const MachineInstr *MI, 7585ffd83dbSDimitry Andric Register BaseReg, 7590b57cec5SDimitry Andric int64_t Offset) const { 7600b57cec5SDimitry Andric assert(MI && "Unable to get the legal offset for nil instruction."); 761e8d8bef9SDimitry Andric StackOffset SaveOffset = StackOffset::getFixed(Offset); 7620b57cec5SDimitry Andric return isAArch64FrameOffsetLegal(*MI, SaveOffset) & AArch64FrameOffsetIsLegal; 7630b57cec5SDimitry Andric } 7640b57cec5SDimitry Andric 7650b57cec5SDimitry Andric /// Insert defining instruction(s) for BaseReg to be a pointer to FrameIdx 7660b57cec5SDimitry Andric /// at the beginning of the basic block. 767e8d8bef9SDimitry Andric Register 768e8d8bef9SDimitry Andric AArch64RegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB, 7690b57cec5SDimitry Andric int FrameIdx, 7700b57cec5SDimitry Andric int64_t Offset) const { 7710b57cec5SDimitry Andric MachineBasicBlock::iterator Ins = MBB->begin(); 7720b57cec5SDimitry Andric DebugLoc DL; // Defaults to "unknown" 7730b57cec5SDimitry Andric if (Ins != MBB->end()) 7740b57cec5SDimitry Andric DL = Ins->getDebugLoc(); 7750b57cec5SDimitry Andric const MachineFunction &MF = *MBB->getParent(); 7760b57cec5SDimitry Andric const AArch64InstrInfo *TII = 7770b57cec5SDimitry Andric MF.getSubtarget<AArch64Subtarget>().getInstrInfo(); 7780b57cec5SDimitry Andric const MCInstrDesc &MCID = TII->get(AArch64::ADDXri); 7790b57cec5SDimitry Andric MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 780e8d8bef9SDimitry Andric Register BaseReg = MRI.createVirtualRegister(&AArch64::GPR64spRegClass); 7810b57cec5SDimitry Andric MRI.constrainRegClass(BaseReg, TII->getRegClass(MCID, 0, this, MF)); 7820b57cec5SDimitry Andric unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0); 7830b57cec5SDimitry Andric 7840b57cec5SDimitry Andric BuildMI(*MBB, Ins, DL, MCID, BaseReg) 7850b57cec5SDimitry Andric .addFrameIndex(FrameIdx) 7860b57cec5SDimitry Andric .addImm(Offset) 7870b57cec5SDimitry Andric .addImm(Shifter); 788e8d8bef9SDimitry Andric 789e8d8bef9SDimitry Andric return BaseReg; 7900b57cec5SDimitry Andric } 7910b57cec5SDimitry Andric 7925ffd83dbSDimitry Andric void AArch64RegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg, 7930b57cec5SDimitry Andric int64_t Offset) const { 7948bcb0991SDimitry Andric // ARM doesn't need the general 64-bit offsets 795e8d8bef9SDimitry Andric StackOffset Off = StackOffset::getFixed(Offset); 7968bcb0991SDimitry Andric 7970b57cec5SDimitry Andric unsigned i = 0; 7980b57cec5SDimitry Andric while (!MI.getOperand(i).isFI()) { 7990b57cec5SDimitry Andric ++i; 8000b57cec5SDimitry Andric assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!"); 8010b57cec5SDimitry Andric } 802e8d8bef9SDimitry Andric 8030b57cec5SDimitry Andric const MachineFunction *MF = MI.getParent()->getParent(); 8040b57cec5SDimitry Andric const AArch64InstrInfo *TII = 8050b57cec5SDimitry Andric MF->getSubtarget<AArch64Subtarget>().getInstrInfo(); 8060b57cec5SDimitry Andric bool Done = rewriteAArch64FrameIndex(MI, i, BaseReg, Off, TII); 8070b57cec5SDimitry Andric assert(Done && "Unable to resolve frame index!"); 8080b57cec5SDimitry Andric (void)Done; 8090b57cec5SDimitry Andric } 8100b57cec5SDimitry Andric 8115ffd83dbSDimitry Andric // Create a scratch register for the frame index elimination in an instruction. 8125ffd83dbSDimitry Andric // This function has special handling of stack tagging loop pseudos, in which 81381ad6265SDimitry Andric // case it can also change the instruction opcode. 8145ffd83dbSDimitry Andric static Register 81581ad6265SDimitry Andric createScratchRegisterForInstruction(MachineInstr &MI, unsigned FIOperandNum, 8165ffd83dbSDimitry Andric const AArch64InstrInfo *TII) { 8175ffd83dbSDimitry Andric // ST*Gloop have a reserved scratch register in operand 1. Use it, and also 8185ffd83dbSDimitry Andric // replace the instruction with the writeback variant because it will now 8195ffd83dbSDimitry Andric // satisfy the operand constraints for it. 82081ad6265SDimitry Andric Register ScratchReg; 82181ad6265SDimitry Andric if (MI.getOpcode() == AArch64::STGloop || 82281ad6265SDimitry Andric MI.getOpcode() == AArch64::STZGloop) { 82381ad6265SDimitry Andric assert(FIOperandNum == 3 && 82481ad6265SDimitry Andric "Wrong frame index operand for STGloop/STZGloop"); 82581ad6265SDimitry Andric unsigned Op = MI.getOpcode() == AArch64::STGloop ? AArch64::STGloop_wback 82681ad6265SDimitry Andric : AArch64::STZGloop_wback; 82781ad6265SDimitry Andric ScratchReg = MI.getOperand(1).getReg(); 82881ad6265SDimitry Andric MI.getOperand(3).ChangeToRegister(ScratchReg, false, false, true); 82981ad6265SDimitry Andric MI.setDesc(TII->get(Op)); 83081ad6265SDimitry Andric MI.tieOperands(1, 3); 8315ffd83dbSDimitry Andric } else { 83281ad6265SDimitry Andric ScratchReg = 83381ad6265SDimitry Andric MI.getMF()->getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass); 83481ad6265SDimitry Andric MI.getOperand(FIOperandNum) 83581ad6265SDimitry Andric .ChangeToRegister(ScratchReg, false, false, true); 8365ffd83dbSDimitry Andric } 83781ad6265SDimitry Andric return ScratchReg; 8385ffd83dbSDimitry Andric } 8395ffd83dbSDimitry Andric 840e8d8bef9SDimitry Andric void AArch64RegisterInfo::getOffsetOpcodes( 841e8d8bef9SDimitry Andric const StackOffset &Offset, SmallVectorImpl<uint64_t> &Ops) const { 842e8d8bef9SDimitry Andric // The smallest scalable element supported by scaled SVE addressing 843e8d8bef9SDimitry Andric // modes are predicates, which are 2 scalable bytes in size. So the scalable 844e8d8bef9SDimitry Andric // byte offset must always be a multiple of 2. 845e8d8bef9SDimitry Andric assert(Offset.getScalable() % 2 == 0 && "Invalid frame offset"); 846e8d8bef9SDimitry Andric 847e8d8bef9SDimitry Andric // Add fixed-sized offset using existing DIExpression interface. 848e8d8bef9SDimitry Andric DIExpression::appendOffset(Ops, Offset.getFixed()); 849e8d8bef9SDimitry Andric 850e8d8bef9SDimitry Andric unsigned VG = getDwarfRegNum(AArch64::VG, true); 851e8d8bef9SDimitry Andric int64_t VGSized = Offset.getScalable() / 2; 852e8d8bef9SDimitry Andric if (VGSized > 0) { 853e8d8bef9SDimitry Andric Ops.push_back(dwarf::DW_OP_constu); 854e8d8bef9SDimitry Andric Ops.push_back(VGSized); 855e8d8bef9SDimitry Andric Ops.append({dwarf::DW_OP_bregx, VG, 0ULL}); 856e8d8bef9SDimitry Andric Ops.push_back(dwarf::DW_OP_mul); 857e8d8bef9SDimitry Andric Ops.push_back(dwarf::DW_OP_plus); 858e8d8bef9SDimitry Andric } else if (VGSized < 0) { 859e8d8bef9SDimitry Andric Ops.push_back(dwarf::DW_OP_constu); 860e8d8bef9SDimitry Andric Ops.push_back(-VGSized); 861e8d8bef9SDimitry Andric Ops.append({dwarf::DW_OP_bregx, VG, 0ULL}); 862e8d8bef9SDimitry Andric Ops.push_back(dwarf::DW_OP_mul); 863e8d8bef9SDimitry Andric Ops.push_back(dwarf::DW_OP_minus); 864e8d8bef9SDimitry Andric } 865e8d8bef9SDimitry Andric } 866e8d8bef9SDimitry Andric 867bdd1243dSDimitry Andric bool AArch64RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 8680b57cec5SDimitry Andric int SPAdj, unsigned FIOperandNum, 8690b57cec5SDimitry Andric RegScavenger *RS) const { 8700b57cec5SDimitry Andric assert(SPAdj == 0 && "Unexpected"); 8710b57cec5SDimitry Andric 8720b57cec5SDimitry Andric MachineInstr &MI = *II; 8730b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 8740b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 8758bcb0991SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo(); 8760b57cec5SDimitry Andric const AArch64InstrInfo *TII = 8770b57cec5SDimitry Andric MF.getSubtarget<AArch64Subtarget>().getInstrInfo(); 8780b57cec5SDimitry Andric const AArch64FrameLowering *TFI = getFrameLowering(MF); 8790b57cec5SDimitry Andric int FrameIndex = MI.getOperand(FIOperandNum).getIndex(); 8808bcb0991SDimitry Andric bool Tagged = 8818bcb0991SDimitry Andric MI.getOperand(FIOperandNum).getTargetFlags() & AArch64II::MO_TAGGED; 8825ffd83dbSDimitry Andric Register FrameReg; 8830b57cec5SDimitry Andric 884e8d8bef9SDimitry Andric // Special handling of dbg_value, stackmap patchpoint statepoint instructions. 885e8d8bef9SDimitry Andric if (MI.getOpcode() == TargetOpcode::STACKMAP || 886e8d8bef9SDimitry Andric MI.getOpcode() == TargetOpcode::PATCHPOINT || 887e8d8bef9SDimitry Andric MI.getOpcode() == TargetOpcode::STATEPOINT) { 8888bcb0991SDimitry Andric StackOffset Offset = 8898bcb0991SDimitry Andric TFI->resolveFrameIndexReference(MF, FrameIndex, FrameReg, 8900b57cec5SDimitry Andric /*PreferFP=*/true, 8910b57cec5SDimitry Andric /*ForSimm=*/false); 892e8d8bef9SDimitry Andric Offset += StackOffset::getFixed(MI.getOperand(FIOperandNum + 1).getImm()); 8930b57cec5SDimitry Andric MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false /*isDef*/); 894e8d8bef9SDimitry Andric MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset.getFixed()); 895bdd1243dSDimitry Andric return false; 8960b57cec5SDimitry Andric } 8970b57cec5SDimitry Andric 8980b57cec5SDimitry Andric if (MI.getOpcode() == TargetOpcode::LOCAL_ESCAPE) { 8990b57cec5SDimitry Andric MachineOperand &FI = MI.getOperand(FIOperandNum); 900e8d8bef9SDimitry Andric StackOffset Offset = TFI->getNonLocalFrameIndexReference(MF, FrameIndex); 901e8d8bef9SDimitry Andric assert(!Offset.getScalable() && 902e8d8bef9SDimitry Andric "Frame offsets with a scalable component are not supported"); 903e8d8bef9SDimitry Andric FI.ChangeToImmediate(Offset.getFixed()); 904bdd1243dSDimitry Andric return false; 9050b57cec5SDimitry Andric } 9060b57cec5SDimitry Andric 9078bcb0991SDimitry Andric StackOffset Offset; 9080b57cec5SDimitry Andric if (MI.getOpcode() == AArch64::TAGPstack) { 9090b57cec5SDimitry Andric // TAGPstack must use the virtual frame register in its 3rd operand. 9100b57cec5SDimitry Andric const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); 9110b57cec5SDimitry Andric FrameReg = MI.getOperand(3).getReg(); 912e8d8bef9SDimitry Andric Offset = StackOffset::getFixed(MFI.getObjectOffset(FrameIndex) + 913e8d8bef9SDimitry Andric AFI->getTaggedBasePointerOffset()); 9148bcb0991SDimitry Andric } else if (Tagged) { 915e8d8bef9SDimitry Andric StackOffset SPOffset = StackOffset::getFixed( 916e8d8bef9SDimitry Andric MFI.getObjectOffset(FrameIndex) + (int64_t)MFI.getStackSize()); 9178bcb0991SDimitry Andric if (MFI.hasVarSizedObjects() || 9188bcb0991SDimitry Andric isAArch64FrameOffsetLegal(MI, SPOffset, nullptr, nullptr, nullptr) != 9198bcb0991SDimitry Andric (AArch64FrameOffsetCanUpdate | AArch64FrameOffsetIsLegal)) { 9208bcb0991SDimitry Andric // Can't update to SP + offset in place. Precalculate the tagged pointer 9218bcb0991SDimitry Andric // in a scratch register. 9228bcb0991SDimitry Andric Offset = TFI->resolveFrameIndexReference( 9238bcb0991SDimitry Andric MF, FrameIndex, FrameReg, /*PreferFP=*/false, /*ForSimm=*/true); 9248bcb0991SDimitry Andric Register ScratchReg = 9258bcb0991SDimitry Andric MF.getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass); 9268bcb0991SDimitry Andric emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset, 9278bcb0991SDimitry Andric TII); 9288bcb0991SDimitry Andric BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(AArch64::LDG), ScratchReg) 9298bcb0991SDimitry Andric .addReg(ScratchReg) 9308bcb0991SDimitry Andric .addReg(ScratchReg) 9318bcb0991SDimitry Andric .addImm(0); 9328bcb0991SDimitry Andric MI.getOperand(FIOperandNum) 9338bcb0991SDimitry Andric .ChangeToRegister(ScratchReg, false, false, true); 934bdd1243dSDimitry Andric return false; 9358bcb0991SDimitry Andric } 9368bcb0991SDimitry Andric FrameReg = AArch64::SP; 937e8d8bef9SDimitry Andric Offset = StackOffset::getFixed(MFI.getObjectOffset(FrameIndex) + 938e8d8bef9SDimitry Andric (int64_t)MFI.getStackSize()); 9390b57cec5SDimitry Andric } else { 9400b57cec5SDimitry Andric Offset = TFI->resolveFrameIndexReference( 9410b57cec5SDimitry Andric MF, FrameIndex, FrameReg, /*PreferFP=*/false, /*ForSimm=*/true); 9420b57cec5SDimitry Andric } 9430b57cec5SDimitry Andric 9440b57cec5SDimitry Andric // Modify MI as necessary to handle as much of 'Offset' as possible 9450b57cec5SDimitry Andric if (rewriteAArch64FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII)) 946bdd1243dSDimitry Andric return true; 9470b57cec5SDimitry Andric 9480b57cec5SDimitry Andric assert((!RS || !RS->isScavengingFrameIndex(FrameIndex)) && 9490b57cec5SDimitry Andric "Emergency spill slot is out of reach"); 9500b57cec5SDimitry Andric 9510b57cec5SDimitry Andric // If we get here, the immediate doesn't fit into the instruction. We folded 9520b57cec5SDimitry Andric // as much as possible above. Handle the rest, providing a register that is 9530b57cec5SDimitry Andric // SP+LargeImm. 95481ad6265SDimitry Andric Register ScratchReg = 95581ad6265SDimitry Andric createScratchRegisterForInstruction(MI, FIOperandNum, TII); 9560b57cec5SDimitry Andric emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset, TII); 957bdd1243dSDimitry Andric return false; 9580b57cec5SDimitry Andric } 9590b57cec5SDimitry Andric 9600b57cec5SDimitry Andric unsigned AArch64RegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC, 9610b57cec5SDimitry Andric MachineFunction &MF) const { 9620b57cec5SDimitry Andric const AArch64FrameLowering *TFI = getFrameLowering(MF); 9630b57cec5SDimitry Andric 9640b57cec5SDimitry Andric switch (RC->getID()) { 9650b57cec5SDimitry Andric default: 9660b57cec5SDimitry Andric return 0; 9670b57cec5SDimitry Andric case AArch64::GPR32RegClassID: 9680b57cec5SDimitry Andric case AArch64::GPR32spRegClassID: 9690b57cec5SDimitry Andric case AArch64::GPR32allRegClassID: 9700b57cec5SDimitry Andric case AArch64::GPR64spRegClassID: 9710b57cec5SDimitry Andric case AArch64::GPR64allRegClassID: 9720b57cec5SDimitry Andric case AArch64::GPR64RegClassID: 9730b57cec5SDimitry Andric case AArch64::GPR32commonRegClassID: 9740b57cec5SDimitry Andric case AArch64::GPR64commonRegClassID: 9750b57cec5SDimitry Andric return 32 - 1 // XZR/SP 9760b57cec5SDimitry Andric - (TFI->hasFP(MF) || TT.isOSDarwin()) // FP 9770b57cec5SDimitry Andric - MF.getSubtarget<AArch64Subtarget>().getNumXRegisterReserved() 9780b57cec5SDimitry Andric - hasBasePointer(MF); // X19 9790b57cec5SDimitry Andric case AArch64::FPR8RegClassID: 9800b57cec5SDimitry Andric case AArch64::FPR16RegClassID: 9810b57cec5SDimitry Andric case AArch64::FPR32RegClassID: 9820b57cec5SDimitry Andric case AArch64::FPR64RegClassID: 9830b57cec5SDimitry Andric case AArch64::FPR128RegClassID: 9840b57cec5SDimitry Andric return 32; 9850b57cec5SDimitry Andric 986bdd1243dSDimitry Andric case AArch64::MatrixIndexGPR32_8_11RegClassID: 987fe6060f1SDimitry Andric case AArch64::MatrixIndexGPR32_12_15RegClassID: 988fe6060f1SDimitry Andric return 4; 989fe6060f1SDimitry Andric 9900b57cec5SDimitry Andric case AArch64::DDRegClassID: 9910b57cec5SDimitry Andric case AArch64::DDDRegClassID: 9920b57cec5SDimitry Andric case AArch64::DDDDRegClassID: 9930b57cec5SDimitry Andric case AArch64::QQRegClassID: 9940b57cec5SDimitry Andric case AArch64::QQQRegClassID: 9950b57cec5SDimitry Andric case AArch64::QQQQRegClassID: 9960b57cec5SDimitry Andric return 32; 9970b57cec5SDimitry Andric 9980b57cec5SDimitry Andric case AArch64::FPR128_loRegClassID: 9995ffd83dbSDimitry Andric case AArch64::FPR64_loRegClassID: 10005ffd83dbSDimitry Andric case AArch64::FPR16_loRegClassID: 10010b57cec5SDimitry Andric return 16; 10025f757f3fSDimitry Andric case AArch64::FPR128_0to7RegClassID: 10035f757f3fSDimitry Andric return 8; 10040b57cec5SDimitry Andric } 10050b57cec5SDimitry Andric } 10060b57cec5SDimitry Andric 10070b57cec5SDimitry Andric unsigned AArch64RegisterInfo::getLocalAddressRegister( 10080b57cec5SDimitry Andric const MachineFunction &MF) const { 10090b57cec5SDimitry Andric const auto &MFI = MF.getFrameInfo(); 10100b57cec5SDimitry Andric if (!MF.hasEHFunclets() && !MFI.hasVarSizedObjects()) 10110b57cec5SDimitry Andric return AArch64::SP; 1012fe6060f1SDimitry Andric else if (hasStackRealignment(MF)) 10130b57cec5SDimitry Andric return getBaseRegister(); 10140b57cec5SDimitry Andric return getFrameRegister(MF); 10150b57cec5SDimitry Andric } 1016e8d8bef9SDimitry Andric 1017e8d8bef9SDimitry Andric /// SrcRC and DstRC will be morphed into NewRC if this returns true 1018e8d8bef9SDimitry Andric bool AArch64RegisterInfo::shouldCoalesce( 1019e8d8bef9SDimitry Andric MachineInstr *MI, const TargetRegisterClass *SrcRC, unsigned SubReg, 1020e8d8bef9SDimitry Andric const TargetRegisterClass *DstRC, unsigned DstSubReg, 1021e8d8bef9SDimitry Andric const TargetRegisterClass *NewRC, LiveIntervals &LIS) const { 1022b3edf446SDimitry Andric MachineRegisterInfo &MRI = MI->getMF()->getRegInfo(); 1023b3edf446SDimitry Andric 1024e8d8bef9SDimitry Andric if (MI->isCopy() && 1025e8d8bef9SDimitry Andric ((DstRC->getID() == AArch64::GPR64RegClassID) || 1026e8d8bef9SDimitry Andric (DstRC->getID() == AArch64::GPR64commonRegClassID)) && 1027e8d8bef9SDimitry Andric MI->getOperand(0).getSubReg() && MI->getOperand(1).getSubReg()) 1028e8d8bef9SDimitry Andric // Do not coalesce in the case of a 32-bit subregister copy 1029e8d8bef9SDimitry Andric // which implements a 32 to 64 bit zero extension 1030e8d8bef9SDimitry Andric // which relies on the upper 32 bits being zeroed. 1031e8d8bef9SDimitry Andric return false; 1032b3edf446SDimitry Andric 1033b3edf446SDimitry Andric auto IsCoalescerBarrier = [](const MachineInstr &MI) { 1034b3edf446SDimitry Andric switch (MI.getOpcode()) { 1035b3edf446SDimitry Andric case AArch64::COALESCER_BARRIER_FPR16: 1036b3edf446SDimitry Andric case AArch64::COALESCER_BARRIER_FPR32: 1037b3edf446SDimitry Andric case AArch64::COALESCER_BARRIER_FPR64: 1038b3edf446SDimitry Andric case AArch64::COALESCER_BARRIER_FPR128: 1039b3edf446SDimitry Andric return true; 1040b3edf446SDimitry Andric default: 1041b3edf446SDimitry Andric return false; 1042b3edf446SDimitry Andric } 1043b3edf446SDimitry Andric }; 1044b3edf446SDimitry Andric 1045b3edf446SDimitry Andric // For calls that temporarily have to toggle streaming mode as part of the 1046b3edf446SDimitry Andric // call-sequence, we need to be more careful when coalescing copy instructions 1047b3edf446SDimitry Andric // so that we don't end up coalescing the NEON/FP result or argument register 1048b3edf446SDimitry Andric // with a whole Z-register, such that after coalescing the register allocator 1049b3edf446SDimitry Andric // will try to spill/reload the entire Z register. 1050b3edf446SDimitry Andric // 1051b3edf446SDimitry Andric // We do this by checking if the node has any defs/uses that are 1052b3edf446SDimitry Andric // COALESCER_BARRIER pseudos. These are 'nops' in practice, but they exist to 1053b3edf446SDimitry Andric // instruct the coalescer to avoid coalescing the copy. 1054b3edf446SDimitry Andric if (MI->isCopy() && SubReg != DstSubReg && 1055b3edf446SDimitry Andric (AArch64::ZPRRegClass.hasSubClassEq(DstRC) || 1056b3edf446SDimitry Andric AArch64::ZPRRegClass.hasSubClassEq(SrcRC))) { 1057b3edf446SDimitry Andric unsigned SrcReg = MI->getOperand(1).getReg(); 1058b3edf446SDimitry Andric if (any_of(MRI.def_instructions(SrcReg), IsCoalescerBarrier)) 1059b3edf446SDimitry Andric return false; 1060b3edf446SDimitry Andric unsigned DstReg = MI->getOperand(0).getReg(); 1061b3edf446SDimitry Andric if (any_of(MRI.use_nodbg_instructions(DstReg), IsCoalescerBarrier)) 1062b3edf446SDimitry Andric return false; 1063b3edf446SDimitry Andric } 1064b3edf446SDimitry Andric 1065e8d8bef9SDimitry Andric return true; 1066e8d8bef9SDimitry Andric } 1067