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