blob: 46ef889d0f20fa95396e70f6ad6d58ab191a8a10 [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 MCSymbol *SPLabel = MMI.getContext().CreateTempSymbol();
118 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::PROLOG_LABEL))
119 .addSym(SPLabel);
120 CFAOffset -= ArgRegsSaveSize;
121 MMI.addFrameInst(
122 MCCFIInstruction::createDefCfaOffset(SPLabel, CFAOffset));
123 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000124
125 if (!AFI->hasStackFrame()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000126 if (NumBytes - ArgRegsSaveSize != 0) {
127 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -(NumBytes - ArgRegsSaveSize),
Anton Korobeynikova8d177b2011-03-05 18:43:50 +0000128 MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000129 MCSymbol *SPLabel = MMI.getContext().CreateTempSymbol();
130 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::PROLOG_LABEL))
131 .addSym(SPLabel);
Oliver Stannardd55e1152014-03-05 15:25:27 +0000132 CFAOffset -= NumBytes - ArgRegsSaveSize;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000133 MMI.addFrameInst(
134 MCCFIInstruction::createDefCfaOffset(SPLabel, CFAOffset));
135 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000136 return;
137 }
138
139 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
140 unsigned Reg = CSI[i].getReg();
141 int FI = CSI[i].getFrameIdx();
142 switch (Reg) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000143 case ARM::R8:
144 case ARM::R9:
145 case ARM::R10:
146 case ARM::R11:
147 if (STI.isTargetMachO()) {
148 GPRCS2Size += 4;
149 break;
150 }
151 // fallthrough
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000152 case ARM::R4:
153 case ARM::R5:
154 case ARM::R6:
155 case ARM::R7:
156 case ARM::LR:
157 if (Reg == FramePtr)
158 FramePtrSpillFI = FI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000159 GPRCS1Size += 4;
160 break;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000161 default:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000162 DPRCSSize += 8;
163 }
164 }
165
166 if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
167 ++MBBI;
168 if (MBBI != MBB.end())
169 dl = MBBI->getDebugLoc();
170 }
171
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000172 // Determine starting offsets of spill areas.
Oliver Stannardd55e1152014-03-05 15:25:27 +0000173 unsigned DPRCSOffset = NumBytes - ArgRegsSaveSize - (GPRCS1Size + GPRCS2Size + DPRCSSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000174 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
175 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
Logan Chien53c18d82013-02-20 12:21:33 +0000176 bool HasFP = hasFP(MF);
177 if (HasFP)
178 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
179 NumBytes);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000180 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
181 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
182 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000183 NumBytes = DPRCSOffset;
Evan Chengeb56dca2010-11-22 18:12:04 +0000184
Tim Northover93bcc662013-11-08 17:18:07 +0000185 int FramePtrOffsetInBlock = 0;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000186 unsigned adjustedGPRCS1Size = GPRCS1Size;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000187 if (tryFoldSPUpdateIntoPushPop(STI, MF, std::prev(MBBI), NumBytes)) {
Tim Northover93bcc662013-11-08 17:18:07 +0000188 FramePtrOffsetInBlock = NumBytes;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000189 adjustedGPRCS1Size += NumBytes;
Tim Northover93bcc662013-11-08 17:18:07 +0000190 NumBytes = 0;
191 }
192
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000193 MCSymbol *SPLabel = MMI.getContext().CreateTempSymbol();
194 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::PROLOG_LABEL)).addSym(SPLabel);
195 if (adjustedGPRCS1Size) {
196 CFAOffset -= adjustedGPRCS1Size;
197 MMI.addFrameInst(
198 MCCFIInstruction::createDefCfaOffset(SPLabel, CFAOffset));
199 }
200 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
201 E = CSI.end(); I != E; ++I) {
202 unsigned Reg = I->getReg();
203 int FI = I->getFrameIdx();
204 switch (Reg) {
205 case ARM::R8:
206 case ARM::R9:
207 case ARM::R10:
208 case ARM::R11:
209 case ARM::R12:
210 if (STI.isTargetMachO())
211 break;
212 // fallthough
213 case ARM::R0:
214 case ARM::R1:
215 case ARM::R2:
216 case ARM::R3:
217 case ARM::R4:
218 case ARM::R5:
219 case ARM::R6:
220 case ARM::R7:
221 case ARM::LR:
222 MMI.addFrameInst(MCCFIInstruction::createOffset(SPLabel,
223 MRI->getDwarfRegNum(Reg, true),
Oliver Stannardd55e1152014-03-05 15:25:27 +0000224 MFI->getObjectOffset(FI)));
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000225 break;
226 }
227 }
228
229
Evan Chengeb56dca2010-11-22 18:12:04 +0000230 // Adjust FP so it point to the stack slot that contains the previous FP.
Logan Chien53c18d82013-02-20 12:21:33 +0000231 if (HasFP) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000232 FramePtrOffsetInBlock += MFI->getObjectOffset(FramePtrSpillFI)
233 + GPRCS1Size + ArgRegsSaveSize;
Jim Grosbach1b8457a2011-08-24 17:46:13 +0000234 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDrSPi), FramePtr)
Tim Northover93bcc662013-11-08 17:18:07 +0000235 .addReg(ARM::SP).addImm(FramePtrOffsetInBlock / 4)
Jim Grosbach1b8457a2011-08-24 17:46:13 +0000236 .setMIFlags(MachineInstr::FrameSetup));
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000237 MCSymbol *SPLabel = MMI.getContext().CreateTempSymbol();
238 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::PROLOG_LABEL))
239 .addSym(SPLabel);
240 if(FramePtrOffsetInBlock) {
241 CFAOffset += FramePtrOffsetInBlock;
242 MMI.addFrameInst(
243 MCCFIInstruction::createDefCfa(SPLabel,
244 MRI->getDwarfRegNum(FramePtr, true), CFAOffset));
245 } else
246 MMI.addFrameInst(
247 MCCFIInstruction::createDefCfaRegister(SPLabel,
248 MRI->getDwarfRegNum(FramePtr, true)));
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) {
260 MCSymbol *SPLabel = MMI.getContext().CreateTempSymbol();
261 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::PROLOG_LABEL))
262 .addSym(SPLabel);
263 CFAOffset -= NumBytes;
264 MMI.addFrameInst(
265 MCCFIInstruction::createDefCfaOffset(SPLabel, CFAOffset));
266 }
267 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000268
Logan Chien53c18d82013-02-20 12:21:33 +0000269 if (STI.isTargetELF() && HasFP)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000270 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
271 AFI->getFramePtrSpillOffset());
272
273 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
274 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
275 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
276
Chad Rosieradd38c12011-10-20 00:07:12 +0000277 // Thumb1 does not currently support dynamic stack realignment. Report a
278 // fatal error rather then silently generate bad code.
279 if (RegInfo->needsStackRealignment(MF))
280 report_fatal_error("Dynamic stack realignment not supported for thumb1.");
Chad Rosier1809d6c2011-10-15 00:28:24 +0000281
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000282 // If we need a base pointer, set it up here. It's whatever the value
283 // of the stack pointer is at this point. Any variable size objects
284 // will be allocated after this, so we can still use the base pointer
285 // to reference locals.
286 if (RegInfo->hasBasePointer(MF))
Jim Grosbache9cc9012011-06-30 23:38:17 +0000287 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), BasePtr)
Jim Grosbachb98ab912011-06-30 22:10:46 +0000288 .addReg(ARM::SP));
Anton Korobeynikova8d177b2011-03-05 18:43:50 +0000289
Eric Christopher39043432011-01-11 00:16:04 +0000290 // If the frame has variable sized objects then the epilogue must restore
291 // the sp from fp. We can assume there's an FP here since hasFP already
292 // checks for hasVarSizedObjects.
293 if (MFI->hasVarSizedObjects())
294 AFI->setShouldRestoreSPFromFP(true);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000295}
296
Craig Topper420525c2012-03-04 03:33:22 +0000297static bool isCSRestore(MachineInstr *MI, const uint16_t *CSRegs) {
Jim Grosbachd86f34d2011-06-29 20:26:39 +0000298 if (MI->getOpcode() == ARM::tLDRspi &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000299 MI->getOperand(1).isFI() &&
300 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs))
301 return true;
302 else if (MI->getOpcode() == ARM::tPOP) {
303 // The first two operands are predicates. The last two are
304 // imp-def and imp-use of SP. Check everything in between.
305 for (int i = 2, e = MI->getNumOperands() - 2; i != e; ++i)
306 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
307 return false;
308 return true;
309 }
310 return false;
311}
312
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000313void Thumb1FrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000314 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000315 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000316 assert((MBBI->getOpcode() == ARM::tBX_RET ||
317 MBBI->getOpcode() == ARM::tPOP_RET) &&
318 "Can only insert epilog into returning blocks");
319 DebugLoc dl = MBBI->getDebugLoc();
320 MachineFrameInfo *MFI = MF.getFrameInfo();
321 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
322 const Thumb1RegisterInfo *RegInfo =
323 static_cast<const Thumb1RegisterInfo*>(MF.getTarget().getRegisterInfo());
324 const Thumb1InstrInfo &TII =
325 *static_cast<const Thumb1InstrInfo*>(MF.getTarget().getInstrInfo());
326
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +0000327 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
328 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000329 int NumBytes = (int)MFI->getStackSize();
David Blaikie7f4a52e2014-03-05 18:53:36 +0000330 assert((unsigned)NumBytes >= ArgRegsSaveSize &&
Oliver Stannardd55e1152014-03-05 15:25:27 +0000331 "ArgRegsSaveSize is included in NumBytes");
Craig Topper420525c2012-03-04 03:33:22 +0000332 const uint16_t *CSRegs = RegInfo->getCalleeSavedRegs();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000333 unsigned FramePtr = RegInfo->getFrameRegister(MF);
334
335 if (!AFI->hasStackFrame()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000336 if (NumBytes - ArgRegsSaveSize != 0)
337 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes - ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000338 } else {
339 // Unwind MBBI to point to first LDR / VLDRD.
340 if (MBBI != MBB.begin()) {
341 do
342 --MBBI;
343 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
344 if (!isCSRestore(MBBI, CSRegs))
345 ++MBBI;
346 }
347
348 // Move SP to start of FP callee save spill area.
349 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
350 AFI->getGPRCalleeSavedArea2Size() +
Oliver Stannardd55e1152014-03-05 15:25:27 +0000351 AFI->getDPRCalleeSavedAreaSize() +
352 ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000353
354 if (AFI->shouldRestoreSPFromFP()) {
355 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
356 // Reset SP based on frame pointer only if the stack frame extends beyond
Eric Christopher39043432011-01-11 00:16:04 +0000357 // frame pointer stack slot, the target is ELF and the function has FP, or
358 // the target uses var sized objects.
Evan Chengeb56dca2010-11-22 18:12:04 +0000359 if (NumBytes) {
360 assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) &&
361 "No scratch register to restore SP from FP!");
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000362 emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
363 TII, *RegInfo);
Jim Grosbache9cc9012011-06-30 23:38:17 +0000364 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000365 ARM::SP)
366 .addReg(ARM::R4));
Evan Chengeb56dca2010-11-22 18:12:04 +0000367 } else
Jim Grosbache9cc9012011-06-30 23:38:17 +0000368 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000369 ARM::SP)
370 .addReg(FramePtr));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000371 } else {
372 if (MBBI->getOpcode() == ARM::tBX_RET &&
373 &MBB.front() != MBBI &&
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000374 std::prev(MBBI)->getOpcode() == ARM::tPOP) {
375 MachineBasicBlock::iterator PMBBI = std::prev(MBBI);
Tim Northoverdee86042013-12-02 14:46:26 +0000376 if (!tryFoldSPUpdateIntoPushPop(STI, MF, PMBBI, NumBytes))
Tim Northover93bcc662013-11-08 17:18:07 +0000377 emitSPUpdate(MBB, PMBBI, TII, dl, *RegInfo, NumBytes);
Tim Northoverdee86042013-12-02 14:46:26 +0000378 } else if (!tryFoldSPUpdateIntoPushPop(STI, MF, MBBI, NumBytes))
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000379 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
380 }
381 }
382
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000383 if (ArgRegsSaveSize) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000384 // Unlike T2 and ARM mode, the T1 pop instruction cannot restore
385 // to LR, and we can't pop the value directly to the PC since
386 // we need to update the SP after popping the value. Therefore, we
387 // pop the old LR into R3 as a temporary.
388
Tim Northover463a5f22014-01-14 22:53:28 +0000389 // Get the last instruction, tBX_RET
390 MBBI = MBB.getLastNonDebugInstr();
391 assert (MBBI->getOpcode() == ARM::tBX_RET);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000392 // Epilogue for vararg functions: pop LR to R3 and branch off it.
393 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP)))
394 .addReg(ARM::R3, RegState::Define);
395
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000396 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000397
Evan Cheng4882e482012-01-08 20:41:16 +0000398 MachineInstrBuilder MIB =
399 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBX_RET_vararg))
400 .addReg(ARM::R3, RegState::Kill);
401 AddDefaultPred(MIB);
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +0000402 MIB.copyImplicitOps(&*MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000403 // erase the old tBX_RET instruction
404 MBB.erase(MBBI);
405 }
406}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000407
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000408bool Thumb1FrameLowering::
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000409spillCalleeSavedRegisters(MachineBasicBlock &MBB,
410 MachineBasicBlock::iterator MI,
411 const std::vector<CalleeSavedInfo> &CSI,
412 const TargetRegisterInfo *TRI) const {
413 if (CSI.empty())
414 return false;
415
416 DebugLoc DL;
417 MachineFunction &MF = *MBB.getParent();
418 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
419
420 if (MI != MBB.end()) DL = MI->getDebugLoc();
421
422 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(ARM::tPUSH));
423 AddDefaultPred(MIB);
424 for (unsigned i = CSI.size(); i != 0; --i) {
425 unsigned Reg = CSI[i-1].getReg();
426 bool isKill = true;
427
428 // Add the callee-saved register as live-in unless it's LR and
429 // @llvm.returnaddress is called. If LR is returned for @llvm.returnaddress
430 // then it's already added to the function and entry block live-in sets.
431 if (Reg == ARM::LR) {
432 MachineFunction &MF = *MBB.getParent();
433 if (MF.getFrameInfo()->isReturnAddressTaken() &&
434 MF.getRegInfo().isLiveIn(Reg))
435 isKill = false;
436 }
437
438 if (isKill)
439 MBB.addLiveIn(Reg);
440
441 MIB.addReg(Reg, getKillRegState(isKill));
442 }
Anton Korobeynikova8d177b2011-03-05 18:43:50 +0000443 MIB.setMIFlags(MachineInstr::FrameSetup);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000444 return true;
445}
446
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000447bool Thumb1FrameLowering::
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000448restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
449 MachineBasicBlock::iterator MI,
450 const std::vector<CalleeSavedInfo> &CSI,
451 const TargetRegisterInfo *TRI) const {
452 if (CSI.empty())
453 return false;
454
455 MachineFunction &MF = *MBB.getParent();
456 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
457 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
458
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000459 bool isVarArg = AFI->getArgRegsSaveSize() > 0;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000460 DebugLoc DL = MI->getDebugLoc();
461 MachineInstrBuilder MIB = BuildMI(MF, DL, TII.get(ARM::tPOP));
462 AddDefaultPred(MIB);
463
464 bool NumRegs = false;
465 for (unsigned i = CSI.size(); i != 0; --i) {
466 unsigned Reg = CSI[i-1].getReg();
467 if (Reg == ARM::LR) {
468 // Special epilogue for vararg functions. See emitEpilogue
469 if (isVarArg)
470 continue;
471 Reg = ARM::PC;
472 (*MIB).setDesc(TII.get(ARM::tPOP_RET));
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +0000473 MIB.copyImplicitOps(&*MI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000474 MI = MBB.erase(MI);
475 }
476 MIB.addReg(Reg, getDefRegState(true));
477 NumRegs = true;
478 }
479
480 // It's illegal to emit pop instruction without operands.
481 if (NumRegs)
482 MBB.insert(MI, &*MIB);
483 else
484 MF.DeleteMachineInstr(MIB);
485
486 return true;
487}