2002-12-28 21:43:30 +01:00
|
|
|
//===-- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function --===//
|
2005-04-22 00:36:52 +02:00
|
|
|
//
|
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.
|
2005-04-22 00:36:52 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-28 21:43:30 +01: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 01:23:41 +01:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2002-12-28 21:43:30 +01:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2002-12-28 22:08:26 +01:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2002-12-28 21:43:30 +01:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/MRegisterInfo.h"
|
2002-12-28 22:00:25 +01:00
|
|
|
#include "llvm/Target/TargetFrameInfo.h"
|
2003-01-14 23:00:31 +01:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2006-08-27 14:54:02 +02:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2006-09-28 02:10:27 +02:00
|
|
|
#include <climits>
|
2004-02-15 01:14:20 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2002-12-28 21:43:30 +01:00
|
|
|
namespace {
|
2006-06-29 00:17:39 +02:00
|
|
|
struct VISIBILITY_HIDDEN PEI : public MachineFunctionPass {
|
2003-01-13 01:23:41 +01:00
|
|
|
const char *getPassName() const {
|
|
|
|
return "Prolog/Epilog Insertion & Frame Finalization";
|
2002-12-28 21:43:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// runOnMachineFunction - Insert prolog/epilog code and replace abstract
|
|
|
|
/// frame indexes with appropriate references.
|
|
|
|
///
|
|
|
|
bool runOnMachineFunction(MachineFunction &Fn) {
|
2006-04-07 18:34:46 +02:00
|
|
|
// Get MachineDebugInfo so that we can track the construction of the
|
|
|
|
// frame.
|
|
|
|
if (MachineDebugInfo *DI = getAnalysisToUpdate<MachineDebugInfo>()) {
|
|
|
|
Fn.getFrameInfo()->setMachineDebugInfo(DI);
|
|
|
|
}
|
2006-09-28 02:10:27 +02:00
|
|
|
|
|
|
|
// Allow the target machine to make some adjustments to the function
|
|
|
|
// e.g. UsedPhysRegs before calculateCalleeSavedRegisters.
|
|
|
|
Fn.getTarget().getRegisterInfo()->processFunctionBeforeCalleeSaveScan(Fn);
|
|
|
|
|
2006-09-27 00:29:31 +02:00
|
|
|
// Scan the function for modified callee saved registers and insert spill
|
|
|
|
// code for any callee saved registers that are modified. Also calculate
|
2002-12-28 21:43:30 +01:00
|
|
|
// the MaxCallFrameSize and HasCalls variables for the function's frame
|
|
|
|
// information and eliminates call frame pseudo instructions.
|
2006-09-27 00:29:31 +02:00
|
|
|
calculateCalleeSavedRegisters(Fn);
|
2004-08-12 21:01:14 +02:00
|
|
|
|
2006-09-28 02:10:27 +02:00
|
|
|
// Add the code to save and restore the callee saved registers
|
|
|
|
saveCalleeSavedRegisters(Fn);
|
2002-12-28 21:43:30 +01:00
|
|
|
|
|
|
|
// 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 21:01:14 +02: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
|
2006-09-27 00:29:31 +02:00
|
|
|
// called functions. Because of this, calculateCalleeSavedRegisters
|
2004-08-12 21:01:14 +02:00
|
|
|
// must be called before this function in order to set the HasCalls
|
|
|
|
// and MaxCallFrameSize variables.
|
2003-01-15 23:52:34 +01:00
|
|
|
insertPrologEpilogCode(Fn);
|
|
|
|
|
2002-12-28 21:43:30 +01:00
|
|
|
// Replace all MO_FrameIndex operands with physical register references
|
|
|
|
// and actual offsets.
|
|
|
|
//
|
|
|
|
replaceFrameIndices(Fn);
|
2004-08-12 21:01:14 +02:00
|
|
|
|
2002-12-28 21:43:30 +01:00
|
|
|
return true;
|
|
|
|
}
|
2006-08-25 21:45:51 +02:00
|
|
|
|
2002-12-28 21:43:30 +01:00
|
|
|
private:
|
2006-09-28 02:10:27 +02:00
|
|
|
// MinCSFrameIndex, MaxCSFrameIndex - Keeps the range of callee save
|
|
|
|
// stack frame indexes.
|
|
|
|
unsigned MinCSFrameIndex, MaxCSFrameIndex;
|
|
|
|
|
2006-09-27 00:29:31 +02:00
|
|
|
void calculateCalleeSavedRegisters(MachineFunction &Fn);
|
2006-09-28 02:10:27 +02:00
|
|
|
void saveCalleeSavedRegisters(MachineFunction &Fn);
|
2002-12-28 21:43:30 +01:00
|
|
|
void calculateFrameObjectOffsets(MachineFunction &Fn);
|
|
|
|
void replaceFrameIndices(MachineFunction &Fn);
|
|
|
|
void insertPrologEpilogCode(MachineFunction &Fn);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2002-12-28 21:43:30 +01:00
|
|
|
/// createPrologEpilogCodeInserter - This function returns a pass that inserts
|
|
|
|
/// prolog and epilog code, and eliminates abstract frame references.
|
|
|
|
///
|
2004-02-15 01:14:20 +01:00
|
|
|
FunctionPass *llvm::createPrologEpilogCodeInserter() { return new PEI(); }
|
2002-12-28 21:43:30 +01:00
|
|
|
|
|
|
|
|
2006-09-28 02:10:27 +02:00
|
|
|
/// calculateCalleeSavedRegisters - Scan the function for modified callee saved
|
2004-08-12 21:01:14 +02:00
|
|
|
/// registers. Also calculate the MaxCallFrameSize and HasCalls variables for
|
2002-12-28 21:43:30 +01:00
|
|
|
/// the function's frame information and eliminates call frame pseudo
|
|
|
|
/// instructions.
|
|
|
|
///
|
2006-09-27 00:29:31 +02:00
|
|
|
void PEI::calculateCalleeSavedRegisters(MachineFunction &Fn) {
|
2002-12-28 21:43:30 +01:00
|
|
|
const MRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
|
2004-08-12 21:01:14 +02:00
|
|
|
const TargetFrameInfo *TFI = Fn.getTarget().getFrameInfo();
|
2002-12-28 21:43:30 +01: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 03:27:10 +01:00
|
|
|
if (I->getOpcode() == FrameSetupOpcode ||
|
2004-08-07 09:07:57 +02:00
|
|
|
I->getOpcode() == FrameDestroyOpcode) {
|
2005-05-13 23:07:15 +02:00
|
|
|
assert(I->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo"
|
2004-08-07 09:07:57 +02:00
|
|
|
" 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 21:43:30 +01:00
|
|
|
} else {
|
2004-08-07 09:07:57 +02:00
|
|
|
++I;
|
2002-12-28 21:43:30 +01:00
|
|
|
}
|
|
|
|
|
2002-12-28 22:08:26 +01:00
|
|
|
MachineFrameInfo *FFI = Fn.getFrameInfo();
|
2002-12-28 21:43:30 +01: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-24 00:13:12 +01:00
|
|
|
const bool *PhysRegsUsed = Fn.getUsedPhysregs();
|
2005-09-30 18:59:07 +02:00
|
|
|
const TargetRegisterClass* const *CSRegClasses =
|
|
|
|
RegInfo->getCalleeSaveRegClasses();
|
2006-08-26 00:56:30 +02:00
|
|
|
std::vector<CalleeSavedInfo> CSI;
|
2002-12-28 21:43:30 +01:00
|
|
|
for (unsigned i = 0; CSRegs[i]; ++i) {
|
|
|
|
unsigned Reg = CSRegs[i];
|
2005-01-24 00:13:12 +01:00
|
|
|
if (PhysRegsUsed[Reg]) {
|
2005-09-30 18:59:07 +02:00
|
|
|
// If the reg is modified, save it!
|
2006-08-25 21:45:51 +02:00
|
|
|
CSI.push_back(CalleeSavedInfo(Reg, CSRegClasses[i]));
|
2003-10-08 07:20:08 +02:00
|
|
|
} else {
|
|
|
|
for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
|
2005-01-24 00:13:12 +01:00
|
|
|
*AliasSet; ++AliasSet) { // Check alias registers too.
|
|
|
|
if (PhysRegsUsed[*AliasSet]) {
|
2006-08-25 21:45:51 +02:00
|
|
|
CSI.push_back(CalleeSavedInfo(Reg, CSRegClasses[i]));
|
2004-08-07 09:07:57 +02:00
|
|
|
break;
|
2004-08-07 09:18:41 +02:00
|
|
|
}
|
2003-10-08 07:20:08 +02:00
|
|
|
}
|
|
|
|
}
|
2002-12-28 21:43:30 +01:00
|
|
|
}
|
|
|
|
|
2006-08-25 21:45:51 +02:00
|
|
|
if (CSI.empty())
|
2006-09-28 02:10:27 +02:00
|
|
|
return; // Early exit if no callee saved registers are modified!
|
2002-12-28 21:43:30 +01:00
|
|
|
|
2004-08-12 21:01:14 +02:00
|
|
|
unsigned NumFixedSpillSlots;
|
2004-08-15 11:18:55 +02:00
|
|
|
const std::pair<unsigned,int> *FixedSpillSlots =
|
2004-08-12 21:01:14 +02:00
|
|
|
TFI->getCalleeSaveSpillSlots(NumFixedSpillSlots);
|
|
|
|
|
2002-12-28 21:43:30 +01:00
|
|
|
// Now that we know which registers need to be saved and restored, allocate
|
|
|
|
// stack slots for them.
|
2006-09-28 02:10:27 +02:00
|
|
|
MinCSFrameIndex = INT_MAX;
|
|
|
|
MaxCSFrameIndex = 0;
|
2006-08-25 21:45:51 +02:00
|
|
|
for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
|
|
|
|
unsigned Reg = CSI[i].getReg();
|
|
|
|
const TargetRegisterClass *RC = CSI[i].getRegClass();
|
2004-08-12 21:01:14 +02:00
|
|
|
|
|
|
|
// Check to see if this physreg must be spilled to a particular stack slot
|
|
|
|
// on this target.
|
2004-08-15 11:18:55 +02:00
|
|
|
const std::pair<unsigned,int> *FixedSlot = FixedSpillSlots;
|
2004-08-12 21:01:14 +02:00
|
|
|
while (FixedSlot != FixedSpillSlots+NumFixedSpillSlots &&
|
|
|
|
FixedSlot->first != Reg)
|
|
|
|
++FixedSlot;
|
|
|
|
|
2004-08-21 21:45:10 +02:00
|
|
|
int FrameIdx;
|
2004-08-12 21:01:14 +02:00
|
|
|
if (FixedSlot == FixedSpillSlots+NumFixedSpillSlots) {
|
|
|
|
// Nope, just spill it anywhere convenient.
|
2006-09-28 20:52:32 +02:00
|
|
|
unsigned Align = RC->getAlignment();
|
|
|
|
unsigned StackAlign = TFI->getStackAlignment();
|
|
|
|
// We may not be able to sastify the desired alignment specification of
|
|
|
|
// the TargetRegisterClass if the stack alignment is smaller. Use the min.
|
|
|
|
Align = std::min(Align, StackAlign);
|
|
|
|
FrameIdx = FFI->CreateStackObject(RC->getSize(), Align);
|
2006-09-28 02:10:27 +02:00
|
|
|
if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
|
|
|
|
if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
|
2004-08-12 21:01:14 +02:00
|
|
|
} else {
|
|
|
|
// Spill it to the stack where we must.
|
2005-09-30 19:19:22 +02:00
|
|
|
FrameIdx = FFI->CreateFixedObject(RC->getSize(), FixedSlot->second);
|
2004-08-12 21:01:14 +02:00
|
|
|
}
|
2006-08-25 21:45:51 +02:00
|
|
|
CSI[i].setFrameIdx(FrameIdx);
|
2002-12-28 21:43:30 +01:00
|
|
|
}
|
2006-08-26 00:56:30 +02:00
|
|
|
|
|
|
|
FFI->setCalleeSavedInfo(CSI);
|
2004-08-12 21:01:14 +02:00
|
|
|
}
|
|
|
|
|
2006-09-28 02:10:27 +02:00
|
|
|
/// saveCalleeSavedRegisters - Insert spill code for any callee saved registers
|
2004-08-12 21:01:14 +02:00
|
|
|
/// that are modified in the function.
|
|
|
|
///
|
2006-09-28 02:10:27 +02:00
|
|
|
void PEI::saveCalleeSavedRegisters(MachineFunction &Fn) {
|
2006-08-25 21:45:51 +02:00
|
|
|
// Get callee saved register information.
|
|
|
|
MachineFrameInfo *FFI = Fn.getFrameInfo();
|
2006-08-26 00:56:30 +02:00
|
|
|
const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
|
2006-08-25 21:45:51 +02:00
|
|
|
|
2006-09-28 02:10:27 +02:00
|
|
|
// Early exit if no callee saved registers are modified!
|
2006-08-25 21:45:51 +02:00
|
|
|
if (CSI.empty())
|
2005-04-22 00:36:52 +02:00
|
|
|
return;
|
2004-08-12 21:01:14 +02:00
|
|
|
|
|
|
|
const MRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
|
2002-12-28 21:43:30 +01:00
|
|
|
|
|
|
|
// Now that we have a stack slot for each register to be saved, insert spill
|
2005-01-23 22:45:01 +01:00
|
|
|
// code into the entry block.
|
2002-12-28 21:43:30 +01:00
|
|
|
MachineBasicBlock *MBB = Fn.begin();
|
|
|
|
MachineBasicBlock::iterator I = MBB->begin();
|
2006-08-25 21:45:51 +02:00
|
|
|
for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
|
2004-08-15 23:56:44 +02:00
|
|
|
// Insert the spill to the stack frame.
|
2006-08-25 21:45:51 +02:00
|
|
|
RegInfo->storeRegToStackSlot(*MBB, I, CSI[i].getReg(), CSI[i].getFrameIdx(),
|
|
|
|
CSI[i].getRegClass());
|
2002-12-28 21:43:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add code to restore the callee-save registers in each exiting block.
|
2004-06-02 07:57:12 +02:00
|
|
|
const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
|
2005-01-23 22:45:01 +01: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 03:27:10 +01:00
|
|
|
if (!FI->empty() && TII.isReturn(FI->back().getOpcode())) {
|
|
|
|
MBB = FI;
|
|
|
|
I = MBB->end(); --I;
|
2002-12-28 21:43:30 +01:00
|
|
|
|
2005-05-15 05:09:58 +02:00
|
|
|
// Skip over all terminator instructions, which are part of the return
|
|
|
|
// sequence.
|
|
|
|
MachineBasicBlock::iterator I2 = I;
|
|
|
|
while (I2 != MBB->begin() && TII.isTerminatorInstr((--I2)->getOpcode()))
|
|
|
|
I = I2;
|
|
|
|
|
2005-08-29 02:10:46 +02:00
|
|
|
bool AtStart = I == MBB->begin();
|
2005-08-27 00:18:32 +02:00
|
|
|
MachineBasicBlock::iterator BeforeI = I;
|
|
|
|
if (!AtStart)
|
|
|
|
--BeforeI;
|
|
|
|
|
2005-05-15 05:09:58 +02:00
|
|
|
// Restore all registers immediately before the return and any terminators
|
|
|
|
// that preceed it.
|
2006-08-25 21:45:51 +02:00
|
|
|
for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
|
|
|
|
RegInfo->loadRegFromStackSlot(*MBB, I, CSI[i].getReg(),
|
|
|
|
CSI[i].getFrameIdx(),
|
|
|
|
CSI[i].getRegClass());
|
2005-01-19 22:32:07 +01:00
|
|
|
assert(I != MBB->begin() &&
|
|
|
|
"loadRegFromStackSlot didn't insert any code!");
|
2005-08-27 00:18:32 +02:00
|
|
|
// Insert in reverse order. loadRegFromStackSlot can insert multiple
|
|
|
|
// instructions.
|
|
|
|
if (AtStart)
|
|
|
|
I = MBB->begin();
|
|
|
|
else {
|
|
|
|
I = BeforeI;
|
|
|
|
++I;
|
|
|
|
}
|
2002-12-28 21:43:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
|
2005-01-23 22:45:01 +01:00
|
|
|
/// abstract stack objects.
|
2002-12-28 21:43:30 +01:00
|
|
|
///
|
|
|
|
void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) {
|
2004-06-02 07:57:12 +02:00
|
|
|
const TargetFrameInfo &TFI = *Fn.getTarget().getFrameInfo();
|
2005-04-22 00:36:52 +02:00
|
|
|
|
2002-12-28 21:43:30 +01:00
|
|
|
bool StackGrowsDown =
|
|
|
|
TFI.getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
|
2005-04-22 00:36:52 +02:00
|
|
|
|
2002-12-28 21:43:30 +01:00
|
|
|
// Loop over all of the stack objects, assigning sequential addresses...
|
2002-12-28 22:08:26 +01:00
|
|
|
MachineFrameInfo *FFI = Fn.getFrameInfo();
|
2002-12-28 21:43:30 +01:00
|
|
|
|
2003-01-16 03:24:20 +01:00
|
|
|
unsigned StackAlignment = TFI.getStackAlignment();
|
2005-11-06 18:43:20 +01:00
|
|
|
unsigned MaxAlign = 0;
|
2003-01-16 03:22:08 +01:00
|
|
|
|
2004-02-15 01:14:20 +01:00
|
|
|
// Start at the beginning of the local area.
|
2004-06-10 08:23:35 +02:00
|
|
|
// The Offset is the distance from the stack top in the direction
|
|
|
|
// of stack growth -- so it's always positive.
|
2003-01-15 23:52:34 +01:00
|
|
|
int Offset = TFI.getOffsetOfLocalArea();
|
2004-06-10 08:23:35 +02:00
|
|
|
if (StackGrowsDown)
|
2004-06-11 08:37:11 +02:00
|
|
|
Offset = -Offset;
|
2005-04-22 00:36:52 +02:00
|
|
|
assert(Offset >= 0
|
2004-06-10 08:23:35 +02:00
|
|
|
&& "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.
|
2005-04-22 00:36:52 +02:00
|
|
|
// We currently don't support filling in holes in between fixed sized objects,
|
2004-06-10 08:23:35 +02:00
|
|
|
// so we adjust 'Offset' to point to the end of last fixed sized
|
2004-02-15 01:14:20 +01:00
|
|
|
// preallocated object.
|
|
|
|
for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
|
2004-06-10 08:23:35 +02: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 {
|
2005-04-22 00:36:52 +02:00
|
|
|
// The maximum distance from the start pointer is at the upper
|
2004-06-10 08:23:35 +02:00
|
|
|
// address of the object.
|
|
|
|
FixedOff = FFI->getObjectOffset(i) + FFI->getObjectSize(i);
|
2005-04-22 00:36:52 +02:00
|
|
|
}
|
|
|
|
if (FixedOff > Offset) Offset = FixedOff;
|
2004-02-15 01:14:20 +01:00
|
|
|
}
|
|
|
|
|
2006-09-28 02:10:27 +02:00
|
|
|
// First assign frame offsets to stack objects that are used to spill
|
|
|
|
// callee save registers.
|
|
|
|
if (StackGrowsDown) {
|
|
|
|
for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
|
|
|
|
if (i < MinCSFrameIndex || i > MaxCSFrameIndex)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If stack grows down, we need to add size of find the lowest
|
|
|
|
// address of the object.
|
|
|
|
Offset += FFI->getObjectSize(i);
|
|
|
|
|
|
|
|
unsigned Align = FFI->getObjectAlignment(i);
|
|
|
|
// If the alignment of this object is greater than that of the stack, then
|
|
|
|
// increase the stack alignment to match.
|
|
|
|
MaxAlign = std::max(MaxAlign, Align);
|
|
|
|
// Adjust to alignment boundary
|
|
|
|
Offset = (Offset+Align-1)/Align*Align;
|
|
|
|
|
|
|
|
FFI->setObjectOffset(i, -Offset); // Set the computed offset
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = FFI->getObjectIndexEnd()-1; i >= 0; --i) {
|
|
|
|
if ((unsigned)i < MinCSFrameIndex || (unsigned)i > MaxCSFrameIndex)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned Align = FFI->getObjectAlignment(i);
|
|
|
|
// If the alignment of this object is greater than that of the stack, then
|
|
|
|
// increase the stack alignment to match.
|
|
|
|
MaxAlign = std::max(MaxAlign, Align);
|
|
|
|
// Adjust to alignment boundary
|
|
|
|
Offset = (Offset+Align-1)/Align*Align;
|
|
|
|
|
|
|
|
FFI->setObjectOffset(i, Offset);
|
|
|
|
Offset += FFI->getObjectSize(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then assign frame offsets to stack objects that are not used to spill
|
|
|
|
// callee save registers.
|
2002-12-28 21:43:30 +01:00
|
|
|
for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
|
2006-09-28 02:10:27 +02:00
|
|
|
if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
|
|
|
|
continue;
|
|
|
|
|
2004-06-10 08:23:35 +02: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 21:43:30 +01:00
|
|
|
|
|
|
|
unsigned Align = FFI->getObjectAlignment(i);
|
2005-11-06 10:00:38 +01:00
|
|
|
// If the alignment of this object is greater than that of the stack, then
|
|
|
|
// increase the stack alignment to match.
|
|
|
|
MaxAlign = std::max(MaxAlign, Align);
|
|
|
|
// Adjust to alignment boundary
|
|
|
|
Offset = (Offset+Align-1)/Align*Align;
|
2005-04-22 00:36:52 +02:00
|
|
|
|
2004-06-10 08:23:35 +02:00
|
|
|
if (StackGrowsDown) {
|
|
|
|
FFI->setObjectOffset(i, -Offset); // Set the computed offset
|
|
|
|
} else {
|
2005-04-22 00:36:52 +02:00
|
|
|
FFI->setObjectOffset(i, Offset);
|
2004-06-10 08:23:35 +02:00
|
|
|
Offset += FFI->getObjectSize(i);
|
|
|
|
}
|
2002-12-28 21:43:30 +01:00
|
|
|
}
|
|
|
|
|
2006-09-28 02:10:27 +02:00
|
|
|
|
2004-02-14 21:10:59 +01: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 21:43:30 +01:00
|
|
|
|
|
|
|
// Set the final value of the stack pointer...
|
2004-06-11 08:37:11 +02:00
|
|
|
FFI->setStackSize(Offset+TFI.getOffsetOfLocalArea());
|
2005-11-06 18:43:20 +01:00
|
|
|
|
|
|
|
// Remember the required stack alignment in case targets need it to perform
|
|
|
|
// dynamic stack alignment.
|
2006-04-03 23:39:57 +02:00
|
|
|
assert(FFI->getMaxAlignment() == MaxAlign &&
|
|
|
|
"Stack alignment calculation broken!");
|
2003-01-15 23:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-28 02:10:27 +02:00
|
|
|
/// insertPrologEpilogCode - Scan the function for modified callee saved
|
|
|
|
/// registers, insert spill code for these callee saved registers, then add
|
2003-01-15 23:52:34 +01:00
|
|
|
/// 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 07:57:12 +02:00
|
|
|
const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
|
2003-01-15 23:52:34 +01: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 03:27:10 +01:00
|
|
|
if (!I->empty() && TII.isReturn(I->back().getOpcode()))
|
2003-01-15 23:52:34 +01:00
|
|
|
Fn.getTarget().getRegisterInfo()->emitEpilogue(Fn, *I);
|
|
|
|
}
|
2002-12-28 21:43:30 +01: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 03:27:10 +01:00
|
|
|
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
|
2004-08-07 09:18:41 +02: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-15 00:00:10 +02:00
|
|
|
MRI.eliminateFrameIndex(I);
|
2004-08-07 09:18:41 +02:00
|
|
|
break;
|
|
|
|
}
|
2002-12-28 21:43:30 +01:00
|
|
|
}
|