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