1 //===-- PrologEpilogCodeInserter.cpp - Insert Prolog & Epilog code for fn -===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Insert SAVE/RESTORE instructions for the function
12 // Insert prolog code at the unique function entry point.
13 // Insert epilog code at each function exit point.
14 // InsertPrologEpilog invokes these only if the function is not compiled
15 // with the leaf function optimization.
17 //===----------------------------------------------------------------------===//
19 #include "SparcInternals.h"
20 #include "SparcRegClassInfo.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineFunctionInfo.h"
23 #include "llvm/CodeGen/MachineCodeForInstruction.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Function.h"
27 #include "llvm/DerivedTypes.h"
28 #include "llvm/Intrinsics.h"
33 struct InsertPrologEpilogCode : public MachineFunctionPass {
34 const char *getPassName() const { return "Sparc Prolog/Epilog Inserter"; }
36 bool runOnMachineFunction(MachineFunction &F) {
37 if (!F.getInfo()->isCompiledAsLeafMethod()) {
44 void InsertPrologCode(MachineFunction &F);
45 void InsertEpilogCode(MachineFunction &F);
48 } // End anonymous namespace
50 //------------------------------------------------------------------------
51 // Create prolog and epilog code for procedure entry and exit
52 //------------------------------------------------------------------------
54 void InsertPrologEpilogCode::InsertPrologCode(MachineFunction &MF)
56 std::vector<MachineInstr*> mvec;
57 const TargetMachine &TM = MF.getTarget();
58 const TargetFrameInfo& frameInfo = TM.getFrameInfo();
60 // The second operand is the stack size. If it does not fit in the
61 // immediate field, we have to use a free register to hold the size.
62 // See the comments below for the choice of this register.
64 unsigned staticStackSize = MF.getInfo()->getStaticStackSize();
66 if (staticStackSize < (unsigned) frameInfo.getMinStackFrameSize())
67 staticStackSize = (unsigned) frameInfo.getMinStackFrameSize();
69 if (unsigned padsz = (staticStackSize %
70 (unsigned) frameInfo.getStackFrameSizeAlignment()))
71 staticStackSize += frameInfo.getStackFrameSizeAlignment() - padsz;
73 int32_t C = - (int) staticStackSize;
74 int SP = TM.getRegInfo().getStackPointer();
75 if (TM.getInstrInfo().constantFitsInImmedField(V9::SAVEi,staticStackSize)) {
76 mvec.push_back(BuildMI(V9::SAVEi, 3).addMReg(SP).addSImm(C)
77 .addMReg(SP, MOTy::Def));
79 // We have to put the stack size value into a register before SAVE.
80 // Use register %g1 since it is volatile across calls. Note that the
81 // local (%l) and in (%i) registers cannot be used before the SAVE!
82 // Do this by creating a code sequence equivalent to:
83 // SETSW -(stackSize), %g1
84 int uregNum = TM.getRegInfo().getUnifiedRegNum(
85 TM.getRegInfo().getRegClassIDOfType(Type::IntTy),
86 SparcIntRegClass::g1);
88 MachineInstr* M = BuildMI(V9::SETHI, 2).addSImm(C)
89 .addMReg(uregNum, MOTy::Def);
93 M = BuildMI(V9::ORi, 3).addMReg(uregNum).addSImm(C)
94 .addMReg(uregNum, MOTy::Def);
98 M = BuildMI(V9::SRAi5, 3).addMReg(uregNum).addZImm(0)
99 .addMReg(uregNum, MOTy::Def);
102 // Now generate the SAVE using the value in register %g1
103 M = BuildMI(V9::SAVEr,3).addMReg(SP).addMReg(uregNum).addMReg(SP,MOTy::Def);
107 // For varargs function bodies, insert instructions to copy incoming
108 // register arguments for the ... list to the stack.
109 // The first K=6 arguments are always received via int arg regs
110 // (%i0 ... %i5 if K=6) .
111 // By copying the varargs arguments to the stack, va_arg() then can
112 // simply assume that all vararg arguments are in an array on the stack.
114 if (MF.getFunction()->getFunctionType()->isVarArg()) {
115 int numFixedArgs = MF.getFunction()->getFunctionType()->getNumParams();
116 int numArgRegs = TM.getRegInfo().getNumOfIntArgRegs();
117 if (numFixedArgs < numArgRegs) {
119 int firstArgReg = TM.getRegInfo().getUnifiedRegNum(
120 TM.getRegInfo().getRegClassIDOfType(Type::IntTy),
121 SparcIntRegClass::i0);
122 int fpReg = TM.getFrameInfo().getIncomingArgBaseRegNum();
123 int argSize = TM.getFrameInfo().getSizeOfEachArgOnStack();
124 int firstArgOffset=TM.getFrameInfo().getFirstIncomingArgOffset(MF,ignore);
125 int nextArgOffset = firstArgOffset + numFixedArgs * argSize;
127 for (int i=numFixedArgs; i < numArgRegs; ++i) {
128 mvec.push_back(BuildMI(V9::STXi, 3).addMReg(firstArgReg+i).
129 addMReg(fpReg).addSImm(nextArgOffset));
130 nextArgOffset += argSize;
135 MF.front().insert(MF.front().begin(), mvec.begin(), mvec.end());
138 void InsertPrologEpilogCode::InsertEpilogCode(MachineFunction &MF)
140 const TargetMachine &TM = MF.getTarget();
141 const TargetInstrInfo &MII = TM.getInstrInfo();
143 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
144 MachineBasicBlock &MBB = *I;
145 const BasicBlock &BB = *I->getBasicBlock();
146 const Instruction *TermInst = (Instruction*)BB.getTerminator();
147 if (TermInst->getOpcode() == Instruction::Ret)
149 int ZR = TM.getRegInfo().getZeroRegNum();
150 MachineInstr *Restore =
151 BuildMI(V9::RESTOREi, 3).addMReg(ZR).addSImm(0).addMReg(ZR, MOTy::Def);
153 MachineCodeForInstruction &termMvec =
154 MachineCodeForInstruction::get(TermInst);
156 // Remove the NOPs in the delay slots of the return instruction
157 unsigned numNOPs = 0;
158 while (termMvec.back()->getOpCode() == V9::NOP)
160 assert( termMvec.back() == MBB.back());
161 delete MBB.pop_back();
165 assert(termMvec.back() == MBB.back());
167 // Check that we found the right number of NOPs and have the right
168 // number of instructions to replace them.
169 unsigned ndelays = MII.getNumDelaySlots(termMvec.back()->getOpCode());
170 assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
171 assert(ndelays == 1 && "Cannot use epilog code for delay slots?");
173 // Append the epilog code to the end of the basic block.
174 MBB.push_back(Restore);
179 FunctionPass *createPrologEpilogInsertionPass() {
180 return new InsertPrologEpilogCode();
183 } // End llvm namespace