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