1 //===- AArch64RegisterInfo.cpp - AArch64 Register Information -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the AArch64 implementation of the TargetRegisterInfo
11 // class.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "AArch64RegisterInfo.h"
16 #include "AArch64FrameLowering.h"
17 #include "AArch64InstrInfo.h"
18 #include "AArch64MachineFunctionInfo.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/IR/Function.h"
28 #include "llvm/IR/DiagnosticInfo.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/CodeGen/TargetFrameLowering.h"
31 #include "llvm/Target/TargetOptions.h"
32 
33 using namespace llvm;
34 
35 #define GET_REGINFO_TARGET_DESC
36 #include "AArch64GenRegisterInfo.inc"
37 
AArch64RegisterInfo(const Triple & TT)38 AArch64RegisterInfo::AArch64RegisterInfo(const Triple &TT)
39     : AArch64GenRegisterInfo(AArch64::LR), TT(TT) {
40   AArch64_MC::initLLVMToCVRegMapping(this);
41 }
42 
43 const MCPhysReg *
getCalleeSavedRegs(const MachineFunction * MF) const44 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 
getCalleeSavedRegsViaCopy(const MachineFunction * MF) const71 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 
UpdateCustomCalleeSavedRegs(MachineFunction & MF) const80 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 *
getSubClassWithSubReg(const TargetRegisterClass * RC,unsigned Idx) const98 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 *
getCallPreservedMask(const MachineFunction & MF,CallingConv::ID CC) const111 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 (MF.getSubtarget<AArch64Subtarget>().getTargetLowering()
125           ->supportSwiftError() &&
126       MF.getFunction().getAttributes().hasAttrSomewhere(Attribute::SwiftError))
127     return SCS ? CSR_AArch64_AAPCS_SwiftError_SCS_RegMask
128                : CSR_AArch64_AAPCS_SwiftError_RegMask;
129   if (CC == CallingConv::PreserveMost)
130     return SCS ? CSR_AArch64_RT_MostRegs_SCS_RegMask
131                : CSR_AArch64_RT_MostRegs_RegMask;
132   else
133     return SCS ? CSR_AArch64_AAPCS_SCS_RegMask : CSR_AArch64_AAPCS_RegMask;
134 }
135 
getTLSCallPreservedMask() const136 const uint32_t *AArch64RegisterInfo::getTLSCallPreservedMask() const {
137   if (TT.isOSDarwin())
138     return CSR_AArch64_TLS_Darwin_RegMask;
139 
140   assert(TT.isOSBinFormatELF() && "Invalid target");
141   return CSR_AArch64_TLS_ELF_RegMask;
142 }
143 
UpdateCustomCallPreservedMask(MachineFunction & MF,const uint32_t ** Mask) const144 void AArch64RegisterInfo::UpdateCustomCallPreservedMask(MachineFunction &MF,
145                                                  const uint32_t **Mask) const {
146   uint32_t *UpdatedMask = MF.allocateRegMask();
147   unsigned RegMaskSize = MachineOperand::getRegMaskSize(getNumRegs());
148   memcpy(UpdatedMask, *Mask, sizeof(UpdatedMask[0]) * RegMaskSize);
149 
150   for (size_t i = 0; i < AArch64::GPR64commonRegClass.getNumRegs(); ++i) {
151     if (MF.getSubtarget<AArch64Subtarget>().isXRegCustomCalleeSaved(i)) {
152       for (MCSubRegIterator SubReg(AArch64::GPR64commonRegClass.getRegister(i),
153                                    this, true);
154            SubReg.isValid(); ++SubReg) {
155         // See TargetRegisterInfo::getCallPreservedMask for how to interpret the
156         // register mask.
157         UpdatedMask[*SubReg / 32] |= 1u << (*SubReg % 32);
158       }
159     }
160   }
161   *Mask = UpdatedMask;
162 }
163 
getNoPreservedMask() const164 const uint32_t *AArch64RegisterInfo::getNoPreservedMask() const {
165   return CSR_AArch64_NoRegs_RegMask;
166 }
167 
168 const uint32_t *
getThisReturnPreservedMask(const MachineFunction & MF,CallingConv::ID CC) const169 AArch64RegisterInfo::getThisReturnPreservedMask(const MachineFunction &MF,
170                                                 CallingConv::ID CC) const {
171   // This should return a register mask that is the same as that returned by
172   // getCallPreservedMask but that additionally preserves the register used for
173   // the first i64 argument (which must also be the register used to return a
174   // single i64 return value)
175   //
176   // In case that the calling convention does not use the same register for
177   // both, the function should return NULL (does not currently apply)
178   assert(CC != CallingConv::GHC && "should not be GHC calling convention.");
179   return CSR_AArch64_AAPCS_ThisReturn_RegMask;
180 }
181 
getWindowsStackProbePreservedMask() const182 const uint32_t *AArch64RegisterInfo::getWindowsStackProbePreservedMask() const {
183   return CSR_AArch64_StackProbe_Windows_RegMask;
184 }
185 
186 BitVector
getReservedRegs(const MachineFunction & MF) const187 AArch64RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
188   const AArch64FrameLowering *TFI = getFrameLowering(MF);
189 
190   // FIXME: avoid re-calculating this every time.
191   BitVector Reserved(getNumRegs());
192   markSuperRegs(Reserved, AArch64::WSP);
193   markSuperRegs(Reserved, AArch64::WZR);
194 
195   if (TFI->hasFP(MF) || TT.isOSDarwin())
196     markSuperRegs(Reserved, AArch64::W29);
197 
198   for (size_t i = 0; i < AArch64::GPR32commonRegClass.getNumRegs(); ++i) {
199     if (MF.getSubtarget<AArch64Subtarget>().isXRegisterReserved(i))
200       markSuperRegs(Reserved, AArch64::GPR32commonRegClass.getRegister(i));
201   }
202 
203   if (hasBasePointer(MF))
204     markSuperRegs(Reserved, AArch64::W19);
205 
206   // SLH uses register W16/X16 as the taint register.
207   if (MF.getFunction().hasFnAttribute(Attribute::SpeculativeLoadHardening))
208     markSuperRegs(Reserved, AArch64::W16);
209 
210   assert(checkAllSuperRegsMarked(Reserved));
211   return Reserved;
212 }
213 
isReservedReg(const MachineFunction & MF,unsigned Reg) const214 bool AArch64RegisterInfo::isReservedReg(const MachineFunction &MF,
215                                       unsigned Reg) const {
216   return getReservedRegs(MF)[Reg];
217 }
218 
isAnyArgRegReserved(const MachineFunction & MF) const219 bool AArch64RegisterInfo::isAnyArgRegReserved(const MachineFunction &MF) const {
220   // FIXME: Get the list of argument registers from TableGen.
221   static const MCPhysReg GPRArgRegs[] = { AArch64::X0, AArch64::X1, AArch64::X2,
222                                           AArch64::X3, AArch64::X4, AArch64::X5,
223                                           AArch64::X6, AArch64::X7 };
224   return std::any_of(std::begin(GPRArgRegs), std::end(GPRArgRegs),
225                      [this, &MF](MCPhysReg r){return isReservedReg(MF, r);});
226 }
227 
emitReservedArgRegCallError(const MachineFunction & MF) const228 void AArch64RegisterInfo::emitReservedArgRegCallError(
229     const MachineFunction &MF) const {
230   const Function &F = MF.getFunction();
231   F.getContext().diagnose(DiagnosticInfoUnsupported{F, "AArch64 doesn't support"
232     " function calls if any of the argument registers is reserved."});
233 }
234 
isAsmClobberable(const MachineFunction & MF,unsigned PhysReg) const235 bool AArch64RegisterInfo::isAsmClobberable(const MachineFunction &MF,
236                                           unsigned PhysReg) const {
237   return !isReservedReg(MF, PhysReg);
238 }
239 
isConstantPhysReg(unsigned PhysReg) const240 bool AArch64RegisterInfo::isConstantPhysReg(unsigned PhysReg) const {
241   return PhysReg == AArch64::WZR || PhysReg == AArch64::XZR;
242 }
243 
244 const TargetRegisterClass *
getPointerRegClass(const MachineFunction & MF,unsigned Kind) const245 AArch64RegisterInfo::getPointerRegClass(const MachineFunction &MF,
246                                       unsigned Kind) const {
247   return &AArch64::GPR64spRegClass;
248 }
249 
250 const TargetRegisterClass *
getCrossCopyRegClass(const TargetRegisterClass * RC) const251 AArch64RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
252   if (RC == &AArch64::CCRRegClass)
253     return &AArch64::GPR64RegClass; // Only MSR & MRS copy NZCV.
254   return RC;
255 }
256 
getBaseRegister() const257 unsigned AArch64RegisterInfo::getBaseRegister() const { return AArch64::X19; }
258 
hasBasePointer(const MachineFunction & MF) const259 bool AArch64RegisterInfo::hasBasePointer(const MachineFunction &MF) const {
260   const MachineFrameInfo &MFI = MF.getFrameInfo();
261 
262   // In the presence of variable sized objects or funclets, if the fixed stack
263   // size is large enough that referencing from the FP won't result in things
264   // being in range relatively often, we can use a base pointer to allow access
265   // from the other direction like the SP normally works.
266   //
267   // Furthermore, if both variable sized objects are present, and the
268   // stack needs to be dynamically re-aligned, the base pointer is the only
269   // reliable way to reference the locals.
270   if (MFI.hasVarSizedObjects() || MF.hasEHFunclets()) {
271     if (needsStackRealignment(MF))
272       return true;
273     // Conservatively estimate whether the negative offset from the frame
274     // pointer will be sufficient to reach. If a function has a smallish
275     // frame, it's less likely to have lots of spills and callee saved
276     // space, so it's all more likely to be within range of the frame pointer.
277     // If it's wrong, we'll materialize the constant and still get to the
278     // object; it's just suboptimal. Negative offsets use the unscaled
279     // load/store instructions, which have a 9-bit signed immediate.
280     return MFI.getLocalFrameSize() >= 256;
281   }
282 
283   return false;
284 }
285 
286 unsigned
getFrameRegister(const MachineFunction & MF) const287 AArch64RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
288   const AArch64FrameLowering *TFI = getFrameLowering(MF);
289   return TFI->hasFP(MF) ? AArch64::FP : AArch64::SP;
290 }
291 
requiresRegisterScavenging(const MachineFunction & MF) const292 bool AArch64RegisterInfo::requiresRegisterScavenging(
293     const MachineFunction &MF) const {
294   return true;
295 }
296 
requiresVirtualBaseRegisters(const MachineFunction & MF) const297 bool AArch64RegisterInfo::requiresVirtualBaseRegisters(
298     const MachineFunction &MF) const {
299   return true;
300 }
301 
302 bool
useFPForScavengingIndex(const MachineFunction & MF) const303 AArch64RegisterInfo::useFPForScavengingIndex(const MachineFunction &MF) const {
304   // This function indicates whether the emergency spillslot should be placed
305   // close to the beginning of the stackframe (closer to FP) or the end
306   // (closer to SP).
307   //
308   // The beginning works most reliably if we have a frame pointer.
309   const AArch64FrameLowering &TFI = *getFrameLowering(MF);
310   return TFI.hasFP(MF);
311 }
312 
requiresFrameIndexScavenging(const MachineFunction & MF) const313 bool AArch64RegisterInfo::requiresFrameIndexScavenging(
314     const MachineFunction &MF) const {
315   return true;
316 }
317 
318 bool
cannotEliminateFrame(const MachineFunction & MF) const319 AArch64RegisterInfo::cannotEliminateFrame(const MachineFunction &MF) const {
320   const MachineFrameInfo &MFI = MF.getFrameInfo();
321   if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI.adjustsStack())
322     return true;
323   return MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken();
324 }
325 
326 /// needsFrameBaseReg - Returns true if the instruction's frame index
327 /// reference would be better served by a base register other than FP
328 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
329 /// references it should create new base registers for.
needsFrameBaseReg(MachineInstr * MI,int64_t Offset) const330 bool AArch64RegisterInfo::needsFrameBaseReg(MachineInstr *MI,
331                                             int64_t Offset) const {
332   for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i)
333     assert(i < MI->getNumOperands() &&
334            "Instr doesn't have FrameIndex operand!");
335 
336   // It's the load/store FI references that cause issues, as it can be difficult
337   // to materialize the offset if it won't fit in the literal field. Estimate
338   // based on the size of the local frame and some conservative assumptions
339   // about the rest of the stack frame (note, this is pre-regalloc, so
340   // we don't know everything for certain yet) whether this offset is likely
341   // to be out of range of the immediate. Return true if so.
342 
343   // We only generate virtual base registers for loads and stores, so
344   // return false for everything else.
345   if (!MI->mayLoad() && !MI->mayStore())
346     return false;
347 
348   // Without a virtual base register, if the function has variable sized
349   // objects, all fixed-size local references will be via the frame pointer,
350   // Approximate the offset and see if it's legal for the instruction.
351   // Note that the incoming offset is based on the SP value at function entry,
352   // so it'll be negative.
353   MachineFunction &MF = *MI->getParent()->getParent();
354   const AArch64FrameLowering *TFI = getFrameLowering(MF);
355   MachineFrameInfo &MFI = MF.getFrameInfo();
356 
357   // Estimate an offset from the frame pointer.
358   // Conservatively assume all GPR callee-saved registers get pushed.
359   // FP, LR, X19-X28, D8-D15. 64-bits each.
360   int64_t FPOffset = Offset - 16 * 20;
361   // Estimate an offset from the stack pointer.
362   // The incoming offset is relating to the SP at the start of the function,
363   // but when we access the local it'll be relative to the SP after local
364   // allocation, so adjust our SP-relative offset by that allocation size.
365   Offset += MFI.getLocalFrameSize();
366   // Assume that we'll have at least some spill slots allocated.
367   // FIXME: This is a total SWAG number. We should run some statistics
368   //        and pick a real one.
369   Offset += 128; // 128 bytes of spill slots
370 
371   // If there is a frame pointer, try using it.
372   // The FP is only available if there is no dynamic realignment. We
373   // don't know for sure yet whether we'll need that, so we guess based
374   // on whether there are any local variables that would trigger it.
375   if (TFI->hasFP(MF) && isFrameOffsetLegal(MI, AArch64::FP, FPOffset))
376     return false;
377 
378   // If we can reference via the stack pointer or base pointer, try that.
379   // FIXME: This (and the code that resolves the references) can be improved
380   //        to only disallow SP relative references in the live range of
381   //        the VLA(s). In practice, it's unclear how much difference that
382   //        would make, but it may be worth doing.
383   if (isFrameOffsetLegal(MI, AArch64::SP, Offset))
384     return false;
385 
386   // The offset likely isn't legal; we want to allocate a virtual base register.
387   return true;
388 }
389 
isFrameOffsetLegal(const MachineInstr * MI,unsigned BaseReg,int64_t Offset) const390 bool AArch64RegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
391                                              unsigned BaseReg,
392                                              int64_t Offset) const {
393   assert(Offset <= INT_MAX && "Offset too big to fit in int.");
394   assert(MI && "Unable to get the legal offset for nil instruction.");
395   int SaveOffset = Offset;
396   return isAArch64FrameOffsetLegal(*MI, SaveOffset) & AArch64FrameOffsetIsLegal;
397 }
398 
399 /// Insert defining instruction(s) for BaseReg to be a pointer to FrameIdx
400 /// at the beginning of the basic block.
materializeFrameBaseRegister(MachineBasicBlock * MBB,unsigned BaseReg,int FrameIdx,int64_t Offset) const401 void AArch64RegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB,
402                                                        unsigned BaseReg,
403                                                        int FrameIdx,
404                                                        int64_t Offset) const {
405   MachineBasicBlock::iterator Ins = MBB->begin();
406   DebugLoc DL; // Defaults to "unknown"
407   if (Ins != MBB->end())
408     DL = Ins->getDebugLoc();
409   const MachineFunction &MF = *MBB->getParent();
410   const AArch64InstrInfo *TII =
411       MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
412   const MCInstrDesc &MCID = TII->get(AArch64::ADDXri);
413   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
414   MRI.constrainRegClass(BaseReg, TII->getRegClass(MCID, 0, this, MF));
415   unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0);
416 
417   BuildMI(*MBB, Ins, DL, MCID, BaseReg)
418       .addFrameIndex(FrameIdx)
419       .addImm(Offset)
420       .addImm(Shifter);
421 }
422 
resolveFrameIndex(MachineInstr & MI,unsigned BaseReg,int64_t Offset) const423 void AArch64RegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg,
424                                             int64_t Offset) const {
425   int Off = Offset; // ARM doesn't need the general 64-bit offsets
426   unsigned i = 0;
427 
428   while (!MI.getOperand(i).isFI()) {
429     ++i;
430     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
431   }
432   const MachineFunction *MF = MI.getParent()->getParent();
433   const AArch64InstrInfo *TII =
434       MF->getSubtarget<AArch64Subtarget>().getInstrInfo();
435   bool Done = rewriteAArch64FrameIndex(MI, i, BaseReg, Off, TII);
436   assert(Done && "Unable to resolve frame index!");
437   (void)Done;
438 }
439 
eliminateFrameIndex(MachineBasicBlock::iterator II,int SPAdj,unsigned FIOperandNum,RegScavenger * RS) const440 void AArch64RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
441                                               int SPAdj, unsigned FIOperandNum,
442                                               RegScavenger *RS) const {
443   assert(SPAdj == 0 && "Unexpected");
444 
445   MachineInstr &MI = *II;
446   MachineBasicBlock &MBB = *MI.getParent();
447   MachineFunction &MF = *MBB.getParent();
448   const AArch64InstrInfo *TII =
449       MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
450   const AArch64FrameLowering *TFI = getFrameLowering(MF);
451 
452   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
453   unsigned FrameReg;
454   int Offset;
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     Offset = TFI->resolveFrameIndexReference(MF, FrameIndex, FrameReg,
460                                              /*PreferFP=*/true);
461     Offset += MI.getOperand(FIOperandNum + 1).getImm();
462     MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false /*isDef*/);
463     MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
464     return;
465   }
466 
467   // Modify MI as necessary to handle as much of 'Offset' as possible
468   Offset = TFI->resolveFrameIndexReference(MF, FrameIndex, FrameReg);
469 
470   if (MI.getOpcode() == TargetOpcode::LOCAL_ESCAPE) {
471     MachineOperand &FI = MI.getOperand(FIOperandNum);
472     FI.ChangeToImmediate(Offset);
473     return;
474   }
475 
476   if (rewriteAArch64FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII))
477     return;
478 
479   assert((!RS || !RS->isScavengingFrameIndex(FrameIndex)) &&
480          "Emergency spill slot is out of reach");
481 
482   // If we get here, the immediate doesn't fit into the instruction.  We folded
483   // as much as possible above.  Handle the rest, providing a register that is
484   // SP+LargeImm.
485   unsigned ScratchReg =
486       MF.getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass);
487   emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset, TII);
488   MI.getOperand(FIOperandNum).ChangeToRegister(ScratchReg, false, false, true);
489 }
490 
getRegPressureLimit(const TargetRegisterClass * RC,MachineFunction & MF) const491 unsigned AArch64RegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
492                                                   MachineFunction &MF) const {
493   const AArch64FrameLowering *TFI = getFrameLowering(MF);
494 
495   switch (RC->getID()) {
496   default:
497     return 0;
498   case AArch64::GPR32RegClassID:
499   case AArch64::GPR32spRegClassID:
500   case AArch64::GPR32allRegClassID:
501   case AArch64::GPR64spRegClassID:
502   case AArch64::GPR64allRegClassID:
503   case AArch64::GPR64RegClassID:
504   case AArch64::GPR32commonRegClassID:
505   case AArch64::GPR64commonRegClassID:
506     return 32 - 1                                   // XZR/SP
507               - (TFI->hasFP(MF) || TT.isOSDarwin()) // FP
508               - MF.getSubtarget<AArch64Subtarget>().getNumXRegisterReserved()
509               - hasBasePointer(MF);  // X19
510   case AArch64::FPR8RegClassID:
511   case AArch64::FPR16RegClassID:
512   case AArch64::FPR32RegClassID:
513   case AArch64::FPR64RegClassID:
514   case AArch64::FPR128RegClassID:
515     return 32;
516 
517   case AArch64::DDRegClassID:
518   case AArch64::DDDRegClassID:
519   case AArch64::DDDDRegClassID:
520   case AArch64::QQRegClassID:
521   case AArch64::QQQRegClassID:
522   case AArch64::QQQQRegClassID:
523     return 32;
524 
525   case AArch64::FPR128_loRegClassID:
526     return 16;
527   }
528 }
529