1 //===-- Nios2TargetMachine.cpp - Define TargetMachine for Nios2 -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implements the info about Nios2 target spec.
12 //===----------------------------------------------------------------------===//
14 #include "Nios2TargetMachine.h"
16 #include "Nios2TargetObjectFile.h"
18 #include "llvm/CodeGen/TargetPassConfig.h"
19 #include "llvm/Support/TargetRegistry.h"
23 #define DEBUG_TYPE "nios2"
25 extern "C" void LLVMInitializeNios2Target() {
26 // Register the target.
27 RegisterTargetMachine<Nios2TargetMachine> X(getTheNios2Target());
30 static std::string computeDataLayout() {
31 return "e-p:32:32:32-i8:8:32-i16:16:32-n32";
34 static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
40 static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM,
41 Reloc::Model RM, bool JIT) {
44 return CodeModel::Small;
47 Nios2TargetMachine::Nios2TargetMachine(const Target &T, const Triple &TT,
48 StringRef CPU, StringRef FS,
49 const TargetOptions &Options,
50 Optional<Reloc::Model> RM,
51 Optional<CodeModel::Model> CM,
52 CodeGenOpt::Level OL, bool JIT)
54 T, computeDataLayout(), TT, CPU, FS, Options,
55 getEffectiveRelocModel(RM),
56 getEffectiveCodeModel(CM, getEffectiveRelocModel(RM), JIT), OL),
57 TLOF(make_unique<Nios2TargetObjectFile>()),
58 Subtarget(TT, CPU, FS, *this) {
62 Nios2TargetMachine::~Nios2TargetMachine() {}
64 const Nios2Subtarget *
65 Nios2TargetMachine::getSubtargetImpl(const Function &F) const {
66 Attribute CPUAttr = F.getFnAttribute("target-cpu");
67 Attribute FSAttr = F.getFnAttribute("target-features");
69 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
70 ? CPUAttr.getValueAsString().str()
72 std::string FS = !FSAttr.hasAttribute(Attribute::None)
73 ? FSAttr.getValueAsString().str()
76 auto &I = SubtargetMap[CPU + FS];
78 // This needs to be done before we create a new subtarget since any
79 // creation will depend on the TM and the code generation flags on the
80 // function that reside in TargetOptions.
81 resetTargetOptions(F);
82 I = llvm::make_unique<Nios2Subtarget>(TargetTriple, CPU, FS, *this);
88 /// Nios2 Code Generator Pass Configuration Options.
89 class Nios2PassConfig : public TargetPassConfig {
91 Nios2PassConfig(Nios2TargetMachine &TM, PassManagerBase *PM)
92 : TargetPassConfig(TM, *PM) {}
94 Nios2TargetMachine &getNios2TargetMachine() const {
95 return getTM<Nios2TargetMachine>();
98 void addCodeGenPrepare() override;
99 bool addInstSelector() override;
100 void addIRPasses() override;
104 TargetPassConfig *Nios2TargetMachine::createPassConfig(PassManagerBase &PM) {
105 return new Nios2PassConfig(*this, &PM);
108 void Nios2PassConfig::addCodeGenPrepare() {
109 TargetPassConfig::addCodeGenPrepare();
112 void Nios2PassConfig::addIRPasses() { TargetPassConfig::addIRPasses(); }
114 // Install an instruction selector pass using
115 // the ISelDag to gen Nios2 code.
116 bool Nios2PassConfig::addInstSelector() {
117 addPass(createNios2ISelDag(getNios2TargetMachine(), getOptLevel()));