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