blob: 001d11574bbc78994f8acd3d02b547cb992e2300 [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"
Misha Brukman7ae7f842002-10-28 00:28:31 +000014#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner5074bb52002-04-09 05:18:31 +000015#include "llvm/CodeGen/MachineCodeForInstruction.h"
16#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Advee997a112002-07-10 21:42:13 +000017#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattner5074bb52002-04-09 05:18:31 +000018#include "llvm/Pass.h"
19#include "llvm/Function.h"
Chris Lattner5074bb52002-04-09 05:18:31 +000020
21namespace {
Chris Lattner7076ff22002-06-25 16:13:21 +000022 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) {
Misha Brukman7ae7f842002-10-28 00:28:31 +000030 MachineFunction &mcodeInfo = MachineFunction::get(&F);
Chris Lattner7076ff22002-06-25 16:13:21 +000031 if (!mcodeInfo.isCompiledAsLeafMethod()) {
32 InsertPrologCode(F);
33 InsertEpilogCode(F);
34 }
35 return false;
Chris Lattner5074bb52002-04-09 05:18:31 +000036 }
Chris Lattner7076ff22002-06-25 16:13:21 +000037
38 void InsertPrologCode(Function &F);
39 void InsertEpilogCode(Function &F);
40 };
Chris Lattner5074bb52002-04-09 05:18:31 +000041
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
Chris Lattner7076ff22002-06-25 16:13:21 +000052void InsertPrologEpilogCode::InsertPrologCode(Function &F)
Chris Lattner5074bb52002-04-09 05:18:31 +000053{
Anand Shukla458496c2002-06-25 20:55:50 +000054 std::vector<MachineInstr*> mvec;
Chris Lattner5074bb52002-04-09 05:18:31 +000055 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.
Vikram S. Adve78044fb2002-10-13 00:24:06 +000060 // See the comments below for the choice of this register.
Chris Lattner5074bb52002-04-09 05:18:31 +000061 //
Misha Brukman7ae7f842002-10-28 00:28:31 +000062 MachineFunction& mcInfo = MachineFunction::get(&F);
Chris Lattner5074bb52002-04-09 05:18:31 +000063 unsigned int staticStackSize = mcInfo.getStaticStackSize();
64
65 if (staticStackSize < (unsigned) frameInfo.getMinStackFrameSize())
66 staticStackSize = (unsigned) frameInfo.getMinStackFrameSize();
Vikram S. Adve78044fb2002-10-13 00:24:06 +000067
Chris Lattner5074bb52002-04-09 05:18:31 +000068 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 {
Vikram S. Advee997a112002-07-10 21:42:13 +000083 // We have to put the stack size value into a register before SAVE.
Vikram S. Adve78044fb2002-10-13 00:24:06 +000084 // 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!
Vikram S. Advee997a112002-07-10 21:42:13 +000086 // Do this by creating a code sequence equivalent to:
Vikram S. Adve78044fb2002-10-13 00:24:06 +000087 // SETSW -(stackSize), %g1
Vikram S. Advee997a112002-07-10 21:42:13 +000088 int32_t C = - (int) staticStackSize;
89 int uregNum = Target.getRegInfo().getUnifiedRegNum(
Chris Lattner5074bb52002-04-09 05:18:31 +000090 Target.getRegInfo().getRegClassIDOfType(Type::IntTy),
Vikram S. Adve78044fb2002-10-13 00:24:06 +000091 SparcIntRegClass::g1);
Vikram S. Advee997a112002-07-10 21:42:13 +000092
93 M = new MachineInstr(SETHI);
94 M->SetMachineOperandConst(0, MachineOperand::MO_SignExtendedImmed, C);
95 M->SetMachineOperandReg(1, uregNum);
96 M->setOperandHi32(0);
Chris Lattner5074bb52002-04-09 05:18:31 +000097 mvec.push_back(M);
98
Vikram S. Advee997a112002-07-10 21:42:13 +000099 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
Vikram S. Adve78044fb2002-10-13 00:24:06 +0000112 // Now generate the SAVE using the value in register %g1
Chris Lattner5074bb52002-04-09 05:18:31 +0000113 M = new MachineInstr(SAVE);
114 M->SetMachineOperandReg(0, Target.getRegInfo().getStackPointer());
Vikram S. Adve6d6deba2002-08-01 14:26:11 +0000115 M->SetMachineOperandReg(1, uregNum);
Chris Lattner5074bb52002-04-09 05:18:31 +0000116 M->SetMachineOperandReg(2, Target.getRegInfo().getStackPointer());
117 mvec.push_back(M);
118 }
119
Chris Lattner8710aab2002-10-28 01:41:47 +0000120 MachineBasicBlock& bbMvec = MachineBasicBlock::get(&F.getEntryNode());
Chris Lattner7076ff22002-06-25 16:13:21 +0000121 bbMvec.insert(bbMvec.begin(), mvec.begin(), mvec.end());
Chris Lattner5074bb52002-04-09 05:18:31 +0000122}
123
Chris Lattner7076ff22002-06-25 16:13:21 +0000124void InsertPrologEpilogCode::InsertEpilogCode(Function &F)
Chris Lattner5074bb52002-04-09 05:18:31 +0000125{
Chris Lattner7076ff22002-06-25 16:13:21 +0000126 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
127 Instruction *TermInst = (Instruction*)I->getTerminator();
Chris Lattner5074bb52002-04-09 05:18:31 +0000128 if (TermInst->getOpcode() == Instruction::Ret)
129 {
Chris Lattner5074bb52002-04-09 05:18:31 +0000130 MachineInstr *Restore = new MachineInstr(RESTORE);
131 Restore->SetMachineOperandReg(0, Target.getRegInfo().getZeroRegNum());
132 Restore->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
133 (int64_t)0);
134 Restore->SetMachineOperandReg(2, Target.getRegInfo().getZeroRegNum());
135
Chris Lattner8710aab2002-10-28 01:41:47 +0000136 MachineBasicBlock& bbMvec = MachineBasicBlock::get(I);
Chris Lattner5074bb52002-04-09 05:18:31 +0000137 MachineCodeForInstruction &termMvec =
138 MachineCodeForInstruction::get(TermInst);
139
140 // Remove the NOPs in the delay slots of the return instruction
141 const MachineInstrInfo &mii = Target.getInstrInfo();
142 unsigned numNOPs = 0;
143 while (termMvec.back()->getOpCode() == NOP)
144 {
145 assert( termMvec.back() == bbMvec.back());
Chris Lattner6b17c832002-04-09 18:02:02 +0000146 delete bbMvec.pop_back();
Chris Lattner5074bb52002-04-09 05:18:31 +0000147 termMvec.pop_back();
Chris Lattner5074bb52002-04-09 05:18:31 +0000148 ++numNOPs;
149 }
150 assert(termMvec.back() == bbMvec.back());
151
152 // Check that we found the right number of NOPs and have the right
153 // number of instructions to replace them.
154 unsigned ndelays = mii.getNumDelaySlots(termMvec.back()->getOpCode());
155 assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
156 assert(ndelays == 1 && "Cannot use epilog code for delay slots?");
157
158 // Append the epilog code to the end of the basic block.
159 bbMvec.push_back(Restore);
160 }
161 }
162}
163
Vikram S. Adve36d3e032002-09-16 15:39:26 +0000164Pass* UltraSparc::getPrologEpilogInsertionPass() {
165 return new InsertPrologEpilogCode(*this);
Chris Lattner5074bb52002-04-09 05:18:31 +0000166}