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