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