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