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"
23 class InsertPrologEpilogCode : public FunctionPass {
24 TargetMachine &Target;
26 InsertPrologEpilogCode(TargetMachine &T) : Target(T) {}
28 const char *getPassName() const { return "Sparc Prolog/Epilog Inserter"; }
30 bool runOnFunction(Function &F) {
31 MachineCodeForMethod &mcodeInfo = MachineCodeForMethod::get(&F);
32 if (!mcodeInfo.isCompiledAsLeafMethod()) {
39 void InsertPrologCode(Function &F);
40 void InsertEpilogCode(Function &F);
43 } // End anonymous namespace
45 //------------------------------------------------------------------------
46 // External Function: GetInstructionsForProlog
47 // External Function: GetInstructionsForEpilog
50 // Create prolog and epilog code for procedure entry and exit
51 //------------------------------------------------------------------------
53 void InsertPrologEpilogCode::InsertPrologCode(Function &F)
55 std::vector<MachineInstr*> mvec;
57 const MachineFrameInfo& frameInfo = Target.getFrameInfo();
59 // The second operand is the stack size. If it does not fit in the
60 // immediate field, we have to use a free register to hold the size.
61 // We will assume that local register `l0' is unused since the SAVE
62 // instruction must be the first instruction in each procedure.
64 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(&F);
65 unsigned int staticStackSize = mcInfo.getStaticStackSize();
67 if (staticStackSize < (unsigned) frameInfo.getMinStackFrameSize())
68 staticStackSize = (unsigned) frameInfo.getMinStackFrameSize();
70 if (unsigned padsz = (staticStackSize %
71 (unsigned) frameInfo.getStackFrameSizeAlignment()))
72 staticStackSize += frameInfo.getStackFrameSizeAlignment() - padsz;
74 if (Target.getInstrInfo().constantFitsInImmedField(SAVE, staticStackSize))
76 M = new MachineInstr(SAVE);
77 M->SetMachineOperandReg(0, Target.getRegInfo().getStackPointer());
78 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
79 - (int) staticStackSize);
80 M->SetMachineOperandReg(2, Target.getRegInfo().getStackPointer());
85 // We have to put the stack size value into a register before SAVE.
86 // Use register %l0 to since it must be unused at function entry.
87 // Do this by creating a code sequence equivalent to:
88 // SETSW -(stackSize), %l0
89 int32_t C = - (int) staticStackSize;
90 int uregNum = Target.getRegInfo().getUnifiedRegNum(
91 Target.getRegInfo().getRegClassIDOfType(Type::IntTy),
92 SparcIntRegClass::l0);
94 M = new MachineInstr(SETHI);
95 M->SetMachineOperandConst(0, MachineOperand::MO_SignExtendedImmed, C);
96 M->SetMachineOperandReg(1, uregNum);
100 M = new MachineInstr(OR);
101 M->SetMachineOperandReg(0, uregNum);
102 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed, C);
103 M->SetMachineOperandReg(2, uregNum);
104 M->setOperandLo32(1);
107 M = new MachineInstr(SRA);
108 M->SetMachineOperandReg(0, uregNum);
109 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed, 0);
110 M->SetMachineOperandReg(2, uregNum);
113 // Now generate the SAVE using the value in register %l0
114 M = new MachineInstr(SAVE);
115 M->SetMachineOperandReg(0, Target.getRegInfo().getStackPointer());
116 M->SetMachineOperandReg(1, uregNum);
117 M->SetMachineOperandReg(2, Target.getRegInfo().getStackPointer());
121 MachineCodeForBasicBlock& bbMvec = MachineCodeForBasicBlock::get(&F.getEntryNode());
122 bbMvec.insert(bbMvec.begin(), mvec.begin(), mvec.end());
125 void InsertPrologEpilogCode::InsertEpilogCode(Function &F)
127 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
128 Instruction *TermInst = (Instruction*)I->getTerminator();
129 if (TermInst->getOpcode() == Instruction::Ret)
131 MachineInstr *Restore = new MachineInstr(RESTORE);
132 Restore->SetMachineOperandReg(0, Target.getRegInfo().getZeroRegNum());
133 Restore->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
135 Restore->SetMachineOperandReg(2, Target.getRegInfo().getZeroRegNum());
137 MachineCodeForBasicBlock& bbMvec = MachineCodeForBasicBlock::get(I);
138 MachineCodeForInstruction &termMvec =
139 MachineCodeForInstruction::get(TermInst);
141 // Remove the NOPs in the delay slots of the return instruction
142 const MachineInstrInfo &mii = Target.getInstrInfo();
143 unsigned numNOPs = 0;
144 while (termMvec.back()->getOpCode() == NOP)
146 assert( termMvec.back() == bbMvec.back());
147 delete bbMvec.pop_back();
151 assert(termMvec.back() == bbMvec.back());
153 // Check that we found the right number of NOPs and have the right
154 // number of instructions to replace them.
155 unsigned ndelays = mii.getNumDelaySlots(termMvec.back()->getOpCode());
156 assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
157 assert(ndelays == 1 && "Cannot use epilog code for delay slots?");
159 // Append the epilog code to the end of the basic block.
160 bbMvec.push_back(Restore);
165 Pass* UltraSparc::getPrologEpilogInsertionPass() {
166 return new InsertPrologEpilogCode(*this);