LLVM API Documentation

Thumb1FrameLowering.cpp
Go to the documentation of this file.
00001 //===-- Thumb1FrameLowering.cpp - Thumb1 Frame 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 Thumb1 implementation of TargetFrameLowering class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "Thumb1FrameLowering.h"
00015 #include "ARMMachineFunctionInfo.h"
00016 #include "llvm/CodeGen/MachineFrameInfo.h"
00017 #include "llvm/CodeGen/MachineFunction.h"
00018 #include "llvm/CodeGen/MachineInstrBuilder.h"
00019 #include "llvm/CodeGen/MachineModuleInfo.h"
00020 #include "llvm/CodeGen/MachineRegisterInfo.h"
00021 
00022 using namespace llvm;
00023 
00024 Thumb1FrameLowering::Thumb1FrameLowering(const ARMSubtarget &sti)
00025     : ARMFrameLowering(sti) {}
00026 
00027 bool Thumb1FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const{
00028   const MachineFrameInfo *FFI = MF.getFrameInfo();
00029   unsigned CFSize = FFI->getMaxCallFrameSize();
00030   // It's not always a good idea to include the call frame as part of the
00031   // stack frame. ARM (especially Thumb) has small immediate offset to
00032   // address the stack frame. So a large call frame can cause poor codegen
00033   // and may even makes it impossible to scavenge a register.
00034   if (CFSize >= ((1 << 8) - 1) * 4 / 2) // Half of imm8 * 4
00035     return false;
00036 
00037   return !MF.getFrameInfo()->hasVarSizedObjects();
00038 }
00039 
00040 static void
00041 emitSPUpdate(MachineBasicBlock &MBB,
00042              MachineBasicBlock::iterator &MBBI,
00043              const TargetInstrInfo &TII, DebugLoc dl,
00044              const Thumb1RegisterInfo &MRI,
00045              int NumBytes, unsigned MIFlags = MachineInstr::NoFlags)  {
00046   emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, TII,
00047                             MRI, MIFlags);
00048 }
00049 
00050 
00051 void Thumb1FrameLowering::
00052 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
00053                               MachineBasicBlock::iterator I) const {
00054   const Thumb1InstrInfo &TII =
00055       *static_cast<const Thumb1InstrInfo *>(MF.getSubtarget().getInstrInfo());
00056   const Thumb1RegisterInfo *RegInfo = static_cast<const Thumb1RegisterInfo *>(
00057       MF.getSubtarget().getRegisterInfo());
00058   if (!hasReservedCallFrame(MF)) {
00059     // If we have alloca, convert as follows:
00060     // ADJCALLSTACKDOWN -> sub, sp, sp, amount
00061     // ADJCALLSTACKUP   -> add, sp, sp, amount
00062     MachineInstr *Old = I;
00063     DebugLoc dl = Old->getDebugLoc();
00064     unsigned Amount = Old->getOperand(0).getImm();
00065     if (Amount != 0) {
00066       // We need to keep the stack aligned properly.  To do this, we round the
00067       // amount of space needed for the outgoing arguments up to the next
00068       // alignment boundary.
00069       unsigned Align = getStackAlignment();
00070       Amount = (Amount+Align-1)/Align*Align;
00071 
00072       // Replace the pseudo instruction with a new instruction...
00073       unsigned Opc = Old->getOpcode();
00074       if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
00075         emitSPUpdate(MBB, I, TII, dl, *RegInfo, -Amount);
00076       } else {
00077         assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
00078         emitSPUpdate(MBB, I, TII, dl, *RegInfo, Amount);
00079       }
00080     }
00081   }
00082   MBB.erase(I);
00083 }
00084 
00085 void Thumb1FrameLowering::emitPrologue(MachineFunction &MF) const {
00086   MachineBasicBlock &MBB = MF.front();
00087   MachineBasicBlock::iterator MBBI = MBB.begin();
00088   MachineFrameInfo  *MFI = MF.getFrameInfo();
00089   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
00090   MachineModuleInfo &MMI = MF.getMMI();
00091   const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
00092   const Thumb1RegisterInfo *RegInfo = static_cast<const Thumb1RegisterInfo *>(
00093       MF.getSubtarget().getRegisterInfo());
00094   const Thumb1InstrInfo &TII =
00095       *static_cast<const Thumb1InstrInfo *>(MF.getSubtarget().getInstrInfo());
00096 
00097   unsigned Align = MF.getTarget()
00098                        .getSubtargetImpl()
00099                        ->getFrameLowering()
00100                        ->getStackAlignment();
00101   unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align);
00102   unsigned NumBytes = MFI->getStackSize();
00103   assert(NumBytes >= ArgRegsSaveSize &&
00104          "ArgRegsSaveSize is included in NumBytes");
00105   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
00106   DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
00107   unsigned FramePtr = RegInfo->getFrameRegister(MF);
00108   unsigned BasePtr = RegInfo->getBaseRegister();
00109   int CFAOffset = 0;
00110 
00111   // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
00112   NumBytes = (NumBytes + 3) & ~3;
00113   MFI->setStackSize(NumBytes);
00114 
00115   // Determine the sizes of each callee-save spill areas and record which frame
00116   // belongs to which callee-save spill areas.
00117   unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
00118   int FramePtrSpillFI = 0;
00119 
00120   if (ArgRegsSaveSize) {
00121     emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -ArgRegsSaveSize,
00122                  MachineInstr::FrameSetup);
00123     CFAOffset -= ArgRegsSaveSize;
00124     unsigned CFIIndex = MMI.addFrameInst(
00125         MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
00126     BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
00127         .addCFIIndex(CFIIndex);
00128   }
00129 
00130   if (!AFI->hasStackFrame()) {
00131     if (NumBytes - ArgRegsSaveSize != 0) {
00132       emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -(NumBytes - ArgRegsSaveSize),
00133                    MachineInstr::FrameSetup);
00134       CFAOffset -= NumBytes - ArgRegsSaveSize;
00135       unsigned CFIIndex = MMI.addFrameInst(
00136           MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
00137       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
00138           .addCFIIndex(CFIIndex);
00139     }
00140     return;
00141   }
00142 
00143   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
00144     unsigned Reg = CSI[i].getReg();
00145     int FI = CSI[i].getFrameIdx();
00146     switch (Reg) {
00147     case ARM::R8:
00148     case ARM::R9:
00149     case ARM::R10:
00150     case ARM::R11:
00151       if (STI.isTargetMachO()) {
00152         GPRCS2Size += 4;
00153         break;
00154       }
00155       // fallthrough
00156     case ARM::R4:
00157     case ARM::R5:
00158     case ARM::R6:
00159     case ARM::R7:
00160     case ARM::LR:
00161       if (Reg == FramePtr)
00162         FramePtrSpillFI = FI;
00163       GPRCS1Size += 4;
00164       break;
00165     default:
00166       DPRCSSize += 8;
00167     }
00168   }
00169 
00170   if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
00171     ++MBBI;
00172     if (MBBI != MBB.end())
00173       dl = MBBI->getDebugLoc();
00174   }
00175 
00176   // Determine starting offsets of spill areas.
00177   unsigned DPRCSOffset  = NumBytes - ArgRegsSaveSize - (GPRCS1Size + GPRCS2Size + DPRCSSize);
00178   unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
00179   unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
00180   bool HasFP = hasFP(MF);
00181   if (HasFP)
00182     AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
00183                                 NumBytes);
00184   AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
00185   AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
00186   AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
00187   NumBytes = DPRCSOffset;
00188 
00189   int FramePtrOffsetInBlock = 0;
00190   unsigned adjustedGPRCS1Size = GPRCS1Size;
00191   if (tryFoldSPUpdateIntoPushPop(STI, MF, std::prev(MBBI), NumBytes)) {
00192     FramePtrOffsetInBlock = NumBytes;
00193     adjustedGPRCS1Size += NumBytes;
00194     NumBytes = 0;
00195   }
00196 
00197   if (adjustedGPRCS1Size) {
00198     CFAOffset -= adjustedGPRCS1Size;
00199     unsigned CFIIndex = MMI.addFrameInst(
00200         MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
00201     BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
00202         .addCFIIndex(CFIIndex);
00203   }
00204   for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
00205          E = CSI.end(); I != E; ++I) {
00206     unsigned Reg = I->getReg();
00207     int FI = I->getFrameIdx();
00208     switch (Reg) {
00209     case ARM::R8:
00210     case ARM::R9:
00211     case ARM::R10:
00212     case ARM::R11:
00213     case ARM::R12:
00214       if (STI.isTargetMachO())
00215         break;
00216       // fallthough
00217     case ARM::R0:
00218     case ARM::R1:
00219     case ARM::R2:
00220     case ARM::R3:
00221     case ARM::R4:
00222     case ARM::R5:
00223     case ARM::R6:
00224     case ARM::R7:
00225     case ARM::LR:
00226       unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
00227           nullptr, MRI->getDwarfRegNum(Reg, true), MFI->getObjectOffset(FI)));
00228       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
00229           .addCFIIndex(CFIIndex);
00230       break;
00231     }
00232   }
00233 
00234 
00235   // Adjust FP so it point to the stack slot that contains the previous FP.
00236   if (HasFP) {
00237     FramePtrOffsetInBlock += MFI->getObjectOffset(FramePtrSpillFI)
00238                          + GPRCS1Size + ArgRegsSaveSize;
00239     AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDrSPi), FramePtr)
00240       .addReg(ARM::SP).addImm(FramePtrOffsetInBlock / 4)
00241       .setMIFlags(MachineInstr::FrameSetup));
00242     if(FramePtrOffsetInBlock) {
00243       CFAOffset += FramePtrOffsetInBlock;
00244       unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfa(
00245           nullptr, MRI->getDwarfRegNum(FramePtr, true), CFAOffset));
00246       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
00247           .addCFIIndex(CFIIndex);
00248     } else {
00249       unsigned CFIIndex =
00250           MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(
00251               nullptr, MRI->getDwarfRegNum(FramePtr, true)));
00252       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
00253           .addCFIIndex(CFIIndex);
00254     }
00255     if (NumBytes > 508)
00256       // If offset is > 508 then sp cannot be adjusted in a single instruction,
00257       // try restoring from fp instead.
00258       AFI->setShouldRestoreSPFromFP(true);
00259   }
00260 
00261   if (NumBytes) {
00262     // Insert it after all the callee-save spills.
00263     emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes,
00264                  MachineInstr::FrameSetup);
00265     if (!HasFP) {
00266       CFAOffset -= NumBytes;
00267       unsigned CFIIndex = MMI.addFrameInst(
00268           MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
00269       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
00270           .addCFIIndex(CFIIndex);
00271     }
00272   }
00273 
00274   if (STI.isTargetELF() && HasFP)
00275     MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
00276                              AFI->getFramePtrSpillOffset());
00277 
00278   AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
00279   AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
00280   AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
00281 
00282   // Thumb1 does not currently support dynamic stack realignment.  Report a
00283   // fatal error rather then silently generate bad code.
00284   if (RegInfo->needsStackRealignment(MF))
00285       report_fatal_error("Dynamic stack realignment not supported for thumb1.");
00286 
00287   // If we need a base pointer, set it up here. It's whatever the value
00288   // of the stack pointer is at this point. Any variable size objects
00289   // will be allocated after this, so we can still use the base pointer
00290   // to reference locals.
00291   if (RegInfo->hasBasePointer(MF))
00292     AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), BasePtr)
00293                    .addReg(ARM::SP));
00294 
00295   // If the frame has variable sized objects then the epilogue must restore
00296   // the sp from fp. We can assume there's an FP here since hasFP already
00297   // checks for hasVarSizedObjects.
00298   if (MFI->hasVarSizedObjects())
00299     AFI->setShouldRestoreSPFromFP(true);
00300 }
00301 
00302 static bool isCSRestore(MachineInstr *MI, const MCPhysReg *CSRegs) {
00303   if (MI->getOpcode() == ARM::tLDRspi &&
00304       MI->getOperand(1).isFI() &&
00305       isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs))
00306     return true;
00307   else if (MI->getOpcode() == ARM::tPOP) {
00308     // The first two operands are predicates. The last two are
00309     // imp-def and imp-use of SP. Check everything in between.
00310     for (int i = 2, e = MI->getNumOperands() - 2; i != e; ++i)
00311       if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
00312         return false;
00313     return true;
00314   }
00315   return false;
00316 }
00317 
00318 void Thumb1FrameLowering::emitEpilogue(MachineFunction &MF,
00319                                    MachineBasicBlock &MBB) const {
00320   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
00321   assert((MBBI->getOpcode() == ARM::tBX_RET ||
00322           MBBI->getOpcode() == ARM::tPOP_RET) &&
00323          "Can only insert epilog into returning blocks");
00324   DebugLoc dl = MBBI->getDebugLoc();
00325   MachineFrameInfo *MFI = MF.getFrameInfo();
00326   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
00327   const Thumb1RegisterInfo *RegInfo = static_cast<const Thumb1RegisterInfo *>(
00328       MF.getSubtarget().getRegisterInfo());
00329   const Thumb1InstrInfo &TII =
00330       *static_cast<const Thumb1InstrInfo *>(MF.getSubtarget().getInstrInfo());
00331 
00332   unsigned Align = MF.getTarget()
00333                        .getSubtargetImpl()
00334                        ->getFrameLowering()
00335                        ->getStackAlignment();
00336   unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align);
00337   int NumBytes = (int)MFI->getStackSize();
00338   assert((unsigned)NumBytes >= ArgRegsSaveSize &&
00339          "ArgRegsSaveSize is included in NumBytes");
00340   const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs();
00341   unsigned FramePtr = RegInfo->getFrameRegister(MF);
00342 
00343   if (!AFI->hasStackFrame()) {
00344     if (NumBytes - ArgRegsSaveSize != 0)
00345       emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes - ArgRegsSaveSize);
00346   } else {
00347     // Unwind MBBI to point to first LDR / VLDRD.
00348     if (MBBI != MBB.begin()) {
00349       do
00350         --MBBI;
00351       while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
00352       if (!isCSRestore(MBBI, CSRegs))
00353         ++MBBI;
00354     }
00355 
00356     // Move SP to start of FP callee save spill area.
00357     NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
00358                  AFI->getGPRCalleeSavedArea2Size() +
00359                  AFI->getDPRCalleeSavedAreaSize() +
00360                  ArgRegsSaveSize);
00361 
00362     if (AFI->shouldRestoreSPFromFP()) {
00363       NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
00364       // Reset SP based on frame pointer only if the stack frame extends beyond
00365       // frame pointer stack slot, the target is ELF and the function has FP, or
00366       // the target uses var sized objects.
00367       if (NumBytes) {
00368         assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) &&
00369                "No scratch register to restore SP from FP!");
00370         emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
00371                                   TII, *RegInfo);
00372         AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
00373                                ARM::SP)
00374           .addReg(ARM::R4));
00375       } else
00376         AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
00377                                ARM::SP)
00378           .addReg(FramePtr));
00379     } else {
00380       if (MBBI->getOpcode() == ARM::tBX_RET &&
00381           &MBB.front() != MBBI &&
00382           std::prev(MBBI)->getOpcode() == ARM::tPOP) {
00383         MachineBasicBlock::iterator PMBBI = std::prev(MBBI);
00384         if (!tryFoldSPUpdateIntoPushPop(STI, MF, PMBBI, NumBytes))
00385           emitSPUpdate(MBB, PMBBI, TII, dl, *RegInfo, NumBytes);
00386       } else if (!tryFoldSPUpdateIntoPushPop(STI, MF, MBBI, NumBytes))
00387         emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
00388     }
00389   }
00390 
00391   bool IsV4PopReturn = false;
00392   for (const CalleeSavedInfo &CSI : MFI->getCalleeSavedInfo())
00393     if (CSI.getReg() == ARM::LR)
00394       IsV4PopReturn = true;
00395   IsV4PopReturn &= STI.hasV4TOps() && !STI.hasV5TOps();
00396 
00397   // Unlike T2 and ARM mode, the T1 pop instruction cannot restore
00398   // to LR, and we can't pop the value directly to the PC since
00399   // we need to update the SP after popping the value. So instead
00400   // we have to emit:
00401   //   POP {r3}
00402   //   ADD sp, #offset
00403   //   BX r3
00404   // If this would clobber a return value, then generate this sequence instead:
00405   //   MOV ip, r3
00406   //   POP {r3}
00407   //   ADD sp, #offset
00408   //   MOV lr, r3
00409   //   MOV r3, ip
00410   //   BX lr
00411   if (ArgRegsSaveSize || IsV4PopReturn) {
00412     // Get the last instruction, tBX_RET
00413     MBBI = MBB.getLastNonDebugInstr();
00414     assert (MBBI->getOpcode() == ARM::tBX_RET);
00415     DebugLoc dl = MBBI->getDebugLoc();
00416 
00417     if (AFI->getReturnRegsCount() <= 3) {
00418       // Epilogue: pop saved LR to R3 and branch off it. 
00419       AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP)))
00420         .addReg(ARM::R3, RegState::Define);
00421 
00422       emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, ArgRegsSaveSize);
00423 
00424       MachineInstrBuilder MIB =
00425         BuildMI(MBB, MBBI, dl, TII.get(ARM::tBX))
00426         .addReg(ARM::R3, RegState::Kill);
00427       AddDefaultPred(MIB);
00428       MIB.copyImplicitOps(&*MBBI);
00429       // erase the old tBX_RET instruction
00430       MBB.erase(MBBI);
00431     } else {
00432       AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
00433         .addReg(ARM::R12, RegState::Define)
00434         .addReg(ARM::R3, RegState::Kill));
00435 
00436       AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP)))
00437         .addReg(ARM::R3, RegState::Define);
00438 
00439       emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, ArgRegsSaveSize);
00440 
00441       AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
00442         .addReg(ARM::LR, RegState::Define)
00443         .addReg(ARM::R3, RegState::Kill));
00444 
00445       AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
00446         .addReg(ARM::R3, RegState::Define)
00447         .addReg(ARM::R12, RegState::Kill));
00448       // Keep the tBX_RET instruction
00449     }
00450   }
00451 }
00452 
00453 bool Thumb1FrameLowering::
00454 spillCalleeSavedRegisters(MachineBasicBlock &MBB,
00455                           MachineBasicBlock::iterator MI,
00456                           const std::vector<CalleeSavedInfo> &CSI,
00457                           const TargetRegisterInfo *TRI) const {
00458   if (CSI.empty())
00459     return false;
00460 
00461   DebugLoc DL;
00462   MachineFunction &MF = *MBB.getParent();
00463   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
00464 
00465   if (MI != MBB.end()) DL = MI->getDebugLoc();
00466 
00467   MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(ARM::tPUSH));
00468   AddDefaultPred(MIB);
00469   for (unsigned i = CSI.size(); i != 0; --i) {
00470     unsigned Reg = CSI[i-1].getReg();
00471     bool isKill = true;
00472 
00473     // Add the callee-saved register as live-in unless it's LR and
00474     // @llvm.returnaddress is called. If LR is returned for @llvm.returnaddress
00475     // then it's already added to the function and entry block live-in sets.
00476     if (Reg == ARM::LR) {
00477       MachineFunction &MF = *MBB.getParent();
00478       if (MF.getFrameInfo()->isReturnAddressTaken() &&
00479           MF.getRegInfo().isLiveIn(Reg))
00480         isKill = false;
00481     }
00482 
00483     if (isKill)
00484       MBB.addLiveIn(Reg);
00485 
00486     MIB.addReg(Reg, getKillRegState(isKill));
00487   }
00488   MIB.setMIFlags(MachineInstr::FrameSetup);
00489   return true;
00490 }
00491 
00492 bool Thumb1FrameLowering::
00493 restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
00494                             MachineBasicBlock::iterator MI,
00495                             const std::vector<CalleeSavedInfo> &CSI,
00496                             const TargetRegisterInfo *TRI) const {
00497   if (CSI.empty())
00498     return false;
00499 
00500   MachineFunction &MF = *MBB.getParent();
00501   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
00502   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
00503 
00504   bool isVarArg = AFI->getArgRegsSaveSize() > 0;
00505   DebugLoc DL = MI->getDebugLoc();
00506   MachineInstrBuilder MIB = BuildMI(MF, DL, TII.get(ARM::tPOP));
00507   AddDefaultPred(MIB);
00508 
00509   bool NumRegs = false;
00510   for (unsigned i = CSI.size(); i != 0; --i) {
00511     unsigned Reg = CSI[i-1].getReg();
00512     if (Reg == ARM::LR) {
00513       // Special epilogue for vararg functions. See emitEpilogue
00514       if (isVarArg)
00515         continue;
00516       // ARMv4T requires BX, see emitEpilogue
00517       if (STI.hasV4TOps() && !STI.hasV5TOps())
00518         continue;
00519       Reg = ARM::PC;
00520       (*MIB).setDesc(TII.get(ARM::tPOP_RET));
00521       MIB.copyImplicitOps(&*MI);
00522       MI = MBB.erase(MI);
00523     }
00524     MIB.addReg(Reg, getDefRegState(true));
00525     NumRegs = true;
00526   }
00527 
00528   // It's illegal to emit pop instruction without operands.
00529   if (NumRegs)
00530     MBB.insert(MI, &*MIB);
00531   else
00532     MF.DeleteMachineInstr(MIB);
00533 
00534   return true;
00535 }