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/MachineFrameInfo.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineModuleInfo.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 Thumb1FrameLowering::Thumb1FrameLowering(const ARMSubtarget &sti)
25 : ARMFrameLowering(sti) {}
27 bool Thumb1FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const{
28 const MachineFrameInfo *FFI = MF.getFrameInfo();
29 unsigned CFSize = FFI->getMaxCallFrameSize();
30 // It's not always a good idea to include the call frame as part of the
31 // stack frame. ARM (especially Thumb) has small immediate offset to
32 // address the stack frame. So a large call frame can cause poor codegen
33 // and may even makes it impossible to scavenge a register.
34 if (CFSize >= ((1 << 8) - 1) * 4 / 2) // Half of imm8 * 4
37 return !MF.getFrameInfo()->hasVarSizedObjects();
41 emitSPUpdate(MachineBasicBlock &MBB,
42 MachineBasicBlock::iterator &MBBI,
43 const TargetInstrInfo &TII, DebugLoc dl,
44 const Thumb1RegisterInfo &MRI,
45 int NumBytes, unsigned MIFlags = MachineInstr::NoFlags) {
46 emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, TII,
51 void Thumb1FrameLowering::
52 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
53 MachineBasicBlock::iterator I) const {
54 const Thumb1InstrInfo &TII =
55 *static_cast<const Thumb1InstrInfo *>(MF.getSubtarget().getInstrInfo());
56 const Thumb1RegisterInfo *RegInfo = static_cast<const Thumb1RegisterInfo *>(
57 MF.getSubtarget().getRegisterInfo());
58 if (!hasReservedCallFrame(MF)) {
59 // If we have alloca, convert as follows:
60 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
61 // ADJCALLSTACKUP -> add, sp, sp, amount
62 MachineInstr *Old = I;
63 DebugLoc dl = Old->getDebugLoc();
64 unsigned Amount = Old->getOperand(0).getImm();
66 // We need to keep the stack aligned properly. To do this, we round the
67 // amount of space needed for the outgoing arguments up to the next
68 // alignment boundary.
69 unsigned Align = getStackAlignment();
70 Amount = (Amount+Align-1)/Align*Align;
72 // Replace the pseudo instruction with a new instruction...
73 unsigned Opc = Old->getOpcode();
74 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
75 emitSPUpdate(MBB, I, TII, dl, *RegInfo, -Amount);
77 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
78 emitSPUpdate(MBB, I, TII, dl, *RegInfo, Amount);
85 void Thumb1FrameLowering::emitPrologue(MachineFunction &MF) const {
86 MachineBasicBlock &MBB = MF.front();
87 MachineBasicBlock::iterator MBBI = MBB.begin();
88 MachineFrameInfo *MFI = MF.getFrameInfo();
89 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
90 MachineModuleInfo &MMI = MF.getMMI();
91 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
92 const Thumb1RegisterInfo *RegInfo = static_cast<const Thumb1RegisterInfo *>(
93 MF.getSubtarget().getRegisterInfo());
94 const Thumb1InstrInfo &TII =
95 *static_cast<const Thumb1InstrInfo *>(MF.getSubtarget().getInstrInfo());
97 unsigned Align = MF.getTarget()
100 ->getStackAlignment();
101 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align);
102 unsigned NumBytes = MFI->getStackSize();
103 assert(NumBytes >= ArgRegsSaveSize &&
104 "ArgRegsSaveSize is included in NumBytes");
105 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
106 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
107 unsigned FramePtr = RegInfo->getFrameRegister(MF);
108 unsigned BasePtr = RegInfo->getBaseRegister();
111 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
112 NumBytes = (NumBytes + 3) & ~3;
113 MFI->setStackSize(NumBytes);
115 // Determine the sizes of each callee-save spill areas and record which frame
116 // belongs to which callee-save spill areas.
117 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
118 int FramePtrSpillFI = 0;
120 if (ArgRegsSaveSize) {
121 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -ArgRegsSaveSize,
122 MachineInstr::FrameSetup);
123 CFAOffset -= ArgRegsSaveSize;
124 unsigned CFIIndex = MMI.addFrameInst(
125 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
126 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
127 .addCFIIndex(CFIIndex);
130 if (!AFI->hasStackFrame()) {
131 if (NumBytes - ArgRegsSaveSize != 0) {
132 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -(NumBytes - ArgRegsSaveSize),
133 MachineInstr::FrameSetup);
134 CFAOffset -= NumBytes - ArgRegsSaveSize;
135 unsigned CFIIndex = MMI.addFrameInst(
136 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
137 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
138 .addCFIIndex(CFIIndex);
143 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
144 unsigned Reg = CSI[i].getReg();
145 int FI = CSI[i].getFrameIdx();
151 if (STI.isTargetMachO()) {
162 FramePtrSpillFI = FI;
170 if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
172 if (MBBI != MBB.end())
173 dl = MBBI->getDebugLoc();
176 // Determine starting offsets of spill areas.
177 unsigned DPRCSOffset = NumBytes - ArgRegsSaveSize - (GPRCS1Size + GPRCS2Size + DPRCSSize);
178 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
179 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
180 bool HasFP = hasFP(MF);
182 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
184 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
185 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
186 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
187 NumBytes = DPRCSOffset;
189 int FramePtrOffsetInBlock = 0;
190 unsigned adjustedGPRCS1Size = GPRCS1Size;
191 if (tryFoldSPUpdateIntoPushPop(STI, MF, std::prev(MBBI), NumBytes)) {
192 FramePtrOffsetInBlock = NumBytes;
193 adjustedGPRCS1Size += NumBytes;
197 if (adjustedGPRCS1Size) {
198 CFAOffset -= adjustedGPRCS1Size;
199 unsigned CFIIndex = MMI.addFrameInst(
200 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
201 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
202 .addCFIIndex(CFIIndex);
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);
235 // Adjust FP so it point to the stack slot that contains the previous FP.
237 FramePtrOffsetInBlock += MFI->getObjectOffset(FramePtrSpillFI)
238 + GPRCS1Size + ArgRegsSaveSize;
239 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDrSPi), FramePtr)
240 .addReg(ARM::SP).addImm(FramePtrOffsetInBlock / 4)
241 .setMIFlags(MachineInstr::FrameSetup));
242 if(FramePtrOffsetInBlock) {
243 CFAOffset += FramePtrOffsetInBlock;
244 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfa(
245 nullptr, MRI->getDwarfRegNum(FramePtr, true), CFAOffset));
246 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
247 .addCFIIndex(CFIIndex);
250 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(
251 nullptr, MRI->getDwarfRegNum(FramePtr, true)));
252 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
253 .addCFIIndex(CFIIndex);
256 // If offset is > 508 then sp cannot be adjusted in a single instruction,
257 // try restoring from fp instead.
258 AFI->setShouldRestoreSPFromFP(true);
262 // Insert it after all the callee-save spills.
263 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes,
264 MachineInstr::FrameSetup);
266 CFAOffset -= NumBytes;
267 unsigned CFIIndex = MMI.addFrameInst(
268 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
269 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
270 .addCFIIndex(CFIIndex);
274 if (STI.isTargetELF() && HasFP)
275 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
276 AFI->getFramePtrSpillOffset());
278 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
279 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
280 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
282 // Thumb1 does not currently support dynamic stack realignment. Report a
283 // fatal error rather then silently generate bad code.
284 if (RegInfo->needsStackRealignment(MF))
285 report_fatal_error("Dynamic stack realignment not supported for thumb1.");
287 // If we need a base pointer, set it up here. It's whatever the value
288 // of the stack pointer is at this point. Any variable size objects
289 // will be allocated after this, so we can still use the base pointer
290 // to reference locals.
291 if (RegInfo->hasBasePointer(MF))
292 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), BasePtr)
295 // If the frame has variable sized objects then the epilogue must restore
296 // the sp from fp. We can assume there's an FP here since hasFP already
297 // checks for hasVarSizedObjects.
298 if (MFI->hasVarSizedObjects())
299 AFI->setShouldRestoreSPFromFP(true);
302 static bool isCSRestore(MachineInstr *MI, const MCPhysReg *CSRegs) {
303 if (MI->getOpcode() == ARM::tLDRspi &&
304 MI->getOperand(1).isFI() &&
305 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs))
307 else if (MI->getOpcode() == ARM::tPOP) {
308 // The first two operands are predicates. The last two are
309 // imp-def and imp-use of SP. Check everything in between.
310 for (int i = 2, e = MI->getNumOperands() - 2; i != e; ++i)
311 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
318 void Thumb1FrameLowering::emitEpilogue(MachineFunction &MF,
319 MachineBasicBlock &MBB) const {
320 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
321 assert((MBBI->getOpcode() == ARM::tBX_RET ||
322 MBBI->getOpcode() == ARM::tPOP_RET) &&
323 "Can only insert epilog into returning blocks");
324 DebugLoc dl = MBBI->getDebugLoc();
325 MachineFrameInfo *MFI = MF.getFrameInfo();
326 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
327 const Thumb1RegisterInfo *RegInfo = static_cast<const Thumb1RegisterInfo *>(
328 MF.getSubtarget().getRegisterInfo());
329 const Thumb1InstrInfo &TII =
330 *static_cast<const Thumb1InstrInfo *>(MF.getSubtarget().getInstrInfo());
332 unsigned Align = MF.getTarget()
335 ->getStackAlignment();
336 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align);
337 int NumBytes = (int)MFI->getStackSize();
338 assert((unsigned)NumBytes >= ArgRegsSaveSize &&
339 "ArgRegsSaveSize is included in NumBytes");
340 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs();
341 unsigned FramePtr = RegInfo->getFrameRegister(MF);
343 if (!AFI->hasStackFrame()) {
344 if (NumBytes - ArgRegsSaveSize != 0)
345 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes - ArgRegsSaveSize);
347 // Unwind MBBI to point to first LDR / VLDRD.
348 if (MBBI != MBB.begin()) {
351 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
352 if (!isCSRestore(MBBI, CSRegs))
356 // Move SP to start of FP callee save spill area.
357 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
358 AFI->getGPRCalleeSavedArea2Size() +
359 AFI->getDPRCalleeSavedAreaSize() +
362 if (AFI->shouldRestoreSPFromFP()) {
363 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
364 // Reset SP based on frame pointer only if the stack frame extends beyond
365 // frame pointer stack slot, the target is ELF and the function has FP, or
366 // the target uses var sized objects.
368 assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) &&
369 "No scratch register to restore SP from FP!");
370 emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
372 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
376 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
380 if (MBBI->getOpcode() == ARM::tBX_RET &&
381 &MBB.front() != MBBI &&
382 std::prev(MBBI)->getOpcode() == ARM::tPOP) {
383 MachineBasicBlock::iterator PMBBI = std::prev(MBBI);
384 if (!tryFoldSPUpdateIntoPushPop(STI, MF, PMBBI, NumBytes))
385 emitSPUpdate(MBB, PMBBI, TII, dl, *RegInfo, NumBytes);
386 } else if (!tryFoldSPUpdateIntoPushPop(STI, MF, MBBI, NumBytes))
387 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
391 if (ArgRegsSaveSize) {
392 // Unlike T2 and ARM mode, the T1 pop instruction cannot restore
393 // to LR, and we can't pop the value directly to the PC since
394 // we need to update the SP after popping the value. Therefore, we
395 // pop the old LR into R3 as a temporary.
397 // Get the last instruction, tBX_RET
398 MBBI = MBB.getLastNonDebugInstr();
399 assert (MBBI->getOpcode() == ARM::tBX_RET);
400 // Epilogue for vararg functions: pop LR to R3 and branch off it.
401 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP)))
402 .addReg(ARM::R3, RegState::Define);
404 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, ArgRegsSaveSize);
406 MachineInstrBuilder MIB =
407 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBX_RET_vararg))
408 .addReg(ARM::R3, RegState::Kill);
410 MIB.copyImplicitOps(&*MBBI);
411 // erase the old tBX_RET instruction
416 bool Thumb1FrameLowering::
417 spillCalleeSavedRegisters(MachineBasicBlock &MBB,
418 MachineBasicBlock::iterator MI,
419 const std::vector<CalleeSavedInfo> &CSI,
420 const TargetRegisterInfo *TRI) const {
425 MachineFunction &MF = *MBB.getParent();
426 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
428 if (MI != MBB.end()) DL = MI->getDebugLoc();
430 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(ARM::tPUSH));
432 for (unsigned i = CSI.size(); i != 0; --i) {
433 unsigned Reg = CSI[i-1].getReg();
436 // Add the callee-saved register as live-in unless it's LR and
437 // @llvm.returnaddress is called. If LR is returned for @llvm.returnaddress
438 // then it's already added to the function and entry block live-in sets.
439 if (Reg == ARM::LR) {
440 MachineFunction &MF = *MBB.getParent();
441 if (MF.getFrameInfo()->isReturnAddressTaken() &&
442 MF.getRegInfo().isLiveIn(Reg))
449 MIB.addReg(Reg, getKillRegState(isKill));
451 MIB.setMIFlags(MachineInstr::FrameSetup);
455 bool Thumb1FrameLowering::
456 restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
457 MachineBasicBlock::iterator MI,
458 const std::vector<CalleeSavedInfo> &CSI,
459 const TargetRegisterInfo *TRI) const {
463 MachineFunction &MF = *MBB.getParent();
464 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
465 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
467 bool isVarArg = AFI->getArgRegsSaveSize() > 0;
468 DebugLoc DL = MI->getDebugLoc();
469 MachineInstrBuilder MIB = BuildMI(MF, DL, TII.get(ARM::tPOP));
472 bool NumRegs = false;
473 for (unsigned i = CSI.size(); i != 0; --i) {
474 unsigned Reg = CSI[i-1].getReg();
475 if (Reg == ARM::LR) {
476 // Special epilogue for vararg functions. See emitEpilogue
480 (*MIB).setDesc(TII.get(ARM::tPOP_RET));
481 MIB.copyImplicitOps(&*MI);
484 MIB.addReg(Reg, getDefRegState(true));
488 // It's illegal to emit pop instruction without operands.
490 MBB.insert(MI, &*MIB);
492 MF.DeleteMachineInstr(MIB);