#include "M68kSubtarget.h"
#include "GISel/M68kCallLowering.h"
#include "GISel/M68kLegalizerInfo.h"
#include "GISel/M68kRegisterBankInfo.h"
#include "M68k.h"
#include "M68kMachineFunction.h"
#include "M68kRegisterInfo.h"
#include "M68kTargetMachine.h"
#include "llvm/CodeGen/MachineJumpTableInfo.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/Function.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
using namespace llvm;
#define DEBUG_TYPE "m68k-subtarget"
#define GET_SUBTARGETINFO_TARGET_DESC
#define GET_SUBTARGETINFO_CTOR
#include "M68kGenSubtargetInfo.inc"
extern bool FixGlobalBaseReg;
static StringRef selectM68kCPU(Triple TT, StringRef CPU) {
if (CPU.empty() || CPU == "generic") {
CPU = "M68000";
}
return CPU;
}
void M68kSubtarget::anchor() {}
M68kSubtarget::M68kSubtarget(const Triple &TT, StringRef CPU, StringRef FS,
const M68kTargetMachine &TM)
: M68kGenSubtargetInfo(TT, CPU, CPU, FS),
UserReservedRegister(M68k::NUM_TARGET_REGS), TM(TM), TSInfo(),
InstrInfo(initializeSubtargetDependencies(CPU, TT, FS, TM)),
FrameLowering(*this, this->getStackAlignment()), TLInfo(TM, *this),
TargetTriple(TT) {
CallLoweringInfo.reset(new M68kCallLowering(*getTargetLowering()));
Legalizer.reset(new M68kLegalizerInfo(*this));
auto *RBI = new M68kRegisterBankInfo(*getRegisterInfo());
RegBankInfo.reset(RBI);
InstSelector.reset(createM68kInstructionSelector(TM, *this, *RBI));
}
const CallLowering *M68kSubtarget::getCallLowering() const {
return CallLoweringInfo.get();
}
InstructionSelector *M68kSubtarget::getInstructionSelector() const {
return InstSelector.get();
}
const LegalizerInfo *M68kSubtarget::getLegalizerInfo() const {
return Legalizer.get();
}
const RegisterBankInfo *M68kSubtarget::getRegBankInfo() const {
return RegBankInfo.get();
}
bool M68kSubtarget::isPositionIndependent() const {
return TM.isPositionIndependent();
}
bool M68kSubtarget::isLegalToCallImmediateAddr() const { return true; }
bool M68kSubtarget::abiUsesSoftFloat() const { return true; }
M68kSubtarget &M68kSubtarget::initializeSubtargetDependencies(
StringRef CPU, Triple TT, StringRef FS, const M68kTargetMachine &TM) {
std::string CPUName = selectM68kCPU(TT, CPU).str();
ParseSubtargetFeatures(CPUName, CPUName, FS);
InstrItins = getInstrItineraryForCPU(CPUName);
stackAlignment = 8;
return *this;
}
unsigned char M68kSubtarget::classifyBlockAddressReference() const {
return M68kII::MO_PC_RELATIVE_ADDRESS;
}
unsigned char
M68kSubtarget::classifyLocalReference(const GlobalValue *GV) const {
switch (TM.getCodeModel()) {
default:
llvm_unreachable("Unsupported code model");
case CodeModel::Small:
case CodeModel::Kernel: {
return M68kII::MO_PC_RELATIVE_ADDRESS;
}
case CodeModel::Medium: {
if (isPositionIndependent()) {
if (atLeastM68020()) {
return M68kII::MO_PC_RELATIVE_ADDRESS;
}
return M68kII::MO_GOTOFF;
} else {
if (atLeastM68020()) {
return M68kII::MO_PC_RELATIVE_ADDRESS;
}
return M68kII::MO_ABSOLUTE_ADDRESS;
}
}
}
}
unsigned char M68kSubtarget::classifyExternalReference(const Module &M) const {
if (TM.shouldAssumeDSOLocal(M, nullptr))
return classifyLocalReference(nullptr);
if (isPositionIndependent())
return M68kII::MO_GOTPCREL;
return M68kII::MO_GOT;
}
unsigned char
M68kSubtarget::classifyGlobalReference(const GlobalValue *GV) const {
return classifyGlobalReference(GV, *GV->getParent());
}
unsigned char M68kSubtarget::classifyGlobalReference(const GlobalValue *GV,
const Module &M) const {
if (TM.shouldAssumeDSOLocal(M, GV))
return classifyLocalReference(GV);
switch (TM.getCodeModel()) {
default:
llvm_unreachable("Unsupported code model");
case CodeModel::Small:
case CodeModel::Kernel: {
if (isPositionIndependent())
return M68kII::MO_GOTPCREL;
return M68kII::MO_PC_RELATIVE_ADDRESS;
}
case CodeModel::Medium: {
if (isPositionIndependent())
return M68kII::MO_GOTPCREL;
if (atLeastM68020())
return M68kII::MO_PC_RELATIVE_ADDRESS;
return M68kII::MO_ABSOLUTE_ADDRESS;
}
}
}
unsigned M68kSubtarget::getJumpTableEncoding() const {
if (isPositionIndependent()) {
if (TM.getCodeModel() == CodeModel::Medium && !atLeastM68020())
return MachineJumpTableInfo::EK_Custom32;
return MachineJumpTableInfo::EK_LabelDifference32;
}
return MachineJumpTableInfo::EK_BlockAddress;
}
unsigned char
M68kSubtarget::classifyGlobalFunctionReference(const GlobalValue *GV) const {
return classifyGlobalFunctionReference(GV, *GV->getParent());
}
unsigned char
M68kSubtarget::classifyGlobalFunctionReference(const GlobalValue *GV,
const Module &M) const {
if (TM.shouldAssumeDSOLocal(M, GV))
return M68kII::MO_NO_FLAG;
auto *F = dyn_cast_or_null<Function>(GV);
if (F && F->hasFnAttribute(Attribute::NonLazyBind)) {
return M68kII::MO_GOTPCREL;
}
return M68kII::MO_PLT;
}