blob: 1961a95ef53f4c1b5252b4814e1f575e609e6c93 [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"
22#include "llvm/Transforms/IPO.h"
23#include "llvm/Transforms/Scalar.h"
24using namespace llvm;
25
26// Optimization Options
27
28enum OptimizationLevels {
29 OPT_FAST_COMPILE = 1,
30 OPT_SIMPLE = 2,
31 OPT_AGGRESSIVE = 3,
32 OPT_LINK_TIME = 4,
33 OPT_AGGRESSIVE_LINK_TIME = 5
34};
35
36static cl::opt<OptimizationLevels> OptLevel(
37 cl::desc("Choose level of optimization to apply:"),
38 cl::init(OPT_FAST_COMPILE), cl::values(
39 clEnumValN(OPT_FAST_COMPILE,"O0",
40 "An alias for the -O1 option."),
41 clEnumValN(OPT_FAST_COMPILE,"O1",
42 "Optimize for linking speed, not execution speed."),
43 clEnumValN(OPT_SIMPLE,"O2",
44 "Perform only required/minimal optimizations"),
45 clEnumValN(OPT_AGGRESSIVE,"O3",
46 "An alias for the -O2 option."),
47 clEnumValN(OPT_LINK_TIME,"O4",
48 "Perform standard link time optimizations"),
49 clEnumValN(OPT_AGGRESSIVE_LINK_TIME,"O5",
50 "Perform aggressive link time optimizations"),
51 clEnumValEnd
52 )
53);
54
Misha Brukman3da94ae2005-04-22 00:00:37 +000055static cl::opt<bool> DisableInline("disable-inlining",
Reid Spencerc457fbb2004-11-20 19:43:28 +000056 cl::desc("Do not run the inliner pass"));
57
58static cl::opt<bool>
59DisableOptimizations("disable-opt",
60 cl::desc("Do not run any optimization passes"));
61
62static cl::opt<bool> DisableInternalize("disable-internalize",
63 cl::desc("Do not mark all symbols as internal"));
64
Misha Brukman3da94ae2005-04-22 00:00:37 +000065static cl::opt<bool> Verify("verify",
Reid Spencerc457fbb2004-11-20 19:43:28 +000066 cl::desc("Verify intermediate results of all passes"));
67
Misha Brukman3da94ae2005-04-22 00:00:37 +000068static cl::opt<bool> Strip("s",
Reid Spencerc457fbb2004-11-20 19:43:28 +000069 cl::desc("Strip symbol info from executable"));
70
Misha Brukman3da94ae2005-04-22 00:00:37 +000071static cl::alias ExportDynamic("export-dynamic",
Reid Spencerc457fbb2004-11-20 19:43:28 +000072 cl::aliasopt(DisableInternalize),
73 cl::desc("Alias for -disable-internalize"));
74
75static cl::list<std::string> LoadableModules("load",
76 cl::value_desc("path to loadable optimization module"),
77 cl::desc("Load an optimization module and run it"));
78
79// A utility function that adds a pass to the pass manager but will also add
80// a verifier pass after if we're supposed to verify.
81static inline void addPass(PassManager &PM, Pass *P) {
82 // Add the pass to the pass manager...
83 PM.add(P);
Misha Brukman3da94ae2005-04-22 00:00:37 +000084
Reid Spencerc457fbb2004-11-20 19:43:28 +000085 // If we are verifying all of the intermediate steps, add the verifier...
Misha Brukman3da94ae2005-04-22 00:00:37 +000086 if (Verify)
Reid Spencerc457fbb2004-11-20 19:43:28 +000087 PM.add(createVerifierPass());
88}
89
90namespace llvm {
91
Misha Brukman3da94ae2005-04-22 00:00:37 +000092/// Optimize - Perform link time optimizations. This will run the scalar
93/// optimizations, any loaded plugin-optimization modules, and then the
Reid Spencerc457fbb2004-11-20 19:43:28 +000094/// inter-procedural optimizations if applicable.
95void Optimize(Module* M) {
96
97 // Instantiate the pass manager to organize the passes.
98 PassManager Passes;
99
100 // If we're verifying, start off with a verification pass.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000101 if (Verify)
Reid Spencerc457fbb2004-11-20 19:43:28 +0000102 Passes.add(createVerifierPass());
103
104 // Add an appropriate TargetData instance for this module...
105 addPass(Passes, new TargetData("gccld", M));
106
107 // Often if the programmer does not specify proper prototypes for the
108 // functions they are calling, they end up calling a vararg version of the
109 // function that does not get a body filled in (the real function has typed
110 // arguments). This pass merges the two functions.
111 addPass(Passes, createFunctionResolvingPass());
112
113 if (!DisableOptimizations) {
114 if (!DisableInternalize) {
115 // Now that composite has been compiled, scan through the module, looking
116 // for a main function. If main is defined, mark all other functions
117 // internal.
118 addPass(Passes, createInternalizePass());
119 }
120
121 // Now that we internalized some globals, see if we can hack on them!
122 addPass(Passes, createGlobalOptimizerPass());
123
124 // Linking modules together can lead to duplicated global constants, only
125 // keep one copy of each constant...
126 addPass(Passes, createConstantMergePass());
127
128 // If the -s command line option was specified, strip the symbols out of the
Chris Lattnerf8f70c12004-12-02 21:27:35 +0000129 // resulting program to make it smaller. -s is a GLD option that we are
Reid Spencerc457fbb2004-11-20 19:43:28 +0000130 // supporting.
131 if (Strip)
Chris Lattnerf8f70c12004-12-02 21:27:35 +0000132 addPass(Passes, createStripSymbolsPass());
Reid Spencerc457fbb2004-11-20 19:43:28 +0000133
134 // Propagate constants at call sites into the functions they call.
135 addPass(Passes, createIPConstantPropagationPass());
136
137 // Remove unused arguments from functions...
138 addPass(Passes, createDeadArgEliminationPass());
139
140 if (!DisableInline)
141 addPass(Passes, createFunctionInliningPass()); // Inline small functions
142
143 addPass(Passes, createPruneEHPass()); // Remove dead EH info
144 addPass(Passes, createGlobalDCEPass()); // Remove dead functions
145
146 // If we didn't decide to inline a function, check to see if we can
147 // transform it to pass arguments by value instead of by reference.
148 addPass(Passes, createArgumentPromotionPass());
149
150 // The IPO passes may leave cruft around. Clean up after them.
151 addPass(Passes, createInstructionCombiningPass());
152
153 addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
154
155 // Run a few AA driven optimizations here and now, to cleanup the code.
156 addPass(Passes, createGlobalsModRefPass()); // IP alias analysis
157
158 addPass(Passes, createLICMPass()); // Hoist loop invariants
159 addPass(Passes, createLoadValueNumberingPass()); // GVN for load instrs
160 addPass(Passes, createGCSEPass()); // Remove common subexprs
161 addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores
162
163 // Cleanup and simplify the code after the scalar optimizations.
164 addPass(Passes, createInstructionCombiningPass());
165
Reid Spencer96690a82004-12-11 03:03:54 +0000166 // Delete basic blocks, which optimization passes may have killed...
167 addPass(Passes, createCFGSimplificationPass());
168
Reid Spencerc457fbb2004-11-20 19:43:28 +0000169 // Now that we have optimized the program, discard unreachable functions...
170 addPass(Passes, createGlobalDCEPass());
171 }
172
173 std::vector<std::string> plugins = LoadableModules;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000174 for (std::vector<std::string>::iterator I = plugins.begin(),
Reid Spencerc457fbb2004-11-20 19:43:28 +0000175 E = plugins.end(); I != E; ++I) {
176 sys::DynamicLibrary dll(I->c_str());
177 typedef void (*OptimizeFunc)(PassManager&,int);
178 OptimizeFunc OF = OptimizeFunc(
179 dll.GetAddressOfSymbol("RunOptimizations"));
180 if (OF == 0) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000181 throw std::string("Optimization Module '") + *I +
Reid Spencerc457fbb2004-11-20 19:43:28 +0000182 "' is missing the RunOptimizations symbol";
183 }
184 (*OF)(Passes,OptLevel);
185 }
186
187 // Make sure everything is still good.
188 Passes.add(createVerifierPass());
189
190 // Run our queue of passes all at once now, efficiently.
191 Passes.run(*M);
192}
193
194}