77aa098e311e55598d4e0a671f5ca48ec0a71cb5
[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, MachineOperand::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, MachineOperand::Def);
90     M->setOperandHi32(0);
91     mvec.push_back(M);
92     
93     M = BuildMI(V9::ORi, 3).addMReg(uregNum).addSImm(C)
94       .addMReg(uregNum, MachineOperand::Def);
95     M->setOperandLo32(1);
96     mvec.push_back(M);
97     
98     M = BuildMI(V9::SRAi5, 3).addMReg(uregNum).addZImm(0)
99       .addMReg(uregNum, MachineOperand::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)
104           .addMReg(SP,MachineOperand::Def);
105     mvec.push_back(M);
106   }
107
108   // For varargs function bodies, insert instructions to copy incoming
109   // register arguments for the ... list to the stack.
110   // The first K=6 arguments are always received via int arg regs
111   // (%i0 ... %i5 if K=6) .
112   // By copying the varargs arguments to the stack, va_arg() then can
113   // simply assume that all vararg arguments are in an array on the stack. 
114   // 
115   if (MF.getFunction()->getFunctionType()->isVarArg()) {
116     int numFixedArgs    = MF.getFunction()->getFunctionType()->getNumParams();
117     int numArgRegs      = TM.getRegInfo().getNumOfIntArgRegs();
118     if (numFixedArgs < numArgRegs) {
119       bool ignore;
120       int firstArgReg   = TM.getRegInfo().getUnifiedRegNum(
121                              TM.getRegInfo().getRegClassIDOfType(Type::IntTy),
122                              SparcIntRegClass::i0);
123       int fpReg         = TM.getFrameInfo().getIncomingArgBaseRegNum();
124       int argSize       = TM.getFrameInfo().getSizeOfEachArgOnStack();
125       int firstArgOffset=TM.getFrameInfo().getFirstIncomingArgOffset(MF,ignore);
126       int nextArgOffset = firstArgOffset + numFixedArgs * argSize;
127
128       for (int i=numFixedArgs; i < numArgRegs; ++i) {
129         mvec.push_back(BuildMI(V9::STXi, 3).addMReg(firstArgReg+i).
130                        addMReg(fpReg).addSImm(nextArgOffset));
131         nextArgOffset += argSize;
132       }
133     }
134   }
135
136   MF.front().insert(MF.front().begin(), mvec.begin(), mvec.end());
137 }
138
139 void InsertPrologEpilogCode::InsertEpilogCode(MachineFunction &MF)
140 {
141   const TargetMachine &TM = MF.getTarget();
142   const TargetInstrInfo &MII = TM.getInstrInfo();
143
144   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
145     MachineBasicBlock &MBB = *I;
146     const BasicBlock &BB = *I->getBasicBlock();
147     const Instruction *TermInst = (Instruction*)BB.getTerminator();
148     if (TermInst->getOpcode() == Instruction::Ret)
149     {
150       int ZR = TM.getRegInfo().getZeroRegNum();
151       MachineInstr *Restore = 
152         BuildMI(V9::RESTOREi, 3).addMReg(ZR).addSImm(0)
153           .addMReg(ZR, MachineOperand::Def);
154       
155       MachineCodeForInstruction &termMvec =
156         MachineCodeForInstruction::get(TermInst);
157       
158       // Remove the NOPs in the delay slots of the return instruction
159       unsigned numNOPs = 0;
160       while (termMvec.back()->getOpcode() == V9::NOP)
161       {
162         assert( termMvec.back() == &MBB.back());
163         termMvec.pop_back();
164         MBB.erase(&MBB.back());
165         ++numNOPs;
166       }
167       assert(termMvec.back() == &MBB.back());
168         
169       // Check that we found the right number of NOPs and have the right
170       // number of instructions to replace them.
171       unsigned ndelays = MII.getNumDelaySlots(termMvec.back()->getOpcode());
172       assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
173       assert(ndelays == 1 && "Cannot use epilog code for delay slots?");
174         
175       // Append the epilog code to the end of the basic block.
176       MBB.push_back(Restore);
177     }
178   }
179 }
180
181 FunctionPass *createPrologEpilogInsertionPass() {
182   return new InsertPrologEpilogCode();
183 }
184
185 } // End llvm namespace