1 //=======- Thumb1FrameInfo.cpp - Thumb1 Frame Information ------*- C++ -*-====//
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 TargetFrameInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "Thumb1FrameInfo.h"
15 #include "ARMBaseInstrInfo.h"
16 #include "ARMMachineFunctionInfo.h"
17 #include "llvm/CodeGen/MachineFrameInfo.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 static void emitSPUpdate(MachineBasicBlock &MBB,
24 MachineBasicBlock::iterator &MBBI,
25 const TargetInstrInfo &TII, DebugLoc dl,
26 const Thumb1RegisterInfo &MRI,
28 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes, TII,
32 void Thumb1FrameInfo::emitPrologue(MachineFunction &MF) const {
33 MachineBasicBlock &MBB = MF.front();
34 MachineBasicBlock::iterator MBBI = MBB.begin();
35 MachineFrameInfo *MFI = MF.getFrameInfo();
36 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
37 const Thumb1RegisterInfo *RegInfo =
38 static_cast<const Thumb1RegisterInfo*>(MF.getTarget().getRegisterInfo());
39 const Thumb1InstrInfo &TII =
40 *static_cast<const Thumb1InstrInfo*>(MF.getTarget().getInstrInfo());
42 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
43 unsigned NumBytes = MFI->getStackSize();
44 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
45 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
46 unsigned FramePtr = RegInfo->getFrameRegister(MF);
47 unsigned BasePtr = RegInfo->getBaseRegister();
49 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
50 NumBytes = (NumBytes + 3) & ~3;
51 MFI->setStackSize(NumBytes);
53 // Determine the sizes of each callee-save spill areas and record which frame
54 // belongs to which callee-save spill areas.
55 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
56 int FramePtrSpillFI = 0;
59 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -VARegSaveSize);
61 if (!AFI->hasStackFrame()) {
63 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes);
67 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
68 unsigned Reg = CSI[i].getReg();
69 int FI = CSI[i].getFrameIdx();
78 AFI->addGPRCalleeSavedArea1Frame(FI);
87 if (STI.isTargetDarwin()) {
88 AFI->addGPRCalleeSavedArea2Frame(FI);
91 AFI->addGPRCalleeSavedArea1Frame(FI);
96 AFI->addDPRCalleeSavedAreaFrame(FI);
101 if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
103 if (MBBI != MBB.end())
104 dl = MBBI->getDebugLoc();
107 // Adjust FP so it point to the stack slot that contains the previous FP.
108 if (RegInfo->hasFP(MF)) {
109 BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDrSPi), FramePtr)
110 .addFrameIndex(FramePtrSpillFI).addImm(0);
111 AFI->setShouldRestoreSPFromFP(true);
114 // Determine starting offsets of spill areas.
115 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
116 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
117 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
118 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
119 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
120 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
121 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
123 NumBytes = DPRCSOffset;
125 // Insert it after all the callee-save spills.
126 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes);
129 if (STI.isTargetELF() && RegInfo->hasFP(MF))
130 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
131 AFI->getFramePtrSpillOffset());
133 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
134 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
135 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
137 // If we need a base pointer, set it up here. It's whatever the value
138 // of the stack pointer is at this point. Any variable size objects
139 // will be allocated after this, so we can still use the base pointer
140 // to reference locals.
141 if (RegInfo->hasBasePointer(MF))
142 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), BasePtr).addReg(ARM::SP);
145 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
146 for (unsigned i = 0; CSRegs[i]; ++i)
147 if (Reg == CSRegs[i])
152 static bool isCSRestore(MachineInstr *MI, const unsigned *CSRegs) {
153 if (MI->getOpcode() == ARM::tRestore &&
154 MI->getOperand(1).isFI() &&
155 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs))
157 else if (MI->getOpcode() == ARM::tPOP) {
158 // The first two operands are predicates. The last two are
159 // imp-def and imp-use of SP. Check everything in between.
160 for (int i = 2, e = MI->getNumOperands() - 2; i != e; ++i)
161 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
168 void Thumb1FrameInfo::emitEpilogue(MachineFunction &MF,
169 MachineBasicBlock &MBB) const {
170 MachineBasicBlock::iterator MBBI = prior(MBB.end());
171 assert((MBBI->getOpcode() == ARM::tBX_RET ||
172 MBBI->getOpcode() == ARM::tPOP_RET) &&
173 "Can only insert epilog into returning blocks");
174 DebugLoc dl = MBBI->getDebugLoc();
175 MachineFrameInfo *MFI = MF.getFrameInfo();
176 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
177 const Thumb1RegisterInfo *RegInfo =
178 static_cast<const Thumb1RegisterInfo*>(MF.getTarget().getRegisterInfo());
179 const Thumb1InstrInfo &TII =
180 *static_cast<const Thumb1InstrInfo*>(MF.getTarget().getInstrInfo());
182 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
183 int NumBytes = (int)MFI->getStackSize();
184 const unsigned *CSRegs = RegInfo->getCalleeSavedRegs();
185 unsigned FramePtr = RegInfo->getFrameRegister(MF);
187 if (!AFI->hasStackFrame()) {
189 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
191 // Unwind MBBI to point to first LDR / VLDRD.
192 if (MBBI != MBB.begin()) {
195 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
196 if (!isCSRestore(MBBI, CSRegs))
200 // Move SP to start of FP callee save spill area.
201 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
202 AFI->getGPRCalleeSavedArea2Size() +
203 AFI->getDPRCalleeSavedAreaSize());
205 if (AFI->shouldRestoreSPFromFP()) {
206 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
207 // Reset SP based on frame pointer only if the stack frame extends beyond
208 // frame pointer stack slot or target is ELF and the function has FP.
210 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, FramePtr, -NumBytes,
213 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVtgpr2gpr), ARM::SP)
216 if (MBBI->getOpcode() == ARM::tBX_RET &&
217 &MBB.front() != MBBI &&
218 prior(MBBI)->getOpcode() == ARM::tPOP) {
219 MachineBasicBlock::iterator PMBBI = prior(MBBI);
220 emitSPUpdate(MBB, PMBBI, TII, dl, *RegInfo, NumBytes);
222 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
227 // Unlike T2 and ARM mode, the T1 pop instruction cannot restore
228 // to LR, and we can't pop the value directly to the PC since
229 // we need to update the SP after popping the value. Therefore, we
230 // pop the old LR into R3 as a temporary.
232 // Move back past the callee-saved register restoration
233 while (MBBI != MBB.end() && isCSRestore(MBBI, CSRegs))
235 // Epilogue for vararg functions: pop LR to R3 and branch off it.
236 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP)))
237 .addReg(ARM::R3, RegState::Define);
239 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, VARegSaveSize);
241 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBX_RET_vararg))
242 .addReg(ARM::R3, RegState::Kill);
243 // erase the old tBX_RET instruction