LLVM 20.0.0git
SystemZTargetMachine.cpp
Go to the documentation of this file.
1//===-- SystemZTargetMachine.cpp - Define TargetMachine for SystemZ -------===//
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
11#include "SystemZ.h"
17#include "llvm/ADT/StringRef.h"
19#include "llvm/CodeGen/Passes.h"
22#include "llvm/IR/DataLayout.h"
27#include <memory>
28#include <optional>
29#include <string>
30
31using namespace llvm;
32
34 "systemz-machine-combiner",
35 cl::desc("Enable the machine combiner pass"),
36 cl::init(true), cl::Hidden);
37
38// NOLINTNEXTLINE(readability-identifier-naming)
40 // Register the target.
51}
52
53static std::string computeDataLayout(const Triple &TT) {
54 std::string Ret;
55
56 // Big endian.
57 Ret += "E";
58
59 // Data mangling.
61
62 // Special features for z/OS.
63 if (TT.isOSzOS()) {
64 if (TT.isArch64Bit()) {
65 // Custom address space for ptr32.
66 Ret += "-p1:32:32";
67 }
68 }
69
70 // Make sure that global data has at least 16 bits of alignment by
71 // default, so that we can refer to it using LARL. We don't have any
72 // special requirements for stack variables though.
73 Ret += "-i1:8:16-i8:8:16";
74
75 // 64-bit integers are naturally aligned.
76 Ret += "-i64:64";
77
78 // 128-bit floats are aligned only to 64 bits.
79 Ret += "-f128:64";
80
81 // The DataLayout string always holds a vector alignment of 64 bits, see
82 // comment in clang/lib/Basic/Targets/SystemZ.h.
83 Ret += "-v128:64";
84
85 // We prefer 16 bits of aligned for all globals; see above.
86 Ret += "-a:8:16";
87
88 // Integer registers are 32 or 64 bits.
89 Ret += "-n32:64";
90
91 return Ret;
92}
93
94static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
95 if (TT.isOSzOS())
96 return std::make_unique<TargetLoweringObjectFileGOFF>();
97
98 // Note: Some times run with -triple s390x-unknown.
99 // In this case, default to ELF unless z/OS specifically provided.
100 return std::make_unique<SystemZELFTargetObjectFile>();
101}
102
103static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
104 // Static code is suitable for use in a dynamic executable; there is no
105 // separate DynamicNoPIC model.
106 if (!RM || *RM == Reloc::DynamicNoPIC)
107 return Reloc::Static;
108 return *RM;
109}
110
111// For SystemZ we define the models as follows:
112//
113// Small: BRASL can call any function and will use a stub if necessary.
114// Locally-binding symbols will always be in range of LARL.
115//
116// Medium: BRASL can call any function and will use a stub if necessary.
117// GOT slots and locally-defined text will always be in range
118// of LARL, but other symbols might not be.
119//
120// Large: Equivalent to Medium for now.
121//
122// Kernel: Equivalent to Medium for now.
123//
124// This means that any PIC module smaller than 4GB meets the
125// requirements of Small, so Small seems like the best default there.
126//
127// All symbols bind locally in a non-PIC module, so the choice is less
128// obvious. There are two cases:
129//
130// - When creating an executable, PLTs and copy relocations allow
131// us to treat external symbols as part of the executable.
132// Any executable smaller than 4GB meets the requirements of Small,
133// so that seems like the best default.
134//
135// - When creating JIT code, stubs will be in range of BRASL if the
136// image is less than 4GB in size. GOT entries will likewise be
137// in range of LARL. However, the JIT environment has no equivalent
138// of copy relocs, so locally-binding data symbols might not be in
139// the range of LARL. We need the Medium model in that case.
140static CodeModel::Model
141getEffectiveSystemZCodeModel(std::optional<CodeModel::Model> CM,
142 Reloc::Model RM, bool JIT) {
143 if (CM) {
144 if (*CM == CodeModel::Tiny)
145 report_fatal_error("Target does not support the tiny CodeModel", false);
146 if (*CM == CodeModel::Kernel)
147 report_fatal_error("Target does not support the kernel CodeModel", false);
148 return *CM;
149 }
150 if (JIT)
152 return CodeModel::Small;
153}
154
156 StringRef CPU, StringRef FS,
157 const TargetOptions &Options,
158 std::optional<Reloc::Model> RM,
159 std::optional<CodeModel::Model> CM,
160 CodeGenOptLevel OL, bool JIT)
162 T, computeDataLayout(TT), TT, CPU, FS, Options,
165 OL),
166 TLOF(createTLOF(getTargetTriple())) {
167 initAsmInfo();
168}
169
171
172const SystemZSubtarget *
174 Attribute CPUAttr = F.getFnAttribute("target-cpu");
175 Attribute TuneAttr = F.getFnAttribute("tune-cpu");
176 Attribute FSAttr = F.getFnAttribute("target-features");
177
178 std::string CPU =
179 CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
180 std::string TuneCPU =
181 TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
182 std::string FS =
183 FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
184
185 // FIXME: This is related to the code below to reset the target options,
186 // we need to know whether the soft float and backchain flags are set on the
187 // function, so we can enable them as subtarget features.
188 bool SoftFloat = F.getFnAttribute("use-soft-float").getValueAsBool();
189 if (SoftFloat)
190 FS += FS.empty() ? "+soft-float" : ",+soft-float";
191 bool BackChain = F.hasFnAttribute("backchain");
192 if (BackChain)
193 FS += FS.empty() ? "+backchain" : ",+backchain";
194
195 auto &I = SubtargetMap[CPU + TuneCPU + FS];
196 if (!I) {
197 // This needs to be done before we create a new subtarget since any
198 // creation will depend on the TM and the code generation flags on the
199 // function that reside in TargetOptions.
201 I = std::make_unique<SystemZSubtarget>(TargetTriple, CPU, TuneCPU, FS,
202 *this);
203 }
204
205 return I.get();
206}
207
208namespace {
209
210/// SystemZ Code Generator Pass Configuration Options.
211class SystemZPassConfig : public TargetPassConfig {
212public:
213 SystemZPassConfig(SystemZTargetMachine &TM, PassManagerBase &PM)
214 : TargetPassConfig(TM, PM) {}
215
216 SystemZTargetMachine &getSystemZTargetMachine() const {
217 return getTM<SystemZTargetMachine>();
218 }
219
221 createPostMachineScheduler(MachineSchedContext *C) const override {
222 return new ScheduleDAGMI(C,
223 std::make_unique<SystemZPostRASchedStrategy>(C),
224 /*RemoveKillFlags=*/true);
225 }
226
227 void addIRPasses() override;
228 bool addInstSelector() override;
229 bool addILPOpts() override;
230 void addPreRegAlloc() override;
231 void addPostRewrite() override;
232 void addPostRegAlloc() override;
233 void addPreSched2() override;
234 void addPreEmitPass() override;
235};
236
237} // end anonymous namespace
238
239void SystemZPassConfig::addIRPasses() {
240 if (getOptLevel() != CodeGenOptLevel::None) {
241 addPass(createSystemZTDCPass());
243 }
244
246
248}
249
250bool SystemZPassConfig::addInstSelector() {
251 addPass(createSystemZISelDag(getSystemZTargetMachine(), getOptLevel()));
252
253 if (getOptLevel() != CodeGenOptLevel::None)
254 addPass(createSystemZLDCleanupPass(getSystemZTargetMachine()));
255
256 return false;
257}
258
259bool SystemZPassConfig::addILPOpts() {
260 addPass(&EarlyIfConverterID);
261
263 addPass(&MachineCombinerID);
264
265 return true;
266}
267
268void SystemZPassConfig::addPreRegAlloc() {
269 addPass(createSystemZCopyPhysRegsPass(getSystemZTargetMachine()));
270}
271
272void SystemZPassConfig::addPostRewrite() {
273 addPass(createSystemZPostRewritePass(getSystemZTargetMachine()));
274}
275
276void SystemZPassConfig::addPostRegAlloc() {
277 // PostRewrite needs to be run at -O0 also (in which case addPostRewrite()
278 // is not called).
279 if (getOptLevel() == CodeGenOptLevel::None)
280 addPass(createSystemZPostRewritePass(getSystemZTargetMachine()));
281}
282
283void SystemZPassConfig::addPreSched2() {
284 if (getOptLevel() != CodeGenOptLevel::None)
285 addPass(&IfConverterID);
286}
287
288void SystemZPassConfig::addPreEmitPass() {
289 // Do instruction shortening before compare elimination because some
290 // vector instructions will be shortened into opcodes that compare
291 // elimination recognizes.
292 if (getOptLevel() != CodeGenOptLevel::None)
293 addPass(createSystemZShortenInstPass(getSystemZTargetMachine()));
294
295 // We eliminate comparisons here rather than earlier because some
296 // transformations can change the set of available CC values and we
297 // generally want those transformations to have priority. This is
298 // especially true in the commonest case where the result of the comparison
299 // is used by a single in-range branch instruction, since we will then
300 // be able to fuse the compare and the branch instead.
301 //
302 // For example, two-address NILF can sometimes be converted into
303 // three-address RISBLG. NILF produces a CC value that indicates whether
304 // the low word is zero, but RISBLG does not modify CC at all. On the
305 // other hand, 64-bit ANDs like NILL can sometimes be converted to RISBG.
306 // The CC value produced by NILL isn't useful for our purposes, but the
307 // value produced by RISBG can be used for any comparison with zero
308 // (not just equality). So there are some transformations that lose
309 // CC values (while still being worthwhile) and others that happen to make
310 // the CC result more useful than it was originally.
311 //
312 // Another reason is that we only want to use BRANCH ON COUNT in cases
313 // where we know that the count register is not going to be spilled.
314 //
315 // Doing it so late makes it more likely that a register will be reused
316 // between the comparison and the branch, but it isn't clear whether
317 // preventing that would be a win or not.
318 if (getOptLevel() != CodeGenOptLevel::None)
319 addPass(createSystemZElimComparePass(getSystemZTargetMachine()));
320 addPass(createSystemZLongBranchPass(getSystemZTargetMachine()));
321
322 // Do final scheduling after all other optimizations, to get an
323 // optimal input for the decoder (branch relaxation must happen
324 // after block placement).
325 if (getOptLevel() != CodeGenOptLevel::None)
326 addPass(&PostMachineSchedulerID);
327}
328
330 return new SystemZPassConfig(*this, PM);
331}
332
335 return TargetTransformInfo(SystemZTTIImpl(this, F));
336}
337
339 BumpPtrAllocator &Allocator, const Function &F,
340 const TargetSubtargetInfo *STI) const {
341 return SystemZMachineFunctionInfo::create<SystemZMachineFunctionInfo>(
342 Allocator, F, STI);
343}
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:135
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 cl::opt< bool > EnableMachineCombinerPass("ppc-machine-combiner", cl::desc("Enable the machine combiner pass"), cl::init(true), cl::Hidden)
const char LLVMTargetMachineRef TM
Basic Register Allocator
static CodeModel::Model getEffectiveSystemZCodeModel(std::optional< CodeModel::Model > CM, Reloc::Model RM, bool JIT)
static cl::opt< bool > EnableMachineCombinerPass("systemz-machine-combiner", cl::desc("Enable the machine combiner pass"), cl::init(true), cl::Hidden)
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSystemZTarget()
static Reloc::Model getEffectiveRelocModel(std::optional< Reloc::Model > RM)
Target-Independent Code Generator Pass Configuration Options pass.
This pass exposes codegen information to IR-level passes.
static std::unique_ptr< TargetLoweringObjectFile > createTLOF()
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:203
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
static const char * getManglingComponent(const Triple &T)
Definition: DataLayout.cpp:175
This class describes a target machine that is implemented with the LLVM target-independent code gener...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A ScheduleDAG for scheduling lists of MachineInstr.
ScheduleDAGMI is an implementation of ScheduleDAGInstrs that simply schedules machine instructions ac...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:215
const SystemZSubtarget * getSubtargetImpl() const =delete
SystemZTargetMachine(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)
TargetTransformInfo getTargetTransformInfo(const Function &F) const override
Get a TargetTransformInfo implementation for the target.
MachineFunctionInfo * createMachineFunctionInfo(BumpPtrAllocator &Allocator, const Function &F, const TargetSubtargetInfo *STI) const override
Create the target's instance of MachineFunctionInfo.
TargetPassConfig * createPassConfig(PassManagerBase &PM) override
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
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.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
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 ...
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ DynamicNoPIC
Definition: CodeGen.h:25
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Target & getTheSystemZTarget()
void initializeSystemZElimComparePass(PassRegistry &)
FunctionPass * createSystemZLongBranchPass(SystemZTargetMachine &TM)
FunctionPass * createSystemZISelDag(SystemZTargetMachine &TM, CodeGenOptLevel OptLevel)
FunctionPass * createSystemZCopyPhysRegsPass(SystemZTargetMachine &TM)
FunctionPass * createSystemZElimComparePass(SystemZTargetMachine &TM)
char & PostMachineSchedulerID
PostMachineScheduler - This pass schedules machine instructions postRA.
void initializeSystemZLongBranchPass(PassRegistry &)
void initializeSystemZShortenInstPass(PassRegistry &)
char & MachineCombinerID
This pass performs instruction combining using trace metrics to estimate critical-path and resource d...
void initializeSystemZDAGToDAGISelLegacyPass(PassRegistry &)
FunctionPass * createSystemZTDCPass()
FunctionPass * createLoopDataPrefetchPass()
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
FunctionPass * createSystemZShortenInstPass(SystemZTargetMachine &TM)
void initializeSystemZPostRewritePass(PassRegistry &)
CodeGenOptLevel
Code generation optimization level.
Definition: CodeGen.h:54
void initializeSystemZTDCPassPass(PassRegistry &)
FunctionPass * createSystemZLDCleanupPass(SystemZTargetMachine &TM)
char & EarlyIfConverterID
EarlyIfConverter - This pass performs if-conversion on SSA form by inserting cmov instructions.
FunctionPass * createSystemZPostRewritePass(SystemZTargetMachine &TM)
char & IfConverterID
IfConverter - This pass performs machine code if conversion.
FunctionPass * createAtomicExpandLegacyPass()
AtomicExpandPass - At IR level this pass replace atomic instructions with __atomic_* library calls,...
void initializeSystemZLDCleanupPass(PassRegistry &)
MachineFunctionInfo - This class can be derived from and used by targets to hold private target-speci...
MachineSchedContext provides enough context from the MachineScheduler pass for the target to instanti...
RegisterTargetMachine - Helper template for registering a target machine implementation,...