1 //===-- Thumb1FrameLowering.cpp - Thumb1 Frame Information ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the Thumb1 implementation of TargetFrameLowering class.
12 //===----------------------------------------------------------------------===//
14 #include "Thumb1FrameLowering.h"
15 #include "ARMMachineFunctionInfo.h"
16 #include "llvm/CodeGen/LivePhysRegs.h"
17 #include "llvm/CodeGen/MachineFrameInfo.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineModuleInfo.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 Thumb1FrameLowering::Thumb1FrameLowering(const ARMSubtarget &sti)
26 : ARMFrameLowering(sti) {}
28 bool Thumb1FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const{
29 const MachineFrameInfo *FFI = MF.getFrameInfo();
30 unsigned CFSize = FFI->getMaxCallFrameSize();
31 // It's not always a good idea to include the call frame as part of the
32 // stack frame. ARM (especially Thumb) has small immediate offset to
33 // address the stack frame. So a large call frame can cause poor codegen
34 // and may even makes it impossible to scavenge a register.
35 if (CFSize >= ((1 << 8) - 1) * 4 / 2) // Half of imm8 * 4
38 return !MF.getFrameInfo()->hasVarSizedObjects();
42 emitSPUpdate(MachineBasicBlock &MBB,
43 MachineBasicBlock::iterator &MBBI,
44 const TargetInstrInfo &TII, DebugLoc dl,
45 const ThumbRegisterInfo &MRI,
46 int NumBytes, unsigned MIFlags = MachineInstr::NoFlags) {
47 emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, TII,
52 void Thumb1FrameLowering::
53 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
54 MachineBasicBlock::iterator I) const {
55 const Thumb1InstrInfo &TII =
56 *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
57 const ThumbRegisterInfo *RegInfo =
58 static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
59 if (!hasReservedCallFrame(MF)) {
60 // If we have alloca, convert as follows:
61 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
62 // ADJCALLSTACKUP -> add, sp, sp, amount
63 MachineInstr *Old = I;
64 DebugLoc dl = Old->getDebugLoc();
65 unsigned Amount = Old->getOperand(0).getImm();
67 // We need to keep the stack aligned properly. To do this, we round the
68 // amount of space needed for the outgoing arguments up to the next
69 // alignment boundary.
70 unsigned Align = getStackAlignment();
71 Amount = (Amount+Align-1)/Align*Align;
73 // Replace the pseudo instruction with a new instruction...
74 unsigned Opc = Old->getOpcode();
75 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
76 emitSPUpdate(MBB, I, TII, dl, *RegInfo, -Amount);
78 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
79 emitSPUpdate(MBB, I, TII, dl, *RegInfo, Amount);
86 void Thumb1FrameLowering::emitPrologue(MachineFunction &MF,
87 MachineBasicBlock &MBB) const {
88 MachineBasicBlock::iterator MBBI = MBB.begin();
89 MachineFrameInfo *MFI = MF.getFrameInfo();
90 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
91 MachineModuleInfo &MMI = MF.getMMI();
92 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
93 const ThumbRegisterInfo *RegInfo =
94 static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
95 const Thumb1InstrInfo &TII =
96 *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
98 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
99 unsigned NumBytes = MFI->getStackSize();
100 assert(NumBytes >= ArgRegsSaveSize &&
101 "ArgRegsSaveSize is included in NumBytes");
102 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
103 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
104 unsigned FramePtr = RegInfo->getFrameRegister(MF);
105 unsigned BasePtr = RegInfo->getBaseRegister();
108 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
109 NumBytes = (NumBytes + 3) & ~3;
110 MFI->setStackSize(NumBytes);
112 // Determine the sizes of each callee-save spill areas and record which frame
113 // belongs to which callee-save spill areas.
114 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
115 int FramePtrSpillFI = 0;
117 if (ArgRegsSaveSize) {
118 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -ArgRegsSaveSize,
119 MachineInstr::FrameSetup);
120 CFAOffset -= ArgRegsSaveSize;
121 unsigned CFIIndex = MMI.addFrameInst(
122 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
123 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
124 .addCFIIndex(CFIIndex)
125 .setMIFlags(MachineInstr::FrameSetup);
128 if (!AFI->hasStackFrame()) {
129 if (NumBytes - ArgRegsSaveSize != 0) {
130 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -(NumBytes - ArgRegsSaveSize),
131 MachineInstr::FrameSetup);
132 CFAOffset -= NumBytes - ArgRegsSaveSize;
133 unsigned CFIIndex = MMI.addFrameInst(
134 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
135 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
136 .addCFIIndex(CFIIndex)
137 .setMIFlags(MachineInstr::FrameSetup);
142 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
143 unsigned Reg = CSI[i].getReg();
144 int FI = CSI[i].getFrameIdx();
150 if (STI.isTargetMachO()) {
161 FramePtrSpillFI = FI;
169 if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
171 if (MBBI != MBB.end())
172 dl = MBBI->getDebugLoc();
175 // Determine starting offsets of spill areas.
176 unsigned DPRCSOffset = NumBytes - ArgRegsSaveSize - (GPRCS1Size + GPRCS2Size + DPRCSSize);
177 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
178 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
179 bool HasFP = hasFP(MF);
181 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
183 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
184 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
185 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
186 NumBytes = DPRCSOffset;
188 int FramePtrOffsetInBlock = 0;
189 unsigned adjustedGPRCS1Size = GPRCS1Size;
190 if (tryFoldSPUpdateIntoPushPop(STI, MF, std::prev(MBBI), NumBytes)) {
191 FramePtrOffsetInBlock = NumBytes;
192 adjustedGPRCS1Size += NumBytes;
196 if (adjustedGPRCS1Size) {
197 CFAOffset -= adjustedGPRCS1Size;
198 unsigned CFIIndex = MMI.addFrameInst(
199 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
200 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
201 .addCFIIndex(CFIIndex)
202 .setMIFlags(MachineInstr::FrameSetup);
204 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
205 E = CSI.end(); I != E; ++I) {
206 unsigned Reg = I->getReg();
207 int FI = I->getFrameIdx();
214 if (STI.isTargetMachO())
226 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
227 nullptr, MRI->getDwarfRegNum(Reg, true), MFI->getObjectOffset(FI)));
228 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
229 .addCFIIndex(CFIIndex)
230 .setMIFlags(MachineInstr::FrameSetup);
236 // Adjust FP so it point to the stack slot that contains the previous FP.
238 FramePtrOffsetInBlock += MFI->getObjectOffset(FramePtrSpillFI)
239 + GPRCS1Size + ArgRegsSaveSize;
240 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDrSPi), FramePtr)
241 .addReg(ARM::SP).addImm(FramePtrOffsetInBlock / 4)
242 .setMIFlags(MachineInstr::FrameSetup));
243 if(FramePtrOffsetInBlock) {
244 CFAOffset += FramePtrOffsetInBlock;
245 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfa(
246 nullptr, MRI->getDwarfRegNum(FramePtr, true), CFAOffset));
247 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
248 .addCFIIndex(CFIIndex)
249 .setMIFlags(MachineInstr::FrameSetup);
252 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(
253 nullptr, MRI->getDwarfRegNum(FramePtr, true)));
254 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
255 .addCFIIndex(CFIIndex)
256 .setMIFlags(MachineInstr::FrameSetup);
259 // If offset is > 508 then sp cannot be adjusted in a single instruction,
260 // try restoring from fp instead.
261 AFI->setShouldRestoreSPFromFP(true);
265 // Insert it after all the callee-save spills.
266 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes,
267 MachineInstr::FrameSetup);
269 CFAOffset -= NumBytes;
270 unsigned CFIIndex = MMI.addFrameInst(
271 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
272 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
273 .addCFIIndex(CFIIndex)
274 .setMIFlags(MachineInstr::FrameSetup);
278 if (STI.isTargetELF() && HasFP)
279 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
280 AFI->getFramePtrSpillOffset());
282 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
283 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
284 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
286 // Thumb1 does not currently support dynamic stack realignment. Report a
287 // fatal error rather then silently generate bad code.
288 if (RegInfo->needsStackRealignment(MF))
289 report_fatal_error("Dynamic stack realignment not supported for thumb1.");
291 // If we need a base pointer, set it up here. It's whatever the value
292 // of the stack pointer is at this point. Any variable size objects
293 // will be allocated after this, so we can still use the base pointer
294 // to reference locals.
295 if (RegInfo->hasBasePointer(MF))
296 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), BasePtr)
299 // If the frame has variable sized objects then the epilogue must restore
300 // the sp from fp. We can assume there's an FP here since hasFP already
301 // checks for hasVarSizedObjects.
302 if (MFI->hasVarSizedObjects())
303 AFI->setShouldRestoreSPFromFP(true);
306 static bool isCSRestore(MachineInstr *MI, const MCPhysReg *CSRegs) {
307 if (MI->getOpcode() == ARM::tLDRspi &&
308 MI->getOperand(1).isFI() &&
309 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs))
311 else if (MI->getOpcode() == ARM::tPOP) {
312 // The first two operands are predicates. The last two are
313 // imp-def and imp-use of SP. Check everything in between.
314 for (int i = 2, e = MI->getNumOperands() - 2; i != e; ++i)
315 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
322 void Thumb1FrameLowering::emitEpilogue(MachineFunction &MF,
323 MachineBasicBlock &MBB) const {
324 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
325 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
326 MachineFrameInfo *MFI = MF.getFrameInfo();
327 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
328 const ThumbRegisterInfo *RegInfo =
329 static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
330 const Thumb1InstrInfo &TII =
331 *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
333 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
334 int NumBytes = (int)MFI->getStackSize();
335 assert((unsigned)NumBytes >= ArgRegsSaveSize &&
336 "ArgRegsSaveSize is included in NumBytes");
337 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
338 unsigned FramePtr = RegInfo->getFrameRegister(MF);
340 if (!AFI->hasStackFrame()) {
341 if (NumBytes - ArgRegsSaveSize != 0)
342 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes - ArgRegsSaveSize);
344 // Unwind MBBI to point to first LDR / VLDRD.
345 if (MBBI != MBB.begin()) {
348 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
349 if (!isCSRestore(MBBI, CSRegs))
353 // Move SP to start of FP callee save spill area.
354 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
355 AFI->getGPRCalleeSavedArea2Size() +
356 AFI->getDPRCalleeSavedAreaSize() +
359 if (AFI->shouldRestoreSPFromFP()) {
360 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
361 // Reset SP based on frame pointer only if the stack frame extends beyond
362 // frame pointer stack slot, the target is ELF and the function has FP, or
363 // the target uses var sized objects.
365 assert(!MFI->getPristineRegs(MF).test(ARM::R4) &&
366 "No scratch register to restore SP from FP!");
367 emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
369 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
373 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
377 if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tBX_RET &&
378 &MBB.front() != MBBI && std::prev(MBBI)->getOpcode() == ARM::tPOP) {
379 MachineBasicBlock::iterator PMBBI = std::prev(MBBI);
380 if (!tryFoldSPUpdateIntoPushPop(STI, MF, PMBBI, NumBytes))
381 emitSPUpdate(MBB, PMBBI, TII, dl, *RegInfo, NumBytes);
382 } else if (!tryFoldSPUpdateIntoPushPop(STI, MF, MBBI, NumBytes))
383 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
387 if (needPopSpecialFixUp(MF)) {
388 bool Done = emitPopSpecialFixUp(MBB, /* DoIt */ true);
390 assert(Done && "Emission of the special fixup failed!?");
394 bool Thumb1FrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const {
395 if (!needPopSpecialFixUp(*MBB.getParent()))
398 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
399 return emitPopSpecialFixUp(*TmpMBB, /* DoIt */ false);
402 bool Thumb1FrameLowering::needPopSpecialFixUp(const MachineFunction &MF) const {
403 ARMFunctionInfo *AFI =
404 const_cast<MachineFunction *>(&MF)->getInfo<ARMFunctionInfo>();
405 if (AFI->getArgRegsSaveSize())
408 bool IsV4PopReturn = false;
409 for (const CalleeSavedInfo &CSI : MF.getFrameInfo()->getCalleeSavedInfo())
410 if (CSI.getReg() == ARM::LR)
411 IsV4PopReturn = true;
412 return IsV4PopReturn && STI.hasV4TOps() && !STI.hasV5TOps();
415 bool Thumb1FrameLowering::emitPopSpecialFixUp(MachineBasicBlock &MBB,
417 MachineFunction &MF = *MBB.getParent();
418 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
419 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
420 const TargetInstrInfo &TII = *STI.getInstrInfo();
421 const ThumbRegisterInfo *RegInfo =
422 static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
424 // If MBBI is a return instruction, we may be able to directly restore
426 // This is possible if we do not need to emit any SP update.
427 // Otherwise, we need a temporary register to pop the value
428 // and copy that value into LR.
429 auto MBBI = MBB.getFirstTerminator();
430 if (!ArgRegsSaveSize && MBBI != MBB.end() &&
431 MBBI->getOpcode() == ARM::tBX_RET) {
434 MachineInstrBuilder MIB =
436 BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII.get(ARM::tPOP_RET)))
437 .addReg(ARM::PC, RegState::Define);
438 MIB.copyImplicitOps(&*MBBI);
439 // erase the old tBX_RET instruction
444 // Look for a temporary register to use.
445 // First, compute the liveness information.
446 LivePhysRegs UsedRegs(STI.getRegisterInfo());
447 UsedRegs.addLiveOuts(&MBB, /*AddPristines*/ true);
448 // The semantic of pristines changed recently and now,
449 // the callee-saved registers that are touched in the function
450 // are not part of the pristines set anymore.
451 // Add those callee-saved now.
452 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
453 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
454 for (unsigned i = 0; CSRegs[i]; ++i)
455 UsedRegs.addReg(CSRegs[i]);
457 DebugLoc dl = DebugLoc();
458 if (MBBI != MBB.end()) {
459 dl = MBBI->getDebugLoc();
460 auto InstUpToMBBI = MBB.end();
461 // The post-decrement is on purpose here.
462 // We want to have the liveness right before MBBI.
463 while (InstUpToMBBI-- != MBBI)
464 UsedRegs.stepBackward(*InstUpToMBBI);
467 // Look for a register that can be directly use in the POP.
469 // And some temporary register, just in case.
470 unsigned TemporaryReg = 0;
471 BitVector PopFriendly =
472 TRI->getAllocatableSet(MF, TRI->getRegClass(ARM::tGPRRegClassID));
473 assert(PopFriendly.any() && "No allocatable pop-friendly register?!");
474 // Rebuild the GPRs from the high registers because they are removed
475 // form the GPR reg class for thumb1.
476 BitVector GPRsNoLRSP =
477 TRI->getAllocatableSet(MF, TRI->getRegClass(ARM::hGPRRegClassID));
478 GPRsNoLRSP |= PopFriendly;
479 GPRsNoLRSP.reset(ARM::LR);
480 GPRsNoLRSP.reset(ARM::SP);
481 GPRsNoLRSP.reset(ARM::PC);
482 for (int Register = GPRsNoLRSP.find_first(); Register != -1;
483 Register = GPRsNoLRSP.find_next(Register)) {
484 if (!UsedRegs.contains(Register)) {
485 // Remember the first pop-friendly register and exit.
486 if (PopFriendly.test(Register)) {
491 // Otherwise, remember that the register will be available to
492 // save a pop-friendly register.
493 TemporaryReg = Register;
497 if (!DoIt && !PopReg && !TemporaryReg)
500 assert((PopReg || TemporaryReg) && "Cannot get LR");
503 assert(!PopReg && "Unnecessary MOV is about to be inserted");
504 PopReg = PopFriendly.find_first();
505 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
506 .addReg(TemporaryReg, RegState::Define)
507 .addReg(PopReg, RegState::Kill));
510 assert(PopReg && "Do not know how to get LR");
511 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP)))
512 .addReg(PopReg, RegState::Define);
514 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, ArgRegsSaveSize);
516 if (!TemporaryReg && MBBI != MBB.end() && MBBI->getOpcode() == ARM::tBX_RET) {
517 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(ARM::tBX))
518 .addReg(PopReg, RegState::Kill);
520 MIB.copyImplicitOps(&*MBBI);
521 // erase the old tBX_RET instruction
526 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
527 .addReg(ARM::LR, RegState::Define)
528 .addReg(PopReg, RegState::Kill));
531 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
532 .addReg(PopReg, RegState::Define)
533 .addReg(TemporaryReg, RegState::Kill));
539 bool Thumb1FrameLowering::
540 spillCalleeSavedRegisters(MachineBasicBlock &MBB,
541 MachineBasicBlock::iterator MI,
542 const std::vector<CalleeSavedInfo> &CSI,
543 const TargetRegisterInfo *TRI) const {
548 const TargetInstrInfo &TII = *STI.getInstrInfo();
550 if (MI != MBB.end()) DL = MI->getDebugLoc();
552 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(ARM::tPUSH));
554 for (unsigned i = CSI.size(); i != 0; --i) {
555 unsigned Reg = CSI[i-1].getReg();
558 // Add the callee-saved register as live-in unless it's LR and
559 // @llvm.returnaddress is called. If LR is returned for @llvm.returnaddress
560 // then it's already added to the function and entry block live-in sets.
561 if (Reg == ARM::LR) {
562 MachineFunction &MF = *MBB.getParent();
563 if (MF.getFrameInfo()->isReturnAddressTaken() &&
564 MF.getRegInfo().isLiveIn(Reg))
571 MIB.addReg(Reg, getKillRegState(isKill));
573 MIB.setMIFlags(MachineInstr::FrameSetup);
577 bool Thumb1FrameLowering::
578 restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
579 MachineBasicBlock::iterator MI,
580 const std::vector<CalleeSavedInfo> &CSI,
581 const TargetRegisterInfo *TRI) const {
585 MachineFunction &MF = *MBB.getParent();
586 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
587 const TargetInstrInfo &TII = *STI.getInstrInfo();
589 bool isVarArg = AFI->getArgRegsSaveSize() > 0;
590 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
591 MachineInstrBuilder MIB = BuildMI(MF, DL, TII.get(ARM::tPOP));
594 bool NumRegs = false;
595 for (unsigned i = CSI.size(); i != 0; --i) {
596 unsigned Reg = CSI[i-1].getReg();
597 if (Reg == ARM::LR && MBB.succ_empty()) {
598 // Special epilogue for vararg functions. See emitEpilogue
601 // ARMv4T requires BX, see emitEpilogue
602 if (STI.hasV4TOps() && !STI.hasV5TOps())
605 (*MIB).setDesc(TII.get(ARM::tPOP_RET));
607 MIB.copyImplicitOps(&*MI);
610 MIB.addReg(Reg, getDefRegState(true));
614 // It's illegal to emit pop instruction without operands.
616 MBB.insert(MI, &*MIB);
618 MF.DeleteMachineInstr(MIB);