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/PluginLoader.h"
48#include "llvm/Support/SourceMgr.h"
49#include "llvm/Support/SystemUtils.h"
50#include "llvm/Support/TargetSelect.h"
51#include "llvm/Support/TimeProfiler.h"
52#include "llvm/Support/ToolOutputFile.h"
53#include "llvm/Support/YAMLTraits.h"
54#include "llvm/Target/TargetMachine.h"
55#include "llvm/TargetParser/Host.h"
56#include "llvm/TargetParser/SubtargetFeature.h"
57#include "llvm/TargetParser/Triple.h"
58#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
59#include "llvm/Transforms/Utils/AssignGUID.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 // Enable debug stream buffering.
404 EnableDebugBuffering = true;
405
406 InitializeAllTargets();
407 InitializeAllTargetMCs();
408 InitializeAllAsmPrinters();
409 InitializeAllAsmParsers();
410
411 // Initialize passes
412 PassRegistry &Registry = *PassRegistry::getPassRegistry();
413 initializeCore(Registry);
414 initializeScalarOpts(Registry);
415 initializeVectorization(Registry);
416 initializeIPO(Registry);
417 initializeAnalysis(Registry);
418 initializeTransformUtils(Registry);
419 initializeInstCombine(Registry);
420 initializeTarget(Registry);
421 // For codegen passes, only passes that do IR to IR transformation are
422 // supported.
423 initializeExpandIRInstsLegacyPassPass(Registry);
424 initializeScalarizeMaskedMemIntrinLegacyPassPass(Registry);
425 initializeSelectOptimizePass(Registry);
426 initializeInlineAsmPreparePass(Registry);
427 initializeCodeGenPrepareLegacyPassPass(Registry);
428 initializeAtomicExpandLegacyPass(Registry);
429 initializeWinEHPreparePass(Registry);
430 initializeDwarfEHPrepareLegacyPassPass(Registry);
431 initializeSafeStackLegacyPassPass(Registry);
432 initializeSjLjEHPreparePass(Registry);
433 initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
434 initializeGlobalMergePass(Registry);
435 initializeIndirectBrExpandLegacyPassPass(Registry);
436 initializeInterleavedLoadCombinePass(Registry);
437 initializeInterleavedAccessPass(Registry);
438 initializePostInlineEntryExitInstrumenterPass(Registry);
439 initializeUnreachableBlockElimLegacyPassPass(Registry);
440 initializeExpandReductionsPass(Registry);
441 initializeWasmEHPreparePass(Registry);
442 initializeWriteBitcodePassPass(Registry);
443 initializeReplaceWithVeclibLegacyPass(Registry);
444 initializeJMCInstrumenterPass(Registry);
445
446 SmallVector<PassPlugin, 1> PluginList;
447 PassPlugins.setCallback([&](const std::string &PluginPath) {
448 auto Plugin = PassPlugin::Load(Filename: PluginPath);
449 if (!Plugin)
450 reportFatalUsageError(Err: Plugin.takeError());
451 PluginList.emplace_back(Args&: Plugin.get());
452 });
453
454 // Register the Target and CPU printer for --version.
455 cl::AddExtraVersionPrinter(func: sys::printDefaultTargetAndDetectedCPU);
456
457 cl::ParseCommandLineOptions(
458 argc, argv, Overview: "llvm .bc -> .bc modular optimizer and analysis printer\n");
459
460 LLVMContext Context;
461
462 // TODO: remove shouldForceLegacyPM().
463 const bool UseNPM =
464 !shouldForceLegacyPM() || PassPipeline.getNumOccurrences() > 0;
465
466 if (UseNPM && !PassList.empty()) {
467 errs() << "The `opt -passname` syntax for the new pass manager is "
468 "not supported, please use `opt -passes=<pipeline>` (or the `-p` "
469 "alias for a more concise version).\n";
470 errs() << "See https://llvm.org/docs/NewPassManager.html#invoking-opt "
471 "for more details on the pass pipeline syntax.\n\n";
472 return 1;
473 }
474
475 if (!UseNPM && PluginList.size()) {
476 errs() << argv[0] << ": " << PassPlugins.ArgStr
477 << " specified with legacy PM.\n";
478 return 1;
479 }
480
481 // FIXME: once the legacy PM code is deleted, move runPassPipeline() here and
482 // construct the PassBuilder before parsing IR so we can reuse the same
483 // PassBuilder for print passes.
484 if (PrintPasses) {
485 printPasses(OS&: outs());
486 return 0;
487 }
488
489 // If user just wants to list available options, skip module loading.
490 auto MAttrs = codegen::getMAttrs();
491 std::string CPUStr = codegen::getCPUStr();
492 std::string TuneCPUStr = codegen::getTuneCPUStr();
493 bool SkipModule =
494 CPUStr == "help" || TuneCPUStr == "help" || is_contained(Range&: MAttrs, Element: "help");
495 if (SkipModule) {
496 Triple TheTriple;
497 if (!TargetTriple.empty())
498 TheTriple = Triple(Triple::normalize(Str: TargetTriple));
499 else
500 TheTriple = Triple(sys::getDefaultTargetTriple());
501
502 std::string Error;
503 const Target *TheTarget =
504 TargetRegistry::lookupTarget(ArchName: codegen::getMArch(), TheTriple, Error);
505 if (!TheTarget) {
506 errs() << argv[0] << ": " << Error << "\n";
507 return 1;
508 }
509
510 // Pass "help" as CPU for -mtune=help
511 std::string SkipModuleCPU = (TuneCPUStr == "help" ? "help" : CPUStr);
512 TargetOptions Options =
513 codegen::InitTargetOptionsFromCodeGenFlags(TheTriple);
514 // Create the target machine just to print the help info. Use unique_ptr
515 // to avoid a memory leak.
516 std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
517 TT: TheTriple, CPU: SkipModuleCPU, Features: codegen::getFeaturesStr(), Options,
518 RM: codegen::getExplicitRelocModel(), CM: codegen::getExplicitCodeModel(),
519 OL: GetCodeGenOptLevel()));
520 if (!TM) {
521 errs() << argv[0] << ": could not allocate target machine\n";
522 return 1;
523 }
524
525 // If we don't have a module then just exit now. We do this down
526 // here since the CPU/Feature help is underneath the target machine
527 // creation.
528 return 0;
529 }
530
531 TimeTracerRAII TimeTracer(argv[0]);
532
533 SMDiagnostic Err;
534
535 Context.setDiscardValueNames(DiscardValueNames);
536 if (!DisableDITypeMap)
537 Context.enableDebugTypeODRUniquing();
538
539 Expected<LLVMRemarkFileHandle> RemarksFileOrErr =
540 setupLLVMOptimizationRemarks(Context, RemarksFilename, RemarksPasses,
541 RemarksFormat, RemarksWithHotness,
542 RemarksHotnessThreshold);
543 if (Error E = RemarksFileOrErr.takeError()) {
544 errs() << toString(E: std::move(E)) << '\n';
545 return 1;
546 }
547 LLVMRemarkFileHandle RemarksFile = std::move(*RemarksFileOrErr);
548
549 codegen::MaybeEnableStatistics();
550
551 StringRef ABIName = mc::getABIName(); // FIXME: Handle module flag.
552
553 // Load the input module...
554 auto SetDataLayout = [&](StringRef IRTriple,
555 StringRef IRLayout) -> std::optional<std::string> {
556 // Data layout specified on the command line has the highest priority.
557 if (!ClDataLayout.empty())
558 return ClDataLayout;
559 // If an explicit data layout is already defined in the IR, don't infer.
560 if (!IRLayout.empty())
561 return std::nullopt;
562
563 // If an explicit triple was specified (either in the IR or on the
564 // command line), use that to infer the default data layout. However, the
565 // command line target triple should override the IR file target triple.
566 std::string TripleStr =
567 TargetTriple.empty() ? IRTriple.str() : Triple::normalize(Str: TargetTriple);
568 // If the triple string is still empty, we don't fall back to
569 // sys::getDefaultTargetTriple() since we do not want to have differing
570 // behaviour dependent on the configured default triple. Therefore, if the
571 // user did not pass -mtriple or define an explicit triple/datalayout in
572 // the IR, we should default to an empty (default) DataLayout.
573 if (TripleStr.empty())
574 return std::nullopt;
575
576 Triple TT(TripleStr);
577
578 std::string Str = TT.computeDataLayout(ABIName);
579 if (Str.empty()) {
580 errs() << argv[0]
581 << ": warning: failed to infer data layout from target triple\n";
582 return std::nullopt;
583 }
584 return Str;
585 };
586 std::unique_ptr<Module> M;
587 if (NoUpgradeDebugInfo)
588 M = parseAssemblyFileWithIndexNoUpgradeDebugInfo(
589 Filename: InputFilename, Err, Context, Slots: nullptr, DataLayoutCallback: SetDataLayout)
590 .Mod;
591 else
592 M = parseIRFile(Filename: InputFilename, Err, Context,
593 Callbacks: ParserCallbacks(SetDataLayout));
594
595 if (!M) {
596 Err.print(ProgName: argv[0], S&: errs());
597 return 1;
598 }
599
600 // Strip debug info before running the verifier.
601 if (StripDebug)
602 StripDebugInfo(M&: *M);
603
604 // Erase module-level named metadata, if requested.
605 if (StripNamedMetadata) {
606 while (!M->named_metadata_empty()) {
607 NamedMDNode *NMD = &*M->named_metadata_begin();
608 M->eraseNamedMetadata(NMD);
609 }
610 }
611
612 // If we are supposed to override the target triple, do so now.
613 if (!TargetTriple.empty())
614 M->setTargetTriple(Triple(Triple::normalize(Str: TargetTriple)));
615
616 // Immediately run the verifier to catch any problems before starting up the
617 // pass pipelines. Otherwise we can crash on broken code during
618 // doInitialization().
619 if (!NoVerify && verifyModule(M: *M, OS: &errs())) {
620 errs() << argv[0] << ": " << InputFilename
621 << ": error: input module is broken!\n";
622 return 1;
623 }
624
625 // Manually assign GUIDs -- updateVCallVisibilityInModule accesses GUIDs, and
626 // there's no way to specify it in the pass pipeline since this runs before
627 // any pass given on the command line.
628 if (hasWholeProgramVisibility(/*WholeProgramVisibilityEnabledInLTO=*/false))
629 AssignGUIDPass::runOnModule(M&: *M);
630
631 // Enable testing of whole program devirtualization on this module by invoking
632 // the facility for updating public visibility to linkage unit visibility when
633 // specified by an internal option. This is normally done during LTO which is
634 // not performed via opt.
635 updateVCallVisibilityInModule(
636 M&: *M,
637 /*WholeProgramVisibilityEnabledInLTO=*/false,
638 // FIXME: These need linker information via a
639 // TBD new interface.
640 /*DynamicExportSymbols=*/{},
641 /*ValidateAllVtablesHaveTypeInfos=*/false,
642 /*IsVisibleToRegularObj=*/[](StringRef) { return true; });
643
644 // Figure out what stream we are supposed to write to...
645 std::unique_ptr<ToolOutputFile> Out;
646 std::unique_ptr<ToolOutputFile> ThinLinkOut;
647 if (NoOutput) {
648 if (!OutputFilename.empty())
649 errs() << "WARNING: The -o (output filename) option is ignored when\n"
650 "the --disable-output option is used.\n";
651 } else {
652 // Default to standard output.
653 if (OutputFilename.empty())
654 OutputFilename = "-";
655
656 std::error_code EC;
657 sys::fs::OpenFlags Flags =
658 OutputAssembly ? sys::fs::OF_TextWithCRLF : sys::fs::OF_None;
659 Out.reset(p: new ToolOutputFile(OutputFilename, EC, Flags));
660 if (EC) {
661 errs() << EC.message() << '\n';
662 return 1;
663 }
664
665 if (!ThinLinkBitcodeFile.empty()) {
666 ThinLinkOut.reset(
667 p: new ToolOutputFile(ThinLinkBitcodeFile, EC, sys::fs::OF_None));
668 if (EC) {
669 errs() << EC.message() << '\n';
670 return 1;
671 }
672 }
673 }
674
675 Triple ModuleTriple(M->getTargetTriple());
676 // Avoid setting target function attributes if no arch is found, by resetting
677 // them first
678 CPUStr.clear();
679 TuneCPUStr.clear();
680 std::string FeaturesStr;
681 std::unique_ptr<TargetMachine> TM;
682 if (ModuleTriple.getArch()) {
683 CPUStr = codegen::getCPUStr();
684 TuneCPUStr = codegen::getTuneCPUStr();
685 FeaturesStr = codegen::getFeaturesStr();
686 Expected<std::unique_ptr<TargetMachine>> ExpectedTM =
687 codegen::createTargetMachineForTriple(TargetTriple: ModuleTriple,
688 OptLevel: GetCodeGenOptLevel());
689 if (auto E = ExpectedTM.takeError()) {
690 errs() << argv[0] << ": WARNING: failed to create target machine for '"
691 << ModuleTriple.str() << "': " << toString(E: std::move(E)) << "\n";
692 } else {
693 TM = std::move(*ExpectedTM);
694 }
695 } else if (ModuleTriple.getArchName() != "unknown" &&
696 ModuleTriple.getArchName() != "") {
697 errs() << argv[0] << ": unrecognized architecture '"
698 << ModuleTriple.getArchName() << "' provided.\n";
699 return 1;
700 }
701
702 TargetOptions CodeGenFlagsOptions;
703 const TargetOptions *Options = TM ? &TM->Options : &CodeGenFlagsOptions;
704 if (!TM) {
705 CodeGenFlagsOptions =
706 codegen::InitTargetOptionsFromCodeGenFlags(TheTriple: ModuleTriple);
707 }
708
709 // Override function attributes based on CPUStr, TuneCPUStr, FeaturesStr, and
710 // command line flags.
711 codegen::setFunctionAttributes(M&: *M, CPU: CPUStr, Features: FeaturesStr, TuneCPU: TuneCPUStr);
712
713 // If the output is set to be emitted to standard out, and standard out is a
714 // console, print out a warning message and refuse to do it. We don't
715 // impress anyone by spewing tons of binary goo to a terminal.
716 if (!Force && !NoOutput && !OutputAssembly)
717 if (CheckBitcodeOutputToConsole(stream_to_check&: Out->os()))
718 NoOutput = true;
719
720 if (OutputThinLTOBC) {
721 M->addModuleFlag(Behavior: Module::Error, Key: "EnableSplitLTOUnit", Val: SplitLTOUnit);
722 if (UnifiedLTO)
723 M->addModuleFlag(Behavior: Module::Error, Key: "UnifiedLTO", Val: 1);
724 }
725
726 // Add an appropriate TargetLibraryInfo pass for the module's triple.
727 TargetLibraryInfoImpl TLII(ModuleTriple, Options->VecLib);
728
729 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
730 if (DisableSimplifyLibCalls)
731 TLII.disableAllFunctions();
732 else {
733 // Disable individual builtin functions in TargetLibraryInfo.
734 for (const std::string &FuncName : DisableBuiltins) {
735 if (LibFunc F = TLII.getLibFunc(funcName: FuncName))
736 TLII.setUnavailable(F);
737 else {
738 errs() << argv[0] << ": cannot disable nonexistent builtin function "
739 << FuncName << '\n';
740 return 1;
741 }
742 }
743
744 for (const std::string &FuncName : EnableBuiltins) {
745 if (LibFunc F = TLII.getLibFunc(funcName: FuncName))
746 TLII.setAvailable(F);
747 else {
748 errs() << argv[0] << ": cannot enable nonexistent builtin function "
749 << FuncName << '\n';
750 return 1;
751 }
752 }
753 }
754
755 if (UseNPM) {
756 if (legacy::debugPassSpecified()) {
757 errs() << "-debug-pass does not work with the new PM, either use "
758 "-debug-pass-manager, or use the legacy PM\n";
759 return 1;
760 }
761 auto NumOLevel = OptLevelO0 + OptLevelO1 + OptLevelO2 + OptLevelO3 +
762 OptLevelOs + OptLevelOz;
763 if (NumOLevel > 1) {
764 errs() << "Cannot specify multiple -O#\n";
765 return 1;
766 }
767 if (NumOLevel > 0 && (PassPipeline.getNumOccurrences() > 0)) {
768 errs() << "Cannot specify -O# and --passes=/--foo-pass, use "
769 "-passes='default<O#>,other-pass'\n";
770 return 1;
771 }
772 std::string Pipeline = PassPipeline;
773
774 if (OptLevelO0)
775 Pipeline = "default<O0>";
776 if (OptLevelO1)
777 Pipeline = "default<O1>";
778 if (OptLevelO2)
779 Pipeline = "default<O2>";
780 if (OptLevelO3)
781 Pipeline = "default<O3>";
782 if (OptLevelOs)
783 Pipeline = "default<Os>";
784 if (OptLevelOz)
785 Pipeline = "default<Oz>";
786 OutputKind OK = OK_NoOutput;
787 if (!NoOutput)
788 OK = OutputAssembly
789 ? OK_OutputAssembly
790 : (OutputThinLTOBC ? OK_OutputThinLTOBitcode : OK_OutputBitcode);
791
792 VerifierKind VK = VerifierKind::InputOutput;
793 if (NoVerify)
794 VK = VerifierKind::None;
795 else if (VerifyEach)
796 VK = VerifierKind::EachPass;
797
798 // The user has asked to use the new pass manager and provided a pipeline
799 // string. Hand off the rest of the functionality to the new code for that
800 // layer.
801 if (!runPassPipeline(
802 Arg0: argv[0], M&: *M, TM: TM.get(), TLII: &TLII, Out: Out.get(), ThinLinkOut: ThinLinkOut.get(),
803 OptRemarkFile: RemarksFile.get(), PassPipeline: Pipeline, PassPlugins: PluginList, PassBuilderCallbacks, OK,
804 VK, /* ShouldPreserveAssemblyUseListOrder */ false,
805 /* ShouldPreserveBitcodeUseListOrder */ true, EmitSummaryIndex,
806 EmitModuleHash, EnableDebugify, VerifyDIPreserve: VerifyDebugInfoPreserve,
807 EnableProfcheck: EnableProfileVerification, UnifiedLTO))
808 return 1;
809 return codegen::MaybeSaveStatistics(OutputFilename, ToolName: "opt");
810 }
811
812 if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz ||
813 OptLevelO3) {
814 errs() << "Cannot use -O# with legacy PM.\n";
815 return 1;
816 }
817 if (EmitSummaryIndex) {
818 errs() << "Cannot use -module-summary with legacy PM.\n";
819 return 1;
820 }
821 if (EmitModuleHash) {
822 errs() << "Cannot use -module-hash with legacy PM.\n";
823 return 1;
824 }
825 if (OutputThinLTOBC) {
826 errs() << "Cannot use -thinlto-bc with legacy PM.\n";
827 return 1;
828 }
829 // Create a PassManager to hold and optimize the collection of passes we are
830 // about to build. If the -debugify-each option is set, wrap each pass with
831 // the (-check)-debugify passes.
832 DebugifyCustomPassManager Passes;
833 DebugifyStatsMap DIStatsMap;
834 DebugInfoPerPass DebugInfoBeforePass;
835 if (DebugifyEach) {
836 Passes.setDebugifyMode(DebugifyMode::SyntheticDebugInfo);
837 Passes.setDIStatsMap(DIStatsMap);
838 } else if (VerifyEachDebugInfoPreserve) {
839 Passes.setDebugifyMode(DebugifyMode::OriginalDebugInfo);
840 Passes.setDebugInfoBeforePass(DebugInfoBeforePass);
841 if (!VerifyDIPreserveExport.empty())
842 Passes.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport);
843 }
844
845 bool AddOneTimeDebugifyPasses =
846 (EnableDebugify && !DebugifyEach) ||
847 (VerifyDebugInfoPreserve && !VerifyEachDebugInfoPreserve);
848
849 Passes.add(P: new TargetLibraryInfoWrapperPass(TLII));
850 Passes.add(P: new RuntimeLibraryInfoWrapper(Options->MCOptions.ABIName,
851 Options->VecLib));
852
853 // Add internal analysis passes from the target machine.
854 Passes.add(P: createTargetTransformInfoWrapperPass(TIRA: TM ? TM->getTargetIRAnalysis()
855 : TargetIRAnalysis()));
856
857 if (AddOneTimeDebugifyPasses) {
858 if (EnableDebugify) {
859 Passes.setDIStatsMap(DIStatsMap);
860 Passes.add(P: createDebugifyModulePass());
861 } else if (VerifyDebugInfoPreserve) {
862 Passes.setDebugInfoBeforePass(DebugInfoBeforePass);
863 Passes.add(P: createDebugifyModulePass(Mode: DebugifyMode::OriginalDebugInfo, NameOfWrappedPass: "",
864 DebugInfoBeforePass: &(Passes.getDebugInfoPerPass())));
865 }
866 }
867
868 if (TM) {
869 Pass *TPC = TM->createPassConfig(PM&: Passes);
870 if (!TPC) {
871 errs() << "Target Machine pass config creation failed.\n";
872 return 1;
873 }
874 Passes.add(P: TPC);
875 }
876
877 // Create a new optimization pass for each one specified on the command line.
878 for (const PassInfo *PassInf : PassList) {
879 if (PassInf->getNormalCtor()) {
880 Pass *P = PassInf->getNormalCtor()();
881 if (P) {
882 // Add the pass to the pass manager.
883 Passes.add(P);
884 // If we are verifying all of the intermediate steps, add the verifier.
885 if (VerifyEach)
886 Passes.add(P: createVerifierPass());
887 }
888 } else {
889 errs() << argv[0] << ": cannot create pass: " << PassInf->getPassName()
890 << "\n";
891 }
892 }
893
894 // Check that the module is well formed on completion of optimization
895 if (!NoVerify && !VerifyEach)
896 Passes.add(P: createVerifierPass());
897
898 if (AddOneTimeDebugifyPasses) {
899 if (EnableDebugify)
900 Passes.add(P: createCheckDebugifyModulePass(Strip: false));
901 else if (VerifyDebugInfoPreserve) {
902 if (!VerifyDIPreserveExport.empty())
903 Passes.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport);
904 Passes.add(P: createCheckDebugifyModulePass(
905 Strip: false, NameOfWrappedPass: "", StatsMap: nullptr, Mode: DebugifyMode::OriginalDebugInfo,
906 DebugInfoBeforePass: &(Passes.getDebugInfoPerPass()), OrigDIVerifyBugsReportFilePath: VerifyDIPreserveExport));
907 }
908 }
909
910 // In run twice mode, we want to make sure the output is bit-by-bit
911 // equivalent if we run the pass manager again, so setup two buffers and
912 // a stream to write to them. Note that llc does something similar and it
913 // may be worth to abstract this out in the future.
914 SmallVector<char, 0> Buffer;
915 SmallVector<char, 0> FirstRunBuffer;
916 std::unique_ptr<raw_svector_ostream> BOS;
917 raw_ostream *OS = nullptr;
918
919 const bool ShouldEmitOutput = !NoOutput;
920
921 // Write bitcode or assembly to the output as the last step...
922 if (ShouldEmitOutput || RunTwice) {
923 assert(Out);
924 OS = &Out->os();
925 if (RunTwice) {
926 BOS = std::make_unique<raw_svector_ostream>(args&: Buffer);
927 OS = BOS.get();
928 }
929 if (OutputAssembly) {
930 Passes.add(P: createPrintModulePass(
931 OS&: *OS, Banner: "", /*ShouldPreserveAssemblyUseListOrder=*/ShouldPreserveUseListOrder: false,
932 /*ShouldRenumberMetadata=*/true));
933 } else
934 Passes.add(P: createBitcodeWriterPass(
935 Str&: *OS, /* ShouldPreserveBitcodeUseListOrder */ ShouldPreserveUseListOrder: true));
936 }
937
938 // Before executing passes, print the final values of the LLVM options.
939 cl::PrintOptionValues();
940
941 if (!RunTwice) {
942 // Now that we have all of the passes ready, run them.
943 Passes.run(M&: *M);
944 } else {
945 // If requested, run all passes twice with the same pass manager to catch
946 // bugs caused by persistent state in the passes.
947 std::unique_ptr<Module> M2(CloneModule(M: *M));
948 // Run all passes on the original module first, so the second run processes
949 // the clone to catch CloneModule bugs.
950 Passes.run(M&: *M);
951 FirstRunBuffer = Buffer;
952 Buffer.clear();
953
954 Passes.run(M&: *M2);
955
956 // Compare the two outputs and make sure they're the same
957 assert(Out);
958 if (Buffer.size() != FirstRunBuffer.size() ||
959 (memcmp(s1: Buffer.data(), s2: FirstRunBuffer.data(), n: Buffer.size()) != 0)) {
960 errs()
961 << "Running the pass manager twice changed the output.\n"
962 "Writing the result of the second run to the specified output.\n"
963 "To generate the one-run comparison binary, just run without\n"
964 "the compile-twice option\n";
965 if (ShouldEmitOutput) {
966 Out->os() << BOS->str();
967 Out->keep();
968 }
969 if (RemarksFile)
970 RemarksFile->keep();
971 return 1;
972 }
973 if (ShouldEmitOutput)
974 Out->os() << BOS->str();
975 }
976
977 if (DebugifyEach && !DebugifyExport.empty())
978 exportDebugifyStats(Path: DebugifyExport, Map: Passes.getDebugifyStatsMap());
979
980 // If a pass reported an error via LLVMContext::emitError, fail without
981 // writing the output module.
982 if (Context.getDiagHandlerPtr()->HasErrors)
983 return 1;
984
985 // Declare success.
986 if (!NoOutput)
987 Out->keep();
988
989 if (RemarksFile)
990 RemarksFile->keep();
991
992 if (ThinLinkOut)
993 ThinLinkOut->keep();
994
995 return codegen::MaybeSaveStatistics(OutputFilename, ToolName: "opt");
996}
997