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/MachineInstr.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 int 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 if (Target.getInstrInfo().constantFitsInImmedField(SAVE, staticStackSize))
74 M = new MachineInstr(SAVE);
75 M->SetMachineOperandReg(0, Target.getRegInfo().getStackPointer());
76 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
77 - (int) staticStackSize);
78 M->SetMachineOperandReg(2, Target.getRegInfo().getStackPointer());
83 // We have to put the stack size value into a register before SAVE.
84 // Use register %g1 since it is volatile across calls. Note that the
85 // local (%l) and in (%i) registers cannot be used before the SAVE!
86 // Do this by creating a code sequence equivalent to:
87 // SETSW -(stackSize), %g1
88 int32_t C = - (int) staticStackSize;
89 int uregNum = Target.getRegInfo().getUnifiedRegNum(
90 Target.getRegInfo().getRegClassIDOfType(Type::IntTy),
91 SparcIntRegClass::g1);
93 M = new MachineInstr(SETHI);
94 M->SetMachineOperandConst(0, MachineOperand::MO_SignExtendedImmed, C);
95 M->SetMachineOperandReg(1, uregNum);
99 M = new MachineInstr(OR);
100 M->SetMachineOperandReg(0, uregNum);
101 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed, C);
102 M->SetMachineOperandReg(2, uregNum);
103 M->setOperandLo32(1);
106 M = new MachineInstr(SRA);
107 M->SetMachineOperandReg(0, uregNum);
108 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed, 0);
109 M->SetMachineOperandReg(2, uregNum);
112 // Now generate the SAVE using the value in register %g1
113 M = new MachineInstr(SAVE);
114 M->SetMachineOperandReg(0, Target.getRegInfo().getStackPointer());
115 M->SetMachineOperandReg(1, uregNum);
116 M->SetMachineOperandReg(2, Target.getRegInfo().getStackPointer());
120 MachineBasicBlock& bbMvec = MachineBasicBlock::get(&F.getEntryNode());
121 bbMvec.insert(bbMvec.begin(), mvec.begin(), mvec.end());
124 void InsertPrologEpilogCode::InsertEpilogCode(Function &F)
126 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
127 Instruction *TermInst = (Instruction*)I->getTerminator();
128 if (TermInst->getOpcode() == Instruction::Ret)
130 MachineInstr *Restore = new MachineInstr(RESTORE);
131 Restore->SetMachineOperandReg(0, Target.getRegInfo().getZeroRegNum());
132 Restore->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
134 Restore->SetMachineOperandReg(2, Target.getRegInfo().getZeroRegNum());
136 MachineBasicBlock& bbMvec = MachineBasicBlock::get(I);
137 MachineCodeForInstruction &termMvec =
138 MachineCodeForInstruction::get(TermInst);
140 // Remove the NOPs in the delay slots of the return instruction
141 const MachineInstrInfo &mii = Target.getInstrInfo();
142 unsigned numNOPs = 0;
143 while (termMvec.back()->getOpCode() == NOP)
145 assert( termMvec.back() == bbMvec.back());
146 delete bbMvec.pop_back();
150 assert(termMvec.back() == bbMvec.back());
152 // Check that we found the right number of NOPs and have the right
153 // number of instructions to replace them.
154 unsigned ndelays = mii.getNumDelaySlots(termMvec.back()->getOpCode());
155 assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
156 assert(ndelays == 1 && "Cannot use epilog code for delay slots?");
158 // Append the epilog code to the end of the basic block.
159 bbMvec.push_back(Restore);
164 Pass* UltraSparc::getPrologEpilogInsertionPass() {
165 return new InsertPrologEpilogCode(*this);