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"
28#include <memory>
29#include <optional>
30#include <string>
31
32using namespace llvm;
33
35 "systemz-machine-combiner",
36 cl::desc("Enable the machine combiner pass"),
37 cl::init(true), cl::Hidden);
38
40 "generic-sched", cl::Hidden, cl::init(false),
41 cl::desc("Run the generic pre-ra scheduler instead of the SystemZ "
42 "scheduler."));
43
44// NOLINTNEXTLINE(readability-identifier-naming)
61
62static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
63 if (TT.isOSzOS())
64 return std::make_unique<TargetLoweringObjectFileGOFF>();
65
66 // Note: Some times run with -triple s390x-unknown.
67 // In this case, default to ELF unless z/OS specifically provided.
68 return std::make_unique<SystemZELFTargetObjectFile>();
69}
70
72 std::optional<Reloc::Model> RM) {
73 if (TT.isOSzOS()) {
74 // On z/OS, constant globals whose initializers contain pointer relocations
75 // (e.g. vtables) must be placed in a writable section (C_WSA64) so the
76 // GOFF binder can apply them at link time. Using DynamicNoPIC causes
77 // getKindForGlobal() to classify such globals as ReadOnlyWithRel instead
78 // of ReadOnly, which routes them to C_WSA64 rather than C_CODE64.
79 if (!RM || *RM == Reloc::DynamicNoPIC)
81 return *RM;
82 }
83 // For ELF/Linux, static code is suitable for use in a dynamic executable;
84 // there is no separate DynamicNoPIC model.
85 if (!RM || *RM == Reloc::DynamicNoPIC)
86 return Reloc::Static;
87 return *RM;
88}
89
90// For SystemZ we define the models as follows:
91//
92// Small: BRASL can call any function and will use a stub if necessary.
93// Locally-binding symbols will always be in range of LARL.
94//
95// Medium: BRASL can call any function and will use a stub if necessary.
96// GOT slots and locally-defined text will always be in range
97// of LARL, but other symbols might not be.
98//
99// Large: Equivalent to Medium for now.
100//
101// Kernel: Equivalent to Medium for now.
102//
103// This means that any PIC module smaller than 4GB meets the
104// requirements of Small, so Small seems like the best default there.
105//
106// All symbols bind locally in a non-PIC module, so the choice is less
107// obvious. There are two cases:
108//
109// - When creating an executable, PLTs and copy relocations allow
110// us to treat external symbols as part of the executable.
111// Any executable smaller than 4GB meets the requirements of Small,
112// so that seems like the best default.
113//
114// - When creating JIT code, stubs will be in range of BRASL if the
115// image is less than 4GB in size. GOT entries will likewise be
116// in range of LARL. However, the JIT environment has no equivalent
117// of copy relocs, so locally-binding data symbols might not be in
118// the range of LARL. We need the Medium model in that case.
119static CodeModel::Model
120getEffectiveSystemZCodeModel(std::optional<CodeModel::Model> CM,
121 Reloc::Model RM, bool JIT) {
122 if (CM) {
123 if (*CM == CodeModel::Tiny)
124 report_fatal_error("Target does not support the tiny CodeModel", false);
125 if (*CM == CodeModel::Kernel)
126 report_fatal_error("Target does not support the kernel CodeModel", false);
127 return *CM;
128 }
129 if (JIT)
131 return CodeModel::Small;
132}
133
135 StringRef CPU, StringRef FS,
136 const TargetOptions &Options,
137 std::optional<Reloc::Model> RM,
138 std::optional<CodeModel::Model> CM,
139 CodeGenOptLevel OL, bool JIT)
141 T, TT, CPU, FS, Options, getEffectiveRelocModel(TT, RM),
143 OL),
144 TLOF(createTLOF(getTargetTriple())) {
145 initAsmInfo();
146}
147
149
150const SystemZSubtarget *
152 Attribute CPUAttr = F.getFnAttribute("target-cpu");
153 Attribute TuneAttr = F.getFnAttribute("tune-cpu");
154 Attribute FSAttr = F.getFnAttribute("target-features");
155
156 std::string CPU =
157 CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
158 std::string TuneCPU =
159 TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
160 std::string FS =
161 FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
162
163 // FIXME: This is related to the code below to reset the target options,
164 // we need to know whether the soft float and backchain flags are set on the
165 // function, so we can enable them as subtarget features.
166 bool SoftFloat = F.getFnAttribute("use-soft-float").getValueAsBool();
167 if (SoftFloat)
168 FS += FS.empty() ? "+soft-float" : ",+soft-float";
169 bool BackChain = F.hasFnAttribute("backchain");
170 if (BackChain)
171 FS += FS.empty() ? "+backchain" : ",+backchain";
172
173 auto &I = SubtargetMap[CPU + TuneCPU + FS];
174 if (!I) {
175 I = std::make_unique<SystemZSubtarget>(TargetTriple, CPU, TuneCPU, FS,
176 *this);
177 }
178
179 return I.get();
180}
181
184 // Use GenericScheduler if requested on CL or for Z10 which has no sched
185 // model.
186 if (GenericSched ||
187 !C->MF->getSubtarget().getSchedModel().hasInstrSchedModel())
188 return nullptr;
189
191}
192
197
198namespace {
199
200/// SystemZ Code Generator Pass Configuration Options.
201class SystemZPassConfig : public TargetPassConfig {
202public:
203 SystemZPassConfig(SystemZTargetMachine &TM, PassManagerBase &PM)
204 : TargetPassConfig(TM, PM) {}
205
206 SystemZTargetMachine &getSystemZTargetMachine() const {
208 }
209
210 void addIRPasses() override;
211 bool addInstSelector() override;
212 bool addILPOpts() override;
213 void addPreRegAlloc() override;
214 void addPostRewrite() override;
215 void addPostRegAlloc() override;
216 void addPreSched2() override;
217 void addPreEmitPass() override;
218};
219
220} // end anonymous namespace
221
222void SystemZPassConfig::addIRPasses() {
223 if (getOptLevel() != CodeGenOptLevel::None) {
225 addPass(createSystemZTDCPass());
227 }
228
230
232}
233
234bool SystemZPassConfig::addInstSelector() {
235 addPass(createSystemZISelDag(getSystemZTargetMachine(), getOptLevel()));
236
237 if (getOptLevel() != CodeGenOptLevel::None)
238 addPass(createSystemZLDCleanupPass(getSystemZTargetMachine()));
239
240 return false;
241}
242
243bool SystemZPassConfig::addILPOpts() {
244 addPass(&EarlyIfConverterLegacyID);
245
247 addPass(&MachineCombinerID);
248
249 return true;
250}
251
252void SystemZPassConfig::addPreRegAlloc() {
253 addPass(createSystemZCopyPhysRegsPass(getSystemZTargetMachine()));
254}
255
256void SystemZPassConfig::addPostRewrite() {
257 addPass(createSystemZPostRewritePass(getSystemZTargetMachine()));
258}
259
260void SystemZPassConfig::addPostRegAlloc() {
261 // PostRewrite needs to be run at -O0 also (in which case addPostRewrite()
262 // is not called).
263 if (getOptLevel() == CodeGenOptLevel::None)
264 addPass(createSystemZPostRewritePass(getSystemZTargetMachine()));
265}
266
267void SystemZPassConfig::addPreSched2() {
268 if (getOptLevel() != CodeGenOptLevel::None)
269 addPass(&IfConverterID);
270}
271
272void SystemZPassConfig::addPreEmitPass() {
273 // Do instruction shortening before compare elimination because some
274 // vector instructions will be shortened into opcodes that compare
275 // elimination recognizes.
276 if (getOptLevel() != CodeGenOptLevel::None)
277 addPass(createSystemZShortenInstPass(getSystemZTargetMachine()));
278
279 // We eliminate comparisons here rather than earlier because some
280 // transformations can change the set of available CC values and we
281 // generally want those transformations to have priority. This is
282 // especially true in the commonest case where the result of the comparison
283 // is used by a single in-range branch instruction, since we will then
284 // be able to fuse the compare and the branch instead.
285 //
286 // For example, two-address NILF can sometimes be converted into
287 // three-address RISBLG. NILF produces a CC value that indicates whether
288 // the low word is zero, but RISBLG does not modify CC at all. On the
289 // other hand, 64-bit ANDs like NILL can sometimes be converted to RISBG.
290 // The CC value produced by NILL isn't useful for our purposes, but the
291 // value produced by RISBG can be used for any comparison with zero
292 // (not just equality). So there are some transformations that lose
293 // CC values (while still being worthwhile) and others that happen to make
294 // the CC result more useful than it was originally.
295 //
296 // Another reason is that we only want to use BRANCH ON COUNT in cases
297 // where we know that the count register is not going to be spilled.
298 //
299 // Doing it so late makes it more likely that a register will be reused
300 // between the comparison and the branch, but it isn't clear whether
301 // preventing that would be a win or not.
302 if (getOptLevel() != CodeGenOptLevel::None)
303 addPass(createSystemZElimComparePass(getSystemZTargetMachine()));
304 addPass(createSystemZLongBranchPass(getSystemZTargetMachine()));
305
306 // Do final scheduling after all other optimizations, to get an
307 // optimal input for the decoder (branch relaxation must happen
308 // after block placement).
309 if (getOptLevel() != CodeGenOptLevel::None)
310 addPass(&PostMachineSchedulerID);
311}
312
314 return new SystemZPassConfig(*this, PM);
315}
316
319 return TargetTransformInfo(std::make_unique<SystemZTTIImpl>(this, F));
320}
321
static std::unique_ptr< TargetLoweringObjectFile > createTLOF(const Triple &TT)
static Reloc::Model getEffectiveRelocModel()
#define X(NUM, ENUM, NAME)
Definition ELF.h:857
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_ABI
Definition Compiler.h:215
#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
static cl::opt< bool > EnableMachineCombinerPass("ppc-machine-combiner", cl::desc("Enable the machine combiner pass"), cl::init(true), cl::Hidden)
const GCNTargetMachine & getTM(const GCNSubtarget *STI)
static CodeModel::Model getEffectiveSystemZCodeModel(std::optional< CodeModel::Model > CM, Reloc::Model RM, bool JIT)
static cl::opt< bool > GenericSched("generic-sched", cl::Hidden, cl::init(false), cl::desc("Run the generic pre-ra scheduler instead of the SystemZ " "scheduler."))
static cl::opt< bool > EnableMachineCombinerPass("systemz-machine-combiner", cl::desc("Enable the machine combiner pass"), cl::init(true), cl::Hidden)
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()
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:106
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:266
CodeGenTargetMachineImpl(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Reloc::Model RM, CodeModel::Model CM, CodeGenOptLevel OL)
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
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
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 ...
@ DynamicNoPIC
Definition CodeGen.h:26
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
ScheduleDAGMILive * createSchedLive(MachineSchedContext *C)
Create the standard converging machine scheduler.
Target & getTheSystemZTarget()
void initializeSystemZElimComparePass(PassRegistry &)
FunctionPass * createSystemZLongBranchPass(SystemZTargetMachine &TM)
FunctionPass * createSystemZISelDag(SystemZTargetMachine &TM, CodeGenOptLevel OptLevel)
ModulePass * createSystemZAlignGlobalsPass()
FunctionPass * createSystemZCopyPhysRegsPass(SystemZTargetMachine &TM)
FunctionPass * createSystemZElimComparePass(SystemZTargetMachine &TM)
void initializeSystemZCopyPhysRegsPass(PassRegistry &)
LLVM_ABI char & PostMachineSchedulerID
PostMachineScheduler - This pass schedules machine instructions postRA.
void initializeSystemZLongBranchPass(PassRegistry &)
void initializeSystemZShortenInstPass(PassRegistry &)
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 &)
ScheduleDAGMI * createSchedPostRA(MachineSchedContext *C)
Create a generic scheduler with no vreg liveness or DAG mutation passes.
FunctionPass * createSystemZTDCPass()
LLVM_ABI FunctionPass * createLoopDataPrefetchPass()
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
FunctionPass * createSystemZShortenInstPass(SystemZTargetMachine &TM)
void initializeSystemZPostRewritePass(PassRegistry &)
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:227
void initializeSystemZTDCPassPass(PassRegistry &)
FunctionPass * createSystemZLDCleanupPass(SystemZTargetMachine &TM)
void initializeSystemZAsmPrinterPass(PassRegistry &)
FunctionPass * createSystemZPostRewritePass(SystemZTargetMachine &TM)
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:390
void initializeSystemZLDCleanupPass(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,...