Don't pass target name into TargetData anymore, it is never used or needed.
[oota-llvm.git] / tools / llvm-ld / Optimize.cpp
1 //===- Optimize.cpp - Optimize a complete program -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
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"
24 using namespace llvm;
25
26 // Optimization Options
27
28 enum 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
36 static 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
55 static cl::opt<bool> DisableInline("disable-inlining",
56   cl::desc("Do not run the inliner pass"));
57
58 static cl::opt<bool>
59 DisableOptimizations("disable-opt",
60   cl::desc("Do not run any optimization passes"));
61
62 static cl::opt<bool> DisableInternalize("disable-internalize",
63   cl::desc("Do not mark all symbols as internal"));
64
65 static cl::opt<bool> Verify("verify",
66   cl::desc("Verify intermediate results of all passes"));
67
68 static cl::opt<bool> Strip("s",
69   cl::desc("Strip symbol info from executable"));
70
71 static cl::alias ExportDynamic("export-dynamic",
72   cl::aliasopt(DisableInternalize),
73   cl::desc("Alias for -disable-internalize"));
74
75 static cl::list<std::string> LoadableModules("load",
76   cl::value_desc("path"),
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.
81 static inline void addPass(PassManager &PM, Pass *P) {
82   // Add the pass to the pass manager...
83   PM.add(P);
84
85   // If we are verifying all of the intermediate steps, add the verifier...
86   if (Verify)
87     PM.add(createVerifierPass());
88 }
89
90 namespace llvm {
91
92 /// Optimize - Perform link time optimizations. This will run the scalar
93 /// optimizations, any loaded plugin-optimization modules, and then the
94 /// inter-procedural optimizations if applicable.
95 void 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.
101   if (Verify)
102     Passes.add(createVerifierPass());
103
104   // Add an appropriate TargetData instance for this module...
105   addPass(Passes, new TargetData(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     // 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));
118
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
127     // resulting program to make it smaller.  -s is a GLD option that we are
128     // supporting.
129     if (Strip)
130       addPass(Passes, createStripSymbolsPass());
131
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
164     // Delete basic blocks, which optimization passes may have killed...
165     addPass(Passes, createCFGSimplificationPass());
166
167     // Now that we have optimized the program, discard unreachable functions...
168     addPass(Passes, createGlobalDCEPass());
169   }
170
171   std::vector<std::string> plugins = LoadableModules;
172   for (std::vector<std::string>::iterator I = plugins.begin(),
173       E = plugins.end(); I != E; ++I) {
174     sys::DynamicLibrary dll(I->c_str());
175     typedef void (*OptimizeFunc)(PassManager&,int);
176     OptimizeFunc OF = OptimizeFunc(
177         (intptr_t)dll.GetAddressOfSymbol("RunOptimizations"));
178     if (OF == 0) {
179       throw std::string("Optimization Module '") + *I +
180         "' 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 }