2011-01-10 13:39:04 +01:00
|
|
|
//=======- MBlazeFrameLowering.cpp - MBlaze Frame Information ------*- C++ -*-====//
|
2010-11-15 01:06:54 +01:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2011-01-10 13:39:04 +01:00
|
|
|
// This file contains the MBlaze implementation of TargetFrameLowering class.
|
2010-11-15 01:06:54 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-01-10 13:39:04 +01:00
|
|
|
#define DEBUG_TYPE "mblaze-frame-lowering"
|
2010-12-29 20:46:28 +01:00
|
|
|
|
2011-01-10 13:39:04 +01:00
|
|
|
#include "MBlazeFrameLowering.h"
|
2010-11-15 01:06:54 +01:00
|
|
|
#include "MBlazeInstrInfo.h"
|
|
|
|
#include "MBlazeMachineFunction.h"
|
2010-12-12 21:52:31 +01:00
|
|
|
#include "InstPrinter/MBlazeInstPrinter.h"
|
2010-11-15 01:06:54 +01:00
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2010-12-29 20:46:28 +01:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-11-15 01:06:54 +01:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2010-12-12 21:52:31 +01:00
|
|
|
namespace llvm {
|
|
|
|
cl::opt<bool> DisableStackAdjust(
|
|
|
|
"disable-mblaze-stack-adjust",
|
|
|
|
cl::init(false),
|
|
|
|
cl::desc("Disable MBlaze stack layout adjustment."),
|
|
|
|
cl::Hidden);
|
|
|
|
}
|
|
|
|
|
2011-01-03 22:40:26 +01:00
|
|
|
static void replaceFrameIndexes(MachineFunction &MF,
|
|
|
|
SmallVector<std::pair<int,int64_t>, 16> &FR) {
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
2011-01-05 18:34:20 +01:00
|
|
|
MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
|
2011-01-03 22:40:26 +01:00
|
|
|
const SmallVector<std::pair<int,int64_t>, 16>::iterator FRB = FR.begin();
|
|
|
|
const SmallVector<std::pair<int,int64_t>, 16>::iterator FRE = FR.end();
|
|
|
|
|
|
|
|
SmallVector<std::pair<int,int64_t>, 16>::iterator FRI = FRB;
|
|
|
|
for (; FRI != FRE; ++FRI) {
|
|
|
|
MFI->RemoveStackObject(FRI->first);
|
|
|
|
int NFI = MFI->CreateFixedObject(4, FRI->second, true);
|
2011-01-05 18:34:20 +01:00
|
|
|
MBlazeFI->recordReplacement(FRI->first, NFI);
|
2011-01-03 22:40:26 +01:00
|
|
|
|
|
|
|
for (MachineFunction::iterator MB=MF.begin(), ME=MF.end(); MB!=ME; ++MB) {
|
|
|
|
MachineBasicBlock::iterator MBB = MB->begin();
|
|
|
|
const MachineBasicBlock::iterator MBE = MB->end();
|
|
|
|
|
|
|
|
for (; MBB != MBE; ++MBB) {
|
|
|
|
MachineInstr::mop_iterator MIB = MBB->operands_begin();
|
|
|
|
const MachineInstr::mop_iterator MIE = MBB->operands_end();
|
|
|
|
|
|
|
|
for (MachineInstr::mop_iterator MII = MIB; MII != MIE; ++MII) {
|
|
|
|
if (!MII->isFI() || MII->getIndex() != FRI->first) continue;
|
|
|
|
DEBUG(dbgs() << "FOUND FI#" << MII->getIndex() << "\n");
|
|
|
|
MII->setIndex(NFI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-15 01:06:54 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Stack Frame Processing methods
|
|
|
|
// +----------------------------+
|
|
|
|
//
|
|
|
|
// The stack is allocated decrementing the stack pointer on
|
|
|
|
// the first instruction of a function prologue. Once decremented,
|
|
|
|
// all stack references are are done through a positive offset
|
|
|
|
// from the stack/frame pointer, so the stack is considered
|
|
|
|
// to grow up.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-12-12 21:52:31 +01:00
|
|
|
static void analyzeFrameIndexes(MachineFunction &MF) {
|
|
|
|
if (DisableStackAdjust) return;
|
|
|
|
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
|
|
|
|
const MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
|
|
|
|
MachineRegisterInfo::livein_iterator LII = MRI.livein_begin();
|
|
|
|
MachineRegisterInfo::livein_iterator LIE = MRI.livein_end();
|
|
|
|
const SmallVector<int, 16> &LiveInFI = MBlazeFI->getLiveIn();
|
|
|
|
SmallVector<MachineInstr*, 16> EraseInstr;
|
2011-01-03 22:40:26 +01:00
|
|
|
SmallVector<std::pair<int,int64_t>, 16> FrameRelocate;
|
2010-12-12 21:52:31 +01:00
|
|
|
|
|
|
|
MachineBasicBlock *MBB = MF.getBlockNumbered(0);
|
|
|
|
MachineBasicBlock::iterator MIB = MBB->begin();
|
|
|
|
MachineBasicBlock::iterator MIE = MBB->end();
|
|
|
|
|
|
|
|
int StackAdjust = 0;
|
|
|
|
int StackOffset = -28;
|
2010-12-29 20:46:28 +01:00
|
|
|
|
|
|
|
// In this loop we are searching frame indexes that corrospond to incoming
|
|
|
|
// arguments that are already in the stack. We look for instruction sequences
|
|
|
|
// like the following:
|
|
|
|
//
|
|
|
|
// LWI REG, FI1, 0
|
|
|
|
// ...
|
|
|
|
// SWI REG, FI2, 0
|
|
|
|
//
|
|
|
|
// As long as there are no defs of REG in the ... part, we can eliminate
|
|
|
|
// the SWI instruction because the value has already been stored to the
|
|
|
|
// stack by the caller. All we need to do is locate FI at the correct
|
|
|
|
// stack location according to the calling convensions.
|
|
|
|
//
|
|
|
|
// Additionally, if the SWI operation kills the def of REG then we don't
|
|
|
|
// need the LWI operation so we can erase it as well.
|
2010-12-12 21:52:31 +01:00
|
|
|
for (unsigned i = 0, e = LiveInFI.size(); i < e; ++i) {
|
|
|
|
for (MachineBasicBlock::iterator I=MIB; I != MIE; ++I) {
|
|
|
|
if (I->getOpcode() != MBlaze::LWI || I->getNumOperands() != 3 ||
|
|
|
|
!I->getOperand(1).isFI() || !I->getOperand(0).isReg() ||
|
|
|
|
I->getOperand(1).getIndex() != LiveInFI[i]) continue;
|
|
|
|
|
|
|
|
unsigned FIReg = I->getOperand(0).getReg();
|
|
|
|
MachineBasicBlock::iterator SI = I;
|
|
|
|
for (SI++; SI != MIE; ++SI) {
|
2010-12-29 20:46:28 +01:00
|
|
|
if (!SI->getOperand(0).isReg() ||
|
|
|
|
!SI->getOperand(1).isFI() ||
|
|
|
|
SI->getOpcode() != MBlaze::SWI) continue;
|
2010-12-12 21:52:31 +01:00
|
|
|
|
|
|
|
int FI = SI->getOperand(1).getIndex();
|
2010-12-29 20:46:28 +01:00
|
|
|
if (SI->getOperand(0).getReg() != FIReg ||
|
|
|
|
MFI->isFixedObjectIndex(FI) ||
|
|
|
|
MFI->getObjectSize(FI) != 4) continue;
|
|
|
|
|
2010-12-12 21:52:31 +01:00
|
|
|
if (SI->getOperand(0).isDef()) break;
|
|
|
|
|
2010-12-29 20:46:28 +01:00
|
|
|
if (SI->getOperand(0).isKill()) {
|
|
|
|
DEBUG(dbgs() << "LWI for FI#" << I->getOperand(1).getIndex()
|
|
|
|
<< " removed\n");
|
2010-12-12 21:52:31 +01:00
|
|
|
EraseInstr.push_back(I);
|
2010-12-29 20:46:28 +01:00
|
|
|
}
|
|
|
|
|
2010-12-12 21:52:31 +01:00
|
|
|
EraseInstr.push_back(SI);
|
2010-12-29 20:46:28 +01:00
|
|
|
DEBUG(dbgs() << "SWI for FI#" << FI << " removed\n");
|
|
|
|
|
2011-01-03 22:40:26 +01:00
|
|
|
FrameRelocate.push_back(std::make_pair(FI,StackOffset));
|
2010-12-29 20:46:28 +01:00
|
|
|
DEBUG(dbgs() << "FI#" << FI << " relocated to " << StackOffset << "\n");
|
|
|
|
|
2010-12-12 21:52:31 +01:00
|
|
|
StackOffset -= 4;
|
|
|
|
StackAdjust += 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-29 20:46:28 +01:00
|
|
|
|
|
|
|
// In this loop we are searching for frame indexes that corrospond to
|
|
|
|
// incoming arguments that are in registers. We look for instruction
|
|
|
|
// sequences like the following:
|
|
|
|
//
|
|
|
|
// ... SWI REG, FI, 0
|
|
|
|
//
|
|
|
|
// As long as the ... part does not define REG and if REG is an incoming
|
|
|
|
// parameter register then we know that, according to ABI convensions, the
|
|
|
|
// caller has allocated stack space for it already. Instead of allocating
|
|
|
|
// stack space on our frame, we record the correct location in the callers
|
|
|
|
// frame.
|
|
|
|
for (MachineRegisterInfo::livein_iterator LI = LII; LI != LIE; ++LI) {
|
|
|
|
for (MachineBasicBlock::iterator I=MIB; I != MIE; ++I) {
|
|
|
|
if (I->definesRegister(LI->first))
|
|
|
|
break;
|
2010-12-12 21:52:31 +01:00
|
|
|
|
2010-12-29 20:46:28 +01:00
|
|
|
if (I->getOpcode() != MBlaze::SWI || I->getNumOperands() != 3 ||
|
|
|
|
!I->getOperand(1).isFI() || !I->getOperand(0).isReg() ||
|
|
|
|
I->getOperand(1).getIndex() < 0) continue;
|
2010-12-12 21:52:31 +01:00
|
|
|
|
|
|
|
if (I->getOperand(0).getReg() == LI->first) {
|
2010-12-29 20:46:28 +01:00
|
|
|
int FI = I->getOperand(1).getIndex();
|
|
|
|
MBlazeFI->recordLiveIn(FI);
|
|
|
|
|
|
|
|
int FILoc = 0;
|
|
|
|
switch (LI->first) {
|
|
|
|
default: llvm_unreachable("invalid incoming parameter!");
|
|
|
|
case MBlaze::R5: FILoc = -4; break;
|
|
|
|
case MBlaze::R6: FILoc = -8; break;
|
|
|
|
case MBlaze::R7: FILoc = -12; break;
|
|
|
|
case MBlaze::R8: FILoc = -16; break;
|
|
|
|
case MBlaze::R9: FILoc = -20; break;
|
|
|
|
case MBlaze::R10: FILoc = -24; break;
|
|
|
|
}
|
2010-12-12 21:52:31 +01:00
|
|
|
|
2010-12-29 20:46:28 +01:00
|
|
|
StackAdjust += 4;
|
2011-01-03 22:40:26 +01:00
|
|
|
FrameRelocate.push_back(std::make_pair(FI,FILoc));
|
2010-12-29 20:46:28 +01:00
|
|
|
DEBUG(dbgs() << "FI#" << FI << " relocated to " << FILoc << "\n");
|
|
|
|
break;
|
2010-12-12 21:52:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-29 20:46:28 +01:00
|
|
|
// Go ahead and erase all of the instructions that we determined were
|
|
|
|
// no longer needed.
|
2010-12-12 21:52:31 +01:00
|
|
|
for (int i = 0, e = EraseInstr.size(); i < e; ++i)
|
|
|
|
MBB->erase(EraseInstr[i]);
|
|
|
|
|
2011-01-03 22:40:26 +01:00
|
|
|
// Replace all of the frame indexes that we have relocated with new
|
|
|
|
// fixed object frame indexes.
|
|
|
|
replaceFrameIndexes(MF, FrameRelocate);
|
2010-12-12 21:52:31 +01:00
|
|
|
}
|
|
|
|
|
2010-12-15 21:27:28 +01:00
|
|
|
static void interruptFrameLayout(MachineFunction &MF) {
|
|
|
|
const Function *F = MF.getFunction();
|
|
|
|
llvm::CallingConv::ID CallConv = F->getCallingConv();
|
|
|
|
|
|
|
|
// If this function is not using either the interrupt_handler
|
|
|
|
// calling convention or the save_volatiles calling convention
|
|
|
|
// then we don't need to do any additional frame layout.
|
|
|
|
if (CallConv != llvm::CallingConv::MBLAZE_INTR &&
|
|
|
|
CallConv != llvm::CallingConv::MBLAZE_SVOL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
const MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
const MBlazeInstrInfo &TII =
|
|
|
|
*static_cast<const MBlazeInstrInfo*>(MF.getTarget().getInstrInfo());
|
|
|
|
|
|
|
|
// Determine if the calling convention is the interrupt_handler
|
|
|
|
// calling convention. Some pieces of the prologue and epilogue
|
|
|
|
// only need to be emitted if we are lowering and interrupt handler.
|
|
|
|
bool isIntr = CallConv == llvm::CallingConv::MBLAZE_INTR;
|
|
|
|
|
|
|
|
// Determine where to put prologue and epilogue additions
|
|
|
|
MachineBasicBlock &MENT = MF.front();
|
|
|
|
MachineBasicBlock &MEXT = MF.back();
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator MENTI = MENT.begin();
|
|
|
|
MachineBasicBlock::iterator MEXTI = prior(MEXT.end());
|
|
|
|
|
|
|
|
DebugLoc ENTDL = MENTI != MENT.end() ? MENTI->getDebugLoc() : DebugLoc();
|
|
|
|
DebugLoc EXTDL = MEXTI != MEXT.end() ? MEXTI->getDebugLoc() : DebugLoc();
|
|
|
|
|
|
|
|
// Store the frame indexes generated during prologue additions for use
|
|
|
|
// when we are generating the epilogue additions.
|
|
|
|
SmallVector<int, 10> VFI;
|
|
|
|
|
|
|
|
// Build the prologue SWI for R3 - R12 if needed. Note that R11 must
|
|
|
|
// always have a SWI because it is used when processing RMSR.
|
|
|
|
for (unsigned r = MBlaze::R3; r <= MBlaze::R12; ++r) {
|
|
|
|
if (!MRI.isPhysRegUsed(r) && !(isIntr && r == MBlaze::R11)) continue;
|
|
|
|
|
|
|
|
int FI = MFI->CreateStackObject(4,4,false,false);
|
|
|
|
VFI.push_back(FI);
|
|
|
|
|
|
|
|
BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), r)
|
|
|
|
.addFrameIndex(FI).addImm(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the prologue SWI for R17, R18
|
|
|
|
int R17FI = MFI->CreateStackObject(4,4,false,false);
|
|
|
|
int R18FI = MFI->CreateStackObject(4,4,false,false);
|
|
|
|
|
|
|
|
BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), MBlaze::R17)
|
|
|
|
.addFrameIndex(R17FI).addImm(0);
|
|
|
|
|
|
|
|
BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), MBlaze::R18)
|
|
|
|
.addFrameIndex(R18FI).addImm(0);
|
|
|
|
|
|
|
|
// Buid the prologue SWI and the epilogue LWI for RMSR if needed
|
|
|
|
if (isIntr) {
|
|
|
|
int MSRFI = MFI->CreateStackObject(4,4,false,false);
|
|
|
|
BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::MFS), MBlaze::R11)
|
|
|
|
.addReg(MBlaze::RMSR);
|
|
|
|
BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), MBlaze::R11)
|
|
|
|
.addFrameIndex(MSRFI).addImm(0);
|
|
|
|
|
|
|
|
BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::LWI), MBlaze::R11)
|
|
|
|
.addFrameIndex(MSRFI).addImm(0);
|
|
|
|
BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::MTS), MBlaze::RMSR)
|
|
|
|
.addReg(MBlaze::R11);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the epilogue LWI for R17, R18
|
|
|
|
BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::LWI), MBlaze::R18)
|
|
|
|
.addFrameIndex(R18FI).addImm(0);
|
|
|
|
|
|
|
|
BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::LWI), MBlaze::R17)
|
|
|
|
.addFrameIndex(R17FI).addImm(0);
|
|
|
|
|
|
|
|
// Build the epilogue LWI for R3 - R12 if needed
|
|
|
|
for (unsigned r = MBlaze::R12, i = VFI.size(); r >= MBlaze::R3; --r) {
|
|
|
|
if (!MRI.isPhysRegUsed(r)) continue;
|
|
|
|
BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::LWI), r)
|
|
|
|
.addFrameIndex(VFI[--i]).addImm(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-12 21:52:31 +01:00
|
|
|
static void determineFrameLayout(MachineFunction &MF) {
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
|
|
|
|
|
|
|
|
// Replace the dummy '0' SPOffset by the negative offsets, as explained on
|
|
|
|
// LowerFORMAL_ARGUMENTS. Leaving '0' for while is necessary to avoid
|
|
|
|
// the approach done by calculateFrameObjectOffsets to the stack frame.
|
|
|
|
MBlazeFI->adjustLoadArgsFI(MFI);
|
|
|
|
MBlazeFI->adjustStoreVarArgsFI(MFI);
|
|
|
|
|
|
|
|
// Get the number of bytes to allocate from the FrameInfo
|
|
|
|
unsigned FrameSize = MFI->getStackSize();
|
2010-12-29 20:46:28 +01:00
|
|
|
DEBUG(dbgs() << "Original Frame Size: " << FrameSize << "\n" );
|
|
|
|
|
2010-12-12 21:52:31 +01:00
|
|
|
// Get the alignments provided by the target, and the maximum alignment
|
|
|
|
// (if any) of the fixed frame objects.
|
|
|
|
// unsigned MaxAlign = MFI->getMaxAlignment();
|
2011-01-10 13:39:04 +01:00
|
|
|
unsigned TargetAlign = MF.getTarget().getFrameLowering()->getStackAlignment();
|
2010-12-12 21:52:31 +01:00
|
|
|
unsigned AlignMask = TargetAlign - 1;
|
|
|
|
|
|
|
|
// Make sure the frame is aligned.
|
|
|
|
FrameSize = (FrameSize + AlignMask) & ~AlignMask;
|
|
|
|
MFI->setStackSize(FrameSize);
|
2010-12-29 20:46:28 +01:00
|
|
|
DEBUG(dbgs() << "Aligned Frame Size: " << FrameSize << "\n" );
|
2010-12-12 21:52:31 +01:00
|
|
|
}
|
|
|
|
|
2011-01-10 13:56:18 +01:00
|
|
|
int MBlazeFrameLowering::getFrameIndexOffset(const MachineFunction &MF, int FI)
|
2011-01-05 18:34:20 +01:00
|
|
|
const {
|
|
|
|
const MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
|
|
|
|
if (MBlazeFI->hasReplacement(FI))
|
|
|
|
FI = MBlazeFI->getReplacement(FI);
|
2011-01-10 13:56:18 +01:00
|
|
|
return TargetFrameLowering::getFrameIndexOffset(MF,FI);
|
2011-01-05 18:34:20 +01:00
|
|
|
}
|
|
|
|
|
2010-11-18 22:19:35 +01:00
|
|
|
// hasFP - Return true if the specified function should have a dedicated frame
|
|
|
|
// pointer register. This is true if the function has variable sized allocas or
|
|
|
|
// if frame pointer elimination is disabled.
|
2011-01-10 13:39:04 +01:00
|
|
|
bool MBlazeFrameLowering::hasFP(const MachineFunction &MF) const {
|
2010-11-18 22:19:35 +01:00
|
|
|
const MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
return DisableFramePointerElim(MF) || MFI->hasVarSizedObjects();
|
|
|
|
}
|
|
|
|
|
2011-01-10 13:39:04 +01:00
|
|
|
void MBlazeFrameLowering::emitPrologue(MachineFunction &MF) const {
|
2010-11-15 01:06:54 +01:00
|
|
|
MachineBasicBlock &MBB = MF.front();
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
const MBlazeInstrInfo &TII =
|
|
|
|
*static_cast<const MBlazeInstrInfo*>(MF.getTarget().getInstrInfo());
|
|
|
|
MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
|
|
|
|
MachineBasicBlock::iterator MBBI = MBB.begin();
|
|
|
|
DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
|
|
|
|
|
2010-12-15 21:27:28 +01:00
|
|
|
llvm::CallingConv::ID CallConv = MF.getFunction()->getCallingConv();
|
|
|
|
bool requiresRA = CallConv == llvm::CallingConv::MBLAZE_INTR;
|
|
|
|
|
2010-12-12 21:52:31 +01:00
|
|
|
// Determine the correct frame layout
|
|
|
|
determineFrameLayout(MF);
|
2010-11-15 01:06:54 +01:00
|
|
|
|
|
|
|
// Get the number of bytes to allocate from the FrameInfo.
|
|
|
|
unsigned StackSize = MFI->getStackSize();
|
|
|
|
|
|
|
|
// No need to allocate space on the stack.
|
2010-12-15 21:27:28 +01:00
|
|
|
if (StackSize == 0 && !MFI->adjustsStack() && !requiresRA) return;
|
2010-11-15 01:06:54 +01:00
|
|
|
|
|
|
|
int FPOffset = MBlazeFI->getFPStackOffset();
|
|
|
|
int RAOffset = MBlazeFI->getRAStackOffset();
|
|
|
|
|
|
|
|
// Adjust stack : addi R1, R1, -imm
|
2010-12-12 23:02:31 +01:00
|
|
|
BuildMI(MBB, MBBI, DL, TII.get(MBlaze::ADDIK), MBlaze::R1)
|
2010-11-15 01:06:54 +01:00
|
|
|
.addReg(MBlaze::R1).addImm(-StackSize);
|
|
|
|
|
|
|
|
// swi R15, R1, stack_loc
|
2010-12-15 21:27:28 +01:00
|
|
|
if (MFI->adjustsStack() || requiresRA) {
|
2010-11-15 01:06:54 +01:00
|
|
|
BuildMI(MBB, MBBI, DL, TII.get(MBlaze::SWI))
|
|
|
|
.addReg(MBlaze::R15).addReg(MBlaze::R1).addImm(RAOffset);
|
|
|
|
}
|
|
|
|
|
2010-11-18 22:19:35 +01:00
|
|
|
if (hasFP(MF)) {
|
2010-11-15 01:06:54 +01:00
|
|
|
// swi R19, R1, stack_loc
|
|
|
|
BuildMI(MBB, MBBI, DL, TII.get(MBlaze::SWI))
|
|
|
|
.addReg(MBlaze::R19).addReg(MBlaze::R1).addImm(FPOffset);
|
|
|
|
|
|
|
|
// add R19, R1, R0
|
|
|
|
BuildMI(MBB, MBBI, DL, TII.get(MBlaze::ADD), MBlaze::R19)
|
|
|
|
.addReg(MBlaze::R1).addReg(MBlaze::R0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-10 13:39:04 +01:00
|
|
|
void MBlazeFrameLowering::emitEpilogue(MachineFunction &MF,
|
2010-11-15 01:06:54 +01:00
|
|
|
MachineBasicBlock &MBB) const {
|
2011-01-13 22:28:52 +01:00
|
|
|
MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
|
2010-11-15 01:06:54 +01:00
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
2010-11-18 22:19:35 +01:00
|
|
|
MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
|
2010-11-15 01:06:54 +01:00
|
|
|
const MBlazeInstrInfo &TII =
|
|
|
|
*static_cast<const MBlazeInstrInfo*>(MF.getTarget().getInstrInfo());
|
|
|
|
|
|
|
|
DebugLoc dl = MBBI->getDebugLoc();
|
|
|
|
|
2010-12-15 21:27:28 +01:00
|
|
|
llvm::CallingConv::ID CallConv = MF.getFunction()->getCallingConv();
|
|
|
|
bool requiresRA = CallConv == llvm::CallingConv::MBLAZE_INTR;
|
|
|
|
|
2010-11-15 01:06:54 +01:00
|
|
|
// Get the FI's where RA and FP are saved.
|
|
|
|
int FPOffset = MBlazeFI->getFPStackOffset();
|
|
|
|
int RAOffset = MBlazeFI->getRAStackOffset();
|
|
|
|
|
2010-11-18 22:19:35 +01:00
|
|
|
if (hasFP(MF)) {
|
2010-11-15 01:06:54 +01:00
|
|
|
// add R1, R19, R0
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(MBlaze::ADD), MBlaze::R1)
|
|
|
|
.addReg(MBlaze::R19).addReg(MBlaze::R0);
|
|
|
|
|
|
|
|
// lwi R19, R1, stack_loc
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(MBlaze::LWI), MBlaze::R19)
|
|
|
|
.addReg(MBlaze::R1).addImm(FPOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// lwi R15, R1, stack_loc
|
2010-12-15 21:27:28 +01:00
|
|
|
if (MFI->adjustsStack() || requiresRA) {
|
2010-11-15 01:06:54 +01:00
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(MBlaze::LWI), MBlaze::R15)
|
|
|
|
.addReg(MBlaze::R1).addImm(RAOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the number of bytes from FrameInfo
|
|
|
|
int StackSize = (int) MFI->getStackSize();
|
|
|
|
|
|
|
|
// addi R1, R1, imm
|
|
|
|
if (StackSize) {
|
2010-12-12 23:02:31 +01:00
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(MBlaze::ADDIK), MBlaze::R1)
|
2010-11-15 01:06:54 +01:00
|
|
|
.addReg(MBlaze::R1).addImm(StackSize);
|
|
|
|
}
|
|
|
|
}
|
2010-12-09 04:42:04 +01:00
|
|
|
|
2011-01-10 13:39:04 +01:00
|
|
|
void MBlazeFrameLowering::
|
|
|
|
processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
|
|
|
|
RegScavenger *RS) const {
|
2010-12-09 04:42:04 +01:00
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
|
2010-12-15 21:27:28 +01:00
|
|
|
llvm::CallingConv::ID CallConv = MF.getFunction()->getCallingConv();
|
|
|
|
bool requiresRA = CallConv == llvm::CallingConv::MBLAZE_INTR;
|
2010-12-09 04:42:04 +01:00
|
|
|
|
2010-12-15 21:27:28 +01:00
|
|
|
if (MFI->adjustsStack() || requiresRA) {
|
2010-12-09 04:42:04 +01:00
|
|
|
MBlazeFI->setRAStackOffset(0);
|
|
|
|
MFI->CreateFixedObject(4,0,true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasFP(MF)) {
|
|
|
|
MBlazeFI->setFPStackOffset(4);
|
|
|
|
MFI->CreateFixedObject(4,4,true);
|
|
|
|
}
|
2010-12-12 21:52:31 +01:00
|
|
|
|
2010-12-15 21:27:28 +01:00
|
|
|
interruptFrameLayout(MF);
|
2010-12-12 21:52:31 +01:00
|
|
|
analyzeFrameIndexes(MF);
|
2010-12-09 04:42:04 +01:00
|
|
|
}
|