1 //===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===// 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 // This file defines the X86 specific subclass of TargetMachine. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "X86TargetMachine.h" 14 #include "MCTargetDesc/X86MCTargetDesc.h" 15 #include "TargetInfo/X86TargetInfo.h" 16 #include "X86.h" 17 #include "X86MachineFunctionInfo.h" 18 #include "X86MacroFusion.h" 19 #include "X86Subtarget.h" 20 #include "X86TargetObjectFile.h" 21 #include "X86TargetTransformInfo.h" 22 #include "llvm/ADT/STLExtras.h" 23 #include "llvm/ADT/SmallString.h" 24 #include "llvm/ADT/StringRef.h" 25 #include "llvm/Analysis/TargetTransformInfo.h" 26 #include "llvm/CodeGen/ExecutionDomainFix.h" 27 #include "llvm/CodeGen/GlobalISel/CSEInfo.h" 28 #include "llvm/CodeGen/GlobalISel/CallLowering.h" 29 #include "llvm/CodeGen/GlobalISel/IRTranslator.h" 30 #include "llvm/CodeGen/GlobalISel/InstructionSelect.h" 31 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h" 32 #include "llvm/CodeGen/GlobalISel/Legalizer.h" 33 #include "llvm/CodeGen/GlobalISel/RegBankSelect.h" 34 #include "llvm/CodeGen/MachineScheduler.h" 35 #include "llvm/CodeGen/Passes.h" 36 #include "llvm/CodeGen/RegAllocRegistry.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/MC/MCAsmInfo.h" 42 #include "llvm/MC/TargetRegistry.h" 43 #include "llvm/Pass.h" 44 #include "llvm/Support/CodeGen.h" 45 #include "llvm/Support/CommandLine.h" 46 #include "llvm/Support/ErrorHandling.h" 47 #include "llvm/Target/TargetLoweringObjectFile.h" 48 #include "llvm/Target/TargetOptions.h" 49 #include "llvm/TargetParser/Triple.h" 50 #include "llvm/Transforms/CFGuard.h" 51 #include <memory> 52 #include <optional> 53 #include <string> 54 55 using namespace llvm; 56 57 static cl::opt<bool> EnableMachineCombinerPass("x86-machine-combiner", 58 cl::desc("Enable the machine combiner pass"), 59 cl::init(true), cl::Hidden); 60 61 static cl::opt<bool> 62 EnableTileRAPass("x86-tile-ra", 63 cl::desc("Enable the tile register allocation pass"), 64 cl::init(true), cl::Hidden); 65 66 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeX86Target() { 67 // Register the target. 68 RegisterTargetMachine<X86TargetMachine> X(getTheX86_32Target()); 69 RegisterTargetMachine<X86TargetMachine> Y(getTheX86_64Target()); 70 71 PassRegistry &PR = *PassRegistry::getPassRegistry(); 72 initializeX86LowerAMXIntrinsicsLegacyPassPass(PR); 73 initializeX86LowerAMXTypeLegacyPassPass(PR); 74 initializeX86PreAMXConfigPassPass(PR); 75 initializeX86PreTileConfigPass(PR); 76 initializeGlobalISel(PR); 77 initializeWinEHStatePassPass(PR); 78 initializeFixupBWInstPassPass(PR); 79 initializeEvexToVexInstPassPass(PR); 80 initializeFixupLEAPassPass(PR); 81 initializeFPSPass(PR); 82 initializeX86FixupSetCCPassPass(PR); 83 initializeX86CallFrameOptimizationPass(PR); 84 initializeX86CmovConverterPassPass(PR); 85 initializeX86TileConfigPass(PR); 86 initializeX86FastPreTileConfigPass(PR); 87 initializeX86FastTileConfigPass(PR); 88 initializeKCFIPass(PR); 89 initializeX86LowerTileCopyPass(PR); 90 initializeX86ExpandPseudoPass(PR); 91 initializeX86ExecutionDomainFixPass(PR); 92 initializeX86DomainReassignmentPass(PR); 93 initializeX86AvoidSFBPassPass(PR); 94 initializeX86AvoidTrailingCallPassPass(PR); 95 initializeX86SpeculativeLoadHardeningPassPass(PR); 96 initializeX86SpeculativeExecutionSideEffectSuppressionPass(PR); 97 initializeX86FlagsCopyLoweringPassPass(PR); 98 initializeX86LoadValueInjectionLoadHardeningPassPass(PR); 99 initializeX86LoadValueInjectionRetHardeningPassPass(PR); 100 initializeX86OptimizeLEAPassPass(PR); 101 initializeX86PartialReductionPass(PR); 102 initializePseudoProbeInserterPass(PR); 103 initializeX86ReturnThunksPass(PR); 104 initializeX86DAGToDAGISelPass(PR); 105 initializeX86ArgumentStackSlotPassPass(PR); 106 } 107 108 static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) { 109 if (TT.isOSBinFormatMachO()) { 110 if (TT.getArch() == Triple::x86_64) 111 return std::make_unique<X86_64MachoTargetObjectFile>(); 112 return std::make_unique<TargetLoweringObjectFileMachO>(); 113 } 114 115 if (TT.isOSBinFormatCOFF()) 116 return std::make_unique<TargetLoweringObjectFileCOFF>(); 117 return std::make_unique<X86ELFTargetObjectFile>(); 118 } 119 120 static std::string computeDataLayout(const Triple &TT) { 121 // X86 is little endian 122 std::string Ret = "e"; 123 124 Ret += DataLayout::getManglingComponent(TT); 125 // X86 and x32 have 32 bit pointers. 126 if (!TT.isArch64Bit() || TT.isX32() || TT.isOSNaCl()) 127 Ret += "-p:32:32"; 128 129 // Address spaces for 32 bit signed, 32 bit unsigned, and 64 bit pointers. 130 Ret += "-p270:32:32-p271:32:32-p272:64:64"; 131 132 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32. 133 if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl()) 134 Ret += "-i64:64"; 135 else if (TT.isOSIAMCU()) 136 Ret += "-i64:32-f64:32"; 137 else 138 Ret += "-f64:32:64"; 139 140 // Some ABIs align long double to 128 bits, others to 32. 141 if (TT.isOSNaCl() || TT.isOSIAMCU()) 142 ; // No f80 143 else if (TT.isArch64Bit() || TT.isOSDarwin() || TT.isWindowsMSVCEnvironment()) 144 Ret += "-f80:128"; 145 else 146 Ret += "-f80:32"; 147 148 if (TT.isOSIAMCU()) 149 Ret += "-f128:32"; 150 151 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits. 152 if (TT.isArch64Bit()) 153 Ret += "-n8:16:32:64"; 154 else 155 Ret += "-n8:16:32"; 156 157 // The stack is aligned to 32 bits on some ABIs and 128 bits on others. 158 if ((!TT.isArch64Bit() && TT.isOSWindows()) || TT.isOSIAMCU()) 159 Ret += "-a:0:32-S32"; 160 else 161 Ret += "-S128"; 162 163 return Ret; 164 } 165 166 static Reloc::Model getEffectiveRelocModel(const Triple &TT, bool JIT, 167 std::optional<Reloc::Model> RM) { 168 bool is64Bit = TT.getArch() == Triple::x86_64; 169 if (!RM) { 170 // JIT codegen should use static relocations by default, since it's 171 // typically executed in process and not relocatable. 172 if (JIT) 173 return Reloc::Static; 174 175 // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode. 176 // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we 177 // use static relocation model by default. 178 if (TT.isOSDarwin()) { 179 if (is64Bit) 180 return Reloc::PIC_; 181 return Reloc::DynamicNoPIC; 182 } 183 if (TT.isOSWindows() && is64Bit) 184 return Reloc::PIC_; 185 return Reloc::Static; 186 } 187 188 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC 189 // is defined as a model for code which may be used in static or dynamic 190 // executables but not necessarily a shared library. On X86-32 we just 191 // compile in -static mode, in x86-64 we use PIC. 192 if (*RM == Reloc::DynamicNoPIC) { 193 if (is64Bit) 194 return Reloc::PIC_; 195 if (!TT.isOSDarwin()) 196 return Reloc::Static; 197 } 198 199 // If we are on Darwin, disallow static relocation model in X86-64 mode, since 200 // the Mach-O file format doesn't support it. 201 if (*RM == Reloc::Static && TT.isOSDarwin() && is64Bit) 202 return Reloc::PIC_; 203 204 return *RM; 205 } 206 207 static CodeModel::Model 208 getEffectiveX86CodeModel(std::optional<CodeModel::Model> CM, bool JIT, 209 bool Is64Bit) { 210 if (CM) { 211 if (*CM == CodeModel::Tiny) 212 report_fatal_error("Target does not support the tiny CodeModel", false); 213 return *CM; 214 } 215 if (JIT) 216 return Is64Bit ? CodeModel::Large : CodeModel::Small; 217 return CodeModel::Small; 218 } 219 220 /// Create an X86 target. 221 /// 222 X86TargetMachine::X86TargetMachine(const Target &T, const Triple &TT, 223 StringRef CPU, StringRef FS, 224 const TargetOptions &Options, 225 std::optional<Reloc::Model> RM, 226 std::optional<CodeModel::Model> CM, 227 CodeGenOpt::Level OL, bool JIT) 228 : LLVMTargetMachine( 229 T, computeDataLayout(TT), TT, CPU, FS, Options, 230 getEffectiveRelocModel(TT, JIT, RM), 231 getEffectiveX86CodeModel(CM, JIT, TT.getArch() == Triple::x86_64), 232 OL), 233 TLOF(createTLOF(getTargetTriple())), IsJIT(JIT) { 234 // On PS4/PS5, the "return address" of a 'noreturn' call must still be within 235 // the calling function, and TrapUnreachable is an easy way to get that. 236 if (TT.isPS() || TT.isOSBinFormatMachO()) { 237 this->Options.TrapUnreachable = true; 238 this->Options.NoTrapAfterNoreturn = TT.isOSBinFormatMachO(); 239 } 240 241 setMachineOutliner(true); 242 243 // x86 supports the debug entry values. 244 setSupportsDebugEntryValues(true); 245 246 initAsmInfo(); 247 } 248 249 X86TargetMachine::~X86TargetMachine() = default; 250 251 const X86Subtarget * 252 X86TargetMachine::getSubtargetImpl(const Function &F) const { 253 Attribute CPUAttr = F.getFnAttribute("target-cpu"); 254 Attribute TuneAttr = F.getFnAttribute("tune-cpu"); 255 Attribute FSAttr = F.getFnAttribute("target-features"); 256 257 StringRef CPU = 258 CPUAttr.isValid() ? CPUAttr.getValueAsString() : (StringRef)TargetCPU; 259 // "x86-64" is a default target setting for many front ends. In these cases, 260 // they actually request for "generic" tuning unless the "tune-cpu" was 261 // specified. 262 StringRef TuneCPU = TuneAttr.isValid() ? TuneAttr.getValueAsString() 263 : CPU == "x86-64" ? "generic" 264 : (StringRef)CPU; 265 StringRef FS = 266 FSAttr.isValid() ? FSAttr.getValueAsString() : (StringRef)TargetFS; 267 268 SmallString<512> Key; 269 // The additions here are ordered so that the definitely short strings are 270 // added first so we won't exceed the small size. We append the 271 // much longer FS string at the end so that we only heap allocate at most 272 // one time. 273 274 // Extract prefer-vector-width attribute. 275 unsigned PreferVectorWidthOverride = 0; 276 Attribute PreferVecWidthAttr = F.getFnAttribute("prefer-vector-width"); 277 if (PreferVecWidthAttr.isValid()) { 278 StringRef Val = PreferVecWidthAttr.getValueAsString(); 279 unsigned Width; 280 if (!Val.getAsInteger(0, Width)) { 281 Key += 'p'; 282 Key += Val; 283 PreferVectorWidthOverride = Width; 284 } 285 } 286 287 // Extract min-legal-vector-width attribute. 288 unsigned RequiredVectorWidth = UINT32_MAX; 289 Attribute MinLegalVecWidthAttr = F.getFnAttribute("min-legal-vector-width"); 290 if (MinLegalVecWidthAttr.isValid()) { 291 StringRef Val = MinLegalVecWidthAttr.getValueAsString(); 292 unsigned Width; 293 if (!Val.getAsInteger(0, Width)) { 294 Key += 'm'; 295 Key += Val; 296 RequiredVectorWidth = Width; 297 } 298 } 299 300 // Add CPU to the Key. 301 Key += CPU; 302 303 // Add tune CPU to the Key. 304 Key += TuneCPU; 305 306 // Keep track of the start of the feature portion of the string. 307 unsigned FSStart = Key.size(); 308 309 // FIXME: This is related to the code below to reset the target options, 310 // we need to know whether or not the soft float flag is set on the 311 // function before we can generate a subtarget. We also need to use 312 // it as a key for the subtarget since that can be the only difference 313 // between two functions. 314 bool SoftFloat = F.getFnAttribute("use-soft-float").getValueAsBool(); 315 // If the soft float attribute is set on the function turn on the soft float 316 // subtarget feature. 317 if (SoftFloat) 318 Key += FS.empty() ? "+soft-float" : "+soft-float,"; 319 320 Key += FS; 321 322 // We may have added +soft-float to the features so move the StringRef to 323 // point to the full string in the Key. 324 FS = Key.substr(FSStart); 325 326 auto &I = SubtargetMap[Key]; 327 if (!I) { 328 // This needs to be done before we create a new subtarget since any 329 // creation will depend on the TM and the code generation flags on the 330 // function that reside in TargetOptions. 331 resetTargetOptions(F); 332 I = std::make_unique<X86Subtarget>( 333 TargetTriple, CPU, TuneCPU, FS, *this, 334 MaybeAlign(F.getParent()->getOverrideStackAlignment()), 335 PreferVectorWidthOverride, RequiredVectorWidth); 336 } 337 return I.get(); 338 } 339 340 bool X86TargetMachine::isNoopAddrSpaceCast(unsigned SrcAS, 341 unsigned DestAS) const { 342 assert(SrcAS != DestAS && "Expected different address spaces!"); 343 if (getPointerSize(SrcAS) != getPointerSize(DestAS)) 344 return false; 345 return SrcAS < 256 && DestAS < 256; 346 } 347 348 //===----------------------------------------------------------------------===// 349 // X86 TTI query. 350 //===----------------------------------------------------------------------===// 351 352 TargetTransformInfo 353 X86TargetMachine::getTargetTransformInfo(const Function &F) const { 354 return TargetTransformInfo(X86TTIImpl(this, F)); 355 } 356 357 //===----------------------------------------------------------------------===// 358 // Pass Pipeline Configuration 359 //===----------------------------------------------------------------------===// 360 361 namespace { 362 363 /// X86 Code Generator Pass Configuration Options. 364 class X86PassConfig : public TargetPassConfig { 365 public: 366 X86PassConfig(X86TargetMachine &TM, PassManagerBase &PM) 367 : TargetPassConfig(TM, PM) {} 368 369 X86TargetMachine &getX86TargetMachine() const { 370 return getTM<X86TargetMachine>(); 371 } 372 373 ScheduleDAGInstrs * 374 createMachineScheduler(MachineSchedContext *C) const override { 375 ScheduleDAGMILive *DAG = createGenericSchedLive(C); 376 DAG->addMutation(createX86MacroFusionDAGMutation()); 377 return DAG; 378 } 379 380 ScheduleDAGInstrs * 381 createPostMachineScheduler(MachineSchedContext *C) const override { 382 ScheduleDAGMI *DAG = createGenericSchedPostRA(C); 383 DAG->addMutation(createX86MacroFusionDAGMutation()); 384 return DAG; 385 } 386 387 void addIRPasses() override; 388 bool addInstSelector() override; 389 bool addIRTranslator() override; 390 bool addLegalizeMachineIR() override; 391 bool addRegBankSelect() override; 392 bool addGlobalInstructionSelect() override; 393 bool addILPOpts() override; 394 bool addPreISel() override; 395 void addMachineSSAOptimization() override; 396 void addPreRegAlloc() override; 397 bool addPostFastRegAllocRewrite() override; 398 void addPostRegAlloc() override; 399 void addPreEmitPass() override; 400 void addPreEmitPass2() override; 401 void addPreSched2() override; 402 bool addRegAssignAndRewriteOptimized() override; 403 404 std::unique_ptr<CSEConfigBase> getCSEConfig() const override; 405 }; 406 407 class X86ExecutionDomainFix : public ExecutionDomainFix { 408 public: 409 static char ID; 410 X86ExecutionDomainFix() : ExecutionDomainFix(ID, X86::VR128XRegClass) {} 411 StringRef getPassName() const override { 412 return "X86 Execution Dependency Fix"; 413 } 414 }; 415 char X86ExecutionDomainFix::ID; 416 417 } // end anonymous namespace 418 419 INITIALIZE_PASS_BEGIN(X86ExecutionDomainFix, "x86-execution-domain-fix", 420 "X86 Execution Domain Fix", false, false) 421 INITIALIZE_PASS_DEPENDENCY(ReachingDefAnalysis) 422 INITIALIZE_PASS_END(X86ExecutionDomainFix, "x86-execution-domain-fix", 423 "X86 Execution Domain Fix", false, false) 424 425 TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) { 426 return new X86PassConfig(*this, PM); 427 } 428 429 MachineFunctionInfo *X86TargetMachine::createMachineFunctionInfo( 430 BumpPtrAllocator &Allocator, const Function &F, 431 const TargetSubtargetInfo *STI) const { 432 return X86MachineFunctionInfo::create<X86MachineFunctionInfo>(Allocator, F, 433 STI); 434 } 435 436 void X86PassConfig::addIRPasses() { 437 addPass(createAtomicExpandPass()); 438 439 // We add both pass anyway and when these two passes run, we skip the pass 440 // based on the option level and option attribute. 441 addPass(createX86LowerAMXIntrinsicsPass()); 442 addPass(createX86LowerAMXTypePass()); 443 444 TargetPassConfig::addIRPasses(); 445 446 if (TM->getOptLevel() != CodeGenOpt::None) { 447 addPass(createInterleavedAccessPass()); 448 addPass(createX86PartialReductionPass()); 449 } 450 451 // Add passes that handle indirect branch removal and insertion of a retpoline 452 // thunk. These will be a no-op unless a function subtarget has the retpoline 453 // feature enabled. 454 addPass(createIndirectBrExpandPass()); 455 456 // Add Control Flow Guard checks. 457 const Triple &TT = TM->getTargetTriple(); 458 if (TT.isOSWindows()) { 459 if (TT.getArch() == Triple::x86_64) { 460 addPass(createCFGuardDispatchPass()); 461 } else { 462 addPass(createCFGuardCheckPass()); 463 } 464 } 465 466 if (TM->Options.JMCInstrument) 467 addPass(createJMCInstrumenterPass()); 468 } 469 470 bool X86PassConfig::addInstSelector() { 471 // Install an instruction selector. 472 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel())); 473 474 // For ELF, cleanup any local-dynamic TLS accesses. 475 if (TM->getTargetTriple().isOSBinFormatELF() && 476 getOptLevel() != CodeGenOpt::None) 477 addPass(createCleanupLocalDynamicTLSPass()); 478 479 addPass(createX86GlobalBaseRegPass()); 480 addPass(createX86ArgumentStackSlotPass()); 481 return false; 482 } 483 484 bool X86PassConfig::addIRTranslator() { 485 addPass(new IRTranslator(getOptLevel())); 486 return false; 487 } 488 489 bool X86PassConfig::addLegalizeMachineIR() { 490 addPass(new Legalizer()); 491 return false; 492 } 493 494 bool X86PassConfig::addRegBankSelect() { 495 addPass(new RegBankSelect()); 496 return false; 497 } 498 499 bool X86PassConfig::addGlobalInstructionSelect() { 500 addPass(new InstructionSelect(getOptLevel())); 501 return false; 502 } 503 504 bool X86PassConfig::addILPOpts() { 505 addPass(&EarlyIfConverterID); 506 if (EnableMachineCombinerPass) 507 addPass(&MachineCombinerID); 508 addPass(createX86CmovConverterPass()); 509 return true; 510 } 511 512 bool X86PassConfig::addPreISel() { 513 // Only add this pass for 32-bit x86 Windows. 514 const Triple &TT = TM->getTargetTriple(); 515 if (TT.isOSWindows() && TT.getArch() == Triple::x86) 516 addPass(createX86WinEHStatePass()); 517 return true; 518 } 519 520 void X86PassConfig::addPreRegAlloc() { 521 if (getOptLevel() != CodeGenOpt::None) { 522 addPass(&LiveRangeShrinkID); 523 addPass(createX86FixupSetCC()); 524 addPass(createX86OptimizeLEAs()); 525 addPass(createX86CallFrameOptimization()); 526 addPass(createX86AvoidStoreForwardingBlocks()); 527 } 528 529 addPass(createX86SpeculativeLoadHardeningPass()); 530 addPass(createX86FlagsCopyLoweringPass()); 531 addPass(createX86DynAllocaExpander()); 532 533 if (getOptLevel() != CodeGenOpt::None) 534 addPass(createX86PreTileConfigPass()); 535 else 536 addPass(createX86FastPreTileConfigPass()); 537 } 538 539 void X86PassConfig::addMachineSSAOptimization() { 540 addPass(createX86DomainReassignmentPass()); 541 TargetPassConfig::addMachineSSAOptimization(); 542 } 543 544 void X86PassConfig::addPostRegAlloc() { 545 addPass(createX86LowerTileCopyPass()); 546 addPass(createX86FloatingPointStackifierPass()); 547 // When -O0 is enabled, the Load Value Injection Hardening pass will fall back 548 // to using the Speculative Execution Side Effect Suppression pass for 549 // mitigation. This is to prevent slow downs due to 550 // analyses needed by the LVIHardening pass when compiling at -O0. 551 if (getOptLevel() != CodeGenOpt::None) 552 addPass(createX86LoadValueInjectionLoadHardeningPass()); 553 } 554 555 void X86PassConfig::addPreSched2() { 556 addPass(createX86ExpandPseudoPass()); 557 addPass(createKCFIPass()); 558 } 559 560 void X86PassConfig::addPreEmitPass() { 561 if (getOptLevel() != CodeGenOpt::None) { 562 addPass(new X86ExecutionDomainFix()); 563 addPass(createBreakFalseDeps()); 564 } 565 566 addPass(createX86IndirectBranchTrackingPass()); 567 568 addPass(createX86IssueVZeroUpperPass()); 569 570 if (getOptLevel() != CodeGenOpt::None) { 571 addPass(createX86FixupBWInsts()); 572 addPass(createX86PadShortFunctions()); 573 addPass(createX86FixupLEAs()); 574 addPass(createX86FixupInstTuning()); 575 addPass(createX86FixupVectorConstants()); 576 } 577 addPass(createX86EvexToVexInsts()); 578 addPass(createX86DiscriminateMemOpsPass()); 579 addPass(createX86InsertPrefetchPass()); 580 addPass(createX86InsertX87waitPass()); 581 } 582 583 void X86PassConfig::addPreEmitPass2() { 584 const Triple &TT = TM->getTargetTriple(); 585 const MCAsmInfo *MAI = TM->getMCAsmInfo(); 586 587 // The X86 Speculative Execution Pass must run after all control 588 // flow graph modifying passes. As a result it was listed to run right before 589 // the X86 Retpoline Thunks pass. The reason it must run after control flow 590 // graph modifications is that the model of LFENCE in LLVM has to be updated 591 // (FIXME: https://bugs.llvm.org/show_bug.cgi?id=45167). Currently the 592 // placement of this pass was hand checked to ensure that the subsequent 593 // passes don't move the code around the LFENCEs in a way that will hurt the 594 // correctness of this pass. This placement has been shown to work based on 595 // hand inspection of the codegen output. 596 addPass(createX86SpeculativeExecutionSideEffectSuppression()); 597 addPass(createX86IndirectThunksPass()); 598 addPass(createX86ReturnThunksPass()); 599 600 // Insert extra int3 instructions after trailing call instructions to avoid 601 // issues in the unwinder. 602 if (TT.isOSWindows() && TT.getArch() == Triple::x86_64) 603 addPass(createX86AvoidTrailingCallPass()); 604 605 // Verify basic block incoming and outgoing cfa offset and register values and 606 // correct CFA calculation rule where needed by inserting appropriate CFI 607 // instructions. 608 if (!TT.isOSDarwin() && 609 (!TT.isOSWindows() || 610 MAI->getExceptionHandlingType() == ExceptionHandling::DwarfCFI)) 611 addPass(createCFIInstrInserter()); 612 613 if (TT.isOSWindows()) { 614 // Identify valid longjmp targets for Windows Control Flow Guard. 615 addPass(createCFGuardLongjmpPass()); 616 // Identify valid eh continuation targets for Windows EHCont Guard. 617 addPass(createEHContGuardCatchretPass()); 618 } 619 addPass(createX86LoadValueInjectionRetHardeningPass()); 620 621 // Insert pseudo probe annotation for callsite profiling 622 addPass(createPseudoProbeInserter()); 623 624 // KCFI indirect call checks are lowered to a bundle, and on Darwin platforms, 625 // also CALL_RVMARKER. 626 addPass(createUnpackMachineBundles([&TT](const MachineFunction &MF) { 627 // Only run bundle expansion if the module uses kcfi, or there are relevant 628 // ObjC runtime functions present in the module. 629 const Function &F = MF.getFunction(); 630 const Module *M = F.getParent(); 631 return M->getModuleFlag("kcfi") || 632 (TT.isOSDarwin() && 633 (M->getFunction("objc_retainAutoreleasedReturnValue") || 634 M->getFunction("objc_unsafeClaimAutoreleasedReturnValue"))); 635 })); 636 } 637 638 bool X86PassConfig::addPostFastRegAllocRewrite() { 639 addPass(createX86FastTileConfigPass()); 640 return true; 641 } 642 643 std::unique_ptr<CSEConfigBase> X86PassConfig::getCSEConfig() const { 644 return getStandardCSEConfigForOpt(TM->getOptLevel()); 645 } 646 647 static bool onlyAllocateTileRegisters(const TargetRegisterInfo &TRI, 648 const TargetRegisterClass &RC) { 649 return static_cast<const X86RegisterInfo &>(TRI).isTileRegisterClass(&RC); 650 } 651 652 bool X86PassConfig::addRegAssignAndRewriteOptimized() { 653 // Don't support tile RA when RA is specified by command line "-regalloc". 654 if (!isCustomizedRegAlloc() && EnableTileRAPass) { 655 // Allocate tile register first. 656 addPass(createGreedyRegisterAllocator(onlyAllocateTileRegisters)); 657 addPass(createX86TileConfigPass()); 658 } 659 return TargetPassConfig::addRegAssignAndRewriteOptimized(); 660 } 661