2012-02-17 08:55:11 +00:00
|
|
|
//===-- MipsTargetMachine.cpp - Define TargetMachine for Mips -------------===//
|
2007-06-06 07:42:06 +00:00
|
|
|
//
|
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-06-06 07:42:06 +00:00
|
|
|
//
|
2011-04-15 21:51:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-06-06 07:42:06 +00:00
|
|
|
//
|
|
|
|
|
// Implements the info about Mips target spec.
|
|
|
|
|
//
|
2011-04-15 21:51:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-06-06 07:42:06 +00:00
|
|
|
|
|
|
|
|
#include "MipsTargetMachine.h"
|
2012-03-17 18:46:09 +00:00
|
|
|
#include "Mips.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "Mips16FrameLowering.h"
|
|
|
|
|
#include "Mips16ISelDAGToDAG.h"
|
|
|
|
|
#include "Mips16ISelLowering.h"
|
|
|
|
|
#include "Mips16InstrInfo.h"
|
2012-08-02 18:21:47 +00:00
|
|
|
#include "MipsFrameLowering.h"
|
|
|
|
|
#include "MipsInstrInfo.h"
|
2013-04-09 19:46:01 +00:00
|
|
|
#include "MipsSEFrameLowering.h"
|
|
|
|
|
#include "MipsSEISelDAGToDAG.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "MipsSEISelLowering.h"
|
|
|
|
|
#include "MipsSEInstrInfo.h"
|
2014-11-13 09:26:31 +00:00
|
|
|
#include "MipsTargetObjectFile.h"
|
2013-04-09 19:46:01 +00:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2012-02-03 05:12:41 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2016-05-10 03:21:59 +00:00
|
|
|
#include "llvm/CodeGen/TargetPassConfig.h"
|
2015-02-13 10:01:29 +00:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2013-04-09 19:46:01 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2011-08-24 18:08:43 +00:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-08-23 10:27:02 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2015-03-14 08:34:25 +00:00
|
|
|
|
2007-06-06 07:42:06 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
2014-04-21 22:55:11 +00:00
|
|
|
#define DEBUG_TYPE "mips"
|
|
|
|
|
|
2009-07-25 06:49:55 +00:00
|
|
|
extern "C" void LLVMInitializeMipsTarget() {
|
|
|
|
|
// Register the target.
|
2016-10-09 23:00:34 +00:00
|
|
|
RegisterTargetMachine<MipsebTargetMachine> X(getTheMipsTarget());
|
|
|
|
|
RegisterTargetMachine<MipselTargetMachine> Y(getTheMipselTarget());
|
|
|
|
|
RegisterTargetMachine<MipsebTargetMachine> A(getTheMips64Target());
|
|
|
|
|
RegisterTargetMachine<MipselTargetMachine> B(getTheMips64elTarget());
|
2007-06-06 07:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-11 15:34:59 +00:00
|
|
|
static std::string computeDataLayout(const Triple &TT, StringRef CPU,
|
2015-03-12 00:07:24 +00:00
|
|
|
const TargetOptions &Options,
|
|
|
|
|
bool isLittle) {
|
2015-01-26 19:03:15 +00:00
|
|
|
std::string Ret = "";
|
2015-09-15 16:17:27 +00:00
|
|
|
MipsABIInfo ABI = MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions);
|
2015-01-26 19:03:15 +00:00
|
|
|
|
|
|
|
|
// There are both little and big endian mips.
|
|
|
|
|
if (isLittle)
|
|
|
|
|
Ret += "e";
|
|
|
|
|
else
|
|
|
|
|
Ret += "E";
|
|
|
|
|
|
2016-07-19 10:49:03 +00:00
|
|
|
if (ABI.IsO32())
|
|
|
|
|
Ret += "-m:m";
|
|
|
|
|
else
|
|
|
|
|
Ret += "-m:e";
|
2015-01-26 19:03:15 +00:00
|
|
|
|
|
|
|
|
// Pointers are 32 bit on some ABIs.
|
|
|
|
|
if (!ABI.IsN64())
|
|
|
|
|
Ret += "-p:32:32";
|
|
|
|
|
|
2015-07-07 21:31:54 +00:00
|
|
|
// 8 and 16 bit integers only need to have natural alignment, but try to
|
2015-01-26 19:03:15 +00:00
|
|
|
// align them to 32 bits. 64 bit integers have natural alignment.
|
|
|
|
|
Ret += "-i8:8:32-i16:16:32-i64:64";
|
|
|
|
|
|
|
|
|
|
// 32 bit registers are always available and the stack is at least 64 bit
|
|
|
|
|
// aligned. On N64 64 bit registers are also available and the stack is
|
|
|
|
|
// 128 bit aligned.
|
|
|
|
|
if (ABI.IsN64() || ABI.IsN32())
|
|
|
|
|
Ret += "-n32:64-S128";
|
|
|
|
|
else
|
|
|
|
|
Ret += "-n32-S64";
|
|
|
|
|
|
|
|
|
|
return Ret;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-18 22:04:49 +00:00
|
|
|
static Reloc::Model getEffectiveRelocModel(CodeModel::Model CM,
|
|
|
|
|
Optional<Reloc::Model> RM) {
|
|
|
|
|
if (!RM.hasValue() || CM == CodeModel::JITDefault)
|
|
|
|
|
return Reloc::Static;
|
|
|
|
|
return *RM;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-28 05:13:42 +00:00
|
|
|
// On function prologue, the stack is created by decrementing
|
|
|
|
|
// its pointer. Once decremented, all references are done with positive
|
2010-11-15 00:06:54 +00:00
|
|
|
// offset from the stack/frame pointer, using StackGrowsUp enables
|
2008-08-06 06:14:43 +00:00
|
|
|
// an easier handling.
|
2008-07-05 19:05:21 +00:00
|
|
|
// Using CodeModel::Large enables different CALL behavior.
|
2015-06-11 19:41:26 +00:00
|
|
|
MipsTargetMachine::MipsTargetMachine(const Target &T, const Triple &TT,
|
2014-07-02 00:54:07 +00:00
|
|
|
StringRef CPU, StringRef FS,
|
|
|
|
|
const TargetOptions &Options,
|
2016-05-18 22:04:49 +00:00
|
|
|
Optional<Reloc::Model> RM,
|
|
|
|
|
CodeModel::Model CM, CodeGenOpt::Level OL,
|
|
|
|
|
bool isLittle)
|
2015-06-11 19:41:26 +00:00
|
|
|
: LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
|
2016-05-18 22:04:49 +00:00
|
|
|
CPU, FS, Options, getEffectiveRelocModel(CM, RM), CM,
|
|
|
|
|
OL),
|
2015-01-26 17:33:46 +00:00
|
|
|
isLittle(isLittle), TLOF(make_unique<MipsTargetObjectFile>()),
|
2015-09-15 16:17:27 +00:00
|
|
|
ABI(MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions)),
|
2015-06-11 19:41:26 +00:00
|
|
|
Subtarget(nullptr), DefaultSubtarget(TT, CPU, FS, isLittle, *this),
|
|
|
|
|
NoMips16Subtarget(TT, CPU, FS.empty() ? "-mips16" : FS.str() + ",-mips16",
|
2015-01-08 18:18:57 +00:00
|
|
|
isLittle, *this),
|
2015-06-11 19:41:26 +00:00
|
|
|
Mips16Subtarget(TT, CPU, FS.empty() ? "+mips16" : FS.str() + ",+mips16",
|
|
|
|
|
isLittle, *this) {
|
2014-07-18 23:41:32 +00:00
|
|
|
Subtarget = &DefaultSubtarget;
|
2013-05-13 01:16:13 +00:00
|
|
|
initAsmInfo();
|
2007-10-09 03:01:19 +00:00
|
|
|
}
|
2007-06-06 07:42:06 +00:00
|
|
|
|
2014-11-20 23:37:18 +00:00
|
|
|
MipsTargetMachine::~MipsTargetMachine() {}
|
|
|
|
|
|
2011-12-20 02:50:00 +00:00
|
|
|
void MipsebTargetMachine::anchor() { }
|
|
|
|
|
|
2015-06-11 19:41:26 +00:00
|
|
|
MipsebTargetMachine::MipsebTargetMachine(const Target &T, const Triple &TT,
|
|
|
|
|
StringRef CPU, StringRef FS,
|
|
|
|
|
const TargetOptions &Options,
|
2016-05-18 22:04:49 +00:00
|
|
|
Optional<Reloc::Model> RM,
|
|
|
|
|
CodeModel::Model CM,
|
2015-06-11 19:41:26 +00:00
|
|
|
CodeGenOpt::Level OL)
|
|
|
|
|
: MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
|
2011-09-21 03:00:58 +00:00
|
|
|
|
2011-12-20 02:50:00 +00:00
|
|
|
void MipselTargetMachine::anchor() { }
|
|
|
|
|
|
2015-06-11 19:41:26 +00:00
|
|
|
MipselTargetMachine::MipselTargetMachine(const Target &T, const Triple &TT,
|
|
|
|
|
StringRef CPU, StringRef FS,
|
|
|
|
|
const TargetOptions &Options,
|
2016-05-18 22:04:49 +00:00
|
|
|
Optional<Reloc::Model> RM,
|
|
|
|
|
CodeModel::Model CM,
|
2015-06-11 19:41:26 +00:00
|
|
|
CodeGenOpt::Level OL)
|
|
|
|
|
: MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
|
2008-06-04 01:45:25 +00:00
|
|
|
|
2014-09-26 01:44:08 +00:00
|
|
|
const MipsSubtarget *
|
2014-09-26 02:57:05 +00:00
|
|
|
MipsTargetMachine::getSubtargetImpl(const Function &F) const {
|
2015-02-14 02:37:48 +00:00
|
|
|
Attribute CPUAttr = F.getFnAttribute("target-cpu");
|
|
|
|
|
Attribute FSAttr = F.getFnAttribute("target-features");
|
2014-09-26 01:44:08 +00:00
|
|
|
|
|
|
|
|
std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
|
|
|
|
|
? CPUAttr.getValueAsString().str()
|
|
|
|
|
: TargetCPU;
|
|
|
|
|
std::string FS = !FSAttr.hasAttribute(Attribute::None)
|
|
|
|
|
? FSAttr.getValueAsString().str()
|
|
|
|
|
: TargetFS;
|
|
|
|
|
bool hasMips16Attr =
|
2015-02-14 02:37:48 +00:00
|
|
|
!F.getFnAttribute("mips16").hasAttribute(Attribute::None);
|
2014-09-26 01:44:08 +00:00
|
|
|
bool hasNoMips16Attr =
|
2015-02-14 02:37:48 +00:00
|
|
|
!F.getFnAttribute("nomips16").hasAttribute(Attribute::None);
|
2014-09-26 01:44:08 +00:00
|
|
|
|
2014-09-29 21:57:54 +00:00
|
|
|
// 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
|
2015-05-07 10:29:52 +00:00
|
|
|
// function, so we can enable it as a subtarget feature.
|
2015-05-12 01:26:05 +00:00
|
|
|
bool softFloat =
|
|
|
|
|
F.hasFnAttribute("use-soft-float") &&
|
|
|
|
|
F.getFnAttribute("use-soft-float").getValueAsString() == "true";
|
2014-09-29 21:57:54 +00:00
|
|
|
|
2014-09-26 01:44:08 +00:00
|
|
|
if (hasMips16Attr)
|
|
|
|
|
FS += FS.empty() ? "+mips16" : ",+mips16";
|
|
|
|
|
else if (hasNoMips16Attr)
|
|
|
|
|
FS += FS.empty() ? "-mips16" : ",-mips16";
|
2015-05-07 10:29:52 +00:00
|
|
|
if (softFloat)
|
|
|
|
|
FS += FS.empty() ? "+soft-float" : ",+soft-float";
|
2014-09-26 01:44:08 +00:00
|
|
|
|
2015-05-07 10:29:52 +00:00
|
|
|
auto &I = SubtargetMap[CPU + FS];
|
2014-09-26 01:44:08 +00:00
|
|
|
if (!I) {
|
|
|
|
|
// This needs to be done before we create a new subtarget since any
|
|
|
|
|
// creation will depend on the TM and the code generation flags on the
|
|
|
|
|
// function that reside in TargetOptions.
|
|
|
|
|
resetTargetOptions(F);
|
2015-06-16 15:44:21 +00:00
|
|
|
I = llvm::make_unique<MipsSubtarget>(TargetTriple, CPU, FS, isLittle,
|
|
|
|
|
*this);
|
2014-09-26 01:44:08 +00:00
|
|
|
}
|
|
|
|
|
return I.get();
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-18 23:41:32 +00:00
|
|
|
void MipsTargetMachine::resetSubtarget(MachineFunction *MF) {
|
|
|
|
|
DEBUG(dbgs() << "resetSubtarget\n");
|
2014-09-26 01:44:08 +00:00
|
|
|
|
2014-09-26 02:57:05 +00:00
|
|
|
Subtarget = const_cast<MipsSubtarget *>(getSubtargetImpl(*MF->getFunction()));
|
2014-08-05 02:39:49 +00:00
|
|
|
MF->setSubtarget(Subtarget);
|
2014-07-18 23:41:32 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-03 05:12:41 +00:00
|
|
|
namespace {
|
|
|
|
|
/// Mips Code Generator Pass Configuration Options.
|
|
|
|
|
class MipsPassConfig : public TargetPassConfig {
|
|
|
|
|
public:
|
2012-02-04 02:56:59 +00:00
|
|
|
MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
|
2013-10-07 19:13:53 +00:00
|
|
|
: 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, so disable it when long branch pass is
|
|
|
|
|
// enabled.
|
|
|
|
|
EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
|
|
|
|
|
}
|
2012-02-03 05:12:41 +00:00
|
|
|
|
|
|
|
|
MipsTargetMachine &getMipsTargetMachine() const {
|
|
|
|
|
return getTM<MipsTargetMachine>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MipsSubtarget &getMipsSubtarget() const {
|
|
|
|
|
return *getMipsTargetMachine().getSubtargetImpl();
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-29 07:58:02 +00:00
|
|
|
void addIRPasses() override;
|
|
|
|
|
bool addInstSelector() override;
|
2014-12-11 21:26:47 +00:00
|
|
|
void addPreEmitPass() override;
|
2014-03-10 16:31:25 +00:00
|
|
|
|
2014-12-11 21:26:47 +00:00
|
|
|
void addPreRegAlloc() override;
|
2014-03-10 16:31:25 +00:00
|
|
|
|
2012-02-03 05:12:41 +00:00
|
|
|
};
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2012-02-04 02:56:59 +00:00
|
|
|
TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
|
|
|
|
|
return new MipsPassConfig(this, PM);
|
2012-02-03 05:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-10 16:58:04 +00:00
|
|
|
void MipsPassConfig::addIRPasses() {
|
|
|
|
|
TargetPassConfig::addIRPasses();
|
2014-10-16 20:34:57 +00:00
|
|
|
addPass(createAtomicExpandPass(&getMipsTargetMachine()));
|
2013-04-10 16:58:04 +00:00
|
|
|
if (getMipsSubtarget().os16())
|
2015-03-14 08:34:25 +00:00
|
|
|
addPass(createMipsOs16Pass(getMipsTargetMachine()));
|
2013-05-10 22:25:39 +00:00
|
|
|
if (getMipsSubtarget().inMips16HardFloat())
|
2015-03-14 09:02:23 +00:00
|
|
|
addPass(createMips16HardFloatPass(getMipsTargetMachine()));
|
2013-04-10 16:58:04 +00:00
|
|
|
}
|
2010-11-15 00:06:54 +00:00
|
|
|
// Install an instruction selector pass using
|
2007-06-06 07:42:06 +00:00
|
|
|
// the ISelDag to gen Mips code.
|
2012-05-01 08:27:43 +00:00
|
|
|
bool MipsPassConfig::addInstSelector() {
|
2015-03-14 09:20:52 +00:00
|
|
|
addPass(createMipsModuleISelDagPass(getMipsTargetMachine()));
|
2016-07-14 13:25:22 +00:00
|
|
|
addPass(createMips16ISelDag(getMipsTargetMachine(), getOptLevel()));
|
|
|
|
|
addPass(createMipsSEISelDag(getMipsTargetMachine(), getOptLevel()));
|
2007-06-06 07:42:06 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-11 21:26:47 +00:00
|
|
|
void MipsPassConfig::addPreRegAlloc() {
|
2016-11-02 15:11:27 +00:00
|
|
|
addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
|
2014-03-10 16:31:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-01 13:20:00 +00:00
|
|
|
TargetIRAnalysis MipsTargetMachine::getTargetIRAnalysis() {
|
2015-09-16 23:38:13 +00:00
|
|
|
return TargetIRAnalysis([this](const Function &F) {
|
2015-02-01 13:20:00 +00:00
|
|
|
if (Subtarget->allowMixed16_32()) {
|
|
|
|
|
DEBUG(errs() << "No Target Transform Info Pass Added\n");
|
|
|
|
|
// FIXME: This is no longer necessary as the TTI returned is per-function.
|
2015-07-09 02:08:42 +00:00
|
|
|
return TargetTransformInfo(F.getParent()->getDataLayout());
|
2015-02-01 13:20:00 +00:00
|
|
|
}
|
2015-01-31 11:17:59 +00:00
|
|
|
|
2015-02-01 13:20:00 +00:00
|
|
|
DEBUG(errs() << "Target Transform Info Pass Added\n");
|
2015-02-01 14:22:17 +00:00
|
|
|
return TargetTransformInfo(BasicTTIImpl(this, F));
|
2015-02-01 13:20:00 +00:00
|
|
|
});
|
2013-04-09 19:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
2010-11-15 00:06:54 +00:00
|
|
|
// Implemented by targets that want to run passes immediately before
|
|
|
|
|
// machine code is emitted. return true if -print-machineinstrs should
|
2007-06-06 07:42:06 +00:00
|
|
|
// print out the code after the passes.
|
2014-12-11 21:26:47 +00:00
|
|
|
void MipsPassConfig::addPreEmitPass() {
|
2012-06-14 01:19:35 +00:00
|
|
|
MipsTargetMachine &TM = getMipsTargetMachine();
|
2016-03-14 16:24:05 +00:00
|
|
|
|
|
|
|
|
// The delay slot filler pass can potientially create forbidden slot (FS)
|
|
|
|
|
// hazards for MIPSR6 which the hazard schedule pass (HSP) will fix. Any
|
|
|
|
|
// (new) pass that creates compact branches after the HSP must handle FS
|
|
|
|
|
// hazards itself or be pipelined before the HSP.
|
2014-12-11 23:18:03 +00:00
|
|
|
addPass(createMipsDelaySlotFillerPass(TM));
|
2016-03-14 18:10:20 +00:00
|
|
|
addPass(createMipsHazardSchedule());
|
2014-12-11 23:18:03 +00:00
|
|
|
addPass(createMipsLongBranchPass(TM));
|
2016-06-28 14:26:39 +00:00
|
|
|
addPass(createMipsConstantIslandPass());
|
2007-06-06 07:42:06 +00:00
|
|
|
}
|