blob: b61e8ef7db4e3d094bd91d4681ebeff735a12769 [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",
Reid Spencer2991b012006-06-07 23:07:51 +000076 cl::value_desc("path"),
Reid Spencerc457fbb2004-11-20 19:43:28 +000077 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...
Chris Lattner831b1212006-06-16 18:23:49 +0000105 addPass(Passes, new TargetData(M));
Reid Spencerc457fbb2004-11-20 19:43:28 +0000106
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) {
Chris Lattnerfbcd54f2005-10-18 06:29:43 +0000114 // Now that composite has been compiled, scan through the module, looking
115 // for a main function. If main is defined, mark all other functions
116 // internal.
117 addPass(Passes, createInternalizePass(!DisableInternalize));
Reid Spencerc457fbb2004-11-20 19:43:28 +0000118
119 // Now that we internalized some globals, see if we can hack on them!
120 addPass(Passes, createGlobalOptimizerPass());
121
122 // Linking modules together can lead to duplicated global constants, only
123 // keep one copy of each constant...
124 addPass(Passes, createConstantMergePass());
125
126 // If the -s command line option was specified, strip the symbols out of the
Chris Lattnerf8f70c12004-12-02 21:27:35 +0000127 // resulting program to make it smaller. -s is a GLD option that we are
Reid Spencerc457fbb2004-11-20 19:43:28 +0000128 // supporting.
129 if (Strip)
Chris Lattnerf8f70c12004-12-02 21:27:35 +0000130 addPass(Passes, createStripSymbolsPass());
Reid Spencerc457fbb2004-11-20 19:43:28 +0000131
132 // Propagate constants at call sites into the functions they call.
133 addPass(Passes, createIPConstantPropagationPass());
134
135 // Remove unused arguments from functions...
136 addPass(Passes, createDeadArgEliminationPass());
137
138 if (!DisableInline)
139 addPass(Passes, createFunctionInliningPass()); // Inline small functions
140
141 addPass(Passes, createPruneEHPass()); // Remove dead EH info
142 addPass(Passes, createGlobalDCEPass()); // Remove dead functions
143
144 // If we didn't decide to inline a function, check to see if we can
145 // transform it to pass arguments by value instead of by reference.
146 addPass(Passes, createArgumentPromotionPass());
147
148 // The IPO passes may leave cruft around. Clean up after them.
149 addPass(Passes, createInstructionCombiningPass());
150
151 addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
152
153 // Run a few AA driven optimizations here and now, to cleanup the code.
154 addPass(Passes, createGlobalsModRefPass()); // IP alias analysis
155
156 addPass(Passes, createLICMPass()); // Hoist loop invariants
157 addPass(Passes, createLoadValueNumberingPass()); // GVN for load instrs
158 addPass(Passes, createGCSEPass()); // Remove common subexprs
159 addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores
160
161 // Cleanup and simplify the code after the scalar optimizations.
162 addPass(Passes, createInstructionCombiningPass());
163
Reid Spencer96690a82004-12-11 03:03:54 +0000164 // Delete basic blocks, which optimization passes may have killed...
165 addPass(Passes, createCFGSimplificationPass());
166
Reid Spencerc457fbb2004-11-20 19:43:28 +0000167 // Now that we have optimized the program, discard unreachable functions...
168 addPass(Passes, createGlobalDCEPass());
169 }
170
171 std::vector<std::string> plugins = LoadableModules;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000172 for (std::vector<std::string>::iterator I = plugins.begin(),
Reid Spencerc457fbb2004-11-20 19:43:28 +0000173 E = plugins.end(); I != E; ++I) {
174 sys::DynamicLibrary dll(I->c_str());
175 typedef void (*OptimizeFunc)(PassManager&,int);
176 OptimizeFunc OF = OptimizeFunc(
Chris Lattner7c5357f2006-06-02 22:11:06 +0000177 (intptr_t)dll.GetAddressOfSymbol("RunOptimizations"));
Reid Spencerc457fbb2004-11-20 19:43:28 +0000178 if (OF == 0) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000179 throw std::string("Optimization Module '") + *I +
Reid Spencerc457fbb2004-11-20 19:43:28 +0000180 "' is missing the RunOptimizations symbol";
181 }
182 (*OF)(Passes,OptLevel);
183 }
184
185 // Make sure everything is still good.
186 Passes.add(createVerifierPass());
187
188 // Run our queue of passes all at once now, efficiently.
189 Passes.run(*M);
190}
191
192}