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 initializeExpandMemCmpLegacyPassPass(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 TimeTracerRAII TimeTracer(argv[0]);
492
493 SMDiagnostic Err;
494
495 Context.setDiscardValueNames(DiscardValueNames);
496 if (!DisableDITypeMap)
497 Context.enableDebugTypeODRUniquing();
498
499 Expected<LLVMRemarkFileHandle> RemarksFileOrErr =
500 setupLLVMOptimizationRemarks(Context, RemarksFilename, RemarksPasses,
501 RemarksFormat, RemarksWithHotness,
502 RemarksHotnessThreshold);
503 if (Error E = RemarksFileOrErr.takeError()) {
504 errs() << toString(E: std::move(E)) << '\n';
505 return 1;
506 }
507 LLVMRemarkFileHandle RemarksFile = std::move(*RemarksFileOrErr);
508
509 codegen::MaybeEnableStatistics();
510
511 StringRef ABIName = mc::getABIName(); // FIXME: Handle module flag.
512
513 // Load the input module...
514 auto SetDataLayout = [&](StringRef IRTriple,
515 StringRef IRLayout) -> std::optional<std::string> {
516 // Data layout specified on the command line has the highest priority.
517 if (!ClDataLayout.empty())
518 return ClDataLayout;
519 // If an explicit data layout is already defined in the IR, don't infer.
520 if (!IRLayout.empty())
521 return std::nullopt;
522
523 // If an explicit triple was specified (either in the IR or on the
524 // command line), use that to infer the default data layout. However, the
525 // command line target triple should override the IR file target triple.
526 std::string TripleStr =
527 TargetTriple.empty() ? IRTriple.str() : Triple::normalize(Str: TargetTriple);
528 // If the triple string is still empty, we don't fall back to
529 // sys::getDefaultTargetTriple() since we do not want to have differing
530 // behaviour dependent on the configured default triple. Therefore, if the
531 // user did not pass -mtriple or define an explicit triple/datalayout in
532 // the IR, we should default to an empty (default) DataLayout.
533 if (TripleStr.empty())
534 return std::nullopt;
535
536 Triple TT(TripleStr);
537
538 std::string Str = TT.computeDataLayout(ABIName);
539 if (Str.empty()) {
540 errs() << argv[0]
541 << ": warning: failed to infer data layout from target triple\n";
542 return std::nullopt;
543 }
544 return Str;
545 };
546 std::unique_ptr<Module> M;
547 if (NoUpgradeDebugInfo)
548 M = parseAssemblyFileWithIndexNoUpgradeDebugInfo(
549 Filename: InputFilename, Err, Context, Slots: nullptr, DataLayoutCallback: SetDataLayout)
550 .Mod;
551 else
552 M = parseIRFile(Filename: InputFilename, Err, Context,
553 Callbacks: ParserCallbacks(SetDataLayout));
554
555 if (!M) {
556 Err.print(ProgName: argv[0], S&: errs());
557 return 1;
558 }
559
560 // Strip debug info before running the verifier.
561 if (StripDebug)
562 StripDebugInfo(M&: *M);
563
564 // Erase module-level named metadata, if requested.
565 if (StripNamedMetadata) {
566 while (!M->named_metadata_empty()) {
567 NamedMDNode *NMD = &*M->named_metadata_begin();
568 M->eraseNamedMetadata(NMD);
569 }
570 }
571
572 // If we are supposed to override the target triple, do so now.
573 if (!TargetTriple.empty())
574 M->setTargetTriple(Triple(Triple::normalize(Str: TargetTriple)));
575
576 // Immediately run the verifier to catch any problems before starting up the
577 // pass pipelines. Otherwise we can crash on broken code during
578 // doInitialization().
579 if (!NoVerify && verifyModule(M: *M, OS: &errs())) {
580 errs() << argv[0] << ": " << InputFilename
581 << ": error: input module is broken!\n";
582 return 1;
583 }
584
585 // Enable testing of whole program devirtualization on this module by invoking
586 // the facility for updating public visibility to linkage unit visibility when
587 // specified by an internal option. This is normally done during LTO which is
588 // not performed via opt.
589 updateVCallVisibilityInModule(
590 M&: *M,
591 /*WholeProgramVisibilityEnabledInLTO=*/false,
592 // FIXME: These need linker information via a
593 // TBD new interface.
594 /*DynamicExportSymbols=*/{},
595 /*ValidateAllVtablesHaveTypeInfos=*/false,
596 /*IsVisibleToRegularObj=*/[](StringRef) { return true; });
597
598 // Figure out what stream we are supposed to write to...
599 std::unique_ptr<ToolOutputFile> Out;
600 std::unique_ptr<ToolOutputFile> ThinLinkOut;
601 if (NoOutput) {
602 if (!OutputFilename.empty())
603 errs() << "WARNING: The -o (output filename) option is ignored when\n"
604 "the --disable-output option is used.\n";
605 } else {
606 // Default to standard output.
607 if (OutputFilename.empty())
608 OutputFilename = "-";
609
610 std::error_code EC;
611 sys::fs::OpenFlags Flags =
612 OutputAssembly ? sys::fs::OF_TextWithCRLF : sys::fs::OF_None;
613 Out.reset(p: new ToolOutputFile(OutputFilename, EC, Flags));
614 if (EC) {
615 errs() << EC.message() << '\n';
616 return 1;
617 }
618
619 if (!ThinLinkBitcodeFile.empty()) {
620 ThinLinkOut.reset(
621 p: new ToolOutputFile(ThinLinkBitcodeFile, EC, sys::fs::OF_None));
622 if (EC) {
623 errs() << EC.message() << '\n';
624 return 1;
625 }
626 }
627 }
628
629 Triple ModuleTriple(M->getTargetTriple());
630 std::string CPUStr, FeaturesStr;
631 std::unique_ptr<TargetMachine> TM;
632 if (ModuleTriple.getArch()) {
633 CPUStr = codegen::getCPUStr();
634 FeaturesStr = codegen::getFeaturesStr();
635 Expected<std::unique_ptr<TargetMachine>> ExpectedTM =
636 codegen::createTargetMachineForTriple(TargetTriple: ModuleTriple.str(),
637 OptLevel: GetCodeGenOptLevel());
638 if (auto E = ExpectedTM.takeError()) {
639 errs() << argv[0] << ": WARNING: failed to create target machine for '"
640 << ModuleTriple.str() << "': " << toString(E: std::move(E)) << "\n";
641 } else {
642 TM = std::move(*ExpectedTM);
643 }
644 } else if (ModuleTriple.getArchName() != "unknown" &&
645 ModuleTriple.getArchName() != "") {
646 errs() << argv[0] << ": unrecognized architecture '"
647 << ModuleTriple.getArchName() << "' provided.\n";
648 return 1;
649 }
650
651 TargetOptions CodeGenFlagsOptions;
652 const TargetOptions *Options = TM ? &TM->Options : &CodeGenFlagsOptions;
653 if (!TM) {
654 CodeGenFlagsOptions =
655 codegen::InitTargetOptionsFromCodeGenFlags(TheTriple: ModuleTriple);
656 }
657
658 // Override function attributes based on CPUStr, FeaturesStr, and command line
659 // flags.
660 codegen::setFunctionAttributes(CPU: CPUStr, Features: FeaturesStr, M&: *M);
661
662 // If the output is set to be emitted to standard out, and standard out is a
663 // console, print out a warning message and refuse to do it. We don't
664 // impress anyone by spewing tons of binary goo to a terminal.
665 if (!Force && !NoOutput && !OutputAssembly)
666 if (CheckBitcodeOutputToConsole(stream_to_check&: Out->os()))
667 NoOutput = true;
668
669 if (OutputThinLTOBC) {
670 M->addModuleFlag(Behavior: Module::Error, Key: "EnableSplitLTOUnit", Val: SplitLTOUnit);
671 if (UnifiedLTO)
672 M->addModuleFlag(Behavior: Module::Error, Key: "UnifiedLTO", Val: 1);
673 }
674
675 // Add an appropriate TargetLibraryInfo pass for the module's triple.
676 TargetLibraryInfoImpl TLII(ModuleTriple, Options->VecLib);
677
678 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
679 if (DisableSimplifyLibCalls)
680 TLII.disableAllFunctions();
681 else {
682 // Disable individual builtin functions in TargetLibraryInfo.
683 LibFunc F;
684 for (const std::string &FuncName : DisableBuiltins) {
685 if (TLII.getLibFunc(funcName: FuncName, F))
686 TLII.setUnavailable(F);
687 else {
688 errs() << argv[0] << ": cannot disable nonexistent builtin function "
689 << FuncName << '\n';
690 return 1;
691 }
692 }
693
694 for (const std::string &FuncName : EnableBuiltins) {
695 if (TLII.getLibFunc(funcName: FuncName, F))
696 TLII.setAvailable(F);
697 else {
698 errs() << argv[0] << ": cannot enable nonexistent builtin function "
699 << FuncName << '\n';
700 return 1;
701 }
702 }
703 }
704
705 if (UseNPM) {
706 if (legacy::debugPassSpecified()) {
707 errs() << "-debug-pass does not work with the new PM, either use "
708 "-debug-pass-manager, or use the legacy PM\n";
709 return 1;
710 }
711 auto NumOLevel = OptLevelO0 + OptLevelO1 + OptLevelO2 + OptLevelO3 +
712 OptLevelOs + OptLevelOz;
713 if (NumOLevel > 1) {
714 errs() << "Cannot specify multiple -O#\n";
715 return 1;
716 }
717 if (NumOLevel > 0 && (PassPipeline.getNumOccurrences() > 0)) {
718 errs() << "Cannot specify -O# and --passes=/--foo-pass, use "
719 "-passes='default<O#>,other-pass'\n";
720 return 1;
721 }
722 std::string Pipeline = PassPipeline;
723
724 if (OptLevelO0)
725 Pipeline = "default<O0>";
726 if (OptLevelO1)
727 Pipeline = "default<O1>";
728 if (OptLevelO2)
729 Pipeline = "default<O2>";
730 if (OptLevelO3)
731 Pipeline = "default<O3>";
732 if (OptLevelOs)
733 Pipeline = "default<Os>";
734 if (OptLevelOz)
735 Pipeline = "default<Oz>";
736 OutputKind OK = OK_NoOutput;
737 if (!NoOutput)
738 OK = OutputAssembly
739 ? OK_OutputAssembly
740 : (OutputThinLTOBC ? OK_OutputThinLTOBitcode : OK_OutputBitcode);
741
742 VerifierKind VK = VerifierKind::InputOutput;
743 if (NoVerify)
744 VK = VerifierKind::None;
745 else if (VerifyEach)
746 VK = VerifierKind::EachPass;
747
748 // The user has asked to use the new pass manager and provided a pipeline
749 // string. Hand off the rest of the functionality to the new code for that
750 // layer.
751 if (!runPassPipeline(
752 Arg0: argv[0], M&: *M, TM: TM.get(), TLII: &TLII, Out: Out.get(), ThinLinkOut: ThinLinkOut.get(),
753 OptRemarkFile: RemarksFile.get(), PassPipeline: Pipeline, PassPlugins: PluginList, PassBuilderCallbacks, OK,
754 VK, /* ShouldPreserveAssemblyUseListOrder */ false,
755 /* ShouldPreserveBitcodeUseListOrder */ true, EmitSummaryIndex,
756 EmitModuleHash, EnableDebugify, VerifyDIPreserve: VerifyDebugInfoPreserve,
757 EnableProfcheck: EnableProfileVerification, UnifiedLTO))
758 return 1;
759 return codegen::MaybeSaveStatistics(OutputFilename, ToolName: "opt");
760 }
761
762 if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz ||
763 OptLevelO3) {
764 errs() << "Cannot use -O# with legacy PM.\n";
765 return 1;
766 }
767 if (EmitSummaryIndex) {
768 errs() << "Cannot use -module-summary with legacy PM.\n";
769 return 1;
770 }
771 if (EmitModuleHash) {
772 errs() << "Cannot use -module-hash with legacy PM.\n";
773 return 1;
774 }
775 if (OutputThinLTOBC) {
776 errs() << "Cannot use -thinlto-bc with legacy PM.\n";
777 return 1;
778 }
779 // Create a PassManager to hold and optimize the collection of passes we are
780 // about to build. If the -debugify-each option is set, wrap each pass with
781 // the (-check)-debugify passes.
782 DebugifyCustomPassManager Passes;
783 DebugifyStatsMap DIStatsMap;
784 DebugInfoPerPass DebugInfoBeforePass;
785 if (DebugifyEach) {
786 Passes.setDebugifyMode(DebugifyMode::SyntheticDebugInfo);
787 Passes.setDIStatsMap(DIStatsMap);
788 } else if (VerifyEachDebugInfoPreserve) {
789 Passes.setDebugifyMode(DebugifyMode::OriginalDebugInfo);
790 Passes.setDebugInfoBeforePass(DebugInfoBeforePass);
791 if (!VerifyDIPreserveExport.empty())
792 Passes.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport);
793 }
794
795 bool AddOneTimeDebugifyPasses =
796 (EnableDebugify && !DebugifyEach) ||
797 (VerifyDebugInfoPreserve && !VerifyEachDebugInfoPreserve);
798
799 Passes.add(P: new TargetLibraryInfoWrapperPass(TLII));
800 Passes.add(P: new RuntimeLibraryInfoWrapper(
801 ModuleTriple, Options->ExceptionModel, Options->FloatABIType,
802 Options->EABIVersion, Options->MCOptions.ABIName, Options->VecLib));
803
804 // Add internal analysis passes from the target machine.
805 Passes.add(P: createTargetTransformInfoWrapperPass(TIRA: TM ? TM->getTargetIRAnalysis()
806 : TargetIRAnalysis()));
807
808 if (AddOneTimeDebugifyPasses) {
809 if (EnableDebugify) {
810 Passes.setDIStatsMap(DIStatsMap);
811 Passes.add(P: createDebugifyModulePass());
812 } else if (VerifyDebugInfoPreserve) {
813 Passes.setDebugInfoBeforePass(DebugInfoBeforePass);
814 Passes.add(P: createDebugifyModulePass(Mode: DebugifyMode::OriginalDebugInfo, NameOfWrappedPass: "",
815 DebugInfoBeforePass: &(Passes.getDebugInfoPerPass())));
816 }
817 }
818
819 if (TM) {
820 Pass *TPC = TM->createPassConfig(PM&: Passes);
821 if (!TPC) {
822 errs() << "Target Machine pass config creation failed.\n";
823 return 1;
824 }
825 Passes.add(P: TPC);
826 }
827
828 // Create a new optimization pass for each one specified on the command line.
829 for (const PassInfo *PassInf : PassList) {
830 if (PassInf->getNormalCtor()) {
831 Pass *P = PassInf->getNormalCtor()();
832 if (P) {
833 // Add the pass to the pass manager.
834 Passes.add(P);
835 // If we are verifying all of the intermediate steps, add the verifier.
836 if (VerifyEach)
837 Passes.add(P: createVerifierPass());
838 }
839 } else {
840 errs() << argv[0] << ": cannot create pass: " << PassInf->getPassName()
841 << "\n";
842 }
843 }
844
845 // Check that the module is well formed on completion of optimization
846 if (!NoVerify && !VerifyEach)
847 Passes.add(P: createVerifierPass());
848
849 if (AddOneTimeDebugifyPasses) {
850 if (EnableDebugify)
851 Passes.add(P: createCheckDebugifyModulePass(Strip: false));
852 else if (VerifyDebugInfoPreserve) {
853 if (!VerifyDIPreserveExport.empty())
854 Passes.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport);
855 Passes.add(P: createCheckDebugifyModulePass(
856 Strip: false, NameOfWrappedPass: "", StatsMap: nullptr, Mode: DebugifyMode::OriginalDebugInfo,
857 DebugInfoBeforePass: &(Passes.getDebugInfoPerPass()), OrigDIVerifyBugsReportFilePath: VerifyDIPreserveExport));
858 }
859 }
860
861 // In run twice mode, we want to make sure the output is bit-by-bit
862 // equivalent if we run the pass manager again, so setup two buffers and
863 // a stream to write to them. Note that llc does something similar and it
864 // may be worth to abstract this out in the future.
865 SmallVector<char, 0> Buffer;
866 SmallVector<char, 0> FirstRunBuffer;
867 std::unique_ptr<raw_svector_ostream> BOS;
868 raw_ostream *OS = nullptr;
869
870 const bool ShouldEmitOutput = !NoOutput;
871
872 // Write bitcode or assembly to the output as the last step...
873 if (ShouldEmitOutput || RunTwice) {
874 assert(Out);
875 OS = &Out->os();
876 if (RunTwice) {
877 BOS = std::make_unique<raw_svector_ostream>(args&: Buffer);
878 OS = BOS.get();
879 }
880 if (OutputAssembly)
881 Passes.add(P: createPrintModulePass(
882 OS&: *OS, Banner: "", /* ShouldPreserveAssemblyUseListOrder */ ShouldPreserveUseListOrder: false));
883 else
884 Passes.add(P: createBitcodeWriterPass(
885 Str&: *OS, /* ShouldPreserveBitcodeUseListOrder */ ShouldPreserveUseListOrder: true));
886 }
887
888 // Before executing passes, print the final values of the LLVM options.
889 cl::PrintOptionValues();
890
891 if (!RunTwice) {
892 // Now that we have all of the passes ready, run them.
893 Passes.run(M&: *M);
894 } else {
895 // If requested, run all passes twice with the same pass manager to catch
896 // bugs caused by persistent state in the passes.
897 std::unique_ptr<Module> M2(CloneModule(M: *M));
898 // Run all passes on the original module first, so the second run processes
899 // the clone to catch CloneModule bugs.
900 Passes.run(M&: *M);
901 FirstRunBuffer = Buffer;
902 Buffer.clear();
903
904 Passes.run(M&: *M2);
905
906 // Compare the two outputs and make sure they're the same
907 assert(Out);
908 if (Buffer.size() != FirstRunBuffer.size() ||
909 (memcmp(s1: Buffer.data(), s2: FirstRunBuffer.data(), n: Buffer.size()) != 0)) {
910 errs()
911 << "Running the pass manager twice changed the output.\n"
912 "Writing the result of the second run to the specified output.\n"
913 "To generate the one-run comparison binary, just run without\n"
914 "the compile-twice option\n";
915 if (ShouldEmitOutput) {
916 Out->os() << BOS->str();
917 Out->keep();
918 }
919 if (RemarksFile)
920 RemarksFile->keep();
921 return 1;
922 }
923 if (ShouldEmitOutput)
924 Out->os() << BOS->str();
925 }
926
927 if (DebugifyEach && !DebugifyExport.empty())
928 exportDebugifyStats(Path: DebugifyExport, Map: Passes.getDebugifyStatsMap());
929
930 // Declare success.
931 if (!NoOutput)
932 Out->keep();
933
934 if (RemarksFile)
935 RemarksFile->keep();
936
937 if (ThinLinkOut)
938 ThinLinkOut->keep();
939
940 return codegen::MaybeSaveStatistics(OutputFilename, ToolName: "opt");
941}
942