blob: 79f6871505423d3a2d66042c5a2269af5d0b48b1 [file] [log] [blame]
Chris Lattner5074bb52002-04-09 05:18:31 +00001//===-- 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
22namespace {
Chris Lattner7076ff22002-06-25 16:13:21 +000023 class InsertPrologEpilogCode : public FunctionPass {
24 TargetMachine &Target;
25 public:
26 InsertPrologEpilogCode(TargetMachine &T) : Target(T) {}
27
28 const char *getPassName() const { return "Sparc Prolog/Epilog Inserter"; }
29
30 bool runOnFunction(Function &F) {
31 MachineCodeForMethod &mcodeInfo = MachineCodeForMethod::get(&F);
32 if (!mcodeInfo.isCompiledAsLeafMethod()) {
33 InsertPrologCode(F);
34 InsertEpilogCode(F);
35 }
36 return false;
Chris Lattner5074bb52002-04-09 05:18:31 +000037 }
Chris Lattner7076ff22002-06-25 16:13:21 +000038
39 void InsertPrologCode(Function &F);
40 void InsertEpilogCode(Function &F);
41 };
Chris Lattner5074bb52002-04-09 05:18:31 +000042
43} // End anonymous namespace
44
45//------------------------------------------------------------------------
46// External Function: GetInstructionsForProlog
47// External Function: GetInstructionsForEpilog
48//
49// Purpose:
50// Create prolog and epilog code for procedure entry and exit
51//------------------------------------------------------------------------
52
Chris Lattner7076ff22002-06-25 16:13:21 +000053void InsertPrologEpilogCode::InsertPrologCode(Function &F)
Chris Lattner5074bb52002-04-09 05:18:31 +000054{
Anand Shukla458496c2002-06-25 20:55:50 +000055 std::vector<MachineInstr*> mvec;
Chris Lattner5074bb52002-04-09 05:18:31 +000056 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 //
Chris Lattner7076ff22002-06-25 16:13:21 +000064 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(&F);
Chris Lattner5074bb52002-04-09 05:18:31 +000065 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
Chris Lattner7076ff22002-06-25 16:13:21 +0000104 MachineCodeForBasicBlock& bbMvec = F.getEntryNode().getMachineInstrVec();
105 bbMvec.insert(bbMvec.begin(), mvec.begin(), mvec.end());
Chris Lattner5074bb52002-04-09 05:18:31 +0000106}
107
Chris Lattner7076ff22002-06-25 16:13:21 +0000108void InsertPrologEpilogCode::InsertEpilogCode(Function &F)
Chris Lattner5074bb52002-04-09 05:18:31 +0000109{
Chris Lattner7076ff22002-06-25 16:13:21 +0000110 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
111 Instruction *TermInst = (Instruction*)I->getTerminator();
Chris Lattner5074bb52002-04-09 05:18:31 +0000112 if (TermInst->getOpcode() == Instruction::Ret)
113 {
Chris Lattner5074bb52002-04-09 05:18:31 +0000114 MachineInstr *Restore = new MachineInstr(RESTORE);
115 Restore->SetMachineOperandReg(0, Target.getRegInfo().getZeroRegNum());
116 Restore->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
117 (int64_t)0);
118 Restore->SetMachineOperandReg(2, Target.getRegInfo().getZeroRegNum());
119
Chris Lattner7076ff22002-06-25 16:13:21 +0000120 MachineCodeForBasicBlock& bbMvec = I->getMachineInstrVec();
Chris Lattner5074bb52002-04-09 05:18:31 +0000121 MachineCodeForInstruction &termMvec =
122 MachineCodeForInstruction::get(TermInst);
123
124 // Remove the NOPs in the delay slots of the return instruction
125 const MachineInstrInfo &mii = Target.getInstrInfo();
126 unsigned numNOPs = 0;
127 while (termMvec.back()->getOpCode() == NOP)
128 {
129 assert( termMvec.back() == bbMvec.back());
Chris Lattner6b17c832002-04-09 18:02:02 +0000130 delete bbMvec.pop_back();
Chris Lattner5074bb52002-04-09 05:18:31 +0000131 termMvec.pop_back();
Chris Lattner5074bb52002-04-09 05:18:31 +0000132 ++numNOPs;
133 }
134 assert(termMvec.back() == bbMvec.back());
135
136 // Check that we found the right number of NOPs and have the right
137 // number of instructions to replace them.
138 unsigned ndelays = mii.getNumDelaySlots(termMvec.back()->getOpCode());
139 assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
140 assert(ndelays == 1 && "Cannot use epilog code for delay slots?");
141
142 // Append the epilog code to the end of the basic block.
143 bbMvec.push_back(Restore);
144 }
145 }
146}
147
148Pass *createPrologEpilogCodeInserter(TargetMachine &TM) {
149 return new InsertPrologEpilogCode(TM);
150}