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