blob: 85c6074843873ee2f5fe512700ac9283a6387f59 [file] [log] [blame]
Reid Spencerc457fbb2004-11-20 19:43:28 +00001//===- Optimize.cpp - Optimize a complete program -------------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
Reid Spencerc457fbb2004-11-20 19:43:28 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
Reid Spencerc457fbb2004-11-20 19:43:28 +00008//===----------------------------------------------------------------------===//
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"
Devang Patel54959d62007-03-07 04:41:30 +000018#include "llvm/Analysis/LoopPass.h"
Reid Spencerc457fbb2004-11-20 19:43:28 +000019#include "llvm/Analysis/Verifier.h"
20#include "llvm/Support/CommandLine.h"
21#include "llvm/System/DynamicLibrary.h"
22#include "llvm/Target/TargetData.h"
Reid Spencer8e33fae2006-08-20 19:18:36 +000023#include "llvm/Target/TargetMachine.h"
Reid Spencerc457fbb2004-11-20 19:43:28 +000024#include "llvm/Transforms/IPO.h"
25#include "llvm/Transforms/Scalar.h"
Reid Spencer8e33fae2006-08-20 19:18:36 +000026#include "llvm/Support/PassNameParser.h"
27#include "llvm/Support/PluginLoader.h"
Bill Wendlinga089d442006-11-17 10:09:22 +000028#include <iostream>
Reid Spencerc457fbb2004-11-20 19:43:28 +000029using namespace llvm;
30
Reid Spencer8e33fae2006-08-20 19:18:36 +000031// Pass Name Options as generated by the PassNameParser
Chris Lattner30967a52006-08-27 22:07:43 +000032static cl::list<const PassInfo*, bool, PassNameParser>
Reid Spencer8e33fae2006-08-20 19:18:36 +000033 OptimizationList(cl::desc("Optimizations available:"));
Reid Spencerc457fbb2004-11-20 19:43:28 +000034
Andrew Lenharth50b57422008-03-19 20:49:51 +000035//Don't veryify at the end
36static cl::opt<bool> DontVerify("disable-verify", cl::ReallyHidden);
37
Reid Spencer8e33fae2006-08-20 19:18:36 +000038// Optimization Enumeration
Reid Spencerc457fbb2004-11-20 19:43:28 +000039enum OptimizationLevels {
40 OPT_FAST_COMPILE = 1,
41 OPT_SIMPLE = 2,
42 OPT_AGGRESSIVE = 3,
43 OPT_LINK_TIME = 4,
44 OPT_AGGRESSIVE_LINK_TIME = 5
45};
46
Reid Spencer8e33fae2006-08-20 19:18:36 +000047// Optimization Options
Reid Spencerc457fbb2004-11-20 19:43:28 +000048static cl::opt<OptimizationLevels> OptLevel(
49 cl::desc("Choose level of optimization to apply:"),
50 cl::init(OPT_FAST_COMPILE), cl::values(
51 clEnumValN(OPT_FAST_COMPILE,"O0",
52 "An alias for the -O1 option."),
53 clEnumValN(OPT_FAST_COMPILE,"O1",
54 "Optimize for linking speed, not execution speed."),
55 clEnumValN(OPT_SIMPLE,"O2",
56 "Perform only required/minimal optimizations"),
57 clEnumValN(OPT_AGGRESSIVE,"O3",
58 "An alias for the -O2 option."),
59 clEnumValN(OPT_LINK_TIME,"O4",
60 "Perform standard link time optimizations"),
61 clEnumValN(OPT_AGGRESSIVE_LINK_TIME,"O5",
62 "Perform aggressive link time optimizations"),
63 clEnumValEnd
64 )
65);
66
Misha Brukman3da94ae2005-04-22 00:00:37 +000067static cl::opt<bool> DisableInline("disable-inlining",
Reid Spencerc457fbb2004-11-20 19:43:28 +000068 cl::desc("Do not run the inliner pass"));
69
70static cl::opt<bool>
71DisableOptimizations("disable-opt",
72 cl::desc("Do not run any optimization passes"));
73
74static cl::opt<bool> DisableInternalize("disable-internalize",
75 cl::desc("Do not mark all symbols as internal"));
76
Reid Spencerec9f0502006-08-20 20:48:44 +000077static cl::opt<bool> VerifyEach("verify-each",
78 cl::desc("Verify intermediate results of all passes"));
Reid Spencerc457fbb2004-11-20 19:43:28 +000079
Misha Brukman3da94ae2005-04-22 00:00:37 +000080static cl::alias ExportDynamic("export-dynamic",
Reid Spencerc457fbb2004-11-20 19:43:28 +000081 cl::aliasopt(DisableInternalize),
82 cl::desc("Alias for -disable-internalize"));
83
Reid Spencer7f04c082007-02-08 18:13:59 +000084static cl::opt<bool> Strip("strip-all",
85 cl::desc("Strip all symbol info from executable"));
86
87static cl::alias A0("s", cl::desc("Alias for --strip-all"),
88 cl::aliasopt(Strip));
89
90static cl::opt<bool> StripDebug("strip-debug",
91 cl::desc("Strip debugger symbol info from executable"));
92
93static cl::alias A1("S", cl::desc("Alias for --strip-debug"),
94 cl::aliasopt(StripDebug));
95
Reid Spencerc457fbb2004-11-20 19:43:28 +000096// A utility function that adds a pass to the pass manager but will also add
97// a verifier pass after if we're supposed to verify.
98static inline void addPass(PassManager &PM, Pass *P) {
99 // Add the pass to the pass manager...
100 PM.add(P);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000101
Reid Spencerc457fbb2004-11-20 19:43:28 +0000102 // If we are verifying all of the intermediate steps, add the verifier...
Reid Spencerec9f0502006-08-20 20:48:44 +0000103 if (VerifyEach)
Reid Spencerc457fbb2004-11-20 19:43:28 +0000104 PM.add(createVerifierPass());
105}
106
107namespace llvm {
108
Misha Brukman3da94ae2005-04-22 00:00:37 +0000109/// Optimize - Perform link time optimizations. This will run the scalar
110/// optimizations, any loaded plugin-optimization modules, and then the
Reid Spencerc457fbb2004-11-20 19:43:28 +0000111/// inter-procedural optimizations if applicable.
112void Optimize(Module* M) {
113
114 // Instantiate the pass manager to organize the passes.
115 PassManager Passes;
116
117 // If we're verifying, start off with a verification pass.
Reid Spencerec9f0502006-08-20 20:48:44 +0000118 if (VerifyEach)
Reid Spencerc457fbb2004-11-20 19:43:28 +0000119 Passes.add(createVerifierPass());
120
121 // Add an appropriate TargetData instance for this module...
Chris Lattner831b1212006-06-16 18:23:49 +0000122 addPass(Passes, new TargetData(M));
Reid Spencerc457fbb2004-11-20 19:43:28 +0000123
Reid Spencerc457fbb2004-11-20 19:43:28 +0000124 if (!DisableOptimizations) {
Chris Lattnerfbcd54f2005-10-18 06:29:43 +0000125 // Now that composite has been compiled, scan through the module, looking
126 // for a main function. If main is defined, mark all other functions
127 // internal.
Chris Lattner037a7042007-06-06 20:51:14 +0000128 if (!DisableInternalize)
129 addPass(Passes, createInternalizePass(true));
Reid Spencerc457fbb2004-11-20 19:43:28 +0000130
Reid Spencer7f04c082007-02-08 18:13:59 +0000131 // Propagate constants at call sites into the functions they call. This
132 // opens opportunities for globalopt (and inlining) by substituting function
133 // pointers passed as arguments to direct uses of functions.
134 addPass(Passes, createIPSCCPPass());
135
Reid Spencerc457fbb2004-11-20 19:43:28 +0000136 // Now that we internalized some globals, see if we can hack on them!
137 addPass(Passes, createGlobalOptimizerPass());
138
139 // Linking modules together can lead to duplicated global constants, only
140 // keep one copy of each constant...
141 addPass(Passes, createConstantMergePass());
142
Reid Spencerc457fbb2004-11-20 19:43:28 +0000143 // Remove unused arguments from functions...
144 addPass(Passes, createDeadArgEliminationPass());
145
Reid Spencer7f04c082007-02-08 18:13:59 +0000146 // Reduce the code after globalopt and ipsccp. Both can open up significant
147 // simplification opportunities, and both can propagate functions through
148 // function pointers. When this happens, we often have to resolve varargs
149 // calls, etc, so let instcombine do this.
150 addPass(Passes, createInstructionCombiningPass());
151
Reid Spencerc457fbb2004-11-20 19:43:28 +0000152 if (!DisableInline)
153 addPass(Passes, createFunctionInliningPass()); // Inline small functions
154
155 addPass(Passes, createPruneEHPass()); // Remove dead EH info
Reid Spencer7f04c082007-02-08 18:13:59 +0000156 addPass(Passes, createGlobalOptimizerPass()); // Optimize globals again.
Reid Spencerc457fbb2004-11-20 19:43:28 +0000157 addPass(Passes, createGlobalDCEPass()); // Remove dead functions
158
159 // If we didn't decide to inline a function, check to see if we can
160 // transform it to pass arguments by value instead of by reference.
161 addPass(Passes, createArgumentPromotionPass());
162
163 // The IPO passes may leave cruft around. Clean up after them.
164 addPass(Passes, createInstructionCombiningPass());
165
166 addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
167
Reid Spencer7f04c082007-02-08 18:13:59 +0000168 // Run a few AA driven optimizations here and now, to cleanup the code.
Reid Spencerc457fbb2004-11-20 19:43:28 +0000169 addPass(Passes, createGlobalsModRefPass()); // IP alias analysis
Reid Spencer7f04c082007-02-08 18:13:59 +0000170
Reid Spencerc457fbb2004-11-20 19:43:28 +0000171 addPass(Passes, createLICMPass()); // Hoist loop invariants
Owen Andersond06eb2c2007-09-08 22:23:52 +0000172 addPass(Passes, createGVNPass()); // Remove redundancies
Owen Andersonf6a05f92007-08-01 06:36:51 +0000173 addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores
Reid Spencerc457fbb2004-11-20 19:43:28 +0000174
175 // Cleanup and simplify the code after the scalar optimizations.
176 addPass(Passes, createInstructionCombiningPass());
177
Reid Spencer96690a82004-12-11 03:03:54 +0000178 // Delete basic blocks, which optimization passes may have killed...
179 addPass(Passes, createCFGSimplificationPass());
180
Reid Spencerc457fbb2004-11-20 19:43:28 +0000181 // Now that we have optimized the program, discard unreachable functions...
182 addPass(Passes, createGlobalDCEPass());
183 }
184
Reid Spencer7f04c082007-02-08 18:13:59 +0000185 // If the -s or -S command line options were specified, strip the symbols out
186 // of the resulting program to make it smaller. -s and -S are GNU ld options
187 // that we are supporting; they alias -strip-all and -strip-debug.
188 if (Strip || StripDebug)
189 addPass(Passes, createStripSymbolsPass(StripDebug && !Strip));
190
Reid Spencer8e33fae2006-08-20 19:18:36 +0000191 // Create a new optimization pass for each one specified on the command line
192 std::auto_ptr<TargetMachine> target;
193 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
194 const PassInfo *Opt = OptimizationList[i];
Reid Spencer8e33fae2006-08-20 19:18:36 +0000195 if (Opt->getNormalCtor())
Reid Spencerdcee9d02006-08-20 20:54:38 +0000196 addPass(Passes, Opt->getNormalCtor()());
Chris Lattnercd950a52006-12-01 21:59:37 +0000197 else
Reid Spencer8e33fae2006-08-20 19:18:36 +0000198 std::cerr << "llvm-ld: cannot create pass: " << Opt->getPassName()
199 << "\n";
200 }
201
202 // The user's passes may leave cruft around. Clean up after them them but
203 // only if we haven't got DisableOptimizations set
204 if (!DisableOptimizations) {
205 addPass(Passes, createInstructionCombiningPass());
206 addPass(Passes, createCFGSimplificationPass());
Chris Lattnerc8c57522007-04-05 16:50:20 +0000207 addPass(Passes, createDeadCodeEliminationPass());
Reid Spencer8e33fae2006-08-20 19:18:36 +0000208 addPass(Passes, createGlobalDCEPass());
Reid Spencerc457fbb2004-11-20 19:43:28 +0000209 }
210
211 // Make sure everything is still good.
Andrew Lenharth50b57422008-03-19 20:49:51 +0000212 if(!DontVerify)
213 Passes.add(createVerifierPass());
Reid Spencerc457fbb2004-11-20 19:43:28 +0000214
215 // Run our queue of passes all at once now, efficiently.
216 Passes.run(*M);
217}
218
219}