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