LLVM 20.0.0git
CSKYTargetMachine.cpp
Go to the documentation of this file.
1//===--- CSKYTargetMachine.cpp - Define TargetMachine for CSKY ------------===//
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 CSKY target spec.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CSKYTargetMachine.h"
14#include "CSKY.h"
16#include "CSKYSubtarget.h"
24#include <optional>
25
26using namespace llvm;
27
30
34}
35
36static std::string computeDataLayout(const Triple &TT) {
37 std::string Ret;
38
39 // Only support little endian for now.
40 // TODO: Add support for big endian.
41 Ret += "e";
42
43 // CSKY is always 32-bit target with the CSKYv2 ABI as prefer now.
44 // It's a 4-byte aligned stack with ELF mangling only.
45 Ret += "-m:e-S32-p:32:32-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:32:32"
46 "-v128:32:32-a:0:32-Fi32-n32";
47
48 return Ret;
49}
50
52 StringRef CPU, StringRef FS,
54 std::optional<Reloc::Model> RM,
55 std::optional<CodeModel::Model> CM,
56 CodeGenOptLevel OL, bool JIT)
58 RM.value_or(Reloc::Static),
59 getEffectiveCodeModel(CM, CodeModel::Small), OL),
60 TLOF(std::make_unique<CSKYELFTargetObjectFile>()) {
62}
63
64const CSKYSubtarget *
66 Attribute CPUAttr = F.getFnAttribute("target-cpu");
67 Attribute TuneAttr = F.getFnAttribute("tune-cpu");
68 Attribute FSAttr = F.getFnAttribute("target-features");
69
70 std::string CPU =
71 CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
72 std::string TuneCPU =
73 TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
74 std::string FS =
75 FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
76
77 std::string Key = CPU + TuneCPU + FS;
78 auto &I = SubtargetMap[Key];
79 if (!I) {
80 // This needs to be done before we create a new subtarget since any
81 // creation will depend on the TM and the code generation flags on the
82 // function that reside in TargetOptions.
84 I = std::make_unique<CSKYSubtarget>(TargetTriple, CPU, TuneCPU, FS, *this);
85 if (I->useHardFloat() && !I->hasAnyFloatExt())
86 errs() << "Hard-float can't be used with current CPU,"
87 " set to Soft-float\n";
88 }
89 return I.get();
90}
91
93 BumpPtrAllocator &Allocator, const Function &F,
94 const TargetSubtargetInfo *STI) const {
95 return CSKYMachineFunctionInfo::create<CSKYMachineFunctionInfo>(Allocator, F,
96 STI);
97}
98
99namespace {
100class CSKYPassConfig : public TargetPassConfig {
101public:
102 CSKYPassConfig(CSKYTargetMachine &TM, PassManagerBase &PM)
103 : TargetPassConfig(TM, PM) {}
104
105 CSKYTargetMachine &getCSKYTargetMachine() const {
106 return getTM<CSKYTargetMachine>();
107 }
108
109 void addIRPasses() override;
110 bool addInstSelector() override;
111 void addPreEmitPass() override;
112};
113
114} // namespace
115
117 return new CSKYPassConfig(*this, PM);
118}
119
120void CSKYPassConfig::addIRPasses() {
123}
124
125bool CSKYPassConfig::addInstSelector() {
126 addPass(createCSKYISelDag(getCSKYTargetMachine(), getOptLevel()));
127
128 return false;
129}
130
131void CSKYPassConfig::addPreEmitPass() {
133}
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeCSKYTarget()
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:128
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static LVOptions Options
Definition: LVOptions.cpp:25
static std::string computeDataLayout()
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
Basic Register Allocator
Target-Independent Code Generator Pass Configuration Options pass.
StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:392
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition: Attributes.h:208
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
const CSKYSubtarget * getSubtargetImpl() const =delete
CSKYTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, std::optional< Reloc::Model > RM, std::optional< CodeModel::Model > CM, CodeGenOptLevel OL, bool JIT)
TargetPassConfig * createPassConfig(PassManagerBase &PM) override
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
MachineFunctionInfo * createMachineFunctionInfo(BumpPtrAllocator &Allocator, const Function &F, const TargetSubtargetInfo *STI) const override
Create the target's instance of MachineFunctionInfo.
implements a set of functionality in the TargetMachine class for targets that make use of the indepen...
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:37
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:229
Triple TargetTriple
Triple string, CPU name, and target feature strings the TargetMachine instance is created with.
Definition: TargetMachine.h:96
std::string TargetFS
Definition: TargetMachine.h:98
std::string TargetCPU
Definition: TargetMachine.h:97
std::unique_ptr< const MCSubtargetInfo > STI
void resetTargetOptions(const Function &F) const
Reset the target options based on the function's attributes.
Target-Independent Code Generator Pass Configuration Options.
virtual void addIRPasses()
Add common target configurable passes that perform LLVM IR to IR transforms following machine indepen...
TargetSubtargetInfo - Generic base class for all target subtargets.
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeCSKYDAGToDAGISelLegacyPass(PassRegistry &)
void initializeCSKYConstantIslandsPass(PassRegistry &)
CodeModel::Model getEffectiveCodeModel(std::optional< CodeModel::Model > CM, CodeModel::Model Default)
Helper method for getting the code model, returning Default if CM does not have a value.
CodeGenOptLevel
Code generation optimization level.
Definition: CodeGen.h:54
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Target & getTheCSKYTarget()
FunctionPass * createCSKYConstantIslandPass()
Returns a pass that converts branches to long branches.
FunctionPass * createAtomicExpandLegacyPass()
AtomicExpandPass - At IR level this pass replace atomic instructions with __atomic_* library calls,...
FunctionPass * createCSKYISelDag(CSKYTargetMachine &TM, CodeGenOptLevel OptLevel)
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
MachineFunctionInfo - This class can be derived from and used by targets to hold private target-speci...
RegisterTargetMachine - Helper template for registering a target machine implementation,...