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" |
| 18 | #include "llvm/Analysis/Verifier.h" |
| 19 | #include "llvm/Support/CommandLine.h" |
| 20 | #include "llvm/System/DynamicLibrary.h" |
| 21 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetMachine.h" |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/IPO.h" |
| 24 | #include "llvm/Transforms/Scalar.h" |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 25 | #include "llvm/Support/PassNameParser.h" |
| 26 | #include "llvm/Support/PluginLoader.h" |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 29 | // Pass Name Options as generated by the PassNameParser |
Chris Lattner | 30967a5 | 2006-08-27 22:07:43 +0000 | [diff] [blame^] | 30 | static cl::list<const PassInfo*, bool, PassNameParser> |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 31 | OptimizationList(cl::desc("Optimizations available:")); |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 32 | |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 33 | // Optimization Enumeration |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 34 | enum OptimizationLevels { |
| 35 | OPT_FAST_COMPILE = 1, |
| 36 | OPT_SIMPLE = 2, |
| 37 | OPT_AGGRESSIVE = 3, |
| 38 | OPT_LINK_TIME = 4, |
| 39 | OPT_AGGRESSIVE_LINK_TIME = 5 |
| 40 | }; |
| 41 | |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 42 | // Optimization Options |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 43 | static cl::opt<OptimizationLevels> OptLevel( |
| 44 | cl::desc("Choose level of optimization to apply:"), |
| 45 | cl::init(OPT_FAST_COMPILE), cl::values( |
| 46 | clEnumValN(OPT_FAST_COMPILE,"O0", |
| 47 | "An alias for the -O1 option."), |
| 48 | clEnumValN(OPT_FAST_COMPILE,"O1", |
| 49 | "Optimize for linking speed, not execution speed."), |
| 50 | clEnumValN(OPT_SIMPLE,"O2", |
| 51 | "Perform only required/minimal optimizations"), |
| 52 | clEnumValN(OPT_AGGRESSIVE,"O3", |
| 53 | "An alias for the -O2 option."), |
| 54 | clEnumValN(OPT_LINK_TIME,"O4", |
| 55 | "Perform standard link time optimizations"), |
| 56 | clEnumValN(OPT_AGGRESSIVE_LINK_TIME,"O5", |
| 57 | "Perform aggressive link time optimizations"), |
| 58 | clEnumValEnd |
| 59 | ) |
| 60 | ); |
| 61 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 62 | static cl::opt<bool> DisableInline("disable-inlining", |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 63 | cl::desc("Do not run the inliner pass")); |
| 64 | |
| 65 | static cl::opt<bool> |
| 66 | DisableOptimizations("disable-opt", |
| 67 | cl::desc("Do not run any optimization passes")); |
| 68 | |
| 69 | static cl::opt<bool> DisableInternalize("disable-internalize", |
| 70 | cl::desc("Do not mark all symbols as internal")); |
| 71 | |
Reid Spencer | ec9f050 | 2006-08-20 20:48:44 +0000 | [diff] [blame] | 72 | static cl::opt<bool> VerifyEach("verify-each", |
| 73 | cl::desc("Verify intermediate results of all passes")); |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 74 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 75 | static cl::opt<bool> Strip("s", |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 76 | cl::desc("Strip symbol info from executable")); |
| 77 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 78 | static cl::alias ExportDynamic("export-dynamic", |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 79 | cl::aliasopt(DisableInternalize), |
| 80 | cl::desc("Alias for -disable-internalize")); |
| 81 | |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 82 | // A utility function that adds a pass to the pass manager but will also add |
| 83 | // a verifier pass after if we're supposed to verify. |
| 84 | static inline void addPass(PassManager &PM, Pass *P) { |
| 85 | // Add the pass to the pass manager... |
| 86 | PM.add(P); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 87 | |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 88 | // If we are verifying all of the intermediate steps, add the verifier... |
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 | PM.add(createVerifierPass()); |
| 91 | } |
| 92 | |
| 93 | namespace llvm { |
| 94 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 95 | /// Optimize - Perform link time optimizations. This will run the scalar |
| 96 | /// optimizations, any loaded plugin-optimization modules, and then the |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 97 | /// inter-procedural optimizations if applicable. |
| 98 | void Optimize(Module* M) { |
| 99 | |
| 100 | // Instantiate the pass manager to organize the passes. |
| 101 | PassManager Passes; |
| 102 | |
| 103 | // If we're verifying, start off with a verification pass. |
Reid Spencer | ec9f050 | 2006-08-20 20:48:44 +0000 | [diff] [blame] | 104 | if (VerifyEach) |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 105 | Passes.add(createVerifierPass()); |
| 106 | |
| 107 | // Add an appropriate TargetData instance for this module... |
Chris Lattner | 831b121 | 2006-06-16 18:23:49 +0000 | [diff] [blame] | 108 | addPass(Passes, new TargetData(M)); |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 109 | |
| 110 | // Often if the programmer does not specify proper prototypes for the |
| 111 | // functions they are calling, they end up calling a vararg version of the |
| 112 | // function that does not get a body filled in (the real function has typed |
| 113 | // arguments). This pass merges the two functions. |
| 114 | addPass(Passes, createFunctionResolvingPass()); |
| 115 | |
| 116 | if (!DisableOptimizations) { |
Chris Lattner | fbcd54f | 2005-10-18 06:29:43 +0000 | [diff] [blame] | 117 | // Now that composite has been compiled, scan through the module, looking |
| 118 | // for a main function. If main is defined, mark all other functions |
| 119 | // internal. |
| 120 | addPass(Passes, createInternalizePass(!DisableInternalize)); |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 121 | |
| 122 | // Now that we internalized some globals, see if we can hack on them! |
| 123 | addPass(Passes, createGlobalOptimizerPass()); |
| 124 | |
| 125 | // Linking modules together can lead to duplicated global constants, only |
| 126 | // keep one copy of each constant... |
| 127 | addPass(Passes, createConstantMergePass()); |
| 128 | |
| 129 | // If the -s command line option was specified, strip the symbols out of the |
Chris Lattner | f8f70c1 | 2004-12-02 21:27:35 +0000 | [diff] [blame] | 130 | // resulting program to make it smaller. -s is a GLD option that we are |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 131 | // supporting. |
| 132 | if (Strip) |
Chris Lattner | f8f70c1 | 2004-12-02 21:27:35 +0000 | [diff] [blame] | 133 | addPass(Passes, createStripSymbolsPass()); |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 134 | |
| 135 | // Propagate constants at call sites into the functions they call. |
| 136 | addPass(Passes, createIPConstantPropagationPass()); |
| 137 | |
| 138 | // Remove unused arguments from functions... |
| 139 | addPass(Passes, createDeadArgEliminationPass()); |
| 140 | |
| 141 | if (!DisableInline) |
| 142 | addPass(Passes, createFunctionInliningPass()); // Inline small functions |
| 143 | |
| 144 | addPass(Passes, createPruneEHPass()); // Remove dead EH info |
| 145 | addPass(Passes, createGlobalDCEPass()); // Remove dead functions |
| 146 | |
| 147 | // If we didn't decide to inline a function, check to see if we can |
| 148 | // transform it to pass arguments by value instead of by reference. |
| 149 | addPass(Passes, createArgumentPromotionPass()); |
| 150 | |
| 151 | // The IPO passes may leave cruft around. Clean up after them. |
| 152 | addPass(Passes, createInstructionCombiningPass()); |
| 153 | |
| 154 | addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas |
| 155 | |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 156 | // Run a few AA driven optimizations, to cleanup the code. |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 157 | addPass(Passes, createGlobalsModRefPass()); // IP alias analysis |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 158 | addPass(Passes, createLICMPass()); // Hoist loop invariants |
| 159 | addPass(Passes, createLoadValueNumberingPass()); // GVN for load instrs |
| 160 | addPass(Passes, createGCSEPass()); // Remove common subexprs |
| 161 | addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores |
| 162 | |
| 163 | // Cleanup and simplify the code after the scalar optimizations. |
| 164 | addPass(Passes, createInstructionCombiningPass()); |
| 165 | |
Reid Spencer | 96690a8 | 2004-12-11 03:03:54 +0000 | [diff] [blame] | 166 | // Delete basic blocks, which optimization passes may have killed... |
| 167 | addPass(Passes, createCFGSimplificationPass()); |
| 168 | |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 169 | // Now that we have optimized the program, discard unreachable functions... |
| 170 | addPass(Passes, createGlobalDCEPass()); |
| 171 | } |
| 172 | |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 173 | // Create a new optimization pass for each one specified on the command line |
| 174 | std::auto_ptr<TargetMachine> target; |
| 175 | for (unsigned i = 0; i < OptimizationList.size(); ++i) { |
| 176 | const PassInfo *Opt = OptimizationList[i]; |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 177 | if (Opt->getNormalCtor()) |
Reid Spencer | dcee9d0 | 2006-08-20 20:54:38 +0000 | [diff] [blame] | 178 | addPass(Passes, Opt->getNormalCtor()()); |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 179 | else if (Opt->getTargetCtor()) { |
| 180 | assert(target.get() && "Could not allocate target machine!"); |
Reid Spencer | dcee9d0 | 2006-08-20 20:54:38 +0000 | [diff] [blame] | 181 | addPass(Passes, Opt->getTargetCtor()(*target.get())); |
Reid Spencer | 8e33fae | 2006-08-20 19:18:36 +0000 | [diff] [blame] | 182 | } else |
| 183 | std::cerr << "llvm-ld: cannot create pass: " << Opt->getPassName() |
| 184 | << "\n"; |
| 185 | } |
| 186 | |
| 187 | // The user's passes may leave cruft around. Clean up after them them but |
| 188 | // only if we haven't got DisableOptimizations set |
| 189 | if (!DisableOptimizations) { |
| 190 | addPass(Passes, createInstructionCombiningPass()); |
| 191 | addPass(Passes, createCFGSimplificationPass()); |
| 192 | addPass(Passes, createGlobalDCEPass()); |
Reid Spencer | c457fbb | 2004-11-20 19:43:28 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Make sure everything is still good. |
| 196 | Passes.add(createVerifierPass()); |
| 197 | |
| 198 | // Run our queue of passes all at once now, efficiently. |
| 199 | Passes.run(*M); |
| 200 | } |
| 201 | |
| 202 | } |