blob: e3f2d2d5981ff036a9f2fab9c4818618c3a0f7c5 [file] [log] [blame]
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001//======- Thumb1FrameLowering.cpp - Thumb1 Frame Information ---*- C++ -*-====//
Anton Korobeynikov33464912010-11-15 00:06:54 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010// This file contains the Thumb1 implementation of TargetFrameLowering class.
Anton Korobeynikov33464912010-11-15 00:06:54 +000011//
12//===----------------------------------------------------------------------===//
13
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000014#include "Thumb1FrameLowering.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000015#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"
Evan Chengab5c7032010-11-22 18:12:04 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000021
22using namespace llvm;
23
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000024bool Thumb1FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000025 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
32 return false;
33
34 return !MF.getFrameInfo()->hasVarSizedObjects();
35}
36
Anton Korobeynikov33464912010-11-15 00:06:54 +000037static void emitSPUpdate(MachineBasicBlock &MBB,
38 MachineBasicBlock::iterator &MBBI,
39 const TargetInstrInfo &TII, DebugLoc dl,
40 const Thumb1RegisterInfo &MRI,
41 int NumBytes) {
42 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes, TII,
43 MRI, dl);
44}
45
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000046void Thumb1FrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +000047 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());
55
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();
62
63 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
64 NumBytes = (NumBytes + 3) & ~3;
65 MFI->setStackSize(NumBytes);
66
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;
71
72 if (VARegSaveSize)
73 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -VARegSaveSize);
74
75 if (!AFI->hasStackFrame()) {
76 if (NumBytes != 0)
77 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes);
78 return;
79 }
80
81 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
82 unsigned Reg = CSI[i].getReg();
83 int FI = CSI[i].getFrameIdx();
84 switch (Reg) {
85 case ARM::R4:
86 case ARM::R5:
87 case ARM::R6:
88 case ARM::R7:
89 case ARM::LR:
90 if (Reg == FramePtr)
91 FramePtrSpillFI = FI;
92 AFI->addGPRCalleeSavedArea1Frame(FI);
93 GPRCS1Size += 4;
94 break;
95 case ARM::R8:
96 case ARM::R9:
97 case ARM::R10:
98 case ARM::R11:
99 if (Reg == FramePtr)
100 FramePtrSpillFI = FI;
101 if (STI.isTargetDarwin()) {
102 AFI->addGPRCalleeSavedArea2Frame(FI);
103 GPRCS2Size += 4;
104 } else {
105 AFI->addGPRCalleeSavedArea1Frame(FI);
106 GPRCS1Size += 4;
107 }
108 break;
109 default:
110 AFI->addDPRCalleeSavedAreaFrame(FI);
111 DPRCSSize += 8;
112 }
113 }
114
115 if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
116 ++MBBI;
117 if (MBBI != MBB.end())
118 dl = MBBI->getDebugLoc();
119 }
120
Anton Korobeynikov33464912010-11-15 00:06:54 +0000121 // 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);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000129 NumBytes = DPRCSOffset;
Evan Chengab5c7032010-11-22 18:12:04 +0000130
131 // Adjust FP so it point to the stack slot that contains the previous FP.
132 if (hasFP(MF)) {
133 BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDrSPi), FramePtr)
134 .addFrameIndex(FramePtrSpillFI).addImm(0);
135 if (NumBytes > 7)
136 // If offset is > 7 then sp cannot be adjusted in a single instruction,
137 // try restoring from fp instead.
138 AFI->setShouldRestoreSPFromFP(true);
139 }
140
141 if (NumBytes)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000142 // Insert it after all the callee-save spills.
143 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000144
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000145 if (STI.isTargetELF() && hasFP(MF))
Anton Korobeynikov33464912010-11-15 00:06:54 +0000146 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
147 AFI->getFramePtrSpillOffset());
148
149 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
150 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
151 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
152
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);
159}
160
161static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
162 for (unsigned i = 0; CSRegs[i]; ++i)
163 if (Reg == CSRegs[i])
164 return true;
165 return false;
166}
167
168static bool isCSRestore(MachineInstr *MI, const unsigned *CSRegs) {
169 if (MI->getOpcode() == ARM::tRestore &&
170 MI->getOperand(1).isFI() &&
171 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs))
172 return true;
173 else if (MI->getOpcode() == ARM::tPOP) {
174 // The first two operands are predicates. The last two are
175 // imp-def and imp-use of SP. Check everything in between.
176 for (int i = 2, e = MI->getNumOperands() - 2; i != e; ++i)
177 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
178 return false;
179 return true;
180 }
181 return false;
182}
183
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000184void Thumb1FrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000185 MachineBasicBlock &MBB) const {
186 MachineBasicBlock::iterator MBBI = prior(MBB.end());
187 assert((MBBI->getOpcode() == ARM::tBX_RET ||
188 MBBI->getOpcode() == ARM::tPOP_RET) &&
189 "Can only insert epilog into returning blocks");
190 DebugLoc dl = MBBI->getDebugLoc();
191 MachineFrameInfo *MFI = MF.getFrameInfo();
192 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
193 const Thumb1RegisterInfo *RegInfo =
194 static_cast<const Thumb1RegisterInfo*>(MF.getTarget().getRegisterInfo());
195 const Thumb1InstrInfo &TII =
196 *static_cast<const Thumb1InstrInfo*>(MF.getTarget().getInstrInfo());
197
198 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
199 int NumBytes = (int)MFI->getStackSize();
200 const unsigned *CSRegs = RegInfo->getCalleeSavedRegs();
201 unsigned FramePtr = RegInfo->getFrameRegister(MF);
202
203 if (!AFI->hasStackFrame()) {
204 if (NumBytes != 0)
205 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
206 } else {
207 // Unwind MBBI to point to first LDR / VLDRD.
208 if (MBBI != MBB.begin()) {
209 do
210 --MBBI;
211 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
212 if (!isCSRestore(MBBI, CSRegs))
213 ++MBBI;
214 }
215
216 // Move SP to start of FP callee save spill area.
217 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
218 AFI->getGPRCalleeSavedArea2Size() +
219 AFI->getDPRCalleeSavedAreaSize());
220
221 if (AFI->shouldRestoreSPFromFP()) {
222 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
223 // Reset SP based on frame pointer only if the stack frame extends beyond
224 // frame pointer stack slot or target is ELF and the function has FP.
Evan Chengab5c7032010-11-22 18:12:04 +0000225 if (NumBytes) {
226 assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) &&
227 "No scratch register to restore SP from FP!");
228 emitThumbRegPlusImmediate(MBB, MBBI, ARM::R4, FramePtr, -NumBytes,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000229 TII, *RegInfo, dl);
Evan Chengab5c7032010-11-22 18:12:04 +0000230 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVtgpr2gpr), ARM::SP)
231 .addReg(ARM::R4);
232 } else
Anton Korobeynikov33464912010-11-15 00:06:54 +0000233 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVtgpr2gpr), ARM::SP)
234 .addReg(FramePtr);
235 } else {
236 if (MBBI->getOpcode() == ARM::tBX_RET &&
237 &MBB.front() != MBBI &&
238 prior(MBBI)->getOpcode() == ARM::tPOP) {
239 MachineBasicBlock::iterator PMBBI = prior(MBBI);
240 emitSPUpdate(MBB, PMBBI, TII, dl, *RegInfo, NumBytes);
241 } else
242 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
243 }
244 }
245
246 if (VARegSaveSize) {
247 // Unlike T2 and ARM mode, the T1 pop instruction cannot restore
248 // to LR, and we can't pop the value directly to the PC since
249 // we need to update the SP after popping the value. Therefore, we
250 // pop the old LR into R3 as a temporary.
251
252 // Move back past the callee-saved register restoration
253 while (MBBI != MBB.end() && isCSRestore(MBBI, CSRegs))
254 ++MBBI;
255 // Epilogue for vararg functions: pop LR to R3 and branch off it.
256 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP)))
257 .addReg(ARM::R3, RegState::Define);
258
259 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, VARegSaveSize);
260
261 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBX_RET_vararg))
262 .addReg(ARM::R3, RegState::Kill);
263 // erase the old tBX_RET instruction
264 MBB.erase(MBBI);
265 }
266}
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000267
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000268bool Thumb1FrameLowering::
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000269spillCalleeSavedRegisters(MachineBasicBlock &MBB,
270 MachineBasicBlock::iterator MI,
271 const std::vector<CalleeSavedInfo> &CSI,
272 const TargetRegisterInfo *TRI) const {
273 if (CSI.empty())
274 return false;
275
276 DebugLoc DL;
277 MachineFunction &MF = *MBB.getParent();
278 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
279
280 if (MI != MBB.end()) DL = MI->getDebugLoc();
281
282 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(ARM::tPUSH));
283 AddDefaultPred(MIB);
284 for (unsigned i = CSI.size(); i != 0; --i) {
285 unsigned Reg = CSI[i-1].getReg();
286 bool isKill = true;
287
288 // Add the callee-saved register as live-in unless it's LR and
289 // @llvm.returnaddress is called. If LR is returned for @llvm.returnaddress
290 // then it's already added to the function and entry block live-in sets.
291 if (Reg == ARM::LR) {
292 MachineFunction &MF = *MBB.getParent();
293 if (MF.getFrameInfo()->isReturnAddressTaken() &&
294 MF.getRegInfo().isLiveIn(Reg))
295 isKill = false;
296 }
297
298 if (isKill)
299 MBB.addLiveIn(Reg);
300
301 MIB.addReg(Reg, getKillRegState(isKill));
302 }
303 return true;
304}
305
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000306bool Thumb1FrameLowering::
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000307restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
308 MachineBasicBlock::iterator MI,
309 const std::vector<CalleeSavedInfo> &CSI,
310 const TargetRegisterInfo *TRI) const {
311 if (CSI.empty())
312 return false;
313
314 MachineFunction &MF = *MBB.getParent();
315 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
316 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
317
318 bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
319 DebugLoc DL = MI->getDebugLoc();
320 MachineInstrBuilder MIB = BuildMI(MF, DL, TII.get(ARM::tPOP));
321 AddDefaultPred(MIB);
322
323 bool NumRegs = false;
324 for (unsigned i = CSI.size(); i != 0; --i) {
325 unsigned Reg = CSI[i-1].getReg();
326 if (Reg == ARM::LR) {
327 // Special epilogue for vararg functions. See emitEpilogue
328 if (isVarArg)
329 continue;
330 Reg = ARM::PC;
331 (*MIB).setDesc(TII.get(ARM::tPOP_RET));
332 MI = MBB.erase(MI);
333 }
334 MIB.addReg(Reg, getDefRegState(true));
335 NumRegs = true;
336 }
337
338 // It's illegal to emit pop instruction without operands.
339 if (NumRegs)
340 MBB.insert(MI, &*MIB);
341 else
342 MF.DeleteMachineInstr(MIB);
343
344 return true;
345}