LLVM API Documentation

ARMBaseRegisterInfo.cpp
Go to the documentation of this file.
00001 //===-- ARMBaseRegisterInfo.cpp - ARM 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 base ARM implementation of TargetRegisterInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "ARMBaseRegisterInfo.h"
00015 #include "ARM.h"
00016 #include "ARMBaseInstrInfo.h"
00017 #include "ARMFrameLowering.h"
00018 #include "ARMMachineFunctionInfo.h"
00019 #include "ARMSubtarget.h"
00020 #include "MCTargetDesc/ARMAddressingModes.h"
00021 #include "llvm/ADT/BitVector.h"
00022 #include "llvm/ADT/SmallVector.h"
00023 #include "llvm/CodeGen/MachineConstantPool.h"
00024 #include "llvm/CodeGen/MachineFrameInfo.h"
00025 #include "llvm/CodeGen/MachineFunction.h"
00026 #include "llvm/CodeGen/MachineInstrBuilder.h"
00027 #include "llvm/CodeGen/MachineRegisterInfo.h"
00028 #include "llvm/CodeGen/RegisterScavenging.h"
00029 #include "llvm/CodeGen/VirtRegMap.h"
00030 #include "llvm/IR/Constants.h"
00031 #include "llvm/IR/DerivedTypes.h"
00032 #include "llvm/IR/Function.h"
00033 #include "llvm/IR/LLVMContext.h"
00034 #include "llvm/Support/Debug.h"
00035 #include "llvm/Support/ErrorHandling.h"
00036 #include "llvm/Support/raw_ostream.h"
00037 #include "llvm/Target/TargetFrameLowering.h"
00038 #include "llvm/Target/TargetMachine.h"
00039 #include "llvm/Target/TargetOptions.h"
00040 
00041 #define DEBUG_TYPE "arm-register-info"
00042 
00043 #define GET_REGINFO_TARGET_DESC
00044 #include "ARMGenRegisterInfo.inc"
00045 
00046 using namespace llvm;
00047 
00048 ARMBaseRegisterInfo::ARMBaseRegisterInfo(const ARMSubtarget &sti)
00049     : ARMGenRegisterInfo(ARM::LR, 0, 0, ARM::PC), STI(sti), BasePtr(ARM::R6) {
00050   if (STI.isTargetMachO()) {
00051     if (STI.isTargetDarwin() || STI.isThumb1Only())
00052       FramePtr = ARM::R7;
00053     else
00054       FramePtr = ARM::R11;
00055   } else if (STI.isTargetWindows())
00056     FramePtr = ARM::R11;
00057   else // ARM EABI
00058     FramePtr = STI.isThumb() ? ARM::R7 : ARM::R11;
00059 }
00060 
00061 const MCPhysReg*
00062 ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
00063   const MCPhysReg *RegList = (STI.isTargetIOS() && !STI.isAAPCS_ABI())
00064                                 ? CSR_iOS_SaveList
00065                                 : CSR_AAPCS_SaveList;
00066 
00067   if (!MF) return RegList;
00068 
00069   const Function *F = MF->getFunction();
00070   if (F->getCallingConv() == CallingConv::GHC) {
00071     // GHC set of callee saved regs is empty as all those regs are
00072     // used for passing STG regs around
00073     return CSR_NoRegs_SaveList;
00074   } else if (F->hasFnAttribute("interrupt")) {
00075     if (STI.isMClass()) {
00076       // M-class CPUs have hardware which saves the registers needed to allow a
00077       // function conforming to the AAPCS to function as a handler.
00078       return CSR_AAPCS_SaveList;
00079     } else if (F->getFnAttribute("interrupt").getValueAsString() == "FIQ") {
00080       // Fast interrupt mode gives the handler a private copy of R8-R14, so less
00081       // need to be saved to restore user-mode state.
00082       return CSR_FIQ_SaveList;
00083     } else {
00084       // Generally only R13-R14 (i.e. SP, LR) are automatically preserved by
00085       // exception handling.
00086       return CSR_GenericInt_SaveList;
00087     }
00088   }
00089 
00090   return RegList;
00091 }
00092 
00093 const uint32_t*
00094 ARMBaseRegisterInfo::getCallPreservedMask(CallingConv::ID CC) const {
00095   if (CC == CallingConv::GHC)
00096     // This is academic becase all GHC calls are (supposed to be) tail calls
00097     return CSR_NoRegs_RegMask;
00098   return (STI.isTargetIOS() && !STI.isAAPCS_ABI())
00099     ? CSR_iOS_RegMask : CSR_AAPCS_RegMask;
00100 }
00101 
00102 const uint32_t*
00103 ARMBaseRegisterInfo::getNoPreservedMask() const {
00104   return CSR_NoRegs_RegMask;
00105 }
00106 
00107 const uint32_t*
00108 ARMBaseRegisterInfo::getThisReturnPreservedMask(CallingConv::ID CC) const {
00109   // This should return a register mask that is the same as that returned by
00110   // getCallPreservedMask but that additionally preserves the register used for
00111   // the first i32 argument (which must also be the register used to return a
00112   // single i32 return value)
00113   //
00114   // In case that the calling convention does not use the same register for
00115   // both or otherwise does not want to enable this optimization, the function
00116   // should return NULL
00117   if (CC == CallingConv::GHC)
00118     // This is academic becase all GHC calls are (supposed to be) tail calls
00119     return nullptr;
00120   return (STI.isTargetIOS() && !STI.isAAPCS_ABI())
00121     ? CSR_iOS_ThisReturn_RegMask : CSR_AAPCS_ThisReturn_RegMask;
00122 }
00123 
00124 BitVector ARMBaseRegisterInfo::
00125 getReservedRegs(const MachineFunction &MF) const {
00126   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
00127 
00128   // FIXME: avoid re-calculating this every time.
00129   BitVector Reserved(getNumRegs());
00130   Reserved.set(ARM::SP);
00131   Reserved.set(ARM::PC);
00132   Reserved.set(ARM::FPSCR);
00133   Reserved.set(ARM::APSR_NZCV);
00134   if (TFI->hasFP(MF))
00135     Reserved.set(FramePtr);
00136   if (hasBasePointer(MF))
00137     Reserved.set(BasePtr);
00138   // Some targets reserve R9.
00139   if (STI.isR9Reserved())
00140     Reserved.set(ARM::R9);
00141   // Reserve D16-D31 if the subtarget doesn't support them.
00142   if (!STI.hasVFP3() || STI.hasD16()) {
00143     assert(ARM::D31 == ARM::D16 + 15);
00144     for (unsigned i = 0; i != 16; ++i)
00145       Reserved.set(ARM::D16 + i);
00146   }
00147   const TargetRegisterClass *RC  = &ARM::GPRPairRegClass;
00148   for(TargetRegisterClass::iterator I = RC->begin(), E = RC->end(); I!=E; ++I)
00149     for (MCSubRegIterator SI(*I, this); SI.isValid(); ++SI)
00150       if (Reserved.test(*SI)) Reserved.set(*I);
00151 
00152   return Reserved;
00153 }
00154 
00155 const TargetRegisterClass*
00156 ARMBaseRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC)
00157                                                                          const {
00158   const TargetRegisterClass *Super = RC;
00159   TargetRegisterClass::sc_iterator I = RC->getSuperClasses();
00160   do {
00161     switch (Super->getID()) {
00162     case ARM::GPRRegClassID:
00163     case ARM::SPRRegClassID:
00164     case ARM::DPRRegClassID:
00165     case ARM::QPRRegClassID:
00166     case ARM::QQPRRegClassID:
00167     case ARM::QQQQPRRegClassID:
00168     case ARM::GPRPairRegClassID:
00169       return Super;
00170     }
00171     Super = *I++;
00172   } while (Super);
00173   return RC;
00174 }
00175 
00176 const TargetRegisterClass *
00177 ARMBaseRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
00178                                                                          const {
00179   return &ARM::GPRRegClass;
00180 }
00181 
00182 const TargetRegisterClass *
00183 ARMBaseRegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
00184   if (RC == &ARM::CCRRegClass)
00185     return nullptr;  // Can't copy CCR registers.
00186   return RC;
00187 }
00188 
00189 unsigned
00190 ARMBaseRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
00191                                          MachineFunction &MF) const {
00192   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
00193 
00194   switch (RC->getID()) {
00195   default:
00196     return 0;
00197   case ARM::tGPRRegClassID:
00198     return TFI->hasFP(MF) ? 4 : 5;
00199   case ARM::GPRRegClassID: {
00200     unsigned FP = TFI->hasFP(MF) ? 1 : 0;
00201     return 10 - FP - (STI.isR9Reserved() ? 1 : 0);
00202   }
00203   case ARM::SPRRegClassID:  // Currently not used as 'rep' register class.
00204   case ARM::DPRRegClassID:
00205     return 32 - 10;
00206   }
00207 }
00208 
00209 // Get the other register in a GPRPair.
00210 static unsigned getPairedGPR(unsigned Reg, bool Odd, const MCRegisterInfo *RI) {
00211   for (MCSuperRegIterator Supers(Reg, RI); Supers.isValid(); ++Supers)
00212     if (ARM::GPRPairRegClass.contains(*Supers))
00213       return RI->getSubReg(*Supers, Odd ? ARM::gsub_1 : ARM::gsub_0);
00214   return 0;
00215 }
00216 
00217 // Resolve the RegPairEven / RegPairOdd register allocator hints.
00218 void
00219 ARMBaseRegisterInfo::getRegAllocationHints(unsigned VirtReg,
00220                                            ArrayRef<MCPhysReg> Order,
00221                                            SmallVectorImpl<MCPhysReg> &Hints,
00222                                            const MachineFunction &MF,
00223                                            const VirtRegMap *VRM) const {
00224   const MachineRegisterInfo &MRI = MF.getRegInfo();
00225   std::pair<unsigned, unsigned> Hint = MRI.getRegAllocationHint(VirtReg);
00226 
00227   unsigned Odd;
00228   switch (Hint.first) {
00229   case ARMRI::RegPairEven:
00230     Odd = 0;
00231     break;
00232   case ARMRI::RegPairOdd:
00233     Odd = 1;
00234     break;
00235   default:
00236     TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints, MF, VRM);
00237     return;
00238   }
00239 
00240   // This register should preferably be even (Odd == 0) or odd (Odd == 1).
00241   // Check if the other part of the pair has already been assigned, and provide
00242   // the paired register as the first hint.
00243   unsigned PairedPhys = 0;
00244   if (VRM && VRM->hasPhys(Hint.second)) {
00245     PairedPhys = getPairedGPR(VRM->getPhys(Hint.second), Odd, this);
00246     if (PairedPhys && MRI.isReserved(PairedPhys))
00247       PairedPhys = 0;
00248   }
00249 
00250   // First prefer the paired physreg.
00251   if (PairedPhys &&
00252       std::find(Order.begin(), Order.end(), PairedPhys) != Order.end())
00253     Hints.push_back(PairedPhys);
00254 
00255   // Then prefer even or odd registers.
00256   for (unsigned I = 0, E = Order.size(); I != E; ++I) {
00257     unsigned Reg = Order[I];
00258     if (Reg == PairedPhys || (getEncodingValue(Reg) & 1) != Odd)
00259       continue;
00260     // Don't provide hints that are paired to a reserved register.
00261     unsigned Paired = getPairedGPR(Reg, !Odd, this);
00262     if (!Paired || MRI.isReserved(Paired))
00263       continue;
00264     Hints.push_back(Reg);
00265   }
00266 }
00267 
00268 void
00269 ARMBaseRegisterInfo::UpdateRegAllocHint(unsigned Reg, unsigned NewReg,
00270                                         MachineFunction &MF) const {
00271   MachineRegisterInfo *MRI = &MF.getRegInfo();
00272   std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg);
00273   if ((Hint.first == (unsigned)ARMRI::RegPairOdd ||
00274        Hint.first == (unsigned)ARMRI::RegPairEven) &&
00275       TargetRegisterInfo::isVirtualRegister(Hint.second)) {
00276     // If 'Reg' is one of the even / odd register pair and it's now changed
00277     // (e.g. coalesced) into a different register. The other register of the
00278     // pair allocation hint must be updated to reflect the relationship
00279     // change.
00280     unsigned OtherReg = Hint.second;
00281     Hint = MRI->getRegAllocationHint(OtherReg);
00282     if (Hint.second == Reg)
00283       // Make sure the pair has not already divorced.
00284       MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
00285   }
00286 }
00287 
00288 bool
00289 ARMBaseRegisterInfo::avoidWriteAfterWrite(const TargetRegisterClass *RC) const {
00290   // CortexA9 has a Write-after-write hazard for NEON registers.
00291   if (!STI.isLikeA9())
00292     return false;
00293 
00294   switch (RC->getID()) {
00295   case ARM::DPRRegClassID:
00296   case ARM::DPR_8RegClassID:
00297   case ARM::DPR_VFP2RegClassID:
00298   case ARM::QPRRegClassID:
00299   case ARM::QPR_8RegClassID:
00300   case ARM::QPR_VFP2RegClassID:
00301   case ARM::SPRRegClassID:
00302   case ARM::SPR_8RegClassID:
00303     // Avoid reusing S, D, and Q registers.
00304     // Don't increase register pressure for QQ and QQQQ.
00305     return true;
00306   default:
00307     return false;
00308   }
00309 }
00310 
00311 bool ARMBaseRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
00312   const MachineFrameInfo *MFI = MF.getFrameInfo();
00313   const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
00314   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
00315 
00316   // When outgoing call frames are so large that we adjust the stack pointer
00317   // around the call, we can no longer use the stack pointer to reach the
00318   // emergency spill slot.
00319   if (needsStackRealignment(MF) && !TFI->hasReservedCallFrame(MF))
00320     return true;
00321 
00322   // Thumb has trouble with negative offsets from the FP. Thumb2 has a limited
00323   // negative range for ldr/str (255), and thumb1 is positive offsets only.
00324   // It's going to be better to use the SP or Base Pointer instead. When there
00325   // are variable sized objects, we can't reference off of the SP, so we
00326   // reserve a Base Pointer.
00327   if (AFI->isThumbFunction() && MFI->hasVarSizedObjects()) {
00328     // Conservatively estimate whether the negative offset from the frame
00329     // pointer will be sufficient to reach. If a function has a smallish
00330     // frame, it's less likely to have lots of spills and callee saved
00331     // space, so it's all more likely to be within range of the frame pointer.
00332     // If it's wrong, the scavenger will still enable access to work, it just
00333     // won't be optimal.
00334     if (AFI->isThumb2Function() && MFI->getLocalFrameSize() < 128)
00335       return false;
00336     return true;
00337   }
00338 
00339   return false;
00340 }
00341 
00342 bool ARMBaseRegisterInfo::canRealignStack(const MachineFunction &MF) const {
00343   const MachineRegisterInfo *MRI = &MF.getRegInfo();
00344   const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
00345   // We can't realign the stack if:
00346   // 1. Dynamic stack realignment is explicitly disabled,
00347   // 2. This is a Thumb1 function (it's not useful, so we don't bother), or
00348   // 3. There are VLAs in the function and the base pointer is disabled.
00349   if (MF.getFunction()->hasFnAttribute("no-realign-stack"))
00350     return false;
00351   if (AFI->isThumb1OnlyFunction())
00352     return false;
00353   // Stack realignment requires a frame pointer.  If we already started
00354   // register allocation with frame pointer elimination, it is too late now.
00355   if (!MRI->canReserveReg(FramePtr))
00356     return false;
00357   // We may also need a base pointer if there are dynamic allocas or stack
00358   // pointer adjustments around calls.
00359   if (MF.getTarget()
00360           .getSubtargetImpl()
00361           ->getFrameLowering()
00362           ->hasReservedCallFrame(MF))
00363     return true;
00364   // A base pointer is required and allowed.  Check that it isn't too late to
00365   // reserve it.
00366   return MRI->canReserveReg(BasePtr);
00367 }
00368 
00369 bool ARMBaseRegisterInfo::
00370 needsStackRealignment(const MachineFunction &MF) const {
00371   const MachineFrameInfo *MFI = MF.getFrameInfo();
00372   const Function *F = MF.getFunction();
00373   unsigned StackAlign = MF.getTarget()
00374                             .getSubtargetImpl()
00375                             ->getFrameLowering()
00376                             ->getStackAlignment();
00377   bool requiresRealignment =
00378     ((MFI->getMaxAlignment() > StackAlign) ||
00379      F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
00380                                      Attribute::StackAlignment));
00381 
00382   return requiresRealignment && canRealignStack(MF);
00383 }
00384 
00385 bool ARMBaseRegisterInfo::
00386 cannotEliminateFrame(const MachineFunction &MF) const {
00387   const MachineFrameInfo *MFI = MF.getFrameInfo();
00388   if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI->adjustsStack())
00389     return true;
00390   return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken()
00391     || needsStackRealignment(MF);
00392 }
00393 
00394 unsigned
00395 ARMBaseRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
00396   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
00397 
00398   if (TFI->hasFP(MF))
00399     return FramePtr;
00400   return ARM::SP;
00401 }
00402 
00403 /// emitLoadConstPool - Emits a load from constpool to materialize the
00404 /// specified immediate.
00405 void ARMBaseRegisterInfo::
00406 emitLoadConstPool(MachineBasicBlock &MBB,
00407                   MachineBasicBlock::iterator &MBBI,
00408                   DebugLoc dl,
00409                   unsigned DestReg, unsigned SubIdx, int Val,
00410                   ARMCC::CondCodes Pred,
00411                   unsigned PredReg, unsigned MIFlags) const {
00412   MachineFunction &MF = *MBB.getParent();
00413   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
00414   MachineConstantPool *ConstantPool = MF.getConstantPool();
00415   const Constant *C =
00416         ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val);
00417   unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
00418 
00419   BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
00420     .addReg(DestReg, getDefRegState(true), SubIdx)
00421     .addConstantPoolIndex(Idx)
00422     .addImm(0).addImm(Pred).addReg(PredReg)
00423     .setMIFlags(MIFlags);
00424 }
00425 
00426 bool ARMBaseRegisterInfo::mayOverrideLocalAssignment() const {
00427   // The native linux build hits a downstream codegen bug when this is enabled.
00428   return STI.isTargetDarwin();
00429 }
00430 
00431 bool ARMBaseRegisterInfo::
00432 requiresRegisterScavenging(const MachineFunction &MF) const {
00433   return true;
00434 }
00435 
00436 bool ARMBaseRegisterInfo::
00437 trackLivenessAfterRegAlloc(const MachineFunction &MF) const {
00438   return true;
00439 }
00440 
00441 bool ARMBaseRegisterInfo::
00442 requiresFrameIndexScavenging(const MachineFunction &MF) const {
00443   return true;
00444 }
00445 
00446 bool ARMBaseRegisterInfo::
00447 requiresVirtualBaseRegisters(const MachineFunction &MF) const {
00448   return true;
00449 }
00450 
00451 int64_t ARMBaseRegisterInfo::
00452 getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const {
00453   const MCInstrDesc &Desc = MI->getDesc();
00454   unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
00455   int64_t InstrOffs = 0;
00456   int Scale = 1;
00457   unsigned ImmIdx = 0;
00458   switch (AddrMode) {
00459   case ARMII::AddrModeT2_i8:
00460   case ARMII::AddrModeT2_i12:
00461   case ARMII::AddrMode_i12:
00462     InstrOffs = MI->getOperand(Idx+1).getImm();
00463     Scale = 1;
00464     break;
00465   case ARMII::AddrMode5: {
00466     // VFP address mode.
00467     const MachineOperand &OffOp = MI->getOperand(Idx+1);
00468     InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm());
00469     if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub)
00470       InstrOffs = -InstrOffs;
00471     Scale = 4;
00472     break;
00473   }
00474   case ARMII::AddrMode2: {
00475     ImmIdx = Idx+2;
00476     InstrOffs = ARM_AM::getAM2Offset(MI->getOperand(ImmIdx).getImm());
00477     if (ARM_AM::getAM2Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
00478       InstrOffs = -InstrOffs;
00479     break;
00480   }
00481   case ARMII::AddrMode3: {
00482     ImmIdx = Idx+2;
00483     InstrOffs = ARM_AM::getAM3Offset(MI->getOperand(ImmIdx).getImm());
00484     if (ARM_AM::getAM3Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
00485       InstrOffs = -InstrOffs;
00486     break;
00487   }
00488   case ARMII::AddrModeT1_s: {
00489     ImmIdx = Idx+1;
00490     InstrOffs = MI->getOperand(ImmIdx).getImm();
00491     Scale = 4;
00492     break;
00493   }
00494   default:
00495     llvm_unreachable("Unsupported addressing mode!");
00496   }
00497 
00498   return InstrOffs * Scale;
00499 }
00500 
00501 /// needsFrameBaseReg - Returns true if the instruction's frame index
00502 /// reference would be better served by a base register other than FP
00503 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
00504 /// references it should create new base registers for.
00505 bool ARMBaseRegisterInfo::
00506 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
00507   for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) {
00508     assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
00509   }
00510 
00511   // It's the load/store FI references that cause issues, as it can be difficult
00512   // to materialize the offset if it won't fit in the literal field. Estimate
00513   // based on the size of the local frame and some conservative assumptions
00514   // about the rest of the stack frame (note, this is pre-regalloc, so
00515   // we don't know everything for certain yet) whether this offset is likely
00516   // to be out of range of the immediate. Return true if so.
00517 
00518   // We only generate virtual base registers for loads and stores, so
00519   // return false for everything else.
00520   unsigned Opc = MI->getOpcode();
00521   switch (Opc) {
00522   case ARM::LDRi12: case ARM::LDRH: case ARM::LDRBi12:
00523   case ARM::STRi12: case ARM::STRH: case ARM::STRBi12:
00524   case ARM::t2LDRi12: case ARM::t2LDRi8:
00525   case ARM::t2STRi12: case ARM::t2STRi8:
00526   case ARM::VLDRS: case ARM::VLDRD:
00527   case ARM::VSTRS: case ARM::VSTRD:
00528   case ARM::tSTRspi: case ARM::tLDRspi:
00529     break;
00530   default:
00531     return false;
00532   }
00533 
00534   // Without a virtual base register, if the function has variable sized
00535   // objects, all fixed-size local references will be via the frame pointer,
00536   // Approximate the offset and see if it's legal for the instruction.
00537   // Note that the incoming offset is based on the SP value at function entry,
00538   // so it'll be negative.
00539   MachineFunction &MF = *MI->getParent()->getParent();
00540   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
00541   MachineFrameInfo *MFI = MF.getFrameInfo();
00542   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
00543 
00544   // Estimate an offset from the frame pointer.
00545   // Conservatively assume all callee-saved registers get pushed. R4-R6
00546   // will be earlier than the FP, so we ignore those.
00547   // R7, LR
00548   int64_t FPOffset = Offset - 8;
00549   // ARM and Thumb2 functions also need to consider R8-R11 and D8-D15
00550   if (!AFI->isThumbFunction() || !AFI->isThumb1OnlyFunction())
00551     FPOffset -= 80;
00552   // Estimate an offset from the stack pointer.
00553   // The incoming offset is relating to the SP at the start of the function,
00554   // but when we access the local it'll be relative to the SP after local
00555   // allocation, so adjust our SP-relative offset by that allocation size.
00556   Offset = -Offset;
00557   Offset += MFI->getLocalFrameSize();
00558   // Assume that we'll have at least some spill slots allocated.
00559   // FIXME: This is a total SWAG number. We should run some statistics
00560   //        and pick a real one.
00561   Offset += 128; // 128 bytes of spill slots
00562 
00563   // If there is a frame pointer, try using it.
00564   // The FP is only available if there is no dynamic realignment. We
00565   // don't know for sure yet whether we'll need that, so we guess based
00566   // on whether there are any local variables that would trigger it.
00567   unsigned StackAlign = TFI->getStackAlignment();
00568   if (TFI->hasFP(MF) &&
00569       !((MFI->getLocalFrameMaxAlign() > StackAlign) && canRealignStack(MF))) {
00570     if (isFrameOffsetLegal(MI, FPOffset))
00571       return false;
00572   }
00573   // If we can reference via the stack pointer, try that.
00574   // FIXME: This (and the code that resolves the references) can be improved
00575   //        to only disallow SP relative references in the live range of
00576   //        the VLA(s). In practice, it's unclear how much difference that
00577   //        would make, but it may be worth doing.
00578   if (!MFI->hasVarSizedObjects() && isFrameOffsetLegal(MI, Offset))
00579     return false;
00580 
00581   // The offset likely isn't legal, we want to allocate a virtual base register.
00582   return true;
00583 }
00584 
00585 /// materializeFrameBaseRegister - Insert defining instruction(s) for BaseReg to
00586 /// be a pointer to FrameIdx at the beginning of the basic block.
00587 void ARMBaseRegisterInfo::
00588 materializeFrameBaseRegister(MachineBasicBlock *MBB,
00589                              unsigned BaseReg, int FrameIdx,
00590                              int64_t Offset) const {
00591   ARMFunctionInfo *AFI = MBB->getParent()->getInfo<ARMFunctionInfo>();
00592   unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri :
00593     (AFI->isThumb1OnlyFunction() ? ARM::tADDrSPi : ARM::t2ADDri);
00594 
00595   MachineBasicBlock::iterator Ins = MBB->begin();
00596   DebugLoc DL;                  // Defaults to "unknown"
00597   if (Ins != MBB->end())
00598     DL = Ins->getDebugLoc();
00599 
00600   const MachineFunction &MF = *MBB->getParent();
00601   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
00602   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
00603   const MCInstrDesc &MCID = TII.get(ADDriOpc);
00604   MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
00605 
00606   MachineInstrBuilder MIB = AddDefaultPred(BuildMI(*MBB, Ins, DL, MCID, BaseReg)
00607     .addFrameIndex(FrameIdx).addImm(Offset));
00608 
00609   if (!AFI->isThumb1OnlyFunction())
00610     AddDefaultCC(MIB);
00611 }
00612 
00613 void ARMBaseRegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg,
00614                                             int64_t Offset) const {
00615   MachineBasicBlock &MBB = *MI.getParent();
00616   MachineFunction &MF = *MBB.getParent();
00617   const ARMBaseInstrInfo &TII =
00618       *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
00619   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
00620   int Off = Offset; // ARM doesn't need the general 64-bit offsets
00621   unsigned i = 0;
00622 
00623   assert(!AFI->isThumb1OnlyFunction() &&
00624          "This resolveFrameIndex does not support Thumb1!");
00625 
00626   while (!MI.getOperand(i).isFI()) {
00627     ++i;
00628     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
00629   }
00630   bool Done = false;
00631   if (!AFI->isThumbFunction())
00632     Done = rewriteARMFrameIndex(MI, i, BaseReg, Off, TII);
00633   else {
00634     assert(AFI->isThumb2Function());
00635     Done = rewriteT2FrameIndex(MI, i, BaseReg, Off, TII);
00636   }
00637   assert (Done && "Unable to resolve frame index!");
00638   (void)Done;
00639 }
00640 
00641 bool ARMBaseRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
00642                                              int64_t Offset) const {
00643   const MCInstrDesc &Desc = MI->getDesc();
00644   unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
00645   unsigned i = 0;
00646 
00647   while (!MI->getOperand(i).isFI()) {
00648     ++i;
00649     assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
00650   }
00651 
00652   // AddrMode4 and AddrMode6 cannot handle any offset.
00653   if (AddrMode == ARMII::AddrMode4 || AddrMode == ARMII::AddrMode6)
00654     return Offset == 0;
00655 
00656   unsigned NumBits = 0;
00657   unsigned Scale = 1;
00658   bool isSigned = true;
00659   switch (AddrMode) {
00660   case ARMII::AddrModeT2_i8:
00661   case ARMII::AddrModeT2_i12:
00662     // i8 supports only negative, and i12 supports only positive, so
00663     // based on Offset sign, consider the appropriate instruction
00664     Scale = 1;
00665     if (Offset < 0) {
00666       NumBits = 8;
00667       Offset = -Offset;
00668     } else {
00669       NumBits = 12;
00670     }
00671     break;
00672   case ARMII::AddrMode5:
00673     // VFP address mode.
00674     NumBits = 8;
00675     Scale = 4;
00676     break;
00677   case ARMII::AddrMode_i12:
00678   case ARMII::AddrMode2:
00679     NumBits = 12;
00680     break;
00681   case ARMII::AddrMode3:
00682     NumBits = 8;
00683     break;
00684   case ARMII::AddrModeT1_s:
00685     NumBits = 5;
00686     Scale = 4;
00687     isSigned = false;
00688     break;
00689   default:
00690     llvm_unreachable("Unsupported addressing mode!");
00691   }
00692 
00693   Offset += getFrameIndexInstrOffset(MI, i);
00694   // Make sure the offset is encodable for instructions that scale the
00695   // immediate.
00696   if ((Offset & (Scale-1)) != 0)
00697     return false;
00698 
00699   if (isSigned && Offset < 0)
00700     Offset = -Offset;
00701 
00702   unsigned Mask = (1 << NumBits) - 1;
00703   if ((unsigned)Offset <= Mask * Scale)
00704     return true;
00705 
00706   return false;
00707 }
00708 
00709 void
00710 ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
00711                                          int SPAdj, unsigned FIOperandNum,
00712                                          RegScavenger *RS) const {
00713   MachineInstr &MI = *II;
00714   MachineBasicBlock &MBB = *MI.getParent();
00715   MachineFunction &MF = *MBB.getParent();
00716   const ARMBaseInstrInfo &TII =
00717       *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
00718   const ARMFrameLowering *TFI = static_cast<const ARMFrameLowering *>(
00719       MF.getSubtarget().getFrameLowering());
00720   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
00721   assert(!AFI->isThumb1OnlyFunction() &&
00722          "This eliminateFrameIndex does not support Thumb1!");
00723   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
00724   unsigned FrameReg;
00725 
00726   int Offset = TFI->ResolveFrameIndexReference(MF, FrameIndex, FrameReg, SPAdj);
00727 
00728   // PEI::scavengeFrameVirtualRegs() cannot accurately track SPAdj because the
00729   // call frame setup/destroy instructions have already been eliminated.  That
00730   // means the stack pointer cannot be used to access the emergency spill slot
00731   // when !hasReservedCallFrame().
00732 #ifndef NDEBUG
00733   if (RS && FrameReg == ARM::SP && RS->isScavengingFrameIndex(FrameIndex)){
00734     assert(TFI->hasReservedCallFrame(MF) &&
00735            "Cannot use SP to access the emergency spill slot in "
00736            "functions without a reserved call frame");
00737     assert(!MF.getFrameInfo()->hasVarSizedObjects() &&
00738            "Cannot use SP to access the emergency spill slot in "
00739            "functions with variable sized frame objects");
00740   }
00741 #endif // NDEBUG
00742 
00743   assert(!MI.isDebugValue() && "DBG_VALUEs should be handled in target-independent code");
00744 
00745   // Modify MI as necessary to handle as much of 'Offset' as possible
00746   bool Done = false;
00747   if (!AFI->isThumbFunction())
00748     Done = rewriteARMFrameIndex(MI, FIOperandNum, FrameReg, Offset, TII);
00749   else {
00750     assert(AFI->isThumb2Function());
00751     Done = rewriteT2FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII);
00752   }
00753   if (Done)
00754     return;
00755 
00756   // If we get here, the immediate doesn't fit into the instruction.  We folded
00757   // as much as possible above, handle the rest, providing a register that is
00758   // SP+LargeImm.
00759   assert((Offset ||
00760           (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4 ||
00761           (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode6) &&
00762          "This code isn't needed if offset already handled!");
00763 
00764   unsigned ScratchReg = 0;
00765   int PIdx = MI.findFirstPredOperandIdx();
00766   ARMCC::CondCodes Pred = (PIdx == -1)
00767     ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
00768   unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
00769   if (Offset == 0)
00770     // Must be addrmode4/6.
00771     MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false, false, false);
00772   else {
00773     ScratchReg = MF.getRegInfo().createVirtualRegister(&ARM::GPRRegClass);
00774     if (!AFI->isThumbFunction())
00775       emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
00776                               Offset, Pred, PredReg, TII);
00777     else {
00778       assert(AFI->isThumb2Function());
00779       emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
00780                              Offset, Pred, PredReg, TII);
00781     }
00782     // Update the original instruction to use the scratch register.
00783     MI.getOperand(FIOperandNum).ChangeToRegister(ScratchReg, false, false,true);
00784   }
00785 }
00786 
00787 bool ARMBaseRegisterInfo::shouldCoalesce(MachineInstr *MI,
00788                                   const TargetRegisterClass *SrcRC,
00789                                   unsigned SubReg,
00790                                   const TargetRegisterClass *DstRC,
00791                                   unsigned DstSubReg,
00792                                   const TargetRegisterClass *NewRC) const {
00793   auto MBB = MI->getParent();
00794   auto MF = MBB->getParent();
00795   const MachineRegisterInfo &MRI = MF->getRegInfo();
00796   // If not copying into a sub-register this should be ok because we shouldn't
00797   // need to split the reg.
00798   if (!DstSubReg)
00799     return true;
00800   // Small registers don't frequently cause a problem, so we can coalesce them.
00801   if (NewRC->getSize() < 32 && DstRC->getSize() < 32 && SrcRC->getSize() < 32)
00802     return true;
00803 
00804   auto NewRCWeight =
00805               MRI.getTargetRegisterInfo()->getRegClassWeight(NewRC);
00806   auto SrcRCWeight =
00807               MRI.getTargetRegisterInfo()->getRegClassWeight(SrcRC);
00808   auto DstRCWeight =
00809               MRI.getTargetRegisterInfo()->getRegClassWeight(DstRC);
00810   // If the source register class is more expensive than the destination, the
00811   // coalescing is probably profitable.
00812   if (SrcRCWeight.RegWeight > NewRCWeight.RegWeight)
00813     return true;
00814   if (DstRCWeight.RegWeight > NewRCWeight.RegWeight)
00815     return true;
00816 
00817   // If the register allocator isn't constrained, we can always allow coalescing
00818   // unfortunately we don't know yet if we will be constrained.
00819   // The goal of this heuristic is to restrict how many expensive registers
00820   // we allow to coalesce in a given basic block.
00821   auto AFI = MF->getInfo<ARMFunctionInfo>();
00822   auto It = AFI->getCoalescedWeight(MBB);
00823 
00824   DEBUG(dbgs() << "\tARM::shouldCoalesce - Coalesced Weight: "
00825     << It->second << "\n");
00826   DEBUG(dbgs() << "\tARM::shouldCoalesce - Reg Weight: "
00827     << NewRCWeight.RegWeight << "\n");
00828 
00829   // This number is the largest round number that which meets the criteria:
00830   //  (1) addresses PR18825
00831   //  (2) generates better code in some test cases (like vldm-shed-a9.ll)
00832   //  (3) Doesn't regress any test cases (in-tree, test-suite, and SPEC)
00833   // In practice the SizeMultiplier will only factor in for straight line code
00834   // that uses a lot of NEON vectors, which isn't terribly common.
00835   unsigned SizeMultiplier = MBB->size()/100;
00836   SizeMultiplier = SizeMultiplier ? SizeMultiplier : 1;
00837   if (It->second < NewRCWeight.WeightLimit * SizeMultiplier) {
00838     It->second += NewRCWeight.RegWeight;
00839     return true;
00840   }
00841   return false;
00842 }