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