Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 1 | //===- Optimize.cpp - Optimize a complete program -------------------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements all optimization of the linked module for llvm-ld. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Module.h" |
| 15 | #include "llvm/PassManager.h" |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/Passes.h" |
Devang Patel | 54959d6 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/LoopPass.h" |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/Verifier.h" |
| 19 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | 006a034 | 2009-06-03 21:06:14 +0000 | [diff] [blame^] | 20 | #include "llvm/Support/StandardPasses.h" |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 21 | #include "llvm/System/DynamicLibrary.h" |
| 22 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetMachine.h" |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/IPO.h" |
| 25 | #include "llvm/Transforms/Scalar.h" |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 26 | #include "llvm/Support/PassNameParser.h" |
| 27 | #include "llvm/Support/PluginLoader.h" |
Bill Wendling | a089d44 | 2006-11-17 10:09:22 +0000 | [diff] [blame] | 28 | #include <iostream> |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 31 | // Pass Name Options as generated by the PassNameParser |
Chris Lattner | 30967a5 | 2006-08-27 22:07:43 +0000 | [diff] [blame] | 32 | static cl::list<const PassInfo*, bool, PassNameParser> |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 33 | OptimizationList(cl::desc("Optimizations available:")); |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 34 | |
Andrew Lenharth | 230293b | 2008-03-19 22:32:43 +0000 | [diff] [blame] | 35 | //Don't verify at the end |
Andrew Lenharth | 50b5742 | 2008-03-19 20:49:51 +0000 | [diff] [blame] | 36 | static cl::opt<bool> DontVerify("disable-verify", cl::ReallyHidden); |
| 37 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 38 | static cl::opt<bool> DisableInline("disable-inlining", |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 39 | cl::desc("Do not run the inliner pass")); |
| 40 | |
| 41 | static cl::opt<bool> |
| 42 | DisableOptimizations("disable-opt", |
| 43 | cl::desc("Do not run any optimization passes")); |
| 44 | |
| 45 | static cl::opt<bool> DisableInternalize("disable-internalize", |
| 46 | cl::desc("Do not mark all symbols as internal")); |
| 47 | |
Reid Spencer | ec9f050 | 2006-08-20 20:48:44 +0000 | [diff] [blame] | 48 | static cl::opt<bool> VerifyEach("verify-each", |
| 49 | cl::desc("Verify intermediate results of all passes")); |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 50 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 51 | static cl::alias ExportDynamic("export-dynamic", |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 52 | cl::aliasopt(DisableInternalize), |
| 53 | cl::desc("Alias for -disable-internalize")); |
| 54 | |
Reid Spencer | 7f04c08 | 2007-02-08 18:13:59 +0000 | [diff] [blame] | 55 | static cl::opt<bool> Strip("strip-all", |
| 56 | cl::desc("Strip all symbol info from executable")); |
| 57 | |
| 58 | static cl::alias A0("s", cl::desc("Alias for --strip-all"), |
| 59 | cl::aliasopt(Strip)); |
| 60 | |
| 61 | static cl::opt<bool> StripDebug("strip-debug", |
| 62 | cl::desc("Strip debugger symbol info from executable")); |
| 63 | |
| 64 | static cl::alias A1("S", cl::desc("Alias for --strip-debug"), |
| 65 | cl::aliasopt(StripDebug)); |
| 66 | |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 67 | // A utility function that adds a pass to the pass manager but will also add |
| 68 | // a verifier pass after if we're supposed to verify. |
| 69 | static inline void addPass(PassManager &PM, Pass *P) { |
| 70 | // Add the pass to the pass manager... |
| 71 | PM.add(P); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 72 | |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 73 | // If we are verifying all of the intermediate steps, add the verifier... |
Reid Spencer | ec9f050 | 2006-08-20 20:48:44 +0000 | [diff] [blame] | 74 | if (VerifyEach) |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 75 | PM.add(createVerifierPass()); |
| 76 | } |
| 77 | |
| 78 | namespace llvm { |
| 79 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 80 | /// Optimize - Perform link time optimizations. This will run the scalar |
| 81 | /// optimizations, any loaded plugin-optimization modules, and then the |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 82 | /// inter-procedural optimizations if applicable. |
| 83 | void Optimize(Module* M) { |
| 84 | |
| 85 | // Instantiate the pass manager to organize the passes. |
| 86 | PassManager Passes; |
| 87 | |
| 88 | // If we're verifying, start off with a verification pass. |
Reid Spencer | ec9f050 | 2006-08-20 20:48:44 +0000 | [diff] [blame] | 89 | if (VerifyEach) |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 90 | Passes.add(createVerifierPass()); |
| 91 | |
| 92 | // Add an appropriate TargetData instance for this module... |
Chris Lattner | 831b121 | 2006-06-16 18:23:49 +0000 | [diff] [blame] | 93 | addPass(Passes, new TargetData(M)); |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 94 | |
Daniel Dunbar | 006a034 | 2009-06-03 21:06:14 +0000 | [diff] [blame^] | 95 | if (!DisableOptimizations) |
| 96 | createStandardLTOPasses(&Passes, !DisableInternalize, !DisableInline, |
| 97 | /*RunSecondGlobalOpt=*/true, VerifyEach); |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 98 | |
Reid Spencer | 7f04c08 | 2007-02-08 18:13:59 +0000 | [diff] [blame] | 99 | // If the -s or -S command line options were specified, strip the symbols out |
| 100 | // of the resulting program to make it smaller. -s and -S are GNU ld options |
| 101 | // that we are supporting; they alias -strip-all and -strip-debug. |
| 102 | if (Strip || StripDebug) |
| 103 | addPass(Passes, createStripSymbolsPass(StripDebug && !Strip)); |
| 104 | |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 105 | // Create a new optimization pass for each one specified on the command line |
| 106 | std::auto_ptr<TargetMachine> target; |
| 107 | for (unsigned i = 0; i < OptimizationList.size(); ++i) { |
| 108 | const PassInfo *Opt = OptimizationList[i]; |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 109 | if (Opt->getNormalCtor()) |
Reid Spencer | dcee9d0 | 2006-08-20 20:54:38 +0000 | [diff] [blame] | 110 | addPass(Passes, Opt->getNormalCtor()()); |
Chris Lattner | cd950a5 | 2006-12-01 21:59:37 +0000 | [diff] [blame] | 111 | else |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 112 | std::cerr << "llvm-ld: cannot create pass: " << Opt->getPassName() |
| 113 | << "\n"; |
| 114 | } |
| 115 | |
| 116 | // The user's passes may leave cruft around. Clean up after them them but |
| 117 | // only if we haven't got DisableOptimizations set |
| 118 | if (!DisableOptimizations) { |
| 119 | addPass(Passes, createInstructionCombiningPass()); |
| 120 | addPass(Passes, createCFGSimplificationPass()); |
Owen Anderson | 735e6ea | 2008-07-02 18:42:07 +0000 | [diff] [blame] | 121 | addPass(Passes, createAggressiveDCEPass()); |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 122 | addPass(Passes, createGlobalDCEPass()); |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // Make sure everything is still good. |
Andrew Lenharth | 230293b | 2008-03-19 22:32:43 +0000 | [diff] [blame] | 126 | if (!DontVerify) |
Andrew Lenharth | 50b5742 | 2008-03-19 20:49:51 +0000 | [diff] [blame] | 127 | Passes.add(createVerifierPass()); |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 128 | |
| 129 | // Run our queue of passes all at once now, efficiently. |
| 130 | Passes.run(*M); |
| 131 | } |
| 132 | |
| 133 | } |