Skip to content

Commit

Permalink
Add "Restored" flag to CalleeSavedInfo
Browse files Browse the repository at this point in the history
The liveness-tracking code assumes that the registers that were saved
in the function's prolog are live outside of the function. Specifically,
that registers that were saved are also live-on-exit from the function.
This isn't always the case as illustrated by the LR register on ARM.

Differential Revision: https://reviews.llvm.org/D36160

llvm-svn: 310619
  • Loading branch information
Krzysztof Parzyszek committed Aug 10, 2017
1 parent b88de41 commit bea30c6
Show file tree
Hide file tree
Showing 25 changed files with 60 additions and 31 deletions.
19 changes: 18 additions & 1 deletion llvm/include/llvm/CodeGen/MachineFrameInfo.h
Expand Up @@ -31,15 +31,30 @@ class AllocaInst;
class CalleeSavedInfo {
unsigned Reg;
int FrameIdx;
/// Flag indicating whether the register is actually restored in the epilog.
/// In most cases, if a register is saved, it is also restored. There are
/// some situations, though, when this is not the case. For example, the
/// LR register on ARM is usually saved, but on exit from the function its
/// saved value may be loaded directly into PC. Since liveness tracking of
/// physical registers treats callee-saved registers are live outside of
/// the function, LR would be treated as live-on-exit, even though in these
/// scenarios it is not. This flag is added to indicate that the saved
/// register described by this object is not restored in the epilog.
/// The long-term solution is to model the liveness of callee-saved registers
/// by implicit uses on the return instructions, however, the required
/// changes in the ARM backend would be quite extensive.
bool Restored;

public:
explicit CalleeSavedInfo(unsigned R, int FI = 0)
: Reg(R), FrameIdx(FI) {}
: Reg(R), FrameIdx(FI), Restored(true) {}

// Accessors.
unsigned getReg() const { return Reg; }
int getFrameIdx() const { return FrameIdx; }
void setFrameIdx(int FI) { FrameIdx = FI; }
bool isRestored() const { return Restored; }
void setRestored(bool R) { Restored = R; }
};

