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" 120b57cec5SDimitry Andric #include "SystemZMachineScheduler.h" 130b57cec5SDimitry Andric #include "SystemZTargetTransformInfo.h" 140b57cec5SDimitry Andric #include "TargetInfo/SystemZTargetInfo.h" 150b57cec5SDimitry Andric #include "llvm/ADT/Optional.h" 160b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 170b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 180b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 190b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h" 230b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 24*349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h" 250b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h" 260b57cec5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h" 270b57cec5SDimitry Andric #include "llvm/Transforms/Scalar.h" 280b57cec5SDimitry Andric #include <string> 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric using namespace llvm; 310b57cec5SDimitry Andric 32480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSystemZTarget() { 330b57cec5SDimitry Andric // Register the target. 340b57cec5SDimitry Andric RegisterTargetMachine<SystemZTargetMachine> X(getTheSystemZTarget()); 350b57cec5SDimitry Andric } 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric // Determine whether we use the vector ABI. 380b57cec5SDimitry Andric static bool UsesVectorABI(StringRef CPU, StringRef FS) { 390b57cec5SDimitry Andric // We use the vector ABI whenever the vector facility is avaiable. 400b57cec5SDimitry Andric // This is the case by default if CPU is z13 or later, and can be 410b57cec5SDimitry Andric // overridden via "[+-]vector" feature string elements. 420b57cec5SDimitry Andric bool VectorABI = true; 435ffd83dbSDimitry Andric bool SoftFloat = false; 440b57cec5SDimitry Andric if (CPU.empty() || CPU == "generic" || 455ffd83dbSDimitry Andric CPU == "z10" || CPU == "z196" || CPU == "zEC12" || 465ffd83dbSDimitry Andric CPU == "arch8" || CPU == "arch9" || CPU == "arch10") 470b57cec5SDimitry Andric VectorABI = false; 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric SmallVector<StringRef, 3> Features; 500b57cec5SDimitry Andric FS.split(Features, ',', -1, false /* KeepEmpty */); 510b57cec5SDimitry Andric for (auto &Feature : Features) { 520b57cec5SDimitry Andric if (Feature == "vector" || Feature == "+vector") 530b57cec5SDimitry Andric VectorABI = true; 540b57cec5SDimitry Andric if (Feature == "-vector") 550b57cec5SDimitry Andric VectorABI = false; 565ffd83dbSDimitry Andric if (Feature == "soft-float" || Feature == "+soft-float") 575ffd83dbSDimitry Andric SoftFloat = true; 585ffd83dbSDimitry Andric if (Feature == "-soft-float") 595ffd83dbSDimitry Andric SoftFloat = false; 600b57cec5SDimitry Andric } 610b57cec5SDimitry Andric 625ffd83dbSDimitry Andric return VectorABI && !SoftFloat; 630b57cec5SDimitry Andric } 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric static std::string computeDataLayout(const Triple &TT, StringRef CPU, 660b57cec5SDimitry Andric StringRef FS) { 670b57cec5SDimitry Andric bool VectorABI = UsesVectorABI(CPU, FS); 680b57cec5SDimitry Andric std::string Ret; 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric // Big endian. 710b57cec5SDimitry Andric Ret += "E"; 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric // Data mangling. 740b57cec5SDimitry Andric Ret += DataLayout::getManglingComponent(TT); 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric // Make sure that global data has at least 16 bits of alignment by 770b57cec5SDimitry Andric // default, so that we can refer to it using LARL. We don't have any 780b57cec5SDimitry Andric // special requirements for stack variables though. 790b57cec5SDimitry Andric Ret += "-i1:8:16-i8:8:16"; 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric // 64-bit integers are naturally aligned. 820b57cec5SDimitry Andric Ret += "-i64:64"; 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric // 128-bit floats are aligned only to 64 bits. 850b57cec5SDimitry Andric Ret += "-f128:64"; 860b57cec5SDimitry Andric 87*349cc55cSDimitry Andric // When using the vector ABI on Linux, 128-bit vectors are also aligned to 64 88*349cc55cSDimitry Andric // bits. On z/OS, vector types are always aligned to 64 bits. 89*349cc55cSDimitry Andric if (VectorABI || TT.isOSzOS()) 900b57cec5SDimitry Andric Ret += "-v128:64"; 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric // We prefer 16 bits of aligned for all globals; see above. 930b57cec5SDimitry Andric Ret += "-a:8:16"; 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric // Integer registers are 32 or 64 bits. 960b57cec5SDimitry Andric Ret += "-n32:64"; 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric return Ret; 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric 101fe6060f1SDimitry Andric static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) { 102fe6060f1SDimitry Andric if (TT.isOSzOS()) 103fe6060f1SDimitry Andric return std::make_unique<TargetLoweringObjectFileGOFF>(); 104fe6060f1SDimitry Andric 105fe6060f1SDimitry Andric // Note: Some times run with -triple s390x-unknown. 106fe6060f1SDimitry Andric // In this case, default to ELF unless z/OS specifically provided. 107fe6060f1SDimitry Andric return std::make_unique<TargetLoweringObjectFileELF>(); 108fe6060f1SDimitry Andric } 109fe6060f1SDimitry Andric 1100b57cec5SDimitry Andric static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) { 1110b57cec5SDimitry Andric // Static code is suitable for use in a dynamic executable; there is no 1120b57cec5SDimitry Andric // separate DynamicNoPIC model. 1130b57cec5SDimitry Andric if (!RM.hasValue() || *RM == Reloc::DynamicNoPIC) 1140b57cec5SDimitry Andric return Reloc::Static; 1150b57cec5SDimitry Andric return *RM; 1160b57cec5SDimitry Andric } 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric // For SystemZ we define the models as follows: 1190b57cec5SDimitry Andric // 1200b57cec5SDimitry Andric // Small: BRASL can call any function and will use a stub if necessary. 1210b57cec5SDimitry Andric // Locally-binding symbols will always be in range of LARL. 1220b57cec5SDimitry Andric // 1230b57cec5SDimitry Andric // Medium: BRASL can call any function and will use a stub if necessary. 1240b57cec5SDimitry Andric // GOT slots and locally-defined text will always be in range 1250b57cec5SDimitry Andric // of LARL, but other symbols might not be. 1260b57cec5SDimitry Andric // 1270b57cec5SDimitry Andric // Large: Equivalent to Medium for now. 1280b57cec5SDimitry Andric // 1290b57cec5SDimitry Andric // Kernel: Equivalent to Medium for now. 1300b57cec5SDimitry Andric // 1310b57cec5SDimitry Andric // This means that any PIC module smaller than 4GB meets the 1320b57cec5SDimitry Andric // requirements of Small, so Small seems like the best default there. 1330b57cec5SDimitry Andric // 1340b57cec5SDimitry Andric // All symbols bind locally in a non-PIC module, so the choice is less 1350b57cec5SDimitry Andric // obvious. There are two cases: 1360b57cec5SDimitry Andric // 1370b57cec5SDimitry Andric // - When creating an executable, PLTs and copy relocations allow 1380b57cec5SDimitry Andric // us to treat external symbols as part of the executable. 1390b57cec5SDimitry Andric // Any executable smaller than 4GB meets the requirements of Small, 1400b57cec5SDimitry Andric // so that seems like the best default. 1410b57cec5SDimitry Andric // 1420b57cec5SDimitry Andric // - When creating JIT code, stubs will be in range of BRASL if the 1430b57cec5SDimitry Andric // image is less than 4GB in size. GOT entries will likewise be 1440b57cec5SDimitry Andric // in range of LARL. However, the JIT environment has no equivalent 1450b57cec5SDimitry Andric // of copy relocs, so locally-binding data symbols might not be in 1460b57cec5SDimitry Andric // the range of LARL. We need the Medium model in that case. 1470b57cec5SDimitry Andric static CodeModel::Model 1480b57cec5SDimitry Andric getEffectiveSystemZCodeModel(Optional<CodeModel::Model> CM, Reloc::Model RM, 1490b57cec5SDimitry Andric bool JIT) { 1500b57cec5SDimitry Andric if (CM) { 1510b57cec5SDimitry Andric if (*CM == CodeModel::Tiny) 1520b57cec5SDimitry Andric report_fatal_error("Target does not support the tiny CodeModel", false); 1530b57cec5SDimitry Andric if (*CM == CodeModel::Kernel) 1540b57cec5SDimitry Andric report_fatal_error("Target does not support the kernel CodeModel", false); 1550b57cec5SDimitry Andric return *CM; 1560b57cec5SDimitry Andric } 1570b57cec5SDimitry Andric if (JIT) 1580b57cec5SDimitry Andric return RM == Reloc::PIC_ ? CodeModel::Small : CodeModel::Medium; 1590b57cec5SDimitry Andric return CodeModel::Small; 1600b57cec5SDimitry Andric } 1610b57cec5SDimitry Andric 1620b57cec5SDimitry Andric SystemZTargetMachine::SystemZTargetMachine(const Target &T, const Triple &TT, 1630b57cec5SDimitry Andric StringRef CPU, StringRef FS, 1640b57cec5SDimitry Andric const TargetOptions &Options, 1650b57cec5SDimitry Andric Optional<Reloc::Model> RM, 1660b57cec5SDimitry Andric Optional<CodeModel::Model> CM, 1670b57cec5SDimitry Andric CodeGenOpt::Level OL, bool JIT) 1680b57cec5SDimitry Andric : LLVMTargetMachine( 1690b57cec5SDimitry Andric T, computeDataLayout(TT, CPU, FS), TT, CPU, FS, Options, 1700b57cec5SDimitry Andric getEffectiveRelocModel(RM), 1710b57cec5SDimitry Andric getEffectiveSystemZCodeModel(CM, getEffectiveRelocModel(RM), JIT), 1720b57cec5SDimitry Andric OL), 173fe6060f1SDimitry Andric TLOF(createTLOF(getTargetTriple())) { 1740b57cec5SDimitry Andric initAsmInfo(); 1750b57cec5SDimitry Andric } 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric SystemZTargetMachine::~SystemZTargetMachine() = default; 1780b57cec5SDimitry Andric 1795ffd83dbSDimitry Andric const SystemZSubtarget * 1805ffd83dbSDimitry Andric SystemZTargetMachine::getSubtargetImpl(const Function &F) const { 1815ffd83dbSDimitry Andric Attribute CPUAttr = F.getFnAttribute("target-cpu"); 1825ffd83dbSDimitry Andric Attribute FSAttr = F.getFnAttribute("target-features"); 1835ffd83dbSDimitry Andric 184e8d8bef9SDimitry Andric std::string CPU = 185e8d8bef9SDimitry Andric CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU; 186e8d8bef9SDimitry Andric std::string FS = 187e8d8bef9SDimitry Andric FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS; 1885ffd83dbSDimitry Andric 1895ffd83dbSDimitry Andric // FIXME: This is related to the code below to reset the target options, 1905ffd83dbSDimitry Andric // we need to know whether or not the soft float flag is set on the 1915ffd83dbSDimitry Andric // function, so we can enable it as a subtarget feature. 192fe6060f1SDimitry Andric bool softFloat = F.getFnAttribute("use-soft-float").getValueAsBool(); 1935ffd83dbSDimitry Andric 1945ffd83dbSDimitry Andric if (softFloat) 1955ffd83dbSDimitry Andric FS += FS.empty() ? "+soft-float" : ",+soft-float"; 1965ffd83dbSDimitry Andric 1975ffd83dbSDimitry Andric auto &I = SubtargetMap[CPU + FS]; 1985ffd83dbSDimitry Andric if (!I) { 1995ffd83dbSDimitry Andric // This needs to be done before we create a new subtarget since any 2005ffd83dbSDimitry Andric // creation will depend on the TM and the code generation flags on the 2015ffd83dbSDimitry Andric // function that reside in TargetOptions. 2025ffd83dbSDimitry Andric resetTargetOptions(F); 2035ffd83dbSDimitry Andric I = std::make_unique<SystemZSubtarget>(TargetTriple, CPU, FS, *this); 2045ffd83dbSDimitry Andric } 2055ffd83dbSDimitry Andric 2065ffd83dbSDimitry Andric return I.get(); 2075ffd83dbSDimitry Andric } 2085ffd83dbSDimitry Andric 2090b57cec5SDimitry Andric namespace { 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric /// SystemZ Code Generator Pass Configuration Options. 2120b57cec5SDimitry Andric class SystemZPassConfig : public TargetPassConfig { 2130b57cec5SDimitry Andric public: 2140b57cec5SDimitry Andric SystemZPassConfig(SystemZTargetMachine &TM, PassManagerBase &PM) 2150b57cec5SDimitry Andric : TargetPassConfig(TM, PM) {} 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric SystemZTargetMachine &getSystemZTargetMachine() const { 2180b57cec5SDimitry Andric return getTM<SystemZTargetMachine>(); 2190b57cec5SDimitry Andric } 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric ScheduleDAGInstrs * 2220b57cec5SDimitry Andric createPostMachineScheduler(MachineSchedContext *C) const override { 2230b57cec5SDimitry Andric return new ScheduleDAGMI(C, 2248bcb0991SDimitry Andric std::make_unique<SystemZPostRASchedStrategy>(C), 2250b57cec5SDimitry Andric /*RemoveKillFlags=*/true); 2260b57cec5SDimitry Andric } 2270b57cec5SDimitry Andric 2280b57cec5SDimitry Andric void addIRPasses() override; 2290b57cec5SDimitry Andric bool addInstSelector() override; 2300b57cec5SDimitry Andric bool addILPOpts() override; 2315ffd83dbSDimitry Andric void addPreRegAlloc() override; 2320b57cec5SDimitry Andric void addPostRewrite() override; 2338bcb0991SDimitry Andric void addPostRegAlloc() override; 2340b57cec5SDimitry Andric void addPreSched2() override; 2350b57cec5SDimitry Andric void addPreEmitPass() override; 2360b57cec5SDimitry Andric }; 2370b57cec5SDimitry Andric 2380b57cec5SDimitry Andric } // end anonymous namespace 2390b57cec5SDimitry Andric 2400b57cec5SDimitry Andric void SystemZPassConfig::addIRPasses() { 2410b57cec5SDimitry Andric if (getOptLevel() != CodeGenOpt::None) { 2420b57cec5SDimitry Andric addPass(createSystemZTDCPass()); 2430b57cec5SDimitry Andric addPass(createLoopDataPrefetchPass()); 2440b57cec5SDimitry Andric } 2450b57cec5SDimitry Andric 2460b57cec5SDimitry Andric TargetPassConfig::addIRPasses(); 2470b57cec5SDimitry Andric } 2480b57cec5SDimitry Andric 2490b57cec5SDimitry Andric bool SystemZPassConfig::addInstSelector() { 2500b57cec5SDimitry Andric addPass(createSystemZISelDag(getSystemZTargetMachine(), getOptLevel())); 2510b57cec5SDimitry Andric 2520b57cec5SDimitry Andric if (getOptLevel() != CodeGenOpt::None) 2530b57cec5SDimitry Andric addPass(createSystemZLDCleanupPass(getSystemZTargetMachine())); 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric return false; 2560b57cec5SDimitry Andric } 2570b57cec5SDimitry Andric 2580b57cec5SDimitry Andric bool SystemZPassConfig::addILPOpts() { 2590b57cec5SDimitry Andric addPass(&EarlyIfConverterID); 2600b57cec5SDimitry Andric return true; 2610b57cec5SDimitry Andric } 2620b57cec5SDimitry Andric 2635ffd83dbSDimitry Andric void SystemZPassConfig::addPreRegAlloc() { 2645ffd83dbSDimitry Andric addPass(createSystemZCopyPhysRegsPass(getSystemZTargetMachine())); 2655ffd83dbSDimitry Andric } 2665ffd83dbSDimitry Andric 2670b57cec5SDimitry Andric void SystemZPassConfig::addPostRewrite() { 2680b57cec5SDimitry Andric addPass(createSystemZPostRewritePass(getSystemZTargetMachine())); 2690b57cec5SDimitry Andric } 2700b57cec5SDimitry Andric 2718bcb0991SDimitry Andric void SystemZPassConfig::addPostRegAlloc() { 2720b57cec5SDimitry Andric // PostRewrite needs to be run at -O0 also (in which case addPostRewrite() 2730b57cec5SDimitry Andric // is not called). 2740b57cec5SDimitry Andric if (getOptLevel() == CodeGenOpt::None) 2750b57cec5SDimitry Andric addPass(createSystemZPostRewritePass(getSystemZTargetMachine())); 2768bcb0991SDimitry Andric } 2770b57cec5SDimitry Andric 2788bcb0991SDimitry Andric void SystemZPassConfig::addPreSched2() { 2790b57cec5SDimitry Andric if (getOptLevel() != CodeGenOpt::None) 2800b57cec5SDimitry Andric addPass(&IfConverterID); 2810b57cec5SDimitry Andric } 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric void SystemZPassConfig::addPreEmitPass() { 2840b57cec5SDimitry Andric // Do instruction shortening before compare elimination because some 2850b57cec5SDimitry Andric // vector instructions will be shortened into opcodes that compare 2860b57cec5SDimitry Andric // elimination recognizes. 2870b57cec5SDimitry Andric if (getOptLevel() != CodeGenOpt::None) 288*349cc55cSDimitry Andric addPass(createSystemZShortenInstPass(getSystemZTargetMachine())); 2890b57cec5SDimitry Andric 2900b57cec5SDimitry Andric // We eliminate comparisons here rather than earlier because some 2910b57cec5SDimitry Andric // transformations can change the set of available CC values and we 2920b57cec5SDimitry Andric // generally want those transformations to have priority. This is 2930b57cec5SDimitry Andric // especially true in the commonest case where the result of the comparison 2940b57cec5SDimitry Andric // is used by a single in-range branch instruction, since we will then 2950b57cec5SDimitry Andric // be able to fuse the compare and the branch instead. 2960b57cec5SDimitry Andric // 2970b57cec5SDimitry Andric // For example, two-address NILF can sometimes be converted into 2980b57cec5SDimitry Andric // three-address RISBLG. NILF produces a CC value that indicates whether 2990b57cec5SDimitry Andric // the low word is zero, but RISBLG does not modify CC at all. On the 3000b57cec5SDimitry Andric // other hand, 64-bit ANDs like NILL can sometimes be converted to RISBG. 3010b57cec5SDimitry Andric // The CC value produced by NILL isn't useful for our purposes, but the 3020b57cec5SDimitry Andric // value produced by RISBG can be used for any comparison with zero 3030b57cec5SDimitry Andric // (not just equality). So there are some transformations that lose 3040b57cec5SDimitry Andric // CC values (while still being worthwhile) and others that happen to make 3050b57cec5SDimitry Andric // the CC result more useful than it was originally. 3060b57cec5SDimitry Andric // 3070b57cec5SDimitry Andric // Another reason is that we only want to use BRANCH ON COUNT in cases 3080b57cec5SDimitry Andric // where we know that the count register is not going to be spilled. 3090b57cec5SDimitry Andric // 3100b57cec5SDimitry Andric // Doing it so late makes it more likely that a register will be reused 3110b57cec5SDimitry Andric // between the comparison and the branch, but it isn't clear whether 3120b57cec5SDimitry Andric // preventing that would be a win or not. 3130b57cec5SDimitry Andric if (getOptLevel() != CodeGenOpt::None) 314*349cc55cSDimitry Andric addPass(createSystemZElimComparePass(getSystemZTargetMachine())); 3150b57cec5SDimitry Andric addPass(createSystemZLongBranchPass(getSystemZTargetMachine())); 3160b57cec5SDimitry Andric 3170b57cec5SDimitry Andric // Do final scheduling after all other optimizations, to get an 3180b57cec5SDimitry Andric // optimal input for the decoder (branch relaxation must happen 3190b57cec5SDimitry Andric // after block placement). 3200b57cec5SDimitry Andric if (getOptLevel() != CodeGenOpt::None) 3210b57cec5SDimitry Andric addPass(&PostMachineSchedulerID); 3220b57cec5SDimitry Andric } 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric TargetPassConfig *SystemZTargetMachine::createPassConfig(PassManagerBase &PM) { 3250b57cec5SDimitry Andric return new SystemZPassConfig(*this, PM); 3260b57cec5SDimitry Andric } 3270b57cec5SDimitry Andric 3280b57cec5SDimitry Andric TargetTransformInfo 3290b57cec5SDimitry Andric SystemZTargetMachine::getTargetTransformInfo(const Function &F) { 3300b57cec5SDimitry Andric return TargetTransformInfo(SystemZTTIImpl(this, F)); 3310b57cec5SDimitry Andric } 332