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