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 "AArch64StackOffset.h"
19 #include "AArch64Subtarget.h"
20 #include "MCTargetDesc/AArch64AddressingModes.h"
21 #include "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/Triple.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/DiagnosticInfo.h"
29 #include "llvm/IR/Function.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Target/TargetOptions.h"
32 
33 using namespace llvm;
34 
35 #define GET_REGINFO_TARGET_DESC
36 #include "AArch64GenRegisterInfo.inc"
37 
38 AArch64RegisterInfo::AArch64RegisterInfo(const Triple &TT)
39     : AArch64GenRegisterInfo(AArch64::LR), TT(TT) {
40   AArch64_MC::initLLVMToCVRegMapping(this);
41 }
42 
43 const MCPhysReg *
44 AArch64RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
45   assert(MF && "Invalid MachineFunction pointer.");
46   if (MF->getFunction().getCallingConv() == CallingConv::CFGuard_Check)
47     return CSR_Win_AArch64_CFGuard_Check_SaveList;
48   if (MF->getSubtarget<AArch64Subtarget>().isTargetWindows())
49     return CSR_Win_AArch64_AAPCS_SaveList;
50   if (MF->getFunction().getCallingConv() == CallingConv::GHC)
51     // GHC set of callee saved regs is empty as all those regs are
52     // used for passing STG regs around
53     return CSR_AArch64_NoRegs_SaveList;
54   if (MF->getFunction().getCallingConv() == CallingConv::AnyReg)
55     return CSR_AArch64_AllRegs_SaveList;
56   if (MF->getFunction().getCallingConv() == CallingConv::AArch64_VectorCall)
57     return CSR_AArch64_AAVPCS_SaveList;
58   if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS)
59     return MF->getInfo<AArch64FunctionInfo>()->isSplitCSR() ?
60            CSR_AArch64_CXX_TLS_Darwin_PE_SaveList :
61            CSR_AArch64_CXX_TLS_Darwin_SaveList;
62   if (MF->getSubtarget<AArch64Subtarget>().getTargetLowering()
63           ->supportSwiftError() &&
64       MF->getFunction().getAttributes().hasAttrSomewhere(
65           Attribute::SwiftError))
66     return CSR_AArch64_AAPCS_SwiftError_SaveList;
67   if (MF->getFunction().getCallingConv() == CallingConv::PreserveMost)
68     return CSR_AArch64_RT_MostRegs_SaveList;
69   if (MF->getSubtarget<AArch64Subtarget>().isTargetDarwin())
70     return CSR_Darwin_AArch64_AAPCS_SaveList;
71   return CSR_AArch64_AAPCS_SaveList;
72 }
73 
74 const MCPhysReg *AArch64RegisterInfo::getCalleeSavedRegsViaCopy(
75     const MachineFunction *MF) const {
76   assert(MF && "Invalid MachineFunction pointer.");
77   if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS &&
78       MF->getInfo<AArch64FunctionInfo>()->isSplitCSR())
79     return CSR_AArch64_CXX_TLS_Darwin_ViaCopy_SaveList;
80   return nullptr;
81 }
82 
83 void AArch64RegisterInfo::UpdateCustomCalleeSavedRegs(
84     MachineFunction &MF) const {
85   const MCPhysReg *CSRs = getCalleeSavedRegs(&MF);
86   SmallVector<MCPhysReg, 32> UpdatedCSRs;
87   for (const MCPhysReg *I = CSRs; *I; ++I)
88     UpdatedCSRs.push_back(*I);
89 
90   for (size_t i = 0; i < AArch64::GPR64commonRegClass.getNumRegs(); ++i) {
91     if (MF.getSubtarget<AArch64Subtarget>().isXRegCustomCalleeSaved(i)) {
92       UpdatedCSRs.push_back(AArch64::GPR64commonRegClass.getRegister(i));
93     }
94   }
95   // Register lists are zero-terminated.
96   UpdatedCSRs.push_back(0);
97   MF.getRegInfo().setCalleeSavedRegs(UpdatedCSRs);
98 }
99 
100 const TargetRegisterClass *
101 AArch64RegisterInfo::getSubClassWithSubReg(const TargetRegisterClass *RC,
102                                        unsigned Idx) const {
103   // edge case for GPR/FPR register classes
104   if (RC == &AArch64::GPR32allRegClass && Idx == AArch64::hsub)
105     return &AArch64::FPR32RegClass;
106   else if (RC == &AArch64::GPR64allRegClass && Idx == AArch64::hsub)
107     return &AArch64::FPR64RegClass;
108 
109   // Forward to TableGen's default version.
110   return AArch64GenRegisterInfo::getSubClassWithSubReg(RC, Idx);
111 }
112 
113 const uint32_t *
114 AArch64RegisterInfo::getCallPreservedMask(const MachineFunction &MF,
115                                           CallingConv::ID CC) const {
116   bool SCS = MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack);
117   if (CC == CallingConv::GHC)
118     // This is academic because all GHC calls are (supposed to be) tail calls
119     return SCS ? CSR_AArch64_NoRegs_SCS_RegMask : CSR_AArch64_NoRegs_RegMask;
120   if (CC == CallingConv::AnyReg)
121     return SCS ? CSR_AArch64_AllRegs_SCS_RegMask : CSR_AArch64_AllRegs_RegMask;
122   if (CC == CallingConv::CXX_FAST_TLS)
123     return SCS ? CSR_AArch64_CXX_TLS_Darwin_SCS_RegMask
124                : CSR_AArch64_CXX_TLS_Darwin_RegMask;
125   if (CC == CallingConv::AArch64_VectorCall)
126     return SCS ? CSR_AArch64_AAVPCS_SCS_RegMask : CSR_AArch64_AAVPCS_RegMask;
127   if (CC == CallingConv::AArch64_SVE_VectorCall)
128     return CSR_AArch64_SVE_AAPCS_RegMask;
129   if (CC == CallingConv::CFGuard_Check)
130     return CSR_Win_AArch64_CFGuard_Check_RegMask;
131   if (MF.getSubtarget<AArch64Subtarget>().getTargetLowering()
132           ->supportSwiftError() &&
133       MF.getFunction().getAttributes().hasAttrSomewhere(Attribute::SwiftError))
134     return SCS ? CSR_AArch64_AAPCS_SwiftError_SCS_RegMask
135                : CSR_AArch64_AAPCS_SwiftError_RegMask;
136   if (CC == CallingConv::PreserveMost)
137     return SCS ? CSR_AArch64_RT_MostRegs_SCS_RegMask
138                : CSR_AArch64_RT_MostRegs_RegMask;
139   else
140     return SCS ? CSR_AArch64_AAPCS_SCS_RegMask : CSR_AArch64_AAPCS_RegMask;
141 }
142 
143 const uint32_t *AArch64RegisterInfo::getTLSCallPreservedMask() const {
144   if (TT.isOSDarwin())
145     return CSR_AArch64_TLS_Darwin_RegMask;
146 
147   assert(TT.isOSBinFormatELF() && "Invalid target");
148   return CSR_AArch64_TLS_ELF_RegMask;
149 }
150 
151 void AArch64RegisterInfo::UpdateCustomCallPreservedMask(MachineFunction &MF,
152                                                  const uint32_t **Mask) const {
153   uint32_t *UpdatedMask = MF.allocateRegMask();
154   unsigned RegMaskSize = MachineOperand::getRegMaskSize(getNumRegs());
155   memcpy(UpdatedMask, *Mask, sizeof(UpdatedMask[0]) * RegMaskSize);
156 
157   for (size_t i = 0; i < AArch64::GPR64commonRegClass.getNumRegs(); ++i) {
158     if (MF.getSubtarget<AArch64Subtarget>().isXRegCustomCalleeSaved(i)) {
159       for (MCSubRegIterator SubReg(AArch64::GPR64commonRegClass.getRegister(i),
160                                    this, true);
161            SubReg.isValid(); ++SubReg) {
162         // See TargetRegisterInfo::getCallPreservedMask for how to interpret the
163         // register mask.
164         UpdatedMask[*SubReg / 32] |= 1u << (*SubReg % 32);
165       }
166     }
167   }
168   *Mask = UpdatedMask;
169 }
170 
171 const uint32_t *AArch64RegisterInfo::getNoPreservedMask() const {
172   return CSR_AArch64_NoRegs_RegMask;
173 }
174 
175 const uint32_t *
176 AArch64RegisterInfo::getThisReturnPreservedMask(const MachineFunction &MF,
177                                                 CallingConv::ID CC) const {
178   // This should return a register mask that is the same as that returned by
179   // getCallPreservedMask but that additionally preserves the register used for
180   // the first i64 argument (which must also be the register used to return a
181   // single i64 return value)
182   //
183   // In case that the calling convention does not use the same register for
184   // both, the function should return NULL (does not currently apply)
185   assert(CC != CallingConv::GHC && "should not be GHC calling convention.");
186   return CSR_AArch64_AAPCS_ThisReturn_RegMask;
187 }
188 
189 const uint32_t *AArch64RegisterInfo::getWindowsStackProbePreservedMask() const {
190   return CSR_AArch64_StackProbe_Windows_RegMask;
191 }
192 
193 BitVector
194 AArch64RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
195   const AArch64FrameLowering *TFI = getFrameLowering(MF);
196 
197   // FIXME: avoid re-calculating this every time.
198   BitVector Reserved(getNumRegs());
199   markSuperRegs(Reserved, AArch64::WSP);
200   markSuperRegs(Reserved, AArch64::WZR);
201 
202   if (TFI->hasFP(MF) || TT.isOSDarwin())
203     markSuperRegs(Reserved, AArch64::W29);
204 
205   for (size_t i = 0; i < AArch64::GPR32commonRegClass.getNumRegs(); ++i) {
206     if (MF.getSubtarget<AArch64Subtarget>().isXRegisterReserved(i))
207       markSuperRegs(Reserved, AArch64::GPR32commonRegClass.getRegister(i));
208   }
209 
210   if (hasBasePointer(MF))
211     markSuperRegs(Reserved, AArch64::W19);
212 
213   // SLH uses register W16/X16 as the taint register.
214   if (MF.getFunction().hasFnAttribute(Attribute::SpeculativeLoadHardening))
215     markSuperRegs(Reserved, AArch64::W16);
216 
217   assert(checkAllSuperRegsMarked(Reserved));
218   return Reserved;
219 }
220 
221 bool AArch64RegisterInfo::isReservedReg(const MachineFunction &MF,
222                                       unsigned Reg) const {
223   return getReservedRegs(MF)[Reg];
224 }
225 
226 bool AArch64RegisterInfo::isAnyArgRegReserved(const MachineFunction &MF) const {
227   return std::any_of(std::begin(*AArch64::GPR64argRegClass.MC),
228                      std::end(*AArch64::GPR64argRegClass.MC),
229                      [this, &MF](MCPhysReg r){return isReservedReg(MF, r);});
230 }
231 
232 void AArch64RegisterInfo::emitReservedArgRegCallError(
233     const MachineFunction &MF) const {
234   const Function &F = MF.getFunction();
235   F.getContext().diagnose(DiagnosticInfoUnsupported{F, "AArch64 doesn't support"
236     " function calls if any of the argument registers is reserved."});
237 }
238 
239 bool AArch64RegisterInfo::isAsmClobberable(const MachineFunction &MF,
240                                           unsigned PhysReg) const {
241   return !isReservedReg(MF, PhysReg);
242 }
243 
244 bool AArch64RegisterInfo::isConstantPhysReg(unsigned PhysReg) const {
245   return PhysReg == AArch64::WZR || PhysReg == AArch64::XZR;
246 }
247 
248 const TargetRegisterClass *
249 AArch64RegisterInfo::getPointerRegClass(const MachineFunction &MF,
250                                       unsigned Kind) const {
251   return &AArch64::GPR64spRegClass;
252 }
253 
254 const TargetRegisterClass *
255 AArch64RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
256   if (RC == &AArch64::CCRRegClass)
257     return &AArch64::GPR64RegClass; // Only MSR & MRS copy NZCV.
258   return RC;
259 }
260 
261 unsigned AArch64RegisterInfo::getBaseRegister() const { return AArch64::X19; }
262 
263 bool AArch64RegisterInfo::hasBasePointer(const MachineFunction &MF) const {
264   const MachineFrameInfo &MFI = MF.getFrameInfo();
265 
266   // In the presence of variable sized objects or funclets, if the fixed stack
267   // size is large enough that referencing from the FP won't result in things
268   // being in range relatively often, we can use a base pointer to allow access
269   // from the other direction like the SP normally works.
270   //
271   // Furthermore, if both variable sized objects are present, and the
272   // stack needs to be dynamically re-aligned, the base pointer is the only
273   // reliable way to reference the locals.
274   if (MFI.hasVarSizedObjects() || MF.hasEHFunclets()) {
275     if (needsStackRealignment(MF))
276       return true;
277     // Conservatively estimate whether the negative offset from the frame
278     // pointer will be sufficient to reach. If a function has a smallish
279     // frame, it's less likely to have lots of spills and callee saved
280     // space, so it's all more likely to be within range of the frame pointer.
281     // If it's wrong, we'll materialize the constant and still get to the
282     // object; it's just suboptimal. Negative offsets use the unscaled
283     // load/store instructions, which have a 9-bit signed immediate.
284     return MFI.getLocalFrameSize() >= 256;
285   }
286 
287   return false;
288 }
289 
290 Register
291 AArch64RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
292   const AArch64FrameLowering *TFI = getFrameLowering(MF);
293   return TFI->hasFP(MF) ? AArch64::FP : AArch64::SP;
294 }
295 
296 bool AArch64RegisterInfo::requiresRegisterScavenging(
297     const MachineFunction &MF) const {
298   return true;
299 }
300 
301 bool AArch64RegisterInfo::requiresVirtualBaseRegisters(
302     const MachineFunction &MF) const {
303   return true;
304 }
305 
306 bool
307 AArch64RegisterInfo::useFPForScavengingIndex(const MachineFunction &MF) const {
308   // This function indicates whether the emergency spillslot should be placed
309   // close to the beginning of the stackframe (closer to FP) or the end
310   // (closer to SP).
311   //
312   // The beginning works most reliably if we have a frame pointer.
313   const AArch64FrameLowering &TFI = *getFrameLowering(MF);
314   return TFI.hasFP(MF);
315 }
316 
317 bool AArch64RegisterInfo::requiresFrameIndexScavenging(
318     const MachineFunction &MF) const {
319   return true;
320 }
321 
322 bool
323 AArch64RegisterInfo::cannotEliminateFrame(const MachineFunction &MF) const {
324   const MachineFrameInfo &MFI = MF.getFrameInfo();
325   if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI.adjustsStack())
326     return true;
327   return MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken();
328 }
329 
330 /// needsFrameBaseReg - Returns true if the instruction's frame index
331 /// reference would be better served by a base register other than FP
332 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
333 /// references it should create new base registers for.
334 bool AArch64RegisterInfo::needsFrameBaseReg(MachineInstr *MI,
335                                             int64_t Offset) const {
336   for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i)
337     assert(i < MI->getNumOperands() &&
338            "Instr doesn't have FrameIndex operand!");
339 
340   // It's the load/store FI references that cause issues, as it can be difficult
341   // to materialize the offset if it won't fit in the literal field. Estimate
342   // based on the size of the local frame and some conservative assumptions
343   // about the rest of the stack frame (note, this is pre-regalloc, so
344   // we don't know everything for certain yet) whether this offset is likely
345   // to be out of range of the immediate. Return true if so.
346 
347   // We only generate virtual base registers for loads and stores, so
348   // return false for everything else.
349   if (!MI->mayLoad() && !MI->mayStore())
350     return false;
351 
352   // Without a virtual base register, if the function has variable sized
353   // objects, all fixed-size local references will be via the frame pointer,
354   // Approximate the offset and see if it's legal for the instruction.
355   // Note that the incoming offset is based on the SP value at function entry,
356   // so it'll be negative.
357   MachineFunction &MF = *MI->getParent()->getParent();
358   const AArch64FrameLowering *TFI = getFrameLowering(MF);
359   MachineFrameInfo &MFI = MF.getFrameInfo();
360 
361   // Estimate an offset from the frame pointer.
362   // Conservatively assume all GPR callee-saved registers get pushed.
363   // FP, LR, X19-X28, D8-D15. 64-bits each.
364   int64_t FPOffset = Offset - 16 * 20;
365   // Estimate an offset from the stack pointer.
366   // The incoming offset is relating to the SP at the start of the function,
367   // but when we access the local it'll be relative to the SP after local
368   // allocation, so adjust our SP-relative offset by that allocation size.
369   Offset += MFI.getLocalFrameSize();
370   // Assume that we'll have at least some spill slots allocated.
371   // FIXME: This is a total SWAG number. We should run some statistics
372   //        and pick a real one.
373   Offset += 128; // 128 bytes of spill slots
374 
375   // If there is a frame pointer, try using it.
376   // The FP is only available if there is no dynamic realignment. We
377   // don't know for sure yet whether we'll need that, so we guess based
378   // on whether there are any local variables that would trigger it.
379   if (TFI->hasFP(MF) && isFrameOffsetLegal(MI, AArch64::FP, FPOffset))
380     return false;
381 
382   // If we can reference via the stack pointer or base pointer, try that.
383   // FIXME: This (and the code that resolves the references) can be improved
384   //        to only disallow SP relative references in the live range of
385   //        the VLA(s). In practice, it's unclear how much difference that
386   //        would make, but it may be worth doing.
387   if (isFrameOffsetLegal(MI, AArch64::SP, Offset))
388     return false;
389 
390   // The offset likely isn't legal; we want to allocate a virtual base register.
391   return true;
392 }
393 
394 bool AArch64RegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
395                                              unsigned BaseReg,
396                                              int64_t Offset) const {
397   assert(Offset <= INT_MAX && "Offset too big to fit in int.");
398   assert(MI && "Unable to get the legal offset for nil instruction.");
399   StackOffset SaveOffset(Offset, MVT::i8);
400   return isAArch64FrameOffsetLegal(*MI, SaveOffset) & AArch64FrameOffsetIsLegal;
401 }
402 
403 /// Insert defining instruction(s) for BaseReg to be a pointer to FrameIdx
404 /// at the beginning of the basic block.
405 void AArch64RegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB,
406                                                        unsigned BaseReg,
407                                                        int FrameIdx,
408                                                        int64_t Offset) const {
409   MachineBasicBlock::iterator Ins = MBB->begin();
410   DebugLoc DL; // Defaults to "unknown"
411   if (Ins != MBB->end())
412     DL = Ins->getDebugLoc();
413   const MachineFunction &MF = *MBB->getParent();
414   const AArch64InstrInfo *TII =
415       MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
416   const MCInstrDesc &MCID = TII->get(AArch64::ADDXri);
417   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
418   MRI.constrainRegClass(BaseReg, TII->getRegClass(MCID, 0, this, MF));
419   unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0);
420 
421   BuildMI(*MBB, Ins, DL, MCID, BaseReg)
422       .addFrameIndex(FrameIdx)
423       .addImm(Offset)
424       .addImm(Shifter);
425 }
426 
427 void AArch64RegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg,
428                                             int64_t Offset) const {
429   // ARM doesn't need the general 64-bit offsets
430   StackOffset Off(Offset, MVT::i8);
431 
432   unsigned i = 0;
433 
434   while (!MI.getOperand(i).isFI()) {
435     ++i;
436     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
437   }
438   const MachineFunction *MF = MI.getParent()->getParent();
439   const AArch64InstrInfo *TII =
440       MF->getSubtarget<AArch64Subtarget>().getInstrInfo();
441   bool Done = rewriteAArch64FrameIndex(MI, i, BaseReg, Off, TII);
442   assert(Done && "Unable to resolve frame index!");
443   (void)Done;
444 }
445 
446 void AArch64RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
447                                               int SPAdj, unsigned FIOperandNum,
448                                               RegScavenger *RS) const {
449   assert(SPAdj == 0 && "Unexpected");
450 
451   MachineInstr &MI = *II;
452   MachineBasicBlock &MBB = *MI.getParent();
453   MachineFunction &MF = *MBB.getParent();
454   const MachineFrameInfo &MFI = MF.getFrameInfo();
455   const AArch64InstrInfo *TII =
456       MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
457   const AArch64FrameLowering *TFI = getFrameLowering(MF);
458 
459   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
460   bool Tagged =
461       MI.getOperand(FIOperandNum).getTargetFlags() & AArch64II::MO_TAGGED;
462   unsigned FrameReg;
463 
464   // Special handling of dbg_value, stackmap and patchpoint instructions.
465   if (MI.isDebugValue() || MI.getOpcode() == TargetOpcode::STACKMAP ||
466       MI.getOpcode() == TargetOpcode::PATCHPOINT) {
467     StackOffset Offset =
468         TFI->resolveFrameIndexReference(MF, FrameIndex, FrameReg,
469                                         /*PreferFP=*/true,
470                                         /*ForSimm=*/false);
471     Offset += StackOffset(MI.getOperand(FIOperandNum + 1).getImm(), MVT::i8);
472     MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false /*isDef*/);
473     MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset.getBytes());
474     return;
475   }
476 
477   if (MI.getOpcode() == TargetOpcode::LOCAL_ESCAPE) {
478     MachineOperand &FI = MI.getOperand(FIOperandNum);
479     int Offset = TFI->getNonLocalFrameIndexReference(MF, FrameIndex);
480     FI.ChangeToImmediate(Offset);
481     return;
482   }
483 
484   StackOffset Offset;
485   if (MI.getOpcode() == AArch64::TAGPstack) {
486     // TAGPstack must use the virtual frame register in its 3rd operand.
487     const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
488     FrameReg = MI.getOperand(3).getReg();
489     Offset = {MFI.getObjectOffset(FrameIndex) +
490                   AFI->getTaggedBasePointerOffset(),
491               MVT::i8};
492   } else if (Tagged) {
493     StackOffset SPOffset = {
494         MFI.getObjectOffset(FrameIndex) + (int64_t)MFI.getStackSize(), MVT::i8};
495     if (MFI.hasVarSizedObjects() ||
496         isAArch64FrameOffsetLegal(MI, SPOffset, nullptr, nullptr, nullptr) !=
497             (AArch64FrameOffsetCanUpdate | AArch64FrameOffsetIsLegal)) {
498       // Can't update to SP + offset in place. Precalculate the tagged pointer
499       // in a scratch register.
500       Offset = TFI->resolveFrameIndexReference(
501           MF, FrameIndex, FrameReg, /*PreferFP=*/false, /*ForSimm=*/true);
502       Register ScratchReg =
503           MF.getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass);
504       emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset,
505                       TII);
506       BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(AArch64::LDG), ScratchReg)
507           .addReg(ScratchReg)
508           .addReg(ScratchReg)
509           .addImm(0);
510       MI.getOperand(FIOperandNum)
511           .ChangeToRegister(ScratchReg, false, false, true);
512       return;
513     }
514     FrameReg = AArch64::SP;
515     Offset = {MFI.getObjectOffset(FrameIndex) + (int64_t)MFI.getStackSize(),
516               MVT::i8};
517   } else {
518     Offset = TFI->resolveFrameIndexReference(
519         MF, FrameIndex, FrameReg, /*PreferFP=*/false, /*ForSimm=*/true);
520   }
521 
522   // Modify MI as necessary to handle as much of 'Offset' as possible
523   if (rewriteAArch64FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII))
524     return;
525 
526   assert((!RS || !RS->isScavengingFrameIndex(FrameIndex)) &&
527          "Emergency spill slot is out of reach");
528 
529   // If we get here, the immediate doesn't fit into the instruction.  We folded
530   // as much as possible above.  Handle the rest, providing a register that is
531   // SP+LargeImm.
532   Register ScratchReg =
533       MF.getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass);
534   emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset, TII);
535   MI.getOperand(FIOperandNum).ChangeToRegister(ScratchReg, false, false, true);
536 }
537 
538 unsigned AArch64RegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
539                                                   MachineFunction &MF) const {
540   const AArch64FrameLowering *TFI = getFrameLowering(MF);
541 
542   switch (RC->getID()) {
543   default:
544     return 0;
545   case AArch64::GPR32RegClassID:
546   case AArch64::GPR32spRegClassID:
547   case AArch64::GPR32allRegClassID:
548   case AArch64::GPR64spRegClassID:
549   case AArch64::GPR64allRegClassID:
550   case AArch64::GPR64RegClassID:
551   case AArch64::GPR32commonRegClassID:
552   case AArch64::GPR64commonRegClassID:
553     return 32 - 1                                   // XZR/SP
554               - (TFI->hasFP(MF) || TT.isOSDarwin()) // FP
555               - MF.getSubtarget<AArch64Subtarget>().getNumXRegisterReserved()
556               - hasBasePointer(MF);  // X19
557   case AArch64::FPR8RegClassID:
558   case AArch64::FPR16RegClassID:
559   case AArch64::FPR32RegClassID:
560   case AArch64::FPR64RegClassID:
561   case AArch64::FPR128RegClassID:
562     return 32;
563 
564   case AArch64::DDRegClassID:
565   case AArch64::DDDRegClassID:
566   case AArch64::DDDDRegClassID:
567   case AArch64::QQRegClassID:
568   case AArch64::QQQRegClassID:
569   case AArch64::QQQQRegClassID:
570     return 32;
571 
572   case AArch64::FPR128_loRegClassID:
573     return 16;
574   }
575 }
576 
577 unsigned AArch64RegisterInfo::getLocalAddressRegister(
578   const MachineFunction &MF) const {
579   const auto &MFI = MF.getFrameInfo();
580   if (!MF.hasEHFunclets() && !MFI.hasVarSizedObjects())
581     return AArch64::SP;
582   else if (needsStackRealignment(MF))
583     return getBaseRegister();
584   return getFrameRegister(MF);
585 }
586