1 //===- opt.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.
12 //===----------------------------------------------------------------------===//
14 #include "NewPMDriver.h"
15 #include "llvm/Analysis/CallGraph.h"
16 #include "llvm/Analysis/CallGraphSCCPass.h"
17 #include "llvm/Analysis/LoopPass.h"
18 #include "llvm/Analysis/RegionPass.h"
19 #include "llvm/Analysis/TargetLibraryInfo.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/AsmParser/Parser.h"
22 #include "llvm/CodeGen/CommandFlags.h"
23 #include "llvm/CodeGen/TargetPassConfig.h"
24 #include "llvm/Config/llvm-config.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/DebugInfo.h"
27 #include "llvm/IR/LLVMContext.h"
28 #include "llvm/IR/LLVMRemarkStreamer.h"
29 #include "llvm/IR/LegacyPassManager.h"
30 #include "llvm/IR/LegacyPassNameParser.h"
31 #include "llvm/IR/Module.h"
32 #include "llvm/IR/ModuleSummaryIndex.h"
33 #include "llvm/IR/Verifier.h"
34 #include "llvm/IRReader/IRReader.h"
35 #include "llvm/InitializePasses.h"
36 #include "llvm/LinkAllIR.h"
37 #include "llvm/LinkAllPasses.h"
38 #include "llvm/MC/TargetRegistry.h"
39 #include "llvm/Passes/PassPlugin.h"
40 #include "llvm/Remarks/HotnessThresholdParser.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/ErrorHandling.h"
43 #include "llvm/Support/FileSystem.h"
44 #include "llvm/Support/InitLLVM.h"
45 #include "llvm/Support/PluginLoader.h"
46 #include "llvm/Support/SourceMgr.h"
47 #include "llvm/Support/SystemUtils.h"
48 #include "llvm/Support/TargetSelect.h"
49 #include "llvm/Support/ToolOutputFile.h"
50 #include "llvm/Support/YAMLTraits.h"
51 #include "llvm/Target/TargetMachine.h"
52 #include "llvm/TargetParser/Host.h"
53 #include "llvm/TargetParser/SubtargetFeature.h"
54 #include "llvm/TargetParser/Triple.h"
55 #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
56 #include "llvm/Transforms/Utils/Cloning.h"
57 #include "llvm/Transforms/Utils/Debugify.h"
62 using namespace opt_tool
;
64 static codegen::RegisterCodeGenFlags CFG
;
66 // The OptimizationList is automatically populated with registered Passes by the
68 static cl::list
<const PassInfo
*, bool, PassNameParser
> PassList(cl::desc(
69 "Optimizations available (use '-passes=' for the new pass manager)"));
71 static cl::opt
<bool> EnableLegacyPassManager(
72 "bugpoint-enable-legacy-pm",
74 "Enable the legacy pass manager. This is strictly for bugpoint "
75 "due to it not working with the new PM, please do not use otherwise."),
78 // This flag specifies a textual description of the optimization pass pipeline
79 // to run over the module. This flag switches opt to use the new pass manager
80 // infrastructure, completely disabling all of the flags specific to the old
82 static cl::opt
<std::string
> PassPipeline(
85 "A textual description of the pass pipeline. To have analysis passes "
86 "available before a certain pass, add 'require<foo-analysis>'."));
87 static cl::alias
PassPipeline2("p", cl::aliasopt(PassPipeline
),
88 cl::desc("Alias for -passes"));
90 static cl::opt
<bool> PrintPasses("print-passes",
91 cl::desc("Print available passes that can be "
92 "specified in -passes=foo and exit"));
94 static cl::opt
<std::string
>
95 InputFilename(cl::Positional
, cl::desc("<input bitcode file>"),
96 cl::init("-"), cl::value_desc("filename"));
98 static cl::opt
<std::string
>
99 OutputFilename("o", cl::desc("Override output filename"),
100 cl::value_desc("filename"));
103 Force("f", cl::desc("Enable binary output on terminals"));
106 NoOutput("disable-output",
107 cl::desc("Do not write result bitcode file"), cl::Hidden
);
110 OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
113 OutputThinLTOBC("thinlto-bc",
114 cl::desc("Write output as ThinLTO-ready bitcode"));
117 SplitLTOUnit("thinlto-split-lto-unit",
118 cl::desc("Enable splitting of a ThinLTO LTOUnit"));
121 UnifiedLTO("unified-lto",
122 cl::desc("Use unified LTO piplines. Ignored unless -thinlto-bc "
123 "is also specified."),
124 cl::Hidden
, cl::init(false));
126 static cl::opt
<std::string
> ThinLinkBitcodeFile(
127 "thin-link-bitcode-file", cl::value_desc("filename"),
129 "A file in which to write minimized bitcode for the thin link only"));
132 NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden
);
134 static cl::opt
<bool> NoUpgradeDebugInfo("disable-upgrade-debug-info",
135 cl::desc("Generate invalid output"),
138 static cl::opt
<bool> VerifyEach("verify-each",
139 cl::desc("Verify after each transform"));
142 DisableDITypeMap("disable-debug-info-type-map",
143 cl::desc("Don't use a uniquing type map for debug info"));
146 StripDebug("strip-debug",
147 cl::desc("Strip debugger symbol info from translation unit"));
150 StripNamedMetadata("strip-named-metadata",
151 cl::desc("Strip module-level named metadata"));
154 OptLevelO0("O0", cl::desc("Optimization level 0. Similar to clang -O0. "
155 "Same as -passes='default<O0>'"));
158 OptLevelO1("O1", cl::desc("Optimization level 1. Similar to clang -O1. "
159 "Same as -passes='default<O1>'"));
162 OptLevelO2("O2", cl::desc("Optimization level 2. Similar to clang -O2. "
163 "Same as -passes='default<O2>'"));
166 OptLevelOs("Os", cl::desc("Like -O2 but size-conscious. Similar to clang "
167 "-Os. Same as -passes='default<Os>'"));
169 static cl::opt
<bool> OptLevelOz(
171 cl::desc("Like -O2 but optimize for code size above all else. Similar to "
172 "clang -Oz. Same as -passes='default<Oz>'"));
175 OptLevelO3("O3", cl::desc("Optimization level 3. Similar to clang -O3. "
176 "Same as -passes='default<O3>'"));
178 static cl::opt
<unsigned> CodeGenOptLevelCL(
180 cl::desc("Override optimization level for codegen hooks, legacy PM only"));
182 static cl::opt
<std::string
>
183 TargetTriple("mtriple", cl::desc("Override target triple for module"));
185 static cl::opt
<bool> EmitSummaryIndex("module-summary",
186 cl::desc("Emit module summary index"),
189 static cl::opt
<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
193 DisableSimplifyLibCalls("disable-simplify-libcalls",
194 cl::desc("Disable simplify-libcalls"));
196 static cl::list
<std::string
> DisableBuiltins(
198 cl::desc("Disable specific target library builtin function"));
200 static cl::opt
<bool> EnableDebugify(
203 "Start the pipeline with debugify and end it with check-debugify"));
205 static cl::opt
<bool> VerifyDebugInfoPreserve(
206 "verify-debuginfo-preserve",
207 cl::desc("Start the pipeline with collecting and end it with checking of "
208 "debug info preservation."));
210 static cl::opt
<std::string
> ClDataLayout("data-layout",
211 cl::desc("data layout string to use"),
212 cl::value_desc("layout-string"),
215 static cl::opt
<bool> PreserveBitcodeUseListOrder(
216 "preserve-bc-uselistorder",
217 cl::desc("Preserve use-list order when writing LLVM bitcode."),
218 cl::init(true), cl::Hidden
);
220 static cl::opt
<bool> PreserveAssemblyUseListOrder(
221 "preserve-ll-uselistorder",
222 cl::desc("Preserve use-list order when writing LLVM assembly."),
223 cl::init(false), cl::Hidden
);
225 static cl::opt
<bool> RunTwice("run-twice",
226 cl::desc("Run all passes twice, re-using the "
227 "same pass manager (legacy PM only)."),
228 cl::init(false), cl::Hidden
);
230 static cl::opt
<bool> DiscardValueNames(
231 "discard-value-names",
232 cl::desc("Discard names from Value (other than GlobalValue)."),
233 cl::init(false), cl::Hidden
);
235 static cl::opt
<bool> TimeTrace(
237 cl::desc("Record time trace"));
239 static cl::opt
<unsigned> TimeTraceGranularity(
240 "time-trace-granularity",
241 cl::desc("Minimum time granularity (in microseconds) traced by time profiler"),
242 cl::init(500), cl::Hidden
);
244 static cl::opt
<std::string
>
245 TimeTraceFile("time-trace-file",
246 cl::desc("Specify time trace file destination"),
247 cl::value_desc("filename"));
249 static cl::opt
<bool> RemarksWithHotness(
250 "pass-remarks-with-hotness",
251 cl::desc("With PGO, include profile count in optimization remarks"),
254 static cl::opt
<std::optional
<uint64_t>, false, remarks::HotnessThresholdParser
>
255 RemarksHotnessThreshold(
256 "pass-remarks-hotness-threshold",
257 cl::desc("Minimum profile count required for "
258 "an optimization remark to be output. "
259 "Use 'auto' to apply the threshold from profile summary"),
260 cl::value_desc("N or 'auto'"), cl::init(0), cl::Hidden
);
262 static cl::opt
<std::string
>
263 RemarksFilename("pass-remarks-output",
264 cl::desc("Output filename for pass remarks"),
265 cl::value_desc("filename"));
267 static cl::opt
<std::string
>
268 RemarksPasses("pass-remarks-filter",
269 cl::desc("Only record optimization remarks from passes whose "
270 "names match the given regular expression"),
271 cl::value_desc("regex"));
273 static cl::opt
<std::string
> RemarksFormat(
274 "pass-remarks-format",
275 cl::desc("The format used for serializing remarks (default: YAML)"),
276 cl::value_desc("format"), cl::init("yaml"));
278 static cl::list
<std::string
>
279 PassPlugins("load-pass-plugin",
280 cl::desc("Load passes from plugin library"));
282 //===----------------------------------------------------------------------===//
283 // CodeGen-related helper functions.
286 static CodeGenOptLevel
GetCodeGenOptLevel() {
287 return static_cast<CodeGenOptLevel
>(unsigned(CodeGenOptLevelCL
));
290 struct TimeTracerRAII
{
291 TimeTracerRAII(StringRef ProgramName
) {
293 timeTraceProfilerInitialize(TimeTraceGranularity
, ProgramName
);
297 if (auto E
= timeTraceProfilerWrite(TimeTraceFile
, OutputFilename
)) {
298 handleAllErrors(std::move(E
), [&](const StringError
&SE
) {
299 errs() << SE
.getMessage() << "\n";
303 timeTraceProfilerCleanup();
308 // For use in NPM transition. Currently this contains most codegen-specific
309 // passes. Remove passes from here when porting to the NPM.
310 // TODO: use a codegen version of PassRegistry.def/PassBuilder::is*Pass() once
312 static bool shouldPinPassToLegacyPM(StringRef Pass
) {
313 std::vector
<StringRef
> PassNameExactToIgnore
= {
316 "amdgpu-simplifylib",
317 "amdgpu-image-intrinsic-opt",
319 "amdgpu-promote-alloca",
320 "amdgpu-promote-alloca-to-vector",
321 "amdgpu-lower-kernel-attributes",
322 "amdgpu-propagate-attributes-early",
323 "amdgpu-propagate-attributes-late",
324 "amdgpu-unify-metadata",
325 "amdgpu-printf-runtime-binding",
326 "amdgpu-always-inline"};
327 if (llvm::is_contained(PassNameExactToIgnore
, Pass
))
330 std::vector
<StringRef
> PassNamePrefix
= {
331 "x86-", "xcore-", "wasm-", "systemz-", "ppc-", "nvvm-",
332 "nvptx-", "mips-", "lanai-", "hexagon-", "bpf-", "avr-",
333 "thumb2-", "arm-", "si-", "gcn-", "amdgpu-", "aarch64-",
334 "amdgcn-", "polly-", "riscv-", "dxil-"};
335 std::vector
<StringRef
> PassNameContain
= {"ehprepare"};
336 std::vector
<StringRef
> PassNameExact
= {
340 "interleaved-load-combine",
341 "unreachableblockelim",
342 "verify-safepoint-ir",
345 "mve-tail-predication",
346 "interleaved-access",
348 "pre-isel-intrinsic-lowering",
355 "lower-amx-intrinsics",
357 "print-polyhedral-info",
358 "replace-with-veclib",
365 "expand-large-div-rem",
368 "expand-large-fp-convert",
371 for (const auto &P
: PassNamePrefix
)
372 if (Pass
.startswith(P
))
374 for (const auto &P
: PassNameContain
)
375 if (Pass
.contains(P
))
377 return llvm::is_contained(PassNameExact
, Pass
);
380 // For use in NPM transition.
381 static bool shouldForceLegacyPM() {
382 for (const auto &P
: PassList
) {
383 StringRef Arg
= P
->getPassArgument();
384 if (shouldPinPassToLegacyPM(Arg
))
390 //===----------------------------------------------------------------------===//
393 int main(int argc
, char **argv
) {
394 InitLLVM
X(argc
, argv
);
396 // Enable debug stream buffering.
397 EnableDebugBuffering
= true;
399 InitializeAllTargets();
400 InitializeAllTargetMCs();
401 InitializeAllAsmPrinters();
402 InitializeAllAsmParsers();
405 PassRegistry
&Registry
= *PassRegistry::getPassRegistry();
406 initializeCore(Registry
);
407 initializeScalarOpts(Registry
);
408 initializeVectorization(Registry
);
409 initializeIPO(Registry
);
410 initializeAnalysis(Registry
);
411 initializeTransformUtils(Registry
);
412 initializeInstCombine(Registry
);
413 initializeTarget(Registry
);
414 // For codegen passes, only passes that do IR to IR transformation are
416 initializeExpandLargeDivRemLegacyPassPass(Registry
);
417 initializeExpandLargeFpConvertLegacyPassPass(Registry
);
418 initializeExpandMemCmpPassPass(Registry
);
419 initializeScalarizeMaskedMemIntrinLegacyPassPass(Registry
);
420 initializeSelectOptimizePass(Registry
);
421 initializeCallBrPreparePass(Registry
);
422 initializeCodeGenPreparePass(Registry
);
423 initializeAtomicExpandPass(Registry
);
424 initializeWinEHPreparePass(Registry
);
425 initializeDwarfEHPrepareLegacyPassPass(Registry
);
426 initializeSafeStackLegacyPassPass(Registry
);
427 initializeSjLjEHPreparePass(Registry
);
428 initializePreISelIntrinsicLoweringLegacyPassPass(Registry
);
429 initializeGlobalMergePass(Registry
);
430 initializeIndirectBrExpandPassPass(Registry
);
431 initializeInterleavedLoadCombinePass(Registry
);
432 initializeInterleavedAccessPass(Registry
);
433 initializeUnreachableBlockElimLegacyPassPass(Registry
);
434 initializeExpandReductionsPass(Registry
);
435 initializeExpandVectorPredicationPass(Registry
);
436 initializeWasmEHPreparePass(Registry
);
437 initializeWriteBitcodePassPass(Registry
);
438 initializeReplaceWithVeclibLegacyPass(Registry
);
439 initializeJMCInstrumenterPass(Registry
);
441 SmallVector
<PassPlugin
, 1> PluginList
;
442 PassPlugins
.setCallback([&](const std::string
&PluginPath
) {
443 auto Plugin
= PassPlugin::Load(PluginPath
);
445 report_fatal_error(Plugin
.takeError(), /*gen_crash_diag=*/false);
446 PluginList
.emplace_back(Plugin
.get());
449 // Register the Target and CPU printer for --version.
450 cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU
);
452 cl::ParseCommandLineOptions(argc
, argv
,
453 "llvm .bc -> .bc modular optimizer and analysis printer\n");
457 // TODO: remove shouldForceLegacyPM().
458 const bool UseNPM
= (!EnableLegacyPassManager
&& !shouldForceLegacyPM()) ||
459 PassPipeline
.getNumOccurrences() > 0;
461 if (UseNPM
&& !PassList
.empty()) {
462 errs() << "The `opt -passname` syntax for the new pass manager is "
463 "not supported, please use `opt -passes=<pipeline>` (or the `-p` "
464 "alias for a more concise version).\n";
465 errs() << "See https://llvm.org/docs/NewPassManager.html#invoking-opt "
466 "for more details on the pass pipeline syntax.\n\n";
470 if (!UseNPM
&& PluginList
.size()) {
471 errs() << argv
[0] << ": " << PassPlugins
.ArgStr
472 << " specified with legacy PM.\n";
476 // FIXME: once the legacy PM code is deleted, move runPassPipeline() here and
477 // construct the PassBuilder before parsing IR so we can reuse the same
478 // PassBuilder for print passes.
484 TimeTracerRAII
TimeTracer(argv
[0]);
488 Context
.setDiscardValueNames(DiscardValueNames
);
489 if (!DisableDITypeMap
)
490 Context
.enableDebugTypeODRUniquing();
492 Expected
<std::unique_ptr
<ToolOutputFile
>> RemarksFileOrErr
=
493 setupLLVMOptimizationRemarks(Context
, RemarksFilename
, RemarksPasses
,
494 RemarksFormat
, RemarksWithHotness
,
495 RemarksHotnessThreshold
);
496 if (Error E
= RemarksFileOrErr
.takeError()) {
497 errs() << toString(std::move(E
)) << '\n';
500 std::unique_ptr
<ToolOutputFile
> RemarksFile
= std::move(*RemarksFileOrErr
);
502 // Load the input module...
503 auto SetDataLayout
= [&](StringRef IRTriple
,
504 StringRef IRLayout
) -> std::optional
<std::string
> {
505 // Data layout specified on the command line has the highest priority.
506 if (!ClDataLayout
.empty())
508 // If an explicit data layout is already defined in the IR, don't infer.
509 if (!IRLayout
.empty())
512 // If an explicit triple was specified (either in the IR or on the
513 // command line), use that to infer the default data layout. However, the
514 // command line target triple should override the IR file target triple.
515 std::string TripleStr
=
516 TargetTriple
.empty() ? IRTriple
.str() : Triple::normalize(TargetTriple
);
517 // If the triple string is still empty, we don't fall back to
518 // sys::getDefaultTargetTriple() since we do not want to have differing
519 // behaviour dependent on the configured default triple. Therefore, if the
520 // user did not pass -mtriple or define an explicit triple/datalayout in
521 // the IR, we should default to an empty (default) DataLayout.
522 if (TripleStr
.empty())
524 // Otherwise we infer the DataLayout from the target machine.
525 Expected
<std::unique_ptr
<TargetMachine
>> ExpectedTM
=
526 codegen::createTargetMachineForTriple(TripleStr
, GetCodeGenOptLevel());
528 errs() << argv
[0] << ": warning: failed to infer data layout: "
529 << toString(ExpectedTM
.takeError()) << "\n";
532 return (*ExpectedTM
)->createDataLayout().getStringRepresentation();
534 std::unique_ptr
<Module
> M
;
535 if (NoUpgradeDebugInfo
)
536 M
= parseAssemblyFileWithIndexNoUpgradeDebugInfo(
537 InputFilename
, Err
, Context
, nullptr, SetDataLayout
)
540 M
= parseIRFile(InputFilename
, Err
, Context
,
541 ParserCallbacks(SetDataLayout
));
544 Err
.print(argv
[0], errs());
548 // Strip debug info before running the verifier.
552 // Erase module-level named metadata, if requested.
553 if (StripNamedMetadata
) {
554 while (!M
->named_metadata_empty()) {
555 NamedMDNode
*NMD
= &*M
->named_metadata_begin();
556 M
->eraseNamedMetadata(NMD
);
560 // If we are supposed to override the target triple, do so now.
561 if (!TargetTriple
.empty())
562 M
->setTargetTriple(Triple::normalize(TargetTriple
));
564 // Immediately run the verifier to catch any problems before starting up the
565 // pass pipelines. Otherwise we can crash on broken code during
566 // doInitialization().
567 if (!NoVerify
&& verifyModule(*M
, &errs())) {
568 errs() << argv
[0] << ": " << InputFilename
569 << ": error: input module is broken!\n";
573 // Enable testing of whole program devirtualization on this module by invoking
574 // the facility for updating public visibility to linkage unit visibility when
575 // specified by an internal option. This is normally done during LTO which is
576 // not performed via opt.
577 updateVCallVisibilityInModule(
579 /*WholeProgramVisibilityEnabledInLTO=*/false,
580 // FIXME: These need linker information via a
581 // TBD new interface.
582 /*DynamicExportSymbols=*/{},
583 /*ValidateAllVtablesHaveTypeInfos=*/false,
584 /*IsVisibleToRegularObj=*/[](StringRef
) { return true; });
586 // Figure out what stream we are supposed to write to...
587 std::unique_ptr
<ToolOutputFile
> Out
;
588 std::unique_ptr
<ToolOutputFile
> ThinLinkOut
;
590 if (!OutputFilename
.empty())
591 errs() << "WARNING: The -o (output filename) option is ignored when\n"
592 "the --disable-output option is used.\n";
594 // Default to standard output.
595 if (OutputFilename
.empty())
596 OutputFilename
= "-";
599 sys::fs::OpenFlags Flags
=
600 OutputAssembly
? sys::fs::OF_TextWithCRLF
: sys::fs::OF_None
;
601 Out
.reset(new ToolOutputFile(OutputFilename
, EC
, Flags
));
603 errs() << EC
.message() << '\n';
607 if (!ThinLinkBitcodeFile
.empty()) {
609 new ToolOutputFile(ThinLinkBitcodeFile
, EC
, sys::fs::OF_None
));
611 errs() << EC
.message() << '\n';
617 Triple
ModuleTriple(M
->getTargetTriple());
618 std::string CPUStr
, FeaturesStr
;
619 std::unique_ptr
<TargetMachine
> TM
;
620 if (ModuleTriple
.getArch()) {
621 CPUStr
= codegen::getCPUStr();
622 FeaturesStr
= codegen::getFeaturesStr();
623 Expected
<std::unique_ptr
<TargetMachine
>> ExpectedTM
=
624 codegen::createTargetMachineForTriple(ModuleTriple
.str(),
625 GetCodeGenOptLevel());
626 if (auto E
= ExpectedTM
.takeError()) {
627 errs() << argv
[0] << ": WARNING: failed to create target machine for '"
628 << ModuleTriple
.str() << "': " << toString(std::move(E
)) << "\n";
630 TM
= std::move(*ExpectedTM
);
632 } else if (ModuleTriple
.getArchName() != "unknown" &&
633 ModuleTriple
.getArchName() != "") {
634 errs() << argv
[0] << ": unrecognized architecture '"
635 << ModuleTriple
.getArchName() << "' provided.\n";
639 // Override function attributes based on CPUStr, FeaturesStr, and command line
641 codegen::setFunctionAttributes(CPUStr
, FeaturesStr
, *M
);
643 // If the output is set to be emitted to standard out, and standard out is a
644 // console, print out a warning message and refuse to do it. We don't
645 // impress anyone by spewing tons of binary goo to a terminal.
646 if (!Force
&& !NoOutput
&& !OutputAssembly
)
647 if (CheckBitcodeOutputToConsole(Out
->os()))
650 if (OutputThinLTOBC
) {
651 M
->addModuleFlag(Module::Error
, "EnableSplitLTOUnit", SplitLTOUnit
);
653 M
->addModuleFlag(Module::Error
, "UnifiedLTO", 1);
656 // Add an appropriate TargetLibraryInfo pass for the module's triple.
657 TargetLibraryInfoImpl
TLII(ModuleTriple
);
659 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
660 if (DisableSimplifyLibCalls
)
661 TLII
.disableAllFunctions();
663 // Disable individual builtin functions in TargetLibraryInfo.
665 for (auto &FuncName
: DisableBuiltins
)
666 if (TLII
.getLibFunc(FuncName
, F
))
667 TLII
.setUnavailable(F
);
669 errs() << argv
[0] << ": cannot disable nonexistent builtin function "
676 if (legacy::debugPassSpecified()) {
677 errs() << "-debug-pass does not work with the new PM, either use "
678 "-debug-pass-manager, or use the legacy PM\n";
681 auto NumOLevel
= OptLevelO0
+ OptLevelO1
+ OptLevelO2
+ OptLevelO3
+
682 OptLevelOs
+ OptLevelOz
;
684 errs() << "Cannot specify multiple -O#\n";
687 if (NumOLevel
> 0 && (PassPipeline
.getNumOccurrences() > 0)) {
688 errs() << "Cannot specify -O# and --passes=/--foo-pass, use "
689 "-passes='default<O#>,other-pass'\n";
692 std::string Pipeline
= PassPipeline
;
695 Pipeline
= "default<O0>";
697 Pipeline
= "default<O1>";
699 Pipeline
= "default<O2>";
701 Pipeline
= "default<O3>";
703 Pipeline
= "default<Os>";
705 Pipeline
= "default<Oz>";
706 OutputKind OK
= OK_NoOutput
;
710 : (OutputThinLTOBC
? OK_OutputThinLTOBitcode
: OK_OutputBitcode
);
712 VerifierKind VK
= VK_VerifyOut
;
716 VK
= VK_VerifyEachPass
;
718 // The user has asked to use the new pass manager and provided a pipeline
719 // string. Hand off the rest of the functionality to the new code for that
721 return runPassPipeline(argv
[0], *M
, TM
.get(), &TLII
, Out
.get(),
722 ThinLinkOut
.get(), RemarksFile
.get(), Pipeline
,
723 PluginList
, OK
, VK
, PreserveAssemblyUseListOrder
,
724 PreserveBitcodeUseListOrder
, EmitSummaryIndex
,
725 EmitModuleHash
, EnableDebugify
,
726 VerifyDebugInfoPreserve
, UnifiedLTO
)
731 if (OptLevelO0
|| OptLevelO1
|| OptLevelO2
|| OptLevelOs
|| OptLevelOz
||
733 errs() << "Cannot use -O# with legacy PM.\n";
736 if (EmitSummaryIndex
) {
737 errs() << "Cannot use -module-summary with legacy PM.\n";
740 if (EmitModuleHash
) {
741 errs() << "Cannot use -module-hash with legacy PM.\n";
744 if (OutputThinLTOBC
) {
745 errs() << "Cannot use -thinlto-bc with legacy PM.\n";
748 // Create a PassManager to hold and optimize the collection of passes we are
749 // about to build. If the -debugify-each option is set, wrap each pass with
750 // the (-check)-debugify passes.
751 DebugifyCustomPassManager Passes
;
752 DebugifyStatsMap DIStatsMap
;
753 DebugInfoPerPass DebugInfoBeforePass
;
755 Passes
.setDebugifyMode(DebugifyMode::SyntheticDebugInfo
);
756 Passes
.setDIStatsMap(DIStatsMap
);
757 } else if (VerifyEachDebugInfoPreserve
) {
758 Passes
.setDebugifyMode(DebugifyMode::OriginalDebugInfo
);
759 Passes
.setDebugInfoBeforePass(DebugInfoBeforePass
);
760 if (!VerifyDIPreserveExport
.empty())
761 Passes
.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport
);
764 bool AddOneTimeDebugifyPasses
=
765 (EnableDebugify
&& !DebugifyEach
) ||
766 (VerifyDebugInfoPreserve
&& !VerifyEachDebugInfoPreserve
);
768 Passes
.add(new TargetLibraryInfoWrapperPass(TLII
));
770 // Add internal analysis passes from the target machine.
771 Passes
.add(createTargetTransformInfoWrapperPass(TM
? TM
->getTargetIRAnalysis()
772 : TargetIRAnalysis()));
774 if (AddOneTimeDebugifyPasses
) {
775 if (EnableDebugify
) {
776 Passes
.setDIStatsMap(DIStatsMap
);
777 Passes
.add(createDebugifyModulePass());
778 } else if (VerifyDebugInfoPreserve
) {
779 Passes
.setDebugInfoBeforePass(DebugInfoBeforePass
);
780 Passes
.add(createDebugifyModulePass(
781 DebugifyMode::OriginalDebugInfo
, "",
782 &(Passes
.getDebugInfoPerPass())));
787 // FIXME: We should dyn_cast this when supported.
788 auto <M
= static_cast<LLVMTargetMachine
&>(*TM
);
789 Pass
*TPC
= LTM
.createPassConfig(Passes
);
793 // Create a new optimization pass for each one specified on the command line
794 for (unsigned i
= 0; i
< PassList
.size(); ++i
) {
795 const PassInfo
*PassInf
= PassList
[i
];
796 if (PassInf
->getNormalCtor()) {
797 Pass
*P
= PassInf
->getNormalCtor()();
799 // Add the pass to the pass manager.
801 // If we are verifying all of the intermediate steps, add the verifier.
803 Passes
.add(createVerifierPass());
806 errs() << argv
[0] << ": cannot create pass: "
807 << PassInf
->getPassName() << "\n";
810 // Check that the module is well formed on completion of optimization
811 if (!NoVerify
&& !VerifyEach
)
812 Passes
.add(createVerifierPass());
814 if (AddOneTimeDebugifyPasses
) {
816 Passes
.add(createCheckDebugifyModulePass(false));
817 else if (VerifyDebugInfoPreserve
) {
818 if (!VerifyDIPreserveExport
.empty())
819 Passes
.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport
);
820 Passes
.add(createCheckDebugifyModulePass(
821 false, "", nullptr, DebugifyMode::OriginalDebugInfo
,
822 &(Passes
.getDebugInfoPerPass()), VerifyDIPreserveExport
));
826 // In run twice mode, we want to make sure the output is bit-by-bit
827 // equivalent if we run the pass manager again, so setup two buffers and
828 // a stream to write to them. Note that llc does something similar and it
829 // may be worth to abstract this out in the future.
830 SmallVector
<char, 0> Buffer
;
831 SmallVector
<char, 0> FirstRunBuffer
;
832 std::unique_ptr
<raw_svector_ostream
> BOS
;
833 raw_ostream
*OS
= nullptr;
835 const bool ShouldEmitOutput
= !NoOutput
;
837 // Write bitcode or assembly to the output as the last step...
838 if (ShouldEmitOutput
|| RunTwice
) {
842 BOS
= std::make_unique
<raw_svector_ostream
>(Buffer
);
846 Passes
.add(createPrintModulePass(*OS
, "", PreserveAssemblyUseListOrder
));
848 Passes
.add(createBitcodeWriterPass(*OS
, PreserveBitcodeUseListOrder
));
851 // Before executing passes, print the final values of the LLVM options.
852 cl::PrintOptionValues();
855 // Now that we have all of the passes ready, run them.
858 // If requested, run all passes twice with the same pass manager to catch
859 // bugs caused by persistent state in the passes.
860 std::unique_ptr
<Module
> M2(CloneModule(*M
));
861 // Run all passes on the original module first, so the second run processes
862 // the clone to catch CloneModule bugs.
864 FirstRunBuffer
= Buffer
;
869 // Compare the two outputs and make sure they're the same
871 if (Buffer
.size() != FirstRunBuffer
.size() ||
872 (memcmp(Buffer
.data(), FirstRunBuffer
.data(), Buffer
.size()) != 0)) {
874 << "Running the pass manager twice changed the output.\n"
875 "Writing the result of the second run to the specified output.\n"
876 "To generate the one-run comparison binary, just run without\n"
877 "the compile-twice option\n";
878 if (ShouldEmitOutput
) {
879 Out
->os() << BOS
->str();
886 if (ShouldEmitOutput
)
887 Out
->os() << BOS
->str();
890 if (DebugifyEach
&& !DebugifyExport
.empty())
891 exportDebugifyStats(DebugifyExport
, Passes
.getDebugifyStatsMap());