LLVM 22.0.0git
SparcTargetMachine.cpp
Go to the documentation of this file.
1//===-- SparcTargetMachine.cpp - Define TargetMachine for Sparc -----------===//
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//
10//===----------------------------------------------------------------------===//
11
12#include "SparcTargetMachine.h"
13#include "LeonPasses.h"
14#include "Sparc.h"
18#include "llvm/CodeGen/Passes.h"
22#include <optional>
23using namespace llvm;
24
26 // Register the target.
30
35}
36
37static cl::opt<bool>
38 BranchRelaxation("sparc-enable-branch-relax", cl::Hidden, cl::init(true),
39 cl::desc("Relax out of range conditional branches"));
40
41static std::string computeDataLayout(const Triple &T, bool is64Bit) {
42 // Sparc is typically big endian, but some are little.
43 std::string Ret = T.getArch() == Triple::sparcel ? "e" : "E";
44 Ret += "-m:e";
45
46 // Some ABIs have 32bit pointers.
47 if (!is64Bit)
48 Ret += "-p:32:32";
49
50 // Alignments for 64 bit integers.
51 Ret += "-i64:64";
52
53 // Alignments for 128 bit integers.
54 // This is not specified in the ABI document but is the de facto standard.
55 Ret += "-i128:128";
56
57 // On SparcV9 128 floats are aligned to 128 bits, on others only to 64.
58 // On SparcV9 registers can hold 64 or 32 bits, on others only 32.
59 if (is64Bit)
60 Ret += "-n32:64";
61 else
62 Ret += "-f128:64-n32";
63
64 if (is64Bit)
65 Ret += "-S128";
66 else
67 Ret += "-S64";
68
69 return Ret;
70}
71
72static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
73 return RM.value_or(Reloc::Static);
74}
75
76// Code models. Some only make sense for 64-bit code.
77//
78// SunCC Reloc CodeModel Constraints
79// abs32 Static Small text+data+bss linked below 2^32 bytes
80// abs44 Static Medium text+data+bss linked below 2^44 bytes
81// abs64 Static Large text smaller than 2^31 bytes
82// pic13 PIC_ Small GOT < 2^13 bytes
83// pic32 PIC_ Medium GOT < 2^32 bytes
84//
85// All code models require that the text segment is smaller than 2GB.
87getEffectiveSparcCodeModel(std::optional<CodeModel::Model> CM, Reloc::Model RM,
88 bool Is64Bit, bool JIT) {
89 if (CM) {
90 if (*CM == CodeModel::Tiny)
91 report_fatal_error("Target does not support the tiny CodeModel", false);
92 if (*CM == CodeModel::Kernel)
93 report_fatal_error("Target does not support the kernel CodeModel", false);
94 return *CM;
95 }
96 if (Is64Bit) {
97 if (JIT)
98 return CodeModel::Large;
100 }
101 return CodeModel::Small;
102}
103
104/// Create an ILP32 architecture model
106 StringRef CPU, StringRef FS,
107 const TargetOptions &Options,
108 std::optional<Reloc::Model> RM,
109 std::optional<CodeModel::Model> CM,
110 CodeGenOptLevel OL, bool JIT,
111 bool is64bit)
113 T, computeDataLayout(TT, is64bit), TT, CPU, FS, Options,
116 JIT),
117 OL),
118 TLOF(std::make_unique<SparcELFTargetObjectFile>()), is64Bit(is64bit) {
119 initAsmInfo();
120}
121
123
124const SparcSubtarget *
126 Attribute CPUAttr = F.getFnAttribute("target-cpu");
127 Attribute TuneAttr = F.getFnAttribute("tune-cpu");
128 Attribute FSAttr = F.getFnAttribute("target-features");
129
130 std::string CPU =
131 CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
132 std::string TuneCPU =
133 TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
134 std::string FS =
135 FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
136
137 // FIXME: This is related to the code below to reset the target options,
138 // we need to know whether or not the soft float flag is set on the
139 // function, so we can enable it as a subtarget feature.
140 bool softFloat = F.getFnAttribute("use-soft-float").getValueAsBool();
141
142 if (softFloat)
143 FS += FS.empty() ? "+soft-float" : ",+soft-float";
144
145 auto &I = SubtargetMap[CPU + FS];
146 if (!I) {
147 // This needs to be done before we create a new subtarget since any
148 // creation will depend on the TM and the code generation flags on the
149 // function that reside in TargetOptions.
151 I = std::make_unique<SparcSubtarget>(CPU, TuneCPU, FS, *this,
152 this->is64Bit);
153 }
154 return I.get();
155}
156
158 BumpPtrAllocator &Allocator, const Function &F,
159 const TargetSubtargetInfo *STI) const {
160 return SparcMachineFunctionInfo::create<SparcMachineFunctionInfo>(Allocator,
161 F, STI);
162}
163
164namespace {
165/// Sparc Code Generator Pass Configuration Options.
166class SparcPassConfig : public TargetPassConfig {
167public:
168 SparcPassConfig(SparcTargetMachine &TM, PassManagerBase &PM)
169 : TargetPassConfig(TM, PM) {}
170
171 SparcTargetMachine &getSparcTargetMachine() const {
172 return getTM<SparcTargetMachine>();
173 }
174
175 void addIRPasses() override;
176 bool addInstSelector() override;
177 void addPreEmitPass() override;
178};
179} // namespace
180
182 return new SparcPassConfig(*this, PM);
183}
184
185void SparcPassConfig::addIRPasses() {
187
189}
190
191bool SparcPassConfig::addInstSelector() {
192 addPass(createSparcISelDag(getSparcTargetMachine()));
193 return false;
194}
195
196void SparcPassConfig::addPreEmitPass(){
197 if (BranchRelaxation)
198 addPass(&BranchRelaxationPassID);
199
201 addPass(new InsertNOPLoad());
202 addPass(new DetectRoundChange());
203 addPass(new FixAllFDIVSQRT());
204 addPass(new ErrataWorkaround());
205}
206
207void SparcV8TargetMachine::anchor() { }
208
210 StringRef CPU, StringRef FS,
211 const TargetOptions &Options,
212 std::optional<Reloc::Model> RM,
213 std::optional<CodeModel::Model> CM,
214 CodeGenOptLevel OL, bool JIT)
215 : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {}
216
217void SparcV9TargetMachine::anchor() { }
218
220 StringRef CPU, StringRef FS,
221 const TargetOptions &Options,
222 std::optional<Reloc::Model> RM,
223 std::optional<CodeModel::Model> CM,
224 CodeGenOptLevel OL, bool JIT)
225 : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {}
226
227void SparcelTargetMachine::anchor() {}
228
230 StringRef CPU, StringRef FS,
231 const TargetOptions &Options,
232 std::optional<Reloc::Model> RM,
233 std::optional<CodeModel::Model> CM,
234 CodeGenOptLevel OL, bool JIT)
235 : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {}
#define LLVM_ABI
Definition: Compiler.h:213
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:132
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
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
Basic Register Allocator
static Reloc::Model getEffectiveRelocModel(std::optional< Reloc::Model > RM)
static cl::opt< bool > BranchRelaxation("sparc-enable-branch-relax", cl::Hidden, cl::init(true), cl::desc("Relax out of range conditional branches"))
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSparcTarget()
static CodeModel::Model getEffectiveSparcCodeModel(std::optional< CodeModel::Model > CM, Reloc::Model RM, bool Is64Bit, bool JIT)
Target-Independent Code Generator Pass Configuration Options pass.
static bool is64Bit(const char *name)
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:400
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition: Attributes.h:223
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:67
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:38
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
TargetPassConfig * createPassConfig(PassManagerBase &PM) override
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
SparcTargetMachine(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, bool is64bit)
Create an ILP32 architecture model.
MachineFunctionInfo * createMachineFunctionInfo(BumpPtrAllocator &Allocator, const Function &F, const TargetSubtargetInfo *STI) const override
Create the target's instance of MachineFunctionInfo.
const SparcSubtarget * getSubtargetImpl(const Function &F) const override
Virtual method implemented by subclasses that returns a reference to that target's TargetSubtargetInf...
~SparcTargetMachine() override
SparcV8TargetMachine(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)
SparcV9TargetMachine(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)
SparcelTargetMachine(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)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:233
std::string TargetFS
std::string TargetCPU
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:47
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:444
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeSparcAsmPrinterPass(PassRegistry &)
Target & getTheSparcTarget()
void initializeSparcDAGToDAGISelLegacyPass(PassRegistry &)
LLVM_ABI char & BranchRelaxationPassID
BranchRelaxation - This pass replaces branches that need to jump further than is supported by a branc...
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition: Error.cpp:167
void initializeErrataWorkaroundPass(PassRegistry &)
CodeGenOptLevel
Code generation optimization level.
Definition: CodeGen.h:82
Target & getTheSparcV9Target()
FunctionPass * createSparcISelDag(SparcTargetMachine &TM)
createSparcISelDag - This pass converts a legalized DAG into a SPARC-specific DAG,...
Target & getTheSparcelTarget()
LLVM_ABI FunctionPass * createAtomicExpandLegacyPass()
AtomicExpandPass - At IR level this pass replace atomic instructions with __atomic_* library calls,...
FunctionPass * createSparcDelaySlotFillerPass()
createSparcDelaySlotFillerPass - Returns a pass that fills in delay slots in Sparc MachineFunctions
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
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,...