1 //===-- PrologEpilogCodeInserter.cpp - Insert Prolog & Epilog code for fn -===//
3 // Insert SAVE/RESTORE instructions for the function
5 // Insert prolog code at the unique function entry point.
6 // Insert epilog code at each function exit point.
7 // InsertPrologEpilog invokes these only if the function is not compiled
8 // with the leaf function optimization.
10 //===----------------------------------------------------------------------===//
12 #include "SparcInternals.h"
13 #include "SparcRegClassInfo.h"
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/CodeGen/MachineCodeForInstruction.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/CodeGen/InstrSelectionSupport.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Function.h"
22 class InsertPrologEpilogCode : public FunctionPass {
23 TargetMachine &Target;
25 InsertPrologEpilogCode(TargetMachine &T) : Target(T) {}
27 const char *getPassName() const { return "Sparc Prolog/Epilog Inserter"; }
29 bool runOnFunction(Function &F) {
30 MachineFunction &mcodeInfo = MachineFunction::get(&F);
31 if (!mcodeInfo.isCompiledAsLeafMethod()) {
38 void InsertPrologCode(Function &F);
39 void InsertEpilogCode(Function &F);
42 } // End anonymous namespace
44 //------------------------------------------------------------------------
45 // External Function: GetInstructionsForProlog
46 // External Function: GetInstructionsForEpilog
49 // Create prolog and epilog code for procedure entry and exit
50 //------------------------------------------------------------------------
52 void InsertPrologEpilogCode::InsertPrologCode(Function &F)
54 std::vector<MachineInstr*> mvec;
56 const MachineFrameInfo& frameInfo = Target.getFrameInfo();
58 // The second operand is the stack size. If it does not fit in the
59 // immediate field, we have to use a free register to hold the size.
60 // See the comments below for the choice of this register.
62 MachineFunction& mcInfo = MachineFunction::get(&F);
63 unsigned staticStackSize = mcInfo.getStaticStackSize();
65 if (staticStackSize < (unsigned) frameInfo.getMinStackFrameSize())
66 staticStackSize = (unsigned) frameInfo.getMinStackFrameSize();
68 if (unsigned padsz = (staticStackSize %
69 (unsigned) frameInfo.getStackFrameSizeAlignment()))
70 staticStackSize += frameInfo.getStackFrameSizeAlignment() - padsz;
72 int32_t C = - (int) staticStackSize;
73 int SP = Target.getRegInfo().getStackPointer();
74 if (Target.getInstrInfo().constantFitsInImmedField(SAVE, staticStackSize)) {
75 M = BuildMI(SAVE, 3).addMReg(SP).addSImm(C).addMReg(SP);
78 // We have to put the stack size value into a register before SAVE.
79 // Use register %g1 since it is volatile across calls. Note that the
80 // local (%l) and in (%i) registers cannot be used before the SAVE!
81 // Do this by creating a code sequence equivalent to:
82 // SETSW -(stackSize), %g1
83 int uregNum = Target.getRegInfo().getUnifiedRegNum(
84 Target.getRegInfo().getRegClassIDOfType(Type::IntTy),
85 SparcIntRegClass::g1);
87 M = BuildMI(SETHI, 2).addSImm(C).addMReg(uregNum);
91 M = BuildMI(OR, 3).addMReg(uregNum).addSImm(C).addMReg(uregNum);
95 M = BuildMI(SRA, 3).addMReg(uregNum).addZImm(0).addMReg(uregNum);
98 // Now generate the SAVE using the value in register %g1
99 M = BuildMI(SAVE, 3).addMReg(SP).addMReg(uregNum).addMReg(SP);
103 MachineBasicBlock& bbMvec = mcInfo.front();
104 bbMvec.insert(bbMvec.begin(), mvec.begin(), mvec.end());
107 void InsertPrologEpilogCode::InsertEpilogCode(Function &F)
109 MachineFunction &MF = MachineFunction::get(&F);
110 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
111 MachineBasicBlock &MBB = *I;
112 BasicBlock &BB = *I->getBasicBlock();
113 Instruction *TermInst = (Instruction*)BB.getTerminator();
114 if (TermInst->getOpcode() == Instruction::Ret)
116 int ZR = Target.getRegInfo().getZeroRegNum();
117 MachineInstr *Restore =
118 BuildMI(RESTORE, 3).addMReg(ZR).addSImm(0).addMReg(ZR);
120 MachineCodeForInstruction &termMvec =
121 MachineCodeForInstruction::get(TermInst);
123 // Remove the NOPs in the delay slots of the return instruction
124 const MachineInstrInfo &mii = Target.getInstrInfo();
125 unsigned numNOPs = 0;
126 while (termMvec.back()->getOpCode() == NOP)
128 assert( termMvec.back() == MBB.back());
129 delete MBB.pop_back();
133 assert(termMvec.back() == MBB.back());
135 // Check that we found the right number of NOPs and have the right
136 // number of instructions to replace them.
137 unsigned ndelays = mii.getNumDelaySlots(termMvec.back()->getOpCode());
138 assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
139 assert(ndelays == 1 && "Cannot use epilog code for delay slots?");
141 // Append the epilog code to the end of the basic block.
142 MBB.push_back(Restore);
147 Pass* UltraSparc::getPrologEpilogInsertionPass() {
148 return new InsertPrologEpilogCode(*this);