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