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