2012-02-18 20:03:15 +08:00
|
|
|
//===-- MSP430FrameLowering.cpp - MSP430 Frame Information ----------------===//
|
2010-11-15 08:06:54 +08: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 20:39:04 +08:00
|
|
|
// This file contains the MSP430 implementation of TargetFrameLowering class.
|
2010-11-15 08:06:54 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
#include "MSP430FrameLowering.h"
|
2010-11-15 08:06:54 +08:00
|
|
|
#include "MSP430InstrInfo.h"
|
|
|
|
#include "MSP430MachineFunctionInfo.h"
|
2014-08-05 05:25:23 +08:00
|
|
|
#include "MSP430Subtarget.h"
|
2010-11-15 08:06:54 +08:00
|
|
|
#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"
|
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/CommandLine.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2010-11-15 08:06:54 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
bool MSP430FrameLowering::hasFP(const MachineFunction &MF) const {
|
2010-11-19 05:19:35 +08:00
|
|
|
const MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
|
2011-12-03 06:16:29 +08:00
|
|
|
return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
|
2010-11-19 05:19:35 +08:00
|
|
|
MF.getFrameInfo()->hasVarSizedObjects() ||
|
|
|
|
MFI->isFrameAddressTaken());
|
|
|
|
}
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
bool MSP430FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
|
2010-11-19 05:19:35 +08:00
|
|
|
return !MF.getFrameInfo()->hasVarSizedObjects();
|
|
|
|
}
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
void MSP430FrameLowering::emitPrologue(MachineFunction &MF) const {
|
2010-11-15 08:06:54 +08:00
|
|
|
MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>();
|
|
|
|
const MSP430InstrInfo &TII =
|
2014-08-05 10:39:49 +08:00
|
|
|
*static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
|
2010-11-15 08:06:54 +08:00
|
|
|
|
|
|
|
MachineBasicBlock::iterator MBBI = MBB.begin();
|
|
|
|
DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
|
|
|
|
|
|
|
|
// Get the number of bytes to allocate from the FrameInfo.
|
|
|
|
uint64_t StackSize = MFI->getStackSize();
|
|
|
|
|
|
|
|
uint64_t NumBytes = 0;
|
2010-11-19 05:19:35 +08:00
|
|
|
if (hasFP(MF)) {
|
2010-11-15 08:06:54 +08:00
|
|
|
// Calculate required stack adjustment
|
|
|
|
uint64_t FrameSize = StackSize - 2;
|
|
|
|
NumBytes = FrameSize - MSP430FI->getCalleeSavedFrameSize();
|
|
|
|
|
|
|
|
// Get the offset of the stack slot for the EBP register... which is
|
|
|
|
// guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
|
|
|
|
// Update the frame offset adjustment.
|
|
|
|
MFI->setOffsetAdjustment(-NumBytes);
|
|
|
|
|
2014-09-10 14:58:14 +08:00
|
|
|
// Save FP into the appropriate stack slot...
|
2010-11-15 08:06:54 +08:00
|
|
|
BuildMI(MBB, MBBI, DL, TII.get(MSP430::PUSH16r))
|
2014-09-10 14:58:14 +08:00
|
|
|
.addReg(MSP430::FP, RegState::Kill);
|
2010-11-15 08:06:54 +08:00
|
|
|
|
2014-09-10 14:58:14 +08:00
|
|
|
// Update FP with the new base value...
|
|
|
|
BuildMI(MBB, MBBI, DL, TII.get(MSP430::MOV16rr), MSP430::FP)
|
|
|
|
.addReg(MSP430::SP);
|
2010-11-15 08:06:54 +08:00
|
|
|
|
|
|
|
// Mark the FramePtr as live-in in every block except the entry.
|
2014-03-02 20:27:27 +08:00
|
|
|
for (MachineFunction::iterator I = std::next(MF.begin()), E = MF.end();
|
2010-11-15 08:06:54 +08:00
|
|
|
I != E; ++I)
|
2014-09-10 14:58:14 +08:00
|
|
|
I->addLiveIn(MSP430::FP);
|
2010-11-15 08:06:54 +08:00
|
|
|
|
|
|
|
} else
|
|
|
|
NumBytes = StackSize - MSP430FI->getCalleeSavedFrameSize();
|
|
|
|
|
|
|
|
// Skip the callee-saved push instructions.
|
|
|
|
while (MBBI != MBB.end() && (MBBI->getOpcode() == MSP430::PUSH16r))
|
|
|
|
++MBBI;
|
|
|
|
|
|
|
|
if (MBBI != MBB.end())
|
|
|
|
DL = MBBI->getDebugLoc();
|
|
|
|
|
2014-09-10 14:58:14 +08:00
|
|
|
if (NumBytes) { // adjust stack pointer: SP -= numbytes
|
|
|
|
// If there is an SUB16ri of SP immediately before this instruction, merge
|
2010-11-15 08:06:54 +08:00
|
|
|
// the two.
|
|
|
|
//NumBytes -= mergeSPUpdates(MBB, MBBI, true);
|
2014-09-10 14:58:14 +08:00
|
|
|
// If there is an ADD16ri or SUB16ri of SP immediately after this
|
2010-11-15 08:06:54 +08:00
|
|
|
// instruction, merge the two instructions.
|
|
|
|
// mergeSPUpdatesDown(MBB, MBBI, &NumBytes);
|
|
|
|
|
|
|
|
if (NumBytes) {
|
|
|
|
MachineInstr *MI =
|
2014-09-10 14:58:14 +08:00
|
|
|
BuildMI(MBB, MBBI, DL, TII.get(MSP430::SUB16ri), MSP430::SP)
|
|
|
|
.addReg(MSP430::SP).addImm(NumBytes);
|
2010-11-15 08:06:54 +08:00
|
|
|
// The SRW implicit def is dead.
|
|
|
|
MI->getOperand(3).setIsDead();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
void MSP430FrameLowering::emitEpilogue(MachineFunction &MF,
|
|
|
|
MachineBasicBlock &MBB) const {
|
2010-11-15 08:06:54 +08:00
|
|
|
const MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>();
|
|
|
|
const MSP430InstrInfo &TII =
|
2014-08-05 10:39:49 +08:00
|
|
|
*static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
|
2010-11-15 08:06:54 +08:00
|
|
|
|
2011-01-14 05:28:52 +08:00
|
|
|
MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
|
2010-11-15 08:06:54 +08:00
|
|
|
unsigned RetOpcode = MBBI->getOpcode();
|
|
|
|
DebugLoc DL = MBBI->getDebugLoc();
|
|
|
|
|
|
|
|
switch (RetOpcode) {
|
|
|
|
case MSP430::RET:
|
|
|
|
case MSP430::RETI: break; // These are ok
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Can only insert epilog into returning blocks");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the number of bytes to allocate from the FrameInfo
|
|
|
|
uint64_t StackSize = MFI->getStackSize();
|
|
|
|
unsigned CSSize = MSP430FI->getCalleeSavedFrameSize();
|
|
|
|
uint64_t NumBytes = 0;
|
|
|
|
|
2010-11-19 05:19:35 +08:00
|
|
|
if (hasFP(MF)) {
|
2010-11-15 08:06:54 +08:00
|
|
|
// Calculate required stack adjustment
|
|
|
|
uint64_t FrameSize = StackSize - 2;
|
|
|
|
NumBytes = FrameSize - CSSize;
|
|
|
|
|
2014-09-10 14:58:14 +08:00
|
|
|
// pop FP.
|
|
|
|
BuildMI(MBB, MBBI, DL, TII.get(MSP430::POP16r), MSP430::FP);
|
2010-11-15 08:06:54 +08:00
|
|
|
} else
|
|
|
|
NumBytes = StackSize - CSSize;
|
|
|
|
|
|
|
|
// Skip the callee-saved pop instructions.
|
|
|
|
while (MBBI != MBB.begin()) {
|
2014-03-02 20:27:27 +08:00
|
|
|
MachineBasicBlock::iterator PI = std::prev(MBBI);
|
2010-11-15 08:06:54 +08:00
|
|
|
unsigned Opc = PI->getOpcode();
|
2011-12-07 15:15:52 +08:00
|
|
|
if (Opc != MSP430::POP16r && !PI->isTerminator())
|
2010-11-15 08:06:54 +08:00
|
|
|
break;
|
|
|
|
--MBBI;
|
|
|
|
}
|
|
|
|
|
|
|
|
DL = MBBI->getDebugLoc();
|
|
|
|
|
2014-09-10 14:58:14 +08:00
|
|
|
// If there is an ADD16ri or SUB16ri of SP immediately before this
|
2010-11-15 08:06:54 +08:00
|
|
|
// instruction, merge the two instructions.
|
|
|
|
//if (NumBytes || MFI->hasVarSizedObjects())
|
|
|
|
// mergeSPUpdatesUp(MBB, MBBI, StackPtr, &NumBytes);
|
|
|
|
|
|
|
|
if (MFI->hasVarSizedObjects()) {
|
|
|
|
BuildMI(MBB, MBBI, DL,
|
2014-09-10 14:58:14 +08:00
|
|
|
TII.get(MSP430::MOV16rr), MSP430::SP).addReg(MSP430::FP);
|
2010-11-15 08:06:54 +08:00
|
|
|
if (CSSize) {
|
|
|
|
MachineInstr *MI =
|
|
|
|
BuildMI(MBB, MBBI, DL,
|
2014-09-10 14:58:14 +08:00
|
|
|
TII.get(MSP430::SUB16ri), MSP430::SP)
|
|
|
|
.addReg(MSP430::SP).addImm(CSSize);
|
2010-11-15 08:06:54 +08:00
|
|
|
// The SRW implicit def is dead.
|
|
|
|
MI->getOperand(3).setIsDead();
|
|
|
|
}
|
|
|
|
} else {
|
2014-09-10 14:58:14 +08:00
|
|
|
// adjust stack pointer back: SP += numbytes
|
2010-11-15 08:06:54 +08:00
|
|
|
if (NumBytes) {
|
|
|
|
MachineInstr *MI =
|
2014-09-10 14:58:14 +08:00
|
|
|
BuildMI(MBB, MBBI, DL, TII.get(MSP430::ADD16ri), MSP430::SP)
|
|
|
|
.addReg(MSP430::SP).addImm(NumBytes);
|
2010-11-15 08:06:54 +08:00
|
|
|
// The SRW implicit def is dead.
|
|
|
|
MI->getOperand(3).setIsDead();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-28 07:05:03 +08:00
|
|
|
|
|
|
|
// FIXME: Can we eleminate these in favour of generic code?
|
|
|
|
bool
|
2011-01-10 20:39:04 +08:00
|
|
|
MSP430FrameLowering::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 false;
|
|
|
|
|
|
|
|
DebugLoc DL;
|
|
|
|
if (MI != MBB.end()) DL = MI->getDebugLoc();
|
|
|
|
|
|
|
|
MachineFunction &MF = *MBB.getParent();
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
|
2010-11-28 07:05:03 +08:00
|
|
|
MSP430MachineFunctionInfo *MFI = MF.getInfo<MSP430MachineFunctionInfo>();
|
|
|
|
MFI->setCalleeSavedFrameSize(CSI.size() * 2);
|
|
|
|
|
|
|
|
for (unsigned i = CSI.size(); i != 0; --i) {
|
|
|
|
unsigned Reg = CSI[i-1].getReg();
|
|
|
|
// Add the callee-saved register as live-in. It's killed at the spill.
|
|
|
|
MBB.addLiveIn(Reg);
|
|
|
|
BuildMI(MBB, MI, DL, TII.get(MSP430::PUSH16r))
|
|
|
|
.addReg(Reg, RegState::Kill);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-01-10 20:39:04 +08:00
|
|
|
MSP430FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
2010-11-28 07:05:03 +08:00
|
|
|
const std::vector<CalleeSavedInfo> &CSI,
|
|
|
|
const TargetRegisterInfo *TRI) const {
|
|
|
|
if (CSI.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DebugLoc DL;
|
|
|
|
if (MI != MBB.end()) DL = MI->getDebugLoc();
|
|
|
|
|
|
|
|
MachineFunction &MF = *MBB.getParent();
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
|
2010-11-28 07:05:03 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = CSI.size(); i != e; ++i)
|
|
|
|
BuildMI(MBB, MI, DL, TII.get(MSP430::POP16r), CSI[i].getReg());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-10-18 01:37:11 +08:00
|
|
|
|
2013-02-22 04:05:00 +08:00
|
|
|
void MSP430FrameLowering::
|
|
|
|
eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I) const {
|
|
|
|
const MSP430InstrInfo &TII =
|
2014-08-05 10:39:49 +08:00
|
|
|
*static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
|
2013-02-22 04:05:00 +08:00
|
|
|
unsigned StackAlign = getStackAlignment();
|
|
|
|
|
|
|
|
if (!hasReservedCallFrame(MF)) {
|
|
|
|
// If the stack pointer can be changed after prologue, turn the
|
2014-09-10 14:58:14 +08:00
|
|
|
// adjcallstackup instruction into a 'sub SP, <amt>' and the
|
|
|
|
// adjcallstackdown instruction into 'add SP, <amt>'
|
2013-02-22 04:05:00 +08:00
|
|
|
// TODO: consider using push / pop instead of sub + store / add
|
|
|
|
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.
|
|
|
|
Amount = (Amount+StackAlign-1)/StackAlign*StackAlign;
|
|
|
|
|
2014-04-25 13:30:21 +08:00
|
|
|
MachineInstr *New = nullptr;
|
2013-02-22 04:05:00 +08:00
|
|
|
if (Old->getOpcode() == TII.getCallFrameSetupOpcode()) {
|
|
|
|
New = BuildMI(MF, Old->getDebugLoc(),
|
2014-09-10 14:58:14 +08:00
|
|
|
TII.get(MSP430::SUB16ri), MSP430::SP)
|
|
|
|
.addReg(MSP430::SP).addImm(Amount);
|
2013-02-22 04:05:00 +08:00
|
|
|
} else {
|
|
|
|
assert(Old->getOpcode() == TII.getCallFrameDestroyOpcode());
|
|
|
|
// factor out the amount the callee already popped.
|
|
|
|
uint64_t CalleeAmt = Old->getOperand(1).getImm();
|
|
|
|
Amount -= CalleeAmt;
|
|
|
|
if (Amount)
|
|
|
|
New = BuildMI(MF, Old->getDebugLoc(),
|
2014-09-10 14:58:14 +08:00
|
|
|
TII.get(MSP430::ADD16ri), MSP430::SP)
|
|
|
|
.addReg(MSP430::SP).addImm(Amount);
|
2013-02-22 04:05:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (New) {
|
|
|
|
// The SRW implicit def is dead.
|
|
|
|
New->getOperand(3).setIsDead();
|
|
|
|
|
|
|
|
// Replace the pseudo instruction with a new instruction...
|
|
|
|
MBB.insert(I, New);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (I->getOpcode() == TII.getCallFrameDestroyOpcode()) {
|
|
|
|
// If we are performing frame pointer elimination and if the callee pops
|
|
|
|
// something off the stack pointer, add it back.
|
|
|
|
if (uint64_t CalleeAmt = I->getOperand(1).getImm()) {
|
|
|
|
MachineInstr *Old = I;
|
|
|
|
MachineInstr *New =
|
|
|
|
BuildMI(MF, Old->getDebugLoc(), TII.get(MSP430::SUB16ri),
|
2014-09-10 14:58:14 +08:00
|
|
|
MSP430::SP).addReg(MSP430::SP).addImm(CalleeAmt);
|
2013-02-22 04:05:00 +08:00
|
|
|
// The SRW implicit def is dead.
|
|
|
|
New->getOperand(3).setIsDead();
|
|
|
|
|
|
|
|
MBB.insert(I, New);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MBB.erase(I);
|
|
|
|
}
|
|
|
|
|
2012-10-18 01:37:11 +08:00
|
|
|
void
|
2013-03-15 04:33:40 +08:00
|
|
|
MSP430FrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
|
|
|
|
RegScavenger *) const {
|
2014-09-10 14:58:14 +08:00
|
|
|
// Create a frame entry for the FP register that must be saved.
|
2013-02-22 04:05:00 +08:00
|
|
|
if (hasFP(MF)) {
|
2012-10-18 01:37:11 +08:00
|
|
|
int FrameIdx = MF.getFrameInfo()->CreateFixedObject(2, -4, true);
|
|
|
|
(void)FrameIdx;
|
|
|
|
assert(FrameIdx == MF.getFrameInfo()->getObjectIndexBegin() &&
|
2014-09-10 14:58:14 +08:00
|
|
|
"Slot for FP register must be last in order to be found!");
|
2012-10-18 01:37:11 +08:00
|
|
|
}
|
|
|
|
}
|