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