Remove separate vector of implicit refs from MachineInstr, and
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9PrologEpilogInserter.cpp
1 //===-- PrologEpilogCodeInserter.cpp - Insert Prolog & Epilog code for fn -===//
2 //
3 // Insert SAVE/RESTORE instructions for the function
4 //
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.
9 //
10 //===----------------------------------------------------------------------===//
11
12 #include "SparcInternals.h"
13 #include "SparcRegClassInfo.h"
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/CodeGen/MachineCodeForInstruction.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/CodeGen/InstrSelectionSupport.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Function.h"
20
21 namespace {
22   class InsertPrologEpilogCode : public FunctionPass {
23     TargetMachine &Target;
24   public:
25     InsertPrologEpilogCode(TargetMachine &T) : Target(T) {}
26     
27     const char *getPassName() const { return "Sparc Prolog/Epilog Inserter"; }
28     
29     bool runOnFunction(Function &F) {
30       MachineFunction &mcodeInfo = MachineFunction::get(&F);
31       if (!mcodeInfo.isCompiledAsLeafMethod()) {
32         InsertPrologCode(F);
33         InsertEpilogCode(F);
34       }
35       return false;
36     }
37     
38     void InsertPrologCode(Function &F);
39     void InsertEpilogCode(Function &F);
40   };
41
42 }  // End anonymous namespace
43
44 //------------------------------------------------------------------------ 
45 // External Function: GetInstructionsForProlog
46 // External Function: GetInstructionsForEpilog
47 //
48 // Purpose:
49 //   Create prolog and epilog code for procedure entry and exit
50 //------------------------------------------------------------------------ 
51
52 void InsertPrologEpilogCode::InsertPrologCode(Function &F)
53 {
54   std::vector<MachineInstr*> mvec;
55   MachineInstr* M;
56   const MachineFrameInfo& frameInfo = Target.getFrameInfo();
57   
58   // The second operand is the stack size. If it does not fit in the
59   // immediate field, we have to use a free register to hold the size.
60   // See the comments below for the choice of this register.
61   // 
62   MachineFunction& mcInfo = MachineFunction::get(&F);
63   unsigned staticStackSize = mcInfo.getStaticStackSize();
64   
65   if (staticStackSize < (unsigned) frameInfo.getMinStackFrameSize())
66     staticStackSize = (unsigned) frameInfo.getMinStackFrameSize();
67
68   if (unsigned padsz = (staticStackSize %
69                         (unsigned) frameInfo.getStackFrameSizeAlignment()))
70     staticStackSize += frameInfo.getStackFrameSizeAlignment() - padsz;
71   
72   int32_t C = - (int) staticStackSize;
73   int SP = Target.getRegInfo().getStackPointer();
74   if (Target.getInstrInfo().constantFitsInImmedField(SAVE, staticStackSize)) {
75     M = BuildMI(SAVE, 3).addMReg(SP).addSImm(C).addMReg(SP);
76     mvec.push_back(M);
77   } else {
78       // We have to put the stack size value into a register before SAVE.
79       // Use register %g1 since it is volatile across calls.  Note that the
80       // local (%l) and in (%i) registers cannot be used before the SAVE!
81       // Do this by creating a code sequence equivalent to:
82       //        SETSW -(stackSize), %g1
83       int uregNum = Target.getRegInfo().getUnifiedRegNum(
84                            Target.getRegInfo().getRegClassIDOfType(Type::IntTy),
85                            SparcIntRegClass::g1);
86       
87       M = BuildMI(SETHI, 2).addSImm(C).addMReg(uregNum);
88       M->setOperandHi32(0);
89       mvec.push_back(M);
90       
91       M = BuildMI(OR, 3).addMReg(uregNum).addSImm(C).addMReg(uregNum);
92       M->setOperandLo32(1);
93       mvec.push_back(M);
94       
95       M = BuildMI(SRA, 3).addMReg(uregNum).addZImm(0).addMReg(uregNum);
96       mvec.push_back(M);
97       
98       // Now generate the SAVE using the value in register %g1
99       M = BuildMI(SAVE, 3).addMReg(SP).addMReg(uregNum).addMReg(SP);
100       mvec.push_back(M);
101     }
102
103   MachineBasicBlock& bbMvec = mcInfo.front();
104   bbMvec.insert(bbMvec.begin(), mvec.begin(), mvec.end());
105 }
106
107 void InsertPrologEpilogCode::InsertEpilogCode(Function &F)
108 {
109   MachineFunction &MF = MachineFunction::get(&F);
110   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
111     MachineBasicBlock &MBB = *I;
112     BasicBlock &BB = *I->getBasicBlock();
113     Instruction *TermInst = (Instruction*)BB.getTerminator();
114     if (TermInst->getOpcode() == Instruction::Ret)
115       {
116         int ZR = Target.getRegInfo().getZeroRegNum();
117         MachineInstr *Restore =
118           BuildMI(RESTORE, 3).addMReg(ZR).addSImm(0).addMReg(ZR);
119         
120         MachineCodeForInstruction &termMvec =
121           MachineCodeForInstruction::get(TermInst);
122         
123         // Remove the NOPs in the delay slots of the return instruction
124         const MachineInstrInfo &mii = Target.getInstrInfo();
125         unsigned numNOPs = 0;
126         while (termMvec.back()->getOpCode() == NOP)
127           {
128             assert( termMvec.back() == MBB.back());
129             delete MBB.pop_back();
130             termMvec.pop_back();
131             ++numNOPs;
132           }
133         assert(termMvec.back() == MBB.back());
134         
135         // Check that we found the right number of NOPs and have the right
136         // number of instructions to replace them.
137         unsigned ndelays = mii.getNumDelaySlots(termMvec.back()->getOpCode());
138         assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
139         assert(ndelays == 1 && "Cannot use epilog code for delay slots?");
140         
141         // Append the epilog code to the end of the basic block.
142         MBB.push_back(Restore);
143       }
144   }
145 }
146
147 Pass* UltraSparc::getPrologEpilogInsertionPass() {
148   return new InsertPrologEpilogCode(*this);
149 }