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 "BreakpointPrinter.h"
16 #include "NewPMDriver.h"
17 #include "PassPrinters.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Analysis/CallGraph.h"
20 #include "llvm/Analysis/CallGraphSCCPass.h"
21 #include "llvm/Analysis/LoopPass.h"
22 #include "llvm/Analysis/RegionPass.h"
23 #include "llvm/Analysis/TargetLibraryInfo.h"
24 #include "llvm/Analysis/TargetTransformInfo.h"
25 #include "llvm/Bitcode/BitcodeWriterPass.h"
26 #include "llvm/CodeGen/CommandFlags.inc"
27 #include "llvm/CodeGen/TargetPassConfig.h"
28 #include "llvm/Config/llvm-config.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/DebugInfo.h"
31 #include "llvm/IR/IRPrintingPasses.h"
32 #include "llvm/IR/LLVMContext.h"
33 #include "llvm/IR/LegacyPassManager.h"
34 #include "llvm/IR/LegacyPassNameParser.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/IR/Verifier.h"
37 #include "llvm/IRReader/IRReader.h"
38 #include "llvm/InitializePasses.h"
39 #include "llvm/LinkAllIR.h"
40 #include "llvm/LinkAllPasses.h"
41 #include "llvm/MC/SubtargetFeature.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Support/FileSystem.h"
44 #include "llvm/Support/Host.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/TargetRegistry.h"
50 #include "llvm/Support/TargetSelect.h"
51 #include "llvm/Support/ToolOutputFile.h"
52 #include "llvm/Support/YAMLTraits.h"
53 #include "llvm/Target/TargetMachine.h"
54 #include "llvm/Transforms/Coroutines.h"
55 #include "llvm/Transforms/IPO/AlwaysInliner.h"
56 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
57 #include "llvm/Transforms/Utils/Cloning.h"
61 using namespace opt_tool
;
63 // The OptimizationList is automatically populated with registered Passes by the
66 static cl::list
<const PassInfo
*, bool, PassNameParser
>
67 PassList(cl::desc("Optimizations available:"));
69 // This flag specifies a textual description of the optimization pass pipeline
70 // to run over the module. This flag switches opt to use the new pass manager
71 // infrastructure, completely disabling all of the flags specific to the old
73 static cl::opt
<std::string
> PassPipeline(
75 cl::desc("A textual description of the pass pipeline for optimizing"),
78 // Other command line options...
80 static cl::opt
<std::string
>
81 InputFilename(cl::Positional
, cl::desc("<input bitcode file>"),
82 cl::init("-"), cl::value_desc("filename"));
84 static cl::opt
<std::string
>
85 OutputFilename("o", cl::desc("Override output filename"),
86 cl::value_desc("filename"));
89 Force("f", cl::desc("Enable binary output on terminals"));
92 PrintEachXForm("p", cl::desc("Print module after each transformation"));
95 NoOutput("disable-output",
96 cl::desc("Do not write result bitcode file"), cl::Hidden
);
99 OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
102 OutputThinLTOBC("thinlto-bc",
103 cl::desc("Write output as ThinLTO-ready bitcode"));
106 SplitLTOUnit("thinlto-split-lto-unit",
107 cl::desc("Enable splitting of a ThinLTO LTOUnit"));
109 static cl::opt
<std::string
> ThinLinkBitcodeFile(
110 "thin-link-bitcode-file", cl::value_desc("filename"),
112 "A file in which to write minimized bitcode for the thin link only"));
115 NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden
);
118 VerifyEach("verify-each", cl::desc("Verify after each transform"));
121 DisableDITypeMap("disable-debug-info-type-map",
122 cl::desc("Don't use a uniquing type map for debug info"));
125 StripDebug("strip-debug",
126 cl::desc("Strip debugger symbol info from translation unit"));
129 StripNamedMetadata("strip-named-metadata",
130 cl::desc("Strip module-level named metadata"));
132 static cl::opt
<bool> DisableInline("disable-inlining",
133 cl::desc("Do not run the inliner pass"));
136 DisableOptimizations("disable-opt",
137 cl::desc("Do not run any optimization passes"));
140 StandardLinkOpts("std-link-opts",
141 cl::desc("Include the standard link time optimizations"));
145 cl::desc("Optimization level 0. Similar to clang -O0"));
149 cl::desc("Optimization level 1. Similar to clang -O1"));
153 cl::desc("Optimization level 2. Similar to clang -O2"));
157 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
161 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
165 cl::desc("Optimization level 3. Similar to clang -O3"));
167 static cl::opt
<unsigned>
168 CodeGenOptLevel("codegen-opt-level",
169 cl::desc("Override optimization level for codegen hooks"));
171 static cl::opt
<std::string
>
172 TargetTriple("mtriple", cl::desc("Override target triple for module"));
175 UnitAtATime("funit-at-a-time",
176 cl::desc("Enable IPO. This corresponds to gcc's -funit-at-a-time"),
180 DisableLoopUnrolling("disable-loop-unrolling",
181 cl::desc("Disable loop unrolling in all relevant passes"),
184 DisableLoopVectorization("disable-loop-vectorization",
185 cl::desc("Disable the loop vectorization pass"),
189 DisableSLPVectorization("disable-slp-vectorization",
190 cl::desc("Disable the slp vectorization pass"),
193 static cl::opt
<bool> EmitSummaryIndex("module-summary",
194 cl::desc("Emit module summary index"),
197 static cl::opt
<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
201 DisableSimplifyLibCalls("disable-simplify-libcalls",
202 cl::desc("Disable simplify-libcalls"));
205 Quiet("q", cl::desc("Obsolete option"), cl::Hidden
);
208 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet
));
211 AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
213 static cl::opt
<bool> EnableDebugify(
216 "Start the pipeline with debugify and end it with check-debugify"));
218 static cl::opt
<bool> DebugifyEach(
221 "Start each pass with debugify and end it with check-debugify"));
223 static cl::opt
<std::string
>
224 DebugifyExport("debugify-export",
225 cl::desc("Export per-pass debugify statistics to this file"),
226 cl::value_desc("filename"), cl::init(""));
229 PrintBreakpoints("print-breakpoints-for-testing",
230 cl::desc("Print select breakpoints location for testing"));
232 static cl::opt
<std::string
> ClDataLayout("data-layout",
233 cl::desc("data layout string to use"),
234 cl::value_desc("layout-string"),
237 static cl::opt
<bool> PreserveBitcodeUseListOrder(
238 "preserve-bc-uselistorder",
239 cl::desc("Preserve use-list order when writing LLVM bitcode."),
240 cl::init(true), cl::Hidden
);
242 static cl::opt
<bool> PreserveAssemblyUseListOrder(
243 "preserve-ll-uselistorder",
244 cl::desc("Preserve use-list order when writing LLVM assembly."),
245 cl::init(false), cl::Hidden
);
248 RunTwice("run-twice",
249 cl::desc("Run all passes twice, re-using the same pass manager."),
250 cl::init(false), cl::Hidden
);
252 static cl::opt
<bool> DiscardValueNames(
253 "discard-value-names",
254 cl::desc("Discard names from Value (other than GlobalValue)."),
255 cl::init(false), cl::Hidden
);
257 static cl::opt
<bool> Coroutines(
259 cl::desc("Enable coroutine passes."),
260 cl::init(false), cl::Hidden
);
262 static cl::opt
<bool> PassRemarksWithHotness(
263 "pass-remarks-with-hotness",
264 cl::desc("With PGO, include profile count in optimization remarks"),
267 static cl::opt
<unsigned> PassRemarksHotnessThreshold(
268 "pass-remarks-hotness-threshold",
269 cl::desc("Minimum profile count required for an optimization remark to be output"),
272 static cl::opt
<std::string
>
273 RemarksFilename("pass-remarks-output",
274 cl::desc("YAML output filename for pass remarks"),
275 cl::value_desc("filename"));
278 PGOKindFlag("pgo-kind", cl::init(NoPGO
), cl::Hidden
,
279 cl::desc("The kind of profile guided optimization"),
280 cl::values(clEnumValN(NoPGO
, "nopgo", "Do not use PGO."),
281 clEnumValN(InstrGen
, "pgo-instr-gen-pipeline",
282 "Instrument the IR to generate profile."),
283 clEnumValN(InstrUse
, "pgo-instr-use-pipeline",
284 "Use instrumented profile to guide PGO."),
285 clEnumValN(SampleUse
, "pgo-sample-use-pipeline",
286 "Use sampled profile to guide PGO.")));
287 cl::opt
<std::string
> ProfileFile("profile-file",
288 cl::desc("Path to the profile."), cl::Hidden
);
290 class OptCustomPassManager
: public legacy::PassManager
{
291 DebugifyStatsMap DIStatsMap
;
294 using super
= legacy::PassManager
;
296 void add(Pass
*P
) override
{
297 // Wrap each pass with (-check)-debugify passes if requested, making
298 // exceptions for passes which shouldn't see -debugify instrumentation.
299 bool WrapWithDebugify
= DebugifyEach
&& !P
->getAsImmutablePass() &&
300 !isIRPrintingPass(P
) && !isBitcodeWriterPass(P
);
301 if (!WrapWithDebugify
) {
306 // Apply -debugify/-check-debugify before/after each pass and collect
307 // debug info loss statistics.
308 PassKind Kind
= P
->getPassKind();
309 StringRef Name
= P
->getPassName();
311 // TODO: Implement Debugify for BasicBlockPass, LoopPass.
314 super::add(createDebugifyFunctionPass());
316 super::add(createCheckDebugifyFunctionPass(true, Name
, &DIStatsMap
));
319 super::add(createDebugifyModulePass());
321 super::add(createCheckDebugifyModulePass(true, Name
, &DIStatsMap
));
329 const DebugifyStatsMap
&getDebugifyStatsMap() const { return DIStatsMap
; }
332 static inline void addPass(legacy::PassManagerBase
&PM
, Pass
*P
) {
333 // Add the pass to the pass manager...
336 // If we are verifying all of the intermediate steps, add the verifier...
338 PM
.add(createVerifierPass());
341 /// This routine adds optimization passes based on selected optimization level,
344 /// OptLevel - Optimization Level
345 static void AddOptimizationPasses(legacy::PassManagerBase
&MPM
,
346 legacy::FunctionPassManager
&FPM
,
347 TargetMachine
*TM
, unsigned OptLevel
,
348 unsigned SizeLevel
) {
349 if (!NoVerify
|| VerifyEach
)
350 FPM
.add(createVerifierPass()); // Verify that input is correct
352 PassManagerBuilder Builder
;
353 Builder
.OptLevel
= OptLevel
;
354 Builder
.SizeLevel
= SizeLevel
;
358 } else if (OptLevel
> 1) {
359 Builder
.Inliner
= createFunctionInliningPass(OptLevel
, SizeLevel
, false);
361 Builder
.Inliner
= createAlwaysInlinerLegacyPass();
363 Builder
.DisableUnitAtATime
= !UnitAtATime
;
364 Builder
.DisableUnrollLoops
= (DisableLoopUnrolling
.getNumOccurrences() > 0) ?
365 DisableLoopUnrolling
: OptLevel
== 0;
367 // This is final, unless there is a #pragma vectorize enable
368 if (DisableLoopVectorization
)
369 Builder
.LoopVectorize
= false;
370 // If option wasn't forced via cmd line (-vectorize-loops, -loop-vectorize)
371 else if (!Builder
.LoopVectorize
)
372 Builder
.LoopVectorize
= OptLevel
> 1 && SizeLevel
< 2;
374 // When #pragma vectorize is on for SLP, do the same as above
375 Builder
.SLPVectorize
=
376 DisableSLPVectorization
? false : OptLevel
> 1 && SizeLevel
< 2;
379 TM
->adjustPassManager(Builder
);
382 addCoroutinePassesToExtensionPoints(Builder
);
384 switch (PGOKindFlag
) {
386 Builder
.EnablePGOInstrGen
= true;
387 Builder
.PGOInstrGen
= ProfileFile
;
390 Builder
.PGOInstrUse
= ProfileFile
;
393 Builder
.PGOSampleUse
= ProfileFile
;
399 Builder
.populateFunctionPassManager(FPM
);
400 Builder
.populateModulePassManager(MPM
);
403 static void AddStandardLinkPasses(legacy::PassManagerBase
&PM
) {
404 PassManagerBuilder Builder
;
405 Builder
.VerifyInput
= true;
406 if (DisableOptimizations
)
407 Builder
.OptLevel
= 0;
410 Builder
.Inliner
= createFunctionInliningPass();
411 Builder
.populateLTOPassManager(PM
);
414 //===----------------------------------------------------------------------===//
415 // CodeGen-related helper functions.
418 static CodeGenOpt::Level
GetCodeGenOptLevel() {
419 if (CodeGenOptLevel
.getNumOccurrences())
420 return static_cast<CodeGenOpt::Level
>(unsigned(CodeGenOptLevel
));
422 return CodeGenOpt::Less
;
424 return CodeGenOpt::Default
;
426 return CodeGenOpt::Aggressive
;
427 return CodeGenOpt::None
;
430 // Returns the TargetMachine instance or zero if no triple is provided.
431 static TargetMachine
* GetTargetMachine(Triple TheTriple
, StringRef CPUStr
,
432 StringRef FeaturesStr
,
433 const TargetOptions
&Options
) {
435 const Target
*TheTarget
= TargetRegistry::lookupTarget(MArch
, TheTriple
,
437 // Some modules don't specify a triple, and this is okay.
442 return TheTarget
->createTargetMachine(TheTriple
.getTriple(), CPUStr
,
443 FeaturesStr
, Options
, getRelocModel(),
444 getCodeModel(), GetCodeGenOptLevel());
447 #ifdef LINK_POLLY_INTO_TOOLS
449 void initializePollyPasses(llvm::PassRegistry
&Registry
);
453 //===----------------------------------------------------------------------===//
456 int main(int argc
, char **argv
) {
457 InitLLVM
X(argc
, argv
);
459 // Enable debug stream buffering.
460 EnableDebugBuffering
= true;
464 InitializeAllTargets();
465 InitializeAllTargetMCs();
466 InitializeAllAsmPrinters();
467 InitializeAllAsmParsers();
470 PassRegistry
&Registry
= *PassRegistry::getPassRegistry();
471 initializeCore(Registry
);
472 initializeCoroutines(Registry
);
473 initializeScalarOpts(Registry
);
474 initializeObjCARCOpts(Registry
);
475 initializeVectorization(Registry
);
476 initializeIPO(Registry
);
477 initializeAnalysis(Registry
);
478 initializeTransformUtils(Registry
);
479 initializeInstCombine(Registry
);
480 initializeAggressiveInstCombine(Registry
);
481 initializeInstrumentation(Registry
);
482 initializeTarget(Registry
);
483 // For codegen passes, only passes that do IR to IR transformation are
485 initializeExpandMemCmpPassPass(Registry
);
486 initializeScalarizeMaskedMemIntrinPass(Registry
);
487 initializeCodeGenPreparePass(Registry
);
488 initializeAtomicExpandPass(Registry
);
489 initializeRewriteSymbolsLegacyPassPass(Registry
);
490 initializeWinEHPreparePass(Registry
);
491 initializeDwarfEHPreparePass(Registry
);
492 initializeSafeStackLegacyPassPass(Registry
);
493 initializeSjLjEHPreparePass(Registry
);
494 initializePreISelIntrinsicLoweringLegacyPassPass(Registry
);
495 initializeGlobalMergePass(Registry
);
496 initializeIndirectBrExpandPassPass(Registry
);
497 initializeInterleavedLoadCombinePass(Registry
);
498 initializeInterleavedAccessPass(Registry
);
499 initializeEntryExitInstrumenterPass(Registry
);
500 initializePostInlineEntryExitInstrumenterPass(Registry
);
501 initializeUnreachableBlockElimLegacyPassPass(Registry
);
502 initializeExpandReductionsPass(Registry
);
503 initializeWasmEHPreparePass(Registry
);
504 initializeWriteBitcodePassPass(Registry
);
506 #ifdef LINK_POLLY_INTO_TOOLS
507 polly::initializePollyPasses(Registry
);
510 cl::ParseCommandLineOptions(argc
, argv
,
511 "llvm .bc -> .bc modular optimizer and analysis printer\n");
513 if (AnalyzeOnly
&& NoOutput
) {
514 errs() << argv
[0] << ": analyze mode conflicts with no-output mode.\n";
520 Context
.setDiscardValueNames(DiscardValueNames
);
521 if (!DisableDITypeMap
)
522 Context
.enableDebugTypeODRUniquing();
524 if (PassRemarksWithHotness
)
525 Context
.setDiagnosticsHotnessRequested(true);
527 if (PassRemarksHotnessThreshold
)
528 Context
.setDiagnosticsHotnessThreshold(PassRemarksHotnessThreshold
);
530 std::unique_ptr
<ToolOutputFile
> OptRemarkFile
;
531 if (RemarksFilename
!= "") {
534 llvm::make_unique
<ToolOutputFile
>(RemarksFilename
, EC
, sys::fs::F_None
);
536 errs() << EC
.message() << '\n';
539 Context
.setDiagnosticsOutputFile(
540 llvm::make_unique
<yaml::Output
>(OptRemarkFile
->os()));
543 // Load the input module...
544 std::unique_ptr
<Module
> M
=
545 parseIRFile(InputFilename
, Err
, Context
, !NoVerify
, ClDataLayout
);
548 Err
.print(argv
[0], errs());
552 // Strip debug info before running the verifier.
556 // Erase module-level named metadata, if requested.
557 if (StripNamedMetadata
) {
558 while (!M
->named_metadata_empty()) {
559 NamedMDNode
*NMD
= &*M
->named_metadata_begin();
560 M
->eraseNamedMetadata(NMD
);
564 // If we are supposed to override the target triple or data layout, do so now.
565 if (!TargetTriple
.empty())
566 M
->setTargetTriple(Triple::normalize(TargetTriple
));
568 // Immediately run the verifier to catch any problems before starting up the
569 // pass pipelines. Otherwise we can crash on broken code during
570 // doInitialization().
571 if (!NoVerify
&& verifyModule(*M
, &errs())) {
572 errs() << argv
[0] << ": " << InputFilename
573 << ": error: input module is broken!\n";
577 // Figure out what stream we are supposed to write to...
578 std::unique_ptr
<ToolOutputFile
> Out
;
579 std::unique_ptr
<ToolOutputFile
> ThinLinkOut
;
581 if (!OutputFilename
.empty())
582 errs() << "WARNING: The -o (output filename) option is ignored when\n"
583 "the --disable-output option is used.\n";
585 // Default to standard output.
586 if (OutputFilename
.empty())
587 OutputFilename
= "-";
590 Out
.reset(new ToolOutputFile(OutputFilename
, EC
, sys::fs::F_None
));
592 errs() << EC
.message() << '\n';
596 if (!ThinLinkBitcodeFile
.empty()) {
598 new ToolOutputFile(ThinLinkBitcodeFile
, EC
, sys::fs::F_None
));
600 errs() << EC
.message() << '\n';
606 Triple
ModuleTriple(M
->getTargetTriple());
607 std::string CPUStr
, FeaturesStr
;
608 TargetMachine
*Machine
= nullptr;
609 const TargetOptions Options
= InitTargetOptionsFromCodeGenFlags();
611 if (ModuleTriple
.getArch()) {
612 CPUStr
= getCPUStr();
613 FeaturesStr
= getFeaturesStr();
614 Machine
= GetTargetMachine(ModuleTriple
, CPUStr
, FeaturesStr
, Options
);
617 std::unique_ptr
<TargetMachine
> TM(Machine
);
619 // Override function attributes based on CPUStr, FeaturesStr, and command line
621 setFunctionAttributes(CPUStr
, FeaturesStr
, *M
);
623 // If the output is set to be emitted to standard out, and standard out is a
624 // console, print out a warning message and refuse to do it. We don't
625 // impress anyone by spewing tons of binary goo to a terminal.
626 if (!Force
&& !NoOutput
&& !AnalyzeOnly
&& !OutputAssembly
)
627 if (CheckBitcodeOutputToConsole(Out
->os(), !Quiet
))
631 M
->addModuleFlag(Module::Error
, "EnableSplitLTOUnit", SplitLTOUnit
);
633 if (PassPipeline
.getNumOccurrences() > 0) {
634 OutputKind OK
= OK_NoOutput
;
638 : (OutputThinLTOBC
? OK_OutputThinLTOBitcode
: OK_OutputBitcode
);
640 VerifierKind VK
= VK_VerifyInAndOut
;
644 VK
= VK_VerifyEachPass
;
646 // The user has asked to use the new pass manager and provided a pipeline
647 // string. Hand off the rest of the functionality to the new code for that
649 return runPassPipeline(argv
[0], *M
, TM
.get(), Out
.get(), ThinLinkOut
.get(),
650 OptRemarkFile
.get(), PassPipeline
, OK
, VK
,
651 PreserveAssemblyUseListOrder
,
652 PreserveBitcodeUseListOrder
, EmitSummaryIndex
,
653 EmitModuleHash
, EnableDebugify
)
658 // Create a PassManager to hold and optimize the collection of passes we are
660 OptCustomPassManager Passes
;
661 bool AddOneTimeDebugifyPasses
= EnableDebugify
&& !DebugifyEach
;
663 // Add an appropriate TargetLibraryInfo pass for the module's triple.
664 TargetLibraryInfoImpl
TLII(ModuleTriple
);
666 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
667 if (DisableSimplifyLibCalls
)
668 TLII
.disableAllFunctions();
669 Passes
.add(new TargetLibraryInfoWrapperPass(TLII
));
671 // Add internal analysis passes from the target machine.
672 Passes
.add(createTargetTransformInfoWrapperPass(TM
? TM
->getTargetIRAnalysis()
673 : TargetIRAnalysis()));
675 if (AddOneTimeDebugifyPasses
)
676 Passes
.add(createDebugifyModulePass());
678 std::unique_ptr
<legacy::FunctionPassManager
> FPasses
;
679 if (OptLevelO0
|| OptLevelO1
|| OptLevelO2
|| OptLevelOs
|| OptLevelOz
||
681 FPasses
.reset(new legacy::FunctionPassManager(M
.get()));
682 FPasses
->add(createTargetTransformInfoWrapperPass(
683 TM
? TM
->getTargetIRAnalysis() : TargetIRAnalysis()));
686 if (PrintBreakpoints
) {
687 // Default to standard output.
689 if (OutputFilename
.empty())
690 OutputFilename
= "-";
693 Out
= llvm::make_unique
<ToolOutputFile
>(OutputFilename
, EC
,
696 errs() << EC
.message() << '\n';
700 Passes
.add(createBreakpointPrinter(Out
->os()));
705 // FIXME: We should dyn_cast this when supported.
706 auto <M
= static_cast<LLVMTargetMachine
&>(*TM
);
707 Pass
*TPC
= LTM
.createPassConfig(Passes
);
711 // Create a new optimization pass for each one specified on the command line
712 for (unsigned i
= 0; i
< PassList
.size(); ++i
) {
713 if (StandardLinkOpts
&&
714 StandardLinkOpts
.getPosition() < PassList
.getPosition(i
)) {
715 AddStandardLinkPasses(Passes
);
716 StandardLinkOpts
= false;
719 if (OptLevelO0
&& OptLevelO0
.getPosition() < PassList
.getPosition(i
)) {
720 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 0, 0);
724 if (OptLevelO1
&& OptLevelO1
.getPosition() < PassList
.getPosition(i
)) {
725 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 1, 0);
729 if (OptLevelO2
&& OptLevelO2
.getPosition() < PassList
.getPosition(i
)) {
730 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 2, 0);
734 if (OptLevelOs
&& OptLevelOs
.getPosition() < PassList
.getPosition(i
)) {
735 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 2, 1);
739 if (OptLevelOz
&& OptLevelOz
.getPosition() < PassList
.getPosition(i
)) {
740 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 2, 2);
744 if (OptLevelO3
&& OptLevelO3
.getPosition() < PassList
.getPosition(i
)) {
745 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 3, 0);
749 const PassInfo
*PassInf
= PassList
[i
];
751 if (PassInf
->getNormalCtor())
752 P
= PassInf
->getNormalCtor()();
754 errs() << argv
[0] << ": cannot create pass: "
755 << PassInf
->getPassName() << "\n";
757 PassKind Kind
= P
->getPassKind();
763 Passes
.add(createBasicBlockPassPrinter(PassInf
, Out
->os(), Quiet
));
766 Passes
.add(createRegionPassPrinter(PassInf
, Out
->os(), Quiet
));
769 Passes
.add(createLoopPassPrinter(PassInf
, Out
->os(), Quiet
));
772 Passes
.add(createFunctionPassPrinter(PassInf
, Out
->os(), Quiet
));
774 case PT_CallGraphSCC
:
775 Passes
.add(createCallGraphPassPrinter(PassInf
, Out
->os(), Quiet
));
778 Passes
.add(createModulePassPrinter(PassInf
, Out
->os(), Quiet
));
786 createPrintModulePass(errs(), "", PreserveAssemblyUseListOrder
));
789 if (StandardLinkOpts
) {
790 AddStandardLinkPasses(Passes
);
791 StandardLinkOpts
= false;
795 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 0, 0);
798 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 1, 0);
801 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 2, 0);
804 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 2, 1);
807 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 2, 2);
810 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 3, 0);
813 FPasses
->doInitialization();
814 for (Function
&F
: *M
)
816 FPasses
->doFinalization();
819 // Check that the module is well formed on completion of optimization
820 if (!NoVerify
&& !VerifyEach
)
821 Passes
.add(createVerifierPass());
823 if (AddOneTimeDebugifyPasses
)
824 Passes
.add(createCheckDebugifyModulePass(false));
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 // Write bitcode or assembly to the output as the last step...
836 if (!NoOutput
&& !AnalyzeOnly
) {
840 BOS
= make_unique
<raw_svector_ostream
>(Buffer
);
843 if (OutputAssembly
) {
844 if (EmitSummaryIndex
)
845 report_fatal_error("Text output is incompatible with -module-summary");
847 report_fatal_error("Text output is incompatible with -module-hash");
848 Passes
.add(createPrintModulePass(*OS
, "", PreserveAssemblyUseListOrder
));
849 } else if (OutputThinLTOBC
)
850 Passes
.add(createWriteThinLTOBitcodePass(
851 *OS
, ThinLinkOut
? &ThinLinkOut
->os() : nullptr));
853 Passes
.add(createBitcodeWriterPass(*OS
, PreserveBitcodeUseListOrder
,
854 EmitSummaryIndex
, EmitModuleHash
));
857 // Before executing passes, print the final values of the LLVM options.
858 cl::PrintOptionValues();
861 // Now that we have all of the passes ready, run them.
864 // If requested, run all passes twice with the same pass manager to catch
865 // bugs caused by persistent state in the passes.
866 std::unique_ptr
<Module
> M2(CloneModule(*M
));
867 // Run all passes on the original module first, so the second run processes
868 // the clone to catch CloneModule bugs.
870 FirstRunBuffer
= Buffer
;
875 // Compare the two outputs and make sure they're the same
877 if (Buffer
.size() != FirstRunBuffer
.size() ||
878 (memcmp(Buffer
.data(), FirstRunBuffer
.data(), Buffer
.size()) != 0)) {
880 << "Running the pass manager twice changed the output.\n"
881 "Writing the result of the second run to the specified output.\n"
882 "To generate the one-run comparison binary, just run without\n"
883 "the compile-twice option\n";
884 Out
->os() << BOS
->str();
887 OptRemarkFile
->keep();
890 Out
->os() << BOS
->str();
893 if (DebugifyEach
&& !DebugifyExport
.empty())
894 exportDebugifyStats(DebugifyExport
, Passes
.getDebugifyStatsMap());
897 if (!NoOutput
|| PrintBreakpoints
)
901 OptRemarkFile
->keep();