1 //===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Optimizations may be specified an arbitrary number of times on the command
11 // line, They are run in the order specified.
13 //===----------------------------------------------------------------------===//
15 #include "BreakpointPrinter.h"
17 #include "NewPMDriver.h"
18 #include "PassPrinters.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/Analysis/CallGraph.h"
21 #include "llvm/Analysis/CallGraphSCCPass.h"
22 #include "llvm/Analysis/LoopPass.h"
23 #include "llvm/Analysis/RegionPass.h"
24 #include "llvm/Analysis/TargetLibraryInfo.h"
25 #include "llvm/Analysis/TargetTransformInfo.h"
26 #include "llvm/Bitcode/BitcodeWriterPass.h"
27 #include "llvm/CodeGen/CommandFlags.inc"
28 #include "llvm/CodeGen/TargetPassConfig.h"
29 #include "llvm/Config/llvm-config.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/DebugInfo.h"
32 #include "llvm/IR/IRPrintingPasses.h"
33 #include "llvm/IR/LLVMContext.h"
34 #include "llvm/IR/LegacyPassManager.h"
35 #include "llvm/IR/LegacyPassNameParser.h"
36 #include "llvm/IR/Module.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"
62 using namespace opt_tool;
64 // The OptimizationList is automatically populated with registered Passes by the
67 static cl::list<const PassInfo*, bool, PassNameParser>
68 PassList(cl::desc("Optimizations available:"));
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
74 static cl::opt<std::string> PassPipeline(
76 cl::desc("A textual description of the pass pipeline for optimizing"),
79 // Other command line options...
81 static cl::opt<std::string>
82 InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
83 cl::init("-"), cl::value_desc("filename"));
85 static cl::opt<std::string>
86 OutputFilename("o", cl::desc("Override output filename"),
87 cl::value_desc("filename"));
90 Force("f", cl::desc("Enable binary output on terminals"));
93 PrintEachXForm("p", cl::desc("Print module after each transformation"));
96 NoOutput("disable-output",
97 cl::desc("Do not write result bitcode file"), cl::Hidden);
100 OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
103 OutputThinLTOBC("thinlto-bc",
104 cl::desc("Write output as ThinLTO-ready bitcode"));
106 static cl::opt<std::string> ThinLinkBitcodeFile(
107 "thin-link-bitcode-file", cl::value_desc("filename"),
109 "A file in which to write minimized bitcode for the thin link only"));
112 NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden);
115 VerifyEach("verify-each", cl::desc("Verify after each transform"));
118 DisableDITypeMap("disable-debug-info-type-map",
119 cl::desc("Don't use a uniquing type map for debug info"));
122 StripDebug("strip-debug",
123 cl::desc("Strip debugger symbol info from translation unit"));
126 StripNamedMetadata("strip-named-metadata",
127 cl::desc("Strip module-level named metadata"));
129 static cl::opt<bool> DisableInline("disable-inlining",
130 cl::desc("Do not run the inliner pass"));
133 DisableOptimizations("disable-opt",
134 cl::desc("Do not run any optimization passes"));
137 StandardLinkOpts("std-link-opts",
138 cl::desc("Include the standard link time optimizations"));
142 cl::desc("Optimization level 0. Similar to clang -O0"));
146 cl::desc("Optimization level 1. Similar to clang -O1"));
150 cl::desc("Optimization level 2. Similar to clang -O2"));
154 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
158 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
162 cl::desc("Optimization level 3. Similar to clang -O3"));
164 static cl::opt<unsigned>
165 CodeGenOptLevel("codegen-opt-level",
166 cl::desc("Override optimization level for codegen hooks"));
168 static cl::opt<std::string>
169 TargetTriple("mtriple", cl::desc("Override target triple for module"));
172 UnitAtATime("funit-at-a-time",
173 cl::desc("Enable IPO. This corresponds to gcc's -funit-at-a-time"),
177 DisableLoopUnrolling("disable-loop-unrolling",
178 cl::desc("Disable loop unrolling in all relevant passes"),
181 DisableLoopVectorization("disable-loop-vectorization",
182 cl::desc("Disable the loop vectorization pass"),
186 DisableSLPVectorization("disable-slp-vectorization",
187 cl::desc("Disable the slp vectorization pass"),
190 static cl::opt<bool> EmitSummaryIndex("module-summary",
191 cl::desc("Emit module summary index"),
194 static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
198 DisableSimplifyLibCalls("disable-simplify-libcalls",
199 cl::desc("Disable simplify-libcalls"));
202 Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
205 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
208 AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
210 static cl::opt<bool> EnableDebugify(
213 "Start the pipeline with debugify and end it with check-debugify"));
215 static cl::opt<bool> DebugifyEach(
218 "Start each pass with debugify and end it with check-debugify"));
220 static cl::opt<std::string>
221 DebugifyExport("debugify-export",
222 cl::desc("Export per-pass debugify statistics to this file"),
223 cl::value_desc("filename"), cl::init(""));
226 PrintBreakpoints("print-breakpoints-for-testing",
227 cl::desc("Print select breakpoints location for testing"));
229 static cl::opt<std::string> ClDataLayout("data-layout",
230 cl::desc("data layout string to use"),
231 cl::value_desc("layout-string"),
234 static cl::opt<bool> PreserveBitcodeUseListOrder(
235 "preserve-bc-uselistorder",
236 cl::desc("Preserve use-list order when writing LLVM bitcode."),
237 cl::init(true), cl::Hidden);
239 static cl::opt<bool> PreserveAssemblyUseListOrder(
240 "preserve-ll-uselistorder",
241 cl::desc("Preserve use-list order when writing LLVM assembly."),
242 cl::init(false), cl::Hidden);
245 RunTwice("run-twice",
246 cl::desc("Run all passes twice, re-using the same pass manager."),
247 cl::init(false), cl::Hidden);
249 static cl::opt<bool> DiscardValueNames(
250 "discard-value-names",
251 cl::desc("Discard names from Value (other than GlobalValue)."),
252 cl::init(false), cl::Hidden);
254 static cl::opt<bool> Coroutines(
256 cl::desc("Enable coroutine passes."),
257 cl::init(false), cl::Hidden);
259 static cl::opt<bool> PassRemarksWithHotness(
260 "pass-remarks-with-hotness",
261 cl::desc("With PGO, include profile count in optimization remarks"),
264 static cl::opt<unsigned> PassRemarksHotnessThreshold(
265 "pass-remarks-hotness-threshold",
266 cl::desc("Minimum profile count required for an optimization remark to be output"),
269 static cl::opt<std::string>
270 RemarksFilename("pass-remarks-output",
271 cl::desc("YAML output filename for pass remarks"),
272 cl::value_desc("filename"));
274 class OptCustomPassManager : public legacy::PassManager {
275 DebugifyStatsMap DIStatsMap;
278 using super = legacy::PassManager;
280 void add(Pass *P) override {
281 // Wrap each pass with (-check)-debugify passes if requested, making
282 // exceptions for passes which shouldn't see -debugify instrumentation.
283 bool WrapWithDebugify = DebugifyEach && !P->getAsImmutablePass() &&
284 !isIRPrintingPass(P) && !isBitcodeWriterPass(P);
285 if (!WrapWithDebugify) {
290 // Apply -debugify/-check-debugify before/after each pass and collect
291 // debug info loss statistics.
292 PassKind Kind = P->getPassKind();
293 StringRef Name = P->getPassName();
295 // TODO: Implement Debugify for BasicBlockPass, LoopPass.
298 super::add(createDebugifyFunctionPass());
300 super::add(createCheckDebugifyFunctionPass(true, Name, &DIStatsMap));
303 super::add(createDebugifyModulePass());
305 super::add(createCheckDebugifyModulePass(true, Name, &DIStatsMap));
313 const DebugifyStatsMap &getDebugifyStatsMap() const { return DIStatsMap; }
316 static inline void addPass(legacy::PassManagerBase &PM, Pass *P) {
317 // Add the pass to the pass manager...
320 // If we are verifying all of the intermediate steps, add the verifier...
322 PM.add(createVerifierPass());
325 /// This routine adds optimization passes based on selected optimization level,
328 /// OptLevel - Optimization Level
329 static void AddOptimizationPasses(legacy::PassManagerBase &MPM,
330 legacy::FunctionPassManager &FPM,
331 TargetMachine *TM, unsigned OptLevel,
332 unsigned SizeLevel) {
333 if (!NoVerify || VerifyEach)
334 FPM.add(createVerifierPass()); // Verify that input is correct
336 PassManagerBuilder Builder;
337 Builder.OptLevel = OptLevel;
338 Builder.SizeLevel = SizeLevel;
342 } else if (OptLevel > 1) {
343 Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
345 Builder.Inliner = createAlwaysInlinerLegacyPass();
347 Builder.DisableUnitAtATime = !UnitAtATime;
348 Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
349 DisableLoopUnrolling : OptLevel == 0;
351 // This is final, unless there is a #pragma vectorize enable
352 if (DisableLoopVectorization)
353 Builder.LoopVectorize = false;
354 // If option wasn't forced via cmd line (-vectorize-loops, -loop-vectorize)
355 else if (!Builder.LoopVectorize)
356 Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
358 // When #pragma vectorize is on for SLP, do the same as above
359 Builder.SLPVectorize =
360 DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2;
363 TM->adjustPassManager(Builder);
366 addCoroutinePassesToExtensionPoints(Builder);
368 Builder.populateFunctionPassManager(FPM);
369 Builder.populateModulePassManager(MPM);
372 static void AddStandardLinkPasses(legacy::PassManagerBase &PM) {
373 PassManagerBuilder Builder;
374 Builder.VerifyInput = true;
375 if (DisableOptimizations)
376 Builder.OptLevel = 0;
379 Builder.Inliner = createFunctionInliningPass();
380 Builder.populateLTOPassManager(PM);
383 //===----------------------------------------------------------------------===//
384 // CodeGen-related helper functions.
387 static CodeGenOpt::Level GetCodeGenOptLevel() {
388 if (CodeGenOptLevel.getNumOccurrences())
389 return static_cast<CodeGenOpt::Level>(unsigned(CodeGenOptLevel));
391 return CodeGenOpt::Less;
393 return CodeGenOpt::Default;
395 return CodeGenOpt::Aggressive;
396 return CodeGenOpt::None;
399 // Returns the TargetMachine instance or zero if no triple is provided.
400 static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
401 StringRef FeaturesStr,
402 const TargetOptions &Options) {
404 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
406 // Some modules don't specify a triple, and this is okay.
411 return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr,
412 FeaturesStr, Options, getRelocModel(),
413 getCodeModel(), GetCodeGenOptLevel());
416 #ifdef LINK_POLLY_INTO_TOOLS
418 void initializePollyPasses(llvm::PassRegistry &Registry);
422 //===----------------------------------------------------------------------===//
425 int main(int argc, char **argv) {
426 InitLLVM X(argc, argv);
428 // Enable debug stream buffering.
429 EnableDebugBuffering = true;
433 InitializeAllTargets();
434 InitializeAllTargetMCs();
435 InitializeAllAsmPrinters();
436 InitializeAllAsmParsers();
439 PassRegistry &Registry = *PassRegistry::getPassRegistry();
440 initializeCore(Registry);
441 initializeCoroutines(Registry);
442 initializeScalarOpts(Registry);
443 initializeObjCARCOpts(Registry);
444 initializeVectorization(Registry);
445 initializeIPO(Registry);
446 initializeAnalysis(Registry);
447 initializeTransformUtils(Registry);
448 initializeInstCombine(Registry);
449 initializeAggressiveInstCombine(Registry);
450 initializeInstrumentation(Registry);
451 initializeTarget(Registry);
452 // For codegen passes, only passes that do IR to IR transformation are
454 initializeExpandMemCmpPassPass(Registry);
455 initializeScalarizeMaskedMemIntrinPass(Registry);
456 initializeCodeGenPreparePass(Registry);
457 initializeAtomicExpandPass(Registry);
458 initializeRewriteSymbolsLegacyPassPass(Registry);
459 initializeWinEHPreparePass(Registry);
460 initializeDwarfEHPreparePass(Registry);
461 initializeSafeStackLegacyPassPass(Registry);
462 initializeSjLjEHPreparePass(Registry);
463 initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
464 initializeGlobalMergePass(Registry);
465 initializeIndirectBrExpandPassPass(Registry);
466 initializeInterleavedAccessPass(Registry);
467 initializeEntryExitInstrumenterPass(Registry);
468 initializePostInlineEntryExitInstrumenterPass(Registry);
469 initializeUnreachableBlockElimLegacyPassPass(Registry);
470 initializeExpandReductionsPass(Registry);
471 initializeWasmEHPreparePass(Registry);
472 initializeWriteBitcodePassPass(Registry);
474 #ifdef LINK_POLLY_INTO_TOOLS
475 polly::initializePollyPasses(Registry);
478 cl::ParseCommandLineOptions(argc, argv,
479 "llvm .bc -> .bc modular optimizer and analysis printer\n");
481 if (AnalyzeOnly && NoOutput) {
482 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
488 Context.setDiscardValueNames(DiscardValueNames);
489 if (!DisableDITypeMap)
490 Context.enableDebugTypeODRUniquing();
492 if (PassRemarksWithHotness)
493 Context.setDiagnosticsHotnessRequested(true);
495 if (PassRemarksHotnessThreshold)
496 Context.setDiagnosticsHotnessThreshold(PassRemarksHotnessThreshold);
498 std::unique_ptr<ToolOutputFile> OptRemarkFile;
499 if (RemarksFilename != "") {
502 llvm::make_unique<ToolOutputFile>(RemarksFilename, EC, sys::fs::F_None);
504 errs() << EC.message() << '\n';
507 Context.setDiagnosticsOutputFile(
508 llvm::make_unique<yaml::Output>(OptRemarkFile->os()));
511 // Load the input module...
512 std::unique_ptr<Module> M =
513 parseIRFile(InputFilename, Err, Context, !NoVerify, ClDataLayout);
516 Err.print(argv[0], errs());
520 // Strip debug info before running the verifier.
524 // Erase module-level named metadata, if requested.
525 if (StripNamedMetadata) {
526 while (!M->named_metadata_empty()) {
527 NamedMDNode *NMD = &*M->named_metadata_begin();
528 M->eraseNamedMetadata(NMD);
532 // If we are supposed to override the target triple or data layout, do so now.
533 if (!TargetTriple.empty())
534 M->setTargetTriple(Triple::normalize(TargetTriple));
536 // Immediately run the verifier to catch any problems before starting up the
537 // pass pipelines. Otherwise we can crash on broken code during
538 // doInitialization().
539 if (!NoVerify && verifyModule(*M, &errs())) {
540 errs() << argv[0] << ": " << InputFilename
541 << ": error: input module is broken!\n";
545 // Figure out what stream we are supposed to write to...
546 std::unique_ptr<ToolOutputFile> Out;
547 std::unique_ptr<ToolOutputFile> ThinLinkOut;
549 if (!OutputFilename.empty())
550 errs() << "WARNING: The -o (output filename) option is ignored when\n"
551 "the --disable-output option is used.\n";
553 // Default to standard output.
554 if (OutputFilename.empty())
555 OutputFilename = "-";
558 Out.reset(new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
560 errs() << EC.message() << '\n';
564 if (!ThinLinkBitcodeFile.empty()) {
566 new ToolOutputFile(ThinLinkBitcodeFile, EC, sys::fs::F_None));
568 errs() << EC.message() << '\n';
574 Triple ModuleTriple(M->getTargetTriple());
575 std::string CPUStr, FeaturesStr;
576 TargetMachine *Machine = nullptr;
577 const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
579 if (ModuleTriple.getArch()) {
580 CPUStr = getCPUStr();
581 FeaturesStr = getFeaturesStr();
582 Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options);
585 std::unique_ptr<TargetMachine> TM(Machine);
587 // Override function attributes based on CPUStr, FeaturesStr, and command line
589 setFunctionAttributes(CPUStr, FeaturesStr, *M);
591 // If the output is set to be emitted to standard out, and standard out is a
592 // console, print out a warning message and refuse to do it. We don't
593 // impress anyone by spewing tons of binary goo to a terminal.
594 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
595 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
598 if (PassPipeline.getNumOccurrences() > 0) {
599 OutputKind OK = OK_NoOutput;
603 : (OutputThinLTOBC ? OK_OutputThinLTOBitcode : OK_OutputBitcode);
605 VerifierKind VK = VK_VerifyInAndOut;
609 VK = VK_VerifyEachPass;
611 // The user has asked to use the new pass manager and provided a pipeline
612 // string. Hand off the rest of the functionality to the new code for that
614 return runPassPipeline(argv[0], *M, TM.get(), Out.get(), ThinLinkOut.get(),
615 OptRemarkFile.get(), PassPipeline, OK, VK,
616 PreserveAssemblyUseListOrder,
617 PreserveBitcodeUseListOrder, EmitSummaryIndex,
618 EmitModuleHash, EnableDebugify)
623 // Create a PassManager to hold and optimize the collection of passes we are
625 OptCustomPassManager Passes;
626 bool AddOneTimeDebugifyPasses = EnableDebugify && !DebugifyEach;
628 // Add an appropriate TargetLibraryInfo pass for the module's triple.
629 TargetLibraryInfoImpl TLII(ModuleTriple);
631 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
632 if (DisableSimplifyLibCalls)
633 TLII.disableAllFunctions();
634 Passes.add(new TargetLibraryInfoWrapperPass(TLII));
636 // Add internal analysis passes from the target machine.
637 Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis()
638 : TargetIRAnalysis()));
640 if (AddOneTimeDebugifyPasses)
641 Passes.add(createDebugifyModulePass());
643 std::unique_ptr<legacy::FunctionPassManager> FPasses;
644 if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz ||
646 FPasses.reset(new legacy::FunctionPassManager(M.get()));
647 FPasses->add(createTargetTransformInfoWrapperPass(
648 TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis()));
651 if (PrintBreakpoints) {
652 // Default to standard output.
654 if (OutputFilename.empty())
655 OutputFilename = "-";
658 Out = llvm::make_unique<ToolOutputFile>(OutputFilename, EC,
661 errs() << EC.message() << '\n';
665 Passes.add(createBreakpointPrinter(Out->os()));
670 // FIXME: We should dyn_cast this when supported.
671 auto <M = static_cast<LLVMTargetMachine &>(*TM);
672 Pass *TPC = LTM.createPassConfig(Passes);
676 // Create a new optimization pass for each one specified on the command line
677 for (unsigned i = 0; i < PassList.size(); ++i) {
678 if (StandardLinkOpts &&
679 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
680 AddStandardLinkPasses(Passes);
681 StandardLinkOpts = false;
684 if (OptLevelO0 && OptLevelO0.getPosition() < PassList.getPosition(i)) {
685 AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
689 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
690 AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
694 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
695 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
699 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
700 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
704 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
705 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
709 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
710 AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
714 const PassInfo *PassInf = PassList[i];
716 if (PassInf->getNormalCtor())
717 P = PassInf->getNormalCtor()();
719 errs() << argv[0] << ": cannot create pass: "
720 << PassInf->getPassName() << "\n";
722 PassKind Kind = P->getPassKind();
728 Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet));
731 Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet));
734 Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet));
737 Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet));
739 case PT_CallGraphSCC:
740 Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet));
743 Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet));
751 createPrintModulePass(errs(), "", PreserveAssemblyUseListOrder));
754 if (StandardLinkOpts) {
755 AddStandardLinkPasses(Passes);
756 StandardLinkOpts = false;
760 AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
763 AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
766 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
769 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
772 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
775 AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
778 FPasses->doInitialization();
779 for (Function &F : *M)
781 FPasses->doFinalization();
784 // Check that the module is well formed on completion of optimization
785 if (!NoVerify && !VerifyEach)
786 Passes.add(createVerifierPass());
788 if (AddOneTimeDebugifyPasses)
789 Passes.add(createCheckDebugifyModulePass(false));
791 // In run twice mode, we want to make sure the output is bit-by-bit
792 // equivalent if we run the pass manager again, so setup two buffers and
793 // a stream to write to them. Note that llc does something similar and it
794 // may be worth to abstract this out in the future.
795 SmallVector<char, 0> Buffer;
796 SmallVector<char, 0> FirstRunBuffer;
797 std::unique_ptr<raw_svector_ostream> BOS;
798 raw_ostream *OS = nullptr;
800 // Write bitcode or assembly to the output as the last step...
801 if (!NoOutput && !AnalyzeOnly) {
805 BOS = make_unique<raw_svector_ostream>(Buffer);
808 if (OutputAssembly) {
809 if (EmitSummaryIndex)
810 report_fatal_error("Text output is incompatible with -module-summary");
812 report_fatal_error("Text output is incompatible with -module-hash");
813 Passes.add(createPrintModulePass(*OS, "", PreserveAssemblyUseListOrder));
814 } else if (OutputThinLTOBC)
815 Passes.add(createWriteThinLTOBitcodePass(
816 *OS, ThinLinkOut ? &ThinLinkOut->os() : nullptr));
818 Passes.add(createBitcodeWriterPass(*OS, PreserveBitcodeUseListOrder,
819 EmitSummaryIndex, EmitModuleHash));
822 // Before executing passes, print the final values of the LLVM options.
823 cl::PrintOptionValues();
826 // Now that we have all of the passes ready, run them.
829 // If requested, run all passes twice with the same pass manager to catch
830 // bugs caused by persistent state in the passes.
831 std::unique_ptr<Module> M2(CloneModule(*M));
832 // Run all passes on the original module first, so the second run processes
833 // the clone to catch CloneModule bugs.
835 FirstRunBuffer = Buffer;
840 // Compare the two outputs and make sure they're the same
842 if (Buffer.size() != FirstRunBuffer.size() ||
843 (memcmp(Buffer.data(), FirstRunBuffer.data(), Buffer.size()) != 0)) {
845 << "Running the pass manager twice changed the output.\n"
846 "Writing the result of the second run to the specified output.\n"
847 "To generate the one-run comparison binary, just run without\n"
848 "the compile-twice option\n";
849 Out->os() << BOS->str();
852 OptRemarkFile->keep();
855 Out->os() << BOS->str();
858 if (DebugifyEach && !DebugifyExport.empty())
859 exportDebugifyStats(DebugifyExport, Passes.getDebugifyStatsMap());
862 if (!NoOutput || PrintBreakpoints)
866 OptRemarkFile->keep();