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