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