2009-07-03 06:18:33 +08:00
|
|
|
//===- Thumb1InstrInfo.cpp - Thumb-1 Instruction Information --------*- C++ -*-===//
|
2009-06-27 05:28:53 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-07-03 06:18:33 +08:00
|
|
|
// This file contains the Thumb-1 implementation of the TargetInstrInfo class.
|
2009-06-27 05:28:53 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ARMInstrInfo.h"
|
|
|
|
#include "ARM.h"
|
|
|
|
#include "ARMGenInstrInfo.inc"
|
|
|
|
#include "ARMMachineFunctionInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2009-07-03 06:18:33 +08:00
|
|
|
#include "Thumb1InstrInfo.h"
|
2009-06-27 05:28:53 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2009-07-03 06:18:33 +08:00
|
|
|
Thumb1InstrInfo::Thumb1InstrInfo(const ARMSubtarget &STI)
|
2009-06-27 20:16:40 +08:00
|
|
|
: ARMBaseInstrInfo(STI), RI(*this, STI) {
|
2009-06-27 05:28:53 +08:00
|
|
|
}
|
|
|
|
|
2009-07-11 14:43:01 +08:00
|
|
|
unsigned Thumb1InstrInfo::getUnindexedOpcode(unsigned Opc) const {
|
2009-07-09 00:09:28 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-11 14:43:01 +08:00
|
|
|
unsigned Thumb1InstrInfo::getOpcode(ARMII::Op Op) const {
|
2009-07-09 00:09:28 +08:00
|
|
|
switch (Op) {
|
|
|
|
case ARMII::ADDri: return ARM::tADDi8;
|
|
|
|
case ARMII::ADDrs: return 0;
|
|
|
|
case ARMII::ADDrr: return ARM::tADDrr;
|
|
|
|
case ARMII::B: return ARM::tB;
|
|
|
|
case ARMII::Bcc: return ARM::tBcc;
|
2009-07-09 04:28:28 +08:00
|
|
|
case ARMII::BX_RET: return ARM::tBX_RET;
|
2009-07-24 08:16:18 +08:00
|
|
|
case ARMII::LDRrr: return ARM::tLDR;
|
|
|
|
case ARMII::LDRri: return 0;
|
2009-07-09 00:09:28 +08:00
|
|
|
case ARMII::MOVr: return ARM::tMOVr;
|
2009-07-24 08:16:18 +08:00
|
|
|
case ARMII::STRrr: return ARM::tSTR;
|
|
|
|
case ARMII::STRri: return 0;
|
2009-07-09 00:09:28 +08:00
|
|
|
case ARMII::SUBri: return ARM::tSUBi8;
|
|
|
|
case ARMII::SUBrs: return 0;
|
|
|
|
case ARMII::SUBrr: return ARM::tSUBrr;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Thumb1InstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
|
|
|
|
if (MBB.empty()) return false;
|
|
|
|
|
|
|
|
switch (MBB.back().getOpcode()) {
|
|
|
|
case ARM::tBX_RET:
|
|
|
|
case ARM::tBX_RET_vararg:
|
|
|
|
case ARM::tPOP_RET:
|
|
|
|
case ARM::tB:
|
|
|
|
case ARM::tBR_JTr:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:18:33 +08:00
|
|
|
bool Thumb1InstrInfo::isMoveInstr(const MachineInstr &MI,
|
|
|
|
unsigned &SrcReg, unsigned &DstReg,
|
|
|
|
unsigned& SrcSubIdx, unsigned& DstSubIdx) const {
|
2009-06-27 05:28:53 +08:00
|
|
|
SrcSubIdx = DstSubIdx = 0; // No sub-registers.
|
|
|
|
|
|
|
|
unsigned oc = MI.getOpcode();
|
|
|
|
switch (oc) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case ARM::tMOVr:
|
|
|
|
case ARM::tMOVhir2lor:
|
|
|
|
case ARM::tMOVlor2hir:
|
|
|
|
case ARM::tMOVhir2hir:
|
|
|
|
assert(MI.getDesc().getNumOperands() >= 2 &&
|
|
|
|
MI.getOperand(0).isReg() &&
|
|
|
|
MI.getOperand(1).isReg() &&
|
|
|
|
"Invalid Thumb MOV instruction");
|
|
|
|
SrcReg = MI.getOperand(1).getReg();
|
|
|
|
DstReg = MI.getOperand(0).getReg();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:18:33 +08:00
|
|
|
unsigned Thumb1InstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
|
|
|
|
int &FrameIndex) const {
|
2009-06-27 05:28:53 +08:00
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
default: break;
|
|
|
|
case ARM::tRestore:
|
|
|
|
if (MI->getOperand(1).isFI() &&
|
|
|
|
MI->getOperand(2).isImm() &&
|
|
|
|
MI->getOperand(2).getImm() == 0) {
|
|
|
|
FrameIndex = MI->getOperand(1).getIndex();
|
|
|
|
return MI->getOperand(0).getReg();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:18:33 +08:00
|
|
|
unsigned Thumb1InstrInfo::isStoreToStackSlot(const MachineInstr *MI,
|
|
|
|
int &FrameIndex) const {
|
2009-06-27 05:28:53 +08:00
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
default: break;
|
|
|
|
case ARM::tSpill:
|
|
|
|
if (MI->getOperand(1).isFI() &&
|
|
|
|
MI->getOperand(2).isImm() &&
|
|
|
|
MI->getOperand(2).getImm() == 0) {
|
|
|
|
FrameIndex = MI->getOperand(1).getIndex();
|
|
|
|
return MI->getOperand(0).getReg();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:18:33 +08:00
|
|
|
bool Thumb1InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I,
|
|
|
|
unsigned DestReg, unsigned SrcReg,
|
|
|
|
const TargetRegisterClass *DestRC,
|
|
|
|
const TargetRegisterClass *SrcRC) const {
|
2009-06-27 05:28:53 +08:00
|
|
|
DebugLoc DL = DebugLoc::getUnknownLoc();
|
|
|
|
if (I != MBB.end()) DL = I->getDebugLoc();
|
|
|
|
|
|
|
|
if (DestRC == ARM::GPRRegisterClass) {
|
|
|
|
if (SrcRC == ARM::GPRRegisterClass) {
|
|
|
|
BuildMI(MBB, I, DL, get(ARM::tMOVhir2hir), DestReg).addReg(SrcReg);
|
|
|
|
return true;
|
|
|
|
} else if (SrcRC == ARM::tGPRRegisterClass) {
|
|
|
|
BuildMI(MBB, I, DL, get(ARM::tMOVlor2hir), DestReg).addReg(SrcReg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (DestRC == ARM::tGPRRegisterClass) {
|
|
|
|
if (SrcRC == ARM::GPRRegisterClass) {
|
|
|
|
BuildMI(MBB, I, DL, get(ARM::tMOVhir2lor), DestReg).addReg(SrcReg);
|
|
|
|
return true;
|
|
|
|
} else if (SrcRC == ARM::tGPRRegisterClass) {
|
|
|
|
BuildMI(MBB, I, DL, get(ARM::tMOVr), DestReg).addReg(SrcReg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:18:33 +08:00
|
|
|
bool Thumb1InstrInfo::
|
2009-06-27 20:16:40 +08:00
|
|
|
canFoldMemoryOperand(const MachineInstr *MI,
|
|
|
|
const SmallVectorImpl<unsigned> &Ops) const {
|
|
|
|
if (Ops.size() != 1) return false;
|
|
|
|
|
|
|
|
unsigned OpNum = Ops[0];
|
|
|
|
unsigned Opc = MI->getOpcode();
|
|
|
|
switch (Opc) {
|
|
|
|
default: break;
|
|
|
|
case ARM::tMOVr:
|
|
|
|
case ARM::tMOVlor2hir:
|
|
|
|
case ARM::tMOVhir2lor:
|
|
|
|
case ARM::tMOVhir2hir: {
|
|
|
|
if (OpNum == 0) { // move -> store
|
|
|
|
unsigned SrcReg = MI->getOperand(1).getReg();
|
2009-06-27 20:59:03 +08:00
|
|
|
if (RI.isPhysicalRegister(SrcReg) && !isARMLowRegister(SrcReg))
|
2009-06-27 20:16:40 +08:00
|
|
|
// tSpill cannot take a high register operand.
|
|
|
|
return false;
|
|
|
|
} else { // move -> load
|
|
|
|
unsigned DstReg = MI->getOperand(0).getReg();
|
2009-06-27 20:59:03 +08:00
|
|
|
if (RI.isPhysicalRegister(DstReg) && !isARMLowRegister(DstReg))
|
2009-06-27 20:16:40 +08:00
|
|
|
// tRestore cannot target a high register operand.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:18:33 +08:00
|
|
|
void Thumb1InstrInfo::
|
2009-06-27 05:28:53 +08:00
|
|
|
storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
|
|
|
|
unsigned SrcReg, bool isKill, int FI,
|
|
|
|
const TargetRegisterClass *RC) const {
|
|
|
|
DebugLoc DL = DebugLoc::getUnknownLoc();
|
|
|
|
if (I != MBB.end()) DL = I->getDebugLoc();
|
|
|
|
|
|
|
|
assert(RC == ARM::tGPRRegisterClass && "Unknown regclass!");
|
|
|
|
|
|
|
|
if (RC == ARM::tGPRRegisterClass) {
|
2009-07-11 14:43:01 +08:00
|
|
|
AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::tSpill))
|
|
|
|
.addReg(SrcReg, getKillRegState(isKill))
|
|
|
|
.addFrameIndex(FI).addImm(0));
|
2009-06-27 05:28:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:18:33 +08:00
|
|
|
void Thumb1InstrInfo::
|
2009-06-27 05:28:53 +08:00
|
|
|
loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
|
|
|
|
unsigned DestReg, int FI,
|
|
|
|
const TargetRegisterClass *RC) const {
|
|
|
|
DebugLoc DL = DebugLoc::getUnknownLoc();
|
|
|
|
if (I != MBB.end()) DL = I->getDebugLoc();
|
|
|
|
|
|
|
|
assert(RC == ARM::tGPRRegisterClass && "Unknown regclass!");
|
|
|
|
|
|
|
|
if (RC == ARM::tGPRRegisterClass) {
|
2009-07-11 14:43:01 +08:00
|
|
|
AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::tRestore), DestReg)
|
|
|
|
.addFrameIndex(FI).addImm(0));
|
2009-06-27 05:28:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:18:33 +08:00
|
|
|
bool Thumb1InstrInfo::
|
2009-06-27 05:28:53 +08:00
|
|
|
spillCalleeSavedRegisters(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI) const {
|
|
|
|
if (CSI.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DebugLoc DL = DebugLoc::getUnknownLoc();
|
|
|
|
if (MI != MBB.end()) DL = MI->getDebugLoc();
|
|
|
|
|
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, get(ARM::tPUSH));
|
|
|
|
for (unsigned i = CSI.size(); i != 0; --i) {
|
|
|
|
unsigned Reg = CSI[i-1].getReg();
|
|
|
|
// Add the callee-saved register as live-in. It's killed at the spill.
|
|
|
|
MBB.addLiveIn(Reg);
|
|
|
|
MIB.addReg(Reg, RegState::Kill);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:18:33 +08:00
|
|
|
bool Thumb1InstrInfo::
|
2009-06-27 05:28:53 +08:00
|
|
|
restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI) const {
|
|
|
|
MachineFunction &MF = *MBB.getParent();
|
|
|
|
ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
|
|
|
|
if (CSI.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
|
|
|
|
MachineInstr *PopMI = MF.CreateMachineInstr(get(ARM::tPOP),MI->getDebugLoc());
|
|
|
|
for (unsigned i = CSI.size(); i != 0; --i) {
|
|
|
|
unsigned Reg = CSI[i-1].getReg();
|
|
|
|
if (Reg == ARM::LR) {
|
|
|
|
// Special epilogue for vararg functions. See emitEpilogue
|
|
|
|
if (isVarArg)
|
|
|
|
continue;
|
|
|
|
Reg = ARM::PC;
|
|
|
|
PopMI->setDesc(get(ARM::tPOP_RET));
|
|
|
|
MI = MBB.erase(MI);
|
|
|
|
}
|
|
|
|
PopMI->addOperand(MachineOperand::CreateReg(Reg, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's illegal to emit pop instruction without operands.
|
|
|
|
if (PopMI->getNumOperands() > 0)
|
|
|
|
MBB.insert(MI, PopMI);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:18:33 +08:00
|
|
|
MachineInstr *Thumb1InstrInfo::
|
2009-06-27 05:28:53 +08:00
|
|
|
foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
|
|
|
|
const SmallVectorImpl<unsigned> &Ops, int FI) const {
|
|
|
|
if (Ops.size() != 1) return NULL;
|
|
|
|
|
|
|
|
unsigned OpNum = Ops[0];
|
|
|
|
unsigned Opc = MI->getOpcode();
|
|
|
|
MachineInstr *NewMI = NULL;
|
|
|
|
switch (Opc) {
|
|
|
|
default: break;
|
|
|
|
case ARM::tMOVr:
|
|
|
|
case ARM::tMOVlor2hir:
|
|
|
|
case ARM::tMOVhir2lor:
|
|
|
|
case ARM::tMOVhir2hir: {
|
|
|
|
if (OpNum == 0) { // move -> store
|
|
|
|
unsigned SrcReg = MI->getOperand(1).getReg();
|
|
|
|
bool isKill = MI->getOperand(1).isKill();
|
2009-06-27 20:59:03 +08:00
|
|
|
if (RI.isPhysicalRegister(SrcReg) && !isARMLowRegister(SrcReg))
|
2009-06-27 05:28:53 +08:00
|
|
|
// tSpill cannot take a high register operand.
|
|
|
|
break;
|
2009-07-11 14:43:01 +08:00
|
|
|
NewMI = AddDefaultPred(BuildMI(MF, MI->getDebugLoc(), get(ARM::tSpill))
|
|
|
|
.addReg(SrcReg, getKillRegState(isKill))
|
|
|
|
.addFrameIndex(FI).addImm(0));
|
2009-06-27 05:28:53 +08:00
|
|
|
} else { // move -> load
|
|
|
|
unsigned DstReg = MI->getOperand(0).getReg();
|
2009-06-27 20:59:03 +08:00
|
|
|
if (RI.isPhysicalRegister(DstReg) && !isARMLowRegister(DstReg))
|
2009-06-27 05:28:53 +08:00
|
|
|
// tRestore cannot target a high register operand.
|
|
|
|
break;
|
|
|
|
bool isDead = MI->getOperand(0).isDead();
|
2009-07-11 14:43:01 +08:00
|
|
|
NewMI = AddDefaultPred(BuildMI(MF, MI->getDebugLoc(), get(ARM::tRestore))
|
|
|
|
.addReg(DstReg,
|
|
|
|
RegState::Define | getDeadRegState(isDead))
|
|
|
|
.addFrameIndex(FI).addImm(0));
|
2009-06-27 05:28:53 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewMI;
|
|
|
|
}
|