2dad57202070cd1cae390f8bddcc783553d45aed
[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/MachineCodeForMethod.h"
15 #include "llvm/CodeGen/MachineCodeForInstruction.h"
16 #include "llvm/CodeGen/MachineInstr.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Function.h"
19 #include "llvm/BasicBlock.h"
20 #include "llvm/Instruction.h"
21
22 namespace {
23
24 class InsertPrologEpilogCode : public MethodPass {
25   TargetMachine &Target;
26 public:
27   InsertPrologEpilogCode(TargetMachine &T) : Target(T) {}
28   bool runOnMethod(Function *F) {
29     MachineCodeForMethod &mcodeInfo = MachineCodeForMethod::get(F);
30     if (!mcodeInfo.isCompiledAsLeafMethod()) {
31       InsertPrologCode(F);
32       InsertEpilogCode(F);
33     }
34     return false;
35   }
36
37   void InsertPrologCode(Function *F);
38   void InsertEpilogCode(Function *F);
39 };
40
41 }  // End anonymous namespace
42
43 //------------------------------------------------------------------------ 
44 // External Function: GetInstructionsForProlog
45 // External Function: GetInstructionsForEpilog
46 //
47 // Purpose:
48 //   Create prolog and epilog code for procedure entry and exit
49 //------------------------------------------------------------------------ 
50
51 void InsertPrologEpilogCode::InsertPrologCode(Function *F)
52 {
53   BasicBlock *entryBB = F->getEntryNode();
54
55   vector<MachineInstr*> mvec;
56   MachineInstr* M;
57   const MachineFrameInfo& frameInfo = Target.getFrameInfo();
58   
59   // The second operand is the stack size. If it does not fit in the
60   // immediate field, we have to use a free register to hold the size.
61   // We will assume that local register `l0' is unused since the SAVE
62   // instruction must be the first instruction in each procedure.
63   // 
64   MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(F);
65   unsigned int staticStackSize = mcInfo.getStaticStackSize();
66   
67   if (staticStackSize < (unsigned) frameInfo.getMinStackFrameSize())
68     staticStackSize = (unsigned) frameInfo.getMinStackFrameSize();
69   
70   if (unsigned padsz = (staticStackSize %
71                         (unsigned) frameInfo.getStackFrameSizeAlignment()))
72     staticStackSize += frameInfo.getStackFrameSizeAlignment() - padsz;
73   
74   if (Target.getInstrInfo().constantFitsInImmedField(SAVE, staticStackSize))
75     {
76       M = new MachineInstr(SAVE);
77       M->SetMachineOperandReg(0, Target.getRegInfo().getStackPointer());
78       M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
79                                    - (int) staticStackSize);
80       M->SetMachineOperandReg(2, Target.getRegInfo().getStackPointer());
81       mvec.push_back(M);
82     }
83   else
84     {
85       M = new MachineInstr(SETSW);
86       M->SetMachineOperandConst(0, MachineOperand::MO_SignExtendedImmed,
87                                 - (int) staticStackSize);
88       M->SetMachineOperandReg(1, MachineOperand::MO_MachineRegister,
89                                  Target.getRegInfo().getUnifiedRegNum(
90                            Target.getRegInfo().getRegClassIDOfType(Type::IntTy),
91                                   SparcIntRegOrder::l0));
92       mvec.push_back(M);
93       
94       M = new MachineInstr(SAVE);
95       M->SetMachineOperandReg(0, Target.getRegInfo().getStackPointer());
96       M->SetMachineOperandReg(1, MachineOperand::MO_MachineRegister,
97                                  Target.getRegInfo().getUnifiedRegNum(
98                            Target.getRegInfo().getRegClassIDOfType(Type::IntTy),
99                                   SparcIntRegOrder::l0));
100       M->SetMachineOperandReg(2, Target.getRegInfo().getStackPointer());
101       mvec.push_back(M);
102     }
103
104   MachineCodeForBasicBlock& bbMvec = entryBB->getMachineInstrVec();
105   bbMvec.insert(entryBB->getMachineInstrVec().begin(),
106                 mvec.begin(), mvec.end());
107 }
108
109 void InsertPrologEpilogCode::InsertEpilogCode(Function *F)
110 {
111   for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
112     Instruction *TermInst = (Instruction*)(*I)->getTerminator();
113     if (TermInst->getOpcode() == Instruction::Ret)
114       {
115         BasicBlock* exitBB = *I;
116
117         MachineInstr *Restore = new MachineInstr(RESTORE);
118         Restore->SetMachineOperandReg(0, Target.getRegInfo().getZeroRegNum());
119         Restore->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
120                                         (int64_t)0);
121         Restore->SetMachineOperandReg(2, Target.getRegInfo().getZeroRegNum());
122         
123         MachineCodeForBasicBlock& bbMvec = exitBB->getMachineInstrVec();
124         MachineCodeForInstruction &termMvec =
125           MachineCodeForInstruction::get(TermInst);
126         
127         // Remove the NOPs in the delay slots of the return instruction
128         const MachineInstrInfo &mii = Target.getInstrInfo();
129         unsigned numNOPs = 0;
130         while (termMvec.back()->getOpCode() == NOP)
131           {
132             assert( termMvec.back() == bbMvec.back());
133             delete bbMvec.pop_back();
134             termMvec.pop_back();
135             ++numNOPs;
136           }
137         assert(termMvec.back() == bbMvec.back());
138         
139         // Check that we found the right number of NOPs and have the right
140         // number of instructions to replace them.
141         unsigned ndelays = mii.getNumDelaySlots(termMvec.back()->getOpCode());
142         assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
143         assert(ndelays == 1 && "Cannot use epilog code for delay slots?");
144         
145         // Append the epilog code to the end of the basic block.
146         bbMvec.push_back(Restore);
147       }
148   }
149 }
150
151 Pass *createPrologEpilogCodeInserter(TargetMachine &TM) {
152   return new InsertPrologEpilogCode(TM);
153 }