2002-12-28 20:43:30 +00:00
|
|
|
//===-- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function --===//
|
2003-10-20 19:43:21 +00: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-12-28 20:43:30 +00:00
|
|
|
//
|
|
|
|
// This pass is responsible for finalizing the functions frame layout, saving
|
|
|
|
// callee saved registers, and for emitting prolog & epilog code for the
|
|
|
|
// function.
|
|
|
|
//
|
|
|
|
// This pass must be run after register allocation. After this pass is
|
|
|
|
// executed, it is illegal to construct MO_FrameIndex operands.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-01-13 00:23:41 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2002-12-28 20:43:30 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2002-12-28 21:08:26 +00:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2002-12-28 20:43:30 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/MRegisterInfo.h"
|
2002-12-28 21:00:25 +00:00
|
|
|
#include "llvm/Target/TargetFrameInfo.h"
|
2003-01-14 22:00:31 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2004-02-15 00:14:20 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-12-28 20:43:30 +00:00
|
|
|
namespace {
|
2003-01-13 00:23:41 +00:00
|
|
|
struct PEI : public MachineFunctionPass {
|
|
|
|
const char *getPassName() const {
|
|
|
|
return "Prolog/Epilog Insertion & Frame Finalization";
|
2002-12-28 20:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// runOnMachineFunction - Insert prolog/epilog code and replace abstract
|
|
|
|
/// frame indexes with appropriate references.
|
|
|
|
///
|
|
|
|
bool runOnMachineFunction(MachineFunction &Fn) {
|
|
|
|
// Scan the function for modified caller saved registers and insert spill
|
|
|
|
// code for any caller saved registers that are modified. Also calculate
|
|
|
|
// the MaxCallFrameSize and HasCalls variables for the function's frame
|
|
|
|
// information and eliminates call frame pseudo instructions.
|
2004-08-12 19:01:14 +00:00
|
|
|
calculateCallerSavedRegisters(Fn);
|
|
|
|
|
|
|
|
// Add the code to save and restore the caller saved registers
|
2002-12-28 20:43:30 +00:00
|
|
|
saveCallerSavedRegisters(Fn);
|
|
|
|
|
|
|
|
// Allow the target machine to make final modifications to the function
|
|
|
|
// before the frame layout is finalized.
|
|
|
|
Fn.getTarget().getRegisterInfo()->processFunctionBeforeFrameFinalized(Fn);
|
|
|
|
|
|
|
|
// Calculate actual frame offsets for all of the abstract stack objects...
|
|
|
|
calculateFrameObjectOffsets(Fn);
|
|
|
|
|
2004-08-12 19:01:14 +00:00
|
|
|
// Add prolog and epilog code to the function. This function is required
|
|
|
|
// to align the stack frame as necessary for any stack variables or
|
|
|
|
// called functions. Because of this, calculateCallerSavedRegisters
|
|
|
|
// must be called before this function in order to set the HasCalls
|
|
|
|
// and MaxCallFrameSize variables.
|
2003-01-15 22:52:34 +00:00
|
|
|
insertPrologEpilogCode(Fn);
|
|
|
|
|
2002-12-28 20:43:30 +00:00
|
|
|
// Replace all MO_FrameIndex operands with physical register references
|
|
|
|
// and actual offsets.
|
|
|
|
//
|
|
|
|
replaceFrameIndices(Fn);
|
2004-08-12 19:01:14 +00:00
|
|
|
|
|
|
|
RegsToSave.clear();
|
|
|
|
StackSlots.clear();
|
2002-12-28 20:43:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2004-08-12 19:01:14 +00:00
|
|
|
std::vector<unsigned> RegsToSave;
|
|
|
|
std::vector<int> StackSlots;
|
|
|
|
|
|
|
|
void calculateCallerSavedRegisters(MachineFunction &Fn);
|
2002-12-28 20:43:30 +00:00
|
|
|
void saveCallerSavedRegisters(MachineFunction &Fn);
|
|
|
|
void calculateFrameObjectOffsets(MachineFunction &Fn);
|
|
|
|
void replaceFrameIndices(MachineFunction &Fn);
|
|
|
|
void insertPrologEpilogCode(MachineFunction &Fn);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-12-28 20:43:30 +00:00
|
|
|
/// createPrologEpilogCodeInserter - This function returns a pass that inserts
|
|
|
|
/// prolog and epilog code, and eliminates abstract frame references.
|
|
|
|
///
|
2004-02-15 00:14:20 +00:00
|
|
|
FunctionPass *llvm::createPrologEpilogCodeInserter() { return new PEI(); }
|
2002-12-28 20:43:30 +00:00
|
|
|
|
|
|
|
|
2004-08-12 19:01:14 +00:00
|
|
|
/// calculateCallerSavedRegisters - Scan the function for modified caller saved
|
|
|
|
/// registers. Also calculate the MaxCallFrameSize and HasCalls variables for
|
2002-12-28 20:43:30 +00:00
|
|
|
/// the function's frame information and eliminates call frame pseudo
|
|
|
|
/// instructions.
|
|
|
|
///
|
2004-08-12 19:01:14 +00:00
|
|
|
void PEI::calculateCallerSavedRegisters(MachineFunction &Fn) {
|
2002-12-28 20:43:30 +00:00
|
|
|
const MRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
|
2004-08-12 19:01:14 +00:00
|
|
|
const TargetFrameInfo *TFI = Fn.getTarget().getFrameInfo();
|
2005-01-22 00:49:16 +00:00
|
|
|
const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
|
2002-12-28 20:43:30 +00:00
|
|
|
|
|
|
|
// Get the callee saved register list...
|
|
|
|
const unsigned *CSRegs = RegInfo->getCalleeSaveRegs();
|
|
|
|
|
|
|
|
// Get the function call frame set-up and tear-down instruction opcode
|
|
|
|
int FrameSetupOpcode = RegInfo->getCallFrameSetupOpcode();
|
|
|
|
int FrameDestroyOpcode = RegInfo->getCallFrameDestroyOpcode();
|
|
|
|
|
|
|
|
// Early exit for targets which have no callee saved registers and no call
|
|
|
|
// frame setup/destroy pseudo instructions.
|
|
|
|
if ((CSRegs == 0 || CSRegs[0] == 0) &&
|
|
|
|
FrameSetupOpcode == -1 && FrameDestroyOpcode == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned MaxCallFrameSize = 0;
|
|
|
|
bool HasCalls = false;
|
|
|
|
|
|
|
|
for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
|
|
|
|
for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); )
|
2004-02-12 02:27:10 +00:00
|
|
|
if (I->getOpcode() == FrameSetupOpcode ||
|
2004-08-07 07:07:57 +00:00
|
|
|
I->getOpcode() == FrameDestroyOpcode) {
|
|
|
|
assert(I->getNumOperands() == 1 && "Call Frame Setup/Destroy Pseudo"
|
|
|
|
" instructions should have a single immediate argument!");
|
|
|
|
unsigned Size = I->getOperand(0).getImmedValue();
|
|
|
|
if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
|
|
|
|
HasCalls = true;
|
|
|
|
RegInfo->eliminateCallFramePseudoInstr(Fn, *BB, I++);
|
2002-12-28 20:43:30 +00:00
|
|
|
} else {
|
2004-08-07 07:07:57 +00:00
|
|
|
++I;
|
2002-12-28 20:43:30 +00:00
|
|
|
}
|
|
|
|
|
2002-12-28 21:08:26 +00:00
|
|
|
MachineFrameInfo *FFI = Fn.getFrameInfo();
|
2002-12-28 20:43:30 +00:00
|
|
|
FFI->setHasCalls(HasCalls);
|
|
|
|
FFI->setMaxCallFrameSize(MaxCallFrameSize);
|
|
|
|
|
|
|
|
// Now figure out which *callee saved* registers are modified by the current
|
|
|
|
// function, thus needing to be saved and restored in the prolog/epilog.
|
|
|
|
//
|
2005-01-23 23:13:12 +00:00
|
|
|
const bool *PhysRegsUsed = Fn.getUsedPhysregs();
|
2002-12-28 20:43:30 +00:00
|
|
|
for (unsigned i = 0; CSRegs[i]; ++i) {
|
|
|
|
unsigned Reg = CSRegs[i];
|
2005-01-23 23:13:12 +00:00
|
|
|
if (PhysRegsUsed[Reg]) {
|
|
|
|
RegsToSave.push_back(Reg); // If the reg is modified, save it!
|
2003-10-08 05:20:08 +00:00
|
|
|
} else {
|
|
|
|
for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
|
2005-01-23 23:13:12 +00:00
|
|
|
*AliasSet; ++AliasSet) { // Check alias registers too.
|
|
|
|
if (PhysRegsUsed[*AliasSet]) {
|
2004-08-07 07:07:57 +00:00
|
|
|
RegsToSave.push_back(Reg);
|
|
|
|
break;
|
2004-08-07 07:18:41 +00:00
|
|
|
}
|
2003-10-08 05:20:08 +00:00
|
|
|
}
|
|
|
|
}
|
2002-12-28 20:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (RegsToSave.empty())
|
|
|
|
return; // Early exit if no caller saved registers are modified!
|
|
|
|
|
2004-08-12 19:01:14 +00:00
|
|
|
unsigned NumFixedSpillSlots;
|
2004-08-15 09:18:55 +00:00
|
|
|
const std::pair<unsigned,int> *FixedSpillSlots =
|
2004-08-12 19:01:14 +00:00
|
|
|
TFI->getCalleeSaveSpillSlots(NumFixedSpillSlots);
|
|
|
|
|
2002-12-28 20:43:30 +00:00
|
|
|
// Now that we know which registers need to be saved and restored, allocate
|
|
|
|
// stack slots for them.
|
|
|
|
for (unsigned i = 0, e = RegsToSave.size(); i != e; ++i) {
|
2004-08-12 19:01:14 +00:00
|
|
|
unsigned Reg = RegsToSave[i];
|
|
|
|
|
|
|
|
// Check to see if this physreg must be spilled to a particular stack slot
|
|
|
|
// on this target.
|
2004-08-15 09:18:55 +00:00
|
|
|
const std::pair<unsigned,int> *FixedSlot = FixedSpillSlots;
|
2004-08-12 19:01:14 +00:00
|
|
|
while (FixedSlot != FixedSpillSlots+NumFixedSpillSlots &&
|
|
|
|
FixedSlot->first != Reg)
|
|
|
|
++FixedSlot;
|
|
|
|
|
2004-08-21 19:45:10 +00:00
|
|
|
int FrameIdx;
|
2004-08-12 19:01:14 +00:00
|
|
|
if (FixedSlot == FixedSpillSlots+NumFixedSpillSlots) {
|
|
|
|
// Nope, just spill it anywhere convenient.
|
2004-08-29 22:00:24 +00:00
|
|
|
FrameIdx = FFI->CreateStackObject(RegInfo->getSpillSize(Reg)/8,
|
2004-08-21 20:04:59 +00:00
|
|
|
RegInfo->getSpillAlignment(Reg)/8);
|
2004-08-12 19:01:14 +00:00
|
|
|
} else {
|
|
|
|
// Spill it to the stack where we must.
|
2004-08-29 22:00:24 +00:00
|
|
|
FrameIdx = FFI->CreateFixedObject(RegInfo->getSpillSize(Reg)/8,
|
2004-08-21 19:45:10 +00:00
|
|
|
FixedSlot->second);
|
2004-08-12 19:01:14 +00:00
|
|
|
}
|
2002-12-28 20:43:30 +00:00
|
|
|
StackSlots.push_back(FrameIdx);
|
|
|
|
}
|
2004-08-12 19:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// saveCallerSavedRegisters - Insert spill code for any caller saved registers
|
|
|
|
/// that are modified in the function.
|
|
|
|
///
|
|
|
|
void PEI::saveCallerSavedRegisters(MachineFunction &Fn) {
|
|
|
|
// Early exit if no caller saved registers are modified!
|
|
|
|
if (RegsToSave.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const MRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
|
2002-12-28 20:43:30 +00:00
|
|
|
|
|
|
|
// Now that we have a stack slot for each register to be saved, insert spill
|
2005-01-23 21:45:01 +00:00
|
|
|
// code into the entry block.
|
2002-12-28 20:43:30 +00:00
|
|
|
MachineBasicBlock *MBB = Fn.begin();
|
|
|
|
MachineBasicBlock::iterator I = MBB->begin();
|
|
|
|
for (unsigned i = 0, e = RegsToSave.size(); i != e; ++i) {
|
2004-08-15 21:56:44 +00:00
|
|
|
// Insert the spill to the stack frame.
|
|
|
|
RegInfo->storeRegToStackSlot(*MBB, I, RegsToSave[i], StackSlots[i]);
|
2002-12-28 20:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add code to restore the callee-save registers in each exiting block.
|
2004-06-02 05:57:12 +00:00
|
|
|
const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
|
2005-01-23 21:45:01 +00:00
|
|
|
for (MachineFunction::iterator FI = Fn.begin(), E = Fn.end(); FI != E; ++FI)
|
|
|
|
// If last instruction is a return instruction, add an epilogue.
|
2004-02-12 02:27:10 +00:00
|
|
|
if (!FI->empty() && TII.isReturn(FI->back().getOpcode())) {
|
|
|
|
MBB = FI;
|
|
|
|
I = MBB->end(); --I;
|
2002-12-28 20:43:30 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = RegsToSave.size(); i != e; ++i) {
|
2005-01-19 21:32:07 +00:00
|
|
|
RegInfo->loadRegFromStackSlot(*MBB, I, RegsToSave[i], StackSlots[i]);
|
|
|
|
assert(I != MBB->begin() &&
|
|
|
|
"loadRegFromStackSlot didn't insert any code!");
|
2004-08-07 07:18:41 +00:00
|
|
|
--I; // Insert in reverse order
|
2002-12-28 20:43:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
|
2005-01-23 21:45:01 +00:00
|
|
|
/// abstract stack objects.
|
2002-12-28 20:43:30 +00:00
|
|
|
///
|
|
|
|
void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) {
|
2004-06-02 05:57:12 +00:00
|
|
|
const TargetFrameInfo &TFI = *Fn.getTarget().getFrameInfo();
|
2002-12-28 20:43:30 +00:00
|
|
|
|
|
|
|
bool StackGrowsDown =
|
|
|
|
TFI.getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
|
|
|
|
|
|
|
|
// Loop over all of the stack objects, assigning sequential addresses...
|
2002-12-28 21:08:26 +00:00
|
|
|
MachineFrameInfo *FFI = Fn.getFrameInfo();
|
2002-12-28 20:43:30 +00:00
|
|
|
|
2003-01-16 02:24:20 +00:00
|
|
|
unsigned StackAlignment = TFI.getStackAlignment();
|
2003-01-16 02:22:08 +00:00
|
|
|
|
2004-02-15 00:14:20 +00:00
|
|
|
// Start at the beginning of the local area.
|
2004-06-10 06:23:35 +00:00
|
|
|
// The Offset is the distance from the stack top in the direction
|
|
|
|
// of stack growth -- so it's always positive.
|
2003-01-15 22:52:34 +00:00
|
|
|
int Offset = TFI.getOffsetOfLocalArea();
|
2004-06-10 06:23:35 +00:00
|
|
|
if (StackGrowsDown)
|
2004-06-11 06:37:11 +00:00
|
|
|
Offset = -Offset;
|
2004-06-10 06:23:35 +00:00
|
|
|
assert(Offset >= 0
|
|
|
|
&& "Local area offset should be in direction of stack growth");
|
|
|
|
|
|
|
|
// If there are fixed sized objects that are preallocated in the local area,
|
|
|
|
// non-fixed objects can't be allocated right at the start of local area.
|
|
|
|
// We currently don't support filling in holes in between fixed sized objects,
|
|
|
|
// so we adjust 'Offset' to point to the end of last fixed sized
|
2004-02-15 00:14:20 +00:00
|
|
|
// preallocated object.
|
|
|
|
for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
|
2004-06-10 06:23:35 +00:00
|
|
|
int FixedOff;
|
|
|
|
if (StackGrowsDown) {
|
|
|
|
// The maximum distance from the stack pointer is at lower address of
|
|
|
|
// the object -- which is given by offset. For down growing stack
|
|
|
|
// the offset is negative, so we negate the offset to get the distance.
|
|
|
|
FixedOff = -FFI->getObjectOffset(i);
|
|
|
|
} else {
|
|
|
|
// The maximum distance from the start pointer is at the upper
|
|
|
|
// address of the object.
|
|
|
|
FixedOff = FFI->getObjectOffset(i) + FFI->getObjectSize(i);
|
|
|
|
}
|
|
|
|
if (FixedOff > Offset) Offset = FixedOff;
|
2004-02-15 00:14:20 +00:00
|
|
|
}
|
|
|
|
|
2002-12-28 20:43:30 +00:00
|
|
|
for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
|
2004-06-10 06:23:35 +00:00
|
|
|
// If stack grows down, we need to add size of find the lowest
|
|
|
|
// address of the object.
|
|
|
|
if (StackGrowsDown)
|
|
|
|
Offset += FFI->getObjectSize(i);
|
2002-12-28 20:43:30 +00:00
|
|
|
|
|
|
|
unsigned Align = FFI->getObjectAlignment(i);
|
2003-01-16 02:24:20 +00:00
|
|
|
assert(Align <= StackAlignment && "Cannot align stack object to higher "
|
2003-01-16 02:22:08 +00:00
|
|
|
"alignment boundary than the stack itself!");
|
2002-12-28 20:43:30 +00:00
|
|
|
Offset = (Offset+Align-1)/Align*Align; // Adjust to Alignment boundary...
|
|
|
|
|
2004-06-10 06:23:35 +00:00
|
|
|
if (StackGrowsDown) {
|
|
|
|
FFI->setObjectOffset(i, -Offset); // Set the computed offset
|
|
|
|
} else {
|
|
|
|
FFI->setObjectOffset(i, Offset);
|
|
|
|
Offset += FFI->getObjectSize(i);
|
|
|
|
}
|
2002-12-28 20:43:30 +00:00
|
|
|
}
|
|
|
|
|
2004-02-14 20:10:59 +00:00
|
|
|
// Align the final stack pointer offset, but only if there are calls in the
|
|
|
|
// function. This ensures that any calls to subroutines have their stack
|
|
|
|
// frames suitable aligned.
|
|
|
|
if (FFI->hasCalls())
|
|
|
|
Offset = (Offset+StackAlignment-1)/StackAlignment*StackAlignment;
|
2002-12-28 20:43:30 +00:00
|
|
|
|
|
|
|
// Set the final value of the stack pointer...
|
2004-06-11 06:37:11 +00:00
|
|
|
FFI->setStackSize(Offset+TFI.getOffsetOfLocalArea());
|
2003-01-15 22:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// insertPrologEpilogCode - Scan the function for modified caller saved
|
|
|
|
/// registers, insert spill code for these caller saved registers, then add
|
|
|
|
/// prolog and epilog code to the function.
|
|
|
|
///
|
|
|
|
void PEI::insertPrologEpilogCode(MachineFunction &Fn) {
|
|
|
|
// Add prologue to the function...
|
|
|
|
Fn.getTarget().getRegisterInfo()->emitPrologue(Fn);
|
|
|
|
|
|
|
|
// Add epilogue to restore the callee-save registers in each exiting block
|
2004-06-02 05:57:12 +00:00
|
|
|
const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
|
2003-01-15 22:52:34 +00:00
|
|
|
for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
|
|
|
|
// If last instruction is a return instruction, add an epilogue
|
2004-02-12 02:27:10 +00:00
|
|
|
if (!I->empty() && TII.isReturn(I->back().getOpcode()))
|
2003-01-15 22:52:34 +00:00
|
|
|
Fn.getTarget().getRegisterInfo()->emitEpilogue(Fn, *I);
|
|
|
|
}
|
2002-12-28 20:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
|
|
|
|
/// register references and actual offsets.
|
|
|
|
///
|
|
|
|
void PEI::replaceFrameIndices(MachineFunction &Fn) {
|
|
|
|
if (!Fn.getFrameInfo()->hasStackObjects()) return; // Nothing to do?
|
|
|
|
|
|
|
|
const TargetMachine &TM = Fn.getTarget();
|
|
|
|
assert(TM.getRegisterInfo() && "TM::getRegisterInfo() must be implemented!");
|
|
|
|
const MRegisterInfo &MRI = *TM.getRegisterInfo();
|
|
|
|
|
|
|
|
for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
|
|
|
|
for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
|
2004-02-12 02:27:10 +00:00
|
|
|
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
|
2004-08-07 07:18:41 +00:00
|
|
|
if (I->getOperand(i).isFrameIndex()) {
|
|
|
|
// If this instruction has a FrameIndex operand, we need to use that
|
|
|
|
// target machine register info object to eliminate it.
|
2004-08-14 22:00:10 +00:00
|
|
|
MRI.eliminateFrameIndex(I);
|
2004-08-07 07:18:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-12-28 20:43:30 +00:00
|
|
|
}
|