1 //===- opt.cpp - The LLVM Modular Optimizer -------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Optimizations may be specified an arbitrary number of times on the command 11 // line, They are run in the order specified. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "BreakpointPrinter.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.h" 27 #include "llvm/IR/DataLayout.h" 28 #include "llvm/IR/DebugInfo.h" 29 #include "llvm/IR/IRPrintingPasses.h" 30 #include "llvm/IR/LLVMContext.h" 31 #include "llvm/IR/LegacyPassManager.h" 32 #include "llvm/IR/LegacyPassNameParser.h" 33 #include "llvm/IR/Module.h" 34 #include "llvm/IR/Verifier.h" 35 #include "llvm/IRReader/IRReader.h" 36 #include "llvm/InitializePasses.h" 37 #include "llvm/LinkAllIR.h" 38 #include "llvm/LinkAllPasses.h" 39 #include "llvm/MC/SubtargetFeature.h" 40 #include "llvm/Support/Debug.h" 41 #include "llvm/Support/FileSystem.h" 42 #include "llvm/Support/Host.h" 43 #include "llvm/Support/ManagedStatic.h" 44 #include "llvm/Support/PluginLoader.h" 45 #include "llvm/Support/PrettyStackTrace.h" 46 #include "llvm/Support/Signals.h" 47 #include "llvm/Support/SourceMgr.h" 48 #include "llvm/Support/SystemUtils.h" 49 #include "llvm/Support/TargetRegistry.h" 50 #include "llvm/Support/TargetSelect.h" 51 #include "llvm/Support/ToolOutputFile.h" 52 #include "llvm/Target/TargetMachine.h" 53 #include "llvm/Transforms/IPO/PassManagerBuilder.h" 54 #include "llvm/Transforms/Utils/Cloning.h" 55 #include <algorithm> 56 #include <memory> 57 using namespace llvm; 58 using namespace opt_tool; 59 60 // The OptimizationList is automatically populated with registered Passes by the 61 // PassNameParser. 62 // 63 static cl::list<const PassInfo*, bool, PassNameParser> 64 PassList(cl::desc("Optimizations available:")); 65 66 // This flag specifies a textual description of the optimization pass pipeline 67 // to run over the module. This flag switches opt to use the new pass manager 68 // infrastructure, completely disabling all of the flags specific to the old 69 // pass management. 70 static cl::opt<std::string> PassPipeline( 71 "passes", 72 cl::desc("A textual description of the pass pipeline for optimizing"), 73 cl::Hidden); 74 75 // Other command line options... 76 // 77 static cl::opt<std::string> 78 InputFilename(cl::Positional, cl::desc("<input bitcode file>"), 79 cl::init("-"), cl::value_desc("filename")); 80 81 static cl::opt<std::string> 82 OutputFilename("o", cl::desc("Override output filename"), 83 cl::value_desc("filename")); 84 85 static cl::opt<bool> 86 Force("f", cl::desc("Enable binary output on terminals")); 87 88 static cl::opt<bool> 89 PrintEachXForm("p", cl::desc("Print module after each transformation")); 90 91 static cl::opt<bool> 92 NoOutput("disable-output", 93 cl::desc("Do not write result bitcode file"), cl::Hidden); 94 95 static cl::opt<bool> 96 OutputAssembly("S", cl::desc("Write output as LLVM assembly")); 97 98 static cl::opt<bool> 99 NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden); 100 101 static cl::opt<bool> 102 VerifyEach("verify-each", cl::desc("Verify after each transform")); 103 104 static cl::opt<bool> 105 StripDebug("strip-debug", 106 cl::desc("Strip debugger symbol info from translation unit")); 107 108 static cl::opt<bool> 109 DisableInline("disable-inlining", cl::desc("Do not run the inliner pass")); 110 111 static cl::opt<bool> 112 DisableOptimizations("disable-opt", 113 cl::desc("Do not run any optimization passes")); 114 115 static cl::opt<bool> 116 StandardLinkOpts("std-link-opts", 117 cl::desc("Include the standard link time optimizations")); 118 119 static cl::opt<bool> 120 OptLevelO1("O1", 121 cl::desc("Optimization level 1. Similar to clang -O1")); 122 123 static cl::opt<bool> 124 OptLevelO2("O2", 125 cl::desc("Optimization level 2. Similar to clang -O2")); 126 127 static cl::opt<bool> 128 OptLevelOs("Os", 129 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os")); 130 131 static cl::opt<bool> 132 OptLevelOz("Oz", 133 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz")); 134 135 static cl::opt<bool> 136 OptLevelO3("O3", 137 cl::desc("Optimization level 3. Similar to clang -O3")); 138 139 static cl::opt<std::string> 140 TargetTriple("mtriple", cl::desc("Override target triple for module")); 141 142 static cl::opt<bool> 143 UnitAtATime("funit-at-a-time", 144 cl::desc("Enable IPO. This corresponds to gcc's -funit-at-a-time"), 145 cl::init(true)); 146 147 static cl::opt<bool> 148 DisableLoopUnrolling("disable-loop-unrolling", 149 cl::desc("Disable loop unrolling in all relevant passes"), 150 cl::init(false)); 151 static cl::opt<bool> 152 DisableLoopVectorization("disable-loop-vectorization", 153 cl::desc("Disable the loop vectorization pass"), 154 cl::init(false)); 155 156 static cl::opt<bool> 157 DisableSLPVectorization("disable-slp-vectorization", 158 cl::desc("Disable the slp vectorization pass"), 159 cl::init(false)); 160 161 static cl::opt<bool> EmitSummaryIndex("module-summary", 162 cl::desc("Emit module summary index"), 163 cl::init(false)); 164 165 static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"), 166 cl::init(false)); 167 168 static cl::opt<bool> 169 DisableSimplifyLibCalls("disable-simplify-libcalls", 170 cl::desc("Disable simplify-libcalls")); 171 172 static cl::opt<bool> 173 Quiet("q", cl::desc("Obsolete option"), cl::Hidden); 174 175 static cl::alias 176 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet)); 177 178 static cl::opt<bool> 179 AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization")); 180 181 static cl::opt<bool> 182 PrintBreakpoints("print-breakpoints-for-testing", 183 cl::desc("Print select breakpoints location for testing")); 184 185 static cl::opt<std::string> 186 DefaultDataLayout("default-data-layout", 187 cl::desc("data layout string to use if not specified by module"), 188 cl::value_desc("layout-string"), cl::init("")); 189 190 static cl::opt<bool> PreserveBitcodeUseListOrder( 191 "preserve-bc-uselistorder", 192 cl::desc("Preserve use-list order when writing LLVM bitcode."), 193 cl::init(true), cl::Hidden); 194 195 static cl::opt<bool> PreserveAssemblyUseListOrder( 196 "preserve-ll-uselistorder", 197 cl::desc("Preserve use-list order when writing LLVM assembly."), 198 cl::init(false), cl::Hidden); 199 200 static cl::opt<bool> 201 RunTwice("run-twice", 202 cl::desc("Run all passes twice, re-using the same pass manager."), 203 cl::init(false), cl::Hidden); 204 205 static cl::opt<bool> DiscardValueNames( 206 "discard-value-names", 207 cl::desc("Discard names from Value (other than GlobalValue)."), 208 cl::init(false), cl::Hidden); 209 210 static inline void addPass(legacy::PassManagerBase &PM, Pass *P) { 211 // Add the pass to the pass manager... 212 PM.add(P); 213 214 // If we are verifying all of the intermediate steps, add the verifier... 215 if (VerifyEach) 216 PM.add(createVerifierPass()); 217 } 218 219 /// This routine adds optimization passes based on selected optimization level, 220 /// OptLevel. 221 /// 222 /// OptLevel - Optimization Level 223 static void AddOptimizationPasses(legacy::PassManagerBase &MPM, 224 legacy::FunctionPassManager &FPM, 225 unsigned OptLevel, unsigned SizeLevel) { 226 if (!NoVerify || VerifyEach) 227 FPM.add(createVerifierPass()); // Verify that input is correct 228 229 PassManagerBuilder Builder; 230 Builder.OptLevel = OptLevel; 231 Builder.SizeLevel = SizeLevel; 232 233 if (DisableInline) { 234 // No inlining pass 235 } else if (OptLevel > 1) { 236 Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel); 237 } else { 238 Builder.Inliner = createAlwaysInlinerPass(); 239 } 240 Builder.DisableUnitAtATime = !UnitAtATime; 241 Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ? 242 DisableLoopUnrolling : OptLevel == 0; 243 244 // This is final, unless there is a #pragma vectorize enable 245 if (DisableLoopVectorization) 246 Builder.LoopVectorize = false; 247 // If option wasn't forced via cmd line (-vectorize-loops, -loop-vectorize) 248 else if (!Builder.LoopVectorize) 249 Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2; 250 251 // When #pragma vectorize is on for SLP, do the same as above 252 Builder.SLPVectorize = 253 DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2; 254 255 Builder.populateFunctionPassManager(FPM); 256 Builder.populateModulePassManager(MPM); 257 } 258 259 static void AddStandardLinkPasses(legacy::PassManagerBase &PM) { 260 PassManagerBuilder Builder; 261 Builder.VerifyInput = true; 262 if (DisableOptimizations) 263 Builder.OptLevel = 0; 264 265 if (!DisableInline) 266 Builder.Inliner = createFunctionInliningPass(); 267 Builder.populateLTOPassManager(PM); 268 } 269 270 //===----------------------------------------------------------------------===// 271 // CodeGen-related helper functions. 272 // 273 274 static CodeGenOpt::Level GetCodeGenOptLevel() { 275 if (OptLevelO1) 276 return CodeGenOpt::Less; 277 if (OptLevelO2) 278 return CodeGenOpt::Default; 279 if (OptLevelO3) 280 return CodeGenOpt::Aggressive; 281 return CodeGenOpt::None; 282 } 283 284 // Returns the TargetMachine instance or zero if no triple is provided. 285 static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr, 286 StringRef FeaturesStr, 287 const TargetOptions &Options) { 288 std::string Error; 289 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple, 290 Error); 291 // Some modules don't specify a triple, and this is okay. 292 if (!TheTarget) { 293 return nullptr; 294 } 295 296 return TheTarget->createTargetMachine(TheTriple.getTriple(), 297 CPUStr, FeaturesStr, Options, 298 RelocModel, CMModel, 299 GetCodeGenOptLevel()); 300 } 301 302 #ifdef LINK_POLLY_INTO_TOOLS 303 namespace polly { 304 void initializePollyPasses(llvm::PassRegistry &Registry); 305 } 306 #endif 307 308 //===----------------------------------------------------------------------===// 309 // main for opt 310 // 311 int main(int argc, char **argv) { 312 sys::PrintStackTraceOnErrorSignal(); 313 llvm::PrettyStackTraceProgram X(argc, argv); 314 315 // Enable debug stream buffering. 316 EnableDebugBuffering = true; 317 318 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 319 LLVMContext Context; 320 321 InitializeAllTargets(); 322 InitializeAllTargetMCs(); 323 InitializeAllAsmPrinters(); 324 325 // Initialize passes 326 PassRegistry &Registry = *PassRegistry::getPassRegistry(); 327 initializeCore(Registry); 328 initializeScalarOpts(Registry); 329 initializeObjCARCOpts(Registry); 330 initializeVectorization(Registry); 331 initializeIPO(Registry); 332 initializeAnalysis(Registry); 333 initializeTransformUtils(Registry); 334 initializeInstCombine(Registry); 335 initializeInstrumentation(Registry); 336 initializeTarget(Registry); 337 // For codegen passes, only passes that do IR to IR transformation are 338 // supported. 339 initializeCodeGenPreparePass(Registry); 340 initializeAtomicExpandPass(Registry); 341 initializeRewriteSymbolsPass(Registry); 342 initializeWinEHPreparePass(Registry); 343 initializeDwarfEHPreparePass(Registry); 344 initializeSafeStackPass(Registry); 345 initializeSjLjEHPreparePass(Registry); 346 347 #ifdef LINK_POLLY_INTO_TOOLS 348 polly::initializePollyPasses(Registry); 349 #endif 350 351 cl::ParseCommandLineOptions(argc, argv, 352 "llvm .bc -> .bc modular optimizer and analysis printer\n"); 353 354 if (AnalyzeOnly && NoOutput) { 355 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n"; 356 return 1; 357 } 358 359 SMDiagnostic Err; 360 361 Context.setDiscardValueNames(DiscardValueNames); 362 363 // Load the input module... 364 std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context); 365 366 if (!M) { 367 Err.print(argv[0], errs()); 368 return 1; 369 } 370 371 // Strip debug info before running the verifier. 372 if (StripDebug) 373 StripDebugInfo(*M); 374 375 // Immediately run the verifier to catch any problems before starting up the 376 // pass pipelines. Otherwise we can crash on broken code during 377 // doInitialization(). 378 if (!NoVerify && verifyModule(*M, &errs())) { 379 errs() << argv[0] << ": " << InputFilename 380 << ": error: input module is broken!\n"; 381 return 1; 382 } 383 384 // If we are supposed to override the target triple, do so now. 385 if (!TargetTriple.empty()) 386 M->setTargetTriple(Triple::normalize(TargetTriple)); 387 388 // Figure out what stream we are supposed to write to... 389 std::unique_ptr<tool_output_file> Out; 390 if (NoOutput) { 391 if (!OutputFilename.empty()) 392 errs() << "WARNING: The -o (output filename) option is ignored when\n" 393 "the --disable-output option is used.\n"; 394 } else { 395 // Default to standard output. 396 if (OutputFilename.empty()) 397 OutputFilename = "-"; 398 399 std::error_code EC; 400 Out.reset(new tool_output_file(OutputFilename, EC, sys::fs::F_None)); 401 if (EC) { 402 errs() << EC.message() << '\n'; 403 return 1; 404 } 405 } 406 407 Triple ModuleTriple(M->getTargetTriple()); 408 std::string CPUStr, FeaturesStr; 409 TargetMachine *Machine = nullptr; 410 const TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); 411 412 if (ModuleTriple.getArch()) { 413 CPUStr = getCPUStr(); 414 FeaturesStr = getFeaturesStr(); 415 Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options); 416 } 417 418 std::unique_ptr<TargetMachine> TM(Machine); 419 420 // Override function attributes based on CPUStr, FeaturesStr, and command line 421 // flags. 422 setFunctionAttributes(CPUStr, FeaturesStr, *M); 423 424 // If the output is set to be emitted to standard out, and standard out is a 425 // console, print out a warning message and refuse to do it. We don't 426 // impress anyone by spewing tons of binary goo to a terminal. 427 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly) 428 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet)) 429 NoOutput = true; 430 431 if (PassPipeline.getNumOccurrences() > 0) { 432 OutputKind OK = OK_NoOutput; 433 if (!NoOutput) 434 OK = OutputAssembly ? OK_OutputAssembly : OK_OutputBitcode; 435 436 VerifierKind VK = VK_VerifyInAndOut; 437 if (NoVerify) 438 VK = VK_NoVerifier; 439 else if (VerifyEach) 440 VK = VK_VerifyEachPass; 441 442 // The user has asked to use the new pass manager and provided a pipeline 443 // string. Hand off the rest of the functionality to the new code for that 444 // layer. 445 return runPassPipeline(argv[0], Context, *M, TM.get(), Out.get(), 446 PassPipeline, OK, VK, PreserveAssemblyUseListOrder, 447 PreserveBitcodeUseListOrder) 448 ? 0 449 : 1; 450 } 451 452 // Create a PassManager to hold and optimize the collection of passes we are 453 // about to build. 454 // 455 legacy::PassManager Passes; 456 457 // Add an appropriate TargetLibraryInfo pass for the module's triple. 458 TargetLibraryInfoImpl TLII(ModuleTriple); 459 460 // The -disable-simplify-libcalls flag actually disables all builtin optzns. 461 if (DisableSimplifyLibCalls) 462 TLII.disableAllFunctions(); 463 Passes.add(new TargetLibraryInfoWrapperPass(TLII)); 464 465 // Add an appropriate DataLayout instance for this module. 466 const DataLayout &DL = M->getDataLayout(); 467 if (DL.isDefault() && !DefaultDataLayout.empty()) { 468 M->setDataLayout(DefaultDataLayout); 469 } 470 471 // Add internal analysis passes from the target machine. 472 Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis() 473 : TargetIRAnalysis())); 474 475 std::unique_ptr<legacy::FunctionPassManager> FPasses; 476 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) { 477 FPasses.reset(new legacy::FunctionPassManager(M.get())); 478 FPasses->add(createTargetTransformInfoWrapperPass( 479 TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis())); 480 } 481 482 if (PrintBreakpoints) { 483 // Default to standard output. 484 if (!Out) { 485 if (OutputFilename.empty()) 486 OutputFilename = "-"; 487 488 std::error_code EC; 489 Out = llvm::make_unique<tool_output_file>(OutputFilename, EC, 490 sys::fs::F_None); 491 if (EC) { 492 errs() << EC.message() << '\n'; 493 return 1; 494 } 495 } 496 Passes.add(createBreakpointPrinter(Out->os())); 497 NoOutput = true; 498 } 499 500 // Create a new optimization pass for each one specified on the command line 501 for (unsigned i = 0; i < PassList.size(); ++i) { 502 if (StandardLinkOpts && 503 StandardLinkOpts.getPosition() < PassList.getPosition(i)) { 504 AddStandardLinkPasses(Passes); 505 StandardLinkOpts = false; 506 } 507 508 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) { 509 AddOptimizationPasses(Passes, *FPasses, 1, 0); 510 OptLevelO1 = false; 511 } 512 513 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) { 514 AddOptimizationPasses(Passes, *FPasses, 2, 0); 515 OptLevelO2 = false; 516 } 517 518 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) { 519 AddOptimizationPasses(Passes, *FPasses, 2, 1); 520 OptLevelOs = false; 521 } 522 523 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) { 524 AddOptimizationPasses(Passes, *FPasses, 2, 2); 525 OptLevelOz = false; 526 } 527 528 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) { 529 AddOptimizationPasses(Passes, *FPasses, 3, 0); 530 OptLevelO3 = false; 531 } 532 533 const PassInfo *PassInf = PassList[i]; 534 Pass *P = nullptr; 535 if (PassInf->getTargetMachineCtor()) 536 P = PassInf->getTargetMachineCtor()(TM.get()); 537 else if (PassInf->getNormalCtor()) 538 P = PassInf->getNormalCtor()(); 539 else 540 errs() << argv[0] << ": cannot create pass: " 541 << PassInf->getPassName() << "\n"; 542 if (P) { 543 PassKind Kind = P->getPassKind(); 544 addPass(Passes, P); 545 546 if (AnalyzeOnly) { 547 switch (Kind) { 548 case PT_BasicBlock: 549 Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet)); 550 break; 551 case PT_Region: 552 Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet)); 553 break; 554 case PT_Loop: 555 Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet)); 556 break; 557 case PT_Function: 558 Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet)); 559 break; 560 case PT_CallGraphSCC: 561 Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet)); 562 break; 563 default: 564 Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet)); 565 break; 566 } 567 } 568 } 569 570 if (PrintEachXForm) 571 Passes.add( 572 createPrintModulePass(errs(), "", PreserveAssemblyUseListOrder)); 573 } 574 575 if (StandardLinkOpts) { 576 AddStandardLinkPasses(Passes); 577 StandardLinkOpts = false; 578 } 579 580 if (OptLevelO1) 581 AddOptimizationPasses(Passes, *FPasses, 1, 0); 582 583 if (OptLevelO2) 584 AddOptimizationPasses(Passes, *FPasses, 2, 0); 585 586 if (OptLevelOs) 587 AddOptimizationPasses(Passes, *FPasses, 2, 1); 588 589 if (OptLevelOz) 590 AddOptimizationPasses(Passes, *FPasses, 2, 2); 591 592 if (OptLevelO3) 593 AddOptimizationPasses(Passes, *FPasses, 3, 0); 594 595 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) { 596 FPasses->doInitialization(); 597 for (Function &F : *M) 598 FPasses->run(F); 599 FPasses->doFinalization(); 600 } 601 602 // Check that the module is well formed on completion of optimization 603 if (!NoVerify && !VerifyEach) 604 Passes.add(createVerifierPass()); 605 606 // In run twice mode, we want to make sure the output is bit-by-bit 607 // equivalent if we run the pass manager again, so setup two buffers and 608 // a stream to write to them. Note that llc does something similar and it 609 // may be worth to abstract this out in the future. 610 SmallVector<char, 0> Buffer; 611 SmallVector<char, 0> CompileTwiceBuffer; 612 std::unique_ptr<raw_svector_ostream> BOS; 613 raw_ostream *OS = nullptr; 614 615 // Write bitcode or assembly to the output as the last step... 616 if (!NoOutput && !AnalyzeOnly) { 617 assert(Out); 618 OS = &Out->os(); 619 if (RunTwice) { 620 BOS = make_unique<raw_svector_ostream>(Buffer); 621 OS = BOS.get(); 622 } 623 if (OutputAssembly) { 624 if (EmitSummaryIndex) 625 report_fatal_error("Text output is incompatible with -module-summary"); 626 if (EmitModuleHash) 627 report_fatal_error("Text output is incompatible with -module-hash"); 628 Passes.add(createPrintModulePass(*OS, "", PreserveAssemblyUseListOrder)); 629 } else 630 Passes.add(createBitcodeWriterPass(*OS, PreserveBitcodeUseListOrder, 631 EmitSummaryIndex, EmitModuleHash)); 632 } 633 634 // Before executing passes, print the final values of the LLVM options. 635 cl::PrintOptionValues(); 636 637 // If requested, run all passes again with the same pass manager to catch 638 // bugs caused by persistent state in the passes 639 if (RunTwice) { 640 std::unique_ptr<Module> M2(CloneModule(M.get())); 641 Passes.run(*M2); 642 CompileTwiceBuffer = Buffer; 643 Buffer.clear(); 644 } 645 646 // Now that we have all of the passes ready, run them. 647 Passes.run(*M); 648 649 // Compare the two outputs and make sure they're the same 650 if (RunTwice) { 651 assert(Out); 652 if (Buffer.size() != CompileTwiceBuffer.size() || 653 (memcmp(Buffer.data(), CompileTwiceBuffer.data(), Buffer.size()) != 654 0)) { 655 errs() << "Running the pass manager twice changed the output.\n" 656 "Writing the result of the second run to the specified output.\n" 657 "To generate the one-run comparison binary, just run without\n" 658 "the compile-twice option\n"; 659 Out->os() << BOS->str(); 660 Out->keep(); 661 return 1; 662 } 663 Out->os() << BOS->str(); 664 } 665 666 // Declare success. 667 if (!NoOutput || PrintBreakpoints) 668 Out->keep(); 669 670 return 0; 671 } 672