blob: 2224652f222606026969fa3cff1c7e452ac78a6a [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- Thumb1FrameLowering.cpp - Thumb1 Frame Information ----------------===//
Anton Korobeynikovf7183ed2010-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 Korobeynikov2f931282011-01-10 12:39:04 +000010// This file contains the Thumb1 implementation of TargetFrameLowering class.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000011//
12//===----------------------------------------------------------------------===//
13
Anton Korobeynikov2f931282011-01-10 12:39:04 +000014#include "Thumb1FrameLowering.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000015#include "ARMMachineFunctionInfo.h"
16#include "llvm/CodeGen/MachineFrameInfo.h"
17#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
Artyom Skrobovf6830f42014-02-14 17:19:07 +000019#include "llvm/CodeGen/MachineModuleInfo.h"
Evan Chengeb56dca2010-11-22 18:12:04 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000021
22using namespace llvm;
23
Jim Grosbache7e2aca2011-09-13 20:30:37 +000024bool Thumb1FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const{
Anton Korobeynikov0eecf5d2010-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 Korobeynikova8d177b2011-03-05 18:43:50 +000037static void
38emitSPUpdate(MachineBasicBlock &MBB,
39 MachineBasicBlock::iterator &MBBI,
40 const TargetInstrInfo &TII, DebugLoc dl,
41 const Thumb1RegisterInfo &MRI,
42 int NumBytes, unsigned MIFlags = MachineInstr::NoFlags) {
Anton Korobeynikove7410dd2011-03-05 18:43:32 +000043 emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, TII,
Anton Korobeynikova8d177b2011-03-05 18:43:50 +000044 MRI, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000045}
46
Eli Bendersky8da87162013-02-21 20:05:00 +000047
48void Thumb1FrameLowering::
49eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
50 MachineBasicBlock::iterator I) const {
51 const Thumb1InstrInfo &TII =
52 *static_cast<const Thumb1InstrInfo*>(MF.getTarget().getInstrInfo());
53 const Thumb1RegisterInfo *RegInfo =
54 static_cast<const Thumb1RegisterInfo*>(MF.getTarget().getRegisterInfo());
55 if (!hasReservedCallFrame(MF)) {
56 // If we have alloca, convert as follows:
57 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
58 // ADJCALLSTACKUP -> add, sp, sp, amount
59 MachineInstr *Old = I;
60 DebugLoc dl = Old->getDebugLoc();
61 unsigned Amount = Old->getOperand(0).getImm();
62 if (Amount != 0) {
63 // We need to keep the stack aligned properly. To do this, we round the
64 // amount of space needed for the outgoing arguments up to the next
65 // alignment boundary.
66 unsigned Align = getStackAlignment();
67 Amount = (Amount+Align-1)/Align*Align;
68
69 // Replace the pseudo instruction with a new instruction...
70 unsigned Opc = Old->getOpcode();
71 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
72 emitSPUpdate(MBB, I, TII, dl, *RegInfo, -Amount);
73 } else {
74 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
75 emitSPUpdate(MBB, I, TII, dl, *RegInfo, Amount);
76 }
77 }
78 }
79 MBB.erase(I);
80}
81
Anton Korobeynikov2f931282011-01-10 12:39:04 +000082void Thumb1FrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000083 MachineBasicBlock &MBB = MF.front();
84 MachineBasicBlock::iterator MBBI = MBB.begin();
85 MachineFrameInfo *MFI = MF.getFrameInfo();
86 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Artyom Skrobovf6830f42014-02-14 17:19:07 +000087 MachineModuleInfo &MMI = MF.getMMI();
88 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000089 const Thumb1RegisterInfo *RegInfo =
90 static_cast<const Thumb1RegisterInfo*>(MF.getTarget().getRegisterInfo());
91 const Thumb1InstrInfo &TII =
92 *static_cast<const Thumb1InstrInfo*>(MF.getTarget().getInstrInfo());
93
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +000094 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
95 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000096 unsigned NumBytes = MFI->getStackSize();
Oliver Stannardd55e1152014-03-05 15:25:27 +000097 assert(NumBytes >= ArgRegsSaveSize &&
98 "ArgRegsSaveSize is included in NumBytes");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000099 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
100 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
101 unsigned FramePtr = RegInfo->getFrameRegister(MF);
102 unsigned BasePtr = RegInfo->getBaseRegister();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000103 int CFAOffset = 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000104
105 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
106 NumBytes = (NumBytes + 3) & ~3;
107 MFI->setStackSize(NumBytes);
108
109 // Determine the sizes of each callee-save spill areas and record which frame
110 // belongs to which callee-save spill areas.
111 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
112 int FramePtrSpillFI = 0;
113
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000114 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000115 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -ArgRegsSaveSize,
Anton Korobeynikova8d177b2011-03-05 18:43:50 +0000116 MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000117 CFAOffset -= ArgRegsSaveSize;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000118 unsigned CFIIndex = MMI.addFrameInst(
119 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
120 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
121 .addCFIIndex(CFIIndex);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000122 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000123
124 if (!AFI->hasStackFrame()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000125 if (NumBytes - ArgRegsSaveSize != 0) {
126 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -(NumBytes - ArgRegsSaveSize),
Anton Korobeynikova8d177b2011-03-05 18:43:50 +0000127 MachineInstr::FrameSetup);
Oliver Stannardd55e1152014-03-05 15:25:27 +0000128 CFAOffset -= NumBytes - ArgRegsSaveSize;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000129 unsigned CFIIndex = MMI.addFrameInst(
130 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
131 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
132 .addCFIIndex(CFIIndex);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000133 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000134 return;
135 }
136
137 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
138 unsigned Reg = CSI[i].getReg();
139 int FI = CSI[i].getFrameIdx();
140 switch (Reg) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000141 case ARM::R8:
142 case ARM::R9:
143 case ARM::R10:
144 case ARM::R11:
145 if (STI.isTargetMachO()) {
146 GPRCS2Size += 4;
147 break;
148 }
149 // fallthrough
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000150 case ARM::R4:
151 case ARM::R5:
152 case ARM::R6:
153 case ARM::R7:
154 case ARM::LR:
155 if (Reg == FramePtr)
156 FramePtrSpillFI = FI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000157 GPRCS1Size += 4;
158 break;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000159 default:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000160 DPRCSSize += 8;
161 }
162 }
163
164 if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
165 ++MBBI;
166 if (MBBI != MBB.end())
167 dl = MBBI->getDebugLoc();
168 }
169
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000170 // Determine starting offsets of spill areas.
Oliver Stannardd55e1152014-03-05 15:25:27 +0000171 unsigned DPRCSOffset = NumBytes - ArgRegsSaveSize - (GPRCS1Size + GPRCS2Size + DPRCSSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000172 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
173 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
Logan Chien53c18d82013-02-20 12:21:33 +0000174 bool HasFP = hasFP(MF);
175 if (HasFP)
176 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
177 NumBytes);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000178 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
179 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
180 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000181 NumBytes = DPRCSOffset;
Evan Chengeb56dca2010-11-22 18:12:04 +0000182
Tim Northover93bcc662013-11-08 17:18:07 +0000183 int FramePtrOffsetInBlock = 0;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000184 unsigned adjustedGPRCS1Size = GPRCS1Size;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000185 if (tryFoldSPUpdateIntoPushPop(STI, MF, std::prev(MBBI), NumBytes)) {
Tim Northover93bcc662013-11-08 17:18:07 +0000186 FramePtrOffsetInBlock = NumBytes;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000187 adjustedGPRCS1Size += NumBytes;
Tim Northover93bcc662013-11-08 17:18:07 +0000188 NumBytes = 0;
189 }
190
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000191 if (adjustedGPRCS1Size) {
192 CFAOffset -= adjustedGPRCS1Size;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000193 unsigned CFIIndex = MMI.addFrameInst(
194 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
195 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
196 .addCFIIndex(CFIIndex);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000197 }
198 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
199 E = CSI.end(); I != E; ++I) {
200 unsigned Reg = I->getReg();
201 int FI = I->getFrameIdx();
202 switch (Reg) {
203 case ARM::R8:
204 case ARM::R9:
205 case ARM::R10:
206 case ARM::R11:
207 case ARM::R12:
208 if (STI.isTargetMachO())
209 break;
210 // fallthough
211 case ARM::R0:
212 case ARM::R1:
213 case ARM::R2:
214 case ARM::R3:
215 case ARM::R4:
216 case ARM::R5:
217 case ARM::R6:
218 case ARM::R7:
219 case ARM::LR:
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000220 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
221 nullptr, MRI->getDwarfRegNum(Reg, true), MFI->getObjectOffset(FI)));
222 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
223 .addCFIIndex(CFIIndex);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000224 break;
225 }
226 }
227
228
Evan Chengeb56dca2010-11-22 18:12:04 +0000229 // Adjust FP so it point to the stack slot that contains the previous FP.
Logan Chien53c18d82013-02-20 12:21:33 +0000230 if (HasFP) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000231 FramePtrOffsetInBlock += MFI->getObjectOffset(FramePtrSpillFI)
232 + GPRCS1Size + ArgRegsSaveSize;
Jim Grosbach1b8457a2011-08-24 17:46:13 +0000233 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDrSPi), FramePtr)
Tim Northover93bcc662013-11-08 17:18:07 +0000234 .addReg(ARM::SP).addImm(FramePtrOffsetInBlock / 4)
Jim Grosbach1b8457a2011-08-24 17:46:13 +0000235 .setMIFlags(MachineInstr::FrameSetup));
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000236 if(FramePtrOffsetInBlock) {
237 CFAOffset += FramePtrOffsetInBlock;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000238 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfa(
239 nullptr, MRI->getDwarfRegNum(FramePtr, true), CFAOffset));
240 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
241 .addCFIIndex(CFIIndex);
242 } else {
243 unsigned CFIIndex =
244 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(
245 nullptr, MRI->getDwarfRegNum(FramePtr, true)));
246 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
247 .addCFIIndex(CFIIndex);
248 }
Jim Grosbachdca85312011-06-13 21:18:25 +0000249 if (NumBytes > 508)
250 // If offset is > 508 then sp cannot be adjusted in a single instruction,
Evan Chengeb56dca2010-11-22 18:12:04 +0000251 // try restoring from fp instead.
252 AFI->setShouldRestoreSPFromFP(true);
253 }
254
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000255 if (NumBytes) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000256 // Insert it after all the callee-save spills.
Anton Korobeynikova8d177b2011-03-05 18:43:50 +0000257 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes,
258 MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000259 if (!HasFP) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000260 CFAOffset -= NumBytes;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000261 unsigned CFIIndex = MMI.addFrameInst(
262 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
263 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
264 .addCFIIndex(CFIIndex);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000265 }
266 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000267
Logan Chien53c18d82013-02-20 12:21:33 +0000268 if (STI.isTargetELF() && HasFP)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000269 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
270 AFI->getFramePtrSpillOffset());
271
272 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
273 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
274 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
275
Chad Rosieradd38c12011-10-20 00:07:12 +0000276 // Thumb1 does not currently support dynamic stack realignment. Report a
277 // fatal error rather then silently generate bad code.
278 if (RegInfo->needsStackRealignment(MF))
279 report_fatal_error("Dynamic stack realignment not supported for thumb1.");
Chad Rosier1809d6c2011-10-15 00:28:24 +0000280
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000281 // If we need a base pointer, set it up here. It's whatever the value
282 // of the stack pointer is at this point. Any variable size objects
283 // will be allocated after this, so we can still use the base pointer
284 // to reference locals.
285 if (RegInfo->hasBasePointer(MF))
Jim Grosbache9cc9012011-06-30 23:38:17 +0000286 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), BasePtr)
Jim Grosbachb98ab912011-06-30 22:10:46 +0000287 .addReg(ARM::SP));
Anton Korobeynikova8d177b2011-03-05 18:43:50 +0000288
Eric Christopher39043432011-01-11 00:16:04 +0000289 // If the frame has variable sized objects then the epilogue must restore
290 // the sp from fp. We can assume there's an FP here since hasFP already
291 // checks for hasVarSizedObjects.
292 if (MFI->hasVarSizedObjects())
293 AFI->setShouldRestoreSPFromFP(true);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000294}
295
Craig Topper420525c2012-03-04 03:33:22 +0000296static bool isCSRestore(MachineInstr *MI, const uint16_t *CSRegs) {
Jim Grosbachd86f34d2011-06-29 20:26:39 +0000297 if (MI->getOpcode() == ARM::tLDRspi &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000298 MI->getOperand(1).isFI() &&
299 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs))
300 return true;
301 else if (MI->getOpcode() == ARM::tPOP) {
302 // The first two operands are predicates. The last two are
303 // imp-def and imp-use of SP. Check everything in between.
304 for (int i = 2, e = MI->getNumOperands() - 2; i != e; ++i)
305 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
306 return false;
307 return true;
308 }
309 return false;
310}
311
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000312void Thumb1FrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000313 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000314 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000315 assert((MBBI->getOpcode() == ARM::tBX_RET ||
316 MBBI->getOpcode() == ARM::tPOP_RET) &&
317 "Can only insert epilog into returning blocks");
318 DebugLoc dl = MBBI->getDebugLoc();
319 MachineFrameInfo *MFI = MF.getFrameInfo();
320 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
321 const Thumb1RegisterInfo *RegInfo =
322 static_cast<const Thumb1RegisterInfo*>(MF.getTarget().getRegisterInfo());
323 const Thumb1InstrInfo &TII =
324 *static_cast<const Thumb1InstrInfo*>(MF.getTarget().getInstrInfo());
325
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +0000326 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
327 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000328 int NumBytes = (int)MFI->getStackSize();
David Blaikie7f4a52e2014-03-05 18:53:36 +0000329 assert((unsigned)NumBytes >= ArgRegsSaveSize &&
Oliver Stannardd55e1152014-03-05 15:25:27 +0000330 "ArgRegsSaveSize is included in NumBytes");
Craig Topper420525c2012-03-04 03:33:22 +0000331 const uint16_t *CSRegs = RegInfo->getCalleeSavedRegs();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000332 unsigned FramePtr = RegInfo->getFrameRegister(MF);
333
334 if (!AFI->hasStackFrame()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000335 if (NumBytes - ArgRegsSaveSize != 0)
336 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes - ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000337 } else {
338 // Unwind MBBI to point to first LDR / VLDRD.
339 if (MBBI != MBB.begin()) {
340 do
341 --MBBI;
342 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
343 if (!isCSRestore(MBBI, CSRegs))
344 ++MBBI;
345 }
346
347 // Move SP to start of FP callee save spill area.
348 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
349 AFI->getGPRCalleeSavedArea2Size() +
Oliver Stannardd55e1152014-03-05 15:25:27 +0000350 AFI->getDPRCalleeSavedAreaSize() +
351 ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000352
353 if (AFI->shouldRestoreSPFromFP()) {
354 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
355 // Reset SP based on frame pointer only if the stack frame extends beyond
Eric Christopher39043432011-01-11 00:16:04 +0000356 // frame pointer stack slot, the target is ELF and the function has FP, or
357 // the target uses var sized objects.
Evan Chengeb56dca2010-11-22 18:12:04 +0000358 if (NumBytes) {
359 assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) &&
360 "No scratch register to restore SP from FP!");
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000361 emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
362 TII, *RegInfo);
Jim Grosbache9cc9012011-06-30 23:38:17 +0000363 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000364 ARM::SP)
365 .addReg(ARM::R4));
Evan Chengeb56dca2010-11-22 18:12:04 +0000366 } else
Jim Grosbache9cc9012011-06-30 23:38:17 +0000367 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000368 ARM::SP)
369 .addReg(FramePtr));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000370 } else {
371 if (MBBI->getOpcode() == ARM::tBX_RET &&
372 &MBB.front() != MBBI &&
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000373 std::prev(MBBI)->getOpcode() == ARM::tPOP) {
374 MachineBasicBlock::iterator PMBBI = std::prev(MBBI);
Tim Northoverdee86042013-12-02 14:46:26 +0000375 if (!tryFoldSPUpdateIntoPushPop(STI, MF, PMBBI, NumBytes))
Tim Northover93bcc662013-11-08 17:18:07 +0000376 emitSPUpdate(MBB, PMBBI, TII, dl, *RegInfo, NumBytes);
Tim Northoverdee86042013-12-02 14:46:26 +0000377 } else if (!tryFoldSPUpdateIntoPushPop(STI, MF, MBBI, NumBytes))
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000378 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
379 }
380 }
381
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000382 if (ArgRegsSaveSize) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000383 // Unlike T2 and ARM mode, the T1 pop instruction cannot restore
384 // to LR, and we can't pop the value directly to the PC since
385 // we need to update the SP after popping the value. Therefore, we
386 // pop the old LR into R3 as a temporary.
387
Tim Northover463a5f22014-01-14 22:53:28 +0000388 // Get the last instruction, tBX_RET
389 MBBI = MBB.getLastNonDebugInstr();
390 assert (MBBI->getOpcode() == ARM::tBX_RET);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000391 // Epilogue for vararg functions: pop LR to R3 and branch off it.
392 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP)))
393 .addReg(ARM::R3, RegState::Define);
394
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000395 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000396
Evan Cheng4882e482012-01-08 20:41:16 +0000397 MachineInstrBuilder MIB =
398 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBX_RET_vararg))
399 .addReg(ARM::R3, RegState::Kill);
400 AddDefaultPred(MIB);
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +0000401 MIB.copyImplicitOps(&*MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000402 // erase the old tBX_RET instruction
403 MBB.erase(MBBI);
404 }
405}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000406
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000407bool Thumb1FrameLowering::
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000408spillCalleeSavedRegisters(MachineBasicBlock &MBB,
409 MachineBasicBlock::iterator MI,
410 const std::vector<CalleeSavedInfo> &CSI,
411 const TargetRegisterInfo *TRI) const {
412 if (CSI.empty())
413 return false;
414
415 DebugLoc DL;
416 MachineFunction &MF = *MBB.getParent();
417 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
418
419 if (MI != MBB.end()) DL = MI->getDebugLoc();
420
421 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(ARM::tPUSH));
422 AddDefaultPred(MIB);
423 for (unsigned i = CSI.size(); i != 0; --i) {
424 unsigned Reg = CSI[i-1].getReg();
425 bool isKill = true;
426
427 // Add the callee-saved register as live-in unless it's LR and
428 // @llvm.returnaddress is called. If LR is returned for @llvm.returnaddress
429 // then it's already added to the function and entry block live-in sets.
430 if (Reg == ARM::LR) {
431 MachineFunction &MF = *MBB.getParent();
432 if (MF.getFrameInfo()->isReturnAddressTaken() &&
433 MF.getRegInfo().isLiveIn(Reg))
434 isKill = false;
435 }
436
437 if (isKill)
438 MBB.addLiveIn(Reg);
439
440 MIB.addReg(Reg, getKillRegState(isKill));
441 }
Anton Korobeynikova8d177b2011-03-05 18:43:50 +0000442 MIB.setMIFlags(MachineInstr::FrameSetup);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000443 return true;
444}
445
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000446bool Thumb1FrameLowering::
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000447restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
448 MachineBasicBlock::iterator MI,
449 const std::vector<CalleeSavedInfo> &CSI,
450 const TargetRegisterInfo *TRI) const {
451 if (CSI.empty())
452 return false;
453
454 MachineFunction &MF = *MBB.getParent();
455 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
456 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
457
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000458 bool isVarArg = AFI->getArgRegsSaveSize() > 0;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000459 DebugLoc DL = MI->getDebugLoc();
460 MachineInstrBuilder MIB = BuildMI(MF, DL, TII.get(ARM::tPOP));
461 AddDefaultPred(MIB);
462
463 bool NumRegs = false;
464 for (unsigned i = CSI.size(); i != 0; --i) {
465 unsigned Reg = CSI[i-1].getReg();
466 if (Reg == ARM::LR) {
467 // Special epilogue for vararg functions. See emitEpilogue
468 if (isVarArg)
469 continue;
470 Reg = ARM::PC;
471 (*MIB).setDesc(TII.get(ARM::tPOP_RET));
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +0000472 MIB.copyImplicitOps(&*MI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000473 MI = MBB.erase(MI);
474 }
475 MIB.addReg(Reg, getDefRegState(true));
476 NumRegs = true;
477 }
478
479 // It's illegal to emit pop instruction without operands.
480 if (NumRegs)
481 MBB.insert(MI, &*MIB);
482 else
483 MF.DeleteMachineInstr(MIB);
484
485 return true;
486}