blob: 29f6bead42fb968b07af6d683e1af7b86f3708b4 [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"
Mandeep Singh Grang98bc25a2018-03-24 18:37:19 +000016#include "RISCVTargetObjectFile.h"
Alex Bradburyb2e54722016-11-01 17:27:54 +000017#include "llvm/ADT/STLExtras.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000018#include "llvm/CodeGen/Passes.h"
Alex Bradburyb2e54722016-11-01 17:27:54 +000019#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
20#include "llvm/CodeGen/TargetPassConfig.h"
21#include "llvm/IR/LegacyPassManager.h"
Alex Bradburyb2e54722016-11-01 17:27:54 +000022#include "llvm/Support/FormattedStream.h"
23#include "llvm/Support/TargetRegistry.h"
24#include "llvm/Target/TargetOptions.h"
25using namespace llvm;
26
27extern "C" void LLVMInitializeRISCVTarget() {
28 RegisterTargetMachine<RISCVTargetMachine> X(getTheRISCV32Target());
29 RegisterTargetMachine<RISCVTargetMachine> Y(getTheRISCV64Target());
30}
31
32static std::string computeDataLayout(const Triple &TT) {
33 if (TT.isArch64Bit()) {
Mandeep Singh Grang47fbc592017-11-16 20:30:49 +000034 return "e-m:e-p:64:64-i64:64-i128:128-n64-S128";
Alex Bradburyb2e54722016-11-01 17:27:54 +000035 } else {
36 assert(TT.isArch32Bit() && "only RV32 and RV64 are currently supported");
Alex Bradburye4f731b2017-02-14 05:20:20 +000037 return "e-m:e-p:32:32-i64:64-n32-S128";
Alex Bradburyb2e54722016-11-01 17:27:54 +000038 }
39}
40
41static Reloc::Model getEffectiveRelocModel(const Triple &TT,
42 Optional<Reloc::Model> RM) {
43 if (!RM.hasValue())
44 return Reloc::Static;
45 return *RM;
46}
47
Rafael Espindola79e238a2017-08-03 02:16:21 +000048static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM) {
49 if (CM)
50 return *CM;
51 return CodeModel::Small;
52}
53
Alex Bradburyb2e54722016-11-01 17:27:54 +000054RISCVTargetMachine::RISCVTargetMachine(const Target &T, const Triple &TT,
55 StringRef CPU, StringRef FS,
56 const TargetOptions &Options,
57 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +000058 Optional<CodeModel::Model> CM,
59 CodeGenOpt::Level OL, bool JIT)
Matthias Braunbb8507e2017-10-12 22:57:28 +000060 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
61 getEffectiveRelocModel(TT, RM),
62 getEffectiveCodeModel(CM), OL),
Mandeep Singh Grang98bc25a2018-03-24 18:37:19 +000063 TLOF(make_unique<RISCVELFTargetObjectFile>()),
Alex Bradbury89718422017-10-19 21:37:38 +000064 Subtarget(TT, CPU, FS, *this) {
Alex Bradburye4f731b2017-02-14 05:20:20 +000065 initAsmInfo();
66}
Alex Bradburyb2e54722016-11-01 17:27:54 +000067
Alex Bradbury89718422017-10-19 21:37:38 +000068namespace {
69class RISCVPassConfig : public TargetPassConfig {
70public:
71 RISCVPassConfig(RISCVTargetMachine &TM, PassManagerBase &PM)
72 : TargetPassConfig(TM, PM) {}
73
74 RISCVTargetMachine &getRISCVTargetMachine() const {
75 return getTM<RISCVTargetMachine>();
76 }
77
Alex Bradburydc790dd2018-06-13 11:58:46 +000078 void addIRPasses() override;
Alex Bradbury89718422017-10-19 21:37:38 +000079 bool addInstSelector() override;
Alex Bradbury315cd3a2018-01-10 21:05:07 +000080 void addPreEmitPass() override;
Alex Bradbury89718422017-10-19 21:37:38 +000081};
82}
83
Alex Bradburyb2e54722016-11-01 17:27:54 +000084TargetPassConfig *RISCVTargetMachine::createPassConfig(PassManagerBase &PM) {
Alex Bradbury89718422017-10-19 21:37:38 +000085 return new RISCVPassConfig(*this, PM);
86}
87
Alex Bradburydc790dd2018-06-13 11:58:46 +000088void RISCVPassConfig::addIRPasses() {
89 addPass(createAtomicExpandPass());
90 TargetPassConfig::addIRPasses();
91}
92
Alex Bradbury89718422017-10-19 21:37:38 +000093bool RISCVPassConfig::addInstSelector() {
94 addPass(createRISCVISelDag(getRISCVTargetMachine()));
95
96 return false;
Alex Bradburyb2e54722016-11-01 17:27:54 +000097}
Alex Bradbury315cd3a2018-01-10 21:05:07 +000098
99void RISCVPassConfig::addPreEmitPass() { addPass(&BranchRelaxationPassID); }