blob: f53970371cdd56a848f797ac609886e434743500 [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"
Reid Spencerc457fbb2004-11-20 19:43:28 +000027using namespace llvm;
28
Reid Spencer8e33fae2006-08-20 19:18:36 +000029// Pass Name Options as generated by the PassNameParser
30static cl::list<const PassInfo*, bool,
31 FilteredPassNameParser<PassInfo::Optimization> >
32 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::opt<bool> Strip("s",
Reid Spencerc457fbb2004-11-20 19:43:28 +000077 cl::desc("Strip symbol info from executable"));
78
Misha Brukman3da94ae2005-04-22 00:00:37 +000079static cl::alias ExportDynamic("export-dynamic",
Reid Spencerc457fbb2004-11-20 19:43:28 +000080 cl::aliasopt(DisableInternalize),
81 cl::desc("Alias for -disable-internalize"));
82
Reid Spencerc457fbb2004-11-20 19:43:28 +000083// A utility function that adds a pass to the pass manager but will also add
84// a verifier pass after if we're supposed to verify.
85static inline void addPass(PassManager &PM, Pass *P) {
86 // Add the pass to the pass manager...
87 PM.add(P);
Misha Brukman3da94ae2005-04-22 00:00:37 +000088
Reid Spencerc457fbb2004-11-20 19:43:28 +000089 // If we are verifying all of the intermediate steps, add the verifier...
Reid Spencerec9f0502006-08-20 20:48:44 +000090 if (VerifyEach)
Reid Spencerc457fbb2004-11-20 19:43:28 +000091 PM.add(createVerifierPass());
92}
93
94namespace llvm {
95
Misha Brukman3da94ae2005-04-22 00:00:37 +000096/// Optimize - Perform link time optimizations. This will run the scalar
97/// optimizations, any loaded plugin-optimization modules, and then the
Reid Spencerc457fbb2004-11-20 19:43:28 +000098/// inter-procedural optimizations if applicable.
99void Optimize(Module* M) {
100
101 // Instantiate the pass manager to organize the passes.
102 PassManager Passes;
103
104 // If we're verifying, start off with a verification pass.
Reid Spencerec9f0502006-08-20 20:48:44 +0000105 if (VerifyEach)
Reid Spencerc457fbb2004-11-20 19:43:28 +0000106 Passes.add(createVerifierPass());
107
108 // Add an appropriate TargetData instance for this module...
Chris Lattner831b1212006-06-16 18:23:49 +0000109 addPass(Passes, new TargetData(M));
Reid Spencerc457fbb2004-11-20 19:43:28 +0000110
111 // Often if the programmer does not specify proper prototypes for the
112 // functions they are calling, they end up calling a vararg version of the
113 // function that does not get a body filled in (the real function has typed
114 // arguments). This pass merges the two functions.
115 addPass(Passes, createFunctionResolvingPass());
116
117 if (!DisableOptimizations) {
Chris Lattnerfbcd54f2005-10-18 06:29:43 +0000118 // Now that composite has been compiled, scan through the module, looking
119 // for a main function. If main is defined, mark all other functions
120 // internal.
121 addPass(Passes, createInternalizePass(!DisableInternalize));
Reid Spencerc457fbb2004-11-20 19:43:28 +0000122
123 // Now that we internalized some globals, see if we can hack on them!
124 addPass(Passes, createGlobalOptimizerPass());
125
126 // Linking modules together can lead to duplicated global constants, only
127 // keep one copy of each constant...
128 addPass(Passes, createConstantMergePass());
129
130 // If the -s command line option was specified, strip the symbols out of the
Chris Lattnerf8f70c12004-12-02 21:27:35 +0000131 // resulting program to make it smaller. -s is a GLD option that we are
Reid Spencerc457fbb2004-11-20 19:43:28 +0000132 // supporting.
133 if (Strip)
Chris Lattnerf8f70c12004-12-02 21:27:35 +0000134 addPass(Passes, createStripSymbolsPass());
Reid Spencerc457fbb2004-11-20 19:43:28 +0000135
136 // Propagate constants at call sites into the functions they call.
137 addPass(Passes, createIPConstantPropagationPass());
138
139 // Remove unused arguments from functions...
140 addPass(Passes, createDeadArgEliminationPass());
141
142 if (!DisableInline)
143 addPass(Passes, createFunctionInliningPass()); // Inline small functions
144
145 addPass(Passes, createPruneEHPass()); // Remove dead EH info
146 addPass(Passes, createGlobalDCEPass()); // Remove dead functions
147
148 // If we didn't decide to inline a function, check to see if we can
149 // transform it to pass arguments by value instead of by reference.
150 addPass(Passes, createArgumentPromotionPass());
151
152 // The IPO passes may leave cruft around. Clean up after them.
153 addPass(Passes, createInstructionCombiningPass());
154
155 addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
156
Reid Spencer8e33fae2006-08-20 19:18:36 +0000157 // Run a few AA driven optimizations, to cleanup the code.
Reid Spencerc457fbb2004-11-20 19:43:28 +0000158 addPass(Passes, createGlobalsModRefPass()); // IP alias analysis
Reid Spencerc457fbb2004-11-20 19:43:28 +0000159 addPass(Passes, createLICMPass()); // Hoist loop invariants
160 addPass(Passes, createLoadValueNumberingPass()); // GVN for load instrs
161 addPass(Passes, createGCSEPass()); // Remove common subexprs
162 addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores
163
164 // Cleanup and simplify the code after the scalar optimizations.
165 addPass(Passes, createInstructionCombiningPass());
166
Reid Spencer96690a82004-12-11 03:03:54 +0000167 // Delete basic blocks, which optimization passes may have killed...
168 addPass(Passes, createCFGSimplificationPass());
169
Reid Spencerc457fbb2004-11-20 19:43:28 +0000170 // Now that we have optimized the program, discard unreachable functions...
171 addPass(Passes, createGlobalDCEPass());
172 }
173
Reid Spencer8e33fae2006-08-20 19:18:36 +0000174 // Create a new optimization pass for each one specified on the command line
175 std::auto_ptr<TargetMachine> target;
176 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
177 const PassInfo *Opt = OptimizationList[i];
Reid Spencer8e33fae2006-08-20 19:18:36 +0000178 if (Opt->getNormalCtor())
Reid Spencerdcee9d02006-08-20 20:54:38 +0000179 addPass(Passes, Opt->getNormalCtor()());
Reid Spencer8e33fae2006-08-20 19:18:36 +0000180 else if (Opt->getTargetCtor()) {
181 assert(target.get() && "Could not allocate target machine!");
Reid Spencerdcee9d02006-08-20 20:54:38 +0000182 addPass(Passes, Opt->getTargetCtor()(*target.get()));
Reid Spencer8e33fae2006-08-20 19:18:36 +0000183 } else
184 std::cerr << "llvm-ld: cannot create pass: " << Opt->getPassName()
185 << "\n";
186 }
187
188 // The user's passes may leave cruft around. Clean up after them them but
189 // only if we haven't got DisableOptimizations set
190 if (!DisableOptimizations) {
191 addPass(Passes, createInstructionCombiningPass());
192 addPass(Passes, createCFGSimplificationPass());
193 addPass(Passes, createGlobalDCEPass());
Reid Spencerc457fbb2004-11-20 19:43:28 +0000194 }
195
196 // Make sure everything is still good.
197 Passes.add(createVerifierPass());
198
199 // Run our queue of passes all at once now, efficiently.
200 Passes.run(*M);
201}
202
203}