• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- MBlazeFrameLowering.cpp - MBlaze Frame Information ------*- 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 the MBlaze implementation of TargetFrameLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #define DEBUG_TYPE "mblaze-frame-lowering"
15 
16 #include "MBlazeFrameLowering.h"
17 #include "MBlazeInstrInfo.h"
18 #include "MBlazeMachineFunction.h"
19 #include "InstPrinter/MBlazeInstPrinter.h"
20 #include "llvm/Function.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/Target/TargetData.h"
27 #include "llvm/Target/TargetOptions.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/raw_ostream.h"
32 
33 using namespace llvm;
34 
35 namespace llvm {
36   cl::opt<bool> DisableStackAdjust(
37     "disable-mblaze-stack-adjust",
38     cl::init(false),
39     cl::desc("Disable MBlaze stack layout adjustment."),
40     cl::Hidden);
41 }
42 
replaceFrameIndexes(MachineFunction & MF,SmallVector<std::pair<int,int64_t>,16> & FR)43 static void replaceFrameIndexes(MachineFunction &MF,
44                                 SmallVector<std::pair<int,int64_t>, 16> &FR) {
45   MachineFrameInfo *MFI = MF.getFrameInfo();
46   MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
47   const SmallVector<std::pair<int,int64_t>, 16>::iterator FRB = FR.begin();
48   const SmallVector<std::pair<int,int64_t>, 16>::iterator FRE = FR.end();
49 
50   SmallVector<std::pair<int,int64_t>, 16>::iterator FRI = FRB;
51   for (; FRI != FRE; ++FRI) {
52     MFI->RemoveStackObject(FRI->first);
53     int NFI = MFI->CreateFixedObject(4, FRI->second, true);
54     MBlazeFI->recordReplacement(FRI->first, NFI);
55 
56     for (MachineFunction::iterator MB=MF.begin(), ME=MF.end(); MB!=ME; ++MB) {
57       MachineBasicBlock::iterator MBB = MB->begin();
58       const MachineBasicBlock::iterator MBE = MB->end();
59 
60       for (; MBB != MBE; ++MBB) {
61         MachineInstr::mop_iterator MIB = MBB->operands_begin();
62         const MachineInstr::mop_iterator MIE = MBB->operands_end();
63 
64         for (MachineInstr::mop_iterator MII = MIB; MII != MIE; ++MII) {
65           if (!MII->isFI() || MII->getIndex() != FRI->first) continue;
66           DEBUG(dbgs() << "FOUND FI#" << MII->getIndex() << "\n");
67           MII->setIndex(NFI);
68         }
69       }
70     }
71   }
72 }
73 
74 //===----------------------------------------------------------------------===//
75 //
76 // Stack Frame Processing methods
77 // +----------------------------+
78 //
79 // The stack is allocated decrementing the stack pointer on
80 // the first instruction of a function prologue. Once decremented,
81 // all stack references are are done through a positive offset
82 // from the stack/frame pointer, so the stack is considered
83 // to grow up.
84 //
85 //===----------------------------------------------------------------------===//
86 
analyzeFrameIndexes(MachineFunction & MF)87 static void analyzeFrameIndexes(MachineFunction &MF) {
88   if (DisableStackAdjust) return;
89 
90   MachineFrameInfo *MFI = MF.getFrameInfo();
91   MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
92   const MachineRegisterInfo &MRI = MF.getRegInfo();
93 
94   MachineRegisterInfo::livein_iterator LII = MRI.livein_begin();
95   MachineRegisterInfo::livein_iterator LIE = MRI.livein_end();
96   const SmallVector<int, 16> &LiveInFI = MBlazeFI->getLiveIn();
97   SmallVector<MachineInstr*, 16> EraseInstr;
98   SmallVector<std::pair<int,int64_t>, 16> FrameRelocate;
99 
100   MachineBasicBlock *MBB = MF.getBlockNumbered(0);
101   MachineBasicBlock::iterator MIB = MBB->begin();
102   MachineBasicBlock::iterator MIE = MBB->end();
103 
104   int StackAdjust = 0;
105   int StackOffset = -28;
106 
107   // In this loop we are searching frame indexes that corrospond to incoming
108   // arguments that are already in the stack. We look for instruction sequences
109   // like the following:
110   //
111   //    LWI REG, FI1, 0
112   //    ...
113   //    SWI REG, FI2, 0
114   //
115   // As long as there are no defs of REG in the ... part, we can eliminate
116   // the SWI instruction because the value has already been stored to the
117   // stack by the caller. All we need to do is locate FI at the correct
118   // stack location according to the calling convensions.
119   //
120   // Additionally, if the SWI operation kills the def of REG then we don't
121   // need the LWI operation so we can erase it as well.
122   for (unsigned i = 0, e = LiveInFI.size(); i < e; ++i) {
123     for (MachineBasicBlock::iterator I=MIB; I != MIE; ++I) {
124       if (I->getOpcode() != MBlaze::LWI || I->getNumOperands() != 3 ||
125           !I->getOperand(1).isFI() || !I->getOperand(0).isReg() ||
126           I->getOperand(1).getIndex() != LiveInFI[i]) continue;
127 
128       unsigned FIReg = I->getOperand(0).getReg();
129       MachineBasicBlock::iterator SI = I;
130       for (SI++; SI != MIE; ++SI) {
131         if (!SI->getOperand(0).isReg() ||
132             !SI->getOperand(1).isFI() ||
133             SI->getOpcode() != MBlaze::SWI) continue;
134 
135         int FI = SI->getOperand(1).getIndex();
136         if (SI->getOperand(0).getReg() != FIReg ||
137             MFI->isFixedObjectIndex(FI) ||
138             MFI->getObjectSize(FI) != 4) continue;
139 
140         if (SI->getOperand(0).isDef()) break;
141 
142         if (SI->getOperand(0).isKill()) {
143           DEBUG(dbgs() << "LWI for FI#" << I->getOperand(1).getIndex()
144                        << " removed\n");
145           EraseInstr.push_back(I);
146         }
147 
148         EraseInstr.push_back(SI);
149         DEBUG(dbgs() << "SWI for FI#" << FI << " removed\n");
150 
151         FrameRelocate.push_back(std::make_pair(FI,StackOffset));
152         DEBUG(dbgs() << "FI#" << FI << " relocated to " << StackOffset << "\n");
153 
154         StackOffset -= 4;
155         StackAdjust += 4;
156         break;
157       }
158     }
159   }
160 
161   // In this loop we are searching for frame indexes that corrospond to
162   // incoming arguments that are in registers. We look for instruction
163   // sequences like the following:
164   //
165   //    ...  SWI REG, FI, 0
166   //
167   // As long as the ... part does not define REG and if REG is an incoming
168   // parameter register then we know that, according to ABI convensions, the
169   // caller has allocated stack space for it already.  Instead of allocating
170   // stack space on our frame, we record the correct location in the callers
171   // frame.
172   for (MachineRegisterInfo::livein_iterator LI = LII; LI != LIE; ++LI) {
173     for (MachineBasicBlock::iterator I=MIB; I != MIE; ++I) {
174       if (I->definesRegister(LI->first))
175         break;
176 
177       if (I->getOpcode() != MBlaze::SWI || I->getNumOperands() != 3 ||
178           !I->getOperand(1).isFI() || !I->getOperand(0).isReg() ||
179           I->getOperand(1).getIndex() < 0) continue;
180 
181       if (I->getOperand(0).getReg() == LI->first) {
182         int FI = I->getOperand(1).getIndex();
183         MBlazeFI->recordLiveIn(FI);
184 
185         int FILoc = 0;
186         switch (LI->first) {
187         default: llvm_unreachable("invalid incoming parameter!");
188         case MBlaze::R5:  FILoc = -4; break;
189         case MBlaze::R6:  FILoc = -8; break;
190         case MBlaze::R7:  FILoc = -12; break;
191         case MBlaze::R8:  FILoc = -16; break;
192         case MBlaze::R9:  FILoc = -20; break;
193         case MBlaze::R10: FILoc = -24; break;
194         }
195 
196         StackAdjust += 4;
197         FrameRelocate.push_back(std::make_pair(FI,FILoc));
198         DEBUG(dbgs() << "FI#" << FI << " relocated to " << FILoc << "\n");
199         break;
200       }
201     }
202   }
203 
204   // Go ahead and erase all of the instructions that we determined were
205   // no longer needed.
206   for (int i = 0, e = EraseInstr.size(); i < e; ++i)
207     MBB->erase(EraseInstr[i]);
208 
209   // Replace all of the frame indexes that we have relocated with new
210   // fixed object frame indexes.
211   replaceFrameIndexes(MF, FrameRelocate);
212 }
213 
interruptFrameLayout(MachineFunction & MF)214 static void interruptFrameLayout(MachineFunction &MF) {
215   const Function *F = MF.getFunction();
216   llvm::CallingConv::ID CallConv = F->getCallingConv();
217 
218   // If this function is not using either the interrupt_handler
219   // calling convention or the save_volatiles calling convention
220   // then we don't need to do any additional frame layout.
221   if (CallConv != llvm::CallingConv::MBLAZE_INTR &&
222       CallConv != llvm::CallingConv::MBLAZE_SVOL)
223       return;
224 
225   MachineFrameInfo *MFI = MF.getFrameInfo();
226   const MachineRegisterInfo &MRI = MF.getRegInfo();
227   const MBlazeInstrInfo &TII =
228     *static_cast<const MBlazeInstrInfo*>(MF.getTarget().getInstrInfo());
229 
230   // Determine if the calling convention is the interrupt_handler
231   // calling convention. Some pieces of the prologue and epilogue
232   // only need to be emitted if we are lowering and interrupt handler.
233   bool isIntr = CallConv == llvm::CallingConv::MBLAZE_INTR;
234 
235   // Determine where to put prologue and epilogue additions
236   MachineBasicBlock &MENT   = MF.front();
237   MachineBasicBlock &MEXT   = MF.back();
238 
239   MachineBasicBlock::iterator MENTI = MENT.begin();
240   MachineBasicBlock::iterator MEXTI = prior(MEXT.end());
241 
242   DebugLoc ENTDL = MENTI != MENT.end() ? MENTI->getDebugLoc() : DebugLoc();
243   DebugLoc EXTDL = MEXTI != MEXT.end() ? MEXTI->getDebugLoc() : DebugLoc();
244 
245   // Store the frame indexes generated during prologue additions for use
246   // when we are generating the epilogue additions.
247   SmallVector<int, 10> VFI;
248 
249   // Build the prologue SWI for R3 - R12 if needed. Note that R11 must
250   // always have a SWI because it is used when processing RMSR.
251   for (unsigned r = MBlaze::R3; r <= MBlaze::R12; ++r) {
252     if (!MRI.isPhysRegUsed(r) && !(isIntr && r == MBlaze::R11)) continue;
253 
254     int FI = MFI->CreateStackObject(4,4,false,false);
255     VFI.push_back(FI);
256 
257     BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), r)
258       .addFrameIndex(FI).addImm(0);
259   }
260 
261   // Build the prologue SWI for R17, R18
262   int R17FI = MFI->CreateStackObject(4,4,false,false);
263   int R18FI = MFI->CreateStackObject(4,4,false,false);
264 
265   BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), MBlaze::R17)
266     .addFrameIndex(R17FI).addImm(0);
267 
268   BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), MBlaze::R18)
269     .addFrameIndex(R18FI).addImm(0);
270 
271   // Buid the prologue SWI and the epilogue LWI for RMSR if needed
272   if (isIntr) {
273     int MSRFI = MFI->CreateStackObject(4,4,false,false);
274     BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::MFS), MBlaze::R11)
275       .addReg(MBlaze::RMSR);
276     BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), MBlaze::R11)
277       .addFrameIndex(MSRFI).addImm(0);
278 
279     BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::LWI), MBlaze::R11)
280       .addFrameIndex(MSRFI).addImm(0);
281     BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::MTS), MBlaze::RMSR)
282       .addReg(MBlaze::R11);
283   }
284 
285   // Build the epilogue LWI for R17, R18
286   BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::LWI), MBlaze::R18)
287     .addFrameIndex(R18FI).addImm(0);
288 
289   BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::LWI), MBlaze::R17)
290     .addFrameIndex(R17FI).addImm(0);
291 
292   // Build the epilogue LWI for R3 - R12 if needed
293   for (unsigned r = MBlaze::R12, i = VFI.size(); r >= MBlaze::R3; --r) {
294     if (!MRI.isPhysRegUsed(r)) continue;
295     BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::LWI), r)
296       .addFrameIndex(VFI[--i]).addImm(0);
297   }
298 }
299 
determineFrameLayout(MachineFunction & MF)300 static void determineFrameLayout(MachineFunction &MF) {
301   MachineFrameInfo *MFI = MF.getFrameInfo();
302   MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
303 
304   // Replace the dummy '0' SPOffset by the negative offsets, as explained on
305   // LowerFORMAL_ARGUMENTS. Leaving '0' for while is necessary to avoid
306   // the approach done by calculateFrameObjectOffsets to the stack frame.
307   MBlazeFI->adjustLoadArgsFI(MFI);
308   MBlazeFI->adjustStoreVarArgsFI(MFI);
309 
310   // Get the number of bytes to allocate from the FrameInfo
311   unsigned FrameSize = MFI->getStackSize();
312   DEBUG(dbgs() << "Original Frame Size: " << FrameSize << "\n" );
313 
314   // Get the alignments provided by the target, and the maximum alignment
315   // (if any) of the fixed frame objects.
316   // unsigned MaxAlign = MFI->getMaxAlignment();
317   unsigned TargetAlign = MF.getTarget().getFrameLowering()->getStackAlignment();
318   unsigned AlignMask = TargetAlign - 1;
319 
320   // Make sure the frame is aligned.
321   FrameSize = (FrameSize + AlignMask) & ~AlignMask;
322   MFI->setStackSize(FrameSize);
323   DEBUG(dbgs() << "Aligned Frame Size: " << FrameSize << "\n" );
324 }
325 
getFrameIndexOffset(const MachineFunction & MF,int FI) const326 int MBlazeFrameLowering::getFrameIndexOffset(const MachineFunction &MF, int FI)
327   const {
328   const MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
329   if (MBlazeFI->hasReplacement(FI))
330     FI = MBlazeFI->getReplacement(FI);
331   return TargetFrameLowering::getFrameIndexOffset(MF,FI);
332 }
333 
334 // hasFP - Return true if the specified function should have a dedicated frame
335 // pointer register.  This is true if the function has variable sized allocas or
336 // if frame pointer elimination is disabled.
hasFP(const MachineFunction & MF) const337 bool MBlazeFrameLowering::hasFP(const MachineFunction &MF) const {
338   const MachineFrameInfo *MFI = MF.getFrameInfo();
339   return DisableFramePointerElim(MF) || MFI->hasVarSizedObjects();
340 }
341 
emitPrologue(MachineFunction & MF) const342 void MBlazeFrameLowering::emitPrologue(MachineFunction &MF) const {
343   MachineBasicBlock &MBB   = MF.front();
344   MachineFrameInfo *MFI    = MF.getFrameInfo();
345   const MBlazeInstrInfo &TII =
346     *static_cast<const MBlazeInstrInfo*>(MF.getTarget().getInstrInfo());
347   MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
348   MachineBasicBlock::iterator MBBI = MBB.begin();
349   DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
350 
351   llvm::CallingConv::ID CallConv = MF.getFunction()->getCallingConv();
352   bool requiresRA = CallConv == llvm::CallingConv::MBLAZE_INTR;
353 
354   // Determine the correct frame layout
355   determineFrameLayout(MF);
356 
357   // Get the number of bytes to allocate from the FrameInfo.
358   unsigned StackSize = MFI->getStackSize();
359 
360   // No need to allocate space on the stack.
361   if (StackSize == 0 && !MFI->adjustsStack() && !requiresRA) return;
362 
363   int FPOffset = MBlazeFI->getFPStackOffset();
364   int RAOffset = MBlazeFI->getRAStackOffset();
365 
366   // Adjust stack : addi R1, R1, -imm
367   BuildMI(MBB, MBBI, DL, TII.get(MBlaze::ADDIK), MBlaze::R1)
368       .addReg(MBlaze::R1).addImm(-StackSize);
369 
370   // swi  R15, R1, stack_loc
371   if (MFI->adjustsStack() || requiresRA) {
372     BuildMI(MBB, MBBI, DL, TII.get(MBlaze::SWI))
373         .addReg(MBlaze::R15).addReg(MBlaze::R1).addImm(RAOffset);
374   }
375 
376   if (hasFP(MF)) {
377     // swi  R19, R1, stack_loc
378     BuildMI(MBB, MBBI, DL, TII.get(MBlaze::SWI))
379       .addReg(MBlaze::R19).addReg(MBlaze::R1).addImm(FPOffset);
380 
381     // add R19, R1, R0
382     BuildMI(MBB, MBBI, DL, TII.get(MBlaze::ADD), MBlaze::R19)
383       .addReg(MBlaze::R1).addReg(MBlaze::R0);
384   }
385 }
386 
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const387 void MBlazeFrameLowering::emitEpilogue(MachineFunction &MF,
388                                    MachineBasicBlock &MBB) const {
389   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
390   MachineFrameInfo *MFI            = MF.getFrameInfo();
391   MBlazeFunctionInfo *MBlazeFI     = MF.getInfo<MBlazeFunctionInfo>();
392   const MBlazeInstrInfo &TII =
393     *static_cast<const MBlazeInstrInfo*>(MF.getTarget().getInstrInfo());
394 
395   DebugLoc dl = MBBI->getDebugLoc();
396 
397   llvm::CallingConv::ID CallConv = MF.getFunction()->getCallingConv();
398   bool requiresRA = CallConv == llvm::CallingConv::MBLAZE_INTR;
399 
400   // Get the FI's where RA and FP are saved.
401   int FPOffset = MBlazeFI->getFPStackOffset();
402   int RAOffset = MBlazeFI->getRAStackOffset();
403 
404   if (hasFP(MF)) {
405     // add R1, R19, R0
406     BuildMI(MBB, MBBI, dl, TII.get(MBlaze::ADD), MBlaze::R1)
407       .addReg(MBlaze::R19).addReg(MBlaze::R0);
408 
409     // lwi  R19, R1, stack_loc
410     BuildMI(MBB, MBBI, dl, TII.get(MBlaze::LWI), MBlaze::R19)
411       .addReg(MBlaze::R1).addImm(FPOffset);
412   }
413 
414   // lwi R15, R1, stack_loc
415   if (MFI->adjustsStack() || requiresRA) {
416     BuildMI(MBB, MBBI, dl, TII.get(MBlaze::LWI), MBlaze::R15)
417       .addReg(MBlaze::R1).addImm(RAOffset);
418   }
419 
420   // Get the number of bytes from FrameInfo
421   int StackSize = (int) MFI->getStackSize();
422 
423   // addi R1, R1, imm
424   if (StackSize) {
425     BuildMI(MBB, MBBI, dl, TII.get(MBlaze::ADDIK), MBlaze::R1)
426       .addReg(MBlaze::R1).addImm(StackSize);
427   }
428 }
429 
430 void MBlazeFrameLowering::
processFunctionBeforeCalleeSavedScan(MachineFunction & MF,RegScavenger * RS) const431 processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
432                                      RegScavenger *RS) const {
433   MachineFrameInfo *MFI = MF.getFrameInfo();
434   MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
435   llvm::CallingConv::ID CallConv = MF.getFunction()->getCallingConv();
436   bool requiresRA = CallConv == llvm::CallingConv::MBLAZE_INTR;
437 
438   if (MFI->adjustsStack() || requiresRA) {
439     MBlazeFI->setRAStackOffset(0);
440     MFI->CreateFixedObject(4,0,true);
441   }
442 
443   if (hasFP(MF)) {
444     MBlazeFI->setFPStackOffset(4);
445     MFI->CreateFixedObject(4,4,true);
446   }
447 
448   interruptFrameLayout(MF);
449   analyzeFrameIndexes(MF);
450 }
451