2008-11-07 10:59:00 +00:00
|
|
|
//===- XCoreRegisterInfo.cpp - XCore Register Information -------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains the XCore implementation of the MRegisterInfo class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "XCoreRegisterInfo.h"
|
|
|
|
#include "XCoreMachineFunctionInfo.h"
|
|
|
|
#include "XCore.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/RegisterScavenging.h"
|
2011-01-10 12:39:04 +00:00
|
|
|
#include "llvm/Target/TargetFrameLowering.h"
|
2008-11-07 10:59:00 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Type.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/ADT/BitVector.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-08 19:04:27 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-06-27 18:32:37 +00:00
|
|
|
|
|
|
|
#define GET_REGINFO_TARGET_DESC
|
2011-06-24 01:44:41 +00:00
|
|
|
#include "XCoreGenRegisterInfo.inc"
|
2011-06-27 18:32:37 +00:00
|
|
|
|
2008-11-07 10:59:00 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
XCoreRegisterInfo::XCoreRegisterInfo(const TargetInstrInfo &tii)
|
2011-07-18 20:57:22 +00:00
|
|
|
: XCoreGenRegisterInfo(XCore::LR), TII(tii) {
|
2008-11-07 10:59:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// helper functions
|
|
|
|
static inline bool isImmUs(unsigned val) {
|
|
|
|
return val <= 11;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool isImmU6(unsigned val) {
|
|
|
|
return val < (1 << 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool isImmU16(unsigned val) {
|
|
|
|
return val < (1 << 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const unsigned XCore_ArgRegs[] = {
|
|
|
|
XCore::R0, XCore::R1, XCore::R2, XCore::R3
|
|
|
|
};
|
|
|
|
|
|
|
|
const unsigned * XCoreRegisterInfo::getArgRegs(const MachineFunction *MF)
|
|
|
|
{
|
|
|
|
return XCore_ArgRegs;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned XCoreRegisterInfo::getNumArgRegs(const MachineFunction *MF)
|
|
|
|
{
|
|
|
|
return array_lengthof(XCore_ArgRegs);
|
|
|
|
}
|
|
|
|
|
2010-04-05 05:57:52 +00:00
|
|
|
bool XCoreRegisterInfo::needsFrameMoves(const MachineFunction &MF) {
|
2011-05-25 03:44:17 +00:00
|
|
|
return MF.getMMI().hasDebugInfo() ||
|
|
|
|
MF.getFunction()->needsUnwindTableEntry();
|
2008-11-07 10:59:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned* XCoreRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF)
|
|
|
|
const {
|
|
|
|
static const unsigned CalleeSavedRegs[] = {
|
|
|
|
XCore::R4, XCore::R5, XCore::R6, XCore::R7,
|
|
|
|
XCore::R8, XCore::R9, XCore::R10, XCore::LR,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
return CalleeSavedRegs;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitVector XCoreRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
|
|
|
|
BitVector Reserved(getNumRegs());
|
2011-01-10 12:39:04 +00:00
|
|
|
const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
|
2010-11-18 21:19:35 +00:00
|
|
|
|
2008-11-07 10:59:00 +00:00
|
|
|
Reserved.set(XCore::CP);
|
|
|
|
Reserved.set(XCore::DP);
|
|
|
|
Reserved.set(XCore::SP);
|
|
|
|
Reserved.set(XCore::LR);
|
2010-11-18 21:19:35 +00:00
|
|
|
if (TFI->hasFP(MF)) {
|
2008-11-07 10:59:00 +00:00
|
|
|
Reserved.set(XCore::R10);
|
|
|
|
}
|
|
|
|
return Reserved;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
XCoreRegisterInfo::requiresRegisterScavenging(const MachineFunction &MF) const {
|
2011-01-10 12:39:04 +00:00
|
|
|
const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
|
2008-11-07 10:59:00 +00:00
|
|
|
|
2010-11-18 21:19:35 +00:00
|
|
|
// TODO can we estimate stack size?
|
|
|
|
return TFI->hasFP(MF);
|
2008-11-07 10:59:00 +00:00
|
|
|
}
|
|
|
|
|
2011-03-15 15:10:11 +00:00
|
|
|
bool
|
|
|
|
XCoreRegisterInfo::useFPForScavengingIndex(const MachineFunction &MF) const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-11-07 10:59:00 +00:00
|
|
|
// This function eliminates ADJCALLSTACKDOWN,
|
|
|
|
// ADJCALLSTACKUP pseudo instructions
|
|
|
|
void XCoreRegisterInfo::
|
|
|
|
eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I) const {
|
2011-01-10 12:39:04 +00:00
|
|
|
const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
|
2010-11-18 21:19:35 +00:00
|
|
|
|
|
|
|
if (!TFI->hasReservedCallFrame(MF)) {
|
2008-11-07 10:59:00 +00:00
|
|
|
// Turn the adjcallstackdown instruction into 'extsp <amt>' and the
|
|
|
|
// adjcallstackup instruction into 'ldaw sp, sp[<amt>]'
|
|
|
|
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.
|
2011-01-10 12:39:04 +00:00
|
|
|
unsigned Align = TFI->getStackAlignment();
|
2008-11-07 10:59:00 +00:00
|
|
|
Amount = (Amount+Align-1)/Align*Align;
|
|
|
|
|
|
|
|
assert(Amount%4 == 0);
|
|
|
|
Amount /= 4;
|
2011-01-10 12:39:04 +00:00
|
|
|
|
2008-11-07 10:59:00 +00:00
|
|
|
bool isU6 = isImmU6(Amount);
|
|
|
|
if (!isU6 && !isImmU16(Amount)) {
|
|
|
|
// FIX could emit multiple instructions in this case.
|
2009-07-08 20:53:28 +00:00
|
|
|
#ifndef NDEBUG
|
2009-08-23 07:05:07 +00:00
|
|
|
errs() << "eliminateCallFramePseudoInstr size too big: "
|
|
|
|
<< Amount << "\n";
|
2009-07-08 20:53:28 +00:00
|
|
|
#endif
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable(0);
|
2008-11-07 10:59:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr *New;
|
|
|
|
if (Old->getOpcode() == XCore::ADJCALLSTACKDOWN) {
|
|
|
|
int Opcode = isU6 ? XCore::EXTSP_u6 : XCore::EXTSP_lu6;
|
2009-02-12 23:08:38 +00:00
|
|
|
New=BuildMI(MF, Old->getDebugLoc(), TII.get(Opcode))
|
2008-11-07 10:59:00 +00:00
|
|
|
.addImm(Amount);
|
|
|
|
} else {
|
|
|
|
assert(Old->getOpcode() == XCore::ADJCALLSTACKUP);
|
|
|
|
int Opcode = isU6 ? XCore::LDAWSP_ru6_RRegs : XCore::LDAWSP_lru6_RRegs;
|
2009-02-12 23:08:38 +00:00
|
|
|
New=BuildMI(MF, Old->getDebugLoc(), TII.get(Opcode), XCore::SP)
|
2008-11-07 10:59:00 +00:00
|
|
|
.addImm(Amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the pseudo instruction with a new instruction...
|
|
|
|
MBB.insert(I, New);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MBB.erase(I);
|
|
|
|
}
|
|
|
|
|
2010-08-26 23:32:16 +00:00
|
|
|
void
|
2009-10-07 17:12:56 +00:00
|
|
|
XCoreRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
|
2010-08-26 23:32:16 +00:00
|
|
|
int SPAdj, RegScavenger *RS) const {
|
2008-11-07 10:59:00 +00:00
|
|
|
assert(SPAdj == 0 && "Unexpected");
|
|
|
|
MachineInstr &MI = *II;
|
2009-02-13 02:29:03 +00:00
|
|
|
DebugLoc dl = MI.getDebugLoc();
|
2008-11-07 10:59:00 +00:00
|
|
|
unsigned i = 0;
|
|
|
|
|
|
|
|
while (!MI.getOperand(i).isFI()) {
|
|
|
|
++i;
|
|
|
|
assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineOperand &FrameOp = MI.getOperand(i);
|
|
|
|
int FrameIndex = FrameOp.getIndex();
|
|
|
|
|
|
|
|
MachineFunction &MF = *MI.getParent()->getParent();
|
2011-01-10 12:39:04 +00:00
|
|
|
const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
|
2008-11-07 10:59:00 +00:00
|
|
|
int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
|
|
|
|
int StackSize = MF.getFrameInfo()->getStackSize();
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2009-07-25 00:23:56 +00:00
|
|
|
DEBUG(errs() << "\nFunction : "
|
|
|
|
<< MF.getFunction()->getName() << "\n");
|
2009-08-23 03:41:05 +00:00
|
|
|
DEBUG(errs() << "<--------->\n");
|
|
|
|
DEBUG(MI.print(errs()));
|
|
|
|
DEBUG(errs() << "FrameIndex : " << FrameIndex << "\n");
|
|
|
|
DEBUG(errs() << "FrameOffset : " << Offset << "\n");
|
|
|
|
DEBUG(errs() << "StackSize : " << StackSize << "\n");
|
2008-11-07 10:59:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
Offset += StackSize;
|
2011-07-14 14:03:48 +00:00
|
|
|
|
|
|
|
unsigned FrameReg = getFrameRegister(MF);
|
|
|
|
|
|
|
|
// Special handling of DBG_VALUE instructions.
|
|
|
|
if (MI.isDebugValue()) {
|
|
|
|
MI.getOperand(i).ChangeToRegister(FrameReg, false /*isDef*/);
|
|
|
|
MI.getOperand(i+1).ChangeToImmediate(Offset);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-07 10:59:00 +00:00
|
|
|
// fold constant into offset.
|
|
|
|
Offset += MI.getOperand(i + 1).getImm();
|
|
|
|
MI.getOperand(i + 1).ChangeToImmediate(0);
|
|
|
|
|
|
|
|
assert(Offset%4 == 0 && "Misaligned stack offset");
|
|
|
|
|
2009-08-23 06:49:22 +00:00
|
|
|
DEBUG(errs() << "Offset : " << Offset << "\n" << "<--------->\n");
|
2008-11-07 10:59:00 +00:00
|
|
|
|
|
|
|
Offset/=4;
|
|
|
|
|
2010-11-18 21:19:35 +00:00
|
|
|
bool FP = TFI->hasFP(MF);
|
2011-07-14 14:03:48 +00:00
|
|
|
|
2009-01-14 18:26:46 +00:00
|
|
|
unsigned Reg = MI.getOperand(0).getReg();
|
|
|
|
bool isKill = MI.getOpcode() == XCore::STWFI && MI.getOperand(0).isKill();
|
|
|
|
|
|
|
|
assert(XCore::GRRegsRegisterClass->contains(Reg) &&
|
|
|
|
"Unexpected register operand");
|
|
|
|
|
|
|
|
MachineBasicBlock &MBB = *MI.getParent();
|
|
|
|
|
2008-11-07 10:59:00 +00:00
|
|
|
if (FP) {
|
|
|
|
bool isUs = isImmUs(Offset);
|
|
|
|
|
|
|
|
if (!isUs) {
|
2010-04-08 10:44:28 +00:00
|
|
|
if (!RS)
|
|
|
|
report_fatal_error("eliminateFrameIndex Frame size too big: " +
|
|
|
|
Twine(Offset));
|
2008-11-07 10:59:00 +00:00
|
|
|
unsigned ScratchReg = RS->scavengeRegister(XCore::GRRegsRegisterClass, II,
|
|
|
|
SPAdj);
|
2009-02-13 02:29:03 +00:00
|
|
|
loadConstant(MBB, II, ScratchReg, Offset, dl);
|
2008-11-07 10:59:00 +00:00
|
|
|
switch (MI.getOpcode()) {
|
2009-01-14 18:26:46 +00:00
|
|
|
case XCore::LDWFI:
|
2009-09-06 12:41:19 +00:00
|
|
|
BuildMI(MBB, II, dl, TII.get(XCore::LDW_3r), Reg)
|
2011-07-14 14:03:48 +00:00
|
|
|
.addReg(FrameReg)
|
2009-05-13 21:33:08 +00:00
|
|
|
.addReg(ScratchReg, RegState::Kill);
|
2008-11-07 10:59:00 +00:00
|
|
|
break;
|
2009-01-14 18:26:46 +00:00
|
|
|
case XCore::STWFI:
|
2009-09-06 12:41:19 +00:00
|
|
|
BuildMI(MBB, II, dl, TII.get(XCore::STW_3r))
|
2009-05-13 21:33:08 +00:00
|
|
|
.addReg(Reg, getKillRegState(isKill))
|
2011-07-14 14:03:48 +00:00
|
|
|
.addReg(FrameReg)
|
2009-05-13 21:33:08 +00:00
|
|
|
.addReg(ScratchReg, RegState::Kill);
|
2008-11-07 10:59:00 +00:00
|
|
|
break;
|
2009-01-14 18:26:46 +00:00
|
|
|
case XCore::LDAWFI:
|
2009-09-06 12:41:19 +00:00
|
|
|
BuildMI(MBB, II, dl, TII.get(XCore::LDAWF_l3r), Reg)
|
2011-07-14 14:03:48 +00:00
|
|
|
.addReg(FrameReg)
|
2009-05-13 21:33:08 +00:00
|
|
|
.addReg(ScratchReg, RegState::Kill);
|
2008-11-07 10:59:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Unexpected Opcode");
|
2008-11-07 10:59:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (MI.getOpcode()) {
|
2009-01-14 18:26:46 +00:00
|
|
|
case XCore::LDWFI:
|
2009-09-06 12:41:19 +00:00
|
|
|
BuildMI(MBB, II, dl, TII.get(XCore::LDW_2rus), Reg)
|
2011-07-14 14:03:48 +00:00
|
|
|
.addReg(FrameReg)
|
2008-11-07 10:59:00 +00:00
|
|
|
.addImm(Offset);
|
|
|
|
break;
|
2009-01-14 18:26:46 +00:00
|
|
|
case XCore::STWFI:
|
2009-09-06 12:41:19 +00:00
|
|
|
BuildMI(MBB, II, dl, TII.get(XCore::STW_2rus))
|
2009-05-13 21:33:08 +00:00
|
|
|
.addReg(Reg, getKillRegState(isKill))
|
2011-07-14 14:03:48 +00:00
|
|
|
.addReg(FrameReg)
|
2008-11-07 10:59:00 +00:00
|
|
|
.addImm(Offset);
|
|
|
|
break;
|
2009-01-14 18:26:46 +00:00
|
|
|
case XCore::LDAWFI:
|
2009-09-06 12:41:19 +00:00
|
|
|
BuildMI(MBB, II, dl, TII.get(XCore::LDAWF_l2rus), Reg)
|
2011-07-14 14:03:48 +00:00
|
|
|
.addReg(FrameReg)
|
2008-11-07 10:59:00 +00:00
|
|
|
.addImm(Offset);
|
|
|
|
break;
|
|
|
|
default:
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Unexpected Opcode");
|
2008-11-07 10:59:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bool isU6 = isImmU6(Offset);
|
2010-04-08 10:44:28 +00:00
|
|
|
if (!isU6 && !isImmU16(Offset))
|
|
|
|
report_fatal_error("eliminateFrameIndex Frame size too big: " +
|
|
|
|
Twine(Offset));
|
2008-11-07 10:59:00 +00:00
|
|
|
|
2009-01-14 18:26:46 +00:00
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
int NewOpcode;
|
|
|
|
case XCore::LDWFI:
|
2008-11-07 10:59:00 +00:00
|
|
|
NewOpcode = (isU6) ? XCore::LDWSP_ru6 : XCore::LDWSP_lru6;
|
2009-02-13 02:29:03 +00:00
|
|
|
BuildMI(MBB, II, dl, TII.get(NewOpcode), Reg)
|
2009-01-14 18:26:46 +00:00
|
|
|
.addImm(Offset);
|
2008-11-07 10:59:00 +00:00
|
|
|
break;
|
2009-01-14 18:26:46 +00:00
|
|
|
case XCore::STWFI:
|
2008-11-07 10:59:00 +00:00
|
|
|
NewOpcode = (isU6) ? XCore::STWSP_ru6 : XCore::STWSP_lru6;
|
2009-02-13 02:29:03 +00:00
|
|
|
BuildMI(MBB, II, dl, TII.get(NewOpcode))
|
2009-05-13 21:33:08 +00:00
|
|
|
.addReg(Reg, getKillRegState(isKill))
|
2009-01-14 18:26:46 +00:00
|
|
|
.addImm(Offset);
|
2008-11-07 10:59:00 +00:00
|
|
|
break;
|
2009-01-14 18:26:46 +00:00
|
|
|
case XCore::LDAWFI:
|
2008-11-07 10:59:00 +00:00
|
|
|
NewOpcode = (isU6) ? XCore::LDAWSP_ru6 : XCore::LDAWSP_lru6;
|
2009-02-13 02:29:03 +00:00
|
|
|
BuildMI(MBB, II, dl, TII.get(NewOpcode), Reg)
|
2009-01-14 18:26:46 +00:00
|
|
|
.addImm(Offset);
|
2008-11-07 10:59:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Unexpected Opcode");
|
2008-11-07 10:59:00 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-14 18:26:46 +00:00
|
|
|
// Erase old instruction.
|
|
|
|
MBB.erase(II);
|
2008-11-07 10:59:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void XCoreRegisterInfo::
|
|
|
|
loadConstant(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
|
2009-02-13 02:29:03 +00:00
|
|
|
unsigned DstReg, int64_t Value, DebugLoc dl) const {
|
2008-11-07 10:59:00 +00:00
|
|
|
// TODO use mkmsk if possible.
|
|
|
|
if (!isImmU16(Value)) {
|
|
|
|
// TODO use constant pool.
|
2010-04-08 10:44:28 +00:00
|
|
|
report_fatal_error("loadConstant value too big " + Twine(Value));
|
2008-11-07 10:59:00 +00:00
|
|
|
}
|
|
|
|
int Opcode = isImmU6(Value) ? XCore::LDC_ru6 : XCore::LDC_lru6;
|
2009-02-13 02:29:03 +00:00
|
|
|
BuildMI(MBB, I, dl, TII.get(Opcode), DstReg).addImm(Value);
|
2008-11-07 10:59:00 +00:00
|
|
|
}
|
|
|
|
|
2009-11-12 20:49:22 +00:00
|
|
|
unsigned XCoreRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
|
2011-01-10 12:39:04 +00:00
|
|
|
const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
|
2010-11-15 00:06:54 +00:00
|
|
|
|
2010-11-18 21:19:35 +00:00
|
|
|
return TFI->hasFP(MF) ? XCore::R10 : XCore::SP;
|
2008-11-07 10:59:00 +00:00
|
|
|
}
|