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 static const unsigned FramePtr = XCore::R10;
33 static const int MaxImmU16 = (1<<16) - 1;
35 // helper functions. FIXME: Eliminate.
36 static inline bool isImmU6(unsigned val) {
37 return val < (1 << 6);
40 static inline bool isImmU16(unsigned val) {
41 return val < (1 << 16);
44 static void EmitDefCfaRegister(MachineBasicBlock &MBB,
45 MachineBasicBlock::iterator MBBI, DebugLoc dl,
46 const TargetInstrInfo &TII,
47 MachineModuleInfo *MMI, unsigned DRegNum) {
48 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
49 BuildMI(MBB, MBBI, dl, TII.get(XCore::PROLOG_LABEL)).addSym(Label);
50 MMI->addFrameInst(MCCFIInstruction::createDefCfaRegister(Label, DRegNum));
53 static void EmitDefCfaOffset(MachineBasicBlock &MBB,
54 MachineBasicBlock::iterator MBBI, DebugLoc dl,
55 const TargetInstrInfo &TII,
56 MachineModuleInfo *MMI, int Offset) {
57 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
58 BuildMI(MBB, MBBI, dl, TII.get(XCore::PROLOG_LABEL)).addSym(Label);
59 MMI->addFrameInst(MCCFIInstruction::createDefCfaOffset(Label, -Offset));
62 static void EmitCfiOffset(MachineBasicBlock &MBB,
63 MachineBasicBlock::iterator MBBI, DebugLoc dl,
64 const TargetInstrInfo &TII, MachineModuleInfo *MMI,
65 unsigned DRegNum, int Offset, MCSymbol *Label) {
67 Label = MMI->getContext().CreateTempSymbol();
68 BuildMI(MBB, MBBI, dl, TII.get(XCore::PROLOG_LABEL)).addSym(Label);
70 MMI->addFrameInst(MCCFIInstruction::createOffset(Label, DRegNum, Offset));
73 /// The SP register is moved in steps of 'MaxImmU16' towards the bottom of the
74 /// frame. During these steps, it may be necessary to spill registers.
75 /// IfNeededExtSP emits the necessary EXTSP instructions to move the SP only
76 /// as far as to make 'OffsetFromBottom' reachable using an STWSP_lru6.
77 /// \param OffsetFromTop the spill offset from the top of the frame.
78 /// \param [in,out] Adjusted the current SP offset from the top of the frame.
79 static void IfNeededExtSP(MachineBasicBlock &MBB,
80 MachineBasicBlock::iterator MBBI, DebugLoc dl,
81 const TargetInstrInfo &TII, MachineModuleInfo *MMI,
82 int OffsetFromTop, int &Adjusted, int FrameSize,
83 bool emitFrameMoves) {
84 while (OffsetFromTop > Adjusted) {
85 assert(Adjusted < FrameSize && "OffsetFromTop is beyond FrameSize");
86 int remaining = FrameSize - Adjusted;
87 int OpImm = (remaining > MaxImmU16) ? MaxImmU16 : remaining;
88 int Opcode = isImmU6(OpImm) ? XCore::EXTSP_u6 : XCore::EXTSP_lu6;
89 BuildMI(MBB, MBBI, dl, TII.get(Opcode)).addImm(OpImm);
92 EmitDefCfaOffset(MBB, MBBI, dl, TII, MMI, Adjusted*4);
96 /// The SP register is moved in steps of 'MaxImmU16' towards the top of the
97 /// frame. During these steps, it may be necessary to re-load registers.
98 /// IfNeededLDAWSP emits the necessary LDAWSP instructions to move the SP only
99 /// as far as to make 'OffsetFromTop' reachable using an LDAWSP_lru6.
100 /// \param OffsetFromTop the spill offset from the top of the frame.
101 /// \param [in,out] RemainingAdj the current SP offset from the top of the frame.
102 static void IfNeededLDAWSP(MachineBasicBlock &MBB,
103 MachineBasicBlock::iterator MBBI, DebugLoc dl,
104 const TargetInstrInfo &TII, int OffsetFromTop,
106 while (OffsetFromTop < RemainingAdj - MaxImmU16) {
107 assert(RemainingAdj && "OffsetFromTop is beyond FrameSize");
108 int OpImm = (RemainingAdj > MaxImmU16) ? MaxImmU16 : RemainingAdj;
109 int Opcode = isImmU6(OpImm) ? XCore::LDAWSP_ru6 : XCore::LDAWSP_lru6;
110 BuildMI(MBB, MBBI, dl, TII.get(Opcode), XCore::SP).addImm(OpImm);
111 RemainingAdj -= OpImm;
115 /// Creates an ordered list of registers that are spilled
116 /// during the emitPrologue/emitEpilogue.
117 /// Registers are ordered according to their frame offset.
118 static void GetSpillList(SmallVectorImpl<std::pair<unsigned,int> > &SpillList,
119 MachineFrameInfo *MFI, XCoreFunctionInfo *XFI,
120 bool fetchLR, bool fetchFP) {
121 int LRSpillOffset = fetchLR? MFI->getObjectOffset(XFI->getLRSpillSlot()) : 0;
122 int FPSpillOffset = fetchFP? MFI->getObjectOffset(XFI->getFPSpillSlot()) : 0;
123 if (fetchLR && fetchFP && LRSpillOffset > FPSpillOffset) {
124 SpillList.push_back(std::pair<unsigned, int>(XCore::LR, LRSpillOffset));
128 SpillList.push_back(std::pair<unsigned, int>(FramePtr, FPSpillOffset));
130 SpillList.push_back(std::pair<unsigned, int>(XCore::LR, LRSpillOffset));
134 //===----------------------------------------------------------------------===//
135 // XCoreFrameLowering:
136 //===----------------------------------------------------------------------===//
138 XCoreFrameLowering::XCoreFrameLowering(const XCoreSubtarget &sti)
139 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, 4, 0) {
143 bool XCoreFrameLowering::hasFP(const MachineFunction &MF) const {
144 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
145 MF.getFrameInfo()->hasVarSizedObjects();
148 void XCoreFrameLowering::emitPrologue(MachineFunction &MF) const {
149 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
150 MachineBasicBlock::iterator MBBI = MBB.begin();
151 MachineFrameInfo *MFI = MF.getFrameInfo();
152 MachineModuleInfo *MMI = &MF.getMMI();
153 const MCRegisterInfo *MRI = MMI->getContext().getRegisterInfo();
154 const XCoreInstrInfo &TII =
155 *static_cast<const XCoreInstrInfo*>(MF.getTarget().getInstrInfo());
156 XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
157 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
159 if (MFI->getMaxAlignment() > getStackAlignment())
160 report_fatal_error("emitPrologue unsupported alignment: "
161 + Twine(MFI->getMaxAlignment()));
163 const AttributeSet &PAL = MF.getFunction()->getAttributes();
164 if (PAL.hasAttrSomewhere(Attribute::Nest))
165 BuildMI(MBB, MBBI, dl, TII.get(XCore::LDWSP_ru6), XCore::R11).addImm(0);
167 // Work out frame sizes.
168 // We will adjust the SP in stages towards the final FrameSize.
169 assert(MFI->getStackSize()%4 == 0 && "Misaligned frame size");
170 const int FrameSize = MFI->getStackSize() / 4;
173 bool saveLR = XFI->getUsesLR();
174 bool UseENTSP = saveLR && FrameSize
175 && (MFI->getObjectOffset(XFI->getLRSpillSlot()) == 0);
179 bool emitFrameMoves = XCoreRegisterInfo::needsFrameMoves(MF);
182 // Allocate space on the stack at the same time as saving LR.
183 Adjusted = (FrameSize > MaxImmU16) ? MaxImmU16 : FrameSize;
184 int Opcode = isImmU6(Adjusted) ? XCore::ENTSP_u6 : XCore::ENTSP_lu6;
185 BuildMI(MBB, MBBI, dl, TII.get(Opcode)).addImm(Adjusted);
186 MBB.addLiveIn(XCore::LR);
187 if (emitFrameMoves) {
188 EmitDefCfaOffset(MBB, MBBI, dl, TII, MMI, Adjusted*4);
189 unsigned DRegNum = MRI->getDwarfRegNum(XCore::LR, true);
190 EmitCfiOffset(MBB, MBBI, dl, TII, MMI, DRegNum, 0, NULL);
194 // If necessary, save LR and FP to the stack, as we EXTSP.
195 SmallVector<std::pair<unsigned,int>,2> SpillList;
196 GetSpillList(SpillList, MFI, XFI, saveLR, FP);
197 for (unsigned i = 0, e = SpillList.size(); i != e; ++i) {
198 unsigned SpillReg = SpillList[i].first;
199 int SpillOffset = SpillList[i].second;
200 assert(SpillOffset % 4 == 0 && "Misaligned stack offset");
201 assert(SpillOffset <= 0 && "Unexpected positive stack offset");
202 int OffsetFromTop = - SpillOffset/4;
203 IfNeededExtSP(MBB, MBBI, dl, TII, MMI, OffsetFromTop, Adjusted, FrameSize,
205 int Offset = Adjusted - OffsetFromTop;
206 int Opcode = isImmU6(Offset) ? XCore::STWSP_ru6 : XCore::STWSP_lru6;
207 BuildMI(MBB, MBBI, dl, TII.get(Opcode)).addReg(SpillReg).addImm(Offset);
208 MBB.addLiveIn(SpillReg);
209 if (emitFrameMoves) {
210 unsigned DRegNum = MRI->getDwarfRegNum(SpillReg, true);
211 EmitCfiOffset(MBB, MBBI, dl, TII, MMI, DRegNum, SpillOffset, NULL);
215 // Complete any remaining Stack adjustment.
216 IfNeededExtSP(MBB, MBBI, dl, TII, MMI, FrameSize, Adjusted, FrameSize,
218 assert(Adjusted==FrameSize && "IfNeededExtSP has not completed adjustment");
221 // Set the FP from the SP.
222 BuildMI(MBB, MBBI, dl, TII.get(XCore::LDAWSP_ru6), FramePtr).addImm(0);
224 EmitDefCfaRegister(MBB, MBBI, dl, TII, MMI,
225 MRI->getDwarfRegNum(FramePtr, true));
228 if (emitFrameMoves) {
229 // Frame moves for callee saved.
230 std::vector<std::pair<MCSymbol*, CalleeSavedInfo> >&SpillLabels =
231 XFI->getSpillLabels();
232 for (unsigned I = 0, E = SpillLabels.size(); I != E; ++I) {
233 MCSymbol *SpillLabel = SpillLabels[I].first;
234 CalleeSavedInfo &CSI = SpillLabels[I].second;
235 int Offset = MFI->getObjectOffset(CSI.getFrameIdx());
236 unsigned DRegNum = MRI->getDwarfRegNum(CSI.getReg(), true);
237 EmitCfiOffset(MBB, MBBI, dl, TII, MMI, DRegNum, Offset, SpillLabel);
242 void XCoreFrameLowering::emitEpilogue(MachineFunction &MF,
243 MachineBasicBlock &MBB) const {
244 MachineFrameInfo *MFI = MF.getFrameInfo();
245 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
246 const XCoreInstrInfo &TII =
247 *static_cast<const XCoreInstrInfo*>(MF.getTarget().getInstrInfo());
248 XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
249 DebugLoc dl = MBBI->getDebugLoc();
251 // Work out frame sizes.
252 // We will adjust the SP in stages towards the final FrameSize.
253 int RemainingAdj = MFI->getStackSize();
254 assert(RemainingAdj%4 == 0 && "Misaligned frame size");
257 bool restoreLR = XFI->getUsesLR();
258 bool UseRETSP = restoreLR && RemainingAdj
259 && (MFI->getObjectOffset(XFI->getLRSpillSlot()) == 0);
264 if (FP) // Restore the stack pointer.
265 BuildMI(MBB, MBBI, dl, TII.get(XCore::SETSP_1r)).addReg(FramePtr);
267 // If necessary, restore LR and FP from the stack, as we EXTSP.
268 SmallVector<std::pair<unsigned,int>,2> SpillList;
269 GetSpillList(SpillList, MFI, XFI, restoreLR, FP);
270 unsigned i = SpillList.size();
272 unsigned SpilledReg = SpillList[i].first;
273 int SpillOffset = SpillList[i].second;
274 assert(SpillOffset % 4 == 0 && "Misaligned stack offset");
275 assert(SpillOffset <= 0 && "Unexpected positive stack offset");
276 int OffsetFromTop = - SpillOffset/4;
277 IfNeededLDAWSP(MBB, MBBI, dl, TII, OffsetFromTop, RemainingAdj);
278 int Offset = RemainingAdj - OffsetFromTop;
279 int Opcode = isImmU6(Offset) ? XCore::LDWSP_ru6 : XCore::LDWSP_lru6;
280 BuildMI(MBB, MBBI, dl, TII.get(Opcode), SpilledReg).addImm(Offset);
284 // Complete all but one of the remaining Stack adjustments.
285 IfNeededLDAWSP(MBB, MBBI, dl, TII, 0, RemainingAdj);
287 // Fold prologue into return instruction
288 assert(MBBI->getOpcode() == XCore::RETSP_u6
289 || MBBI->getOpcode() == XCore::RETSP_lu6);
290 int Opcode = isImmU6(RemainingAdj) ? XCore::RETSP_u6 : XCore::RETSP_lu6;
291 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(Opcode))
292 .addImm(RemainingAdj);
293 for (unsigned i = 3, e = MBBI->getNumOperands(); i < e; ++i)
294 MIB->addOperand(MBBI->getOperand(i)); // copy any variadic operands
295 MBB.erase(MBBI); // Erase the previous return instruction.
297 int Opcode = isImmU6(RemainingAdj) ? XCore::LDAWSP_ru6 :
299 BuildMI(MBB, MBBI, dl, TII.get(Opcode), XCore::SP).addImm(RemainingAdj);
300 // Don't erase the return instruction.
302 } // else Don't erase the return instruction.
305 bool XCoreFrameLowering::
306 spillCalleeSavedRegisters(MachineBasicBlock &MBB,
307 MachineBasicBlock::iterator MI,
308 const std::vector<CalleeSavedInfo> &CSI,
309 const TargetRegisterInfo *TRI) const {
313 MachineFunction *MF = MBB.getParent();
314 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
316 XCoreFunctionInfo *XFI = MF->getInfo<XCoreFunctionInfo>();
317 bool emitFrameMoves = XCoreRegisterInfo::needsFrameMoves(*MF);
321 DL = MI->getDebugLoc();
323 for (std::vector<CalleeSavedInfo>::const_iterator it = CSI.begin();
324 it != CSI.end(); ++it) {
325 // Add the callee-saved register as live-in. It's killed at the spill.
326 MBB.addLiveIn(it->getReg());
328 unsigned Reg = it->getReg();
329 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
330 TII.storeRegToStackSlot(MBB, MI, Reg, true,
331 it->getFrameIdx(), RC, TRI);
332 if (emitFrameMoves) {
333 MCSymbol *SaveLabel = MF->getContext().CreateTempSymbol();
334 BuildMI(MBB, MI, DL, TII.get(XCore::PROLOG_LABEL)).addSym(SaveLabel);
335 XFI->getSpillLabels().push_back(std::make_pair(SaveLabel, *it));
341 bool XCoreFrameLowering::
342 restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
343 MachineBasicBlock::iterator MI,
344 const std::vector<CalleeSavedInfo> &CSI,
345 const TargetRegisterInfo *TRI) const{
346 MachineFunction *MF = MBB.getParent();
347 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
349 bool AtStart = MI == MBB.begin();
350 MachineBasicBlock::iterator BeforeI = MI;
353 for (std::vector<CalleeSavedInfo>::const_iterator it = CSI.begin();
354 it != CSI.end(); ++it) {
355 unsigned Reg = it->getReg();
356 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
357 TII.loadRegFromStackSlot(MBB, MI, it->getReg(), it->getFrameIdx(),
359 assert(MI != MBB.begin() &&
360 "loadRegFromStackSlot didn't insert any code!");
361 // Insert in reverse order. loadRegFromStackSlot can insert multiple
373 // This function eliminates ADJCALLSTACKDOWN,
374 // ADJCALLSTACKUP pseudo instructions
375 void XCoreFrameLowering::
376 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
377 MachineBasicBlock::iterator I) const {
378 const XCoreInstrInfo &TII =
379 *static_cast<const XCoreInstrInfo*>(MF.getTarget().getInstrInfo());
380 if (!hasReservedCallFrame(MF)) {
381 // Turn the adjcallstackdown instruction into 'extsp <amt>' and the
382 // adjcallstackup instruction into 'ldaw sp, sp[<amt>]'
383 MachineInstr *Old = I;
384 uint64_t Amount = Old->getOperand(0).getImm();
386 // We need to keep the stack aligned properly. To do this, we round the
387 // amount of space needed for the outgoing arguments up to the next
388 // alignment boundary.
389 unsigned Align = getStackAlignment();
390 Amount = (Amount+Align-1)/Align*Align;
392 assert(Amount%4 == 0);
395 bool isU6 = isImmU6(Amount);
396 if (!isU6 && !isImmU16(Amount)) {
397 // FIX could emit multiple instructions in this case.
399 errs() << "eliminateCallFramePseudoInstr size too big: "
406 if (Old->getOpcode() == XCore::ADJCALLSTACKDOWN) {
407 int Opcode = isU6 ? XCore::EXTSP_u6 : XCore::EXTSP_lu6;
408 New=BuildMI(MF, Old->getDebugLoc(), TII.get(Opcode))
411 assert(Old->getOpcode() == XCore::ADJCALLSTACKUP);
412 int Opcode = isU6 ? XCore::LDAWSP_ru6 : XCore::LDAWSP_lru6;
413 New=BuildMI(MF, Old->getDebugLoc(), TII.get(Opcode), XCore::SP)
417 // Replace the pseudo instruction with a new instruction...
425 void XCoreFrameLowering::
426 processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
427 RegScavenger *RS) const {
428 MachineFrameInfo *MFI = MF.getFrameInfo();
429 bool LRUsed = MF.getRegInfo().isPhysRegUsed(XCore::LR);
430 const TargetRegisterClass *RC = &XCore::GRRegsRegClass;
431 XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
433 MF.getRegInfo().setPhysRegUnused(XCore::LR);
435 bool isVarArg = MF.getFunction()->isVarArg();
438 // A fixed offset of 0 allows us to save/restore LR using entsp/retsp.
439 FrameIdx = MFI->CreateFixedObject(RC->getSize(), 0, true);
441 FrameIdx = MFI->CreateStackObject(RC->getSize(), RC->getAlignment(),
444 XFI->setUsesLR(FrameIdx);
445 XFI->setLRSpillSlot(FrameIdx);
448 // A callee save register is used to hold the FP.
449 // This needs saving / restoring in the epilogue / prologue.
451 XFI->setFPSpillSlot(MFI->CreateStackObject(RC->getSize(),
456 void XCoreFrameLowering::
457 processFunctionBeforeFrameFinalized(MachineFunction &MF,
458 RegScavenger *RS) const {
459 assert(RS && "requiresRegisterScavenging failed");
460 MachineFrameInfo *MFI = MF.getFrameInfo();
461 const TargetRegisterClass *RC = &XCore::GRRegsRegClass;
462 XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
463 // Reserve slots close to SP or frame pointer for Scavenging spills.
464 // When using SP for small frames, we don't need any scratch registers.
465 // When using SP for large frames, we may need 2 scratch registers.
466 // When using FP, for large or small frames, we may need 1 scratch register.
467 if (XFI->isLargeFrame(MF) || hasFP(MF))
468 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
471 if (XFI->isLargeFrame(MF) && !hasFP(MF))
472 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),