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