blob: 4a63f8eb09ad37d92ab2c8e568adebf19c8fc967 [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//
Misha Brukman3da94ae2005-04-22 00:00:37 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencerc457fbb2004-11-20 19:43:28 +00006// University of Illinois Open Source 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"
18#include "llvm/Analysis/Verifier.h"
19#include "llvm/Support/CommandLine.h"
20#include "llvm/System/DynamicLibrary.h"
21#include "llvm/Target/TargetData.h"
Reid Spencer8e33fae2006-08-20 19:18:36 +000022#include "llvm/Target/TargetMachine.h"
Reid Spencerc457fbb2004-11-20 19:43:28 +000023#include "llvm/Transforms/IPO.h"
24#include "llvm/Transforms/Scalar.h"
Reid Spencer8e33fae2006-08-20 19:18:36 +000025#include "llvm/Support/PassNameParser.h"
26#include "llvm/Support/PluginLoader.h"
Bill Wendlinga089d442006-11-17 10:09:22 +000027#include <iostream>
Reid Spencerc457fbb2004-11-20 19:43:28 +000028using namespace llvm;
29
Reid Spencer8e33fae2006-08-20 19:18:36 +000030// Pass Name Options as generated by the PassNameParser
Chris Lattner30967a52006-08-27 22:07:43 +000031static cl::list<const PassInfo*, bool, PassNameParser>
Reid Spencer8e33fae2006-08-20 19:18:36 +000032 OptimizationList(cl::desc("Optimizations available:"));
Reid Spencerc457fbb2004-11-20 19:43:28 +000033
Reid Spencer8e33fae2006-08-20 19:18:36 +000034// Optimization Enumeration
Reid Spencerc457fbb2004-11-20 19:43:28 +000035enum OptimizationLevels {
36 OPT_FAST_COMPILE = 1,
37 OPT_SIMPLE = 2,
38 OPT_AGGRESSIVE = 3,
39 OPT_LINK_TIME = 4,
40 OPT_AGGRESSIVE_LINK_TIME = 5
41};
42
Reid Spencer8e33fae2006-08-20 19:18:36 +000043// Optimization Options
Reid Spencerc457fbb2004-11-20 19:43:28 +000044static cl::opt<OptimizationLevels> OptLevel(
45 cl::desc("Choose level of optimization to apply:"),
46 cl::init(OPT_FAST_COMPILE), cl::values(
47 clEnumValN(OPT_FAST_COMPILE,"O0",
48 "An alias for the -O1 option."),
49 clEnumValN(OPT_FAST_COMPILE,"O1",
50 "Optimize for linking speed, not execution speed."),
51 clEnumValN(OPT_SIMPLE,"O2",
52 "Perform only required/minimal optimizations"),
53 clEnumValN(OPT_AGGRESSIVE,"O3",
54 "An alias for the -O2 option."),
55 clEnumValN(OPT_LINK_TIME,"O4",
56 "Perform standard link time optimizations"),
57 clEnumValN(OPT_AGGRESSIVE_LINK_TIME,"O5",
58 "Perform aggressive link time optimizations"),
59 clEnumValEnd
60 )
61);
62
Misha Brukman3da94ae2005-04-22 00:00:37 +000063static cl::opt<bool> DisableInline("disable-inlining",
Reid Spencerc457fbb2004-11-20 19:43:28 +000064 cl::desc("Do not run the inliner pass"));
65
66static cl::opt<bool>
67DisableOptimizations("disable-opt",
68 cl::desc("Do not run any optimization passes"));
69
70static cl::opt<bool> DisableInternalize("disable-internalize",
71 cl::desc("Do not mark all symbols as internal"));
72
Reid Spencerec9f0502006-08-20 20:48:44 +000073static cl::opt<bool> VerifyEach("verify-each",
74 cl::desc("Verify intermediate results of all passes"));
Reid Spencerc457fbb2004-11-20 19:43:28 +000075
Misha Brukman3da94ae2005-04-22 00:00:37 +000076static cl::alias ExportDynamic("export-dynamic",
Reid Spencerc457fbb2004-11-20 19:43:28 +000077 cl::aliasopt(DisableInternalize),
78 cl::desc("Alias for -disable-internalize"));
79
Reid Spencer7f04c082007-02-08 18:13:59 +000080static cl::opt<bool> Strip("strip-all",
81 cl::desc("Strip all symbol info from executable"));
82
83static cl::alias A0("s", cl::desc("Alias for --strip-all"),
84 cl::aliasopt(Strip));
85
86static cl::opt<bool> StripDebug("strip-debug",
87 cl::desc("Strip debugger symbol info from executable"));
88
89static cl::alias A1("S", cl::desc("Alias for --strip-debug"),
90 cl::aliasopt(StripDebug));
91
Reid Spencerc457fbb2004-11-20 19:43:28 +000092// A utility function that adds a pass to the pass manager but will also add
93// a verifier pass after if we're supposed to verify.
94static inline void addPass(PassManager &PM, Pass *P) {
95 // Add the pass to the pass manager...
96 PM.add(P);
Misha Brukman3da94ae2005-04-22 00:00:37 +000097
Reid Spencerc457fbb2004-11-20 19:43:28 +000098 // If we are verifying all of the intermediate steps, add the verifier...
Reid Spencerec9f0502006-08-20 20:48:44 +000099 if (VerifyEach)
Reid Spencerc457fbb2004-11-20 19:43:28 +0000100 PM.add(createVerifierPass());
101}
102
103namespace llvm {
104
Misha Brukman3da94ae2005-04-22 00:00:37 +0000105/// Optimize - Perform link time optimizations. This will run the scalar
106/// optimizations, any loaded plugin-optimization modules, and then the
Reid Spencerc457fbb2004-11-20 19:43:28 +0000107/// inter-procedural optimizations if applicable.
108void Optimize(Module* M) {
109
110 // Instantiate the pass manager to organize the passes.
111 PassManager Passes;
112
113 // If we're verifying, start off with a verification pass.
Reid Spencerec9f0502006-08-20 20:48:44 +0000114 if (VerifyEach)
Reid Spencerc457fbb2004-11-20 19:43:28 +0000115 Passes.add(createVerifierPass());
116
117 // Add an appropriate TargetData instance for this module...
Chris Lattner831b1212006-06-16 18:23:49 +0000118 addPass(Passes, new TargetData(M));
Reid Spencerc457fbb2004-11-20 19:43:28 +0000119
Reid Spencerc457fbb2004-11-20 19:43:28 +0000120 if (!DisableOptimizations) {
Chris Lattnerfbcd54f2005-10-18 06:29:43 +0000121 // Now that composite has been compiled, scan through the module, looking
122 // for a main function. If main is defined, mark all other functions
123 // internal.
124 addPass(Passes, createInternalizePass(!DisableInternalize));
Reid Spencerc457fbb2004-11-20 19:43:28 +0000125
Reid Spencer7f04c082007-02-08 18:13:59 +0000126 // Propagate constants at call sites into the functions they call. This
127 // opens opportunities for globalopt (and inlining) by substituting function
128 // pointers passed as arguments to direct uses of functions.
129 addPass(Passes, createIPSCCPPass());
130
Reid Spencerc457fbb2004-11-20 19:43:28 +0000131 // Now that we internalized some globals, see if we can hack on them!
132 addPass(Passes, createGlobalOptimizerPass());
133
134 // Linking modules together can lead to duplicated global constants, only
135 // keep one copy of each constant...
136 addPass(Passes, createConstantMergePass());
137
Reid Spencerc457fbb2004-11-20 19:43:28 +0000138 // Remove unused arguments from functions...
139 addPass(Passes, createDeadArgEliminationPass());
140
Reid Spencer7f04c082007-02-08 18:13:59 +0000141 // Reduce the code after globalopt and ipsccp. Both can open up significant
142 // simplification opportunities, and both can propagate functions through
143 // function pointers. When this happens, we often have to resolve varargs
144 // calls, etc, so let instcombine do this.
145 addPass(Passes, createInstructionCombiningPass());
146
Reid Spencerc457fbb2004-11-20 19:43:28 +0000147 if (!DisableInline)
148 addPass(Passes, createFunctionInliningPass()); // Inline small functions
149
150 addPass(Passes, createPruneEHPass()); // Remove dead EH info
Reid Spencer7f04c082007-02-08 18:13:59 +0000151 addPass(Passes, createGlobalOptimizerPass()); // Optimize globals again.
Reid Spencerc457fbb2004-11-20 19:43:28 +0000152 addPass(Passes, createGlobalDCEPass()); // Remove dead functions
153
154 // If we didn't decide to inline a function, check to see if we can
155 // transform it to pass arguments by value instead of by reference.
156 addPass(Passes, createArgumentPromotionPass());
157
158 // The IPO passes may leave cruft around. Clean up after them.
159 addPass(Passes, createInstructionCombiningPass());
160
161 addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
162
Reid Spencer7f04c082007-02-08 18:13:59 +0000163 // Run a few AA driven optimizations here and now, to cleanup the code.
Reid Spencerc457fbb2004-11-20 19:43:28 +0000164 addPass(Passes, createGlobalsModRefPass()); // IP alias analysis
Reid Spencer7f04c082007-02-08 18:13:59 +0000165
Reid Spencerc457fbb2004-11-20 19:43:28 +0000166 addPass(Passes, createLICMPass()); // Hoist loop invariants
167 addPass(Passes, createLoadValueNumberingPass()); // GVN for load instrs
168 addPass(Passes, createGCSEPass()); // Remove common subexprs
169 addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores
170
171 // Cleanup and simplify the code after the scalar optimizations.
172 addPass(Passes, createInstructionCombiningPass());
173
Reid Spencer96690a82004-12-11 03:03:54 +0000174 // Delete basic blocks, which optimization passes may have killed...
175 addPass(Passes, createCFGSimplificationPass());
176
Reid Spencerc457fbb2004-11-20 19:43:28 +0000177 // Now that we have optimized the program, discard unreachable functions...
178 addPass(Passes, createGlobalDCEPass());
179 }
180
Reid Spencer7f04c082007-02-08 18:13:59 +0000181 // If the -s or -S command line options were specified, strip the symbols out
182 // of the resulting program to make it smaller. -s and -S are GNU ld options
183 // that we are supporting; they alias -strip-all and -strip-debug.
184 if (Strip || StripDebug)
185 addPass(Passes, createStripSymbolsPass(StripDebug && !Strip));
186
Reid Spencer8e33fae2006-08-20 19:18:36 +0000187 // Create a new optimization pass for each one specified on the command line
188 std::auto_ptr<TargetMachine> target;
189 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
190 const PassInfo *Opt = OptimizationList[i];
Reid Spencer8e33fae2006-08-20 19:18:36 +0000191 if (Opt->getNormalCtor())
Reid Spencerdcee9d02006-08-20 20:54:38 +0000192 addPass(Passes, Opt->getNormalCtor()());
Chris Lattnercd950a52006-12-01 21:59:37 +0000193 else
Reid Spencer8e33fae2006-08-20 19:18:36 +0000194 std::cerr << "llvm-ld: cannot create pass: " << Opt->getPassName()
195 << "\n";
196 }
197
198 // The user's passes may leave cruft around. Clean up after them them but
199 // only if we haven't got DisableOptimizations set
200 if (!DisableOptimizations) {
201 addPass(Passes, createInstructionCombiningPass());
202 addPass(Passes, createCFGSimplificationPass());
203 addPass(Passes, createGlobalDCEPass());
Reid Spencerc457fbb2004-11-20 19:43:28 +0000204 }
205
206 // Make sure everything is still good.
207 Passes.add(createVerifierPass());
208
209 // Run our queue of passes all at once now, efficiently.
210 Passes.run(*M);
211}
212
213}