blob: 0dc38570171c1cf8d673d989af845d7b22108429 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- Optimize.cpp - Optimize a complete program -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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/LoopPass.h"
19#include "llvm/Analysis/Verifier.h"
20#include "llvm/Support/CommandLine.h"
21#include "llvm/System/DynamicLibrary.h"
22#include "llvm/Target/TargetData.h"
23#include "llvm/Target/TargetMachine.h"
24#include "llvm/Transforms/IPO.h"
25#include "llvm/Transforms/Scalar.h"
26#include "llvm/Support/PassNameParser.h"
27#include "llvm/Support/PluginLoader.h"
28#include <iostream>
29using namespace llvm;
30
31// Pass Name Options as generated by the PassNameParser
32static cl::list<const PassInfo*, bool, PassNameParser>
33 OptimizationList(cl::desc("Optimizations available:"));
34
Andrew Lenharth0e7614e2008-03-19 22:32:43 +000035//Don't verify at the end
Andrew Lenharth553e50a2008-03-19 20:49:51 +000036static cl::opt<bool> DontVerify("disable-verify", cl::ReallyHidden);
37
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038static cl::opt<bool> DisableInline("disable-inlining",
39 cl::desc("Do not run the inliner pass"));
40
41static cl::opt<bool>
42DisableOptimizations("disable-opt",
43 cl::desc("Do not run any optimization passes"));
44
45static cl::opt<bool> DisableInternalize("disable-internalize",
46 cl::desc("Do not mark all symbols as internal"));
47
48static cl::opt<bool> VerifyEach("verify-each",
49 cl::desc("Verify intermediate results of all passes"));
50
51static cl::alias ExportDynamic("export-dynamic",
52 cl::aliasopt(DisableInternalize),
53 cl::desc("Alias for -disable-internalize"));
54
55static cl::opt<bool> Strip("strip-all",
56 cl::desc("Strip all symbol info from executable"));
57
58static cl::alias A0("s", cl::desc("Alias for --strip-all"),
59 cl::aliasopt(Strip));
60
61static cl::opt<bool> StripDebug("strip-debug",
62 cl::desc("Strip debugger symbol info from executable"));
63
64static cl::alias A1("S", cl::desc("Alias for --strip-debug"),
65 cl::aliasopt(StripDebug));
66
67// A utility function that adds a pass to the pass manager but will also add
68// a verifier pass after if we're supposed to verify.
69static inline void addPass(PassManager &PM, Pass *P) {
70 // Add the pass to the pass manager...
71 PM.add(P);
72
73 // If we are verifying all of the intermediate steps, add the verifier...
74 if (VerifyEach)
75 PM.add(createVerifierPass());
76}
77
78namespace llvm {
79
80/// Optimize - Perform link time optimizations. This will run the scalar
81/// optimizations, any loaded plugin-optimization modules, and then the
82/// inter-procedural optimizations if applicable.
83void Optimize(Module* M) {
84
85 // Instantiate the pass manager to organize the passes.
86 PassManager Passes;
87
88 // If we're verifying, start off with a verification pass.
89 if (VerifyEach)
90 Passes.add(createVerifierPass());
91
92 // Add an appropriate TargetData instance for this module...
93 addPass(Passes, new TargetData(M));
94
95 if (!DisableOptimizations) {
96 // Now that composite has been compiled, scan through the module, looking
97 // for a main function. If main is defined, mark all other functions
98 // internal.
99 if (!DisableInternalize)
100 addPass(Passes, createInternalizePass(true));
101
102 // Propagate constants at call sites into the functions they call. This
103 // opens opportunities for globalopt (and inlining) by substituting function
104 // pointers passed as arguments to direct uses of functions.
105 addPass(Passes, createIPSCCPPass());
106
107 // Now that we internalized some globals, see if we can hack on them!
108 addPass(Passes, createGlobalOptimizerPass());
109
110 // Linking modules together can lead to duplicated global constants, only
111 // keep one copy of each constant...
112 addPass(Passes, createConstantMergePass());
113
114 // Remove unused arguments from functions...
115 addPass(Passes, createDeadArgEliminationPass());
116
117 // Reduce the code after globalopt and ipsccp. Both can open up significant
118 // simplification opportunities, and both can propagate functions through
119 // function pointers. When this happens, we often have to resolve varargs
120 // calls, etc, so let instcombine do this.
121 addPass(Passes, createInstructionCombiningPass());
122
123 if (!DisableInline)
124 addPass(Passes, createFunctionInliningPass()); // Inline small functions
125
126 addPass(Passes, createPruneEHPass()); // Remove dead EH info
127 addPass(Passes, createGlobalOptimizerPass()); // Optimize globals again.
128 addPass(Passes, createGlobalDCEPass()); // Remove dead functions
129
130 // If we didn't decide to inline a function, check to see if we can
131 // transform it to pass arguments by value instead of by reference.
132 addPass(Passes, createArgumentPromotionPass());
133
134 // The IPO passes may leave cruft around. Clean up after them.
135 addPass(Passes, createInstructionCombiningPass());
Chris Lattnere38bd5392008-04-21 04:28:40 +0000136 addPass(Passes, createJumpThreadingPass()); // Thread jumps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
138
139 // Run a few AA driven optimizations here and now, to cleanup the code.
140 addPass(Passes, createGlobalsModRefPass()); // IP alias analysis
141
142 addPass(Passes, createLICMPass()); // Hoist loop invariants
Owen Anderson1b295922007-09-08 22:23:52 +0000143 addPass(Passes, createGVNPass()); // Remove redundancies
Owen Anderson7aee1532008-04-22 07:12:26 +0000144 addPass(Passes, createMemCpyOptPass()); // Remove dead memcpy's
Owen Anderson59bf86c2007-08-01 06:36:51 +0000145 addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146
147 // Cleanup and simplify the code after the scalar optimizations.
148 addPass(Passes, createInstructionCombiningPass());
149
Chris Lattnere38bd5392008-04-21 04:28:40 +0000150 addPass(Passes, createJumpThreadingPass()); // Thread jumps.
151
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 // 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, createDeadCodeEliminationPass());
182 addPass(Passes, createGlobalDCEPass());
183 }
184
185 // Make sure everything is still good.
Andrew Lenharth0e7614e2008-03-19 22:32:43 +0000186 if (!DontVerify)
Andrew Lenharth553e50a2008-03-19 20:49:51 +0000187 Passes.add(createVerifierPass());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188
189 // Run our queue of passes all at once now, efficiently.
190 Passes.run(*M);
191}
192
193}