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