Remove this pass as its no longer needed.
[oota-llvm.git] / tools / llvm-ld / Optimize.cpp
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/Transforms/IPO.h"
23 #include "llvm/Transforms/Scalar.h"
24 using namespace llvm;
25
26 // Optimization Options
27
28 enum OptimizationLevels {
29   OPT_FAST_COMPILE         = 1,
30   OPT_SIMPLE               = 2,
31   OPT_AGGRESSIVE           = 3,
32   OPT_LINK_TIME            = 4,
33   OPT_AGGRESSIVE_LINK_TIME = 5
34 };
35
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"),
51     clEnumValEnd
52   )
53 );
54
55 static cl::opt<bool> DisableInline("disable-inlining", 
56   cl::desc("Do not run the inliner pass"));
57
58 static cl::opt<bool>
59 DisableOptimizations("disable-opt",
60   cl::desc("Do not run any optimization passes"));
61
62 static cl::opt<bool> DisableInternalize("disable-internalize",
63   cl::desc("Do not mark all symbols as internal"));
64
65 static cl::opt<bool> Verify("verify", 
66   cl::desc("Verify intermediate results of all passes"));
67
68 static cl::opt<bool> Strip("s", 
69   cl::desc("Strip symbol info from executable"));
70
71 static cl::alias ExportDynamic("export-dynamic", 
72   cl::aliasopt(DisableInternalize),
73   cl::desc("Alias for -disable-internalize"));
74
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"));
78
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...
83   PM.add(P);
84   
85   // If we are verifying all of the intermediate steps, add the verifier...
86   if (Verify) 
87     PM.add(createVerifierPass());
88 }
89
90 namespace llvm {
91
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) {
96
97   // Instantiate the pass manager to organize the passes.
98   PassManager Passes;
99
100   // If we're verifying, start off with a verification pass.
101   if (Verify) 
102     Passes.add(createVerifierPass());
103
104   // Add an appropriate TargetData instance for this module...
105   addPass(Passes, new TargetData("gccld", M));
106
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());
112
113   if (!DisableOptimizations) {
114     if (!DisableInternalize) {
115       // Now that composite has been compiled, scan through the module, looking
116       // for a main function.  If main is defined, mark all other functions
117       // internal.
118       addPass(Passes, createInternalizePass());
119     }
120
121     // Now that we internalized some globals, see if we can hack on them!
122     addPass(Passes, createGlobalOptimizerPass());
123
124     // Linking modules together can lead to duplicated global constants, only
125     // keep one copy of each constant...
126     addPass(Passes, createConstantMergePass());
127
128     // If the -s command line option was specified, strip the symbols out of the
129     // resulting program to make it smaller.  -s is a GLD option that we are
130     // supporting.
131     if (Strip)
132       addPass(Passes, createStripSymbolsPass());
133
134     // Propagate constants at call sites into the functions they call.
135     addPass(Passes, createIPConstantPropagationPass());
136
137     // Remove unused arguments from functions...
138     addPass(Passes, createDeadArgEliminationPass());
139
140     if (!DisableInline)
141       addPass(Passes, createFunctionInliningPass()); // Inline small functions
142
143     addPass(Passes, createPruneEHPass());            // Remove dead EH info
144     addPass(Passes, createGlobalDCEPass());          // Remove dead functions
145
146     // If we didn't decide to inline a function, check to see if we can
147     // transform it to pass arguments by value instead of by reference.
148     addPass(Passes, createArgumentPromotionPass());
149
150     // The IPO passes may leave cruft around.  Clean up after them.
151     addPass(Passes, createInstructionCombiningPass());
152
153     addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
154
155     // Run a few AA driven optimizations here and now, to cleanup the code.
156     addPass(Passes, createGlobalsModRefPass());      // IP alias analysis
157
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
166     // Now that we have optimized the program, discard unreachable functions...
167     addPass(Passes, createGlobalDCEPass());
168   }
169
170   std::vector<std::string> plugins = LoadableModules;
171   for (std::vector<std::string>::iterator I = plugins.begin(), 
172       E = plugins.end(); I != E; ++I) {
173     sys::DynamicLibrary dll(I->c_str());
174     typedef void (*OptimizeFunc)(PassManager&,int);
175     OptimizeFunc OF = OptimizeFunc(
176         dll.GetAddressOfSymbol("RunOptimizations"));
177     if (OF == 0) {
178       throw std::string("Optimization Module '") + *I + 
179         "' is missing the RunOptimizations symbol";
180     }
181     (*OF)(Passes,OptLevel);
182   }
183
184   // Make sure everything is still good.
185   Passes.add(createVerifierPass());
186
187   // Run our queue of passes all at once now, efficiently.
188   Passes.run(*M);
189 }
190
191 }