2009-08-02 19:32:10 +02:00
|
|
|
//===- BlackfinRegisterInfo.cpp - Blackfin 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 Blackfin implementation of the TargetRegisterInfo
|
|
|
|
// class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Blackfin.h"
|
|
|
|
#include "BlackfinRegisterInfo.h"
|
|
|
|
#include "BlackfinSubtarget.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineLocation.h"
|
|
|
|
#include "llvm/CodeGen/RegisterScavenging.h"
|
2011-01-10 13:39:04 +01:00
|
|
|
#include "llvm/Target/TargetFrameLowering.h"
|
2009-08-02 19:32:10 +02:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Type.h"
|
|
|
|
#include "llvm/ADT/BitVector.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
BlackfinRegisterInfo::BlackfinRegisterInfo(BlackfinSubtarget &st,
|
|
|
|
const TargetInstrInfo &tii)
|
|
|
|
: BlackfinGenRegisterInfo(BF::ADJCALLSTACKDOWN, BF::ADJCALLSTACKUP),
|
|
|
|
Subtarget(st),
|
|
|
|
TII(tii) {}
|
|
|
|
|
|
|
|
const unsigned*
|
|
|
|
BlackfinRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
|
|
|
|
using namespace BF;
|
|
|
|
static const unsigned CalleeSavedRegs[] = {
|
|
|
|
FP,
|
|
|
|
R4, R5, R6, R7,
|
|
|
|
P3, P4, P5,
|
|
|
|
0 };
|
|
|
|
return CalleeSavedRegs;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitVector
|
|
|
|
BlackfinRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
|
2011-01-10 13:39:04 +01:00
|
|
|
const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
|
2010-11-18 22:19:35 +01:00
|
|
|
|
2009-08-02 19:32:10 +02:00
|
|
|
using namespace BF;
|
|
|
|
BitVector Reserved(getNumRegs());
|
2009-08-04 21:16:55 +02:00
|
|
|
Reserved.set(AZ);
|
|
|
|
Reserved.set(AN);
|
|
|
|
Reserved.set(AQ);
|
|
|
|
Reserved.set(AC0);
|
|
|
|
Reserved.set(AC1);
|
|
|
|
Reserved.set(AV0);
|
|
|
|
Reserved.set(AV0S);
|
|
|
|
Reserved.set(AV1);
|
|
|
|
Reserved.set(AV1S);
|
|
|
|
Reserved.set(V);
|
|
|
|
Reserved.set(VS);
|
2009-08-08 23:42:22 +02:00
|
|
|
Reserved.set(CYCLES).set(CYCLES2);
|
2009-08-02 19:32:10 +02:00
|
|
|
Reserved.set(L0);
|
|
|
|
Reserved.set(L1);
|
|
|
|
Reserved.set(L2);
|
|
|
|
Reserved.set(L3);
|
|
|
|
Reserved.set(SP);
|
|
|
|
Reserved.set(RETS);
|
2010-11-18 22:19:35 +01:00
|
|
|
if (TFI->hasFP(MF))
|
2009-08-02 19:32:10 +02:00
|
|
|
Reserved.set(FP);
|
|
|
|
return Reserved;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BlackfinRegisterInfo::
|
|
|
|
requiresRegisterScavenging(const MachineFunction &MF) const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit instructions to add delta to D/P register. ScratchReg must be of the
|
|
|
|
// same class as Reg (P).
|
|
|
|
void BlackfinRegisterInfo::adjustRegister(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I,
|
|
|
|
DebugLoc DL,
|
|
|
|
unsigned Reg,
|
|
|
|
unsigned ScratchReg,
|
|
|
|
int delta) const {
|
|
|
|
if (!delta)
|
|
|
|
return;
|
2009-08-12 08:22:07 +02:00
|
|
|
if (isInt<7>(delta)) {
|
2009-08-02 19:32:10 +02:00
|
|
|
BuildMI(MBB, I, DL, TII.get(BF::ADDpp_imm7), Reg)
|
|
|
|
.addReg(Reg) // No kill on two-addr operand
|
|
|
|
.addImm(delta);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We must load delta into ScratchReg and add that.
|
|
|
|
loadConstant(MBB, I, DL, ScratchReg, delta);
|
|
|
|
if (BF::PRegClass.contains(Reg)) {
|
2009-08-03 21:32:30 +02:00
|
|
|
assert(BF::PRegClass.contains(ScratchReg) &&
|
|
|
|
"ScratchReg must be a P register");
|
2009-08-02 19:32:10 +02:00
|
|
|
BuildMI(MBB, I, DL, TII.get(BF::ADDpp), Reg)
|
|
|
|
.addReg(Reg, RegState::Kill)
|
|
|
|
.addReg(ScratchReg, RegState::Kill);
|
|
|
|
} else {
|
2009-08-03 21:32:30 +02:00
|
|
|
assert(BF::DRegClass.contains(Reg) && "Reg must be a D or P register");
|
|
|
|
assert(BF::DRegClass.contains(ScratchReg) &&
|
|
|
|
"ScratchReg must be a D register");
|
2009-08-02 19:32:10 +02:00
|
|
|
BuildMI(MBB, I, DL, TII.get(BF::ADD), Reg)
|
|
|
|
.addReg(Reg, RegState::Kill)
|
|
|
|
.addReg(ScratchReg, RegState::Kill);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit instructions to load a constant into D/P register
|
|
|
|
void BlackfinRegisterInfo::loadConstant(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I,
|
|
|
|
DebugLoc DL,
|
|
|
|
unsigned Reg,
|
|
|
|
int value) const {
|
2009-08-12 08:22:07 +02:00
|
|
|
if (isInt<7>(value)) {
|
2009-08-02 19:32:10 +02:00
|
|
|
BuildMI(MBB, I, DL, TII.get(BF::LOADimm7), Reg).addImm(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-29 23:13:41 +02:00
|
|
|
if (isUInt<16>(value)) {
|
2009-08-02 19:32:10 +02:00
|
|
|
BuildMI(MBB, I, DL, TII.get(BF::LOADuimm16), Reg).addImm(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-12 08:22:07 +02:00
|
|
|
if (isInt<16>(value)) {
|
2009-08-02 19:32:10 +02:00
|
|
|
BuildMI(MBB, I, DL, TII.get(BF::LOADimm16), Reg).addImm(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We must split into halves
|
|
|
|
BuildMI(MBB, I, DL,
|
2010-05-24 16:48:12 +02:00
|
|
|
TII.get(BF::LOAD16i), getSubReg(Reg, BF::hi16))
|
2009-08-02 19:32:10 +02:00
|
|
|
.addImm((value >> 16) & 0xffff)
|
|
|
|
.addReg(Reg, RegState::ImplicitDefine);
|
|
|
|
BuildMI(MBB, I, DL,
|
2010-05-24 16:48:12 +02:00
|
|
|
TII.get(BF::LOAD16i), getSubReg(Reg, BF::lo16))
|
2009-08-02 19:32:10 +02:00
|
|
|
.addImm(value & 0xffff)
|
|
|
|
.addReg(Reg, RegState::ImplicitKill)
|
|
|
|
.addReg(Reg, RegState::ImplicitDefine);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlackfinRegisterInfo::
|
|
|
|
eliminateCallFramePseudoInstr(MachineFunction &MF,
|
|
|
|
MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I) const {
|
2011-01-10 13:39:04 +01:00
|
|
|
const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
|
2010-11-18 22:19:35 +01:00
|
|
|
|
|
|
|
if (!TFI->hasReservedCallFrame(MF)) {
|
2009-08-02 19:32:10 +02:00
|
|
|
int64_t Amount = I->getOperand(0).getImm();
|
|
|
|
if (Amount != 0) {
|
2009-08-03 21:32:30 +02:00
|
|
|
assert(Amount%4 == 0 && "Unaligned call frame size");
|
2009-08-02 19:32:10 +02:00
|
|
|
if (I->getOpcode() == BF::ADJCALLSTACKDOWN) {
|
|
|
|
adjustRegister(MBB, I, I->getDebugLoc(), BF::SP, BF::P1, -Amount);
|
|
|
|
} else {
|
2009-08-03 21:32:30 +02:00
|
|
|
assert(I->getOpcode() == BF::ADJCALLSTACKUP &&
|
|
|
|
"Unknown call frame pseudo instruction");
|
2009-08-02 19:32:10 +02:00
|
|
|
adjustRegister(MBB, I, I->getDebugLoc(), BF::SP, BF::P1, Amount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MBB.erase(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// findScratchRegister - Find a 'free' register. Try for a call-clobbered
|
|
|
|
/// register first and then a spilled callee-saved register if that fails.
|
|
|
|
static unsigned findScratchRegister(MachineBasicBlock::iterator II,
|
|
|
|
RegScavenger *RS,
|
|
|
|
const TargetRegisterClass *RC,
|
|
|
|
int SPAdj) {
|
|
|
|
assert(RS && "Register scavenging must be on");
|
2009-08-18 23:14:54 +02:00
|
|
|
unsigned Reg = RS->FindUnusedReg(RC);
|
2009-08-02 19:32:10 +02:00
|
|
|
if (Reg == 0)
|
|
|
|
Reg = RS->scavengeRegister(RC, II, SPAdj);
|
|
|
|
return Reg;
|
|
|
|
}
|
|
|
|
|
2010-08-27 01:32:16 +02:00
|
|
|
void
|
2009-10-07 19:12:56 +02:00
|
|
|
BlackfinRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
|
2010-08-27 01:32:16 +02:00
|
|
|
int SPAdj, RegScavenger *RS) const {
|
2009-08-02 19:32:10 +02:00
|
|
|
MachineInstr &MI = *II;
|
|
|
|
MachineBasicBlock &MBB = *MI.getParent();
|
|
|
|
MachineFunction &MF = *MBB.getParent();
|
2011-01-10 13:39:04 +01:00
|
|
|
const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
|
2009-08-02 19:32:10 +02:00
|
|
|
DebugLoc DL = MI.getDebugLoc();
|
|
|
|
|
2009-08-03 21:32:30 +02:00
|
|
|
unsigned FIPos;
|
|
|
|
for (FIPos=0; !MI.getOperand(FIPos).isFI(); ++FIPos) {
|
|
|
|
assert(FIPos < MI.getNumOperands() &&
|
|
|
|
"Instr doesn't have FrameIndex operand!");
|
2009-08-02 19:32:10 +02:00
|
|
|
}
|
2009-08-03 21:32:30 +02:00
|
|
|
int FrameIndex = MI.getOperand(FIPos).getIndex();
|
|
|
|
assert(FIPos+1 < MI.getNumOperands() && MI.getOperand(FIPos+1).isImm());
|
2009-08-02 19:32:10 +02:00
|
|
|
int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex)
|
2009-08-03 21:32:30 +02:00
|
|
|
+ MI.getOperand(FIPos+1).getImm();
|
2009-08-02 19:32:10 +02:00
|
|
|
unsigned BaseReg = BF::FP;
|
2010-11-18 22:19:35 +01:00
|
|
|
if (TFI->hasFP(MF)) {
|
2009-08-03 21:32:30 +02:00
|
|
|
assert(SPAdj==0 && "Unexpected SP adjust in function with frame pointer");
|
2009-08-02 19:32:10 +02:00
|
|
|
} else {
|
|
|
|
BaseReg = BF::SP;
|
|
|
|
Offset += MF.getFrameInfo()->getStackSize() + SPAdj;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isStore = false;
|
|
|
|
|
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
case BF::STORE32fi:
|
|
|
|
isStore = true;
|
|
|
|
case BF::LOAD32fi: {
|
2009-08-03 21:32:30 +02:00
|
|
|
assert(Offset%4 == 0 && "Unaligned i32 stack access");
|
|
|
|
assert(FIPos==1 && "Bad frame index operand");
|
|
|
|
MI.getOperand(FIPos).ChangeToRegister(BaseReg, false);
|
|
|
|
MI.getOperand(FIPos+1).setImm(Offset);
|
2010-03-29 23:13:41 +02:00
|
|
|
if (isUInt<6>(Offset)) {
|
2009-08-02 19:32:10 +02:00
|
|
|
MI.setDesc(TII.get(isStore
|
|
|
|
? BF::STORE32p_uimm6m4
|
|
|
|
: BF::LOAD32p_uimm6m4));
|
2010-08-27 01:32:16 +02:00
|
|
|
return;
|
2009-08-02 19:32:10 +02:00
|
|
|
}
|
2010-03-29 23:13:41 +02:00
|
|
|
if (BaseReg == BF::FP && isUInt<7>(-Offset)) {
|
2009-08-02 19:32:10 +02:00
|
|
|
MI.setDesc(TII.get(isStore
|
|
|
|
? BF::STORE32fp_nimm7m4
|
|
|
|
: BF::LOAD32fp_nimm7m4));
|
2009-08-03 21:32:30 +02:00
|
|
|
MI.getOperand(FIPos+1).setImm(-Offset);
|
2010-08-27 01:32:16 +02:00
|
|
|
return;
|
2009-08-02 19:32:10 +02:00
|
|
|
}
|
2009-08-12 08:22:07 +02:00
|
|
|
if (isInt<18>(Offset)) {
|
2009-08-02 19:32:10 +02:00
|
|
|
MI.setDesc(TII.get(isStore
|
|
|
|
? BF::STORE32p_imm18m4
|
|
|
|
: BF::LOAD32p_imm18m4));
|
2010-08-27 01:32:16 +02:00
|
|
|
return;
|
2009-08-02 19:32:10 +02:00
|
|
|
}
|
|
|
|
// Use RegScavenger to calculate proper offset...
|
|
|
|
MI.dump();
|
|
|
|
llvm_unreachable("Stack frame offset too big");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BF::ADDpp: {
|
2009-08-03 21:32:30 +02:00
|
|
|
assert(MI.getOperand(0).isReg() && "ADD instruction needs a register");
|
2009-08-02 19:32:10 +02:00
|
|
|
unsigned DestReg = MI.getOperand(0).getReg();
|
|
|
|
// We need to produce a stack offset in a P register. We emit:
|
|
|
|
// P0 = offset;
|
|
|
|
// P0 = BR + P0;
|
2009-08-03 21:32:30 +02:00
|
|
|
assert(FIPos==1 && "Bad frame index operand");
|
2009-08-02 19:32:10 +02:00
|
|
|
loadConstant(MBB, II, DL, DestReg, Offset);
|
|
|
|
MI.getOperand(1).ChangeToRegister(DestReg, false, false, true);
|
|
|
|
MI.getOperand(2).ChangeToRegister(BaseReg, false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BF::STORE16fi:
|
|
|
|
isStore = true;
|
|
|
|
case BF::LOAD16fi: {
|
2009-08-03 21:32:30 +02:00
|
|
|
assert(Offset%2 == 0 && "Unaligned i16 stack access");
|
|
|
|
assert(FIPos==1 && "Bad frame index operand");
|
2009-08-02 19:32:10 +02:00
|
|
|
// We need a P register to use as an address
|
|
|
|
unsigned ScratchReg = findScratchRegister(II, RS, &BF::PRegClass, SPAdj);
|
2009-08-03 21:32:30 +02:00
|
|
|
assert(ScratchReg && "Could not scavenge register");
|
2009-08-02 19:32:10 +02:00
|
|
|
loadConstant(MBB, II, DL, ScratchReg, Offset);
|
|
|
|
BuildMI(MBB, II, DL, TII.get(BF::ADDpp), ScratchReg)
|
|
|
|
.addReg(ScratchReg, RegState::Kill)
|
|
|
|
.addReg(BaseReg);
|
|
|
|
MI.setDesc(TII.get(isStore ? BF::STORE16pi : BF::LOAD16pi));
|
|
|
|
MI.getOperand(1).ChangeToRegister(ScratchReg, false, false, true);
|
|
|
|
MI.RemoveOperand(2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BF::STORE8fi: {
|
|
|
|
// This is an AnyCC spill, we need a scratch register.
|
2009-08-03 21:32:30 +02:00
|
|
|
assert(FIPos==1 && "Bad frame index operand");
|
2009-08-02 19:32:10 +02:00
|
|
|
MachineOperand SpillReg = MI.getOperand(0);
|
|
|
|
unsigned ScratchReg = findScratchRegister(II, RS, &BF::DRegClass, SPAdj);
|
2009-08-03 21:32:30 +02:00
|
|
|
assert(ScratchReg && "Could not scavenge register");
|
2009-08-02 19:32:10 +02:00
|
|
|
if (SpillReg.getReg()==BF::NCC) {
|
|
|
|
BuildMI(MBB, II, DL, TII.get(BF::MOVENCC_z), ScratchReg)
|
|
|
|
.addOperand(SpillReg);
|
|
|
|
BuildMI(MBB, II, DL, TII.get(BF::BITTGL), ScratchReg)
|
|
|
|
.addReg(ScratchReg).addImm(0);
|
|
|
|
} else {
|
|
|
|
BuildMI(MBB, II, DL, TII.get(BF::MOVECC_zext), ScratchReg)
|
|
|
|
.addOperand(SpillReg);
|
|
|
|
}
|
|
|
|
// STORE D
|
|
|
|
MI.setDesc(TII.get(BF::STORE8p_imm16));
|
|
|
|
MI.getOperand(0).ChangeToRegister(ScratchReg, false, false, true);
|
2009-08-03 21:32:30 +02:00
|
|
|
MI.getOperand(FIPos).ChangeToRegister(BaseReg, false);
|
|
|
|
MI.getOperand(FIPos+1).setImm(Offset);
|
2009-08-02 19:32:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BF::LOAD8fi: {
|
|
|
|
// This is an restore, we need a scratch register.
|
2009-08-03 21:32:30 +02:00
|
|
|
assert(FIPos==1 && "Bad frame index operand");
|
2009-08-02 19:32:10 +02:00
|
|
|
MachineOperand SpillReg = MI.getOperand(0);
|
|
|
|
unsigned ScratchReg = findScratchRegister(II, RS, &BF::DRegClass, SPAdj);
|
2009-08-03 21:32:30 +02:00
|
|
|
assert(ScratchReg && "Could not scavenge register");
|
2009-08-02 19:32:10 +02:00
|
|
|
MI.setDesc(TII.get(BF::LOAD32p_imm16_8z));
|
|
|
|
MI.getOperand(0).ChangeToRegister(ScratchReg, true);
|
2009-08-03 21:32:30 +02:00
|
|
|
MI.getOperand(FIPos).ChangeToRegister(BaseReg, false);
|
|
|
|
MI.getOperand(FIPos+1).setImm(Offset);
|
2009-08-02 19:32:10 +02:00
|
|
|
++II;
|
|
|
|
if (SpillReg.getReg()==BF::CC) {
|
|
|
|
// CC = D
|
|
|
|
BuildMI(MBB, II, DL, TII.get(BF::MOVECC_nz), BF::CC)
|
|
|
|
.addReg(ScratchReg, RegState::Kill);
|
|
|
|
} else {
|
|
|
|
// Restore NCC (CC = D==0)
|
|
|
|
BuildMI(MBB, II, DL, TII.get(BF::SETEQri_not), BF::NCC)
|
|
|
|
.addReg(ScratchReg, RegState::Kill)
|
|
|
|
.addImm(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Cannot eliminate frame index");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned BlackfinRegisterInfo::getRARegister() const {
|
|
|
|
return BF::RETS;
|
|
|
|
}
|
|
|
|
|
2009-11-12 21:49:22 +01:00
|
|
|
unsigned
|
|
|
|
BlackfinRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
|
2011-01-10 13:39:04 +01:00
|
|
|
const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
|
2010-11-18 22:19:35 +01:00
|
|
|
|
|
|
|
return TFI->hasFP(MF) ? BF::FP : BF::SP;
|
2009-08-02 19:32:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned BlackfinRegisterInfo::getEHExceptionRegister() const {
|
|
|
|
llvm_unreachable("What is the exception register");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned BlackfinRegisterInfo::getEHHandlerRegister() const {
|
|
|
|
llvm_unreachable("What is the exception handler register");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BlackfinRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
|
|
|
|
llvm_unreachable("What is the dwarf register number");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "BlackfinGenRegisterInfo.inc"
|
|
|
|
|