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