2012-02-18 20:03:15 +08:00
|
|
|
//===-- HexagonFrameLowering.cpp - Define frame lowering ------------------===//
|
2011-12-13 05:14:40 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-02-06 18:19:29 +08:00
|
|
|
|
2012-03-18 02:46:09 +08:00
|
|
|
#include "HexagonFrameLowering.h"
|
2011-12-13 05:14:40 +08:00
|
|
|
#include "Hexagon.h"
|
|
|
|
#include "HexagonInstrInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "HexagonMachineFunctionInfo.h"
|
2011-12-13 05:14:40 +08:00
|
|
|
#include "HexagonRegisterInfo.h"
|
|
|
|
#include "HexagonSubtarget.h"
|
|
|
|
#include "HexagonTargetMachine.h"
|
2012-02-06 18:19:29 +08:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2011-12-13 05:14:40 +08:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2011-12-13 05:14:40 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2012-02-06 18:19:29 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2012-02-06 18:19:29 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2011-12-13 05:14:40 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2012-02-06 18:19:29 +08:00
|
|
|
#include "llvm/CodeGen/RegisterScavenging.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2011-12-13 05:14:40 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/MC/MachineLocation.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2011-12-13 05:14:40 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<bool> DisableDeallocRet(
|
|
|
|
"disable-hexagon-dealloc-ret",
|
|
|
|
cl::Hidden,
|
|
|
|
cl::desc("Disable Dealloc Return for Hexagon target"));
|
|
|
|
|
|
|
|
/// determineFrameLayout - Determine the size of the frame and maximum call
|
|
|
|
/// frame size.
|
|
|
|
void HexagonFrameLowering::determineFrameLayout(MachineFunction &MF) const {
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
|
|
|
|
// Get the number of bytes to allocate from the FrameInfo.
|
|
|
|
unsigned FrameSize = MFI->getStackSize();
|
|
|
|
|
|
|
|
// Get the alignments provided by the target.
|
2014-08-05 05:25:23 +08:00
|
|
|
unsigned TargetAlign = MF.getTarget()
|
|
|
|
.getSubtargetImpl()
|
|
|
|
->getFrameLowering()
|
|
|
|
->getStackAlignment();
|
2011-12-13 05:14:40 +08:00
|
|
|
// Get the maximum call frame size of all the calls.
|
|
|
|
unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
|
|
|
|
|
|
|
|
// If we have dynamic alloca then maxCallFrameSize needs to be aligned so
|
|
|
|
// that allocations will be aligned.
|
|
|
|
if (MFI->hasVarSizedObjects())
|
|
|
|
maxCallFrameSize = RoundUpToAlignment(maxCallFrameSize, TargetAlign);
|
|
|
|
|
|
|
|
// Update maximum call frame size.
|
|
|
|
MFI->setMaxCallFrameSize(maxCallFrameSize);
|
|
|
|
|
|
|
|
// Include call frame size in total.
|
|
|
|
FrameSize += maxCallFrameSize;
|
|
|
|
|
|
|
|
// Make sure the frame is aligned.
|
|
|
|
FrameSize = RoundUpToAlignment(FrameSize, TargetAlign);
|
|
|
|
|
|
|
|
// Update frame info.
|
|
|
|
MFI->setStackSize(FrameSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HexagonFrameLowering::emitPrologue(MachineFunction &MF) const {
|
|
|
|
MachineBasicBlock &MBB = MF.front();
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
MachineBasicBlock::iterator MBBI = MBB.begin();
|
2014-08-05 05:25:23 +08:00
|
|
|
const HexagonRegisterInfo *QRI = static_cast<const HexagonRegisterInfo *>(
|
2014-08-05 10:39:49 +08:00
|
|
|
MF.getSubtarget().getRegisterInfo());
|
2011-12-13 05:14:40 +08:00
|
|
|
DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
|
|
|
|
determineFrameLayout(MF);
|
|
|
|
|
|
|
|
// Get the number of bytes to allocate from the FrameInfo.
|
|
|
|
int NumBytes = (int) MFI->getStackSize();
|
|
|
|
|
|
|
|
// LLVM expects allocframe not to be the first instruction in the
|
|
|
|
// basic block.
|
|
|
|
MachineBasicBlock::iterator InsertPt = MBB.begin();
|
|
|
|
|
|
|
|
//
|
|
|
|
// ALLOCA adjust regs. Iterate over ADJDYNALLOC nodes and change the offset.
|
|
|
|
//
|
|
|
|
HexagonMachineFunctionInfo *FuncInfo =
|
|
|
|
MF.getInfo<HexagonMachineFunctionInfo>();
|
|
|
|
const std::vector<MachineInstr*>& AdjustRegs =
|
|
|
|
FuncInfo->getAllocaAdjustInsts();
|
|
|
|
for (std::vector<MachineInstr*>::const_iterator i = AdjustRegs.begin(),
|
|
|
|
e = AdjustRegs.end();
|
|
|
|
i != e; ++i) {
|
|
|
|
MachineInstr* MI = *i;
|
|
|
|
assert((MI->getOpcode() == Hexagon::ADJDYNALLOC) &&
|
|
|
|
"Expected adjust alloca node");
|
|
|
|
|
|
|
|
MachineOperand& MO = MI->getOperand(2);
|
|
|
|
assert(MO.isImm() && "Expected immediate");
|
|
|
|
MO.setImm(MFI->getMaxCallFrameSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Only insert ALLOCFRAME if we need to.
|
|
|
|
//
|
|
|
|
if (hasFP(MF)) {
|
|
|
|
// Check for overflow.
|
|
|
|
// Hexagon_TODO: Ugh! hardcoding. Is there an API that can be used?
|
2011-12-13 05:52:59 +08:00
|
|
|
const int ALLOCFRAME_MAX = 16384;
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
|
2011-12-13 05:14:40 +08:00
|
|
|
|
|
|
|
if (NumBytes >= ALLOCFRAME_MAX) {
|
|
|
|
// Emit allocframe(#0).
|
|
|
|
BuildMI(MBB, InsertPt, dl, TII.get(Hexagon::ALLOCFRAME)).addImm(0);
|
|
|
|
|
|
|
|
// Subtract offset from frame pointer.
|
|
|
|
BuildMI(MBB, InsertPt, dl, TII.get(Hexagon::CONST32_Int_Real),
|
|
|
|
HEXAGON_RESERVED_REG_1).addImm(NumBytes);
|
|
|
|
BuildMI(MBB, InsertPt, dl, TII.get(Hexagon::SUB_rr),
|
|
|
|
QRI->getStackRegister()).
|
|
|
|
addReg(QRI->getStackRegister()).
|
|
|
|
addReg(HEXAGON_RESERVED_REG_1);
|
|
|
|
} else {
|
|
|
|
BuildMI(MBB, InsertPt, dl, TII.get(Hexagon::ALLOCFRAME)).addImm(NumBytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Returns true if MBB has a machine instructions that indicates a tail call
|
|
|
|
// in the block.
|
|
|
|
bool HexagonFrameLowering::hasTailCall(MachineBasicBlock &MBB) const {
|
|
|
|
MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
|
|
|
|
unsigned RetOpcode = MBBI->getOpcode();
|
|
|
|
|
2012-12-06 03:00:34 +08:00
|
|
|
return RetOpcode == Hexagon::TCRETURNtg || RetOpcode == Hexagon::TCRETURNtext;
|
|
|
|
}
|
2011-12-13 05:14:40 +08:00
|
|
|
|
|
|
|
void HexagonFrameLowering::emitEpilogue(MachineFunction &MF,
|
|
|
|
MachineBasicBlock &MBB) const {
|
2014-03-02 20:27:27 +08:00
|
|
|
MachineBasicBlock::iterator MBBI = std::prev(MBB.end());
|
2011-12-13 05:14:40 +08:00
|
|
|
DebugLoc dl = MBBI->getDebugLoc();
|
|
|
|
//
|
2013-05-11 04:27:34 +08:00
|
|
|
// Only insert deallocframe if we need to. Also at -O0. See comment
|
|
|
|
// in emitPrologue above.
|
2011-12-13 05:14:40 +08:00
|
|
|
//
|
2013-05-11 04:27:34 +08:00
|
|
|
if (hasFP(MF) || MF.getTarget().getOptLevel() == CodeGenOpt::None) {
|
2014-03-02 20:27:27 +08:00
|
|
|
MachineBasicBlock::iterator MBBI = std::prev(MBB.end());
|
2011-12-13 05:14:40 +08:00
|
|
|
MachineBasicBlock::iterator MBBI_end = MBB.end();
|
|
|
|
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
|
2013-05-11 04:27:34 +08:00
|
|
|
// Handle EH_RETURN.
|
|
|
|
if (MBBI->getOpcode() == Hexagon::EH_RETURN_JMPR) {
|
2013-05-11 05:44:02 +08:00
|
|
|
assert(MBBI->getOperand(0).isReg() && "Offset should be in register!");
|
2013-05-11 04:27:34 +08:00
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(Hexagon::DEALLOCFRAME));
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(Hexagon::ADD_rr),
|
|
|
|
Hexagon::R29).addReg(Hexagon::R29).addReg(Hexagon::R28);
|
|
|
|
return;
|
|
|
|
}
|
2011-12-13 05:14:40 +08:00
|
|
|
// Replace 'jumpr r31' instruction with dealloc_return for V4 and higher
|
|
|
|
// versions.
|
2014-06-27 08:13:47 +08:00
|
|
|
if (MF.getTarget().getSubtarget<HexagonSubtarget>().hasV4TOps() &&
|
|
|
|
MBBI->getOpcode() == Hexagon::JMPret && !DisableDeallocRet) {
|
2013-05-11 04:27:34 +08:00
|
|
|
// Check for RESTORE_DEALLOC_RET_JMP_V4 call. Don't emit an extra DEALLOC
|
|
|
|
// instruction if we encounter it.
|
|
|
|
MachineBasicBlock::iterator BeforeJMPR =
|
2014-03-02 20:27:27 +08:00
|
|
|
MBB.begin() == MBBI ? MBBI : std::prev(MBBI);
|
2013-05-11 04:27:34 +08:00
|
|
|
if (BeforeJMPR != MBBI &&
|
|
|
|
BeforeJMPR->getOpcode() == Hexagon::RESTORE_DEALLOC_RET_JMP_V4) {
|
|
|
|
// Remove the JMPR node.
|
|
|
|
MBB.erase(MBBI);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-13 05:14:40 +08:00
|
|
|
// Add dealloc_return.
|
2013-05-11 04:27:34 +08:00
|
|
|
MachineInstrBuilder MIB =
|
|
|
|
BuildMI(MBB, MBBI_end, dl, TII.get(Hexagon::DEALLOC_RET_V4));
|
|
|
|
// Transfer the function live-out registers.
|
|
|
|
MIB->copyImplicitOps(*MBB.getParent(), &*MBBI);
|
|
|
|
// Remove the JUMPR node.
|
|
|
|
MBB.erase(MBBI);
|
|
|
|
} else { // Add deallocframe for V2 and V3, and V4 tail calls.
|
|
|
|
// Check for RESTORE_DEALLOC_BEFORE_TAILCALL_V4. We don't need an extra
|
|
|
|
// DEALLOCFRAME instruction after it.
|
|
|
|
MachineBasicBlock::iterator Term = MBB.getFirstTerminator();
|
|
|
|
MachineBasicBlock::iterator I =
|
2014-03-02 20:27:27 +08:00
|
|
|
Term == MBB.begin() ? MBB.end() : std::prev(Term);
|
2013-05-11 04:27:34 +08:00
|
|
|
if (I != MBB.end() &&
|
|
|
|
I->getOpcode() == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4)
|
|
|
|
return;
|
|
|
|
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(Hexagon::DEALLOCFRAME));
|
2011-12-13 05:14:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HexagonFrameLowering::hasFP(const MachineFunction &MF) const {
|
|
|
|
const MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
const HexagonMachineFunctionInfo *FuncInfo =
|
|
|
|
MF.getInfo<HexagonMachineFunctionInfo>();
|
|
|
|
return (MFI->hasCalls() || (MFI->getStackSize() > 0) ||
|
|
|
|
FuncInfo->hasClobberLR() );
|
|
|
|
}
|
|
|
|
|
2012-05-31 06:40:03 +08:00
|
|
|
static inline
|
|
|
|
unsigned uniqueSuperReg(unsigned Reg, const TargetRegisterInfo *TRI) {
|
|
|
|
MCSuperRegIterator SRI(Reg, TRI);
|
|
|
|
assert(SRI.isValid() && "Expected a superreg");
|
|
|
|
unsigned SuperReg = *SRI;
|
|
|
|
++SRI;
|
|
|
|
assert(!SRI.isValid() && "Expected exactly one superreg");
|
|
|
|
return SuperReg;
|
|
|
|
}
|
|
|
|
|
2011-12-13 05:14:40 +08:00
|
|
|
bool
|
|
|
|
HexagonFrameLowering::spillCalleeSavedRegisters(
|
|
|
|
MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI,
|
|
|
|
const TargetRegisterInfo *TRI) const {
|
|
|
|
MachineFunction *MF = MBB.getParent();
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
|
2011-12-13 05:14:40 +08:00
|
|
|
|
|
|
|
if (CSI.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can only schedule double loads if we spill contiguous callee-saved regs
|
|
|
|
// For instance, we cannot scheduled double-word loads if we spill r24,
|
|
|
|
// r26, and r27.
|
|
|
|
// Hexagon_TODO: We can try to double-word align odd registers for -O2 and
|
|
|
|
// above.
|
|
|
|
bool ContiguousRegs = true;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < CSI.size(); ++i) {
|
|
|
|
unsigned Reg = CSI[i].getReg();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Check if we can use a double-word store.
|
|
|
|
//
|
2012-05-31 06:40:03 +08:00
|
|
|
unsigned SuperReg = uniqueSuperReg(Reg, TRI);
|
2011-12-13 05:14:40 +08:00
|
|
|
bool CanUseDblStore = false;
|
2014-04-25 13:30:21 +08:00
|
|
|
const TargetRegisterClass* SuperRegClass = nullptr;
|
2011-12-13 05:14:40 +08:00
|
|
|
|
|
|
|
if (ContiguousRegs && (i < CSI.size()-1)) {
|
2012-05-31 06:40:03 +08:00
|
|
|
unsigned SuperRegNext = uniqueSuperReg(CSI[i+1].getReg(), TRI);
|
|
|
|
SuperRegClass = TRI->getMinimalPhysRegClass(SuperReg);
|
|
|
|
CanUseDblStore = (SuperRegNext == SuperReg);
|
2011-12-13 05:14:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (CanUseDblStore) {
|
2012-05-31 06:40:03 +08:00
|
|
|
TII.storeRegToStackSlot(MBB, MI, SuperReg, true,
|
2011-12-13 05:14:40 +08:00
|
|
|
CSI[i+1].getFrameIdx(), SuperRegClass, TRI);
|
2012-05-31 06:40:03 +08:00
|
|
|
MBB.addLiveIn(SuperReg);
|
2011-12-13 05:14:40 +08:00
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
// Cannot use a double-word store.
|
|
|
|
ContiguousRegs = false;
|
|
|
|
const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
|
|
|
|
TII.storeRegToStackSlot(MBB, MI, Reg, true, CSI[i].getFrameIdx(), RC,
|
|
|
|
TRI);
|
|
|
|
MBB.addLiveIn(Reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool HexagonFrameLowering::restoreCalleeSavedRegisters(
|
|
|
|
MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI,
|
|
|
|
const TargetRegisterInfo *TRI) const {
|
|
|
|
|
|
|
|
MachineFunction *MF = MBB.getParent();
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
|
2011-12-13 05:14:40 +08:00
|
|
|
|
|
|
|
if (CSI.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can only schedule double loads if we spill contiguous callee-saved regs
|
|
|
|
// For instance, we cannot scheduled double-word loads if we spill r24,
|
|
|
|
// r26, and r27.
|
|
|
|
// Hexagon_TODO: We can try to double-word align odd registers for -O2 and
|
|
|
|
// above.
|
|
|
|
bool ContiguousRegs = true;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < CSI.size(); ++i) {
|
|
|
|
unsigned Reg = CSI[i].getReg();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Check if we can use a double-word load.
|
|
|
|
//
|
2012-05-31 06:40:03 +08:00
|
|
|
unsigned SuperReg = uniqueSuperReg(Reg, TRI);
|
2014-04-25 13:30:21 +08:00
|
|
|
const TargetRegisterClass* SuperRegClass = nullptr;
|
2011-12-13 05:14:40 +08:00
|
|
|
bool CanUseDblLoad = false;
|
|
|
|
if (ContiguousRegs && (i < CSI.size()-1)) {
|
2012-05-31 06:40:03 +08:00
|
|
|
unsigned SuperRegNext = uniqueSuperReg(CSI[i+1].getReg(), TRI);
|
|
|
|
SuperRegClass = TRI->getMinimalPhysRegClass(SuperReg);
|
|
|
|
CanUseDblLoad = (SuperRegNext == SuperReg);
|
2011-12-13 05:14:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (CanUseDblLoad) {
|
2012-05-31 06:40:03 +08:00
|
|
|
TII.loadRegFromStackSlot(MBB, MI, SuperReg, CSI[i+1].getFrameIdx(),
|
2011-12-13 05:14:40 +08:00
|
|
|
SuperRegClass, TRI);
|
2012-05-31 06:40:03 +08:00
|
|
|
MBB.addLiveIn(SuperReg);
|
2011-12-13 05:14:40 +08:00
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
// Cannot use a double-word load.
|
|
|
|
ContiguousRegs = false;
|
|
|
|
const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
|
|
|
|
TII.loadRegFromStackSlot(MBB, MI, Reg, CSI[i].getFrameIdx(), RC, TRI);
|
|
|
|
MBB.addLiveIn(Reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-22 04:05:00 +08:00
|
|
|
void HexagonFrameLowering::
|
|
|
|
eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I) const {
|
|
|
|
MachineInstr &MI = *I;
|
|
|
|
|
|
|
|
if (MI.getOpcode() == Hexagon::ADJCALLSTACKDOWN) {
|
|
|
|
// Hexagon_TODO: add code
|
|
|
|
} else if (MI.getOpcode() == Hexagon::ADJCALLSTACKUP) {
|
|
|
|
// Hexagon_TODO: add code
|
|
|
|
} else {
|
|
|
|
llvm_unreachable("Cannot handle this call frame pseudo instruction");
|
|
|
|
}
|
|
|
|
MBB.erase(I);
|
|
|
|
}
|
|
|
|
|
2011-12-13 05:14:40 +08:00
|
|
|
int HexagonFrameLowering::getFrameIndexOffset(const MachineFunction &MF,
|
|
|
|
int FI) const {
|
|
|
|
return MF.getFrameInfo()->getObjectOffset(FI);
|
|
|
|
}
|