Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / tools / opt / opt.cpp
blob4afe7508fc252169c290d33e7113a5104d2989b8
1 //===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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 "Debugify.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"
58 #include <algorithm>
59 #include <memory>
60 using namespace llvm;
61 using namespace opt_tool;
63 // The OptimizationList is automatically populated with registered Passes by the
64 // PassNameParser.
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
72 // pass management.
73 static cl::opt<std::string> PassPipeline(
74 "passes",
75 cl::desc("A textual description of the pass pipeline for optimizing"),
76 cl::Hidden);
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"));
88 static cl::opt<bool>
89 Force("f", cl::desc("Enable binary output on terminals"));
91 static cl::opt<bool>
92 PrintEachXForm("p", cl::desc("Print module after each transformation"));
94 static cl::opt<bool>
95 NoOutput("disable-output",
96 cl::desc("Do not write result bitcode file"), cl::Hidden);
98 static cl::opt<bool>
99 OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
101 static cl::opt<bool>
102 OutputThinLTOBC("thinlto-bc",
103 cl::desc("Write output as ThinLTO-ready bitcode"));
105 static cl::opt<bool>
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"),
111 cl::desc(
112 "A file in which to write minimized bitcode for the thin link only"));
114 static cl::opt<bool>
115 NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden);
117 static cl::opt<bool>
118 VerifyEach("verify-each", cl::desc("Verify after each transform"));
120 static cl::opt<bool>
121 DisableDITypeMap("disable-debug-info-type-map",
122 cl::desc("Don't use a uniquing type map for debug info"));
124 static cl::opt<bool>
125 StripDebug("strip-debug",
126 cl::desc("Strip debugger symbol info from translation unit"));
128 static cl::opt<bool>
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"));
135 static cl::opt<bool>
136 DisableOptimizations("disable-opt",
137 cl::desc("Do not run any optimization passes"));
139 static cl::opt<bool>
140 StandardLinkOpts("std-link-opts",
141 cl::desc("Include the standard link time optimizations"));
143 static cl::opt<bool>
144 OptLevelO0("O0",
145 cl::desc("Optimization level 0. Similar to clang -O0"));
147 static cl::opt<bool>
148 OptLevelO1("O1",
149 cl::desc("Optimization level 1. Similar to clang -O1"));
151 static cl::opt<bool>
152 OptLevelO2("O2",
153 cl::desc("Optimization level 2. Similar to clang -O2"));
155 static cl::opt<bool>
156 OptLevelOs("Os",
157 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
159 static cl::opt<bool>
160 OptLevelOz("Oz",
161 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
163 static cl::opt<bool>
164 OptLevelO3("O3",
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"));
174 static cl::opt<bool>
175 UnitAtATime("funit-at-a-time",
176 cl::desc("Enable IPO. This corresponds to gcc's -funit-at-a-time"),
177 cl::init(true));
179 static cl::opt<bool>
180 DisableLoopUnrolling("disable-loop-unrolling",
181 cl::desc("Disable loop unrolling in all relevant passes"),
182 cl::init(false));
183 static cl::opt<bool>
184 DisableLoopVectorization("disable-loop-vectorization",
185 cl::desc("Disable the loop vectorization pass"),
186 cl::init(false));
188 static cl::opt<bool>
189 DisableSLPVectorization("disable-slp-vectorization",
190 cl::desc("Disable the slp vectorization pass"),
191 cl::init(false));
193 static cl::opt<bool> EmitSummaryIndex("module-summary",
194 cl::desc("Emit module summary index"),
195 cl::init(false));
197 static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
198 cl::init(false));
200 static cl::opt<bool>
201 DisableSimplifyLibCalls("disable-simplify-libcalls",
202 cl::desc("Disable simplify-libcalls"));
204 static cl::opt<bool>
205 Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
207 static cl::alias
208 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
210 static cl::opt<bool>
211 AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
213 static cl::opt<bool> EnableDebugify(
214 "enable-debugify",
215 cl::desc(
216 "Start the pipeline with debugify and end it with check-debugify"));
218 static cl::opt<bool> DebugifyEach(
219 "debugify-each",
220 cl::desc(
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(""));
228 static cl::opt<bool>
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"),
235 cl::init(""));
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);
247 static cl::opt<bool>
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(
258 "enable-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"),
265 cl::Hidden);
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"),
270 cl::Hidden);
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"));
277 cl::opt<PGOKind>
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;
293 public:
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) {
302 super::add(P);
303 return;
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.
312 switch (Kind) {
313 case PT_Function:
314 super::add(createDebugifyFunctionPass());
315 super::add(P);
316 super::add(createCheckDebugifyFunctionPass(true, Name, &DIStatsMap));
317 break;
318 case PT_Module:
319 super::add(createDebugifyModulePass());
320 super::add(P);
321 super::add(createCheckDebugifyModulePass(true, Name, &DIStatsMap));
322 break;
323 default:
324 super::add(P);
325 break;
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...
334 PM.add(P);
336 // If we are verifying all of the intermediate steps, add the verifier...
337 if (VerifyEach)
338 PM.add(createVerifierPass());
341 /// This routine adds optimization passes based on selected optimization level,
342 /// OptLevel.
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;
356 if (DisableInline) {
357 // No inlining pass
358 } else if (OptLevel > 1) {
359 Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
360 } else {
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;
378 if (TM)
379 TM->adjustPassManager(Builder);
381 if (Coroutines)
382 addCoroutinePassesToExtensionPoints(Builder);
384 switch (PGOKindFlag) {
385 case InstrGen:
386 Builder.EnablePGOInstrGen = true;
387 Builder.PGOInstrGen = ProfileFile;
388 break;
389 case InstrUse:
390 Builder.PGOInstrUse = ProfileFile;
391 break;
392 case SampleUse:
393 Builder.PGOSampleUse = ProfileFile;
394 break;
395 default:
396 break;
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;
409 if (!DisableInline)
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));
421 if (OptLevelO1)
422 return CodeGenOpt::Less;
423 if (OptLevelO2)
424 return CodeGenOpt::Default;
425 if (OptLevelO3)
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) {
434 std::string Error;
435 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
436 Error);
437 // Some modules don't specify a triple, and this is okay.
438 if (!TheTarget) {
439 return nullptr;
442 return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr,
443 FeaturesStr, Options, getRelocModel(),
444 getCodeModel(), GetCodeGenOptLevel());
447 #ifdef LINK_POLLY_INTO_TOOLS
448 namespace polly {
449 void initializePollyPasses(llvm::PassRegistry &Registry);
451 #endif
453 //===----------------------------------------------------------------------===//
454 // main for opt
456 int main(int argc, char **argv) {
457 InitLLVM X(argc, argv);
459 // Enable debug stream buffering.
460 EnableDebugBuffering = true;
462 LLVMContext Context;
464 InitializeAllTargets();
465 InitializeAllTargetMCs();
466 InitializeAllAsmPrinters();
467 InitializeAllAsmParsers();
469 // Initialize passes
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
484 // supported.
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);
508 #endif
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";
515 return 1;
518 SMDiagnostic Err;
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 != "") {
532 std::error_code EC;
533 OptRemarkFile =
534 llvm::make_unique<ToolOutputFile>(RemarksFilename, EC, sys::fs::F_None);
535 if (EC) {
536 errs() << EC.message() << '\n';
537 return 1;
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);
547 if (!M) {
548 Err.print(argv[0], errs());
549 return 1;
552 // Strip debug info before running the verifier.
553 if (StripDebug)
554 StripDebugInfo(*M);
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";
574 return 1;
577 // Figure out what stream we are supposed to write to...
578 std::unique_ptr<ToolOutputFile> Out;
579 std::unique_ptr<ToolOutputFile> ThinLinkOut;
580 if (NoOutput) {
581 if (!OutputFilename.empty())
582 errs() << "WARNING: The -o (output filename) option is ignored when\n"
583 "the --disable-output option is used.\n";
584 } else {
585 // Default to standard output.
586 if (OutputFilename.empty())
587 OutputFilename = "-";
589 std::error_code EC;
590 Out.reset(new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
591 if (EC) {
592 errs() << EC.message() << '\n';
593 return 1;
596 if (!ThinLinkBitcodeFile.empty()) {
597 ThinLinkOut.reset(
598 new ToolOutputFile(ThinLinkBitcodeFile, EC, sys::fs::F_None));
599 if (EC) {
600 errs() << EC.message() << '\n';
601 return 1;
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
620 // flags.
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))
628 NoOutput = true;
630 if (OutputThinLTOBC)
631 M->addModuleFlag(Module::Error, "EnableSplitLTOUnit", SplitLTOUnit);
633 if (PassPipeline.getNumOccurrences() > 0) {
634 OutputKind OK = OK_NoOutput;
635 if (!NoOutput)
636 OK = OutputAssembly
637 ? OK_OutputAssembly
638 : (OutputThinLTOBC ? OK_OutputThinLTOBitcode : OK_OutputBitcode);
640 VerifierKind VK = VK_VerifyInAndOut;
641 if (NoVerify)
642 VK = VK_NoVerifier;
643 else if (VerifyEach)
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
648 // layer.
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)
655 : 1;
658 // Create a PassManager to hold and optimize the collection of passes we are
659 // about to build.
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 ||
680 OptLevelO3) {
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.
688 if (!Out) {
689 if (OutputFilename.empty())
690 OutputFilename = "-";
692 std::error_code EC;
693 Out = llvm::make_unique<ToolOutputFile>(OutputFilename, EC,
694 sys::fs::F_None);
695 if (EC) {
696 errs() << EC.message() << '\n';
697 return 1;
700 Passes.add(createBreakpointPrinter(Out->os()));
701 NoOutput = true;
704 if (TM) {
705 // FIXME: We should dyn_cast this when supported.
706 auto &LTM = static_cast<LLVMTargetMachine &>(*TM);
707 Pass *TPC = LTM.createPassConfig(Passes);
708 Passes.add(TPC);
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);
721 OptLevelO0 = false;
724 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
725 AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
726 OptLevelO1 = false;
729 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
730 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
731 OptLevelO2 = false;
734 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
735 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
736 OptLevelOs = false;
739 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
740 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
741 OptLevelOz = false;
744 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
745 AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
746 OptLevelO3 = false;
749 const PassInfo *PassInf = PassList[i];
750 Pass *P = nullptr;
751 if (PassInf->getNormalCtor())
752 P = PassInf->getNormalCtor()();
753 else
754 errs() << argv[0] << ": cannot create pass: "
755 << PassInf->getPassName() << "\n";
756 if (P) {
757 PassKind Kind = P->getPassKind();
758 addPass(Passes, P);
760 if (AnalyzeOnly) {
761 switch (Kind) {
762 case PT_BasicBlock:
763 Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet));
764 break;
765 case PT_Region:
766 Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet));
767 break;
768 case PT_Loop:
769 Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet));
770 break;
771 case PT_Function:
772 Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet));
773 break;
774 case PT_CallGraphSCC:
775 Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet));
776 break;
777 default:
778 Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet));
779 break;
784 if (PrintEachXForm)
785 Passes.add(
786 createPrintModulePass(errs(), "", PreserveAssemblyUseListOrder));
789 if (StandardLinkOpts) {
790 AddStandardLinkPasses(Passes);
791 StandardLinkOpts = false;
794 if (OptLevelO0)
795 AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
797 if (OptLevelO1)
798 AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
800 if (OptLevelO2)
801 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
803 if (OptLevelOs)
804 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
806 if (OptLevelOz)
807 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
809 if (OptLevelO3)
810 AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
812 if (FPasses) {
813 FPasses->doInitialization();
814 for (Function &F : *M)
815 FPasses->run(F);
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) {
837 assert(Out);
838 OS = &Out->os();
839 if (RunTwice) {
840 BOS = make_unique<raw_svector_ostream>(Buffer);
841 OS = BOS.get();
843 if (OutputAssembly) {
844 if (EmitSummaryIndex)
845 report_fatal_error("Text output is incompatible with -module-summary");
846 if (EmitModuleHash)
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));
852 else
853 Passes.add(createBitcodeWriterPass(*OS, PreserveBitcodeUseListOrder,
854 EmitSummaryIndex, EmitModuleHash));
857 // Before executing passes, print the final values of the LLVM options.
858 cl::PrintOptionValues();
860 if (!RunTwice) {
861 // Now that we have all of the passes ready, run them.
862 Passes.run(*M);
863 } else {
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.
869 Passes.run(*M);
870 FirstRunBuffer = Buffer;
871 Buffer.clear();
873 Passes.run(*M2);
875 // Compare the two outputs and make sure they're the same
876 assert(Out);
877 if (Buffer.size() != FirstRunBuffer.size() ||
878 (memcmp(Buffer.data(), FirstRunBuffer.data(), Buffer.size()) != 0)) {
879 errs()
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();
885 Out->keep();
886 if (OptRemarkFile)
887 OptRemarkFile->keep();
888 return 1;
890 Out->os() << BOS->str();
893 if (DebugifyEach && !DebugifyExport.empty())
894 exportDebugifyStats(DebugifyExport, Passes.getDebugifyStatsMap());
896 // Declare success.
897 if (!NoOutput || PrintBreakpoints)
898 Out->keep();
900 if (OptRemarkFile)
901 OptRemarkFile->keep();
903 if (ThinLinkOut)
904 ThinLinkOut->keep();
906 return 0;