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