2002-04-09 05:18:31 +00:00
|
|
|
//===-- PrologEpilogCodeInserter.cpp - Insert Prolog & Epilog code for fn -===//
|
|
|
|
//
|
|
|
|
// Insert SAVE/RESTORE instructions for the function
|
|
|
|
//
|
|
|
|
// Insert prolog code at the unique function entry point.
|
|
|
|
// Insert epilog code at each function exit point.
|
|
|
|
// InsertPrologEpilog invokes these only if the function is not compiled
|
|
|
|
// with the leaf function optimization.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SparcInternals.h"
|
|
|
|
#include "SparcRegClassInfo.h"
|
2002-12-28 20:16:08 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionInfo.h"
|
2002-04-09 05:18:31 +00:00
|
|
|
#include "llvm/CodeGen/MachineCodeForInstruction.h"
|
2002-10-28 21:43:57 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2002-04-09 05:18:31 +00:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Function.h"
|
2003-05-25 15:59:47 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Intrinsics.h"
|
2002-04-09 05:18:31 +00:00
|
|
|
|
|
|
|
namespace {
|
2002-12-28 20:16:08 +00:00
|
|
|
struct InsertPrologEpilogCode : public MachineFunctionPass {
|
2002-06-25 16:13:21 +00:00
|
|
|
const char *getPassName() const { return "Sparc Prolog/Epilog Inserter"; }
|
|
|
|
|
2002-12-28 20:16:08 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction &F) {
|
|
|
|
if (!F.getInfo()->isCompiledAsLeafMethod()) {
|
2002-06-25 16:13:21 +00:00
|
|
|
InsertPrologCode(F);
|
|
|
|
InsertEpilogCode(F);
|
|
|
|
}
|
|
|
|
return false;
|
2002-04-09 05:18:31 +00:00
|
|
|
}
|
2002-06-25 16:13:21 +00:00
|
|
|
|
2002-12-28 20:16:08 +00:00
|
|
|
void InsertPrologCode(MachineFunction &F);
|
|
|
|
void InsertEpilogCode(MachineFunction &F);
|
2002-06-25 16:13:21 +00:00
|
|
|
};
|
2002-04-09 05:18:31 +00:00
|
|
|
|
|
|
|
} // End anonymous namespace
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Create prolog and epilog code for procedure entry and exit
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
|
2002-12-28 20:16:08 +00:00
|
|
|
void InsertPrologEpilogCode::InsertPrologCode(MachineFunction &MF)
|
2002-04-09 05:18:31 +00:00
|
|
|
{
|
2002-06-25 20:55:50 +00:00
|
|
|
std::vector<MachineInstr*> mvec;
|
2002-12-28 20:16:08 +00:00
|
|
|
const TargetMachine &TM = MF.getTarget();
|
|
|
|
const TargetFrameInfo& frameInfo = TM.getFrameInfo();
|
2002-04-09 05:18:31 +00:00
|
|
|
|
|
|
|
// The second operand is the stack size. If it does not fit in the
|
|
|
|
// immediate field, we have to use a free register to hold the size.
|
2002-10-13 00:24:06 +00:00
|
|
|
// See the comments below for the choice of this register.
|
2002-04-09 05:18:31 +00:00
|
|
|
//
|
2002-12-28 20:16:08 +00:00
|
|
|
unsigned staticStackSize = MF.getInfo()->getStaticStackSize();
|
2002-04-09 05:18:31 +00:00
|
|
|
|
|
|
|
if (staticStackSize < (unsigned) frameInfo.getMinStackFrameSize())
|
|
|
|
staticStackSize = (unsigned) frameInfo.getMinStackFrameSize();
|
2002-10-13 00:24:06 +00:00
|
|
|
|
2002-04-09 05:18:31 +00:00
|
|
|
if (unsigned padsz = (staticStackSize %
|
|
|
|
(unsigned) frameInfo.getStackFrameSizeAlignment()))
|
|
|
|
staticStackSize += frameInfo.getStackFrameSizeAlignment() - padsz;
|
|
|
|
|
2002-10-28 21:43:57 +00:00
|
|
|
int32_t C = - (int) staticStackSize;
|
2002-12-28 20:16:08 +00:00
|
|
|
int SP = TM.getRegInfo().getStackPointer();
|
2003-05-27 22:35:43 +00:00
|
|
|
if (TM.getInstrInfo().constantFitsInImmedField(V9::SAVEi,staticStackSize)) {
|
|
|
|
mvec.push_back(BuildMI(V9::SAVEi, 3).addMReg(SP).addSImm(C)
|
|
|
|
.addMReg(SP, MOTy::Def));
|
2002-10-28 21:43:57 +00:00
|
|
|
} else {
|
2002-12-28 20:16:08 +00:00
|
|
|
// We have to put the stack size value into a register before SAVE.
|
|
|
|
// Use register %g1 since it is volatile across calls. Note that the
|
|
|
|
// local (%l) and in (%i) registers cannot be used before the SAVE!
|
|
|
|
// Do this by creating a code sequence equivalent to:
|
|
|
|
// SETSW -(stackSize), %g1
|
|
|
|
int uregNum = TM.getRegInfo().getUnifiedRegNum(
|
|
|
|
TM.getRegInfo().getRegClassIDOfType(Type::IntTy),
|
|
|
|
SparcIntRegClass::g1);
|
|
|
|
|
2003-05-20 20:32:24 +00:00
|
|
|
MachineInstr* M = BuildMI(V9::SETHI, 2).addSImm(C)
|
|
|
|
.addMReg(uregNum, MOTy::Def);
|
2002-12-28 20:16:08 +00:00
|
|
|
M->setOperandHi32(0);
|
|
|
|
mvec.push_back(M);
|
|
|
|
|
2003-05-27 22:35:43 +00:00
|
|
|
M = BuildMI(V9::ORi, 3).addMReg(uregNum).addSImm(C)
|
2003-05-20 20:32:24 +00:00
|
|
|
.addMReg(uregNum, MOTy::Def);
|
2002-12-28 20:16:08 +00:00
|
|
|
M->setOperandLo32(1);
|
|
|
|
mvec.push_back(M);
|
|
|
|
|
2003-06-06 09:52:23 +00:00
|
|
|
M = BuildMI(V9::SRAi5, 3).addMReg(uregNum).addZImm(0)
|
2003-05-20 20:32:24 +00:00
|
|
|
.addMReg(uregNum, MOTy::Def);
|
2002-12-28 20:16:08 +00:00
|
|
|
mvec.push_back(M);
|
|
|
|
|
|
|
|
// Now generate the SAVE using the value in register %g1
|
2003-05-27 22:35:43 +00:00
|
|
|
M = BuildMI(V9::SAVEr,3).addMReg(SP).addMReg(uregNum).addMReg(SP,MOTy::Def);
|
2002-12-28 20:16:08 +00:00
|
|
|
mvec.push_back(M);
|
|
|
|
}
|
2002-04-09 05:18:31 +00:00
|
|
|
|
2003-05-25 15:59:47 +00:00
|
|
|
// For varargs function bodies, insert instructions to copy incoming
|
|
|
|
// register arguments for the ... list to the stack.
|
|
|
|
// The first K=6 arguments are always received via int arg regs
|
|
|
|
// (%i0 ... %i5 if K=6) .
|
|
|
|
// By copying the varargs arguments to the stack, va_arg() then can
|
|
|
|
// simply assume that all vararg arguments are in an array on the stack.
|
|
|
|
//
|
|
|
|
if (MF.getFunction()->getFunctionType()->isVarArg()) {
|
|
|
|
int numFixedArgs = MF.getFunction()->getFunctionType()->getNumParams();
|
|
|
|
int numArgRegs = TM.getRegInfo().getNumOfIntArgRegs();
|
|
|
|
if (numFixedArgs < numArgRegs) {
|
|
|
|
bool ignore;
|
|
|
|
int firstArgReg = TM.getRegInfo().getUnifiedRegNum(
|
|
|
|
TM.getRegInfo().getRegClassIDOfType(Type::IntTy),
|
|
|
|
SparcIntRegClass::i0);
|
|
|
|
int fpReg = TM.getFrameInfo().getIncomingArgBaseRegNum();
|
|
|
|
int argSize = TM.getFrameInfo().getSizeOfEachArgOnStack();
|
|
|
|
int firstArgOffset=TM.getFrameInfo().getFirstIncomingArgOffset(MF,ignore);
|
|
|
|
int nextArgOffset = firstArgOffset + numFixedArgs * argSize;
|
|
|
|
|
|
|
|
for (int i=numFixedArgs; i < numArgRegs; ++i) {
|
2003-05-27 22:35:43 +00:00
|
|
|
mvec.push_back(BuildMI(V9::STXi, 3).addMReg(firstArgReg+i).
|
2003-05-25 15:59:47 +00:00
|
|
|
addMReg(fpReg).addSImm(nextArgOffset));
|
|
|
|
nextArgOffset += argSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-28 20:16:08 +00:00
|
|
|
MF.front().insert(MF.front().begin(), mvec.begin(), mvec.end());
|
2002-04-09 05:18:31 +00:00
|
|
|
}
|
|
|
|
|
2002-12-28 20:16:08 +00:00
|
|
|
void InsertPrologEpilogCode::InsertEpilogCode(MachineFunction &MF)
|
2002-04-09 05:18:31 +00:00
|
|
|
{
|
2002-12-28 20:16:08 +00:00
|
|
|
const TargetMachine &TM = MF.getTarget();
|
2003-01-14 22:00:31 +00:00
|
|
|
const TargetInstrInfo &MII = TM.getInstrInfo();
|
2002-12-28 20:16:08 +00:00
|
|
|
|
2002-10-28 20:01:13 +00:00
|
|
|
for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
|
|
|
|
MachineBasicBlock &MBB = *I;
|
2003-07-26 23:04:00 +00:00
|
|
|
const BasicBlock &BB = *I->getBasicBlock();
|
|
|
|
const Instruction *TermInst = (Instruction*)BB.getTerminator();
|
2002-04-09 05:18:31 +00:00
|
|
|
if (TermInst->getOpcode() == Instruction::Ret)
|
2003-05-20 20:32:24 +00:00
|
|
|
{
|
|
|
|
int ZR = TM.getRegInfo().getZeroRegNum();
|
|
|
|
MachineInstr *Restore =
|
2003-05-27 22:35:43 +00:00
|
|
|
BuildMI(V9::RESTOREi, 3).addMReg(ZR).addSImm(0).addMReg(ZR, MOTy::Def);
|
2003-05-20 20:32:24 +00:00
|
|
|
|
|
|
|
MachineCodeForInstruction &termMvec =
|
|
|
|
MachineCodeForInstruction::get(TermInst);
|
|
|
|
|
|
|
|
// Remove the NOPs in the delay slots of the return instruction
|
|
|
|
unsigned numNOPs = 0;
|
|
|
|
while (termMvec.back()->getOpCode() == V9::NOP)
|
2002-04-09 05:18:31 +00:00
|
|
|
{
|
2003-05-20 20:32:24 +00:00
|
|
|
assert( termMvec.back() == MBB.back());
|
|
|
|
delete MBB.pop_back();
|
|
|
|
termMvec.pop_back();
|
|
|
|
++numNOPs;
|
|
|
|
}
|
|
|
|
assert(termMvec.back() == MBB.back());
|
2002-04-09 05:18:31 +00:00
|
|
|
|
2003-05-20 20:32:24 +00:00
|
|
|
// Check that we found the right number of NOPs and have the right
|
|
|
|
// number of instructions to replace them.
|
|
|
|
unsigned ndelays = MII.getNumDelaySlots(termMvec.back()->getOpCode());
|
|
|
|
assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
|
|
|
|
assert(ndelays == 1 && "Cannot use epilog code for delay slots?");
|
2002-04-09 05:18:31 +00:00
|
|
|
|
2003-05-20 20:32:24 +00:00
|
|
|
// Append the epilog code to the end of the basic block.
|
|
|
|
MBB.push_back(Restore);
|
|
|
|
}
|
2002-04-09 05:18:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-16 15:39:26 +00:00
|
|
|
Pass* UltraSparc::getPrologEpilogInsertionPass() {
|
2002-12-28 20:16:08 +00:00
|
|
|
return new InsertPrologEpilogCode();
|
2002-04-09 05:18:31 +00:00
|
|
|
}
|