blob: b7594dde709d649773018582d3bdc289daedd75b [file] [log] [blame]
Nikolai Bozhenov82f08012017-05-29 09:48:30 +00001//===-- 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"
Nikolai Bozhenov1cf9c542017-12-07 12:35:02 +000016#include "Nios2TargetObjectFile.h"
Nikolai Bozhenov82f08012017-05-29 09:48:30 +000017
Nikolai Bozhenovebbde142017-09-19 11:54:29 +000018#include "llvm/CodeGen/TargetPassConfig.h"
19#include "llvm/Support/TargetRegistry.h"
20
Nikolai Bozhenov82f08012017-05-29 09:48:30 +000021using namespace llvm;
22
23#define DEBUG_TYPE "nios2"
24
25extern "C" void LLVMInitializeNios2Target() {
26 // Register the target.
Nikolai Bozhenovebbde142017-09-19 11:54:29 +000027 RegisterTargetMachine<Nios2TargetMachine> X(getTheNios2Target());
Nikolai Bozhenov82f08012017-05-29 09:48:30 +000028}
29
Nikolai Bozhenovebbde142017-09-19 11:54:29 +000030static std::string computeDataLayout() {
Nikolai Bozhenov82f08012017-05-29 09:48:30 +000031 return "e-p:32:32:32-i8:8:32-i16:16:32-n32";
32}
33
Nikolai Bozhenovebbde142017-09-19 11:54:29 +000034static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
35 if (!RM.hasValue())
Nikolai Bozhenov82f08012017-05-29 09:48:30 +000036 return Reloc::Static;
37 return *RM;
38}
39
Nikolai Bozhenov1cf9c542017-12-07 12:35:02 +000040static 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
Nikolai Bozhenov82f08012017-05-29 09:48:30 +000047Nios2TargetMachine::Nios2TargetMachine(const Target &T, const Triple &TT,
48 StringRef CPU, StringRef FS,
49 const TargetOptions &Options,
50 Optional<Reloc::Model> RM,
Nikolai Bozhenovebbde142017-09-19 11:54:29 +000051 Optional<CodeModel::Model> CM,
52 CodeGenOpt::Level OL, bool JIT)
Nikolai Bozhenov1cf9c542017-12-07 12:35:02 +000053 : 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}
Nikolai Bozhenov82f08012017-05-29 09:48:30 +000061
62Nios2TargetMachine::~Nios2TargetMachine() {}
Nikolai Bozhenovebbde142017-09-19 11:54:29 +000063
64const Nios2Subtarget *
65Nios2TargetMachine::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
87namespace {
88/// Nios2 Code Generator Pass Configuration Options.
89class Nios2PassConfig : public TargetPassConfig {
90public:
91 Nios2PassConfig(Nios2TargetMachine &TM, PassManagerBase *PM)
92 : TargetPassConfig(TM, *PM) {}
93
94 Nios2TargetMachine &getNios2TargetMachine() const {
95 return getTM<Nios2TargetMachine>();
96 }
97
Nikolai Bozhenovebbde142017-09-19 11:54:29 +000098 void addCodeGenPrepare() override;
Nikolai Bozhenov1cf9c542017-12-07 12:35:02 +000099 bool addInstSelector() override;
Nikolai Bozhenovebbde142017-09-19 11:54:29 +0000100 void addIRPasses() override;
101};
102} // namespace
103
104TargetPassConfig *Nios2TargetMachine::createPassConfig(PassManagerBase &PM) {
105 return new Nios2PassConfig(*this, &PM);
106}
107
108void Nios2PassConfig::addCodeGenPrepare() {
109 TargetPassConfig::addCodeGenPrepare();
110}
111
112void Nios2PassConfig::addIRPasses() { TargetPassConfig::addIRPasses(); }
Nikolai Bozhenov1cf9c542017-12-07 12:35:02 +0000113
114// Install an instruction selector pass using
115// the ISelDag to gen Nios2 code.
116bool Nios2PassConfig::addInstSelector() {
117 addPass(createNios2ISelDag(getNios2TargetMachine(), getOptLevel()));
118 return false;
119}