1 //===-- XCoreFrameLowering.cpp - Frame info for XCore Target --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains XCore frame information that doesn't fit anywhere else
13 //===----------------------------------------------------------------------===//
15 #include "XCoreFrameLowering.h"
17 #include "XCoreInstrInfo.h"
18 #include "XCoreMachineFunctionInfo.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineModuleInfo.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/CodeGen/RegisterScavenging.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Target/TargetOptions.h"
32 // helper functions. FIXME: Eliminate.
33 static inline bool isImmUs(unsigned val) {
37 static inline bool isImmU6(unsigned val) {
38 return val < (1 << 6);
41 static inline bool isImmU16(unsigned val) {
42 return val < (1 << 16);
45 static void loadFromStack(MachineBasicBlock &MBB,
46 MachineBasicBlock::iterator I,
47 unsigned DstReg, int Offset, DebugLoc dl,
48 const TargetInstrInfo &TII) {
49 assert(Offset%4 == 0 && "Misaligned stack offset");
51 bool isU6 = isImmU6(Offset);
52 if (!isU6 && !isImmU16(Offset))
53 report_fatal_error("loadFromStack offset too big " + Twine(Offset));
54 int Opcode = isU6 ? XCore::LDWSP_ru6 : XCore::LDWSP_lru6;
55 BuildMI(MBB, I, dl, TII.get(Opcode), DstReg)
60 static void storeToStack(MachineBasicBlock &MBB,
61 MachineBasicBlock::iterator I,
62 unsigned SrcReg, int Offset, DebugLoc dl,
63 const TargetInstrInfo &TII) {
64 assert(Offset%4 == 0 && "Misaligned stack offset");
66 bool isU6 = isImmU6(Offset);
67 if (!isU6 && !isImmU16(Offset))
68 report_fatal_error("storeToStack offset too big " + Twine(Offset));
69 int Opcode = isU6 ? XCore::STWSP_ru6 : XCore::STWSP_lru6;
70 BuildMI(MBB, I, dl, TII.get(Opcode))
76 //===----------------------------------------------------------------------===//
77 // XCoreFrameLowering:
78 //===----------------------------------------------------------------------===//
80 XCoreFrameLowering::XCoreFrameLowering(const XCoreSubtarget &sti)
81 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, 4, 0) {
85 bool XCoreFrameLowering::hasFP(const MachineFunction &MF) const {
86 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
87 MF.getFrameInfo()->hasVarSizedObjects();
90 void XCoreFrameLowering::emitPrologue(MachineFunction &MF) const {
91 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
92 MachineBasicBlock::iterator MBBI = MBB.begin();
93 MachineFrameInfo *MFI = MF.getFrameInfo();
94 MachineModuleInfo *MMI = &MF.getMMI();
95 const XCoreInstrInfo &TII =
96 *static_cast<const XCoreInstrInfo*>(MF.getTarget().getInstrInfo());
97 XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
98 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
101 const AttributeSet &PAL = MF.getFunction()->getAttributes();
103 if (PAL.hasAttrSomewhere(Attribute::Nest))
104 loadFromStack(MBB, MBBI, XCore::R11, 0, dl, TII);
106 // Work out frame sizes.
107 int FrameSize = MFI->getStackSize();
108 assert(FrameSize%4 == 0 && "Misaligned frame size");
111 bool isU6 = isImmU6(FrameSize);
113 if (!isU6 && !isImmU16(FrameSize)) {
114 // FIXME could emit multiple instructions.
115 report_fatal_error("emitPrologue Frame size too big: " + Twine(FrameSize));
117 bool emitFrameMoves = XCoreRegisterInfo::needsFrameMoves(MF);
119 // Do we need to allocate space on the stack?
121 bool saveLR = XFI->getUsesLR();
122 bool LRSavedOnEntry = false;
124 if (saveLR && (MFI->getObjectOffset(XFI->getLRSpillSlot()) == 0)) {
125 Opcode = (isU6) ? XCore::ENTSP_u6 : XCore::ENTSP_lu6;
126 MBB.addLiveIn(XCore::LR);
128 LRSavedOnEntry = true;
130 Opcode = (isU6) ? XCore::EXTSP_u6 : XCore::EXTSP_lu6;
132 BuildMI(MBB, MBBI, dl, TII.get(Opcode)).addImm(FrameSize);
134 if (emitFrameMoves) {
135 std::vector<MachineMove> &Moves = MMI->getFrameMoves();
137 // Show update of SP.
138 MCSymbol *FrameLabel = MMI->getContext().CreateTempSymbol();
139 BuildMI(MBB, MBBI, dl, TII.get(XCore::PROLOG_LABEL)).addSym(FrameLabel);
141 MachineLocation SPDst(MachineLocation::VirtualFP);
142 MachineLocation SPSrc(MachineLocation::VirtualFP, -FrameSize * 4);
143 Moves.push_back(MachineMove(FrameLabel, SPDst, SPSrc));
145 if (LRSavedOnEntry) {
146 MachineLocation CSDst(MachineLocation::VirtualFP, 0);
147 MachineLocation CSSrc(XCore::LR);
148 Moves.push_back(MachineMove(FrameLabel, CSDst, CSSrc));
152 int LRSpillOffset = MFI->getObjectOffset(XFI->getLRSpillSlot());
153 storeToStack(MBB, MBBI, XCore::LR, LRSpillOffset + FrameSize*4, dl, TII);
154 MBB.addLiveIn(XCore::LR);
156 if (emitFrameMoves) {
157 MCSymbol *SaveLRLabel = MMI->getContext().CreateTempSymbol();
158 BuildMI(MBB, MBBI, dl, TII.get(XCore::PROLOG_LABEL)).addSym(SaveLRLabel);
159 MachineLocation CSDst(MachineLocation::VirtualFP, LRSpillOffset);
160 MachineLocation CSSrc(XCore::LR);
161 MMI->getFrameMoves().push_back(MachineMove(SaveLRLabel, CSDst, CSSrc));
167 // Save R10 to the stack.
168 int FPSpillOffset = MFI->getObjectOffset(XFI->getFPSpillSlot());
169 storeToStack(MBB, MBBI, XCore::R10, FPSpillOffset + FrameSize*4, dl, TII);
170 // R10 is live-in. It is killed at the spill.
171 MBB.addLiveIn(XCore::R10);
172 if (emitFrameMoves) {
173 MCSymbol *SaveR10Label = MMI->getContext().CreateTempSymbol();
174 BuildMI(MBB, MBBI, dl, TII.get(XCore::PROLOG_LABEL)).addSym(SaveR10Label);
175 MachineLocation CSDst(MachineLocation::VirtualFP, FPSpillOffset);
176 MachineLocation CSSrc(XCore::R10);
177 MMI->getFrameMoves().push_back(MachineMove(SaveR10Label, CSDst, CSSrc));
179 // Set the FP from the SP.
180 unsigned FramePtr = XCore::R10;
181 BuildMI(MBB, MBBI, dl, TII.get(XCore::LDAWSP_ru6), FramePtr)
183 if (emitFrameMoves) {
184 // Show FP is now valid.
185 MCSymbol *FrameLabel = MMI->getContext().CreateTempSymbol();
186 BuildMI(MBB, MBBI, dl, TII.get(XCore::PROLOG_LABEL)).addSym(FrameLabel);
187 MachineLocation SPDst(FramePtr);
188 MachineLocation SPSrc(MachineLocation::VirtualFP);
189 MMI->getFrameMoves().push_back(MachineMove(FrameLabel, SPDst, SPSrc));
193 if (emitFrameMoves) {
194 // Frame moves for callee saved.
195 std::vector<MachineMove> &Moves = MMI->getFrameMoves();
196 std::vector<std::pair<MCSymbol*, CalleeSavedInfo> >&SpillLabels =
197 XFI->getSpillLabels();
198 for (unsigned I = 0, E = SpillLabels.size(); I != E; ++I) {
199 MCSymbol *SpillLabel = SpillLabels[I].first;
200 CalleeSavedInfo &CSI = SpillLabels[I].second;
201 int Offset = MFI->getObjectOffset(CSI.getFrameIdx());
202 unsigned Reg = CSI.getReg();
203 MachineLocation CSDst(MachineLocation::VirtualFP, Offset);
204 MachineLocation CSSrc(Reg);
205 Moves.push_back(MachineMove(SpillLabel, CSDst, CSSrc));
210 void XCoreFrameLowering::emitEpilogue(MachineFunction &MF,
211 MachineBasicBlock &MBB) const {
212 MachineFrameInfo *MFI = MF.getFrameInfo();
213 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
214 const XCoreInstrInfo &TII =
215 *static_cast<const XCoreInstrInfo*>(MF.getTarget().getInstrInfo());
216 DebugLoc dl = MBBI->getDebugLoc();
220 // Restore the stack pointer.
221 unsigned FramePtr = XCore::R10;
222 BuildMI(MBB, MBBI, dl, TII.get(XCore::SETSP_1r))
226 // Work out frame sizes.
227 int FrameSize = MFI->getStackSize();
229 assert(FrameSize%4 == 0 && "Misaligned frame size");
233 bool isU6 = isImmU6(FrameSize);
235 if (!isU6 && !isImmU16(FrameSize)) {
236 // FIXME could emit multiple instructions.
237 report_fatal_error("emitEpilogue Frame size too big: " + Twine(FrameSize));
241 XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
245 int FPSpillOffset = MFI->getObjectOffset(XFI->getFPSpillSlot());
246 FPSpillOffset += FrameSize*4;
247 loadFromStack(MBB, MBBI, XCore::R10, FPSpillOffset, dl, TII);
249 bool restoreLR = XFI->getUsesLR();
250 if (restoreLR && MFI->getObjectOffset(XFI->getLRSpillSlot()) != 0) {
251 int LRSpillOffset = MFI->getObjectOffset(XFI->getLRSpillSlot());
252 LRSpillOffset += FrameSize*4;
253 loadFromStack(MBB, MBBI, XCore::LR, LRSpillOffset, dl, TII);
257 // Fold prologue into return instruction
258 assert(MBBI->getOpcode() == XCore::RETSP_u6
259 || MBBI->getOpcode() == XCore::RETSP_lu6);
260 int Opcode = (isU6) ? XCore::RETSP_u6 : XCore::RETSP_lu6;
261 BuildMI(MBB, MBBI, dl, TII.get(Opcode)).addImm(FrameSize);
264 int Opcode = (isU6) ? XCore::LDAWSP_ru6 : XCore::LDAWSP_lru6;
265 BuildMI(MBB, MBBI, dl, TII.get(Opcode), XCore::SP).addImm(FrameSize);
270 bool XCoreFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
271 MachineBasicBlock::iterator MI,
272 const std::vector<CalleeSavedInfo> &CSI,
273 const TargetRegisterInfo *TRI) const {
277 MachineFunction *MF = MBB.getParent();
278 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
280 XCoreFunctionInfo *XFI = MF->getInfo<XCoreFunctionInfo>();
281 bool emitFrameMoves = XCoreRegisterInfo::needsFrameMoves(*MF);
284 if (MI != MBB.end()) DL = MI->getDebugLoc();
286 for (std::vector<CalleeSavedInfo>::const_iterator it = CSI.begin();
287 it != CSI.end(); ++it) {
288 // Add the callee-saved register as live-in. It's killed at the spill.
289 MBB.addLiveIn(it->getReg());
291 unsigned Reg = it->getReg();
292 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
293 TII.storeRegToStackSlot(MBB, MI, Reg, true,
294 it->getFrameIdx(), RC, TRI);
295 if (emitFrameMoves) {
296 MCSymbol *SaveLabel = MF->getContext().CreateTempSymbol();
297 BuildMI(MBB, MI, DL, TII.get(XCore::PROLOG_LABEL)).addSym(SaveLabel);
298 XFI->getSpillLabels().push_back(std::make_pair(SaveLabel, *it));
304 bool XCoreFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
305 MachineBasicBlock::iterator MI,
306 const std::vector<CalleeSavedInfo> &CSI,
307 const TargetRegisterInfo *TRI) const{
308 MachineFunction *MF = MBB.getParent();
309 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
311 bool AtStart = MI == MBB.begin();
312 MachineBasicBlock::iterator BeforeI = MI;
315 for (std::vector<CalleeSavedInfo>::const_iterator it = CSI.begin();
316 it != CSI.end(); ++it) {
317 unsigned Reg = it->getReg();
318 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
319 TII.loadRegFromStackSlot(MBB, MI, it->getReg(), it->getFrameIdx(),
321 assert(MI != MBB.begin() &&
322 "loadRegFromStackSlot didn't insert any code!");
323 // Insert in reverse order. loadRegFromStackSlot can insert multiple
335 // This function eliminates ADJCALLSTACKDOWN,
336 // ADJCALLSTACKUP pseudo instructions
337 void XCoreFrameLowering::
338 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
339 MachineBasicBlock::iterator I) const {
340 const XCoreInstrInfo &TII =
341 *static_cast<const XCoreInstrInfo*>(MF.getTarget().getInstrInfo());
342 if (!hasReservedCallFrame(MF)) {
343 // Turn the adjcallstackdown instruction into 'extsp <amt>' and the
344 // adjcallstackup instruction into 'ldaw sp, sp[<amt>]'
345 MachineInstr *Old = I;
346 uint64_t Amount = Old->getOperand(0).getImm();
348 // We need to keep the stack aligned properly. To do this, we round the
349 // amount of space needed for the outgoing arguments up to the next
350 // alignment boundary.
351 unsigned Align = getStackAlignment();
352 Amount = (Amount+Align-1)/Align*Align;
354 assert(Amount%4 == 0);
357 bool isU6 = isImmU6(Amount);
358 if (!isU6 && !isImmU16(Amount)) {
359 // FIX could emit multiple instructions in this case.
361 errs() << "eliminateCallFramePseudoInstr size too big: "
368 if (Old->getOpcode() == XCore::ADJCALLSTACKDOWN) {
369 int Opcode = isU6 ? XCore::EXTSP_u6 : XCore::EXTSP_lu6;
370 New=BuildMI(MF, Old->getDebugLoc(), TII.get(Opcode))
373 assert(Old->getOpcode() == XCore::ADJCALLSTACKUP);
374 int Opcode = isU6 ? XCore::LDAWSP_ru6 : XCore::LDAWSP_lru6;
375 New=BuildMI(MF, Old->getDebugLoc(), TII.get(Opcode), XCore::SP)
379 // Replace the pseudo instruction with a new instruction...
388 XCoreFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
389 RegScavenger *RS) const {
390 MachineFrameInfo *MFI = MF.getFrameInfo();
391 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
392 bool LRUsed = MF.getRegInfo().isPhysRegUsed(XCore::LR);
393 const TargetRegisterClass *RC = &XCore::GRRegsRegClass;
394 XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
396 MF.getRegInfo().setPhysRegUnused(XCore::LR);
398 bool isVarArg = MF.getFunction()->isVarArg();
401 // A fixed offset of 0 allows us to save / restore LR using entsp / retsp.
402 FrameIdx = MFI->CreateFixedObject(RC->getSize(), 0, true);
404 FrameIdx = MFI->CreateStackObject(RC->getSize(), RC->getAlignment(),
407 XFI->setUsesLR(FrameIdx);
408 XFI->setLRSpillSlot(FrameIdx);
410 if (RegInfo->requiresRegisterScavenging(MF)) {
411 // Reserve a slot close to SP or frame pointer.
412 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
417 // A callee save register is used to hold the FP.
418 // This needs saving / restoring in the epilogue / prologue.
419 XFI->setFPSpillSlot(MFI->CreateStackObject(RC->getSize(),