2012-02-18 13:03:15 +01:00
|
|
|
//===-- XCoreInstrInfo.cpp - XCore Instruction Information ----------------===//
|
2008-11-07 11:59:00 +01:00
|
|
|
//
|
|
|
|
// 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 TargetInstrInfo class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "XCoreInstrInfo.h"
|
|
|
|
#include "XCore.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "XCoreMachineFunctionInfo.h"
|
2010-04-05 07:48:36 +02:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
2008-11-07 11:59:00 +01:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 22:10:48 +02:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2011-08-24 20:08:43 +02:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2008-11-07 11:59:00 +01:00
|
|
|
|
2011-07-01 19:57:27 +02:00
|
|
|
#define GET_INSTRINFO_CTOR
|
2011-06-28 22:07:07 +02:00
|
|
|
#include "XCoreGenInstrInfo.inc"
|
|
|
|
|
2008-11-07 11:59:00 +01:00
|
|
|
namespace llvm {
|
|
|
|
namespace XCore {
|
|
|
|
|
|
|
|
// XCore Condition Codes
|
|
|
|
enum CondCode {
|
|
|
|
COND_TRUE,
|
|
|
|
COND_FALSE,
|
|
|
|
COND_INVALID
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2009-08-13 00:10:57 +02:00
|
|
|
XCoreInstrInfo::XCoreInstrInfo()
|
2011-07-01 19:57:27 +02:00
|
|
|
: XCoreGenInstrInfo(XCore::ADJCALLSTACKDOWN, XCore::ADJCALLSTACKUP),
|
2013-06-07 23:04:35 +02:00
|
|
|
RI() {
|
2008-11-07 11:59:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isZeroImm(const MachineOperand &op) {
|
|
|
|
return op.isImm() && op.getImm() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// isLoadFromStackSlot - If the specified machine instruction is a direct
|
|
|
|
/// load from a stack slot, return the virtual or physical register number of
|
|
|
|
/// the destination along with the FrameIndex of the loaded stack slot. If
|
|
|
|
/// not, return 0. This predicate must return 0 if the instruction has
|
|
|
|
/// any side effects other than loading from the stack slot.
|
|
|
|
unsigned
|
2008-11-18 20:49:32 +01:00
|
|
|
XCoreInstrInfo::isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const{
|
2008-11-07 11:59:00 +01:00
|
|
|
int Opcode = MI->getOpcode();
|
2009-01-14 19:26:46 +01:00
|
|
|
if (Opcode == XCore::LDWFI)
|
2008-11-07 11:59:00 +01:00
|
|
|
{
|
|
|
|
if ((MI->getOperand(1).isFI()) && // is a stack slot
|
|
|
|
(MI->getOperand(2).isImm()) && // the imm is zero
|
|
|
|
(isZeroImm(MI->getOperand(2))))
|
|
|
|
{
|
|
|
|
FrameIndex = MI->getOperand(1).getIndex();
|
|
|
|
return MI->getOperand(0).getReg();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// isStoreToStackSlot - If the specified machine instruction is a direct
|
|
|
|
/// store to a stack slot, return the virtual or physical register number of
|
|
|
|
/// the source reg along with the FrameIndex of the loaded stack slot. If
|
|
|
|
/// not, return 0. This predicate must return 0 if the instruction has
|
|
|
|
/// any side effects other than storing to the stack slot.
|
|
|
|
unsigned
|
2008-11-18 20:49:32 +01:00
|
|
|
XCoreInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
|
|
|
|
int &FrameIndex) const {
|
2008-11-07 11:59:00 +01:00
|
|
|
int Opcode = MI->getOpcode();
|
2009-01-14 19:26:46 +01:00
|
|
|
if (Opcode == XCore::STWFI)
|
2008-11-07 11:59:00 +01:00
|
|
|
{
|
|
|
|
if ((MI->getOperand(1).isFI()) && // is a stack slot
|
|
|
|
(MI->getOperand(2).isImm()) && // the imm is zero
|
2009-01-14 19:26:46 +01:00
|
|
|
(isZeroImm(MI->getOperand(2))))
|
2008-11-07 11:59:00 +01:00
|
|
|
{
|
|
|
|
FrameIndex = MI->getOperand(1).getIndex();
|
|
|
|
return MI->getOperand(0).getReg();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Branch Analysis
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static inline bool IsBRU(unsigned BrOpc) {
|
|
|
|
return BrOpc == XCore::BRFU_u6
|
|
|
|
|| BrOpc == XCore::BRFU_lu6
|
|
|
|
|| BrOpc == XCore::BRBU_u6
|
|
|
|
|| BrOpc == XCore::BRBU_lu6;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool IsBRT(unsigned BrOpc) {
|
|
|
|
return BrOpc == XCore::BRFT_ru6
|
|
|
|
|| BrOpc == XCore::BRFT_lru6
|
|
|
|
|| BrOpc == XCore::BRBT_ru6
|
|
|
|
|| BrOpc == XCore::BRBT_lru6;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool IsBRF(unsigned BrOpc) {
|
|
|
|
return BrOpc == XCore::BRFF_ru6
|
|
|
|
|| BrOpc == XCore::BRFF_lru6
|
|
|
|
|| BrOpc == XCore::BRBF_ru6
|
|
|
|
|| BrOpc == XCore::BRBF_lru6;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool IsCondBranch(unsigned BrOpc) {
|
|
|
|
return IsBRF(BrOpc) || IsBRT(BrOpc);
|
|
|
|
}
|
|
|
|
|
2010-02-23 14:25:07 +01:00
|
|
|
static inline bool IsBR_JT(unsigned BrOpc) {
|
|
|
|
return BrOpc == XCore::BR_JT
|
|
|
|
|| BrOpc == XCore::BR_JT32;
|
|
|
|
}
|
|
|
|
|
2008-11-07 11:59:00 +01:00
|
|
|
/// GetCondFromBranchOpc - Return the XCore CC that matches
|
|
|
|
/// the correspondent Branch instruction opcode.
|
|
|
|
static XCore::CondCode GetCondFromBranchOpc(unsigned BrOpc)
|
|
|
|
{
|
|
|
|
if (IsBRT(BrOpc)) {
|
|
|
|
return XCore::COND_TRUE;
|
|
|
|
} else if (IsBRF(BrOpc)) {
|
|
|
|
return XCore::COND_FALSE;
|
|
|
|
} else {
|
|
|
|
return XCore::COND_INVALID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// GetCondBranchFromCond - Return the Branch instruction
|
|
|
|
/// opcode that matches the cc.
|
|
|
|
static inline unsigned GetCondBranchFromCond(XCore::CondCode CC)
|
|
|
|
{
|
|
|
|
switch (CC) {
|
2009-07-14 18:55:14 +02:00
|
|
|
default: llvm_unreachable("Illegal condition code!");
|
2008-11-07 11:59:00 +01:00
|
|
|
case XCore::COND_TRUE : return XCore::BRFT_lru6;
|
|
|
|
case XCore::COND_FALSE : return XCore::BRFF_lru6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// GetOppositeBranchCondition - Return the inverse of the specified
|
|
|
|
/// condition, e.g. turning COND_E to COND_NE.
|
|
|
|
static inline XCore::CondCode GetOppositeBranchCondition(XCore::CondCode CC)
|
|
|
|
{
|
|
|
|
switch (CC) {
|
2009-07-14 18:55:14 +02:00
|
|
|
default: llvm_unreachable("Illegal condition code!");
|
2008-11-07 11:59:00 +01:00
|
|
|
case XCore::COND_TRUE : return XCore::COND_FALSE;
|
|
|
|
case XCore::COND_FALSE : return XCore::COND_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
|
|
|
|
/// true if it cannot be understood (e.g. it's a switch dispatch or isn't
|
|
|
|
/// implemented for a target). Upon success, this returns false and returns
|
|
|
|
/// with the following information in various cases:
|
|
|
|
///
|
|
|
|
/// 1. If this block ends with no branches (it just falls through to its succ)
|
|
|
|
/// just return false, leaving TBB/FBB null.
|
|
|
|
/// 2. If this block ends with only an unconditional branch, it sets TBB to be
|
|
|
|
/// the destination block.
|
|
|
|
/// 3. If this block ends with an conditional branch and it falls through to
|
|
|
|
/// an successor block, it sets TBB to be the branch destination block and a
|
|
|
|
/// list of operands that evaluate the condition. These
|
|
|
|
/// operands can be passed to other TargetInstrInfo methods to create new
|
|
|
|
/// branches.
|
|
|
|
/// 4. If this block ends with an conditional branch and an unconditional
|
|
|
|
/// block, it returns the 'true' destination in TBB, the 'false' destination
|
|
|
|
/// in FBB, and a list of operands that evaluate the condition. These
|
|
|
|
/// operands can be passed to other TargetInstrInfo methods to create new
|
|
|
|
/// branches.
|
|
|
|
///
|
|
|
|
/// Note that RemoveBranch and InsertBranch must be implemented to support
|
|
|
|
/// cases where this method returns success.
|
|
|
|
///
|
|
|
|
bool
|
|
|
|
XCoreInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
|
2009-02-09 08:14:22 +01:00
|
|
|
MachineBasicBlock *&FBB,
|
|
|
|
SmallVectorImpl<MachineOperand> &Cond,
|
|
|
|
bool AllowModify) const {
|
2008-11-07 11:59:00 +01:00
|
|
|
// If the block has no terminators, it just falls into the block after it.
|
|
|
|
MachineBasicBlock::iterator I = MBB.end();
|
2010-04-02 03:38:09 +02:00
|
|
|
if (I == MBB.begin())
|
|
|
|
return false;
|
|
|
|
--I;
|
|
|
|
while (I->isDebugValue()) {
|
|
|
|
if (I == MBB.begin())
|
|
|
|
return false;
|
|
|
|
--I;
|
|
|
|
}
|
|
|
|
if (!isUnpredicatedTerminator(I))
|
2008-11-07 11:59:00 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Get the last instruction in the block.
|
|
|
|
MachineInstr *LastInst = I;
|
|
|
|
|
|
|
|
// If there is only one terminator instruction, process it.
|
|
|
|
if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
|
|
|
|
if (IsBRU(LastInst->getOpcode())) {
|
|
|
|
TBB = LastInst->getOperand(0).getMBB();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
XCore::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
|
|
|
|
if (BranchCode == XCore::COND_INVALID)
|
|
|
|
return true; // Can't handle indirect branch.
|
|
|
|
|
|
|
|
// Conditional branch
|
|
|
|
// Block ends with fall-through condbranch.
|
|
|
|
|
|
|
|
TBB = LastInst->getOperand(1).getMBB();
|
|
|
|
Cond.push_back(MachineOperand::CreateImm(BranchCode));
|
|
|
|
Cond.push_back(LastInst->getOperand(0));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the instruction before it if it's a terminator.
|
|
|
|
MachineInstr *SecondLastInst = I;
|
|
|
|
|
|
|
|
// If there are three terminators, we don't know what sort of block this is.
|
|
|
|
if (SecondLastInst && I != MBB.begin() &&
|
|
|
|
isUnpredicatedTerminator(--I))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
unsigned SecondLastOpc = SecondLastInst->getOpcode();
|
|
|
|
XCore::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
|
|
|
|
|
|
|
|
// If the block ends with conditional branch followed by unconditional,
|
|
|
|
// handle it.
|
|
|
|
if (BranchCode != XCore::COND_INVALID
|
|
|
|
&& IsBRU(LastInst->getOpcode())) {
|
|
|
|
|
|
|
|
TBB = SecondLastInst->getOperand(1).getMBB();
|
|
|
|
Cond.push_back(MachineOperand::CreateImm(BranchCode));
|
|
|
|
Cond.push_back(SecondLastInst->getOperand(0));
|
|
|
|
|
|
|
|
FBB = LastInst->getOperand(0).getMBB();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the block ends with two unconditional branches, handle it. The second
|
|
|
|
// one is not executed, so remove it.
|
|
|
|
if (IsBRU(SecondLastInst->getOpcode()) &&
|
|
|
|
IsBRU(LastInst->getOpcode())) {
|
|
|
|
TBB = SecondLastInst->getOperand(0).getMBB();
|
|
|
|
I = LastInst;
|
2009-02-09 08:14:22 +01:00
|
|
|
if (AllowModify)
|
|
|
|
I->eraseFromParent();
|
2008-11-07 11:59:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-23 14:25:07 +01:00
|
|
|
// Likewise if it ends with a branch table followed by an unconditional branch.
|
|
|
|
if (IsBR_JT(SecondLastInst->getOpcode()) && IsBRU(LastInst->getOpcode())) {
|
|
|
|
I = LastInst;
|
|
|
|
if (AllowModify)
|
|
|
|
I->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-11-07 11:59:00 +01:00
|
|
|
// Otherwise, can't handle this.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
XCoreInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
|
|
|
|
MachineBasicBlock *FBB,
|
2010-06-18 00:43:56 +02:00
|
|
|
const SmallVectorImpl<MachineOperand> &Cond,
|
|
|
|
DebugLoc DL)const{
|
2008-11-07 11:59:00 +01:00
|
|
|
// Shouldn't be a fall through.
|
|
|
|
assert(TBB && "InsertBranch must not be told to insert a fallthrough");
|
|
|
|
assert((Cond.size() == 2 || Cond.size() == 0) &&
|
|
|
|
"Unexpected number of components!");
|
|
|
|
|
|
|
|
if (FBB == 0) { // One way branch.
|
|
|
|
if (Cond.empty()) {
|
|
|
|
// Unconditional branch
|
2010-06-18 00:43:56 +02:00
|
|
|
BuildMI(&MBB, DL, get(XCore::BRFU_lu6)).addMBB(TBB);
|
2008-11-07 11:59:00 +01:00
|
|
|
} else {
|
|
|
|
// Conditional branch.
|
|
|
|
unsigned Opc = GetCondBranchFromCond((XCore::CondCode)Cond[0].getImm());
|
2010-06-18 00:43:56 +02:00
|
|
|
BuildMI(&MBB, DL, get(Opc)).addReg(Cond[1].getReg())
|
2008-11-07 11:59:00 +01:00
|
|
|
.addMBB(TBB);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Two-way Conditional branch.
|
|
|
|
assert(Cond.size() == 2 && "Unexpected number of components!");
|
|
|
|
unsigned Opc = GetCondBranchFromCond((XCore::CondCode)Cond[0].getImm());
|
2010-06-18 00:43:56 +02:00
|
|
|
BuildMI(&MBB, DL, get(Opc)).addReg(Cond[1].getReg())
|
2008-11-07 11:59:00 +01:00
|
|
|
.addMBB(TBB);
|
2010-06-18 00:43:56 +02:00
|
|
|
BuildMI(&MBB, DL, get(XCore::BRFU_lu6)).addMBB(FBB);
|
2008-11-07 11:59:00 +01:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
XCoreInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
|
|
|
|
MachineBasicBlock::iterator I = MBB.end();
|
|
|
|
if (I == MBB.begin()) return 0;
|
|
|
|
--I;
|
2010-04-02 03:38:09 +02:00
|
|
|
while (I->isDebugValue()) {
|
|
|
|
if (I == MBB.begin())
|
|
|
|
return 0;
|
|
|
|
--I;
|
|
|
|
}
|
2008-11-07 11:59:00 +01:00
|
|
|
if (!IsBRU(I->getOpcode()) && !IsCondBranch(I->getOpcode()))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Remove the branch.
|
|
|
|
I->eraseFromParent();
|
|
|
|
|
|
|
|
I = MBB.end();
|
|
|
|
|
|
|
|
if (I == MBB.begin()) return 1;
|
|
|
|
--I;
|
|
|
|
if (!IsCondBranch(I->getOpcode()))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// Remove the branch.
|
|
|
|
I->eraseFromParent();
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2010-07-11 09:56:13 +02:00
|
|
|
void XCoreInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I, DebugLoc DL,
|
|
|
|
unsigned DestReg, unsigned SrcReg,
|
|
|
|
bool KillSrc) const {
|
|
|
|
bool GRDest = XCore::GRRegsRegClass.contains(DestReg);
|
|
|
|
bool GRSrc = XCore::GRRegsRegClass.contains(SrcReg);
|
|
|
|
|
|
|
|
if (GRDest && GRSrc) {
|
|
|
|
BuildMI(MBB, I, DL, get(XCore::ADD_2rus), DestReg)
|
|
|
|
.addReg(SrcReg, getKillRegState(KillSrc))
|
|
|
|
.addImm(0);
|
|
|
|
return;
|
2008-11-07 11:59:00 +01:00
|
|
|
}
|
|
|
|
|
2010-07-11 09:56:13 +02:00
|
|
|
if (GRDest && SrcReg == XCore::SP) {
|
|
|
|
BuildMI(MBB, I, DL, get(XCore::LDAWSP_ru6), DestReg).addImm(0);
|
|
|
|
return;
|
2008-11-07 11:59:00 +01:00
|
|
|
}
|
2010-07-11 09:56:13 +02:00
|
|
|
|
|
|
|
if (DestReg == XCore::SP && GRSrc) {
|
2009-02-12 01:02:55 +01:00
|
|
|
BuildMI(MBB, I, DL, get(XCore::SETSP_1r))
|
2010-07-11 09:56:13 +02:00
|
|
|
.addReg(SrcReg, getKillRegState(KillSrc));
|
|
|
|
return;
|
2008-11-07 11:59:00 +01:00
|
|
|
}
|
2010-07-11 09:56:13 +02:00
|
|
|
llvm_unreachable("Impossible reg-to-reg copy");
|
2008-11-07 11:59:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void XCoreInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
|
2009-02-12 01:02:55 +01:00
|
|
|
MachineBasicBlock::iterator I,
|
|
|
|
unsigned SrcReg, bool isKill,
|
|
|
|
int FrameIndex,
|
2010-05-06 21:06:44 +02:00
|
|
|
const TargetRegisterClass *RC,
|
|
|
|
const TargetRegisterInfo *TRI) const
|
2008-11-07 11:59:00 +01:00
|
|
|
{
|
2010-04-02 22:16:16 +02:00
|
|
|
DebugLoc DL;
|
2009-02-12 01:02:55 +01:00
|
|
|
if (I != MBB.end()) DL = I->getDebugLoc();
|
|
|
|
BuildMI(MBB, I, DL, get(XCore::STWFI))
|
2009-05-13 23:33:08 +02:00
|
|
|
.addReg(SrcReg, getKillRegState(isKill))
|
2009-02-12 01:02:55 +01:00
|
|
|
.addFrameIndex(FrameIndex)
|
|
|
|
.addImm(0);
|
2008-11-07 11:59:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void XCoreInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
|
2009-02-12 01:02:55 +01:00
|
|
|
MachineBasicBlock::iterator I,
|
|
|
|
unsigned DestReg, int FrameIndex,
|
2010-05-06 21:06:44 +02:00
|
|
|
const TargetRegisterClass *RC,
|
|
|
|
const TargetRegisterInfo *TRI) const
|
2008-11-07 11:59:00 +01:00
|
|
|
{
|
2010-04-02 22:16:16 +02:00
|
|
|
DebugLoc DL;
|
2009-02-12 01:02:55 +01:00
|
|
|
if (I != MBB.end()) DL = I->getDebugLoc();
|
|
|
|
BuildMI(MBB, I, DL, get(XCore::LDWFI), DestReg)
|
|
|
|
.addFrameIndex(FrameIndex)
|
|
|
|
.addImm(0);
|
2008-11-07 11:59:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ReverseBranchCondition - Return the inverse opcode of the
|
|
|
|
/// specified Branch instruction.
|
|
|
|
bool XCoreInstrInfo::
|
2010-11-28 00:05:03 +01:00
|
|
|
ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
|
2008-11-07 11:59:00 +01:00
|
|
|
assert((Cond.size() == 2) &&
|
|
|
|
"Invalid XCore branch condition!");
|
|
|
|
Cond[0].setImm(GetOppositeBranchCondition((XCore::CondCode)Cond[0].getImm()));
|
|
|
|
return false;
|
|
|
|
}
|