2012-02-17 08:55:11 +00:00
|
|
|
//===-- MipsFrameLowering.cpp - Mips Frame Information --------------------===//
|
2010-11-15 00:06:54 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
2011-04-15 21:51:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-11-15 00:06:54 +00:00
|
|
|
//
|
2011-01-10 12:39:04 +00:00
|
|
|
// This file contains the Mips implementation of TargetFrameLowering class.
|
2010-11-15 00:06:54 +00:00
|
|
|
//
|
2011-04-15 21:51:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-11-15 00:06:54 +00:00
|
|
|
|
2011-01-10 12:39:04 +00:00
|
|
|
#include "MipsFrameLowering.h"
|
2012-03-17 18:46:09 +00:00
|
|
|
#include "MipsAnalyzeImmediate.h"
|
2010-11-15 00:06:54 +00:00
|
|
|
#include "MipsInstrInfo.h"
|
|
|
|
#include "MipsMachineFunction.h"
|
2011-11-11 22:58:42 +00:00
|
|
|
#include "MCTargetDesc/MipsBaseInfo.h"
|
2010-11-15 00:06:54 +00: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"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
|
2011-04-15 21:51:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-11-15 00:06:54 +00: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 done thought a positive offset
|
|
|
|
// from the stack/frame pointer, so the stack is considering
|
|
|
|
// to grow up! Otherwise terrible hacks would have to be made
|
|
|
|
// to get this stack ABI compliant :)
|
|
|
|
//
|
|
|
|
// The stack frame required by the ABI (after call):
|
|
|
|
// Offset
|
|
|
|
//
|
|
|
|
// 0 ----------
|
|
|
|
// 4 Args to pass
|
|
|
|
// . saved $GP (used in PIC)
|
|
|
|
// . Alloca allocations
|
|
|
|
// . Local Area
|
|
|
|
// . CPU "Callee Saved" Registers
|
|
|
|
// . saved FP
|
|
|
|
// . saved RA
|
|
|
|
// . FPU "Callee Saved" Registers
|
|
|
|
// StackSize -----------
|
|
|
|
//
|
|
|
|
// Offset - offset from sp after stack allocation on function prologue
|
|
|
|
//
|
|
|
|
// The sp is the stack pointer subtracted/added from the stack size
|
|
|
|
// at the Prologue/Epilogue
|
|
|
|
//
|
|
|
|
// References to the previous stack (to obtain arguments) are done
|
|
|
|
// with offsets that exceeds the stack size: (stacksize+(4*(num_arg-1))
|
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
// - reference to the actual stack frame
|
|
|
|
// for any local area var there is smt like : FI >= 0, StackOffset: 4
|
|
|
|
// sw REGX, 4(SP)
|
|
|
|
//
|
|
|
|
// - reference to previous stack frame
|
|
|
|
// suppose there's a load to the 5th arguments : FI < 0, StackOffset: 16.
|
|
|
|
// The emitted instruction will be something like:
|
|
|
|
// lw REGX, 16+StackSize(SP)
|
|
|
|
//
|
|
|
|
// Since the total stack size is unknown on LowerFormalArguments, all
|
|
|
|
// stack references (ObjectOffset) created to reference the function
|
|
|
|
// arguments, are negative numbers. This way, on eliminateFrameIndex it's
|
|
|
|
// possible to detect those references and the offsets are adjusted to
|
|
|
|
// their real location.
|
|
|
|
//
|
2011-04-15 21:51:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-11-15 00:06:54 +00:00
|
|
|
|
2010-11-18 21:19:35 +00:00
|
|
|
// hasFP - Return true if the specified function should have a dedicated frame
|
2011-04-15 21:51:11 +00:00
|
|
|
// pointer register. This is true if the function has variable sized allocas or
|
|
|
|
// if frame pointer elimination is disabled.
|
2011-01-10 12:39:04 +00:00
|
|
|
bool MipsFrameLowering::hasFP(const MachineFunction &MF) const {
|
2010-11-18 21:19:35 +00:00
|
|
|
const MachineFrameInfo *MFI = MF.getFrameInfo();
|
2011-12-02 22:16:29 +00:00
|
|
|
return MF.getTarget().Options.DisableFramePointerElim(MF) ||
|
|
|
|
MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken();
|
2010-11-18 21:19:35 +00:00
|
|
|
}
|
|
|
|
|
2011-01-10 12:39:04 +00:00
|
|
|
void MipsFrameLowering::emitPrologue(MachineFunction &MF) const {
|
2010-11-15 00:06:54 +00:00
|
|
|
MachineBasicBlock &MBB = MF.front();
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
const MipsRegisterInfo *RegInfo =
|
|
|
|
static_cast<const MipsRegisterInfo*>(MF.getTarget().getRegisterInfo());
|
|
|
|
const MipsInstrInfo &TII =
|
|
|
|
*static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
|
|
|
|
MachineBasicBlock::iterator MBBI = MBB.begin();
|
|
|
|
DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
|
2011-11-15 18:53:55 +00:00
|
|
|
unsigned SP = STI.isABI_N64() ? Mips::SP_64 : Mips::SP;
|
|
|
|
unsigned FP = STI.isABI_N64() ? Mips::FP_64 : Mips::FP;
|
|
|
|
unsigned ZERO = STI.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO;
|
2011-11-11 04:00:29 +00:00
|
|
|
unsigned ADDu = STI.isABI_N64() ? Mips::DADDu : Mips::ADDu;
|
|
|
|
unsigned ADDiu = STI.isABI_N64() ? Mips::DADDiu : Mips::ADDiu;
|
2010-11-15 00:06:54 +00:00
|
|
|
|
2011-05-23 20:16:59 +00:00
|
|
|
// First, compute final stack size.
|
2012-07-31 21:28:49 +00:00
|
|
|
uint64_t StackSize = MFI->getStackSize();
|
2012-02-28 07:46:26 +00:00
|
|
|
|
2011-05-21 02:29:26 +00:00
|
|
|
// No need to allocate space on the stack.
|
|
|
|
if (StackSize == 0 && !MFI->adjustsStack()) return;
|
|
|
|
|
2011-06-07 02:17:21 +00:00
|
|
|
MachineModuleInfo &MMI = MF.getMMI();
|
|
|
|
std::vector<MachineMove> &Moves = MMI.getFrameMoves();
|
|
|
|
MachineLocation DstML, SrcML;
|
|
|
|
|
2012-01-25 04:12:04 +00:00
|
|
|
// Adjust stack.
|
2012-07-23 23:45:54 +00:00
|
|
|
if (isInt<16>(-StackSize)) {// addi sp, sp, (-stacksize)
|
|
|
|
if (STI.inMips16Mode())
|
|
|
|
BuildMI(MBB, MBBI, dl,
|
|
|
|
TII.get(Mips::SaveRaF16)).addImm(StackSize); // cleanup
|
|
|
|
else
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(ADDiu), SP).addReg(SP).addImm(-StackSize);
|
|
|
|
}
|
2012-03-28 00:22:50 +00:00
|
|
|
else { // Expand immediate that doesn't fit in 16-bit.
|
2012-06-14 01:17:13 +00:00
|
|
|
unsigned ATReg = STI.isABI_N64() ? Mips::AT_64 : Mips::AT;
|
|
|
|
|
|
|
|
MF.getInfo<MipsFunctionInfo>()->setEmitNOAT();
|
|
|
|
Mips::loadImmediate(-StackSize, STI.isABI_N64(), TII, MBB, MBBI, dl, false,
|
|
|
|
0);
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(ADDu), SP).addReg(SP).addReg(ATReg);
|
2012-03-28 00:22:50 +00:00
|
|
|
}
|
2010-11-15 00:06:54 +00:00
|
|
|
|
2011-06-07 02:17:21 +00:00
|
|
|
// emit ".cfi_def_cfa_offset StackSize"
|
|
|
|
MCSymbol *AdjustSPLabel = MMI.getContext().CreateTempSymbol();
|
|
|
|
BuildMI(MBB, MBBI, dl,
|
|
|
|
TII.get(TargetOpcode::PROLOG_LABEL)).addSym(AdjustSPLabel);
|
|
|
|
DstML = MachineLocation(MachineLocation::VirtualFP);
|
|
|
|
SrcML = MachineLocation(MachineLocation::VirtualFP, -StackSize);
|
|
|
|
Moves.push_back(MachineMove(AdjustSPLabel, DstML, SrcML));
|
|
|
|
|
2011-05-26 18:59:03 +00:00
|
|
|
const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
|
2011-06-07 02:17:21 +00:00
|
|
|
|
|
|
|
if (CSI.size()) {
|
2011-06-07 18:58:42 +00:00
|
|
|
// Find the instruction past the last instruction that saves a callee-saved
|
|
|
|
// register to the stack.
|
2011-06-07 02:17:21 +00:00
|
|
|
for (unsigned i = 0; i < CSI.size(); ++i)
|
|
|
|
++MBBI;
|
2012-02-28 07:46:26 +00:00
|
|
|
|
2011-06-07 18:58:42 +00:00
|
|
|
// Iterate over list of callee-saved registers and emit .cfi_offset
|
|
|
|
// directives.
|
2011-06-07 02:17:21 +00:00
|
|
|
MCSymbol *CSLabel = MMI.getContext().CreateTempSymbol();
|
|
|
|
BuildMI(MBB, MBBI, dl,
|
|
|
|
TII.get(TargetOpcode::PROLOG_LABEL)).addSym(CSLabel);
|
2012-02-28 07:46:26 +00:00
|
|
|
|
2011-06-07 02:17:21 +00:00
|
|
|
for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
|
|
|
|
E = CSI.end(); I != E; ++I) {
|
|
|
|
int64_t Offset = MFI->getObjectOffset(I->getFrameIdx());
|
|
|
|
unsigned Reg = I->getReg();
|
|
|
|
|
|
|
|
// If Reg is a double precision register, emit two cfa_offsets,
|
|
|
|
// one for each of the paired single precision registers.
|
2012-04-20 07:30:17 +00:00
|
|
|
if (Mips::AFGR64RegClass.contains(Reg)) {
|
2011-06-07 02:17:21 +00:00
|
|
|
MachineLocation DstML0(MachineLocation::VirtualFP, Offset);
|
|
|
|
MachineLocation DstML1(MachineLocation::VirtualFP, Offset + 4);
|
2012-05-30 18:40:49 +00:00
|
|
|
MachineLocation SrcML0(RegInfo->getSubReg(Reg, Mips::sub_fpeven));
|
|
|
|
MachineLocation SrcML1(RegInfo->getSubReg(Reg, Mips::sub_fpodd));
|
2011-06-07 02:17:21 +00:00
|
|
|
|
|
|
|
if (!STI.isLittle())
|
|
|
|
std::swap(SrcML0, SrcML1);
|
|
|
|
|
|
|
|
Moves.push_back(MachineMove(CSLabel, DstML0, SrcML0));
|
|
|
|
Moves.push_back(MachineMove(CSLabel, DstML1, SrcML1));
|
2012-04-20 07:30:17 +00:00
|
|
|
} else {
|
2011-06-07 02:17:21 +00:00
|
|
|
// Reg is either in CPURegs or FGR32.
|
|
|
|
DstML = MachineLocation(MachineLocation::VirtualFP, Offset);
|
|
|
|
SrcML = MachineLocation(Reg);
|
|
|
|
Moves.push_back(MachineMove(CSLabel, DstML, SrcML));
|
|
|
|
}
|
|
|
|
}
|
2012-02-28 07:46:26 +00:00
|
|
|
}
|
2011-06-07 02:17:21 +00:00
|
|
|
|
2011-05-26 18:59:03 +00:00
|
|
|
// if framepointer enabled, set it to point to the stack pointer.
|
2011-06-07 02:17:21 +00:00
|
|
|
if (hasFP(MF)) {
|
2012-02-28 07:46:26 +00:00
|
|
|
// Insert instruction "move $fp, $sp" at this location.
|
2011-11-15 18:53:55 +00:00
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(ADDu), FP).addReg(SP).addReg(ZERO);
|
2010-11-15 00:06:54 +00:00
|
|
|
|
2012-02-28 07:46:26 +00:00
|
|
|
// emit ".cfi_def_cfa_register $fp"
|
2011-06-07 02:17:21 +00:00
|
|
|
MCSymbol *SetFPLabel = MMI.getContext().CreateTempSymbol();
|
|
|
|
BuildMI(MBB, MBBI, dl,
|
|
|
|
TII.get(TargetOpcode::PROLOG_LABEL)).addSym(SetFPLabel);
|
2011-11-15 18:53:55 +00:00
|
|
|
DstML = MachineLocation(FP);
|
2011-06-07 02:17:21 +00:00
|
|
|
SrcML = MachineLocation(MachineLocation::VirtualFP);
|
|
|
|
Moves.push_back(MachineMove(SetFPLabel, DstML, SrcML));
|
|
|
|
}
|
2010-11-15 00:06:54 +00:00
|
|
|
}
|
|
|
|
|
2011-01-10 12:39:04 +00:00
|
|
|
void MipsFrameLowering::emitEpilogue(MachineFunction &MF,
|
2010-11-15 00:06:54 +00:00
|
|
|
MachineBasicBlock &MBB) const {
|
2011-01-13 21:28:52 +00:00
|
|
|
MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
|
2010-11-15 00:06:54 +00:00
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
const MipsInstrInfo &TII =
|
|
|
|
*static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
|
|
|
|
DebugLoc dl = MBBI->getDebugLoc();
|
2011-11-15 18:53:55 +00:00
|
|
|
unsigned SP = STI.isABI_N64() ? Mips::SP_64 : Mips::SP;
|
|
|
|
unsigned FP = STI.isABI_N64() ? Mips::FP_64 : Mips::FP;
|
|
|
|
unsigned ZERO = STI.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO;
|
|
|
|
unsigned ADDu = STI.isABI_N64() ? Mips::DADDu : Mips::ADDu;
|
|
|
|
unsigned ADDiu = STI.isABI_N64() ? Mips::DADDiu : Mips::ADDiu;
|
2010-11-15 00:06:54 +00:00
|
|
|
|
2011-05-20 18:39:33 +00:00
|
|
|
// if framepointer enabled, restore the stack pointer.
|
2011-05-21 02:29:26 +00:00
|
|
|
if (hasFP(MF)) {
|
|
|
|
// Find the first instruction that restores a callee-saved register.
|
|
|
|
MachineBasicBlock::iterator I = MBBI;
|
2012-02-28 07:46:26 +00:00
|
|
|
|
2011-05-21 02:29:26 +00:00
|
|
|
for (unsigned i = 0; i < MFI->getCalleeSavedInfo().size(); ++i)
|
|
|
|
--I;
|
|
|
|
|
|
|
|
// Insert instruction "move $sp, $fp" at this location.
|
2011-11-15 18:53:55 +00:00
|
|
|
BuildMI(MBB, I, dl, TII.get(ADDu), SP).addReg(FP).addReg(ZERO);
|
2011-05-21 02:29:26 +00:00
|
|
|
}
|
2010-11-15 00:06:54 +00:00
|
|
|
|
2012-01-25 04:12:04 +00:00
|
|
|
// Get the number of bytes from FrameInfo
|
|
|
|
uint64_t StackSize = MFI->getStackSize();
|
|
|
|
|
|
|
|
if (!StackSize)
|
|
|
|
return;
|
2011-03-04 20:48:08 +00:00
|
|
|
|
2012-01-25 04:12:04 +00:00
|
|
|
// Adjust stack.
|
2012-07-23 23:45:54 +00:00
|
|
|
if (isInt<16>(StackSize)) { // addi sp, sp, (-stacksize)
|
|
|
|
if (STI.inMips16Mode())
|
|
|
|
// assumes stacksize multiple of 8
|
|
|
|
BuildMI(MBB, MBBI, dl,
|
|
|
|
TII.get(Mips::RestoreRaF16)).addImm(StackSize);
|
|
|
|
else
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(ADDiu), SP).addReg(SP).addImm(StackSize);
|
|
|
|
}
|
2012-06-14 01:17:13 +00:00
|
|
|
else { // Expand immediate that doesn't fit in 16-bit.
|
|
|
|
unsigned ATReg = STI.isABI_N64() ? Mips::AT_64 : Mips::AT;
|
|
|
|
|
|
|
|
MF.getInfo<MipsFunctionInfo>()->setEmitNOAT();
|
|
|
|
Mips::loadImmediate(StackSize, STI.isABI_N64(), TII, MBB, MBBI, dl, false,
|
|
|
|
0);
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(ADDu), SP).addReg(SP).addReg(ATReg);
|
|
|
|
}
|
2010-11-15 00:06:54 +00:00
|
|
|
}
|
2011-01-18 19:50:18 +00:00
|
|
|
|
2011-05-20 18:39:33 +00:00
|
|
|
void MipsFrameLowering::
|
|
|
|
processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
|
|
|
|
RegScavenger *RS) const {
|
2012-06-14 21:10:56 +00:00
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
2011-11-15 18:53:55 +00:00
|
|
|
unsigned FP = STI.isABI_N64() ? Mips::FP_64 : Mips::FP;
|
2011-05-20 18:39:33 +00:00
|
|
|
|
|
|
|
// FIXME: remove this code if register allocator can correctly mark
|
|
|
|
// $fp and $ra used or unused.
|
|
|
|
|
|
|
|
// Mark $fp and $ra as used or unused.
|
|
|
|
if (hasFP(MF))
|
2011-11-15 18:53:55 +00:00
|
|
|
MRI.setPhysRegUsed(FP);
|
2012-07-10 00:19:06 +00:00
|
|
|
}
|
2011-05-20 18:39:33 +00:00
|
|
|
|
2012-07-10 00:19:06 +00:00
|
|
|
bool MipsFrameLowering::
|
|
|
|
spillCalleeSavedRegisters(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI,
|
|
|
|
const TargetRegisterInfo *TRI) const {
|
|
|
|
MachineFunction *MF = MBB.getParent();
|
|
|
|
MachineBasicBlock *EntryBlock = MF->begin();
|
|
|
|
const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
|
2012-07-11 00:53:32 +00:00
|
|
|
// Add the callee-saved register as live-in. Do not add if the register is
|
|
|
|
// RA and return address is taken, because it has already been added in
|
|
|
|
// method MipsTargetLowering::LowerRETURNADDR.
|
|
|
|
// It's killed at the spill, unless the register is RA and return address
|
|
|
|
// is taken.
|
|
|
|
unsigned Reg = CSI[i].getReg();
|
|
|
|
bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA || Reg == Mips::RA_64)
|
|
|
|
&& MF->getFrameInfo()->isReturnAddressTaken();
|
|
|
|
if (!IsRAAndRetAddrIsTaken)
|
|
|
|
EntryBlock->addLiveIn(Reg);
|
2012-07-10 00:19:06 +00:00
|
|
|
|
|
|
|
// Insert the spill to the stack frame.
|
2012-07-11 00:53:32 +00:00
|
|
|
bool IsKill = !IsRAAndRetAddrIsTaken;
|
2012-07-10 00:19:06 +00:00
|
|
|
const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
|
2012-07-11 00:53:32 +00:00
|
|
|
TII.storeRegToStackSlot(*EntryBlock, MI, Reg, IsKill,
|
2012-07-10 00:19:06 +00:00
|
|
|
CSI[i].getFrameIdx(), RC, TRI);
|
2012-01-25 04:19:22 +00:00
|
|
|
}
|
2012-07-10 00:19:06 +00:00
|
|
|
|
|
|
|
return true;
|
2011-05-20 18:39:33 +00:00
|
|
|
}
|