blob: 8a6dda8214849fe923126b2e971190e0f9ce6755 [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);
Eric Christopher94bb7b52011-01-11 00:16:04 +0000159
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);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000165}
166
167static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
168 for (unsigned i = 0; CSRegs[i]; ++i)
169 if (Reg == CSRegs[i])
170 return true;
171 return false;
172}
173
174static 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))
178 return true;
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))
184 return false;
185 return true;
186 }
187 return false;
188}
189
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000190void Thumb1FrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000191 MachineBasicBlock &MBB) const {
192 MachineBasicBlock::iterator MBBI = prior(MBB.end());
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());
203
204 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
205 int NumBytes = (int)MFI->getStackSize();
206 const unsigned *CSRegs = RegInfo->getCalleeSavedRegs();
207 unsigned FramePtr = RegInfo->getFrameRegister(MF);
208
209 if (!AFI->hasStackFrame()) {
210 if (NumBytes != 0)
211 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
212 } else {
213 // Unwind MBBI to point to first LDR / VLDRD.
214 if (MBBI != MBB.begin()) {
215 do
216 --MBBI;
217 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
218 if (!isCSRestore(MBBI, CSRegs))
219 ++MBBI;
220 }
221
222 // Move SP to start of FP callee save spill area.
223 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
224 AFI->getGPRCalleeSavedArea2Size() +
225 AFI->getDPRCalleeSavedAreaSize());
226
227 if (AFI->shouldRestoreSPFromFP()) {
228 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
229 // Reset SP based on frame pointer only if the stack frame extends beyond
Eric Christopher94bb7b52011-01-11 00:16:04 +0000230 // frame pointer stack slot, the target is ELF and the function has FP, or
231 // the target uses var sized objects.
Evan Chengab5c7032010-11-22 18:12:04 +0000232 if (NumBytes) {
233 assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) &&
234 "No scratch register to restore SP from FP!");
235 emitThumbRegPlusImmediate(MBB, MBBI, ARM::R4, FramePtr, -NumBytes,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000236 TII, *RegInfo, dl);
Evan Chengab5c7032010-11-22 18:12:04 +0000237 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVtgpr2gpr), ARM::SP)
238 .addReg(ARM::R4);
239 } else
Anton Korobeynikov33464912010-11-15 00:06:54 +0000240 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVtgpr2gpr), ARM::SP)
241 .addReg(FramePtr);
242 } else {
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);
248 } else
249 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
250 }
251 }
252
253 if (VARegSaveSize) {
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.
258
259 // Move back past the callee-saved register restoration
260 while (MBBI != MBB.end() && isCSRestore(MBBI, CSRegs))
261 ++MBBI;
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);
265
266 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, VARegSaveSize);
267
268 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBX_RET_vararg))
269 .addReg(ARM::R3, RegState::Kill);
270 // erase the old tBX_RET instruction
271 MBB.erase(MBBI);
272 }
273}
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000274
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000275bool Thumb1FrameLowering::
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000276spillCalleeSavedRegisters(MachineBasicBlock &MBB,
277 MachineBasicBlock::iterator MI,
278 const std::vector<CalleeSavedInfo> &CSI,
279 const TargetRegisterInfo *TRI) const {
280 if (CSI.empty())
281 return false;
282
283 DebugLoc DL;
284 MachineFunction &MF = *MBB.getParent();
285 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
286
287 if (MI != MBB.end()) DL = MI->getDebugLoc();
288
289 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(ARM::tPUSH));
290 AddDefaultPred(MIB);
291 for (unsigned i = CSI.size(); i != 0; --i) {
292 unsigned Reg = CSI[i-1].getReg();
293 bool isKill = true;
294
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))
302 isKill = false;
303 }
304
305 if (isKill)
306 MBB.addLiveIn(Reg);
307
308 MIB.addReg(Reg, getKillRegState(isKill));
309 }
310 return true;
311}
312
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000313bool Thumb1FrameLowering::
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000314restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
315 MachineBasicBlock::iterator MI,
316 const std::vector<CalleeSavedInfo> &CSI,
317 const TargetRegisterInfo *TRI) const {
318 if (CSI.empty())
319 return false;
320
321 MachineFunction &MF = *MBB.getParent();
322 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
323 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
324
325 bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
326 DebugLoc DL = MI->getDebugLoc();
327 MachineInstrBuilder MIB = BuildMI(MF, DL, TII.get(ARM::tPOP));
328 AddDefaultPred(MIB);
329
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
335 if (isVarArg)
336 continue;
337 Reg = ARM::PC;
338 (*MIB).setDesc(TII.get(ARM::tPOP_RET));
339 MI = MBB.erase(MI);
340 }
341 MIB.addReg(Reg, getDefRegState(true));
342 NumRegs = true;
343 }
344
345 // It's illegal to emit pop instruction without operands.
346 if (NumRegs)
347 MBB.insert(MI, &*MIB);
348 else
349 MF.DeleteMachineInstr(MIB);
350
351 return true;
352}