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 | // |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 5 | // This file was developed by Reid Spencer and is distributed under the |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source 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" |
| 16 | #include "llvm/Analysis/LoadValueNumbering.h" |
| 17 | #include "llvm/Analysis/Passes.h" |
Devang Patel | 54959d6 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/LoopPass.h" |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/Verifier.h" |
| 20 | #include "llvm/Support/CommandLine.h" |
| 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 | |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 35 | // Optimization Enumeration |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 36 | enum OptimizationLevels { |
| 37 | OPT_FAST_COMPILE = 1, |
| 38 | OPT_SIMPLE = 2, |
| 39 | OPT_AGGRESSIVE = 3, |
| 40 | OPT_LINK_TIME = 4, |
| 41 | OPT_AGGRESSIVE_LINK_TIME = 5 |
| 42 | }; |
| 43 | |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 44 | // Optimization Options |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 45 | static cl::opt<OptimizationLevels> OptLevel( |
| 46 | cl::desc("Choose level of optimization to apply:"), |
| 47 | cl::init(OPT_FAST_COMPILE), cl::values( |
| 48 | clEnumValN(OPT_FAST_COMPILE,"O0", |
| 49 | "An alias for the -O1 option."), |
| 50 | clEnumValN(OPT_FAST_COMPILE,"O1", |
| 51 | "Optimize for linking speed, not execution speed."), |
| 52 | clEnumValN(OPT_SIMPLE,"O2", |
| 53 | "Perform only required/minimal optimizations"), |
| 54 | clEnumValN(OPT_AGGRESSIVE,"O3", |
| 55 | "An alias for the -O2 option."), |
| 56 | clEnumValN(OPT_LINK_TIME,"O4", |
| 57 | "Perform standard link time optimizations"), |
| 58 | clEnumValN(OPT_AGGRESSIVE_LINK_TIME,"O5", |
| 59 | "Perform aggressive link time optimizations"), |
| 60 | clEnumValEnd |
| 61 | ) |
| 62 | ); |
| 63 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 64 | static cl::opt<bool> DisableInline("disable-inlining", |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 65 | cl::desc("Do not run the inliner pass")); |
| 66 | |
| 67 | static cl::opt<bool> |
| 68 | DisableOptimizations("disable-opt", |
| 69 | cl::desc("Do not run any optimization passes")); |
| 70 | |
| 71 | static cl::opt<bool> DisableInternalize("disable-internalize", |
| 72 | cl::desc("Do not mark all symbols as internal")); |
| 73 | |
Reid Spencer | ec9f050 | 2006-08-20 20:48:44 +0000 | [diff] [blame] | 74 | static cl::opt<bool> VerifyEach("verify-each", |
| 75 | cl::desc("Verify intermediate results of all passes")); |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 76 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 77 | static cl::alias ExportDynamic("export-dynamic", |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 78 | cl::aliasopt(DisableInternalize), |
| 79 | cl::desc("Alias for -disable-internalize")); |
| 80 | |
Reid Spencer | 7f04c08 | 2007-02-08 18:13:59 +0000 | [diff] [blame] | 81 | static cl::opt<bool> Strip("strip-all", |
| 82 | cl::desc("Strip all symbol info from executable")); |
| 83 | |
| 84 | static cl::alias A0("s", cl::desc("Alias for --strip-all"), |
| 85 | cl::aliasopt(Strip)); |
| 86 | |
| 87 | static cl::opt<bool> StripDebug("strip-debug", |
| 88 | cl::desc("Strip debugger symbol info from executable")); |
| 89 | |
| 90 | static cl::alias A1("S", cl::desc("Alias for --strip-debug"), |
| 91 | cl::aliasopt(StripDebug)); |
| 92 | |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 93 | // A utility function that adds a pass to the pass manager but will also add |
| 94 | // a verifier pass after if we're supposed to verify. |
| 95 | static inline void addPass(PassManager &PM, Pass *P) { |
| 96 | // Add the pass to the pass manager... |
| 97 | PM.add(P); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 98 | |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 99 | // If we are verifying all of the intermediate steps, add the verifier... |
Reid Spencer | ec9f050 | 2006-08-20 20:48:44 +0000 | [diff] [blame] | 100 | if (VerifyEach) |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 101 | PM.add(createVerifierPass()); |
| 102 | } |
| 103 | |
| 104 | namespace llvm { |
| 105 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 106 | /// Optimize - Perform link time optimizations. This will run the scalar |
| 107 | /// optimizations, any loaded plugin-optimization modules, and then the |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 108 | /// inter-procedural optimizations if applicable. |
| 109 | void Optimize(Module* M) { |
| 110 | |
| 111 | // Instantiate the pass manager to organize the passes. |
| 112 | PassManager Passes; |
| 113 | |
| 114 | // If we're verifying, start off with a verification pass. |
Reid Spencer | ec9f050 | 2006-08-20 20:48:44 +0000 | [diff] [blame] | 115 | if (VerifyEach) |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 116 | Passes.add(createVerifierPass()); |
| 117 | |
| 118 | // Add an appropriate TargetData instance for this module... |
Chris Lattner | 831b121 | 2006-06-16 18:23:49 +0000 | [diff] [blame] | 119 | addPass(Passes, new TargetData(M)); |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 120 | |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 121 | if (!DisableOptimizations) { |
Chris Lattner | fbcd54f | 2005-10-18 06:29:43 +0000 | [diff] [blame] | 122 | // Now that composite has been compiled, scan through the module, looking |
| 123 | // for a main function. If main is defined, mark all other functions |
| 124 | // internal. |
Chris Lattner | 037a704 | 2007-06-06 20:51:14 +0000 | [diff] [blame] | 125 | if (!DisableInternalize) |
| 126 | addPass(Passes, createInternalizePass(true)); |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 127 | |
Reid Spencer | 7f04c08 | 2007-02-08 18:13:59 +0000 | [diff] [blame] | 128 | // Propagate constants at call sites into the functions they call. This |
| 129 | // opens opportunities for globalopt (and inlining) by substituting function |
| 130 | // pointers passed as arguments to direct uses of functions. |
| 131 | addPass(Passes, createIPSCCPPass()); |
| 132 | |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 133 | // Now that we internalized some globals, see if we can hack on them! |
| 134 | addPass(Passes, createGlobalOptimizerPass()); |
| 135 | |
| 136 | // Linking modules together can lead to duplicated global constants, only |
| 137 | // keep one copy of each constant... |
| 138 | addPass(Passes, createConstantMergePass()); |
| 139 | |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 140 | // Remove unused arguments from functions... |
| 141 | addPass(Passes, createDeadArgEliminationPass()); |
| 142 | |
Reid Spencer | 7f04c08 | 2007-02-08 18:13:59 +0000 | [diff] [blame] | 143 | // Reduce the code after globalopt and ipsccp. Both can open up significant |
| 144 | // simplification opportunities, and both can propagate functions through |
| 145 | // function pointers. When this happens, we often have to resolve varargs |
| 146 | // calls, etc, so let instcombine do this. |
| 147 | addPass(Passes, createInstructionCombiningPass()); |
| 148 | |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 149 | if (!DisableInline) |
| 150 | addPass(Passes, createFunctionInliningPass()); // Inline small functions |
| 151 | |
| 152 | addPass(Passes, createPruneEHPass()); // Remove dead EH info |
Reid Spencer | 7f04c08 | 2007-02-08 18:13:59 +0000 | [diff] [blame] | 153 | addPass(Passes, createGlobalOptimizerPass()); // Optimize globals again. |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 154 | addPass(Passes, createGlobalDCEPass()); // Remove dead functions |
| 155 | |
| 156 | // If we didn't decide to inline a function, check to see if we can |
| 157 | // transform it to pass arguments by value instead of by reference. |
| 158 | addPass(Passes, createArgumentPromotionPass()); |
| 159 | |
| 160 | // The IPO passes may leave cruft around. Clean up after them. |
| 161 | addPass(Passes, createInstructionCombiningPass()); |
| 162 | |
| 163 | addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas |
| 164 | |
Reid Spencer | 7f04c08 | 2007-02-08 18:13:59 +0000 | [diff] [blame] | 165 | // Run a few AA driven optimizations here and now, to cleanup the code. |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 166 | addPass(Passes, createGlobalsModRefPass()); // IP alias analysis |
Reid Spencer | 7f04c08 | 2007-02-08 18:13:59 +0000 | [diff] [blame] | 167 | |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 168 | addPass(Passes, createLICMPass()); // Hoist loop invariants |
Owen Anderson | d06eb2c | 2007-09-08 22:23:52 +0000 | [diff] [blame] | 169 | addPass(Passes, createGVNPass()); // Remove redundancies |
Owen Anderson | f6a05f9 | 2007-08-01 06:36:51 +0000 | [diff] [blame] | 170 | addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 171 | |
| 172 | // Cleanup and simplify the code after the scalar optimizations. |
| 173 | addPass(Passes, createInstructionCombiningPass()); |
| 174 | |
Reid Spencer | 96690a8 | 2004-12-11 03:03:54 +0000 | [diff] [blame] | 175 | // Delete basic blocks, which optimization passes may have killed... |
| 176 | addPass(Passes, createCFGSimplificationPass()); |
| 177 | |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 178 | // Now that we have optimized the program, discard unreachable functions... |
| 179 | addPass(Passes, createGlobalDCEPass()); |
| 180 | } |
| 181 | |
Reid Spencer | 7f04c08 | 2007-02-08 18:13:59 +0000 | [diff] [blame] | 182 | // If the -s or -S command line options were specified, strip the symbols out |
| 183 | // of the resulting program to make it smaller. -s and -S are GNU ld options |
| 184 | // that we are supporting; they alias -strip-all and -strip-debug. |
| 185 | if (Strip || StripDebug) |
| 186 | addPass(Passes, createStripSymbolsPass(StripDebug && !Strip)); |
| 187 | |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 188 | // Create a new optimization pass for each one specified on the command line |
| 189 | std::auto_ptr<TargetMachine> target; |
| 190 | for (unsigned i = 0; i < OptimizationList.size(); ++i) { |
| 191 | const PassInfo *Opt = OptimizationList[i]; |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 192 | if (Opt->getNormalCtor()) |
Reid Spencer | dcee9d0 | 2006-08-20 20:54:38 +0000 | [diff] [blame] | 193 | addPass(Passes, Opt->getNormalCtor()()); |
Chris Lattner | cd950a5 | 2006-12-01 21:59:37 +0000 | [diff] [blame] | 194 | else |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 195 | std::cerr << "llvm-ld: cannot create pass: " << Opt->getPassName() |
| 196 | << "\n"; |
| 197 | } |
| 198 | |
| 199 | // The user's passes may leave cruft around. Clean up after them them but |
| 200 | // only if we haven't got DisableOptimizations set |
| 201 | if (!DisableOptimizations) { |
| 202 | addPass(Passes, createInstructionCombiningPass()); |
| 203 | addPass(Passes, createCFGSimplificationPass()); |
Chris Lattner | c8c5752 | 2007-04-05 16:50:20 +0000 | [diff] [blame] | 204 | addPass(Passes, createDeadCodeEliminationPass()); |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 205 | addPass(Passes, createGlobalDCEPass()); |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // Make sure everything is still good. |
| 209 | Passes.add(createVerifierPass()); |
| 210 | |
| 211 | // Run our queue of passes all at once now, efficiently. |
| 212 | Passes.run(*M); |
| 213 | } |
| 214 | |
| 215 | } |