blob: 6a3c1e15eafb131ebad4a8c33e5fb57d90ff0b6a [file] [log] [blame]
Anton Korobeynikov33464912010-11-15 00:06:54 +00001//=======- Thumb1FrameInfo.cpp - Thumb1 Frame Information ------*- C++ -*-====//
2//
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//
10// This file contains the Thumb1 implementation of TargetFrameInfo class.
11//
12//===----------------------------------------------------------------------===//
13
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"
20
21using namespace llvm;
22
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000023bool Thumb1FrameInfo::hasReservedCallFrame(const MachineFunction &MF) const {
24 const MachineFrameInfo *FFI = MF.getFrameInfo();
25 unsigned CFSize = FFI->getMaxCallFrameSize();
26 // It's not always a good idea to include the call frame as part of the
27 // stack frame. ARM (especially Thumb) has small immediate offset to
28 // address the stack frame. So a large call frame can cause poor codegen
29 // and may even makes it impossible to scavenge a register.
30 if (CFSize >= ((1 << 8) - 1) * 4 / 2) // Half of imm8 * 4
31 return false;
32
33 return !MF.getFrameInfo()->hasVarSizedObjects();
34}
35
Anton Korobeynikov33464912010-11-15 00:06:54 +000036static void emitSPUpdate(MachineBasicBlock &MBB,
37 MachineBasicBlock::iterator &MBBI,
38 const TargetInstrInfo &TII, DebugLoc dl,
39 const Thumb1RegisterInfo &MRI,
40 int NumBytes) {
41 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes, TII,
42 MRI, dl);
43}
44
45void Thumb1FrameInfo::emitPrologue(MachineFunction &MF) const {
46 MachineBasicBlock &MBB = MF.front();
47 MachineBasicBlock::iterator MBBI = MBB.begin();
48 MachineFrameInfo *MFI = MF.getFrameInfo();
49 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
50 const Thumb1RegisterInfo *RegInfo =
51 static_cast<const Thumb1RegisterInfo*>(MF.getTarget().getRegisterInfo());
52 const Thumb1InstrInfo &TII =
53 *static_cast<const Thumb1InstrInfo*>(MF.getTarget().getInstrInfo());
54
55 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
56 unsigned NumBytes = MFI->getStackSize();
57 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
58 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
59 unsigned FramePtr = RegInfo->getFrameRegister(MF);
60 unsigned BasePtr = RegInfo->getBaseRegister();
61
62 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
63 NumBytes = (NumBytes + 3) & ~3;
64 MFI->setStackSize(NumBytes);
65
66 // Determine the sizes of each callee-save spill areas and record which frame
67 // belongs to which callee-save spill areas.
68 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
69 int FramePtrSpillFI = 0;
70
71 if (VARegSaveSize)
72 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -VARegSaveSize);
73
74 if (!AFI->hasStackFrame()) {
75 if (NumBytes != 0)
76 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes);
77 return;
78 }
79
80 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
81 unsigned Reg = CSI[i].getReg();
82 int FI = CSI[i].getFrameIdx();
83 switch (Reg) {
84 case ARM::R4:
85 case ARM::R5:
86 case ARM::R6:
87 case ARM::R7:
88 case ARM::LR:
89 if (Reg == FramePtr)
90 FramePtrSpillFI = FI;
91 AFI->addGPRCalleeSavedArea1Frame(FI);
92 GPRCS1Size += 4;
93 break;
94 case ARM::R8:
95 case ARM::R9:
96 case ARM::R10:
97 case ARM::R11:
98 if (Reg == FramePtr)
99 FramePtrSpillFI = FI;
100 if (STI.isTargetDarwin()) {
101 AFI->addGPRCalleeSavedArea2Frame(FI);
102 GPRCS2Size += 4;
103 } else {
104 AFI->addGPRCalleeSavedArea1Frame(FI);
105 GPRCS1Size += 4;
106 }
107 break;
108 default:
109 AFI->addDPRCalleeSavedAreaFrame(FI);
110 DPRCSSize += 8;
111 }
112 }
113
114 if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
115 ++MBBI;
116 if (MBBI != MBB.end())
117 dl = MBBI->getDebugLoc();
118 }
119
120 // Adjust FP so it point to the stack slot that contains the previous FP.
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000121 if (hasFP(MF)) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000122 BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDrSPi), FramePtr)
123 .addFrameIndex(FramePtrSpillFI).addImm(0);
124 AFI->setShouldRestoreSPFromFP(true);
125 }
126
127 // Determine starting offsets of spill areas.
128 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
129 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
130 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
131 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
132 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
133 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
134 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
135
136 NumBytes = DPRCSOffset;
137 if (NumBytes) {
138 // Insert it after all the callee-save spills.
139 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes);
140 }
141
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000142 if (STI.isTargetELF() && hasFP(MF))
Anton Korobeynikov33464912010-11-15 00:06:54 +0000143 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
144 AFI->getFramePtrSpillOffset());
145
146 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
147 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
148 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
149
150 // If we need a base pointer, set it up here. It's whatever the value
151 // of the stack pointer is at this point. Any variable size objects
152 // will be allocated after this, so we can still use the base pointer
153 // to reference locals.
154 if (RegInfo->hasBasePointer(MF))
155 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), BasePtr).addReg(ARM::SP);
156}
157
158static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
159 for (unsigned i = 0; CSRegs[i]; ++i)
160 if (Reg == CSRegs[i])
161 return true;
162 return false;
163}
164
165static bool isCSRestore(MachineInstr *MI, const unsigned *CSRegs) {
166 if (MI->getOpcode() == ARM::tRestore &&
167 MI->getOperand(1).isFI() &&
168 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs))
169 return true;
170 else if (MI->getOpcode() == ARM::tPOP) {
171 // The first two operands are predicates. The last two are
172 // imp-def and imp-use of SP. Check everything in between.
173 for (int i = 2, e = MI->getNumOperands() - 2; i != e; ++i)
174 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
175 return false;
176 return true;
177 }
178 return false;
179}
180
181void Thumb1FrameInfo::emitEpilogue(MachineFunction &MF,
182 MachineBasicBlock &MBB) const {
183 MachineBasicBlock::iterator MBBI = prior(MBB.end());
184 assert((MBBI->getOpcode() == ARM::tBX_RET ||
185 MBBI->getOpcode() == ARM::tPOP_RET) &&
186 "Can only insert epilog into returning blocks");
187 DebugLoc dl = MBBI->getDebugLoc();
188 MachineFrameInfo *MFI = MF.getFrameInfo();
189 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
190 const Thumb1RegisterInfo *RegInfo =
191 static_cast<const Thumb1RegisterInfo*>(MF.getTarget().getRegisterInfo());
192 const Thumb1InstrInfo &TII =
193 *static_cast<const Thumb1InstrInfo*>(MF.getTarget().getInstrInfo());
194
195 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
196 int NumBytes = (int)MFI->getStackSize();
197 const unsigned *CSRegs = RegInfo->getCalleeSavedRegs();
198 unsigned FramePtr = RegInfo->getFrameRegister(MF);
199
200 if (!AFI->hasStackFrame()) {
201 if (NumBytes != 0)
202 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
203 } else {
204 // Unwind MBBI to point to first LDR / VLDRD.
205 if (MBBI != MBB.begin()) {
206 do
207 --MBBI;
208 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
209 if (!isCSRestore(MBBI, CSRegs))
210 ++MBBI;
211 }
212
213 // Move SP to start of FP callee save spill area.
214 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
215 AFI->getGPRCalleeSavedArea2Size() +
216 AFI->getDPRCalleeSavedAreaSize());
217
218 if (AFI->shouldRestoreSPFromFP()) {
219 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
220 // Reset SP based on frame pointer only if the stack frame extends beyond
221 // frame pointer stack slot or target is ELF and the function has FP.
222 if (NumBytes)
223 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, FramePtr, -NumBytes,
224 TII, *RegInfo, dl);
225 else
226 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVtgpr2gpr), ARM::SP)
227 .addReg(FramePtr);
228 } else {
229 if (MBBI->getOpcode() == ARM::tBX_RET &&
230 &MBB.front() != MBBI &&
231 prior(MBBI)->getOpcode() == ARM::tPOP) {
232 MachineBasicBlock::iterator PMBBI = prior(MBBI);
233 emitSPUpdate(MBB, PMBBI, TII, dl, *RegInfo, NumBytes);
234 } else
235 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
236 }
237 }
238
239 if (VARegSaveSize) {
240 // Unlike T2 and ARM mode, the T1 pop instruction cannot restore
241 // to LR, and we can't pop the value directly to the PC since
242 // we need to update the SP after popping the value. Therefore, we
243 // pop the old LR into R3 as a temporary.
244
245 // Move back past the callee-saved register restoration
246 while (MBBI != MBB.end() && isCSRestore(MBBI, CSRegs))
247 ++MBBI;
248 // Epilogue for vararg functions: pop LR to R3 and branch off it.
249 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP)))
250 .addReg(ARM::R3, RegState::Define);
251
252 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, VARegSaveSize);
253
254 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBX_RET_vararg))
255 .addReg(ARM::R3, RegState::Kill);
256 // erase the old tBX_RET instruction
257 MBB.erase(MBBI);
258 }
259}