Rewrote uses of deprecated `MachineFunction::get(BasicBlock *BB)'.
[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/MachineInstr.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 int 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   if (Target.getInstrInfo().constantFitsInImmedField(SAVE, staticStackSize))
73     {
74       M = new MachineInstr(SAVE);
75       M->SetMachineOperandReg(0, Target.getRegInfo().getStackPointer());
76       M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
77                                    - (int) staticStackSize);
78       M->SetMachineOperandReg(2, Target.getRegInfo().getStackPointer());
79       mvec.push_back(M);
80     }
81   else
82     {
83       // We have to put the stack size value into a register before SAVE.
84       // Use register %g1 since it is volatile across calls.  Note that the
85       // local (%l) and in (%i) registers cannot be used before the SAVE!
86       // Do this by creating a code sequence equivalent to:
87       //        SETSW -(stackSize), %g1
88       int32_t C = - (int) staticStackSize;
89       int uregNum = Target.getRegInfo().getUnifiedRegNum(
90                            Target.getRegInfo().getRegClassIDOfType(Type::IntTy),
91                            SparcIntRegClass::g1);
92       
93       M = new MachineInstr(SETHI);
94       M->SetMachineOperandConst(0, MachineOperand::MO_SignExtendedImmed, C);
95       M->SetMachineOperandReg(1, uregNum); 
96       M->setOperandHi32(0);
97       mvec.push_back(M);
98       
99       M = new MachineInstr(OR);
100       M->SetMachineOperandReg(0, uregNum);
101       M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed, C);
102       M->SetMachineOperandReg(2, uregNum);
103       M->setOperandLo32(1);
104       mvec.push_back(M);
105       
106       M = new MachineInstr(SRA);
107       M->SetMachineOperandReg(0, uregNum);
108       M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed, 0);
109       M->SetMachineOperandReg(2, uregNum);
110       mvec.push_back(M);
111       
112       // Now generate the SAVE using the value in register %g1
113       M = new MachineInstr(SAVE);
114       M->SetMachineOperandReg(0, Target.getRegInfo().getStackPointer());
115       M->SetMachineOperandReg(1, uregNum);
116       M->SetMachineOperandReg(2, Target.getRegInfo().getStackPointer());
117       mvec.push_back(M);
118     }
119
120   MachineBasicBlock& bbMvec = mcInfo.front();
121   bbMvec.insert(bbMvec.begin(), mvec.begin(), mvec.end());
122 }
123
124 void InsertPrologEpilogCode::InsertEpilogCode(Function &F)
125 {
126   MachineFunction &MF = MachineFunction::get(&F);
127   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
128     MachineBasicBlock &MBB = *I;
129     BasicBlock &BB = *I->getBasicBlock();
130     Instruction *TermInst = (Instruction*)BB.getTerminator();
131     if (TermInst->getOpcode() == Instruction::Ret)
132       {
133         MachineInstr *Restore = new MachineInstr(RESTORE);
134         Restore->SetMachineOperandReg(0, Target.getRegInfo().getZeroRegNum());
135         Restore->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
136                                         (int64_t)0);
137         Restore->SetMachineOperandReg(2, Target.getRegInfo().getZeroRegNum());
138         
139         MachineCodeForInstruction &termMvec =
140           MachineCodeForInstruction::get(TermInst);
141         
142         // Remove the NOPs in the delay slots of the return instruction
143         const MachineInstrInfo &mii = Target.getInstrInfo();
144         unsigned numNOPs = 0;
145         while (termMvec.back()->getOpCode() == NOP)
146           {
147             assert( termMvec.back() == MBB.back());
148             delete MBB.pop_back();
149             termMvec.pop_back();
150             ++numNOPs;
151           }
152         assert(termMvec.back() == MBB.back());
153         
154         // Check that we found the right number of NOPs and have the right
155         // number of instructions to replace them.
156         unsigned ndelays = mii.getNumDelaySlots(termMvec.back()->getOpCode());
157         assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
158         assert(ndelays == 1 && "Cannot use epilog code for delay slots?");
159         
160         // Append the epilog code to the end of the basic block.
161         MBB.push_back(Restore);
162       }
163   }
164 }
165
166 Pass* UltraSparc::getPrologEpilogInsertionPass() {
167   return new InsertPrologEpilogCode(*this);
168 }