1 //===- optdriver.cpp - The LLVM Modular Optimizer -------------------------===//
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
7 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
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/TargetLibraryInfo.h"
21 #include "llvm/Analysis/TargetTransformInfo.h"
22 #include "llvm/AsmParser/Parser.h"
23 #include "llvm/CodeGen/CommandFlags.h"
24 #include "llvm/CodeGen/TargetPassConfig.h"
25 #include "llvm/Config/llvm-config.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/DebugInfo.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/LLVMRemarkStreamer.h"
30 #include "llvm/IR/LegacyPassManager.h"
31 #include "llvm/IR/LegacyPassNameParser.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/IR/ModuleSummaryIndex.h"
34 #include "llvm/IR/Verifier.h"
35 #include "llvm/IRReader/IRReader.h"
36 #include "llvm/InitializePasses.h"
37 #include "llvm/LinkAllIR.h"
38 #include "llvm/LinkAllPasses.h"
39 #include "llvm/MC/TargetRegistry.h"
40 #include "llvm/Passes/PassPlugin.h"
41 #include "llvm/Remarks/HotnessThresholdParser.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Support/ErrorHandling.h"
44 #include "llvm/Support/FileSystem.h"
45 #include "llvm/Support/InitLLVM.h"
46 #include "llvm/Support/PluginLoader.h"
47 #include "llvm/Support/SourceMgr.h"
48 #include "llvm/Support/SystemUtils.h"
49 #include "llvm/Support/TargetSelect.h"
50 #include "llvm/Support/TimeProfiler.h"
51 #include "llvm/Support/ToolOutputFile.h"
52 #include "llvm/Support/YAMLTraits.h"
53 #include "llvm/Target/TargetMachine.h"
54 #include "llvm/TargetParser/Host.h"
55 #include "llvm/TargetParser/SubtargetFeature.h"
56 #include "llvm/TargetParser/Triple.h"
57 #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
58 #include "llvm/Transforms/Utils/Cloning.h"
59 #include "llvm/Transforms/Utils/Debugify.h"
64 using namespace opt_tool
;
66 static codegen::RegisterCodeGenFlags CFG
;
68 // The OptimizationList is automatically populated with registered Passes by the
70 static cl::list
<const PassInfo
*, bool, PassNameParser
> PassList(cl::desc(
71 "Optimizations available (use \"-passes=\" for the new pass manager)"));
73 static cl::opt
<bool> EnableLegacyPassManager(
74 "bugpoint-enable-legacy-pm",
76 "Enable the legacy pass manager. This is strictly for bugpoint "
77 "due to it not working with the new PM, please do not use otherwise."),
80 // This flag specifies a textual description of the optimization pass pipeline
81 // to run over the module. This flag switches opt to use the new pass manager
82 // infrastructure, completely disabling all of the flags specific to the old
84 static cl::opt
<std::string
> PassPipeline(
87 "A textual description of the pass pipeline. To have analysis passes "
88 "available before a certain pass, add \"require<foo-analysis>\"."));
89 static cl::alias
PassPipeline2("p", cl::aliasopt(PassPipeline
),
90 cl::desc("Alias for -passes"));
92 static cl::opt
<bool> PrintPasses("print-passes",
93 cl::desc("Print available passes that can be "
94 "specified in -passes=foo and exit"));
96 static cl::opt
<std::string
> InputFilename(cl::Positional
,
97 cl::desc("<input bitcode file>"),
99 cl::value_desc("filename"));
101 static cl::opt
<std::string
> OutputFilename("o",
102 cl::desc("Override output filename"),
103 cl::value_desc("filename"));
105 static cl::opt
<bool> Force("f", cl::desc("Enable binary output on terminals"));
107 static cl::opt
<bool> NoOutput("disable-output",
108 cl::desc("Do not write result bitcode file"),
111 static cl::opt
<bool> OutputAssembly("S",
112 cl::desc("Write output as LLVM assembly"));
115 OutputThinLTOBC("thinlto-bc",
116 cl::desc("Write output as ThinLTO-ready bitcode"));
119 SplitLTOUnit("thinlto-split-lto-unit",
120 cl::desc("Enable splitting of a ThinLTO LTOUnit"));
123 UnifiedLTO("unified-lto",
124 cl::desc("Use unified LTO piplines. Ignored unless -thinlto-bc "
125 "is also specified."),
126 cl::Hidden
, cl::init(false));
128 static cl::opt
<std::string
> ThinLinkBitcodeFile(
129 "thin-link-bitcode-file", cl::value_desc("filename"),
131 "A file in which to write minimized bitcode for the thin link only"));
133 static cl::opt
<bool> NoVerify("disable-verify",
134 cl::desc("Do not run the verifier"), cl::Hidden
);
136 static cl::opt
<bool> NoUpgradeDebugInfo("disable-upgrade-debug-info",
137 cl::desc("Generate invalid output"),
140 static cl::opt
<bool> VerifyEach("verify-each",
141 cl::desc("Verify after each transform"));
144 DisableDITypeMap("disable-debug-info-type-map",
145 cl::desc("Don't use a uniquing type map for debug info"));
148 StripDebug("strip-debug",
149 cl::desc("Strip debugger symbol info from translation unit"));
152 StripNamedMetadata("strip-named-metadata",
153 cl::desc("Strip module-level named metadata"));
156 OptLevelO0("O0", cl::desc("Optimization level 0. Similar to clang -O0. "
157 "Same as -passes=\"default<O0>\""));
160 OptLevelO1("O1", cl::desc("Optimization level 1. Similar to clang -O1. "
161 "Same as -passes=\"default<O1>\""));
164 OptLevelO2("O2", cl::desc("Optimization level 2. Similar to clang -O2. "
165 "Same as -passes=\"default<O2>\""));
168 OptLevelOs("Os", cl::desc("Like -O2 but size-conscious. Similar to clang "
169 "-Os. Same as -passes=\"default<Os>\""));
171 static cl::opt
<bool> OptLevelOz(
173 cl::desc("Like -O2 but optimize for code size above all else. Similar to "
174 "clang -Oz. Same as -passes=\"default<Oz>\""));
177 OptLevelO3("O3", cl::desc("Optimization level 3. Similar to clang -O3. "
178 "Same as -passes=\"default<O3>\""));
180 static cl::opt
<unsigned> CodeGenOptLevelCL(
182 cl::desc("Override optimization level for codegen hooks, legacy PM only"));
184 static cl::opt
<std::string
>
185 TargetTriple("mtriple", cl::desc("Override target triple for module"));
187 static cl::opt
<bool> EmitSummaryIndex("module-summary",
188 cl::desc("Emit module summary index"),
191 static cl::opt
<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
195 DisableSimplifyLibCalls("disable-simplify-libcalls",
196 cl::desc("Disable simplify-libcalls"));
198 static cl::list
<std::string
> DisableBuiltins(
200 cl::desc("Disable specific target library builtin function"));
202 static cl::opt
<bool> EnableDebugify(
205 "Start the pipeline with debugify and end it with check-debugify"));
207 static cl::opt
<bool> VerifyDebugInfoPreserve(
208 "verify-debuginfo-preserve",
209 cl::desc("Start the pipeline with collecting and end it with checking of "
210 "debug info preservation."));
212 static cl::opt
<std::string
> ClDataLayout("data-layout",
213 cl::desc("data layout string to use"),
214 cl::value_desc("layout-string"),
217 static cl::opt
<bool> PreserveBitcodeUseListOrder(
218 "preserve-bc-uselistorder",
219 cl::desc("Preserve use-list order when writing LLVM bitcode."),
220 cl::init(true), cl::Hidden
);
222 static cl::opt
<bool> PreserveAssemblyUseListOrder(
223 "preserve-ll-uselistorder",
224 cl::desc("Preserve use-list order when writing LLVM assembly."),
225 cl::init(false), cl::Hidden
);
227 static cl::opt
<bool> RunTwice("run-twice",
228 cl::desc("Run all passes twice, re-using the "
229 "same pass manager (legacy PM only)."),
230 cl::init(false), cl::Hidden
);
232 static cl::opt
<bool> DiscardValueNames(
233 "discard-value-names",
234 cl::desc("Discard names from Value (other than GlobalValue)."),
235 cl::init(false), cl::Hidden
);
237 static cl::opt
<bool> TimeTrace("time-trace", cl::desc("Record time trace"));
239 static cl::opt
<unsigned> TimeTraceGranularity(
240 "time-trace-granularity",
242 "Minimum time granularity (in microseconds) traced by time profiler"),
243 cl::init(500), cl::Hidden
);
245 static cl::opt
<std::string
>
246 TimeTraceFile("time-trace-file",
247 cl::desc("Specify time trace file destination"),
248 cl::value_desc("filename"));
250 static cl::opt
<bool> RemarksWithHotness(
251 "pass-remarks-with-hotness",
252 cl::desc("With PGO, include profile count in optimization remarks"),
255 static cl::opt
<std::optional
<uint64_t>, false, remarks::HotnessThresholdParser
>
256 RemarksHotnessThreshold(
257 "pass-remarks-hotness-threshold",
258 cl::desc("Minimum profile count required for "
259 "an optimization remark to be output. "
260 "Use 'auto' to apply the threshold from profile summary"),
261 cl::value_desc("N or 'auto'"), cl::init(0), cl::Hidden
);
263 static cl::opt
<std::string
>
264 RemarksFilename("pass-remarks-output",
265 cl::desc("Output filename for pass remarks"),
266 cl::value_desc("filename"));
268 static cl::opt
<std::string
>
269 RemarksPasses("pass-remarks-filter",
270 cl::desc("Only record optimization remarks from passes whose "
271 "names match the given regular expression"),
272 cl::value_desc("regex"));
274 static cl::opt
<std::string
> RemarksFormat(
275 "pass-remarks-format",
276 cl::desc("The format used for serializing remarks (default: YAML)"),
277 cl::value_desc("format"), cl::init("yaml"));
279 static cl::list
<std::string
>
280 PassPlugins("load-pass-plugin",
281 cl::desc("Load passes from plugin library"));
283 static cl::opt
<bool> TryUseNewDbgInfoFormat(
284 "try-experimental-debuginfo-iterators",
285 cl::desc("Enable debuginfo iterator positions, if they're built in"),
286 cl::init(false), cl::Hidden
);
288 extern cl::opt
<bool> UseNewDbgInfoFormat
;
290 //===----------------------------------------------------------------------===//
291 // CodeGen-related helper functions.
294 static CodeGenOptLevel
GetCodeGenOptLevel() {
295 return static_cast<CodeGenOptLevel
>(unsigned(CodeGenOptLevelCL
));
298 struct TimeTracerRAII
{
299 TimeTracerRAII(StringRef ProgramName
) {
301 timeTraceProfilerInitialize(TimeTraceGranularity
, ProgramName
);
305 if (auto E
= timeTraceProfilerWrite(TimeTraceFile
, OutputFilename
)) {
306 handleAllErrors(std::move(E
), [&](const StringError
&SE
) {
307 errs() << SE
.getMessage() << "\n";
311 timeTraceProfilerCleanup();
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
320 static bool shouldPinPassToLegacyPM(StringRef Pass
) {
321 static constexpr StringLiteral PassNameExactToIgnore
[] = {
324 "amdgpu-simplifylib",
325 "amdgpu-image-intrinsic-opt",
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-unify-metadata",
333 "amdgpu-printf-runtime-binding",
334 "amdgpu-always-inline"};
335 if (llvm::is_contained(PassNameExactToIgnore
, Pass
))
338 static constexpr StringLiteral PassNamePrefix
[] = {
339 "x86-", "xcore-", "wasm-", "systemz-", "ppc-", "nvvm-",
340 "nvptx-", "mips-", "lanai-", "hexagon-", "bpf-", "avr-",
341 "thumb2-", "arm-", "si-", "gcn-", "amdgpu-", "aarch64-",
342 "amdgcn-", "polly-", "riscv-", "dxil-"};
343 static constexpr StringLiteral PassNameContain
[] = {"-eh-prepare"};
344 static constexpr StringLiteral PassNameExact
[] = {
348 "interleaved-load-combine",
349 "unreachableblockelim",
350 "verify-safepoint-ir",
353 "mve-tail-predication",
354 "interleaved-access",
356 "pre-isel-intrinsic-lowering",
363 "lower-amx-intrinsics",
365 "print-polyhedral-info",
366 "replace-with-veclib",
373 "expand-large-div-rem",
376 "expand-large-fp-convert",
380 for (const auto &P
: PassNamePrefix
)
381 if (Pass
.starts_with(P
))
383 for (const auto &P
: PassNameContain
)
384 if (Pass
.contains(P
))
386 return llvm::is_contained(PassNameExact
, Pass
);
389 // For use in NPM transition.
390 static bool shouldForceLegacyPM() {
391 for (const auto &P
: PassList
) {
392 StringRef Arg
= P
->getPassArgument();
393 if (shouldPinPassToLegacyPM(Arg
))
399 //===----------------------------------------------------------------------===//
402 extern "C" int optMain(
403 int argc
, char **argv
,
404 ArrayRef
<std::function
<void(llvm::PassBuilder
&)>> PassBuilderCallbacks
) {
405 InitLLVM
X(argc
, argv
);
407 // Enable debug stream buffering.
408 EnableDebugBuffering
= true;
410 InitializeAllTargets();
411 InitializeAllTargetMCs();
412 InitializeAllAsmPrinters();
413 InitializeAllAsmParsers();
416 PassRegistry
&Registry
= *PassRegistry::getPassRegistry();
417 initializeCore(Registry
);
418 initializeScalarOpts(Registry
);
419 initializeVectorization(Registry
);
420 initializeIPO(Registry
);
421 initializeAnalysis(Registry
);
422 initializeTransformUtils(Registry
);
423 initializeInstCombine(Registry
);
424 initializeTarget(Registry
);
425 // For codegen passes, only passes that do IR to IR transformation are
427 initializeExpandLargeDivRemLegacyPassPass(Registry
);
428 initializeExpandLargeFpConvertLegacyPassPass(Registry
);
429 initializeExpandMemCmpLegacyPassPass(Registry
);
430 initializeScalarizeMaskedMemIntrinLegacyPassPass(Registry
);
431 initializeSelectOptimizePass(Registry
);
432 initializeCallBrPreparePass(Registry
);
433 initializeCodeGenPrepareLegacyPassPass(Registry
);
434 initializeAtomicExpandLegacyPass(Registry
);
435 initializeWinEHPreparePass(Registry
);
436 initializeDwarfEHPrepareLegacyPassPass(Registry
);
437 initializeSafeStackLegacyPassPass(Registry
);
438 initializeSjLjEHPreparePass(Registry
);
439 initializePreISelIntrinsicLoweringLegacyPassPass(Registry
);
440 initializeGlobalMergePass(Registry
);
441 initializeIndirectBrExpandLegacyPassPass(Registry
);
442 initializeInterleavedLoadCombinePass(Registry
);
443 initializeInterleavedAccessPass(Registry
);
444 initializePostInlineEntryExitInstrumenterPass(Registry
);
445 initializeUnreachableBlockElimLegacyPassPass(Registry
);
446 initializeExpandReductionsPass(Registry
);
447 initializeWasmEHPreparePass(Registry
);
448 initializeWriteBitcodePassPass(Registry
);
449 initializeReplaceWithVeclibLegacyPass(Registry
);
450 initializeJMCInstrumenterPass(Registry
);
452 SmallVector
<PassPlugin
, 1> PluginList
;
453 PassPlugins
.setCallback([&](const std::string
&PluginPath
) {
454 auto Plugin
= PassPlugin::Load(PluginPath
);
456 report_fatal_error(Plugin
.takeError(), /*gen_crash_diag=*/false);
457 PluginList
.emplace_back(Plugin
.get());
460 // Register the Target and CPU printer for --version.
461 cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU
);
463 cl::ParseCommandLineOptions(
464 argc
, argv
, "llvm .bc -> .bc modular optimizer and analysis printer\n");
466 // RemoveDIs debug-info transition: tests may request that we /try/ to use the
467 // new debug-info format.
468 if (TryUseNewDbgInfoFormat
) {
469 // Turn the new debug-info format on.
470 UseNewDbgInfoFormat
= true;
475 // TODO: remove shouldForceLegacyPM().
476 const bool UseNPM
= (!EnableLegacyPassManager
&& !shouldForceLegacyPM()) ||
477 PassPipeline
.getNumOccurrences() > 0;
479 if (UseNPM
&& !PassList
.empty()) {
480 errs() << "The `opt -passname` syntax for the new pass manager is "
481 "not supported, please use `opt -passes=<pipeline>` (or the `-p` "
482 "alias for a more concise version).\n";
483 errs() << "See https://llvm.org/docs/NewPassManager.html#invoking-opt "
484 "for more details on the pass pipeline syntax.\n\n";
488 if (!UseNPM
&& PluginList
.size()) {
489 errs() << argv
[0] << ": " << PassPlugins
.ArgStr
490 << " specified with legacy PM.\n";
494 // FIXME: once the legacy PM code is deleted, move runPassPipeline() here and
495 // construct the PassBuilder before parsing IR so we can reuse the same
496 // PassBuilder for print passes.
502 TimeTracerRAII
TimeTracer(argv
[0]);
506 Context
.setDiscardValueNames(DiscardValueNames
);
507 if (!DisableDITypeMap
)
508 Context
.enableDebugTypeODRUniquing();
510 Expected
<std::unique_ptr
<ToolOutputFile
>> RemarksFileOrErr
=
511 setupLLVMOptimizationRemarks(Context
, RemarksFilename
, RemarksPasses
,
512 RemarksFormat
, RemarksWithHotness
,
513 RemarksHotnessThreshold
);
514 if (Error E
= RemarksFileOrErr
.takeError()) {
515 errs() << toString(std::move(E
)) << '\n';
518 std::unique_ptr
<ToolOutputFile
> RemarksFile
= std::move(*RemarksFileOrErr
);
520 // Load the input module...
521 auto SetDataLayout
= [&](StringRef IRTriple
,
522 StringRef IRLayout
) -> std::optional
<std::string
> {
523 // Data layout specified on the command line has the highest priority.
524 if (!ClDataLayout
.empty())
526 // If an explicit data layout is already defined in the IR, don't infer.
527 if (!IRLayout
.empty())
530 // If an explicit triple was specified (either in the IR or on the
531 // command line), use that to infer the default data layout. However, the
532 // command line target triple should override the IR file target triple.
533 std::string TripleStr
=
534 TargetTriple
.empty() ? IRTriple
.str() : Triple::normalize(TargetTriple
);
535 // If the triple string is still empty, we don't fall back to
536 // sys::getDefaultTargetTriple() since we do not want to have differing
537 // behaviour dependent on the configured default triple. Therefore, if the
538 // user did not pass -mtriple or define an explicit triple/datalayout in
539 // the IR, we should default to an empty (default) DataLayout.
540 if (TripleStr
.empty())
542 // Otherwise we infer the DataLayout from the target machine.
543 Expected
<std::unique_ptr
<TargetMachine
>> ExpectedTM
=
544 codegen::createTargetMachineForTriple(TripleStr
, GetCodeGenOptLevel());
546 errs() << argv
[0] << ": warning: failed to infer data layout: "
547 << toString(ExpectedTM
.takeError()) << "\n";
550 return (*ExpectedTM
)->createDataLayout().getStringRepresentation();
552 std::unique_ptr
<Module
> M
;
553 if (NoUpgradeDebugInfo
)
554 M
= parseAssemblyFileWithIndexNoUpgradeDebugInfo(
555 InputFilename
, Err
, Context
, nullptr, SetDataLayout
)
558 M
= parseIRFile(InputFilename
, Err
, Context
,
559 ParserCallbacks(SetDataLayout
));
562 Err
.print(argv
[0], errs());
566 // Strip debug info before running the verifier.
570 // Erase module-level named metadata, if requested.
571 if (StripNamedMetadata
) {
572 while (!M
->named_metadata_empty()) {
573 NamedMDNode
*NMD
= &*M
->named_metadata_begin();
574 M
->eraseNamedMetadata(NMD
);
578 // If we are supposed to override the target triple, do so now.
579 if (!TargetTriple
.empty())
580 M
->setTargetTriple(Triple::normalize(TargetTriple
));
582 // Immediately run the verifier to catch any problems before starting up the
583 // pass pipelines. Otherwise we can crash on broken code during
584 // doInitialization().
585 if (!NoVerify
&& verifyModule(*M
, &errs())) {
586 errs() << argv
[0] << ": " << InputFilename
587 << ": error: input module is broken!\n";
591 // Enable testing of whole program devirtualization on this module by invoking
592 // the facility for updating public visibility to linkage unit visibility when
593 // specified by an internal option. This is normally done during LTO which is
594 // not performed via opt.
595 updateVCallVisibilityInModule(
597 /*WholeProgramVisibilityEnabledInLTO=*/false,
598 // FIXME: These need linker information via a
599 // TBD new interface.
600 /*DynamicExportSymbols=*/{},
601 /*ValidateAllVtablesHaveTypeInfos=*/false,
602 /*IsVisibleToRegularObj=*/[](StringRef
) { return true; });
604 // Figure out what stream we are supposed to write to...
605 std::unique_ptr
<ToolOutputFile
> Out
;
606 std::unique_ptr
<ToolOutputFile
> ThinLinkOut
;
608 if (!OutputFilename
.empty())
609 errs() << "WARNING: The -o (output filename) option is ignored when\n"
610 "the --disable-output option is used.\n";
612 // Default to standard output.
613 if (OutputFilename
.empty())
614 OutputFilename
= "-";
617 sys::fs::OpenFlags Flags
=
618 OutputAssembly
? sys::fs::OF_TextWithCRLF
: sys::fs::OF_None
;
619 Out
.reset(new ToolOutputFile(OutputFilename
, EC
, Flags
));
621 errs() << EC
.message() << '\n';
625 if (!ThinLinkBitcodeFile
.empty()) {
627 new ToolOutputFile(ThinLinkBitcodeFile
, EC
, sys::fs::OF_None
));
629 errs() << EC
.message() << '\n';
635 Triple
ModuleTriple(M
->getTargetTriple());
636 std::string CPUStr
, FeaturesStr
;
637 std::unique_ptr
<TargetMachine
> TM
;
638 if (ModuleTriple
.getArch()) {
639 CPUStr
= codegen::getCPUStr();
640 FeaturesStr
= codegen::getFeaturesStr();
641 Expected
<std::unique_ptr
<TargetMachine
>> ExpectedTM
=
642 codegen::createTargetMachineForTriple(ModuleTriple
.str(),
643 GetCodeGenOptLevel());
644 if (auto E
= ExpectedTM
.takeError()) {
645 errs() << argv
[0] << ": WARNING: failed to create target machine for '"
646 << ModuleTriple
.str() << "': " << toString(std::move(E
)) << "\n";
648 TM
= std::move(*ExpectedTM
);
650 } else if (ModuleTriple
.getArchName() != "unknown" &&
651 ModuleTriple
.getArchName() != "") {
652 errs() << argv
[0] << ": unrecognized architecture '"
653 << ModuleTriple
.getArchName() << "' provided.\n";
657 // Override function attributes based on CPUStr, FeaturesStr, and command line
659 codegen::setFunctionAttributes(CPUStr
, FeaturesStr
, *M
);
661 // If the output is set to be emitted to standard out, and standard out is a
662 // console, print out a warning message and refuse to do it. We don't
663 // impress anyone by spewing tons of binary goo to a terminal.
664 if (!Force
&& !NoOutput
&& !OutputAssembly
)
665 if (CheckBitcodeOutputToConsole(Out
->os()))
668 if (OutputThinLTOBC
) {
669 M
->addModuleFlag(Module::Error
, "EnableSplitLTOUnit", SplitLTOUnit
);
671 M
->addModuleFlag(Module::Error
, "UnifiedLTO", 1);
674 // Add an appropriate TargetLibraryInfo pass for the module's triple.
675 TargetLibraryInfoImpl
TLII(ModuleTriple
);
677 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
678 if (DisableSimplifyLibCalls
)
679 TLII
.disableAllFunctions();
681 // Disable individual builtin functions in TargetLibraryInfo.
683 for (auto &FuncName
: DisableBuiltins
)
684 if (TLII
.getLibFunc(FuncName
, F
))
685 TLII
.setUnavailable(F
);
687 errs() << argv
[0] << ": cannot disable nonexistent builtin function "
694 if (legacy::debugPassSpecified()) {
695 errs() << "-debug-pass does not work with the new PM, either use "
696 "-debug-pass-manager, or use the legacy PM\n";
699 auto NumOLevel
= OptLevelO0
+ OptLevelO1
+ OptLevelO2
+ OptLevelO3
+
700 OptLevelOs
+ OptLevelOz
;
702 errs() << "Cannot specify multiple -O#\n";
705 if (NumOLevel
> 0 && (PassPipeline
.getNumOccurrences() > 0)) {
706 errs() << "Cannot specify -O# and --passes=/--foo-pass, use "
707 "-passes='default<O#>,other-pass'\n";
710 std::string Pipeline
= PassPipeline
;
713 Pipeline
= "default<O0>";
715 Pipeline
= "default<O1>";
717 Pipeline
= "default<O2>";
719 Pipeline
= "default<O3>";
721 Pipeline
= "default<Os>";
723 Pipeline
= "default<Oz>";
724 OutputKind OK
= OK_NoOutput
;
728 : (OutputThinLTOBC
? OK_OutputThinLTOBitcode
: OK_OutputBitcode
);
730 VerifierKind VK
= VerifierKind::InputOutput
;
732 VK
= VerifierKind::None
;
734 VK
= VerifierKind::EachPass
;
736 // The user has asked to use the new pass manager and provided a pipeline
737 // string. Hand off the rest of the functionality to the new code for that
739 return runPassPipeline(
740 argv
[0], *M
, TM
.get(), &TLII
, Out
.get(), ThinLinkOut
.get(),
741 RemarksFile
.get(), Pipeline
, PluginList
, PassBuilderCallbacks
,
742 OK
, VK
, PreserveAssemblyUseListOrder
,
743 PreserveBitcodeUseListOrder
, EmitSummaryIndex
, EmitModuleHash
,
744 EnableDebugify
, VerifyDebugInfoPreserve
, UnifiedLTO
)
749 if (OptLevelO0
|| OptLevelO1
|| OptLevelO2
|| OptLevelOs
|| OptLevelOz
||
751 errs() << "Cannot use -O# with legacy PM.\n";
754 if (EmitSummaryIndex
) {
755 errs() << "Cannot use -module-summary with legacy PM.\n";
758 if (EmitModuleHash
) {
759 errs() << "Cannot use -module-hash with legacy PM.\n";
762 if (OutputThinLTOBC
) {
763 errs() << "Cannot use -thinlto-bc with legacy PM.\n";
766 // Create a PassManager to hold and optimize the collection of passes we are
767 // about to build. If the -debugify-each option is set, wrap each pass with
768 // the (-check)-debugify passes.
769 DebugifyCustomPassManager Passes
;
770 DebugifyStatsMap DIStatsMap
;
771 DebugInfoPerPass DebugInfoBeforePass
;
773 Passes
.setDebugifyMode(DebugifyMode::SyntheticDebugInfo
);
774 Passes
.setDIStatsMap(DIStatsMap
);
775 } else if (VerifyEachDebugInfoPreserve
) {
776 Passes
.setDebugifyMode(DebugifyMode::OriginalDebugInfo
);
777 Passes
.setDebugInfoBeforePass(DebugInfoBeforePass
);
778 if (!VerifyDIPreserveExport
.empty())
779 Passes
.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport
);
782 bool AddOneTimeDebugifyPasses
=
783 (EnableDebugify
&& !DebugifyEach
) ||
784 (VerifyDebugInfoPreserve
&& !VerifyEachDebugInfoPreserve
);
786 Passes
.add(new TargetLibraryInfoWrapperPass(TLII
));
788 // Add internal analysis passes from the target machine.
789 Passes
.add(createTargetTransformInfoWrapperPass(TM
? TM
->getTargetIRAnalysis()
790 : TargetIRAnalysis()));
792 if (AddOneTimeDebugifyPasses
) {
793 if (EnableDebugify
) {
794 Passes
.setDIStatsMap(DIStatsMap
);
795 Passes
.add(createDebugifyModulePass());
796 } else if (VerifyDebugInfoPreserve
) {
797 Passes
.setDebugInfoBeforePass(DebugInfoBeforePass
);
798 Passes
.add(createDebugifyModulePass(DebugifyMode::OriginalDebugInfo
, "",
799 &(Passes
.getDebugInfoPerPass())));
804 Pass
*TPC
= TM
->createPassConfig(Passes
);
806 errs() << "Target Machine pass config creation failed.\n";
812 // Create a new optimization pass for each one specified on the command line
813 for (unsigned i
= 0; i
< PassList
.size(); ++i
) {
814 const PassInfo
*PassInf
= PassList
[i
];
815 if (PassInf
->getNormalCtor()) {
816 Pass
*P
= PassInf
->getNormalCtor()();
818 // Add the pass to the pass manager.
820 // If we are verifying all of the intermediate steps, add the verifier.
822 Passes
.add(createVerifierPass());
825 errs() << argv
[0] << ": cannot create pass: " << PassInf
->getPassName()
829 // Check that the module is well formed on completion of optimization
830 if (!NoVerify
&& !VerifyEach
)
831 Passes
.add(createVerifierPass());
833 if (AddOneTimeDebugifyPasses
) {
835 Passes
.add(createCheckDebugifyModulePass(false));
836 else if (VerifyDebugInfoPreserve
) {
837 if (!VerifyDIPreserveExport
.empty())
838 Passes
.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport
);
839 Passes
.add(createCheckDebugifyModulePass(
840 false, "", nullptr, DebugifyMode::OriginalDebugInfo
,
841 &(Passes
.getDebugInfoPerPass()), VerifyDIPreserveExport
));
845 // In run twice mode, we want to make sure the output is bit-by-bit
846 // equivalent if we run the pass manager again, so setup two buffers and
847 // a stream to write to them. Note that llc does something similar and it
848 // may be worth to abstract this out in the future.
849 SmallVector
<char, 0> Buffer
;
850 SmallVector
<char, 0> FirstRunBuffer
;
851 std::unique_ptr
<raw_svector_ostream
> BOS
;
852 raw_ostream
*OS
= nullptr;
854 const bool ShouldEmitOutput
= !NoOutput
;
856 // Write bitcode or assembly to the output as the last step...
857 if (ShouldEmitOutput
|| RunTwice
) {
861 BOS
= std::make_unique
<raw_svector_ostream
>(Buffer
);
865 Passes
.add(createPrintModulePass(*OS
, "", PreserveAssemblyUseListOrder
));
867 Passes
.add(createBitcodeWriterPass(*OS
, PreserveBitcodeUseListOrder
));
870 // Before executing passes, print the final values of the LLVM options.
871 cl::PrintOptionValues();
874 // Now that we have all of the passes ready, run them.
877 // If requested, run all passes twice with the same pass manager to catch
878 // bugs caused by persistent state in the passes.
879 std::unique_ptr
<Module
> M2(CloneModule(*M
));
880 // Run all passes on the original module first, so the second run processes
881 // the clone to catch CloneModule bugs.
883 FirstRunBuffer
= Buffer
;
888 // Compare the two outputs and make sure they're the same
890 if (Buffer
.size() != FirstRunBuffer
.size() ||
891 (memcmp(Buffer
.data(), FirstRunBuffer
.data(), Buffer
.size()) != 0)) {
893 << "Running the pass manager twice changed the output.\n"
894 "Writing the result of the second run to the specified output.\n"
895 "To generate the one-run comparison binary, just run without\n"
896 "the compile-twice option\n";
897 if (ShouldEmitOutput
) {
898 Out
->os() << BOS
->str();
905 if (ShouldEmitOutput
)
906 Out
->os() << BOS
->str();
909 if (DebugifyEach
&& !DebugifyExport
.empty())
910 exportDebugifyStats(DebugifyExport
, Passes
.getDebugifyStatsMap());