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