1 //===-- RISCVTargetMachine.cpp - Define TargetMachine for RISCV -----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Implements the info about RISCV target spec.
11 //===----------------------------------------------------------------------===//
13 #include "RISCVTargetMachine.h"
15 #include "RISCVTargetObjectFile.h"
16 #include "RISCVTargetTransformInfo.h"
17 #include "TargetInfo/RISCVTargetInfo.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/Analysis/TargetTransformInfo.h"
20 #include "llvm/CodeGen/GlobalISel/IRTranslator.h"
21 #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
22 #include "llvm/CodeGen/GlobalISel/Legalizer.h"
23 #include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
24 #include "llvm/CodeGen/Passes.h"
25 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
26 #include "llvm/CodeGen/TargetPassConfig.h"
27 #include "llvm/IR/LegacyPassManager.h"
28 #include "llvm/Support/FormattedStream.h"
29 #include "llvm/Support/TargetRegistry.h"
30 #include "llvm/Target/TargetOptions.h"
33 extern "C" void LLVMInitializeRISCVTarget() {
34 RegisterTargetMachine
<RISCVTargetMachine
> X(getTheRISCV32Target());
35 RegisterTargetMachine
<RISCVTargetMachine
> Y(getTheRISCV64Target());
36 auto PR
= PassRegistry::getPassRegistry();
37 initializeGlobalISel(*PR
);
38 initializeRISCVExpandPseudoPass(*PR
);
41 static StringRef
computeDataLayout(const Triple
&TT
) {
42 if (TT
.isArch64Bit()) {
43 return "e-m:e-p:64:64-i64:64-i128:128-n64-S128";
45 assert(TT
.isArch32Bit() && "only RV32 and RV64 are currently supported");
46 return "e-m:e-p:32:32-i64:64-n32-S128";
50 static Reloc::Model
getEffectiveRelocModel(const Triple
&TT
,
51 Optional
<Reloc::Model
> RM
) {
57 RISCVTargetMachine::RISCVTargetMachine(const Target
&T
, const Triple
&TT
,
58 StringRef CPU
, StringRef FS
,
59 const TargetOptions
&Options
,
60 Optional
<Reloc::Model
> RM
,
61 Optional
<CodeModel::Model
> CM
,
62 CodeGenOpt::Level OL
, bool JIT
)
63 : LLVMTargetMachine(T
, computeDataLayout(TT
), TT
, CPU
, FS
, Options
,
64 getEffectiveRelocModel(TT
, RM
),
65 getEffectiveCodeModel(CM
, CodeModel::Small
), OL
),
66 TLOF(std::make_unique
<RISCVELFTargetObjectFile
>()),
67 Subtarget(TT
, CPU
, FS
, Options
.MCOptions
.getABIName(), *this) {
72 RISCVTargetMachine::getTargetTransformInfo(const Function
&F
) {
73 return TargetTransformInfo(RISCVTTIImpl(this, F
));
77 class RISCVPassConfig
: public TargetPassConfig
{
79 RISCVPassConfig(RISCVTargetMachine
&TM
, PassManagerBase
&PM
)
80 : TargetPassConfig(TM
, PM
) {}
82 RISCVTargetMachine
&getRISCVTargetMachine() const {
83 return getTM
<RISCVTargetMachine
>();
86 void addIRPasses() override
;
87 bool addInstSelector() override
;
88 bool addIRTranslator() override
;
89 bool addLegalizeMachineIR() override
;
90 bool addRegBankSelect() override
;
91 bool addGlobalInstructionSelect() override
;
92 void addPreEmitPass() override
;
93 void addPreEmitPass2() override
;
94 void addPreRegAlloc() override
;
98 TargetPassConfig
*RISCVTargetMachine::createPassConfig(PassManagerBase
&PM
) {
99 return new RISCVPassConfig(*this, PM
);
102 void RISCVPassConfig::addIRPasses() {
103 addPass(createAtomicExpandPass());
104 TargetPassConfig::addIRPasses();
107 bool RISCVPassConfig::addInstSelector() {
108 addPass(createRISCVISelDag(getRISCVTargetMachine()));
113 bool RISCVPassConfig::addIRTranslator() {
114 addPass(new IRTranslator());
118 bool RISCVPassConfig::addLegalizeMachineIR() {
119 addPass(new Legalizer());
123 bool RISCVPassConfig::addRegBankSelect() {
124 addPass(new RegBankSelect());
128 bool RISCVPassConfig::addGlobalInstructionSelect() {
129 addPass(new InstructionSelect());
133 void RISCVPassConfig::addPreEmitPass() { addPass(&BranchRelaxationPassID
); }
135 void RISCVPassConfig::addPreEmitPass2() {
136 // Schedule the expansion of AMOs at the last possible moment, avoiding the
137 // possibility for other passes to break the requirements for forward
138 // progress in the LR/SC block.
139 addPass(createRISCVExpandPseudoPass());
142 void RISCVPassConfig::addPreRegAlloc() {
143 addPass(createRISCVMergeBaseOffsetOptPass());