LLVM API Documentation

AArch64RegisterInfo.cpp
Go to the documentation of this file.
00001 //===- AArch64RegisterInfo.cpp - AArch64 Register Information -------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the AArch64 implementation of the TargetRegisterInfo
00011 // class.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "AArch64RegisterInfo.h"
00016 #include "AArch64FrameLowering.h"
00017 #include "AArch64InstrInfo.h"
00018 #include "AArch64Subtarget.h"
00019 #include "MCTargetDesc/AArch64AddressingModes.h"
00020 #include "llvm/ADT/BitVector.h"
00021 #include "llvm/CodeGen/MachineFrameInfo.h"
00022 #include "llvm/CodeGen/MachineInstrBuilder.h"
00023 #include "llvm/CodeGen/MachineRegisterInfo.h"
00024 #include "llvm/CodeGen/RegisterScavenging.h"
00025 #include "llvm/IR/Function.h"
00026 #include "llvm/Support/CommandLine.h"
00027 #include "llvm/Support/raw_ostream.h"
00028 #include "llvm/Target/TargetFrameLowering.h"
00029 #include "llvm/Target/TargetOptions.h"
00030 
00031 using namespace llvm;
00032 
00033 #define GET_REGINFO_TARGET_DESC
00034 #include "AArch64GenRegisterInfo.inc"
00035 
00036 AArch64RegisterInfo::AArch64RegisterInfo(const AArch64InstrInfo *tii,
00037                                          const AArch64Subtarget *sti)
00038     : AArch64GenRegisterInfo(AArch64::LR), TII(tii), STI(sti) {}
00039 
00040 const MCPhysReg *
00041 AArch64RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
00042   assert(MF && "Invalid MachineFunction pointer.");
00043   if (MF->getFunction()->getCallingConv() == CallingConv::AnyReg)
00044     return CSR_AArch64_AllRegs_SaveList;
00045   else
00046     return CSR_AArch64_AAPCS_SaveList;
00047 }
00048 
00049 const uint32_t *
00050 AArch64RegisterInfo::getCallPreservedMask(CallingConv::ID CC) const {
00051   if (CC == CallingConv::AnyReg)
00052     return CSR_AArch64_AllRegs_RegMask;
00053   else
00054     return CSR_AArch64_AAPCS_RegMask;
00055 }
00056 
00057 const uint32_t *AArch64RegisterInfo::getTLSCallPreservedMask() const {
00058   if (STI->isTargetDarwin())
00059     return CSR_AArch64_TLS_Darwin_RegMask;
00060 
00061   assert(STI->isTargetELF() && "only expect Darwin or ELF TLS");
00062   return CSR_AArch64_TLS_ELF_RegMask;
00063 }
00064 
00065 const uint32_t *
00066 AArch64RegisterInfo::getThisReturnPreservedMask(CallingConv::ID) const {
00067   // This should return a register mask that is the same as that returned by
00068   // getCallPreservedMask but that additionally preserves the register used for
00069   // the first i64 argument (which must also be the register used to return a
00070   // single i64 return value)
00071   //
00072   // In case that the calling convention does not use the same register for
00073   // both, the function should return NULL (does not currently apply)
00074   return CSR_AArch64_AAPCS_ThisReturn_RegMask;
00075 }
00076 
00077 BitVector
00078 AArch64RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
00079   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
00080 
00081   // FIXME: avoid re-calculating this every time.
00082   BitVector Reserved(getNumRegs());
00083   Reserved.set(AArch64::SP);
00084   Reserved.set(AArch64::XZR);
00085   Reserved.set(AArch64::WSP);
00086   Reserved.set(AArch64::WZR);
00087 
00088   if (TFI->hasFP(MF) || STI->isTargetDarwin()) {
00089     Reserved.set(AArch64::FP);
00090     Reserved.set(AArch64::W29);
00091   }
00092 
00093   if (STI->isTargetDarwin()) {
00094     Reserved.set(AArch64::X18); // Platform register
00095     Reserved.set(AArch64::W18);
00096   }
00097 
00098   if (hasBasePointer(MF)) {
00099     Reserved.set(AArch64::X19);
00100     Reserved.set(AArch64::W19);
00101   }
00102 
00103   return Reserved;
00104 }
00105 
00106 bool AArch64RegisterInfo::isReservedReg(const MachineFunction &MF,
00107                                       unsigned Reg) const {
00108   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
00109 
00110   switch (Reg) {
00111   default:
00112     break;
00113   case AArch64::SP:
00114   case AArch64::XZR:
00115   case AArch64::WSP:
00116   case AArch64::WZR:
00117     return true;
00118   case AArch64::X18:
00119   case AArch64::W18:
00120     return STI->isTargetDarwin();
00121   case AArch64::FP:
00122   case AArch64::W29:
00123     return TFI->hasFP(MF) || STI->isTargetDarwin();
00124   case AArch64::W19:
00125   case AArch64::X19:
00126     return hasBasePointer(MF);
00127   }
00128 
00129   return false;
00130 }
00131 
00132 const TargetRegisterClass *
00133 AArch64RegisterInfo::getPointerRegClass(const MachineFunction &MF,
00134                                       unsigned Kind) const {
00135   return &AArch64::GPR64RegClass;
00136 }
00137 
00138 const TargetRegisterClass *
00139 AArch64RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
00140   if (RC == &AArch64::CCRRegClass)
00141     return &AArch64::GPR64RegClass; // Only MSR & MRS copy NZCV.
00142   return RC;
00143 }
00144 
00145 unsigned AArch64RegisterInfo::getBaseRegister() const { return AArch64::X19; }
00146 
00147 bool AArch64RegisterInfo::hasBasePointer(const MachineFunction &MF) const {
00148   const MachineFrameInfo *MFI = MF.getFrameInfo();
00149 
00150   // In the presence of variable sized objects, if the fixed stack size is
00151   // large enough that referencing from the FP won't result in things being
00152   // in range relatively often, we can use a base pointer to allow access
00153   // from the other direction like the SP normally works.
00154   if (MFI->hasVarSizedObjects()) {
00155     // Conservatively estimate whether the negative offset from the frame
00156     // pointer will be sufficient to reach. If a function has a smallish
00157     // frame, it's less likely to have lots of spills and callee saved
00158     // space, so it's all more likely to be within range of the frame pointer.
00159     // If it's wrong, we'll materialize the constant and still get to the
00160     // object; it's just suboptimal. Negative offsets use the unscaled
00161     // load/store instructions, which have a 9-bit signed immediate.
00162     if (MFI->getLocalFrameSize() < 256)
00163       return false;
00164     return true;
00165   }
00166 
00167   return false;
00168 }
00169 
00170 unsigned
00171 AArch64RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
00172   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
00173 
00174   return TFI->hasFP(MF) ? AArch64::FP : AArch64::SP;
00175 }
00176 
00177 bool AArch64RegisterInfo::requiresRegisterScavenging(
00178     const MachineFunction &MF) const {
00179   return true;
00180 }
00181 
00182 bool AArch64RegisterInfo::requiresVirtualBaseRegisters(
00183     const MachineFunction &MF) const {
00184   return true;
00185 }
00186 
00187 bool
00188 AArch64RegisterInfo::useFPForScavengingIndex(const MachineFunction &MF) const {
00189   const MachineFrameInfo *MFI = MF.getFrameInfo();
00190   // AArch64FrameLowering::resolveFrameIndexReference() can always fall back
00191   // to the stack pointer, so only put the emergency spill slot next to the
00192   // FP when there's no better way to access it (SP or base pointer).
00193   return MFI->hasVarSizedObjects() && !hasBasePointer(MF);
00194 }
00195 
00196 bool AArch64RegisterInfo::requiresFrameIndexScavenging(
00197     const MachineFunction &MF) const {
00198   return true;
00199 }
00200 
00201 bool
00202 AArch64RegisterInfo::cannotEliminateFrame(const MachineFunction &MF) const {
00203   const MachineFrameInfo *MFI = MF.getFrameInfo();
00204   // Only consider eliminating leaf frames.
00205   if (MFI->hasCalls() || (MF.getTarget().Options.DisableFramePointerElim(MF) &&
00206                           MFI->adjustsStack()))
00207     return true;
00208   return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken();
00209 }
00210 
00211 /// needsFrameBaseReg - Returns true if the instruction's frame index
00212 /// reference would be better served by a base register other than FP
00213 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
00214 /// references it should create new base registers for.
00215 bool AArch64RegisterInfo::needsFrameBaseReg(MachineInstr *MI,
00216                                             int64_t Offset) const {
00217   for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i)
00218     assert(i < MI->getNumOperands() &&
00219            "Instr doesn't have FrameIndex operand!");
00220 
00221   // It's the load/store FI references that cause issues, as it can be difficult
00222   // to materialize the offset if it won't fit in the literal field. Estimate
00223   // based on the size of the local frame and some conservative assumptions
00224   // about the rest of the stack frame (note, this is pre-regalloc, so
00225   // we don't know everything for certain yet) whether this offset is likely
00226   // to be out of range of the immediate. Return true if so.
00227 
00228   // We only generate virtual base registers for loads and stores, so
00229   // return false for everything else.
00230   if (!MI->mayLoad() && !MI->mayStore())
00231     return false;
00232 
00233   // Without a virtual base register, if the function has variable sized
00234   // objects, all fixed-size local references will be via the frame pointer,
00235   // Approximate the offset and see if it's legal for the instruction.
00236   // Note that the incoming offset is based on the SP value at function entry,
00237   // so it'll be negative.
00238   MachineFunction &MF = *MI->getParent()->getParent();
00239   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
00240   MachineFrameInfo *MFI = MF.getFrameInfo();
00241 
00242   // Estimate an offset from the frame pointer.
00243   // Conservatively assume all GPR callee-saved registers get pushed.
00244   // FP, LR, X19-X28, D8-D15. 64-bits each.
00245   int64_t FPOffset = Offset - 16 * 20;
00246   // Estimate an offset from the stack pointer.
00247   // The incoming offset is relating to the SP at the start of the function,
00248   // but when we access the local it'll be relative to the SP after local
00249   // allocation, so adjust our SP-relative offset by that allocation size.
00250   Offset += MFI->getLocalFrameSize();
00251   // Assume that we'll have at least some spill slots allocated.
00252   // FIXME: This is a total SWAG number. We should run some statistics
00253   //        and pick a real one.
00254   Offset += 128; // 128 bytes of spill slots
00255 
00256   // If there is a frame pointer, try using it.
00257   // The FP is only available if there is no dynamic realignment. We
00258   // don't know for sure yet whether we'll need that, so we guess based
00259   // on whether there are any local variables that would trigger it.
00260   if (TFI->hasFP(MF) && isFrameOffsetLegal(MI, FPOffset))
00261     return false;
00262 
00263   // If we can reference via the stack pointer or base pointer, try that.
00264   // FIXME: This (and the code that resolves the references) can be improved
00265   //        to only disallow SP relative references in the live range of
00266   //        the VLA(s). In practice, it's unclear how much difference that
00267   //        would make, but it may be worth doing.
00268   if (isFrameOffsetLegal(MI, Offset))
00269     return false;
00270 
00271   // The offset likely isn't legal; we want to allocate a virtual base register.
00272   return true;
00273 }
00274 
00275 bool AArch64RegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
00276                                              int64_t Offset) const {
00277   assert(Offset <= INT_MAX && "Offset too big to fit in int.");
00278   assert(MI && "Unable to get the legal offset for nil instruction.");
00279   int SaveOffset = Offset;
00280   return isAArch64FrameOffsetLegal(*MI, SaveOffset) & AArch64FrameOffsetIsLegal;
00281 }
00282 
00283 /// Insert defining instruction(s) for BaseReg to be a pointer to FrameIdx
00284 /// at the beginning of the basic block.
00285 void AArch64RegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB,
00286                                                        unsigned BaseReg,
00287                                                        int FrameIdx,
00288                                                        int64_t Offset) const {
00289   MachineBasicBlock::iterator Ins = MBB->begin();
00290   DebugLoc DL; // Defaults to "unknown"
00291   if (Ins != MBB->end())
00292     DL = Ins->getDebugLoc();
00293 
00294   const MCInstrDesc &MCID = TII->get(AArch64::ADDXri);
00295   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
00296   const MachineFunction &MF = *MBB->getParent();
00297   MRI.constrainRegClass(BaseReg, TII->getRegClass(MCID, 0, this, MF));
00298   unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0);
00299 
00300   BuildMI(*MBB, Ins, DL, MCID, BaseReg)
00301       .addFrameIndex(FrameIdx)
00302       .addImm(Offset)
00303       .addImm(Shifter);
00304 }
00305 
00306 void AArch64RegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg,
00307                                             int64_t Offset) const {
00308   int Off = Offset; // ARM doesn't need the general 64-bit offsets
00309   unsigned i = 0;
00310 
00311   while (!MI.getOperand(i).isFI()) {
00312     ++i;
00313     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
00314   }
00315   bool Done = rewriteAArch64FrameIndex(MI, i, BaseReg, Off, TII);
00316   assert(Done && "Unable to resolve frame index!");
00317   (void)Done;
00318 }
00319 
00320 void AArch64RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
00321                                               int SPAdj, unsigned FIOperandNum,
00322                                               RegScavenger *RS) const {
00323   assert(SPAdj == 0 && "Unexpected");
00324 
00325   MachineInstr &MI = *II;
00326   MachineBasicBlock &MBB = *MI.getParent();
00327   MachineFunction &MF = *MBB.getParent();
00328   const AArch64FrameLowering *TFI = static_cast<const AArch64FrameLowering *>(
00329       MF.getSubtarget().getFrameLowering());
00330 
00331   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
00332   unsigned FrameReg;
00333   int Offset;
00334 
00335   // Special handling of dbg_value, stackmap and patchpoint instructions.
00336   if (MI.isDebugValue() || MI.getOpcode() == TargetOpcode::STACKMAP ||
00337       MI.getOpcode() == TargetOpcode::PATCHPOINT) {
00338     Offset = TFI->resolveFrameIndexReference(MF, FrameIndex, FrameReg,
00339                                              /*PreferFP=*/true);
00340     Offset += MI.getOperand(FIOperandNum + 1).getImm();
00341     MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false /*isDef*/);
00342     MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
00343     return;
00344   }
00345 
00346   // Modify MI as necessary to handle as much of 'Offset' as possible
00347   Offset = TFI->resolveFrameIndexReference(MF, FrameIndex, FrameReg);
00348   if (rewriteAArch64FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII))
00349     return;
00350 
00351   assert((!RS || !RS->isScavengingFrameIndex(FrameIndex)) &&
00352          "Emergency spill slot is out of reach");
00353 
00354   // If we get here, the immediate doesn't fit into the instruction.  We folded
00355   // as much as possible above.  Handle the rest, providing a register that is
00356   // SP+LargeImm.
00357   unsigned ScratchReg =
00358       MF.getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass);
00359   emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset, TII);
00360   MI.getOperand(FIOperandNum).ChangeToRegister(ScratchReg, false, false, true);
00361 }
00362 
00363 namespace llvm {
00364 
00365 unsigned AArch64RegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
00366                                                   MachineFunction &MF) const {
00367   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
00368 
00369   switch (RC->getID()) {
00370   default:
00371     return 0;
00372   case AArch64::GPR32RegClassID:
00373   case AArch64::GPR32spRegClassID:
00374   case AArch64::GPR32allRegClassID:
00375   case AArch64::GPR64spRegClassID:
00376   case AArch64::GPR64allRegClassID:
00377   case AArch64::GPR64RegClassID:
00378   case AArch64::GPR32commonRegClassID:
00379   case AArch64::GPR64commonRegClassID:
00380     return 32 - 1                                      // XZR/SP
00381            - (TFI->hasFP(MF) || STI->isTargetDarwin()) // FP
00382            - STI->isTargetDarwin() // X18 reserved as platform register
00383            - hasBasePointer(MF);   // X19
00384   case AArch64::FPR8RegClassID:
00385   case AArch64::FPR16RegClassID:
00386   case AArch64::FPR32RegClassID:
00387   case AArch64::FPR64RegClassID:
00388   case AArch64::FPR128RegClassID:
00389     return 32;
00390 
00391   case AArch64::DDRegClassID:
00392   case AArch64::DDDRegClassID:
00393   case AArch64::DDDDRegClassID:
00394   case AArch64::QQRegClassID:
00395   case AArch64::QQQRegClassID:
00396   case AArch64::QQQQRegClassID:
00397     return 32;
00398 
00399   case AArch64::FPR128_loRegClassID:
00400     return 16;
00401   }
00402 }
00403 
00404 } // namespace llvm