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"
15 #include "NewPMDriver.h"
16 #include "PassPrinters.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/Analysis/CallGraph.h"
19 #include "llvm/Analysis/CallGraphSCCPass.h"
20 #include "llvm/Analysis/LoopPass.h"
21 #include "llvm/Analysis/RegionPass.h"
22 #include "llvm/Analysis/TargetLibraryInfo.h"
23 #include "llvm/Analysis/TargetTransformInfo.h"
24 #include "llvm/Bitcode/BitcodeWriterPass.h"
25 #include "llvm/CodeGen/CommandFlags.inc"
26 #include "llvm/CodeGen/TargetPassConfig.h"
27 #include "llvm/Config/llvm-config.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/DebugInfo.h"
30 #include "llvm/IR/IRPrintingPasses.h"
31 #include "llvm/IR/LLVMContext.h"
32 #include "llvm/IR/LegacyPassManager.h"
33 #include "llvm/IR/LegacyPassNameParser.h"
34 #include "llvm/IR/Module.h"
35 #include "llvm/IR/RemarkStreamer.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"
58 #include "llvm/Transforms/Utils/Debugify.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"));
196 static cl::list<std::string>
197 DisableBuiltins("disable-builtin",
198 cl::desc("Disable specific target library builtin function"),
203 Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
206 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
209 AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
211 static cl::opt<bool> EnableDebugify(
214 "Start the pipeline with debugify and end it with check-debugify"));
216 static cl::opt<bool> DebugifyEach(
219 "Start each pass with debugify and end it with check-debugify"));
221 static cl::opt<std::string>
222 DebugifyExport("debugify-export",
223 cl::desc("Export per-pass debugify statistics to this file"),
224 cl::value_desc("filename"), cl::init(""));
227 PrintBreakpoints("print-breakpoints-for-testing",
228 cl::desc("Print select breakpoints location for testing"));
230 static cl::opt<std::string> ClDataLayout("data-layout",
231 cl::desc("data layout string to use"),
232 cl::value_desc("layout-string"),
235 static cl::opt<bool> PreserveBitcodeUseListOrder(
236 "preserve-bc-uselistorder",
237 cl::desc("Preserve use-list order when writing LLVM bitcode."),
238 cl::init(true), cl::Hidden);
240 static cl::opt<bool> PreserveAssemblyUseListOrder(
241 "preserve-ll-uselistorder",
242 cl::desc("Preserve use-list order when writing LLVM assembly."),
243 cl::init(false), cl::Hidden);
246 RunTwice("run-twice",
247 cl::desc("Run all passes twice, re-using the same pass manager."),
248 cl::init(false), cl::Hidden);
250 static cl::opt<bool> DiscardValueNames(
251 "discard-value-names",
252 cl::desc("Discard names from Value (other than GlobalValue)."),
253 cl::init(false), cl::Hidden);
255 static cl::opt<bool> Coroutines(
257 cl::desc("Enable coroutine passes."),
258 cl::init(false), cl::Hidden);
260 static cl::opt<bool> RemarksWithHotness(
261 "pass-remarks-with-hotness",
262 cl::desc("With PGO, include profile count in optimization remarks"),
265 static cl::opt<unsigned>
266 RemarksHotnessThreshold("pass-remarks-hotness-threshold",
267 cl::desc("Minimum profile count required for "
268 "an optimization remark to be output"),
271 static cl::opt<std::string>
272 RemarksFilename("pass-remarks-output",
273 cl::desc("Output filename for pass remarks"),
274 cl::value_desc("filename"));
276 static cl::opt<std::string>
277 RemarksPasses("pass-remarks-filter",
278 cl::desc("Only record optimization remarks from passes whose "
279 "names match the given regular expression"),
280 cl::value_desc("regex"));
282 static cl::opt<std::string> RemarksFormat(
283 "pass-remarks-format",
284 cl::desc("The format used for serializing remarks (default: YAML)"),
285 cl::value_desc("format"), cl::init("yaml"));
288 PGOKindFlag("pgo-kind", cl::init(NoPGO), cl::Hidden,
289 cl::desc("The kind of profile guided optimization"),
290 cl::values(clEnumValN(NoPGO, "nopgo", "Do not use PGO."),
291 clEnumValN(InstrGen, "pgo-instr-gen-pipeline",
292 "Instrument the IR to generate profile."),
293 clEnumValN(InstrUse, "pgo-instr-use-pipeline",
294 "Use instrumented profile to guide PGO."),
295 clEnumValN(SampleUse, "pgo-sample-use-pipeline",
296 "Use sampled profile to guide PGO.")));
297 cl::opt<std::string> ProfileFile("profile-file",
298 cl::desc("Path to the profile."), cl::Hidden);
300 cl::opt<CSPGOKind> CSPGOKindFlag(
301 "cspgo-kind", cl::init(NoCSPGO), cl::Hidden,
302 cl::desc("The kind of context sensitive profile guided optimization"),
304 clEnumValN(NoCSPGO, "nocspgo", "Do not use CSPGO."),
306 CSInstrGen, "cspgo-instr-gen-pipeline",
307 "Instrument (context sensitive) the IR to generate profile."),
309 CSInstrUse, "cspgo-instr-use-pipeline",
310 "Use instrumented (context sensitive) profile to guide PGO.")));
311 cl::opt<std::string> CSProfileGenFile(
312 "cs-profilegen-file",
313 cl::desc("Path to the instrumented context sensitive profile."),
316 class OptCustomPassManager : public legacy::PassManager {
317 DebugifyStatsMap DIStatsMap;
320 using super = legacy::PassManager;
322 void add(Pass *P) override {
323 // Wrap each pass with (-check)-debugify passes if requested, making
324 // exceptions for passes which shouldn't see -debugify instrumentation.
325 bool WrapWithDebugify = DebugifyEach && !P->getAsImmutablePass() &&
326 !isIRPrintingPass(P) && !isBitcodeWriterPass(P);
327 if (!WrapWithDebugify) {
332 // Apply -debugify/-check-debugify before/after each pass and collect
333 // debug info loss statistics.
334 PassKind Kind = P->getPassKind();
335 StringRef Name = P->getPassName();
337 // TODO: Implement Debugify for LoopPass.
340 super::add(createDebugifyFunctionPass());
342 super::add(createCheckDebugifyFunctionPass(true, Name, &DIStatsMap));
345 super::add(createDebugifyModulePass());
347 super::add(createCheckDebugifyModulePass(true, Name, &DIStatsMap));
355 const DebugifyStatsMap &getDebugifyStatsMap() const { return DIStatsMap; }
358 static inline void addPass(legacy::PassManagerBase &PM, Pass *P) {
359 // Add the pass to the pass manager...
362 // If we are verifying all of the intermediate steps, add the verifier...
364 PM.add(createVerifierPass());
367 /// This routine adds optimization passes based on selected optimization level,
370 /// OptLevel - Optimization Level
371 static void AddOptimizationPasses(legacy::PassManagerBase &MPM,
372 legacy::FunctionPassManager &FPM,
373 TargetMachine *TM, unsigned OptLevel,
374 unsigned SizeLevel) {
375 if (!NoVerify || VerifyEach)
376 FPM.add(createVerifierPass()); // Verify that input is correct
378 PassManagerBuilder Builder;
379 Builder.OptLevel = OptLevel;
380 Builder.SizeLevel = SizeLevel;
384 } else if (OptLevel > 1) {
385 Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
387 Builder.Inliner = createAlwaysInlinerLegacyPass();
389 Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
390 DisableLoopUnrolling : OptLevel == 0;
392 // Check if vectorization is explicitly disabled via -vectorize-loops=false.
393 // The flag enables vectorization in the LoopVectorize pass, it is on by
394 // default, and if it was disabled, leave it disabled here.
395 // Another flag that exists: -loop-vectorize, controls adding the pass to the
396 // pass manager. If set, the pass is added, and there is no additional check
398 if (Builder.LoopVectorize)
399 Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
401 // When #pragma vectorize is on for SLP, do the same as above
402 Builder.SLPVectorize =
403 DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2;
406 TM->adjustPassManager(Builder);
409 addCoroutinePassesToExtensionPoints(Builder);
411 switch (PGOKindFlag) {
413 Builder.EnablePGOInstrGen = true;
414 Builder.PGOInstrGen = ProfileFile;
417 Builder.PGOInstrUse = ProfileFile;
420 Builder.PGOSampleUse = ProfileFile;
426 switch (CSPGOKindFlag) {
428 Builder.EnablePGOCSInstrGen = true;
431 Builder.EnablePGOCSInstrUse = true;
437 Builder.populateFunctionPassManager(FPM);
438 Builder.populateModulePassManager(MPM);
441 static void AddStandardLinkPasses(legacy::PassManagerBase &PM) {
442 PassManagerBuilder Builder;
443 Builder.VerifyInput = true;
444 if (DisableOptimizations)
445 Builder.OptLevel = 0;
448 Builder.Inliner = createFunctionInliningPass();
449 Builder.populateLTOPassManager(PM);
452 //===----------------------------------------------------------------------===//
453 // CodeGen-related helper functions.
456 static CodeGenOpt::Level GetCodeGenOptLevel() {
457 if (CodeGenOptLevel.getNumOccurrences())
458 return static_cast<CodeGenOpt::Level>(unsigned(CodeGenOptLevel));
460 return CodeGenOpt::Less;
462 return CodeGenOpt::Default;
464 return CodeGenOpt::Aggressive;
465 return CodeGenOpt::None;
468 // Returns the TargetMachine instance or zero if no triple is provided.
469 static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
470 StringRef FeaturesStr,
471 const TargetOptions &Options) {
473 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
475 // Some modules don't specify a triple, and this is okay.
480 return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr,
481 FeaturesStr, Options, getRelocModel(),
482 getCodeModel(), GetCodeGenOptLevel());
485 #ifdef BUILD_EXAMPLES
486 void initializeExampleIRTransforms(llvm::PassRegistry &Registry);
490 void exportDebugifyStats(llvm::StringRef Path, const DebugifyStatsMap &Map) {
492 raw_fd_ostream OS{Path, EC};
494 errs() << "Could not open file: " << EC.message() << ", " << Path << '\n';
498 OS << "Pass Name" << ',' << "# of missing debug values" << ','
499 << "# of missing locations" << ',' << "Missing/Expected value ratio" << ','
500 << "Missing/Expected location ratio" << '\n';
501 for (const auto &Entry : Map) {
502 StringRef Pass = Entry.first;
503 DebugifyStatistics Stats = Entry.second;
505 OS << Pass << ',' << Stats.NumDbgValuesMissing << ','
506 << Stats.NumDbgLocsMissing << ',' << Stats.getMissingValueRatio() << ','
507 << Stats.getEmptyLocationRatio() << '\n';
511 //===----------------------------------------------------------------------===//
514 int main(int argc, char **argv) {
515 InitLLVM X(argc, argv);
517 // Enable debug stream buffering.
518 EnableDebugBuffering = true;
522 InitializeAllTargets();
523 InitializeAllTargetMCs();
524 InitializeAllAsmPrinters();
525 InitializeAllAsmParsers();
528 PassRegistry &Registry = *PassRegistry::getPassRegistry();
529 initializeCore(Registry);
530 initializeCoroutines(Registry);
531 initializeScalarOpts(Registry);
532 initializeObjCARCOpts(Registry);
533 initializeVectorization(Registry);
534 initializeIPO(Registry);
535 initializeAnalysis(Registry);
536 initializeTransformUtils(Registry);
537 initializeInstCombine(Registry);
538 initializeAggressiveInstCombine(Registry);
539 initializeInstrumentation(Registry);
540 initializeTarget(Registry);
541 // For codegen passes, only passes that do IR to IR transformation are
543 initializeExpandMemCmpPassPass(Registry);
544 initializeScalarizeMaskedMemIntrinPass(Registry);
545 initializeCodeGenPreparePass(Registry);
546 initializeAtomicExpandPass(Registry);
547 initializeRewriteSymbolsLegacyPassPass(Registry);
548 initializeWinEHPreparePass(Registry);
549 initializeDwarfEHPreparePass(Registry);
550 initializeSafeStackLegacyPassPass(Registry);
551 initializeSjLjEHPreparePass(Registry);
552 initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
553 initializeGlobalMergePass(Registry);
554 initializeIndirectBrExpandPassPass(Registry);
555 initializeInterleavedLoadCombinePass(Registry);
556 initializeInterleavedAccessPass(Registry);
557 initializeEntryExitInstrumenterPass(Registry);
558 initializePostInlineEntryExitInstrumenterPass(Registry);
559 initializeUnreachableBlockElimLegacyPassPass(Registry);
560 initializeExpandReductionsPass(Registry);
561 initializeWasmEHPreparePass(Registry);
562 initializeWriteBitcodePassPass(Registry);
563 initializeHardwareLoopsPass(Registry);
564 initializeTypePromotionPass(Registry);
566 #ifdef BUILD_EXAMPLES
567 initializeExampleIRTransforms(Registry);
570 cl::ParseCommandLineOptions(argc, argv,
571 "llvm .bc -> .bc modular optimizer and analysis printer\n");
573 if (AnalyzeOnly && NoOutput) {
574 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
580 Context.setDiscardValueNames(DiscardValueNames);
581 if (!DisableDITypeMap)
582 Context.enableDebugTypeODRUniquing();
584 Expected<std::unique_ptr<ToolOutputFile>> RemarksFileOrErr =
585 setupOptimizationRemarks(Context, RemarksFilename, RemarksPasses,
586 RemarksFormat, RemarksWithHotness,
587 RemarksHotnessThreshold);
588 if (Error E = RemarksFileOrErr.takeError()) {
589 errs() << toString(std::move(E)) << '\n';
592 std::unique_ptr<ToolOutputFile> RemarksFile = std::move(*RemarksFileOrErr);
594 // Load the input module...
595 std::unique_ptr<Module> M =
596 parseIRFile(InputFilename, Err, Context, !NoVerify, ClDataLayout);
599 Err.print(argv[0], errs());
603 // Strip debug info before running the verifier.
607 // Erase module-level named metadata, if requested.
608 if (StripNamedMetadata) {
609 while (!M->named_metadata_empty()) {
610 NamedMDNode *NMD = &*M->named_metadata_begin();
611 M->eraseNamedMetadata(NMD);
615 // If we are supposed to override the target triple or data layout, do so now.
616 if (!TargetTriple.empty())
617 M->setTargetTriple(Triple::normalize(TargetTriple));
619 // Immediately run the verifier to catch any problems before starting up the
620 // pass pipelines. Otherwise we can crash on broken code during
621 // doInitialization().
622 if (!NoVerify && verifyModule(*M, &errs())) {
623 errs() << argv[0] << ": " << InputFilename
624 << ": error: input module is broken!\n";
628 // Figure out what stream we are supposed to write to...
629 std::unique_ptr<ToolOutputFile> Out;
630 std::unique_ptr<ToolOutputFile> ThinLinkOut;
632 if (!OutputFilename.empty())
633 errs() << "WARNING: The -o (output filename) option is ignored when\n"
634 "the --disable-output option is used.\n";
636 // Default to standard output.
637 if (OutputFilename.empty())
638 OutputFilename = "-";
641 sys::fs::OpenFlags Flags = OutputAssembly ? sys::fs::OF_Text
643 Out.reset(new ToolOutputFile(OutputFilename, EC, Flags));
645 errs() << EC.message() << '\n';
649 if (!ThinLinkBitcodeFile.empty()) {
651 new ToolOutputFile(ThinLinkBitcodeFile, EC, sys::fs::OF_None));
653 errs() << EC.message() << '\n';
659 Triple ModuleTriple(M->getTargetTriple());
660 std::string CPUStr, FeaturesStr;
661 TargetMachine *Machine = nullptr;
662 const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
664 if (ModuleTriple.getArch()) {
665 CPUStr = getCPUStr();
666 FeaturesStr = getFeaturesStr();
667 Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options);
668 } else if (ModuleTriple.getArchName() != "unknown" &&
669 ModuleTriple.getArchName() != "") {
670 errs() << argv[0] << ": unrecognized architecture '"
671 << ModuleTriple.getArchName() << "' provided.\n";
675 std::unique_ptr<TargetMachine> TM(Machine);
677 // Override function attributes based on CPUStr, FeaturesStr, and command line
679 setFunctionAttributes(CPUStr, FeaturesStr, *M);
681 // If the output is set to be emitted to standard out, and standard out is a
682 // console, print out a warning message and refuse to do it. We don't
683 // impress anyone by spewing tons of binary goo to a terminal.
684 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
685 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
689 M->addModuleFlag(Module::Error, "EnableSplitLTOUnit", SplitLTOUnit);
691 if (PassPipeline.getNumOccurrences() > 0) {
692 OutputKind OK = OK_NoOutput;
696 : (OutputThinLTOBC ? OK_OutputThinLTOBitcode : OK_OutputBitcode);
698 VerifierKind VK = VK_VerifyInAndOut;
702 VK = VK_VerifyEachPass;
704 // The user has asked to use the new pass manager and provided a pipeline
705 // string. Hand off the rest of the functionality to the new code for that
707 return runPassPipeline(argv[0], *M, TM.get(), Out.get(), ThinLinkOut.get(),
708 RemarksFile.get(), PassPipeline, OK, VK,
709 PreserveAssemblyUseListOrder,
710 PreserveBitcodeUseListOrder, EmitSummaryIndex,
711 EmitModuleHash, EnableDebugify)
716 // Create a PassManager to hold and optimize the collection of passes we are
718 OptCustomPassManager Passes;
719 bool AddOneTimeDebugifyPasses = EnableDebugify && !DebugifyEach;
721 // Add an appropriate TargetLibraryInfo pass for the module's triple.
722 TargetLibraryInfoImpl TLII(ModuleTriple);
724 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
725 if (DisableSimplifyLibCalls)
726 TLII.disableAllFunctions();
728 // Disable individual builtin functions in TargetLibraryInfo.
730 for (auto &FuncName : DisableBuiltins)
731 if (TLII.getLibFunc(FuncName, F))
732 TLII.setUnavailable(F);
734 errs() << argv[0] << ": cannot disable nonexistent builtin function "
740 Passes.add(new TargetLibraryInfoWrapperPass(TLII));
742 // Add internal analysis passes from the target machine.
743 Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis()
744 : TargetIRAnalysis()));
746 if (AddOneTimeDebugifyPasses)
747 Passes.add(createDebugifyModulePass());
749 std::unique_ptr<legacy::FunctionPassManager> FPasses;
750 if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz ||
752 FPasses.reset(new legacy::FunctionPassManager(M.get()));
753 FPasses->add(createTargetTransformInfoWrapperPass(
754 TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis()));
757 if (PrintBreakpoints) {
758 // Default to standard output.
760 if (OutputFilename.empty())
761 OutputFilename = "-";
764 Out = std::make_unique<ToolOutputFile>(OutputFilename, EC,
767 errs() << EC.message() << '\n';
771 Passes.add(createBreakpointPrinter(Out->os()));
776 // FIXME: We should dyn_cast this when supported.
777 auto <M = static_cast<LLVMTargetMachine &>(*TM);
778 Pass *TPC = LTM.createPassConfig(Passes);
782 // Create a new optimization pass for each one specified on the command line
783 for (unsigned i = 0; i < PassList.size(); ++i) {
784 if (StandardLinkOpts &&
785 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
786 AddStandardLinkPasses(Passes);
787 StandardLinkOpts = false;
790 if (OptLevelO0 && OptLevelO0.getPosition() < PassList.getPosition(i)) {
791 AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
795 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
796 AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
800 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
801 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
805 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
806 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
810 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
811 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
815 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
816 AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
820 const PassInfo *PassInf = PassList[i];
822 if (PassInf->getNormalCtor())
823 P = PassInf->getNormalCtor()();
825 errs() << argv[0] << ": cannot create pass: "
826 << PassInf->getPassName() << "\n";
828 PassKind Kind = P->getPassKind();
834 Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet));
837 Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet));
840 Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet));
842 case PT_CallGraphSCC:
843 Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet));
846 Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet));
854 createPrintModulePass(errs(), "", PreserveAssemblyUseListOrder));
857 if (StandardLinkOpts) {
858 AddStandardLinkPasses(Passes);
859 StandardLinkOpts = false;
863 AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
866 AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
869 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
872 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
875 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
878 AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
881 FPasses->doInitialization();
882 for (Function &F : *M)
884 FPasses->doFinalization();
887 // Check that the module is well formed on completion of optimization
888 if (!NoVerify && !VerifyEach)
889 Passes.add(createVerifierPass());
891 if (AddOneTimeDebugifyPasses)
892 Passes.add(createCheckDebugifyModulePass(false));
894 // In run twice mode, we want to make sure the output is bit-by-bit
895 // equivalent if we run the pass manager again, so setup two buffers and
896 // a stream to write to them. Note that llc does something similar and it
897 // may be worth to abstract this out in the future.
898 SmallVector<char, 0> Buffer;
899 SmallVector<char, 0> FirstRunBuffer;
900 std::unique_ptr<raw_svector_ostream> BOS;
901 raw_ostream *OS = nullptr;
903 const bool ShouldEmitOutput = !NoOutput && !AnalyzeOnly;
905 // Write bitcode or assembly to the output as the last step...
906 if (ShouldEmitOutput || RunTwice) {
910 BOS = std::make_unique<raw_svector_ostream>(Buffer);
913 if (OutputAssembly) {
914 if (EmitSummaryIndex)
915 report_fatal_error("Text output is incompatible with -module-summary");
917 report_fatal_error("Text output is incompatible with -module-hash");
918 Passes.add(createPrintModulePass(*OS, "", PreserveAssemblyUseListOrder));
919 } else if (OutputThinLTOBC)
920 Passes.add(createWriteThinLTOBitcodePass(
921 *OS, ThinLinkOut ? &ThinLinkOut->os() : nullptr));
923 Passes.add(createBitcodeWriterPass(*OS, PreserveBitcodeUseListOrder,
924 EmitSummaryIndex, EmitModuleHash));
927 // Before executing passes, print the final values of the LLVM options.
928 cl::PrintOptionValues();
931 // Now that we have all of the passes ready, run them.
934 // If requested, run all passes twice with the same pass manager to catch
935 // bugs caused by persistent state in the passes.
936 std::unique_ptr<Module> M2(CloneModule(*M));
937 // Run all passes on the original module first, so the second run processes
938 // the clone to catch CloneModule bugs.
940 FirstRunBuffer = Buffer;
945 // Compare the two outputs and make sure they're the same
947 if (Buffer.size() != FirstRunBuffer.size() ||
948 (memcmp(Buffer.data(), FirstRunBuffer.data(), Buffer.size()) != 0)) {
950 << "Running the pass manager twice changed the output.\n"
951 "Writing the result of the second run to the specified output.\n"
952 "To generate the one-run comparison binary, just run without\n"
953 "the compile-twice option\n";
954 if (ShouldEmitOutput) {
955 Out->os() << BOS->str();
962 if (ShouldEmitOutput)
963 Out->os() << BOS->str();
966 if (DebugifyEach && !DebugifyExport.empty())
967 exportDebugifyStats(DebugifyExport, Passes.getDebugifyStatsMap());
970 if (!NoOutput || PrintBreakpoints)