323d0cfb5abd675b513baa10b03057ef560afa62
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9PrologEpilogInserter.cpp
1 //===-- PrologEpilogCodeInserter.cpp - Insert Prolog & Epilog code for fn -===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // Insert SAVE/RESTORE instructions for the function
11 //
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.
16 //
17 //===----------------------------------------------------------------------===//
18
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"
29
30 namespace llvm {
31
32 namespace {
33   struct InsertPrologEpilogCode : public MachineFunctionPass {
34     const char *getPassName() const { return "Sparc Prolog/Epilog Inserter"; }
35     
36     bool runOnMachineFunction(MachineFunction &F) {
37       if (!F.getInfo()->isCompiledAsLeafMethod()) {
38         InsertPrologCode(F);
39         InsertEpilogCode(F);
40       }
41       return false;
42     }
43     
44     void InsertPrologCode(MachineFunction &F);
45     void InsertEpilogCode(MachineFunction &F);
46   };
47
48 }  // End anonymous namespace
49
50 //------------------------------------------------------------------------ 
51 //   Create prolog and epilog code for procedure entry and exit
52 //------------------------------------------------------------------------ 
53
54 void InsertPrologEpilogCode::InsertPrologCode(MachineFunction &MF)
55 {
56   std::vector<MachineInstr*> mvec;
57   const TargetMachine &TM = MF.getTarget();
58   const TargetFrameInfo& frameInfo = TM.getFrameInfo();
59   
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.
63   // 
64   unsigned staticStackSize = MF.getInfo()->getStaticStackSize();
65   
66   if (staticStackSize < (unsigned) frameInfo.getMinStackFrameSize())
67     staticStackSize = (unsigned) frameInfo.getMinStackFrameSize();
68
69   if (unsigned padsz = (staticStackSize %
70                         (unsigned) frameInfo.getStackFrameSizeAlignment()))
71     staticStackSize += frameInfo.getStackFrameSizeAlignment() - padsz;
72   
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));
78   } else {
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);
87
88     MachineInstr* M = BuildMI(V9::SETHI, 2).addSImm(C)
89       .addMReg(uregNum, MOTy::Def);
90     M->setOperandHi32(0);
91     mvec.push_back(M);
92     
93     M = BuildMI(V9::ORi, 3).addMReg(uregNum).addSImm(C)
94       .addMReg(uregNum, MOTy::Def);
95     M->setOperandLo32(1);
96     mvec.push_back(M);
97     
98     M = BuildMI(V9::SRAi5, 3).addMReg(uregNum).addZImm(0)
99       .addMReg(uregNum, MOTy::Def);
100     mvec.push_back(M);
101     
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);
104     mvec.push_back(M);
105   }
106
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. 
113   // 
114   if (MF.getFunction()->getFunctionType()->isVarArg()) {
115     int numFixedArgs    = MF.getFunction()->getFunctionType()->getNumParams();
116     int numArgRegs      = TM.getRegInfo().getNumOfIntArgRegs();
117     if (numFixedArgs < numArgRegs) {
118       bool ignore;
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;
126
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;
131       }
132     }
133   }
134
135   MF.front().insert(MF.front().begin(), mvec.begin(), mvec.end());
136 }
137
138 void InsertPrologEpilogCode::InsertEpilogCode(MachineFunction &MF)
139 {
140   const TargetMachine &TM = MF.getTarget();
141   const TargetInstrInfo &MII = TM.getInstrInfo();
142
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)
148     {
149       int ZR = TM.getRegInfo().getZeroRegNum();
150       MachineInstr *Restore = 
151         BuildMI(V9::RESTOREi, 3).addMReg(ZR).addSImm(0).addMReg(ZR, MOTy::Def);
152       
153       MachineCodeForInstruction &termMvec =
154         MachineCodeForInstruction::get(TermInst);
155       
156       // Remove the NOPs in the delay slots of the return instruction
157       unsigned numNOPs = 0;
158       while (termMvec.back()->getOpCode() == V9::NOP)
159       {
160         assert( termMvec.back() == MBB.back());
161         delete MBB.pop_back();
162         termMvec.pop_back();
163         ++numNOPs;
164       }
165       assert(termMvec.back() == MBB.back());
166         
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?");
172         
173       // Append the epilog code to the end of the basic block.
174       MBB.push_back(Restore);
175     }
176   }
177 }
178
179 FunctionPass *createPrologEpilogInsertionPass() {
180   return new InsertPrologEpilogCode();
181 }
182
183 } // End llvm namespace