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 "PassPrinters.h"
17#include "llvm/ADT/Triple.h"
18#include "llvm/Analysis/CallGraph.h"
19#include "llvm/Analysis/CallGraphSCCPass.h"
20#include "llvm/Analysis/LoopPass.h"
21#include "llvm/Analysis/RegionPass.h"
22#include "llvm/Analysis/TargetLibraryInfo.h"
23#include "llvm/Analysis/TargetTransformInfo.h"
24#include "llvm/AsmParser/Parser.h"
25#include "llvm/Bitcode/BitcodeWriterPass.h"
26#include "llvm/CodeGen/CommandFlags.h"
27#include "llvm/CodeGen/TargetPassConfig.h"
28#include "llvm/Config/llvm-config.h"
29#include "llvm/IR/DataLayout.h"
30#include "llvm/IR/DebugInfo.h"
31#include "llvm/IR/IRPrintingPasses.h"
32#include "llvm/IR/LLVMContext.h"
33#include "llvm/IR/LLVMRemarkStreamer.h"
34#include "llvm/IR/LegacyPassManager.h"
35#include "llvm/IR/LegacyPassNameParser.h"
36#include "llvm/IR/Module.h"
37#include "llvm/IR/Verifier.h"
38#include "llvm/IRReader/IRReader.h"
39#include "llvm/InitializePasses.h"
40#include "llvm/LinkAllIR.h"
41#include "llvm/LinkAllPasses.h"
42#include "llvm/MC/SubtargetFeature.h"
43#include "llvm/Support/Debug.h"
44#include "llvm/Support/FileSystem.h"
45#include "llvm/Support/Host.h"
46#include "llvm/Support/InitLLVM.h"
47#include "llvm/Support/PluginLoader.h"
48#include "llvm/Support/SourceMgr.h"
49#include "llvm/Support/SystemUtils.h"
50#include "llvm/Support/TargetRegistry.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/Coroutines.h"
56#include "llvm/Transforms/IPO/AlwaysInliner.h"
57#include "llvm/Transforms/IPO/PassManagerBuilder.h"
58#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
59#include "llvm/Transforms/Utils/Cloning.h"
60#include "llvm/Transforms/Utils/Debugify.h"
61#include <algorithm>
62#include <memory>
63using namespace llvm;
64using namespace opt_tool;
65
66static codegen::RegisterCodeGenFlags CFG;
67
68// The OptimizationList is automatically populated with registered Passes by the
69// PassNameParser.
70//
71static cl::list<const PassInfo*, bool, PassNameParser>
72PassList(cl::desc("Optimizations available:"));
73
74static cl::opt<bool> EnableNewPassManager(
75    "enable-new-pm", cl::desc("Enable the new pass manager"), 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.
81static cl::opt<std::string> PassPipeline(
82    "passes",
83    cl::desc("A textual description of the pass pipeline for optimizing"),
84    cl::Hidden);
85
86// Other command line options...
87//
88static cl::opt<std::string>
89InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
90    cl::init("-"), cl::value_desc("filename"));
91
92static cl::opt<std::string>
93OutputFilename("o", cl::desc("Override output filename"),
94               cl::value_desc("filename"));
95
96static cl::opt<bool>
97Force("f", cl::desc("Enable binary output on terminals"));
98
99static cl::opt<bool>
100PrintEachXForm("p", cl::desc("Print module after each transformation"));
101
102static cl::opt<bool>
103NoOutput("disable-output",
104         cl::desc("Do not write result bitcode file"), cl::Hidden);
105
106static cl::opt<bool>
107OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
108
109static cl::opt<bool>
110    OutputThinLTOBC("thinlto-bc",
111                    cl::desc("Write output as ThinLTO-ready bitcode"));
112
113static cl::opt<bool>
114    SplitLTOUnit("thinlto-split-lto-unit",
115                 cl::desc("Enable splitting of a ThinLTO LTOUnit"));
116
117static cl::opt<std::string> ThinLinkBitcodeFile(
118    "thin-link-bitcode-file", cl::value_desc("filename"),
119    cl::desc(
120        "A file in which to write minimized bitcode for the thin link only"));
121
122static cl::opt<bool>
123NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden);
124
125static cl::opt<bool> NoUpgradeDebugInfo("disable-upgrade-debug-info",
126                                        cl::desc("Generate invalid output"),
127                                        cl::ReallyHidden);
128
129static cl::opt<bool> VerifyEach("verify-each",
130                                cl::desc("Verify after each transform"));
131
132static cl::opt<bool>
133    DisableDITypeMap("disable-debug-info-type-map",
134                     cl::desc("Don't use a uniquing type map for debug info"));
135
136static cl::opt<bool>
137StripDebug("strip-debug",
138           cl::desc("Strip debugger symbol info from translation unit"));
139
140static cl::opt<bool>
141    StripNamedMetadata("strip-named-metadata",
142                       cl::desc("Strip module-level named metadata"));
143
144static cl::opt<bool> DisableInline("disable-inlining",
145                                   cl::desc("Do not run the inliner pass"));
146
147static cl::opt<bool>
148DisableOptimizations("disable-opt",
149                     cl::desc("Do not run any optimization passes"));
150
151static cl::opt<bool>
152StandardLinkOpts("std-link-opts",
153                 cl::desc("Include the standard link time optimizations"));
154
155static cl::opt<bool>
156OptLevelO0("O0",
157  cl::desc("Optimization level 0. Similar to clang -O0"));
158
159static cl::opt<bool>
160OptLevelO1("O1",
161           cl::desc("Optimization level 1. Similar to clang -O1"));
162
163static cl::opt<bool>
164OptLevelO2("O2",
165           cl::desc("Optimization level 2. Similar to clang -O2"));
166
167static cl::opt<bool>
168OptLevelOs("Os",
169           cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
170
171static cl::opt<bool>
172OptLevelOz("Oz",
173           cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
174
175static cl::opt<bool>
176OptLevelO3("O3",
177           cl::desc("Optimization level 3. Similar to clang -O3"));
178
179static cl::opt<unsigned>
180CodeGenOptLevel("codegen-opt-level",
181                cl::desc("Override optimization level for codegen hooks"));
182
183static cl::opt<std::string>
184TargetTriple("mtriple", cl::desc("Override target triple for module"));
185
186cl::opt<bool> DisableLoopUnrolling(
187    "disable-loop-unrolling",
188    cl::desc("Disable loop unrolling in all relevant passes"), cl::init(false));
189
190static cl::opt<bool> EmitSummaryIndex("module-summary",
191                                      cl::desc("Emit module summary index"),
192                                      cl::init(false));
193
194static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
195                                    cl::init(false));
196
197static cl::opt<bool>
198DisableSimplifyLibCalls("disable-simplify-libcalls",
199                        cl::desc("Disable simplify-libcalls"));
200
201static cl::list<std::string>
202DisableBuiltins("disable-builtin",
203                cl::desc("Disable specific target library builtin function"),
204                cl::ZeroOrMore);
205
206static cl::opt<bool>
207AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
208
209static cl::opt<bool> EnableDebugify(
210    "enable-debugify",
211    cl::desc(
212        "Start the pipeline with debugify and end it with check-debugify"));
213
214static cl::opt<bool> DebugifyEach(
215    "debugify-each",
216    cl::desc(
217        "Start each pass with debugify and end it with check-debugify"));
218
219static cl::opt<std::string>
220    DebugifyExport("debugify-export",
221                   cl::desc("Export per-pass debugify statistics to this file"),
222                   cl::value_desc("filename"), cl::init(""));
223
224static cl::opt<bool>
225PrintBreakpoints("print-breakpoints-for-testing",
226                 cl::desc("Print select breakpoints location for testing"));
227
228static cl::opt<std::string> ClDataLayout("data-layout",
229                                         cl::desc("data layout string to use"),
230                                         cl::value_desc("layout-string"),
231                                         cl::init(""));
232
233static cl::opt<bool> PreserveBitcodeUseListOrder(
234    "preserve-bc-uselistorder",
235    cl::desc("Preserve use-list order when writing LLVM bitcode."),
236    cl::init(true), cl::Hidden);
237
238static cl::opt<bool> PreserveAssemblyUseListOrder(
239    "preserve-ll-uselistorder",
240    cl::desc("Preserve use-list order when writing LLVM assembly."),
241    cl::init(false), cl::Hidden);
242
243static cl::opt<bool>
244    RunTwice("run-twice",
245             cl::desc("Run all passes twice, re-using the same pass manager."),
246             cl::init(false), cl::Hidden);
247
248static cl::opt<bool> DiscardValueNames(
249    "discard-value-names",
250    cl::desc("Discard names from Value (other than GlobalValue)."),
251    cl::init(false), cl::Hidden);
252
253static cl::opt<bool> Coroutines(
254  "enable-coroutines",
255  cl::desc("Enable coroutine passes."),
256  cl::init(false), cl::Hidden);
257
258static cl::opt<bool> TimeTrace(
259    "time-trace",
260    cl::desc("Record time trace"));
261
262static cl::opt<unsigned> TimeTraceGranularity(
263    "time-trace-granularity",
264    cl::desc("Minimum time granularity (in microseconds) traced by time profiler"),
265    cl::init(500), cl::Hidden);
266
267static cl::opt<std::string>
268    TimeTraceFile("time-trace-file",
269                    cl::desc("Specify time trace file destination"),
270                    cl::value_desc("filename"));
271
272static cl::opt<bool> RemarksWithHotness(
273    "pass-remarks-with-hotness",
274    cl::desc("With PGO, include profile count in optimization remarks"),
275    cl::Hidden);
276
277static cl::opt<unsigned>
278    RemarksHotnessThreshold("pass-remarks-hotness-threshold",
279                            cl::desc("Minimum profile count required for "
280                                     "an optimization remark to be output"),
281                            cl::Hidden);
282
283static cl::opt<std::string>
284    RemarksFilename("pass-remarks-output",
285                    cl::desc("Output filename for pass remarks"),
286                    cl::value_desc("filename"));
287
288static cl::opt<std::string>
289    RemarksPasses("pass-remarks-filter",
290                  cl::desc("Only record optimization remarks from passes whose "
291                           "names match the given regular expression"),
292                  cl::value_desc("regex"));
293
294static cl::opt<std::string> RemarksFormat(
295    "pass-remarks-format",
296    cl::desc("The format used for serializing remarks (default: YAML)"),
297    cl::value_desc("format"), cl::init("yaml"));
298
299cl::opt<PGOKind>
300    PGOKindFlag("pgo-kind", cl::init(NoPGO), cl::Hidden,
301                cl::desc("The kind of profile guided optimization"),
302                cl::values(clEnumValN(NoPGO, "nopgo", "Do not use PGO."),
303                           clEnumValN(InstrGen, "pgo-instr-gen-pipeline",
304                                      "Instrument the IR to generate profile."),
305                           clEnumValN(InstrUse, "pgo-instr-use-pipeline",
306                                      "Use instrumented profile to guide PGO."),
307                           clEnumValN(SampleUse, "pgo-sample-use-pipeline",
308                                      "Use sampled profile to guide PGO.")));
309cl::opt<std::string> ProfileFile("profile-file",
310                                 cl::desc("Path to the profile."), cl::Hidden);
311
312cl::opt<CSPGOKind> CSPGOKindFlag(
313    "cspgo-kind", cl::init(NoCSPGO), cl::Hidden,
314    cl::desc("The kind of context sensitive profile guided optimization"),
315    cl::values(
316        clEnumValN(NoCSPGO, "nocspgo", "Do not use CSPGO."),
317        clEnumValN(
318            CSInstrGen, "cspgo-instr-gen-pipeline",
319            "Instrument (context sensitive) the IR to generate profile."),
320        clEnumValN(
321            CSInstrUse, "cspgo-instr-use-pipeline",
322            "Use instrumented (context sensitive) profile to guide PGO.")));
323cl::opt<std::string> CSProfileGenFile(
324    "cs-profilegen-file",
325    cl::desc("Path to the instrumented context sensitive profile."),
326    cl::Hidden);
327
328class OptCustomPassManager : public legacy::PassManager {
329  DebugifyStatsMap DIStatsMap;
330
331public:
332  using super = legacy::PassManager;
333
334  void add(Pass *P) override {
335    // Wrap each pass with (-check)-debugify passes if requested, making
336    // exceptions for passes which shouldn't see -debugify instrumentation.
337    bool WrapWithDebugify = DebugifyEach && !P->getAsImmutablePass() &&
338                            !isIRPrintingPass(P) && !isBitcodeWriterPass(P);
339    if (!WrapWithDebugify) {
340      super::add(P);
341      return;
342    }
343
344    // Apply -debugify/-check-debugify before/after each pass and collect
345    // debug info loss statistics.
346    PassKind Kind = P->getPassKind();
347    StringRef Name = P->getPassName();
348
349    // TODO: Implement Debugify for LoopPass.
350    switch (Kind) {
351      case PT_Function:
352        super::add(createDebugifyFunctionPass());
353        super::add(P);
354        super::add(createCheckDebugifyFunctionPass(true, Name, &DIStatsMap));
355        break;
356      case PT_Module:
357        super::add(createDebugifyModulePass());
358        super::add(P);
359        super::add(createCheckDebugifyModulePass(true, Name, &DIStatsMap));
360        break;
361      default:
362        super::add(P);
363        break;
364    }
365  }
366
367  const DebugifyStatsMap &getDebugifyStatsMap() const { return DIStatsMap; }
368};
369
370static inline void addPass(legacy::PassManagerBase &PM, Pass *P) {
371  // Add the pass to the pass manager...
372  PM.add(P);
373
374  // If we are verifying all of the intermediate steps, add the verifier...
375  if (VerifyEach)
376    PM.add(createVerifierPass());
377}
378
379/// This routine adds optimization passes based on selected optimization level,
380/// OptLevel.
381///
382/// OptLevel - Optimization Level
383static void AddOptimizationPasses(legacy::PassManagerBase &MPM,
384                                  legacy::FunctionPassManager &FPM,
385                                  TargetMachine *TM, unsigned OptLevel,
386                                  unsigned SizeLevel) {
387  if (!NoVerify || VerifyEach)
388    FPM.add(createVerifierPass()); // Verify that input is correct
389
390  PassManagerBuilder Builder;
391  Builder.OptLevel = OptLevel;
392  Builder.SizeLevel = SizeLevel;
393
394  if (DisableInline) {
395    // No inlining pass
396  } else if (OptLevel > 1) {
397    Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
398  } else {
399    Builder.Inliner = createAlwaysInlinerLegacyPass();
400  }
401  Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
402                               DisableLoopUnrolling : OptLevel == 0;
403
404  Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
405
406  Builder.SLPVectorize = OptLevel > 1 && SizeLevel < 2;
407
408  if (TM)
409    TM->adjustPassManager(Builder);
410
411  if (Coroutines)
412    addCoroutinePassesToExtensionPoints(Builder);
413
414  switch (PGOKindFlag) {
415  case InstrGen:
416    Builder.EnablePGOInstrGen = true;
417    Builder.PGOInstrGen = ProfileFile;
418    break;
419  case InstrUse:
420    Builder.PGOInstrUse = ProfileFile;
421    break;
422  case SampleUse:
423    Builder.PGOSampleUse = ProfileFile;
424    break;
425  default:
426    break;
427  }
428
429  switch (CSPGOKindFlag) {
430  case CSInstrGen:
431    Builder.EnablePGOCSInstrGen = true;
432    break;
433  case CSInstrUse:
434    Builder.EnablePGOCSInstrUse = true;
435    break;
436  default:
437    break;
438  }
439
440  Builder.populateFunctionPassManager(FPM);
441  Builder.populateModulePassManager(MPM);
442}
443
444static void AddStandardLinkPasses(legacy::PassManagerBase &PM) {
445  PassManagerBuilder Builder;
446  Builder.VerifyInput = true;
447  if (DisableOptimizations)
448    Builder.OptLevel = 0;
449
450  if (!DisableInline)
451    Builder.Inliner = createFunctionInliningPass();
452  Builder.populateLTOPassManager(PM);
453}
454
455//===----------------------------------------------------------------------===//
456// CodeGen-related helper functions.
457//
458
459static CodeGenOpt::Level GetCodeGenOptLevel() {
460  if (CodeGenOptLevel.getNumOccurrences())
461    return static_cast<CodeGenOpt::Level>(unsigned(CodeGenOptLevel));
462  if (OptLevelO1)
463    return CodeGenOpt::Less;
464  if (OptLevelO2)
465    return CodeGenOpt::Default;
466  if (OptLevelO3)
467    return CodeGenOpt::Aggressive;
468  return CodeGenOpt::None;
469}
470
471// Returns the TargetMachine instance or zero if no triple is provided.
472static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
473                                       StringRef FeaturesStr,
474                                       const TargetOptions &Options) {
475  std::string Error;
476  const Target *TheTarget =
477      TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error);
478  // Some modules don't specify a triple, and this is okay.
479  if (!TheTarget) {
480    return nullptr;
481  }
482
483  return TheTarget->createTargetMachine(
484      TheTriple.getTriple(), codegen::getCPUStr(), codegen::getFeaturesStr(),
485      Options, codegen::getExplicitRelocModel(),
486      codegen::getExplicitCodeModel(), GetCodeGenOptLevel());
487}
488
489#ifdef BUILD_EXAMPLES
490void initializeExampleIRTransforms(llvm::PassRegistry &Registry);
491#endif
492
493
494void exportDebugifyStats(llvm::StringRef Path, const DebugifyStatsMap &Map) {
495  std::error_code EC;
496  raw_fd_ostream OS{Path, EC};
497  if (EC) {
498    errs() << "Could not open file: " << EC.message() << ", " << Path << '\n';
499    return;
500  }
501
502  OS << "Pass Name" << ',' << "# of missing debug values" << ','
503     << "# of missing locations" << ',' << "Missing/Expected value ratio" << ','
504     << "Missing/Expected location ratio" << '\n';
505  for (const auto &Entry : Map) {
506    StringRef Pass = Entry.first;
507    DebugifyStatistics Stats = Entry.second;
508
509    OS << Pass << ',' << Stats.NumDbgValuesMissing << ','
510       << Stats.NumDbgLocsMissing << ',' << Stats.getMissingValueRatio() << ','
511       << Stats.getEmptyLocationRatio() << '\n';
512  }
513}
514
515struct TimeTracerRAII {
516  TimeTracerRAII(StringRef ProgramName) {
517    if (TimeTrace)
518      timeTraceProfilerInitialize(TimeTraceGranularity, ProgramName);
519  }
520  ~TimeTracerRAII() {
521    if (TimeTrace) {
522      if (auto E = timeTraceProfilerWrite(TimeTraceFile, OutputFilename)) {
523        handleAllErrors(std::move(E), [&](const StringError &SE) {
524          errs() << SE.getMessage() << "\n";
525        });
526        return;
527      }
528      timeTraceProfilerCleanup();
529    }
530  }
531};
532
533//===----------------------------------------------------------------------===//
534// main for opt
535//
536int main(int argc, char **argv) {
537  InitLLVM X(argc, argv);
538
539  // Enable debug stream buffering.
540  EnableDebugBuffering = true;
541
542  LLVMContext Context;
543
544  InitializeAllTargets();
545  InitializeAllTargetMCs();
546  InitializeAllAsmPrinters();
547  InitializeAllAsmParsers();
548
549  // Initialize passes
550  PassRegistry &Registry = *PassRegistry::getPassRegistry();
551  initializeCore(Registry);
552  initializeCoroutines(Registry);
553  initializeScalarOpts(Registry);
554  initializeObjCARCOpts(Registry);
555  initializeVectorization(Registry);
556  initializeIPO(Registry);
557  initializeAnalysis(Registry);
558  initializeTransformUtils(Registry);
559  initializeInstCombine(Registry);
560  initializeAggressiveInstCombine(Registry);
561  initializeInstrumentation(Registry);
562  initializeTarget(Registry);
563  // For codegen passes, only passes that do IR to IR transformation are
564  // supported.
565  initializeExpandMemCmpPassPass(Registry);
566  initializeScalarizeMaskedMemIntrinPass(Registry);
567  initializeCodeGenPreparePass(Registry);
568  initializeAtomicExpandPass(Registry);
569  initializeRewriteSymbolsLegacyPassPass(Registry);
570  initializeWinEHPreparePass(Registry);
571  initializeDwarfEHPreparePass(Registry);
572  initializeSafeStackLegacyPassPass(Registry);
573  initializeSjLjEHPreparePass(Registry);
574  initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
575  initializeGlobalMergePass(Registry);
576  initializeIndirectBrExpandPassPass(Registry);
577  initializeInterleavedLoadCombinePass(Registry);
578  initializeInterleavedAccessPass(Registry);
579  initializeEntryExitInstrumenterPass(Registry);
580  initializePostInlineEntryExitInstrumenterPass(Registry);
581  initializeUnreachableBlockElimLegacyPassPass(Registry);
582  initializeExpandReductionsPass(Registry);
583  initializeWasmEHPreparePass(Registry);
584  initializeWriteBitcodePassPass(Registry);
585  initializeHardwareLoopsPass(Registry);
586  initializeTypePromotionPass(Registry);
587
588#ifdef BUILD_EXAMPLES
589  initializeExampleIRTransforms(Registry);
590#endif
591
592  cl::ParseCommandLineOptions(argc, argv,
593    "llvm .bc -> .bc modular optimizer and analysis printer\n");
594
595  if (AnalyzeOnly && NoOutput) {
596    errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
597    return 1;
598  }
599
600  TimeTracerRAII TimeTracer(argv[0]);
601
602  SMDiagnostic Err;
603
604  Context.setDiscardValueNames(DiscardValueNames);
605  if (!DisableDITypeMap)
606    Context.enableDebugTypeODRUniquing();
607
608  Expected<std::unique_ptr<ToolOutputFile>> RemarksFileOrErr =
609      setupLLVMOptimizationRemarks(Context, RemarksFilename, RemarksPasses,
610                                   RemarksFormat, RemarksWithHotness,
611                                   RemarksHotnessThreshold);
612  if (Error E = RemarksFileOrErr.takeError()) {
613    errs() << toString(std::move(E)) << '\n';
614    return 1;
615  }
616  std::unique_ptr<ToolOutputFile> RemarksFile = std::move(*RemarksFileOrErr);
617
618  // Load the input module...
619  auto SetDataLayout = [](StringRef) -> Optional<std::string> {
620    if (ClDataLayout.empty())
621      return None;
622    return ClDataLayout;
623  };
624  std::unique_ptr<Module> M;
625  if (NoUpgradeDebugInfo)
626    M = parseAssemblyFileWithIndexNoUpgradeDebugInfo(
627            InputFilename, Err, Context, nullptr, SetDataLayout)
628            .Mod;
629  else
630    M = parseIRFile(InputFilename, Err, Context, SetDataLayout);
631
632  if (!M) {
633    Err.print(argv[0], errs());
634    return 1;
635  }
636
637  // Strip debug info before running the verifier.
638  if (StripDebug)
639    StripDebugInfo(*M);
640
641  // Erase module-level named metadata, if requested.
642  if (StripNamedMetadata) {
643    while (!M->named_metadata_empty()) {
644      NamedMDNode *NMD = &*M->named_metadata_begin();
645      M->eraseNamedMetadata(NMD);
646    }
647  }
648
649  // If we are supposed to override the target triple or data layout, do so now.
650  if (!TargetTriple.empty())
651    M->setTargetTriple(Triple::normalize(TargetTriple));
652
653  // Immediately run the verifier to catch any problems before starting up the
654  // pass pipelines.  Otherwise we can crash on broken code during
655  // doInitialization().
656  if (!NoVerify && verifyModule(*M, &errs())) {
657    errs() << argv[0] << ": " << InputFilename
658           << ": error: input module is broken!\n";
659    return 1;
660  }
661
662  // Enable testing of whole program devirtualization on this module by invoking
663  // the facility for updating public visibility to linkage unit visibility when
664  // specified by an internal option. This is normally done during LTO which is
665  // not performed via opt.
666  updateVCallVisibilityInModule(*M,
667                                /* WholeProgramVisibilityEnabledInLTO */ false);
668
669  // Figure out what stream we are supposed to write to...
670  std::unique_ptr<ToolOutputFile> Out;
671  std::unique_ptr<ToolOutputFile> ThinLinkOut;
672  if (NoOutput) {
673    if (!OutputFilename.empty())
674      errs() << "WARNING: The -o (output filename) option is ignored when\n"
675                "the --disable-output option is used.\n";
676  } else {
677    // Default to standard output.
678    if (OutputFilename.empty())
679      OutputFilename = "-";
680
681    std::error_code EC;
682    sys::fs::OpenFlags Flags = OutputAssembly ? sys::fs::OF_Text
683                                              : sys::fs::OF_None;
684    Out.reset(new ToolOutputFile(OutputFilename, EC, Flags));
685    if (EC) {
686      errs() << EC.message() << '\n';
687      return 1;
688    }
689
690    if (!ThinLinkBitcodeFile.empty()) {
691      ThinLinkOut.reset(
692          new ToolOutputFile(ThinLinkBitcodeFile, EC, sys::fs::OF_None));
693      if (EC) {
694        errs() << EC.message() << '\n';
695        return 1;
696      }
697    }
698  }
699
700  Triple ModuleTriple(M->getTargetTriple());
701  std::string CPUStr, FeaturesStr;
702  TargetMachine *Machine = nullptr;
703  const TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
704
705  if (ModuleTriple.getArch()) {
706    CPUStr = codegen::getCPUStr();
707    FeaturesStr = codegen::getFeaturesStr();
708    Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options);
709  } else if (ModuleTriple.getArchName() != "unknown" &&
710             ModuleTriple.getArchName() != "") {
711    errs() << argv[0] << ": unrecognized architecture '"
712           << ModuleTriple.getArchName() << "' provided.\n";
713    return 1;
714  }
715
716  std::unique_ptr<TargetMachine> TM(Machine);
717
718  // Override function attributes based on CPUStr, FeaturesStr, and command line
719  // flags.
720  codegen::setFunctionAttributes(CPUStr, FeaturesStr, *M);
721
722  // If the output is set to be emitted to standard out, and standard out is a
723  // console, print out a warning message and refuse to do it.  We don't
724  // impress anyone by spewing tons of binary goo to a terminal.
725  if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
726    if (CheckBitcodeOutputToConsole(Out->os()))
727      NoOutput = true;
728
729  if (OutputThinLTOBC)
730    M->addModuleFlag(Module::Error, "EnableSplitLTOUnit", SplitLTOUnit);
731
732  if (EnableNewPassManager || PassPipeline.getNumOccurrences() > 0) {
733    if (PassPipeline.getNumOccurrences() > 0 && PassList.size() > 0) {
734      errs()
735          << "Cannot specify passes via both -foo-pass and --passes=foo-pass";
736      return 1;
737    }
738    SmallVector<StringRef, 4> Passes;
739    for (const auto &P : PassList) {
740      Passes.push_back(P->getPassArgument());
741    }
742    if (OptLevelO0)
743      Passes.push_back("default<O0>");
744    if (OptLevelO1)
745      Passes.push_back("default<O1>");
746    if (OptLevelO2)
747      Passes.push_back("default<O2>");
748    if (OptLevelO3)
749      Passes.push_back("default<O3>");
750    if (OptLevelOs)
751      Passes.push_back("default<Os>");
752    if (OptLevelOz)
753      Passes.push_back("default<Oz>");
754    OutputKind OK = OK_NoOutput;
755    if (!NoOutput)
756      OK = OutputAssembly
757               ? OK_OutputAssembly
758               : (OutputThinLTOBC ? OK_OutputThinLTOBitcode : OK_OutputBitcode);
759
760    VerifierKind VK = VK_VerifyInAndOut;
761    if (NoVerify)
762      VK = VK_NoVerifier;
763    else if (VerifyEach)
764      VK = VK_VerifyEachPass;
765
766    // The user has asked to use the new pass manager and provided a pipeline
767    // string. Hand off the rest of the functionality to the new code for that
768    // layer.
769    return runPassPipeline(argv[0], *M, TM.get(), Out.get(), ThinLinkOut.get(),
770                           RemarksFile.get(), PassPipeline, Passes, OK, VK,
771                           PreserveAssemblyUseListOrder,
772                           PreserveBitcodeUseListOrder, EmitSummaryIndex,
773                           EmitModuleHash, EnableDebugify, Coroutines)
774               ? 0
775               : 1;
776  }
777
778  // Create a PassManager to hold and optimize the collection of passes we are
779  // about to build.
780  OptCustomPassManager Passes;
781  bool AddOneTimeDebugifyPasses = EnableDebugify && !DebugifyEach;
782
783  // Add an appropriate TargetLibraryInfo pass for the module's triple.
784  TargetLibraryInfoImpl TLII(ModuleTriple);
785
786  // The -disable-simplify-libcalls flag actually disables all builtin optzns.
787  if (DisableSimplifyLibCalls)
788    TLII.disableAllFunctions();
789  else {
790    // Disable individual builtin functions in TargetLibraryInfo.
791    LibFunc F;
792    for (auto &FuncName : DisableBuiltins)
793      if (TLII.getLibFunc(FuncName, F))
794        TLII.setUnavailable(F);
795      else {
796        errs() << argv[0] << ": cannot disable nonexistent builtin function "
797               << FuncName << '\n';
798        return 1;
799      }
800  }
801
802  Passes.add(new TargetLibraryInfoWrapperPass(TLII));
803
804  // Add internal analysis passes from the target machine.
805  Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis()
806                                                     : TargetIRAnalysis()));
807
808  if (AddOneTimeDebugifyPasses)
809    Passes.add(createDebugifyModulePass());
810
811  std::unique_ptr<legacy::FunctionPassManager> FPasses;
812  if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz ||
813      OptLevelO3) {
814    FPasses.reset(new legacy::FunctionPassManager(M.get()));
815    FPasses->add(createTargetTransformInfoWrapperPass(
816        TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis()));
817  }
818
819  if (PrintBreakpoints) {
820    // Default to standard output.
821    if (!Out) {
822      if (OutputFilename.empty())
823        OutputFilename = "-";
824
825      std::error_code EC;
826      Out = std::make_unique<ToolOutputFile>(OutputFilename, EC,
827                                              sys::fs::OF_None);
828      if (EC) {
829        errs() << EC.message() << '\n';
830        return 1;
831      }
832    }
833    Passes.add(createBreakpointPrinter(Out->os()));
834    NoOutput = true;
835  }
836
837  if (TM) {
838    // FIXME: We should dyn_cast this when supported.
839    auto &LTM = static_cast<LLVMTargetMachine &>(*TM);
840    Pass *TPC = LTM.createPassConfig(Passes);
841    Passes.add(TPC);
842  }
843
844  // Create a new optimization pass for each one specified on the command line
845  for (unsigned i = 0; i < PassList.size(); ++i) {
846    if (StandardLinkOpts &&
847        StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
848      AddStandardLinkPasses(Passes);
849      StandardLinkOpts = false;
850    }
851
852    if (OptLevelO0 && OptLevelO0.getPosition() < PassList.getPosition(i)) {
853      AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
854      OptLevelO0 = false;
855    }
856
857    if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
858      AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
859      OptLevelO1 = false;
860    }
861
862    if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
863      AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
864      OptLevelO2 = false;
865    }
866
867    if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
868      AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
869      OptLevelOs = false;
870    }
871
872    if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
873      AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
874      OptLevelOz = false;
875    }
876
877    if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
878      AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
879      OptLevelO3 = false;
880    }
881
882    const PassInfo *PassInf = PassList[i];
883    Pass *P = nullptr;
884    if (PassInf->getNormalCtor())
885      P = PassInf->getNormalCtor()();
886    else
887      errs() << argv[0] << ": cannot create pass: "
888             << PassInf->getPassName() << "\n";
889    if (P) {
890      PassKind Kind = P->getPassKind();
891      addPass(Passes, P);
892
893      if (AnalyzeOnly) {
894        switch (Kind) {
895        case PT_Region:
896          Passes.add(createRegionPassPrinter(PassInf, Out->os()));
897          break;
898        case PT_Loop:
899          Passes.add(createLoopPassPrinter(PassInf, Out->os()));
900          break;
901        case PT_Function:
902          Passes.add(createFunctionPassPrinter(PassInf, Out->os()));
903          break;
904        case PT_CallGraphSCC:
905          Passes.add(createCallGraphPassPrinter(PassInf, Out->os()));
906          break;
907        default:
908          Passes.add(createModulePassPrinter(PassInf, Out->os()));
909          break;
910        }
911      }
912    }
913
914    if (PrintEachXForm)
915      Passes.add(
916          createPrintModulePass(errs(), "", PreserveAssemblyUseListOrder));
917  }
918
919  if (StandardLinkOpts) {
920    AddStandardLinkPasses(Passes);
921    StandardLinkOpts = false;
922  }
923
924  if (OptLevelO0)
925    AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
926
927  if (OptLevelO1)
928    AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
929
930  if (OptLevelO2)
931    AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
932
933  if (OptLevelOs)
934    AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
935
936  if (OptLevelOz)
937    AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
938
939  if (OptLevelO3)
940    AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
941
942  if (FPasses) {
943    FPasses->doInitialization();
944    for (Function &F : *M)
945      FPasses->run(F);
946    FPasses->doFinalization();
947  }
948
949  // Check that the module is well formed on completion of optimization
950  if (!NoVerify && !VerifyEach)
951    Passes.add(createVerifierPass());
952
953  if (AddOneTimeDebugifyPasses)
954    Passes.add(createCheckDebugifyModulePass(false));
955
956  // In run twice mode, we want to make sure the output is bit-by-bit
957  // equivalent if we run the pass manager again, so setup two buffers and
958  // a stream to write to them. Note that llc does something similar and it
959  // may be worth to abstract this out in the future.
960  SmallVector<char, 0> Buffer;
961  SmallVector<char, 0> FirstRunBuffer;
962  std::unique_ptr<raw_svector_ostream> BOS;
963  raw_ostream *OS = nullptr;
964
965  const bool ShouldEmitOutput = !NoOutput && !AnalyzeOnly;
966
967  // Write bitcode or assembly to the output as the last step...
968  if (ShouldEmitOutput || RunTwice) {
969    assert(Out);
970    OS = &Out->os();
971    if (RunTwice) {
972      BOS = std::make_unique<raw_svector_ostream>(Buffer);
973      OS = BOS.get();
974    }
975    if (OutputAssembly) {
976      if (EmitSummaryIndex)
977        report_fatal_error("Text output is incompatible with -module-summary");
978      if (EmitModuleHash)
979        report_fatal_error("Text output is incompatible with -module-hash");
980      Passes.add(createPrintModulePass(*OS, "", PreserveAssemblyUseListOrder));
981    } else if (OutputThinLTOBC)
982      Passes.add(createWriteThinLTOBitcodePass(
983          *OS, ThinLinkOut ? &ThinLinkOut->os() : nullptr));
984    else
985      Passes.add(createBitcodeWriterPass(*OS, PreserveBitcodeUseListOrder,
986                                         EmitSummaryIndex, EmitModuleHash));
987  }
988
989  // Before executing passes, print the final values of the LLVM options.
990  cl::PrintOptionValues();
991
992  if (!RunTwice) {
993    // Now that we have all of the passes ready, run them.
994    Passes.run(*M);
995  } else {
996    // If requested, run all passes twice with the same pass manager to catch
997    // bugs caused by persistent state in the passes.
998    std::unique_ptr<Module> M2(CloneModule(*M));
999    // Run all passes on the original module first, so the second run processes
1000    // the clone to catch CloneModule bugs.
1001    Passes.run(*M);
1002    FirstRunBuffer = Buffer;
1003    Buffer.clear();
1004
1005    Passes.run(*M2);
1006
1007    // Compare the two outputs and make sure they're the same
1008    assert(Out);
1009    if (Buffer.size() != FirstRunBuffer.size() ||
1010        (memcmp(Buffer.data(), FirstRunBuffer.data(), Buffer.size()) != 0)) {
1011      errs()
1012          << "Running the pass manager twice changed the output.\n"
1013             "Writing the result of the second run to the specified output.\n"
1014             "To generate the one-run comparison binary, just run without\n"
1015             "the compile-twice option\n";
1016      if (ShouldEmitOutput) {
1017        Out->os() << BOS->str();
1018        Out->keep();
1019      }
1020      if (RemarksFile)
1021        RemarksFile->keep();
1022      return 1;
1023    }
1024    if (ShouldEmitOutput)
1025      Out->os() << BOS->str();
1026  }
1027
1028  if (DebugifyEach && !DebugifyExport.empty())
1029    exportDebugifyStats(DebugifyExport, Passes.getDebugifyStatsMap());
1030
1031  // Declare success.
1032  if (!NoOutput || PrintBreakpoints)
1033    Out->keep();
1034
1035  if (RemarksFile)
1036    RemarksFile->keep();
1037
1038  if (ThinLinkOut)
1039    ThinLinkOut->keep();
1040
1041  return 0;
1042}
1043