LLVM 24.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"
29#include <memory>
30#include <optional>
31#include <string>
32
33using namespace llvm;
34
35// NOLINTNEXTLINE(readability-identifier-naming)
53
54static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
55 if (TT.isOSzOS())
56 return std::make_unique<TargetLoweringObjectFileGOFF>();
57
58 // Note: Some times run with -triple s390x-unknown.
59 // In this case, default to ELF unless z/OS specifically provided.
60 return std::make_unique<SystemZELFTargetObjectFile>();
61}
62
64 std::optional<Reloc::Model> RM) {
65 if (TT.isOSzOS()) {
66 // z/OS code is always position-independent, so -fpic and -fno-pic have
67 // no effect. Ignore the relocation model requested by the frontend.
68 // This also prevents optimizations (such as relative lookup tables)
69 // that generate 32-bit relative offsets, which are
70 // incompatible with the GOFF binder's requirement for 8-byte relocations
71 // in C_WSA64.
72 //
73 // Constant globals whose initializers contain pointer relocations (e.g.
74 // vtables) must be placed in a writable section (C_WSA64) so the GOFF
75 // binder can apply relocations at link time. Using DynamicNoPIC causes
76 // getKindForGlobal() to classify such globals as ReadOnlyWithRel instead
77 // of ReadOnly, which routes them to C_WSA64 rather than C_CODE64.
79 }
80 // For ELF/Linux, static code is suitable for use in a dynamic executable;
81 // there is no separate DynamicNoPIC model.
82 if (!RM || *RM == Reloc::DynamicNoPIC)
83 return Reloc::Static;
84 return *RM;
85}
86
87// For SystemZ we define the models as follows:
88//
89// Small: BRASL can call any function and will use a stub if necessary.
90// Locally-binding symbols will always be in range of LARL.
91//
92// Medium: BRASL can call any function and will use a stub if necessary.
93// GOT slots and locally-defined text will always be in range
94// of LARL, but other symbols might not be.
95//
96// Large: Equivalent to Medium for now.
97//
98// Kernel: Equivalent to Medium for now.
99//
100// This means that any PIC module smaller than 4GB meets the
101// requirements of Small, so Small seems like the best default there.
102//
103// All symbols bind locally in a non-PIC module, so the choice is less
104// obvious. There are two cases:
105//
106// - When creating an executable, PLTs and copy relocations allow
107// us to treat external symbols as part of the executable.
108// Any executable smaller than 4GB meets the requirements of Small,
109// so that seems like the best default.
110//
111// - When creating JIT code, stubs will be in range of BRASL if the
112// image is less than 4GB in size. GOT entries will likewise be
113// in range of LARL. However, the JIT environment has no equivalent
114// of copy relocs, so locally-binding data symbols might not be in
115// the range of LARL. We need the Medium model in that case.
116static CodeModel::Model
117getEffectiveSystemZCodeModel(std::optional<CodeModel::Model> CM,
118 Reloc::Model RM, bool JIT) {
119 if (CM) {
120 if (*CM == CodeModel::Tiny)
121 report_fatal_error("Target does not support the tiny CodeModel", false);
122 if (*CM == CodeModel::Kernel)
123 report_fatal_error("Target does not support the kernel CodeModel", false);
124 return *CM;
125 }
126 if (JIT)
128 return CodeModel::Small;
129}
130
132 StringRef CPU, StringRef FS,
133 const TargetOptions &Options,
134 std::optional<Reloc::Model> RM,
135 std::optional<CodeModel::Model> CM,
136 CodeGenOptLevel OL, bool JIT)
138 T, TT, CPU, FS, Options, getEffectiveRelocModel(TT, RM),
140 OL),
141 CLOpts(cl::LibraryOptionsContext::get<SystemZOptions>(
142 Options.MCOptions.LibraryOptions)),
143 TLOF(createTLOF(getTargetTriple())) {
144 initAsmInfo();
145}
146
148
149const SystemZSubtarget *
151 AttributeSet FnAttrs = F.getAttributes().getFnAttrs();
152 const Module *M = F.getParent();
153 if (const SystemZSubtarget *ST = LastSubtarget.lookup(FnAttrs, M))
154 return ST;
155
156 SubtargetAttrs Attrs;
157 Attrs.CPU = FnAttrs.getAttribute("target-cpu");
158 Attrs.TuneCPU = FnAttrs.getAttribute("tune-cpu");
159 Attrs.FS = FnAttrs.getAttribute("target-features");
160 Attrs.SoftFloat = FnAttrs.getAttribute("use-soft-float");
161 Attrs.BackChain = FnAttrs.getAttribute("backchain");
162
163 if (const SystemZSubtarget *ST = LastSubtarget.lookup(FnAttrs, M, Attrs))
164 return ST;
165
166 std::string CPU =
167 Attrs.CPU.isValid() ? Attrs.CPU.getValueAsString().str() : TargetCPU;
168 std::string TuneCPU =
169 Attrs.TuneCPU.isValid() ? Attrs.TuneCPU.getValueAsString().str() : CPU;
170 std::string FS =
171 Attrs.FS.isValid() ? Attrs.FS.getValueAsString().str() : TargetFS;
172
173 // FIXME: This is related to the code below to reset the target options,
174 // we need to know whether the soft float and backchain flags are set on the
175 // function, so we can enable them as subtarget features.
176 bool SoftFloat = Attrs.SoftFloat.getValueAsBool();
177 if (SoftFloat)
178 FS += FS.empty() ? "+soft-float" : ",+soft-float";
179 bool BackChain = Attrs.BackChain.isValid();
180 if (BackChain)
181 FS += FS.empty() ? "+backchain" : ",+backchain";
182
183 auto &I = SubtargetMap[CPU + TuneCPU + FS];
184 if (!I) {
185 I = std::make_unique<SystemZSubtarget>(TargetTriple, CPU, TuneCPU, FS,
186 *this);
187 }
188
189 LastSubtarget.set(FnAttrs, M, Attrs, I.get());
190 return I.get();
191}
192
195 // Use GenericScheduler if requested on CL or for Z10 which has no sched
196 // model.
197 if (CLOpts.generic_sched ||
198 !C->MF->getSubtarget().getSchedModel().hasInstrSchedModel())
199 return nullptr;
200
202}
203
208
209namespace {
210
211/// SystemZ Code Generator Pass Configuration Options.
212class SystemZPassConfig : public TargetPassConfig {
213public:
214 SystemZPassConfig(SystemZTargetMachine &TM, PassManagerBase &PM)
215 : TargetPassConfig(TM, PM) {}
216
217 SystemZTargetMachine &getSystemZTargetMachine() const {
219 }
220
221 void addIRPasses() override;
222 bool addInstSelector() override;
223 bool addILPOpts() override;
224 void addPreRegAlloc() override;
225 void addPostRewrite() override;
226 void addPostRegAlloc() override;
227 void addPreSched2() override;
228 void addPreEmitPass() override;
229};
230
231} // end anonymous namespace
232
233void SystemZPassConfig::addIRPasses() {
234 if (getOptLevel() != CodeGenOptLevel::None) {
238 }
239
241
243}
244
245bool SystemZPassConfig::addInstSelector() {
246 addPass(
247 createSystemZISelDagLegacyPass(getSystemZTargetMachine(), getOptLevel()));
248
249 if (getOptLevel() != CodeGenOptLevel::None)
250 addPass(createSystemZLDCleanupLegacyPass(getSystemZTargetMachine()));
251
252 return false;
253}
254
255bool SystemZPassConfig::addILPOpts() {
256 addPass(&EarlyIfConverterLegacyID);
257
258 if (getSystemZTargetMachine().getCLOpts().machine_combiner)
259 addPass(&MachineCombinerID);
260
261 return true;
262}
263
264void SystemZPassConfig::addPreRegAlloc() {
265 addPass(createSystemZCopyPhysRegsLegacyPass(getSystemZTargetMachine()));
266}
267
268void SystemZPassConfig::addPostRewrite() {
269 addPass(createSystemZPostRewriteLegacyPass(getSystemZTargetMachine()));
270}
271
272void SystemZPassConfig::addPostRegAlloc() {
273 // PostRewrite needs to be run at -O0 also (in which case addPostRewrite()
274 // is not called).
275 if (getOptLevel() == CodeGenOptLevel::None)
276 addPass(createSystemZPostRewriteLegacyPass(getSystemZTargetMachine()));
277}
278
279void SystemZPassConfig::addPreSched2() {
280 if (getOptLevel() != CodeGenOptLevel::None)
281 addPass(&IfConverterID);
282}
283
284void SystemZPassConfig::addPreEmitPass() {
285 // Do instruction shortening before compare elimination because some
286 // vector instructions will be shortened into opcodes that compare
287 // elimination recognizes.
288 if (getOptLevel() != CodeGenOptLevel::None)
289 addPass(createSystemZShortenInstLegacyPass(getSystemZTargetMachine()));
290
291 // We eliminate comparisons here rather than earlier because some
292 // transformations can change the set of available CC values and we
293 // generally want those transformations to have priority. This is
294 // especially true in the commonest case where the result of the comparison
295 // is used by a single in-range branch instruction, since we will then
296 // be able to fuse the compare and the branch instead.
297 //
298 // For example, two-address NILF can sometimes be converted into
299 // three-address RISBLG. NILF produces a CC value that indicates whether
300 // the low word is zero, but RISBLG does not modify CC at all. On the
301 // other hand, 64-bit ANDs like NILL can sometimes be converted to RISBG.
302 // The CC value produced by NILL isn't useful for our purposes, but the
303 // value produced by RISBG can be used for any comparison with zero
304 // (not just equality). So there are some transformations that lose
305 // CC values (while still being worthwhile) and others that happen to make
306 // the CC result more useful than it was originally.
307 //
308 // Another reason is that we only want to use BRANCH ON COUNT in cases
309 // where we know that the count register is not going to be spilled.
310 //
311 // Doing it so late makes it more likely that a register will be reused
312 // between the comparison and the branch, but it isn't clear whether
313 // preventing that would be a win or not.
314 if (getOptLevel() != CodeGenOptLevel::None)
315 addPass(createSystemZElimCompareLegacyPass(getSystemZTargetMachine()));
316 addPass(createSystemZLongBranchLegacyPass(getSystemZTargetMachine()));
317
318 // Do final scheduling after all other optimizations, to get an
319 // optimal input for the decoder (branch relaxation must happen
320 // after block placement).
321 if (getOptLevel() != CodeGenOptLevel::None)
322 addPass(&PostMachineSchedulerID);
323}
324
326 return new SystemZPassConfig(*this, PM);
327}
328
331 return TargetTransformInfo(std::make_unique<SystemZTTIImpl>(this, F));
332}
333
static std::unique_ptr< TargetLoweringObjectFile > createTLOF(const Triple &TT)
static Reloc::Model getEffectiveRelocModel()
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_ABI
Definition Compiler.h:211
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define T
const GCNTargetMachine & getTM(const GCNSubtarget *STI)
static CodeModel::Model getEffectiveSystemZCodeModel(std::optional< CodeModel::Model > CM, Reloc::Model RM, bool JIT)
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSystemZTarget()
Target-Independent Code Generator Pass Configuration Options pass.
This pass exposes codegen information to IR-level passes.
static std::unique_ptr< TargetLoweringObjectFile > createTLOF()
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:448
LLVM_ABI Attribute getAttribute(Attribute::AttrKind Kind) const
Return the attribute object.
CodeGenTargetMachineImpl(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Reloc::Model RM, CodeModel::Model CM, CodeGenOptLevel OL)
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A ScheduleDAG for scheduling lists of MachineInstr.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
ScheduleDAGInstrs * createMachineScheduler(MachineSchedContext *C) const override
Create an instance of ScheduleDAGInstrs to be run within the standard MachineScheduler pass for this ...
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 ...
ScheduleDAGInstrs * createPostMachineScheduler(MachineSchedContext *C) const override
Similar to createMachineScheduler but used when postRA machine scheduling is enabled.
Triple TargetTriple
Triple string, CPU name, and target feature strings the TargetMachine instance is created with.
const Triple & getTargetTriple() const
std::unique_ptr< const MCSubtargetInfo > STI
TargetOptions Options
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:48
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
Registers T with cl::parseCommandLineOptions.
const AArch64MCOptions & getCLOpts(const MCTargetOptions &Options)
@ DynamicNoPIC
Definition CodeGen.h:26
This namespace contains all of the command line option processing machinery.
This is an optimization pass for GlobalISel generic memory operations.
ScheduleDAGMILive * createSchedLive(MachineSchedContext *C)
Create the standard converging machine scheduler.
Target & getTheSystemZTarget()
FunctionPass * createSystemZISelDagLegacyPass(SystemZTargetMachine &TM, CodeGenOptLevel OptLevel)
void initializeSystemZPostRewriteLegacyPass(PassRegistry &)
FunctionPass * createSystemZLDCleanupLegacyPass(SystemZTargetMachine &TM)
void initializeSystemZCopyPhysRegsLegacyPass(PassRegistry &)
LLVM_ABI char & PostMachineSchedulerID
PostMachineScheduler - This pass schedules machine instructions postRA.
LLVM_ABI char & EarlyIfConverterLegacyID
EarlyIfConverter - This pass performs if-conversion on SSA form by inserting cmov instructions.
LLVM_ABI char & MachineCombinerID
This pass performs instruction combining using trace metrics to estimate critical-path and resource d...
static Reloc::Model getEffectiveRelocModel(std::optional< Reloc::Model > RM)
void initializeSystemZDAGToDAGISelLegacyPass(PassRegistry &)
void initializeSystemZTDCLegacyPass(PassRegistry &)
ScheduleDAGMI * createSchedPostRA(MachineSchedContext *C)
Create a generic scheduler with no vreg liveness or DAG mutation passes.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
void initializeSystemZAlignGlobalsLegacyPass(PassRegistry &)
LLVM_ABI FunctionPass * createLoopDataPrefetchPass()
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
FunctionPass * createSystemZTDCLegacyPass()
FunctionPass * createSystemZCopyPhysRegsLegacyPass(SystemZTargetMachine &TM)
void initializeSystemZLDCleanupLegacyPass(PassRegistry &)
void initializeSystemZElimCompareLegacyPass(PassRegistry &)
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:280
void initializeSystemZAsmPrinterPass(PassRegistry &)
FunctionPass * createSystemZShortenInstLegacyPass(SystemZTargetMachine &TM)
ModulePass * createSystemZAlignGlobalsLegacyPass()
LLVM_ABI char & IfConverterID
IfConverter - This pass performs machine code if conversion.
LLVM_ABI FunctionPass * createAtomicExpandLegacyPass()
AtomicExpandPass - At IR level this pass replace atomic instructions with __atomic_* library calls,...
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:391
FunctionPass * createSystemZPostRewriteLegacyPass(SystemZTargetMachine &TM)
void initializeSystemZLongBranchLegacyPass(PassRegistry &)
FunctionPass * createSystemZElimCompareLegacyPass(SystemZTargetMachine &TM)
FunctionPass * createSystemZLongBranchLegacyPass(SystemZTargetMachine &TM)
void initializeSystemZShortenInstLegacyPass(PassRegistry &)
MachineFunctionInfo - This class can be derived from and used by targets to hold private target-speci...
static FuncInfoTy * create(BumpPtrAllocator &Allocator, const Function &F, const SubtargetTy *STI)
Factory function: default behavior is to call new using the supplied allocator.
MachineSchedContext provides enough context from the MachineScheduler pass for the target to instanti...
RegisterTargetMachine - Helper template for registering a target machine implementation,...