1 //===- opt.cpp - The LLVM Modular Optimizer -------------------------------===// 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 // Optimizations may be specified an arbitrary number of times on the command 10 // line, They are run in the order specified. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "BreakpointPrinter.h" 15 #include "Debugify.h" 16 #include "NewPMDriver.h" 17 #include "PassPrinters.h" 18 #include "llvm/ADT/Triple.h" 19 #include "llvm/Analysis/CallGraph.h" 20 #include "llvm/Analysis/CallGraphSCCPass.h" 21 #include "llvm/Analysis/LoopPass.h" 22 #include "llvm/Analysis/RegionPass.h" 23 #include "llvm/Analysis/TargetLibraryInfo.h" 24 #include "llvm/Analysis/TargetTransformInfo.h" 25 #include "llvm/Bitcode/BitcodeWriterPass.h" 26 #include "llvm/CodeGen/CommandFlags.inc" 27 #include "llvm/CodeGen/TargetPassConfig.h" 28 #include "llvm/Config/llvm-config.h" 29 #include "llvm/IR/DataLayout.h" 30 #include "llvm/IR/DebugInfo.h" 31 #include "llvm/IR/IRPrintingPasses.h" 32 #include "llvm/IR/LLVMContext.h" 33 #include "llvm/IR/LegacyPassManager.h" 34 #include "llvm/IR/LegacyPassNameParser.h" 35 #include "llvm/IR/Module.h" 36 #include "llvm/IR/RemarkStreamer.h" 37 #include "llvm/IR/Verifier.h" 38 #include "llvm/IRReader/IRReader.h" 39 #include "llvm/InitializePasses.h" 40 #include "llvm/LinkAllIR.h" 41 #include "llvm/LinkAllPasses.h" 42 #include "llvm/MC/SubtargetFeature.h" 43 #include "llvm/Support/Debug.h" 44 #include "llvm/Support/FileSystem.h" 45 #include "llvm/Support/Host.h" 46 #include "llvm/Support/InitLLVM.h" 47 #include "llvm/Support/PluginLoader.h" 48 #include "llvm/Support/SourceMgr.h" 49 #include "llvm/Support/SystemUtils.h" 50 #include "llvm/Support/TargetRegistry.h" 51 #include "llvm/Support/TargetSelect.h" 52 #include "llvm/Support/ToolOutputFile.h" 53 #include "llvm/Support/YAMLTraits.h" 54 #include "llvm/Target/TargetMachine.h" 55 #include "llvm/Transforms/Coroutines.h" 56 #include "llvm/Transforms/IPO/AlwaysInliner.h" 57 #include "llvm/Transforms/IPO/PassManagerBuilder.h" 58 #include "llvm/Transforms/Utils/Cloning.h" 59 #include <algorithm> 60 #include <memory> 61 using namespace llvm; 62 using namespace opt_tool; 63 64 // The OptimizationList is automatically populated with registered Passes by the 65 // PassNameParser. 66 // 67 static cl::list<const PassInfo*, bool, PassNameParser> 68 PassList(cl::desc("Optimizations available:")); 69 70 // This flag specifies a textual description of the optimization pass pipeline 71 // to run over the module. This flag switches opt to use the new pass manager 72 // infrastructure, completely disabling all of the flags specific to the old 73 // pass management. 74 static cl::opt<std::string> PassPipeline( 75 "passes", 76 cl::desc("A textual description of the pass pipeline for optimizing"), 77 cl::Hidden); 78 79 // Other command line options... 80 // 81 static cl::opt<std::string> 82 InputFilename(cl::Positional, cl::desc("<input bitcode file>"), 83 cl::init("-"), cl::value_desc("filename")); 84 85 static cl::opt<std::string> 86 OutputFilename("o", cl::desc("Override output filename"), 87 cl::value_desc("filename")); 88 89 static cl::opt<bool> 90 Force("f", cl::desc("Enable binary output on terminals")); 91 92 static cl::opt<bool> 93 PrintEachXForm("p", cl::desc("Print module after each transformation")); 94 95 static cl::opt<bool> 96 NoOutput("disable-output", 97 cl::desc("Do not write result bitcode file"), cl::Hidden); 98 99 static cl::opt<bool> 100 OutputAssembly("S", cl::desc("Write output as LLVM assembly")); 101 102 static cl::opt<bool> 103 OutputThinLTOBC("thinlto-bc", 104 cl::desc("Write output as ThinLTO-ready bitcode")); 105 106 static cl::opt<bool> 107 SplitLTOUnit("thinlto-split-lto-unit", 108 cl::desc("Enable splitting of a ThinLTO LTOUnit")); 109 110 static cl::opt<std::string> ThinLinkBitcodeFile( 111 "thin-link-bitcode-file", cl::value_desc("filename"), 112 cl::desc( 113 "A file in which to write minimized bitcode for the thin link only")); 114 115 static cl::opt<bool> 116 NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden); 117 118 static cl::opt<bool> 119 VerifyEach("verify-each", cl::desc("Verify after each transform")); 120 121 static cl::opt<bool> 122 DisableDITypeMap("disable-debug-info-type-map", 123 cl::desc("Don't use a uniquing type map for debug info")); 124 125 static cl::opt<bool> 126 StripDebug("strip-debug", 127 cl::desc("Strip debugger symbol info from translation unit")); 128 129 static cl::opt<bool> 130 StripNamedMetadata("strip-named-metadata", 131 cl::desc("Strip module-level named metadata")); 132 133 static cl::opt<bool> DisableInline("disable-inlining", 134 cl::desc("Do not run the inliner pass")); 135 136 static cl::opt<bool> 137 DisableOptimizations("disable-opt", 138 cl::desc("Do not run any optimization passes")); 139 140 static cl::opt<bool> 141 StandardLinkOpts("std-link-opts", 142 cl::desc("Include the standard link time optimizations")); 143 144 static cl::opt<bool> 145 OptLevelO0("O0", 146 cl::desc("Optimization level 0. Similar to clang -O0")); 147 148 static cl::opt<bool> 149 OptLevelO1("O1", 150 cl::desc("Optimization level 1. Similar to clang -O1")); 151 152 static cl::opt<bool> 153 OptLevelO2("O2", 154 cl::desc("Optimization level 2. Similar to clang -O2")); 155 156 static cl::opt<bool> 157 OptLevelOs("Os", 158 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os")); 159 160 static cl::opt<bool> 161 OptLevelOz("Oz", 162 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz")); 163 164 static cl::opt<bool> 165 OptLevelO3("O3", 166 cl::desc("Optimization level 3. Similar to clang -O3")); 167 168 static cl::opt<unsigned> 169 CodeGenOptLevel("codegen-opt-level", 170 cl::desc("Override optimization level for codegen hooks")); 171 172 static cl::opt<std::string> 173 TargetTriple("mtriple", cl::desc("Override target triple for module")); 174 175 static cl::opt<bool> 176 DisableLoopUnrolling("disable-loop-unrolling", 177 cl::desc("Disable loop unrolling in all relevant passes"), 178 cl::init(false)); 179 static cl::opt<bool> 180 DisableLoopVectorization("disable-loop-vectorization", 181 cl::desc("Disable the loop vectorization pass"), 182 cl::init(false)); 183 184 static cl::opt<bool> 185 DisableSLPVectorization("disable-slp-vectorization", 186 cl::desc("Disable the slp vectorization pass"), 187 cl::init(false)); 188 189 static cl::opt<bool> EmitSummaryIndex("module-summary", 190 cl::desc("Emit module summary index"), 191 cl::init(false)); 192 193 static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"), 194 cl::init(false)); 195 196 static cl::opt<bool> 197 DisableSimplifyLibCalls("disable-simplify-libcalls", 198 cl::desc("Disable simplify-libcalls")); 199 200 static cl::opt<bool> 201 Quiet("q", cl::desc("Obsolete option"), cl::Hidden); 202 203 static cl::alias 204 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet)); 205 206 static cl::opt<bool> 207 AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization")); 208 209 static cl::opt<bool> EnableDebugify( 210 "enable-debugify", 211 cl::desc( 212 "Start the pipeline with debugify and end it with check-debugify")); 213 214 static cl::opt<bool> DebugifyEach( 215 "debugify-each", 216 cl::desc( 217 "Start each pass with debugify and end it with check-debugify")); 218 219 static cl::opt<std::string> 220 DebugifyExport("debugify-export", 221 cl::desc("Export per-pass debugify statistics to this file"), 222 cl::value_desc("filename"), cl::init("")); 223 224 static cl::opt<bool> 225 PrintBreakpoints("print-breakpoints-for-testing", 226 cl::desc("Print select breakpoints location for testing")); 227 228 static cl::opt<std::string> ClDataLayout("data-layout", 229 cl::desc("data layout string to use"), 230 cl::value_desc("layout-string"), 231 cl::init("")); 232 233 static cl::opt<bool> PreserveBitcodeUseListOrder( 234 "preserve-bc-uselistorder", 235 cl::desc("Preserve use-list order when writing LLVM bitcode."), 236 cl::init(true), cl::Hidden); 237 238 static cl::opt<bool> PreserveAssemblyUseListOrder( 239 "preserve-ll-uselistorder", 240 cl::desc("Preserve use-list order when writing LLVM assembly."), 241 cl::init(false), cl::Hidden); 242 243 static cl::opt<bool> 244 RunTwice("run-twice", 245 cl::desc("Run all passes twice, re-using the same pass manager."), 246 cl::init(false), cl::Hidden); 247 248 static cl::opt<bool> DiscardValueNames( 249 "discard-value-names", 250 cl::desc("Discard names from Value (other than GlobalValue)."), 251 cl::init(false), cl::Hidden); 252 253 static cl::opt<bool> Coroutines( 254 "enable-coroutines", 255 cl::desc("Enable coroutine passes."), 256 cl::init(false), cl::Hidden); 257 258 static cl::opt<bool> PassRemarksWithHotness( 259 "pass-remarks-with-hotness", 260 cl::desc("With PGO, include profile count in optimization remarks"), 261 cl::Hidden); 262 263 static cl::opt<unsigned> PassRemarksHotnessThreshold( 264 "pass-remarks-hotness-threshold", 265 cl::desc("Minimum profile count required for an optimization remark to be output"), 266 cl::Hidden); 267 268 static cl::opt<std::string> 269 RemarksFilename("pass-remarks-output", 270 cl::desc("YAML output filename for pass remarks"), 271 cl::value_desc("filename")); 272 273 static cl::opt<std::string> 274 RemarksPasses("pass-remarks-filter", 275 cl::desc("Only record optimization remarks from passes whose " 276 "names match the given regular expression"), 277 cl::value_desc("regex")); 278 279 cl::opt<PGOKind> 280 PGOKindFlag("pgo-kind", cl::init(NoPGO), cl::Hidden, 281 cl::desc("The kind of profile guided optimization"), 282 cl::values(clEnumValN(NoPGO, "nopgo", "Do not use PGO."), 283 clEnumValN(InstrGen, "pgo-instr-gen-pipeline", 284 "Instrument the IR to generate profile."), 285 clEnumValN(InstrUse, "pgo-instr-use-pipeline", 286 "Use instrumented profile to guide PGO."), 287 clEnumValN(SampleUse, "pgo-sample-use-pipeline", 288 "Use sampled profile to guide PGO."))); 289 cl::opt<std::string> ProfileFile("profile-file", 290 cl::desc("Path to the profile."), cl::Hidden); 291 292 cl::opt<CSPGOKind> CSPGOKindFlag( 293 "cspgo-kind", cl::init(NoCSPGO), cl::Hidden, 294 cl::desc("The kind of context sensitive profile guided optimization"), 295 cl::values( 296 clEnumValN(NoCSPGO, "nocspgo", "Do not use CSPGO."), 297 clEnumValN( 298 CSInstrGen, "cspgo-instr-gen-pipeline", 299 "Instrument (context sensitive) the IR to generate profile."), 300 clEnumValN( 301 CSInstrUse, "cspgo-instr-use-pipeline", 302 "Use instrumented (context sensitive) profile to guide PGO."))); 303 cl::opt<std::string> CSProfileGenFile( 304 "cs-profilegen-file", 305 cl::desc("Path to the instrumented context sensitive profile."), 306 cl::Hidden); 307 308 class OptCustomPassManager : public legacy::PassManager { 309 DebugifyStatsMap DIStatsMap; 310 311 public: 312 using super = legacy::PassManager; 313 314 void add(Pass *P) override { 315 // Wrap each pass with (-check)-debugify passes if requested, making 316 // exceptions for passes which shouldn't see -debugify instrumentation. 317 bool WrapWithDebugify = DebugifyEach && !P->getAsImmutablePass() && 318 !isIRPrintingPass(P) && !isBitcodeWriterPass(P); 319 if (!WrapWithDebugify) { 320 super::add(P); 321 return; 322 } 323 324 // Apply -debugify/-check-debugify before/after each pass and collect 325 // debug info loss statistics. 326 PassKind Kind = P->getPassKind(); 327 StringRef Name = P->getPassName(); 328 329 // TODO: Implement Debugify for BasicBlockPass, LoopPass. 330 switch (Kind) { 331 case PT_Function: 332 super::add(createDebugifyFunctionPass()); 333 super::add(P); 334 super::add(createCheckDebugifyFunctionPass(true, Name, &DIStatsMap)); 335 break; 336 case PT_Module: 337 super::add(createDebugifyModulePass()); 338 super::add(P); 339 super::add(createCheckDebugifyModulePass(true, Name, &DIStatsMap)); 340 break; 341 default: 342 super::add(P); 343 break; 344 } 345 } 346 347 const DebugifyStatsMap &getDebugifyStatsMap() const { return DIStatsMap; } 348 }; 349 350 static inline void addPass(legacy::PassManagerBase &PM, Pass *P) { 351 // Add the pass to the pass manager... 352 PM.add(P); 353 354 // If we are verifying all of the intermediate steps, add the verifier... 355 if (VerifyEach) 356 PM.add(createVerifierPass()); 357 } 358 359 /// This routine adds optimization passes based on selected optimization level, 360 /// OptLevel. 361 /// 362 /// OptLevel - Optimization Level 363 static void AddOptimizationPasses(legacy::PassManagerBase &MPM, 364 legacy::FunctionPassManager &FPM, 365 TargetMachine *TM, unsigned OptLevel, 366 unsigned SizeLevel) { 367 if (!NoVerify || VerifyEach) 368 FPM.add(createVerifierPass()); // Verify that input is correct 369 370 PassManagerBuilder Builder; 371 Builder.OptLevel = OptLevel; 372 Builder.SizeLevel = SizeLevel; 373 374 if (DisableInline) { 375 // No inlining pass 376 } else if (OptLevel > 1) { 377 Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false); 378 } else { 379 Builder.Inliner = createAlwaysInlinerLegacyPass(); 380 } 381 Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ? 382 DisableLoopUnrolling : OptLevel == 0; 383 384 // This is final, unless there is a #pragma vectorize enable 385 if (DisableLoopVectorization) 386 Builder.LoopVectorize = false; 387 // If option wasn't forced via cmd line (-vectorize-loops, -loop-vectorize) 388 else if (!Builder.LoopVectorize) 389 Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2; 390 391 // When #pragma vectorize is on for SLP, do the same as above 392 Builder.SLPVectorize = 393 DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2; 394 395 if (TM) 396 TM->adjustPassManager(Builder); 397 398 if (Coroutines) 399 addCoroutinePassesToExtensionPoints(Builder); 400 401 switch (PGOKindFlag) { 402 case InstrGen: 403 Builder.EnablePGOInstrGen = true; 404 Builder.PGOInstrGen = ProfileFile; 405 break; 406 case InstrUse: 407 Builder.PGOInstrUse = ProfileFile; 408 break; 409 case SampleUse: 410 Builder.PGOSampleUse = ProfileFile; 411 break; 412 default: 413 break; 414 } 415 416 switch (CSPGOKindFlag) { 417 case CSInstrGen: 418 Builder.EnablePGOCSInstrGen = true; 419 break; 420 case CSInstrUse: 421 Builder.EnablePGOCSInstrUse = true; 422 break; 423 default: 424 break; 425 } 426 427 Builder.populateFunctionPassManager(FPM); 428 Builder.populateModulePassManager(MPM); 429 } 430 431 static void AddStandardLinkPasses(legacy::PassManagerBase &PM) { 432 PassManagerBuilder Builder; 433 Builder.VerifyInput = true; 434 if (DisableOptimizations) 435 Builder.OptLevel = 0; 436 437 if (!DisableInline) 438 Builder.Inliner = createFunctionInliningPass(); 439 Builder.populateLTOPassManager(PM); 440 } 441 442 //===----------------------------------------------------------------------===// 443 // CodeGen-related helper functions. 444 // 445 446 static CodeGenOpt::Level GetCodeGenOptLevel() { 447 if (CodeGenOptLevel.getNumOccurrences()) 448 return static_cast<CodeGenOpt::Level>(unsigned(CodeGenOptLevel)); 449 if (OptLevelO1) 450 return CodeGenOpt::Less; 451 if (OptLevelO2) 452 return CodeGenOpt::Default; 453 if (OptLevelO3) 454 return CodeGenOpt::Aggressive; 455 return CodeGenOpt::None; 456 } 457 458 // Returns the TargetMachine instance or zero if no triple is provided. 459 static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr, 460 StringRef FeaturesStr, 461 const TargetOptions &Options) { 462 std::string Error; 463 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple, 464 Error); 465 // Some modules don't specify a triple, and this is okay. 466 if (!TheTarget) { 467 return nullptr; 468 } 469 470 return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr, 471 FeaturesStr, Options, getRelocModel(), 472 getCodeModel(), GetCodeGenOptLevel()); 473 } 474 475 #ifdef LINK_POLLY_INTO_TOOLS 476 namespace polly { 477 void initializePollyPasses(llvm::PassRegistry &Registry); 478 } 479 #endif 480 481 //===----------------------------------------------------------------------===// 482 // main for opt 483 // 484 int main(int argc, char **argv) { 485 InitLLVM X(argc, argv); 486 487 // Enable debug stream buffering. 488 EnableDebugBuffering = true; 489 490 LLVMContext Context; 491 492 InitializeAllTargets(); 493 InitializeAllTargetMCs(); 494 InitializeAllAsmPrinters(); 495 InitializeAllAsmParsers(); 496 497 // Initialize passes 498 PassRegistry &Registry = *PassRegistry::getPassRegistry(); 499 initializeCore(Registry); 500 initializeCoroutines(Registry); 501 initializeScalarOpts(Registry); 502 initializeObjCARCOpts(Registry); 503 initializeVectorization(Registry); 504 initializeIPO(Registry); 505 initializeAnalysis(Registry); 506 initializeTransformUtils(Registry); 507 initializeInstCombine(Registry); 508 initializeAggressiveInstCombine(Registry); 509 initializeInstrumentation(Registry); 510 initializeTarget(Registry); 511 // For codegen passes, only passes that do IR to IR transformation are 512 // supported. 513 initializeExpandMemCmpPassPass(Registry); 514 initializeScalarizeMaskedMemIntrinPass(Registry); 515 initializeCodeGenPreparePass(Registry); 516 initializeAtomicExpandPass(Registry); 517 initializeRewriteSymbolsLegacyPassPass(Registry); 518 initializeWinEHPreparePass(Registry); 519 initializeDwarfEHPreparePass(Registry); 520 initializeSafeStackLegacyPassPass(Registry); 521 initializeSjLjEHPreparePass(Registry); 522 initializePreISelIntrinsicLoweringLegacyPassPass(Registry); 523 initializeGlobalMergePass(Registry); 524 initializeIndirectBrExpandPassPass(Registry); 525 initializeInterleavedLoadCombinePass(Registry); 526 initializeInterleavedAccessPass(Registry); 527 initializeEntryExitInstrumenterPass(Registry); 528 initializePostInlineEntryExitInstrumenterPass(Registry); 529 initializeUnreachableBlockElimLegacyPassPass(Registry); 530 initializeExpandReductionsPass(Registry); 531 initializeWasmEHPreparePass(Registry); 532 initializeWriteBitcodePassPass(Registry); 533 534 #ifdef LINK_POLLY_INTO_TOOLS 535 polly::initializePollyPasses(Registry); 536 #endif 537 538 cl::ParseCommandLineOptions(argc, argv, 539 "llvm .bc -> .bc modular optimizer and analysis printer\n"); 540 541 if (AnalyzeOnly && NoOutput) { 542 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n"; 543 return 1; 544 } 545 546 SMDiagnostic Err; 547 548 Context.setDiscardValueNames(DiscardValueNames); 549 if (!DisableDITypeMap) 550 Context.enableDebugTypeODRUniquing(); 551 552 if (PassRemarksWithHotness) 553 Context.setDiagnosticsHotnessRequested(true); 554 555 if (PassRemarksHotnessThreshold) 556 Context.setDiagnosticsHotnessThreshold(PassRemarksHotnessThreshold); 557 558 std::unique_ptr<ToolOutputFile> OptRemarkFile; 559 if (RemarksFilename != "") { 560 std::error_code EC; 561 OptRemarkFile = 562 llvm::make_unique<ToolOutputFile>(RemarksFilename, EC, sys::fs::F_None); 563 if (EC) { 564 errs() << EC.message() << '\n'; 565 return 1; 566 } 567 Context.setRemarkStreamer(llvm::make_unique<RemarkStreamer>( 568 RemarksFilename, OptRemarkFile->os())); 569 570 if (!RemarksPasses.empty()) 571 if (Error E = Context.getRemarkStreamer()->setFilter(RemarksPasses)) { 572 errs() << E << '\n'; 573 return 1; 574 } 575 } 576 577 // Load the input module... 578 std::unique_ptr<Module> M = 579 parseIRFile(InputFilename, Err, Context, !NoVerify, ClDataLayout); 580 581 if (!M) { 582 Err.print(argv[0], errs()); 583 return 1; 584 } 585 586 // Strip debug info before running the verifier. 587 if (StripDebug) 588 StripDebugInfo(*M); 589 590 // Erase module-level named metadata, if requested. 591 if (StripNamedMetadata) { 592 while (!M->named_metadata_empty()) { 593 NamedMDNode *NMD = &*M->named_metadata_begin(); 594 M->eraseNamedMetadata(NMD); 595 } 596 } 597 598 // If we are supposed to override the target triple or data layout, do so now. 599 if (!TargetTriple.empty()) 600 M->setTargetTriple(Triple::normalize(TargetTriple)); 601 602 // Immediately run the verifier to catch any problems before starting up the 603 // pass pipelines. Otherwise we can crash on broken code during 604 // doInitialization(). 605 if (!NoVerify && verifyModule(*M, &errs())) { 606 errs() << argv[0] << ": " << InputFilename 607 << ": error: input module is broken!\n"; 608 return 1; 609 } 610 611 // Figure out what stream we are supposed to write to... 612 std::unique_ptr<ToolOutputFile> Out; 613 std::unique_ptr<ToolOutputFile> ThinLinkOut; 614 if (NoOutput) { 615 if (!OutputFilename.empty()) 616 errs() << "WARNING: The -o (output filename) option is ignored when\n" 617 "the --disable-output option is used.\n"; 618 } else { 619 // Default to standard output. 620 if (OutputFilename.empty()) 621 OutputFilename = "-"; 622 623 std::error_code EC; 624 Out.reset(new ToolOutputFile(OutputFilename, EC, sys::fs::F_None)); 625 if (EC) { 626 errs() << EC.message() << '\n'; 627 return 1; 628 } 629 630 if (!ThinLinkBitcodeFile.empty()) { 631 ThinLinkOut.reset( 632 new ToolOutputFile(ThinLinkBitcodeFile, EC, sys::fs::F_None)); 633 if (EC) { 634 errs() << EC.message() << '\n'; 635 return 1; 636 } 637 } 638 } 639 640 Triple ModuleTriple(M->getTargetTriple()); 641 std::string CPUStr, FeaturesStr; 642 TargetMachine *Machine = nullptr; 643 const TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); 644 645 if (ModuleTriple.getArch()) { 646 CPUStr = getCPUStr(); 647 FeaturesStr = getFeaturesStr(); 648 Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options); 649 } else if (ModuleTriple.getArchName() != "unknown" && 650 ModuleTriple.getArchName() != "") { 651 errs() << argv[0] << ": unrecognized architecture '" 652 << ModuleTriple.getArchName() << "' provided.\n"; 653 return 1; 654 } 655 656 std::unique_ptr<TargetMachine> TM(Machine); 657 658 // Override function attributes based on CPUStr, FeaturesStr, and command line 659 // flags. 660 setFunctionAttributes(CPUStr, FeaturesStr, *M); 661 662 // If the output is set to be emitted to standard out, and standard out is a 663 // console, print out a warning message and refuse to do it. We don't 664 // impress anyone by spewing tons of binary goo to a terminal. 665 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly) 666 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet)) 667 NoOutput = true; 668 669 if (OutputThinLTOBC) 670 M->addModuleFlag(Module::Error, "EnableSplitLTOUnit", SplitLTOUnit); 671 672 if (PassPipeline.getNumOccurrences() > 0) { 673 OutputKind OK = OK_NoOutput; 674 if (!NoOutput) 675 OK = OutputAssembly 676 ? OK_OutputAssembly 677 : (OutputThinLTOBC ? OK_OutputThinLTOBitcode : OK_OutputBitcode); 678 679 VerifierKind VK = VK_VerifyInAndOut; 680 if (NoVerify) 681 VK = VK_NoVerifier; 682 else if (VerifyEach) 683 VK = VK_VerifyEachPass; 684 685 // The user has asked to use the new pass manager and provided a pipeline 686 // string. Hand off the rest of the functionality to the new code for that 687 // layer. 688 return runPassPipeline(argv[0], *M, TM.get(), Out.get(), ThinLinkOut.get(), 689 OptRemarkFile.get(), PassPipeline, OK, VK, 690 PreserveAssemblyUseListOrder, 691 PreserveBitcodeUseListOrder, EmitSummaryIndex, 692 EmitModuleHash, EnableDebugify) 693 ? 0 694 : 1; 695 } 696 697 // Create a PassManager to hold and optimize the collection of passes we are 698 // about to build. 699 OptCustomPassManager Passes; 700 bool AddOneTimeDebugifyPasses = EnableDebugify && !DebugifyEach; 701 702 // Add an appropriate TargetLibraryInfo pass for the module's triple. 703 TargetLibraryInfoImpl TLII(ModuleTriple); 704 705 // The -disable-simplify-libcalls flag actually disables all builtin optzns. 706 if (DisableSimplifyLibCalls) 707 TLII.disableAllFunctions(); 708 Passes.add(new TargetLibraryInfoWrapperPass(TLII)); 709 710 // Add internal analysis passes from the target machine. 711 Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis() 712 : TargetIRAnalysis())); 713 714 if (AddOneTimeDebugifyPasses) 715 Passes.add(createDebugifyModulePass()); 716 717 std::unique_ptr<legacy::FunctionPassManager> FPasses; 718 if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || 719 OptLevelO3) { 720 FPasses.reset(new legacy::FunctionPassManager(M.get())); 721 FPasses->add(createTargetTransformInfoWrapperPass( 722 TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis())); 723 } 724 725 if (PrintBreakpoints) { 726 // Default to standard output. 727 if (!Out) { 728 if (OutputFilename.empty()) 729 OutputFilename = "-"; 730 731 std::error_code EC; 732 Out = llvm::make_unique<ToolOutputFile>(OutputFilename, EC, 733 sys::fs::F_None); 734 if (EC) { 735 errs() << EC.message() << '\n'; 736 return 1; 737 } 738 } 739 Passes.add(createBreakpointPrinter(Out->os())); 740 NoOutput = true; 741 } 742 743 if (TM) { 744 // FIXME: We should dyn_cast this when supported. 745 auto <M = static_cast<LLVMTargetMachine &>(*TM); 746 Pass *TPC = LTM.createPassConfig(Passes); 747 Passes.add(TPC); 748 } 749 750 // Create a new optimization pass for each one specified on the command line 751 for (unsigned i = 0; i < PassList.size(); ++i) { 752 if (StandardLinkOpts && 753 StandardLinkOpts.getPosition() < PassList.getPosition(i)) { 754 AddStandardLinkPasses(Passes); 755 StandardLinkOpts = false; 756 } 757 758 if (OptLevelO0 && OptLevelO0.getPosition() < PassList.getPosition(i)) { 759 AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0); 760 OptLevelO0 = false; 761 } 762 763 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) { 764 AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0); 765 OptLevelO1 = false; 766 } 767 768 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) { 769 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0); 770 OptLevelO2 = false; 771 } 772 773 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) { 774 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1); 775 OptLevelOs = false; 776 } 777 778 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) { 779 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2); 780 OptLevelOz = false; 781 } 782 783 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) { 784 AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0); 785 OptLevelO3 = false; 786 } 787 788 const PassInfo *PassInf = PassList[i]; 789 Pass *P = nullptr; 790 if (PassInf->getNormalCtor()) 791 P = PassInf->getNormalCtor()(); 792 else 793 errs() << argv[0] << ": cannot create pass: " 794 << PassInf->getPassName() << "\n"; 795 if (P) { 796 PassKind Kind = P->getPassKind(); 797 addPass(Passes, P); 798 799 if (AnalyzeOnly) { 800 switch (Kind) { 801 case PT_BasicBlock: 802 Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet)); 803 break; 804 case PT_Region: 805 Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet)); 806 break; 807 case PT_Loop: 808 Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet)); 809 break; 810 case PT_Function: 811 Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet)); 812 break; 813 case PT_CallGraphSCC: 814 Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet)); 815 break; 816 default: 817 Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet)); 818 break; 819 } 820 } 821 } 822 823 if (PrintEachXForm) 824 Passes.add( 825 createPrintModulePass(errs(), "", PreserveAssemblyUseListOrder)); 826 } 827 828 if (StandardLinkOpts) { 829 AddStandardLinkPasses(Passes); 830 StandardLinkOpts = false; 831 } 832 833 if (OptLevelO0) 834 AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0); 835 836 if (OptLevelO1) 837 AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0); 838 839 if (OptLevelO2) 840 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0); 841 842 if (OptLevelOs) 843 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1); 844 845 if (OptLevelOz) 846 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2); 847 848 if (OptLevelO3) 849 AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0); 850 851 if (FPasses) { 852 FPasses->doInitialization(); 853 for (Function &F : *M) 854 FPasses->run(F); 855 FPasses->doFinalization(); 856 } 857 858 // Check that the module is well formed on completion of optimization 859 if (!NoVerify && !VerifyEach) 860 Passes.add(createVerifierPass()); 861 862 if (AddOneTimeDebugifyPasses) 863 Passes.add(createCheckDebugifyModulePass(false)); 864 865 // In run twice mode, we want to make sure the output is bit-by-bit 866 // equivalent if we run the pass manager again, so setup two buffers and 867 // a stream to write to them. Note that llc does something similar and it 868 // may be worth to abstract this out in the future. 869 SmallVector<char, 0> Buffer; 870 SmallVector<char, 0> FirstRunBuffer; 871 std::unique_ptr<raw_svector_ostream> BOS; 872 raw_ostream *OS = nullptr; 873 874 // Write bitcode or assembly to the output as the last step... 875 if (!NoOutput && !AnalyzeOnly) { 876 assert(Out); 877 OS = &Out->os(); 878 if (RunTwice) { 879 BOS = make_unique<raw_svector_ostream>(Buffer); 880 OS = BOS.get(); 881 } 882 if (OutputAssembly) { 883 if (EmitSummaryIndex) 884 report_fatal_error("Text output is incompatible with -module-summary"); 885 if (EmitModuleHash) 886 report_fatal_error("Text output is incompatible with -module-hash"); 887 Passes.add(createPrintModulePass(*OS, "", PreserveAssemblyUseListOrder)); 888 } else if (OutputThinLTOBC) 889 Passes.add(createWriteThinLTOBitcodePass( 890 *OS, ThinLinkOut ? &ThinLinkOut->os() : nullptr)); 891 else 892 Passes.add(createBitcodeWriterPass(*OS, PreserveBitcodeUseListOrder, 893 EmitSummaryIndex, EmitModuleHash)); 894 } 895 896 // Before executing passes, print the final values of the LLVM options. 897 cl::PrintOptionValues(); 898 899 if (!RunTwice) { 900 // Now that we have all of the passes ready, run them. 901 Passes.run(*M); 902 } else { 903 // If requested, run all passes twice with the same pass manager to catch 904 // bugs caused by persistent state in the passes. 905 std::unique_ptr<Module> M2(CloneModule(*M)); 906 // Run all passes on the original module first, so the second run processes 907 // the clone to catch CloneModule bugs. 908 Passes.run(*M); 909 FirstRunBuffer = Buffer; 910 Buffer.clear(); 911 912 Passes.run(*M2); 913 914 // Compare the two outputs and make sure they're the same 915 assert(Out); 916 if (Buffer.size() != FirstRunBuffer.size() || 917 (memcmp(Buffer.data(), FirstRunBuffer.data(), Buffer.size()) != 0)) { 918 errs() 919 << "Running the pass manager twice changed the output.\n" 920 "Writing the result of the second run to the specified output.\n" 921 "To generate the one-run comparison binary, just run without\n" 922 "the compile-twice option\n"; 923 Out->os() << BOS->str(); 924 Out->keep(); 925 if (OptRemarkFile) 926 OptRemarkFile->keep(); 927 return 1; 928 } 929 Out->os() << BOS->str(); 930 } 931 932 if (DebugifyEach && !DebugifyExport.empty()) 933 exportDebugifyStats(DebugifyExport, Passes.getDebugifyStatsMap()); 934 935 // Declare success. 936 if (!NoOutput || PrintBreakpoints) 937 Out->keep(); 938 939 if (OptRemarkFile) 940 OptRemarkFile->keep(); 941 942 if (ThinLinkOut) 943 ThinLinkOut->keep(); 944 945 return 0; 946 } 947