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 assert(checkAllSuperRegsMarked(Reserved)); 342 return Reserved; 343 } 344 345 bool AArch64RegisterInfo::isReservedReg(const MachineFunction &MF, 346 MCRegister Reg) const { 347 return getReservedRegs(MF)[Reg]; 348 } 349 350 bool AArch64RegisterInfo::isAnyArgRegReserved(const MachineFunction &MF) const { 351 return llvm::any_of(*AArch64::GPR64argRegClass.MC, [this, &MF](MCPhysReg r) { 352 return isReservedReg(MF, r); 353 }); 354 } 355 356 void AArch64RegisterInfo::emitReservedArgRegCallError( 357 const MachineFunction &MF) const { 358 const Function &F = MF.getFunction(); 359 F.getContext().diagnose(DiagnosticInfoUnsupported{F, ("AArch64 doesn't support" 360 " function calls if any of the argument registers is reserved.")}); 361 } 362 363 bool AArch64RegisterInfo::isAsmClobberable(const MachineFunction &MF, 364 MCRegister PhysReg) const { 365 return !isReservedReg(MF, PhysReg); 366 } 367 368 bool AArch64RegisterInfo::isConstantPhysReg(MCRegister PhysReg) const { 369 return PhysReg == AArch64::WZR || PhysReg == AArch64::XZR; 370 } 371 372 const TargetRegisterClass * 373 AArch64RegisterInfo::getPointerRegClass(const MachineFunction &MF, 374 unsigned Kind) const { 375 return &AArch64::GPR64spRegClass; 376 } 377 378 const TargetRegisterClass * 379 AArch64RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const { 380 if (RC == &AArch64::CCRRegClass) 381 return &AArch64::GPR64RegClass; // Only MSR & MRS copy NZCV. 382 return RC; 383 } 384 385 unsigned AArch64RegisterInfo::getBaseRegister() const { return AArch64::X19; } 386 387 bool AArch64RegisterInfo::hasBasePointer(const MachineFunction &MF) const { 388 const MachineFrameInfo &MFI = MF.getFrameInfo(); 389 390 // In the presence of variable sized objects or funclets, if the fixed stack 391 // size is large enough that referencing from the FP won't result in things 392 // being in range relatively often, we can use a base pointer to allow access 393 // from the other direction like the SP normally works. 394 // 395 // Furthermore, if both variable sized objects are present, and the 396 // stack needs to be dynamically re-aligned, the base pointer is the only 397 // reliable way to reference the locals. 398 if (MFI.hasVarSizedObjects() || MF.hasEHFunclets()) { 399 if (hasStackRealignment(MF)) 400 return true; 401 402 if (MF.getSubtarget<AArch64Subtarget>().hasSVE()) { 403 const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); 404 // Frames that have variable sized objects and scalable SVE objects, 405 // should always use a basepointer. 406 if (!AFI->hasCalculatedStackSizeSVE() || AFI->getStackSizeSVE()) 407 return true; 408 } 409 410 // Conservatively estimate whether the negative offset from the frame 411 // pointer will be sufficient to reach. If a function has a smallish 412 // frame, it's less likely to have lots of spills and callee saved 413 // space, so it's all more likely to be within range of the frame pointer. 414 // If it's wrong, we'll materialize the constant and still get to the 415 // object; it's just suboptimal. Negative offsets use the unscaled 416 // load/store instructions, which have a 9-bit signed immediate. 417 return MFI.getLocalFrameSize() >= 256; 418 } 419 420 return false; 421 } 422 423 bool AArch64RegisterInfo::isArgumentRegister(const MachineFunction &MF, 424 MCRegister Reg) const { 425 CallingConv::ID CC = MF.getFunction().getCallingConv(); 426 const AArch64Subtarget &STI = MF.getSubtarget<AArch64Subtarget>(); 427 bool IsVarArg = STI.isCallingConvWin64(MF.getFunction().getCallingConv()); 428 429 auto HasReg = [](ArrayRef<MCRegister> RegList, MCRegister Reg) { 430 return llvm::any_of(RegList, 431 [Reg](const MCRegister R) { return R == Reg; }); 432 }; 433 434 switch (CC) { 435 default: 436 report_fatal_error("Unsupported calling convention."); 437 case CallingConv::WebKit_JS: 438 return HasReg(CC_AArch64_WebKit_JS_ArgRegs, Reg); 439 case CallingConv::GHC: 440 return HasReg(CC_AArch64_GHC_ArgRegs, Reg); 441 case CallingConv::C: 442 case CallingConv::Fast: 443 case CallingConv::PreserveMost: 444 case CallingConv::CXX_FAST_TLS: 445 case CallingConv::Swift: 446 case CallingConv::SwiftTail: 447 case CallingConv::Tail: 448 if (STI.isTargetWindows() && IsVarArg) 449 return HasReg(CC_AArch64_Win64_VarArg_ArgRegs, Reg); 450 if (!STI.isTargetDarwin()) { 451 switch (CC) { 452 default: 453 return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg); 454 case CallingConv::Swift: 455 case CallingConv::SwiftTail: 456 return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg) || 457 HasReg(CC_AArch64_AAPCS_Swift_ArgRegs, Reg); 458 } 459 } 460 if (!IsVarArg) { 461 switch (CC) { 462 default: 463 return HasReg(CC_AArch64_DarwinPCS_ArgRegs, Reg); 464 case CallingConv::Swift: 465 case CallingConv::SwiftTail: 466 return HasReg(CC_AArch64_DarwinPCS_ArgRegs, Reg) || 467 HasReg(CC_AArch64_DarwinPCS_Swift_ArgRegs, Reg); 468 } 469 } 470 if (STI.isTargetILP32()) 471 return HasReg(CC_AArch64_DarwinPCS_ILP32_VarArg_ArgRegs, Reg); 472 return HasReg(CC_AArch64_DarwinPCS_VarArg_ArgRegs, Reg); 473 case CallingConv::Win64: 474 if (IsVarArg) 475 HasReg(CC_AArch64_Win64_VarArg_ArgRegs, Reg); 476 return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg); 477 case CallingConv::CFGuard_Check: 478 return HasReg(CC_AArch64_Win64_CFGuard_Check_ArgRegs, Reg); 479 case CallingConv::AArch64_VectorCall: 480 case CallingConv::AArch64_SVE_VectorCall: 481 return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg); 482 } 483 } 484 485 Register 486 AArch64RegisterInfo::getFrameRegister(const MachineFunction &MF) const { 487 const AArch64FrameLowering *TFI = getFrameLowering(MF); 488 return TFI->hasFP(MF) ? AArch64::FP : AArch64::SP; 489 } 490 491 bool AArch64RegisterInfo::requiresRegisterScavenging( 492 const MachineFunction &MF) const { 493 return true; 494 } 495 496 bool AArch64RegisterInfo::requiresVirtualBaseRegisters( 497 const MachineFunction &MF) const { 498 return true; 499 } 500 501 bool 502 AArch64RegisterInfo::useFPForScavengingIndex(const MachineFunction &MF) const { 503 // This function indicates whether the emergency spillslot should be placed 504 // close to the beginning of the stackframe (closer to FP) or the end 505 // (closer to SP). 506 // 507 // The beginning works most reliably if we have a frame pointer. 508 // In the presence of any non-constant space between FP and locals, 509 // (e.g. in case of stack realignment or a scalable SVE area), it is 510 // better to use SP or BP. 511 const AArch64FrameLowering &TFI = *getFrameLowering(MF); 512 const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); 513 assert((!MF.getSubtarget<AArch64Subtarget>().hasSVE() || 514 AFI->hasCalculatedStackSizeSVE()) && 515 "Expected SVE area to be calculated by this point"); 516 return TFI.hasFP(MF) && !hasStackRealignment(MF) && !AFI->getStackSizeSVE(); 517 } 518 519 bool AArch64RegisterInfo::requiresFrameIndexScavenging( 520 const MachineFunction &MF) const { 521 return true; 522 } 523 524 bool 525 AArch64RegisterInfo::cannotEliminateFrame(const MachineFunction &MF) const { 526 const MachineFrameInfo &MFI = MF.getFrameInfo(); 527 if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI.adjustsStack()) 528 return true; 529 return MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken(); 530 } 531 532 /// needsFrameBaseReg - Returns true if the instruction's frame index 533 /// reference would be better served by a base register other than FP 534 /// or SP. Used by LocalStackFrameAllocation to determine which frame index 535 /// references it should create new base registers for. 536 bool AArch64RegisterInfo::needsFrameBaseReg(MachineInstr *MI, 537 int64_t Offset) const { 538 for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) 539 assert(i < MI->getNumOperands() && 540 "Instr doesn't have FrameIndex operand!"); 541 542 // It's the load/store FI references that cause issues, as it can be difficult 543 // to materialize the offset if it won't fit in the literal field. Estimate 544 // based on the size of the local frame and some conservative assumptions 545 // about the rest of the stack frame (note, this is pre-regalloc, so 546 // we don't know everything for certain yet) whether this offset is likely 547 // to be out of range of the immediate. Return true if so. 548 549 // We only generate virtual base registers for loads and stores, so 550 // return false for everything else. 551 if (!MI->mayLoad() && !MI->mayStore()) 552 return false; 553 554 // Without a virtual base register, if the function has variable sized 555 // objects, all fixed-size local references will be via the frame pointer, 556 // Approximate the offset and see if it's legal for the instruction. 557 // Note that the incoming offset is based on the SP value at function entry, 558 // so it'll be negative. 559 MachineFunction &MF = *MI->getParent()->getParent(); 560 const AArch64FrameLowering *TFI = getFrameLowering(MF); 561 MachineFrameInfo &MFI = MF.getFrameInfo(); 562 563 // Estimate an offset from the frame pointer. 564 // Conservatively assume all GPR callee-saved registers get pushed. 565 // FP, LR, X19-X28, D8-D15. 64-bits each. 566 int64_t FPOffset = Offset - 16 * 20; 567 // Estimate an offset from the stack pointer. 568 // The incoming offset is relating to the SP at the start of the function, 569 // but when we access the local it'll be relative to the SP after local 570 // allocation, so adjust our SP-relative offset by that allocation size. 571 Offset += MFI.getLocalFrameSize(); 572 // Assume that we'll have at least some spill slots allocated. 573 // FIXME: This is a total SWAG number. We should run some statistics 574 // and pick a real one. 575 Offset += 128; // 128 bytes of spill slots 576 577 // If there is a frame pointer, try using it. 578 // The FP is only available if there is no dynamic realignment. We 579 // don't know for sure yet whether we'll need that, so we guess based 580 // on whether there are any local variables that would trigger it. 581 if (TFI->hasFP(MF) && isFrameOffsetLegal(MI, AArch64::FP, FPOffset)) 582 return false; 583 584 // If we can reference via the stack pointer or base pointer, try that. 585 // FIXME: This (and the code that resolves the references) can be improved 586 // to only disallow SP relative references in the live range of 587 // the VLA(s). In practice, it's unclear how much difference that 588 // would make, but it may be worth doing. 589 if (isFrameOffsetLegal(MI, AArch64::SP, Offset)) 590 return false; 591 592 // If even offset 0 is illegal, we don't want a virtual base register. 593 if (!isFrameOffsetLegal(MI, AArch64::SP, 0)) 594 return false; 595 596 // The offset likely isn't legal; we want to allocate a virtual base register. 597 return true; 598 } 599 600 bool AArch64RegisterInfo::isFrameOffsetLegal(const MachineInstr *MI, 601 Register BaseReg, 602 int64_t Offset) const { 603 assert(MI && "Unable to get the legal offset for nil instruction."); 604 StackOffset SaveOffset = StackOffset::getFixed(Offset); 605 return isAArch64FrameOffsetLegal(*MI, SaveOffset) & AArch64FrameOffsetIsLegal; 606 } 607 608 /// Insert defining instruction(s) for BaseReg to be a pointer to FrameIdx 609 /// at the beginning of the basic block. 610 Register 611 AArch64RegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB, 612 int FrameIdx, 613 int64_t Offset) const { 614 MachineBasicBlock::iterator Ins = MBB->begin(); 615 DebugLoc DL; // Defaults to "unknown" 616 if (Ins != MBB->end()) 617 DL = Ins->getDebugLoc(); 618 const MachineFunction &MF = *MBB->getParent(); 619 const AArch64InstrInfo *TII = 620 MF.getSubtarget<AArch64Subtarget>().getInstrInfo(); 621 const MCInstrDesc &MCID = TII->get(AArch64::ADDXri); 622 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 623 Register BaseReg = MRI.createVirtualRegister(&AArch64::GPR64spRegClass); 624 MRI.constrainRegClass(BaseReg, TII->getRegClass(MCID, 0, this, MF)); 625 unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0); 626 627 BuildMI(*MBB, Ins, DL, MCID, BaseReg) 628 .addFrameIndex(FrameIdx) 629 .addImm(Offset) 630 .addImm(Shifter); 631 632 return BaseReg; 633 } 634 635 void AArch64RegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg, 636 int64_t Offset) const { 637 // ARM doesn't need the general 64-bit offsets 638 StackOffset Off = StackOffset::getFixed(Offset); 639 640 unsigned i = 0; 641 while (!MI.getOperand(i).isFI()) { 642 ++i; 643 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!"); 644 } 645 646 const MachineFunction *MF = MI.getParent()->getParent(); 647 const AArch64InstrInfo *TII = 648 MF->getSubtarget<AArch64Subtarget>().getInstrInfo(); 649 bool Done = rewriteAArch64FrameIndex(MI, i, BaseReg, Off, TII); 650 assert(Done && "Unable to resolve frame index!"); 651 (void)Done; 652 } 653 654 // Create a scratch register for the frame index elimination in an instruction. 655 // This function has special handling of stack tagging loop pseudos, in which 656 // case it can also change the instruction opcode. 657 static Register 658 createScratchRegisterForInstruction(MachineInstr &MI, unsigned FIOperandNum, 659 const AArch64InstrInfo *TII) { 660 // ST*Gloop have a reserved scratch register in operand 1. Use it, and also 661 // replace the instruction with the writeback variant because it will now 662 // satisfy the operand constraints for it. 663 Register ScratchReg; 664 if (MI.getOpcode() == AArch64::STGloop || 665 MI.getOpcode() == AArch64::STZGloop) { 666 assert(FIOperandNum == 3 && 667 "Wrong frame index operand for STGloop/STZGloop"); 668 unsigned Op = MI.getOpcode() == AArch64::STGloop ? AArch64::STGloop_wback 669 : AArch64::STZGloop_wback; 670 ScratchReg = MI.getOperand(1).getReg(); 671 MI.getOperand(3).ChangeToRegister(ScratchReg, false, false, true); 672 MI.setDesc(TII->get(Op)); 673 MI.tieOperands(1, 3); 674 } else { 675 ScratchReg = 676 MI.getMF()->getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass); 677 MI.getOperand(FIOperandNum) 678 .ChangeToRegister(ScratchReg, false, false, true); 679 } 680 return ScratchReg; 681 } 682 683 void AArch64RegisterInfo::getOffsetOpcodes( 684 const StackOffset &Offset, SmallVectorImpl<uint64_t> &Ops) const { 685 // The smallest scalable element supported by scaled SVE addressing 686 // modes are predicates, which are 2 scalable bytes in size. So the scalable 687 // byte offset must always be a multiple of 2. 688 assert(Offset.getScalable() % 2 == 0 && "Invalid frame offset"); 689 690 // Add fixed-sized offset using existing DIExpression interface. 691 DIExpression::appendOffset(Ops, Offset.getFixed()); 692 693 unsigned VG = getDwarfRegNum(AArch64::VG, true); 694 int64_t VGSized = Offset.getScalable() / 2; 695 if (VGSized > 0) { 696 Ops.push_back(dwarf::DW_OP_constu); 697 Ops.push_back(VGSized); 698 Ops.append({dwarf::DW_OP_bregx, VG, 0ULL}); 699 Ops.push_back(dwarf::DW_OP_mul); 700 Ops.push_back(dwarf::DW_OP_plus); 701 } else if (VGSized < 0) { 702 Ops.push_back(dwarf::DW_OP_constu); 703 Ops.push_back(-VGSized); 704 Ops.append({dwarf::DW_OP_bregx, VG, 0ULL}); 705 Ops.push_back(dwarf::DW_OP_mul); 706 Ops.push_back(dwarf::DW_OP_minus); 707 } 708 } 709 710 void AArch64RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 711 int SPAdj, unsigned FIOperandNum, 712 RegScavenger *RS) const { 713 assert(SPAdj == 0 && "Unexpected"); 714 715 MachineInstr &MI = *II; 716 MachineBasicBlock &MBB = *MI.getParent(); 717 MachineFunction &MF = *MBB.getParent(); 718 const MachineFrameInfo &MFI = MF.getFrameInfo(); 719 const AArch64InstrInfo *TII = 720 MF.getSubtarget<AArch64Subtarget>().getInstrInfo(); 721 const AArch64FrameLowering *TFI = getFrameLowering(MF); 722 int FrameIndex = MI.getOperand(FIOperandNum).getIndex(); 723 bool Tagged = 724 MI.getOperand(FIOperandNum).getTargetFlags() & AArch64II::MO_TAGGED; 725 Register FrameReg; 726 727 // Special handling of dbg_value, stackmap patchpoint statepoint instructions. 728 if (MI.getOpcode() == TargetOpcode::STACKMAP || 729 MI.getOpcode() == TargetOpcode::PATCHPOINT || 730 MI.getOpcode() == TargetOpcode::STATEPOINT) { 731 StackOffset Offset = 732 TFI->resolveFrameIndexReference(MF, FrameIndex, FrameReg, 733 /*PreferFP=*/true, 734 /*ForSimm=*/false); 735 Offset += StackOffset::getFixed(MI.getOperand(FIOperandNum + 1).getImm()); 736 MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false /*isDef*/); 737 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset.getFixed()); 738 return; 739 } 740 741 if (MI.getOpcode() == TargetOpcode::LOCAL_ESCAPE) { 742 MachineOperand &FI = MI.getOperand(FIOperandNum); 743 StackOffset Offset = TFI->getNonLocalFrameIndexReference(MF, FrameIndex); 744 assert(!Offset.getScalable() && 745 "Frame offsets with a scalable component are not supported"); 746 FI.ChangeToImmediate(Offset.getFixed()); 747 return; 748 } 749 750 StackOffset Offset; 751 if (MI.getOpcode() == AArch64::TAGPstack) { 752 // TAGPstack must use the virtual frame register in its 3rd operand. 753 const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); 754 FrameReg = MI.getOperand(3).getReg(); 755 Offset = StackOffset::getFixed(MFI.getObjectOffset(FrameIndex) + 756 AFI->getTaggedBasePointerOffset()); 757 } else if (Tagged) { 758 StackOffset SPOffset = StackOffset::getFixed( 759 MFI.getObjectOffset(FrameIndex) + (int64_t)MFI.getStackSize()); 760 if (MFI.hasVarSizedObjects() || 761 isAArch64FrameOffsetLegal(MI, SPOffset, nullptr, nullptr, nullptr) != 762 (AArch64FrameOffsetCanUpdate | AArch64FrameOffsetIsLegal)) { 763 // Can't update to SP + offset in place. Precalculate the tagged pointer 764 // in a scratch register. 765 Offset = TFI->resolveFrameIndexReference( 766 MF, FrameIndex, FrameReg, /*PreferFP=*/false, /*ForSimm=*/true); 767 Register ScratchReg = 768 MF.getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass); 769 emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset, 770 TII); 771 BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(AArch64::LDG), ScratchReg) 772 .addReg(ScratchReg) 773 .addReg(ScratchReg) 774 .addImm(0); 775 MI.getOperand(FIOperandNum) 776 .ChangeToRegister(ScratchReg, false, false, true); 777 return; 778 } 779 FrameReg = AArch64::SP; 780 Offset = StackOffset::getFixed(MFI.getObjectOffset(FrameIndex) + 781 (int64_t)MFI.getStackSize()); 782 } else { 783 Offset = TFI->resolveFrameIndexReference( 784 MF, FrameIndex, FrameReg, /*PreferFP=*/false, /*ForSimm=*/true); 785 } 786 787 // Modify MI as necessary to handle as much of 'Offset' as possible 788 if (rewriteAArch64FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII)) 789 return; 790 791 assert((!RS || !RS->isScavengingFrameIndex(FrameIndex)) && 792 "Emergency spill slot is out of reach"); 793 794 // If we get here, the immediate doesn't fit into the instruction. We folded 795 // as much as possible above. Handle the rest, providing a register that is 796 // SP+LargeImm. 797 Register ScratchReg = 798 createScratchRegisterForInstruction(MI, FIOperandNum, TII); 799 emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset, TII); 800 } 801 802 unsigned AArch64RegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC, 803 MachineFunction &MF) const { 804 const AArch64FrameLowering *TFI = getFrameLowering(MF); 805 806 switch (RC->getID()) { 807 default: 808 return 0; 809 case AArch64::GPR32RegClassID: 810 case AArch64::GPR32spRegClassID: 811 case AArch64::GPR32allRegClassID: 812 case AArch64::GPR64spRegClassID: 813 case AArch64::GPR64allRegClassID: 814 case AArch64::GPR64RegClassID: 815 case AArch64::GPR32commonRegClassID: 816 case AArch64::GPR64commonRegClassID: 817 return 32 - 1 // XZR/SP 818 - (TFI->hasFP(MF) || TT.isOSDarwin()) // FP 819 - MF.getSubtarget<AArch64Subtarget>().getNumXRegisterReserved() 820 - hasBasePointer(MF); // X19 821 case AArch64::FPR8RegClassID: 822 case AArch64::FPR16RegClassID: 823 case AArch64::FPR32RegClassID: 824 case AArch64::FPR64RegClassID: 825 case AArch64::FPR128RegClassID: 826 return 32; 827 828 case AArch64::MatrixIndexGPR32_12_15RegClassID: 829 return 4; 830 831 case AArch64::DDRegClassID: 832 case AArch64::DDDRegClassID: 833 case AArch64::DDDDRegClassID: 834 case AArch64::QQRegClassID: 835 case AArch64::QQQRegClassID: 836 case AArch64::QQQQRegClassID: 837 return 32; 838 839 case AArch64::FPR128_loRegClassID: 840 case AArch64::FPR64_loRegClassID: 841 case AArch64::FPR16_loRegClassID: 842 return 16; 843 } 844 } 845 846 unsigned AArch64RegisterInfo::getLocalAddressRegister( 847 const MachineFunction &MF) const { 848 const auto &MFI = MF.getFrameInfo(); 849 if (!MF.hasEHFunclets() && !MFI.hasVarSizedObjects()) 850 return AArch64::SP; 851 else if (hasStackRealignment(MF)) 852 return getBaseRegister(); 853 return getFrameRegister(MF); 854 } 855 856 /// SrcRC and DstRC will be morphed into NewRC if this returns true 857 bool AArch64RegisterInfo::shouldCoalesce( 858 MachineInstr *MI, const TargetRegisterClass *SrcRC, unsigned SubReg, 859 const TargetRegisterClass *DstRC, unsigned DstSubReg, 860 const TargetRegisterClass *NewRC, LiveIntervals &LIS) const { 861 if (MI->isCopy() && 862 ((DstRC->getID() == AArch64::GPR64RegClassID) || 863 (DstRC->getID() == AArch64::GPR64commonRegClassID)) && 864 MI->getOperand(0).getSubReg() && MI->getOperand(1).getSubReg()) 865 // Do not coalesce in the case of a 32-bit subregister copy 866 // which implements a 32 to 64 bit zero extension 867 // which relies on the upper 32 bits being zeroed. 868 return false; 869 return true; 870 } 871