blob: 79883c100fad763ea8e0663675e4cc7fa405c9c6 [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"
Quentin Colombet71a71482015-07-20 21:42:14 +000016#include "llvm/CodeGen/LivePhysRegs.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
Artyom Skrobovf6830f42014-02-14 17:19:07 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
Evan Chengeb56dca2010-11-22 18:12:04 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000022
23using namespace llvm;
24
Eric Christopher45fb7b62014-06-26 19:29:59 +000025Thumb1FrameLowering::Thumb1FrameLowering(const ARMSubtarget &sti)
26 : ARMFrameLowering(sti) {}
27
Jim Grosbache7e2aca2011-09-13 20:30:37 +000028bool Thumb1FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const{
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000029 const MachineFrameInfo *FFI = MF.getFrameInfo();
30 unsigned CFSize = FFI->getMaxCallFrameSize();
31 // It's not always a good idea to include the call frame as part of the
32 // stack frame. ARM (especially Thumb) has small immediate offset to
33 // address the stack frame. So a large call frame can cause poor codegen
34 // and may even makes it impossible to scavenge a register.
35 if (CFSize >= ((1 << 8) - 1) * 4 / 2) // Half of imm8 * 4
36 return false;
37
38 return !MF.getFrameInfo()->hasVarSizedObjects();
39}
40
Anton Korobeynikova8d177b2011-03-05 18:43:50 +000041static void
42emitSPUpdate(MachineBasicBlock &MBB,
43 MachineBasicBlock::iterator &MBBI,
44 const TargetInstrInfo &TII, DebugLoc dl,
Eric Christopherae326492015-03-12 22:48:50 +000045 const ThumbRegisterInfo &MRI,
Anton Korobeynikova8d177b2011-03-05 18:43:50 +000046 int NumBytes, unsigned MIFlags = MachineInstr::NoFlags) {
Anton Korobeynikove7410dd2011-03-05 18:43:32 +000047 emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, TII,
Anton Korobeynikova8d177b2011-03-05 18:43:50 +000048 MRI, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000049}
50
Eli Bendersky8da87162013-02-21 20:05:00 +000051
52void Thumb1FrameLowering::
53eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
54 MachineBasicBlock::iterator I) const {
55 const Thumb1InstrInfo &TII =
Eric Christopher1b21f002015-01-29 00:19:33 +000056 *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
Eric Christopherae326492015-03-12 22:48:50 +000057 const ThumbRegisterInfo *RegInfo =
58 static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
Eli Bendersky8da87162013-02-21 20:05:00 +000059 if (!hasReservedCallFrame(MF)) {
60 // If we have alloca, convert as follows:
61 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
62 // ADJCALLSTACKUP -> add, sp, sp, amount
63 MachineInstr *Old = I;
64 DebugLoc dl = Old->getDebugLoc();
65 unsigned Amount = Old->getOperand(0).getImm();
66 if (Amount != 0) {
67 // We need to keep the stack aligned properly. To do this, we round the
68 // amount of space needed for the outgoing arguments up to the next
69 // alignment boundary.
70 unsigned Align = getStackAlignment();
71 Amount = (Amount+Align-1)/Align*Align;
72
73 // Replace the pseudo instruction with a new instruction...
74 unsigned Opc = Old->getOpcode();
75 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
76 emitSPUpdate(MBB, I, TII, dl, *RegInfo, -Amount);
77 } else {
78 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
79 emitSPUpdate(MBB, I, TII, dl, *RegInfo, Amount);
80 }
81 }
82 }
83 MBB.erase(I);
84}
85
Quentin Colombet61b305e2015-05-05 17:38:16 +000086void Thumb1FrameLowering::emitPrologue(MachineFunction &MF,
87 MachineBasicBlock &MBB) const {
88 assert(&MBB == &MF.front() && "Shrink-wrapping not yet implemented");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000089 MachineBasicBlock::iterator MBBI = MBB.begin();
90 MachineFrameInfo *MFI = MF.getFrameInfo();
91 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Artyom Skrobovf6830f42014-02-14 17:19:07 +000092 MachineModuleInfo &MMI = MF.getMMI();
93 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Eric Christopherae326492015-03-12 22:48:50 +000094 const ThumbRegisterInfo *RegInfo =
95 static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000096 const Thumb1InstrInfo &TII =
Eric Christopher1b21f002015-01-29 00:19:33 +000097 *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000098
Tim Northover8cda34f2015-03-11 18:54:22 +000099 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000100 unsigned NumBytes = MFI->getStackSize();
Oliver Stannardd55e1152014-03-05 15:25:27 +0000101 assert(NumBytes >= ArgRegsSaveSize &&
102 "ArgRegsSaveSize is included in NumBytes");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000103 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
104 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
105 unsigned FramePtr = RegInfo->getFrameRegister(MF);
106 unsigned BasePtr = RegInfo->getBaseRegister();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000107 int CFAOffset = 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000108
109 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
110 NumBytes = (NumBytes + 3) & ~3;
111 MFI->setStackSize(NumBytes);
112
113 // Determine the sizes of each callee-save spill areas and record which frame
114 // belongs to which callee-save spill areas.
115 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
116 int FramePtrSpillFI = 0;
117
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000118 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000119 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -ArgRegsSaveSize,
Anton Korobeynikova8d177b2011-03-05 18:43:50 +0000120 MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000121 CFAOffset -= ArgRegsSaveSize;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000122 unsigned CFIIndex = MMI.addFrameInst(
123 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
124 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantld9e64b62014-12-22 23:09:14 +0000125 .addCFIIndex(CFIIndex)
126 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000127 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000128
129 if (!AFI->hasStackFrame()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000130 if (NumBytes - ArgRegsSaveSize != 0) {
131 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -(NumBytes - ArgRegsSaveSize),
Anton Korobeynikova8d177b2011-03-05 18:43:50 +0000132 MachineInstr::FrameSetup);
Oliver Stannardd55e1152014-03-05 15:25:27 +0000133 CFAOffset -= NumBytes - ArgRegsSaveSize;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000134 unsigned CFIIndex = MMI.addFrameInst(
135 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
136 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantld9e64b62014-12-22 23:09:14 +0000137 .addCFIIndex(CFIIndex)
138 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000139 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000140 return;
141 }
142
143 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
144 unsigned Reg = CSI[i].getReg();
145 int FI = CSI[i].getFrameIdx();
146 switch (Reg) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000147 case ARM::R8:
148 case ARM::R9:
149 case ARM::R10:
150 case ARM::R11:
151 if (STI.isTargetMachO()) {
152 GPRCS2Size += 4;
153 break;
154 }
155 // fallthrough
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000156 case ARM::R4:
157 case ARM::R5:
158 case ARM::R6:
159 case ARM::R7:
160 case ARM::LR:
161 if (Reg == FramePtr)
162 FramePtrSpillFI = FI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000163 GPRCS1Size += 4;
164 break;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000165 default:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000166 DPRCSSize += 8;
167 }
168 }
169
170 if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
171 ++MBBI;
172 if (MBBI != MBB.end())
173 dl = MBBI->getDebugLoc();
174 }
175
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000176 // Determine starting offsets of spill areas.
Oliver Stannardd55e1152014-03-05 15:25:27 +0000177 unsigned DPRCSOffset = NumBytes - ArgRegsSaveSize - (GPRCS1Size + GPRCS2Size + DPRCSSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000178 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
179 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
Logan Chien53c18d82013-02-20 12:21:33 +0000180 bool HasFP = hasFP(MF);
181 if (HasFP)
182 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
183 NumBytes);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000184 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
185 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
186 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000187 NumBytes = DPRCSOffset;
Evan Chengeb56dca2010-11-22 18:12:04 +0000188
Tim Northover93bcc662013-11-08 17:18:07 +0000189 int FramePtrOffsetInBlock = 0;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000190 unsigned adjustedGPRCS1Size = GPRCS1Size;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000191 if (tryFoldSPUpdateIntoPushPop(STI, MF, std::prev(MBBI), NumBytes)) {
Tim Northover93bcc662013-11-08 17:18:07 +0000192 FramePtrOffsetInBlock = NumBytes;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000193 adjustedGPRCS1Size += NumBytes;
Tim Northover93bcc662013-11-08 17:18:07 +0000194 NumBytes = 0;
195 }
196
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000197 if (adjustedGPRCS1Size) {
198 CFAOffset -= adjustedGPRCS1Size;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000199 unsigned CFIIndex = MMI.addFrameInst(
200 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
201 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantld9e64b62014-12-22 23:09:14 +0000202 .addCFIIndex(CFIIndex)
203 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000204 }
205 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
206 E = CSI.end(); I != E; ++I) {
207 unsigned Reg = I->getReg();
208 int FI = I->getFrameIdx();
209 switch (Reg) {
210 case ARM::R8:
211 case ARM::R9:
212 case ARM::R10:
213 case ARM::R11:
214 case ARM::R12:
215 if (STI.isTargetMachO())
216 break;
217 // fallthough
218 case ARM::R0:
219 case ARM::R1:
220 case ARM::R2:
221 case ARM::R3:
222 case ARM::R4:
223 case ARM::R5:
224 case ARM::R6:
225 case ARM::R7:
226 case ARM::LR:
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000227 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
228 nullptr, MRI->getDwarfRegNum(Reg, true), MFI->getObjectOffset(FI)));
229 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantld9e64b62014-12-22 23:09:14 +0000230 .addCFIIndex(CFIIndex)
231 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000232 break;
233 }
234 }
235
236
Evan Chengeb56dca2010-11-22 18:12:04 +0000237 // Adjust FP so it point to the stack slot that contains the previous FP.
Logan Chien53c18d82013-02-20 12:21:33 +0000238 if (HasFP) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000239 FramePtrOffsetInBlock += MFI->getObjectOffset(FramePtrSpillFI)
240 + GPRCS1Size + ArgRegsSaveSize;
Jim Grosbach1b8457a2011-08-24 17:46:13 +0000241 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDrSPi), FramePtr)
Tim Northover93bcc662013-11-08 17:18:07 +0000242 .addReg(ARM::SP).addImm(FramePtrOffsetInBlock / 4)
Jim Grosbach1b8457a2011-08-24 17:46:13 +0000243 .setMIFlags(MachineInstr::FrameSetup));
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000244 if(FramePtrOffsetInBlock) {
245 CFAOffset += FramePtrOffsetInBlock;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000246 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfa(
247 nullptr, MRI->getDwarfRegNum(FramePtr, true), CFAOffset));
248 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantld9e64b62014-12-22 23:09:14 +0000249 .addCFIIndex(CFIIndex)
250 .setMIFlags(MachineInstr::FrameSetup);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000251 } else {
252 unsigned CFIIndex =
253 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(
254 nullptr, MRI->getDwarfRegNum(FramePtr, true)));
255 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantld9e64b62014-12-22 23:09:14 +0000256 .addCFIIndex(CFIIndex)
257 .setMIFlags(MachineInstr::FrameSetup);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000258 }
Jim Grosbachdca85312011-06-13 21:18:25 +0000259 if (NumBytes > 508)
260 // If offset is > 508 then sp cannot be adjusted in a single instruction,
Evan Chengeb56dca2010-11-22 18:12:04 +0000261 // try restoring from fp instead.
262 AFI->setShouldRestoreSPFromFP(true);
263 }
264
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000265 if (NumBytes) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000266 // Insert it after all the callee-save spills.
Anton Korobeynikova8d177b2011-03-05 18:43:50 +0000267 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes,
268 MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000269 if (!HasFP) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000270 CFAOffset -= NumBytes;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000271 unsigned CFIIndex = MMI.addFrameInst(
272 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
273 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantld9e64b62014-12-22 23:09:14 +0000274 .addCFIIndex(CFIIndex)
275 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000276 }
277 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000278
Logan Chien53c18d82013-02-20 12:21:33 +0000279 if (STI.isTargetELF() && HasFP)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000280 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
281 AFI->getFramePtrSpillOffset());
282
283 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
284 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
285 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
286
Chad Rosieradd38c12011-10-20 00:07:12 +0000287 // Thumb1 does not currently support dynamic stack realignment. Report a
288 // fatal error rather then silently generate bad code.
289 if (RegInfo->needsStackRealignment(MF))
290 report_fatal_error("Dynamic stack realignment not supported for thumb1.");
Chad Rosier1809d6c2011-10-15 00:28:24 +0000291
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000292 // If we need a base pointer, set it up here. It's whatever the value
293 // of the stack pointer is at this point. Any variable size objects
294 // will be allocated after this, so we can still use the base pointer
295 // to reference locals.
296 if (RegInfo->hasBasePointer(MF))
Jim Grosbache9cc9012011-06-30 23:38:17 +0000297 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), BasePtr)
Jim Grosbachb98ab912011-06-30 22:10:46 +0000298 .addReg(ARM::SP));
Anton Korobeynikova8d177b2011-03-05 18:43:50 +0000299
Eric Christopher39043432011-01-11 00:16:04 +0000300 // If the frame has variable sized objects then the epilogue must restore
301 // the sp from fp. We can assume there's an FP here since hasFP already
302 // checks for hasVarSizedObjects.
303 if (MFI->hasVarSizedObjects())
304 AFI->setShouldRestoreSPFromFP(true);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000305}
306
Craig Topper840beec2014-04-04 05:16:06 +0000307static bool isCSRestore(MachineInstr *MI, const MCPhysReg *CSRegs) {
Jim Grosbachd86f34d2011-06-29 20:26:39 +0000308 if (MI->getOpcode() == ARM::tLDRspi &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000309 MI->getOperand(1).isFI() &&
310 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs))
311 return true;
312 else if (MI->getOpcode() == ARM::tPOP) {
313 // The first two operands are predicates. The last two are
314 // imp-def and imp-use of SP. Check everything in between.
315 for (int i = 2, e = MI->getNumOperands() - 2; i != e; ++i)
316 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
317 return false;
318 return true;
319 }
320 return false;
321}
322
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000323void Thumb1FrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000324 MachineBasicBlock &MBB) const {
Quentin Colombet71a71482015-07-20 21:42:14 +0000325 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
326 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000327 MachineFrameInfo *MFI = MF.getFrameInfo();
328 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Eric Christopherae326492015-03-12 22:48:50 +0000329 const ThumbRegisterInfo *RegInfo =
330 static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000331 const Thumb1InstrInfo &TII =
Eric Christopher1b21f002015-01-29 00:19:33 +0000332 *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000333
Tim Northover8cda34f2015-03-11 18:54:22 +0000334 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000335 int NumBytes = (int)MFI->getStackSize();
David Blaikie7f4a52e2014-03-05 18:53:36 +0000336 assert((unsigned)NumBytes >= ArgRegsSaveSize &&
Oliver Stannardd55e1152014-03-05 15:25:27 +0000337 "ArgRegsSaveSize is included in NumBytes");
Eric Christopher7af952872015-03-11 21:41:28 +0000338 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000339 unsigned FramePtr = RegInfo->getFrameRegister(MF);
340
341 if (!AFI->hasStackFrame()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000342 if (NumBytes - ArgRegsSaveSize != 0)
343 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes - ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000344 } else {
345 // Unwind MBBI to point to first LDR / VLDRD.
346 if (MBBI != MBB.begin()) {
347 do
348 --MBBI;
349 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
350 if (!isCSRestore(MBBI, CSRegs))
351 ++MBBI;
352 }
353
354 // Move SP to start of FP callee save spill area.
355 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
356 AFI->getGPRCalleeSavedArea2Size() +
Oliver Stannardd55e1152014-03-05 15:25:27 +0000357 AFI->getDPRCalleeSavedAreaSize() +
358 ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000359
360 if (AFI->shouldRestoreSPFromFP()) {
361 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
362 // Reset SP based on frame pointer only if the stack frame extends beyond
Eric Christopher39043432011-01-11 00:16:04 +0000363 // frame pointer stack slot, the target is ELF and the function has FP, or
364 // the target uses var sized objects.
Evan Chengeb56dca2010-11-22 18:12:04 +0000365 if (NumBytes) {
Matthias Braun02564862015-07-14 17:17:13 +0000366 assert(!MFI->getPristineRegs(MF).test(ARM::R4) &&
Evan Chengeb56dca2010-11-22 18:12:04 +0000367 "No scratch register to restore SP from FP!");
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000368 emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
369 TII, *RegInfo);
Jim Grosbache9cc9012011-06-30 23:38:17 +0000370 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000371 ARM::SP)
372 .addReg(ARM::R4));
Evan Chengeb56dca2010-11-22 18:12:04 +0000373 } else
Jim Grosbache9cc9012011-06-30 23:38:17 +0000374 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000375 ARM::SP)
376 .addReg(FramePtr));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000377 } else {
Quentin Colombet71a71482015-07-20 21:42:14 +0000378 if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tBX_RET &&
379 &MBB.front() != MBBI && std::prev(MBBI)->getOpcode() == ARM::tPOP) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000380 MachineBasicBlock::iterator PMBBI = std::prev(MBBI);
Tim Northoverdee86042013-12-02 14:46:26 +0000381 if (!tryFoldSPUpdateIntoPushPop(STI, MF, PMBBI, NumBytes))
Tim Northover93bcc662013-11-08 17:18:07 +0000382 emitSPUpdate(MBB, PMBBI, TII, dl, *RegInfo, NumBytes);
Tim Northoverdee86042013-12-02 14:46:26 +0000383 } else if (!tryFoldSPUpdateIntoPushPop(STI, MF, MBBI, NumBytes))
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000384 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
385 }
386 }
387
Jonathan Roelofsef84bda2014-08-05 21:32:21 +0000388 bool IsV4PopReturn = false;
389 for (const CalleeSavedInfo &CSI : MFI->getCalleeSavedInfo())
390 if (CSI.getReg() == ARM::LR)
391 IsV4PopReturn = true;
392 IsV4PopReturn &= STI.hasV4TOps() && !STI.hasV5TOps();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000393
Jonathan Roelofsef84bda2014-08-05 21:32:21 +0000394 // Unlike T2 and ARM mode, the T1 pop instruction cannot restore
395 // to LR, and we can't pop the value directly to the PC since
396 // we need to update the SP after popping the value. So instead
397 // we have to emit:
398 // POP {r3}
399 // ADD sp, #offset
400 // BX r3
401 // If this would clobber a return value, then generate this sequence instead:
402 // MOV ip, r3
403 // POP {r3}
404 // ADD sp, #offset
405 // MOV lr, r3
406 // MOV r3, ip
407 // BX lr
408 if (ArgRegsSaveSize || IsV4PopReturn) {
Quentin Colombet71a71482015-07-20 21:42:14 +0000409 // If MBBI is a return instruction, we may be able to directly restore
410 // LR in the PC.
411 // This is possible if we do not need to emit any SP update.
412 // Otherwise, we need a temporary register to pop the value
413 // and copy that value into LR.
414 MBBI = MBB.getFirstTerminator();
415 if (!ArgRegsSaveSize && MBBI != MBB.end() &&
416 MBBI->getOpcode() == ARM::tBX_RET) {
Jonathan Roelofsef84bda2014-08-05 21:32:21 +0000417 MachineInstrBuilder MIB =
Quentin Colombet71a71482015-07-20 21:42:14 +0000418 AddDefaultPred(
419 BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII.get(ARM::tPOP_RET)))
420 .addReg(ARM::PC, RegState::Define);
421 MIB.copyImplicitOps(&*MBBI);
422 // erase the old tBX_RET instruction
423 MBB.erase(MBBI);
424 return;
425 }
426
427 // Look for a temporary register to use.
428 // First, compute the liveness information.
429 LivePhysRegs UsedRegs(STI.getRegisterInfo());
430 UsedRegs.addLiveOuts(&MBB, /*AddPristines*/ true);
431 // The semantic of pristines changed recently and now,
432 // the callee-saved registers that are touched in the function
433 // are not part of the pristines set anymore.
434 // Add those callee-saved now.
435 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
436 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
437 for (unsigned i = 0; CSRegs[i]; ++i)
438 UsedRegs.addReg(CSRegs[i]);
439
440 DebugLoc dl = DebugLoc();
441 if (MBBI != MBB.end()) {
442 dl = MBBI->getDebugLoc();
443 auto InstUpToMBBI = MBB.end();
444 // The post-decrement is on purpose here.
445 // We want to have the liveness right before MBBI.
446 while (InstUpToMBBI-- != MBBI)
447 UsedRegs.stepBackward(*InstUpToMBBI);
448 }
449
450 // Look for a register that can be directly use in the POP.
451 unsigned PopReg = 0;
452 // And some temporary register, just in case.
453 unsigned TemporaryReg = 0;
454 BitVector PopFriendly =
455 TRI->getAllocatableSet(MF, TRI->getRegClass(ARM::tGPRRegClassID));
456 assert(PopFriendly.any() && "No allocatable pop-friendly register?!");
457 // Rebuild the GPRs from the high registers because they are removed
458 // form the GPR reg class for thumb1.
459 BitVector GPRsNoLRSP =
460 TRI->getAllocatableSet(MF, TRI->getRegClass(ARM::hGPRRegClassID));
461 GPRsNoLRSP |= PopFriendly;
462 GPRsNoLRSP.reset(ARM::LR);
463 GPRsNoLRSP.reset(ARM::SP);
464 GPRsNoLRSP.reset(ARM::PC);
465 for (int Register = GPRsNoLRSP.find_first(); Register != -1;
466 Register = GPRsNoLRSP.find_next(Register)) {
467 if (!UsedRegs.contains(Register)) {
468 // Remember the first pop-friendly register and exit.
469 if (PopFriendly.test(Register)) {
470 PopReg = Register;
471 TemporaryReg = 0;
472 break;
473 }
474 // Otherwise, remember that the register will be available to
475 // save a pop-friendly register.
476 TemporaryReg = Register;
477 }
478 }
479
480 assert((PopReg || TemporaryReg) && "Cannot get LR");
481
482 if (TemporaryReg) {
483 assert(!PopReg && "Unnecessary MOV is about to be inserted");
484 PopReg = PopFriendly.find_first();
485 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
486 .addReg(TemporaryReg, RegState::Define)
487 .addReg(PopReg, RegState::Kill));
488 }
489
490 assert(PopReg && "Do not know how to get LR");
491 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP)))
492 .addReg(PopReg, RegState::Define);
493
494 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, ArgRegsSaveSize);
495
496 if (!TemporaryReg && MBBI != MBB.end() &&
497 MBBI->getOpcode() == ARM::tBX_RET) {
498 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(ARM::tBX))
499 .addReg(PopReg, RegState::Kill);
Jonathan Roelofsef84bda2014-08-05 21:32:21 +0000500 AddDefaultPred(MIB);
501 MIB.copyImplicitOps(&*MBBI);
502 // erase the old tBX_RET instruction
503 MBB.erase(MBBI);
Quentin Colombet71a71482015-07-20 21:42:14 +0000504 return;
505 }
506
507 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
508 .addReg(ARM::LR, RegState::Define)
509 .addReg(PopReg, RegState::Kill));
510
511 if (TemporaryReg) {
Jonathan Roelofsef84bda2014-08-05 21:32:21 +0000512 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
Quentin Colombet71a71482015-07-20 21:42:14 +0000513 .addReg(PopReg, RegState::Define)
514 .addReg(TemporaryReg, RegState::Kill));
Jonathan Roelofsef84bda2014-08-05 21:32:21 +0000515 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000516 }
517}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000518
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000519bool Thumb1FrameLowering::
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000520spillCalleeSavedRegisters(MachineBasicBlock &MBB,
521 MachineBasicBlock::iterator MI,
522 const std::vector<CalleeSavedInfo> &CSI,
523 const TargetRegisterInfo *TRI) const {
524 if (CSI.empty())
525 return false;
526
527 DebugLoc DL;
Eric Christopher1b21f002015-01-29 00:19:33 +0000528 const TargetInstrInfo &TII = *STI.getInstrInfo();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000529
530 if (MI != MBB.end()) DL = MI->getDebugLoc();
531
532 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(ARM::tPUSH));
533 AddDefaultPred(MIB);
534 for (unsigned i = CSI.size(); i != 0; --i) {
535 unsigned Reg = CSI[i-1].getReg();
536 bool isKill = true;
537
538 // Add the callee-saved register as live-in unless it's LR and
539 // @llvm.returnaddress is called. If LR is returned for @llvm.returnaddress
540 // then it's already added to the function and entry block live-in sets.
541 if (Reg == ARM::LR) {
542 MachineFunction &MF = *MBB.getParent();
543 if (MF.getFrameInfo()->isReturnAddressTaken() &&
544 MF.getRegInfo().isLiveIn(Reg))
545 isKill = false;
546 }
547
548 if (isKill)
549 MBB.addLiveIn(Reg);
550
551 MIB.addReg(Reg, getKillRegState(isKill));
552 }
Anton Korobeynikova8d177b2011-03-05 18:43:50 +0000553 MIB.setMIFlags(MachineInstr::FrameSetup);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000554 return true;
555}
556
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000557bool Thumb1FrameLowering::
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000558restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
559 MachineBasicBlock::iterator MI,
560 const std::vector<CalleeSavedInfo> &CSI,
561 const TargetRegisterInfo *TRI) const {
562 if (CSI.empty())
563 return false;
564
565 MachineFunction &MF = *MBB.getParent();
566 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Eric Christopher1b21f002015-01-29 00:19:33 +0000567 const TargetInstrInfo &TII = *STI.getInstrInfo();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000568
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000569 bool isVarArg = AFI->getArgRegsSaveSize() > 0;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000570 DebugLoc DL = MI->getDebugLoc();
571 MachineInstrBuilder MIB = BuildMI(MF, DL, TII.get(ARM::tPOP));
572 AddDefaultPred(MIB);
573
574 bool NumRegs = false;
575 for (unsigned i = CSI.size(); i != 0; --i) {
576 unsigned Reg = CSI[i-1].getReg();
Quentin Colombet71a71482015-07-20 21:42:14 +0000577 if (Reg == ARM::LR && MBB.succ_empty()) {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000578 // Special epilogue for vararg functions. See emitEpilogue
579 if (isVarArg)
580 continue;
Jonathan Roelofsef84bda2014-08-05 21:32:21 +0000581 // ARMv4T requires BX, see emitEpilogue
582 if (STI.hasV4TOps() && !STI.hasV5TOps())
583 continue;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000584 Reg = ARM::PC;
585 (*MIB).setDesc(TII.get(ARM::tPOP_RET));
Quentin Colombet71a71482015-07-20 21:42:14 +0000586 if (MI != MBB.end())
587 MIB.copyImplicitOps(&*MI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000588 MI = MBB.erase(MI);
589 }
590 MIB.addReg(Reg, getDefRegState(true));
591 NumRegs = true;
592 }
593
594 // It's illegal to emit pop instruction without operands.
595 if (NumRegs)
596 MBB.insert(MI, &*MIB);
597 else
598 MF.DeleteMachineInstr(MIB);
599
600 return true;
601}