2012-02-18 20:03:15 +08:00
|
|
|
//===-- XCoreFrameLowering.cpp - Frame info for XCore Target --------------===//
|
2008-11-07 18:59:00 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains XCore frame information that doesn't fit anywhere else
|
|
|
|
// cleanly...
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
#include "XCoreFrameLowering.h"
|
2012-03-18 02:46:09 +08:00
|
|
|
#include "XCore.h"
|
2010-11-15 08:06:54 +08:00
|
|
|
#include "XCoreInstrInfo.h"
|
|
|
|
#include "XCoreMachineFunctionInfo.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"
|
2010-11-28 07:05:25 +08:00
|
|
|
#include "llvm/CodeGen/RegisterScavenging.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
2010-11-15 08:06:54 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2010-11-15 08:06:54 +08:00
|
|
|
|
2008-11-07 18:59:00 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2013-12-02 18:18:14 +08:00
|
|
|
static const unsigned FramePtr = XCore::R10;
|
|
|
|
static const int MaxImmU16 = (1<<16) - 1;
|
|
|
|
|
2010-11-15 08:06:54 +08:00
|
|
|
// helper functions. FIXME: Eliminate.
|
|
|
|
static inline bool isImmU6(unsigned val) {
|
|
|
|
return val < (1 << 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool isImmU16(unsigned val) {
|
|
|
|
return val < (1 << 16);
|
|
|
|
}
|
|
|
|
|
2013-12-02 18:18:14 +08:00
|
|
|
static void EmitDefCfaRegister(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI, DebugLoc dl,
|
|
|
|
const TargetInstrInfo &TII,
|
|
|
|
MachineModuleInfo *MMI, unsigned DRegNum) {
|
|
|
|
MCSymbol *Label = MMI->getContext().CreateTempSymbol();
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(XCore::PROLOG_LABEL)).addSym(Label);
|
|
|
|
MMI->addFrameInst(MCCFIInstruction::createDefCfaRegister(Label, DRegNum));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void EmitDefCfaOffset(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI, DebugLoc dl,
|
|
|
|
const TargetInstrInfo &TII,
|
|
|
|
MachineModuleInfo *MMI, int Offset) {
|
|
|
|
MCSymbol *Label = MMI->getContext().CreateTempSymbol();
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(XCore::PROLOG_LABEL)).addSym(Label);
|
|
|
|
MMI->addFrameInst(MCCFIInstruction::createDefCfaOffset(Label, -Offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void EmitCfiOffset(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI, DebugLoc dl,
|
|
|
|
const TargetInstrInfo &TII, MachineModuleInfo *MMI,
|
|
|
|
unsigned DRegNum, int Offset, MCSymbol *Label) {
|
|
|
|
if (!Label) {
|
|
|
|
Label = MMI->getContext().CreateTempSymbol();
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(XCore::PROLOG_LABEL)).addSym(Label);
|
|
|
|
}
|
|
|
|
MMI->addFrameInst(MCCFIInstruction::createOffset(Label, DRegNum, Offset));
|
2010-11-15 08:06:54 +08:00
|
|
|
}
|
|
|
|
|
2013-12-02 18:18:14 +08:00
|
|
|
/// The SP register is moved in steps of 'MaxImmU16' towards the bottom of the
|
|
|
|
/// frame. During these steps, it may be necessary to spill registers.
|
|
|
|
/// IfNeededExtSP emits the necessary EXTSP instructions to move the SP only
|
|
|
|
/// as far as to make 'OffsetFromBottom' reachable using an STWSP_lru6.
|
|
|
|
/// \param OffsetFromTop the spill offset from the top of the frame.
|
|
|
|
/// \param [in] [out] Adjusted the current SP offset from the top of the frame.
|
|
|
|
static void IfNeededExtSP(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI, DebugLoc dl,
|
|
|
|
const TargetInstrInfo &TII, MachineModuleInfo *MMI,
|
|
|
|
int OffsetFromTop, int &Adjusted, int FrameSize,
|
|
|
|
bool emitFrameMoves) {
|
|
|
|
while (OffsetFromTop > Adjusted) {
|
|
|
|
assert(Adjusted < FrameSize && "OffsetFromTop is beyond FrameSize");
|
|
|
|
int remaining = FrameSize - Adjusted;
|
|
|
|
int OpImm = (remaining > MaxImmU16) ? MaxImmU16 : remaining;
|
|
|
|
int Opcode = isImmU6(OpImm) ? XCore::EXTSP_u6 : XCore::EXTSP_lu6;
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(Opcode)).addImm(OpImm);
|
|
|
|
Adjusted += OpImm;
|
|
|
|
if (emitFrameMoves)
|
|
|
|
EmitDefCfaOffset(MBB, MBBI, dl, TII, MMI, Adjusted*4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The SP register is moved in steps of 'MaxImmU16' towards the top of the
|
|
|
|
/// frame. During these steps, it may be necessary to re-load registers.
|
|
|
|
/// IfNeededLDAWSP emits the necessary LDAWSP instructions to move the SP only
|
|
|
|
/// as far as to make 'OffsetFromTop' reachable using an LDAWSP_lru6.
|
|
|
|
/// \param OffsetFromTop the spill offset from the top of the frame.
|
|
|
|
/// \param [in] [out] RemainingAdj the current SP offset from the top of the frame.
|
|
|
|
static void IfNeededLDAWSP(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI, DebugLoc dl,
|
|
|
|
const TargetInstrInfo &TII, int OffsetFromTop,
|
|
|
|
int &RemainingAdj) {
|
|
|
|
while (OffsetFromTop < RemainingAdj - MaxImmU16) {
|
|
|
|
assert(RemainingAdj && "OffsetFromTop is beyond FrameSize");
|
|
|
|
int OpImm = (RemainingAdj > MaxImmU16) ? MaxImmU16 : RemainingAdj;
|
|
|
|
int Opcode = isImmU6(OpImm) ? XCore::LDAWSP_ru6 : XCore::LDAWSP_lru6;
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(Opcode), XCore::SP).addImm(OpImm);
|
|
|
|
RemainingAdj -= OpImm;
|
|
|
|
}
|
|
|
|
}
|
2010-11-15 08:06:54 +08:00
|
|
|
|
2013-12-02 18:18:14 +08:00
|
|
|
/// Creates an ordered list of registers that are spilled
|
|
|
|
/// during the emitPrologue/emitEpilogue.
|
|
|
|
/// Registers are ordered according to their frame offset.
|
|
|
|
static void GetSpillList(SmallVectorImpl<std::pair<unsigned,int> > &SpillList,
|
|
|
|
MachineFrameInfo *MFI, XCoreFunctionInfo *XFI,
|
|
|
|
bool fetchLR, bool fetchFP) {
|
|
|
|
int LRSpillOffset = fetchLR? MFI->getObjectOffset(XFI->getLRSpillSlot()) : 0;
|
|
|
|
int FPSpillOffset = fetchFP? MFI->getObjectOffset(XFI->getFPSpillSlot()) : 0;
|
|
|
|
if (fetchLR && fetchFP && LRSpillOffset > FPSpillOffset) {
|
2013-12-02 18:29:26 +08:00
|
|
|
SpillList.push_back(std::pair<unsigned, int>(XCore::LR, LRSpillOffset));
|
2013-12-02 18:18:14 +08:00
|
|
|
fetchLR = false;
|
|
|
|
}
|
|
|
|
if (fetchFP)
|
2013-12-02 18:29:26 +08:00
|
|
|
SpillList.push_back(std::pair<unsigned, int>(FramePtr, FPSpillOffset));
|
2013-12-02 18:18:14 +08:00
|
|
|
if (fetchLR)
|
2013-12-02 18:29:26 +08:00
|
|
|
SpillList.push_back(std::pair<unsigned, int>(XCore::LR, LRSpillOffset));
|
2010-11-15 08:06:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-07 18:59:00 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2011-01-10 20:39:04 +08:00
|
|
|
// XCoreFrameLowering:
|
2008-11-07 18:59:00 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
XCoreFrameLowering::XCoreFrameLowering(const XCoreSubtarget &sti)
|
2012-06-07 03:47:08 +08:00
|
|
|
: TargetFrameLowering(TargetFrameLowering::StackGrowsDown, 4, 0) {
|
2008-11-07 18:59:00 +08:00
|
|
|
// Do nothing
|
|
|
|
}
|
2010-11-15 08:06:54 +08:00
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
bool XCoreFrameLowering::hasFP(const MachineFunction &MF) const {
|
2011-12-03 06:16:29 +08:00
|
|
|
return MF.getTarget().Options.DisableFramePointerElim(MF) ||
|
2013-12-02 18:18:14 +08:00
|
|
|
MF.getFrameInfo()->hasVarSizedObjects();
|
2010-11-19 05:19:35 +08:00
|
|
|
}
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
void XCoreFrameLowering::emitPrologue(MachineFunction &MF) const {
|
2010-11-15 08:06:54 +08:00
|
|
|
MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
|
|
|
|
MachineBasicBlock::iterator MBBI = MBB.begin();
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
MachineModuleInfo *MMI = &MF.getMMI();
|
2013-11-13 18:19:31 +08:00
|
|
|
const MCRegisterInfo *MRI = MMI->getContext().getRegisterInfo();
|
2010-11-15 08:06:54 +08:00
|
|
|
const XCoreInstrInfo &TII =
|
|
|
|
*static_cast<const XCoreInstrInfo*>(MF.getTarget().getInstrInfo());
|
|
|
|
XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
|
|
|
|
DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
|
|
|
|
|
2013-11-12 18:11:05 +08:00
|
|
|
if (MFI->getMaxAlignment() > getStackAlignment())
|
|
|
|
report_fatal_error("emitPrologue unsupported alignment: "
|
|
|
|
+ Twine(MFI->getMaxAlignment()));
|
|
|
|
|
2012-12-08 07:16:57 +08:00
|
|
|
const AttributeSet &PAL = MF.getFunction()->getAttributes();
|
2012-12-31 08:49:59 +08:00
|
|
|
if (PAL.hasAttrSomewhere(Attribute::Nest))
|
2013-12-02 18:18:14 +08:00
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(XCore::LDWSP_ru6), XCore::R11).addImm(0);
|
2010-11-15 08:06:54 +08:00
|
|
|
|
|
|
|
// Work out frame sizes.
|
2013-12-02 18:18:14 +08:00
|
|
|
// We will adjust the SP in stages towards the final FrameSize.
|
|
|
|
assert(MFI->getStackSize()%4 == 0 && "Misaligned frame size");
|
|
|
|
const int FrameSize = MFI->getStackSize() / 4;
|
|
|
|
int Adjusted = 0;
|
2010-11-15 08:06:54 +08:00
|
|
|
|
2013-05-10 00:43:42 +08:00
|
|
|
bool saveLR = XFI->getUsesLR();
|
2013-12-02 18:18:14 +08:00
|
|
|
bool UseENTSP = saveLR && FrameSize
|
|
|
|
&& (MFI->getObjectOffset(XFI->getLRSpillSlot()) == 0);
|
|
|
|
if (UseENTSP)
|
|
|
|
saveLR = false;
|
|
|
|
bool FP = hasFP(MF);
|
|
|
|
bool emitFrameMoves = XCoreRegisterInfo::needsFrameMoves(MF);
|
2010-11-15 08:06:54 +08:00
|
|
|
|
2013-12-02 18:18:14 +08:00
|
|
|
if (UseENTSP) {
|
|
|
|
// Allocate space on the stack at the same time as saving LR.
|
|
|
|
Adjusted = (FrameSize > MaxImmU16) ? MaxImmU16 : FrameSize;
|
|
|
|
int Opcode = isImmU6(Adjusted) ? XCore::ENTSP_u6 : XCore::ENTSP_lu6;
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(Opcode)).addImm(Adjusted);
|
|
|
|
MBB.addLiveIn(XCore::LR);
|
2010-11-15 08:06:54 +08:00
|
|
|
if (emitFrameMoves) {
|
2013-12-02 18:18:14 +08:00
|
|
|
EmitDefCfaOffset(MBB, MBBI, dl, TII, MMI, Adjusted*4);
|
|
|
|
unsigned DRegNum = MRI->getDwarfRegNum(XCore::LR, true);
|
|
|
|
EmitCfiOffset(MBB, MBBI, dl, TII, MMI, DRegNum, 0, NULL);
|
2010-11-15 08:06:54 +08:00
|
|
|
}
|
2013-05-10 00:43:42 +08:00
|
|
|
}
|
2010-11-15 08:06:54 +08:00
|
|
|
|
2013-12-02 18:18:14 +08:00
|
|
|
// If necessary, save LR and FP to the stack, as we EXTSP.
|
|
|
|
SmallVector<std::pair<unsigned,int>,2> SpillList;
|
|
|
|
GetSpillList(SpillList, MFI, XFI, saveLR, FP);
|
|
|
|
for (unsigned i = 0, e = SpillList.size(); i != e; ++i) {
|
|
|
|
unsigned SpillReg = SpillList[i].first;
|
|
|
|
int SpillOffset = SpillList[i].second;
|
|
|
|
assert(SpillOffset % 4 == 0 && "Misaligned stack offset");
|
|
|
|
assert(SpillOffset <= 0 && "Unexpected positive stack offset");
|
|
|
|
int OffsetFromTop = - SpillOffset/4;
|
|
|
|
IfNeededExtSP(MBB, MBBI, dl, TII, MMI, OffsetFromTop, Adjusted, FrameSize,
|
|
|
|
emitFrameMoves);
|
|
|
|
int Offset = Adjusted - OffsetFromTop;
|
|
|
|
int Opcode = isImmU6(Offset) ? XCore::STWSP_ru6 : XCore::STWSP_lru6;
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(Opcode)).addReg(SpillReg).addImm(Offset);
|
|
|
|
MBB.addLiveIn(SpillReg);
|
2013-05-10 00:43:42 +08:00
|
|
|
if (emitFrameMoves) {
|
2013-12-02 18:18:14 +08:00
|
|
|
unsigned DRegNum = MRI->getDwarfRegNum(SpillReg, true);
|
|
|
|
EmitCfiOffset(MBB, MBBI, dl, TII, MMI, DRegNum, SpillOffset, NULL);
|
2010-11-15 08:06:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-02 18:18:14 +08:00
|
|
|
// Complete any remaining Stack adjustment.
|
|
|
|
IfNeededExtSP(MBB, MBBI, dl, TII, MMI, FrameSize, Adjusted, FrameSize,
|
|
|
|
emitFrameMoves);
|
|
|
|
assert(Adjusted==FrameSize && "IfNeededExtSP has not completed adjustment");
|
|
|
|
|
2010-11-15 08:06:54 +08:00
|
|
|
if (FP) {
|
|
|
|
// Set the FP from the SP.
|
2013-11-13 18:19:31 +08:00
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(XCore::LDAWSP_ru6), FramePtr).addImm(0);
|
2013-12-02 18:18:14 +08:00
|
|
|
if (emitFrameMoves)
|
|
|
|
EmitDefCfaRegister(MBB, MBBI, dl, TII, MMI,
|
|
|
|
MRI->getDwarfRegNum(FramePtr, true));
|
2013-11-13 18:19:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (emitFrameMoves) {
|
|
|
|
// Frame moves for callee saved.
|
|
|
|
std::vector<std::pair<MCSymbol*, CalleeSavedInfo> >&SpillLabels =
|
|
|
|
XFI->getSpillLabels();
|
|
|
|
for (unsigned I = 0, E = SpillLabels.size(); I != E; ++I) {
|
|
|
|
MCSymbol *SpillLabel = SpillLabels[I].first;
|
|
|
|
CalleeSavedInfo &CSI = SpillLabels[I].second;
|
|
|
|
int Offset = MFI->getObjectOffset(CSI.getFrameIdx());
|
2013-12-02 18:18:14 +08:00
|
|
|
unsigned DRegNum = MRI->getDwarfRegNum(CSI.getReg(), true);
|
|
|
|
EmitCfiOffset(MBB, MBBI, dl, TII, MMI, DRegNum, Offset, SpillLabel);
|
2010-11-15 08:06:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
void XCoreFrameLowering::emitEpilogue(MachineFunction &MF,
|
2010-11-15 08:06:54 +08:00
|
|
|
MachineBasicBlock &MBB) const {
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
2011-01-14 06:47:43 +08:00
|
|
|
MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
|
2010-11-15 08:06:54 +08:00
|
|
|
const XCoreInstrInfo &TII =
|
|
|
|
*static_cast<const XCoreInstrInfo*>(MF.getTarget().getInstrInfo());
|
2013-05-10 00:43:42 +08:00
|
|
|
XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
|
2010-11-15 08:06:54 +08:00
|
|
|
DebugLoc dl = MBBI->getDebugLoc();
|
|
|
|
|
|
|
|
// Work out frame sizes.
|
2013-12-02 18:18:14 +08:00
|
|
|
// We will adjust the SP in stages towards the final FrameSize.
|
|
|
|
int RemainingAdj = MFI->getStackSize();
|
|
|
|
assert(RemainingAdj%4 == 0 && "Misaligned frame size");
|
|
|
|
RemainingAdj /= 4;
|
2010-11-15 08:06:54 +08:00
|
|
|
|
2013-05-10 00:43:42 +08:00
|
|
|
bool restoreLR = XFI->getUsesLR();
|
2013-12-02 18:18:14 +08:00
|
|
|
bool UseRETSP = restoreLR && RemainingAdj
|
|
|
|
&& (MFI->getObjectOffset(XFI->getLRSpillSlot()) == 0);
|
|
|
|
if (UseRETSP)
|
2013-05-10 00:43:42 +08:00
|
|
|
restoreLR = false;
|
2013-12-02 18:18:14 +08:00
|
|
|
bool FP = hasFP(MF);
|
|
|
|
|
|
|
|
if (FP) // Restore the stack pointer.
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(XCore::SETSP_1r)).addReg(FramePtr);
|
|
|
|
|
|
|
|
// If necessary, restore LR and FP from the stack, as we EXTSP.
|
|
|
|
SmallVector<std::pair<unsigned,int>,2> SpillList;
|
|
|
|
GetSpillList(SpillList, MFI, XFI, restoreLR, FP);
|
|
|
|
unsigned i = SpillList.size();
|
|
|
|
while (i--) {
|
|
|
|
unsigned SpilledReg = SpillList[i].first;
|
|
|
|
int SpillOffset = SpillList[i].second;
|
|
|
|
assert(SpillOffset % 4 == 0 && "Misaligned stack offset");
|
|
|
|
assert(SpillOffset <= 0 && "Unexpected positive stack offset");
|
|
|
|
int OffsetFromTop = - SpillOffset/4;
|
|
|
|
IfNeededLDAWSP(MBB, MBBI, dl, TII, OffsetFromTop, RemainingAdj);
|
|
|
|
int Offset = RemainingAdj - OffsetFromTop;
|
|
|
|
int Opcode = isImmU6(Offset) ? XCore::LDWSP_ru6 : XCore::LDWSP_lru6;
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(Opcode), SpilledReg).addImm(Offset);
|
2013-05-10 00:43:42 +08:00
|
|
|
}
|
|
|
|
|
2013-12-02 18:18:14 +08:00
|
|
|
if (RemainingAdj) {
|
|
|
|
// Complete all but one of the remaining Stack adjustments.
|
|
|
|
IfNeededLDAWSP(MBB, MBBI, dl, TII, 0, RemainingAdj);
|
|
|
|
if (UseRETSP) {
|
2010-11-15 08:06:54 +08:00
|
|
|
// Fold prologue into return instruction
|
|
|
|
assert(MBBI->getOpcode() == XCore::RETSP_u6
|
2013-12-02 18:18:14 +08:00
|
|
|
|| MBBI->getOpcode() == XCore::RETSP_lu6);
|
|
|
|
int Opcode = isImmU6(RemainingAdj) ? XCore::RETSP_u6 : XCore::RETSP_lu6;
|
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(Opcode))
|
|
|
|
.addImm(RemainingAdj);
|
2013-07-17 18:58:37 +08:00
|
|
|
for (unsigned i = 3, e = MBBI->getNumOperands(); i < e; ++i)
|
|
|
|
MIB->addOperand(MBBI->getOperand(i)); // copy any variadic operands
|
2013-12-02 18:18:14 +08:00
|
|
|
MBB.erase(MBBI); // Erase the previous return instruction.
|
2010-11-15 08:06:54 +08:00
|
|
|
} else {
|
2013-12-02 18:18:14 +08:00
|
|
|
int Opcode = isImmU6(RemainingAdj) ? XCore::LDAWSP_ru6 :
|
|
|
|
XCore::LDAWSP_lru6;
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(Opcode), XCore::SP).addImm(RemainingAdj);
|
|
|
|
// Don't erase the return instruction.
|
2010-11-15 08:06:54 +08:00
|
|
|
}
|
2013-12-02 18:18:14 +08:00
|
|
|
} // else Don't erase the return instruction.
|
2010-11-15 08:06:54 +08:00
|
|
|
}
|
2010-11-19 07:25:52 +08:00
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
bool XCoreFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
|
2010-11-28 07:05:03 +08:00
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI,
|
|
|
|
const TargetRegisterInfo *TRI) const {
|
|
|
|
if (CSI.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
MachineFunction *MF = MBB.getParent();
|
|
|
|
const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
|
|
|
|
|
|
|
|
XCoreFunctionInfo *XFI = MF->getInfo<XCoreFunctionInfo>();
|
|
|
|
bool emitFrameMoves = XCoreRegisterInfo::needsFrameMoves(*MF);
|
|
|
|
|
|
|
|
DebugLoc DL;
|
2013-12-02 18:18:14 +08:00
|
|
|
if (MI != MBB.end())
|
|
|
|
DL = MI->getDebugLoc();
|
2010-11-28 07:05:03 +08:00
|
|
|
|
|
|
|
for (std::vector<CalleeSavedInfo>::const_iterator it = CSI.begin();
|
|
|
|
it != CSI.end(); ++it) {
|
|
|
|
// Add the callee-saved register as live-in. It's killed at the spill.
|
|
|
|
MBB.addLiveIn(it->getReg());
|
|
|
|
|
|
|
|
unsigned Reg = it->getReg();
|
|
|
|
const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
|
|
|
|
TII.storeRegToStackSlot(MBB, MI, Reg, true,
|
|
|
|
it->getFrameIdx(), RC, TRI);
|
|
|
|
if (emitFrameMoves) {
|
|
|
|
MCSymbol *SaveLabel = MF->getContext().CreateTempSymbol();
|
|
|
|
BuildMI(MBB, MI, DL, TII.get(XCore::PROLOG_LABEL)).addSym(SaveLabel);
|
|
|
|
XFI->getSpillLabels().push_back(std::make_pair(SaveLabel, *it));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
bool XCoreFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
|
2010-11-28 07:05:03 +08:00
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI,
|
|
|
|
const TargetRegisterInfo *TRI) const{
|
|
|
|
MachineFunction *MF = MBB.getParent();
|
|
|
|
const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
|
|
|
|
|
|
|
|
bool AtStart = MI == MBB.begin();
|
|
|
|
MachineBasicBlock::iterator BeforeI = MI;
|
|
|
|
if (!AtStart)
|
|
|
|
--BeforeI;
|
|
|
|
for (std::vector<CalleeSavedInfo>::const_iterator it = CSI.begin();
|
|
|
|
it != CSI.end(); ++it) {
|
|
|
|
unsigned Reg = it->getReg();
|
|
|
|
const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
|
|
|
|
TII.loadRegFromStackSlot(MBB, MI, it->getReg(), it->getFrameIdx(),
|
|
|
|
RC, TRI);
|
|
|
|
assert(MI != MBB.begin() &&
|
|
|
|
"loadRegFromStackSlot didn't insert any code!");
|
|
|
|
// Insert in reverse order. loadRegFromStackSlot can insert multiple
|
|
|
|
// instructions.
|
|
|
|
if (AtStart)
|
|
|
|
MI = MBB.begin();
|
|
|
|
else {
|
|
|
|
MI = BeforeI;
|
|
|
|
++MI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2010-11-28 07:05:25 +08:00
|
|
|
|
2013-02-22 04:05:00 +08:00
|
|
|
// This function eliminates ADJCALLSTACKDOWN,
|
|
|
|
// ADJCALLSTACKUP pseudo instructions
|
|
|
|
void XCoreFrameLowering::
|
|
|
|
eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I) const {
|
|
|
|
const XCoreInstrInfo &TII =
|
|
|
|
*static_cast<const XCoreInstrInfo*>(MF.getTarget().getInstrInfo());
|
|
|
|
if (!hasReservedCallFrame(MF)) {
|
|
|
|
// Turn the adjcallstackdown instruction into 'extsp <amt>' and the
|
|
|
|
// adjcallstackup instruction into 'ldaw sp, sp[<amt>]'
|
|
|
|
MachineInstr *Old = I;
|
|
|
|
uint64_t Amount = Old->getOperand(0).getImm();
|
|
|
|
if (Amount != 0) {
|
|
|
|
// We need to keep the stack aligned properly. To do this, we round the
|
|
|
|
// amount of space needed for the outgoing arguments up to the next
|
|
|
|
// alignment boundary.
|
|
|
|
unsigned Align = getStackAlignment();
|
|
|
|
Amount = (Amount+Align-1)/Align*Align;
|
|
|
|
|
|
|
|
assert(Amount%4 == 0);
|
|
|
|
Amount /= 4;
|
|
|
|
|
|
|
|
bool isU6 = isImmU6(Amount);
|
|
|
|
if (!isU6 && !isImmU16(Amount)) {
|
|
|
|
// FIX could emit multiple instructions in this case.
|
|
|
|
#ifndef NDEBUG
|
|
|
|
errs() << "eliminateCallFramePseudoInstr size too big: "
|
|
|
|
<< Amount << "\n";
|
|
|
|
#endif
|
|
|
|
llvm_unreachable(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr *New;
|
|
|
|
if (Old->getOpcode() == XCore::ADJCALLSTACKDOWN) {
|
|
|
|
int Opcode = isU6 ? XCore::EXTSP_u6 : XCore::EXTSP_lu6;
|
|
|
|
New=BuildMI(MF, Old->getDebugLoc(), TII.get(Opcode))
|
|
|
|
.addImm(Amount);
|
|
|
|
} else {
|
|
|
|
assert(Old->getOpcode() == XCore::ADJCALLSTACKUP);
|
2013-04-05 03:57:46 +08:00
|
|
|
int Opcode = isU6 ? XCore::LDAWSP_ru6 : XCore::LDAWSP_lru6;
|
2013-02-22 04:05:00 +08:00
|
|
|
New=BuildMI(MF, Old->getDebugLoc(), TII.get(Opcode), XCore::SP)
|
|
|
|
.addImm(Amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the pseudo instruction with a new instruction...
|
|
|
|
MBB.insert(I, New);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MBB.erase(I);
|
|
|
|
}
|
|
|
|
|
2010-11-28 07:05:25 +08:00
|
|
|
void
|
2011-01-10 20:39:04 +08:00
|
|
|
XCoreFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
|
2010-11-28 07:05:25 +08:00
|
|
|
RegScavenger *RS) const {
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
|
|
|
|
bool LRUsed = MF.getRegInfo().isPhysRegUsed(XCore::LR);
|
2012-04-20 15:30:17 +08:00
|
|
|
const TargetRegisterClass *RC = &XCore::GRRegsRegClass;
|
2010-11-28 07:05:25 +08:00
|
|
|
XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
|
|
|
|
if (LRUsed) {
|
|
|
|
MF.getRegInfo().setPhysRegUnused(XCore::LR);
|
|
|
|
|
|
|
|
bool isVarArg = MF.getFunction()->isVarArg();
|
|
|
|
int FrameIdx;
|
|
|
|
if (! isVarArg) {
|
|
|
|
// A fixed offset of 0 allows us to save / restore LR using entsp / retsp.
|
|
|
|
FrameIdx = MFI->CreateFixedObject(RC->getSize(), 0, true);
|
|
|
|
} else {
|
|
|
|
FrameIdx = MFI->CreateStackObject(RC->getSize(), RC->getAlignment(),
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
XFI->setUsesLR(FrameIdx);
|
|
|
|
XFI->setLRSpillSlot(FrameIdx);
|
|
|
|
}
|
|
|
|
if (RegInfo->requiresRegisterScavenging(MF)) {
|
|
|
|
// Reserve a slot close to SP or frame pointer.
|
2013-03-23 07:32:27 +08:00
|
|
|
RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
|
2010-11-28 07:05:25 +08:00
|
|
|
RC->getAlignment(),
|
|
|
|
false));
|
|
|
|
}
|
|
|
|
if (hasFP(MF)) {
|
|
|
|
// A callee save register is used to hold the FP.
|
|
|
|
// This needs saving / restoring in the epilogue / prologue.
|
|
|
|
XFI->setFPSpillSlot(MFI->CreateStackObject(RC->getSize(),
|
|
|
|
RC->getAlignment(),
|
|
|
|
false));
|
|
|
|
}
|
|
|
|
}
|