1 //===- AArch64RegisterInfo.cpp - AArch64 Register Information -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains the AArch64 implementation of the TargetRegisterInfo 10 // class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "AArch64RegisterInfo.h" 15 #include "AArch64FrameLowering.h" 16 #include "AArch64InstrInfo.h" 17 #include "AArch64MachineFunctionInfo.h" 18 #include "AArch64Subtarget.h" 19 #include "MCTargetDesc/AArch64AddressingModes.h" 20 #include "llvm/ADT/BitVector.h" 21 #include "llvm/ADT/Triple.h" 22 #include "llvm/BinaryFormat/Dwarf.h" 23 #include "llvm/CodeGen/MachineFrameInfo.h" 24 #include "llvm/CodeGen/MachineInstrBuilder.h" 25 #include "llvm/CodeGen/MachineRegisterInfo.h" 26 #include "llvm/CodeGen/RegisterScavenging.h" 27 #include "llvm/CodeGen/TargetFrameLowering.h" 28 #include "llvm/IR/DebugInfoMetadata.h" 29 #include "llvm/IR/DiagnosticInfo.h" 30 #include "llvm/IR/Function.h" 31 #include "llvm/Support/raw_ostream.h" 32 #include "llvm/Target/TargetOptions.h" 33 34 using namespace llvm; 35 36 #define GET_CC_REGISTER_LISTS 37 #include "AArch64GenCallingConv.inc" 38 #define GET_REGINFO_TARGET_DESC 39 #include "AArch64GenRegisterInfo.inc" 40 41 AArch64RegisterInfo::AArch64RegisterInfo(const Triple &TT) 42 : AArch64GenRegisterInfo(AArch64::LR), TT(TT) { 43 AArch64_MC::initLLVMToCVRegMapping(this); 44 } 45 46 /// Return whether the register needs a CFI entry. Not all unwinders may know 47 /// about SVE registers, so we assume the lowest common denominator, i.e. the 48 /// callee-saves required by the base ABI. For the SVE registers z8-z15 only the 49 /// lower 64-bits (d8-d15) need to be saved. The lower 64-bits subreg is 50 /// returned in \p RegToUseForCFI. 51 bool AArch64RegisterInfo::regNeedsCFI(unsigned Reg, 52 unsigned &RegToUseForCFI) const { 53 if (AArch64::PPRRegClass.contains(Reg)) 54 return false; 55 56 if (AArch64::ZPRRegClass.contains(Reg)) { 57 RegToUseForCFI = getSubReg(Reg, AArch64::dsub); 58 for (int I = 0; CSR_AArch64_AAPCS_SaveList[I]; ++I) { 59 if (CSR_AArch64_AAPCS_SaveList[I] == RegToUseForCFI) 60 return true; 61 } 62 return false; 63 } 64 65 RegToUseForCFI = Reg; 66 return true; 67 } 68 69 bool AArch64RegisterInfo::hasSVEArgsOrReturn(const MachineFunction *MF) { 70 const Function &F = MF->getFunction(); 71 return isa<ScalableVectorType>(F.getReturnType()) || 72 any_of(F.args(), [](const Argument &Arg) { 73 return isa<ScalableVectorType>(Arg.getType()); 74 }); 75 } 76 77 const MCPhysReg * 78 AArch64RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const { 79 assert(MF && "Invalid MachineFunction pointer."); 80 81 if (MF->getFunction().getCallingConv() == CallingConv::GHC) 82 // GHC set of callee saved regs is empty as all those regs are 83 // used for passing STG regs around 84 return CSR_AArch64_NoRegs_SaveList; 85 if (MF->getFunction().getCallingConv() == CallingConv::AnyReg) 86 return CSR_AArch64_AllRegs_SaveList; 87 88 // Darwin has its own CSR_AArch64_AAPCS_SaveList, which means most CSR save 89 // lists depending on that will need to have their Darwin variant as well. 90 if (MF->getSubtarget<AArch64Subtarget>().isTargetDarwin()) 91 return getDarwinCalleeSavedRegs(MF); 92 93 if (MF->getFunction().getCallingConv() == CallingConv::CFGuard_Check) 94 return CSR_Win_AArch64_CFGuard_Check_SaveList; 95 if (MF->getSubtarget<AArch64Subtarget>().isTargetWindows()) 96 return CSR_Win_AArch64_AAPCS_SaveList; 97 if (MF->getFunction().getCallingConv() == CallingConv::AArch64_VectorCall) 98 return CSR_AArch64_AAVPCS_SaveList; 99 if (MF->getFunction().getCallingConv() == CallingConv::AArch64_SVE_VectorCall) 100 return CSR_AArch64_SVE_AAPCS_SaveList; 101 if (MF->getSubtarget<AArch64Subtarget>().getTargetLowering() 102 ->supportSwiftError() && 103 MF->getFunction().getAttributes().hasAttrSomewhere( 104 Attribute::SwiftError)) 105 return CSR_AArch64_AAPCS_SwiftError_SaveList; 106 if (MF->getFunction().getCallingConv() == CallingConv::SwiftTail) 107 return CSR_AArch64_AAPCS_SwiftTail_SaveList; 108 if (MF->getFunction().getCallingConv() == CallingConv::PreserveMost) 109 return CSR_AArch64_RT_MostRegs_SaveList; 110 if (MF->getFunction().getCallingConv() == CallingConv::Win64) 111 // This is for OSes other than Windows; Windows is a separate case further 112 // above. 113 return CSR_AArch64_AAPCS_X18_SaveList; 114 if (hasSVEArgsOrReturn(MF)) 115 return CSR_AArch64_SVE_AAPCS_SaveList; 116 return CSR_AArch64_AAPCS_SaveList; 117 } 118 119 const MCPhysReg * 120 AArch64RegisterInfo::getDarwinCalleeSavedRegs(const MachineFunction *MF) const { 121 assert(MF && "Invalid MachineFunction pointer."); 122 assert(MF->getSubtarget<AArch64Subtarget>().isTargetDarwin() && 123 "Invalid subtarget for getDarwinCalleeSavedRegs"); 124 125 if (MF->getFunction().getCallingConv() == CallingConv::CFGuard_Check) 126 report_fatal_error( 127 "Calling convention CFGuard_Check is unsupported on Darwin."); 128 if (MF->getFunction().getCallingConv() == CallingConv::AArch64_VectorCall) 129 return CSR_Darwin_AArch64_AAVPCS_SaveList; 130 if (MF->getFunction().getCallingConv() == CallingConv::AArch64_SVE_VectorCall) 131 report_fatal_error( 132 "Calling convention SVE_VectorCall is unsupported on Darwin."); 133 if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS) 134 return MF->getInfo<AArch64FunctionInfo>()->isSplitCSR() 135 ? CSR_Darwin_AArch64_CXX_TLS_PE_SaveList 136 : CSR_Darwin_AArch64_CXX_TLS_SaveList; 137 if (MF->getSubtarget<AArch64Subtarget>().getTargetLowering() 138 ->supportSwiftError() && 139 MF->getFunction().getAttributes().hasAttrSomewhere( 140 Attribute::SwiftError)) 141 return CSR_Darwin_AArch64_AAPCS_SwiftError_SaveList; 142 if (MF->getFunction().getCallingConv() == CallingConv::SwiftTail) 143 return CSR_Darwin_AArch64_AAPCS_SwiftTail_SaveList; 144 if (MF->getFunction().getCallingConv() == CallingConv::PreserveMost) 145 return CSR_Darwin_AArch64_RT_MostRegs_SaveList; 146 return CSR_Darwin_AArch64_AAPCS_SaveList; 147 } 148 149 const MCPhysReg *AArch64RegisterInfo::getCalleeSavedRegsViaCopy( 150 const MachineFunction *MF) const { 151 assert(MF && "Invalid MachineFunction pointer."); 152 if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS && 153 MF->getInfo<AArch64FunctionInfo>()->isSplitCSR()) 154 return CSR_Darwin_AArch64_CXX_TLS_ViaCopy_SaveList; 155 return nullptr; 156 } 157 158 void AArch64RegisterInfo::UpdateCustomCalleeSavedRegs( 159 MachineFunction &MF) const { 160 const MCPhysReg *CSRs = getCalleeSavedRegs(&MF); 161 SmallVector<MCPhysReg, 32> UpdatedCSRs; 162 for (const MCPhysReg *I = CSRs; *I; ++I) 163 UpdatedCSRs.push_back(*I); 164 165 for (size_t i = 0; i < AArch64::GPR64commonRegClass.getNumRegs(); ++i) { 166 if (MF.getSubtarget<AArch64Subtarget>().isXRegCustomCalleeSaved(i)) { 167 UpdatedCSRs.push_back(AArch64::GPR64commonRegClass.getRegister(i)); 168 } 169 } 170 // Register lists are zero-terminated. 171 UpdatedCSRs.push_back(0); 172 MF.getRegInfo().setCalleeSavedRegs(UpdatedCSRs); 173 } 174 175 const TargetRegisterClass * 176 AArch64RegisterInfo::getSubClassWithSubReg(const TargetRegisterClass *RC, 177 unsigned Idx) const { 178 // edge case for GPR/FPR register classes 179 if (RC == &AArch64::GPR32allRegClass && Idx == AArch64::hsub) 180 return &AArch64::FPR32RegClass; 181 else if (RC == &AArch64::GPR64allRegClass && Idx == AArch64::hsub) 182 return &AArch64::FPR64RegClass; 183 184 // Forward to TableGen's default version. 185 return AArch64GenRegisterInfo::getSubClassWithSubReg(RC, Idx); 186 } 187 188 const uint32_t * 189 AArch64RegisterInfo::getDarwinCallPreservedMask(const MachineFunction &MF, 190 CallingConv::ID CC) const { 191 assert(MF.getSubtarget<AArch64Subtarget>().isTargetDarwin() && 192 "Invalid subtarget for getDarwinCallPreservedMask"); 193 194 if (CC == CallingConv::CXX_FAST_TLS) 195 return CSR_Darwin_AArch64_CXX_TLS_RegMask; 196 if (CC == CallingConv::AArch64_VectorCall) 197 return CSR_Darwin_AArch64_AAVPCS_RegMask; 198 if (CC == CallingConv::AArch64_SVE_VectorCall) 199 report_fatal_error( 200 "Calling convention SVE_VectorCall is unsupported on Darwin."); 201 if (CC == CallingConv::CFGuard_Check) 202 report_fatal_error( 203 "Calling convention CFGuard_Check is unsupported on Darwin."); 204 if (MF.getSubtarget<AArch64Subtarget>() 205 .getTargetLowering() 206 ->supportSwiftError() && 207 MF.getFunction().getAttributes().hasAttrSomewhere(Attribute::SwiftError)) 208 return CSR_Darwin_AArch64_AAPCS_SwiftError_RegMask; 209 if (CC == CallingConv::SwiftTail) 210 return CSR_Darwin_AArch64_AAPCS_SwiftTail_RegMask; 211 if (CC == CallingConv::PreserveMost) 212 return CSR_Darwin_AArch64_RT_MostRegs_RegMask; 213 return CSR_Darwin_AArch64_AAPCS_RegMask; 214 } 215 216 const uint32_t * 217 AArch64RegisterInfo::getCallPreservedMask(const MachineFunction &MF, 218 CallingConv::ID CC) const { 219 bool SCS = MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack); 220 if (CC == CallingConv::GHC) 221 // This is academic because all GHC calls are (supposed to be) tail calls 222 return SCS ? CSR_AArch64_NoRegs_SCS_RegMask : CSR_AArch64_NoRegs_RegMask; 223 if (CC == CallingConv::AnyReg) 224 return SCS ? CSR_AArch64_AllRegs_SCS_RegMask : CSR_AArch64_AllRegs_RegMask; 225 226 // All the following calling conventions are handled differently on Darwin. 227 if (MF.getSubtarget<AArch64Subtarget>().isTargetDarwin()) { 228 if (SCS) 229 report_fatal_error("ShadowCallStack attribute not supported on Darwin."); 230 return getDarwinCallPreservedMask(MF, CC); 231 } 232 233 if (CC == CallingConv::AArch64_VectorCall) 234 return SCS ? CSR_AArch64_AAVPCS_SCS_RegMask : CSR_AArch64_AAVPCS_RegMask; 235 if (CC == CallingConv::AArch64_SVE_VectorCall) 236 return SCS ? CSR_AArch64_SVE_AAPCS_SCS_RegMask 237 : CSR_AArch64_SVE_AAPCS_RegMask; 238 if (CC == CallingConv::CFGuard_Check) 239 return CSR_Win_AArch64_CFGuard_Check_RegMask; 240 if (MF.getSubtarget<AArch64Subtarget>().getTargetLowering() 241 ->supportSwiftError() && 242 MF.getFunction().getAttributes().hasAttrSomewhere(Attribute::SwiftError)) 243 return SCS ? CSR_AArch64_AAPCS_SwiftError_SCS_RegMask 244 : CSR_AArch64_AAPCS_SwiftError_RegMask; 245 if (CC == CallingConv::SwiftTail) { 246 if (SCS) 247 report_fatal_error("ShadowCallStack attribute not supported with swifttail"); 248 return CSR_AArch64_AAPCS_SwiftTail_RegMask; 249 } 250 if (CC == CallingConv::PreserveMost) 251 return SCS ? CSR_AArch64_RT_MostRegs_SCS_RegMask 252 : CSR_AArch64_RT_MostRegs_RegMask; 253 else 254 return SCS ? CSR_AArch64_AAPCS_SCS_RegMask : CSR_AArch64_AAPCS_RegMask; 255 } 256 257 const uint32_t *AArch64RegisterInfo::getCustomEHPadPreservedMask( 258 const MachineFunction &MF) const { 259 if (MF.getSubtarget<AArch64Subtarget>().isTargetLinux()) 260 return CSR_AArch64_AAPCS_RegMask; 261 262 return nullptr; 263 } 264 265 const uint32_t *AArch64RegisterInfo::getTLSCallPreservedMask() const { 266 if (TT.isOSDarwin()) 267 return CSR_Darwin_AArch64_TLS_RegMask; 268 269 assert(TT.isOSBinFormatELF() && "Invalid target"); 270 return CSR_AArch64_TLS_ELF_RegMask; 271 } 272 273 void AArch64RegisterInfo::UpdateCustomCallPreservedMask(MachineFunction &MF, 274 const uint32_t **Mask) const { 275 uint32_t *UpdatedMask = MF.allocateRegMask(); 276 unsigned RegMaskSize = MachineOperand::getRegMaskSize(getNumRegs()); 277 memcpy(UpdatedMask, *Mask, sizeof(UpdatedMask[0]) * RegMaskSize); 278 279 for (size_t i = 0; i < AArch64::GPR64commonRegClass.getNumRegs(); ++i) { 280 if (MF.getSubtarget<AArch64Subtarget>().isXRegCustomCalleeSaved(i)) { 281 for (MCSubRegIterator SubReg(AArch64::GPR64commonRegClass.getRegister(i), 282 this, true); 283 SubReg.isValid(); ++SubReg) { 284 // See TargetRegisterInfo::getCallPreservedMask for how to interpret the 285 // register mask. 286 UpdatedMask[*SubReg / 32] |= 1u << (*SubReg % 32); 287 } 288 } 289 } 290 *Mask = UpdatedMask; 291 } 292 293 const uint32_t *AArch64RegisterInfo::getNoPreservedMask() const { 294 return CSR_AArch64_NoRegs_RegMask; 295 } 296 297 const uint32_t * 298 AArch64RegisterInfo::getThisReturnPreservedMask(const MachineFunction &MF, 299 CallingConv::ID CC) const { 300 // This should return a register mask that is the same as that returned by 301 // getCallPreservedMask but that additionally preserves the register used for 302 // the first i64 argument (which must also be the register used to return a 303 // single i64 return value) 304 // 305 // In case that the calling convention does not use the same register for 306 // both, the function should return NULL (does not currently apply) 307 assert(CC != CallingConv::GHC && "should not be GHC calling convention."); 308 if (MF.getSubtarget<AArch64Subtarget>().isTargetDarwin()) 309 return CSR_Darwin_AArch64_AAPCS_ThisReturn_RegMask; 310 return CSR_AArch64_AAPCS_ThisReturn_RegMask; 311 } 312 313 const uint32_t *AArch64RegisterInfo::getWindowsStackProbePreservedMask() const { 314 return CSR_AArch64_StackProbe_Windows_RegMask; 315 } 316 317 BitVector 318 AArch64RegisterInfo::getReservedRegs(const MachineFunction &MF) const { 319 const AArch64FrameLowering *TFI = getFrameLowering(MF); 320 321 // FIXME: avoid re-calculating this every time. 322 BitVector Reserved(getNumRegs()); 323 markSuperRegs(Reserved, AArch64::WSP); 324 markSuperRegs(Reserved, AArch64::WZR); 325 326 if (TFI->hasFP(MF) || TT.isOSDarwin()) 327 markSuperRegs(Reserved, AArch64::W29); 328 329 for (size_t i = 0; i < AArch64::GPR32commonRegClass.getNumRegs(); ++i) { 330 if (MF.getSubtarget<AArch64Subtarget>().isXRegisterReserved(i)) 331 markSuperRegs(Reserved, AArch64::GPR32commonRegClass.getRegister(i)); 332 } 333 334 if (hasBasePointer(MF)) 335 markSuperRegs(Reserved, AArch64::W19); 336 337 // SLH uses register W16/X16 as the taint register. 338 if (MF.getFunction().hasFnAttribute(Attribute::SpeculativeLoadHardening)) 339 markSuperRegs(Reserved, AArch64::W16); 340 341 // SME tiles are not allocatable. 342 if (MF.getSubtarget<AArch64Subtarget>().hasSME()) { 343 for (MCSubRegIterator SubReg(AArch64::ZA, this, /*self=*/true); 344 SubReg.isValid(); ++SubReg) 345 Reserved.set(*SubReg); 346 } 347 348 assert(checkAllSuperRegsMarked(Reserved)); 349 return Reserved; 350 } 351 352 bool AArch64RegisterInfo::isReservedReg(const MachineFunction &MF, 353 MCRegister Reg) const { 354 return getReservedRegs(MF)[Reg]; 355 } 356 357 bool AArch64RegisterInfo::isAnyArgRegReserved(const MachineFunction &MF) const { 358 return llvm::any_of(*AArch64::GPR64argRegClass.MC, [this, &MF](MCPhysReg r) { 359 return isReservedReg(MF, r); 360 }); 361 } 362 363 void AArch64RegisterInfo::emitReservedArgRegCallError( 364 const MachineFunction &MF) const { 365 const Function &F = MF.getFunction(); 366 F.getContext().diagnose(DiagnosticInfoUnsupported{F, ("AArch64 doesn't support" 367 " function calls if any of the argument registers is reserved.")}); 368 } 369 370 bool AArch64RegisterInfo::isAsmClobberable(const MachineFunction &MF, 371 MCRegister PhysReg) const { 372 return !isReservedReg(MF, PhysReg); 373 } 374 375 bool AArch64RegisterInfo::isConstantPhysReg(MCRegister PhysReg) const { 376 return PhysReg == AArch64::WZR || PhysReg == AArch64::XZR; 377 } 378 379 const TargetRegisterClass * 380 AArch64RegisterInfo::getPointerRegClass(const MachineFunction &MF, 381 unsigned Kind) const { 382 return &AArch64::GPR64spRegClass; 383 } 384 385 const TargetRegisterClass * 386 AArch64RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const { 387 if (RC == &AArch64::CCRRegClass) 388 return &AArch64::GPR64RegClass; // Only MSR & MRS copy NZCV. 389 return RC; 390 } 391 392 unsigned AArch64RegisterInfo::getBaseRegister() const { return AArch64::X19; } 393 394 bool AArch64RegisterInfo::hasBasePointer(const MachineFunction &MF) const { 395 const MachineFrameInfo &MFI = MF.getFrameInfo(); 396 397 // In the presence of variable sized objects or funclets, if the fixed stack 398 // size is large enough that referencing from the FP won't result in things 399 // being in range relatively often, we can use a base pointer to allow access 400 // from the other direction like the SP normally works. 401 // 402 // Furthermore, if both variable sized objects are present, and the 403 // stack needs to be dynamically re-aligned, the base pointer is the only 404 // reliable way to reference the locals. 405 if (MFI.hasVarSizedObjects() || MF.hasEHFunclets()) { 406 if (hasStackRealignment(MF)) 407 return true; 408 409 if (MF.getSubtarget<AArch64Subtarget>().hasSVE()) { 410 const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); 411 // Frames that have variable sized objects and scalable SVE objects, 412 // should always use a basepointer. 413 if (!AFI->hasCalculatedStackSizeSVE() || AFI->getStackSizeSVE()) 414 return true; 415 } 416 417 // Conservatively estimate whether the negative offset from the frame 418 // pointer will be sufficient to reach. If a function has a smallish 419 // frame, it's less likely to have lots of spills and callee saved 420 // space, so it's all more likely to be within range of the frame pointer. 421 // If it's wrong, we'll materialize the constant and still get to the 422 // object; it's just suboptimal. Negative offsets use the unscaled 423 // load/store instructions, which have a 9-bit signed immediate. 424 return MFI.getLocalFrameSize() >= 256; 425 } 426 427 return false; 428 } 429 430 bool AArch64RegisterInfo::isArgumentRegister(const MachineFunction &MF, 431 MCRegister Reg) const { 432 CallingConv::ID CC = MF.getFunction().getCallingConv(); 433 const AArch64Subtarget &STI = MF.getSubtarget<AArch64Subtarget>(); 434 bool IsVarArg = STI.isCallingConvWin64(MF.getFunction().getCallingConv()); 435 436 auto HasReg = [](ArrayRef<MCRegister> RegList, MCRegister Reg) { 437 return llvm::any_of(RegList, 438 [Reg](const MCRegister R) { return R == Reg; }); 439 }; 440 441 switch (CC) { 442 default: 443 report_fatal_error("Unsupported calling convention."); 444 case CallingConv::WebKit_JS: 445 return HasReg(CC_AArch64_WebKit_JS_ArgRegs, Reg); 446 case CallingConv::GHC: 447 return HasReg(CC_AArch64_GHC_ArgRegs, Reg); 448 case CallingConv::C: 449 case CallingConv::Fast: 450 case CallingConv::PreserveMost: 451 case CallingConv::CXX_FAST_TLS: 452 case CallingConv::Swift: 453 case CallingConv::SwiftTail: 454 case CallingConv::Tail: 455 if (STI.isTargetWindows() && IsVarArg) 456 return HasReg(CC_AArch64_Win64_VarArg_ArgRegs, Reg); 457 if (!STI.isTargetDarwin()) { 458 switch (CC) { 459 default: 460 return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg); 461 case CallingConv::Swift: 462 case CallingConv::SwiftTail: 463 return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg) || 464 HasReg(CC_AArch64_AAPCS_Swift_ArgRegs, Reg); 465 } 466 } 467 if (!IsVarArg) { 468 switch (CC) { 469 default: 470 return HasReg(CC_AArch64_DarwinPCS_ArgRegs, Reg); 471 case CallingConv::Swift: 472 case CallingConv::SwiftTail: 473 return HasReg(CC_AArch64_DarwinPCS_ArgRegs, Reg) || 474 HasReg(CC_AArch64_DarwinPCS_Swift_ArgRegs, Reg); 475 } 476 } 477 if (STI.isTargetILP32()) 478 return HasReg(CC_AArch64_DarwinPCS_ILP32_VarArg_ArgRegs, Reg); 479 return HasReg(CC_AArch64_DarwinPCS_VarArg_ArgRegs, Reg); 480 case CallingConv::Win64: 481 if (IsVarArg) 482 HasReg(CC_AArch64_Win64_VarArg_ArgRegs, Reg); 483 return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg); 484 case CallingConv::CFGuard_Check: 485 return HasReg(CC_AArch64_Win64_CFGuard_Check_ArgRegs, Reg); 486 case CallingConv::AArch64_VectorCall: 487 case CallingConv::AArch64_SVE_VectorCall: 488 return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg); 489 } 490 } 491 492 Register 493 AArch64RegisterInfo::getFrameRegister(const MachineFunction &MF) const { 494 const AArch64FrameLowering *TFI = getFrameLowering(MF); 495 return TFI->hasFP(MF) ? AArch64::FP : AArch64::SP; 496 } 497 498 bool AArch64RegisterInfo::requiresRegisterScavenging( 499 const MachineFunction &MF) const { 500 return true; 501 } 502 503 bool AArch64RegisterInfo::requiresVirtualBaseRegisters( 504 const MachineFunction &MF) const { 505 return true; 506 } 507 508 bool 509 AArch64RegisterInfo::useFPForScavengingIndex(const MachineFunction &MF) const { 510 // This function indicates whether the emergency spillslot should be placed 511 // close to the beginning of the stackframe (closer to FP) or the end 512 // (closer to SP). 513 // 514 // The beginning works most reliably if we have a frame pointer. 515 // In the presence of any non-constant space between FP and locals, 516 // (e.g. in case of stack realignment or a scalable SVE area), it is 517 // better to use SP or BP. 518 const AArch64FrameLowering &TFI = *getFrameLowering(MF); 519 const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); 520 assert((!MF.getSubtarget<AArch64Subtarget>().hasSVE() || 521 AFI->hasCalculatedStackSizeSVE()) && 522 "Expected SVE area to be calculated by this point"); 523 return TFI.hasFP(MF) && !hasStackRealignment(MF) && !AFI->getStackSizeSVE(); 524 } 525 526 bool AArch64RegisterInfo::requiresFrameIndexScavenging( 527 const MachineFunction &MF) const { 528 return true; 529 } 530 531 bool 532 AArch64RegisterInfo::cannotEliminateFrame(const MachineFunction &MF) const { 533 const MachineFrameInfo &MFI = MF.getFrameInfo(); 534 if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI.adjustsStack()) 535 return true; 536 return MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken(); 537 } 538 539 /// needsFrameBaseReg - Returns true if the instruction's frame index 540 /// reference would be better served by a base register other than FP 541 /// or SP. Used by LocalStackFrameAllocation to determine which frame index 542 /// references it should create new base registers for. 543 bool AArch64RegisterInfo::needsFrameBaseReg(MachineInstr *MI, 544 int64_t Offset) const { 545 for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) 546 assert(i < MI->getNumOperands() && 547 "Instr doesn't have FrameIndex operand!"); 548 549 // It's the load/store FI references that cause issues, as it can be difficult 550 // to materialize the offset if it won't fit in the literal field. Estimate 551 // based on the size of the local frame and some conservative assumptions 552 // about the rest of the stack frame (note, this is pre-regalloc, so 553 // we don't know everything for certain yet) whether this offset is likely 554 // to be out of range of the immediate. Return true if so. 555 556 // We only generate virtual base registers for loads and stores, so 557 // return false for everything else. 558 if (!MI->mayLoad() && !MI->mayStore()) 559 return false; 560 561 // Without a virtual base register, if the function has variable sized 562 // objects, all fixed-size local references will be via the frame pointer, 563 // Approximate the offset and see if it's legal for the instruction. 564 // Note that the incoming offset is based on the SP value at function entry, 565 // so it'll be negative. 566 MachineFunction &MF = *MI->getParent()->getParent(); 567 const AArch64FrameLowering *TFI = getFrameLowering(MF); 568 MachineFrameInfo &MFI = MF.getFrameInfo(); 569 570 // Estimate an offset from the frame pointer. 571 // Conservatively assume all GPR callee-saved registers get pushed. 572 // FP, LR, X19-X28, D8-D15. 64-bits each. 573 int64_t FPOffset = Offset - 16 * 20; 574 // Estimate an offset from the stack pointer. 575 // The incoming offset is relating to the SP at the start of the function, 576 // but when we access the local it'll be relative to the SP after local 577 // allocation, so adjust our SP-relative offset by that allocation size. 578 Offset += MFI.getLocalFrameSize(); 579 // Assume that we'll have at least some spill slots allocated. 580 // FIXME: This is a total SWAG number. We should run some statistics 581 // and pick a real one. 582 Offset += 128; // 128 bytes of spill slots 583 584 // If there is a frame pointer, try using it. 585 // The FP is only available if there is no dynamic realignment. We 586 // don't know for sure yet whether we'll need that, so we guess based 587 // on whether there are any local variables that would trigger it. 588 if (TFI->hasFP(MF) && isFrameOffsetLegal(MI, AArch64::FP, FPOffset)) 589 return false; 590 591 // If we can reference via the stack pointer or base pointer, try that. 592 // FIXME: This (and the code that resolves the references) can be improved 593 // to only disallow SP relative references in the live range of 594 // the VLA(s). In practice, it's unclear how much difference that 595 // would make, but it may be worth doing. 596 if (isFrameOffsetLegal(MI, AArch64::SP, Offset)) 597 return false; 598 599 // If even offset 0 is illegal, we don't want a virtual base register. 600 if (!isFrameOffsetLegal(MI, AArch64::SP, 0)) 601 return false; 602 603 // The offset likely isn't legal; we want to allocate a virtual base register. 604 return true; 605 } 606 607 bool AArch64RegisterInfo::isFrameOffsetLegal(const MachineInstr *MI, 608 Register BaseReg, 609 int64_t Offset) const { 610 assert(MI && "Unable to get the legal offset for nil instruction."); 611 StackOffset SaveOffset = StackOffset::getFixed(Offset); 612 return isAArch64FrameOffsetLegal(*MI, SaveOffset) & AArch64FrameOffsetIsLegal; 613 } 614 615 /// Insert defining instruction(s) for BaseReg to be a pointer to FrameIdx 616 /// at the beginning of the basic block. 617 Register 618 AArch64RegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB, 619 int FrameIdx, 620 int64_t Offset) const { 621 MachineBasicBlock::iterator Ins = MBB->begin(); 622 DebugLoc DL; // Defaults to "unknown" 623 if (Ins != MBB->end()) 624 DL = Ins->getDebugLoc(); 625 const MachineFunction &MF = *MBB->getParent(); 626 const AArch64InstrInfo *TII = 627 MF.getSubtarget<AArch64Subtarget>().getInstrInfo(); 628 const MCInstrDesc &MCID = TII->get(AArch64::ADDXri); 629 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 630 Register BaseReg = MRI.createVirtualRegister(&AArch64::GPR64spRegClass); 631 MRI.constrainRegClass(BaseReg, TII->getRegClass(MCID, 0, this, MF)); 632 unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0); 633 634 BuildMI(*MBB, Ins, DL, MCID, BaseReg) 635 .addFrameIndex(FrameIdx) 636 .addImm(Offset) 637 .addImm(Shifter); 638 639 return BaseReg; 640 } 641 642 void AArch64RegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg, 643 int64_t Offset) const { 644 // ARM doesn't need the general 64-bit offsets 645 StackOffset Off = StackOffset::getFixed(Offset); 646 647 unsigned i = 0; 648 while (!MI.getOperand(i).isFI()) { 649 ++i; 650 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!"); 651 } 652 653 const MachineFunction *MF = MI.getParent()->getParent(); 654 const AArch64InstrInfo *TII = 655 MF->getSubtarget<AArch64Subtarget>().getInstrInfo(); 656 bool Done = rewriteAArch64FrameIndex(MI, i, BaseReg, Off, TII); 657 assert(Done && "Unable to resolve frame index!"); 658 (void)Done; 659 } 660 661 // Create a scratch register for the frame index elimination in an instruction. 662 // This function has special handling of stack tagging loop pseudos, in which 663 // case it can also change the instruction opcode. 664 static Register 665 createScratchRegisterForInstruction(MachineInstr &MI, unsigned FIOperandNum, 666 const AArch64InstrInfo *TII) { 667 // ST*Gloop have a reserved scratch register in operand 1. Use it, and also 668 // replace the instruction with the writeback variant because it will now 669 // satisfy the operand constraints for it. 670 Register ScratchReg; 671 if (MI.getOpcode() == AArch64::STGloop || 672 MI.getOpcode() == AArch64::STZGloop) { 673 assert(FIOperandNum == 3 && 674 "Wrong frame index operand for STGloop/STZGloop"); 675 unsigned Op = MI.getOpcode() == AArch64::STGloop ? AArch64::STGloop_wback 676 : AArch64::STZGloop_wback; 677 ScratchReg = MI.getOperand(1).getReg(); 678 MI.getOperand(3).ChangeToRegister(ScratchReg, false, false, true); 679 MI.setDesc(TII->get(Op)); 680 MI.tieOperands(1, 3); 681 } else { 682 ScratchReg = 683 MI.getMF()->getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass); 684 MI.getOperand(FIOperandNum) 685 .ChangeToRegister(ScratchReg, false, false, true); 686 } 687 return ScratchReg; 688 } 689 690 void AArch64RegisterInfo::getOffsetOpcodes( 691 const StackOffset &Offset, SmallVectorImpl<uint64_t> &Ops) const { 692 // The smallest scalable element supported by scaled SVE addressing 693 // modes are predicates, which are 2 scalable bytes in size. So the scalable 694 // byte offset must always be a multiple of 2. 695 assert(Offset.getScalable() % 2 == 0 && "Invalid frame offset"); 696 697 // Add fixed-sized offset using existing DIExpression interface. 698 DIExpression::appendOffset(Ops, Offset.getFixed()); 699 700 unsigned VG = getDwarfRegNum(AArch64::VG, true); 701 int64_t VGSized = Offset.getScalable() / 2; 702 if (VGSized > 0) { 703 Ops.push_back(dwarf::DW_OP_constu); 704 Ops.push_back(VGSized); 705 Ops.append({dwarf::DW_OP_bregx, VG, 0ULL}); 706 Ops.push_back(dwarf::DW_OP_mul); 707 Ops.push_back(dwarf::DW_OP_plus); 708 } else if (VGSized < 0) { 709 Ops.push_back(dwarf::DW_OP_constu); 710 Ops.push_back(-VGSized); 711 Ops.append({dwarf::DW_OP_bregx, VG, 0ULL}); 712 Ops.push_back(dwarf::DW_OP_mul); 713 Ops.push_back(dwarf::DW_OP_minus); 714 } 715 } 716 717 void AArch64RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 718 int SPAdj, unsigned FIOperandNum, 719 RegScavenger *RS) const { 720 assert(SPAdj == 0 && "Unexpected"); 721 722 MachineInstr &MI = *II; 723 MachineBasicBlock &MBB = *MI.getParent(); 724 MachineFunction &MF = *MBB.getParent(); 725 const MachineFrameInfo &MFI = MF.getFrameInfo(); 726 const AArch64InstrInfo *TII = 727 MF.getSubtarget<AArch64Subtarget>().getInstrInfo(); 728 const AArch64FrameLowering *TFI = getFrameLowering(MF); 729 int FrameIndex = MI.getOperand(FIOperandNum).getIndex(); 730 bool Tagged = 731 MI.getOperand(FIOperandNum).getTargetFlags() & AArch64II::MO_TAGGED; 732 Register FrameReg; 733 734 // Special handling of dbg_value, stackmap patchpoint statepoint instructions. 735 if (MI.getOpcode() == TargetOpcode::STACKMAP || 736 MI.getOpcode() == TargetOpcode::PATCHPOINT || 737 MI.getOpcode() == TargetOpcode::STATEPOINT) { 738 StackOffset Offset = 739 TFI->resolveFrameIndexReference(MF, FrameIndex, FrameReg, 740 /*PreferFP=*/true, 741 /*ForSimm=*/false); 742 Offset += StackOffset::getFixed(MI.getOperand(FIOperandNum + 1).getImm()); 743 MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false /*isDef*/); 744 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset.getFixed()); 745 return; 746 } 747 748 if (MI.getOpcode() == TargetOpcode::LOCAL_ESCAPE) { 749 MachineOperand &FI = MI.getOperand(FIOperandNum); 750 StackOffset Offset = TFI->getNonLocalFrameIndexReference(MF, FrameIndex); 751 assert(!Offset.getScalable() && 752 "Frame offsets with a scalable component are not supported"); 753 FI.ChangeToImmediate(Offset.getFixed()); 754 return; 755 } 756 757 StackOffset Offset; 758 if (MI.getOpcode() == AArch64::TAGPstack) { 759 // TAGPstack must use the virtual frame register in its 3rd operand. 760 const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); 761 FrameReg = MI.getOperand(3).getReg(); 762 Offset = StackOffset::getFixed(MFI.getObjectOffset(FrameIndex) + 763 AFI->getTaggedBasePointerOffset()); 764 } else if (Tagged) { 765 StackOffset SPOffset = StackOffset::getFixed( 766 MFI.getObjectOffset(FrameIndex) + (int64_t)MFI.getStackSize()); 767 if (MFI.hasVarSizedObjects() || 768 isAArch64FrameOffsetLegal(MI, SPOffset, nullptr, nullptr, nullptr) != 769 (AArch64FrameOffsetCanUpdate | AArch64FrameOffsetIsLegal)) { 770 // Can't update to SP + offset in place. Precalculate the tagged pointer 771 // in a scratch register. 772 Offset = TFI->resolveFrameIndexReference( 773 MF, FrameIndex, FrameReg, /*PreferFP=*/false, /*ForSimm=*/true); 774 Register ScratchReg = 775 MF.getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass); 776 emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset, 777 TII); 778 BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(AArch64::LDG), ScratchReg) 779 .addReg(ScratchReg) 780 .addReg(ScratchReg) 781 .addImm(0); 782 MI.getOperand(FIOperandNum) 783 .ChangeToRegister(ScratchReg, false, false, true); 784 return; 785 } 786 FrameReg = AArch64::SP; 787 Offset = StackOffset::getFixed(MFI.getObjectOffset(FrameIndex) + 788 (int64_t)MFI.getStackSize()); 789 } else { 790 Offset = TFI->resolveFrameIndexReference( 791 MF, FrameIndex, FrameReg, /*PreferFP=*/false, /*ForSimm=*/true); 792 } 793 794 // Modify MI as necessary to handle as much of 'Offset' as possible 795 if (rewriteAArch64FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII)) 796 return; 797 798 assert((!RS || !RS->isScavengingFrameIndex(FrameIndex)) && 799 "Emergency spill slot is out of reach"); 800 801 // If we get here, the immediate doesn't fit into the instruction. We folded 802 // as much as possible above. Handle the rest, providing a register that is 803 // SP+LargeImm. 804 Register ScratchReg = 805 createScratchRegisterForInstruction(MI, FIOperandNum, TII); 806 emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset, TII); 807 } 808 809 unsigned AArch64RegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC, 810 MachineFunction &MF) const { 811 const AArch64FrameLowering *TFI = getFrameLowering(MF); 812 813 switch (RC->getID()) { 814 default: 815 return 0; 816 case AArch64::GPR32RegClassID: 817 case AArch64::GPR32spRegClassID: 818 case AArch64::GPR32allRegClassID: 819 case AArch64::GPR64spRegClassID: 820 case AArch64::GPR64allRegClassID: 821 case AArch64::GPR64RegClassID: 822 case AArch64::GPR32commonRegClassID: 823 case AArch64::GPR64commonRegClassID: 824 return 32 - 1 // XZR/SP 825 - (TFI->hasFP(MF) || TT.isOSDarwin()) // FP 826 - MF.getSubtarget<AArch64Subtarget>().getNumXRegisterReserved() 827 - hasBasePointer(MF); // X19 828 case AArch64::FPR8RegClassID: 829 case AArch64::FPR16RegClassID: 830 case AArch64::FPR32RegClassID: 831 case AArch64::FPR64RegClassID: 832 case AArch64::FPR128RegClassID: 833 return 32; 834 835 case AArch64::MatrixIndexGPR32_12_15RegClassID: 836 return 4; 837 838 case AArch64::DDRegClassID: 839 case AArch64::DDDRegClassID: 840 case AArch64::DDDDRegClassID: 841 case AArch64::QQRegClassID: 842 case AArch64::QQQRegClassID: 843 case AArch64::QQQQRegClassID: 844 return 32; 845 846 case AArch64::FPR128_loRegClassID: 847 case AArch64::FPR64_loRegClassID: 848 case AArch64::FPR16_loRegClassID: 849 return 16; 850 } 851 } 852 853 unsigned AArch64RegisterInfo::getLocalAddressRegister( 854 const MachineFunction &MF) const { 855 const auto &MFI = MF.getFrameInfo(); 856 if (!MF.hasEHFunclets() && !MFI.hasVarSizedObjects()) 857 return AArch64::SP; 858 else if (hasStackRealignment(MF)) 859 return getBaseRegister(); 860 return getFrameRegister(MF); 861 } 862 863 /// SrcRC and DstRC will be morphed into NewRC if this returns true 864 bool AArch64RegisterInfo::shouldCoalesce( 865 MachineInstr *MI, const TargetRegisterClass *SrcRC, unsigned SubReg, 866 const TargetRegisterClass *DstRC, unsigned DstSubReg, 867 const TargetRegisterClass *NewRC, LiveIntervals &LIS) const { 868 if (MI->isCopy() && 869 ((DstRC->getID() == AArch64::GPR64RegClassID) || 870 (DstRC->getID() == AArch64::GPR64commonRegClassID)) && 871 MI->getOperand(0).getSubReg() && MI->getOperand(1).getSubReg()) 872 // Do not coalesce in the case of a 32-bit subregister copy 873 // which implements a 32 to 64 bit zero extension 874 // which relies on the upper 32 bits being zeroed. 875 return false; 876 return true; 877 } 878