]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/Target/Nios2/Nios2TargetMachine.cpp
Vendor import of llvm trunk r338536:
[FreeBSD/FreeBSD.git] / lib / Target / Nios2 / Nios2TargetMachine.cpp
1 //===-- Nios2TargetMachine.cpp - Define TargetMachine for Nios2 -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implements the info about Nios2 target spec.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Nios2TargetMachine.h"
15 #include "Nios2.h"
16 #include "Nios2TargetObjectFile.h"
17
18 #include "llvm/CodeGen/TargetPassConfig.h"
19 #include "llvm/Support/TargetRegistry.h"
20
21 using namespace llvm;
22
23 #define DEBUG_TYPE "nios2"
24
25 extern "C" void LLVMInitializeNios2Target() {
26   // Register the target.
27   RegisterTargetMachine<Nios2TargetMachine> X(getTheNios2Target());
28 }
29
30 static std::string computeDataLayout() {
31   return "e-p:32:32:32-i8:8:32-i16:16:32-n32";
32 }
33
34 static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
35   if (!RM.hasValue())
36     return Reloc::Static;
37   return *RM;
38 }
39
40 static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM,
41                                               Reloc::Model RM, bool JIT) {
42   if (CM)
43     return *CM;
44   return CodeModel::Small;
45 }
46
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)
53     : LLVMTargetMachine(
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) {
59   initAsmInfo();
60 }
61
62 Nios2TargetMachine::~Nios2TargetMachine() {}
63
64 const Nios2Subtarget *
65 Nios2TargetMachine::getSubtargetImpl(const Function &F) const {
66   Attribute CPUAttr = F.getFnAttribute("target-cpu");
67   Attribute FSAttr = F.getFnAttribute("target-features");
68
69   std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
70                         ? CPUAttr.getValueAsString().str()
71                         : TargetCPU;
72   std::string FS = !FSAttr.hasAttribute(Attribute::None)
73                        ? FSAttr.getValueAsString().str()
74                        : TargetFS;
75
76   auto &I = SubtargetMap[CPU + FS];
77   if (!I) {
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);
83   }
84   return I.get();
85 }
86
87 namespace {
88 /// Nios2 Code Generator Pass Configuration Options.
89 class Nios2PassConfig : public TargetPassConfig {
90 public:
91   Nios2PassConfig(Nios2TargetMachine &TM, PassManagerBase *PM)
92       : TargetPassConfig(TM, *PM) {}
93
94   Nios2TargetMachine &getNios2TargetMachine() const {
95     return getTM<Nios2TargetMachine>();
96   }
97
98   void addCodeGenPrepare() override;
99   bool addInstSelector() override;
100   void addIRPasses() override;
101 };
102 } // namespace
103
104 TargetPassConfig *Nios2TargetMachine::createPassConfig(PassManagerBase &PM) {
105   return new Nios2PassConfig(*this, &PM);
106 }
107
108 void Nios2PassConfig::addCodeGenPrepare() {
109   TargetPassConfig::addCodeGenPrepare();
110 }
111
112 void Nios2PassConfig::addIRPasses() { TargetPassConfig::addIRPasses(); }
113
114 // Install an instruction selector pass using
115 // the ISelDag to gen Nios2 code.
116 bool Nios2PassConfig::addInstSelector() {
117   addPass(createNios2ISelDag(getNios2TargetMachine(), getOptLevel()));
118   return false;
119 }