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