Fix a really wrong comment.
[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 is distributed under the University of Illinois Open Source
6 // 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/Passes.h"
17 #include "llvm/Analysis/LoopPass.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>
28 using namespace llvm;
29
30 // Pass Name Options as generated by the PassNameParser
31 static cl::list<const PassInfo*, bool, PassNameParser>
32   OptimizationList(cl::desc("Optimizations available:"));
33
34 //Don't verify at the end
35 static cl::opt<bool> DontVerify("disable-verify", cl::ReallyHidden);
36
37 static cl::opt<bool> DisableInline("disable-inlining",
38   cl::desc("Do not run the inliner pass"));
39
40 static cl::opt<bool>
41 DisableOptimizations("disable-opt",
42   cl::desc("Do not run any optimization passes"));
43
44 static cl::opt<bool> DisableInternalize("disable-internalize",
45   cl::desc("Do not mark all symbols as internal"));
46
47 static cl::opt<bool> VerifyEach("verify-each",
48  cl::desc("Verify intermediate results of all passes"));
49
50 static cl::alias ExportDynamic("export-dynamic",
51   cl::aliasopt(DisableInternalize),
52   cl::desc("Alias for -disable-internalize"));
53
54 static cl::opt<bool> Strip("strip-all", 
55   cl::desc("Strip all symbol info from executable"));
56
57 static cl::alias A0("s", cl::desc("Alias for --strip-all"), 
58   cl::aliasopt(Strip));
59
60 static cl::opt<bool> StripDebug("strip-debug",
61   cl::desc("Strip debugger symbol info from executable"));
62
63 static cl::alias A1("S", cl::desc("Alias for --strip-debug"),
64   cl::aliasopt(StripDebug));
65
66 // A utility function that adds a pass to the pass manager but will also add
67 // a verifier pass after if we're supposed to verify.
68 static inline void addPass(PassManager &PM, Pass *P) {
69   // Add the pass to the pass manager...
70   PM.add(P);
71
72   // If we are verifying all of the intermediate steps, add the verifier...
73   if (VerifyEach)
74     PM.add(createVerifierPass());
75 }
76
77 namespace llvm {
78
79 /// Optimize - Perform link time optimizations. This will run the scalar
80 /// optimizations, any loaded plugin-optimization modules, and then the
81 /// inter-procedural optimizations if applicable.
82 void Optimize(Module* M) {
83
84   // Instantiate the pass manager to organize the passes.
85   PassManager Passes;
86
87   // If we're verifying, start off with a verification pass.
88   if (VerifyEach)
89     Passes.add(createVerifierPass());
90
91   // Add an appropriate TargetData instance for this module...
92   addPass(Passes, new TargetData(M));
93
94   if (!DisableOptimizations) {
95     // Now that composite has been compiled, scan through the module, looking
96     // for a main function.  If main is defined, mark all other functions
97     // internal.
98     if (!DisableInternalize)
99       addPass(Passes, createInternalizePass(true));
100
101     // Propagate constants at call sites into the functions they call.  This
102     // opens opportunities for globalopt (and inlining) by substituting function
103     // pointers passed as arguments to direct uses of functions.  
104     addPass(Passes, createIPSCCPPass());
105
106     // Now that we internalized some globals, see if we can hack on them!
107     addPass(Passes, createGlobalOptimizerPass());
108
109     // Linking modules together can lead to duplicated global constants, only
110     // keep one copy of each constant...
111     addPass(Passes, createConstantMergePass());
112
113     // Remove unused arguments from functions...
114     addPass(Passes, createDeadArgEliminationPass());
115
116     // Reduce the code after globalopt and ipsccp.  Both can open up significant
117     // simplification opportunities, and both can propagate functions through
118     // function pointers.  When this happens, we often have to resolve varargs
119     // calls, etc, so let instcombine do this.
120     addPass(Passes, createInstructionCombiningPass());
121
122     if (!DisableInline)
123       addPass(Passes, createFunctionInliningPass()); // Inline small functions
124
125     addPass(Passes, createPruneEHPass());            // Remove dead EH info
126     addPass(Passes, createGlobalOptimizerPass());    // Optimize globals again.
127     addPass(Passes, createGlobalDCEPass());          // Remove dead functions
128
129     // If we didn't decide to inline a function, check to see if we can
130     // transform it to pass arguments by value instead of by reference.
131     addPass(Passes, createArgumentPromotionPass());
132
133     // The IPO passes may leave cruft around.  Clean up after them.
134     addPass(Passes, createInstructionCombiningPass());
135     addPass(Passes, createJumpThreadingPass());        // Thread jumps.
136     addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
137
138     // Run a few AA driven optimizations here and now, to cleanup the code.
139     addPass(Passes, createGlobalsModRefPass());      // IP alias analysis
140
141     addPass(Passes, createLICMPass());               // Hoist loop invariants
142     addPass(Passes, createGVNPass());                  // Remove redundancies
143     addPass(Passes, createMemCpyOptPass());          // Remove dead memcpy's
144     addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores
145
146     // Cleanup and simplify the code after the scalar optimizations.
147     addPass(Passes, createInstructionCombiningPass());
148
149     addPass(Passes, createJumpThreadingPass());        // Thread jumps.
150     addPass(Passes, createPromoteMemoryToRegisterPass()); // Cleanup jumpthread.
151     
152     // Delete basic blocks, which optimization passes may have killed...
153     addPass(Passes, createCFGSimplificationPass());
154
155     // Now that we have optimized the program, discard unreachable functions...
156     addPass(Passes, createGlobalDCEPass());
157   }
158
159   // If the -s or -S command line options were specified, strip the symbols out
160   // of the resulting program to make it smaller.  -s and -S are GNU ld options
161   // that we are supporting; they alias -strip-all and -strip-debug.
162   if (Strip || StripDebug)
163     addPass(Passes, createStripSymbolsPass(StripDebug && !Strip));
164
165   // Create a new optimization pass for each one specified on the command line
166   std::auto_ptr<TargetMachine> target;
167   for (unsigned i = 0; i < OptimizationList.size(); ++i) {
168     const PassInfo *Opt = OptimizationList[i];
169     if (Opt->getNormalCtor())
170       addPass(Passes, Opt->getNormalCtor()());
171     else
172       std::cerr << "llvm-ld: cannot create pass: " << Opt->getPassName() 
173                 << "\n";
174   }
175
176   // The user's passes may leave cruft around. Clean up after them them but
177   // only if we haven't got DisableOptimizations set
178   if (!DisableOptimizations) {
179     addPass(Passes, createInstructionCombiningPass());
180     addPass(Passes, createCFGSimplificationPass());
181     addPass(Passes, createAggressiveDCEPass());
182     addPass(Passes, createGlobalDCEPass());
183   }
184
185   // Make sure everything is still good.
186   if (!DontVerify)
187     Passes.add(createVerifierPass());
188
189   // Run our queue of passes all at once now, efficiently.
190   Passes.run(*M);
191 }
192
193 }