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/LegacyPassNameParser.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/IR/UseListOrder.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/IR/LegacyPassManager.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/FileSystem.h"
43 #include "llvm/Support/Host.h"
44 #include "llvm/Support/ManagedStatic.h"
45 #include "llvm/Support/PluginLoader.h"
46 #include "llvm/Support/PrettyStackTrace.h"
47 #include "llvm/Support/Signals.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/Target/TargetMachine.h"
54 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
58 using namespace opt_tool;
60 // The OptimizationList is automatically populated with registered Passes by the
63 static cl::list<const PassInfo*, bool, PassNameParser>
64 PassList(cl::desc("Optimizations available:"));
66 // This flag specifies a textual description of the optimization pass pipeline
67 // to run over the module. This flag switches opt to use the new pass manager
68 // infrastructure, completely disabling all of the flags specific to the old
70 static cl::opt<std::string> PassPipeline(
72 cl::desc("A textual description of the pass pipeline for optimizing"),
75 // Other command line options...
77 static cl::opt<std::string>
78 InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
79 cl::init("-"), cl::value_desc("filename"));
81 static cl::opt<std::string>
82 OutputFilename("o", cl::desc("Override output filename"),
83 cl::value_desc("filename"));
86 Force("f", cl::desc("Enable binary output on terminals"));
89 PrintEachXForm("p", cl::desc("Print module after each transformation"));
92 NoOutput("disable-output",
93 cl::desc("Do not write result bitcode file"), cl::Hidden);
96 OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
99 NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
102 VerifyEach("verify-each", cl::desc("Verify after each transform"));
105 StripDebug("strip-debug",
106 cl::desc("Strip debugger symbol info from translation unit"));
109 DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
112 DisableOptimizations("disable-opt",
113 cl::desc("Do not run any optimization passes"));
116 StandardLinkOpts("std-link-opts",
117 cl::desc("Include the standard link time optimizations"));
121 cl::desc("Optimization level 1. Similar to clang -O1"));
125 cl::desc("Optimization level 2. Similar to clang -O2"));
129 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
133 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
137 cl::desc("Optimization level 3. Similar to clang -O3"));
139 static cl::opt<std::string>
140 TargetTriple("mtriple", cl::desc("Override target triple for module"));
143 UnitAtATime("funit-at-a-time",
144 cl::desc("Enable IPO. This corresponds to gcc's -funit-at-a-time"),
148 DisableLoopUnrolling("disable-loop-unrolling",
149 cl::desc("Disable loop unrolling in all relevant passes"),
152 DisableLoopVectorization("disable-loop-vectorization",
153 cl::desc("Disable the loop vectorization pass"),
157 DisableSLPVectorization("disable-slp-vectorization",
158 cl::desc("Disable the slp vectorization pass"),
163 DisableSimplifyLibCalls("disable-simplify-libcalls",
164 cl::desc("Disable simplify-libcalls"));
167 Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
170 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
173 AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
176 PrintBreakpoints("print-breakpoints-for-testing",
177 cl::desc("Print select breakpoints location for testing"));
179 static cl::opt<std::string>
180 DefaultDataLayout("default-data-layout",
181 cl::desc("data layout string to use if not specified by module"),
182 cl::value_desc("layout-string"), cl::init(""));
186 static inline void addPass(legacy::PassManagerBase &PM, Pass *P) {
187 // Add the pass to the pass manager...
190 // If we are verifying all of the intermediate steps, add the verifier...
192 PM.add(createVerifierPass());
195 /// This routine adds optimization passes based on selected optimization level,
198 /// OptLevel - Optimization Level
199 static void AddOptimizationPasses(legacy::PassManagerBase &MPM,
200 legacy::FunctionPassManager &FPM,
201 unsigned OptLevel, unsigned SizeLevel) {
202 FPM.add(createVerifierPass()); // Verify that input is correct
204 PassManagerBuilder Builder;
205 Builder.OptLevel = OptLevel;
206 Builder.SizeLevel = SizeLevel;
210 } else if (OptLevel > 1) {
211 Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel);
213 Builder.Inliner = createAlwaysInlinerPass();
215 Builder.DisableUnitAtATime = !UnitAtATime;
216 Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
217 DisableLoopUnrolling : OptLevel == 0;
219 // This is final, unless there is a #pragma vectorize enable
220 if (DisableLoopVectorization)
221 Builder.LoopVectorize = false;
222 // If option wasn't forced via cmd line (-vectorize-loops, -loop-vectorize)
223 else if (!Builder.LoopVectorize)
224 Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
226 // When #pragma vectorize is on for SLP, do the same as above
227 Builder.SLPVectorize =
228 DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2;
230 Builder.populateFunctionPassManager(FPM);
231 Builder.populateModulePassManager(MPM);
234 static void AddStandardLinkPasses(legacy::PassManagerBase &PM) {
235 PassManagerBuilder Builder;
236 Builder.VerifyInput = true;
237 if (DisableOptimizations)
238 Builder.OptLevel = 0;
241 Builder.Inliner = createFunctionInliningPass();
242 Builder.populateLTOPassManager(PM);
245 //===----------------------------------------------------------------------===//
246 // CodeGen-related helper functions.
249 static CodeGenOpt::Level GetCodeGenOptLevel() {
251 return CodeGenOpt::Less;
253 return CodeGenOpt::Default;
255 return CodeGenOpt::Aggressive;
256 return CodeGenOpt::None;
259 // Returns the TargetMachine instance or zero if no triple is provided.
260 static TargetMachine* GetTargetMachine(Triple TheTriple) {
262 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
264 // Some modules don't specify a triple, and this is okay.
269 // Package up features to be passed to target/subtarget
270 std::string FeaturesStr;
271 if (MAttrs.size() || MCPU == "native") {
272 SubtargetFeatures Features;
274 // If user asked for the 'native' CPU, we need to autodetect features.
275 // This is necessary for x86 where the CPU might not support all the
276 // features the autodetected CPU name lists in the target. For example,
277 // not all Sandybridge processors support AVX.
278 if (MCPU == "native") {
279 StringMap<bool> HostFeatures;
280 if (sys::getHostCPUFeatures(HostFeatures))
281 for (auto &F : HostFeatures)
282 Features.AddFeature(F.first(), F.second);
285 for (unsigned i = 0; i != MAttrs.size(); ++i)
286 Features.AddFeature(MAttrs[i]);
287 FeaturesStr = Features.getString();
290 if (MCPU == "native")
291 MCPU = sys::getHostCPUName();
293 return TheTarget->createTargetMachine(TheTriple.getTriple(),
295 InitTargetOptionsFromCodeGenFlags(),
297 GetCodeGenOptLevel());
300 #ifdef LINK_POLLY_INTO_TOOLS
302 void initializePollyPasses(llvm::PassRegistry &Registry);
306 //===----------------------------------------------------------------------===//
309 int main(int argc, char **argv) {
310 sys::PrintStackTraceOnErrorSignal();
311 llvm::PrettyStackTraceProgram X(argc, argv);
313 // Enable debug stream buffering.
314 EnableDebugBuffering = true;
316 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
317 LLVMContext &Context = getGlobalContext();
319 InitializeAllTargets();
320 InitializeAllTargetMCs();
321 InitializeAllAsmPrinters();
324 PassRegistry &Registry = *PassRegistry::getPassRegistry();
325 initializeCore(Registry);
326 initializeScalarOpts(Registry);
327 initializeObjCARCOpts(Registry);
328 initializeVectorization(Registry);
329 initializeIPO(Registry);
330 initializeAnalysis(Registry);
331 initializeIPA(Registry);
332 initializeTransformUtils(Registry);
333 initializeInstCombine(Registry);
334 initializeInstrumentation(Registry);
335 initializeTarget(Registry);
336 // For codegen passes, only passes that do IR to IR transformation are
338 initializeCodeGenPreparePass(Registry);
339 initializeAtomicExpandPass(Registry);
340 initializeRewriteSymbolsPass(Registry);
341 initializeWinEHPreparePass(Registry);
342 initializeDwarfEHPreparePass(Registry);
344 #ifdef LINK_POLLY_INTO_TOOLS
345 polly::initializePollyPasses(Registry);
348 // Turn on -preserve-bc-uselistorder by default, but let the command-line
350 setPreserveBitcodeUseListOrder(true);
352 cl::ParseCommandLineOptions(argc, argv,
353 "llvm .bc -> .bc modular optimizer and analysis printer\n");
355 if (AnalyzeOnly && NoOutput) {
356 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
362 // Load the input module...
363 std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
366 Err.print(argv[0], errs());
370 // Strip debug info before running the verifier.
374 // Immediately run the verifier to catch any problems before starting up the
375 // pass pipelines. Otherwise we can crash on broken code during
376 // doInitialization().
377 if (!NoVerify && verifyModule(*M, &errs())) {
378 errs() << argv[0] << ": " << InputFilename
379 << ": error: input module is broken!\n";
383 // If we are supposed to override the target triple, do so now.
384 if (!TargetTriple.empty())
385 M->setTargetTriple(Triple::normalize(TargetTriple));
387 // Figure out what stream we are supposed to write to...
388 std::unique_ptr<tool_output_file> Out;
390 if (!OutputFilename.empty())
391 errs() << "WARNING: The -o (output filename) option is ignored when\n"
392 "the --disable-output option is used.\n";
394 // Default to standard output.
395 if (OutputFilename.empty())
396 OutputFilename = "-";
399 Out.reset(new tool_output_file(OutputFilename, EC, sys::fs::F_None));
401 errs() << EC.message() << '\n';
406 Triple ModuleTriple(M->getTargetTriple());
407 TargetMachine *Machine = nullptr;
408 if (ModuleTriple.getArch())
409 Machine = GetTargetMachine(ModuleTriple);
410 std::unique_ptr<TargetMachine> TM(Machine);
412 // If the output is set to be emitted to standard out, and standard out is a
413 // console, print out a warning message and refuse to do it. We don't
414 // impress anyone by spewing tons of binary goo to a terminal.
415 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
416 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
419 if (PassPipeline.getNumOccurrences() > 0) {
420 OutputKind OK = OK_NoOutput;
422 OK = OutputAssembly ? OK_OutputAssembly : OK_OutputBitcode;
424 VerifierKind VK = VK_VerifyInAndOut;
428 VK = VK_VerifyEachPass;
430 // The user has asked to use the new pass manager and provided a pipeline
431 // string. Hand off the rest of the functionality to the new code for that
433 return runPassPipeline(argv[0], Context, *M, TM.get(), Out.get(),
434 PassPipeline, OK, VK,
435 shouldPreserveBitcodeUseListOrder())
440 // Create a PassManager to hold and optimize the collection of passes we are
443 legacy::PassManager Passes;
445 // Add an appropriate TargetLibraryInfo pass for the module's triple.
446 TargetLibraryInfoImpl TLII(ModuleTriple);
448 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
449 if (DisableSimplifyLibCalls)
450 TLII.disableAllFunctions();
451 Passes.add(new TargetLibraryInfoWrapperPass(TLII));
453 // Add an appropriate DataLayout instance for this module.
454 const DataLayout &DL = M->getDataLayout();
455 if (DL.isDefault() && !DefaultDataLayout.empty()) {
456 M->setDataLayout(DefaultDataLayout);
459 // Add internal analysis passes from the target machine.
460 Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis()
461 : TargetIRAnalysis()));
463 std::unique_ptr<legacy::FunctionPassManager> FPasses;
464 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
465 FPasses.reset(new legacy::FunctionPassManager(M.get()));
466 FPasses->add(createTargetTransformInfoWrapperPass(
467 TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis()));
470 if (PrintBreakpoints) {
471 // Default to standard output.
473 if (OutputFilename.empty())
474 OutputFilename = "-";
477 Out = llvm::make_unique<tool_output_file>(OutputFilename, EC,
480 errs() << EC.message() << '\n';
484 Passes.add(createBreakpointPrinter(Out->os()));
488 // Create a new optimization pass for each one specified on the command line
489 for (unsigned i = 0; i < PassList.size(); ++i) {
490 if (StandardLinkOpts &&
491 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
492 AddStandardLinkPasses(Passes);
493 StandardLinkOpts = false;
496 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
497 AddOptimizationPasses(Passes, *FPasses, 1, 0);
501 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
502 AddOptimizationPasses(Passes, *FPasses, 2, 0);
506 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
507 AddOptimizationPasses(Passes, *FPasses, 2, 1);
511 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
512 AddOptimizationPasses(Passes, *FPasses, 2, 2);
516 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
517 AddOptimizationPasses(Passes, *FPasses, 3, 0);
521 const PassInfo *PassInf = PassList[i];
523 if (PassInf->getTargetMachineCtor())
524 P = PassInf->getTargetMachineCtor()(TM.get());
525 else if (PassInf->getNormalCtor())
526 P = PassInf->getNormalCtor()();
528 errs() << argv[0] << ": cannot create pass: "
529 << PassInf->getPassName() << "\n";
531 PassKind Kind = P->getPassKind();
537 Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet));
540 Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet));
543 Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet));
546 Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet));
548 case PT_CallGraphSCC:
549 Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet));
552 Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet));
559 Passes.add(createPrintModulePass(errs()));
562 if (StandardLinkOpts) {
563 AddStandardLinkPasses(Passes);
564 StandardLinkOpts = false;
568 AddOptimizationPasses(Passes, *FPasses, 1, 0);
571 AddOptimizationPasses(Passes, *FPasses, 2, 0);
574 AddOptimizationPasses(Passes, *FPasses, 2, 1);
577 AddOptimizationPasses(Passes, *FPasses, 2, 2);
580 AddOptimizationPasses(Passes, *FPasses, 3, 0);
582 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
583 FPasses->doInitialization();
584 for (Function &F : *M)
586 FPasses->doFinalization();
589 // Check that the module is well formed on completion of optimization
590 if (!NoVerify && !VerifyEach)
591 Passes.add(createVerifierPass());
593 // Write bitcode or assembly to the output as the last step...
594 if (!NoOutput && !AnalyzeOnly) {
596 Passes.add(createPrintModulePass(Out->os()));
598 Passes.add(createBitcodeWriterPass(Out->os(),
599 shouldPreserveBitcodeUseListOrder()));
602 // Before executing passes, print the final values of the LLVM options.
603 cl::PrintOptionValues();
605 // Now that we have all of the passes ready, run them.
609 if (!NoOutput || PrintBreakpoints)