1 //===- TargetPassConfig.cpp - Target independent code generation passes ---===// 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 interfaces to access the target independent code 10 // generation passes provided by the LLVM backend. 11 // 12 //===---------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/TargetPassConfig.h" 15 #include "llvm/ADT/DenseMap.h" 16 #include "llvm/ADT/SmallVector.h" 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/Analysis/BasicAliasAnalysis.h" 19 #include "llvm/Analysis/CallGraphSCCPass.h" 20 #include "llvm/Analysis/ScopedNoAliasAA.h" 21 #include "llvm/Analysis/TargetTransformInfo.h" 22 #include "llvm/Analysis/TypeBasedAliasAnalysis.h" 23 #include "llvm/CodeGen/BasicBlockSectionsProfileReader.h" 24 #include "llvm/CodeGen/CSEConfigBase.h" 25 #include "llvm/CodeGen/MachineFunctionPass.h" 26 #include "llvm/CodeGen/MachinePassRegistry.h" 27 #include "llvm/CodeGen/Passes.h" 28 #include "llvm/CodeGen/RegAllocRegistry.h" 29 #include "llvm/IR/IRPrintingPasses.h" 30 #include "llvm/IR/LegacyPassManager.h" 31 #include "llvm/IR/PassInstrumentation.h" 32 #include "llvm/IR/Verifier.h" 33 #include "llvm/InitializePasses.h" 34 #include "llvm/MC/MCAsmInfo.h" 35 #include "llvm/MC/MCTargetOptions.h" 36 #include "llvm/Pass.h" 37 #include "llvm/Support/CodeGen.h" 38 #include "llvm/Support/CommandLine.h" 39 #include "llvm/Support/Compiler.h" 40 #include "llvm/Support/Debug.h" 41 #include "llvm/Support/Discriminator.h" 42 #include "llvm/Support/ErrorHandling.h" 43 #include "llvm/Support/SaveAndRestore.h" 44 #include "llvm/Support/Threading.h" 45 #include "llvm/Target/CGPassBuilderOption.h" 46 #include "llvm/Target/TargetMachine.h" 47 #include "llvm/Transforms/Scalar.h" 48 #include "llvm/Transforms/Utils.h" 49 #include <cassert> 50 #include <optional> 51 #include <string> 52 53 using namespace llvm; 54 55 static cl::opt<bool> 56 EnableIPRA("enable-ipra", cl::init(false), cl::Hidden, 57 cl::desc("Enable interprocedural register allocation " 58 "to reduce load/store at procedure calls.")); 59 static cl::opt<bool> DisablePostRASched("disable-post-ra", cl::Hidden, 60 cl::desc("Disable Post Regalloc Scheduler")); 61 static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden, 62 cl::desc("Disable branch folding")); 63 static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden, 64 cl::desc("Disable tail duplication")); 65 static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden, 66 cl::desc("Disable pre-register allocation tail duplication")); 67 static cl::opt<bool> DisableBlockPlacement("disable-block-placement", 68 cl::Hidden, cl::desc("Disable probability-driven block placement")); 69 static cl::opt<bool> EnableBlockPlacementStats("enable-block-placement-stats", 70 cl::Hidden, cl::desc("Collect probability-driven block placement stats")); 71 static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden, 72 cl::desc("Disable Stack Slot Coloring")); 73 static cl::opt<bool> DisableMachineDCE("disable-machine-dce", cl::Hidden, 74 cl::desc("Disable Machine Dead Code Elimination")); 75 static cl::opt<bool> DisableEarlyIfConversion("disable-early-ifcvt", cl::Hidden, 76 cl::desc("Disable Early If-conversion")); 77 static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden, 78 cl::desc("Disable Machine LICM")); 79 static cl::opt<bool> DisableMachineCSE("disable-machine-cse", cl::Hidden, 80 cl::desc("Disable Machine Common Subexpression Elimination")); 81 static cl::opt<cl::boolOrDefault> OptimizeRegAlloc( 82 "optimize-regalloc", cl::Hidden, 83 cl::desc("Enable optimized register allocation compilation path.")); 84 static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm", 85 cl::Hidden, 86 cl::desc("Disable Machine LICM")); 87 static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden, 88 cl::desc("Disable Machine Sinking")); 89 static cl::opt<bool> DisablePostRAMachineSink("disable-postra-machine-sink", 90 cl::Hidden, 91 cl::desc("Disable PostRA Machine Sinking")); 92 static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden, 93 cl::desc("Disable Loop Strength Reduction Pass")); 94 static cl::opt<bool> DisableConstantHoisting("disable-constant-hoisting", 95 cl::Hidden, cl::desc("Disable ConstantHoisting")); 96 static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden, 97 cl::desc("Disable Codegen Prepare")); 98 static cl::opt<bool> DisableCopyProp("disable-copyprop", cl::Hidden, 99 cl::desc("Disable Copy Propagation pass")); 100 static cl::opt<bool> DisablePartialLibcallInlining("disable-partial-libcall-inlining", 101 cl::Hidden, cl::desc("Disable Partial Libcall Inlining")); 102 static cl::opt<bool> EnableImplicitNullChecks( 103 "enable-implicit-null-checks", 104 cl::desc("Fold null checks into faulting memory operations"), 105 cl::init(false), cl::Hidden); 106 static cl::opt<bool> DisableMergeICmps("disable-mergeicmps", 107 cl::desc("Disable MergeICmps Pass"), 108 cl::init(false), cl::Hidden); 109 static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden, 110 cl::desc("Print LLVM IR produced by the loop-reduce pass")); 111 static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden, 112 cl::desc("Print LLVM IR input to isel pass")); 113 static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden, 114 cl::desc("Dump garbage collector data")); 115 static cl::opt<cl::boolOrDefault> 116 VerifyMachineCode("verify-machineinstrs", cl::Hidden, 117 cl::desc("Verify generated machine code")); 118 static cl::opt<cl::boolOrDefault> 119 DebugifyAndStripAll("debugify-and-strip-all-safe", cl::Hidden, 120 cl::desc("Debugify MIR before and Strip debug after " 121 "each pass except those known to be unsafe " 122 "when debug info is present")); 123 static cl::opt<cl::boolOrDefault> DebugifyCheckAndStripAll( 124 "debugify-check-and-strip-all-safe", cl::Hidden, 125 cl::desc( 126 "Debugify MIR before, by checking and stripping the debug info after, " 127 "each pass except those known to be unsafe when debug info is " 128 "present")); 129 // Enable or disable the MachineOutliner. 130 static cl::opt<RunOutliner> EnableMachineOutliner( 131 "enable-machine-outliner", cl::desc("Enable the machine outliner"), 132 cl::Hidden, cl::ValueOptional, cl::init(RunOutliner::TargetDefault), 133 cl::values(clEnumValN(RunOutliner::AlwaysOutline, "always", 134 "Run on all functions guaranteed to be beneficial"), 135 clEnumValN(RunOutliner::NeverOutline, "never", 136 "Disable all outlining"), 137 // Sentinel value for unspecified option. 138 clEnumValN(RunOutliner::AlwaysOutline, "", ""))); 139 // Disable the pass to fix unwind information. Whether the pass is included in 140 // the pipeline is controlled via the target options, this option serves as 141 // manual override. 142 static cl::opt<bool> DisableCFIFixup("disable-cfi-fixup", cl::Hidden, 143 cl::desc("Disable the CFI fixup pass")); 144 // Enable or disable FastISel. Both options are needed, because 145 // FastISel is enabled by default with -fast, and we wish to be 146 // able to enable or disable fast-isel independently from -O0. 147 static cl::opt<cl::boolOrDefault> 148 EnableFastISelOption("fast-isel", cl::Hidden, 149 cl::desc("Enable the \"fast\" instruction selector")); 150 151 static cl::opt<cl::boolOrDefault> EnableGlobalISelOption( 152 "global-isel", cl::Hidden, 153 cl::desc("Enable the \"global\" instruction selector")); 154 155 // FIXME: remove this after switching to NPM or GlobalISel, whichever gets there 156 // first... 157 static cl::opt<bool> 158 PrintAfterISel("print-after-isel", cl::init(false), cl::Hidden, 159 cl::desc("Print machine instrs after ISel")); 160 161 static cl::opt<GlobalISelAbortMode> EnableGlobalISelAbort( 162 "global-isel-abort", cl::Hidden, 163 cl::desc("Enable abort calls when \"global\" instruction selection " 164 "fails to lower/select an instruction"), 165 cl::values( 166 clEnumValN(GlobalISelAbortMode::Disable, "0", "Disable the abort"), 167 clEnumValN(GlobalISelAbortMode::Enable, "1", "Enable the abort"), 168 clEnumValN(GlobalISelAbortMode::DisableWithDiag, "2", 169 "Disable the abort but emit a diagnostic on failure"))); 170 171 // An option that disables inserting FS-AFDO discriminators before emit. 172 // This is mainly for debugging and tuning purpose. 173 static cl::opt<bool> 174 FSNoFinalDiscrim("fs-no-final-discrim", cl::init(false), cl::Hidden, 175 cl::desc("Do not insert FS-AFDO discriminators before " 176 "emit.")); 177 // Disable MIRProfileLoader before RegAlloc. This is for for debugging and 178 // tuning purpose. 179 static cl::opt<bool> DisableRAFSProfileLoader( 180 "disable-ra-fsprofile-loader", cl::init(false), cl::Hidden, 181 cl::desc("Disable MIRProfileLoader before RegAlloc")); 182 // Disable MIRProfileLoader before BloackPlacement. This is for for debugging 183 // and tuning purpose. 184 static cl::opt<bool> DisableLayoutFSProfileLoader( 185 "disable-layout-fsprofile-loader", cl::init(false), cl::Hidden, 186 cl::desc("Disable MIRProfileLoader before BlockPlacement")); 187 // Specify FSProfile file name. 188 static cl::opt<std::string> 189 FSProfileFile("fs-profile-file", cl::init(""), cl::value_desc("filename"), 190 cl::desc("Flow Sensitive profile file name."), cl::Hidden); 191 // Specify Remapping file for FSProfile. 192 static cl::opt<std::string> FSRemappingFile( 193 "fs-remapping-file", cl::init(""), cl::value_desc("filename"), 194 cl::desc("Flow Sensitive profile remapping file name."), cl::Hidden); 195 196 // Temporary option to allow experimenting with MachineScheduler as a post-RA 197 // scheduler. Targets can "properly" enable this with 198 // substitutePass(&PostRASchedulerID, &PostMachineSchedulerID). 199 // Targets can return true in targetSchedulesPostRAScheduling() and 200 // insert a PostRA scheduling pass wherever it wants. 201 static cl::opt<bool> MISchedPostRA( 202 "misched-postra", cl::Hidden, 203 cl::desc( 204 "Run MachineScheduler post regalloc (independent of preRA sched)")); 205 206 // Experimental option to run live interval analysis early. 207 static cl::opt<bool> EarlyLiveIntervals("early-live-intervals", cl::Hidden, 208 cl::desc("Run live interval analysis earlier in the pipeline")); 209 210 /// Option names for limiting the codegen pipeline. 211 /// Those are used in error reporting and we didn't want 212 /// to duplicate their names all over the place. 213 static const char StartAfterOptName[] = "start-after"; 214 static const char StartBeforeOptName[] = "start-before"; 215 static const char StopAfterOptName[] = "stop-after"; 216 static const char StopBeforeOptName[] = "stop-before"; 217 218 static cl::opt<std::string> 219 StartAfterOpt(StringRef(StartAfterOptName), 220 cl::desc("Resume compilation after a specific pass"), 221 cl::value_desc("pass-name"), cl::init(""), cl::Hidden); 222 223 static cl::opt<std::string> 224 StartBeforeOpt(StringRef(StartBeforeOptName), 225 cl::desc("Resume compilation before a specific pass"), 226 cl::value_desc("pass-name"), cl::init(""), cl::Hidden); 227 228 static cl::opt<std::string> 229 StopAfterOpt(StringRef(StopAfterOptName), 230 cl::desc("Stop compilation after a specific pass"), 231 cl::value_desc("pass-name"), cl::init(""), cl::Hidden); 232 233 static cl::opt<std::string> 234 StopBeforeOpt(StringRef(StopBeforeOptName), 235 cl::desc("Stop compilation before a specific pass"), 236 cl::value_desc("pass-name"), cl::init(""), cl::Hidden); 237 238 /// Enable the machine function splitter pass. 239 static cl::opt<bool> EnableMachineFunctionSplitter( 240 "enable-split-machine-functions", cl::Hidden, 241 cl::desc("Split out cold blocks from machine functions based on profile " 242 "information.")); 243 244 /// Disable the expand reductions pass for testing. 245 static cl::opt<bool> DisableExpandReductions( 246 "disable-expand-reductions", cl::init(false), cl::Hidden, 247 cl::desc("Disable the expand reduction intrinsics pass from running")); 248 249 /// Disable the select optimization pass. 250 static cl::opt<bool> DisableSelectOptimize( 251 "disable-select-optimize", cl::init(true), cl::Hidden, 252 cl::desc("Disable the select-optimization pass from running")); 253 254 /// Allow standard passes to be disabled by command line options. This supports 255 /// simple binary flags that either suppress the pass or do nothing. 256 /// i.e. -disable-mypass=false has no effect. 257 /// These should be converted to boolOrDefault in order to use applyOverride. 258 static IdentifyingPassPtr applyDisable(IdentifyingPassPtr PassID, 259 bool Override) { 260 if (Override) 261 return IdentifyingPassPtr(); 262 return PassID; 263 } 264 265 /// Allow standard passes to be disabled by the command line, regardless of who 266 /// is adding the pass. 267 /// 268 /// StandardID is the pass identified in the standard pass pipeline and provided 269 /// to addPass(). It may be a target-specific ID in the case that the target 270 /// directly adds its own pass, but in that case we harmlessly fall through. 271 /// 272 /// TargetID is the pass that the target has configured to override StandardID. 273 /// 274 /// StandardID may be a pseudo ID. In that case TargetID is the name of the real 275 /// pass to run. This allows multiple options to control a single pass depending 276 /// on where in the pipeline that pass is added. 277 static IdentifyingPassPtr overridePass(AnalysisID StandardID, 278 IdentifyingPassPtr TargetID) { 279 if (StandardID == &PostRASchedulerID) 280 return applyDisable(TargetID, DisablePostRASched); 281 282 if (StandardID == &BranchFolderPassID) 283 return applyDisable(TargetID, DisableBranchFold); 284 285 if (StandardID == &TailDuplicateID) 286 return applyDisable(TargetID, DisableTailDuplicate); 287 288 if (StandardID == &EarlyTailDuplicateID) 289 return applyDisable(TargetID, DisableEarlyTailDup); 290 291 if (StandardID == &MachineBlockPlacementID) 292 return applyDisable(TargetID, DisableBlockPlacement); 293 294 if (StandardID == &StackSlotColoringID) 295 return applyDisable(TargetID, DisableSSC); 296 297 if (StandardID == &DeadMachineInstructionElimID) 298 return applyDisable(TargetID, DisableMachineDCE); 299 300 if (StandardID == &EarlyIfConverterID) 301 return applyDisable(TargetID, DisableEarlyIfConversion); 302 303 if (StandardID == &EarlyMachineLICMID) 304 return applyDisable(TargetID, DisableMachineLICM); 305 306 if (StandardID == &MachineCSEID) 307 return applyDisable(TargetID, DisableMachineCSE); 308 309 if (StandardID == &MachineLICMID) 310 return applyDisable(TargetID, DisablePostRAMachineLICM); 311 312 if (StandardID == &MachineSinkingID) 313 return applyDisable(TargetID, DisableMachineSink); 314 315 if (StandardID == &PostRAMachineSinkingID) 316 return applyDisable(TargetID, DisablePostRAMachineSink); 317 318 if (StandardID == &MachineCopyPropagationID) 319 return applyDisable(TargetID, DisableCopyProp); 320 321 return TargetID; 322 } 323 324 // Find the FSProfile file name. The internal option takes the precedence 325 // before getting from TargetMachine. 326 static std::string getFSProfileFile(const TargetMachine *TM) { 327 if (!FSProfileFile.empty()) 328 return FSProfileFile.getValue(); 329 const std::optional<PGOOptions> &PGOOpt = TM->getPGOOption(); 330 if (PGOOpt == std::nullopt || PGOOpt->Action != PGOOptions::SampleUse) 331 return std::string(); 332 return PGOOpt->ProfileFile; 333 } 334 335 // Find the Profile remapping file name. The internal option takes the 336 // precedence before getting from TargetMachine. 337 static std::string getFSRemappingFile(const TargetMachine *TM) { 338 if (!FSRemappingFile.empty()) 339 return FSRemappingFile.getValue(); 340 const std::optional<PGOOptions> &PGOOpt = TM->getPGOOption(); 341 if (PGOOpt == std::nullopt || PGOOpt->Action != PGOOptions::SampleUse) 342 return std::string(); 343 return PGOOpt->ProfileRemappingFile; 344 } 345 346 //===---------------------------------------------------------------------===// 347 /// TargetPassConfig 348 //===---------------------------------------------------------------------===// 349 350 INITIALIZE_PASS(TargetPassConfig, "targetpassconfig", 351 "Target Pass Configuration", false, false) 352 char TargetPassConfig::ID = 0; 353 354 namespace { 355 356 struct InsertedPass { 357 AnalysisID TargetPassID; 358 IdentifyingPassPtr InsertedPassID; 359 360 InsertedPass(AnalysisID TargetPassID, IdentifyingPassPtr InsertedPassID) 361 : TargetPassID(TargetPassID), InsertedPassID(InsertedPassID) {} 362 363 Pass *getInsertedPass() const { 364 assert(InsertedPassID.isValid() && "Illegal Pass ID!"); 365 if (InsertedPassID.isInstance()) 366 return InsertedPassID.getInstance(); 367 Pass *NP = Pass::createPass(InsertedPassID.getID()); 368 assert(NP && "Pass ID not registered"); 369 return NP; 370 } 371 }; 372 373 } // end anonymous namespace 374 375 namespace llvm { 376 377 extern cl::opt<bool> EnableFSDiscriminator; 378 379 class PassConfigImpl { 380 public: 381 // List of passes explicitly substituted by this target. Normally this is 382 // empty, but it is a convenient way to suppress or replace specific passes 383 // that are part of a standard pass pipeline without overridding the entire 384 // pipeline. This mechanism allows target options to inherit a standard pass's 385 // user interface. For example, a target may disable a standard pass by 386 // default by substituting a pass ID of zero, and the user may still enable 387 // that standard pass with an explicit command line option. 388 DenseMap<AnalysisID,IdentifyingPassPtr> TargetPasses; 389 390 /// Store the pairs of <AnalysisID, AnalysisID> of which the second pass 391 /// is inserted after each instance of the first one. 392 SmallVector<InsertedPass, 4> InsertedPasses; 393 }; 394 395 } // end namespace llvm 396 397 // Out of line virtual method. 398 TargetPassConfig::~TargetPassConfig() { 399 delete Impl; 400 } 401 402 static const PassInfo *getPassInfo(StringRef PassName) { 403 if (PassName.empty()) 404 return nullptr; 405 406 const PassRegistry &PR = *PassRegistry::getPassRegistry(); 407 const PassInfo *PI = PR.getPassInfo(PassName); 408 if (!PI) 409 report_fatal_error(Twine('\"') + Twine(PassName) + 410 Twine("\" pass is not registered.")); 411 return PI; 412 } 413 414 static AnalysisID getPassIDFromName(StringRef PassName) { 415 const PassInfo *PI = getPassInfo(PassName); 416 return PI ? PI->getTypeInfo() : nullptr; 417 } 418 419 static std::pair<StringRef, unsigned> 420 getPassNameAndInstanceNum(StringRef PassName) { 421 StringRef Name, InstanceNumStr; 422 std::tie(Name, InstanceNumStr) = PassName.split(','); 423 424 unsigned InstanceNum = 0; 425 if (!InstanceNumStr.empty() && InstanceNumStr.getAsInteger(10, InstanceNum)) 426 report_fatal_error("invalid pass instance specifier " + PassName); 427 428 return std::make_pair(Name, InstanceNum); 429 } 430 431 void TargetPassConfig::setStartStopPasses() { 432 StringRef StartBeforeName; 433 std::tie(StartBeforeName, StartBeforeInstanceNum) = 434 getPassNameAndInstanceNum(StartBeforeOpt); 435 436 StringRef StartAfterName; 437 std::tie(StartAfterName, StartAfterInstanceNum) = 438 getPassNameAndInstanceNum(StartAfterOpt); 439 440 StringRef StopBeforeName; 441 std::tie(StopBeforeName, StopBeforeInstanceNum) 442 = getPassNameAndInstanceNum(StopBeforeOpt); 443 444 StringRef StopAfterName; 445 std::tie(StopAfterName, StopAfterInstanceNum) 446 = getPassNameAndInstanceNum(StopAfterOpt); 447 448 StartBefore = getPassIDFromName(StartBeforeName); 449 StartAfter = getPassIDFromName(StartAfterName); 450 StopBefore = getPassIDFromName(StopBeforeName); 451 StopAfter = getPassIDFromName(StopAfterName); 452 if (StartBefore && StartAfter) 453 report_fatal_error(Twine(StartBeforeOptName) + Twine(" and ") + 454 Twine(StartAfterOptName) + Twine(" specified!")); 455 if (StopBefore && StopAfter) 456 report_fatal_error(Twine(StopBeforeOptName) + Twine(" and ") + 457 Twine(StopAfterOptName) + Twine(" specified!")); 458 Started = (StartAfter == nullptr) && (StartBefore == nullptr); 459 } 460 461 CGPassBuilderOption llvm::getCGPassBuilderOption() { 462 CGPassBuilderOption Opt; 463 464 #define SET_OPTION(Option) \ 465 if (Option.getNumOccurrences()) \ 466 Opt.Option = Option; 467 468 SET_OPTION(EnableFastISelOption) 469 SET_OPTION(EnableGlobalISelAbort) 470 SET_OPTION(EnableGlobalISelOption) 471 SET_OPTION(EnableIPRA) 472 SET_OPTION(OptimizeRegAlloc) 473 SET_OPTION(VerifyMachineCode) 474 475 #define SET_BOOLEAN_OPTION(Option) Opt.Option = Option; 476 477 SET_BOOLEAN_OPTION(EarlyLiveIntervals) 478 SET_BOOLEAN_OPTION(EnableBlockPlacementStats) 479 SET_BOOLEAN_OPTION(EnableImplicitNullChecks) 480 SET_BOOLEAN_OPTION(EnableMachineOutliner) 481 SET_BOOLEAN_OPTION(MISchedPostRA) 482 SET_BOOLEAN_OPTION(DisableMergeICmps) 483 SET_BOOLEAN_OPTION(DisableLSR) 484 SET_BOOLEAN_OPTION(DisableConstantHoisting) 485 SET_BOOLEAN_OPTION(DisableCGP) 486 SET_BOOLEAN_OPTION(DisablePartialLibcallInlining) 487 SET_BOOLEAN_OPTION(DisableSelectOptimize) 488 SET_BOOLEAN_OPTION(PrintLSR) 489 SET_BOOLEAN_OPTION(PrintISelInput) 490 SET_BOOLEAN_OPTION(PrintGCInfo) 491 492 return Opt; 493 } 494 495 static void registerPartialPipelineCallback(PassInstrumentationCallbacks &PIC, 496 LLVMTargetMachine &LLVMTM) { 497 StringRef StartBefore; 498 StringRef StartAfter; 499 StringRef StopBefore; 500 StringRef StopAfter; 501 502 unsigned StartBeforeInstanceNum = 0; 503 unsigned StartAfterInstanceNum = 0; 504 unsigned StopBeforeInstanceNum = 0; 505 unsigned StopAfterInstanceNum = 0; 506 507 std::tie(StartBefore, StartBeforeInstanceNum) = 508 getPassNameAndInstanceNum(StartBeforeOpt); 509 std::tie(StartAfter, StartAfterInstanceNum) = 510 getPassNameAndInstanceNum(StartAfterOpt); 511 std::tie(StopBefore, StopBeforeInstanceNum) = 512 getPassNameAndInstanceNum(StopBeforeOpt); 513 std::tie(StopAfter, StopAfterInstanceNum) = 514 getPassNameAndInstanceNum(StopAfterOpt); 515 516 if (StartBefore.empty() && StartAfter.empty() && StopBefore.empty() && 517 StopAfter.empty()) 518 return; 519 520 std::tie(StartBefore, std::ignore) = 521 LLVMTM.getPassNameFromLegacyName(StartBefore); 522 std::tie(StartAfter, std::ignore) = 523 LLVMTM.getPassNameFromLegacyName(StartAfter); 524 std::tie(StopBefore, std::ignore) = 525 LLVMTM.getPassNameFromLegacyName(StopBefore); 526 std::tie(StopAfter, std::ignore) = 527 LLVMTM.getPassNameFromLegacyName(StopAfter); 528 if (!StartBefore.empty() && !StartAfter.empty()) 529 report_fatal_error(Twine(StartBeforeOptName) + Twine(" and ") + 530 Twine(StartAfterOptName) + Twine(" specified!")); 531 if (!StopBefore.empty() && !StopAfter.empty()) 532 report_fatal_error(Twine(StopBeforeOptName) + Twine(" and ") + 533 Twine(StopAfterOptName) + Twine(" specified!")); 534 535 PIC.registerShouldRunOptionalPassCallback( 536 [=, EnableCurrent = StartBefore.empty() && StartAfter.empty(), 537 EnableNext = std::optional<bool>(), StartBeforeCount = 0u, 538 StartAfterCount = 0u, StopBeforeCount = 0u, 539 StopAfterCount = 0u](StringRef P, Any) mutable { 540 bool StartBeforePass = !StartBefore.empty() && P.contains(StartBefore); 541 bool StartAfterPass = !StartAfter.empty() && P.contains(StartAfter); 542 bool StopBeforePass = !StopBefore.empty() && P.contains(StopBefore); 543 bool StopAfterPass = !StopAfter.empty() && P.contains(StopAfter); 544 545 // Implement -start-after/-stop-after 546 if (EnableNext) { 547 EnableCurrent = *EnableNext; 548 EnableNext.reset(); 549 } 550 551 // Using PIC.registerAfterPassCallback won't work because if this 552 // callback returns false, AfterPassCallback is also skipped. 553 if (StartAfterPass && StartAfterCount++ == StartAfterInstanceNum) { 554 assert(!EnableNext && "Error: assign to EnableNext more than once"); 555 EnableNext = true; 556 } 557 if (StopAfterPass && StopAfterCount++ == StopAfterInstanceNum) { 558 assert(!EnableNext && "Error: assign to EnableNext more than once"); 559 EnableNext = false; 560 } 561 562 if (StartBeforePass && StartBeforeCount++ == StartBeforeInstanceNum) 563 EnableCurrent = true; 564 if (StopBeforePass && StopBeforeCount++ == StopBeforeInstanceNum) 565 EnableCurrent = false; 566 return EnableCurrent; 567 }); 568 } 569 570 void llvm::registerCodeGenCallback(PassInstrumentationCallbacks &PIC, 571 LLVMTargetMachine &LLVMTM) { 572 573 // Register a callback for disabling passes. 574 PIC.registerShouldRunOptionalPassCallback([](StringRef P, Any) { 575 576 #define DISABLE_PASS(Option, Name) \ 577 if (Option && P.contains(#Name)) \ 578 return false; 579 DISABLE_PASS(DisableBlockPlacement, MachineBlockPlacementPass) 580 DISABLE_PASS(DisableBranchFold, BranchFolderPass) 581 DISABLE_PASS(DisableCopyProp, MachineCopyPropagationPass) 582 DISABLE_PASS(DisableEarlyIfConversion, EarlyIfConverterPass) 583 DISABLE_PASS(DisableEarlyTailDup, EarlyTailDuplicatePass) 584 DISABLE_PASS(DisableMachineCSE, MachineCSEPass) 585 DISABLE_PASS(DisableMachineDCE, DeadMachineInstructionElimPass) 586 DISABLE_PASS(DisableMachineLICM, EarlyMachineLICMPass) 587 DISABLE_PASS(DisableMachineSink, MachineSinkingPass) 588 DISABLE_PASS(DisablePostRAMachineLICM, MachineLICMPass) 589 DISABLE_PASS(DisablePostRAMachineSink, PostRAMachineSinkingPass) 590 DISABLE_PASS(DisablePostRASched, PostRASchedulerPass) 591 DISABLE_PASS(DisableSSC, StackSlotColoringPass) 592 DISABLE_PASS(DisableTailDuplicate, TailDuplicatePass) 593 594 return true; 595 }); 596 597 registerPartialPipelineCallback(PIC, LLVMTM); 598 } 599 600 // Out of line constructor provides default values for pass options and 601 // registers all common codegen passes. 602 TargetPassConfig::TargetPassConfig(LLVMTargetMachine &TM, PassManagerBase &pm) 603 : ImmutablePass(ID), PM(&pm), TM(&TM) { 604 Impl = new PassConfigImpl(); 605 606 // Register all target independent codegen passes to activate their PassIDs, 607 // including this pass itself. 608 initializeCodeGen(*PassRegistry::getPassRegistry()); 609 610 // Also register alias analysis passes required by codegen passes. 611 initializeBasicAAWrapperPassPass(*PassRegistry::getPassRegistry()); 612 initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry()); 613 614 if (EnableIPRA.getNumOccurrences()) 615 TM.Options.EnableIPRA = EnableIPRA; 616 else { 617 // If not explicitly specified, use target default. 618 TM.Options.EnableIPRA |= TM.useIPRA(); 619 } 620 621 if (TM.Options.EnableIPRA) 622 setRequiresCodeGenSCCOrder(); 623 624 if (EnableGlobalISelAbort.getNumOccurrences()) 625 TM.Options.GlobalISelAbort = EnableGlobalISelAbort; 626 627 setStartStopPasses(); 628 } 629 630 CodeGenOpt::Level TargetPassConfig::getOptLevel() const { 631 return TM->getOptLevel(); 632 } 633 634 /// Insert InsertedPassID pass after TargetPassID. 635 void TargetPassConfig::insertPass(AnalysisID TargetPassID, 636 IdentifyingPassPtr InsertedPassID) { 637 assert(((!InsertedPassID.isInstance() && 638 TargetPassID != InsertedPassID.getID()) || 639 (InsertedPassID.isInstance() && 640 TargetPassID != InsertedPassID.getInstance()->getPassID())) && 641 "Insert a pass after itself!"); 642 Impl->InsertedPasses.emplace_back(TargetPassID, InsertedPassID); 643 } 644 645 /// createPassConfig - Create a pass configuration object to be used by 646 /// addPassToEmitX methods for generating a pipeline of CodeGen passes. 647 /// 648 /// Targets may override this to extend TargetPassConfig. 649 TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) { 650 return new TargetPassConfig(*this, PM); 651 } 652 653 TargetPassConfig::TargetPassConfig() 654 : ImmutablePass(ID) { 655 report_fatal_error("Trying to construct TargetPassConfig without a target " 656 "machine. Scheduling a CodeGen pass without a target " 657 "triple set?"); 658 } 659 660 bool TargetPassConfig::willCompleteCodeGenPipeline() { 661 return StopBeforeOpt.empty() && StopAfterOpt.empty(); 662 } 663 664 bool TargetPassConfig::hasLimitedCodeGenPipeline() { 665 return !StartBeforeOpt.empty() || !StartAfterOpt.empty() || 666 !willCompleteCodeGenPipeline(); 667 } 668 669 std::string 670 TargetPassConfig::getLimitedCodeGenPipelineReason(const char *Separator) { 671 if (!hasLimitedCodeGenPipeline()) 672 return std::string(); 673 std::string Res; 674 static cl::opt<std::string> *PassNames[] = {&StartAfterOpt, &StartBeforeOpt, 675 &StopAfterOpt, &StopBeforeOpt}; 676 static const char *OptNames[] = {StartAfterOptName, StartBeforeOptName, 677 StopAfterOptName, StopBeforeOptName}; 678 bool IsFirst = true; 679 for (int Idx = 0; Idx < 4; ++Idx) 680 if (!PassNames[Idx]->empty()) { 681 if (!IsFirst) 682 Res += Separator; 683 IsFirst = false; 684 Res += OptNames[Idx]; 685 } 686 return Res; 687 } 688 689 // Helper to verify the analysis is really immutable. 690 void TargetPassConfig::setOpt(bool &Opt, bool Val) { 691 assert(!Initialized && "PassConfig is immutable"); 692 Opt = Val; 693 } 694 695 void TargetPassConfig::substitutePass(AnalysisID StandardID, 696 IdentifyingPassPtr TargetID) { 697 Impl->TargetPasses[StandardID] = TargetID; 698 } 699 700 IdentifyingPassPtr TargetPassConfig::getPassSubstitution(AnalysisID ID) const { 701 DenseMap<AnalysisID, IdentifyingPassPtr>::const_iterator 702 I = Impl->TargetPasses.find(ID); 703 if (I == Impl->TargetPasses.end()) 704 return ID; 705 return I->second; 706 } 707 708 bool TargetPassConfig::isPassSubstitutedOrOverridden(AnalysisID ID) const { 709 IdentifyingPassPtr TargetID = getPassSubstitution(ID); 710 IdentifyingPassPtr FinalPtr = overridePass(ID, TargetID); 711 return !FinalPtr.isValid() || FinalPtr.isInstance() || 712 FinalPtr.getID() != ID; 713 } 714 715 /// Add a pass to the PassManager if that pass is supposed to be run. If the 716 /// Started/Stopped flags indicate either that the compilation should start at 717 /// a later pass or that it should stop after an earlier pass, then do not add 718 /// the pass. Finally, compare the current pass against the StartAfter 719 /// and StopAfter options and change the Started/Stopped flags accordingly. 720 void TargetPassConfig::addPass(Pass *P) { 721 assert(!Initialized && "PassConfig is immutable"); 722 723 // Cache the Pass ID here in case the pass manager finds this pass is 724 // redundant with ones already scheduled / available, and deletes it. 725 // Fundamentally, once we add the pass to the manager, we no longer own it 726 // and shouldn't reference it. 727 AnalysisID PassID = P->getPassID(); 728 729 if (StartBefore == PassID && StartBeforeCount++ == StartBeforeInstanceNum) 730 Started = true; 731 if (StopBefore == PassID && StopBeforeCount++ == StopBeforeInstanceNum) 732 Stopped = true; 733 if (Started && !Stopped) { 734 if (AddingMachinePasses) { 735 // Construct banner message before PM->add() as that may delete the pass. 736 std::string Banner = 737 std::string("After ") + std::string(P->getPassName()); 738 addMachinePrePasses(); 739 PM->add(P); 740 addMachinePostPasses(Banner); 741 } else { 742 PM->add(P); 743 } 744 745 // Add the passes after the pass P if there is any. 746 for (const auto &IP : Impl->InsertedPasses) 747 if (IP.TargetPassID == PassID) 748 addPass(IP.getInsertedPass()); 749 } else { 750 delete P; 751 } 752 753 if (StopAfter == PassID && StopAfterCount++ == StopAfterInstanceNum) 754 Stopped = true; 755 756 if (StartAfter == PassID && StartAfterCount++ == StartAfterInstanceNum) 757 Started = true; 758 if (Stopped && !Started) 759 report_fatal_error("Cannot stop compilation after pass that is not run"); 760 } 761 762 /// Add a CodeGen pass at this point in the pipeline after checking for target 763 /// and command line overrides. 764 /// 765 /// addPass cannot return a pointer to the pass instance because is internal the 766 /// PassManager and the instance we create here may already be freed. 767 AnalysisID TargetPassConfig::addPass(AnalysisID PassID) { 768 IdentifyingPassPtr TargetID = getPassSubstitution(PassID); 769 IdentifyingPassPtr FinalPtr = overridePass(PassID, TargetID); 770 if (!FinalPtr.isValid()) 771 return nullptr; 772 773 Pass *P; 774 if (FinalPtr.isInstance()) 775 P = FinalPtr.getInstance(); 776 else { 777 P = Pass::createPass(FinalPtr.getID()); 778 if (!P) 779 llvm_unreachable("Pass ID not registered"); 780 } 781 AnalysisID FinalID = P->getPassID(); 782 addPass(P); // Ends the lifetime of P. 783 784 return FinalID; 785 } 786 787 void TargetPassConfig::printAndVerify(const std::string &Banner) { 788 addPrintPass(Banner); 789 addVerifyPass(Banner); 790 } 791 792 void TargetPassConfig::addPrintPass(const std::string &Banner) { 793 if (PrintAfterISel) 794 PM->add(createMachineFunctionPrinterPass(dbgs(), Banner)); 795 } 796 797 void TargetPassConfig::addVerifyPass(const std::string &Banner) { 798 bool Verify = VerifyMachineCode == cl::BOU_TRUE; 799 #ifdef EXPENSIVE_CHECKS 800 if (VerifyMachineCode == cl::BOU_UNSET) 801 Verify = TM->isMachineVerifierClean(); 802 #endif 803 if (Verify) 804 PM->add(createMachineVerifierPass(Banner)); 805 } 806 807 void TargetPassConfig::addDebugifyPass() { 808 PM->add(createDebugifyMachineModulePass()); 809 } 810 811 void TargetPassConfig::addStripDebugPass() { 812 PM->add(createStripDebugMachineModulePass(/*OnlyDebugified=*/true)); 813 } 814 815 void TargetPassConfig::addCheckDebugPass() { 816 PM->add(createCheckDebugMachineModulePass()); 817 } 818 819 void TargetPassConfig::addMachinePrePasses(bool AllowDebugify) { 820 if (AllowDebugify && DebugifyIsSafe && 821 (DebugifyAndStripAll == cl::BOU_TRUE || 822 DebugifyCheckAndStripAll == cl::BOU_TRUE)) 823 addDebugifyPass(); 824 } 825 826 void TargetPassConfig::addMachinePostPasses(const std::string &Banner) { 827 if (DebugifyIsSafe) { 828 if (DebugifyCheckAndStripAll == cl::BOU_TRUE) { 829 addCheckDebugPass(); 830 addStripDebugPass(); 831 } else if (DebugifyAndStripAll == cl::BOU_TRUE) 832 addStripDebugPass(); 833 } 834 addVerifyPass(Banner); 835 } 836 837 /// Add common target configurable passes that perform LLVM IR to IR transforms 838 /// following machine independent optimization. 839 void TargetPassConfig::addIRPasses() { 840 // Before running any passes, run the verifier to determine if the input 841 // coming from the front-end and/or optimizer is valid. 842 if (!DisableVerify) 843 addPass(createVerifierPass()); 844 845 if (getOptLevel() != CodeGenOpt::None) { 846 // Basic AliasAnalysis support. 847 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that 848 // BasicAliasAnalysis wins if they disagree. This is intended to help 849 // support "obvious" type-punning idioms. 850 addPass(createTypeBasedAAWrapperPass()); 851 addPass(createScopedNoAliasAAWrapperPass()); 852 addPass(createBasicAAWrapperPass()); 853 854 // Run loop strength reduction before anything else. 855 if (!DisableLSR) { 856 addPass(createCanonicalizeFreezeInLoopsPass()); 857 addPass(createLoopStrengthReducePass()); 858 if (PrintLSR) 859 addPass(createPrintFunctionPass(dbgs(), 860 "\n\n*** Code after LSR ***\n")); 861 } 862 863 // The MergeICmpsPass tries to create memcmp calls by grouping sequences of 864 // loads and compares. ExpandMemCmpPass then tries to expand those calls 865 // into optimally-sized loads and compares. The transforms are enabled by a 866 // target lowering hook. 867 if (!DisableMergeICmps) 868 addPass(createMergeICmpsLegacyPass()); 869 addPass(createExpandMemCmpPass()); 870 } 871 872 // Run GC lowering passes for builtin collectors 873 // TODO: add a pass insertion point here 874 addPass(&GCLoweringID); 875 addPass(&ShadowStackGCLoweringID); 876 addPass(createLowerConstantIntrinsicsPass()); 877 878 // For MachO, lower @llvm.global_dtors into @llvm.global_ctors with 879 // __cxa_atexit() calls to avoid emitting the deprecated __mod_term_func. 880 if (TM->getTargetTriple().isOSBinFormatMachO() && 881 TM->Options.LowerGlobalDtorsViaCxaAtExit) 882 addPass(createLowerGlobalDtorsLegacyPass()); 883 884 // Make sure that no unreachable blocks are instruction selected. 885 addPass(createUnreachableBlockEliminationPass()); 886 887 // Prepare expensive constants for SelectionDAG. 888 if (getOptLevel() != CodeGenOpt::None && !DisableConstantHoisting) 889 addPass(createConstantHoistingPass()); 890 891 if (getOptLevel() != CodeGenOpt::None) 892 addPass(createReplaceWithVeclibLegacyPass()); 893 894 if (getOptLevel() != CodeGenOpt::None && !DisablePartialLibcallInlining) 895 addPass(createPartiallyInlineLibCallsPass()); 896 897 // Expand vector predication intrinsics into standard IR instructions. 898 // This pass has to run before ScalarizeMaskedMemIntrin and ExpandReduction 899 // passes since it emits those kinds of intrinsics. 900 addPass(createExpandVectorPredicationPass()); 901 902 // Add scalarization of target's unsupported masked memory intrinsics pass. 903 // the unsupported intrinsic will be replaced with a chain of basic blocks, 904 // that stores/loads element one-by-one if the appropriate mask bit is set. 905 addPass(createScalarizeMaskedMemIntrinLegacyPass()); 906 907 // Expand reduction intrinsics into shuffle sequences if the target wants to. 908 // Allow disabling it for testing purposes. 909 if (!DisableExpandReductions) 910 addPass(createExpandReductionsPass()); 911 912 if (getOptLevel() != CodeGenOpt::None) 913 addPass(createTLSVariableHoistPass()); 914 915 // Convert conditional moves to conditional jumps when profitable. 916 if (getOptLevel() != CodeGenOpt::None && !DisableSelectOptimize) 917 addPass(createSelectOptimizePass()); 918 } 919 920 /// Turn exception handling constructs into something the code generators can 921 /// handle. 922 void TargetPassConfig::addPassesToHandleExceptions() { 923 const MCAsmInfo *MCAI = TM->getMCAsmInfo(); 924 assert(MCAI && "No MCAsmInfo"); 925 switch (MCAI->getExceptionHandlingType()) { 926 case ExceptionHandling::SjLj: 927 // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both 928 // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise, 929 // catch info can get misplaced when a selector ends up more than one block 930 // removed from the parent invoke(s). This could happen when a landing 931 // pad is shared by multiple invokes and is also a target of a normal 932 // edge from elsewhere. 933 addPass(createSjLjEHPreparePass(TM)); 934 [[fallthrough]]; 935 case ExceptionHandling::DwarfCFI: 936 case ExceptionHandling::ARM: 937 case ExceptionHandling::AIX: 938 addPass(createDwarfEHPass(getOptLevel())); 939 break; 940 case ExceptionHandling::WinEH: 941 // We support using both GCC-style and MSVC-style exceptions on Windows, so 942 // add both preparation passes. Each pass will only actually run if it 943 // recognizes the personality function. 944 addPass(createWinEHPass()); 945 addPass(createDwarfEHPass(getOptLevel())); 946 break; 947 case ExceptionHandling::Wasm: 948 // Wasm EH uses Windows EH instructions, but it does not need to demote PHIs 949 // on catchpads and cleanuppads because it does not outline them into 950 // funclets. Catchswitch blocks are not lowered in SelectionDAG, so we 951 // should remove PHIs there. 952 addPass(createWinEHPass(/*DemoteCatchSwitchPHIOnly=*/false)); 953 addPass(createWasmEHPass()); 954 break; 955 case ExceptionHandling::None: 956 addPass(createLowerInvokePass()); 957 958 // The lower invoke pass may create unreachable code. Remove it. 959 addPass(createUnreachableBlockEliminationPass()); 960 break; 961 } 962 } 963 964 /// Add pass to prepare the LLVM IR for code generation. This should be done 965 /// before exception handling preparation passes. 966 void TargetPassConfig::addCodeGenPrepare() { 967 if (getOptLevel() != CodeGenOpt::None && !DisableCGP) 968 addPass(createCodeGenPreparePass()); 969 } 970 971 /// Add common passes that perform LLVM IR to IR transforms in preparation for 972 /// instruction selection. 973 void TargetPassConfig::addISelPrepare() { 974 addPreISel(); 975 976 // Force codegen to run according to the callgraph. 977 if (requiresCodeGenSCCOrder()) 978 addPass(new DummyCGSCCPass); 979 980 // Add both the safe stack and the stack protection passes: each of them will 981 // only protect functions that have corresponding attributes. 982 addPass(createSafeStackPass()); 983 addPass(createStackProtectorPass()); 984 985 if (PrintISelInput) 986 addPass(createPrintFunctionPass( 987 dbgs(), "\n\n*** Final LLVM Code input to ISel ***\n")); 988 989 // All passes which modify the LLVM IR are now complete; run the verifier 990 // to ensure that the IR is valid. 991 if (!DisableVerify) 992 addPass(createVerifierPass()); 993 } 994 995 bool TargetPassConfig::addCoreISelPasses() { 996 // Enable FastISel with -fast-isel, but allow that to be overridden. 997 TM->setO0WantsFastISel(EnableFastISelOption != cl::BOU_FALSE); 998 999 // Determine an instruction selector. 1000 enum class SelectorType { SelectionDAG, FastISel, GlobalISel }; 1001 SelectorType Selector; 1002 1003 if (EnableFastISelOption == cl::BOU_TRUE) 1004 Selector = SelectorType::FastISel; 1005 else if (EnableGlobalISelOption == cl::BOU_TRUE || 1006 (TM->Options.EnableGlobalISel && 1007 EnableGlobalISelOption != cl::BOU_FALSE)) 1008 Selector = SelectorType::GlobalISel; 1009 else if (TM->getOptLevel() == CodeGenOpt::None && TM->getO0WantsFastISel()) 1010 Selector = SelectorType::FastISel; 1011 else 1012 Selector = SelectorType::SelectionDAG; 1013 1014 // Set consistently TM->Options.EnableFastISel and EnableGlobalISel. 1015 if (Selector == SelectorType::FastISel) { 1016 TM->setFastISel(true); 1017 TM->setGlobalISel(false); 1018 } else if (Selector == SelectorType::GlobalISel) { 1019 TM->setFastISel(false); 1020 TM->setGlobalISel(true); 1021 } 1022 1023 // FIXME: Injecting into the DAGISel pipeline seems to cause issues with 1024 // analyses needing to be re-run. This can result in being unable to 1025 // schedule passes (particularly with 'Function Alias Analysis 1026 // Results'). It's not entirely clear why but AFAICT this seems to be 1027 // due to one FunctionPassManager not being able to use analyses from a 1028 // previous one. As we're injecting a ModulePass we break the usual 1029 // pass manager into two. GlobalISel with the fallback path disabled 1030 // and -run-pass seem to be unaffected. The majority of GlobalISel 1031 // testing uses -run-pass so this probably isn't too bad. 1032 SaveAndRestore SavedDebugifyIsSafe(DebugifyIsSafe); 1033 if (Selector != SelectorType::GlobalISel || !isGlobalISelAbortEnabled()) 1034 DebugifyIsSafe = false; 1035 1036 // Add instruction selector passes. 1037 if (Selector == SelectorType::GlobalISel) { 1038 SaveAndRestore SavedAddingMachinePasses(AddingMachinePasses, true); 1039 if (addIRTranslator()) 1040 return true; 1041 1042 addPreLegalizeMachineIR(); 1043 1044 if (addLegalizeMachineIR()) 1045 return true; 1046 1047 // Before running the register bank selector, ask the target if it 1048 // wants to run some passes. 1049 addPreRegBankSelect(); 1050 1051 if (addRegBankSelect()) 1052 return true; 1053 1054 addPreGlobalInstructionSelect(); 1055 1056 if (addGlobalInstructionSelect()) 1057 return true; 1058 1059 // Pass to reset the MachineFunction if the ISel failed. 1060 addPass(createResetMachineFunctionPass( 1061 reportDiagnosticWhenGlobalISelFallback(), isGlobalISelAbortEnabled())); 1062 1063 // Provide a fallback path when we do not want to abort on 1064 // not-yet-supported input. 1065 if (!isGlobalISelAbortEnabled() && addInstSelector()) 1066 return true; 1067 1068 } else if (addInstSelector()) 1069 return true; 1070 1071 // Expand pseudo-instructions emitted by ISel. Don't run the verifier before 1072 // FinalizeISel. 1073 addPass(&FinalizeISelID); 1074 1075 // Print the instruction selected machine code... 1076 printAndVerify("After Instruction Selection"); 1077 1078 return false; 1079 } 1080 1081 bool TargetPassConfig::addISelPasses() { 1082 if (TM->useEmulatedTLS()) 1083 addPass(createLowerEmuTLSPass()); 1084 1085 addPass(createPreISelIntrinsicLoweringPass()); 1086 PM->add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis())); 1087 addPass(createExpandLargeDivRemPass()); 1088 addPass(createExpandLargeFpConvertPass()); 1089 addIRPasses(); 1090 addCodeGenPrepare(); 1091 addPassesToHandleExceptions(); 1092 addISelPrepare(); 1093 1094 return addCoreISelPasses(); 1095 } 1096 1097 /// -regalloc=... command line option. 1098 static FunctionPass *useDefaultRegisterAllocator() { return nullptr; } 1099 static cl::opt<RegisterRegAlloc::FunctionPassCtor, false, 1100 RegisterPassParser<RegisterRegAlloc>> 1101 RegAlloc("regalloc", cl::Hidden, cl::init(&useDefaultRegisterAllocator), 1102 cl::desc("Register allocator to use")); 1103 1104 /// Add the complete set of target-independent postISel code generator passes. 1105 /// 1106 /// This can be read as the standard order of major LLVM CodeGen stages. Stages 1107 /// with nontrivial configuration or multiple passes are broken out below in 1108 /// add%Stage routines. 1109 /// 1110 /// Any TargetPassConfig::addXX routine may be overriden by the Target. The 1111 /// addPre/Post methods with empty header implementations allow injecting 1112 /// target-specific fixups just before or after major stages. Additionally, 1113 /// targets have the flexibility to change pass order within a stage by 1114 /// overriding default implementation of add%Stage routines below. Each 1115 /// technique has maintainability tradeoffs because alternate pass orders are 1116 /// not well supported. addPre/Post works better if the target pass is easily 1117 /// tied to a common pass. But if it has subtle dependencies on multiple passes, 1118 /// the target should override the stage instead. 1119 /// 1120 /// TODO: We could use a single addPre/Post(ID) hook to allow pass injection 1121 /// before/after any target-independent pass. But it's currently overkill. 1122 void TargetPassConfig::addMachinePasses() { 1123 AddingMachinePasses = true; 1124 1125 // Add passes that optimize machine instructions in SSA form. 1126 if (getOptLevel() != CodeGenOpt::None) { 1127 addMachineSSAOptimization(); 1128 } else { 1129 // If the target requests it, assign local variables to stack slots relative 1130 // to one another and simplify frame index references where possible. 1131 addPass(&LocalStackSlotAllocationID); 1132 } 1133 1134 if (TM->Options.EnableIPRA) 1135 addPass(createRegUsageInfoPropPass()); 1136 1137 // Run pre-ra passes. 1138 addPreRegAlloc(); 1139 1140 // Debugifying the register allocator passes seems to provoke some 1141 // non-determinism that affects CodeGen and there doesn't seem to be a point 1142 // where it becomes safe again so stop debugifying here. 1143 DebugifyIsSafe = false; 1144 1145 // Add a FSDiscriminator pass right before RA, so that we could get 1146 // more precise SampleFDO profile for RA. 1147 if (EnableFSDiscriminator) { 1148 addPass(createMIRAddFSDiscriminatorsPass( 1149 sampleprof::FSDiscriminatorPass::Pass1)); 1150 const std::string ProfileFile = getFSProfileFile(TM); 1151 if (!ProfileFile.empty() && !DisableRAFSProfileLoader) 1152 addPass( 1153 createMIRProfileLoaderPass(ProfileFile, getFSRemappingFile(TM), 1154 sampleprof::FSDiscriminatorPass::Pass1)); 1155 } 1156 1157 // Run register allocation and passes that are tightly coupled with it, 1158 // including phi elimination and scheduling. 1159 if (getOptimizeRegAlloc()) 1160 addOptimizedRegAlloc(); 1161 else 1162 addFastRegAlloc(); 1163 1164 // Run post-ra passes. 1165 addPostRegAlloc(); 1166 1167 addPass(&RemoveRedundantDebugValuesID); 1168 1169 addPass(&FixupStatepointCallerSavedID); 1170 1171 // Insert prolog/epilog code. Eliminate abstract frame index references... 1172 if (getOptLevel() != CodeGenOpt::None) { 1173 addPass(&PostRAMachineSinkingID); 1174 addPass(&ShrinkWrapID); 1175 } 1176 1177 // Prolog/Epilog inserter needs a TargetMachine to instantiate. But only 1178 // do so if it hasn't been disabled, substituted, or overridden. 1179 if (!isPassSubstitutedOrOverridden(&PrologEpilogCodeInserterID)) 1180 addPass(createPrologEpilogInserterPass()); 1181 1182 /// Add passes that optimize machine instructions after register allocation. 1183 if (getOptLevel() != CodeGenOpt::None) 1184 addMachineLateOptimization(); 1185 1186 // Expand pseudo instructions before second scheduling pass. 1187 addPass(&ExpandPostRAPseudosID); 1188 1189 // Run pre-sched2 passes. 1190 addPreSched2(); 1191 1192 if (EnableImplicitNullChecks) 1193 addPass(&ImplicitNullChecksID); 1194 1195 // Second pass scheduler. 1196 // Let Target optionally insert this pass by itself at some other 1197 // point. 1198 if (getOptLevel() != CodeGenOpt::None && 1199 !TM->targetSchedulesPostRAScheduling()) { 1200 if (MISchedPostRA) 1201 addPass(&PostMachineSchedulerID); 1202 else 1203 addPass(&PostRASchedulerID); 1204 } 1205 1206 // GC 1207 if (addGCPasses()) { 1208 if (PrintGCInfo) 1209 addPass(createGCInfoPrinter(dbgs())); 1210 } 1211 1212 // Basic block placement. 1213 if (getOptLevel() != CodeGenOpt::None) 1214 addBlockPlacement(); 1215 1216 // Insert before XRay Instrumentation. 1217 addPass(&FEntryInserterID); 1218 1219 addPass(&XRayInstrumentationID); 1220 addPass(&PatchableFunctionID); 1221 1222 if (EnableFSDiscriminator && !FSNoFinalDiscrim) 1223 // Add FS discriminators here so that all the instruction duplicates 1224 // in different BBs get their own discriminators. With this, we can "sum" 1225 // the SampleFDO counters instead of using MAX. This will improve the 1226 // SampleFDO profile quality. 1227 addPass(createMIRAddFSDiscriminatorsPass( 1228 sampleprof::FSDiscriminatorPass::PassLast)); 1229 1230 addPreEmitPass(); 1231 1232 if (TM->Options.EnableIPRA) 1233 // Collect register usage information and produce a register mask of 1234 // clobbered registers, to be used to optimize call sites. 1235 addPass(createRegUsageInfoCollector()); 1236 1237 // FIXME: Some backends are incompatible with running the verifier after 1238 // addPreEmitPass. Maybe only pass "false" here for those targets? 1239 addPass(&FuncletLayoutID); 1240 1241 addPass(&StackMapLivenessID); 1242 addPass(&LiveDebugValuesID); 1243 addPass(&MachineSanitizerBinaryMetadataID); 1244 1245 if (TM->Options.EnableMachineOutliner && getOptLevel() != CodeGenOpt::None && 1246 EnableMachineOutliner != RunOutliner::NeverOutline) { 1247 bool RunOnAllFunctions = 1248 (EnableMachineOutliner == RunOutliner::AlwaysOutline); 1249 bool AddOutliner = 1250 RunOnAllFunctions || TM->Options.SupportsDefaultOutlining; 1251 if (AddOutliner) 1252 addPass(createMachineOutlinerPass(RunOnAllFunctions)); 1253 } 1254 1255 // Machine function splitter uses the basic block sections feature. Both 1256 // cannot be enabled at the same time. Basic block sections takes precedence. 1257 // FIXME: In principle, BasicBlockSection::Labels and splitting can used 1258 // together. Update this check once we have addressed any issues. 1259 if (TM->getBBSectionsType() != llvm::BasicBlockSection::None) { 1260 if (TM->getBBSectionsType() == llvm::BasicBlockSection::List) { 1261 addPass(llvm::createBasicBlockSectionsProfileReaderPass( 1262 TM->getBBSectionsFuncListBuf())); 1263 } 1264 addPass(llvm::createBasicBlockSectionsPass()); 1265 } else if (TM->Options.EnableMachineFunctionSplitter || 1266 EnableMachineFunctionSplitter) { 1267 addPass(createMachineFunctionSplitterPass()); 1268 } 1269 1270 if (!DisableCFIFixup && TM->Options.EnableCFIFixup) 1271 addPass(createCFIFixup()); 1272 1273 PM->add(createStackFrameLayoutAnalysisPass()); 1274 1275 // Add passes that directly emit MI after all other MI passes. 1276 addPreEmitPass2(); 1277 1278 AddingMachinePasses = false; 1279 } 1280 1281 /// Add passes that optimize machine instructions in SSA form. 1282 void TargetPassConfig::addMachineSSAOptimization() { 1283 // Pre-ra tail duplication. 1284 addPass(&EarlyTailDuplicateID); 1285 1286 // Optimize PHIs before DCE: removing dead PHI cycles may make more 1287 // instructions dead. 1288 addPass(&OptimizePHIsID); 1289 1290 // This pass merges large allocas. StackSlotColoring is a different pass 1291 // which merges spill slots. 1292 addPass(&StackColoringID); 1293 1294 // If the target requests it, assign local variables to stack slots relative 1295 // to one another and simplify frame index references where possible. 1296 addPass(&LocalStackSlotAllocationID); 1297 1298 // With optimization, dead code should already be eliminated. However 1299 // there is one known exception: lowered code for arguments that are only 1300 // used by tail calls, where the tail calls reuse the incoming stack 1301 // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll). 1302 addPass(&DeadMachineInstructionElimID); 1303 1304 // Allow targets to insert passes that improve instruction level parallelism, 1305 // like if-conversion. Such passes will typically need dominator trees and 1306 // loop info, just like LICM and CSE below. 1307 addILPOpts(); 1308 1309 addPass(&EarlyMachineLICMID); 1310 addPass(&MachineCSEID); 1311 1312 addPass(&MachineSinkingID); 1313 1314 addPass(&PeepholeOptimizerID); 1315 // Clean-up the dead code that may have been generated by peephole 1316 // rewriting. 1317 addPass(&DeadMachineInstructionElimID); 1318 } 1319 1320 //===---------------------------------------------------------------------===// 1321 /// Register Allocation Pass Configuration 1322 //===---------------------------------------------------------------------===// 1323 1324 bool TargetPassConfig::getOptimizeRegAlloc() const { 1325 switch (OptimizeRegAlloc) { 1326 case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None; 1327 case cl::BOU_TRUE: return true; 1328 case cl::BOU_FALSE: return false; 1329 } 1330 llvm_unreachable("Invalid optimize-regalloc state"); 1331 } 1332 1333 /// A dummy default pass factory indicates whether the register allocator is 1334 /// overridden on the command line. 1335 static llvm::once_flag InitializeDefaultRegisterAllocatorFlag; 1336 1337 static RegisterRegAlloc 1338 defaultRegAlloc("default", 1339 "pick register allocator based on -O option", 1340 useDefaultRegisterAllocator); 1341 1342 static void initializeDefaultRegisterAllocatorOnce() { 1343 if (!RegisterRegAlloc::getDefault()) 1344 RegisterRegAlloc::setDefault(RegAlloc); 1345 } 1346 1347 /// Instantiate the default register allocator pass for this target for either 1348 /// the optimized or unoptimized allocation path. This will be added to the pass 1349 /// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc 1350 /// in the optimized case. 1351 /// 1352 /// A target that uses the standard regalloc pass order for fast or optimized 1353 /// allocation may still override this for per-target regalloc 1354 /// selection. But -regalloc=... always takes precedence. 1355 FunctionPass *TargetPassConfig::createTargetRegisterAllocator(bool Optimized) { 1356 if (Optimized) 1357 return createGreedyRegisterAllocator(); 1358 else 1359 return createFastRegisterAllocator(); 1360 } 1361 1362 /// Find and instantiate the register allocation pass requested by this target 1363 /// at the current optimization level. Different register allocators are 1364 /// defined as separate passes because they may require different analysis. 1365 /// 1366 /// This helper ensures that the regalloc= option is always available, 1367 /// even for targets that override the default allocator. 1368 /// 1369 /// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs, 1370 /// this can be folded into addPass. 1371 FunctionPass *TargetPassConfig::createRegAllocPass(bool Optimized) { 1372 // Initialize the global default. 1373 llvm::call_once(InitializeDefaultRegisterAllocatorFlag, 1374 initializeDefaultRegisterAllocatorOnce); 1375 1376 RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault(); 1377 if (Ctor != useDefaultRegisterAllocator) 1378 return Ctor(); 1379 1380 // With no -regalloc= override, ask the target for a regalloc pass. 1381 return createTargetRegisterAllocator(Optimized); 1382 } 1383 1384 bool TargetPassConfig::isCustomizedRegAlloc() { 1385 return RegAlloc != 1386 (RegisterRegAlloc::FunctionPassCtor)&useDefaultRegisterAllocator; 1387 } 1388 1389 bool TargetPassConfig::addRegAssignAndRewriteFast() { 1390 if (RegAlloc != (RegisterRegAlloc::FunctionPassCtor)&useDefaultRegisterAllocator && 1391 RegAlloc != (RegisterRegAlloc::FunctionPassCtor)&createFastRegisterAllocator) 1392 report_fatal_error("Must use fast (default) register allocator for unoptimized regalloc."); 1393 1394 addPass(createRegAllocPass(false)); 1395 1396 // Allow targets to change the register assignments after 1397 // fast register allocation. 1398 addPostFastRegAllocRewrite(); 1399 return true; 1400 } 1401 1402 bool TargetPassConfig::addRegAssignAndRewriteOptimized() { 1403 // Add the selected register allocation pass. 1404 addPass(createRegAllocPass(true)); 1405 1406 // Allow targets to change the register assignments before rewriting. 1407 addPreRewrite(); 1408 1409 // Finally rewrite virtual registers. 1410 addPass(&VirtRegRewriterID); 1411 1412 // Regalloc scoring for ML-driven eviction - noop except when learning a new 1413 // eviction policy. 1414 addPass(createRegAllocScoringPass()); 1415 return true; 1416 } 1417 1418 /// Return true if the default global register allocator is in use and 1419 /// has not be overriden on the command line with '-regalloc=...' 1420 bool TargetPassConfig::usingDefaultRegAlloc() const { 1421 return RegAlloc.getNumOccurrences() == 0; 1422 } 1423 1424 /// Add the minimum set of target-independent passes that are required for 1425 /// register allocation. No coalescing or scheduling. 1426 void TargetPassConfig::addFastRegAlloc() { 1427 addPass(&PHIEliminationID); 1428 addPass(&TwoAddressInstructionPassID); 1429 1430 addRegAssignAndRewriteFast(); 1431 } 1432 1433 /// Add standard target-independent passes that are tightly coupled with 1434 /// optimized register allocation, including coalescing, machine instruction 1435 /// scheduling, and register allocation itself. 1436 void TargetPassConfig::addOptimizedRegAlloc() { 1437 addPass(&DetectDeadLanesID); 1438 1439 addPass(&ProcessImplicitDefsID); 1440 1441 // LiveVariables currently requires pure SSA form. 1442 // 1443 // FIXME: Once TwoAddressInstruction pass no longer uses kill flags, 1444 // LiveVariables can be removed completely, and LiveIntervals can be directly 1445 // computed. (We still either need to regenerate kill flags after regalloc, or 1446 // preferably fix the scavenger to not depend on them). 1447 // FIXME: UnreachableMachineBlockElim is a dependant pass of LiveVariables. 1448 // When LiveVariables is removed this has to be removed/moved either. 1449 // Explicit addition of UnreachableMachineBlockElim allows stopping before or 1450 // after it with -stop-before/-stop-after. 1451 addPass(&UnreachableMachineBlockElimID); 1452 addPass(&LiveVariablesID); 1453 1454 // Edge splitting is smarter with machine loop info. 1455 addPass(&MachineLoopInfoID); 1456 addPass(&PHIEliminationID); 1457 1458 // Eventually, we want to run LiveIntervals before PHI elimination. 1459 if (EarlyLiveIntervals) 1460 addPass(&LiveIntervalsID); 1461 1462 addPass(&TwoAddressInstructionPassID); 1463 addPass(&RegisterCoalescerID); 1464 1465 // The machine scheduler may accidentally create disconnected components 1466 // when moving subregister definitions around, avoid this by splitting them to 1467 // separate vregs before. Splitting can also improve reg. allocation quality. 1468 addPass(&RenameIndependentSubregsID); 1469 1470 // PreRA instruction scheduling. 1471 addPass(&MachineSchedulerID); 1472 1473 if (addRegAssignAndRewriteOptimized()) { 1474 // Perform stack slot coloring and post-ra machine LICM. 1475 addPass(&StackSlotColoringID); 1476 1477 // Allow targets to expand pseudo instructions depending on the choice of 1478 // registers before MachineCopyPropagation. 1479 addPostRewrite(); 1480 1481 // Copy propagate to forward register uses and try to eliminate COPYs that 1482 // were not coalesced. 1483 addPass(&MachineCopyPropagationID); 1484 1485 // Run post-ra machine LICM to hoist reloads / remats. 1486 // 1487 // FIXME: can this move into MachineLateOptimization? 1488 addPass(&MachineLICMID); 1489 } 1490 } 1491 1492 //===---------------------------------------------------------------------===// 1493 /// Post RegAlloc Pass Configuration 1494 //===---------------------------------------------------------------------===// 1495 1496 /// Add passes that optimize machine instructions after register allocation. 1497 void TargetPassConfig::addMachineLateOptimization() { 1498 // Cleanup of redundant immediate/address loads. 1499 addPass(&MachineLateInstrsCleanupID); 1500 1501 // Branch folding must be run after regalloc and prolog/epilog insertion. 1502 addPass(&BranchFolderPassID); 1503 1504 // Tail duplication. 1505 // Note that duplicating tail just increases code size and degrades 1506 // performance for targets that require Structured Control Flow. 1507 // In addition it can also make CFG irreducible. Thus we disable it. 1508 if (!TM->requiresStructuredCFG()) 1509 addPass(&TailDuplicateID); 1510 1511 // Copy propagation. 1512 addPass(&MachineCopyPropagationID); 1513 } 1514 1515 /// Add standard GC passes. 1516 bool TargetPassConfig::addGCPasses() { 1517 addPass(&GCMachineCodeAnalysisID); 1518 return true; 1519 } 1520 1521 /// Add standard basic block placement passes. 1522 void TargetPassConfig::addBlockPlacement() { 1523 if (EnableFSDiscriminator) { 1524 addPass(createMIRAddFSDiscriminatorsPass( 1525 sampleprof::FSDiscriminatorPass::Pass2)); 1526 const std::string ProfileFile = getFSProfileFile(TM); 1527 if (!ProfileFile.empty() && !DisableLayoutFSProfileLoader) 1528 addPass( 1529 createMIRProfileLoaderPass(ProfileFile, getFSRemappingFile(TM), 1530 sampleprof::FSDiscriminatorPass::Pass2)); 1531 } 1532 if (addPass(&MachineBlockPlacementID)) { 1533 // Run a separate pass to collect block placement statistics. 1534 if (EnableBlockPlacementStats) 1535 addPass(&MachineBlockPlacementStatsID); 1536 } 1537 } 1538 1539 //===---------------------------------------------------------------------===// 1540 /// GlobalISel Configuration 1541 //===---------------------------------------------------------------------===// 1542 bool TargetPassConfig::isGlobalISelAbortEnabled() const { 1543 return TM->Options.GlobalISelAbort == GlobalISelAbortMode::Enable; 1544 } 1545 1546 bool TargetPassConfig::reportDiagnosticWhenGlobalISelFallback() const { 1547 return TM->Options.GlobalISelAbort == GlobalISelAbortMode::DisableWithDiag; 1548 } 1549 1550 bool TargetPassConfig::isGISelCSEEnabled() const { 1551 return true; 1552 } 1553 1554 std::unique_ptr<CSEConfigBase> TargetPassConfig::getCSEConfig() const { 1555 return std::make_unique<CSEConfigBase>(); 1556 } 1557