LLVM  14.0.0git
RISCVTargetMachine.cpp
Go to the documentation of this file.
1 //===-- RISCVTargetMachine.cpp - Define TargetMachine for RISCV -----------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implements the info about RISCV target spec.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "RISCVTargetMachine.h"
15 #include "RISCV.h"
16 #include "RISCVTargetObjectFile.h"
19 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/CodeGen/Passes.h"
29 #include "llvm/InitializePasses.h"
33 using namespace llvm;
34 
38  auto *PR = PassRegistry::getPassRegistry();
44 }
45 
46 static StringRef computeDataLayout(const Triple &TT) {
47  if (TT.isArch64Bit())
48  return "e-m:e-p:64:64-i64:64-i128:128-n64-S128";
49  assert(TT.isArch32Bit() && "only RV32 and RV64 are currently supported");
50  return "e-m:e-p:32:32-i64:64-n32-S128";
51 }
52 
55  if (!RM.hasValue())
56  return Reloc::Static;
57  return *RM;
58 }
59 
61  StringRef CPU, StringRef FS,
62  const TargetOptions &Options,
65  CodeGenOpt::Level OL, bool JIT)
68  getEffectiveCodeModel(CM, CodeModel::Small), OL),
69  TLOF(std::make_unique<RISCVELFTargetObjectFile>()) {
70  initAsmInfo();
71 
72  // RISC-V supports the MachineOutliner.
73  setMachineOutliner(true);
74 }
75 
76 const RISCVSubtarget *
78  Attribute CPUAttr = F.getFnAttribute("target-cpu");
79  Attribute TuneAttr = F.getFnAttribute("tune-cpu");
80  Attribute FSAttr = F.getFnAttribute("target-features");
81 
82  std::string CPU =
83  CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
84  std::string TuneCPU =
85  TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
86  std::string FS =
87  FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
88  std::string Key = CPU + TuneCPU + FS;
89  auto &I = SubtargetMap[Key];
90  if (!I) {
91  // This needs to be done before we create a new subtarget since any
92  // creation will depend on the TM and the code generation flags on the
93  // function that reside in TargetOptions.
95  auto ABIName = Options.MCOptions.getABIName();
96  if (const MDString *ModuleTargetABI = dyn_cast_or_null<MDString>(
97  F.getParent()->getModuleFlag("target-abi"))) {
98  auto TargetABI = RISCVABI::getTargetABI(ABIName);
99  if (TargetABI != RISCVABI::ABI_Unknown &&
100  ModuleTargetABI->getString() != ABIName) {
101  report_fatal_error("-target-abi option != target-abi module flag");
102  }
103  ABIName = ModuleTargetABI->getString();
104  }
105  I = std::make_unique<RISCVSubtarget>(TargetTriple, CPU, TuneCPU, FS, ABIName, *this);
106  }
107  return I.get();
108 }
109 
112  return TargetTransformInfo(RISCVTTIImpl(this, F));
113 }
114 
115 // A RISC-V hart has a single byte-addressable address space of 2^XLEN bytes
116 // for all memory accesses, so it is reasonable to assume that an
117 // implementation has no-op address space casts. If an implementation makes a
118 // change to this, they can override it here.
120  unsigned DstAS) const {
121  return true;
122 }
123 
124 namespace {
125 class RISCVPassConfig : public TargetPassConfig {
126 public:
127  RISCVPassConfig(RISCVTargetMachine &TM, PassManagerBase &PM)
128  : TargetPassConfig(TM, PM) {}
129 
130  RISCVTargetMachine &getRISCVTargetMachine() const {
131  return getTM<RISCVTargetMachine>();
132  }
133 
134  void addIRPasses() override;
135  bool addInstSelector() override;
136  bool addIRTranslator() override;
137  bool addLegalizeMachineIR() override;
138  bool addRegBankSelect() override;
139  bool addGlobalInstructionSelect() override;
140  void addPreEmitPass() override;
141  void addPreEmitPass2() override;
142  void addPreSched2() override;
143  void addPreRegAlloc() override;
144 };
145 } // namespace
146 
148  return new RISCVPassConfig(*this, PM);
149 }
150 
151 void RISCVPassConfig::addIRPasses() {
152  addPass(createAtomicExpandPass());
153 
155 
157 }
158 
159 bool RISCVPassConfig::addInstSelector() {
160  addPass(createRISCVISelDag(getRISCVTargetMachine()));
161 
162  return false;
163 }
164 
165 bool RISCVPassConfig::addIRTranslator() {
166  addPass(new IRTranslator(getOptLevel()));
167  return false;
168 }
169 
170 bool RISCVPassConfig::addLegalizeMachineIR() {
171  addPass(new Legalizer());
172  return false;
173 }
174 
175 bool RISCVPassConfig::addRegBankSelect() {
176  addPass(new RegBankSelect());
177  return false;
178 }
179 
180 bool RISCVPassConfig::addGlobalInstructionSelect() {
181  addPass(new InstructionSelect(getOptLevel()));
182  return false;
183 }
184 
185 void RISCVPassConfig::addPreSched2() {}
186 
187 void RISCVPassConfig::addPreEmitPass() { addPass(&BranchRelaxationPassID); }
188 
189 void RISCVPassConfig::addPreEmitPass2() {
190  addPass(createRISCVExpandPseudoPass());
191  // Schedule the expansion of AMOs at the last possible moment, avoiding the
192  // possibility for other passes to break the requirements for forward
193  // progress in the LR/SC block.
195 }
196 
197 void RISCVPassConfig::addPreRegAlloc() {
198  if (TM->getOptLevel() != CodeGenOpt::None)
200  addPass(createRISCVInsertVSETVLIPass());
201 }
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::Attribute::isValid
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition: Attributes.h:167
llvm::RISCVTTIImpl
Definition: RISCVTargetTransformInfo.h:28
llvm::getTheRISCV64Target
Target & getTheRISCV64Target()
Definition: RISCVTargetInfo.cpp:18
llvm::createRISCVInsertVSETVLIPass
FunctionPass * createRISCVInsertVSETVLIPass()
llvm::RISCVABI::getTargetABI
ABI getTargetABI(StringRef ABIName)
Definition: RISCVBaseInfo.cpp:65
llvm::TargetOptions
Definition: TargetOptions.h:124
llvm::RISCVTargetMachine::createPassConfig
TargetPassConfig * createPassConfig(PassManagerBase &PM) override
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
Definition: RISCVTargetMachine.cpp:147
llvm::Function
Definition: Function.h:61
llvm::Attribute
Definition: Attributes.h:52
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:137
llvm::MCTargetOptions::getABIName
StringRef getABIName() const
getABIName - If this returns a non-empty string this represents the textual name of the ABI that we w...
Definition: MCTargetOptions.cpp:22
llvm::TargetTransformInfo
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Definition: TargetTransformInfo.h:168
llvm::createRISCVMergeBaseOffsetOptPass
FunctionPass * createRISCVMergeBaseOffsetOptPass()
Returns an instance of the Merge Base Offset Optimization pass.
Definition: RISCVMergeBaseOffset.cpp:291
llvm::RISCVTargetMachine
Definition: RISCVTargetMachine.h:23
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
llvm::createRISCVExpandAtomicPseudoPass
FunctionPass * createRISCVExpandAtomicPseudoPass()
llvm::RISCVTargetMachine::getTargetTransformInfo
TargetTransformInfo getTargetTransformInfo(const Function &F) override
Get a TargetTransformInfo implementation for the target.
Definition: RISCVTargetMachine.cpp:111
InstructionSelect.h
llvm::Optional< Reloc::Model >
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::initializeGlobalISel
void initializeGlobalISel(PassRegistry &)
Initialize all passes linked into the GlobalISel library.
Definition: GlobalISel.cpp:18
llvm::RISCVTargetMachine::getSubtargetImpl
const RISCVSubtarget * getSubtargetImpl() const =delete
llvm::initializeRISCVMergeBaseOffsetOptPass
void initializeRISCVMergeBaseOffsetOptPass(PassRegistry &)
STLExtras.h
llvm::initializeRISCVExpandPseudoPass
void initializeRISCVExpandPseudoPass(PassRegistry &)
llvm::RISCVTargetMachine::RISCVTargetMachine
RISCVTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM, CodeGenOpt::Level OL, bool JIT)
Definition: RISCVTargetMachine.cpp:60
LegacyPassManager.h
llvm::RISCVELFTargetObjectFile
This implementation is used for RISCV ELF targets.
Definition: RISCVTargetObjectFile.h:17
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::Reloc::Model
Model
Definition: CodeGen.h:22
computeDataLayout
static StringRef computeDataLayout(const Triple &TT)
Definition: RISCVTargetMachine.cpp:46
FormattedStream.h
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::TargetMachine::setMachineOutliner
void setMachineOutliner(bool Enable)
Definition: TargetMachine.h:252
llvm::Legalizer
Definition: Legalizer.h:31
Y
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
RISCVTargetObjectFile.h
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:481
llvm::TargetOptions::MCOptions
MCTargetOptions MCOptions
Machine level options.
Definition: TargetOptions.h:414
llvm::CodeModel::Small
@ Small
Definition: CodeGen.h:28
llvm::createAtomicExpandPass
FunctionPass * createAtomicExpandPass()
llvm::InstructionSelect
This pass is responsible for selecting generic machine instructions to target-specific instructions.
Definition: InstructionSelect.h:31
llvm::RegisterTargetMachine
RegisterTargetMachine - Helper template for registering a target machine implementation,...
Definition: TargetRegistry.h:1275
LLVMInitializeRISCVTarget
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTarget()
Definition: RISCVTargetMachine.cpp:35
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
Options
const char LLVMTargetMachineRef LLVMPassBuilderOptionsRef Options
Definition: PassBuilderBindings.cpp:48
llvm::TargetMachine::TargetFS
std::string TargetFS
Definition: TargetMachine.h:100
llvm::StringRef::str
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:245
llvm::Attribute::getValueAsString
StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:301
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
llvm::TargetMachine::resetTargetOptions
void resetTargetOptions(const Function &F) const
Reset the target options based on the function's attributes.
Definition: TargetMachine.cpp:56
Passes.h
llvm::TargetPassConfig
Target-Independent Code Generator Pass Configuration Options.
Definition: TargetPassConfig.h:84
llvm::TargetMachine::TargetTriple
Triple TargetTriple
Triple string, CPU name, and target feature strings the TargetMachine instance is created with.
Definition: TargetMachine.h:98
llvm::EngineKind::JIT
@ JIT
Definition: ExecutionEngine.h:524
LLVM_EXTERNAL_VISIBILITY
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:132
llvm::initializeRISCVGatherScatterLoweringPass
void initializeRISCVGatherScatterLoweringPass(PassRegistry &)
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::TargetPassConfig::addIRPasses
virtual void addIRPasses()
Add common target configurable passes that perform LLVM IR to IR transforms following machine indepen...
Definition: TargetPassConfig.cpp:850
TargetPassConfig.h
llvm::RISCVSubtarget
Definition: RISCVSubtarget.h:35
llvm::TargetMachine::Options
TargetOptions Options
Definition: TargetMachine.h:120
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::createRISCVExpandPseudoPass
FunctionPass * createRISCVExpandPseudoPass()
llvm::X86AS::FS
@ FS
Definition: X86.h:188
RISCV.h
llvm::CodeGenOpt::None
@ None
Definition: CodeGen.h:53
TargetOptions.h
llvm::AArch64::RM
@ RM
Definition: AArch64ISelLowering.h:472
llvm::createRISCVGatherScatterLoweringPass
FunctionPass * createRISCVGatherScatterLoweringPass()
llvm::createRISCVISelDag
FunctionPass * createRISCVISelDag(RISCVTargetMachine &TM)
Definition: RISCVISelDAGToDAG.cpp:1889
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::Reloc::Static
@ Static
Definition: CodeGen.h:22
IRTranslator.h
llvm::CodeGenOpt::Level
Level
Definition: CodeGen.h:52
llvm::getEffectiveRelocModel
static Reloc::Model getEffectiveRelocModel(Optional< Reloc::Model > RM)
Definition: AVRTargetMachine.cpp:40
llvm::getEffectiveCodeModel
CodeModel::Model getEffectiveCodeModel(Optional< CodeModel::Model > CM, CodeModel::Model Default)
Helper method for getting the code model, returning Default if CM does not have a value.
Definition: TargetMachine.h:481
llvm::LLVMTargetMachine::initAsmInfo
void initAsmInfo()
Definition: LLVMTargetMachine.cpp:41
std
Definition: BitVector.h:838
RegBankSelect.h
llvm::LLVMTargetMachine
This class describes a target machine that is implemented with the LLVM target-independent code gener...
Definition: TargetMachine.h:393
RISCVBaseInfo.h
llvm::BranchRelaxationPassID
char & BranchRelaxationPassID
BranchRelaxation - This pass replaces branches that need to jump further than is supported by a branc...
Definition: BranchRelaxation.cpp:119
Legalizer.h
llvm::initializeRISCVInsertVSETVLIPass
void initializeRISCVInsertVSETVLIPass(PassRegistry &)
llvm::RISCVABI::ABI_Unknown
@ ABI_Unknown
Definition: RISCVBaseInfo.h:298
TargetTransformInfo.h
llvm::RISCVTargetMachine::isNoopAddrSpaceCast
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DstAS) const override
Returns true if a cast between SrcAS and DestAS is a noop.
Definition: RISCVTargetMachine.cpp:119
llvm::legacy::PassManagerBase
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
Definition: LegacyPassManager.h:39
llvm::IRTranslator
Definition: IRTranslator.h:62
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
RISCVTargetInfo.h
llvm::RegBankSelect
This pass implements the reg bank selector pass used in the GlobalISel pipeline.
Definition: RegBankSelect.h:91
llvm::MDString
A single uniqued string.
Definition: Metadata.h:611
RISCVTargetTransformInfo.h
TargetRegistry.h
InitializePasses.h
llvm::getTheRISCV32Target
Target & getTheRISCV32Target()
Definition: RISCVTargetInfo.cpp:13
llvm::TargetMachine::TargetCPU
std::string TargetCPU
Definition: TargetMachine.h:99
TargetLoweringObjectFileImpl.h
RISCVTargetMachine.h