mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-12-16 11:30:51 +00:00
bf992817f2
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@86400 91177308-0d34-0410-b5e6-96231b3b80d8
321 lines
11 KiB
C++
321 lines
11 KiB
C++
//===- Thumb1InstrInfo.cpp - Thumb-1 Instruction 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 Thumb-1 implementation of the TargetInstrInfo class.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "Thumb1InstrInfo.h"
|
|
#include "ARM.h"
|
|
#include "ARMConstantPoolValue.h"
|
|
#include "ARMGenInstrInfo.inc"
|
|
#include "ARMMachineFunctionInfo.h"
|
|
#include "llvm/Constants.h"
|
|
#include "llvm/Function.h"
|
|
#include "llvm/GlobalValue.h"
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
#include "llvm/CodeGen/MachineMemOperand.h"
|
|
#include "llvm/CodeGen/PseudoSourceValue.h"
|
|
#include "llvm/ADT/SmallVector.h"
|
|
#include "Thumb1InstrInfo.h"
|
|
|
|
using namespace llvm;
|
|
|
|
Thumb1InstrInfo::Thumb1InstrInfo(const ARMSubtarget &STI)
|
|
: ARMBaseInstrInfo(STI), RI(*this, STI) {
|
|
}
|
|
|
|
unsigned Thumb1InstrInfo::getUnindexedOpcode(unsigned Opc) const {
|
|
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::tBRIND:
|
|
case ARM::tBR_JTr:
|
|
return true;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool Thumb1InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
|
|
MachineBasicBlock::iterator I,
|
|
unsigned DestReg, unsigned SrcReg,
|
|
const TargetRegisterClass *DestRC,
|
|
const TargetRegisterClass *SrcRC) const {
|
|
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::tMOVgpr2gpr), DestReg).addReg(SrcReg);
|
|
return true;
|
|
} else if (SrcRC == ARM::tGPRRegisterClass) {
|
|
BuildMI(MBB, I, DL, get(ARM::tMOVtgpr2gpr), DestReg).addReg(SrcReg);
|
|
return true;
|
|
}
|
|
} else if (DestRC == ARM::tGPRRegisterClass) {
|
|
if (SrcRC == ARM::GPRRegisterClass) {
|
|
BuildMI(MBB, I, DL, get(ARM::tMOVgpr2tgpr), DestReg).addReg(SrcReg);
|
|
return true;
|
|
} else if (SrcRC == ARM::tGPRRegisterClass) {
|
|
BuildMI(MBB, I, DL, get(ARM::tMOVr), DestReg).addReg(SrcReg);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool Thumb1InstrInfo::
|
|
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::tMOVtgpr2gpr:
|
|
case ARM::tMOVgpr2tgpr:
|
|
case ARM::tMOVgpr2gpr: {
|
|
if (OpNum == 0) { // move -> store
|
|
unsigned SrcReg = MI->getOperand(1).getReg();
|
|
if (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
|
|
!isARMLowRegister(SrcReg))
|
|
// tSpill cannot take a high register operand.
|
|
return false;
|
|
} else { // move -> load
|
|
unsigned DstReg = MI->getOperand(0).getReg();
|
|
if (TargetRegisterInfo::isPhysicalRegister(DstReg) &&
|
|
!isARMLowRegister(DstReg))
|
|
// tRestore cannot target a high register operand.
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void Thumb1InstrInfo::
|
|
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 ||
|
|
(TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
|
|
isARMLowRegister(SrcReg))) && "Unknown regclass!");
|
|
|
|
if (RC == ARM::tGPRRegisterClass) {
|
|
MachineFunction &MF = *MBB.getParent();
|
|
MachineFrameInfo &MFI = *MF.getFrameInfo();
|
|
MachineMemOperand *MMO =
|
|
MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FI),
|
|
MachineMemOperand::MOStore, 0,
|
|
MFI.getObjectSize(FI),
|
|
MFI.getObjectAlignment(FI));
|
|
AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::tSpill))
|
|
.addReg(SrcReg, getKillRegState(isKill))
|
|
.addFrameIndex(FI).addImm(0).addMemOperand(MMO));
|
|
}
|
|
}
|
|
|
|
void Thumb1InstrInfo::
|
|
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 ||
|
|
(TargetRegisterInfo::isPhysicalRegister(DestReg) &&
|
|
isARMLowRegister(DestReg))) && "Unknown regclass!");
|
|
|
|
if (RC == ARM::tGPRRegisterClass) {
|
|
MachineFunction &MF = *MBB.getParent();
|
|
MachineFrameInfo &MFI = *MF.getFrameInfo();
|
|
MachineMemOperand *MMO =
|
|
MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FI),
|
|
MachineMemOperand::MOLoad, 0,
|
|
MFI.getObjectSize(FI),
|
|
MFI.getObjectAlignment(FI));
|
|
AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::tRestore), DestReg)
|
|
.addFrameIndex(FI).addImm(0).addMemOperand(MMO));
|
|
}
|
|
}
|
|
|
|
bool Thumb1InstrInfo::
|
|
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));
|
|
AddDefaultPred(MIB);
|
|
MIB.addReg(0); // No write back.
|
|
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;
|
|
}
|
|
|
|
bool Thumb1InstrInfo::
|
|
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;
|
|
DebugLoc DL = MI->getDebugLoc();
|
|
MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM::tPOP));
|
|
AddDefaultPred(MIB);
|
|
MIB.addReg(0); // No write back.
|
|
|
|
bool NumRegs = 0;
|
|
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;
|
|
(*MIB).setDesc(get(ARM::tPOP_RET));
|
|
MI = MBB.erase(MI);
|
|
}
|
|
MIB.addReg(Reg, getDefRegState(true));
|
|
++NumRegs;
|
|
}
|
|
|
|
// It's illegal to emit pop instruction without operands.
|
|
if (NumRegs)
|
|
MBB.insert(MI, &*MIB);
|
|
|
|
return true;
|
|
}
|
|
|
|
MachineInstr *Thumb1InstrInfo::
|
|
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::tMOVtgpr2gpr:
|
|
case ARM::tMOVgpr2tgpr:
|
|
case ARM::tMOVgpr2gpr: {
|
|
if (OpNum == 0) { // move -> store
|
|
unsigned SrcReg = MI->getOperand(1).getReg();
|
|
bool isKill = MI->getOperand(1).isKill();
|
|
if (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
|
|
!isARMLowRegister(SrcReg))
|
|
// tSpill cannot take a high register operand.
|
|
break;
|
|
NewMI = AddDefaultPred(BuildMI(MF, MI->getDebugLoc(), get(ARM::tSpill))
|
|
.addReg(SrcReg, getKillRegState(isKill))
|
|
.addFrameIndex(FI).addImm(0));
|
|
} else { // move -> load
|
|
unsigned DstReg = MI->getOperand(0).getReg();
|
|
if (TargetRegisterInfo::isPhysicalRegister(DstReg) &&
|
|
!isARMLowRegister(DstReg))
|
|
// tRestore cannot target a high register operand.
|
|
break;
|
|
bool isDead = MI->getOperand(0).isDead();
|
|
NewMI = AddDefaultPred(BuildMI(MF, MI->getDebugLoc(), get(ARM::tRestore))
|
|
.addReg(DstReg,
|
|
RegState::Define | getDeadRegState(isDead))
|
|
.addFrameIndex(FI).addImm(0));
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
return NewMI;
|
|
}
|
|
|
|
void Thumb1InstrInfo::reMaterialize(MachineBasicBlock &MBB,
|
|
MachineBasicBlock::iterator I,
|
|
unsigned DestReg, unsigned SubIdx,
|
|
const MachineInstr *Orig) const {
|
|
DebugLoc dl = Orig->getDebugLoc();
|
|
unsigned Opcode = Orig->getOpcode();
|
|
switch (Opcode) {
|
|
default: {
|
|
MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
|
|
MI->getOperand(0).setReg(DestReg);
|
|
MBB.insert(I, MI);
|
|
break;
|
|
}
|
|
case ARM::tLDRpci_pic: {
|
|
MachineFunction &MF = *MBB.getParent();
|
|
ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
|
|
MachineConstantPool *MCP = MF.getConstantPool();
|
|
unsigned CPI = Orig->getOperand(1).getIndex();
|
|
const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
|
|
assert(MCPE.isMachineConstantPoolEntry() &&
|
|
"Expecting a machine constantpool entry!");
|
|
ARMConstantPoolValue *ACPV =
|
|
static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
|
|
unsigned PCLabelId = AFI->createConstPoolEntryUId();
|
|
ARMConstantPoolValue *NewCPV = 0;
|
|
if (ACPV->isGlobalValue())
|
|
NewCPV = new ARMConstantPoolValue(ACPV->getGV(), PCLabelId,
|
|
ARMCP::CPValue, 4);
|
|
else if (ACPV->isExtSymbol())
|
|
NewCPV = new ARMConstantPoolValue(MF.getFunction()->getContext(),
|
|
ACPV->getSymbol(), PCLabelId, 4);
|
|
else if (ACPV->isBlockAddress())
|
|
NewCPV = new ARMConstantPoolValue(ACPV->getBlockAddress(), PCLabelId,
|
|
ARMCP::CPBlockAddress, 4);
|
|
else
|
|
llvm_unreachable("Unexpected ARM constantpool value type!!");
|
|
CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
|
|
MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
|
|
DestReg)
|
|
.addConstantPoolIndex(CPI).addImm(PCLabelId);
|
|
(*MIB).setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
|
|
break;
|
|
}
|
|
}
|
|
|
|
MachineInstr *NewMI = prior(I);
|
|
NewMI->getOperand(0).setSubReg(SubIdx);
|
|
}
|
|
|