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