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