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/RemarkStreamer.h"
37 #include "llvm/IR/Verifier.h"
38 #include "llvm/IRReader/IRReader.h"
39 #include "llvm/InitializePasses.h"
40 #include "llvm/LinkAllIR.h"
41 #include "llvm/LinkAllPasses.h"
42 #include "llvm/MC/SubtargetFeature.h"
43 #include "llvm/Support/Debug.h"
44 #include "llvm/Support/FileSystem.h"
45 #include "llvm/Support/Host.h"
46 #include "llvm/Support/InitLLVM.h"
47 #include "llvm/Support/PluginLoader.h"
48 #include "llvm/Support/SourceMgr.h"
49 #include "llvm/Support/SystemUtils.h"
50 #include "llvm/Support/TargetRegistry.h"
51 #include "llvm/Support/TargetSelect.h"
52 #include "llvm/Support/ToolOutputFile.h"
53 #include "llvm/Support/YAMLTraits.h"
54 #include "llvm/Target/TargetMachine.h"
55 #include "llvm/Transforms/Coroutines.h"
56 #include "llvm/Transforms/IPO/AlwaysInliner.h"
57 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
58 #include "llvm/Transforms/Utils/Cloning.h"
62 using namespace opt_tool
;
64 // The OptimizationList is automatically populated with registered Passes by the
67 static cl::list
<const PassInfo
*, bool, PassNameParser
>
68 PassList(cl::desc("Optimizations available:"));
70 // This flag specifies a textual description of the optimization pass pipeline
71 // to run over the module. This flag switches opt to use the new pass manager
72 // infrastructure, completely disabling all of the flags specific to the old
74 static cl::opt
<std::string
> PassPipeline(
76 cl::desc("A textual description of the pass pipeline for optimizing"),
79 // Other command line options...
81 static cl::opt
<std::string
>
82 InputFilename(cl::Positional
, cl::desc("<input bitcode file>"),
83 cl::init("-"), cl::value_desc("filename"));
85 static cl::opt
<std::string
>
86 OutputFilename("o", cl::desc("Override output filename"),
87 cl::value_desc("filename"));
90 Force("f", cl::desc("Enable binary output on terminals"));
93 PrintEachXForm("p", cl::desc("Print module after each transformation"));
96 NoOutput("disable-output",
97 cl::desc("Do not write result bitcode file"), cl::Hidden
);
100 OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
103 OutputThinLTOBC("thinlto-bc",
104 cl::desc("Write output as ThinLTO-ready bitcode"));
107 SplitLTOUnit("thinlto-split-lto-unit",
108 cl::desc("Enable splitting of a ThinLTO LTOUnit"));
110 static cl::opt
<std::string
> ThinLinkBitcodeFile(
111 "thin-link-bitcode-file", cl::value_desc("filename"),
113 "A file in which to write minimized bitcode for the thin link only"));
116 NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden
);
119 VerifyEach("verify-each", cl::desc("Verify after each transform"));
122 DisableDITypeMap("disable-debug-info-type-map",
123 cl::desc("Don't use a uniquing type map for debug info"));
126 StripDebug("strip-debug",
127 cl::desc("Strip debugger symbol info from translation unit"));
130 StripNamedMetadata("strip-named-metadata",
131 cl::desc("Strip module-level named metadata"));
133 static cl::opt
<bool> DisableInline("disable-inlining",
134 cl::desc("Do not run the inliner pass"));
137 DisableOptimizations("disable-opt",
138 cl::desc("Do not run any optimization passes"));
141 StandardLinkOpts("std-link-opts",
142 cl::desc("Include the standard link time optimizations"));
146 cl::desc("Optimization level 0. Similar to clang -O0"));
150 cl::desc("Optimization level 1. Similar to clang -O1"));
154 cl::desc("Optimization level 2. Similar to clang -O2"));
158 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
162 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
166 cl::desc("Optimization level 3. Similar to clang -O3"));
168 static cl::opt
<unsigned>
169 CodeGenOptLevel("codegen-opt-level",
170 cl::desc("Override optimization level for codegen hooks"));
172 static cl::opt
<std::string
>
173 TargetTriple("mtriple", cl::desc("Override target triple for module"));
176 DisableLoopUnrolling("disable-loop-unrolling",
177 cl::desc("Disable loop unrolling in all relevant passes"),
181 DisableSLPVectorization("disable-slp-vectorization",
182 cl::desc("Disable the slp vectorization pass"),
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"));
197 Quiet("q", cl::desc("Obsolete option"), cl::Hidden
);
200 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet
));
203 AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
205 static cl::opt
<bool> EnableDebugify(
208 "Start the pipeline with debugify and end it with check-debugify"));
210 static cl::opt
<bool> DebugifyEach(
213 "Start each pass with debugify and end it with check-debugify"));
215 static cl::opt
<std::string
>
216 DebugifyExport("debugify-export",
217 cl::desc("Export per-pass debugify statistics to this file"),
218 cl::value_desc("filename"), cl::init(""));
221 PrintBreakpoints("print-breakpoints-for-testing",
222 cl::desc("Print select breakpoints location for testing"));
224 static cl::opt
<std::string
> ClDataLayout("data-layout",
225 cl::desc("data layout string to use"),
226 cl::value_desc("layout-string"),
229 static cl::opt
<bool> PreserveBitcodeUseListOrder(
230 "preserve-bc-uselistorder",
231 cl::desc("Preserve use-list order when writing LLVM bitcode."),
232 cl::init(true), cl::Hidden
);
234 static cl::opt
<bool> PreserveAssemblyUseListOrder(
235 "preserve-ll-uselistorder",
236 cl::desc("Preserve use-list order when writing LLVM assembly."),
237 cl::init(false), cl::Hidden
);
240 RunTwice("run-twice",
241 cl::desc("Run all passes twice, re-using the same pass manager."),
242 cl::init(false), cl::Hidden
);
244 static cl::opt
<bool> DiscardValueNames(
245 "discard-value-names",
246 cl::desc("Discard names from Value (other than GlobalValue)."),
247 cl::init(false), cl::Hidden
);
249 static cl::opt
<bool> Coroutines(
251 cl::desc("Enable coroutine passes."),
252 cl::init(false), cl::Hidden
);
254 static cl::opt
<bool> RemarksWithHotness(
255 "pass-remarks-with-hotness",
256 cl::desc("With PGO, include profile count in optimization remarks"),
259 static cl::opt
<unsigned>
260 RemarksHotnessThreshold("pass-remarks-hotness-threshold",
261 cl::desc("Minimum profile count required for "
262 "an optimization remark to be output"),
265 static cl::opt
<std::string
>
266 RemarksFilename("pass-remarks-output",
267 cl::desc("Output filename for pass remarks"),
268 cl::value_desc("filename"));
270 static cl::opt
<std::string
>
271 RemarksPasses("pass-remarks-filter",
272 cl::desc("Only record optimization remarks from passes whose "
273 "names match the given regular expression"),
274 cl::value_desc("regex"));
276 static cl::opt
<std::string
> RemarksFormat(
277 "pass-remarks-format",
278 cl::desc("The format used for serializing remarks (default: YAML)"),
279 cl::value_desc("format"), cl::init("yaml"));
282 PGOKindFlag("pgo-kind", cl::init(NoPGO
), cl::Hidden
,
283 cl::desc("The kind of profile guided optimization"),
284 cl::values(clEnumValN(NoPGO
, "nopgo", "Do not use PGO."),
285 clEnumValN(InstrGen
, "pgo-instr-gen-pipeline",
286 "Instrument the IR to generate profile."),
287 clEnumValN(InstrUse
, "pgo-instr-use-pipeline",
288 "Use instrumented profile to guide PGO."),
289 clEnumValN(SampleUse
, "pgo-sample-use-pipeline",
290 "Use sampled profile to guide PGO.")));
291 cl::opt
<std::string
> ProfileFile("profile-file",
292 cl::desc("Path to the profile."), cl::Hidden
);
294 cl::opt
<CSPGOKind
> CSPGOKindFlag(
295 "cspgo-kind", cl::init(NoCSPGO
), cl::Hidden
,
296 cl::desc("The kind of context sensitive profile guided optimization"),
298 clEnumValN(NoCSPGO
, "nocspgo", "Do not use CSPGO."),
300 CSInstrGen
, "cspgo-instr-gen-pipeline",
301 "Instrument (context sensitive) the IR to generate profile."),
303 CSInstrUse
, "cspgo-instr-use-pipeline",
304 "Use instrumented (context sensitive) profile to guide PGO.")));
305 cl::opt
<std::string
> CSProfileGenFile(
306 "cs-profilegen-file",
307 cl::desc("Path to the instrumented context sensitive profile."),
310 class OptCustomPassManager
: public legacy::PassManager
{
311 DebugifyStatsMap DIStatsMap
;
314 using super
= legacy::PassManager
;
316 void add(Pass
*P
) override
{
317 // Wrap each pass with (-check)-debugify passes if requested, making
318 // exceptions for passes which shouldn't see -debugify instrumentation.
319 bool WrapWithDebugify
= DebugifyEach
&& !P
->getAsImmutablePass() &&
320 !isIRPrintingPass(P
) && !isBitcodeWriterPass(P
);
321 if (!WrapWithDebugify
) {
326 // Apply -debugify/-check-debugify before/after each pass and collect
327 // debug info loss statistics.
328 PassKind Kind
= P
->getPassKind();
329 StringRef Name
= P
->getPassName();
331 // TODO: Implement Debugify for BasicBlockPass, LoopPass.
334 super::add(createDebugifyFunctionPass());
336 super::add(createCheckDebugifyFunctionPass(true, Name
, &DIStatsMap
));
339 super::add(createDebugifyModulePass());
341 super::add(createCheckDebugifyModulePass(true, Name
, &DIStatsMap
));
349 const DebugifyStatsMap
&getDebugifyStatsMap() const { return DIStatsMap
; }
352 static inline void addPass(legacy::PassManagerBase
&PM
, Pass
*P
) {
353 // Add the pass to the pass manager...
356 // If we are verifying all of the intermediate steps, add the verifier...
358 PM
.add(createVerifierPass());
361 /// This routine adds optimization passes based on selected optimization level,
364 /// OptLevel - Optimization Level
365 static void AddOptimizationPasses(legacy::PassManagerBase
&MPM
,
366 legacy::FunctionPassManager
&FPM
,
367 TargetMachine
*TM
, unsigned OptLevel
,
368 unsigned SizeLevel
) {
369 if (!NoVerify
|| VerifyEach
)
370 FPM
.add(createVerifierPass()); // Verify that input is correct
372 PassManagerBuilder Builder
;
373 Builder
.OptLevel
= OptLevel
;
374 Builder
.SizeLevel
= SizeLevel
;
378 } else if (OptLevel
> 1) {
379 Builder
.Inliner
= createFunctionInliningPass(OptLevel
, SizeLevel
, false);
381 Builder
.Inliner
= createAlwaysInlinerLegacyPass();
383 Builder
.DisableUnrollLoops
= (DisableLoopUnrolling
.getNumOccurrences() > 0) ?
384 DisableLoopUnrolling
: OptLevel
== 0;
386 // Check if vectorization is explicitly disabled via -vectorize-loops=false.
387 // The flag enables vectorization in the LoopVectorize pass, it is on by
388 // default, and if it was disabled, leave it disabled here.
389 // Another flag that exists: -loop-vectorize, controls adding the pass to the
390 // pass manager. If set, the pass is added, and there is no additional check
392 if (Builder
.LoopVectorize
)
393 Builder
.LoopVectorize
= OptLevel
> 1 && SizeLevel
< 2;
395 // When #pragma vectorize is on for SLP, do the same as above
396 Builder
.SLPVectorize
=
397 DisableSLPVectorization
? false : OptLevel
> 1 && SizeLevel
< 2;
400 TM
->adjustPassManager(Builder
);
403 addCoroutinePassesToExtensionPoints(Builder
);
405 switch (PGOKindFlag
) {
407 Builder
.EnablePGOInstrGen
= true;
408 Builder
.PGOInstrGen
= ProfileFile
;
411 Builder
.PGOInstrUse
= ProfileFile
;
414 Builder
.PGOSampleUse
= ProfileFile
;
420 switch (CSPGOKindFlag
) {
422 Builder
.EnablePGOCSInstrGen
= true;
425 Builder
.EnablePGOCSInstrUse
= true;
431 Builder
.populateFunctionPassManager(FPM
);
432 Builder
.populateModulePassManager(MPM
);
435 static void AddStandardLinkPasses(legacy::PassManagerBase
&PM
) {
436 PassManagerBuilder Builder
;
437 Builder
.VerifyInput
= true;
438 if (DisableOptimizations
)
439 Builder
.OptLevel
= 0;
442 Builder
.Inliner
= createFunctionInliningPass();
443 Builder
.populateLTOPassManager(PM
);
446 //===----------------------------------------------------------------------===//
447 // CodeGen-related helper functions.
450 static CodeGenOpt::Level
GetCodeGenOptLevel() {
451 if (CodeGenOptLevel
.getNumOccurrences())
452 return static_cast<CodeGenOpt::Level
>(unsigned(CodeGenOptLevel
));
454 return CodeGenOpt::Less
;
456 return CodeGenOpt::Default
;
458 return CodeGenOpt::Aggressive
;
459 return CodeGenOpt::None
;
462 // Returns the TargetMachine instance or zero if no triple is provided.
463 static TargetMachine
* GetTargetMachine(Triple TheTriple
, StringRef CPUStr
,
464 StringRef FeaturesStr
,
465 const TargetOptions
&Options
) {
467 const Target
*TheTarget
= TargetRegistry::lookupTarget(MArch
, TheTriple
,
469 // Some modules don't specify a triple, and this is okay.
474 return TheTarget
->createTargetMachine(TheTriple
.getTriple(), CPUStr
,
475 FeaturesStr
, Options
, getRelocModel(),
476 getCodeModel(), GetCodeGenOptLevel());
479 #ifdef LINK_POLLY_INTO_TOOLS
481 void initializePollyPasses(llvm::PassRegistry
&Registry
);
485 //===----------------------------------------------------------------------===//
488 int main(int argc
, char **argv
) {
489 InitLLVM
X(argc
, argv
);
491 // Enable debug stream buffering.
492 EnableDebugBuffering
= true;
496 InitializeAllTargets();
497 InitializeAllTargetMCs();
498 InitializeAllAsmPrinters();
499 InitializeAllAsmParsers();
502 PassRegistry
&Registry
= *PassRegistry::getPassRegistry();
503 initializeCore(Registry
);
504 initializeCoroutines(Registry
);
505 initializeScalarOpts(Registry
);
506 initializeObjCARCOpts(Registry
);
507 initializeVectorization(Registry
);
508 initializeIPO(Registry
);
509 initializeAnalysis(Registry
);
510 initializeTransformUtils(Registry
);
511 initializeInstCombine(Registry
);
512 initializeAggressiveInstCombine(Registry
);
513 initializeInstrumentation(Registry
);
514 initializeTarget(Registry
);
515 // For codegen passes, only passes that do IR to IR transformation are
517 initializeExpandMemCmpPassPass(Registry
);
518 initializeScalarizeMaskedMemIntrinPass(Registry
);
519 initializeCodeGenPreparePass(Registry
);
520 initializeAtomicExpandPass(Registry
);
521 initializeRewriteSymbolsLegacyPassPass(Registry
);
522 initializeWinEHPreparePass(Registry
);
523 initializeDwarfEHPreparePass(Registry
);
524 initializeSafeStackLegacyPassPass(Registry
);
525 initializeSjLjEHPreparePass(Registry
);
526 initializePreISelIntrinsicLoweringLegacyPassPass(Registry
);
527 initializeGlobalMergePass(Registry
);
528 initializeIndirectBrExpandPassPass(Registry
);
529 initializeInterleavedLoadCombinePass(Registry
);
530 initializeInterleavedAccessPass(Registry
);
531 initializeEntryExitInstrumenterPass(Registry
);
532 initializePostInlineEntryExitInstrumenterPass(Registry
);
533 initializeUnreachableBlockElimLegacyPassPass(Registry
);
534 initializeExpandReductionsPass(Registry
);
535 initializeWasmEHPreparePass(Registry
);
536 initializeWriteBitcodePassPass(Registry
);
537 initializeHardwareLoopsPass(Registry
);
539 #ifdef LINK_POLLY_INTO_TOOLS
540 polly::initializePollyPasses(Registry
);
543 cl::ParseCommandLineOptions(argc
, argv
,
544 "llvm .bc -> .bc modular optimizer and analysis printer\n");
546 if (AnalyzeOnly
&& NoOutput
) {
547 errs() << argv
[0] << ": analyze mode conflicts with no-output mode.\n";
553 Context
.setDiscardValueNames(DiscardValueNames
);
554 if (!DisableDITypeMap
)
555 Context
.enableDebugTypeODRUniquing();
557 Expected
<std::unique_ptr
<ToolOutputFile
>> RemarksFileOrErr
=
558 setupOptimizationRemarks(Context
, RemarksFilename
, RemarksPasses
,
559 RemarksFormat
, RemarksWithHotness
,
560 RemarksHotnessThreshold
);
561 if (Error E
= RemarksFileOrErr
.takeError()) {
562 errs() << toString(std::move(E
)) << '\n';
565 std::unique_ptr
<ToolOutputFile
> RemarksFile
= std::move(*RemarksFileOrErr
);
567 // Load the input module...
568 std::unique_ptr
<Module
> M
=
569 parseIRFile(InputFilename
, Err
, Context
, !NoVerify
, ClDataLayout
);
572 Err
.print(argv
[0], errs());
576 // Strip debug info before running the verifier.
580 // Erase module-level named metadata, if requested.
581 if (StripNamedMetadata
) {
582 while (!M
->named_metadata_empty()) {
583 NamedMDNode
*NMD
= &*M
->named_metadata_begin();
584 M
->eraseNamedMetadata(NMD
);
588 // If we are supposed to override the target triple or data layout, do so now.
589 if (!TargetTriple
.empty())
590 M
->setTargetTriple(Triple::normalize(TargetTriple
));
592 // Immediately run the verifier to catch any problems before starting up the
593 // pass pipelines. Otherwise we can crash on broken code during
594 // doInitialization().
595 if (!NoVerify
&& verifyModule(*M
, &errs())) {
596 errs() << argv
[0] << ": " << InputFilename
597 << ": error: input module is broken!\n";
601 // Figure out what stream we are supposed to write to...
602 std::unique_ptr
<ToolOutputFile
> Out
;
603 std::unique_ptr
<ToolOutputFile
> ThinLinkOut
;
605 if (!OutputFilename
.empty())
606 errs() << "WARNING: The -o (output filename) option is ignored when\n"
607 "the --disable-output option is used.\n";
609 // Default to standard output.
610 if (OutputFilename
.empty())
611 OutputFilename
= "-";
614 sys::fs::OpenFlags Flags
= OutputAssembly
? sys::fs::OF_Text
616 Out
.reset(new ToolOutputFile(OutputFilename
, EC
, Flags
));
618 errs() << EC
.message() << '\n';
622 if (!ThinLinkBitcodeFile
.empty()) {
624 new ToolOutputFile(ThinLinkBitcodeFile
, EC
, sys::fs::OF_None
));
626 errs() << EC
.message() << '\n';
632 Triple
ModuleTriple(M
->getTargetTriple());
633 std::string CPUStr
, FeaturesStr
;
634 TargetMachine
*Machine
= nullptr;
635 const TargetOptions Options
= InitTargetOptionsFromCodeGenFlags();
637 if (ModuleTriple
.getArch()) {
638 CPUStr
= getCPUStr();
639 FeaturesStr
= getFeaturesStr();
640 Machine
= GetTargetMachine(ModuleTriple
, CPUStr
, FeaturesStr
, Options
);
641 } else if (ModuleTriple
.getArchName() != "unknown" &&
642 ModuleTriple
.getArchName() != "") {
643 errs() << argv
[0] << ": unrecognized architecture '"
644 << ModuleTriple
.getArchName() << "' provided.\n";
648 std::unique_ptr
<TargetMachine
> TM(Machine
);
650 // Override function attributes based on CPUStr, FeaturesStr, and command line
652 setFunctionAttributes(CPUStr
, FeaturesStr
, *M
);
654 // If the output is set to be emitted to standard out, and standard out is a
655 // console, print out a warning message and refuse to do it. We don't
656 // impress anyone by spewing tons of binary goo to a terminal.
657 if (!Force
&& !NoOutput
&& !AnalyzeOnly
&& !OutputAssembly
)
658 if (CheckBitcodeOutputToConsole(Out
->os(), !Quiet
))
662 M
->addModuleFlag(Module::Error
, "EnableSplitLTOUnit", SplitLTOUnit
);
664 if (PassPipeline
.getNumOccurrences() > 0) {
665 OutputKind OK
= OK_NoOutput
;
669 : (OutputThinLTOBC
? OK_OutputThinLTOBitcode
: OK_OutputBitcode
);
671 VerifierKind VK
= VK_VerifyInAndOut
;
675 VK
= VK_VerifyEachPass
;
677 // The user has asked to use the new pass manager and provided a pipeline
678 // string. Hand off the rest of the functionality to the new code for that
680 return runPassPipeline(argv
[0], *M
, TM
.get(), Out
.get(), ThinLinkOut
.get(),
681 RemarksFile
.get(), PassPipeline
, OK
, VK
,
682 PreserveAssemblyUseListOrder
,
683 PreserveBitcodeUseListOrder
, EmitSummaryIndex
,
684 EmitModuleHash
, EnableDebugify
)
689 // Create a PassManager to hold and optimize the collection of passes we are
691 OptCustomPassManager Passes
;
692 bool AddOneTimeDebugifyPasses
= EnableDebugify
&& !DebugifyEach
;
694 // Add an appropriate TargetLibraryInfo pass for the module's triple.
695 TargetLibraryInfoImpl
TLII(ModuleTriple
);
697 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
698 if (DisableSimplifyLibCalls
)
699 TLII
.disableAllFunctions();
700 Passes
.add(new TargetLibraryInfoWrapperPass(TLII
));
702 // Add internal analysis passes from the target machine.
703 Passes
.add(createTargetTransformInfoWrapperPass(TM
? TM
->getTargetIRAnalysis()
704 : TargetIRAnalysis()));
706 if (AddOneTimeDebugifyPasses
)
707 Passes
.add(createDebugifyModulePass());
709 std::unique_ptr
<legacy::FunctionPassManager
> FPasses
;
710 if (OptLevelO0
|| OptLevelO1
|| OptLevelO2
|| OptLevelOs
|| OptLevelOz
||
712 FPasses
.reset(new legacy::FunctionPassManager(M
.get()));
713 FPasses
->add(createTargetTransformInfoWrapperPass(
714 TM
? TM
->getTargetIRAnalysis() : TargetIRAnalysis()));
717 if (PrintBreakpoints
) {
718 // Default to standard output.
720 if (OutputFilename
.empty())
721 OutputFilename
= "-";
724 Out
= std::make_unique
<ToolOutputFile
>(OutputFilename
, EC
,
727 errs() << EC
.message() << '\n';
731 Passes
.add(createBreakpointPrinter(Out
->os()));
736 // FIXME: We should dyn_cast this when supported.
737 auto <M
= static_cast<LLVMTargetMachine
&>(*TM
);
738 Pass
*TPC
= LTM
.createPassConfig(Passes
);
742 // Create a new optimization pass for each one specified on the command line
743 for (unsigned i
= 0; i
< PassList
.size(); ++i
) {
744 if (StandardLinkOpts
&&
745 StandardLinkOpts
.getPosition() < PassList
.getPosition(i
)) {
746 AddStandardLinkPasses(Passes
);
747 StandardLinkOpts
= false;
750 if (OptLevelO0
&& OptLevelO0
.getPosition() < PassList
.getPosition(i
)) {
751 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 0, 0);
755 if (OptLevelO1
&& OptLevelO1
.getPosition() < PassList
.getPosition(i
)) {
756 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 1, 0);
760 if (OptLevelO2
&& OptLevelO2
.getPosition() < PassList
.getPosition(i
)) {
761 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 2, 0);
765 if (OptLevelOs
&& OptLevelOs
.getPosition() < PassList
.getPosition(i
)) {
766 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 2, 1);
770 if (OptLevelOz
&& OptLevelOz
.getPosition() < PassList
.getPosition(i
)) {
771 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 2, 2);
775 if (OptLevelO3
&& OptLevelO3
.getPosition() < PassList
.getPosition(i
)) {
776 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 3, 0);
780 const PassInfo
*PassInf
= PassList
[i
];
782 if (PassInf
->getNormalCtor())
783 P
= PassInf
->getNormalCtor()();
785 errs() << argv
[0] << ": cannot create pass: "
786 << PassInf
->getPassName() << "\n";
788 PassKind Kind
= P
->getPassKind();
794 Passes
.add(createBasicBlockPassPrinter(PassInf
, Out
->os(), Quiet
));
797 Passes
.add(createRegionPassPrinter(PassInf
, Out
->os(), Quiet
));
800 Passes
.add(createLoopPassPrinter(PassInf
, Out
->os(), Quiet
));
803 Passes
.add(createFunctionPassPrinter(PassInf
, Out
->os(), Quiet
));
805 case PT_CallGraphSCC
:
806 Passes
.add(createCallGraphPassPrinter(PassInf
, Out
->os(), Quiet
));
809 Passes
.add(createModulePassPrinter(PassInf
, Out
->os(), Quiet
));
817 createPrintModulePass(errs(), "", PreserveAssemblyUseListOrder
));
820 if (StandardLinkOpts
) {
821 AddStandardLinkPasses(Passes
);
822 StandardLinkOpts
= false;
826 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 0, 0);
829 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 1, 0);
832 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 2, 0);
835 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 2, 1);
838 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 2, 2);
841 AddOptimizationPasses(Passes
, *FPasses
, TM
.get(), 3, 0);
844 FPasses
->doInitialization();
845 for (Function
&F
: *M
)
847 FPasses
->doFinalization();
850 // Check that the module is well formed on completion of optimization
851 if (!NoVerify
&& !VerifyEach
)
852 Passes
.add(createVerifierPass());
854 if (AddOneTimeDebugifyPasses
)
855 Passes
.add(createCheckDebugifyModulePass(false));
857 // In run twice mode, we want to make sure the output is bit-by-bit
858 // equivalent if we run the pass manager again, so setup two buffers and
859 // a stream to write to them. Note that llc does something similar and it
860 // may be worth to abstract this out in the future.
861 SmallVector
<char, 0> Buffer
;
862 SmallVector
<char, 0> FirstRunBuffer
;
863 std::unique_ptr
<raw_svector_ostream
> BOS
;
864 raw_ostream
*OS
= nullptr;
866 // Write bitcode or assembly to the output as the last step...
867 if (!NoOutput
&& !AnalyzeOnly
) {
871 BOS
= std::make_unique
<raw_svector_ostream
>(Buffer
);
874 if (OutputAssembly
) {
875 if (EmitSummaryIndex
)
876 report_fatal_error("Text output is incompatible with -module-summary");
878 report_fatal_error("Text output is incompatible with -module-hash");
879 Passes
.add(createPrintModulePass(*OS
, "", PreserveAssemblyUseListOrder
));
880 } else if (OutputThinLTOBC
)
881 Passes
.add(createWriteThinLTOBitcodePass(
882 *OS
, ThinLinkOut
? &ThinLinkOut
->os() : nullptr));
884 Passes
.add(createBitcodeWriterPass(*OS
, PreserveBitcodeUseListOrder
,
885 EmitSummaryIndex
, EmitModuleHash
));
888 // Before executing passes, print the final values of the LLVM options.
889 cl::PrintOptionValues();
892 // Now that we have all of the passes ready, run them.
895 // If requested, run all passes twice with the same pass manager to catch
896 // bugs caused by persistent state in the passes.
897 std::unique_ptr
<Module
> M2(CloneModule(*M
));
898 // Run all passes on the original module first, so the second run processes
899 // the clone to catch CloneModule bugs.
901 FirstRunBuffer
= Buffer
;
906 // Compare the two outputs and make sure they're the same
908 if (Buffer
.size() != FirstRunBuffer
.size() ||
909 (memcmp(Buffer
.data(), FirstRunBuffer
.data(), Buffer
.size()) != 0)) {
911 << "Running the pass manager twice changed the output.\n"
912 "Writing the result of the second run to the specified output.\n"
913 "To generate the one-run comparison binary, just run without\n"
914 "the compile-twice option\n";
915 Out
->os() << BOS
->str();
921 Out
->os() << BOS
->str();
924 if (DebugifyEach
&& !DebugifyExport
.empty())
925 exportDebugifyStats(DebugifyExport
, Passes
.getDebugifyStatsMap());
928 if (!NoOutput
|| PrintBreakpoints
)