blob: 34da6de504df0ae046e467b715a9239a6dc206b4 [file] [log] [blame]
Alex Bradburyb2e54722016-11-01 17:27:54 +00001//===-- 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 Bradbury89718422017-10-19 21:37:38 +000014#include "RISCV.h"
Alex Bradburyb2e54722016-11-01 17:27:54 +000015#include "RISCVTargetMachine.h"
16#include "llvm/ADT/STLExtras.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000017#include "llvm/CodeGen/Passes.h"
Alex Bradburyb2e54722016-11-01 17:27:54 +000018#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
19#include "llvm/CodeGen/TargetPassConfig.h"
20#include "llvm/IR/LegacyPassManager.h"
Alex Bradburyb2e54722016-11-01 17:27:54 +000021#include "llvm/Support/FormattedStream.h"
22#include "llvm/Support/TargetRegistry.h"
23#include "llvm/Target/TargetOptions.h"
24using namespace llvm;
25
26extern "C" void LLVMInitializeRISCVTarget() {
27 RegisterTargetMachine<RISCVTargetMachine> X(getTheRISCV32Target());
28 RegisterTargetMachine<RISCVTargetMachine> Y(getTheRISCV64Target());
29}
30
31static 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 Bradburye4f731b2017-02-14 05:20:20 +000036 return "e-m:e-p:32:32-i64:64-n32-S128";
Alex Bradburyb2e54722016-11-01 17:27:54 +000037 }
38}
39
40static 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 Espindola79e238a2017-08-03 02:16:21 +000047static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM) {
48 if (CM)
49 return *CM;
50 return CodeModel::Small;
51}
52
Alex Bradburyb2e54722016-11-01 17:27:54 +000053RISCVTargetMachine::RISCVTargetMachine(const Target &T, const Triple &TT,
54 StringRef CPU, StringRef FS,
55 const TargetOptions &Options,
56 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +000057 Optional<CodeModel::Model> CM,
58 CodeGenOpt::Level OL, bool JIT)
Matthias Braunbb8507e2017-10-12 22:57:28 +000059 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
60 getEffectiveRelocModel(TT, RM),
61 getEffectiveCodeModel(CM), OL),
Alex Bradbury89718422017-10-19 21:37:38 +000062 TLOF(make_unique<TargetLoweringObjectFileELF>()),
63 Subtarget(TT, CPU, FS, *this) {
Alex Bradburye4f731b2017-02-14 05:20:20 +000064 initAsmInfo();
65}
Alex Bradburyb2e54722016-11-01 17:27:54 +000066
Alex Bradbury89718422017-10-19 21:37:38 +000067namespace {
68class RISCVPassConfig : public TargetPassConfig {
69public:
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 Bradburyb2e54722016-11-01 17:27:54 +000081TargetPassConfig *RISCVTargetMachine::createPassConfig(PassManagerBase &PM) {
Alex Bradbury89718422017-10-19 21:37:38 +000082 return new RISCVPassConfig(*this, PM);
83}
84
85bool RISCVPassConfig::addInstSelector() {
86 addPass(createRISCVISelDag(getRISCVTargetMachine()));
87
88 return false;
Alex Bradburyb2e54722016-11-01 17:27:54 +000089}