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/MachineCodeForMethod.h"
15 #include "llvm/CodeGen/MachineCodeForBasicBlock.h"
16 #include "llvm/CodeGen/MachineCodeForInstruction.h"
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "llvm/CodeGen/InstrSelectionSupport.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Function.h"
21 #include "llvm/BasicBlock.h"
22 #include "llvm/Instruction.h"
25 class InsertPrologEpilogCode : public FunctionPass {
26 TargetMachine &Target;
28 InsertPrologEpilogCode(TargetMachine &T) : Target(T) {}
30 const char *getPassName() const { return "Sparc Prolog/Epilog Inserter"; }
32 bool runOnFunction(Function &F) {
33 MachineCodeForMethod &mcodeInfo = MachineCodeForMethod::get(&F);
34 if (!mcodeInfo.isCompiledAsLeafMethod()) {
41 void InsertPrologCode(Function &F);
42 void InsertEpilogCode(Function &F);
45 } // End anonymous namespace
47 //------------------------------------------------------------------------
48 // External Function: GetInstructionsForProlog
49 // External Function: GetInstructionsForEpilog
52 // Create prolog and epilog code for procedure entry and exit
53 //------------------------------------------------------------------------
55 void InsertPrologEpilogCode::InsertPrologCode(Function &F)
57 std::vector<MachineInstr*> mvec;
59 const MachineFrameInfo& frameInfo = Target.getFrameInfo();
61 // The second operand is the stack size. If it does not fit in the
62 // immediate field, we have to use a free register to hold the size.
63 // We will assume that local register `l0' is unused since the SAVE
64 // instruction must be the first instruction in each procedure.
66 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(&F);
67 unsigned int staticStackSize = mcInfo.getStaticStackSize();
69 if (staticStackSize < (unsigned) frameInfo.getMinStackFrameSize())
70 staticStackSize = (unsigned) frameInfo.getMinStackFrameSize();
72 if (unsigned padsz = (staticStackSize %
73 (unsigned) frameInfo.getStackFrameSizeAlignment()))
74 staticStackSize += frameInfo.getStackFrameSizeAlignment() - padsz;
76 if (Target.getInstrInfo().constantFitsInImmedField(SAVE, staticStackSize))
78 M = new MachineInstr(SAVE);
79 M->SetMachineOperandReg(0, Target.getRegInfo().getStackPointer());
80 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
81 - (int) staticStackSize);
82 M->SetMachineOperandReg(2, Target.getRegInfo().getStackPointer());
87 // We have to put the stack size value into a register before SAVE.
88 // Use register %l0 to since it must be unused at function entry.
89 // Do this by creating a code sequence equivalent to:
90 // SETSW -(stackSize), %l0
91 int32_t C = - (int) staticStackSize;
92 int uregNum = Target.getRegInfo().getUnifiedRegNum(
93 Target.getRegInfo().getRegClassIDOfType(Type::IntTy),
94 SparcIntRegOrder::l0);
96 M = new MachineInstr(SETHI);
97 M->SetMachineOperandConst(0, MachineOperand::MO_SignExtendedImmed, C);
98 M->SetMachineOperandReg(1, uregNum);
102 M = new MachineInstr(OR);
103 M->SetMachineOperandReg(0, uregNum);
104 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed, C);
105 M->SetMachineOperandReg(2, uregNum);
106 M->setOperandLo32(1);
109 M = new MachineInstr(SRA);
110 M->SetMachineOperandReg(0, uregNum);
111 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed, 0);
112 M->SetMachineOperandReg(2, uregNum);
115 // Now generate the SAVE using the value in register %l0
116 M = new MachineInstr(SAVE);
117 M->SetMachineOperandReg(0, Target.getRegInfo().getStackPointer());
118 M->SetMachineOperandReg(1, uregNum);
119 M->SetMachineOperandReg(2, Target.getRegInfo().getStackPointer());
123 MachineCodeForBasicBlock& bbMvec = MachineCodeForBasicBlock::get(&F.getEntryNode());
124 bbMvec.insert(bbMvec.begin(), mvec.begin(), mvec.end());
127 void InsertPrologEpilogCode::InsertEpilogCode(Function &F)
129 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
130 Instruction *TermInst = (Instruction*)I->getTerminator();
131 if (TermInst->getOpcode() == Instruction::Ret)
133 MachineInstr *Restore = new MachineInstr(RESTORE);
134 Restore->SetMachineOperandReg(0, Target.getRegInfo().getZeroRegNum());
135 Restore->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
137 Restore->SetMachineOperandReg(2, Target.getRegInfo().getZeroRegNum());
139 MachineCodeForBasicBlock& bbMvec = MachineCodeForBasicBlock::get(I);
140 MachineCodeForInstruction &termMvec =
141 MachineCodeForInstruction::get(TermInst);
143 // Remove the NOPs in the delay slots of the return instruction
144 const MachineInstrInfo &mii = Target.getInstrInfo();
145 unsigned numNOPs = 0;
146 while (termMvec.back()->getOpCode() == NOP)
148 assert( termMvec.back() == bbMvec.back());
149 delete bbMvec.pop_back();
153 assert(termMvec.back() == bbMvec.back());
155 // Check that we found the right number of NOPs and have the right
156 // number of instructions to replace them.
157 unsigned ndelays = mii.getNumDelaySlots(termMvec.back()->getOpCode());
158 assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
159 assert(ndelays == 1 && "Cannot use epilog code for delay slots?");
161 // Append the epilog code to the end of the basic block.
162 bbMvec.push_back(Restore);
167 Pass *createPrologEpilogCodeInserter(TargetMachine &TM) {
168 return new InsertPrologEpilogCode(TM);