Alex Bradbury | b2e5472 | 2016-11-01 17:27:54 +0000 | [diff] [blame] | 1 | //===-- RISCVTargetMachine.cpp - Define TargetMachine for RISCV -----------===// |
| 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 RISCV target spec. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame^] | 14 | #include "RISCV.h" |
Alex Bradbury | b2e5472 | 2016-11-01 17:27:54 +0000 | [diff] [blame] | 15 | #include "RISCVTargetMachine.h" |
| 16 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/Passes.h" |
Alex Bradbury | b2e5472 | 2016-11-01 17:27:54 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" |
| 19 | #include "llvm/CodeGen/TargetPassConfig.h" |
| 20 | #include "llvm/IR/LegacyPassManager.h" |
Alex Bradbury | b2e5472 | 2016-11-01 17:27:54 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FormattedStream.h" |
| 22 | #include "llvm/Support/TargetRegistry.h" |
| 23 | #include "llvm/Target/TargetOptions.h" |
| 24 | using namespace llvm; |
| 25 | |
| 26 | extern "C" void LLVMInitializeRISCVTarget() { |
| 27 | RegisterTargetMachine<RISCVTargetMachine> X(getTheRISCV32Target()); |
| 28 | RegisterTargetMachine<RISCVTargetMachine> Y(getTheRISCV64Target()); |
| 29 | } |
| 30 | |
| 31 | static std::string computeDataLayout(const Triple &TT) { |
| 32 | if (TT.isArch64Bit()) { |
| 33 | return "e-m:e-i64:64-n32:64-S128"; |
| 34 | } else { |
| 35 | assert(TT.isArch32Bit() && "only RV32 and RV64 are currently supported"); |
Alex Bradbury | e4f731b | 2017-02-14 05:20:20 +0000 | [diff] [blame] | 36 | return "e-m:e-p:32:32-i64:64-n32-S128"; |
Alex Bradbury | b2e5472 | 2016-11-01 17:27:54 +0000 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
| 40 | static Reloc::Model getEffectiveRelocModel(const Triple &TT, |
| 41 | Optional<Reloc::Model> RM) { |
| 42 | if (!RM.hasValue()) |
| 43 | return Reloc::Static; |
| 44 | return *RM; |
| 45 | } |
| 46 | |
Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 47 | static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM) { |
| 48 | if (CM) |
| 49 | return *CM; |
| 50 | return CodeModel::Small; |
| 51 | } |
| 52 | |
Alex Bradbury | b2e5472 | 2016-11-01 17:27:54 +0000 | [diff] [blame] | 53 | RISCVTargetMachine::RISCVTargetMachine(const Target &T, const Triple &TT, |
| 54 | StringRef CPU, StringRef FS, |
| 55 | const TargetOptions &Options, |
| 56 | Optional<Reloc::Model> RM, |
Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 57 | Optional<CodeModel::Model> CM, |
| 58 | CodeGenOpt::Level OL, bool JIT) |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 59 | : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options, |
| 60 | getEffectiveRelocModel(TT, RM), |
| 61 | getEffectiveCodeModel(CM), OL), |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame^] | 62 | TLOF(make_unique<TargetLoweringObjectFileELF>()), |
| 63 | Subtarget(TT, CPU, FS, *this) { |
Alex Bradbury | e4f731b | 2017-02-14 05:20:20 +0000 | [diff] [blame] | 64 | initAsmInfo(); |
| 65 | } |
Alex Bradbury | b2e5472 | 2016-11-01 17:27:54 +0000 | [diff] [blame] | 66 | |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame^] | 67 | namespace { |
| 68 | class RISCVPassConfig : public TargetPassConfig { |
| 69 | public: |
| 70 | RISCVPassConfig(RISCVTargetMachine &TM, PassManagerBase &PM) |
| 71 | : TargetPassConfig(TM, PM) {} |
| 72 | |
| 73 | RISCVTargetMachine &getRISCVTargetMachine() const { |
| 74 | return getTM<RISCVTargetMachine>(); |
| 75 | } |
| 76 | |
| 77 | bool addInstSelector() override; |
| 78 | }; |
| 79 | } |
| 80 | |
Alex Bradbury | b2e5472 | 2016-11-01 17:27:54 +0000 | [diff] [blame] | 81 | TargetPassConfig *RISCVTargetMachine::createPassConfig(PassManagerBase &PM) { |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame^] | 82 | return new RISCVPassConfig(*this, PM); |
| 83 | } |
| 84 | |
| 85 | bool RISCVPassConfig::addInstSelector() { |
| 86 | addPass(createRISCVISelDag(getRISCVTargetMachine())); |
| 87 | |
| 88 | return false; |
Alex Bradbury | b2e5472 | 2016-11-01 17:27:54 +0000 | [diff] [blame] | 89 | } |