1 //===-- ARMTargetMachine.cpp - Define TargetMachine for ARM ---------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "ARM.h" 14 #include "ARMFrameLowering.h" 15 #include "ARMTargetMachine.h" 16 #include "ARMTargetObjectFile.h" 17 #include "ARMTargetTransformInfo.h" 18 #include "llvm/CodeGen/Passes.h" 19 #include "llvm/CodeGen/TargetPassConfig.h" 20 #include "llvm/IR/Function.h" 21 #include "llvm/IR/LegacyPassManager.h" 22 #include "llvm/MC/MCAsmInfo.h" 23 #include "llvm/Support/CommandLine.h" 24 #include "llvm/Support/FormattedStream.h" 25 #include "llvm/Support/TargetRegistry.h" 26 #include "llvm/Target/TargetOptions.h" 27 #include "llvm/Transforms/Scalar.h" 28 using namespace llvm; 29 30 static cl::opt<bool> 31 DisableA15SDOptimization("disable-a15-sd-optimization", cl::Hidden, 32 cl::desc("Inhibit optimization of S->D register accesses on A15"), 33 cl::init(false)); 34 35 static cl::opt<bool> 36 EnableAtomicTidy("arm-atomic-cfg-tidy", cl::Hidden, 37 cl::desc("Run SimplifyCFG after expanding atomic operations" 38 " to make use of cmpxchg flow-based information"), 39 cl::init(true)); 40 41 static cl::opt<bool> 42 EnableARMLoadStoreOpt("arm-load-store-opt", cl::Hidden, 43 cl::desc("Enable ARM load/store optimization pass"), 44 cl::init(true)); 45 46 // FIXME: Unify control over GlobalMerge. 47 static cl::opt<cl::boolOrDefault> 48 EnableGlobalMerge("arm-global-merge", cl::Hidden, 49 cl::desc("Enable the global merge pass")); 50 51 extern "C" void LLVMInitializeARMTarget() { 52 // Register the target. 53 RegisterTargetMachine<ARMLETargetMachine> X(TheARMLETarget); 54 RegisterTargetMachine<ARMBETargetMachine> Y(TheARMBETarget); 55 RegisterTargetMachine<ThumbLETargetMachine> A(TheThumbLETarget); 56 RegisterTargetMachine<ThumbBETargetMachine> B(TheThumbBETarget); 57 58 PassRegistry &Registry = *PassRegistry::getPassRegistry(); 59 initializeARMLoadStoreOptPass(Registry); 60 initializeARMPreAllocLoadStoreOptPass(Registry); 61 } 62 63 static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) { 64 if (TT.isOSBinFormatMachO()) 65 return make_unique<TargetLoweringObjectFileMachO>(); 66 if (TT.isOSWindows()) 67 return make_unique<TargetLoweringObjectFileCOFF>(); 68 return make_unique<ARMElfTargetObjectFile>(); 69 } 70 71 static ARMBaseTargetMachine::ARMABI 72 computeTargetABI(const Triple &TT, StringRef CPU, 73 const TargetOptions &Options) { 74 if (Options.MCOptions.getABIName() == "aapcs16") 75 return ARMBaseTargetMachine::ARM_ABI_AAPCS16; 76 else if (Options.MCOptions.getABIName().startswith("aapcs")) 77 return ARMBaseTargetMachine::ARM_ABI_AAPCS; 78 else if (Options.MCOptions.getABIName().startswith("apcs")) 79 return ARMBaseTargetMachine::ARM_ABI_APCS; 80 81 assert(Options.MCOptions.getABIName().empty() && 82 "Unknown target-abi option!"); 83 84 ARMBaseTargetMachine::ARMABI TargetABI = 85 ARMBaseTargetMachine::ARM_ABI_UNKNOWN; 86 87 // FIXME: This is duplicated code from the front end and should be unified. 88 if (TT.isOSBinFormatMachO()) { 89 if (TT.getEnvironment() == llvm::Triple::EABI || 90 (TT.getOS() == llvm::Triple::UnknownOS && TT.isOSBinFormatMachO()) || 91 CPU.startswith("cortex-m")) { 92 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS; 93 } else if (TT.isWatchABI()) { 94 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS16; 95 } else { 96 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS; 97 } 98 } else if (TT.isOSWindows()) { 99 // FIXME: this is invalid for WindowsCE 100 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS; 101 } else { 102 // Select the default based on the platform. 103 switch (TT.getEnvironment()) { 104 case llvm::Triple::Android: 105 case llvm::Triple::GNUEABI: 106 case llvm::Triple::GNUEABIHF: 107 case llvm::Triple::MuslEABI: 108 case llvm::Triple::MuslEABIHF: 109 case llvm::Triple::EABIHF: 110 case llvm::Triple::EABI: 111 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS; 112 break; 113 case llvm::Triple::GNU: 114 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS; 115 break; 116 default: 117 if (TT.isOSNetBSD()) 118 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS; 119 else 120 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS; 121 break; 122 } 123 } 124 125 return TargetABI; 126 } 127 128 static std::string computeDataLayout(const Triple &TT, StringRef CPU, 129 const TargetOptions &Options, 130 bool isLittle) { 131 auto ABI = computeTargetABI(TT, CPU, Options); 132 std::string Ret = ""; 133 134 if (isLittle) 135 // Little endian. 136 Ret += "e"; 137 else 138 // Big endian. 139 Ret += "E"; 140 141 Ret += DataLayout::getManglingComponent(TT); 142 143 // Pointers are 32 bits and aligned to 32 bits. 144 Ret += "-p:32:32"; 145 146 // ABIs other than APCS have 64 bit integers with natural alignment. 147 if (ABI != ARMBaseTargetMachine::ARM_ABI_APCS) 148 Ret += "-i64:64"; 149 150 // We have 64 bits floats. The APCS ABI requires them to be aligned to 32 151 // bits, others to 64 bits. We always try to align to 64 bits. 152 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS) 153 Ret += "-f64:32:64"; 154 155 // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others 156 // to 64. We always ty to give them natural alignment. 157 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS) 158 Ret += "-v64:32:64-v128:32:128"; 159 else if (ABI != ARMBaseTargetMachine::ARM_ABI_AAPCS16) 160 Ret += "-v128:64:128"; 161 162 // Try to align aggregates to 32 bits (the default is 64 bits, which has no 163 // particular hardware support on 32-bit ARM). 164 Ret += "-a:0:32"; 165 166 // Integer registers are 32 bits. 167 Ret += "-n32"; 168 169 // The stack is 128 bit aligned on NaCl, 64 bit aligned on AAPCS and 32 bit 170 // aligned everywhere else. 171 if (TT.isOSNaCl() || ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16) 172 Ret += "-S128"; 173 else if (ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS) 174 Ret += "-S64"; 175 else 176 Ret += "-S32"; 177 178 return Ret; 179 } 180 181 static Reloc::Model getEffectiveRelocModel(const Triple &TT, 182 Optional<Reloc::Model> RM) { 183 if (!RM.hasValue()) 184 // Default relocation model on Darwin is PIC. 185 return TT.isOSBinFormatMachO() ? Reloc::PIC_ : Reloc::Static; 186 187 if (*RM == Reloc::ROPI || *RM == Reloc::RWPI || *RM == Reloc::ROPI_RWPI) 188 assert(TT.isOSBinFormatELF() && 189 "ROPI/RWPI currently only supported for ELF"); 190 191 // DynamicNoPIC is only used on darwin. 192 if (*RM == Reloc::DynamicNoPIC && !TT.isOSDarwin()) 193 return Reloc::Static; 194 195 return *RM; 196 } 197 198 /// Create an ARM architecture model. 199 /// 200 ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T, const Triple &TT, 201 StringRef CPU, StringRef FS, 202 const TargetOptions &Options, 203 Optional<Reloc::Model> RM, 204 CodeModel::Model CM, 205 CodeGenOpt::Level OL, bool isLittle) 206 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT, 207 CPU, FS, Options, getEffectiveRelocModel(TT, RM), CM, 208 OL), 209 TargetABI(computeTargetABI(TT, CPU, Options)), 210 TLOF(createTLOF(getTargetTriple())), 211 Subtarget(TT, CPU, FS, *this, isLittle), isLittle(isLittle) { 212 213 // Default to triple-appropriate float ABI 214 if (Options.FloatABIType == FloatABI::Default) 215 this->Options.FloatABIType = 216 Subtarget.isTargetHardFloat() ? FloatABI::Hard : FloatABI::Soft; 217 218 // Default to triple-appropriate EABI 219 if (Options.EABIVersion == EABI::Default || 220 Options.EABIVersion == EABI::Unknown) { 221 // musl is compatible with glibc with regard to EABI version 222 if (Subtarget.isTargetGNUAEABI() || Subtarget.isTargetMuslAEABI()) 223 this->Options.EABIVersion = EABI::GNU; 224 else 225 this->Options.EABIVersion = EABI::EABI5; 226 } 227 } 228 229 ARMBaseTargetMachine::~ARMBaseTargetMachine() {} 230 231 const ARMSubtarget * 232 ARMBaseTargetMachine::getSubtargetImpl(const Function &F) const { 233 Attribute CPUAttr = F.getFnAttribute("target-cpu"); 234 Attribute FSAttr = F.getFnAttribute("target-features"); 235 236 std::string CPU = !CPUAttr.hasAttribute(Attribute::None) 237 ? CPUAttr.getValueAsString().str() 238 : TargetCPU; 239 std::string FS = !FSAttr.hasAttribute(Attribute::None) 240 ? FSAttr.getValueAsString().str() 241 : TargetFS; 242 243 // FIXME: This is related to the code below to reset the target options, 244 // we need to know whether or not the soft float flag is set on the 245 // function before we can generate a subtarget. We also need to use 246 // it as a key for the subtarget since that can be the only difference 247 // between two functions. 248 bool SoftFloat = 249 F.getFnAttribute("use-soft-float").getValueAsString() == "true"; 250 // If the soft float attribute is set on the function turn on the soft float 251 // subtarget feature. 252 if (SoftFloat) 253 FS += FS.empty() ? "+soft-float" : ",+soft-float"; 254 255 auto &I = SubtargetMap[CPU + FS]; 256 if (!I) { 257 // This needs to be done before we create a new subtarget since any 258 // creation will depend on the TM and the code generation flags on the 259 // function that reside in TargetOptions. 260 resetTargetOptions(F); 261 I = llvm::make_unique<ARMSubtarget>(TargetTriple, CPU, FS, *this, isLittle); 262 } 263 return I.get(); 264 } 265 266 TargetIRAnalysis ARMBaseTargetMachine::getTargetIRAnalysis() { 267 return TargetIRAnalysis([this](const Function &F) { 268 return TargetTransformInfo(ARMTTIImpl(this, F)); 269 }); 270 } 271 272 void ARMTargetMachine::anchor() {} 273 274 ARMTargetMachine::ARMTargetMachine(const Target &T, const Triple &TT, 275 StringRef CPU, StringRef FS, 276 const TargetOptions &Options, 277 Optional<Reloc::Model> RM, 278 CodeModel::Model CM, CodeGenOpt::Level OL, 279 bool isLittle) 280 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, isLittle) { 281 initAsmInfo(); 282 if (!Subtarget.hasARMOps()) 283 report_fatal_error("CPU: '" + Subtarget.getCPUString() + "' does not " 284 "support ARM mode execution!"); 285 } 286 287 void ARMLETargetMachine::anchor() {} 288 289 ARMLETargetMachine::ARMLETargetMachine(const Target &T, const Triple &TT, 290 StringRef CPU, StringRef FS, 291 const TargetOptions &Options, 292 Optional<Reloc::Model> RM, 293 CodeModel::Model CM, 294 CodeGenOpt::Level OL) 295 : ARMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {} 296 297 void ARMBETargetMachine::anchor() {} 298 299 ARMBETargetMachine::ARMBETargetMachine(const Target &T, const Triple &TT, 300 StringRef CPU, StringRef FS, 301 const TargetOptions &Options, 302 Optional<Reloc::Model> RM, 303 CodeModel::Model CM, 304 CodeGenOpt::Level OL) 305 : ARMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {} 306 307 void ThumbTargetMachine::anchor() {} 308 309 ThumbTargetMachine::ThumbTargetMachine(const Target &T, const Triple &TT, 310 StringRef CPU, StringRef FS, 311 const TargetOptions &Options, 312 Optional<Reloc::Model> RM, 313 CodeModel::Model CM, 314 CodeGenOpt::Level OL, bool isLittle) 315 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, isLittle) { 316 initAsmInfo(); 317 } 318 319 void ThumbLETargetMachine::anchor() {} 320 321 ThumbLETargetMachine::ThumbLETargetMachine(const Target &T, const Triple &TT, 322 StringRef CPU, StringRef FS, 323 const TargetOptions &Options, 324 Optional<Reloc::Model> RM, 325 CodeModel::Model CM, 326 CodeGenOpt::Level OL) 327 : ThumbTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {} 328 329 void ThumbBETargetMachine::anchor() {} 330 331 ThumbBETargetMachine::ThumbBETargetMachine(const Target &T, const Triple &TT, 332 StringRef CPU, StringRef FS, 333 const TargetOptions &Options, 334 Optional<Reloc::Model> RM, 335 CodeModel::Model CM, 336 CodeGenOpt::Level OL) 337 : ThumbTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {} 338 339 namespace { 340 /// ARM Code Generator Pass Configuration Options. 341 class ARMPassConfig : public TargetPassConfig { 342 public: 343 ARMPassConfig(ARMBaseTargetMachine *TM, PassManagerBase &PM) 344 : TargetPassConfig(TM, PM) {} 345 346 ARMBaseTargetMachine &getARMTargetMachine() const { 347 return getTM<ARMBaseTargetMachine>(); 348 } 349 350 void addIRPasses() override; 351 bool addPreISel() override; 352 bool addInstSelector() override; 353 void addPreRegAlloc() override; 354 void addPreSched2() override; 355 void addPreEmitPass() override; 356 }; 357 } // namespace 358 359 TargetPassConfig *ARMBaseTargetMachine::createPassConfig(PassManagerBase &PM) { 360 return new ARMPassConfig(this, PM); 361 } 362 363 void ARMPassConfig::addIRPasses() { 364 if (TM->Options.ThreadModel == ThreadModel::Single) 365 addPass(createLowerAtomicPass()); 366 else 367 addPass(createAtomicExpandPass(TM)); 368 369 // Cmpxchg instructions are often used with a subsequent comparison to 370 // determine whether it succeeded. We can exploit existing control-flow in 371 // ldrex/strex loops to simplify this, but it needs tidying up. 372 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy) 373 addPass(createCFGSimplificationPass(-1, [this](const Function &F) { 374 const auto &ST = this->TM->getSubtarget<ARMSubtarget>(F); 375 return ST.hasAnyDataBarrier() && !ST.isThumb1Only(); 376 })); 377 378 TargetPassConfig::addIRPasses(); 379 380 // Match interleaved memory accesses to ldN/stN intrinsics. 381 if (TM->getOptLevel() != CodeGenOpt::None) 382 addPass(createInterleavedAccessPass(TM)); 383 } 384 385 bool ARMPassConfig::addPreISel() { 386 if ((TM->getOptLevel() != CodeGenOpt::None && 387 EnableGlobalMerge == cl::BOU_UNSET) || 388 EnableGlobalMerge == cl::BOU_TRUE) { 389 // FIXME: This is using the thumb1 only constant value for 390 // maximal global offset for merging globals. We may want 391 // to look into using the old value for non-thumb1 code of 392 // 4095 based on the TargetMachine, but this starts to become 393 // tricky when doing code gen per function. 394 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) && 395 (EnableGlobalMerge == cl::BOU_UNSET); 396 // Merging of extern globals is enabled by default on non-Mach-O as we 397 // expect it to be generally either beneficial or harmless. On Mach-O it 398 // is disabled as we emit the .subsections_via_symbols directive which 399 // means that merging extern globals is not safe. 400 bool MergeExternalByDefault = !TM->getTargetTriple().isOSBinFormatMachO(); 401 addPass(createGlobalMergePass(TM, 127, OnlyOptimizeForSize, 402 MergeExternalByDefault)); 403 } 404 405 return false; 406 } 407 408 bool ARMPassConfig::addInstSelector() { 409 addPass(createARMISelDag(getARMTargetMachine(), getOptLevel())); 410 return false; 411 } 412 413 void ARMPassConfig::addPreRegAlloc() { 414 if (getOptLevel() != CodeGenOpt::None) { 415 addPass(createMLxExpansionPass()); 416 417 if (EnableARMLoadStoreOpt) 418 addPass(createARMLoadStoreOptimizationPass(/* pre-register alloc */ true)); 419 420 if (!DisableA15SDOptimization) 421 addPass(createA15SDOptimizerPass()); 422 } 423 } 424 425 void ARMPassConfig::addPreSched2() { 426 if (getOptLevel() != CodeGenOpt::None) { 427 if (EnableARMLoadStoreOpt) 428 addPass(createARMLoadStoreOptimizationPass()); 429 430 addPass(createExecutionDependencyFixPass(&ARM::DPRRegClass)); 431 } 432 433 // Expand some pseudo instructions into multiple instructions to allow 434 // proper scheduling. 435 addPass(createARMExpandPseudoPass()); 436 437 if (getOptLevel() != CodeGenOpt::None) { 438 // in v8, IfConversion depends on Thumb instruction widths 439 addPass(createThumb2SizeReductionPass([this](const Function &F) { 440 return this->TM->getSubtarget<ARMSubtarget>(F).restrictIT(); 441 })); 442 443 addPass(createIfConverter([this](const Function &F) { 444 return !this->TM->getSubtarget<ARMSubtarget>(F).isThumb1Only(); 445 })); 446 } 447 addPass(createThumb2ITBlockPass()); 448 } 449 450 void ARMPassConfig::addPreEmitPass() { 451 addPass(createThumb2SizeReductionPass()); 452 453 // Constant island pass work on unbundled instructions. 454 addPass(createUnpackMachineBundles([this](const Function &F) { 455 return this->TM->getSubtarget<ARMSubtarget>(F).isThumb2(); 456 })); 457 458 // Don't optimize barriers at -O0. 459 if (getOptLevel() != CodeGenOpt::None) 460 addPass(createARMOptimizeBarriersPass()); 461 462 addPass(createARMConstantIslandPass()); 463 } 464