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