xref: /freebsd/contrib/llvm-project/llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===-- SystemZTargetMachine.cpp - Define TargetMachine for SystemZ -------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "SystemZTargetMachine.h"
100b57cec5SDimitry Andric #include "MCTargetDesc/SystemZMCTargetDesc.h"
110b57cec5SDimitry Andric #include "SystemZ.h"
12bdd1243dSDimitry Andric #include "SystemZMachineFunctionInfo.h"
130b57cec5SDimitry Andric #include "SystemZMachineScheduler.h"
140b57cec5SDimitry Andric #include "SystemZTargetTransformInfo.h"
150b57cec5SDimitry Andric #include "TargetInfo/SystemZTargetInfo.h"
160b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
170b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
210b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
22349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
230b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h"
240b57cec5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h"
250b57cec5SDimitry Andric #include "llvm/Transforms/Scalar.h"
26*5f757f3fSDimitry Andric #include <memory>
27bdd1243dSDimitry Andric #include <optional>
280b57cec5SDimitry Andric #include <string>
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric using namespace llvm;
310b57cec5SDimitry Andric 
32*5f757f3fSDimitry Andric // NOLINTNEXTLINE(readability-identifier-naming)
33480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSystemZTarget() {
340b57cec5SDimitry Andric   // Register the target.
350b57cec5SDimitry Andric   RegisterTargetMachine<SystemZTargetMachine> X(getTheSystemZTarget());
3604eeddc0SDimitry Andric   auto &PR = *PassRegistry::getPassRegistry();
3704eeddc0SDimitry Andric   initializeSystemZElimComparePass(PR);
3804eeddc0SDimitry Andric   initializeSystemZShortenInstPass(PR);
3904eeddc0SDimitry Andric   initializeSystemZLongBranchPass(PR);
4004eeddc0SDimitry Andric   initializeSystemZLDCleanupPass(PR);
4104eeddc0SDimitry Andric   initializeSystemZShortenInstPass(PR);
4204eeddc0SDimitry Andric   initializeSystemZPostRewritePass(PR);
4304eeddc0SDimitry Andric   initializeSystemZTDCPassPass(PR);
44bdd1243dSDimitry Andric   initializeSystemZDAGToDAGISelPass(PR);
450b57cec5SDimitry Andric }
460b57cec5SDimitry Andric 
47bdd1243dSDimitry Andric static std::string computeDataLayout(const Triple &TT) {
480b57cec5SDimitry Andric   std::string Ret;
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   // Big endian.
510b57cec5SDimitry Andric   Ret += "E";
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   // Data mangling.
540b57cec5SDimitry Andric   Ret += DataLayout::getManglingComponent(TT);
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric   // Make sure that global data has at least 16 bits of alignment by
570b57cec5SDimitry Andric   // default, so that we can refer to it using LARL.  We don't have any
580b57cec5SDimitry Andric   // special requirements for stack variables though.
590b57cec5SDimitry Andric   Ret += "-i1:8:16-i8:8:16";
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   // 64-bit integers are naturally aligned.
620b57cec5SDimitry Andric   Ret += "-i64:64";
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric   // 128-bit floats are aligned only to 64 bits.
650b57cec5SDimitry Andric   Ret += "-f128:64";
660b57cec5SDimitry Andric 
67bdd1243dSDimitry Andric   // The DataLayout string always holds a vector alignment of 64 bits, see
68bdd1243dSDimitry Andric   // comment in clang/lib/Basic/Targets/SystemZ.h.
690b57cec5SDimitry Andric   Ret += "-v128:64";
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric   // We prefer 16 bits of aligned for all globals; see above.
720b57cec5SDimitry Andric   Ret += "-a:8:16";
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric   // Integer registers are 32 or 64 bits.
750b57cec5SDimitry Andric   Ret += "-n32:64";
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   return Ret;
780b57cec5SDimitry Andric }
790b57cec5SDimitry Andric 
80fe6060f1SDimitry Andric static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
81fe6060f1SDimitry Andric   if (TT.isOSzOS())
82fe6060f1SDimitry Andric     return std::make_unique<TargetLoweringObjectFileGOFF>();
83fe6060f1SDimitry Andric 
84fe6060f1SDimitry Andric   // Note: Some times run with -triple s390x-unknown.
85fe6060f1SDimitry Andric   // In this case, default to ELF unless z/OS specifically provided.
86fe6060f1SDimitry Andric   return std::make_unique<TargetLoweringObjectFileELF>();
87fe6060f1SDimitry Andric }
88fe6060f1SDimitry Andric 
89bdd1243dSDimitry Andric static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
900b57cec5SDimitry Andric   // Static code is suitable for use in a dynamic executable; there is no
910b57cec5SDimitry Andric   // separate DynamicNoPIC model.
9281ad6265SDimitry Andric   if (!RM || *RM == Reloc::DynamicNoPIC)
930b57cec5SDimitry Andric     return Reloc::Static;
940b57cec5SDimitry Andric   return *RM;
950b57cec5SDimitry Andric }
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric // For SystemZ we define the models as follows:
980b57cec5SDimitry Andric //
990b57cec5SDimitry Andric // Small:  BRASL can call any function and will use a stub if necessary.
1000b57cec5SDimitry Andric //         Locally-binding symbols will always be in range of LARL.
1010b57cec5SDimitry Andric //
1020b57cec5SDimitry Andric // Medium: BRASL can call any function and will use a stub if necessary.
1030b57cec5SDimitry Andric //         GOT slots and locally-defined text will always be in range
1040b57cec5SDimitry Andric //         of LARL, but other symbols might not be.
1050b57cec5SDimitry Andric //
1060b57cec5SDimitry Andric // Large:  Equivalent to Medium for now.
1070b57cec5SDimitry Andric //
1080b57cec5SDimitry Andric // Kernel: Equivalent to Medium for now.
1090b57cec5SDimitry Andric //
1100b57cec5SDimitry Andric // This means that any PIC module smaller than 4GB meets the
1110b57cec5SDimitry Andric // requirements of Small, so Small seems like the best default there.
1120b57cec5SDimitry Andric //
1130b57cec5SDimitry Andric // All symbols bind locally in a non-PIC module, so the choice is less
1140b57cec5SDimitry Andric // obvious.  There are two cases:
1150b57cec5SDimitry Andric //
1160b57cec5SDimitry Andric // - When creating an executable, PLTs and copy relocations allow
1170b57cec5SDimitry Andric //   us to treat external symbols as part of the executable.
1180b57cec5SDimitry Andric //   Any executable smaller than 4GB meets the requirements of Small,
1190b57cec5SDimitry Andric //   so that seems like the best default.
1200b57cec5SDimitry Andric //
1210b57cec5SDimitry Andric // - When creating JIT code, stubs will be in range of BRASL if the
1220b57cec5SDimitry Andric //   image is less than 4GB in size.  GOT entries will likewise be
1230b57cec5SDimitry Andric //   in range of LARL.  However, the JIT environment has no equivalent
1240b57cec5SDimitry Andric //   of copy relocs, so locally-binding data symbols might not be in
1250b57cec5SDimitry Andric //   the range of LARL.  We need the Medium model in that case.
1260b57cec5SDimitry Andric static CodeModel::Model
127bdd1243dSDimitry Andric getEffectiveSystemZCodeModel(std::optional<CodeModel::Model> CM,
128bdd1243dSDimitry Andric                              Reloc::Model RM, bool JIT) {
1290b57cec5SDimitry Andric   if (CM) {
1300b57cec5SDimitry Andric     if (*CM == CodeModel::Tiny)
1310b57cec5SDimitry Andric       report_fatal_error("Target does not support the tiny CodeModel", false);
1320b57cec5SDimitry Andric     if (*CM == CodeModel::Kernel)
1330b57cec5SDimitry Andric       report_fatal_error("Target does not support the kernel CodeModel", false);
1340b57cec5SDimitry Andric     return *CM;
1350b57cec5SDimitry Andric   }
1360b57cec5SDimitry Andric   if (JIT)
1370b57cec5SDimitry Andric     return RM == Reloc::PIC_ ? CodeModel::Small : CodeModel::Medium;
1380b57cec5SDimitry Andric   return CodeModel::Small;
1390b57cec5SDimitry Andric }
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric SystemZTargetMachine::SystemZTargetMachine(const Target &T, const Triple &TT,
1420b57cec5SDimitry Andric                                            StringRef CPU, StringRef FS,
1430b57cec5SDimitry Andric                                            const TargetOptions &Options,
144bdd1243dSDimitry Andric                                            std::optional<Reloc::Model> RM,
145bdd1243dSDimitry Andric                                            std::optional<CodeModel::Model> CM,
146*5f757f3fSDimitry Andric                                            CodeGenOptLevel OL, bool JIT)
1470b57cec5SDimitry Andric     : LLVMTargetMachine(
148bdd1243dSDimitry Andric           T, computeDataLayout(TT), TT, CPU, FS, Options,
1490b57cec5SDimitry Andric           getEffectiveRelocModel(RM),
1500b57cec5SDimitry Andric           getEffectiveSystemZCodeModel(CM, getEffectiveRelocModel(RM), JIT),
1510b57cec5SDimitry Andric           OL),
152fe6060f1SDimitry Andric       TLOF(createTLOF(getTargetTriple())) {
1530b57cec5SDimitry Andric   initAsmInfo();
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric SystemZTargetMachine::~SystemZTargetMachine() = default;
1570b57cec5SDimitry Andric 
1585ffd83dbSDimitry Andric const SystemZSubtarget *
1595ffd83dbSDimitry Andric SystemZTargetMachine::getSubtargetImpl(const Function &F) const {
1605ffd83dbSDimitry Andric   Attribute CPUAttr = F.getFnAttribute("target-cpu");
16181ad6265SDimitry Andric   Attribute TuneAttr = F.getFnAttribute("tune-cpu");
1625ffd83dbSDimitry Andric   Attribute FSAttr = F.getFnAttribute("target-features");
1635ffd83dbSDimitry Andric 
164e8d8bef9SDimitry Andric   std::string CPU =
165e8d8bef9SDimitry Andric       CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
16681ad6265SDimitry Andric   std::string TuneCPU =
16781ad6265SDimitry Andric       TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
168e8d8bef9SDimitry Andric   std::string FS =
169e8d8bef9SDimitry Andric       FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
1705ffd83dbSDimitry Andric 
1715ffd83dbSDimitry Andric   // FIXME: This is related to the code below to reset the target options,
172*5f757f3fSDimitry Andric   // we need to know whether the soft float and backchain flags are set on the
173*5f757f3fSDimitry Andric   // function, so we can enable them as subtarget features.
174*5f757f3fSDimitry Andric   bool SoftFloat = F.getFnAttribute("use-soft-float").getValueAsBool();
175*5f757f3fSDimitry Andric   if (SoftFloat)
1765ffd83dbSDimitry Andric     FS += FS.empty() ? "+soft-float" : ",+soft-float";
177*5f757f3fSDimitry Andric   bool BackChain = F.hasFnAttribute("backchain");
178*5f757f3fSDimitry Andric   if (BackChain)
179*5f757f3fSDimitry Andric     FS += FS.empty() ? "+backchain" : ",+backchain";
1805ffd83dbSDimitry Andric 
18181ad6265SDimitry Andric   auto &I = SubtargetMap[CPU + TuneCPU + FS];
1825ffd83dbSDimitry Andric   if (!I) {
1835ffd83dbSDimitry Andric     // This needs to be done before we create a new subtarget since any
1845ffd83dbSDimitry Andric     // creation will depend on the TM and the code generation flags on the
1855ffd83dbSDimitry Andric     // function that reside in TargetOptions.
1865ffd83dbSDimitry Andric     resetTargetOptions(F);
18781ad6265SDimitry Andric     I = std::make_unique<SystemZSubtarget>(TargetTriple, CPU, TuneCPU, FS,
18881ad6265SDimitry Andric                                            *this);
1895ffd83dbSDimitry Andric   }
1905ffd83dbSDimitry Andric 
1915ffd83dbSDimitry Andric   return I.get();
1925ffd83dbSDimitry Andric }
1935ffd83dbSDimitry Andric 
1940b57cec5SDimitry Andric namespace {
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric /// SystemZ Code Generator Pass Configuration Options.
1970b57cec5SDimitry Andric class SystemZPassConfig : public TargetPassConfig {
1980b57cec5SDimitry Andric public:
1990b57cec5SDimitry Andric   SystemZPassConfig(SystemZTargetMachine &TM, PassManagerBase &PM)
2000b57cec5SDimitry Andric     : TargetPassConfig(TM, PM) {}
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric   SystemZTargetMachine &getSystemZTargetMachine() const {
2030b57cec5SDimitry Andric     return getTM<SystemZTargetMachine>();
2040b57cec5SDimitry Andric   }
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric   ScheduleDAGInstrs *
2070b57cec5SDimitry Andric   createPostMachineScheduler(MachineSchedContext *C) const override {
2080b57cec5SDimitry Andric     return new ScheduleDAGMI(C,
2098bcb0991SDimitry Andric                              std::make_unique<SystemZPostRASchedStrategy>(C),
2100b57cec5SDimitry Andric                              /*RemoveKillFlags=*/true);
2110b57cec5SDimitry Andric   }
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric   void addIRPasses() override;
2140b57cec5SDimitry Andric   bool addInstSelector() override;
2150b57cec5SDimitry Andric   bool addILPOpts() override;
2165ffd83dbSDimitry Andric   void addPreRegAlloc() override;
2170b57cec5SDimitry Andric   void addPostRewrite() override;
2188bcb0991SDimitry Andric   void addPostRegAlloc() override;
2190b57cec5SDimitry Andric   void addPreSched2() override;
2200b57cec5SDimitry Andric   void addPreEmitPass() override;
2210b57cec5SDimitry Andric };
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric } // end anonymous namespace
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric void SystemZPassConfig::addIRPasses() {
226*5f757f3fSDimitry Andric   if (getOptLevel() != CodeGenOptLevel::None) {
2270b57cec5SDimitry Andric     addPass(createSystemZTDCPass());
2280b57cec5SDimitry Andric     addPass(createLoopDataPrefetchPass());
2290b57cec5SDimitry Andric   }
2300b57cec5SDimitry Andric 
231*5f757f3fSDimitry Andric   addPass(createAtomicExpandPass());
232*5f757f3fSDimitry Andric 
2330b57cec5SDimitry Andric   TargetPassConfig::addIRPasses();
2340b57cec5SDimitry Andric }
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric bool SystemZPassConfig::addInstSelector() {
2370b57cec5SDimitry Andric   addPass(createSystemZISelDag(getSystemZTargetMachine(), getOptLevel()));
2380b57cec5SDimitry Andric 
239*5f757f3fSDimitry Andric   if (getOptLevel() != CodeGenOptLevel::None)
2400b57cec5SDimitry Andric     addPass(createSystemZLDCleanupPass(getSystemZTargetMachine()));
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric   return false;
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric bool SystemZPassConfig::addILPOpts() {
2460b57cec5SDimitry Andric   addPass(&EarlyIfConverterID);
2470b57cec5SDimitry Andric   return true;
2480b57cec5SDimitry Andric }
2490b57cec5SDimitry Andric 
2505ffd83dbSDimitry Andric void SystemZPassConfig::addPreRegAlloc() {
2515ffd83dbSDimitry Andric   addPass(createSystemZCopyPhysRegsPass(getSystemZTargetMachine()));
2525ffd83dbSDimitry Andric }
2535ffd83dbSDimitry Andric 
2540b57cec5SDimitry Andric void SystemZPassConfig::addPostRewrite() {
2550b57cec5SDimitry Andric   addPass(createSystemZPostRewritePass(getSystemZTargetMachine()));
2560b57cec5SDimitry Andric }
2570b57cec5SDimitry Andric 
2588bcb0991SDimitry Andric void SystemZPassConfig::addPostRegAlloc() {
2590b57cec5SDimitry Andric   // PostRewrite needs to be run at -O0 also (in which case addPostRewrite()
2600b57cec5SDimitry Andric   // is not called).
261*5f757f3fSDimitry Andric   if (getOptLevel() == CodeGenOptLevel::None)
2620b57cec5SDimitry Andric     addPass(createSystemZPostRewritePass(getSystemZTargetMachine()));
2638bcb0991SDimitry Andric }
2640b57cec5SDimitry Andric 
2658bcb0991SDimitry Andric void SystemZPassConfig::addPreSched2() {
266*5f757f3fSDimitry Andric   if (getOptLevel() != CodeGenOptLevel::None)
2670b57cec5SDimitry Andric     addPass(&IfConverterID);
2680b57cec5SDimitry Andric }
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric void SystemZPassConfig::addPreEmitPass() {
2710b57cec5SDimitry Andric   // Do instruction shortening before compare elimination because some
2720b57cec5SDimitry Andric   // vector instructions will be shortened into opcodes that compare
2730b57cec5SDimitry Andric   // elimination recognizes.
274*5f757f3fSDimitry Andric   if (getOptLevel() != CodeGenOptLevel::None)
275349cc55cSDimitry Andric     addPass(createSystemZShortenInstPass(getSystemZTargetMachine()));
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric   // We eliminate comparisons here rather than earlier because some
2780b57cec5SDimitry Andric   // transformations can change the set of available CC values and we
2790b57cec5SDimitry Andric   // generally want those transformations to have priority.  This is
2800b57cec5SDimitry Andric   // especially true in the commonest case where the result of the comparison
2810b57cec5SDimitry Andric   // is used by a single in-range branch instruction, since we will then
2820b57cec5SDimitry Andric   // be able to fuse the compare and the branch instead.
2830b57cec5SDimitry Andric   //
2840b57cec5SDimitry Andric   // For example, two-address NILF can sometimes be converted into
2850b57cec5SDimitry Andric   // three-address RISBLG.  NILF produces a CC value that indicates whether
2860b57cec5SDimitry Andric   // the low word is zero, but RISBLG does not modify CC at all.  On the
2870b57cec5SDimitry Andric   // other hand, 64-bit ANDs like NILL can sometimes be converted to RISBG.
2880b57cec5SDimitry Andric   // The CC value produced by NILL isn't useful for our purposes, but the
2890b57cec5SDimitry Andric   // value produced by RISBG can be used for any comparison with zero
2900b57cec5SDimitry Andric   // (not just equality).  So there are some transformations that lose
2910b57cec5SDimitry Andric   // CC values (while still being worthwhile) and others that happen to make
2920b57cec5SDimitry Andric   // the CC result more useful than it was originally.
2930b57cec5SDimitry Andric   //
2940b57cec5SDimitry Andric   // Another reason is that we only want to use BRANCH ON COUNT in cases
2950b57cec5SDimitry Andric   // where we know that the count register is not going to be spilled.
2960b57cec5SDimitry Andric   //
2970b57cec5SDimitry Andric   // Doing it so late makes it more likely that a register will be reused
2980b57cec5SDimitry Andric   // between the comparison and the branch, but it isn't clear whether
2990b57cec5SDimitry Andric   // preventing that would be a win or not.
300*5f757f3fSDimitry Andric   if (getOptLevel() != CodeGenOptLevel::None)
301349cc55cSDimitry Andric     addPass(createSystemZElimComparePass(getSystemZTargetMachine()));
3020b57cec5SDimitry Andric   addPass(createSystemZLongBranchPass(getSystemZTargetMachine()));
3030b57cec5SDimitry Andric 
3040b57cec5SDimitry Andric   // Do final scheduling after all other optimizations, to get an
3050b57cec5SDimitry Andric   // optimal input for the decoder (branch relaxation must happen
3060b57cec5SDimitry Andric   // after block placement).
307*5f757f3fSDimitry Andric   if (getOptLevel() != CodeGenOptLevel::None)
3080b57cec5SDimitry Andric     addPass(&PostMachineSchedulerID);
3090b57cec5SDimitry Andric }
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric TargetPassConfig *SystemZTargetMachine::createPassConfig(PassManagerBase &PM) {
3120b57cec5SDimitry Andric   return new SystemZPassConfig(*this, PM);
3130b57cec5SDimitry Andric }
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric TargetTransformInfo
31681ad6265SDimitry Andric SystemZTargetMachine::getTargetTransformInfo(const Function &F) const {
3170b57cec5SDimitry Andric   return TargetTransformInfo(SystemZTTIImpl(this, F));
3180b57cec5SDimitry Andric }
319bdd1243dSDimitry Andric 
320bdd1243dSDimitry Andric MachineFunctionInfo *SystemZTargetMachine::createMachineFunctionInfo(
321bdd1243dSDimitry Andric     BumpPtrAllocator &Allocator, const Function &F,
322bdd1243dSDimitry Andric     const TargetSubtargetInfo *STI) const {
323bdd1243dSDimitry Andric   return SystemZMachineFunctionInfo::create<SystemZMachineFunctionInfo>(
324bdd1243dSDimitry Andric       Allocator, F, STI);
325bdd1243dSDimitry Andric }
326