1//===- optdriver.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. Common driver library for re-use
11// by potential downstream opt-variants.
12//
13//===----------------------------------------------------------------------===//
14
15#include "NewPMDriver.h"
16#include "llvm/Analysis/CallGraph.h"
17#include "llvm/Analysis/CallGraphSCCPass.h"
18#include "llvm/Analysis/LoopPass.h"
19#include "llvm/Analysis/RegionPass.h"
20#include "llvm/Analysis/RuntimeLibcallInfo.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/MCTargetOptionsCommandFlags.h"
41#include "llvm/MC/TargetRegistry.h"
42#include "llvm/Plugins/PassPlugin.h"
43#include "llvm/Remarks/HotnessThresholdParser.h"
44#include "llvm/Support/Debug.h"
45#include "llvm/Support/ErrorHandling.h"
46#include "llvm/Support/FileSystem.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/TimeProfiler.h"
53#include "llvm/Support/ToolOutputFile.h"
54#include "llvm/Support/YAMLTraits.h"
55#include "llvm/Target/TargetMachine.h"
56#include "llvm/TargetParser/Host.h"
57#include "llvm/TargetParser/SubtargetFeature.h"
58#include "llvm/TargetParser/Triple.h"
59#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
60#include "llvm/Transforms/Utils/Cloning.h"
61#include "llvm/Transforms/Utils/Debugify.h"
62#include <algorithm>
63#include <memory>
64#include <optional>
65using namespace llvm;
66using namespace opt_tool;
67
68static codegen::RegisterCodeGenFlags CFG;
69static codegen::RegisterSaveStatsFlag SSF;
70
71// The OptimizationList is automatically populated with registered Passes by the
72// PassNameParser.
73static cl::list<const PassInfo *, bool, PassNameParser> PassList(cl::desc(
74 "Optimizations available (use \"-passes=\" for the new pass manager)"));
75
76static cl::opt<bool> EnableLegacyPassManager(
77 "bugpoint-enable-legacy-pm",
78 cl::desc(
79 "Enable the legacy pass manager. This is strictly for bugpoint "
80 "due to it not working with the new PM, please do not use otherwise."),
81 cl::init(Val: false));
82
83// This flag specifies a textual description of the optimization pass pipeline
84// to run over the module. This flag switches opt to use the new pass manager
85// infrastructure, completely disabling all of the flags specific to the old
86// pass management.
87static cl::opt<std::string> PassPipeline(
88 "passes",
89 cl::desc(
90 "A textual (comma separated) description of the pass pipeline e.g.,"
91 "-passes=\"foo,bar\", to have analysis passes available before a pass, "
92 "add \"require<foo-analysis>\". See "
93 "https://llvm.org/docs/NewPassManager.html#invoking-opt "
94 "for more details on the pass pipeline syntax. "));
95
96static cl::alias PassPipeline2("p", cl::aliasopt(PassPipeline),
97 cl::desc("Alias for -passes"));
98
99static cl::opt<bool> PrintPasses("print-passes",
100 cl::desc("Print available passes that can be "
101 "specified in -passes=foo and exit"));
102
103static cl::opt<std::string> InputFilename(cl::Positional,
104 cl::desc("<input bitcode file>"),
105 cl::init(Val: "-"),
106 cl::value_desc("filename"));
107
108static cl::opt<std::string> OutputFilename("o",
109 cl::desc("Override output filename"),
110 cl::value_desc("filename"));
111
112static cl::opt<bool> Force("f", cl::desc("Enable binary output on terminals"));
113
114static cl::opt<bool> NoOutput("disable-output",
115 cl::desc("Do not write result bitcode file"),
116 cl::Hidden);
117
118static cl::opt<bool> OutputAssembly("S",
119 cl::desc("Write output as LLVM assembly"));
120
121static cl::opt<bool>
122 OutputThinLTOBC("thinlto-bc",
123 cl::desc("Write output as ThinLTO-ready bitcode"));
124
125static cl::opt<bool>
126 SplitLTOUnit("thinlto-split-lto-unit",
127 cl::desc("Enable splitting of a ThinLTO LTOUnit"));
128
129static cl::opt<bool>
130 UnifiedLTO("unified-lto",
131 cl::desc("Use unified LTO piplines. Ignored unless -thinlto-bc "
132 "is also specified."),
133 cl::Hidden, cl::init(Val: false));
134
135static cl::opt<std::string> ThinLinkBitcodeFile(
136 "thin-link-bitcode-file", cl::value_desc("filename"),
137 cl::desc(
138 "A file in which to write minimized bitcode for the thin link only"));
139
140static cl::opt<bool> NoVerify("disable-verify",
141 cl::desc("Do not run the verifier"), cl::Hidden);
142
143static cl::opt<bool> NoUpgradeDebugInfo("disable-upgrade-debug-info",
144 cl::desc("Generate invalid output"),
145 cl::ReallyHidden);
146
147static cl::opt<bool> VerifyEach("verify-each",
148 cl::desc("Verify after each transform"));
149
150static cl::opt<bool>
151 DisableDITypeMap("disable-debug-info-type-map",
152 cl::desc("Don't use a uniquing type map for debug info"));
153
154static cl::opt<bool>
155 StripDebug("strip-debug",
156 cl::desc("Strip debugger symbol info from translation unit"));
157
158static cl::opt<bool>
159 StripNamedMetadata("strip-named-metadata",
160 cl::desc("Strip module-level named metadata"));
161
162static cl::opt<bool>
163 OptLevelO0("O0", cl::desc("Optimization level 0. Similar to clang -O0. "
164 "Same as -passes=\"default<O0>\""));
165
166static cl::opt<bool>
167 OptLevelO1("O1", cl::desc("Optimization level 1. Similar to clang -O1. "
168 "Same as -passes=\"default<O1>\""));
169
170static cl::opt<bool>
171 OptLevelO2("O2", cl::desc("Optimization level 2. Similar to clang -O2. "
172 "Same as -passes=\"default<O2>\""));
173
174static cl::opt<bool>
175 OptLevelOs("Os", cl::desc("Like -O2 but size-conscious. Similar to clang "
176 "-Os. Same as -passes=\"default<Os>\""));
177
178static cl::opt<bool> OptLevelOz(
179 "Oz",
180 cl::desc("Like -O2 but optimize for code size above all else. Similar to "
181 "clang -Oz. Same as -passes=\"default<Oz>\""));
182
183static cl::opt<bool>
184 OptLevelO3("O3", cl::desc("Optimization level 3. Similar to clang -O3. "
185 "Same as -passes=\"default<O3>\""));
186
187static cl::opt<unsigned> CodeGenOptLevelCL(
188 "codegen-opt-level",
189 cl::desc("Override optimization level for codegen hooks, legacy PM only"));
190
191static cl::opt<std::string>
192 TargetTriple("mtriple", cl::desc("Override target triple for module"));
193
194static cl::opt<bool> EmitSummaryIndex("module-summary",
195 cl::desc("Emit module summary index"),
196 cl::init(Val: false));
197
198static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
199 cl::init(Val: false));
200
201static cl::opt<bool>
202 DisableSimplifyLibCalls("disable-simplify-libcalls",
203 cl::desc("Disable simplify-libcalls"));
204
205static cl::list<std::string> DisableBuiltins(
206 "disable-builtin",
207 cl::desc("Disable specific target library builtin function"));
208
209static cl::list<std::string> EnableBuiltins(
210 "enable-builtin",
211 cl::desc("Enable specific target library builtin functions"));
212
213static cl::opt<bool> EnableDebugify(
214 "enable-debugify",
215 cl::desc(
216 "Start the pipeline with debugify and end it with check-debugify"));
217
218static cl::opt<bool> VerifyDebugInfoPreserve(
219 "verify-debuginfo-preserve",
220 cl::desc("Start the pipeline with collecting and end it with checking of "
221 "debug info preservation."));
222
223static cl::opt<bool> EnableProfileVerification(
224 "enable-profcheck",
225#if defined(LLVM_ENABLE_PROFCHECK)
226 cl::init(true),
227#else
228 cl::init(Val: false),
229#endif
230 cl::desc(
231 "Start the pipeline with prof-inject and end it with prof-verify"));
232
233static cl::opt<std::string> ClDataLayout("data-layout",
234 cl::desc("data layout string to use"),
235 cl::value_desc("layout-string"),
236 cl::init(Val: ""));
237
238static cl::opt<bool> RunTwice("run-twice",
239 cl::desc("Run all passes twice, re-using the "
240 "same pass manager (legacy PM only)."),
241 cl::init(Val: false), cl::Hidden);
242
243static cl::opt<bool> DiscardValueNames(
244 "discard-value-names",
245 cl::desc("Discard names from Value (other than GlobalValue)."),
246 cl::init(Val: false), cl::Hidden);
247
248static cl::opt<bool> TimeTrace("time-trace", cl::desc("Record time trace"));
249
250static cl::opt<unsigned> TimeTraceGranularity(
251 "time-trace-granularity",
252 cl::desc(
253 "Minimum time granularity (in microseconds) traced by time profiler"),
254 cl::init(Val: 500), cl::Hidden);
255
256static cl::opt<std::string>
257 TimeTraceFile("time-trace-file",
258 cl::desc("Specify time trace file destination"),
259 cl::value_desc("filename"));
260
261static cl::opt<bool> RemarksWithHotness(
262 "pass-remarks-with-hotness",
263 cl::desc("With PGO, include profile count in optimization remarks"),
264 cl::Hidden);
265
266static cl::opt<std::optional<uint64_t>, false, remarks::HotnessThresholdParser>
267 RemarksHotnessThreshold(
268 "pass-remarks-hotness-threshold",
269 cl::desc("Minimum profile count required for "
270 "an optimization remark to be output. "
271 "Use 'auto' to apply the threshold from profile summary"),
272 cl::value_desc("N or 'auto'"), cl::init(Val: 0), cl::Hidden);
273
274static cl::opt<std::string>
275 RemarksFilename("pass-remarks-output",
276 cl::desc("Output filename for pass remarks"),
277 cl::value_desc("filename"));
278
279static cl::opt<std::string>
280 RemarksPasses("pass-remarks-filter",
281 cl::desc("Only record optimization remarks from passes whose "
282 "names match the given regular expression"),
283 cl::value_desc("regex"));
284
285static cl::opt<std::string> RemarksFormat(
286 "pass-remarks-format",
287 cl::desc("The format used for serializing remarks (default: YAML)"),
288 cl::value_desc("format"), cl::init(Val: "yaml"));
289
290static cl::list<std::string>
291 PassPlugins("load-pass-plugin",
292 cl::desc("Load passes from plugin library"));
293
294//===----------------------------------------------------------------------===//
295// CodeGen-related helper functions.
296//
297
298static CodeGenOptLevel GetCodeGenOptLevel() {
299 return static_cast<CodeGenOptLevel>(unsigned(CodeGenOptLevelCL));
300}
301
302namespace {
303struct TimeTracerRAII {
304 TimeTracerRAII(StringRef ProgramName) {
305 if (TimeTrace)
306 timeTraceProfilerInitialize(TimeTraceGranularity, ProcName: ProgramName);
307 }
308 ~TimeTracerRAII() {
309 if (!TimeTrace)
310 return;
311 if (auto E = timeTraceProfilerWrite(PreferredFileName: TimeTraceFile, FallbackFileName: OutputFilename)) {
312 handleAllErrors(E: std::move(E), Handlers: [&](const StringError &SE) {
313 errs() << SE.getMessage() << "\n";
314 });
315 return;
316 }
317 timeTraceProfilerCleanup();
318 }
319};
320} // namespace
321
322// For use in NPM transition. Currently this contains most codegen-specific
323// passes. Remove passes from here when porting to the NPM.
324// TODO: use a codegen version of PassRegistry.def/PassBuilder::is*Pass() once
325// it exists.
326static bool shouldPinPassToLegacyPM(StringRef Pass) {
327 static constexpr StringLiteral PassNameExactToIgnore[] = {
328 "nvvm-reflect",
329 "nvvm-intr-range",
330 "amdgpu-simplifylib",
331 "amdgpu-image-intrinsic-opt",
332 "amdgpu-usenative",
333 "amdgpu-promote-alloca",
334 "amdgpu-promote-alloca-to-vector",
335 "amdgpu-lower-kernel-attributes",
336 "amdgpu-propagate-attributes-early",
337 "amdgpu-propagate-attributes-late",
338 "amdgpu-printf-runtime-binding",
339 "amdgpu-always-inline"};
340 if (llvm::is_contained(Range: PassNameExactToIgnore, Element: Pass))
341 return false;
342
343 static constexpr StringLiteral PassNamePrefix[] = {
344 "x86-", "xcore-", "wasm-", "systemz-", "ppc-", "nvvm-",
345 "nvptx-", "mips-", "lanai-", "hexagon-", "bpf-", "avr-",
346 "thumb2-", "arm-", "si-", "gcn-", "amdgpu-", "aarch64-",
347 "amdgcn-", "polly-", "riscv-", "dxil-"};
348 static constexpr StringLiteral PassNameContain[] = {"-eh-prepare"};
349 static constexpr StringLiteral PassNameExact[] = {
350 "safe-stack",
351 "cost-model",
352 "codegenprepare",
353 "interleaved-load-combine",
354 "unreachableblockelim",
355 "verify-safepoint-ir",
356 "atomic-expand",
357 "expandvp",
358 "mve-tail-predication",
359 "interleaved-access",
360 "global-merge",
361 "pre-isel-intrinsic-lowering",
362 "expand-reductions",
363 "indirectbr-expand",
364 "generic-to-nvvm",
365 "expand-memcmp",
366 "loop-reduce",
367 "lower-amx-type",
368 "lower-amx-intrinsics",
369 "polyhedral-info",
370 "print-polyhedral-info",
371 "replace-with-veclib",
372 "jmc-instrumenter",
373 "dot-regions",
374 "dot-regions-only",
375 "view-regions",
376 "view-regions-only",
377 "select-optimize",
378 "structurizecfg",
379 "fix-irreducible",
380 "expand-ir-insts",
381 "callbrprepare",
382 "scalarizer",
383 };
384 for (StringLiteral P : PassNamePrefix)
385 if (Pass.starts_with(Prefix: P))
386 return true;
387 for (StringLiteral P : PassNameContain)
388 if (Pass.contains(Other: P))
389 return true;
390 return llvm::is_contained(Range: PassNameExact, Element: Pass);
391}
392
393// For use in NPM transition.
394static bool shouldForceLegacyPM() {
395 for (const PassInfo *P : PassList) {
396 StringRef Arg = P->getPassArgument();
397 if (shouldPinPassToLegacyPM(Pass: Arg))
398 return true;
399 }
400 return false;
401}
402
403//===----------------------------------------------------------------------===//
404// main for opt
405//
406extern "C" int
407optMain(int argc, char **argv,
408 ArrayRef<std::function<void(PassBuilder &)>> PassBuilderCallbacks) {
409 InitLLVM X(argc, argv);
410
411 // Enable debug stream buffering.
412 EnableDebugBuffering = true;
413
414 InitializeAllTargets();
415 InitializeAllTargetMCs();
416 InitializeAllAsmPrinters();
417 InitializeAllAsmParsers();
418
419 // Initialize passes
420 PassRegistry &Registry = *PassRegistry::getPassRegistry();
421 initializeCore(Registry);
422 initializeScalarOpts(Registry);
423 initializeVectorization(Registry);
424 initializeIPO(Registry);
425 initializeAnalysis(Registry);
426 initializeTransformUtils(Registry);
427 initializeInstCombine(Registry);
428 initializeTarget(Registry);
429 // For codegen passes, only passes that do IR to IR transformation are
430 // supported.
431 initializeExpandIRInstsLegacyPassPass(Registry);
432 initializeExpandMemCmpLegacyPassPass(Registry);
433 initializeScalarizeMaskedMemIntrinLegacyPassPass(Registry);
434 initializeSelectOptimizePass(Registry);
435 initializeCallBrPreparePass(Registry);
436 initializeCodeGenPrepareLegacyPassPass(Registry);
437 initializeAtomicExpandLegacyPass(Registry);
438 initializeWinEHPreparePass(Registry);
439 initializeDwarfEHPrepareLegacyPassPass(Registry);
440 initializeSafeStackLegacyPassPass(Registry);
441 initializeSjLjEHPreparePass(Registry);
442 initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
443 initializeGlobalMergePass(Registry);
444 initializeIndirectBrExpandLegacyPassPass(Registry);
445 initializeInterleavedLoadCombinePass(Registry);
446 initializeInterleavedAccessPass(Registry);
447 initializePostInlineEntryExitInstrumenterPass(Registry);
448 initializeUnreachableBlockElimLegacyPassPass(Registry);
449 initializeExpandReductionsPass(Registry);
450 initializeWasmEHPreparePass(Registry);
451 initializeWriteBitcodePassPass(Registry);
452 initializeReplaceWithVeclibLegacyPass(Registry);
453 initializeJMCInstrumenterPass(Registry);
454
455 SmallVector<PassPlugin, 1> PluginList;
456 PassPlugins.setCallback([&](const std::string &PluginPath) {
457 auto Plugin = PassPlugin::Load(Filename: PluginPath);
458 if (!Plugin)
459 reportFatalUsageError(Err: Plugin.takeError());
460 PluginList.emplace_back(Args&: Plugin.get());
461 });
462
463 // Register the Target and CPU printer for --version.
464 cl::AddExtraVersionPrinter(func: sys::printDefaultTargetAndDetectedCPU);
465
466 cl::ParseCommandLineOptions(
467 argc, argv, Overview: "llvm .bc -> .bc modular optimizer and analysis printer\n");
468
469 LLVMContext Context;
470
471 // TODO: remove shouldForceLegacyPM().
472 const bool UseNPM = (!EnableLegacyPassManager && !shouldForceLegacyPM()) ||
473 PassPipeline.getNumOccurrences() > 0;
474
475 if (UseNPM && !PassList.empty()) {
476 errs() << "The `opt -passname` syntax for the new pass manager is "
477 "not supported, please use `opt -passes=<pipeline>` (or the `-p` "
478 "alias for a more concise version).\n";
479 errs() << "See https://llvm.org/docs/NewPassManager.html#invoking-opt "
480 "for more details on the pass pipeline syntax.\n\n";
481 return 1;
482 }
483
484 if (!UseNPM && PluginList.size()) {
485 errs() << argv[0] << ": " << PassPlugins.ArgStr
486 << " specified with legacy PM.\n";
487 return 1;
488 }
489
490 // FIXME: once the legacy PM code is deleted, move runPassPipeline() here and
491 // construct the PassBuilder before parsing IR so we can reuse the same
492 // PassBuilder for print passes.
493 if (PrintPasses) {
494 printPasses(OS&: outs());
495 return 0;
496 }
497
498 TimeTracerRAII TimeTracer(argv[0]);
499
500 SMDiagnostic Err;
501
502 Context.setDiscardValueNames(DiscardValueNames);
503 if (!DisableDITypeMap)
504 Context.enableDebugTypeODRUniquing();
505
506 Expected<LLVMRemarkFileHandle> RemarksFileOrErr =
507 setupLLVMOptimizationRemarks(Context, RemarksFilename, RemarksPasses,
508 RemarksFormat, RemarksWithHotness,
509 RemarksHotnessThreshold);
510 if (Error E = RemarksFileOrErr.takeError()) {
511 errs() << toString(E: std::move(E)) << '\n';
512 return 1;
513 }
514 LLVMRemarkFileHandle RemarksFile = std::move(*RemarksFileOrErr);
515
516 codegen::MaybeEnableStatistics();
517
518 StringRef ABIName = mc::getABIName(); // FIXME: Handle module flag.
519
520 // Load the input module...
521 auto SetDataLayout = [&](StringRef IRTriple,
522 StringRef IRLayout) -> std::optional<std::string> {
523 // Data layout specified on the command line has the highest priority.
524 if (!ClDataLayout.empty())
525 return ClDataLayout;
526 // If an explicit data layout is already defined in the IR, don't infer.
527 if (!IRLayout.empty())
528 return std::nullopt;
529
530 // If an explicit triple was specified (either in the IR or on the
531 // command line), use that to infer the default data layout. However, the
532 // command line target triple should override the IR file target triple.
533 std::string TripleStr =
534 TargetTriple.empty() ? IRTriple.str() : Triple::normalize(Str: TargetTriple);
535 // If the triple string is still empty, we don't fall back to
536 // sys::getDefaultTargetTriple() since we do not want to have differing
537 // behaviour dependent on the configured default triple. Therefore, if the
538 // user did not pass -mtriple or define an explicit triple/datalayout in
539 // the IR, we should default to an empty (default) DataLayout.
540 if (TripleStr.empty())
541 return std::nullopt;
542
543 Triple TT(TripleStr);
544
545 std::string Str = TT.computeDataLayout(ABIName);
546 if (Str.empty()) {
547 errs() << argv[0]
548 << ": warning: failed to infer data layout from target triple\n";
549 return std::nullopt;
550 }
551 return Str;
552 };
553 std::unique_ptr<Module> M;
554 if (NoUpgradeDebugInfo)
555 M = parseAssemblyFileWithIndexNoUpgradeDebugInfo(
556 Filename: InputFilename, Err, Context, Slots: nullptr, DataLayoutCallback: SetDataLayout)
557 .Mod;
558 else
559 M = parseIRFile(Filename: InputFilename, Err, Context,
560 Callbacks: ParserCallbacks(SetDataLayout));
561
562 if (!M) {
563 Err.print(ProgName: argv[0], S&: errs());
564 return 1;
565 }
566
567 // Strip debug info before running the verifier.
568 if (StripDebug)
569 StripDebugInfo(M&: *M);
570
571 // Erase module-level named metadata, if requested.
572 if (StripNamedMetadata) {
573 while (!M->named_metadata_empty()) {
574 NamedMDNode *NMD = &*M->named_metadata_begin();
575 M->eraseNamedMetadata(NMD);
576 }
577 }
578
579 // If we are supposed to override the target triple, do so now.
580 if (!TargetTriple.empty())
581 M->setTargetTriple(Triple(Triple::normalize(Str: TargetTriple)));
582
583 // Immediately run the verifier to catch any problems before starting up the
584 // pass pipelines. Otherwise we can crash on broken code during
585 // doInitialization().
586 if (!NoVerify && verifyModule(M: *M, OS: &errs())) {
587 errs() << argv[0] << ": " << InputFilename
588 << ": error: input module is broken!\n";
589 return 1;
590 }
591
592 // Enable testing of whole program devirtualization on this module by invoking
593 // the facility for updating public visibility to linkage unit visibility when
594 // specified by an internal option. This is normally done during LTO which is
595 // not performed via opt.
596 updateVCallVisibilityInModule(
597 M&: *M,
598 /*WholeProgramVisibilityEnabledInLTO=*/false,
599 // FIXME: These need linker information via a
600 // TBD new interface.
601 /*DynamicExportSymbols=*/{},
602 /*ValidateAllVtablesHaveTypeInfos=*/false,
603 /*IsVisibleToRegularObj=*/[](StringRef) { return true; });
604
605 // Figure out what stream we are supposed to write to...
606 std::unique_ptr<ToolOutputFile> Out;
607 std::unique_ptr<ToolOutputFile> ThinLinkOut;
608 if (NoOutput) {
609 if (!OutputFilename.empty())
610 errs() << "WARNING: The -o (output filename) option is ignored when\n"
611 "the --disable-output option is used.\n";
612 } else {
613 // Default to standard output.
614 if (OutputFilename.empty())
615 OutputFilename = "-";
616
617 std::error_code EC;
618 sys::fs::OpenFlags Flags =
619 OutputAssembly ? sys::fs::OF_TextWithCRLF : sys::fs::OF_None;
620 Out.reset(p: new ToolOutputFile(OutputFilename, EC, Flags));
621 if (EC) {
622 errs() << EC.message() << '\n';
623 return 1;
624 }
625
626 if (!ThinLinkBitcodeFile.empty()) {
627 ThinLinkOut.reset(
628 p: new ToolOutputFile(ThinLinkBitcodeFile, EC, sys::fs::OF_None));
629 if (EC) {
630 errs() << EC.message() << '\n';
631 return 1;
632 }
633 }
634 }
635
636 Triple ModuleTriple(M->getTargetTriple());
637 std::string CPUStr, FeaturesStr;
638 std::unique_ptr<TargetMachine> TM;
639 if (ModuleTriple.getArch()) {
640 CPUStr = codegen::getCPUStr();
641 FeaturesStr = codegen::getFeaturesStr();
642 Expected<std::unique_ptr<TargetMachine>> ExpectedTM =
643 codegen::createTargetMachineForTriple(TargetTriple: ModuleTriple.str(),
644 OptLevel: GetCodeGenOptLevel());
645 if (auto E = ExpectedTM.takeError()) {
646 errs() << argv[0] << ": WARNING: failed to create target machine for '"
647 << ModuleTriple.str() << "': " << toString(E: std::move(E)) << "\n";
648 } else {
649 TM = std::move(*ExpectedTM);
650 }
651 } else if (ModuleTriple.getArchName() != "unknown" &&
652 ModuleTriple.getArchName() != "") {
653 errs() << argv[0] << ": unrecognized architecture '"
654 << ModuleTriple.getArchName() << "' provided.\n";
655 return 1;
656 }
657
658 TargetOptions CodeGenFlagsOptions;
659 const TargetOptions *Options = TM ? &TM->Options : &CodeGenFlagsOptions;
660 if (!TM) {
661 CodeGenFlagsOptions =
662 codegen::InitTargetOptionsFromCodeGenFlags(TheTriple: ModuleTriple);
663 }
664
665 // Override function attributes based on CPUStr, FeaturesStr, and command line
666 // flags.
667 codegen::setFunctionAttributes(CPU: CPUStr, Features: FeaturesStr, M&: *M);
668
669 // If the output is set to be emitted to standard out, and standard out is a
670 // console, print out a warning message and refuse to do it. We don't
671 // impress anyone by spewing tons of binary goo to a terminal.
672 if (!Force && !NoOutput && !OutputAssembly)
673 if (CheckBitcodeOutputToConsole(stream_to_check&: Out->os()))
674 NoOutput = true;
675
676 if (OutputThinLTOBC) {
677 M->addModuleFlag(Behavior: Module::Error, Key: "EnableSplitLTOUnit", Val: SplitLTOUnit);
678 if (UnifiedLTO)
679 M->addModuleFlag(Behavior: Module::Error, Key: "UnifiedLTO", Val: 1);
680 }
681
682 // Add an appropriate TargetLibraryInfo pass for the module's triple.
683 TargetLibraryInfoImpl TLII(ModuleTriple, Options->VecLib);
684
685 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
686 if (DisableSimplifyLibCalls)
687 TLII.disableAllFunctions();
688 else {
689 // Disable individual builtin functions in TargetLibraryInfo.
690 LibFunc F;
691 for (const std::string &FuncName : DisableBuiltins) {
692 if (TLII.getLibFunc(funcName: FuncName, F))
693 TLII.setUnavailable(F);
694 else {
695 errs() << argv[0] << ": cannot disable nonexistent builtin function "
696 << FuncName << '\n';
697 return 1;
698 }
699 }
700
701 for (const std::string &FuncName : EnableBuiltins) {
702 if (TLII.getLibFunc(funcName: FuncName, F))
703 TLII.setAvailable(F);
704 else {
705 errs() << argv[0] << ": cannot enable nonexistent builtin function "
706 << FuncName << '\n';
707 return 1;
708 }
709 }
710 }
711
712 if (UseNPM) {
713 if (legacy::debugPassSpecified()) {
714 errs() << "-debug-pass does not work with the new PM, either use "
715 "-debug-pass-manager, or use the legacy PM\n";
716 return 1;
717 }
718 auto NumOLevel = OptLevelO0 + OptLevelO1 + OptLevelO2 + OptLevelO3 +
719 OptLevelOs + OptLevelOz;
720 if (NumOLevel > 1) {
721 errs() << "Cannot specify multiple -O#\n";
722 return 1;
723 }
724 if (NumOLevel > 0 && (PassPipeline.getNumOccurrences() > 0)) {
725 errs() << "Cannot specify -O# and --passes=/--foo-pass, use "
726 "-passes='default<O#>,other-pass'\n";
727 return 1;
728 }
729 std::string Pipeline = PassPipeline;
730
731 if (OptLevelO0)
732 Pipeline = "default<O0>";
733 if (OptLevelO1)
734 Pipeline = "default<O1>";
735 if (OptLevelO2)
736 Pipeline = "default<O2>";
737 if (OptLevelO3)
738 Pipeline = "default<O3>";
739 if (OptLevelOs)
740 Pipeline = "default<Os>";
741 if (OptLevelOz)
742 Pipeline = "default<Oz>";
743 OutputKind OK = OK_NoOutput;
744 if (!NoOutput)
745 OK = OutputAssembly
746 ? OK_OutputAssembly
747 : (OutputThinLTOBC ? OK_OutputThinLTOBitcode : OK_OutputBitcode);
748
749 VerifierKind VK = VerifierKind::InputOutput;
750 if (NoVerify)
751 VK = VerifierKind::None;
752 else if (VerifyEach)
753 VK = VerifierKind::EachPass;
754
755 // The user has asked to use the new pass manager and provided a pipeline
756 // string. Hand off the rest of the functionality to the new code for that
757 // layer.
758 if (!runPassPipeline(
759 Arg0: argv[0], M&: *M, TM: TM.get(), TLII: &TLII, Out: Out.get(), ThinLinkOut: ThinLinkOut.get(),
760 OptRemarkFile: RemarksFile.get(), PassPipeline: Pipeline, PassPlugins: PluginList, PassBuilderCallbacks, OK,
761 VK, /* ShouldPreserveAssemblyUseListOrder */ false,
762 /* ShouldPreserveBitcodeUseListOrder */ true, EmitSummaryIndex,
763 EmitModuleHash, EnableDebugify, VerifyDIPreserve: VerifyDebugInfoPreserve,
764 EnableProfcheck: EnableProfileVerification, UnifiedLTO))
765 return 1;
766 return codegen::MaybeSaveStatistics(OutputFilename, ToolName: "opt");
767 }
768
769 if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz ||
770 OptLevelO3) {
771 errs() << "Cannot use -O# with legacy PM.\n";
772 return 1;
773 }
774 if (EmitSummaryIndex) {
775 errs() << "Cannot use -module-summary with legacy PM.\n";
776 return 1;
777 }
778 if (EmitModuleHash) {
779 errs() << "Cannot use -module-hash with legacy PM.\n";
780 return 1;
781 }
782 if (OutputThinLTOBC) {
783 errs() << "Cannot use -thinlto-bc with legacy PM.\n";
784 return 1;
785 }
786 // Create a PassManager to hold and optimize the collection of passes we are
787 // about to build. If the -debugify-each option is set, wrap each pass with
788 // the (-check)-debugify passes.
789 DebugifyCustomPassManager Passes;
790 DebugifyStatsMap DIStatsMap;
791 DebugInfoPerPass DebugInfoBeforePass;
792 if (DebugifyEach) {
793 Passes.setDebugifyMode(DebugifyMode::SyntheticDebugInfo);
794 Passes.setDIStatsMap(DIStatsMap);
795 } else if (VerifyEachDebugInfoPreserve) {
796 Passes.setDebugifyMode(DebugifyMode::OriginalDebugInfo);
797 Passes.setDebugInfoBeforePass(DebugInfoBeforePass);
798 if (!VerifyDIPreserveExport.empty())
799 Passes.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport);
800 }
801
802 bool AddOneTimeDebugifyPasses =
803 (EnableDebugify && !DebugifyEach) ||
804 (VerifyDebugInfoPreserve && !VerifyEachDebugInfoPreserve);
805
806 Passes.add(P: new TargetLibraryInfoWrapperPass(TLII));
807 Passes.add(P: new RuntimeLibraryInfoWrapper(
808 ModuleTriple, Options->ExceptionModel, Options->FloatABIType,
809 Options->EABIVersion, Options->MCOptions.ABIName, Options->VecLib));
810
811 // Add internal analysis passes from the target machine.
812 Passes.add(P: createTargetTransformInfoWrapperPass(TIRA: TM ? TM->getTargetIRAnalysis()
813 : TargetIRAnalysis()));
814
815 if (AddOneTimeDebugifyPasses) {
816 if (EnableDebugify) {
817 Passes.setDIStatsMap(DIStatsMap);
818 Passes.add(P: createDebugifyModulePass());
819 } else if (VerifyDebugInfoPreserve) {
820 Passes.setDebugInfoBeforePass(DebugInfoBeforePass);
821 Passes.add(P: createDebugifyModulePass(Mode: DebugifyMode::OriginalDebugInfo, NameOfWrappedPass: "",
822 DebugInfoBeforePass: &(Passes.getDebugInfoPerPass())));
823 }
824 }
825
826 if (TM) {
827 Pass *TPC = TM->createPassConfig(PM&: Passes);
828 if (!TPC) {
829 errs() << "Target Machine pass config creation failed.\n";
830 return 1;
831 }
832 Passes.add(P: TPC);
833 }
834
835 // Create a new optimization pass for each one specified on the command line.
836 for (const PassInfo *PassInf : PassList) {
837 if (PassInf->getNormalCtor()) {
838 Pass *P = PassInf->getNormalCtor()();
839 if (P) {
840 // Add the pass to the pass manager.
841 Passes.add(P);
842 // If we are verifying all of the intermediate steps, add the verifier.
843 if (VerifyEach)
844 Passes.add(P: createVerifierPass());
845 }
846 } else {
847 errs() << argv[0] << ": cannot create pass: " << PassInf->getPassName()
848 << "\n";
849 }
850 }
851
852 // Check that the module is well formed on completion of optimization
853 if (!NoVerify && !VerifyEach)
854 Passes.add(P: createVerifierPass());
855
856 if (AddOneTimeDebugifyPasses) {
857 if (EnableDebugify)
858 Passes.add(P: createCheckDebugifyModulePass(Strip: false));
859 else if (VerifyDebugInfoPreserve) {
860 if (!VerifyDIPreserveExport.empty())
861 Passes.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport);
862 Passes.add(P: createCheckDebugifyModulePass(
863 Strip: false, NameOfWrappedPass: "", StatsMap: nullptr, Mode: DebugifyMode::OriginalDebugInfo,
864 DebugInfoBeforePass: &(Passes.getDebugInfoPerPass()), OrigDIVerifyBugsReportFilePath: VerifyDIPreserveExport));
865 }
866 }
867
868 // In run twice mode, we want to make sure the output is bit-by-bit
869 // equivalent if we run the pass manager again, so setup two buffers and
870 // a stream to write to them. Note that llc does something similar and it
871 // may be worth to abstract this out in the future.
872 SmallVector<char, 0> Buffer;
873 SmallVector<char, 0> FirstRunBuffer;
874 std::unique_ptr<raw_svector_ostream> BOS;
875 raw_ostream *OS = nullptr;
876
877 const bool ShouldEmitOutput = !NoOutput;
878
879 // Write bitcode or assembly to the output as the last step...
880 if (ShouldEmitOutput || RunTwice) {
881 assert(Out);
882 OS = &Out->os();
883 if (RunTwice) {
884 BOS = std::make_unique<raw_svector_ostream>(args&: Buffer);
885 OS = BOS.get();
886 }
887 if (OutputAssembly)
888 Passes.add(P: createPrintModulePass(
889 OS&: *OS, Banner: "", /* ShouldPreserveAssemblyUseListOrder */ ShouldPreserveUseListOrder: false));
890 else
891 Passes.add(P: createBitcodeWriterPass(
892 Str&: *OS, /* ShouldPreserveBitcodeUseListOrder */ ShouldPreserveUseListOrder: true));
893 }
894
895 // Before executing passes, print the final values of the LLVM options.
896 cl::PrintOptionValues();
897
898 if (!RunTwice) {
899 // Now that we have all of the passes ready, run them.
900 Passes.run(M&: *M);
901 } else {
902 // If requested, run all passes twice with the same pass manager to catch
903 // bugs caused by persistent state in the passes.
904 std::unique_ptr<Module> M2(CloneModule(M: *M));
905 // Run all passes on the original module first, so the second run processes
906 // the clone to catch CloneModule bugs.
907 Passes.run(M&: *M);
908 FirstRunBuffer = Buffer;
909 Buffer.clear();
910
911 Passes.run(M&: *M2);
912
913 // Compare the two outputs and make sure they're the same
914 assert(Out);
915 if (Buffer.size() != FirstRunBuffer.size() ||
916 (memcmp(s1: Buffer.data(), s2: FirstRunBuffer.data(), n: Buffer.size()) != 0)) {
917 errs()
918 << "Running the pass manager twice changed the output.\n"
919 "Writing the result of the second run to the specified output.\n"
920 "To generate the one-run comparison binary, just run without\n"
921 "the compile-twice option\n";
922 if (ShouldEmitOutput) {
923 Out->os() << BOS->str();
924 Out->keep();
925 }
926 if (RemarksFile)
927 RemarksFile->keep();
928 return 1;
929 }
930 if (ShouldEmitOutput)
931 Out->os() << BOS->str();
932 }
933
934 if (DebugifyEach && !DebugifyExport.empty())
935 exportDebugifyStats(Path: DebugifyExport, Map: Passes.getDebugifyStatsMap());
936
937 // Declare success.
938 if (!NoOutput)
939 Out->keep();
940
941 if (RemarksFile)
942 RemarksFile->keep();
943
944 if (ThinLinkOut)
945 ThinLinkOut->keep();
946
947 return codegen::MaybeSaveStatistics(OutputFilename, ToolName: "opt");
948}
949