Fix PR4948 (and a leak): by not destroying the DwarfException
[llvm/avr.git] / tools / llvm-ld / Optimize.cpp
blob6143dc87d356a2d10b3efe2100a6e6d6a4346322
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.
12 //===----------------------------------------------------------------------===//
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 using namespace llvm;
31 // Pass Name Options as generated by the PassNameParser
32 static cl::list<const PassInfo*, bool, PassNameParser>
33 OptimizationList(cl::desc("Optimizations available:"));
35 //Don't verify at the end
36 static cl::opt<bool> DontVerify("disable-verify", cl::ReallyHidden);
38 static cl::opt<bool> DisableInline("disable-inlining",
39 cl::desc("Do not run the inliner pass"));
41 static cl::opt<bool>
42 DisableOptimizations("disable-opt",
43 cl::desc("Do not run any optimization passes"));
45 static cl::opt<bool> DisableInternalize("disable-internalize",
46 cl::desc("Do not mark all symbols as internal"));
48 static cl::opt<bool> VerifyEach("verify-each",
49 cl::desc("Verify intermediate results of all passes"));
51 static cl::alias ExportDynamic("export-dynamic",
52 cl::aliasopt(DisableInternalize),
53 cl::desc("Alias for -disable-internalize"));
55 static cl::opt<bool> Strip("strip-all",
56 cl::desc("Strip all symbol info from executable"));
58 static cl::alias A0("s", cl::desc("Alias for --strip-all"),
59 cl::aliasopt(Strip));
61 static cl::opt<bool> StripDebug("strip-debug",
62 cl::desc("Strip debugger symbol info from executable"));
64 static cl::alias A1("S", cl::desc("Alias for --strip-debug"),
65 cl::aliasopt(StripDebug));
67 // A utility function that adds a pass to the pass manager but will also add
68 // a verifier pass after if we're supposed to verify.
69 static inline void addPass(PassManager &PM, Pass *P) {
70 // Add the pass to the pass manager...
71 PM.add(P);
73 // If we are verifying all of the intermediate steps, add the verifier...
74 if (VerifyEach)
75 PM.add(createVerifierPass());
78 namespace llvm {
80 /// Optimize - Perform link time optimizations. This will run the scalar
81 /// optimizations, any loaded plugin-optimization modules, and then the
82 /// inter-procedural optimizations if applicable.
83 void Optimize(Module* M) {
85 // Instantiate the pass manager to organize the passes.
86 PassManager Passes;
88 // If we're verifying, start off with a verification pass.
89 if (VerifyEach)
90 Passes.add(createVerifierPass());
92 // Add an appropriate TargetData instance for this module...
93 addPass(Passes, new TargetData(M));
95 if (!DisableOptimizations)
96 createStandardLTOPasses(&Passes, !DisableInternalize, !DisableInline,
97 VerifyEach);
99 // If the -s or -S command line options were specified, strip the symbols out
100 // of the resulting program to make it smaller. -s and -S are GNU ld options
101 // that we are supporting; they alias -strip-all and -strip-debug.
102 if (Strip || StripDebug)
103 addPass(Passes, createStripSymbolsPass(StripDebug && !Strip));
105 // Create a new optimization pass for each one specified on the command line
106 std::auto_ptr<TargetMachine> target;
107 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
108 const PassInfo *Opt = OptimizationList[i];
109 if (Opt->getNormalCtor())
110 addPass(Passes, Opt->getNormalCtor()());
111 else
112 errs() << "llvm-ld: cannot create pass: " << Opt->getPassName()
113 << "\n";
116 // The user's passes may leave cruft around. Clean up after them them but
117 // only if we haven't got DisableOptimizations set
118 if (!DisableOptimizations) {
119 addPass(Passes, createInstructionCombiningPass());
120 addPass(Passes, createCFGSimplificationPass());
121 addPass(Passes, createAggressiveDCEPass());
122 addPass(Passes, createGlobalDCEPass());
125 // Make sure everything is still good.
126 if (!DontVerify)
127 Passes.add(createVerifierPass());
129 // Run our queue of passes all at once now, efficiently.
130 Passes.run(*M);