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 "SystemZMachineScheduler.h" 13 #include "SystemZTargetTransformInfo.h" 14 #include "TargetInfo/SystemZTargetInfo.h" 15 #include "llvm/ADT/Optional.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/ADT/StringRef.h" 19 #include "llvm/Analysis/TargetTransformInfo.h" 20 #include "llvm/CodeGen/Passes.h" 21 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 22 #include "llvm/CodeGen/TargetPassConfig.h" 23 #include "llvm/IR/DataLayout.h" 24 #include "llvm/MC/TargetRegistry.h" 25 #include "llvm/Support/CodeGen.h" 26 #include "llvm/Target/TargetLoweringObjectFile.h" 27 #include "llvm/Transforms/Scalar.h" 28 #include <string> 29 30 using namespace llvm; 31 32 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSystemZTarget() { 33 // Register the target. 34 RegisterTargetMachine<SystemZTargetMachine> X(getTheSystemZTarget()); 35 } 36 37 // Determine whether we use the vector ABI. 38 static bool UsesVectorABI(StringRef CPU, StringRef FS) { 39 // We use the vector ABI whenever the vector facility is avaiable. 40 // This is the case by default if CPU is z13 or later, and can be 41 // overridden via "[+-]vector" feature string elements. 42 bool VectorABI = true; 43 bool SoftFloat = false; 44 if (CPU.empty() || CPU == "generic" || 45 CPU == "z10" || CPU == "z196" || CPU == "zEC12" || 46 CPU == "arch8" || CPU == "arch9" || CPU == "arch10") 47 VectorABI = false; 48 49 SmallVector<StringRef, 3> Features; 50 FS.split(Features, ',', -1, false /* KeepEmpty */); 51 for (auto &Feature : Features) { 52 if (Feature == "vector" || Feature == "+vector") 53 VectorABI = true; 54 if (Feature == "-vector") 55 VectorABI = false; 56 if (Feature == "soft-float" || Feature == "+soft-float") 57 SoftFloat = true; 58 if (Feature == "-soft-float") 59 SoftFloat = false; 60 } 61 62 return VectorABI && !SoftFloat; 63 } 64 65 static std::string computeDataLayout(const Triple &TT, StringRef CPU, 66 StringRef FS) { 67 bool VectorABI = UsesVectorABI(CPU, FS); 68 std::string Ret; 69 70 // Big endian. 71 Ret += "E"; 72 73 // Data mangling. 74 Ret += DataLayout::getManglingComponent(TT); 75 76 // Make sure that global data has at least 16 bits of alignment by 77 // default, so that we can refer to it using LARL. We don't have any 78 // special requirements for stack variables though. 79 Ret += "-i1:8:16-i8:8:16"; 80 81 // 64-bit integers are naturally aligned. 82 Ret += "-i64:64"; 83 84 // 128-bit floats are aligned only to 64 bits. 85 Ret += "-f128:64"; 86 87 // When using the vector ABI on Linux, 128-bit vectors are also aligned to 64 88 // bits. On z/OS, vector types are always aligned to 64 bits. 89 if (VectorABI || TT.isOSzOS()) 90 Ret += "-v128:64"; 91 92 // We prefer 16 bits of aligned for all globals; see above. 93 Ret += "-a:8:16"; 94 95 // Integer registers are 32 or 64 bits. 96 Ret += "-n32:64"; 97 98 return Ret; 99 } 100 101 static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) { 102 if (TT.isOSzOS()) 103 return std::make_unique<TargetLoweringObjectFileGOFF>(); 104 105 // Note: Some times run with -triple s390x-unknown. 106 // In this case, default to ELF unless z/OS specifically provided. 107 return std::make_unique<TargetLoweringObjectFileELF>(); 108 } 109 110 static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) { 111 // Static code is suitable for use in a dynamic executable; there is no 112 // separate DynamicNoPIC model. 113 if (!RM.hasValue() || *RM == Reloc::DynamicNoPIC) 114 return Reloc::Static; 115 return *RM; 116 } 117 118 // For SystemZ we define the models as follows: 119 // 120 // Small: BRASL can call any function and will use a stub if necessary. 121 // Locally-binding symbols will always be in range of LARL. 122 // 123 // Medium: BRASL can call any function and will use a stub if necessary. 124 // GOT slots and locally-defined text will always be in range 125 // of LARL, but other symbols might not be. 126 // 127 // Large: Equivalent to Medium for now. 128 // 129 // Kernel: Equivalent to Medium for now. 130 // 131 // This means that any PIC module smaller than 4GB meets the 132 // requirements of Small, so Small seems like the best default there. 133 // 134 // All symbols bind locally in a non-PIC module, so the choice is less 135 // obvious. There are two cases: 136 // 137 // - When creating an executable, PLTs and copy relocations allow 138 // us to treat external symbols as part of the executable. 139 // Any executable smaller than 4GB meets the requirements of Small, 140 // so that seems like the best default. 141 // 142 // - When creating JIT code, stubs will be in range of BRASL if the 143 // image is less than 4GB in size. GOT entries will likewise be 144 // in range of LARL. However, the JIT environment has no equivalent 145 // of copy relocs, so locally-binding data symbols might not be in 146 // the range of LARL. We need the Medium model in that case. 147 static CodeModel::Model 148 getEffectiveSystemZCodeModel(Optional<CodeModel::Model> CM, Reloc::Model RM, 149 bool JIT) { 150 if (CM) { 151 if (*CM == CodeModel::Tiny) 152 report_fatal_error("Target does not support the tiny CodeModel", false); 153 if (*CM == CodeModel::Kernel) 154 report_fatal_error("Target does not support the kernel CodeModel", false); 155 return *CM; 156 } 157 if (JIT) 158 return RM == Reloc::PIC_ ? CodeModel::Small : CodeModel::Medium; 159 return CodeModel::Small; 160 } 161 162 SystemZTargetMachine::SystemZTargetMachine(const Target &T, const Triple &TT, 163 StringRef CPU, StringRef FS, 164 const TargetOptions &Options, 165 Optional<Reloc::Model> RM, 166 Optional<CodeModel::Model> CM, 167 CodeGenOpt::Level OL, bool JIT) 168 : LLVMTargetMachine( 169 T, computeDataLayout(TT, CPU, FS), TT, CPU, FS, Options, 170 getEffectiveRelocModel(RM), 171 getEffectiveSystemZCodeModel(CM, getEffectiveRelocModel(RM), JIT), 172 OL), 173 TLOF(createTLOF(getTargetTriple())) { 174 initAsmInfo(); 175 } 176 177 SystemZTargetMachine::~SystemZTargetMachine() = default; 178 179 const SystemZSubtarget * 180 SystemZTargetMachine::getSubtargetImpl(const Function &F) const { 181 Attribute CPUAttr = F.getFnAttribute("target-cpu"); 182 Attribute FSAttr = F.getFnAttribute("target-features"); 183 184 std::string CPU = 185 CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU; 186 std::string FS = 187 FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS; 188 189 // FIXME: This is related to the code below to reset the target options, 190 // we need to know whether or not the soft float flag is set on the 191 // function, so we can enable it as a subtarget feature. 192 bool softFloat = F.getFnAttribute("use-soft-float").getValueAsBool(); 193 194 if (softFloat) 195 FS += FS.empty() ? "+soft-float" : ",+soft-float"; 196 197 auto &I = SubtargetMap[CPU + FS]; 198 if (!I) { 199 // This needs to be done before we create a new subtarget since any 200 // creation will depend on the TM and the code generation flags on the 201 // function that reside in TargetOptions. 202 resetTargetOptions(F); 203 I = std::make_unique<SystemZSubtarget>(TargetTriple, CPU, FS, *this); 204 } 205 206 return I.get(); 207 } 208 209 namespace { 210 211 /// SystemZ Code Generator Pass Configuration Options. 212 class SystemZPassConfig : public TargetPassConfig { 213 public: 214 SystemZPassConfig(SystemZTargetMachine &TM, PassManagerBase &PM) 215 : TargetPassConfig(TM, PM) {} 216 217 SystemZTargetMachine &getSystemZTargetMachine() const { 218 return getTM<SystemZTargetMachine>(); 219 } 220 221 ScheduleDAGInstrs * 222 createPostMachineScheduler(MachineSchedContext *C) const override { 223 return new ScheduleDAGMI(C, 224 std::make_unique<SystemZPostRASchedStrategy>(C), 225 /*RemoveKillFlags=*/true); 226 } 227 228 void addIRPasses() override; 229 bool addInstSelector() override; 230 bool addILPOpts() override; 231 void addPreRegAlloc() override; 232 void addPostRewrite() override; 233 void addPostRegAlloc() override; 234 void addPreSched2() override; 235 void addPreEmitPass() override; 236 }; 237 238 } // end anonymous namespace 239 240 void SystemZPassConfig::addIRPasses() { 241 if (getOptLevel() != CodeGenOpt::None) { 242 addPass(createSystemZTDCPass()); 243 addPass(createLoopDataPrefetchPass()); 244 } 245 246 TargetPassConfig::addIRPasses(); 247 } 248 249 bool SystemZPassConfig::addInstSelector() { 250 addPass(createSystemZISelDag(getSystemZTargetMachine(), getOptLevel())); 251 252 if (getOptLevel() != CodeGenOpt::None) 253 addPass(createSystemZLDCleanupPass(getSystemZTargetMachine())); 254 255 return false; 256 } 257 258 bool SystemZPassConfig::addILPOpts() { 259 addPass(&EarlyIfConverterID); 260 return true; 261 } 262 263 void SystemZPassConfig::addPreRegAlloc() { 264 addPass(createSystemZCopyPhysRegsPass(getSystemZTargetMachine())); 265 } 266 267 void SystemZPassConfig::addPostRewrite() { 268 addPass(createSystemZPostRewritePass(getSystemZTargetMachine())); 269 } 270 271 void SystemZPassConfig::addPostRegAlloc() { 272 // PostRewrite needs to be run at -O0 also (in which case addPostRewrite() 273 // is not called). 274 if (getOptLevel() == CodeGenOpt::None) 275 addPass(createSystemZPostRewritePass(getSystemZTargetMachine())); 276 } 277 278 void SystemZPassConfig::addPreSched2() { 279 if (getOptLevel() != CodeGenOpt::None) 280 addPass(&IfConverterID); 281 } 282 283 void SystemZPassConfig::addPreEmitPass() { 284 // Do instruction shortening before compare elimination because some 285 // vector instructions will be shortened into opcodes that compare 286 // elimination recognizes. 287 if (getOptLevel() != CodeGenOpt::None) 288 addPass(createSystemZShortenInstPass(getSystemZTargetMachine())); 289 290 // We eliminate comparisons here rather than earlier because some 291 // transformations can change the set of available CC values and we 292 // generally want those transformations to have priority. This is 293 // especially true in the commonest case where the result of the comparison 294 // is used by a single in-range branch instruction, since we will then 295 // be able to fuse the compare and the branch instead. 296 // 297 // For example, two-address NILF can sometimes be converted into 298 // three-address RISBLG. NILF produces a CC value that indicates whether 299 // the low word is zero, but RISBLG does not modify CC at all. On the 300 // other hand, 64-bit ANDs like NILL can sometimes be converted to RISBG. 301 // The CC value produced by NILL isn't useful for our purposes, but the 302 // value produced by RISBG can be used for any comparison with zero 303 // (not just equality). So there are some transformations that lose 304 // CC values (while still being worthwhile) and others that happen to make 305 // the CC result more useful than it was originally. 306 // 307 // Another reason is that we only want to use BRANCH ON COUNT in cases 308 // where we know that the count register is not going to be spilled. 309 // 310 // Doing it so late makes it more likely that a register will be reused 311 // between the comparison and the branch, but it isn't clear whether 312 // preventing that would be a win or not. 313 if (getOptLevel() != CodeGenOpt::None) 314 addPass(createSystemZElimComparePass(getSystemZTargetMachine())); 315 addPass(createSystemZLongBranchPass(getSystemZTargetMachine())); 316 317 // Do final scheduling after all other optimizations, to get an 318 // optimal input for the decoder (branch relaxation must happen 319 // after block placement). 320 if (getOptLevel() != CodeGenOpt::None) 321 addPass(&PostMachineSchedulerID); 322 } 323 324 TargetPassConfig *SystemZTargetMachine::createPassConfig(PassManagerBase &PM) { 325 return new SystemZPassConfig(*this, PM); 326 } 327 328 TargetTransformInfo 329 SystemZTargetMachine::getTargetTransformInfo(const Function &F) { 330 return TargetTransformInfo(SystemZTTIImpl(this, F)); 331 } 332