2012-02-17 08:55:11 +00:00
|
|
|
//===-- MipsRegisterInfo.cpp - MIPS Register Information -== --------------===//
|
2007-06-06 07:42:06 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-06-06 07:42:06 +00:00
|
|
|
//
|
2011-04-15 21:51:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-06-06 07:42:06 +00:00
|
|
|
//
|
2008-02-10 18:45:23 +00:00
|
|
|
// This file contains the MIPS implementation of the TargetRegisterInfo class.
|
2007-06-06 07:42:06 +00:00
|
|
|
//
|
2011-04-15 21:51:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-06-06 07:42:06 +00:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "mips-reg-info"
|
|
|
|
|
|
|
|
#include "Mips.h"
|
2012-01-25 03:55:10 +00:00
|
|
|
#include "MipsAnalyzeImmediate.h"
|
2008-07-14 14:42:54 +00:00
|
|
|
#include "MipsSubtarget.h"
|
2007-06-06 07:42:06 +00:00
|
|
|
#include "MipsRegisterInfo.h"
|
2007-07-11 23:21:31 +00:00
|
|
|
#include "MipsMachineFunction.h"
|
2007-06-06 07:42:06 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Type.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/CodeGen/ValueTypes.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2011-01-10 12:39:04 +00:00
|
|
|
#include "llvm/Target/TargetFrameLowering.h"
|
2007-06-06 07:42:06 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-07-25 00:23:56 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-06-06 07:42:06 +00:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2011-07-01 01:04:43 +00:00
|
|
|
#include "llvm/Analysis/DebugInfo.h"
|
2011-06-27 18:32:37 +00:00
|
|
|
|
|
|
|
#define GET_REGINFO_TARGET_DESC
|
2011-06-24 01:44:41 +00:00
|
|
|
#include "MipsGenRegisterInfo.inc"
|
2007-06-06 07:42:06 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2011-03-04 17:51:39 +00:00
|
|
|
MipsRegisterInfo::MipsRegisterInfo(const MipsSubtarget &ST,
|
2008-07-14 14:42:54 +00:00
|
|
|
const TargetInstrInfo &tii)
|
2011-07-18 20:57:22 +00:00
|
|
|
: MipsGenRegisterInfo(Mips::RA), Subtarget(ST), TII(tii) {}
|
2007-06-06 07:42:06 +00:00
|
|
|
|
2009-08-12 22:10:57 +00:00
|
|
|
unsigned MipsRegisterInfo::getPICCallReg() { return Mips::T9; }
|
2008-07-14 14:42:54 +00:00
|
|
|
|
2011-04-15 21:51:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2011-03-04 17:51:39 +00:00
|
|
|
// Callee Saved Registers methods
|
2011-04-15 21:51:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-08-28 05:13:42 +00:00
|
|
|
|
2007-06-06 07:42:06 +00:00
|
|
|
/// Mips Callee Saved Registers
|
2012-03-04 03:33:22 +00:00
|
|
|
const uint16_t* MipsRegisterInfo::
|
2011-03-04 17:51:39 +00:00
|
|
|
getCalleeSavedRegs(const MachineFunction *MF) const
|
2007-06-06 07:42:06 +00:00
|
|
|
{
|
2012-03-01 22:27:29 +00:00
|
|
|
if (Subtarget.isSingleFloat())
|
|
|
|
return CSR_SingleFloatOnly_SaveList;
|
|
|
|
else if (!Subtarget.hasMips64())
|
|
|
|
return CSR_O32_SaveList;
|
|
|
|
else if (Subtarget.isABI_N32())
|
|
|
|
return CSR_N32_SaveList;
|
|
|
|
|
|
|
|
assert(Subtarget.isABI_N64());
|
|
|
|
return CSR_N64_SaveList;
|
|
|
|
}
|
2011-09-23 18:11:56 +00:00
|
|
|
|
2012-03-01 22:27:29 +00:00
|
|
|
const uint32_t*
|
|
|
|
MipsRegisterInfo::getCallPreservedMask(CallingConv::ID) const
|
|
|
|
{
|
2008-08-06 06:14:43 +00:00
|
|
|
if (Subtarget.isSingleFloat())
|
2012-03-01 22:27:29 +00:00
|
|
|
return CSR_SingleFloatOnly_RegMask;
|
2011-09-23 18:11:56 +00:00
|
|
|
else if (!Subtarget.hasMips64())
|
2012-03-01 22:27:29 +00:00
|
|
|
return CSR_O32_RegMask;
|
2011-09-23 18:11:56 +00:00
|
|
|
else if (Subtarget.isABI_N32())
|
2012-03-01 22:27:29 +00:00
|
|
|
return CSR_N32_RegMask;
|
2012-02-28 07:46:26 +00:00
|
|
|
|
2011-09-23 18:11:56 +00:00
|
|
|
assert(Subtarget.isABI_N64());
|
2012-03-01 22:27:29 +00:00
|
|
|
return CSR_N64_RegMask;
|
2007-06-06 07:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BitVector MipsRegisterInfo::
|
2010-11-18 21:19:35 +00:00
|
|
|
getReservedRegs(const MachineFunction &MF) const {
|
2011-09-23 18:11:56 +00:00
|
|
|
static const unsigned ReservedCPURegs[] = {
|
2012-02-28 07:46:26 +00:00
|
|
|
Mips::ZERO, Mips::AT, Mips::K0, Mips::K1,
|
2012-02-24 22:34:47 +00:00
|
|
|
Mips::SP, Mips::FP, Mips::RA
|
2011-09-23 18:11:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned ReservedCPU64Regs[] = {
|
2012-02-28 07:46:26 +00:00
|
|
|
Mips::ZERO_64, Mips::AT_64, Mips::K0_64, Mips::K1_64,
|
2012-02-24 22:34:47 +00:00
|
|
|
Mips::SP_64, Mips::FP_64, Mips::RA_64
|
2011-09-23 18:11:56 +00:00
|
|
|
};
|
|
|
|
|
2007-06-06 07:42:06 +00:00
|
|
|
BitVector Reserved(getNumRegs());
|
2011-09-23 18:11:56 +00:00
|
|
|
typedef TargetRegisterClass::iterator RegIter;
|
|
|
|
|
2011-11-07 19:03:40 +00:00
|
|
|
for (unsigned I = 0; I < array_lengthof(ReservedCPURegs); ++I)
|
|
|
|
Reserved.set(ReservedCPURegs[I]);
|
2011-09-23 18:11:56 +00:00
|
|
|
|
|
|
|
if (Subtarget.hasMips64()) {
|
2011-11-07 19:03:40 +00:00
|
|
|
for (unsigned I = 0; I < array_lengthof(ReservedCPU64Regs); ++I)
|
|
|
|
Reserved.set(ReservedCPU64Regs[I]);
|
2009-03-21 00:05:07 +00:00
|
|
|
|
2011-09-23 18:11:56 +00:00
|
|
|
// Reserve all registers in AFGR64.
|
|
|
|
for (RegIter Reg = Mips::AFGR64RegisterClass->begin();
|
|
|
|
Reg != Mips::AFGR64RegisterClass->end(); ++Reg)
|
|
|
|
Reserved.set(*Reg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Reserve all registers in CPU64Regs & FGR64.
|
|
|
|
for (RegIter Reg = Mips::CPU64RegsRegisterClass->begin();
|
|
|
|
Reg != Mips::CPU64RegsRegisterClass->end(); ++Reg)
|
|
|
|
Reserved.set(*Reg);
|
|
|
|
|
|
|
|
for (RegIter Reg = Mips::FGR64RegisterClass->begin();
|
|
|
|
Reg != Mips::FGR64RegisterClass->end(); ++Reg)
|
|
|
|
Reserved.set(*Reg);
|
|
|
|
}
|
2012-02-28 07:46:26 +00:00
|
|
|
|
|
|
|
// If GP is dedicated as a global base register, reserve it.
|
2012-02-24 22:34:47 +00:00
|
|
|
if (MF.getInfo<MipsFunctionInfo>()->globalBaseRegFixed()) {
|
|
|
|
Reserved.set(Mips::GP);
|
|
|
|
Reserved.set(Mips::GP_64);
|
|
|
|
}
|
|
|
|
|
2007-06-06 07:42:06 +00:00
|
|
|
return Reserved;
|
|
|
|
}
|
|
|
|
|
2011-03-04 17:51:39 +00:00
|
|
|
// This function eliminate ADJCALLSTACKDOWN,
|
2007-06-06 07:42:06 +00:00
|
|
|
// ADJCALLSTACKUP pseudo instructions
|
|
|
|
void MipsRegisterInfo::
|
|
|
|
eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I) const {
|
|
|
|
// Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
|
|
|
|
MBB.erase(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FrameIndex represent objects inside a abstract stack.
|
|
|
|
// We must replace FrameIndex with an stack/frame pointer
|
|
|
|
// direct reference.
|
2010-08-26 23:32:16 +00:00
|
|
|
void MipsRegisterInfo::
|
2009-10-07 17:12:56 +00:00
|
|
|
eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj,
|
2010-08-26 23:32:16 +00:00
|
|
|
RegScavenger *RS) const {
|
2007-11-05 03:02:32 +00:00
|
|
|
MachineInstr &MI = *II;
|
2007-06-06 07:42:06 +00:00
|
|
|
MachineFunction &MF = *MI.getParent()->getParent();
|
2011-05-21 03:01:03 +00:00
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
|
2007-06-06 07:42:06 +00:00
|
|
|
|
2007-07-11 23:21:31 +00:00
|
|
|
unsigned i = 0;
|
2008-10-03 15:45:36 +00:00
|
|
|
while (!MI.getOperand(i).isFI()) {
|
2007-06-06 07:42:06 +00:00
|
|
|
++i;
|
2011-03-04 17:51:39 +00:00
|
|
|
assert(i < MI.getNumOperands() &&
|
2007-06-06 07:42:06 +00:00
|
|
|
"Instr doesn't have FrameIndex operand!");
|
|
|
|
}
|
|
|
|
|
2009-08-23 06:49:22 +00:00
|
|
|
DEBUG(errs() << "\nFunction : " << MF.getFunction()->getName() << "\n";
|
|
|
|
errs() << "<--------->\n" << MI);
|
2008-08-02 19:42:36 +00:00
|
|
|
|
2007-12-30 23:10:15 +00:00
|
|
|
int FrameIndex = MI.getOperand(i).getIndex();
|
2012-01-25 03:55:10 +00:00
|
|
|
uint64_t stackSize = MF.getFrameInfo()->getStackSize();
|
|
|
|
int64_t spOffset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
|
2007-06-06 07:42:06 +00:00
|
|
|
|
2009-08-23 06:49:22 +00:00
|
|
|
DEBUG(errs() << "FrameIndex : " << FrameIndex << "\n"
|
|
|
|
<< "spOffset : " << spOffset << "\n"
|
|
|
|
<< "stackSize : " << stackSize << "\n");
|
2007-06-06 07:42:06 +00:00
|
|
|
|
2011-07-01 01:04:43 +00:00
|
|
|
const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
|
|
|
|
int MinCSFI = 0;
|
|
|
|
int MaxCSFI = -1;
|
|
|
|
|
|
|
|
if (CSI.size()) {
|
|
|
|
MinCSFI = CSI[0].getFrameIdx();
|
|
|
|
MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following stack frame objects are always referenced relative to $sp:
|
|
|
|
// 1. Outgoing arguments.
|
|
|
|
// 2. Pointer to dynamically allocated stack space.
|
|
|
|
// 3. Locations for callee-saved registers.
|
2012-02-28 07:46:26 +00:00
|
|
|
// Everything else is referenced relative to whatever register
|
2011-07-01 01:04:43 +00:00
|
|
|
// getFrameRegister() returns.
|
|
|
|
unsigned FrameReg;
|
2011-05-23 20:16:59 +00:00
|
|
|
|
2011-07-01 01:04:43 +00:00
|
|
|
if (MipsFI->isOutArgFI(FrameIndex) || MipsFI->isDynAllocFI(FrameIndex) ||
|
|
|
|
(FrameIndex >= MinCSFI && FrameIndex <= MaxCSFI))
|
2011-11-15 18:53:55 +00:00
|
|
|
FrameReg = Subtarget.isABI_N64() ? Mips::SP_64 : Mips::SP;
|
2011-07-01 01:04:43 +00:00
|
|
|
else
|
2012-02-28 07:46:26 +00:00
|
|
|
FrameReg = getFrameRegister(MF);
|
|
|
|
|
2011-05-23 20:16:59 +00:00
|
|
|
// Calculate final offset.
|
2011-06-21 00:40:49 +00:00
|
|
|
// - There is no need to change the offset if the frame object is one of the
|
|
|
|
// following: an outgoing argument, pointer to a dynamically allocated
|
|
|
|
// stack space or a $gp restore location,
|
2011-05-23 20:16:59 +00:00
|
|
|
// - If the frame object is any of the following, its offset must be adjusted
|
|
|
|
// by adding the size of the stack:
|
2012-02-28 07:46:26 +00:00
|
|
|
// incoming argument, callee-saved register location or local variable.
|
2012-01-25 03:55:10 +00:00
|
|
|
int64_t Offset;
|
2011-07-01 01:04:43 +00:00
|
|
|
|
2011-06-21 00:40:49 +00:00
|
|
|
if (MipsFI->isOutArgFI(FrameIndex) || MipsFI->isGPFI(FrameIndex) ||
|
|
|
|
MipsFI->isDynAllocFI(FrameIndex))
|
2011-05-23 20:16:59 +00:00
|
|
|
Offset = spOffset;
|
|
|
|
else
|
2012-01-25 03:55:10 +00:00
|
|
|
Offset = spOffset + (int64_t)stackSize;
|
2011-05-23 20:16:59 +00:00
|
|
|
|
2011-07-07 18:57:00 +00:00
|
|
|
Offset += MI.getOperand(i+1).getImm();
|
2007-06-06 07:42:06 +00:00
|
|
|
|
2009-08-23 06:49:22 +00:00
|
|
|
DEBUG(errs() << "Offset : " << Offset << "\n" << "<--------->\n");
|
2007-06-06 07:42:06 +00:00
|
|
|
|
2011-07-07 19:13:09 +00:00
|
|
|
// If MI is not a debug value, make sure Offset fits in the 16-bit immediate
|
2012-02-28 07:46:26 +00:00
|
|
|
// field.
|
2012-01-25 03:55:10 +00:00
|
|
|
if (!MI.isDebugValue() && !isInt<16>(Offset)) {
|
2011-07-07 19:13:09 +00:00
|
|
|
MachineBasicBlock &MBB = *MI.getParent();
|
2011-03-04 20:48:08 +00:00
|
|
|
DebugLoc DL = II->getDebugLoc();
|
2012-01-25 03:55:10 +00:00
|
|
|
MipsAnalyzeImmediate AnalyzeImm;
|
|
|
|
unsigned Size = Subtarget.isABI_N64() ? 64 : 32;
|
|
|
|
unsigned LUi = Subtarget.isABI_N64() ? Mips::LUi64 : Mips::LUi;
|
|
|
|
unsigned ADDu = Subtarget.isABI_N64() ? Mips::DADDu : Mips::ADDu;
|
2012-02-28 07:46:26 +00:00
|
|
|
unsigned ZEROReg = Subtarget.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO;
|
2012-01-25 03:55:10 +00:00
|
|
|
unsigned ATReg = Subtarget.isABI_N64() ? Mips::AT_64 : Mips::AT;
|
|
|
|
const MipsAnalyzeImmediate::InstSeq &Seq =
|
|
|
|
AnalyzeImm.Analyze(Offset, Size, true /* LastInstrIsADDiu */);
|
|
|
|
MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
|
2011-03-04 20:48:08 +00:00
|
|
|
|
|
|
|
// FIXME: change this when mips goes MC".
|
2011-07-07 19:13:09 +00:00
|
|
|
BuildMI(MBB, II, DL, TII.get(Mips::NOAT));
|
2011-03-04 20:48:08 +00:00
|
|
|
|
2012-01-25 03:55:10 +00:00
|
|
|
// The first instruction can be a LUi, which is different from other
|
|
|
|
// instructions (ADDiu, ORI and SLL) in that it does not have a register
|
|
|
|
// operand.
|
|
|
|
if (Inst->Opc == LUi)
|
|
|
|
BuildMI(MBB, II, DL, TII.get(LUi), ATReg)
|
|
|
|
.addImm(SignExtend64<16>(Inst->ImmOpnd));
|
|
|
|
else
|
|
|
|
BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ZEROReg)
|
|
|
|
.addImm(SignExtend64<16>(Inst->ImmOpnd));
|
|
|
|
|
|
|
|
// Build the remaining instructions in Seq except for the last one.
|
|
|
|
for (++Inst; Inst != Seq.end() - 1; ++Inst)
|
|
|
|
BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ATReg)
|
|
|
|
.addImm(SignExtend64<16>(Inst->ImmOpnd));
|
|
|
|
|
|
|
|
BuildMI(MBB, II, DL, TII.get(ADDu), ATReg).addReg(FrameReg).addReg(ATReg);
|
|
|
|
|
|
|
|
FrameReg = ATReg;
|
|
|
|
Offset = SignExtend64<16>(Inst->ImmOpnd);
|
2011-03-04 20:48:08 +00:00
|
|
|
BuildMI(MBB, ++II, MI.getDebugLoc(), TII.get(Mips::ATMACRO));
|
2011-07-07 19:13:09 +00:00
|
|
|
}
|
2011-03-04 20:48:08 +00:00
|
|
|
|
2011-07-07 19:13:09 +00:00
|
|
|
MI.getOperand(i).ChangeToRegister(FrameReg, false);
|
|
|
|
MI.getOperand(i+1).ChangeToImmediate(Offset);
|
2007-06-06 07:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned MipsRegisterInfo::
|
2009-11-12 20:49:22 +00:00
|
|
|
getFrameRegister(const MachineFunction &MF) const {
|
2011-01-10 12:39:04 +00:00
|
|
|
const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
|
2011-11-15 18:53:55 +00:00
|
|
|
bool IsN64 = Subtarget.isABI_N64();
|
2010-11-18 21:19:35 +00:00
|
|
|
|
2011-11-15 18:53:55 +00:00
|
|
|
return TFI->hasFP(MF) ? (IsN64 ? Mips::FP_64 : Mips::FP) :
|
|
|
|
(IsN64 ? Mips::SP_64 : Mips::SP);
|
2007-06-06 07:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned MipsRegisterInfo::
|
|
|
|
getEHExceptionRegister() const {
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("What is the exception register");
|
2007-06-06 07:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned MipsRegisterInfo::
|
|
|
|
getEHHandlerRegister() const {
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("What is the exception handler register");
|
2007-06-06 07:42:06 +00:00
|
|
|
}
|