fix a file system case sensitivity issue.
[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 is distributed under the University of Illinois Open Source
6 // 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/Support/CommandLine.h"
16 #include "llvm/Support/PassManagerBuilder.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/Support/DynamicLibrary.h"
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/Support/PassNameParser.h"
22 #include "llvm/Support/PluginLoader.h"
23 using namespace llvm;
24
25 // Pass Name Options as generated by the PassNameParser
26 static cl::list<const PassInfo*, bool, PassNameParser>
27   OptimizationList(cl::desc("Optimizations available:"));
28
29 //Don't verify at the end
30 static cl::opt<bool> DontVerify("disable-verify", cl::ReallyHidden);
31
32 static cl::opt<bool> DisableInline("disable-inlining",
33   cl::desc("Do not run the inliner pass"));
34
35 static cl::opt<bool>
36 DisableOptimizations("disable-opt",
37   cl::desc("Do not run any optimization passes"));
38
39 static cl::opt<bool> DisableInternalize("disable-internalize",
40   cl::desc("Do not mark all symbols as internal"));
41
42 static cl::opt<bool> VerifyEach("verify-each",
43  cl::desc("Verify intermediate results of all passes"));
44
45 static cl::alias ExportDynamic("export-dynamic",
46   cl::aliasopt(DisableInternalize),
47   cl::desc("Alias for -disable-internalize"));
48
49 static cl::opt<bool> Strip("strip-all", 
50   cl::desc("Strip all symbol info from executable"));
51
52 static cl::alias A0("s", cl::desc("Alias for --strip-all"), 
53   cl::aliasopt(Strip));
54
55 static cl::opt<bool> StripDebug("strip-debug",
56   cl::desc("Strip debugger symbol info from executable"));
57
58 static cl::alias A1("S", cl::desc("Alias for --strip-debug"),
59   cl::aliasopt(StripDebug));
60
61 // A utility function that adds a pass to the pass manager but will also add
62 // a verifier pass after if we're supposed to verify.
63 static inline void addPass(PassManager &PM, Pass *P) {
64   // Add the pass to the pass manager...
65   PM.add(P);
66
67   // If we are verifying all of the intermediate steps, add the verifier...
68   if (VerifyEach)
69     PM.add(createVerifierPass());
70 }
71
72 namespace llvm {
73 /// Optimize - Perform link time optimizations. This will run the scalar
74 /// optimizations, any loaded plugin-optimization modules, and then the
75 /// inter-procedural optimizations if applicable.
76 void Optimize(Module *M) {
77
78   // Instantiate the pass manager to organize the passes.
79   PassManager Passes;
80
81   // If we're verifying, start off with a verification pass.
82   if (VerifyEach)
83     Passes.add(createVerifierPass());
84
85   // Add an appropriate TargetData instance for this module...
86   addPass(Passes, new TargetData(M));
87
88   if (!DisableOptimizations)
89     PassManagerBuilder().populateLTOPassManager(Passes, !DisableInternalize,
90                                                 !DisableInline);
91
92   // If the -s or -S command line options were specified, strip the symbols out
93   // of the resulting program to make it smaller.  -s and -S are GNU ld options
94   // that we are supporting; they alias -strip-all and -strip-debug.
95   if (Strip || StripDebug)
96     addPass(Passes, createStripSymbolsPass(StripDebug && !Strip));
97
98   // Create a new optimization pass for each one specified on the command line
99   std::auto_ptr<TargetMachine> target;
100   for (unsigned i = 0; i < OptimizationList.size(); ++i) {
101     const PassInfo *Opt = OptimizationList[i];
102     if (Opt->getNormalCtor())
103       addPass(Passes, Opt->getNormalCtor()());
104     else
105       errs() << "llvm-ld: cannot create pass: " << Opt->getPassName() 
106              << "\n";
107   }
108
109   // The user's passes may leave cruft around. Clean up after them them but
110   // only if we haven't got DisableOptimizations set
111   if (!DisableOptimizations) {
112     addPass(Passes, createInstructionCombiningPass());
113     addPass(Passes, createCFGSimplificationPass());
114     addPass(Passes, createAggressiveDCEPass());
115     addPass(Passes, createGlobalDCEPass());
116   }
117
118   // Make sure everything is still good.
119   if (!DontVerify)
120     Passes.add(createVerifierPass());
121
122   // Run our queue of passes all at once now, efficiently.
123   Passes.run(*M);
124 }
125
126 }