1 //======- Thumb1FrameLowering.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 TargetFrameLowering class.
12 //===----------------------------------------------------------------------===//
14 #include "Thumb1FrameLowering.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"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 bool Thumb1FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
25 const MachineFrameInfo *FFI = MF.getFrameInfo();
26 unsigned CFSize = FFI->getMaxCallFrameSize();
27 // It's not always a good idea to include the call frame as part of the
28 // stack frame. ARM (especially Thumb) has small immediate offset to
29 // address the stack frame. So a large call frame can cause poor codegen
30 // and may even makes it impossible to scavenge a register.
31 if (CFSize >= ((1 << 8) - 1) * 4 / 2) // Half of imm8 * 4
34 return !MF.getFrameInfo()->hasVarSizedObjects();
37 static void emitSPUpdate(MachineBasicBlock &MBB,
38 MachineBasicBlock::iterator &MBBI,
39 const TargetInstrInfo &TII, DebugLoc dl,
40 const Thumb1RegisterInfo &MRI,
42 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes, TII,
46 void Thumb1FrameLowering::emitPrologue(MachineFunction &MF) const {
47 MachineBasicBlock &MBB = MF.front();
48 MachineBasicBlock::iterator MBBI = MBB.begin();
49 MachineFrameInfo *MFI = MF.getFrameInfo();
50 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
51 const Thumb1RegisterInfo *RegInfo =
52 static_cast<const Thumb1RegisterInfo*>(MF.getTarget().getRegisterInfo());
53 const Thumb1InstrInfo &TII =
54 *static_cast<const Thumb1InstrInfo*>(MF.getTarget().getInstrInfo());
56 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
57 unsigned NumBytes = MFI->getStackSize();
58 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
59 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
60 unsigned FramePtr = RegInfo->getFrameRegister(MF);
61 unsigned BasePtr = RegInfo->getBaseRegister();
63 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
64 NumBytes = (NumBytes + 3) & ~3;
65 MFI->setStackSize(NumBytes);
67 // Determine the sizes of each callee-save spill areas and record which frame
68 // belongs to which callee-save spill areas.
69 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
70 int FramePtrSpillFI = 0;
73 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -VARegSaveSize);
75 if (!AFI->hasStackFrame()) {
77 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes);
81 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
82 unsigned Reg = CSI[i].getReg();
83 int FI = CSI[i].getFrameIdx();
92 AFI->addGPRCalleeSavedArea1Frame(FI);
100 FramePtrSpillFI = FI;
101 if (STI.isTargetDarwin()) {
102 AFI->addGPRCalleeSavedArea2Frame(FI);
105 AFI->addGPRCalleeSavedArea1Frame(FI);
110 AFI->addDPRCalleeSavedAreaFrame(FI);
115 if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
117 if (MBBI != MBB.end())
118 dl = MBBI->getDebugLoc();
121 // Determine starting offsets of spill areas.
122 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
123 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
124 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
125 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
126 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
127 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
128 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
129 NumBytes = DPRCSOffset;
131 // Adjust FP so it point to the stack slot that contains the previous FP.
133 BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDrSPi), FramePtr)
134 .addFrameIndex(FramePtrSpillFI).addImm(0);
136 // If offset is > 7 then sp cannot be adjusted in a single instruction,
137 // try restoring from fp instead.
138 AFI->setShouldRestoreSPFromFP(true);
142 // Insert it after all the callee-save spills.
143 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes);
145 if (STI.isTargetELF() && hasFP(MF))
146 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
147 AFI->getFramePtrSpillOffset());
149 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
150 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
151 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
153 // If we need a base pointer, set it up here. It's whatever the value
154 // of the stack pointer is at this point. Any variable size objects
155 // will be allocated after this, so we can still use the base pointer
156 // to reference locals.
157 if (RegInfo->hasBasePointer(MF))
158 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), BasePtr).addReg(ARM::SP);
160 // If the frame has variable sized objects then the epilogue must restore
161 // the sp from fp. We can assume there's an FP here since hasFP already
162 // checks for hasVarSizedObjects.
163 if (MFI->hasVarSizedObjects())
164 AFI->setShouldRestoreSPFromFP(true);
167 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
168 for (unsigned i = 0; CSRegs[i]; ++i)
169 if (Reg == CSRegs[i])
174 static bool isCSRestore(MachineInstr *MI, const unsigned *CSRegs) {
175 if (MI->getOpcode() == ARM::tRestore &&
176 MI->getOperand(1).isFI() &&
177 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs))
179 else if (MI->getOpcode() == ARM::tPOP) {
180 // The first two operands are predicates. The last two are
181 // imp-def and imp-use of SP. Check everything in between.
182 for (int i = 2, e = MI->getNumOperands() - 2; i != e; ++i)
183 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
190 void Thumb1FrameLowering::emitEpilogue(MachineFunction &MF,
191 MachineBasicBlock &MBB) const {
192 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
193 assert((MBBI->getOpcode() == ARM::tBX_RET ||
194 MBBI->getOpcode() == ARM::tPOP_RET) &&
195 "Can only insert epilog into returning blocks");
196 DebugLoc dl = MBBI->getDebugLoc();
197 MachineFrameInfo *MFI = MF.getFrameInfo();
198 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
199 const Thumb1RegisterInfo *RegInfo =
200 static_cast<const Thumb1RegisterInfo*>(MF.getTarget().getRegisterInfo());
201 const Thumb1InstrInfo &TII =
202 *static_cast<const Thumb1InstrInfo*>(MF.getTarget().getInstrInfo());
204 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
205 int NumBytes = (int)MFI->getStackSize();
206 const unsigned *CSRegs = RegInfo->getCalleeSavedRegs();
207 unsigned FramePtr = RegInfo->getFrameRegister(MF);
209 if (!AFI->hasStackFrame()) {
211 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
213 // Unwind MBBI to point to first LDR / VLDRD.
214 if (MBBI != MBB.begin()) {
217 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
218 if (!isCSRestore(MBBI, CSRegs))
222 // Move SP to start of FP callee save spill area.
223 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
224 AFI->getGPRCalleeSavedArea2Size() +
225 AFI->getDPRCalleeSavedAreaSize());
227 if (AFI->shouldRestoreSPFromFP()) {
228 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
229 // Reset SP based on frame pointer only if the stack frame extends beyond
230 // frame pointer stack slot, the target is ELF and the function has FP, or
231 // the target uses var sized objects.
233 assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) &&
234 "No scratch register to restore SP from FP!");
235 emitThumbRegPlusImmediate(MBB, MBBI, ARM::R4, FramePtr, -NumBytes,
237 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVtgpr2gpr), ARM::SP)
240 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVtgpr2gpr), ARM::SP)
243 if (MBBI->getOpcode() == ARM::tBX_RET &&
244 &MBB.front() != MBBI &&
245 prior(MBBI)->getOpcode() == ARM::tPOP) {
246 MachineBasicBlock::iterator PMBBI = prior(MBBI);
247 emitSPUpdate(MBB, PMBBI, TII, dl, *RegInfo, NumBytes);
249 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
254 // Unlike T2 and ARM mode, the T1 pop instruction cannot restore
255 // to LR, and we can't pop the value directly to the PC since
256 // we need to update the SP after popping the value. Therefore, we
257 // pop the old LR into R3 as a temporary.
259 // Move back past the callee-saved register restoration
260 while (MBBI != MBB.end() && isCSRestore(MBBI, CSRegs))
262 // Epilogue for vararg functions: pop LR to R3 and branch off it.
263 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP)))
264 .addReg(ARM::R3, RegState::Define);
266 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, VARegSaveSize);
268 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBX_RET_vararg))
269 .addReg(ARM::R3, RegState::Kill);
270 // erase the old tBX_RET instruction
275 bool Thumb1FrameLowering::
276 spillCalleeSavedRegisters(MachineBasicBlock &MBB,
277 MachineBasicBlock::iterator MI,
278 const std::vector<CalleeSavedInfo> &CSI,
279 const TargetRegisterInfo *TRI) const {
284 MachineFunction &MF = *MBB.getParent();
285 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
287 if (MI != MBB.end()) DL = MI->getDebugLoc();
289 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(ARM::tPUSH));
291 for (unsigned i = CSI.size(); i != 0; --i) {
292 unsigned Reg = CSI[i-1].getReg();
295 // Add the callee-saved register as live-in unless it's LR and
296 // @llvm.returnaddress is called. If LR is returned for @llvm.returnaddress
297 // then it's already added to the function and entry block live-in sets.
298 if (Reg == ARM::LR) {
299 MachineFunction &MF = *MBB.getParent();
300 if (MF.getFrameInfo()->isReturnAddressTaken() &&
301 MF.getRegInfo().isLiveIn(Reg))
308 MIB.addReg(Reg, getKillRegState(isKill));
313 bool Thumb1FrameLowering::
314 restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
315 MachineBasicBlock::iterator MI,
316 const std::vector<CalleeSavedInfo> &CSI,
317 const TargetRegisterInfo *TRI) const {
321 MachineFunction &MF = *MBB.getParent();
322 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
323 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
325 bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
326 DebugLoc DL = MI->getDebugLoc();
327 MachineInstrBuilder MIB = BuildMI(MF, DL, TII.get(ARM::tPOP));
330 bool NumRegs = false;
331 for (unsigned i = CSI.size(); i != 0; --i) {
332 unsigned Reg = CSI[i-1].getReg();
333 if (Reg == ARM::LR) {
334 // Special epilogue for vararg functions. See emitEpilogue
338 (*MIB).setDesc(TII.get(ARM::tPOP_RET));
341 MIB.addReg(Reg, getDefRegState(true));
345 // It's illegal to emit pop instruction without operands.
347 MBB.insert(MI, &*MIB);
349 MF.DeleteMachineInstr(MIB);