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