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
9#include "SystemZTargetMachine.h"
10#include "MCTargetDesc/SystemZMCTargetDesc.h"
11#include "SystemZ.h"
12#include "SystemZMachineFunctionInfo.h"
13#include "SystemZMachineScheduler.h"
14#include "SystemZTargetObjectFile.h"
15#include "SystemZTargetTransformInfo.h"
16#include "TargetInfo/SystemZTargetInfo.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/Analysis/TargetTransformInfo.h"
19#include "llvm/CodeGen/Passes.h"
20#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
21#include "llvm/CodeGen/TargetPassConfig.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/MC/TargetRegistry.h"
24#include "llvm/Support/CodeGen.h"
25#include "llvm/Support/Compiler.h"
26#include "llvm/Target/TargetLoweringObjectFile.h"
27#include "llvm/Transforms/Scalar.h"
28#include <memory>
29#include <optional>
30#include <string>
31
32using namespace llvm;
33
34cl::opt<bool>
35 EnableMachineCombinerPass("systemz-machine-combiner",
36 cl::desc("Enable the machine combiner pass"),
37 cl::init(Val: true), cl::Hidden);
38
39static cl::opt<bool> GenericSched(
40 "generic-sched", cl::Hidden, cl::init(Val: false),
41 cl::desc("Run the generic pre-ra scheduler instead of the SystemZ "
42 "scheduler."));
43
44// NOLINTNEXTLINE(readability-identifier-naming)
45extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
46LLVMInitializeSystemZTarget() {
47 // Register the target.
48 RegisterTargetMachine<SystemZTargetMachine> X(getTheSystemZTarget());
49 auto &PR = *PassRegistry::getPassRegistry();
50 initializeSystemZAlignGlobalsLegacyPass(PR);
51 initializeSystemZAsmPrinterPass(PR);
52 initializeSystemZElimCompareLegacyPass(PR);
53 initializeSystemZShortenInstLegacyPass(PR);
54 initializeSystemZLongBranchLegacyPass(PR);
55 initializeSystemZLDCleanupLegacyPass(PR);
56 initializeSystemZPostRewriteLegacyPass(PR);
57 initializeSystemZTDCLegacyPass(PR);
58 initializeSystemZDAGToDAGISelLegacyPass(PR);
59 initializeSystemZCopyPhysRegsLegacyPass(PR);
60}
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
71static Reloc::Model getEffectiveRelocModel(const Triple &TT,
72 std::optional<Reloc::Model> RM) {
73 if (TT.isOSzOS()) {
74 // z/OS code is always position-independent, so -fpic and -fno-pic have
75 // no effect. Ignore the relocation model requested by the frontend.
76 // This also prevents optimizations (such as relative lookup tables)
77 // that generate 32-bit relative offsets, which are
78 // incompatible with the GOFF binder's requirement for 8-byte relocations
79 // in C_WSA64.
80 //
81 // Constant globals whose initializers contain pointer relocations (e.g.
82 // vtables) must be placed in a writable section (C_WSA64) so the GOFF
83 // binder can apply relocations at link time. Using DynamicNoPIC causes
84 // getKindForGlobal() to classify such globals as ReadOnlyWithRel instead
85 // of ReadOnly, which routes them to C_WSA64 rather than C_CODE64.
86 return Reloc::DynamicNoPIC;
87 }
88 // For ELF/Linux, static code is suitable for use in a dynamic executable;
89 // there is no separate DynamicNoPIC model.
90 if (!RM || *RM == Reloc::DynamicNoPIC)
91 return Reloc::Static;
92 return *RM;
93}
94
95// For SystemZ we define the models as follows:
96//
97// Small: BRASL can call any function and will use a stub if necessary.
98// Locally-binding symbols will always be in range of LARL.
99//
100// Medium: BRASL can call any function and will use a stub if necessary.
101// GOT slots and locally-defined text will always be in range
102// of LARL, but other symbols might not be.
103//
104// Large: Equivalent to Medium for now.
105//
106// Kernel: Equivalent to Medium for now.
107//
108// This means that any PIC module smaller than 4GB meets the
109// requirements of Small, so Small seems like the best default there.
110//
111// All symbols bind locally in a non-PIC module, so the choice is less
112// obvious. There are two cases:
113//
114// - When creating an executable, PLTs and copy relocations allow
115// us to treat external symbols as part of the executable.
116// Any executable smaller than 4GB meets the requirements of Small,
117// so that seems like the best default.
118//
119// - When creating JIT code, stubs will be in range of BRASL if the
120// image is less than 4GB in size. GOT entries will likewise be
121// in range of LARL. However, the JIT environment has no equivalent
122// of copy relocs, so locally-binding data symbols might not be in
123// the range of LARL. We need the Medium model in that case.
124static CodeModel::Model
125getEffectiveSystemZCodeModel(std::optional<CodeModel::Model> CM,
126 Reloc::Model RM, bool JIT) {
127 if (CM) {
128 if (*CM == CodeModel::Tiny)
129 report_fatal_error(reason: "Target does not support the tiny CodeModel", gen_crash_diag: false);
130 if (*CM == CodeModel::Kernel)
131 report_fatal_error(reason: "Target does not support the kernel CodeModel", gen_crash_diag: false);
132 return *CM;
133 }
134 if (JIT)
135 return RM == Reloc::PIC_ ? CodeModel::Small : CodeModel::Medium;
136 return CodeModel::Small;
137}
138
139SystemZTargetMachine::SystemZTargetMachine(const Target &T, const Triple &TT,
140 StringRef CPU, StringRef FS,
141 const TargetOptions &Options,
142 std::optional<Reloc::Model> RM,
143 std::optional<CodeModel::Model> CM,
144 CodeGenOptLevel OL, bool JIT)
145 : CodeGenTargetMachineImpl(
146 T, TT, CPU, FS, Options, getEffectiveRelocModel(TT, RM),
147 getEffectiveSystemZCodeModel(CM, RM: getEffectiveRelocModel(TT, RM), JIT),
148 OL),
149 TLOF(createTLOF(TT: getTargetTriple())) {
150 initAsmInfo();
151}
152
153SystemZTargetMachine::~SystemZTargetMachine() = default;
154
155const SystemZSubtarget *
156SystemZTargetMachine::getSubtargetImpl(const Function &F) const {
157 AttributeSet FnAttrs = F.getAttributes().getFnAttrs();
158 const Module *M = F.getParent();
159 if (const SystemZSubtarget *ST = LastSubtarget.lookup(NewFnAttrs: FnAttrs, NewM: M))
160 return ST;
161
162 SubtargetAttrs Attrs;
163 Attrs.CPU = FnAttrs.getAttribute(Kind: "target-cpu");
164 Attrs.TuneCPU = FnAttrs.getAttribute(Kind: "tune-cpu");
165 Attrs.FS = FnAttrs.getAttribute(Kind: "target-features");
166 Attrs.SoftFloat = FnAttrs.getAttribute(Kind: "use-soft-float");
167 Attrs.BackChain = FnAttrs.getAttribute(Kind: "backchain");
168
169 if (const SystemZSubtarget *ST = LastSubtarget.lookup(NewFnAttrs: FnAttrs, NewM: M, NewAttrs: Attrs))
170 return ST;
171
172 std::string CPU =
173 Attrs.CPU.isValid() ? Attrs.CPU.getValueAsString().str() : TargetCPU;
174 std::string TuneCPU =
175 Attrs.TuneCPU.isValid() ? Attrs.TuneCPU.getValueAsString().str() : CPU;
176 std::string FS =
177 Attrs.FS.isValid() ? Attrs.FS.getValueAsString().str() : TargetFS;
178
179 // FIXME: This is related to the code below to reset the target options,
180 // we need to know whether the soft float and backchain flags are set on the
181 // function, so we can enable them as subtarget features.
182 bool SoftFloat = Attrs.SoftFloat.getValueAsBool();
183 if (SoftFloat)
184 FS += FS.empty() ? "+soft-float" : ",+soft-float";
185 bool BackChain = Attrs.BackChain.isValid();
186 if (BackChain)
187 FS += FS.empty() ? "+backchain" : ",+backchain";
188
189 auto &I = SubtargetMap[CPU + TuneCPU + FS];
190 if (!I) {
191 I = std::make_unique<SystemZSubtarget>(args: TargetTriple, args&: CPU, args&: TuneCPU, args&: FS,
192 args: *this);
193 }
194
195 LastSubtarget.set(NewFnAttrs: FnAttrs, NewM: M, NewAttrs: Attrs, NewSubtarget: I.get());
196 return I.get();
197}
198
199ScheduleDAGInstrs *
200SystemZTargetMachine::createMachineScheduler(MachineSchedContext *C) const {
201 // Use GenericScheduler if requested on CL or for Z10 which has no sched
202 // model.
203 if (GenericSched ||
204 !C->MF->getSubtarget().getSchedModel().hasInstrSchedModel())
205 return nullptr;
206
207 return createSchedLive<SystemZPreRASchedStrategy>(C);
208}
209
210ScheduleDAGInstrs *
211SystemZTargetMachine::createPostMachineScheduler(MachineSchedContext *C) const {
212 return createSchedPostRA<SystemZPostRASchedStrategy>(C);
213}
214
215namespace {
216
217/// SystemZ Code Generator Pass Configuration Options.
218class SystemZPassConfig : public TargetPassConfig {
219public:
220 SystemZPassConfig(SystemZTargetMachine &TM, PassManagerBase &PM)
221 : TargetPassConfig(TM, PM) {}
222
223 SystemZTargetMachine &getSystemZTargetMachine() const {
224 return getTM<SystemZTargetMachine>();
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(P: createSystemZAlignGlobalsLegacyPass());
242 addPass(P: createSystemZTDCLegacyPass());
243 addPass(P: createLoopDataPrefetchPass());
244 }
245
246 addPass(P: createAtomicExpandLegacyPass());
247
248 TargetPassConfig::addIRPasses();
249}
250
251bool SystemZPassConfig::addInstSelector() {
252 addPass(
253 P: createSystemZISelDagLegacyPass(TM&: getSystemZTargetMachine(), OptLevel: getOptLevel()));
254
255 if (getOptLevel() != CodeGenOptLevel::None)
256 addPass(P: createSystemZLDCleanupLegacyPass(TM&: getSystemZTargetMachine()));
257
258 return false;
259}
260
261bool SystemZPassConfig::addILPOpts() {
262 addPass(PassID: &EarlyIfConverterLegacyID);
263
264 if (EnableMachineCombinerPass)
265 addPass(PassID: &MachineCombinerID);
266
267 return true;
268}
269
270void SystemZPassConfig::addPreRegAlloc() {
271 addPass(P: createSystemZCopyPhysRegsLegacyPass(TM&: getSystemZTargetMachine()));
272}
273
274void SystemZPassConfig::addPostRewrite() {
275 addPass(P: createSystemZPostRewriteLegacyPass(TM&: getSystemZTargetMachine()));
276}
277
278void SystemZPassConfig::addPostRegAlloc() {
279 // PostRewrite needs to be run at -O0 also (in which case addPostRewrite()
280 // is not called).
281 if (getOptLevel() == CodeGenOptLevel::None)
282 addPass(P: createSystemZPostRewriteLegacyPass(TM&: getSystemZTargetMachine()));
283}
284
285void SystemZPassConfig::addPreSched2() {
286 if (getOptLevel() != CodeGenOptLevel::None)
287 addPass(PassID: &IfConverterID);
288}
289
290void SystemZPassConfig::addPreEmitPass() {
291 // Do instruction shortening before compare elimination because some
292 // vector instructions will be shortened into opcodes that compare
293 // elimination recognizes.
294 if (getOptLevel() != CodeGenOptLevel::None)
295 addPass(P: createSystemZShortenInstLegacyPass(TM&: getSystemZTargetMachine()));
296
297 // We eliminate comparisons here rather than earlier because some
298 // transformations can change the set of available CC values and we
299 // generally want those transformations to have priority. This is
300 // especially true in the commonest case where the result of the comparison
301 // is used by a single in-range branch instruction, since we will then
302 // be able to fuse the compare and the branch instead.
303 //
304 // For example, two-address NILF can sometimes be converted into
305 // three-address RISBLG. NILF produces a CC value that indicates whether
306 // the low word is zero, but RISBLG does not modify CC at all. On the
307 // other hand, 64-bit ANDs like NILL can sometimes be converted to RISBG.
308 // The CC value produced by NILL isn't useful for our purposes, but the
309 // value produced by RISBG can be used for any comparison with zero
310 // (not just equality). So there are some transformations that lose
311 // CC values (while still being worthwhile) and others that happen to make
312 // the CC result more useful than it was originally.
313 //
314 // Another reason is that we only want to use BRANCH ON COUNT in cases
315 // where we know that the count register is not going to be spilled.
316 //
317 // Doing it so late makes it more likely that a register will be reused
318 // between the comparison and the branch, but it isn't clear whether
319 // preventing that would be a win or not.
320 if (getOptLevel() != CodeGenOptLevel::None)
321 addPass(P: createSystemZElimCompareLegacyPass(TM&: getSystemZTargetMachine()));
322 addPass(P: createSystemZLongBranchLegacyPass(TM&: getSystemZTargetMachine()));
323
324 // Do final scheduling after all other optimizations, to get an
325 // optimal input for the decoder (branch relaxation must happen
326 // after block placement).
327 if (getOptLevel() != CodeGenOptLevel::None)
328 addPass(PassID: &PostMachineSchedulerID);
329}
330
331TargetPassConfig *SystemZTargetMachine::createPassConfig(PassManagerBase &PM) {
332 return new SystemZPassConfig(*this, PM);
333}
334
335TargetTransformInfo
336SystemZTargetMachine::getTargetTransformInfo(const Function &F) const {
337 return TargetTransformInfo(std::make_unique<SystemZTTIImpl>(args: this, args: F));
338}
339
340MachineFunctionInfo *SystemZTargetMachine::createMachineFunctionInfo(
341 BumpPtrAllocator &Allocator, const Function &F,
342 const TargetSubtargetInfo *STI) const {
343 return SystemZMachineFunctionInfo::create<SystemZMachineFunctionInfo>(
344 Allocator, F, STI);
345}
346