Change getFrameMoves to return a const reference.
[oota-llvm.git] / lib / Target / XCore / XCoreFrameLowering.cpp
1 //===-- XCoreFrameLowering.cpp - Frame info for XCore Target --------------===//
2 //
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 //
10 // This file contains XCore frame information that doesn't fit anywhere else
11 // cleanly...
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "XCoreFrameLowering.h"
16 #include "XCore.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"
29
30 using namespace llvm;
31
32 // helper functions. FIXME: Eliminate.
33 static inline bool isImmUs(unsigned val) {
34   return val <= 11;
35 }
36
37 static inline bool isImmU6(unsigned val) {
38   return val < (1 << 6);
39 }
40
41 static inline bool isImmU16(unsigned val) {
42   return val < (1 << 16);
43 }
44
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");
50   Offset/=4;
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)
56     .addImm(Offset);
57 }
58
59
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");
65   Offset/=4;
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))
71     .addReg(SrcReg)
72     .addImm(Offset);
73 }
74
75
76 //===----------------------------------------------------------------------===//
77 // XCoreFrameLowering:
78 //===----------------------------------------------------------------------===//
79
80 XCoreFrameLowering::XCoreFrameLowering(const XCoreSubtarget &sti)
81   : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, 4, 0) {
82   // Do nothing
83 }
84
85 bool XCoreFrameLowering::hasFP(const MachineFunction &MF) const {
86   return MF.getTarget().Options.DisableFramePointerElim(MF) ||
87     MF.getFrameInfo()->hasVarSizedObjects();
88 }
89
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();
99
100   bool FP = hasFP(MF);
101   const AttributeSet &PAL = MF.getFunction()->getAttributes();
102
103   if (PAL.hasAttrSomewhere(Attribute::Nest))
104     loadFromStack(MBB, MBBI, XCore::R11, 0, dl, TII);
105
106   // Work out frame sizes.
107   int FrameSize = MFI->getStackSize();
108   assert(FrameSize%4 == 0 && "Misaligned frame size");
109   FrameSize/=4;
110
111   bool isU6 = isImmU6(FrameSize);
112
113   if (!isU6 && !isImmU16(FrameSize)) {
114     // FIXME could emit multiple instructions.
115     report_fatal_error("emitPrologue Frame size too big: " + Twine(FrameSize));
116   }
117   bool emitFrameMoves = XCoreRegisterInfo::needsFrameMoves(MF);
118
119   bool saveLR = XFI->getUsesLR();
120   // Do we need to allocate space on the stack?
121   if (FrameSize) {
122     bool LRSavedOnEntry = false;
123     int Opcode;
124     if (saveLR && (MFI->getObjectOffset(XFI->getLRSpillSlot()) == 0)) {
125       Opcode = (isU6) ? XCore::ENTSP_u6 : XCore::ENTSP_lu6;
126       MBB.addLiveIn(XCore::LR);
127       saveLR = false;
128       LRSavedOnEntry = true;
129     } else {
130       Opcode = (isU6) ? XCore::EXTSP_u6 : XCore::EXTSP_lu6;
131     }
132     BuildMI(MBB, MBBI, dl, TII.get(Opcode)).addImm(FrameSize);
133
134     if (emitFrameMoves) {
135
136       // Show update of SP.
137       MCSymbol *FrameLabel = MMI->getContext().CreateTempSymbol();
138       BuildMI(MBB, MBBI, dl, TII.get(XCore::PROLOG_LABEL)).addSym(FrameLabel);
139
140       MachineLocation SPDst(MachineLocation::VirtualFP);
141       MachineLocation SPSrc(MachineLocation::VirtualFP, -FrameSize * 4);
142       MMI->addFrameMove(FrameLabel, SPDst, SPSrc);
143
144       if (LRSavedOnEntry) {
145         MachineLocation CSDst(MachineLocation::VirtualFP, 0);
146         MachineLocation CSSrc(XCore::LR);
147         MMI->addFrameMove(FrameLabel, CSDst, CSSrc);
148       }
149     }
150   }
151   if (saveLR) {
152     int LRSpillOffset = MFI->getObjectOffset(XFI->getLRSpillSlot());
153     storeToStack(MBB, MBBI, XCore::LR, LRSpillOffset + FrameSize*4, dl, TII);
154     MBB.addLiveIn(XCore::LR);
155
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->addFrameMove(SaveLRLabel, CSDst, CSSrc);
162     }
163   }
164
165   if (FP) {
166     // Save R10 to the stack.
167     int FPSpillOffset = MFI->getObjectOffset(XFI->getFPSpillSlot());
168     storeToStack(MBB, MBBI, XCore::R10, FPSpillOffset + FrameSize*4, dl, TII);
169     // R10 is live-in. It is killed at the spill.
170     MBB.addLiveIn(XCore::R10);
171     if (emitFrameMoves) {
172       MCSymbol *SaveR10Label = MMI->getContext().CreateTempSymbol();
173       BuildMI(MBB, MBBI, dl, TII.get(XCore::PROLOG_LABEL)).addSym(SaveR10Label);
174       MachineLocation CSDst(MachineLocation::VirtualFP, FPSpillOffset);
175       MachineLocation CSSrc(XCore::R10);
176       MMI->addFrameMove(SaveR10Label, CSDst, CSSrc);
177     }
178     // Set the FP from the SP.
179     unsigned FramePtr = XCore::R10;
180     BuildMI(MBB, MBBI, dl, TII.get(XCore::LDAWSP_ru6), FramePtr)
181       .addImm(0);
182     if (emitFrameMoves) {
183       // Show FP is now valid.
184       MCSymbol *FrameLabel = MMI->getContext().CreateTempSymbol();
185       BuildMI(MBB, MBBI, dl, TII.get(XCore::PROLOG_LABEL)).addSym(FrameLabel);
186       MachineLocation SPDst(FramePtr);
187       MachineLocation SPSrc(MachineLocation::VirtualFP);
188       MMI->addFrameMove(FrameLabel, SPDst, SPSrc);
189     }
190   }
191
192   if (emitFrameMoves) {
193     // Frame moves for callee saved.
194     std::vector<std::pair<MCSymbol*, CalleeSavedInfo> >&SpillLabels =
195         XFI->getSpillLabels();
196     for (unsigned I = 0, E = SpillLabels.size(); I != E; ++I) {
197       MCSymbol *SpillLabel = SpillLabels[I].first;
198       CalleeSavedInfo &CSI = SpillLabels[I].second;
199       int Offset = MFI->getObjectOffset(CSI.getFrameIdx());
200       unsigned Reg = CSI.getReg();
201       MachineLocation CSDst(MachineLocation::VirtualFP, Offset);
202       MachineLocation CSSrc(Reg);
203       MMI->addFrameMove(SpillLabel, CSDst, CSSrc);
204     }
205   }
206 }
207
208 void XCoreFrameLowering::emitEpilogue(MachineFunction &MF,
209                                      MachineBasicBlock &MBB) const {
210   MachineFrameInfo *MFI            = MF.getFrameInfo();
211   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
212   const XCoreInstrInfo &TII =
213     *static_cast<const XCoreInstrInfo*>(MF.getTarget().getInstrInfo());
214   XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
215   DebugLoc dl = MBBI->getDebugLoc();
216
217   bool FP = hasFP(MF);
218   if (FP) {
219     // Restore the stack pointer.
220     unsigned FramePtr = XCore::R10;
221     BuildMI(MBB, MBBI, dl, TII.get(XCore::SETSP_1r))
222       .addReg(FramePtr);
223   }
224
225   // Work out frame sizes.
226   int FrameSize = MFI->getStackSize();
227
228   assert(FrameSize%4 == 0 && "Misaligned frame size");
229
230   FrameSize/=4;
231
232   bool isU6 = isImmU6(FrameSize);
233
234   if (!isU6 && !isImmU16(FrameSize)) {
235     // FIXME could emit multiple instructions.
236     report_fatal_error("emitEpilogue Frame size too big: " + Twine(FrameSize));
237   }
238
239   if (FP) {
240     // Restore R10
241     int FPSpillOffset = MFI->getObjectOffset(XFI->getFPSpillSlot());
242     FPSpillOffset += FrameSize*4;
243     loadFromStack(MBB, MBBI, XCore::R10, FPSpillOffset, dl, TII);
244   }
245
246   bool restoreLR = XFI->getUsesLR();
247   if (restoreLR &&
248       (FrameSize == 0 || MFI->getObjectOffset(XFI->getLRSpillSlot()) != 0)) {
249     int LRSpillOffset = MFI->getObjectOffset(XFI->getLRSpillSlot());
250     LRSpillOffset += FrameSize*4;
251     loadFromStack(MBB, MBBI, XCore::LR, LRSpillOffset, dl, TII);
252     restoreLR = false;
253   }
254
255   if (FrameSize) {
256     if (restoreLR) {
257       // Fold prologue into return instruction
258       assert(MFI->getObjectOffset(XFI->getLRSpillSlot()) == 0);
259       assert(MBBI->getOpcode() == XCore::RETSP_u6
260         || MBBI->getOpcode() == XCore::RETSP_lu6);
261       int Opcode = (isU6) ? XCore::RETSP_u6 : XCore::RETSP_lu6;
262       BuildMI(MBB, MBBI, dl, TII.get(Opcode)).addImm(FrameSize);
263       MBB.erase(MBBI);
264     } else {
265       int Opcode = (isU6) ? XCore::LDAWSP_ru6 : XCore::LDAWSP_lru6;
266       BuildMI(MBB, MBBI, dl, TII.get(Opcode), XCore::SP).addImm(FrameSize);
267     }
268   }
269 }
270
271 bool XCoreFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
272                                                MachineBasicBlock::iterator MI,
273                                         const std::vector<CalleeSavedInfo> &CSI,
274                                           const TargetRegisterInfo *TRI) const {
275   if (CSI.empty())
276     return true;
277
278   MachineFunction *MF = MBB.getParent();
279   const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
280
281   XCoreFunctionInfo *XFI = MF->getInfo<XCoreFunctionInfo>();
282   bool emitFrameMoves = XCoreRegisterInfo::needsFrameMoves(*MF);
283
284   DebugLoc DL;
285   if (MI != MBB.end()) DL = MI->getDebugLoc();
286
287   for (std::vector<CalleeSavedInfo>::const_iterator it = CSI.begin();
288                                                     it != CSI.end(); ++it) {
289     // Add the callee-saved register as live-in. It's killed at the spill.
290     MBB.addLiveIn(it->getReg());
291
292     unsigned Reg = it->getReg();
293     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
294     TII.storeRegToStackSlot(MBB, MI, Reg, true,
295                             it->getFrameIdx(), RC, TRI);
296     if (emitFrameMoves) {
297       MCSymbol *SaveLabel = MF->getContext().CreateTempSymbol();
298       BuildMI(MBB, MI, DL, TII.get(XCore::PROLOG_LABEL)).addSym(SaveLabel);
299       XFI->getSpillLabels().push_back(std::make_pair(SaveLabel, *it));
300     }
301   }
302   return true;
303 }
304
305 bool XCoreFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
306                                                  MachineBasicBlock::iterator MI,
307                                         const std::vector<CalleeSavedInfo> &CSI,
308                                             const TargetRegisterInfo *TRI) const{
309   MachineFunction *MF = MBB.getParent();
310   const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
311
312   bool AtStart = MI == MBB.begin();
313   MachineBasicBlock::iterator BeforeI = MI;
314   if (!AtStart)
315     --BeforeI;
316   for (std::vector<CalleeSavedInfo>::const_iterator it = CSI.begin();
317                                                     it != CSI.end(); ++it) {
318     unsigned Reg = it->getReg();
319     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
320     TII.loadRegFromStackSlot(MBB, MI, it->getReg(), it->getFrameIdx(),
321                              RC, TRI);
322     assert(MI != MBB.begin() &&
323            "loadRegFromStackSlot didn't insert any code!");
324     // Insert in reverse order.  loadRegFromStackSlot can insert multiple
325     // instructions.
326     if (AtStart)
327       MI = MBB.begin();
328     else {
329       MI = BeforeI;
330       ++MI;
331     }
332   }
333   return true;
334 }
335
336 // This function eliminates ADJCALLSTACKDOWN,
337 // ADJCALLSTACKUP pseudo instructions
338 void XCoreFrameLowering::
339 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
340                               MachineBasicBlock::iterator I) const {
341   const XCoreInstrInfo &TII =
342     *static_cast<const XCoreInstrInfo*>(MF.getTarget().getInstrInfo());
343   if (!hasReservedCallFrame(MF)) {
344     // Turn the adjcallstackdown instruction into 'extsp <amt>' and the
345     // adjcallstackup instruction into 'ldaw sp, sp[<amt>]'
346     MachineInstr *Old = I;
347     uint64_t Amount = Old->getOperand(0).getImm();
348     if (Amount != 0) {
349       // We need to keep the stack aligned properly.  To do this, we round the
350       // amount of space needed for the outgoing arguments up to the next
351       // alignment boundary.
352       unsigned Align = getStackAlignment();
353       Amount = (Amount+Align-1)/Align*Align;
354
355       assert(Amount%4 == 0);
356       Amount /= 4;
357
358       bool isU6 = isImmU6(Amount);
359       if (!isU6 && !isImmU16(Amount)) {
360         // FIX could emit multiple instructions in this case.
361 #ifndef NDEBUG
362         errs() << "eliminateCallFramePseudoInstr size too big: "
363                << Amount << "\n";
364 #endif
365         llvm_unreachable(0);
366       }
367
368       MachineInstr *New;
369       if (Old->getOpcode() == XCore::ADJCALLSTACKDOWN) {
370         int Opcode = isU6 ? XCore::EXTSP_u6 : XCore::EXTSP_lu6;
371         New=BuildMI(MF, Old->getDebugLoc(), TII.get(Opcode))
372           .addImm(Amount);
373       } else {
374         assert(Old->getOpcode() == XCore::ADJCALLSTACKUP);
375         int Opcode = isU6 ? XCore::LDAWSP_ru6 : XCore::LDAWSP_lru6;
376         New=BuildMI(MF, Old->getDebugLoc(), TII.get(Opcode), XCore::SP)
377           .addImm(Amount);
378       }
379
380       // Replace the pseudo instruction with a new instruction...
381       MBB.insert(I, New);
382     }
383   }
384   
385   MBB.erase(I);
386 }
387
388 void
389 XCoreFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
390                                                      RegScavenger *RS) const {
391   MachineFrameInfo *MFI = MF.getFrameInfo();
392   const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
393   bool LRUsed = MF.getRegInfo().isPhysRegUsed(XCore::LR);
394   const TargetRegisterClass *RC = &XCore::GRRegsRegClass;
395   XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
396   if (LRUsed) {
397     MF.getRegInfo().setPhysRegUnused(XCore::LR);
398
399     bool isVarArg = MF.getFunction()->isVarArg();
400     int FrameIdx;
401     if (! isVarArg) {
402       // A fixed offset of 0 allows us to save / restore LR using entsp / retsp.
403       FrameIdx = MFI->CreateFixedObject(RC->getSize(), 0, true);
404     } else {
405       FrameIdx = MFI->CreateStackObject(RC->getSize(), RC->getAlignment(),
406                                         false);
407     }
408     XFI->setUsesLR(FrameIdx);
409     XFI->setLRSpillSlot(FrameIdx);
410   }
411   if (RegInfo->requiresRegisterScavenging(MF)) {
412     // Reserve a slot close to SP or frame pointer.
413     RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
414                                                        RC->getAlignment(),
415                                                        false));
416   }
417   if (hasFP(MF)) {
418     // A callee save register is used to hold the FP.
419     // This needs saving / restoring in the epilogue / prologue.
420     XFI->setFPSpillSlot(MFI->CreateStackObject(RC->getSize(),
421                                                RC->getAlignment(),
422                                                false));
423   }
424 }