1 //===-- XCoreFrameLowering.cpp - Frame info for XCore Target -----*- C++ -*-==//
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 "XCore.h"
16 #include "XCoreFrameLowering.h"
17 #include "XCoreInstrInfo.h"
18 #include "XCoreMachineFunctionInfo.h"
19 #include "llvm/Function.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineModuleInfo.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/RegisterScavenging.h"
26 #include "llvm/Target/TargetData.h"
27 #include "llvm/Target/TargetOptions.h"
28 #include "llvm/Support/ErrorHandling.h"
29
30 using namespace llvm;
31
32 // helper functions. FIXME: Eliminate.
isImmUs(unsigned val)33 static inline bool isImmUs(unsigned val) {
34 return val <= 11;
35 }
36
isImmU6(unsigned val)37 static inline bool isImmU6(unsigned val) {
38 return val < (1 << 6);
39 }
40
isImmU16(unsigned val)41 static inline bool isImmU16(unsigned val) {
42 return val < (1 << 16);
43 }
44
loadFromStack(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,unsigned DstReg,int Offset,DebugLoc dl,const TargetInstrInfo & TII)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
storeToStack(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,unsigned SrcReg,int Offset,DebugLoc dl,const TargetInstrInfo & TII)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
XCoreFrameLowering(const XCoreSubtarget & sti)80 XCoreFrameLowering::XCoreFrameLowering(const XCoreSubtarget &sti)
81 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, 4, 0),
82 STI(sti) {
83 // Do nothing
84 }
85
hasFP(const MachineFunction & MF) const86 bool XCoreFrameLowering::hasFP(const MachineFunction &MF) const {
87 return DisableFramePointerElim(MF) || MF.getFrameInfo()->hasVarSizedObjects();
88 }
89
emitPrologue(MachineFunction & MF) const90 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 XCoreRegisterInfo *RegInfo =
96 static_cast<const XCoreRegisterInfo*>(MF.getTarget().getRegisterInfo());
97 const XCoreInstrInfo &TII =
98 *static_cast<const XCoreInstrInfo*>(MF.getTarget().getInstrInfo());
99 XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
100 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
101
102 bool FP = hasFP(MF);
103 bool Nested = MF.getFunction()->getAttributes().hasAttrSomewhere(Attribute::Nest);
104
105 if (Nested) {
106 loadFromStack(MBB, MBBI, XCore::R11, 0, dl, TII);
107 }
108
109 // Work out frame sizes.
110 int FrameSize = MFI->getStackSize();
111 assert(FrameSize%4 == 0 && "Misaligned frame size");
112 FrameSize/=4;
113
114 bool isU6 = isImmU6(FrameSize);
115
116 if (!isU6 && !isImmU16(FrameSize)) {
117 // FIXME could emit multiple instructions.
118 report_fatal_error("emitPrologue Frame size too big: " + Twine(FrameSize));
119 }
120 bool emitFrameMoves = RegInfo->needsFrameMoves(MF);
121
122 // Do we need to allocate space on the stack?
123 if (FrameSize) {
124 bool saveLR = XFI->getUsesLR();
125 bool LRSavedOnEntry = false;
126 int Opcode;
127 if (saveLR && (MFI->getObjectOffset(XFI->getLRSpillSlot()) == 0)) {
128 Opcode = (isU6) ? XCore::ENTSP_u6 : XCore::ENTSP_lu6;
129 MBB.addLiveIn(XCore::LR);
130 saveLR = false;
131 LRSavedOnEntry = true;
132 } else {
133 Opcode = (isU6) ? XCore::EXTSP_u6 : XCore::EXTSP_lu6;
134 }
135 BuildMI(MBB, MBBI, dl, TII.get(Opcode)).addImm(FrameSize);
136
137 if (emitFrameMoves) {
138 std::vector<MachineMove> &Moves = MMI->getFrameMoves();
139
140 // Show update of SP.
141 MCSymbol *FrameLabel = MMI->getContext().CreateTempSymbol();
142 BuildMI(MBB, MBBI, dl, TII.get(XCore::PROLOG_LABEL)).addSym(FrameLabel);
143
144 MachineLocation SPDst(MachineLocation::VirtualFP);
145 MachineLocation SPSrc(MachineLocation::VirtualFP, -FrameSize * 4);
146 Moves.push_back(MachineMove(FrameLabel, SPDst, SPSrc));
147
148 if (LRSavedOnEntry) {
149 MachineLocation CSDst(MachineLocation::VirtualFP, 0);
150 MachineLocation CSSrc(XCore::LR);
151 Moves.push_back(MachineMove(FrameLabel, CSDst, CSSrc));
152 }
153 }
154 if (saveLR) {
155 int LRSpillOffset = MFI->getObjectOffset(XFI->getLRSpillSlot());
156 storeToStack(MBB, MBBI, XCore::LR, LRSpillOffset + FrameSize*4, dl, TII);
157 MBB.addLiveIn(XCore::LR);
158
159 if (emitFrameMoves) {
160 MCSymbol *SaveLRLabel = MMI->getContext().CreateTempSymbol();
161 BuildMI(MBB, MBBI, dl, TII.get(XCore::PROLOG_LABEL)).addSym(SaveLRLabel);
162 MachineLocation CSDst(MachineLocation::VirtualFP, LRSpillOffset);
163 MachineLocation CSSrc(XCore::LR);
164 MMI->getFrameMoves().push_back(MachineMove(SaveLRLabel, CSDst, CSSrc));
165 }
166 }
167 }
168
169 if (FP) {
170 // Save R10 to the stack.
171 int FPSpillOffset = MFI->getObjectOffset(XFI->getFPSpillSlot());
172 storeToStack(MBB, MBBI, XCore::R10, FPSpillOffset + FrameSize*4, dl, TII);
173 // R10 is live-in. It is killed at the spill.
174 MBB.addLiveIn(XCore::R10);
175 if (emitFrameMoves) {
176 MCSymbol *SaveR10Label = MMI->getContext().CreateTempSymbol();
177 BuildMI(MBB, MBBI, dl, TII.get(XCore::PROLOG_LABEL)).addSym(SaveR10Label);
178 MachineLocation CSDst(MachineLocation::VirtualFP, FPSpillOffset);
179 MachineLocation CSSrc(XCore::R10);
180 MMI->getFrameMoves().push_back(MachineMove(SaveR10Label, CSDst, CSSrc));
181 }
182 // Set the FP from the SP.
183 unsigned FramePtr = XCore::R10;
184 BuildMI(MBB, MBBI, dl, TII.get(XCore::LDAWSP_ru6), FramePtr)
185 .addImm(0);
186 if (emitFrameMoves) {
187 // Show FP is now valid.
188 MCSymbol *FrameLabel = MMI->getContext().CreateTempSymbol();
189 BuildMI(MBB, MBBI, dl, TII.get(XCore::PROLOG_LABEL)).addSym(FrameLabel);
190 MachineLocation SPDst(FramePtr);
191 MachineLocation SPSrc(MachineLocation::VirtualFP);
192 MMI->getFrameMoves().push_back(MachineMove(FrameLabel, SPDst, SPSrc));
193 }
194 }
195
196 if (emitFrameMoves) {
197 // Frame moves for callee saved.
198 std::vector<MachineMove> &Moves = MMI->getFrameMoves();
199 std::vector<std::pair<MCSymbol*, CalleeSavedInfo> >&SpillLabels =
200 XFI->getSpillLabels();
201 for (unsigned I = 0, E = SpillLabels.size(); I != E; ++I) {
202 MCSymbol *SpillLabel = SpillLabels[I].first;
203 CalleeSavedInfo &CSI = SpillLabels[I].second;
204 int Offset = MFI->getObjectOffset(CSI.getFrameIdx());
205 unsigned Reg = CSI.getReg();
206 MachineLocation CSDst(MachineLocation::VirtualFP, Offset);
207 MachineLocation CSSrc(Reg);
208 Moves.push_back(MachineMove(SpillLabel, CSDst, CSSrc));
209 }
210 }
211 }
212
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const213 void XCoreFrameLowering::emitEpilogue(MachineFunction &MF,
214 MachineBasicBlock &MBB) const {
215 MachineFrameInfo *MFI = MF.getFrameInfo();
216 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
217 const XCoreInstrInfo &TII =
218 *static_cast<const XCoreInstrInfo*>(MF.getTarget().getInstrInfo());
219 DebugLoc dl = MBBI->getDebugLoc();
220
221 bool FP = hasFP(MF);
222 if (FP) {
223 // Restore the stack pointer.
224 unsigned FramePtr = XCore::R10;
225 BuildMI(MBB, MBBI, dl, TII.get(XCore::SETSP_1r))
226 .addReg(FramePtr);
227 }
228
229 // Work out frame sizes.
230 int FrameSize = MFI->getStackSize();
231
232 assert(FrameSize%4 == 0 && "Misaligned frame size");
233
234 FrameSize/=4;
235
236 bool isU6 = isImmU6(FrameSize);
237
238 if (!isU6 && !isImmU16(FrameSize)) {
239 // FIXME could emit multiple instructions.
240 report_fatal_error("emitEpilogue Frame size too big: " + Twine(FrameSize));
241 }
242
243 if (FrameSize) {
244 XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
245
246 if (FP) {
247 // Restore R10
248 int FPSpillOffset = MFI->getObjectOffset(XFI->getFPSpillSlot());
249 FPSpillOffset += FrameSize*4;
250 loadFromStack(MBB, MBBI, XCore::R10, FPSpillOffset, dl, TII);
251 }
252 bool restoreLR = XFI->getUsesLR();
253 if (restoreLR && MFI->getObjectOffset(XFI->getLRSpillSlot()) != 0) {
254 int LRSpillOffset = MFI->getObjectOffset(XFI->getLRSpillSlot());
255 LRSpillOffset += FrameSize*4;
256 loadFromStack(MBB, MBBI, XCore::LR, LRSpillOffset, dl, TII);
257 restoreLR = false;
258 }
259 if (restoreLR) {
260 // Fold prologue into return instruction
261 assert(MBBI->getOpcode() == XCore::RETSP_u6
262 || MBBI->getOpcode() == XCore::RETSP_lu6);
263 int Opcode = (isU6) ? XCore::RETSP_u6 : XCore::RETSP_lu6;
264 BuildMI(MBB, MBBI, dl, TII.get(Opcode)).addImm(FrameSize);
265 MBB.erase(MBBI);
266 } else {
267 int Opcode = (isU6) ? XCore::LDAWSP_ru6_RRegs : XCore::LDAWSP_lru6_RRegs;
268 BuildMI(MBB, MBBI, dl, TII.get(Opcode), XCore::SP).addImm(FrameSize);
269 }
270 }
271 }
272
spillCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,const std::vector<CalleeSavedInfo> & CSI,const TargetRegisterInfo * TRI) const273 bool XCoreFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
274 MachineBasicBlock::iterator MI,
275 const std::vector<CalleeSavedInfo> &CSI,
276 const TargetRegisterInfo *TRI) const {
277 if (CSI.empty())
278 return true;
279
280 MachineFunction *MF = MBB.getParent();
281 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
282
283 XCoreFunctionInfo *XFI = MF->getInfo<XCoreFunctionInfo>();
284 bool emitFrameMoves = XCoreRegisterInfo::needsFrameMoves(*MF);
285
286 DebugLoc DL;
287 if (MI != MBB.end()) DL = MI->getDebugLoc();
288
289 for (std::vector<CalleeSavedInfo>::const_iterator it = CSI.begin();
290 it != CSI.end(); ++it) {
291 // Add the callee-saved register as live-in. It's killed at the spill.
292 MBB.addLiveIn(it->getReg());
293
294 unsigned Reg = it->getReg();
295 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
296 TII.storeRegToStackSlot(MBB, MI, Reg, true,
297 it->getFrameIdx(), RC, TRI);
298 if (emitFrameMoves) {
299 MCSymbol *SaveLabel = MF->getContext().CreateTempSymbol();
300 BuildMI(MBB, MI, DL, TII.get(XCore::PROLOG_LABEL)).addSym(SaveLabel);
301 XFI->getSpillLabels().push_back(std::make_pair(SaveLabel, *it));
302 }
303 }
304 return true;
305 }
306
restoreCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,const std::vector<CalleeSavedInfo> & CSI,const TargetRegisterInfo * TRI) const307 bool XCoreFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
308 MachineBasicBlock::iterator MI,
309 const std::vector<CalleeSavedInfo> &CSI,
310 const TargetRegisterInfo *TRI) const{
311 MachineFunction *MF = MBB.getParent();
312 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
313
314 bool AtStart = MI == MBB.begin();
315 MachineBasicBlock::iterator BeforeI = MI;
316 if (!AtStart)
317 --BeforeI;
318 for (std::vector<CalleeSavedInfo>::const_iterator it = CSI.begin();
319 it != CSI.end(); ++it) {
320 unsigned Reg = it->getReg();
321 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
322 TII.loadRegFromStackSlot(MBB, MI, it->getReg(), it->getFrameIdx(),
323 RC, TRI);
324 assert(MI != MBB.begin() &&
325 "loadRegFromStackSlot didn't insert any code!");
326 // Insert in reverse order. loadRegFromStackSlot can insert multiple
327 // instructions.
328 if (AtStart)
329 MI = MBB.begin();
330 else {
331 MI = BeforeI;
332 ++MI;
333 }
334 }
335 return true;
336 }
337
338 void
processFunctionBeforeCalleeSavedScan(MachineFunction & MF,RegScavenger * RS) const339 XCoreFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
340 RegScavenger *RS) const {
341 MachineFrameInfo *MFI = MF.getFrameInfo();
342 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
343 bool LRUsed = MF.getRegInfo().isPhysRegUsed(XCore::LR);
344 const TargetRegisterClass *RC = XCore::GRRegsRegisterClass;
345 XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
346 if (LRUsed) {
347 MF.getRegInfo().setPhysRegUnused(XCore::LR);
348
349 bool isVarArg = MF.getFunction()->isVarArg();
350 int FrameIdx;
351 if (! isVarArg) {
352 // A fixed offset of 0 allows us to save / restore LR using entsp / retsp.
353 FrameIdx = MFI->CreateFixedObject(RC->getSize(), 0, true);
354 } else {
355 FrameIdx = MFI->CreateStackObject(RC->getSize(), RC->getAlignment(),
356 false);
357 }
358 XFI->setUsesLR(FrameIdx);
359 XFI->setLRSpillSlot(FrameIdx);
360 }
361 if (RegInfo->requiresRegisterScavenging(MF)) {
362 // Reserve a slot close to SP or frame pointer.
363 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
364 RC->getAlignment(),
365 false));
366 }
367 if (hasFP(MF)) {
368 // A callee save register is used to hold the FP.
369 // This needs saving / restoring in the epilogue / prologue.
370 XFI->setFPSpillSlot(MFI->CreateStackObject(RC->getSize(),
371 RC->getAlignment(),
372 false));
373 }
374 }
375
376 void XCoreFrameLowering::
processFunctionBeforeFrameFinalized(MachineFunction & MF) const377 processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
378
379 }
380