/// The MachineFrameInfo class represents an abstract stack frame until
Expand Down Expand Up @@ -667,6 +682,8 @@ class MachineFrameInfo {
const std::vector<CalleeSavedInfo> &getCalleeSavedInfo() const {
return CSInfo;
}
/// \copydoc getCalleeSavedInfo()
std::vector<CalleeSavedInfo> &getCalleeSavedInfo() { return CSInfo; }

/// Used by prolog/epilog inserter to set the function's callee saved
/// information.
Expand Down
4 changes: 3 additions & 1 deletion llvm/include/llvm/Target/TargetFrameLowering.h
Expand Up @@ -193,10 +193,12 @@ class TargetFrameLowering {
/// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
/// saved registers and returns true if it isn't possible / profitable to do
/// so by issuing a series of load instructions via loadRegToStackSlot().
/// If it returns true, and any of the registers in CSI is not restored,
/// it sets the corresponding Restored flag in CSI to false.
/// Returns false otherwise.
virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const {
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/CodeGen/LivePhysRegs.cpp
Expand Up @@ -192,7 +192,8 @@ void LivePhysRegs::addLiveOutsNoPristines(const MachineBasicBlock &MBB) {
const MachineFrameInfo &MFI = MF.getFrameInfo();
if (MFI.isCalleeSavedInfoValid()) {
for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
addReg(Info.getReg());
if (Info.isRestored())
addReg(Info.getReg());
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/CodeGen/PrologEpilogInserter.cpp
Expand Up @@ -489,7 +489,7 @@ static void insertCSRSaves(MachineBasicBlock &SaveBlock,

/// Insert restore code for the callee-saved registers used in the function.
static void insertCSRRestores(MachineBasicBlock &RestoreBlock,
ArrayRef<CalleeSavedInfo> CSI) {
std::vector<CalleeSavedInfo> &CSI) {
MachineFunction &Fn = *RestoreBlock.getParent();
const TargetInstrInfo &TII = *Fn.getSubtarget().getInstrInfo();
const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering();
Expand Down Expand Up @@ -534,7 +534,7 @@ static void doSpillCalleeSavedRegs(MachineFunction &Fn, RegScavenger *RS,
if (!F->hasFnAttribute(Attribute::Naked)) {
MFI.setCalleeSavedInfoValid(true);

ArrayRef<CalleeSavedInfo> CSI = MFI.getCalleeSavedInfo();
std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
if (!CSI.empty()) {
for (MachineBasicBlock *SaveBlock : SaveBlocks) {
insertCSRSaves(*SaveBlock, CSI);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
Expand Up @@ -1092,7 +1092,7 @@ bool AArch64FrameLowering::spillCalleeSavedRegisters(

bool AArch64FrameLowering::restoreCalleeSavedRegisters(
MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const {
MachineFunction &MF = *MBB.getParent();
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AArch64/AArch64FrameLowering.h
Expand Up @@ -50,7 +50,7 @@ class AArch64FrameLowering : public TargetFrameLowering {

bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const override;

/// \brief Can this function use the red zone for local allocations.
Expand Down
19 changes: 14 additions & 5 deletions llvm/lib/Target/ARM/ARMFrameLowering.cpp
Expand Up @@ -1019,7 +1019,7 @@ void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,

void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
unsigned LdmOpc, unsigned LdrOpc,
bool isVarArg, bool NoGap,
bool(*Func)(unsigned, bool),
Expand Down Expand Up @@ -1090,9 +1090,18 @@ void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
.add(predOps(ARMCC::AL));
for (unsigned i = 0, e = Regs.size(); i < e; ++i)
MIB.addReg(Regs[i], getDefRegState(true));
if (DeleteRet && MI != MBB.end()) {
MIB.copyImplicitOps(*MI);
MI->eraseFromParent();
if (DeleteRet) {
if (MI != MBB.end()) {
MIB.copyImplicitOps(*MI);
MI->eraseFromParent();
}
// If LR is not restored, mark it in CSI.
for (CalleeSavedInfo &I : CSI) {
if (I.getReg() != ARM::LR)
continue;
I.setRestored(false);
break;
}
}
MI = MIB;
} else if (Regs.size() == 1) {
Expand Down Expand Up @@ -1421,7 +1430,7 @@ bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,

bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const {
if (CSI.empty())
return false;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/ARM/ARMFrameLowering.h
Expand Up @@ -38,7 +38,7 @@ class ARMFrameLowering : public TargetFrameLowering {

bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const override;

bool noFramePointerElim(const MachineFunction &MF) const override;
Expand Down Expand Up @@ -69,7 +69,7 @@ class ARMFrameLowering : public TargetFrameLowering {
bool(*Func)(unsigned, bool), unsigned NumAlignedDPRCS2Regs,
unsigned MIFlags = 0) const;
void emitPopInst(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI, unsigned LdmOpc,
std::vector<CalleeSavedInfo> &CSI, unsigned LdmOpc,
unsigned LdrOpc, bool isVarArg, bool NoGap,
bool(*Func)(unsigned, bool),
unsigned NumAlignedDPRCS2Regs) const;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/ARM/Thumb1FrameLowering.cpp
Expand Up @@ -780,7 +780,7 @@ spillCalleeSavedRegisters(MachineBasicBlock &MBB,
bool Thumb1FrameLowering::
restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const {
if (CSI.empty())
return false;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/ARM/Thumb1FrameLowering.h
Expand Up @@ -36,7 +36,7 @@ class Thumb1FrameLowering : public ARMFrameLowering {
const TargetRegisterInfo *TRI) const override;
bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const override;

bool hasReservedCallFrame(const MachineFunction &MF) const override;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AVR/AVRFrameLowering.cpp
Expand Up @@ -275,7 +275,7 @@ bool AVRFrameLowering::spillCalleeSavedRegisters(

bool AVRFrameLowering::restoreCalleeSavedRegisters(
MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const {
if (CSI.empty()) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AVR/AVRFrameLowering.h
Expand Up @@ -30,7 +30,7 @@ class AVRFrameLowering : public TargetFrameLowering {
bool
restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const override;
bool hasReservedCallFrame(const MachineFunction &MF) const override;
bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/Hexagon/HexagonFrameLowering.h
Expand Up @@ -48,7 +48,7 @@ class HexagonFrameLowering : public TargetFrameLowering {
}

bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI, const std::vector<CalleeSavedInfo> &CSI,
MachineBasicBlock::iterator MI, std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const override {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/MSP430/MSP430FrameLowering.cpp
Expand Up @@ -206,7 +206,7 @@ MSP430FrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
bool
MSP430FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const {
if (CSI.empty())
return false;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/MSP430/MSP430FrameLowering.h
Expand Up @@ -40,7 +40,7 @@ class MSP430FrameLowering : public TargetFrameLowering {
const TargetRegisterInfo *TRI) const override;
bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const override;

bool hasFP(const MachineFunction &MF) const override;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/Mips/Mips16FrameLowering.cpp
Expand Up @@ -143,7 +143,7 @@ spillCalleeSavedRegisters(MachineBasicBlock &MBB,

bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const {
//
// Registers RA,S0,S1 are the callee saved registers and they will be restored
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/Mips/Mips16FrameLowering.h
Expand Up @@ -33,7 +33,7 @@ class Mips16FrameLowering : public MipsFrameLowering {

bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const override;

bool hasReservedCallFrame(const MachineFunction &MF) const override;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/PowerPC/PPCFrameLowering.cpp
Expand Up @@ -2067,7 +2067,7 @@ eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
bool
PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const {

// Currently, this function only handles SVR4 32- and 64-bit ABIs.
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/PowerPC/PPCFrameLowering.h
Expand Up @@ -106,7 +106,7 @@ class PPCFrameLowering: public TargetFrameLowering {

bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const override;

/// targetHandlesStackFrameRounding - Returns true if the target is
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp
Expand Up @@ -220,7 +220,7 @@ spillCalleeSavedRegisters(MachineBasicBlock &MBB,
bool SystemZFrameLowering::
restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const {
if (CSI.empty())
return false;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/SystemZ/SystemZFrameLowering.h
Expand Up @@ -35,7 +35,7 @@ class SystemZFrameLowering : public TargetFrameLowering {
const TargetRegisterInfo *TRI) const override;
bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBII,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const
override;
void processFunctionBeforeFrameFinalized(MachineFunction &MF,
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/X86/X86FrameLowering.cpp
Expand Up @@ -1999,7 +1999,7 @@ bool X86FrameLowering::spillCalleeSavedRegisters(

bool X86FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const {
if (CSI.empty())
return false;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/X86/X86FrameLowering.h
Expand Up @@ -89,7 +89,7 @@ class X86FrameLowering : public TargetFrameLowering {

bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const override;

bool hasFP(const MachineFunction &MF) const override;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/XCore/XCoreFrameLowering.cpp
Expand Up @@ -452,7 +452,7 @@ spillCalleeSavedRegisters(MachineBasicBlock &MBB,
bool XCoreFrameLowering::
restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const{
MachineFunction *MF = MBB.getParent();
const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/XCore/XCoreFrameLowering.h
Expand Up @@ -38,7 +38,7 @@ namespace llvm {
const TargetRegisterInfo *TRI) const override;
bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI,
const std::vector<CalleeSavedInfo> &CSI,
std::vector<CalleeSavedInfo> &CSI,
const TargetRegisterInfo *TRI) const override;

MachineBasicBlock::iterator
Expand Down

0 comments on commit bea30c6

Please sign in to comment.