blob: e14f7f77ff66f8405561a2aeb5818f82e4fd1683 [file]
//===-- MipsTargetMachine.cpp - Define TargetMachine for Mips -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Implements the info about Mips target spec.
//
//===----------------------------------------------------------------------===//
#include "MipsTargetMachine.h"
#include "MCTargetDesc/MipsABIInfo.h"
#include "MCTargetDesc/MipsMCTargetDesc.h"
#include "Mips.h"
#include "MipsMachineFunction.h"
#include "MipsSubtarget.h"
#include "MipsTargetObjectFile.h"
#include "MipsTargetTransformInfo.h"
#include "TargetInfo/MipsTargetInfo.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/CodeGen/BasicTTIImpl.h"
#include "llvm/CodeGen/GlobalISel/CSEInfo.h"
#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
#include "llvm/CodeGen/GlobalISel/Legalizer.h"
#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/Function.h"
#include "llvm/InitializePasses.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Option/LibraryOptions.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetOptions.h"
#include <optional>
#include <string>
using namespace llvm;
#define DEBUG_TYPE "mips"
extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeMipsTarget() {
static opt::RegisterLibraryOptions<MipsOptions> O;
// Register the target.
RegisterTargetMachine<MipsebTargetMachine> X(getTheMipsTarget());
RegisterTargetMachine<MipselTargetMachine> Y(getTheMipselTarget());
RegisterTargetMachine<MipsebTargetMachine> A(getTheMips64Target());
RegisterTargetMachine<MipselTargetMachine> B(getTheMips64elTarget());
RegisterTargetMachine<NanoMipsTargetMachine> C(getTheNanoMipsTarget());
PassRegistry *PR = PassRegistry::getPassRegistry();
initializeGlobalISel(*PR);
initializeMipsAsmPrinterPass(*PR);
initializeMipsDelaySlotFillerLegacyPass(*PR);
initializeMipsBranchExpansionLegacyPass(*PR);
initializeMicroMipsSizeReduceLegacyPass(*PR);
initializeMipsPreLegalizerCombinerLegacyPass(*PR);
initializeMipsPostLegalizerCombinerLegacyPass(*PR);
initializeMipsMulMulBugFixLegacyPass(*PR);
initializeMipsDAGToDAGISelLegacyPass(*PR);
}
static std::unique_ptr<TargetLoweringObjectFile>
createTLOF(const Triple &TT, bool UseSmallSection) {
if (TT.isOSBinFormatCOFF())
return std::make_unique<TargetLoweringObjectFileCOFF>();
return std::make_unique<MipsTargetObjectFile>(UseSmallSection);
}
static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
return RM.value_or(Reloc::Static);
}
// On function prologue, the stack is created by decrementing
// its pointer. Once decremented, all references are done with positive
// offset from the stack/frame pointer, using StackGrowsUp enables
// an easier handling.
// Using CodeModel::Large enables different CALL behavior.
MipsTargetMachine::MipsTargetMachine(const Target &T, const Triple &TT,
StringRef CPU, StringRef FS,
const TargetOptions &Options,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT,
bool isLittle)
: CodeGenTargetMachineImpl(T, TT, CPU, FS, Options,
getEffectiveRelocModel(RM),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
CLOpts(cl::LibraryOptionsContext::get<MipsOptions>(
Options.MCOptions.LibraryOptions)),
MCCLOpts(MIPS_MC::getCLOpts(Options.MCOptions)), isLittle(isLittle),
IsJIT(JIT), DefaultSubtarget(TT, CPU, FS, Options.MCOptions.getABIName(),
isLittle, *this, std::nullopt),
TLOF(createTLOF(TT, DefaultSubtarget.useSmallSection())) {
initAsmInfo();
// Mips supports the debug entry values.
setSupportsDebugEntryValues(true);
}
MipsTargetMachine::~MipsTargetMachine() = default;
void MipsebTargetMachine::anchor() {}
MipsebTargetMachine::MipsebTargetMachine(const Target &T, const Triple &TT,
StringRef CPU, StringRef FS,
const TargetOptions &Options,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
: MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {}
void MipselTargetMachine::anchor() {}
MipselTargetMachine::MipselTargetMachine(const Target &T, const Triple &TT,
StringRef CPU, StringRef FS,
const TargetOptions &Options,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
: MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {}
void NanoMipsTargetMachine::anchor() {}
NanoMipsTargetMachine::NanoMipsTargetMachine(const Target &T, const Triple &TT,
StringRef CPU, StringRef FS,
const TargetOptions &Options,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
: MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {}
const MipsSubtarget *
MipsTargetMachine::getSubtargetImpl(const Function &F) const {
AttributeSet FnAttrs = F.getAttributes().getFnAttrs();
const Module *M = F.getParent();
if (const MipsSubtarget *ST = LastSubtarget.lookup(FnAttrs, M))
return ST;
SubtargetAttrs Attrs;
Attrs.CPU = FnAttrs.getAttribute("target-cpu");
Attrs.FS = FnAttrs.getAttribute("target-features");
Attrs.Mips16 = FnAttrs.getAttribute("mips16");
Attrs.NoMips16 = FnAttrs.getAttribute("nomips16");
Attrs.MicroMips = FnAttrs.getAttribute("micromips");
Attrs.NoMicroMips = FnAttrs.getAttribute("nomicromips");
Attrs.SoftFloat = FnAttrs.getAttribute("use-soft-float");
Attrs.ABIName = getTargetABIName(*M);
if (const MipsSubtarget *ST = LastSubtarget.lookup(FnAttrs, M, Attrs))
return ST;
std::string CPU =
Attrs.CPU.isValid() ? Attrs.CPU.getValueAsString().str() : TargetCPU;
std::string FS =
Attrs.FS.isValid() ? Attrs.FS.getValueAsString().str() : TargetFS;
bool hasMips16Attr = Attrs.Mips16.isValid();
bool hasNoMips16Attr = Attrs.NoMips16.isValid();
bool HasMicroMipsAttr = Attrs.MicroMips.isValid();
bool HasNoMicroMipsAttr = Attrs.NoMicroMips.isValid();
// FIXME: This is related to the code below to reset the target options,
// we need to know whether or not the soft float flag is set on the
// function, so we can enable it as a subtarget feature.
bool softFloat = Attrs.SoftFloat.getValueAsBool();
if (getTargetTriple().isNanoMips())
softFloat = true;
if (hasMips16Attr)
FS += FS.empty() ? "+mips16" : ",+mips16";
else if (hasNoMips16Attr)
FS += FS.empty() ? "-mips16" : ",-mips16";
if (HasMicroMipsAttr)
FS += FS.empty() ? "+micromips" : ",+micromips";
else if (HasNoMicroMipsAttr)
FS += FS.empty() ? "-micromips" : ",-micromips";
if (softFloat)
FS += FS.empty() ? "+soft-float" : ",+soft-float";
auto &I = SubtargetMap[CPU + FS + Attrs.ABIName.str()];
if (!I) {
I = std::make_unique<MipsSubtarget>(
TargetTriple, CPU, FS, Attrs.ABIName, isLittle, *this,
MaybeAlign(F.getParent()->getOverrideStackAlignment()));
}
LastSubtarget.set(FnAttrs, M, Attrs, I.get());
return I.get();
}
namespace {
/// Mips Code Generator Pass Configuration Options.
class MipsPassConfig : public TargetPassConfig {
public:
MipsPassConfig(MipsTargetMachine &TM, PassManagerBase &PM)
: TargetPassConfig(TM, PM) {
// The current implementation of long branch pass requires a scratch
// register ($at) to be available before branch instructions. Tail merging
// can break this requirement.
EnableTailMerge = false;
EnableLoopTermFold = true;
}
MipsTargetMachine &getMipsTargetMachine() const {
return getTM<MipsTargetMachine>();
}
void addIRPasses() override;
bool addInstSelector() override;
void addPreEmitPass() override;
void addPreRegAlloc() override;
bool addIRTranslator() override;
void addPreLegalizeMachineIR() override;
bool addLegalizeMachineIR() override;
void addPreRegBankSelect() override;
bool addRegBankSelect() override;
bool addGlobalInstructionSelect() override;
std::unique_ptr<CSEConfigBase> getCSEConfig() const override;
};
} // end anonymous namespace
TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
return new MipsPassConfig(*this, PM);
}
std::unique_ptr<CSEConfigBase> MipsPassConfig::getCSEConfig() const {
return getStandardCSEConfigForOpt(TM->getOptLevel());
}
void MipsPassConfig::addIRPasses() {
TargetPassConfig::addIRPasses();
addPass(createAtomicExpandLegacyPass());
if (getMipsTargetMachine().getCLOpts().os16)
addPass(createMipsOs16LegacyPass());
addPass(createMips16HardFloatLegacyPass());
}
// Install an instruction selector pass using
// the ISelDag to gen Mips code.
bool MipsPassConfig::addInstSelector() {
addPass(createMips16ISelDAGLegacyPass(getMipsTargetMachine(), getOptLevel()));
addPass(createMipsSEISelDAGLegacyPass(getMipsTargetMachine(), getOptLevel()));
return false;
}
void MipsPassConfig::addPreRegAlloc() {
addPass(createMipsOptimizePICCallLegacyPass());
}
TargetTransformInfo
MipsTargetMachine::getTargetTransformInfo(const Function &F) const {
LLVM_DEBUG(errs() << "Target Transform Info Pass Added\n");
return TargetTransformInfo(std::make_unique<MipsTTIImpl>(this, F));
}
MachineFunctionInfo *MipsTargetMachine::createMachineFunctionInfo(
BumpPtrAllocator &Allocator, const Function &F,
const TargetSubtargetInfo *STI) const {
return MipsFunctionInfo::create<MipsFunctionInfo>(Allocator, F, STI);
}
// Implemented by targets that want to run passes immediately before
// machine code is emitted.
void MipsPassConfig::addPreEmitPass() {
// Expand pseudo instructions that are sensitive to register allocation.
addPass(createMipsExpandPseudoLegacyPass());
// The microMIPS size reduction pass performs instruction reselection for
// instructions which can be remapped to a 16 bit instruction.
addPass(createMicroMipsSizeReduceLegacyPass());
// This pass inserts a nop instruction between two back-to-back multiplication
// instructions when the "mfix4300" flag is passed.
if (getMipsTargetMachine().getCLOpts().mfix4300)
addPass(createMipsMulMulBugFixLegacyPass());
// The delay slot filler pass can potientially create forbidden slot hazards
// for MIPSR6 and therefore it should go before MipsBranchExpansion pass.
addPass(createMipsDelaySlotFillerLegacyPass());
// This pass expands branches and takes care about the forbidden slot hazards.
// Expanding branches may potentially create forbidden slot hazards for
// MIPSR6, and fixing such hazard may potentially break a branch by extending
// its offset out of range. That's why this pass combine these two tasks, and
// runs them alternately until one of them finishes without any changes. Only
// then we can be sure that all branches are expanded properly and no hazards
// exists.
// Any new pass should go before this pass.
addPass(createMipsBranchExpansionLegacyPass());
addPass(createMipsConstantIslandLegacyPass());
}
bool MipsPassConfig::addIRTranslator() {
addPass(new IRTranslatorLegacy(getOptLevel()));
return false;
}
void MipsPassConfig::addPreLegalizeMachineIR() {
addPass(createMipsPreLegalizeCombinerLegacyPass());
}
bool MipsPassConfig::addLegalizeMachineIR() {
addPass(new LegalizerLegacy());
return false;
}
void MipsPassConfig::addPreRegBankSelect() {
bool IsOptNone = getOptLevel() == CodeGenOptLevel::None;
addPass(createMipsPostLegalizeCombinerLegacyPass(IsOptNone));
}
bool MipsPassConfig::addRegBankSelect() {
addPass(new RegBankSelectLegacy());
return false;
}
bool MipsPassConfig::addGlobalInstructionSelect() {
addPass(new InstructionSelectLegacy(getOptLevel()));
return false;
}