1 //===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===// 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 // This file defines the X86 specific subclass of TargetMachine. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "X86TargetMachine.h" 15 #include "MCTargetDesc/X86MCTargetDesc.h" 16 #include "X86.h" 17 #include "X86CallLowering.h" 18 #include "X86LegalizerInfo.h" 19 #include "X86MacroFusion.h" 20 #include "X86Subtarget.h" 21 #include "X86TargetObjectFile.h" 22 #include "X86TargetTransformInfo.h" 23 #include "llvm/ADT/Optional.h" 24 #include "llvm/ADT/STLExtras.h" 25 #include "llvm/ADT/SmallString.h" 26 #include "llvm/ADT/StringRef.h" 27 #include "llvm/ADT/Triple.h" 28 #include "llvm/Analysis/TargetTransformInfo.h" 29 #include "llvm/CodeGen/ExecutionDomainFix.h" 30 #include "llvm/CodeGen/GlobalISel/CallLowering.h" 31 #include "llvm/CodeGen/GlobalISel/IRTranslator.h" 32 #include "llvm/CodeGen/GlobalISel/InstructionSelect.h" 33 #include "llvm/CodeGen/GlobalISel/Legalizer.h" 34 #include "llvm/CodeGen/GlobalISel/RegBankSelect.h" 35 #include "llvm/CodeGen/MachineScheduler.h" 36 #include "llvm/CodeGen/Passes.h" 37 #include "llvm/CodeGen/TargetPassConfig.h" 38 #include "llvm/IR/Attributes.h" 39 #include "llvm/IR/DataLayout.h" 40 #include "llvm/IR/Function.h" 41 #include "llvm/Pass.h" 42 #include "llvm/Support/CodeGen.h" 43 #include "llvm/Support/CommandLine.h" 44 #include "llvm/Support/ErrorHandling.h" 45 #include "llvm/Support/TargetRegistry.h" 46 #include "llvm/Target/TargetLoweringObjectFile.h" 47 #include "llvm/Target/TargetOptions.h" 48 #include <memory> 49 #include <string> 50 51 using namespace llvm; 52 53 static cl::opt<bool> EnableMachineCombinerPass("x86-machine-combiner", 54 cl::desc("Enable the machine combiner pass"), 55 cl::init(true), cl::Hidden); 56 57 namespace llvm { 58 59 void initializeWinEHStatePassPass(PassRegistry &); 60 void initializeFixupLEAPassPass(PassRegistry &); 61 void initializeShadowCallStackPass(PassRegistry &); 62 void initializeX86CallFrameOptimizationPass(PassRegistry &); 63 void initializeX86CmovConverterPassPass(PassRegistry &); 64 void initializeX86ExecutionDomainFixPass(PassRegistry &); 65 void initializeX86DomainReassignmentPass(PassRegistry &); 66 void initializeX86AvoidSFBPassPass(PassRegistry &); 67 void initializeX86FlagsCopyLoweringPassPass(PassRegistry &); 68 69 } // end namespace llvm 70 71 extern "C" void LLVMInitializeX86Target() { 72 // Register the target. 73 RegisterTargetMachine<X86TargetMachine> X(getTheX86_32Target()); 74 RegisterTargetMachine<X86TargetMachine> Y(getTheX86_64Target()); 75 76 PassRegistry &PR = *PassRegistry::getPassRegistry(); 77 initializeGlobalISel(PR); 78 initializeWinEHStatePassPass(PR); 79 initializeFixupBWInstPassPass(PR); 80 initializeEvexToVexInstPassPass(PR); 81 initializeFixupLEAPassPass(PR); 82 initializeShadowCallStackPass(PR); 83 initializeX86CallFrameOptimizationPass(PR); 84 initializeX86CmovConverterPassPass(PR); 85 initializeX86ExecutionDomainFixPass(PR); 86 initializeX86DomainReassignmentPass(PR); 87 initializeX86AvoidSFBPassPass(PR); 88 initializeX86FlagsCopyLoweringPassPass(PR); 89 } 90 91 static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) { 92 if (TT.isOSBinFormatMachO()) { 93 if (TT.getArch() == Triple::x86_64) 94 return llvm::make_unique<X86_64MachoTargetObjectFile>(); 95 return llvm::make_unique<TargetLoweringObjectFileMachO>(); 96 } 97 98 if (TT.isOSFreeBSD()) 99 return llvm::make_unique<X86FreeBSDTargetObjectFile>(); 100 if (TT.isOSLinux() || TT.isOSNaCl() || TT.isOSIAMCU()) 101 return llvm::make_unique<X86LinuxNaClTargetObjectFile>(); 102 if (TT.isOSSolaris()) 103 return llvm::make_unique<X86SolarisTargetObjectFile>(); 104 if (TT.isOSFuchsia()) 105 return llvm::make_unique<X86FuchsiaTargetObjectFile>(); 106 if (TT.isOSBinFormatELF()) 107 return llvm::make_unique<X86ELFTargetObjectFile>(); 108 if (TT.isKnownWindowsMSVCEnvironment() || TT.isWindowsCoreCLREnvironment()) 109 return llvm::make_unique<X86WindowsTargetObjectFile>(); 110 if (TT.isOSBinFormatCOFF()) 111 return llvm::make_unique<TargetLoweringObjectFileCOFF>(); 112 llvm_unreachable("unknown subtarget type"); 113 } 114 115 static std::string computeDataLayout(const Triple &TT) { 116 // X86 is little endian 117 std::string Ret = "e"; 118 119 Ret += DataLayout::getManglingComponent(TT); 120 // X86 and x32 have 32 bit pointers. 121 if ((TT.isArch64Bit() && 122 (TT.getEnvironment() == Triple::GNUX32 || TT.isOSNaCl())) || 123 !TT.isArch64Bit()) 124 Ret += "-p:32:32"; 125 126 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32. 127 if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl()) 128 Ret += "-i64:64"; 129 else if (TT.isOSIAMCU()) 130 Ret += "-i64:32-f64:32"; 131 else 132 Ret += "-f64:32:64"; 133 134 // Some ABIs align long double to 128 bits, others to 32. 135 if (TT.isOSNaCl() || TT.isOSIAMCU()) 136 ; // No f80 137 else if (TT.isArch64Bit() || TT.isOSDarwin()) 138 Ret += "-f80:128"; 139 else 140 Ret += "-f80:32"; 141 142 if (TT.isOSIAMCU()) 143 Ret += "-f128:32"; 144 145 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits. 146 if (TT.isArch64Bit()) 147 Ret += "-n8:16:32:64"; 148 else 149 Ret += "-n8:16:32"; 150 151 // The stack is aligned to 32 bits on some ABIs and 128 bits on others. 152 if ((!TT.isArch64Bit() && TT.isOSWindows()) || TT.isOSIAMCU()) 153 Ret += "-a:0:32-S32"; 154 else 155 Ret += "-S128"; 156 157 return Ret; 158 } 159 160 static Reloc::Model getEffectiveRelocModel(const Triple &TT, 161 Optional<Reloc::Model> RM) { 162 bool is64Bit = TT.getArch() == Triple::x86_64; 163 if (!RM.hasValue()) { 164 // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode. 165 // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we 166 // use static relocation model by default. 167 if (TT.isOSDarwin()) { 168 if (is64Bit) 169 return Reloc::PIC_; 170 return Reloc::DynamicNoPIC; 171 } 172 if (TT.isOSWindows() && is64Bit) 173 return Reloc::PIC_; 174 return Reloc::Static; 175 } 176 177 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC 178 // is defined as a model for code which may be used in static or dynamic 179 // executables but not necessarily a shared library. On X86-32 we just 180 // compile in -static mode, in x86-64 we use PIC. 181 if (*RM == Reloc::DynamicNoPIC) { 182 if (is64Bit) 183 return Reloc::PIC_; 184 if (!TT.isOSDarwin()) 185 return Reloc::Static; 186 } 187 188 // If we are on Darwin, disallow static relocation model in X86-64 mode, since 189 // the Mach-O file format doesn't support it. 190 if (*RM == Reloc::Static && TT.isOSDarwin() && is64Bit) 191 return Reloc::PIC_; 192 193 return *RM; 194 } 195 196 static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM, 197 bool JIT, bool Is64Bit) { 198 if (CM) 199 return *CM; 200 if (JIT) 201 return Is64Bit ? CodeModel::Large : CodeModel::Small; 202 return CodeModel::Small; 203 } 204 205 /// Create an X86 target. 206 /// 207 X86TargetMachine::X86TargetMachine(const Target &T, const Triple &TT, 208 StringRef CPU, StringRef FS, 209 const TargetOptions &Options, 210 Optional<Reloc::Model> RM, 211 Optional<CodeModel::Model> CM, 212 CodeGenOpt::Level OL, bool JIT) 213 : LLVMTargetMachine( 214 T, computeDataLayout(TT), TT, CPU, FS, Options, 215 getEffectiveRelocModel(TT, RM), 216 getEffectiveCodeModel(CM, JIT, TT.getArch() == Triple::x86_64), OL), 217 TLOF(createTLOF(getTargetTriple())) { 218 // Windows stack unwinder gets confused when execution flow "falls through" 219 // after a call to 'noreturn' function. 220 // To prevent that, we emit a trap for 'unreachable' IR instructions. 221 // (which on X86, happens to be the 'ud2' instruction) 222 // On PS4, the "return address" of a 'noreturn' call must still be within 223 // the calling function, and TrapUnreachable is an easy way to get that. 224 // The check here for 64-bit windows is a bit icky, but as we're unlikely 225 // to ever want to mix 32 and 64-bit windows code in a single module 226 // this should be fine. 227 if ((TT.isOSWindows() && TT.getArch() == Triple::x86_64) || TT.isPS4() || 228 TT.isOSBinFormatMachO()) 229 this->Options.TrapUnreachable = true; 230 231 initAsmInfo(); 232 } 233 234 X86TargetMachine::~X86TargetMachine() = default; 235 236 const X86Subtarget * 237 X86TargetMachine::getSubtargetImpl(const Function &F) const { 238 Attribute CPUAttr = F.getFnAttribute("target-cpu"); 239 Attribute FSAttr = F.getFnAttribute("target-features"); 240 241 StringRef CPU = !CPUAttr.hasAttribute(Attribute::None) 242 ? CPUAttr.getValueAsString() 243 : (StringRef)TargetCPU; 244 StringRef FS = !FSAttr.hasAttribute(Attribute::None) 245 ? FSAttr.getValueAsString() 246 : (StringRef)TargetFS; 247 248 SmallString<512> Key; 249 Key.reserve(CPU.size() + FS.size()); 250 Key += CPU; 251 Key += FS; 252 253 // FIXME: This is related to the code below to reset the target options, 254 // we need to know whether or not the soft float flag is set on the 255 // function before we can generate a subtarget. We also need to use 256 // it as a key for the subtarget since that can be the only difference 257 // between two functions. 258 bool SoftFloat = 259 F.getFnAttribute("use-soft-float").getValueAsString() == "true"; 260 // If the soft float attribute is set on the function turn on the soft float 261 // subtarget feature. 262 if (SoftFloat) 263 Key += FS.empty() ? "+soft-float" : ",+soft-float"; 264 265 // Keep track of the key width after all features are added so we can extract 266 // the feature string out later. 267 unsigned CPUFSWidth = Key.size(); 268 269 // Extract prefer-vector-width attribute. 270 unsigned PreferVectorWidthOverride = 0; 271 if (F.hasFnAttribute("prefer-vector-width")) { 272 StringRef Val = F.getFnAttribute("prefer-vector-width").getValueAsString(); 273 unsigned Width; 274 if (!Val.getAsInteger(0, Width)) { 275 Key += ",prefer-vector-width="; 276 Key += Val; 277 PreferVectorWidthOverride = Width; 278 } 279 } 280 281 // Extract required-vector-width attribute. 282 unsigned RequiredVectorWidth = UINT32_MAX; 283 if (F.hasFnAttribute("required-vector-width")) { 284 StringRef Val = F.getFnAttribute("required-vector-width").getValueAsString(); 285 unsigned Width; 286 if (!Val.getAsInteger(0, Width)) { 287 Key += ",required-vector-width="; 288 Key += Val; 289 RequiredVectorWidth = Width; 290 } 291 } 292 293 // Extracted here so that we make sure there is backing for the StringRef. If 294 // we assigned earlier, its possible the SmallString reallocated leaving a 295 // dangling StringRef. 296 FS = Key.slice(CPU.size(), CPUFSWidth); 297 298 auto &I = SubtargetMap[Key]; 299 if (!I) { 300 // This needs to be done before we create a new subtarget since any 301 // creation will depend on the TM and the code generation flags on the 302 // function that reside in TargetOptions. 303 resetTargetOptions(F); 304 I = llvm::make_unique<X86Subtarget>(TargetTriple, CPU, FS, *this, 305 Options.StackAlignmentOverride, 306 PreferVectorWidthOverride, 307 RequiredVectorWidth); 308 } 309 return I.get(); 310 } 311 312 //===----------------------------------------------------------------------===// 313 // Command line options for x86 314 //===----------------------------------------------------------------------===// 315 static cl::opt<bool> 316 UseVZeroUpper("x86-use-vzeroupper", cl::Hidden, 317 cl::desc("Minimize AVX to SSE transition penalty"), 318 cl::init(true)); 319 320 //===----------------------------------------------------------------------===// 321 // X86 TTI query. 322 //===----------------------------------------------------------------------===// 323 324 TargetTransformInfo 325 X86TargetMachine::getTargetTransformInfo(const Function &F) { 326 return TargetTransformInfo(X86TTIImpl(this, F)); 327 } 328 329 //===----------------------------------------------------------------------===// 330 // Pass Pipeline Configuration 331 //===----------------------------------------------------------------------===// 332 333 namespace { 334 335 /// X86 Code Generator Pass Configuration Options. 336 class X86PassConfig : public TargetPassConfig { 337 public: 338 X86PassConfig(X86TargetMachine &TM, PassManagerBase &PM) 339 : TargetPassConfig(TM, PM) {} 340 341 X86TargetMachine &getX86TargetMachine() const { 342 return getTM<X86TargetMachine>(); 343 } 344 345 ScheduleDAGInstrs * 346 createMachineScheduler(MachineSchedContext *C) const override { 347 ScheduleDAGMILive *DAG = createGenericSchedLive(C); 348 DAG->addMutation(createX86MacroFusionDAGMutation()); 349 return DAG; 350 } 351 352 void addIRPasses() override; 353 bool addInstSelector() override; 354 bool addIRTranslator() override; 355 bool addLegalizeMachineIR() override; 356 bool addRegBankSelect() override; 357 bool addGlobalInstructionSelect() override; 358 bool addILPOpts() override; 359 bool addPreISel() override; 360 void addMachineSSAOptimization() override; 361 void addPreRegAlloc() override; 362 void addPostRegAlloc() override; 363 void addPreEmitPass() override; 364 void addPreEmitPass2() override; 365 void addPreSched2() override; 366 }; 367 368 class X86ExecutionDomainFix : public ExecutionDomainFix { 369 public: 370 static char ID; 371 X86ExecutionDomainFix() : ExecutionDomainFix(ID, X86::VR128XRegClass) {} 372 StringRef getPassName() const override { 373 return "X86 Execution Dependency Fix"; 374 } 375 }; 376 char X86ExecutionDomainFix::ID; 377 378 } // end anonymous namespace 379 380 INITIALIZE_PASS_BEGIN(X86ExecutionDomainFix, "x86-execution-domain-fix", 381 "X86 Execution Domain Fix", false, false) 382 INITIALIZE_PASS_DEPENDENCY(ReachingDefAnalysis) 383 INITIALIZE_PASS_END(X86ExecutionDomainFix, "x86-execution-domain-fix", 384 "X86 Execution Domain Fix", false, false) 385 386 TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) { 387 return new X86PassConfig(*this, PM); 388 } 389 390 void X86PassConfig::addIRPasses() { 391 addPass(createAtomicExpandPass()); 392 393 TargetPassConfig::addIRPasses(); 394 395 if (TM->getOptLevel() != CodeGenOpt::None) 396 addPass(createInterleavedAccessPass()); 397 398 // Add passes that handle indirect branch removal and insertion of a retpoline 399 // thunk. These will be a no-op unless a function subtarget has the retpoline 400 // feature enabled. 401 addPass(createIndirectBrExpandPass()); 402 } 403 404 bool X86PassConfig::addInstSelector() { 405 // Install an instruction selector. 406 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel())); 407 408 // For ELF, cleanup any local-dynamic TLS accesses. 409 if (TM->getTargetTriple().isOSBinFormatELF() && 410 getOptLevel() != CodeGenOpt::None) 411 addPass(createCleanupLocalDynamicTLSPass()); 412 413 addPass(createX86GlobalBaseRegPass()); 414 return false; 415 } 416 417 bool X86PassConfig::addIRTranslator() { 418 addPass(new IRTranslator()); 419 return false; 420 } 421 422 bool X86PassConfig::addLegalizeMachineIR() { 423 addPass(new Legalizer()); 424 return false; 425 } 426 427 bool X86PassConfig::addRegBankSelect() { 428 addPass(new RegBankSelect()); 429 return false; 430 } 431 432 bool X86PassConfig::addGlobalInstructionSelect() { 433 addPass(new InstructionSelect()); 434 return false; 435 } 436 437 bool X86PassConfig::addILPOpts() { 438 addPass(&EarlyIfConverterID); 439 if (EnableMachineCombinerPass) 440 addPass(&MachineCombinerID); 441 addPass(createX86CmovConverterPass()); 442 return true; 443 } 444 445 bool X86PassConfig::addPreISel() { 446 // Only add this pass for 32-bit x86 Windows. 447 const Triple &TT = TM->getTargetTriple(); 448 if (TT.isOSWindows() && TT.getArch() == Triple::x86) 449 addPass(createX86WinEHStatePass()); 450 return true; 451 } 452 453 void X86PassConfig::addPreRegAlloc() { 454 if (getOptLevel() != CodeGenOpt::None) { 455 addPass(&LiveRangeShrinkID); 456 addPass(createX86FixupSetCC()); 457 addPass(createX86OptimizeLEAs()); 458 addPass(createX86CallFrameOptimization()); 459 addPass(createX86AvoidStoreForwardingBlocks()); 460 } 461 462 addPass(createX86FlagsCopyLoweringPass()); 463 addPass(createX86WinAllocaExpander()); 464 } 465 void X86PassConfig::addMachineSSAOptimization() { 466 addPass(createX86DomainReassignmentPass()); 467 TargetPassConfig::addMachineSSAOptimization(); 468 } 469 470 void X86PassConfig::addPostRegAlloc() { 471 addPass(createX86FloatingPointStackifierPass()); 472 } 473 474 void X86PassConfig::addPreSched2() { addPass(createX86ExpandPseudoPass()); } 475 476 void X86PassConfig::addPreEmitPass() { 477 if (getOptLevel() != CodeGenOpt::None) { 478 addPass(new X86ExecutionDomainFix()); 479 addPass(createBreakFalseDeps()); 480 } 481 482 addPass(createShadowCallStackPass()); 483 addPass(createX86IndirectBranchTrackingPass()); 484 485 if (UseVZeroUpper) 486 addPass(createX86IssueVZeroUpperPass()); 487 488 if (getOptLevel() != CodeGenOpt::None) { 489 addPass(createX86FixupBWInsts()); 490 addPass(createX86PadShortFunctions()); 491 addPass(createX86FixupLEAs()); 492 addPass(createX86EvexToVexInsts()); 493 } 494 } 495 496 void X86PassConfig::addPreEmitPass2() { 497 addPass(createX86RetpolineThunksPass()); 498 } 499