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