1 //===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Optimizations may be specified an arbitrary number of times on the command
10 // line, They are run in the order specified.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "BreakpointPrinter.h"
15 #include "NewPMDriver.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/Analysis/CallGraph.h"
18 #include "llvm/Analysis/CallGraphSCCPass.h"
19 #include "llvm/Analysis/LoopPass.h"
20 #include "llvm/Analysis/RegionPass.h"
21 #include "llvm/Analysis/TargetLibraryInfo.h"
22 #include "llvm/Analysis/TargetTransformInfo.h"
23 #include "llvm/AsmParser/Parser.h"
24 #include "llvm/CodeGen/CommandFlags.h"
25 #include "llvm/CodeGen/TargetPassConfig.h"
26 #include "llvm/Config/llvm-config.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/DebugInfo.h"
29 #include "llvm/IR/LLVMContext.h"
30 #include "llvm/IR/LLVMRemarkStreamer.h"
31 #include "llvm/IR/LegacyPassManager.h"
32 #include "llvm/IR/LegacyPassNameParser.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/IR/ModuleSummaryIndex.h"
35 #include "llvm/IR/Verifier.h"
36 #include "llvm/IRReader/IRReader.h"
37 #include "llvm/InitializePasses.h"
38 #include "llvm/LinkAllIR.h"
39 #include "llvm/LinkAllPasses.h"
40 #include "llvm/MC/SubtargetFeature.h"
41 #include "llvm/MC/TargetRegistry.h"
42 #include "llvm/Passes/PassPlugin.h"
43 #include "llvm/Remarks/HotnessThresholdParser.h"
44 #include "llvm/Support/Debug.h"
45 #include "llvm/Support/FileSystem.h"
46 #include "llvm/Support/Host.h"
47 #include "llvm/Support/InitLLVM.h"
48 #include "llvm/Support/PluginLoader.h"
49 #include "llvm/Support/SourceMgr.h"
50 #include "llvm/Support/SystemUtils.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/IPO/AlwaysInliner.h"
56 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
57 #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
58 #include "llvm/Transforms/Utils/Cloning.h"
59 #include "llvm/Transforms/Utils/Debugify.h"
60 #include <algorithm>
61 #include <memory>
62 using namespace llvm;
63 using namespace opt_tool;
64 
65 static codegen::RegisterCodeGenFlags CFG;
66 
67 // The OptimizationList is automatically populated with registered Passes by the
68 // PassNameParser.
69 static cl::list<const PassInfo *, bool, PassNameParser> PassList(cl::desc(
70     "Optimizations available (use '-passes=' for the new pass manager)"));
71 
72 static cl::opt<bool> EnableNewPassManager(
73     "enable-new-pm",
74     cl::desc("Enable the new pass manager, translating "
75              "'opt -foo' to 'opt -passes=foo'. This is strictly for the new PM "
76              "migration, use '-passes=' when possible."),
77     cl::init(true));
78 
79 // This flag specifies a textual description of the optimization pass pipeline
80 // to run over the module. This flag switches opt to use the new pass manager
81 // infrastructure, completely disabling all of the flags specific to the old
82 // pass management.
83 static cl::opt<std::string> PassPipeline(
84     "passes",
85     cl::desc(
86         "A textual description of the pass pipeline. To have analysis passes "
87         "available before a certain pass, add 'require<foo-analysis>'."));
88 
89 static cl::opt<bool> PrintPasses("print-passes",
90                                  cl::desc("Print available passes that can be "
91                                           "specified in -passes=foo and exit"));
92 
93 static cl::opt<std::string>
94 InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
95     cl::init("-"), cl::value_desc("filename"));
96 
97 static cl::opt<std::string>
98 OutputFilename("o", cl::desc("Override output filename"),
99                cl::value_desc("filename"));
100 
101 static cl::opt<bool>
102 Force("f", cl::desc("Enable binary output on terminals"));
103 
104 static cl::opt<bool>
105 NoOutput("disable-output",
106          cl::desc("Do not write result bitcode file"), cl::Hidden);
107 
108 static cl::opt<bool>
109 OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
110 
111 static cl::opt<bool>
112     OutputThinLTOBC("thinlto-bc",
113                     cl::desc("Write output as ThinLTO-ready bitcode"));
114 
115 static cl::opt<bool>
116     SplitLTOUnit("thinlto-split-lto-unit",
117                  cl::desc("Enable splitting of a ThinLTO LTOUnit"));
118 
119 static cl::opt<std::string> ThinLinkBitcodeFile(
120     "thin-link-bitcode-file", cl::value_desc("filename"),
121     cl::desc(
122         "A file in which to write minimized bitcode for the thin link only"));
123 
124 static cl::opt<bool>
125 NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden);
126 
127 static cl::opt<bool> NoUpgradeDebugInfo("disable-upgrade-debug-info",
128                                         cl::desc("Generate invalid output"),
129                                         cl::ReallyHidden);
130 
131 static cl::opt<bool> VerifyEach("verify-each",
132                                 cl::desc("Verify after each transform"));
133 
134 static cl::opt<bool>
135     DisableDITypeMap("disable-debug-info-type-map",
136                      cl::desc("Don't use a uniquing type map for debug info"));
137 
138 static cl::opt<bool>
139 StripDebug("strip-debug",
140            cl::desc("Strip debugger symbol info from translation unit"));
141 
142 static cl::opt<bool>
143     StripNamedMetadata("strip-named-metadata",
144                        cl::desc("Strip module-level named metadata"));
145 
146 
147 
148 static cl::opt<bool>
149     OptLevelO0("O0", cl::desc("Optimization level 0. Similar to clang -O0. "
150                               "Use -passes='default<O0>' for the new PM"));
151 
152 static cl::opt<bool>
153     OptLevelO1("O1", cl::desc("Optimization level 1. Similar to clang -O1. "
154                               "Use -passes='default<O1>' for the new PM"));
155 
156 static cl::opt<bool>
157     OptLevelO2("O2", cl::desc("Optimization level 2. Similar to clang -O2. "
158                               "Use -passes='default<O2>' for the new PM"));
159 
160 static cl::opt<bool>
161     OptLevelOs("Os", cl::desc("Like -O2 but size-conscious. Similar to clang "
162                               "-Os. Use -passes='default<Os>' for the new PM"));
163 
164 static cl::opt<bool> OptLevelOz(
165     "Oz",
166     cl::desc("Like -O2 but optimize for code size above all else. Similar to "
167              "clang -Oz. Use -passes='default<Oz>' for the new PM"));
168 
169 static cl::opt<bool>
170     OptLevelO3("O3", cl::desc("Optimization level 3. Similar to clang -O3. "
171                               "Use -passes='default<O3>' for the new PM"));
172 
173 static cl::opt<unsigned> CodeGenOptLevel(
174     "codegen-opt-level",
175     cl::desc("Override optimization level for codegen hooks, legacy PM only"));
176 
177 static cl::opt<std::string>
178 TargetTriple("mtriple", cl::desc("Override target triple for module"));
179 
180 cl::opt<bool> DisableLoopUnrolling(
181     "disable-loop-unrolling",
182     cl::desc("Disable loop unrolling in all relevant passes"), cl::init(false));
183 
184 static cl::opt<bool> EmitSummaryIndex("module-summary",
185                                       cl::desc("Emit module summary index"),
186                                       cl::init(false));
187 
188 static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
189                                     cl::init(false));
190 
191 static cl::opt<bool>
192 DisableSimplifyLibCalls("disable-simplify-libcalls",
193                         cl::desc("Disable simplify-libcalls"));
194 
195 static cl::list<std::string> DisableBuiltins(
196     "disable-builtin",
197     cl::desc("Disable specific target library builtin function"));
198 
199 static cl::opt<bool> EnableDebugify(
200     "enable-debugify",
201     cl::desc(
202         "Start the pipeline with debugify and end it with check-debugify"));
203 
204 static cl::opt<bool> VerifyDebugInfoPreserve(
205     "verify-debuginfo-preserve",
206     cl::desc("Start the pipeline with collecting and end it with checking of "
207              "debug info preservation."));
208 
209 static cl::opt<bool> VerifyEachDebugInfoPreserve(
210     "verify-each-debuginfo-preserve",
211     cl::desc("Start each pass with collecting and end it with checking of "
212              "debug info preservation."));
213 
214 static cl::opt<std::string>
215     VerifyDIPreserveExport("verify-di-preserve-export",
216                    cl::desc("Export debug info preservation failures into "
217                             "specified (JSON) file (should be abs path as we use"
218                             " append mode to insert new JSON objects)"),
219                    cl::value_desc("filename"), cl::init(""));
220 
221 static cl::opt<bool>
222 PrintBreakpoints("print-breakpoints-for-testing",
223                  cl::desc("Print select breakpoints location for testing"));
224 
225 static cl::opt<std::string> ClDataLayout("data-layout",
226                                          cl::desc("data layout string to use"),
227                                          cl::value_desc("layout-string"),
228                                          cl::init(""));
229 
230 static cl::opt<bool> PreserveBitcodeUseListOrder(
231     "preserve-bc-uselistorder",
232     cl::desc("Preserve use-list order when writing LLVM bitcode."),
233     cl::init(true), cl::Hidden);
234 
235 static cl::opt<bool> PreserveAssemblyUseListOrder(
236     "preserve-ll-uselistorder",
237     cl::desc("Preserve use-list order when writing LLVM assembly."),
238     cl::init(false), cl::Hidden);
239 
240 static cl::opt<bool> RunTwice("run-twice",
241                               cl::desc("Run all passes twice, re-using the "
242                                        "same pass manager (legacy PM only)."),
243                               cl::init(false), cl::Hidden);
244 
245 static cl::opt<bool> DiscardValueNames(
246     "discard-value-names",
247     cl::desc("Discard names from Value (other than GlobalValue)."),
248     cl::init(false), cl::Hidden);
249 
250 static cl::opt<bool> TimeTrace(
251     "time-trace",
252     cl::desc("Record time trace"));
253 
254 static cl::opt<unsigned> TimeTraceGranularity(
255     "time-trace-granularity",
256     cl::desc("Minimum time granularity (in microseconds) traced by time profiler"),
257     cl::init(500), cl::Hidden);
258 
259 static cl::opt<std::string>
260     TimeTraceFile("time-trace-file",
261                     cl::desc("Specify time trace file destination"),
262                     cl::value_desc("filename"));
263 
264 static cl::opt<bool> RemarksWithHotness(
265     "pass-remarks-with-hotness",
266     cl::desc("With PGO, include profile count in optimization remarks"),
267     cl::Hidden);
268 
269 static cl::opt<Optional<uint64_t>, false, remarks::HotnessThresholdParser>
270     RemarksHotnessThreshold(
271         "pass-remarks-hotness-threshold",
272         cl::desc("Minimum profile count required for "
273                  "an optimization remark to be output. "
274                  "Use 'auto' to apply the threshold from profile summary."),
275         cl::value_desc("N or 'auto'"), cl::init(0), cl::Hidden);
276 
277 static cl::opt<std::string>
278     RemarksFilename("pass-remarks-output",
279                     cl::desc("Output filename for pass remarks"),
280                     cl::value_desc("filename"));
281 
282 static cl::opt<std::string>
283     RemarksPasses("pass-remarks-filter",
284                   cl::desc("Only record optimization remarks from passes whose "
285                            "names match the given regular expression"),
286                   cl::value_desc("regex"));
287 
288 static cl::opt<std::string> RemarksFormat(
289     "pass-remarks-format",
290     cl::desc("The format used for serializing remarks (default: YAML)"),
291     cl::value_desc("format"), cl::init("yaml"));
292 
293 static cl::list<std::string>
294     PassPlugins("load-pass-plugin",
295                 cl::desc("Load passes from plugin library"));
296 
297 namespace llvm {
298 cl::opt<PGOKind>
299     PGOKindFlag("pgo-kind", cl::init(NoPGO), cl::Hidden,
300                 cl::desc("The kind of profile guided optimization"),
301                 cl::values(clEnumValN(NoPGO, "nopgo", "Do not use PGO."),
302                            clEnumValN(InstrGen, "pgo-instr-gen-pipeline",
303                                       "Instrument the IR to generate profile."),
304                            clEnumValN(InstrUse, "pgo-instr-use-pipeline",
305                                       "Use instrumented profile to guide PGO."),
306                            clEnumValN(SampleUse, "pgo-sample-use-pipeline",
307                                       "Use sampled profile to guide PGO.")));
308 cl::opt<std::string> ProfileFile("profile-file",
309                                  cl::desc("Path to the profile."), cl::Hidden);
310 
311 cl::opt<CSPGOKind> CSPGOKindFlag(
312     "cspgo-kind", cl::init(NoCSPGO), cl::Hidden,
313     cl::desc("The kind of context sensitive profile guided optimization"),
314     cl::values(
315         clEnumValN(NoCSPGO, "nocspgo", "Do not use CSPGO."),
316         clEnumValN(
317             CSInstrGen, "cspgo-instr-gen-pipeline",
318             "Instrument (context sensitive) the IR to generate profile."),
319         clEnumValN(
320             CSInstrUse, "cspgo-instr-use-pipeline",
321             "Use instrumented (context sensitive) profile to guide PGO.")));
322 cl::opt<std::string> CSProfileGenFile(
323     "cs-profilegen-file",
324     cl::desc("Path to the instrumented context sensitive profile."),
325     cl::Hidden);
326 } // namespace llvm
327 
328 static inline void addPass(legacy::PassManagerBase &PM, Pass *P) {
329   // Add the pass to the pass manager...
330   PM.add(P);
331 
332   // If we are verifying all of the intermediate steps, add the verifier...
333   if (VerifyEach)
334     PM.add(createVerifierPass());
335 }
336 
337 /// This routine adds optimization passes based on selected optimization level,
338 /// OptLevel.
339 ///
340 /// OptLevel - Optimization Level
341 static void AddOptimizationPasses(legacy::PassManagerBase &MPM,
342                                   legacy::FunctionPassManager &FPM,
343                                   TargetMachine *TM, unsigned OptLevel,
344                                   unsigned SizeLevel) {
345   if (!NoVerify || VerifyEach)
346     FPM.add(createVerifierPass()); // Verify that input is correct
347 
348   PassManagerBuilder Builder;
349   Builder.OptLevel = OptLevel;
350   Builder.SizeLevel = SizeLevel;
351 
352   if (OptLevel > 1) {
353     Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
354   } else {
355     Builder.Inliner = createAlwaysInlinerLegacyPass();
356   }
357   Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
358                                DisableLoopUnrolling : OptLevel == 0;
359 
360   Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
361 
362   Builder.SLPVectorize = OptLevel > 1 && SizeLevel < 2;
363 
364   if (TM)
365     TM->adjustPassManager(Builder);
366 
367   switch (PGOKindFlag) {
368   case InstrGen:
369     Builder.EnablePGOInstrGen = true;
370     Builder.PGOInstrGen = ProfileFile;
371     break;
372   case InstrUse:
373     Builder.PGOInstrUse = ProfileFile;
374     break;
375   case SampleUse:
376     Builder.PGOSampleUse = ProfileFile;
377     break;
378   default:
379     break;
380   }
381 
382   switch (CSPGOKindFlag) {
383   case CSInstrGen:
384     Builder.EnablePGOCSInstrGen = true;
385     break;
386   case CSInstrUse:
387     Builder.EnablePGOCSInstrUse = true;
388     break;
389   default:
390     break;
391   }
392 
393   Builder.populateFunctionPassManager(FPM);
394   Builder.populateModulePassManager(MPM);
395 }
396 
397 //===----------------------------------------------------------------------===//
398 // CodeGen-related helper functions.
399 //
400 
401 static CodeGenOpt::Level GetCodeGenOptLevel() {
402   if (CodeGenOptLevel.getNumOccurrences())
403     return static_cast<CodeGenOpt::Level>(unsigned(CodeGenOptLevel));
404   if (OptLevelO1)
405     return CodeGenOpt::Less;
406   if (OptLevelO2)
407     return CodeGenOpt::Default;
408   if (OptLevelO3)
409     return CodeGenOpt::Aggressive;
410   return CodeGenOpt::None;
411 }
412 
413 // Returns the TargetMachine instance or zero if no triple is provided.
414 static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
415                                        StringRef FeaturesStr,
416                                        const TargetOptions &Options) {
417   std::string Error;
418   const Target *TheTarget =
419       TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error);
420   // Some modules don't specify a triple, and this is okay.
421   if (!TheTarget) {
422     return nullptr;
423   }
424 
425   return TheTarget->createTargetMachine(
426       TheTriple.getTriple(), codegen::getCPUStr(), codegen::getFeaturesStr(),
427       Options, codegen::getExplicitRelocModel(),
428       codegen::getExplicitCodeModel(), GetCodeGenOptLevel());
429 }
430 
431 #ifdef BUILD_EXAMPLES
432 void initializeExampleIRTransforms(llvm::PassRegistry &Registry);
433 #endif
434 
435 struct TimeTracerRAII {
436   TimeTracerRAII(StringRef ProgramName) {
437     if (TimeTrace)
438       timeTraceProfilerInitialize(TimeTraceGranularity, ProgramName);
439   }
440   ~TimeTracerRAII() {
441     if (TimeTrace) {
442       if (auto E = timeTraceProfilerWrite(TimeTraceFile, OutputFilename)) {
443         handleAllErrors(std::move(E), [&](const StringError &SE) {
444           errs() << SE.getMessage() << "\n";
445         });
446         return;
447       }
448       timeTraceProfilerCleanup();
449     }
450   }
451 };
452 
453 // For use in NPM transition. Currently this contains most codegen-specific
454 // passes. Remove passes from here when porting to the NPM.
455 // TODO: use a codegen version of PassRegistry.def/PassBuilder::is*Pass() once
456 // it exists.
457 static bool shouldPinPassToLegacyPM(StringRef Pass) {
458   std::vector<StringRef> PassNameExactToIgnore = {
459       "nvvm-reflect",
460       "nvvm-intr-range",
461       "amdgpu-simplifylib",
462       "amdgpu-usenative",
463       "amdgpu-promote-alloca",
464       "amdgpu-promote-alloca-to-vector",
465       "amdgpu-lower-kernel-attributes",
466       "amdgpu-propagate-attributes-early",
467       "amdgpu-propagate-attributes-late",
468       "amdgpu-unify-metadata",
469       "amdgpu-printf-runtime-binding",
470       "amdgpu-always-inline"};
471   if (llvm::is_contained(PassNameExactToIgnore, Pass))
472     return false;
473 
474   std::vector<StringRef> PassNamePrefix = {
475       "x86-",    "xcore-", "wasm-",  "systemz-", "ppc-",    "nvvm-",
476       "nvptx-",  "mips-",  "lanai-", "hexagon-", "bpf-",    "avr-",
477       "thumb2-", "arm-",   "si-",    "gcn-",     "amdgpu-", "aarch64-",
478       "amdgcn-", "polly-", "riscv-", "dxil-"};
479   std::vector<StringRef> PassNameContain = {"ehprepare"};
480   std::vector<StringRef> PassNameExact = {
481       "safe-stack",           "cost-model",
482       "codegenprepare",       "interleaved-load-combine",
483       "unreachableblockelim", "verify-safepoint-ir",
484       "atomic-expand",        "expandvp",
485       "hardware-loops",       "type-promotion",
486       "mve-tail-predication", "interleaved-access",
487       "global-merge",         "pre-isel-intrinsic-lowering",
488       "expand-reductions",    "indirectbr-expand",
489       "generic-to-nvvm",      "expandmemcmp",
490       "loop-reduce",          "lower-amx-type",
491       "pre-amx-config",       "lower-amx-intrinsics",
492       "polyhedral-info",      "print-polyhedral-info",
493       "replace-with-veclib",  "jmc-instrument",
494       "dot-regions",          "dot-regions-only",
495       "view-regions",         "view-regions-only",
496       "select-optimize"};
497   for (const auto &P : PassNamePrefix)
498     if (Pass.startswith(P))
499       return true;
500   for (const auto &P : PassNameContain)
501     if (Pass.contains(P))
502       return true;
503   return llvm::is_contained(PassNameExact, Pass);
504 }
505 
506 // For use in NPM transition.
507 static bool shouldForceLegacyPM() {
508   for (const auto &P : PassList) {
509     StringRef Arg = P->getPassArgument();
510     if (shouldPinPassToLegacyPM(Arg))
511       return true;
512   }
513   return false;
514 }
515 
516 //===----------------------------------------------------------------------===//
517 // main for opt
518 //
519 int main(int argc, char **argv) {
520   InitLLVM X(argc, argv);
521 
522   // Enable debug stream buffering.
523   EnableDebugBuffering = true;
524 
525   InitializeAllTargets();
526   InitializeAllTargetMCs();
527   InitializeAllAsmPrinters();
528   InitializeAllAsmParsers();
529 
530   // Initialize passes
531   PassRegistry &Registry = *PassRegistry::getPassRegistry();
532   initializeCore(Registry);
533   initializeScalarOpts(Registry);
534   initializeObjCARCOpts(Registry);
535   initializeVectorization(Registry);
536   initializeIPO(Registry);
537   initializeAnalysis(Registry);
538   initializeTransformUtils(Registry);
539   initializeInstCombine(Registry);
540   initializeAggressiveInstCombine(Registry);
541   initializeInstrumentation(Registry);
542   initializeTarget(Registry);
543   // For codegen passes, only passes that do IR to IR transformation are
544   // supported.
545   initializeExpandMemCmpPassPass(Registry);
546   initializeScalarizeMaskedMemIntrinLegacyPassPass(Registry);
547   initializeSelectOptimizePass(Registry);
548   initializeCodeGenPreparePass(Registry);
549   initializeAtomicExpandPass(Registry);
550   initializeRewriteSymbolsLegacyPassPass(Registry);
551   initializeWinEHPreparePass(Registry);
552   initializeDwarfEHPrepareLegacyPassPass(Registry);
553   initializeSafeStackLegacyPassPass(Registry);
554   initializeSjLjEHPreparePass(Registry);
555   initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
556   initializeGlobalMergePass(Registry);
557   initializeIndirectBrExpandPassPass(Registry);
558   initializeInterleavedLoadCombinePass(Registry);
559   initializeInterleavedAccessPass(Registry);
560   initializeEntryExitInstrumenterPass(Registry);
561   initializePostInlineEntryExitInstrumenterPass(Registry);
562   initializeUnreachableBlockElimLegacyPassPass(Registry);
563   initializeExpandReductionsPass(Registry);
564   initializeExpandVectorPredicationPass(Registry);
565   initializeWasmEHPreparePass(Registry);
566   initializeWriteBitcodePassPass(Registry);
567   initializeHardwareLoopsPass(Registry);
568   initializeTypePromotionPass(Registry);
569   initializeReplaceWithVeclibLegacyPass(Registry);
570   initializeJMCInstrumenterPass(Registry);
571 
572 #ifdef BUILD_EXAMPLES
573   initializeExampleIRTransforms(Registry);
574 #endif
575 
576   SmallVector<PassPlugin, 1> PluginList;
577   PassPlugins.setCallback([&](const std::string &PluginPath) {
578     auto Plugin = PassPlugin::Load(PluginPath);
579     if (!Plugin) {
580       errs() << "Failed to load passes from '" << PluginPath
581              << "'. Request ignored.\n";
582       return;
583     }
584     PluginList.emplace_back(Plugin.get());
585   });
586 
587   cl::ParseCommandLineOptions(argc, argv,
588     "llvm .bc -> .bc modular optimizer and analysis printer\n");
589 
590   LLVMContext Context;
591 
592   // If `-passes=` is specified, use NPM.
593   // If `-enable-new-pm` is specified and there are no codegen passes, use NPM.
594   // e.g. `-enable-new-pm -sroa` will use NPM.
595   // but `-enable-new-pm -codegenprepare` will still revert to legacy PM.
596   const bool UseNPM = (EnableNewPassManager && !shouldForceLegacyPM()) ||
597                       PassPipeline.getNumOccurrences() > 0;
598 
599   if (!UseNPM && PluginList.size()) {
600     errs() << argv[0] << ": " << PassPlugins.ArgStr
601            << " specified with legacy PM.\n";
602     return 1;
603   }
604 
605   // FIXME: once the legacy PM code is deleted, move runPassPipeline() here and
606   // construct the PassBuilder before parsing IR so we can reuse the same
607   // PassBuilder for print passes.
608   if (PrintPasses) {
609     printPasses(outs());
610     return 0;
611   }
612 
613   TimeTracerRAII TimeTracer(argv[0]);
614 
615   SMDiagnostic Err;
616 
617   Context.setDiscardValueNames(DiscardValueNames);
618   if (!DisableDITypeMap)
619     Context.enableDebugTypeODRUniquing();
620 
621   Expected<std::unique_ptr<ToolOutputFile>> RemarksFileOrErr =
622       setupLLVMOptimizationRemarks(Context, RemarksFilename, RemarksPasses,
623                                    RemarksFormat, RemarksWithHotness,
624                                    RemarksHotnessThreshold);
625   if (Error E = RemarksFileOrErr.takeError()) {
626     errs() << toString(std::move(E)) << '\n';
627     return 1;
628   }
629   std::unique_ptr<ToolOutputFile> RemarksFile = std::move(*RemarksFileOrErr);
630 
631   // Load the input module...
632   auto SetDataLayout = [](StringRef) -> Optional<std::string> {
633     if (ClDataLayout.empty())
634       return None;
635     return ClDataLayout;
636   };
637   std::unique_ptr<Module> M;
638   if (NoUpgradeDebugInfo)
639     M = parseAssemblyFileWithIndexNoUpgradeDebugInfo(
640             InputFilename, Err, Context, nullptr, SetDataLayout)
641             .Mod;
642   else
643     M = parseIRFile(InputFilename, Err, Context, SetDataLayout);
644 
645   if (!M) {
646     Err.print(argv[0], errs());
647     return 1;
648   }
649 
650   // Strip debug info before running the verifier.
651   if (StripDebug)
652     StripDebugInfo(*M);
653 
654   // Erase module-level named metadata, if requested.
655   if (StripNamedMetadata) {
656     while (!M->named_metadata_empty()) {
657       NamedMDNode *NMD = &*M->named_metadata_begin();
658       M->eraseNamedMetadata(NMD);
659     }
660   }
661 
662   // If we are supposed to override the target triple or data layout, do so now.
663   if (!TargetTriple.empty())
664     M->setTargetTriple(Triple::normalize(TargetTriple));
665 
666   // Immediately run the verifier to catch any problems before starting up the
667   // pass pipelines.  Otherwise we can crash on broken code during
668   // doInitialization().
669   if (!NoVerify && verifyModule(*M, &errs())) {
670     errs() << argv[0] << ": " << InputFilename
671            << ": error: input module is broken!\n";
672     return 1;
673   }
674 
675   // Enable testing of whole program devirtualization on this module by invoking
676   // the facility for updating public visibility to linkage unit visibility when
677   // specified by an internal option. This is normally done during LTO which is
678   // not performed via opt.
679   updateVCallVisibilityInModule(*M,
680                                 /* WholeProgramVisibilityEnabledInLTO */ false,
681                                 /* DynamicExportSymbols */ {});
682 
683   // Figure out what stream we are supposed to write to...
684   std::unique_ptr<ToolOutputFile> Out;
685   std::unique_ptr<ToolOutputFile> ThinLinkOut;
686   if (NoOutput) {
687     if (!OutputFilename.empty())
688       errs() << "WARNING: The -o (output filename) option is ignored when\n"
689                 "the --disable-output option is used.\n";
690   } else {
691     // Default to standard output.
692     if (OutputFilename.empty())
693       OutputFilename = "-";
694 
695     std::error_code EC;
696     sys::fs::OpenFlags Flags =
697         OutputAssembly ? sys::fs::OF_TextWithCRLF : sys::fs::OF_None;
698     Out.reset(new ToolOutputFile(OutputFilename, EC, Flags));
699     if (EC) {
700       errs() << EC.message() << '\n';
701       return 1;
702     }
703 
704     if (!ThinLinkBitcodeFile.empty()) {
705       ThinLinkOut.reset(
706           new ToolOutputFile(ThinLinkBitcodeFile, EC, sys::fs::OF_None));
707       if (EC) {
708         errs() << EC.message() << '\n';
709         return 1;
710       }
711     }
712   }
713 
714   Triple ModuleTriple(M->getTargetTriple());
715   std::string CPUStr, FeaturesStr;
716   TargetMachine *Machine = nullptr;
717   const TargetOptions Options =
718       codegen::InitTargetOptionsFromCodeGenFlags(ModuleTriple);
719 
720   if (ModuleTriple.getArch()) {
721     CPUStr = codegen::getCPUStr();
722     FeaturesStr = codegen::getFeaturesStr();
723     Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options);
724   } else if (ModuleTriple.getArchName() != "unknown" &&
725              ModuleTriple.getArchName() != "") {
726     errs() << argv[0] << ": unrecognized architecture '"
727            << ModuleTriple.getArchName() << "' provided.\n";
728     return 1;
729   }
730 
731   std::unique_ptr<TargetMachine> TM(Machine);
732 
733   // Override function attributes based on CPUStr, FeaturesStr, and command line
734   // flags.
735   codegen::setFunctionAttributes(CPUStr, FeaturesStr, *M);
736 
737   // If the output is set to be emitted to standard out, and standard out is a
738   // console, print out a warning message and refuse to do it.  We don't
739   // impress anyone by spewing tons of binary goo to a terminal.
740   if (!Force && !NoOutput && !OutputAssembly)
741     if (CheckBitcodeOutputToConsole(Out->os()))
742       NoOutput = true;
743 
744   if (OutputThinLTOBC)
745     M->addModuleFlag(Module::Error, "EnableSplitLTOUnit", SplitLTOUnit);
746 
747   // Add an appropriate TargetLibraryInfo pass for the module's triple.
748   TargetLibraryInfoImpl TLII(ModuleTriple);
749 
750   // The -disable-simplify-libcalls flag actually disables all builtin optzns.
751   if (DisableSimplifyLibCalls)
752     TLII.disableAllFunctions();
753   else {
754     // Disable individual builtin functions in TargetLibraryInfo.
755     LibFunc F;
756     for (auto &FuncName : DisableBuiltins)
757       if (TLII.getLibFunc(FuncName, F))
758         TLII.setUnavailable(F);
759       else {
760         errs() << argv[0] << ": cannot disable nonexistent builtin function "
761                << FuncName << '\n';
762         return 1;
763       }
764   }
765 
766   if (UseNPM) {
767     if (legacy::debugPassSpecified()) {
768       errs()
769           << "-debug-pass does not work with the new PM, either use "
770              "-debug-pass-manager, or use the legacy PM (-enable-new-pm=0)\n";
771       return 1;
772     }
773     if (PassPipeline.getNumOccurrences() > 0 && PassList.size() > 0) {
774       errs()
775           << "Cannot specify passes via both -foo-pass and --passes=foo-pass\n";
776       return 1;
777     }
778     auto NumOLevel = OptLevelO0 + OptLevelO1 + OptLevelO2 + OptLevelO3 +
779                      OptLevelOs + OptLevelOz;
780     if (NumOLevel > 1) {
781       errs() << "Cannot specify multiple -O#\n";
782       return 1;
783     }
784     if (NumOLevel > 0 &&
785         (PassPipeline.getNumOccurrences() > 0 || PassList.size() > 0)) {
786       errs() << "Cannot specify -O# and --passes=/--foo-pass, use "
787                 "-passes='default<O#>,other-pass'\n";
788       return 1;
789     }
790     std::string Pipeline = PassPipeline;
791 
792     SmallVector<StringRef, 4> Passes;
793     if (OptLevelO0)
794       Pipeline = "default<O0>";
795     if (OptLevelO1)
796       Pipeline = "default<O1>";
797     if (OptLevelO2)
798       Pipeline = "default<O2>";
799     if (OptLevelO3)
800       Pipeline = "default<O3>";
801     if (OptLevelOs)
802       Pipeline = "default<Os>";
803     if (OptLevelOz)
804       Pipeline = "default<Oz>";
805     for (const auto &P : PassList)
806       Passes.push_back(P->getPassArgument());
807     OutputKind OK = OK_NoOutput;
808     if (!NoOutput)
809       OK = OutputAssembly
810                ? OK_OutputAssembly
811                : (OutputThinLTOBC ? OK_OutputThinLTOBitcode : OK_OutputBitcode);
812 
813     VerifierKind VK = VK_VerifyInAndOut;
814     if (NoVerify)
815       VK = VK_NoVerifier;
816     else if (VerifyEach)
817       VK = VK_VerifyEachPass;
818 
819     // The user has asked to use the new pass manager and provided a pipeline
820     // string. Hand off the rest of the functionality to the new code for that
821     // layer.
822     return runPassPipeline(argv[0], *M, TM.get(), &TLII, Out.get(),
823                            ThinLinkOut.get(), RemarksFile.get(), Pipeline,
824                            Passes, PluginList, OK, VK, PreserveAssemblyUseListOrder,
825                            PreserveBitcodeUseListOrder, EmitSummaryIndex,
826                            EmitModuleHash, EnableDebugify)
827                ? 0
828                : 1;
829   }
830 
831   // Create a PassManager to hold and optimize the collection of passes we are
832   // about to build. If the -debugify-each option is set, wrap each pass with
833   // the (-check)-debugify passes.
834   DebugifyCustomPassManager Passes;
835   DebugifyStatsMap DIStatsMap;
836   DebugInfoPerPass DebugInfoBeforePass;
837   if (DebugifyEach) {
838     Passes.setDebugifyMode(DebugifyMode::SyntheticDebugInfo);
839     Passes.setDIStatsMap(DIStatsMap);
840   } else if (VerifyEachDebugInfoPreserve) {
841     Passes.setDebugifyMode(DebugifyMode::OriginalDebugInfo);
842     Passes.setDebugInfoBeforePass(DebugInfoBeforePass);
843     if (!VerifyDIPreserveExport.empty())
844       Passes.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport);
845   }
846 
847   bool AddOneTimeDebugifyPasses =
848       (EnableDebugify && !DebugifyEach) ||
849       (VerifyDebugInfoPreserve && !VerifyEachDebugInfoPreserve);
850 
851   Passes.add(new TargetLibraryInfoWrapperPass(TLII));
852 
853   // Add internal analysis passes from the target machine.
854   Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis()
855                                                      : TargetIRAnalysis()));
856 
857   if (AddOneTimeDebugifyPasses) {
858     if (EnableDebugify) {
859       Passes.setDIStatsMap(DIStatsMap);
860       Passes.add(createDebugifyModulePass());
861     } else if (VerifyDebugInfoPreserve) {
862       Passes.setDebugInfoBeforePass(DebugInfoBeforePass);
863       Passes.add(createDebugifyModulePass(
864           DebugifyMode::OriginalDebugInfo, "",
865           &(Passes.getDebugInfoPerPass())));
866     }
867   }
868 
869   std::unique_ptr<legacy::FunctionPassManager> FPasses;
870   if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz ||
871       OptLevelO3) {
872     FPasses.reset(new legacy::FunctionPassManager(M.get()));
873     FPasses->add(createTargetTransformInfoWrapperPass(
874         TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis()));
875   }
876 
877   if (PrintBreakpoints) {
878     // Default to standard output.
879     if (!Out) {
880       if (OutputFilename.empty())
881         OutputFilename = "-";
882 
883       std::error_code EC;
884       Out = std::make_unique<ToolOutputFile>(OutputFilename, EC,
885                                               sys::fs::OF_None);
886       if (EC) {
887         errs() << EC.message() << '\n';
888         return 1;
889       }
890     }
891     Passes.add(createBreakpointPrinter(Out->os()));
892     NoOutput = true;
893   }
894 
895   if (TM) {
896     // FIXME: We should dyn_cast this when supported.
897     auto &LTM = static_cast<LLVMTargetMachine &>(*TM);
898     Pass *TPC = LTM.createPassConfig(Passes);
899     Passes.add(TPC);
900   }
901 
902   // Create a new optimization pass for each one specified on the command line
903   for (unsigned i = 0; i < PassList.size(); ++i) {
904     if (OptLevelO0 && OptLevelO0.getPosition() < PassList.getPosition(i)) {
905       AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
906       OptLevelO0 = false;
907     }
908 
909     if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
910       AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
911       OptLevelO1 = false;
912     }
913 
914     if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
915       AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
916       OptLevelO2 = false;
917     }
918 
919     if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
920       AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
921       OptLevelOs = false;
922     }
923 
924     if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
925       AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
926       OptLevelOz = false;
927     }
928 
929     if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
930       AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
931       OptLevelO3 = false;
932     }
933 
934     const PassInfo *PassInf = PassList[i];
935     Pass *P = nullptr;
936     if (PassInf->getNormalCtor())
937       P = PassInf->getNormalCtor()();
938     else
939       errs() << argv[0] << ": cannot create pass: "
940              << PassInf->getPassName() << "\n";
941     if (P)
942       addPass(Passes, P);
943   }
944 
945   if (OptLevelO0)
946     AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
947 
948   if (OptLevelO1)
949     AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
950 
951   if (OptLevelO2)
952     AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
953 
954   if (OptLevelOs)
955     AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
956 
957   if (OptLevelOz)
958     AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
959 
960   if (OptLevelO3)
961     AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
962 
963   if (FPasses) {
964     FPasses->doInitialization();
965     for (Function &F : *M)
966       FPasses->run(F);
967     FPasses->doFinalization();
968   }
969 
970   // Check that the module is well formed on completion of optimization
971   if (!NoVerify && !VerifyEach)
972     Passes.add(createVerifierPass());
973 
974   if (AddOneTimeDebugifyPasses) {
975     if (EnableDebugify)
976       Passes.add(createCheckDebugifyModulePass(false));
977     else if (VerifyDebugInfoPreserve) {
978       if (!VerifyDIPreserveExport.empty())
979         Passes.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport);
980       Passes.add(createCheckDebugifyModulePass(
981           false, "", nullptr, DebugifyMode::OriginalDebugInfo,
982           &(Passes.getDebugInfoPerPass()), VerifyDIPreserveExport));
983     }
984   }
985 
986   // In run twice mode, we want to make sure the output is bit-by-bit
987   // equivalent if we run the pass manager again, so setup two buffers and
988   // a stream to write to them. Note that llc does something similar and it
989   // may be worth to abstract this out in the future.
990   SmallVector<char, 0> Buffer;
991   SmallVector<char, 0> FirstRunBuffer;
992   std::unique_ptr<raw_svector_ostream> BOS;
993   raw_ostream *OS = nullptr;
994 
995   const bool ShouldEmitOutput = !NoOutput;
996 
997   // Write bitcode or assembly to the output as the last step...
998   if (ShouldEmitOutput || RunTwice) {
999     assert(Out);
1000     OS = &Out->os();
1001     if (RunTwice) {
1002       BOS = std::make_unique<raw_svector_ostream>(Buffer);
1003       OS = BOS.get();
1004     }
1005     if (OutputAssembly) {
1006       if (EmitSummaryIndex)
1007         report_fatal_error("Text output is incompatible with -module-summary");
1008       if (EmitModuleHash)
1009         report_fatal_error("Text output is incompatible with -module-hash");
1010       Passes.add(createPrintModulePass(*OS, "", PreserveAssemblyUseListOrder));
1011     } else if (OutputThinLTOBC)
1012       Passes.add(createWriteThinLTOBitcodePass(
1013           *OS, ThinLinkOut ? &ThinLinkOut->os() : nullptr));
1014     else
1015       Passes.add(createBitcodeWriterPass(*OS, PreserveBitcodeUseListOrder,
1016                                          EmitSummaryIndex, EmitModuleHash));
1017   }
1018 
1019   // Before executing passes, print the final values of the LLVM options.
1020   cl::PrintOptionValues();
1021 
1022   if (!RunTwice) {
1023     // Now that we have all of the passes ready, run them.
1024     Passes.run(*M);
1025   } else {
1026     // If requested, run all passes twice with the same pass manager to catch
1027     // bugs caused by persistent state in the passes.
1028     std::unique_ptr<Module> M2(CloneModule(*M));
1029     // Run all passes on the original module first, so the second run processes
1030     // the clone to catch CloneModule bugs.
1031     Passes.run(*M);
1032     FirstRunBuffer = Buffer;
1033     Buffer.clear();
1034 
1035     Passes.run(*M2);
1036 
1037     // Compare the two outputs and make sure they're the same
1038     assert(Out);
1039     if (Buffer.size() != FirstRunBuffer.size() ||
1040         (memcmp(Buffer.data(), FirstRunBuffer.data(), Buffer.size()) != 0)) {
1041       errs()
1042           << "Running the pass manager twice changed the output.\n"
1043              "Writing the result of the second run to the specified output.\n"
1044              "To generate the one-run comparison binary, just run without\n"
1045              "the compile-twice option\n";
1046       if (ShouldEmitOutput) {
1047         Out->os() << BOS->str();
1048         Out->keep();
1049       }
1050       if (RemarksFile)
1051         RemarksFile->keep();
1052       return 1;
1053     }
1054     if (ShouldEmitOutput)
1055       Out->os() << BOS->str();
1056   }
1057 
1058   if (DebugifyEach && !DebugifyExport.empty())
1059     exportDebugifyStats(DebugifyExport, Passes.getDebugifyStatsMap());
1060 
1061   // Declare success.
1062   if (!NoOutput || PrintBreakpoints)
1063     Out->keep();
1064 
1065   if (RemarksFile)
1066     RemarksFile->keep();
1067 
1068   if (ThinLinkOut)
1069     ThinLinkOut->keep();
1070 
1071   return 0;
1072 }
1073