xref: /freebsd/contrib/llvm-project/llvm/tools/opt/optdriver.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- optdriver.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. Common driver library for re-use
11 // by potential downstream opt-variants.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "NewPMDriver.h"
16 #include "llvm/Analysis/CallGraph.h"
17 #include "llvm/Analysis/CallGraphSCCPass.h"
18 #include "llvm/Analysis/LoopPass.h"
19 #include "llvm/Analysis/RegionPass.h"
20 #include "llvm/Analysis/TargetLibraryInfo.h"
21 #include "llvm/Analysis/TargetTransformInfo.h"
22 #include "llvm/AsmParser/Parser.h"
23 #include "llvm/CodeGen/CommandFlags.h"
24 #include "llvm/CodeGen/TargetPassConfig.h"
25 #include "llvm/Config/llvm-config.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/DebugInfo.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/LLVMRemarkStreamer.h"
30 #include "llvm/IR/LegacyPassManager.h"
31 #include "llvm/IR/LegacyPassNameParser.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/IR/ModuleSummaryIndex.h"
34 #include "llvm/IR/Verifier.h"
35 #include "llvm/IRReader/IRReader.h"
36 #include "llvm/InitializePasses.h"
37 #include "llvm/LinkAllIR.h"
38 #include "llvm/LinkAllPasses.h"
39 #include "llvm/MC/TargetRegistry.h"
40 #include "llvm/Passes/PassPlugin.h"
41 #include "llvm/Remarks/HotnessThresholdParser.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Support/ErrorHandling.h"
44 #include "llvm/Support/FileSystem.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/TargetSelect.h"
50 #include "llvm/Support/TimeProfiler.h"
51 #include "llvm/Support/ToolOutputFile.h"
52 #include "llvm/Support/YAMLTraits.h"
53 #include "llvm/Target/TargetMachine.h"
54 #include "llvm/TargetParser/Host.h"
55 #include "llvm/TargetParser/SubtargetFeature.h"
56 #include "llvm/TargetParser/Triple.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 #include <optional>
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> EnableLegacyPassManager(
74     "bugpoint-enable-legacy-pm",
75     cl::desc(
76         "Enable the legacy pass manager. This is strictly for bugpoint "
77         "due to it not working with the new PM, please do not use otherwise."),
78     cl::init(false));
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 (comma separated) description of the pass pipeline e.g.,"
88         "-passes=\"foo,bar\", to have analysis passes available before a pass, "
89         "add \"require<foo-analysis>\". See "
90         "https://llvm.org/docs/NewPassManager.html#invoking-opt "
91         "for more details on the pass pipeline syntax. "));
92 
93 static cl::alias PassPipeline2("p", cl::aliasopt(PassPipeline),
94                                cl::desc("Alias for -passes"));
95 
96 static cl::opt<bool> PrintPasses("print-passes",
97                                  cl::desc("Print available passes that can be "
98                                           "specified in -passes=foo and exit"));
99 
100 static cl::opt<std::string> InputFilename(cl::Positional,
101                                           cl::desc("<input bitcode file>"),
102                                           cl::init("-"),
103                                           cl::value_desc("filename"));
104 
105 static cl::opt<std::string> OutputFilename("o",
106                                            cl::desc("Override output filename"),
107                                            cl::value_desc("filename"));
108 
109 static cl::opt<bool> Force("f", cl::desc("Enable binary output on terminals"));
110 
111 static cl::opt<bool> NoOutput("disable-output",
112                               cl::desc("Do not write result bitcode file"),
113                               cl::Hidden);
114 
115 static cl::opt<bool> OutputAssembly("S",
116                                     cl::desc("Write output as LLVM assembly"));
117 
118 static cl::opt<bool>
119     OutputThinLTOBC("thinlto-bc",
120                     cl::desc("Write output as ThinLTO-ready bitcode"));
121 
122 static cl::opt<bool>
123     SplitLTOUnit("thinlto-split-lto-unit",
124                  cl::desc("Enable splitting of a ThinLTO LTOUnit"));
125 
126 static cl::opt<bool>
127     UnifiedLTO("unified-lto",
128                cl::desc("Use unified LTO piplines. Ignored unless -thinlto-bc "
129                         "is also specified."),
130                cl::Hidden, cl::init(false));
131 
132 static cl::opt<std::string> ThinLinkBitcodeFile(
133     "thin-link-bitcode-file", cl::value_desc("filename"),
134     cl::desc(
135         "A file in which to write minimized bitcode for the thin link only"));
136 
137 static cl::opt<bool> NoVerify("disable-verify",
138                               cl::desc("Do not run the verifier"), cl::Hidden);
139 
140 static cl::opt<bool> NoUpgradeDebugInfo("disable-upgrade-debug-info",
141                                         cl::desc("Generate invalid output"),
142                                         cl::ReallyHidden);
143 
144 static cl::opt<bool> VerifyEach("verify-each",
145                                 cl::desc("Verify after each transform"));
146 
147 static cl::opt<bool>
148     DisableDITypeMap("disable-debug-info-type-map",
149                      cl::desc("Don't use a uniquing type map for debug info"));
150 
151 static cl::opt<bool>
152     StripDebug("strip-debug",
153                cl::desc("Strip debugger symbol info from translation unit"));
154 
155 static cl::opt<bool>
156     StripNamedMetadata("strip-named-metadata",
157                        cl::desc("Strip module-level named metadata"));
158 
159 static cl::opt<bool>
160     OptLevelO0("O0", cl::desc("Optimization level 0. Similar to clang -O0. "
161                               "Same as -passes=\"default<O0>\""));
162 
163 static cl::opt<bool>
164     OptLevelO1("O1", cl::desc("Optimization level 1. Similar to clang -O1. "
165                               "Same as -passes=\"default<O1>\""));
166 
167 static cl::opt<bool>
168     OptLevelO2("O2", cl::desc("Optimization level 2. Similar to clang -O2. "
169                               "Same as -passes=\"default<O2>\""));
170 
171 static cl::opt<bool>
172     OptLevelOs("Os", cl::desc("Like -O2 but size-conscious. Similar to clang "
173                               "-Os. Same as -passes=\"default<Os>\""));
174 
175 static cl::opt<bool> OptLevelOz(
176     "Oz",
177     cl::desc("Like -O2 but optimize for code size above all else. Similar to "
178              "clang -Oz. Same as -passes=\"default<Oz>\""));
179 
180 static cl::opt<bool>
181     OptLevelO3("O3", cl::desc("Optimization level 3. Similar to clang -O3. "
182                               "Same as -passes=\"default<O3>\""));
183 
184 static cl::opt<unsigned> CodeGenOptLevelCL(
185     "codegen-opt-level",
186     cl::desc("Override optimization level for codegen hooks, legacy PM only"));
187 
188 static cl::opt<std::string>
189     TargetTriple("mtriple", cl::desc("Override target triple for module"));
190 
191 static cl::opt<bool> EmitSummaryIndex("module-summary",
192                                       cl::desc("Emit module summary index"),
193                                       cl::init(false));
194 
195 static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
196                                     cl::init(false));
197 
198 static cl::opt<bool>
199     DisableSimplifyLibCalls("disable-simplify-libcalls",
200                             cl::desc("Disable simplify-libcalls"));
201 
202 static cl::list<std::string> DisableBuiltins(
203     "disable-builtin",
204     cl::desc("Disable specific target library builtin function"));
205 
206 static cl::list<std::string> EnableBuiltins(
207     "enable-builtin",
208     cl::desc("Enable specific target library builtin functions"));
209 
210 static cl::opt<bool> EnableDebugify(
211     "enable-debugify",
212     cl::desc(
213         "Start the pipeline with debugify and end it with check-debugify"));
214 
215 static cl::opt<bool> VerifyDebugInfoPreserve(
216     "verify-debuginfo-preserve",
217     cl::desc("Start the pipeline with collecting and end it with checking of "
218              "debug info preservation."));
219 
220 static cl::opt<std::string> ClDataLayout("data-layout",
221                                          cl::desc("data layout string to use"),
222                                          cl::value_desc("layout-string"),
223                                          cl::init(""));
224 
225 static cl::opt<bool> PreserveBitcodeUseListOrder(
226     "preserve-bc-uselistorder",
227     cl::desc("Preserve use-list order when writing LLVM bitcode."),
228     cl::init(true), cl::Hidden);
229 
230 static cl::opt<bool> PreserveAssemblyUseListOrder(
231     "preserve-ll-uselistorder",
232     cl::desc("Preserve use-list order when writing LLVM assembly."),
233     cl::init(false), cl::Hidden);
234 
235 static cl::opt<bool> RunTwice("run-twice",
236                               cl::desc("Run all passes twice, re-using the "
237                                        "same pass manager (legacy PM only)."),
238                               cl::init(false), cl::Hidden);
239 
240 static cl::opt<bool> DiscardValueNames(
241     "discard-value-names",
242     cl::desc("Discard names from Value (other than GlobalValue)."),
243     cl::init(false), cl::Hidden);
244 
245 static cl::opt<bool> TimeTrace("time-trace", cl::desc("Record time trace"));
246 
247 static cl::opt<unsigned> TimeTraceGranularity(
248     "time-trace-granularity",
249     cl::desc(
250         "Minimum time granularity (in microseconds) traced by time profiler"),
251     cl::init(500), cl::Hidden);
252 
253 static cl::opt<std::string>
254     TimeTraceFile("time-trace-file",
255                   cl::desc("Specify time trace file destination"),
256                   cl::value_desc("filename"));
257 
258 static cl::opt<bool> RemarksWithHotness(
259     "pass-remarks-with-hotness",
260     cl::desc("With PGO, include profile count in optimization remarks"),
261     cl::Hidden);
262 
263 static cl::opt<std::optional<uint64_t>, false, remarks::HotnessThresholdParser>
264     RemarksHotnessThreshold(
265         "pass-remarks-hotness-threshold",
266         cl::desc("Minimum profile count required for "
267                  "an optimization remark to be output. "
268                  "Use 'auto' to apply the threshold from profile summary"),
269         cl::value_desc("N or 'auto'"), cl::init(0), cl::Hidden);
270 
271 static cl::opt<std::string>
272     RemarksFilename("pass-remarks-output",
273                     cl::desc("Output filename for pass remarks"),
274                     cl::value_desc("filename"));
275 
276 static cl::opt<std::string>
277     RemarksPasses("pass-remarks-filter",
278                   cl::desc("Only record optimization remarks from passes whose "
279                            "names match the given regular expression"),
280                   cl::value_desc("regex"));
281 
282 static cl::opt<std::string> RemarksFormat(
283     "pass-remarks-format",
284     cl::desc("The format used for serializing remarks (default: YAML)"),
285     cl::value_desc("format"), cl::init("yaml"));
286 
287 static cl::list<std::string>
288     PassPlugins("load-pass-plugin",
289                 cl::desc("Load passes from plugin library"));
290 
291 //===----------------------------------------------------------------------===//
292 // CodeGen-related helper functions.
293 //
294 
GetCodeGenOptLevel()295 static CodeGenOptLevel GetCodeGenOptLevel() {
296   return static_cast<CodeGenOptLevel>(unsigned(CodeGenOptLevelCL));
297 }
298 
299 struct TimeTracerRAII {
TimeTracerRAIITimeTracerRAII300   TimeTracerRAII(StringRef ProgramName) {
301     if (TimeTrace)
302       timeTraceProfilerInitialize(TimeTraceGranularity, ProgramName);
303   }
~TimeTracerRAIITimeTracerRAII304   ~TimeTracerRAII() {
305     if (TimeTrace) {
306       if (auto E = timeTraceProfilerWrite(TimeTraceFile, OutputFilename)) {
307         handleAllErrors(std::move(E), [&](const StringError &SE) {
308           errs() << SE.getMessage() << "\n";
309         });
310         return;
311       }
312       timeTraceProfilerCleanup();
313     }
314   }
315 };
316 
317 // For use in NPM transition. Currently this contains most codegen-specific
318 // passes. Remove passes from here when porting to the NPM.
319 // TODO: use a codegen version of PassRegistry.def/PassBuilder::is*Pass() once
320 // it exists.
shouldPinPassToLegacyPM(StringRef Pass)321 static bool shouldPinPassToLegacyPM(StringRef Pass) {
322   static constexpr StringLiteral PassNameExactToIgnore[] = {
323       "nvvm-reflect",
324       "nvvm-intr-range",
325       "amdgpu-simplifylib",
326       "amdgpu-image-intrinsic-opt",
327       "amdgpu-usenative",
328       "amdgpu-promote-alloca",
329       "amdgpu-promote-alloca-to-vector",
330       "amdgpu-lower-kernel-attributes",
331       "amdgpu-propagate-attributes-early",
332       "amdgpu-propagate-attributes-late",
333       "amdgpu-unify-metadata",
334       "amdgpu-printf-runtime-binding",
335       "amdgpu-always-inline"};
336   if (llvm::is_contained(PassNameExactToIgnore, Pass))
337     return false;
338 
339   static constexpr StringLiteral PassNamePrefix[] = {
340       "x86-",    "xcore-", "wasm-",  "systemz-", "ppc-",    "nvvm-",
341       "nvptx-",  "mips-",  "lanai-", "hexagon-", "bpf-",    "avr-",
342       "thumb2-", "arm-",   "si-",    "gcn-",     "amdgpu-", "aarch64-",
343       "amdgcn-", "polly-", "riscv-", "dxil-"};
344   static constexpr StringLiteral PassNameContain[] = {"-eh-prepare"};
345   static constexpr StringLiteral PassNameExact[] = {
346       "safe-stack",
347       "cost-model",
348       "codegenprepare",
349       "interleaved-load-combine",
350       "unreachableblockelim",
351       "verify-safepoint-ir",
352       "atomic-expand",
353       "expandvp",
354       "mve-tail-predication",
355       "interleaved-access",
356       "global-merge",
357       "pre-isel-intrinsic-lowering",
358       "expand-reductions",
359       "indirectbr-expand",
360       "generic-to-nvvm",
361       "expand-memcmp",
362       "loop-reduce",
363       "lower-amx-type",
364       "lower-amx-intrinsics",
365       "polyhedral-info",
366       "print-polyhedral-info",
367       "replace-with-veclib",
368       "jmc-instrumenter",
369       "dot-regions",
370       "dot-regions-only",
371       "view-regions",
372       "view-regions-only",
373       "select-optimize",
374       "expand-large-div-rem",
375       "structurizecfg",
376       "fix-irreducible",
377       "expand-fp",
378       "callbrprepare",
379       "scalarizer",
380   };
381   for (const auto &P : PassNamePrefix)
382     if (Pass.starts_with(P))
383       return true;
384   for (const auto &P : PassNameContain)
385     if (Pass.contains(P))
386       return true;
387   return llvm::is_contained(PassNameExact, Pass);
388 }
389 
390 // For use in NPM transition.
shouldForceLegacyPM()391 static bool shouldForceLegacyPM() {
392   for (const auto &P : PassList) {
393     StringRef Arg = P->getPassArgument();
394     if (shouldPinPassToLegacyPM(Arg))
395       return true;
396   }
397   return false;
398 }
399 
400 //===----------------------------------------------------------------------===//
401 // main for opt
402 //
optMain(int argc,char ** argv,ArrayRef<std::function<void (llvm::PassBuilder &)>> PassBuilderCallbacks)403 extern "C" int optMain(
404     int argc, char **argv,
405     ArrayRef<std::function<void(llvm::PassBuilder &)>> PassBuilderCallbacks) {
406   InitLLVM X(argc, argv);
407 
408   // Enable debug stream buffering.
409   EnableDebugBuffering = true;
410 
411   InitializeAllTargets();
412   InitializeAllTargetMCs();
413   InitializeAllAsmPrinters();
414   InitializeAllAsmParsers();
415 
416   // Initialize passes
417   PassRegistry &Registry = *PassRegistry::getPassRegistry();
418   initializeCore(Registry);
419   initializeScalarOpts(Registry);
420   initializeVectorization(Registry);
421   initializeIPO(Registry);
422   initializeAnalysis(Registry);
423   initializeTransformUtils(Registry);
424   initializeInstCombine(Registry);
425   initializeTarget(Registry);
426   // For codegen passes, only passes that do IR to IR transformation are
427   // supported.
428   initializeExpandLargeDivRemLegacyPassPass(Registry);
429   initializeExpandFpLegacyPassPass(Registry);
430   initializeExpandMemCmpLegacyPassPass(Registry);
431   initializeScalarizeMaskedMemIntrinLegacyPassPass(Registry);
432   initializeSelectOptimizePass(Registry);
433   initializeCallBrPreparePass(Registry);
434   initializeCodeGenPrepareLegacyPassPass(Registry);
435   initializeAtomicExpandLegacyPass(Registry);
436   initializeWinEHPreparePass(Registry);
437   initializeDwarfEHPrepareLegacyPassPass(Registry);
438   initializeSafeStackLegacyPassPass(Registry);
439   initializeSjLjEHPreparePass(Registry);
440   initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
441   initializeGlobalMergePass(Registry);
442   initializeIndirectBrExpandLegacyPassPass(Registry);
443   initializeInterleavedLoadCombinePass(Registry);
444   initializeInterleavedAccessPass(Registry);
445   initializePostInlineEntryExitInstrumenterPass(Registry);
446   initializeUnreachableBlockElimLegacyPassPass(Registry);
447   initializeExpandReductionsPass(Registry);
448   initializeWasmEHPreparePass(Registry);
449   initializeWriteBitcodePassPass(Registry);
450   initializeReplaceWithVeclibLegacyPass(Registry);
451   initializeJMCInstrumenterPass(Registry);
452 
453   SmallVector<PassPlugin, 1> PluginList;
454   PassPlugins.setCallback([&](const std::string &PluginPath) {
455     auto Plugin = PassPlugin::Load(PluginPath);
456     if (!Plugin)
457       reportFatalUsageError(Plugin.takeError());
458     PluginList.emplace_back(Plugin.get());
459   });
460 
461   // Register the Target and CPU printer for --version.
462   cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU);
463 
464   cl::ParseCommandLineOptions(
465       argc, argv, "llvm .bc -> .bc modular optimizer and analysis printer\n");
466 
467   LLVMContext Context;
468 
469   // TODO: remove shouldForceLegacyPM().
470   const bool UseNPM = (!EnableLegacyPassManager && !shouldForceLegacyPM()) ||
471                       PassPipeline.getNumOccurrences() > 0;
472 
473   if (UseNPM && !PassList.empty()) {
474     errs() << "The `opt -passname` syntax for the new pass manager is "
475               "not supported, please use `opt -passes=<pipeline>` (or the `-p` "
476               "alias for a more concise version).\n";
477     errs() << "See https://llvm.org/docs/NewPassManager.html#invoking-opt "
478               "for more details on the pass pipeline syntax.\n\n";
479     return 1;
480   }
481 
482   if (!UseNPM && PluginList.size()) {
483     errs() << argv[0] << ": " << PassPlugins.ArgStr
484            << " specified with legacy PM.\n";
485     return 1;
486   }
487 
488   // FIXME: once the legacy PM code is deleted, move runPassPipeline() here and
489   // construct the PassBuilder before parsing IR so we can reuse the same
490   // PassBuilder for print passes.
491   if (PrintPasses) {
492     printPasses(outs());
493     return 0;
494   }
495 
496   TimeTracerRAII TimeTracer(argv[0]);
497 
498   SMDiagnostic Err;
499 
500   Context.setDiscardValueNames(DiscardValueNames);
501   if (!DisableDITypeMap)
502     Context.enableDebugTypeODRUniquing();
503 
504   Expected<std::unique_ptr<ToolOutputFile>> RemarksFileOrErr =
505       setupLLVMOptimizationRemarks(Context, RemarksFilename, RemarksPasses,
506                                    RemarksFormat, RemarksWithHotness,
507                                    RemarksHotnessThreshold);
508   if (Error E = RemarksFileOrErr.takeError()) {
509     errs() << toString(std::move(E)) << '\n';
510     return 1;
511   }
512   std::unique_ptr<ToolOutputFile> RemarksFile = std::move(*RemarksFileOrErr);
513 
514   // Load the input module...
515   auto SetDataLayout = [&](StringRef IRTriple,
516                            StringRef IRLayout) -> std::optional<std::string> {
517     // Data layout specified on the command line has the highest priority.
518     if (!ClDataLayout.empty())
519       return ClDataLayout;
520     // If an explicit data layout is already defined in the IR, don't infer.
521     if (!IRLayout.empty())
522       return std::nullopt;
523 
524     // If an explicit triple was specified (either in the IR or on the
525     // command line), use that to infer the default data layout. However, the
526     // command line target triple should override the IR file target triple.
527     std::string TripleStr =
528         TargetTriple.empty() ? IRTriple.str() : Triple::normalize(TargetTriple);
529     // If the triple string is still empty, we don't fall back to
530     // sys::getDefaultTargetTriple() since we do not want to have differing
531     // behaviour dependent on the configured default triple. Therefore, if the
532     // user did not pass -mtriple or define an explicit triple/datalayout in
533     // the IR, we should default to an empty (default) DataLayout.
534     if (TripleStr.empty())
535       return std::nullopt;
536     // Otherwise we infer the DataLayout from the target machine.
537     Expected<std::unique_ptr<TargetMachine>> ExpectedTM =
538         codegen::createTargetMachineForTriple(TripleStr, GetCodeGenOptLevel());
539     if (!ExpectedTM) {
540       errs() << argv[0] << ": warning: failed to infer data layout: "
541              << toString(ExpectedTM.takeError()) << "\n";
542       return std::nullopt;
543     }
544     return (*ExpectedTM)->createDataLayout().getStringRepresentation();
545   };
546   std::unique_ptr<Module> M;
547   if (NoUpgradeDebugInfo)
548     M = parseAssemblyFileWithIndexNoUpgradeDebugInfo(
549             InputFilename, Err, Context, nullptr, SetDataLayout)
550             .Mod;
551   else
552     M = parseIRFile(InputFilename, Err, Context,
553                     ParserCallbacks(SetDataLayout));
554 
555   if (!M) {
556     Err.print(argv[0], errs());
557     return 1;
558   }
559 
560   // Strip debug info before running the verifier.
561   if (StripDebug)
562     StripDebugInfo(*M);
563 
564   // Erase module-level named metadata, if requested.
565   if (StripNamedMetadata) {
566     while (!M->named_metadata_empty()) {
567       NamedMDNode *NMD = &*M->named_metadata_begin();
568       M->eraseNamedMetadata(NMD);
569     }
570   }
571 
572   // If we are supposed to override the target triple, do so now.
573   if (!TargetTriple.empty())
574     M->setTargetTriple(Triple(Triple::normalize(TargetTriple)));
575 
576   // Immediately run the verifier to catch any problems before starting up the
577   // pass pipelines.  Otherwise we can crash on broken code during
578   // doInitialization().
579   if (!NoVerify && verifyModule(*M, &errs())) {
580     errs() << argv[0] << ": " << InputFilename
581            << ": error: input module is broken!\n";
582     return 1;
583   }
584 
585   // Enable testing of whole program devirtualization on this module by invoking
586   // the facility for updating public visibility to linkage unit visibility when
587   // specified by an internal option. This is normally done during LTO which is
588   // not performed via opt.
589   updateVCallVisibilityInModule(
590       *M,
591       /*WholeProgramVisibilityEnabledInLTO=*/false,
592       // FIXME: These need linker information via a
593       // TBD new interface.
594       /*DynamicExportSymbols=*/{},
595       /*ValidateAllVtablesHaveTypeInfos=*/false,
596       /*IsVisibleToRegularObj=*/[](StringRef) { return true; });
597 
598   // Figure out what stream we are supposed to write to...
599   std::unique_ptr<ToolOutputFile> Out;
600   std::unique_ptr<ToolOutputFile> ThinLinkOut;
601   if (NoOutput) {
602     if (!OutputFilename.empty())
603       errs() << "WARNING: The -o (output filename) option is ignored when\n"
604                 "the --disable-output option is used.\n";
605   } else {
606     // Default to standard output.
607     if (OutputFilename.empty())
608       OutputFilename = "-";
609 
610     std::error_code EC;
611     sys::fs::OpenFlags Flags =
612         OutputAssembly ? sys::fs::OF_TextWithCRLF : sys::fs::OF_None;
613     Out.reset(new ToolOutputFile(OutputFilename, EC, Flags));
614     if (EC) {
615       errs() << EC.message() << '\n';
616       return 1;
617     }
618 
619     if (!ThinLinkBitcodeFile.empty()) {
620       ThinLinkOut.reset(
621           new ToolOutputFile(ThinLinkBitcodeFile, EC, sys::fs::OF_None));
622       if (EC) {
623         errs() << EC.message() << '\n';
624         return 1;
625       }
626     }
627   }
628 
629   Triple ModuleTriple(M->getTargetTriple());
630   std::string CPUStr, FeaturesStr;
631   std::unique_ptr<TargetMachine> TM;
632   if (ModuleTriple.getArch()) {
633     CPUStr = codegen::getCPUStr();
634     FeaturesStr = codegen::getFeaturesStr();
635     Expected<std::unique_ptr<TargetMachine>> ExpectedTM =
636         codegen::createTargetMachineForTriple(ModuleTriple.str(),
637                                               GetCodeGenOptLevel());
638     if (auto E = ExpectedTM.takeError()) {
639       errs() << argv[0] << ": WARNING: failed to create target machine for '"
640              << ModuleTriple.str() << "': " << toString(std::move(E)) << "\n";
641     } else {
642       TM = std::move(*ExpectedTM);
643     }
644   } else if (ModuleTriple.getArchName() != "unknown" &&
645              ModuleTriple.getArchName() != "") {
646     errs() << argv[0] << ": unrecognized architecture '"
647            << ModuleTriple.getArchName() << "' provided.\n";
648     return 1;
649   }
650 
651   // Override function attributes based on CPUStr, FeaturesStr, and command line
652   // flags.
653   codegen::setFunctionAttributes(CPUStr, FeaturesStr, *M);
654 
655   // If the output is set to be emitted to standard out, and standard out is a
656   // console, print out a warning message and refuse to do it.  We don't
657   // impress anyone by spewing tons of binary goo to a terminal.
658   if (!Force && !NoOutput && !OutputAssembly)
659     if (CheckBitcodeOutputToConsole(Out->os()))
660       NoOutput = true;
661 
662   if (OutputThinLTOBC) {
663     M->addModuleFlag(Module::Error, "EnableSplitLTOUnit", SplitLTOUnit);
664     if (UnifiedLTO)
665       M->addModuleFlag(Module::Error, "UnifiedLTO", 1);
666   }
667 
668   // Add an appropriate TargetLibraryInfo pass for the module's triple.
669   TargetLibraryInfoImpl TLII(ModuleTriple);
670 
671   // The -disable-simplify-libcalls flag actually disables all builtin optzns.
672   if (DisableSimplifyLibCalls)
673     TLII.disableAllFunctions();
674   else {
675     // Disable individual builtin functions in TargetLibraryInfo.
676     LibFunc F;
677     for (auto &FuncName : DisableBuiltins) {
678       if (TLII.getLibFunc(FuncName, F))
679         TLII.setUnavailable(F);
680       else {
681         errs() << argv[0] << ": cannot disable nonexistent builtin function "
682                << FuncName << '\n';
683         return 1;
684       }
685     }
686 
687     for (auto &FuncName : EnableBuiltins) {
688       if (TLII.getLibFunc(FuncName, F))
689         TLII.setAvailable(F);
690       else {
691         errs() << argv[0] << ": cannot enable nonexistent builtin function "
692                << FuncName << '\n';
693         return 1;
694       }
695     }
696   }
697 
698   if (UseNPM) {
699     if (legacy::debugPassSpecified()) {
700       errs() << "-debug-pass does not work with the new PM, either use "
701                 "-debug-pass-manager, or use the legacy PM\n";
702       return 1;
703     }
704     auto NumOLevel = OptLevelO0 + OptLevelO1 + OptLevelO2 + OptLevelO3 +
705                      OptLevelOs + OptLevelOz;
706     if (NumOLevel > 1) {
707       errs() << "Cannot specify multiple -O#\n";
708       return 1;
709     }
710     if (NumOLevel > 0 && (PassPipeline.getNumOccurrences() > 0)) {
711       errs() << "Cannot specify -O# and --passes=/--foo-pass, use "
712                 "-passes='default<O#>,other-pass'\n";
713       return 1;
714     }
715     std::string Pipeline = PassPipeline;
716 
717     if (OptLevelO0)
718       Pipeline = "default<O0>";
719     if (OptLevelO1)
720       Pipeline = "default<O1>";
721     if (OptLevelO2)
722       Pipeline = "default<O2>";
723     if (OptLevelO3)
724       Pipeline = "default<O3>";
725     if (OptLevelOs)
726       Pipeline = "default<Os>";
727     if (OptLevelOz)
728       Pipeline = "default<Oz>";
729     OutputKind OK = OK_NoOutput;
730     if (!NoOutput)
731       OK = OutputAssembly
732                ? OK_OutputAssembly
733                : (OutputThinLTOBC ? OK_OutputThinLTOBitcode : OK_OutputBitcode);
734 
735     VerifierKind VK = VerifierKind::InputOutput;
736     if (NoVerify)
737       VK = VerifierKind::None;
738     else if (VerifyEach)
739       VK = VerifierKind::EachPass;
740 
741     // The user has asked to use the new pass manager and provided a pipeline
742     // string. Hand off the rest of the functionality to the new code for that
743     // layer.
744     return runPassPipeline(
745                argv[0], *M, TM.get(), &TLII, Out.get(), ThinLinkOut.get(),
746                RemarksFile.get(), Pipeline, PluginList, PassBuilderCallbacks,
747                OK, VK, PreserveAssemblyUseListOrder,
748                PreserveBitcodeUseListOrder, EmitSummaryIndex, EmitModuleHash,
749                EnableDebugify, VerifyDebugInfoPreserve, UnifiedLTO)
750                ? 0
751                : 1;
752   }
753 
754   if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz ||
755       OptLevelO3) {
756     errs() << "Cannot use -O# with legacy PM.\n";
757     return 1;
758   }
759   if (EmitSummaryIndex) {
760     errs() << "Cannot use -module-summary with legacy PM.\n";
761     return 1;
762   }
763   if (EmitModuleHash) {
764     errs() << "Cannot use -module-hash with legacy PM.\n";
765     return 1;
766   }
767   if (OutputThinLTOBC) {
768     errs() << "Cannot use -thinlto-bc with legacy PM.\n";
769     return 1;
770   }
771   // Create a PassManager to hold and optimize the collection of passes we are
772   // about to build. If the -debugify-each option is set, wrap each pass with
773   // the (-check)-debugify passes.
774   DebugifyCustomPassManager Passes;
775   DebugifyStatsMap DIStatsMap;
776   DebugInfoPerPass DebugInfoBeforePass;
777   if (DebugifyEach) {
778     Passes.setDebugifyMode(DebugifyMode::SyntheticDebugInfo);
779     Passes.setDIStatsMap(DIStatsMap);
780   } else if (VerifyEachDebugInfoPreserve) {
781     Passes.setDebugifyMode(DebugifyMode::OriginalDebugInfo);
782     Passes.setDebugInfoBeforePass(DebugInfoBeforePass);
783     if (!VerifyDIPreserveExport.empty())
784       Passes.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport);
785   }
786 
787   bool AddOneTimeDebugifyPasses =
788       (EnableDebugify && !DebugifyEach) ||
789       (VerifyDebugInfoPreserve && !VerifyEachDebugInfoPreserve);
790 
791   Passes.add(new TargetLibraryInfoWrapperPass(TLII));
792 
793   // Add internal analysis passes from the target machine.
794   Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis()
795                                                      : TargetIRAnalysis()));
796 
797   if (AddOneTimeDebugifyPasses) {
798     if (EnableDebugify) {
799       Passes.setDIStatsMap(DIStatsMap);
800       Passes.add(createDebugifyModulePass());
801     } else if (VerifyDebugInfoPreserve) {
802       Passes.setDebugInfoBeforePass(DebugInfoBeforePass);
803       Passes.add(createDebugifyModulePass(DebugifyMode::OriginalDebugInfo, "",
804                                           &(Passes.getDebugInfoPerPass())));
805     }
806   }
807 
808   if (TM) {
809     Pass *TPC = TM->createPassConfig(Passes);
810     if (!TPC) {
811       errs() << "Target Machine pass config creation failed.\n";
812       return 1;
813     }
814     Passes.add(TPC);
815   }
816 
817   // Create a new optimization pass for each one specified on the command line
818   for (unsigned i = 0; i < PassList.size(); ++i) {
819     const PassInfo *PassInf = PassList[i];
820     if (PassInf->getNormalCtor()) {
821       Pass *P = PassInf->getNormalCtor()();
822       if (P) {
823         // Add the pass to the pass manager.
824         Passes.add(P);
825         // If we are verifying all of the intermediate steps, add the verifier.
826         if (VerifyEach)
827           Passes.add(createVerifierPass());
828       }
829     } else
830       errs() << argv[0] << ": cannot create pass: " << PassInf->getPassName()
831              << "\n";
832   }
833 
834   // Check that the module is well formed on completion of optimization
835   if (!NoVerify && !VerifyEach)
836     Passes.add(createVerifierPass());
837 
838   if (AddOneTimeDebugifyPasses) {
839     if (EnableDebugify)
840       Passes.add(createCheckDebugifyModulePass(false));
841     else if (VerifyDebugInfoPreserve) {
842       if (!VerifyDIPreserveExport.empty())
843         Passes.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport);
844       Passes.add(createCheckDebugifyModulePass(
845           false, "", nullptr, DebugifyMode::OriginalDebugInfo,
846           &(Passes.getDebugInfoPerPass()), VerifyDIPreserveExport));
847     }
848   }
849 
850   // In run twice mode, we want to make sure the output is bit-by-bit
851   // equivalent if we run the pass manager again, so setup two buffers and
852   // a stream to write to them. Note that llc does something similar and it
853   // may be worth to abstract this out in the future.
854   SmallVector<char, 0> Buffer;
855   SmallVector<char, 0> FirstRunBuffer;
856   std::unique_ptr<raw_svector_ostream> BOS;
857   raw_ostream *OS = nullptr;
858 
859   const bool ShouldEmitOutput = !NoOutput;
860 
861   // Write bitcode or assembly to the output as the last step...
862   if (ShouldEmitOutput || RunTwice) {
863     assert(Out);
864     OS = &Out->os();
865     if (RunTwice) {
866       BOS = std::make_unique<raw_svector_ostream>(Buffer);
867       OS = BOS.get();
868     }
869     if (OutputAssembly)
870       Passes.add(createPrintModulePass(*OS, "", PreserveAssemblyUseListOrder));
871     else
872       Passes.add(createBitcodeWriterPass(*OS, PreserveBitcodeUseListOrder));
873   }
874 
875   // Before executing passes, print the final values of the LLVM options.
876   cl::PrintOptionValues();
877 
878   if (!RunTwice) {
879     // Now that we have all of the passes ready, run them.
880     Passes.run(*M);
881   } else {
882     // If requested, run all passes twice with the same pass manager to catch
883     // bugs caused by persistent state in the passes.
884     std::unique_ptr<Module> M2(CloneModule(*M));
885     // Run all passes on the original module first, so the second run processes
886     // the clone to catch CloneModule bugs.
887     Passes.run(*M);
888     FirstRunBuffer = Buffer;
889     Buffer.clear();
890 
891     Passes.run(*M2);
892 
893     // Compare the two outputs and make sure they're the same
894     assert(Out);
895     if (Buffer.size() != FirstRunBuffer.size() ||
896         (memcmp(Buffer.data(), FirstRunBuffer.data(), Buffer.size()) != 0)) {
897       errs()
898           << "Running the pass manager twice changed the output.\n"
899              "Writing the result of the second run to the specified output.\n"
900              "To generate the one-run comparison binary, just run without\n"
901              "the compile-twice option\n";
902       if (ShouldEmitOutput) {
903         Out->os() << BOS->str();
904         Out->keep();
905       }
906       if (RemarksFile)
907         RemarksFile->keep();
908       return 1;
909     }
910     if (ShouldEmitOutput)
911       Out->os() << BOS->str();
912   }
913 
914   if (DebugifyEach && !DebugifyExport.empty())
915     exportDebugifyStats(DebugifyExport, Passes.getDebugifyStatsMap());
916 
917   // Declare success.
918   if (!NoOutput)
919     Out->keep();
920 
921   if (RemarksFile)
922     RemarksFile->keep();
923 
924   if (ThinLinkOut)
925     ThinLinkOut->keep();
926 
927   return 0;
928 }
929