AMDGPU: Mark test as XFAIL in expensive_checks builds
[llvm-project.git] / llvm / lib / Target / SystemZ / SystemZTargetMachine.cpp
blob9000df2f6be0f448abc6acf2c9896aaabfafe2bc
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 //===----------------------------------------------------------------------===//
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/Target/TargetLoweringObjectFile.h"
26 #include "llvm/Transforms/Scalar.h"
27 #include <memory>
28 #include <optional>
29 #include <string>
31 using namespace llvm;
33 static cl::opt<bool> EnableMachineCombinerPass(
34 "systemz-machine-combiner",
35 cl::desc("Enable the machine combiner pass"),
36 cl::init(true), cl::Hidden);
38 // NOLINTNEXTLINE(readability-identifier-naming)
39 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSystemZTarget() {
40 // Register the target.
41 RegisterTargetMachine<SystemZTargetMachine> X(getTheSystemZTarget());
42 auto &PR = *PassRegistry::getPassRegistry();
43 initializeSystemZElimComparePass(PR);
44 initializeSystemZShortenInstPass(PR);
45 initializeSystemZLongBranchPass(PR);
46 initializeSystemZLDCleanupPass(PR);
47 initializeSystemZShortenInstPass(PR);
48 initializeSystemZPostRewritePass(PR);
49 initializeSystemZTDCPassPass(PR);
50 initializeSystemZDAGToDAGISelLegacyPass(PR);
53 static std::string computeDataLayout(const Triple &TT) {
54 std::string Ret;
56 // Big endian.
57 Ret += "E";
59 // Data mangling.
60 Ret += DataLayout::getManglingComponent(TT);
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";
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";
75 // 64-bit integers are naturally aligned.
76 Ret += "-i64:64";
78 // 128-bit floats are aligned only to 64 bits.
79 Ret += "-f128:64";
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";
85 // We prefer 16 bits of aligned for all globals; see above.
86 Ret += "-a:8:16";
88 // Integer registers are 32 or 64 bits.
89 Ret += "-n32:64";
91 return Ret;
94 static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
95 if (TT.isOSzOS())
96 return std::make_unique<TargetLoweringObjectFileGOFF>();
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>();
103 static 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;
111 // For SystemZ we define the models as follows:
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.
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.
120 // Large: Equivalent to Medium for now.
122 // Kernel: Equivalent to Medium for now.
124 // This means that any PIC module smaller than 4GB meets the
125 // requirements of Small, so Small seems like the best default there.
127 // All symbols bind locally in a non-PIC module, so the choice is less
128 // obvious. There are two cases:
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.
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.
140 static CodeModel::Model
141 getEffectiveSystemZCodeModel(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;
150 if (JIT)
151 return RM == Reloc::PIC_ ? CodeModel::Small : CodeModel::Medium;
152 return CodeModel::Small;
155 SystemZTargetMachine::SystemZTargetMachine(const Target &T, const Triple &TT,
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)
161 : CodeGenTargetMachineImpl(
162 T, computeDataLayout(TT), TT, CPU, FS, Options,
163 getEffectiveRelocModel(RM),
164 getEffectiveSystemZCodeModel(CM, getEffectiveRelocModel(RM), JIT),
165 OL),
166 TLOF(createTLOF(getTargetTriple())) {
167 initAsmInfo();
170 SystemZTargetMachine::~SystemZTargetMachine() = default;
172 const SystemZSubtarget *
173 SystemZTargetMachine::getSubtargetImpl(const Function &F) const {
174 Attribute CPUAttr = F.getFnAttribute("target-cpu");
175 Attribute TuneAttr = F.getFnAttribute("tune-cpu");
176 Attribute FSAttr = F.getFnAttribute("target-features");
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;
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";
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.
200 resetTargetOptions(F);
201 I = std::make_unique<SystemZSubtarget>(TargetTriple, CPU, TuneCPU, FS,
202 *this);
205 return I.get();
208 namespace {
210 /// SystemZ Code Generator Pass Configuration Options.
211 class SystemZPassConfig : public TargetPassConfig {
212 public:
213 SystemZPassConfig(SystemZTargetMachine &TM, PassManagerBase &PM)
214 : TargetPassConfig(TM, PM) {}
216 SystemZTargetMachine &getSystemZTargetMachine() const {
217 return getTM<SystemZTargetMachine>();
220 ScheduleDAGInstrs *
221 createPostMachineScheduler(MachineSchedContext *C) const override {
222 return new ScheduleDAGMI(C,
223 std::make_unique<SystemZPostRASchedStrategy>(C),
224 /*RemoveKillFlags=*/true);
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;
237 } // end anonymous namespace
239 void SystemZPassConfig::addIRPasses() {
240 if (getOptLevel() != CodeGenOptLevel::None) {
241 addPass(createSystemZTDCPass());
242 addPass(createLoopDataPrefetchPass());
245 addPass(createAtomicExpandLegacyPass());
247 TargetPassConfig::addIRPasses();
250 bool SystemZPassConfig::addInstSelector() {
251 addPass(createSystemZISelDag(getSystemZTargetMachine(), getOptLevel()));
253 if (getOptLevel() != CodeGenOptLevel::None)
254 addPass(createSystemZLDCleanupPass(getSystemZTargetMachine()));
256 return false;
259 bool SystemZPassConfig::addILPOpts() {
260 addPass(&EarlyIfConverterLegacyID);
262 if (EnableMachineCombinerPass)
263 addPass(&MachineCombinerID);
265 return true;
268 void SystemZPassConfig::addPreRegAlloc() {
269 addPass(createSystemZCopyPhysRegsPass(getSystemZTargetMachine()));
272 void SystemZPassConfig::addPostRewrite() {
273 addPass(createSystemZPostRewritePass(getSystemZTargetMachine()));
276 void 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()));
283 void SystemZPassConfig::addPreSched2() {
284 if (getOptLevel() != CodeGenOptLevel::None)
285 addPass(&IfConverterID);
288 void 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()));
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.
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.
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.
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()));
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);
329 TargetPassConfig *SystemZTargetMachine::createPassConfig(PassManagerBase &PM) {
330 return new SystemZPassConfig(*this, PM);
333 TargetTransformInfo
334 SystemZTargetMachine::getTargetTransformInfo(const Function &F) const {
335 return TargetTransformInfo(SystemZTTIImpl(this, F));
338 MachineFunctionInfo *SystemZTargetMachine::createMachineFunctionInfo(
339 BumpPtrAllocator &Allocator, const Function &F,
340 const TargetSubtargetInfo *STI) const {
341 return SystemZMachineFunctionInfo::create<SystemZMachineFunctionInfo>(
342 Allocator, F, STI);