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