blob: 349cef04f8def995a156d22f52c305eb71e8a5bd [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"
Vikram S. Adve1ce40962002-07-08 23:31:24 +000015#include "llvm/CodeGen/MachineCodeForBasicBlock.h"
Chris Lattner5074bb52002-04-09 05:18:31 +000016#include "llvm/CodeGen/MachineCodeForInstruction.h"
17#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Advee997a112002-07-10 21:42:13 +000018#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattner5074bb52002-04-09 05:18:31 +000019#include "llvm/Pass.h"
20#include "llvm/Function.h"
21#include "llvm/BasicBlock.h"
22#include "llvm/Instruction.h"
23
24namespace {
Chris Lattner7076ff22002-06-25 16:13:21 +000025 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;
Chris Lattner5074bb52002-04-09 05:18:31 +000039 }
Chris Lattner7076ff22002-06-25 16:13:21 +000040
41 void InsertPrologCode(Function &F);
42 void InsertEpilogCode(Function &F);
43 };
Chris Lattner5074bb52002-04-09 05:18:31 +000044
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
Chris Lattner7076ff22002-06-25 16:13:21 +000055void InsertPrologEpilogCode::InsertPrologCode(Function &F)
Chris Lattner5074bb52002-04-09 05:18:31 +000056{
Anand Shukla458496c2002-06-25 20:55:50 +000057 std::vector<MachineInstr*> mvec;
Chris Lattner5074bb52002-04-09 05:18:31 +000058 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 //
Chris Lattner7076ff22002-06-25 16:13:21 +000066 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(&F);
Chris Lattner5074bb52002-04-09 05:18:31 +000067 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 {
Vikram S. Advee997a112002-07-10 21:42:13 +000087 // 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(
Chris Lattner5074bb52002-04-09 05:18:31 +000093 Target.getRegInfo().getRegClassIDOfType(Type::IntTy),
Vikram S. Advee997a112002-07-10 21:42:13 +000094 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);
Chris Lattner5074bb52002-04-09 05:18:31 +0000100 mvec.push_back(M);
101
Vikram S. Advee997a112002-07-10 21:42:13 +0000102 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
Chris Lattner5074bb52002-04-09 05:18:31 +0000116 M = new MachineInstr(SAVE);
117 M->SetMachineOperandReg(0, Target.getRegInfo().getStackPointer());
Vikram S. Advee997a112002-07-10 21:42:13 +0000118 M->SetMachineOperandReg(1, MachineOperand::MO_MachineRegister, uregNum);
Chris Lattner5074bb52002-04-09 05:18:31 +0000119 M->SetMachineOperandReg(2, Target.getRegInfo().getStackPointer());
120 mvec.push_back(M);
121 }
122
Vikram S. Adve1ce40962002-07-08 23:31:24 +0000123 MachineCodeForBasicBlock& bbMvec = MachineCodeForBasicBlock::get(&F.getEntryNode());
Chris Lattner7076ff22002-06-25 16:13:21 +0000124 bbMvec.insert(bbMvec.begin(), mvec.begin(), mvec.end());
Chris Lattner5074bb52002-04-09 05:18:31 +0000125}
126
Chris Lattner7076ff22002-06-25 16:13:21 +0000127void InsertPrologEpilogCode::InsertEpilogCode(Function &F)
Chris Lattner5074bb52002-04-09 05:18:31 +0000128{
Chris Lattner7076ff22002-06-25 16:13:21 +0000129 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
130 Instruction *TermInst = (Instruction*)I->getTerminator();
Chris Lattner5074bb52002-04-09 05:18:31 +0000131 if (TermInst->getOpcode() == Instruction::Ret)
132 {
Chris Lattner5074bb52002-04-09 05:18:31 +0000133 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
Vikram S. Adve1ce40962002-07-08 23:31:24 +0000139 MachineCodeForBasicBlock& bbMvec = MachineCodeForBasicBlock::get(I);
Chris Lattner5074bb52002-04-09 05:18:31 +0000140 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());
Chris Lattner6b17c832002-04-09 18:02:02 +0000149 delete bbMvec.pop_back();
Chris Lattner5074bb52002-04-09 05:18:31 +0000150 termMvec.pop_back();
Chris Lattner5074bb52002-04-09 05:18:31 +0000151 ++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
167Pass *createPrologEpilogCodeInserter(TargetMachine &TM) {
168 return new InsertPrologEpilogCode(TM);
169}