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