1 //===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Optimizations may be specified an arbitrary number of times on the command
11 // line, They are run in the order specified.
13 //===----------------------------------------------------------------------===//
15 #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.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/DebugInfo.h"
29 #include "llvm/IR/IRPrintingPasses.h"
30 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/LegacyPassManager.h"
32 #include "llvm/IR/LegacyPassNameParser.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/IR/Verifier.h"
35 #include "llvm/IRReader/IRReader.h"
36 #include "llvm/InitializePasses.h"
37 #include "llvm/LinkAllIR.h"
38 #include "llvm/LinkAllPasses.h"
39 #include "llvm/MC/SubtargetFeature.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/FileSystem.h"
42 #include "llvm/Support/Host.h"
43 #include "llvm/Support/ManagedStatic.h"
44 #include "llvm/Support/PluginLoader.h"
45 #include "llvm/Support/PrettyStackTrace.h"
46 #include "llvm/Support/Signals.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"));
105 static cl::opt<std::string> ThinLinkBitcodeFile(
106 "thin-link-bitcode-file", cl::value_desc("filename"),
108 "A file in which to write minimized bitcode for the thin link only"));
111 NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden);
114 VerifyEach("verify-each", cl::desc("Verify after each transform"));
117 DisableDITypeMap("disable-debug-info-type-map",
118 cl::desc("Don't use a uniquing type map for debug info"));
121 StripDebug("strip-debug",
122 cl::desc("Strip debugger symbol info from translation unit"));
125 DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
128 DisableOptimizations("disable-opt",
129 cl::desc("Do not run any optimization passes"));
132 StandardLinkOpts("std-link-opts",
133 cl::desc("Include the standard link time optimizations"));
137 cl::desc("Optimization level 0. Similar to clang -O0"));
141 cl::desc("Optimization level 1. Similar to clang -O1"));
145 cl::desc("Optimization level 2. Similar to clang -O2"));
149 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
153 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
157 cl::desc("Optimization level 3. Similar to clang -O3"));
159 static cl::opt<unsigned>
160 CodeGenOptLevel("codegen-opt-level",
161 cl::desc("Override optimization level for codegen hooks"));
163 static cl::opt<std::string>
164 TargetTriple("mtriple", cl::desc("Override target triple for module"));
167 UnitAtATime("funit-at-a-time",
168 cl::desc("Enable IPO. This corresponds to gcc's -funit-at-a-time"),
172 DisableLoopUnrolling("disable-loop-unrolling",
173 cl::desc("Disable loop unrolling in all relevant passes"),
176 DisableLoopVectorization("disable-loop-vectorization",
177 cl::desc("Disable the loop vectorization pass"),
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"));
206 PrintBreakpoints("print-breakpoints-for-testing",
207 cl::desc("Print select breakpoints location for testing"));
209 static cl::opt<std::string> ClDataLayout("data-layout",
210 cl::desc("data layout string to use"),
211 cl::value_desc("layout-string"),
214 static cl::opt<bool> PreserveBitcodeUseListOrder(
215 "preserve-bc-uselistorder",
216 cl::desc("Preserve use-list order when writing LLVM bitcode."),
217 cl::init(true), cl::Hidden);
219 static cl::opt<bool> PreserveAssemblyUseListOrder(
220 "preserve-ll-uselistorder",
221 cl::desc("Preserve use-list order when writing LLVM assembly."),
222 cl::init(false), cl::Hidden);
225 RunTwice("run-twice",
226 cl::desc("Run all passes twice, re-using the same pass manager."),
227 cl::init(false), cl::Hidden);
229 static cl::opt<bool> DiscardValueNames(
230 "discard-value-names",
231 cl::desc("Discard names from Value (other than GlobalValue)."),
232 cl::init(false), cl::Hidden);
234 static cl::opt<bool> Coroutines(
236 cl::desc("Enable coroutine passes."),
237 cl::init(false), cl::Hidden);
239 static cl::opt<bool> PassRemarksWithHotness(
240 "pass-remarks-with-hotness",
241 cl::desc("With PGO, include profile count in optimization remarks"),
244 static cl::opt<std::string>
245 RemarksFilename("pass-remarks-output",
246 cl::desc("YAML output filename for pass remarks"),
247 cl::value_desc("filename"));
249 static inline void addPass(legacy::PassManagerBase &PM, Pass *P) {
250 // Add the pass to the pass manager...
253 // If we are verifying all of the intermediate steps, add the verifier...
255 PM.add(createVerifierPass());
258 /// This routine adds optimization passes based on selected optimization level,
261 /// OptLevel - Optimization Level
262 static void AddOptimizationPasses(legacy::PassManagerBase &MPM,
263 legacy::FunctionPassManager &FPM,
264 TargetMachine *TM, unsigned OptLevel,
265 unsigned SizeLevel) {
266 if (!NoVerify || VerifyEach)
267 FPM.add(createVerifierPass()); // Verify that input is correct
269 PassManagerBuilder Builder;
270 Builder.OptLevel = OptLevel;
271 Builder.SizeLevel = SizeLevel;
275 } else if (OptLevel > 1) {
276 Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
278 Builder.Inliner = createAlwaysInlinerLegacyPass();
280 Builder.DisableUnitAtATime = !UnitAtATime;
281 Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
282 DisableLoopUnrolling : OptLevel == 0;
284 // This is final, unless there is a #pragma vectorize enable
285 if (DisableLoopVectorization)
286 Builder.LoopVectorize = false;
287 // If option wasn't forced via cmd line (-vectorize-loops, -loop-vectorize)
288 else if (!Builder.LoopVectorize)
289 Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
291 // When #pragma vectorize is on for SLP, do the same as above
292 Builder.SLPVectorize =
293 DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2;
296 TM->adjustPassManager(Builder);
299 addCoroutinePassesToExtensionPoints(Builder);
301 Builder.populateFunctionPassManager(FPM);
302 Builder.populateModulePassManager(MPM);
305 static void AddStandardLinkPasses(legacy::PassManagerBase &PM) {
306 PassManagerBuilder Builder;
307 Builder.VerifyInput = true;
308 if (DisableOptimizations)
309 Builder.OptLevel = 0;
312 Builder.Inliner = createFunctionInliningPass();
313 Builder.populateLTOPassManager(PM);
316 //===----------------------------------------------------------------------===//
317 // CodeGen-related helper functions.
320 static CodeGenOpt::Level GetCodeGenOptLevel() {
321 if (CodeGenOptLevel.getNumOccurrences())
322 return static_cast<CodeGenOpt::Level>(unsigned(CodeGenOptLevel));
324 return CodeGenOpt::Less;
326 return CodeGenOpt::Default;
328 return CodeGenOpt::Aggressive;
329 return CodeGenOpt::None;
332 // Returns the TargetMachine instance or zero if no triple is provided.
333 static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
334 StringRef FeaturesStr,
335 const TargetOptions &Options) {
337 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
339 // Some modules don't specify a triple, and this is okay.
344 return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr,
345 FeaturesStr, Options, getRelocModel(),
346 CMModel, GetCodeGenOptLevel());
349 #ifdef LINK_POLLY_INTO_TOOLS
351 void initializePollyPasses(llvm::PassRegistry &Registry);
355 //===----------------------------------------------------------------------===//
358 int main(int argc, char **argv) {
359 sys::PrintStackTraceOnErrorSignal(argv[0]);
360 llvm::PrettyStackTraceProgram X(argc, argv);
362 // Enable debug stream buffering.
363 EnableDebugBuffering = true;
365 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
368 InitializeAllTargets();
369 InitializeAllTargetMCs();
370 InitializeAllAsmPrinters();
371 InitializeAllAsmParsers();
374 PassRegistry &Registry = *PassRegistry::getPassRegistry();
375 initializeCore(Registry);
376 initializeCoroutines(Registry);
377 initializeScalarOpts(Registry);
378 initializeObjCARCOpts(Registry);
379 initializeVectorization(Registry);
380 initializeIPO(Registry);
381 initializeAnalysis(Registry);
382 initializeTransformUtils(Registry);
383 initializeInstCombine(Registry);
384 initializeInstrumentation(Registry);
385 initializeTarget(Registry);
386 // For codegen passes, only passes that do IR to IR transformation are
388 initializeCodeGenPreparePass(Registry);
389 initializeAtomicExpandPass(Registry);
390 initializeRewriteSymbolsLegacyPassPass(Registry);
391 initializeWinEHPreparePass(Registry);
392 initializeDwarfEHPreparePass(Registry);
393 initializeSafeStackPass(Registry);
394 initializeSjLjEHPreparePass(Registry);
395 initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
396 initializeGlobalMergePass(Registry);
397 initializeInterleavedAccessPass(Registry);
398 initializeCountingFunctionInserterPass(Registry);
399 initializeUnreachableBlockElimLegacyPassPass(Registry);
401 #ifdef LINK_POLLY_INTO_TOOLS
402 polly::initializePollyPasses(Registry);
405 cl::ParseCommandLineOptions(argc, argv,
406 "llvm .bc -> .bc modular optimizer and analysis printer\n");
408 if (AnalyzeOnly && NoOutput) {
409 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
415 Context.setDiscardValueNames(DiscardValueNames);
416 if (!DisableDITypeMap)
417 Context.enableDebugTypeODRUniquing();
419 if (PassRemarksWithHotness)
420 Context.setDiagnosticHotnessRequested(true);
422 std::unique_ptr<tool_output_file> YamlFile;
423 if (RemarksFilename != "") {
425 YamlFile = llvm::make_unique<tool_output_file>(RemarksFilename, EC,
428 errs() << EC.message() << '\n';
431 Context.setDiagnosticsOutputFile(
432 llvm::make_unique<yaml::Output>(YamlFile->os()));
435 // Load the input module...
436 std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
439 Err.print(argv[0], errs());
443 // Strip debug info before running the verifier.
447 // Immediately run the verifier to catch any problems before starting up the
448 // pass pipelines. Otherwise we can crash on broken code during
449 // doInitialization().
450 if (!NoVerify && verifyModule(*M, &errs())) {
451 errs() << argv[0] << ": " << InputFilename
452 << ": error: input module is broken!\n";
456 // If we are supposed to override the target triple or data layout, do so now.
457 if (!TargetTriple.empty())
458 M->setTargetTriple(Triple::normalize(TargetTriple));
459 if (!ClDataLayout.empty())
460 M->setDataLayout(ClDataLayout);
462 // Figure out what stream we are supposed to write to...
463 std::unique_ptr<tool_output_file> Out;
464 std::unique_ptr<tool_output_file> ThinLinkOut;
466 if (!OutputFilename.empty())
467 errs() << "WARNING: The -o (output filename) option is ignored when\n"
468 "the --disable-output option is used.\n";
470 // Default to standard output.
471 if (OutputFilename.empty())
472 OutputFilename = "-";
475 Out.reset(new tool_output_file(OutputFilename, EC, sys::fs::F_None));
477 errs() << EC.message() << '\n';
481 if (!ThinLinkBitcodeFile.empty()) {
483 new tool_output_file(ThinLinkBitcodeFile, EC, sys::fs::F_None));
485 errs() << EC.message() << '\n';
491 Triple ModuleTriple(M->getTargetTriple());
492 std::string CPUStr, FeaturesStr;
493 TargetMachine *Machine = nullptr;
494 const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
496 if (ModuleTriple.getArch()) {
497 CPUStr = getCPUStr();
498 FeaturesStr = getFeaturesStr();
499 Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options);
502 std::unique_ptr<TargetMachine> TM(Machine);
504 // Override function attributes based on CPUStr, FeaturesStr, and command line
506 setFunctionAttributes(CPUStr, FeaturesStr, *M);
508 // If the output is set to be emitted to standard out, and standard out is a
509 // console, print out a warning message and refuse to do it. We don't
510 // impress anyone by spewing tons of binary goo to a terminal.
511 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
512 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
515 if (PassPipeline.getNumOccurrences() > 0) {
516 OutputKind OK = OK_NoOutput;
518 OK = OutputAssembly ? OK_OutputAssembly : OK_OutputBitcode;
520 VerifierKind VK = VK_VerifyInAndOut;
524 VK = VK_VerifyEachPass;
526 // The user has asked to use the new pass manager and provided a pipeline
527 // string. Hand off the rest of the functionality to the new code for that
529 return runPassPipeline(argv[0], *M, TM.get(), Out.get(),
530 PassPipeline, OK, VK, PreserveAssemblyUseListOrder,
531 PreserveBitcodeUseListOrder, EmitSummaryIndex,
537 // Create a PassManager to hold and optimize the collection of passes we are
540 legacy::PassManager Passes;
542 // Add an appropriate TargetLibraryInfo pass for the module's triple.
543 TargetLibraryInfoImpl TLII(ModuleTriple);
545 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
546 if (DisableSimplifyLibCalls)
547 TLII.disableAllFunctions();
548 Passes.add(new TargetLibraryInfoWrapperPass(TLII));
550 // Add internal analysis passes from the target machine.
551 Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis()
552 : TargetIRAnalysis()));
554 std::unique_ptr<legacy::FunctionPassManager> FPasses;
555 if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz ||
557 FPasses.reset(new legacy::FunctionPassManager(M.get()));
558 FPasses->add(createTargetTransformInfoWrapperPass(
559 TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis()));
562 if (PrintBreakpoints) {
563 // Default to standard output.
565 if (OutputFilename.empty())
566 OutputFilename = "-";
569 Out = llvm::make_unique<tool_output_file>(OutputFilename, EC,
572 errs() << EC.message() << '\n';
576 Passes.add(createBreakpointPrinter(Out->os()));
580 // Create a new optimization pass for each one specified on the command line
581 for (unsigned i = 0; i < PassList.size(); ++i) {
582 if (StandardLinkOpts &&
583 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
584 AddStandardLinkPasses(Passes);
585 StandardLinkOpts = false;
588 if (OptLevelO0 && OptLevelO0.getPosition() < PassList.getPosition(i)) {
589 AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
593 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
594 AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
598 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
599 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
603 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
604 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
608 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
609 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
613 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
614 AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
618 const PassInfo *PassInf = PassList[i];
620 if (PassInf->getTargetMachineCtor())
621 P = PassInf->getTargetMachineCtor()(TM.get());
622 else if (PassInf->getNormalCtor())
623 P = PassInf->getNormalCtor()();
625 errs() << argv[0] << ": cannot create pass: "
626 << PassInf->getPassName() << "\n";
628 PassKind Kind = P->getPassKind();
634 Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet));
637 Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet));
640 Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet));
643 Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet));
645 case PT_CallGraphSCC:
646 Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet));
649 Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet));
657 createPrintModulePass(errs(), "", PreserveAssemblyUseListOrder));
660 if (StandardLinkOpts) {
661 AddStandardLinkPasses(Passes);
662 StandardLinkOpts = false;
666 AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
669 AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
672 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
675 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
678 AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
681 AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
684 FPasses->doInitialization();
685 for (Function &F : *M)
687 FPasses->doFinalization();
690 // Check that the module is well formed on completion of optimization
691 if (!NoVerify && !VerifyEach)
692 Passes.add(createVerifierPass());
694 // In run twice mode, we want to make sure the output is bit-by-bit
695 // equivalent if we run the pass manager again, so setup two buffers and
696 // a stream to write to them. Note that llc does something similar and it
697 // may be worth to abstract this out in the future.
698 SmallVector<char, 0> Buffer;
699 SmallVector<char, 0> CompileTwiceBuffer;
700 std::unique_ptr<raw_svector_ostream> BOS;
701 raw_ostream *OS = nullptr;
703 // Write bitcode or assembly to the output as the last step...
704 if (!NoOutput && !AnalyzeOnly) {
708 BOS = make_unique<raw_svector_ostream>(Buffer);
711 if (OutputAssembly) {
712 if (EmitSummaryIndex)
713 report_fatal_error("Text output is incompatible with -module-summary");
715 report_fatal_error("Text output is incompatible with -module-hash");
716 Passes.add(createPrintModulePass(*OS, "", PreserveAssemblyUseListOrder));
717 } else if (OutputThinLTOBC)
718 Passes.add(createWriteThinLTOBitcodePass(
719 *OS, ThinLinkOut ? &ThinLinkOut->os() : nullptr));
721 Passes.add(createBitcodeWriterPass(*OS, PreserveBitcodeUseListOrder,
722 EmitSummaryIndex, EmitModuleHash));
725 // Before executing passes, print the final values of the LLVM options.
726 cl::PrintOptionValues();
728 // If requested, run all passes again with the same pass manager to catch
729 // bugs caused by persistent state in the passes
731 std::unique_ptr<Module> M2(CloneModule(M.get()));
733 CompileTwiceBuffer = Buffer;
737 // Now that we have all of the passes ready, run them.
740 // Compare the two outputs and make sure they're the same
743 if (Buffer.size() != CompileTwiceBuffer.size() ||
744 (memcmp(Buffer.data(), CompileTwiceBuffer.data(), Buffer.size()) !=
746 errs() << "Running the pass manager twice changed the output.\n"
747 "Writing the result of the second run to the specified output.\n"
748 "To generate the one-run comparison binary, just run without\n"
749 "the compile-twice option\n";
750 Out->os() << BOS->str();
756 Out->os() << BOS->str();
760 if (!NoOutput || PrintBreakpoints)