2004-04-25 09:04:49 +02:00
|
|
|
//===-- SparcV9PrologEpilogCodeInserter.cpp - Insert Fn Prolog & Epilog ---===//
|
2003-10-20 21:43:21 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-04-09 07:18:31 +02:00
|
|
|
//
|
2004-08-16 20:27:25 +02:00
|
|
|
// This is the SparcV9 target's own PrologEpilogInserter. It creates prolog and
|
|
|
|
// epilog instructions for functions which have not been compiled using "leaf
|
|
|
|
// function optimizations". These instructions include the SAVE and RESTORE
|
|
|
|
// instructions used to rotate the SPARC register windows. Prologs are
|
|
|
|
// attached to the unique function entry, and epilogs are attached to each
|
|
|
|
// function exit.
|
2002-04-09 07:18:31 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-02-25 19:44:15 +01:00
|
|
|
#include "SparcV9Internals.h"
|
|
|
|
#include "SparcV9RegClassInfo.h"
|
2004-08-12 19:56:01 +02:00
|
|
|
#include "SparcV9RegisterInfo.h"
|
|
|
|
#include "SparcV9FrameInfo.h"
|
2004-08-16 23:55:02 +02:00
|
|
|
#include "MachineFunctionInfo.h"
|
|
|
|
#include "MachineCodeForInstruction.h"
|
2002-12-28 21:16:08 +01:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2002-10-28 22:43:57 +01:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2002-04-09 07:18:31 +02:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Function.h"
|
2003-05-25 17:59:47 +02:00
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Intrinsics.h"
|
2002-04-09 07:18:31 +02:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
namespace llvm {
|
|
|
|
|
2002-04-09 07:18:31 +02:00
|
|
|
namespace {
|
2002-12-28 21:16:08 +01:00
|
|
|
struct InsertPrologEpilogCode : public MachineFunctionPass {
|
2004-02-25 19:44:15 +01:00
|
|
|
const char *getPassName() const { return "SparcV9 Prolog/Epilog Inserter"; }
|
2002-06-25 18:13:21 +02:00
|
|
|
|
2002-12-28 21:16:08 +01:00
|
|
|
bool runOnMachineFunction(MachineFunction &F) {
|
2004-08-18 20:13:37 +02:00
|
|
|
if (!F.getInfo<SparcV9FunctionInfo>()->isCompiledAsLeafMethod()) {
|
2002-06-25 18:13:21 +02:00
|
|
|
InsertPrologCode(F);
|
|
|
|
InsertEpilogCode(F);
|
|
|
|
}
|
|
|
|
return false;
|
2002-04-09 07:18:31 +02:00
|
|
|
}
|
2002-06-25 18:13:21 +02:00
|
|
|
|
2002-12-28 21:16:08 +01:00
|
|
|
void InsertPrologCode(MachineFunction &F);
|
|
|
|
void InsertEpilogCode(MachineFunction &F);
|
2002-06-25 18:13:21 +02:00
|
|
|
};
|
2002-04-09 07:18:31 +02:00
|
|
|
|
|
|
|
} // End anonymous namespace
|
|
|
|
|
2004-03-31 22:58:37 +02:00
|
|
|
static unsigned getStaticStackSize (MachineFunction &MF) {
|
2004-06-02 07:55:25 +02:00
|
|
|
const TargetFrameInfo& frameInfo = *MF.getTarget().getFrameInfo();
|
2004-08-18 20:13:37 +02:00
|
|
|
unsigned staticStackSize = MF.getInfo<SparcV9FunctionInfo>()->getStaticStackSize();
|
2004-08-12 20:36:28 +02:00
|
|
|
if (staticStackSize < (unsigned)SparcV9FrameInfo::MinStackFrameSize)
|
2004-08-12 20:29:05 +02:00
|
|
|
staticStackSize = SparcV9FrameInfo::MinStackFrameSize;
|
2004-08-12 20:20:55 +02:00
|
|
|
if (unsigned padsz = staticStackSize %
|
|
|
|
SparcV9FrameInfo::StackFrameSizeAlignment)
|
|
|
|
staticStackSize += SparcV9FrameInfo::StackFrameSizeAlignment - padsz;
|
2004-03-31 22:58:37 +02:00
|
|
|
return staticStackSize;
|
|
|
|
}
|
|
|
|
|
2002-12-28 21:16:08 +01:00
|
|
|
void InsertPrologEpilogCode::InsertPrologCode(MachineFunction &MF)
|
2002-04-09 07:18:31 +02:00
|
|
|
{
|
2002-06-25 22:55:50 +02:00
|
|
|
std::vector<MachineInstr*> mvec;
|
2002-12-28 21:16:08 +01:00
|
|
|
const TargetMachine &TM = MF.getTarget();
|
2004-06-02 07:55:25 +02:00
|
|
|
const TargetFrameInfo& frameInfo = *TM.getFrameInfo();
|
2002-04-09 07:18:31 +02: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 02:24:06 +02:00
|
|
|
// See the comments below for the choice of this register.
|
2004-03-31 22:58:37 +02:00
|
|
|
unsigned staticStackSize = getStaticStackSize (MF);
|
2002-10-28 22:43:57 +01:00
|
|
|
int32_t C = - (int) staticStackSize;
|
2004-06-02 07:55:25 +02:00
|
|
|
int SP = TM.getRegInfo()->getStackPointer();
|
|
|
|
if (TM.getInstrInfo()->constantFitsInImmedField(V9::SAVEi,staticStackSize)) {
|
2003-05-28 00:35:43 +02:00
|
|
|
mvec.push_back(BuildMI(V9::SAVEi, 3).addMReg(SP).addSImm(C)
|
2004-02-22 20:23:26 +01:00
|
|
|
.addMReg(SP, MachineOperand::Def));
|
2002-10-28 22:43:57 +01:00
|
|
|
} else {
|
2002-12-28 21:16:08 +01: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
|
2004-06-02 07:55:25 +02:00
|
|
|
int uregNum = TM.getRegInfo()->getUnifiedRegNum(
|
|
|
|
TM.getRegInfo()->getRegClassIDOfType(Type::IntTy),
|
2004-02-25 19:44:15 +01:00
|
|
|
SparcV9IntRegClass::g1);
|
2002-12-28 21:16:08 +01:00
|
|
|
|
2003-05-20 22:32:24 +02:00
|
|
|
MachineInstr* M = BuildMI(V9::SETHI, 2).addSImm(C)
|
2004-02-22 20:23:26 +01:00
|
|
|
.addMReg(uregNum, MachineOperand::Def);
|
2004-07-19 09:52:35 +02:00
|
|
|
M->getOperand(0).markHi32();
|
2002-12-28 21:16:08 +01:00
|
|
|
mvec.push_back(M);
|
|
|
|
|
2003-05-28 00:35:43 +02:00
|
|
|
M = BuildMI(V9::ORi, 3).addMReg(uregNum).addSImm(C)
|
2004-02-22 20:23:26 +01:00
|
|
|
.addMReg(uregNum, MachineOperand::Def);
|
2004-07-19 09:52:35 +02:00
|
|
|
M->getOperand(1).markLo32();
|
2002-12-28 21:16:08 +01:00
|
|
|
mvec.push_back(M);
|
|
|
|
|
2003-06-06 11:52:23 +02:00
|
|
|
M = BuildMI(V9::SRAi5, 3).addMReg(uregNum).addZImm(0)
|
2004-02-22 20:23:26 +01:00
|
|
|
.addMReg(uregNum, MachineOperand::Def);
|
2002-12-28 21:16:08 +01:00
|
|
|
mvec.push_back(M);
|
|
|
|
|
|
|
|
// Now generate the SAVE using the value in register %g1
|
2004-02-22 20:23:26 +01:00
|
|
|
M = BuildMI(V9::SAVEr,3).addMReg(SP).addMReg(uregNum)
|
|
|
|
.addMReg(SP,MachineOperand::Def);
|
2002-12-28 21:16:08 +01:00
|
|
|
mvec.push_back(M);
|
|
|
|
}
|
2002-04-09 07:18:31 +02:00
|
|
|
|
2003-05-25 17:59:47 +02: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();
|
2004-06-02 07:55:25 +02:00
|
|
|
int numArgRegs = TM.getRegInfo()->getNumOfIntArgRegs();
|
2003-05-25 17:59:47 +02:00
|
|
|
if (numFixedArgs < numArgRegs) {
|
2004-06-02 07:55:25 +02:00
|
|
|
const TargetFrameInfo &FI = *TM.getFrameInfo();
|
|
|
|
int firstArgReg = TM.getRegInfo()->getUnifiedRegNum(
|
|
|
|
TM.getRegInfo()->getRegClassIDOfType(Type::IntTy),
|
2004-02-25 19:44:15 +01:00
|
|
|
SparcV9IntRegClass::i0);
|
2004-08-12 19:56:01 +02:00
|
|
|
int fpReg = SparcV9::i6;
|
|
|
|
int argSize = 8;
|
|
|
|
int firstArgOffset= SparcV9FrameInfo::FirstIncomingArgOffsetFromFP;
|
2003-05-25 17:59:47 +02:00
|
|
|
int nextArgOffset = firstArgOffset + numFixedArgs * argSize;
|
|
|
|
|
|
|
|
for (int i=numFixedArgs; i < numArgRegs; ++i) {
|
2003-05-28 00:35:43 +02:00
|
|
|
mvec.push_back(BuildMI(V9::STXi, 3).addMReg(firstArgReg+i).
|
2003-05-25 17:59:47 +02:00
|
|
|
addMReg(fpReg).addSImm(nextArgOffset));
|
|
|
|
nextArgOffset += argSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-28 21:16:08 +01:00
|
|
|
MF.front().insert(MF.front().begin(), mvec.begin(), mvec.end());
|
2002-04-09 07:18:31 +02:00
|
|
|
}
|
|
|
|
|
2002-12-28 21:16:08 +01:00
|
|
|
void InsertPrologEpilogCode::InsertEpilogCode(MachineFunction &MF)
|
2002-04-09 07:18:31 +02:00
|
|
|
{
|
2002-12-28 21:16:08 +01:00
|
|
|
const TargetMachine &TM = MF.getTarget();
|
2004-06-02 07:55:25 +02:00
|
|
|
const TargetInstrInfo &MII = *TM.getInstrInfo();
|
2002-12-28 21:16:08 +01:00
|
|
|
|
2002-10-28 21:01:13 +01:00
|
|
|
for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
|
|
|
|
MachineBasicBlock &MBB = *I;
|
2003-07-27 01:04:00 +02:00
|
|
|
const BasicBlock &BB = *I->getBasicBlock();
|
|
|
|
const Instruction *TermInst = (Instruction*)BB.getTerminator();
|
2002-04-09 07:18:31 +02:00
|
|
|
if (TermInst->getOpcode() == Instruction::Ret)
|
2003-05-20 22:32:24 +02:00
|
|
|
{
|
2004-06-02 07:55:25 +02:00
|
|
|
int ZR = TM.getRegInfo()->getZeroRegNum();
|
2003-05-20 22:32:24 +02:00
|
|
|
MachineInstr *Restore =
|
2004-02-22 20:23:26 +01:00
|
|
|
BuildMI(V9::RESTOREi, 3).addMReg(ZR).addSImm(0)
|
|
|
|
.addMReg(ZR, MachineOperand::Def);
|
2003-05-20 22:32:24 +02:00
|
|
|
|
|
|
|
MachineCodeForInstruction &termMvec =
|
|
|
|
MachineCodeForInstruction::get(TermInst);
|
|
|
|
|
|
|
|
// Remove the NOPs in the delay slots of the return instruction
|
|
|
|
unsigned numNOPs = 0;
|
2004-02-11 21:47:34 +01:00
|
|
|
while (termMvec.back()->getOpcode() == V9::NOP)
|
2002-04-09 07:18:31 +02:00
|
|
|
{
|
2004-02-12 03:27:10 +01:00
|
|
|
assert( termMvec.back() == &MBB.back());
|
2003-05-20 22:32:24 +02:00
|
|
|
termMvec.pop_back();
|
2004-02-12 03:27:10 +01:00
|
|
|
MBB.erase(&MBB.back());
|
2003-05-20 22:32:24 +02:00
|
|
|
++numNOPs;
|
|
|
|
}
|
2004-02-12 03:27:10 +01:00
|
|
|
assert(termMvec.back() == &MBB.back());
|
2002-04-09 07:18:31 +02:00
|
|
|
|
2003-05-20 22:32:24 +02:00
|
|
|
// Check that we found the right number of NOPs and have the right
|
|
|
|
// number of instructions to replace them.
|
2004-02-11 21:47:34 +01:00
|
|
|
unsigned ndelays = MII.getNumDelaySlots(termMvec.back()->getOpcode());
|
2003-05-20 22:32:24 +02:00
|
|
|
assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
|
|
|
|
assert(ndelays == 1 && "Cannot use epilog code for delay slots?");
|
2002-04-09 07:18:31 +02:00
|
|
|
|
2003-05-20 22:32:24 +02:00
|
|
|
// Append the epilog code to the end of the basic block.
|
|
|
|
MBB.push_back(Restore);
|
|
|
|
}
|
2002-04-09 07:18:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-13 01:19:02 +01:00
|
|
|
FunctionPass *createPrologEpilogInsertionPass() {
|
2002-12-28 21:16:08 +01:00
|
|
|
return new InsertPrologEpilogCode();
|
2002-04-09 07:18:31 +02:00
|
|
|
}
|
2003-11-11 23:41:34 +01:00
|
|
|
|
|
|
|
} // End llvm namespace
|