Nikolai Bozhenov | 82f0801 | 2017-05-29 09:48:30 +0000 | [diff] [blame] | 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 | |
Nikolai Bozhenov | ebbde14 | 2017-09-19 11:54:29 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/TargetPassConfig.h" |
| 18 | #include "llvm/Support/TargetRegistry.h" |
| 19 | |
Nikolai Bozhenov | 82f0801 | 2017-05-29 09:48:30 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
| 22 | #define DEBUG_TYPE "nios2" |
| 23 | |
| 24 | extern "C" void LLVMInitializeNios2Target() { |
| 25 | // Register the target. |
Nikolai Bozhenov | ebbde14 | 2017-09-19 11:54:29 +0000 | [diff] [blame] | 26 | RegisterTargetMachine<Nios2TargetMachine> X(getTheNios2Target()); |
Nikolai Bozhenov | 82f0801 | 2017-05-29 09:48:30 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Nikolai Bozhenov | ebbde14 | 2017-09-19 11:54:29 +0000 | [diff] [blame] | 29 | static std::string computeDataLayout() { |
Nikolai Bozhenov | 82f0801 | 2017-05-29 09:48:30 +0000 | [diff] [blame] | 30 | return "e-p:32:32:32-i8:8:32-i16:16:32-n32"; |
| 31 | } |
| 32 | |
Nikolai Bozhenov | ebbde14 | 2017-09-19 11:54:29 +0000 | [diff] [blame] | 33 | static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) { |
| 34 | if (!RM.hasValue()) |
Nikolai Bozhenov | 82f0801 | 2017-05-29 09:48:30 +0000 | [diff] [blame] | 35 | return Reloc::Static; |
| 36 | return *RM; |
| 37 | } |
| 38 | |
| 39 | Nios2TargetMachine::Nios2TargetMachine(const Target &T, const Triple &TT, |
| 40 | StringRef CPU, StringRef FS, |
| 41 | const TargetOptions &Options, |
| 42 | Optional<Reloc::Model> RM, |
Nikolai Bozhenov | ebbde14 | 2017-09-19 11:54:29 +0000 | [diff] [blame] | 43 | Optional<CodeModel::Model> CM, |
| 44 | CodeGenOpt::Level OL, bool JIT) |
| 45 | : LLVMTargetMachine(T, computeDataLayout(), TT, CPU, FS, Options, |
Eric Christopher | adc4bc6 | 2017-09-20 20:32:23 +0000 | [diff] [blame] | 46 | getEffectiveRelocModel(RM), *CM, OL) {} |
Nikolai Bozhenov | 82f0801 | 2017-05-29 09:48:30 +0000 | [diff] [blame] | 47 | |
| 48 | Nios2TargetMachine::~Nios2TargetMachine() {} |
Nikolai Bozhenov | ebbde14 | 2017-09-19 11:54:29 +0000 | [diff] [blame] | 49 | |
| 50 | const Nios2Subtarget * |
| 51 | Nios2TargetMachine::getSubtargetImpl(const Function &F) const { |
| 52 | Attribute CPUAttr = F.getFnAttribute("target-cpu"); |
| 53 | Attribute FSAttr = F.getFnAttribute("target-features"); |
| 54 | |
| 55 | std::string CPU = !CPUAttr.hasAttribute(Attribute::None) |
| 56 | ? CPUAttr.getValueAsString().str() |
| 57 | : TargetCPU; |
| 58 | std::string FS = !FSAttr.hasAttribute(Attribute::None) |
| 59 | ? FSAttr.getValueAsString().str() |
| 60 | : TargetFS; |
| 61 | |
| 62 | auto &I = SubtargetMap[CPU + FS]; |
| 63 | if (!I) { |
| 64 | // This needs to be done before we create a new subtarget since any |
| 65 | // creation will depend on the TM and the code generation flags on the |
| 66 | // function that reside in TargetOptions. |
| 67 | resetTargetOptions(F); |
| 68 | I = llvm::make_unique<Nios2Subtarget>(TargetTriple, CPU, FS, *this); |
| 69 | } |
| 70 | return I.get(); |
| 71 | } |
| 72 | |
| 73 | namespace { |
| 74 | /// Nios2 Code Generator Pass Configuration Options. |
| 75 | class Nios2PassConfig : public TargetPassConfig { |
| 76 | public: |
| 77 | Nios2PassConfig(Nios2TargetMachine &TM, PassManagerBase *PM) |
| 78 | : TargetPassConfig(TM, *PM) {} |
| 79 | |
| 80 | Nios2TargetMachine &getNios2TargetMachine() const { |
| 81 | return getTM<Nios2TargetMachine>(); |
| 82 | } |
| 83 | |
Nikolai Bozhenov | ebbde14 | 2017-09-19 11:54:29 +0000 | [diff] [blame] | 84 | void addCodeGenPrepare() override; |
| 85 | void addIRPasses() override; |
| 86 | }; |
| 87 | } // namespace |
| 88 | |
| 89 | TargetPassConfig *Nios2TargetMachine::createPassConfig(PassManagerBase &PM) { |
| 90 | return new Nios2PassConfig(*this, &PM); |
| 91 | } |
| 92 | |
| 93 | void Nios2PassConfig::addCodeGenPrepare() { |
| 94 | TargetPassConfig::addCodeGenPrepare(); |
| 95 | } |
| 96 | |
| 97 | void Nios2PassConfig::addIRPasses() { TargetPassConfig::addIRPasses(); } |