1 //===- Optimize.cpp - Optimize a complete program -------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements all optimization of the linked module for llvm-ld.
12 //===----------------------------------------------------------------------===//
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/Transforms/IPO.h"
23 #include "llvm/Transforms/Scalar.h"
26 // Optimization Options
28 enum OptimizationLevels {
33 OPT_AGGRESSIVE_LINK_TIME = 5
36 static cl::opt<OptimizationLevels> OptLevel(
37 cl::desc("Choose level of optimization to apply:"),
38 cl::init(OPT_FAST_COMPILE), cl::values(
39 clEnumValN(OPT_FAST_COMPILE,"O0",
40 "An alias for the -O1 option."),
41 clEnumValN(OPT_FAST_COMPILE,"O1",
42 "Optimize for linking speed, not execution speed."),
43 clEnumValN(OPT_SIMPLE,"O2",
44 "Perform only required/minimal optimizations"),
45 clEnumValN(OPT_AGGRESSIVE,"O3",
46 "An alias for the -O2 option."),
47 clEnumValN(OPT_LINK_TIME,"O4",
48 "Perform standard link time optimizations"),
49 clEnumValN(OPT_AGGRESSIVE_LINK_TIME,"O5",
50 "Perform aggressive link time optimizations"),
55 static cl::opt<bool> DisableInline("disable-inlining",
56 cl::desc("Do not run the inliner pass"));
59 DisableOptimizations("disable-opt",
60 cl::desc("Do not run any optimization passes"));
62 static cl::opt<bool> DisableInternalize("disable-internalize",
63 cl::desc("Do not mark all symbols as internal"));
65 static cl::opt<bool> Verify("verify",
66 cl::desc("Verify intermediate results of all passes"));
68 static cl::opt<bool> Strip("s",
69 cl::desc("Strip symbol info from executable"));
71 static cl::alias ExportDynamic("export-dynamic",
72 cl::aliasopt(DisableInternalize),
73 cl::desc("Alias for -disable-internalize"));
75 static cl::list<std::string> LoadableModules("load",
76 cl::value_desc("path to loadable optimization module"),
77 cl::desc("Load an optimization module and run it"));
79 // A utility function that adds a pass to the pass manager but will also add
80 // a verifier pass after if we're supposed to verify.
81 static inline void addPass(PassManager &PM, Pass *P) {
82 // Add the pass to the pass manager...
85 // If we are verifying all of the intermediate steps, add the verifier...
87 PM.add(createVerifierPass());
92 /// Optimize - Perform link time optimizations. This will run the scalar
93 /// optimizations, any loaded plugin-optimization modules, and then the
94 /// inter-procedural optimizations if applicable.
95 void Optimize(Module* M) {
97 // Instantiate the pass manager to organize the passes.
100 // If we're verifying, start off with a verification pass.
102 Passes.add(createVerifierPass());
104 // Add an appropriate TargetData instance for this module...
105 addPass(Passes, new TargetData("gccld", M));
107 // Often if the programmer does not specify proper prototypes for the
108 // functions they are calling, they end up calling a vararg version of the
109 // function that does not get a body filled in (the real function has typed
110 // arguments). This pass merges the two functions.
111 addPass(Passes, createFunctionResolvingPass());
113 if (!DisableOptimizations) {
114 // Now that composite has been compiled, scan through the module, looking
115 // for a main function. If main is defined, mark all other functions
117 addPass(Passes, createInternalizePass(!DisableInternalize));
119 // Now that we internalized some globals, see if we can hack on them!
120 addPass(Passes, createGlobalOptimizerPass());
122 // Linking modules together can lead to duplicated global constants, only
123 // keep one copy of each constant...
124 addPass(Passes, createConstantMergePass());
126 // If the -s command line option was specified, strip the symbols out of the
127 // resulting program to make it smaller. -s is a GLD option that we are
130 addPass(Passes, createStripSymbolsPass());
132 // Propagate constants at call sites into the functions they call.
133 addPass(Passes, createIPConstantPropagationPass());
135 // Remove unused arguments from functions...
136 addPass(Passes, createDeadArgEliminationPass());
139 addPass(Passes, createFunctionInliningPass()); // Inline small functions
141 addPass(Passes, createPruneEHPass()); // Remove dead EH info
142 addPass(Passes, createGlobalDCEPass()); // Remove dead functions
144 // If we didn't decide to inline a function, check to see if we can
145 // transform it to pass arguments by value instead of by reference.
146 addPass(Passes, createArgumentPromotionPass());
148 // The IPO passes may leave cruft around. Clean up after them.
149 addPass(Passes, createInstructionCombiningPass());
151 addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
153 // Run a few AA driven optimizations here and now, to cleanup the code.
154 addPass(Passes, createGlobalsModRefPass()); // IP alias analysis
156 addPass(Passes, createLICMPass()); // Hoist loop invariants
157 addPass(Passes, createLoadValueNumberingPass()); // GVN for load instrs
158 addPass(Passes, createGCSEPass()); // Remove common subexprs
159 addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores
161 // Cleanup and simplify the code after the scalar optimizations.
162 addPass(Passes, createInstructionCombiningPass());
164 // Delete basic blocks, which optimization passes may have killed...
165 addPass(Passes, createCFGSimplificationPass());
167 // Now that we have optimized the program, discard unreachable functions...
168 addPass(Passes, createGlobalDCEPass());
171 std::vector<std::string> plugins = LoadableModules;
172 for (std::vector<std::string>::iterator I = plugins.begin(),
173 E = plugins.end(); I != E; ++I) {
174 sys::DynamicLibrary dll(I->c_str());
175 typedef void (*OptimizeFunc)(PassManager&,int);
176 OptimizeFunc OF = OptimizeFunc(
177 dll.GetAddressOfSymbol("RunOptimizations"));
179 throw std::string("Optimization Module '") + *I +
180 "' is missing the RunOptimizations symbol";
182 (*OF)(Passes,OptLevel);
185 // Make sure everything is still good.
186 Passes.add(createVerifierPass());
188 // Run our queue of passes all at once now, efficiently.