1 //===-- TargetPassConfig.cpp - Target independent code generation passes --===// 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 interfaces to access the target independent code 11 // generation passes provided by the LLVM backend. 12 // 13 //===---------------------------------------------------------------------===// 14 15 #include "llvm/CodeGen/TargetPassConfig.h" 16 17 #include "llvm/Analysis/BasicAliasAnalysis.h" 18 #include "llvm/Analysis/CFLAndersAliasAnalysis.h" 19 #include "llvm/Analysis/CFLSteensAliasAnalysis.h" 20 #include "llvm/Analysis/CallGraphSCCPass.h" 21 #include "llvm/Analysis/Passes.h" 22 #include "llvm/Analysis/ScopedNoAliasAA.h" 23 #include "llvm/Analysis/TypeBasedAliasAnalysis.h" 24 #include "llvm/CodeGen/MachineFunctionPass.h" 25 #include "llvm/CodeGen/RegAllocRegistry.h" 26 #include "llvm/CodeGen/RegisterUsageInfo.h" 27 #include "llvm/IR/IRPrintingPasses.h" 28 #include "llvm/IR/LegacyPassManager.h" 29 #include "llvm/IR/Verifier.h" 30 #include "llvm/MC/MCAsmInfo.h" 31 #include "llvm/Support/Debug.h" 32 #include "llvm/Support/ErrorHandling.h" 33 #include "llvm/Support/raw_ostream.h" 34 #include "llvm/Target/TargetMachine.h" 35 #include "llvm/Transforms/Instrumentation.h" 36 #include "llvm/Transforms/Scalar.h" 37 #include "llvm/Transforms/Utils/SymbolRewriter.h" 38 39 using namespace llvm; 40 41 static cl::opt<bool> DisablePostRA("disable-post-ra", cl::Hidden, 42 cl::desc("Disable Post Regalloc")); 43 static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden, 44 cl::desc("Disable branch folding")); 45 static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden, 46 cl::desc("Disable tail duplication")); 47 static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden, 48 cl::desc("Disable pre-register allocation tail duplication")); 49 static cl::opt<bool> DisableBlockPlacement("disable-block-placement", 50 cl::Hidden, cl::desc("Disable probability-driven block placement")); 51 static cl::opt<bool> EnableBlockPlacementStats("enable-block-placement-stats", 52 cl::Hidden, cl::desc("Collect probability-driven block placement stats")); 53 static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden, 54 cl::desc("Disable Stack Slot Coloring")); 55 static cl::opt<bool> DisableMachineDCE("disable-machine-dce", cl::Hidden, 56 cl::desc("Disable Machine Dead Code Elimination")); 57 static cl::opt<bool> DisableEarlyIfConversion("disable-early-ifcvt", cl::Hidden, 58 cl::desc("Disable Early If-conversion")); 59 static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden, 60 cl::desc("Disable Machine LICM")); 61 static cl::opt<bool> DisableMachineCSE("disable-machine-cse", cl::Hidden, 62 cl::desc("Disable Machine Common Subexpression Elimination")); 63 static cl::opt<cl::boolOrDefault> OptimizeRegAlloc( 64 "optimize-regalloc", cl::Hidden, 65 cl::desc("Enable optimized register allocation compilation path.")); 66 static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm", 67 cl::Hidden, 68 cl::desc("Disable Machine LICM")); 69 static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden, 70 cl::desc("Disable Machine Sinking")); 71 static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden, 72 cl::desc("Disable Loop Strength Reduction Pass")); 73 static cl::opt<bool> DisableConstantHoisting("disable-constant-hoisting", 74 cl::Hidden, cl::desc("Disable ConstantHoisting")); 75 static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden, 76 cl::desc("Disable Codegen Prepare")); 77 static cl::opt<bool> DisableCopyProp("disable-copyprop", cl::Hidden, 78 cl::desc("Disable Copy Propagation pass")); 79 static cl::opt<bool> DisablePartialLibcallInlining("disable-partial-libcall-inlining", 80 cl::Hidden, cl::desc("Disable Partial Libcall Inlining")); 81 static cl::opt<bool> EnableImplicitNullChecks( 82 "enable-implicit-null-checks", 83 cl::desc("Fold null checks into faulting memory operations"), 84 cl::init(false)); 85 static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden, 86 cl::desc("Print LLVM IR produced by the loop-reduce pass")); 87 static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden, 88 cl::desc("Print LLVM IR input to isel pass")); 89 static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden, 90 cl::desc("Dump garbage collector data")); 91 static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden, 92 cl::desc("Verify generated machine code"), 93 cl::init(false), 94 cl::ZeroOrMore); 95 96 static cl::opt<std::string> 97 PrintMachineInstrs("print-machineinstrs", cl::ValueOptional, 98 cl::desc("Print machine instrs"), 99 cl::value_desc("pass-name"), cl::init("option-unspecified")); 100 101 static cl::opt<bool> EnableGlobalISelAbort( 102 "global-isel-abort", cl::Hidden, 103 cl::desc("Enable abort calls when \"global\" instruction selection " 104 "fails to lower/select an instruction"), 105 cl::init(true)); 106 107 // Temporary option to allow experimenting with MachineScheduler as a post-RA 108 // scheduler. Targets can "properly" enable this with 109 // substitutePass(&PostRASchedulerID, &PostMachineSchedulerID). 110 // Targets can return true in targetSchedulesPostRAScheduling() and 111 // insert a PostRA scheduling pass wherever it wants. 112 cl::opt<bool> MISchedPostRA("misched-postra", cl::Hidden, 113 cl::desc("Run MachineScheduler post regalloc (independent of preRA sched)")); 114 115 // Experimental option to run live interval analysis early. 116 static cl::opt<bool> EarlyLiveIntervals("early-live-intervals", cl::Hidden, 117 cl::desc("Run live interval analysis earlier in the pipeline")); 118 119 // Experimental option to use CFL-AA in codegen 120 enum class CFLAAType { None, Steensgaard, Andersen, Both }; 121 static cl::opt<CFLAAType> UseCFLAA( 122 "use-cfl-aa-in-codegen", cl::init(CFLAAType::None), cl::Hidden, 123 cl::desc("Enable the new, experimental CFL alias analysis in CodeGen"), 124 cl::values(clEnumValN(CFLAAType::None, "none", "Disable CFL-AA"), 125 clEnumValN(CFLAAType::Steensgaard, "steens", 126 "Enable unification-based CFL-AA"), 127 clEnumValN(CFLAAType::Andersen, "anders", 128 "Enable inclusion-based CFL-AA"), 129 clEnumValN(CFLAAType::Both, "both", 130 "Enable both variants of CFL-AA"), 131 clEnumValEnd)); 132 133 /// Allow standard passes to be disabled by command line options. This supports 134 /// simple binary flags that either suppress the pass or do nothing. 135 /// i.e. -disable-mypass=false has no effect. 136 /// These should be converted to boolOrDefault in order to use applyOverride. 137 static IdentifyingPassPtr applyDisable(IdentifyingPassPtr PassID, 138 bool Override) { 139 if (Override) 140 return IdentifyingPassPtr(); 141 return PassID; 142 } 143 144 /// Allow standard passes to be disabled by the command line, regardless of who 145 /// is adding the pass. 146 /// 147 /// StandardID is the pass identified in the standard pass pipeline and provided 148 /// to addPass(). It may be a target-specific ID in the case that the target 149 /// directly adds its own pass, but in that case we harmlessly fall through. 150 /// 151 /// TargetID is the pass that the target has configured to override StandardID. 152 /// 153 /// StandardID may be a pseudo ID. In that case TargetID is the name of the real 154 /// pass to run. This allows multiple options to control a single pass depending 155 /// on where in the pipeline that pass is added. 156 static IdentifyingPassPtr overridePass(AnalysisID StandardID, 157 IdentifyingPassPtr TargetID) { 158 if (StandardID == &PostRASchedulerID) 159 return applyDisable(TargetID, DisablePostRA); 160 161 if (StandardID == &BranchFolderPassID) 162 return applyDisable(TargetID, DisableBranchFold); 163 164 if (StandardID == &TailDuplicateID) 165 return applyDisable(TargetID, DisableTailDuplicate); 166 167 if (StandardID == &TargetPassConfig::EarlyTailDuplicateID) 168 return applyDisable(TargetID, DisableEarlyTailDup); 169 170 if (StandardID == &MachineBlockPlacementID) 171 return applyDisable(TargetID, DisableBlockPlacement); 172 173 if (StandardID == &StackSlotColoringID) 174 return applyDisable(TargetID, DisableSSC); 175 176 if (StandardID == &DeadMachineInstructionElimID) 177 return applyDisable(TargetID, DisableMachineDCE); 178 179 if (StandardID == &EarlyIfConverterID) 180 return applyDisable(TargetID, DisableEarlyIfConversion); 181 182 if (StandardID == &MachineLICMID) 183 return applyDisable(TargetID, DisableMachineLICM); 184 185 if (StandardID == &MachineCSEID) 186 return applyDisable(TargetID, DisableMachineCSE); 187 188 if (StandardID == &TargetPassConfig::PostRAMachineLICMID) 189 return applyDisable(TargetID, DisablePostRAMachineLICM); 190 191 if (StandardID == &MachineSinkingID) 192 return applyDisable(TargetID, DisableMachineSink); 193 194 if (StandardID == &MachineCopyPropagationID) 195 return applyDisable(TargetID, DisableCopyProp); 196 197 return TargetID; 198 } 199 200 //===---------------------------------------------------------------------===// 201 /// TargetPassConfig 202 //===---------------------------------------------------------------------===// 203 204 INITIALIZE_PASS(TargetPassConfig, "targetpassconfig", 205 "Target Pass Configuration", false, false) 206 char TargetPassConfig::ID = 0; 207 208 // Pseudo Pass IDs. 209 char TargetPassConfig::EarlyTailDuplicateID = 0; 210 char TargetPassConfig::PostRAMachineLICMID = 0; 211 212 namespace { 213 struct InsertedPass { 214 AnalysisID TargetPassID; 215 IdentifyingPassPtr InsertedPassID; 216 bool VerifyAfter; 217 bool PrintAfter; 218 219 InsertedPass(AnalysisID TargetPassID, IdentifyingPassPtr InsertedPassID, 220 bool VerifyAfter, bool PrintAfter) 221 : TargetPassID(TargetPassID), InsertedPassID(InsertedPassID), 222 VerifyAfter(VerifyAfter), PrintAfter(PrintAfter) {} 223 224 Pass *getInsertedPass() const { 225 assert(InsertedPassID.isValid() && "Illegal Pass ID!"); 226 if (InsertedPassID.isInstance()) 227 return InsertedPassID.getInstance(); 228 Pass *NP = Pass::createPass(InsertedPassID.getID()); 229 assert(NP && "Pass ID not registered"); 230 return NP; 231 } 232 }; 233 } 234 235 namespace llvm { 236 class PassConfigImpl { 237 public: 238 // List of passes explicitly substituted by this target. Normally this is 239 // empty, but it is a convenient way to suppress or replace specific passes 240 // that are part of a standard pass pipeline without overridding the entire 241 // pipeline. This mechanism allows target options to inherit a standard pass's 242 // user interface. For example, a target may disable a standard pass by 243 // default by substituting a pass ID of zero, and the user may still enable 244 // that standard pass with an explicit command line option. 245 DenseMap<AnalysisID,IdentifyingPassPtr> TargetPasses; 246 247 /// Store the pairs of <AnalysisID, AnalysisID> of which the second pass 248 /// is inserted after each instance of the first one. 249 SmallVector<InsertedPass, 4> InsertedPasses; 250 }; 251 } // namespace llvm 252 253 // Out of line virtual method. 254 TargetPassConfig::~TargetPassConfig() { 255 delete Impl; 256 } 257 258 // Out of line constructor provides default values for pass options and 259 // registers all common codegen passes. 260 TargetPassConfig::TargetPassConfig(TargetMachine *tm, PassManagerBase &pm) 261 : ImmutablePass(ID), PM(&pm), StartBefore(nullptr), StartAfter(nullptr), 262 StopAfter(nullptr), Started(true), Stopped(false), 263 AddingMachinePasses(false), TM(tm), Impl(nullptr), Initialized(false), 264 DisableVerify(false), EnableTailMerge(true) { 265 266 Impl = new PassConfigImpl(); 267 268 // Register all target independent codegen passes to activate their PassIDs, 269 // including this pass itself. 270 initializeCodeGen(*PassRegistry::getPassRegistry()); 271 272 // Also register alias analysis passes required by codegen passes. 273 initializeBasicAAWrapperPassPass(*PassRegistry::getPassRegistry()); 274 initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry()); 275 276 // Substitute Pseudo Pass IDs for real ones. 277 substitutePass(&EarlyTailDuplicateID, &TailDuplicateID); 278 substitutePass(&PostRAMachineLICMID, &MachineLICMID); 279 280 if (StringRef(PrintMachineInstrs.getValue()).equals("")) 281 TM->Options.PrintMachineCode = true; 282 } 283 284 CodeGenOpt::Level TargetPassConfig::getOptLevel() const { 285 return TM->getOptLevel(); 286 } 287 288 /// Insert InsertedPassID pass after TargetPassID. 289 void TargetPassConfig::insertPass(AnalysisID TargetPassID, 290 IdentifyingPassPtr InsertedPassID, 291 bool VerifyAfter, bool PrintAfter) { 292 assert(((!InsertedPassID.isInstance() && 293 TargetPassID != InsertedPassID.getID()) || 294 (InsertedPassID.isInstance() && 295 TargetPassID != InsertedPassID.getInstance()->getPassID())) && 296 "Insert a pass after itself!"); 297 Impl->InsertedPasses.emplace_back(TargetPassID, InsertedPassID, VerifyAfter, 298 PrintAfter); 299 } 300 301 /// createPassConfig - Create a pass configuration object to be used by 302 /// addPassToEmitX methods for generating a pipeline of CodeGen passes. 303 /// 304 /// Targets may override this to extend TargetPassConfig. 305 TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) { 306 return new TargetPassConfig(this, PM); 307 } 308 309 TargetPassConfig::TargetPassConfig() 310 : ImmutablePass(ID), PM(nullptr) { 311 llvm_unreachable("TargetPassConfig should not be constructed on-the-fly"); 312 } 313 314 // Helper to verify the analysis is really immutable. 315 void TargetPassConfig::setOpt(bool &Opt, bool Val) { 316 assert(!Initialized && "PassConfig is immutable"); 317 Opt = Val; 318 } 319 320 void TargetPassConfig::substitutePass(AnalysisID StandardID, 321 IdentifyingPassPtr TargetID) { 322 Impl->TargetPasses[StandardID] = TargetID; 323 } 324 325 IdentifyingPassPtr TargetPassConfig::getPassSubstitution(AnalysisID ID) const { 326 DenseMap<AnalysisID, IdentifyingPassPtr>::const_iterator 327 I = Impl->TargetPasses.find(ID); 328 if (I == Impl->TargetPasses.end()) 329 return ID; 330 return I->second; 331 } 332 333 bool TargetPassConfig::isPassSubstitutedOrOverridden(AnalysisID ID) const { 334 IdentifyingPassPtr TargetID = getPassSubstitution(ID); 335 IdentifyingPassPtr FinalPtr = overridePass(ID, TargetID); 336 return !FinalPtr.isValid() || FinalPtr.isInstance() || 337 FinalPtr.getID() != ID; 338 } 339 340 /// Add a pass to the PassManager if that pass is supposed to be run. If the 341 /// Started/Stopped flags indicate either that the compilation should start at 342 /// a later pass or that it should stop after an earlier pass, then do not add 343 /// the pass. Finally, compare the current pass against the StartAfter 344 /// and StopAfter options and change the Started/Stopped flags accordingly. 345 void TargetPassConfig::addPass(Pass *P, bool verifyAfter, bool printAfter) { 346 assert(!Initialized && "PassConfig is immutable"); 347 348 // Cache the Pass ID here in case the pass manager finds this pass is 349 // redundant with ones already scheduled / available, and deletes it. 350 // Fundamentally, once we add the pass to the manager, we no longer own it 351 // and shouldn't reference it. 352 AnalysisID PassID = P->getPassID(); 353 354 if (StartBefore == PassID) 355 Started = true; 356 if (Started && !Stopped) { 357 std::string Banner; 358 // Construct banner message before PM->add() as that may delete the pass. 359 if (AddingMachinePasses && (printAfter || verifyAfter)) 360 Banner = std::string("After ") + std::string(P->getPassName()); 361 PM->add(P); 362 if (AddingMachinePasses) { 363 if (printAfter) 364 addPrintPass(Banner); 365 if (verifyAfter) 366 addVerifyPass(Banner); 367 } 368 369 // Add the passes after the pass P if there is any. 370 for (auto IP : Impl->InsertedPasses) { 371 if (IP.TargetPassID == PassID) 372 addPass(IP.getInsertedPass(), IP.VerifyAfter, IP.PrintAfter); 373 } 374 } else { 375 delete P; 376 } 377 if (StopAfter == PassID) 378 Stopped = true; 379 if (StartAfter == PassID) 380 Started = true; 381 if (Stopped && !Started) 382 report_fatal_error("Cannot stop compilation after pass that is not run"); 383 } 384 385 /// Add a CodeGen pass at this point in the pipeline after checking for target 386 /// and command line overrides. 387 /// 388 /// addPass cannot return a pointer to the pass instance because is internal the 389 /// PassManager and the instance we create here may already be freed. 390 AnalysisID TargetPassConfig::addPass(AnalysisID PassID, bool verifyAfter, 391 bool printAfter) { 392 IdentifyingPassPtr TargetID = getPassSubstitution(PassID); 393 IdentifyingPassPtr FinalPtr = overridePass(PassID, TargetID); 394 if (!FinalPtr.isValid()) 395 return nullptr; 396 397 Pass *P; 398 if (FinalPtr.isInstance()) 399 P = FinalPtr.getInstance(); 400 else { 401 P = Pass::createPass(FinalPtr.getID()); 402 if (!P) 403 llvm_unreachable("Pass ID not registered"); 404 } 405 AnalysisID FinalID = P->getPassID(); 406 addPass(P, verifyAfter, printAfter); // Ends the lifetime of P. 407 408 return FinalID; 409 } 410 411 void TargetPassConfig::printAndVerify(const std::string &Banner) { 412 addPrintPass(Banner); 413 addVerifyPass(Banner); 414 } 415 416 void TargetPassConfig::addPrintPass(const std::string &Banner) { 417 if (TM->shouldPrintMachineCode()) 418 PM->add(createMachineFunctionPrinterPass(dbgs(), Banner)); 419 } 420 421 void TargetPassConfig::addVerifyPass(const std::string &Banner) { 422 if (VerifyMachineCode) 423 PM->add(createMachineVerifierPass(Banner)); 424 } 425 426 /// Add common target configurable passes that perform LLVM IR to IR transforms 427 /// following machine independent optimization. 428 void TargetPassConfig::addIRPasses() { 429 switch (UseCFLAA) { 430 case CFLAAType::Steensgaard: 431 addPass(createCFLSteensAAWrapperPass()); 432 break; 433 case CFLAAType::Andersen: 434 addPass(createCFLAndersAAWrapperPass()); 435 break; 436 case CFLAAType::Both: 437 addPass(createCFLAndersAAWrapperPass()); 438 addPass(createCFLSteensAAWrapperPass()); 439 break; 440 default: 441 break; 442 } 443 444 // Basic AliasAnalysis support. 445 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that 446 // BasicAliasAnalysis wins if they disagree. This is intended to help 447 // support "obvious" type-punning idioms. 448 addPass(createTypeBasedAAWrapperPass()); 449 addPass(createScopedNoAliasAAWrapperPass()); 450 addPass(createBasicAAWrapperPass()); 451 452 // Before running any passes, run the verifier to determine if the input 453 // coming from the front-end and/or optimizer is valid. 454 if (!DisableVerify) 455 addPass(createVerifierPass()); 456 457 // Run loop strength reduction before anything else. 458 if (getOptLevel() != CodeGenOpt::None && !DisableLSR) { 459 addPass(createLoopStrengthReducePass()); 460 if (PrintLSR) 461 addPass(createPrintFunctionPass(dbgs(), "\n\n*** Code after LSR ***\n")); 462 } 463 464 // Run GC lowering passes for builtin collectors 465 // TODO: add a pass insertion point here 466 addPass(createGCLoweringPass()); 467 addPass(createShadowStackGCLoweringPass()); 468 469 // Make sure that no unreachable blocks are instruction selected. 470 addPass(createUnreachableBlockEliminationPass()); 471 472 // Prepare expensive constants for SelectionDAG. 473 if (getOptLevel() != CodeGenOpt::None && !DisableConstantHoisting) 474 addPass(createConstantHoistingPass()); 475 476 if (getOptLevel() != CodeGenOpt::None && !DisablePartialLibcallInlining) 477 addPass(createPartiallyInlineLibCallsPass()); 478 } 479 480 /// Turn exception handling constructs into something the code generators can 481 /// handle. 482 void TargetPassConfig::addPassesToHandleExceptions() { 483 const MCAsmInfo *MCAI = TM->getMCAsmInfo(); 484 assert(MCAI && "No MCAsmInfo"); 485 switch (MCAI->getExceptionHandlingType()) { 486 case ExceptionHandling::SjLj: 487 // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both 488 // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise, 489 // catch info can get misplaced when a selector ends up more than one block 490 // removed from the parent invoke(s). This could happen when a landing 491 // pad is shared by multiple invokes and is also a target of a normal 492 // edge from elsewhere. 493 addPass(createSjLjEHPreparePass()); 494 LLVM_FALLTHROUGH; 495 case ExceptionHandling::DwarfCFI: 496 case ExceptionHandling::ARM: 497 addPass(createDwarfEHPass(TM)); 498 break; 499 case ExceptionHandling::WinEH: 500 // We support using both GCC-style and MSVC-style exceptions on Windows, so 501 // add both preparation passes. Each pass will only actually run if it 502 // recognizes the personality function. 503 addPass(createWinEHPass(TM)); 504 addPass(createDwarfEHPass(TM)); 505 break; 506 case ExceptionHandling::None: 507 addPass(createLowerInvokePass()); 508 509 // The lower invoke pass may create unreachable code. Remove it. 510 addPass(createUnreachableBlockEliminationPass()); 511 break; 512 } 513 } 514 515 /// Add pass to prepare the LLVM IR for code generation. This should be done 516 /// before exception handling preparation passes. 517 void TargetPassConfig::addCodeGenPrepare() { 518 if (getOptLevel() != CodeGenOpt::None && !DisableCGP) 519 addPass(createCodeGenPreparePass(TM)); 520 addPass(createRewriteSymbolsPass()); 521 } 522 523 /// Add common passes that perform LLVM IR to IR transforms in preparation for 524 /// instruction selection. 525 void TargetPassConfig::addISelPrepare() { 526 addPreISel(); 527 528 // Force codegen to run according to the callgraph. 529 if (TM->Options.EnableIPRA) 530 addPass(new DummyCGSCCPass); 531 532 // Add both the safe stack and the stack protection passes: each of them will 533 // only protect functions that have corresponding attributes. 534 addPass(createSafeStackPass(TM)); 535 addPass(createStackProtectorPass(TM)); 536 537 if (PrintISelInput) 538 addPass(createPrintFunctionPass( 539 dbgs(), "\n\n*** Final LLVM Code input to ISel ***\n")); 540 541 // All passes which modify the LLVM IR are now complete; run the verifier 542 // to ensure that the IR is valid. 543 if (!DisableVerify) 544 addPass(createVerifierPass()); 545 } 546 547 /// Add the complete set of target-independent postISel code generator passes. 548 /// 549 /// This can be read as the standard order of major LLVM CodeGen stages. Stages 550 /// with nontrivial configuration or multiple passes are broken out below in 551 /// add%Stage routines. 552 /// 553 /// Any TargetPassConfig::addXX routine may be overriden by the Target. The 554 /// addPre/Post methods with empty header implementations allow injecting 555 /// target-specific fixups just before or after major stages. Additionally, 556 /// targets have the flexibility to change pass order within a stage by 557 /// overriding default implementation of add%Stage routines below. Each 558 /// technique has maintainability tradeoffs because alternate pass orders are 559 /// not well supported. addPre/Post works better if the target pass is easily 560 /// tied to a common pass. But if it has subtle dependencies on multiple passes, 561 /// the target should override the stage instead. 562 /// 563 /// TODO: We could use a single addPre/Post(ID) hook to allow pass injection 564 /// before/after any target-independent pass. But it's currently overkill. 565 void TargetPassConfig::addMachinePasses() { 566 AddingMachinePasses = true; 567 568 if (TM->Options.EnableIPRA) 569 addPass(createRegUsageInfoPropPass()); 570 571 // Insert a machine instr printer pass after the specified pass. 572 if (!StringRef(PrintMachineInstrs.getValue()).equals("") && 573 !StringRef(PrintMachineInstrs.getValue()).equals("option-unspecified")) { 574 const PassRegistry *PR = PassRegistry::getPassRegistry(); 575 const PassInfo *TPI = PR->getPassInfo(PrintMachineInstrs.getValue()); 576 const PassInfo *IPI = PR->getPassInfo(StringRef("machineinstr-printer")); 577 assert (TPI && IPI && "Pass ID not registered!"); 578 const char *TID = (const char *)(TPI->getTypeInfo()); 579 const char *IID = (const char *)(IPI->getTypeInfo()); 580 insertPass(TID, IID); 581 } 582 583 // Print the instruction selected machine code... 584 printAndVerify("After Instruction Selection"); 585 586 // Expand pseudo-instructions emitted by ISel. 587 addPass(&ExpandISelPseudosID); 588 589 // Add passes that optimize machine instructions in SSA form. 590 if (getOptLevel() != CodeGenOpt::None) { 591 addMachineSSAOptimization(); 592 } else { 593 // If the target requests it, assign local variables to stack slots relative 594 // to one another and simplify frame index references where possible. 595 addPass(&LocalStackSlotAllocationID, false); 596 } 597 598 // Run pre-ra passes. 599 addPreRegAlloc(); 600 601 // Run register allocation and passes that are tightly coupled with it, 602 // including phi elimination and scheduling. 603 if (getOptimizeRegAlloc()) 604 addOptimizedRegAlloc(createRegAllocPass(true)); 605 else 606 addFastRegAlloc(createRegAllocPass(false)); 607 608 // Run post-ra passes. 609 addPostRegAlloc(); 610 611 // Insert prolog/epilog code. Eliminate abstract frame index references... 612 if (getOptLevel() != CodeGenOpt::None) 613 addPass(&ShrinkWrapID); 614 615 // Prolog/Epilog inserter needs a TargetMachine to instantiate. But only 616 // do so if it hasn't been disabled, substituted, or overridden. 617 if (!isPassSubstitutedOrOverridden(&PrologEpilogCodeInserterID)) 618 addPass(createPrologEpilogInserterPass(TM)); 619 620 /// Add passes that optimize machine instructions after register allocation. 621 if (getOptLevel() != CodeGenOpt::None) 622 addMachineLateOptimization(); 623 624 // Expand pseudo instructions before second scheduling pass. 625 addPass(&ExpandPostRAPseudosID); 626 627 // Run pre-sched2 passes. 628 addPreSched2(); 629 630 if (EnableImplicitNullChecks) 631 addPass(&ImplicitNullChecksID); 632 633 // Second pass scheduler. 634 // Let Target optionally insert this pass by itself at some other 635 // point. 636 if (getOptLevel() != CodeGenOpt::None && 637 !TM->targetSchedulesPostRAScheduling()) { 638 if (MISchedPostRA) 639 addPass(&PostMachineSchedulerID); 640 else 641 addPass(&PostRASchedulerID); 642 } 643 644 // GC 645 if (addGCPasses()) { 646 if (PrintGCInfo) 647 addPass(createGCInfoPrinter(dbgs()), false, false); 648 } 649 650 // Basic block placement. 651 if (getOptLevel() != CodeGenOpt::None) 652 addBlockPlacement(); 653 654 addPreEmitPass(); 655 656 if (TM->Options.EnableIPRA) 657 // Collect register usage information and produce a register mask of 658 // clobbered registers, to be used to optimize call sites. 659 addPass(createRegUsageInfoCollector()); 660 661 addPass(&FuncletLayoutID, false); 662 663 addPass(&StackMapLivenessID, false); 664 addPass(&LiveDebugValuesID, false); 665 666 addPass(&XRayInstrumentationID, false); 667 addPass(&PatchableFunctionID, false); 668 669 AddingMachinePasses = false; 670 } 671 672 /// Add passes that optimize machine instructions in SSA form. 673 void TargetPassConfig::addMachineSSAOptimization() { 674 // Pre-ra tail duplication. 675 addPass(&EarlyTailDuplicateID); 676 677 // Optimize PHIs before DCE: removing dead PHI cycles may make more 678 // instructions dead. 679 addPass(&OptimizePHIsID, false); 680 681 // This pass merges large allocas. StackSlotColoring is a different pass 682 // which merges spill slots. 683 addPass(&StackColoringID, false); 684 685 // If the target requests it, assign local variables to stack slots relative 686 // to one another and simplify frame index references where possible. 687 addPass(&LocalStackSlotAllocationID, false); 688 689 // With optimization, dead code should already be eliminated. However 690 // there is one known exception: lowered code for arguments that are only 691 // used by tail calls, where the tail calls reuse the incoming stack 692 // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll). 693 addPass(&DeadMachineInstructionElimID); 694 695 // Allow targets to insert passes that improve instruction level parallelism, 696 // like if-conversion. Such passes will typically need dominator trees and 697 // loop info, just like LICM and CSE below. 698 addILPOpts(); 699 700 addPass(&MachineLICMID, false); 701 addPass(&MachineCSEID, false); 702 addPass(&MachineSinkingID); 703 704 addPass(&PeepholeOptimizerID); 705 // Clean-up the dead code that may have been generated by peephole 706 // rewriting. 707 addPass(&DeadMachineInstructionElimID); 708 } 709 710 //===---------------------------------------------------------------------===// 711 /// Register Allocation Pass Configuration 712 //===---------------------------------------------------------------------===// 713 714 bool TargetPassConfig::getOptimizeRegAlloc() const { 715 switch (OptimizeRegAlloc) { 716 case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None; 717 case cl::BOU_TRUE: return true; 718 case cl::BOU_FALSE: return false; 719 } 720 llvm_unreachable("Invalid optimize-regalloc state"); 721 } 722 723 /// RegisterRegAlloc's global Registry tracks allocator registration. 724 MachinePassRegistry RegisterRegAlloc::Registry; 725 726 /// A dummy default pass factory indicates whether the register allocator is 727 /// overridden on the command line. 728 LLVM_DEFINE_ONCE_FLAG(InitializeDefaultRegisterAllocatorFlag); 729 static FunctionPass *useDefaultRegisterAllocator() { return nullptr; } 730 static RegisterRegAlloc 731 defaultRegAlloc("default", 732 "pick register allocator based on -O option", 733 useDefaultRegisterAllocator); 734 735 /// -regalloc=... command line option. 736 static cl::opt<RegisterRegAlloc::FunctionPassCtor, false, 737 RegisterPassParser<RegisterRegAlloc> > 738 RegAlloc("regalloc", 739 cl::init(&useDefaultRegisterAllocator), 740 cl::desc("Register allocator to use")); 741 742 static void initializeDefaultRegisterAllocatorOnce() { 743 RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault(); 744 745 if (!Ctor) { 746 Ctor = RegAlloc; 747 RegisterRegAlloc::setDefault(RegAlloc); 748 } 749 } 750 751 752 /// Instantiate the default register allocator pass for this target for either 753 /// the optimized or unoptimized allocation path. This will be added to the pass 754 /// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc 755 /// in the optimized case. 756 /// 757 /// A target that uses the standard regalloc pass order for fast or optimized 758 /// allocation may still override this for per-target regalloc 759 /// selection. But -regalloc=... always takes precedence. 760 FunctionPass *TargetPassConfig::createTargetRegisterAllocator(bool Optimized) { 761 if (Optimized) 762 return createGreedyRegisterAllocator(); 763 else 764 return createFastRegisterAllocator(); 765 } 766 767 /// Find and instantiate the register allocation pass requested by this target 768 /// at the current optimization level. Different register allocators are 769 /// defined as separate passes because they may require different analysis. 770 /// 771 /// This helper ensures that the regalloc= option is always available, 772 /// even for targets that override the default allocator. 773 /// 774 /// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs, 775 /// this can be folded into addPass. 776 FunctionPass *TargetPassConfig::createRegAllocPass(bool Optimized) { 777 // Initialize the global default. 778 llvm::call_once(InitializeDefaultRegisterAllocatorFlag, 779 initializeDefaultRegisterAllocatorOnce); 780 781 RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault(); 782 if (Ctor != useDefaultRegisterAllocator) 783 return Ctor(); 784 785 // With no -regalloc= override, ask the target for a regalloc pass. 786 return createTargetRegisterAllocator(Optimized); 787 } 788 789 /// Return true if the default global register allocator is in use and 790 /// has not be overriden on the command line with '-regalloc=...' 791 bool TargetPassConfig::usingDefaultRegAlloc() const { 792 return RegAlloc.getNumOccurrences() == 0; 793 } 794 795 /// Add the minimum set of target-independent passes that are required for 796 /// register allocation. No coalescing or scheduling. 797 void TargetPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) { 798 addPass(&PHIEliminationID, false); 799 addPass(&TwoAddressInstructionPassID, false); 800 801 if (RegAllocPass) 802 addPass(RegAllocPass); 803 } 804 805 /// Add standard target-independent passes that are tightly coupled with 806 /// optimized register allocation, including coalescing, machine instruction 807 /// scheduling, and register allocation itself. 808 void TargetPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) { 809 addPass(&DetectDeadLanesID, false); 810 811 addPass(&ProcessImplicitDefsID, false); 812 813 // LiveVariables currently requires pure SSA form. 814 // 815 // FIXME: Once TwoAddressInstruction pass no longer uses kill flags, 816 // LiveVariables can be removed completely, and LiveIntervals can be directly 817 // computed. (We still either need to regenerate kill flags after regalloc, or 818 // preferably fix the scavenger to not depend on them). 819 addPass(&LiveVariablesID, false); 820 821 // Edge splitting is smarter with machine loop info. 822 addPass(&MachineLoopInfoID, false); 823 addPass(&PHIEliminationID, false); 824 825 // Eventually, we want to run LiveIntervals before PHI elimination. 826 if (EarlyLiveIntervals) 827 addPass(&LiveIntervalsID, false); 828 829 addPass(&TwoAddressInstructionPassID, false); 830 addPass(&RegisterCoalescerID); 831 832 // The machine scheduler may accidentally create disconnected components 833 // when moving subregister definitions around, avoid this by splitting them to 834 // separate vregs before. Splitting can also improve reg. allocation quality. 835 addPass(&RenameIndependentSubregsID); 836 837 // PreRA instruction scheduling. 838 addPass(&MachineSchedulerID); 839 840 if (RegAllocPass) { 841 // Add the selected register allocation pass. 842 addPass(RegAllocPass); 843 844 // Allow targets to change the register assignments before rewriting. 845 addPreRewrite(); 846 847 // Finally rewrite virtual registers. 848 addPass(&VirtRegRewriterID); 849 850 // Perform stack slot coloring and post-ra machine LICM. 851 // 852 // FIXME: Re-enable coloring with register when it's capable of adding 853 // kill markers. 854 addPass(&StackSlotColoringID); 855 856 // Run post-ra machine LICM to hoist reloads / remats. 857 // 858 // FIXME: can this move into MachineLateOptimization? 859 addPass(&PostRAMachineLICMID); 860 } 861 } 862 863 //===---------------------------------------------------------------------===// 864 /// Post RegAlloc Pass Configuration 865 //===---------------------------------------------------------------------===// 866 867 /// Add passes that optimize machine instructions after register allocation. 868 void TargetPassConfig::addMachineLateOptimization() { 869 // Branch folding must be run after regalloc and prolog/epilog insertion. 870 addPass(&BranchFolderPassID); 871 872 // Tail duplication. 873 // Note that duplicating tail just increases code size and degrades 874 // performance for targets that require Structured Control Flow. 875 // In addition it can also make CFG irreducible. Thus we disable it. 876 if (!TM->requiresStructuredCFG()) 877 addPass(&TailDuplicateID); 878 879 // Copy propagation. 880 addPass(&MachineCopyPropagationID); 881 } 882 883 /// Add standard GC passes. 884 bool TargetPassConfig::addGCPasses() { 885 addPass(&GCMachineCodeAnalysisID, false); 886 return true; 887 } 888 889 /// Add standard basic block placement passes. 890 void TargetPassConfig::addBlockPlacement() { 891 if (addPass(&MachineBlockPlacementID)) { 892 // Run a separate pass to collect block placement statistics. 893 if (EnableBlockPlacementStats) 894 addPass(&MachineBlockPlacementStatsID); 895 } 896 } 897 898 //===---------------------------------------------------------------------===// 899 /// GlobalISel Configuration 900 //===---------------------------------------------------------------------===// 901 bool TargetPassConfig::isGlobalISelAbortEnabled() const { 902 return EnableGlobalISelAbort; 903 } 904