2012-02-18 12:03:15 +00:00
|
|
|
//===-- MSP430InstrInfo.cpp - MSP430 Instruction Information --------------===//
|
2009-05-03 12:57:15 +00: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 MSP430 implementation of the TargetInstrInfo class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MSP430InstrInfo.h"
|
2012-03-17 18:46:09 +00:00
|
|
|
#include "MSP430.h"
|
2009-05-03 13:11:04 +00:00
|
|
|
#include "MSP430MachineFunctionInfo.h"
|
2009-05-03 12:57:15 +00:00
|
|
|
#include "MSP430TargetMachine.h"
|
2009-05-03 13:09:57 +00:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2009-05-03 12:57:15 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2011-08-24 18:08:43 +00:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2009-05-03 12:57:15 +00:00
|
|
|
|
2011-07-01 17:57:27 +00:00
|
|
|
#define GET_INSTRINFO_CTOR
|
2011-06-28 20:07:07 +00:00
|
|
|
#include "MSP430GenInstrInfo.inc"
|
|
|
|
|
2009-05-03 12:57:15 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
MSP430InstrInfo::MSP430InstrInfo(MSP430TargetMachine &tm)
|
2011-07-01 17:57:27 +00:00
|
|
|
: MSP430GenInstrInfo(MSP430::ADJCALLSTACKDOWN, MSP430::ADJCALLSTACKUP),
|
2013-06-07 06:30:15 +00:00
|
|
|
RI(tm) {}
|
2009-05-03 13:02:04 +00:00
|
|
|
|
2009-05-03 13:09:57 +00:00
|
|
|
void MSP430InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
unsigned SrcReg, bool isKill, int FrameIdx,
|
2010-05-06 19:06:44 +00:00
|
|
|
const TargetRegisterClass *RC,
|
|
|
|
const TargetRegisterInfo *TRI) const {
|
2010-04-02 20:16:16 +00:00
|
|
|
DebugLoc DL;
|
2009-05-03 13:09:57 +00:00
|
|
|
if (MI != MBB.end()) DL = MI->getDebugLoc();
|
2009-11-07 17:13:57 +00:00
|
|
|
MachineFunction &MF = *MBB.getParent();
|
|
|
|
MachineFrameInfo &MFI = *MF.getFrameInfo();
|
|
|
|
|
|
|
|
MachineMemOperand *MMO =
|
2011-11-15 07:34:52 +00:00
|
|
|
MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
|
2010-09-21 04:39:43 +00:00
|
|
|
MachineMemOperand::MOStore,
|
2009-11-07 17:13:57 +00:00
|
|
|
MFI.getObjectSize(FrameIdx),
|
|
|
|
MFI.getObjectAlignment(FrameIdx));
|
2009-05-03 13:09:57 +00:00
|
|
|
|
|
|
|
if (RC == &MSP430::GR16RegClass)
|
|
|
|
BuildMI(MBB, MI, DL, get(MSP430::MOV16mr))
|
|
|
|
.addFrameIndex(FrameIdx).addImm(0)
|
2009-11-07 17:13:57 +00:00
|
|
|
.addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
|
2009-05-03 13:09:57 +00:00
|
|
|
else if (RC == &MSP430::GR8RegClass)
|
|
|
|
BuildMI(MBB, MI, DL, get(MSP430::MOV8mr))
|
|
|
|
.addFrameIndex(FrameIdx).addImm(0)
|
2009-11-07 17:13:57 +00:00
|
|
|
.addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
|
2009-05-03 13:09:57 +00:00
|
|
|
else
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Cannot store this register to stack slot!");
|
2009-05-03 13:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
unsigned DestReg, int FrameIdx,
|
2010-05-06 19:06:44 +00:00
|
|
|
const TargetRegisterClass *RC,
|
|
|
|
const TargetRegisterInfo *TRI) const{
|
2010-04-02 20:16:16 +00:00
|
|
|
DebugLoc DL;
|
2009-05-03 13:09:57 +00:00
|
|
|
if (MI != MBB.end()) DL = MI->getDebugLoc();
|
2009-11-07 17:13:57 +00:00
|
|
|
MachineFunction &MF = *MBB.getParent();
|
|
|
|
MachineFrameInfo &MFI = *MF.getFrameInfo();
|
|
|
|
|
|
|
|
MachineMemOperand *MMO =
|
2011-11-15 07:34:52 +00:00
|
|
|
MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
|
2010-09-21 04:39:43 +00:00
|
|
|
MachineMemOperand::MOLoad,
|
2009-11-07 17:13:57 +00:00
|
|
|
MFI.getObjectSize(FrameIdx),
|
|
|
|
MFI.getObjectAlignment(FrameIdx));
|
2009-05-03 13:09:57 +00:00
|
|
|
|
|
|
|
if (RC == &MSP430::GR16RegClass)
|
|
|
|
BuildMI(MBB, MI, DL, get(MSP430::MOV16rm))
|
2009-11-07 17:13:57 +00:00
|
|
|
.addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
|
2009-05-03 13:09:57 +00:00
|
|
|
else if (RC == &MSP430::GR8RegClass)
|
|
|
|
BuildMI(MBB, MI, DL, get(MSP430::MOV8rm))
|
2009-11-07 17:13:57 +00:00
|
|
|
.addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
|
2009-05-03 13:09:57 +00:00
|
|
|
else
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Cannot store this register to stack slot!");
|
2009-05-03 13:09:57 +00:00
|
|
|
}
|
|
|
|
|
2010-07-11 06:53:30 +00:00
|
|
|
void MSP430InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I, DebugLoc DL,
|
|
|
|
unsigned DestReg, unsigned SrcReg,
|
|
|
|
bool KillSrc) const {
|
|
|
|
unsigned Opc;
|
|
|
|
if (MSP430::GR16RegClass.contains(DestReg, SrcReg))
|
|
|
|
Opc = MSP430::MOV16rr;
|
|
|
|
else if (MSP430::GR8RegClass.contains(DestReg, SrcReg))
|
|
|
|
Opc = MSP430::MOV8rr;
|
|
|
|
else
|
|
|
|
llvm_unreachable("Impossible reg-to-reg copy");
|
2009-05-03 13:05:42 +00:00
|
|
|
|
2010-07-11 06:53:30 +00:00
|
|
|
BuildMI(MBB, I, DL, get(Opc), DestReg)
|
|
|
|
.addReg(SrcReg, getKillRegState(KillSrc));
|
2009-05-03 13:02:04 +00:00
|
|
|
}
|
|
|
|
|
2009-10-21 19:17:18 +00:00
|
|
|
unsigned MSP430InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
|
|
|
|
MachineBasicBlock::iterator I = MBB.end();
|
|
|
|
unsigned Count = 0;
|
|
|
|
|
|
|
|
while (I != MBB.begin()) {
|
|
|
|
--I;
|
2010-04-02 01:38:09 +00:00
|
|
|
if (I->isDebugValue())
|
|
|
|
continue;
|
2009-10-21 19:17:18 +00:00
|
|
|
if (I->getOpcode() != MSP430::JMP &&
|
2010-05-01 12:04:32 +00:00
|
|
|
I->getOpcode() != MSP430::JCC &&
|
|
|
|
I->getOpcode() != MSP430::Br &&
|
|
|
|
I->getOpcode() != MSP430::Bm)
|
2009-10-21 19:17:18 +00:00
|
|
|
break;
|
|
|
|
// Remove the branch.
|
|
|
|
I->eraseFromParent();
|
|
|
|
I = MBB.end();
|
|
|
|
++Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MSP430InstrInfo::
|
|
|
|
ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
|
|
|
|
assert(Cond.size() == 1 && "Invalid Xbranch condition!");
|
|
|
|
|
|
|
|
MSP430CC::CondCodes CC = static_cast<MSP430CC::CondCodes>(Cond[0].getImm());
|
|
|
|
|
|
|
|
switch (CC) {
|
2012-02-07 02:50:20 +00:00
|
|
|
default: llvm_unreachable("Invalid branch condition!");
|
2009-10-21 19:17:18 +00:00
|
|
|
case MSP430CC::COND_E:
|
|
|
|
CC = MSP430CC::COND_NE;
|
|
|
|
break;
|
|
|
|
case MSP430CC::COND_NE:
|
|
|
|
CC = MSP430CC::COND_E;
|
|
|
|
break;
|
|
|
|
case MSP430CC::COND_L:
|
|
|
|
CC = MSP430CC::COND_GE;
|
|
|
|
break;
|
|
|
|
case MSP430CC::COND_GE:
|
|
|
|
CC = MSP430CC::COND_L;
|
|
|
|
break;
|
|
|
|
case MSP430CC::COND_HS:
|
|
|
|
CC = MSP430CC::COND_LO;
|
|
|
|
break;
|
|
|
|
case MSP430CC::COND_LO:
|
|
|
|
CC = MSP430CC::COND_HS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cond[0].setImm(CC);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
|
2011-12-07 07:15:52 +00:00
|
|
|
if (!MI->isTerminator()) return false;
|
2009-10-21 19:17:18 +00:00
|
|
|
|
|
|
|
// Conditional branch is a special case.
|
2011-12-07 07:15:52 +00:00
|
|
|
if (MI->isBranch() && !MI->isBarrier())
|
2009-10-21 19:17:18 +00:00
|
|
|
return true;
|
2011-12-07 07:15:52 +00:00
|
|
|
if (!MI->isPredicable())
|
2009-10-21 19:17:18 +00:00
|
|
|
return true;
|
|
|
|
return !isPredicated(MI);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MSP430InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock *&TBB,
|
|
|
|
MachineBasicBlock *&FBB,
|
|
|
|
SmallVectorImpl<MachineOperand> &Cond,
|
|
|
|
bool AllowModify) const {
|
|
|
|
// Start from the bottom of the block and work up, examining the
|
|
|
|
// terminator instructions.
|
|
|
|
MachineBasicBlock::iterator I = MBB.end();
|
|
|
|
while (I != MBB.begin()) {
|
|
|
|
--I;
|
2010-04-02 01:38:09 +00:00
|
|
|
if (I->isDebugValue())
|
|
|
|
continue;
|
|
|
|
|
2009-10-21 19:17:18 +00:00
|
|
|
// Working from the bottom, when we see a non-terminator
|
|
|
|
// instruction, we're done.
|
|
|
|
if (!isUnpredicatedTerminator(I))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// A terminator that isn't a branch can't easily be handled
|
|
|
|
// by this analysis.
|
2011-12-07 07:15:52 +00:00
|
|
|
if (!I->isBranch())
|
2009-10-21 19:17:18 +00:00
|
|
|
return true;
|
|
|
|
|
2010-05-01 12:04:32 +00:00
|
|
|
// Cannot handle indirect branches.
|
|
|
|
if (I->getOpcode() == MSP430::Br ||
|
|
|
|
I->getOpcode() == MSP430::Bm)
|
|
|
|
return true;
|
|
|
|
|
2009-10-21 19:17:18 +00:00
|
|
|
// Handle unconditional branches.
|
|
|
|
if (I->getOpcode() == MSP430::JMP) {
|
|
|
|
if (!AllowModify) {
|
|
|
|
TBB = I->getOperand(0).getMBB();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the block has any instructions after a JMP, delete them.
|
2009-12-03 00:50:42 +00:00
|
|
|
while (llvm::next(I) != MBB.end())
|
|
|
|
llvm::next(I)->eraseFromParent();
|
2009-10-21 19:17:18 +00:00
|
|
|
Cond.clear();
|
|
|
|
FBB = 0;
|
|
|
|
|
|
|
|
// Delete the JMP if it's equivalent to a fall-through.
|
|
|
|
if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
|
|
|
|
TBB = 0;
|
|
|
|
I->eraseFromParent();
|
|
|
|
I = MBB.end();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TBB is used to indicate the unconditinal destination.
|
|
|
|
TBB = I->getOperand(0).getMBB();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle conditional branches.
|
|
|
|
assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
|
|
|
|
MSP430CC::CondCodes BranchCode =
|
|
|
|
static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
|
|
|
|
if (BranchCode == MSP430CC::COND_INVALID)
|
|
|
|
return true; // Can't handle weird stuff.
|
|
|
|
|
|
|
|
// Working from the bottom, handle the first conditional branch.
|
|
|
|
if (Cond.empty()) {
|
|
|
|
FBB = TBB;
|
|
|
|
TBB = I->getOperand(0).getMBB();
|
|
|
|
Cond.push_back(MachineOperand::CreateImm(BranchCode));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle subsequent conditional branches. Only handle the case where all
|
|
|
|
// conditional branches branch to the same destination.
|
|
|
|
assert(Cond.size() == 1);
|
|
|
|
assert(TBB);
|
|
|
|
|
|
|
|
// Only handle the case where all conditional branches branch to
|
|
|
|
// the same destination.
|
|
|
|
if (TBB != I->getOperand(0).getMBB())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
|
|
|
|
// If the conditions are the same, we can leave them alone.
|
|
|
|
if (OldBranchCode == BranchCode)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-03 13:15:22 +00:00
|
|
|
unsigned
|
|
|
|
MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
|
|
|
|
MachineBasicBlock *FBB,
|
2010-06-17 22:43:56 +00:00
|
|
|
const SmallVectorImpl<MachineOperand> &Cond,
|
|
|
|
DebugLoc DL) const {
|
2009-05-03 13:15:22 +00:00
|
|
|
// Shouldn't be a fall through.
|
|
|
|
assert(TBB && "InsertBranch must not be told to insert a fallthrough");
|
|
|
|
assert((Cond.size() == 1 || Cond.size() == 0) &&
|
|
|
|
"MSP430 branch conditions have one component!");
|
|
|
|
|
|
|
|
if (Cond.empty()) {
|
|
|
|
// Unconditional branch?
|
|
|
|
assert(!FBB && "Unconditional branch with multiple successors!");
|
2010-04-02 20:16:16 +00:00
|
|
|
BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(TBB);
|
2009-05-03 13:15:22 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conditional branch.
|
|
|
|
unsigned Count = 0;
|
2010-04-02 20:16:16 +00:00
|
|
|
BuildMI(&MBB, DL, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
|
2009-10-21 19:17:18 +00:00
|
|
|
++Count;
|
2009-05-03 13:15:22 +00:00
|
|
|
|
2009-10-21 19:17:18 +00:00
|
|
|
if (FBB) {
|
|
|
|
// Two-way Conditional branch. Insert the second branch.
|
2010-04-02 20:16:16 +00:00
|
|
|
BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(FBB);
|
2009-10-21 19:17:18 +00:00
|
|
|
++Count;
|
|
|
|
}
|
2009-05-03 13:15:22 +00:00
|
|
|
return Count;
|
|
|
|
}
|
2010-01-15 21:19:05 +00:00
|
|
|
|
|
|
|
/// GetInstSize - Return the number of bytes of code the specified
|
|
|
|
/// instruction may be. This returns the maximum number of bytes.
|
|
|
|
///
|
|
|
|
unsigned MSP430InstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &Desc = MI->getDesc();
|
2010-01-15 21:19:05 +00:00
|
|
|
|
|
|
|
switch (Desc.TSFlags & MSP430II::SizeMask) {
|
|
|
|
default:
|
|
|
|
switch (Desc.getOpcode()) {
|
2012-02-07 02:50:20 +00:00
|
|
|
default: llvm_unreachable("Unknown instruction size!");
|
2010-07-16 22:20:36 +00:00
|
|
|
case TargetOpcode::PROLOG_LABEL:
|
2010-02-09 19:54:29 +00:00
|
|
|
case TargetOpcode::EH_LABEL:
|
|
|
|
case TargetOpcode::IMPLICIT_DEF:
|
|
|
|
case TargetOpcode::KILL:
|
2010-04-07 19:51:44 +00:00
|
|
|
case TargetOpcode::DBG_VALUE:
|
2010-01-15 21:19:05 +00:00
|
|
|
return 0;
|
2010-02-09 19:54:29 +00:00
|
|
|
case TargetOpcode::INLINEASM: {
|
2010-01-15 21:19:05 +00:00
|
|
|
const MachineFunction *MF = MI->getParent()->getParent();
|
|
|
|
const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
|
|
|
|
return TII.getInlineAsmLength(MI->getOperand(0).getSymbolName(),
|
|
|
|
*MF->getTarget().getMCAsmInfo());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case MSP430II::SizeSpecial:
|
|
|
|
switch (MI->getOpcode()) {
|
2012-02-07 02:50:20 +00:00
|
|
|
default: llvm_unreachable("Unknown instruction size!");
|
2010-01-15 21:19:05 +00:00
|
|
|
case MSP430::SAR8r1c:
|
|
|
|
case MSP430::SAR16r1c:
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
case MSP430II::Size2Bytes:
|
|
|
|
return 2;
|
|
|
|
case MSP430II::Size4Bytes:
|
|
|
|
return 4;
|
|
|
|
case MSP430II::Size6Bytes:
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
}
|