blob: f788f060d15750287a191673255449e7f68b5e38 [file] [log] [blame]
Reid Spencerc457fbb2004-11-20 19:43:28 +00001//===- Optimize.cpp - Optimize a complete program -------------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
Reid Spencerc457fbb2004-11-20 19:43:28 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
Reid Spencerc457fbb2004-11-20 19:43:28 +00008//===----------------------------------------------------------------------===//
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"
Reid Spencerc457fbb2004-11-20 19:43:28 +000016#include "llvm/Analysis/Passes.h"
Devang Patel54959d62007-03-07 04:41:30 +000017#include "llvm/Analysis/LoopPass.h"
Reid Spencerc457fbb2004-11-20 19:43:28 +000018#include "llvm/Analysis/Verifier.h"
19#include "llvm/Support/CommandLine.h"
20#include "llvm/System/DynamicLibrary.h"
21#include "llvm/Target/TargetData.h"
Reid Spencer8e33fae2006-08-20 19:18:36 +000022#include "llvm/Target/TargetMachine.h"
Reid Spencerc457fbb2004-11-20 19:43:28 +000023#include "llvm/Transforms/IPO.h"
24#include "llvm/Transforms/Scalar.h"
Reid Spencer8e33fae2006-08-20 19:18:36 +000025#include "llvm/Support/PassNameParser.h"
26#include "llvm/Support/PluginLoader.h"
Bill Wendlinga089d442006-11-17 10:09:22 +000027#include <iostream>
Reid Spencerc457fbb2004-11-20 19:43:28 +000028using namespace llvm;
29
Reid Spencer8e33fae2006-08-20 19:18:36 +000030// Pass Name Options as generated by the PassNameParser
Chris Lattner30967a52006-08-27 22:07:43 +000031static cl::list<const PassInfo*, bool, PassNameParser>
Reid Spencer8e33fae2006-08-20 19:18:36 +000032 OptimizationList(cl::desc("Optimizations available:"));
Reid Spencerc457fbb2004-11-20 19:43:28 +000033
Andrew Lenharth230293b2008-03-19 22:32:43 +000034//Don't verify at the end
Andrew Lenharth50b57422008-03-19 20:49:51 +000035static cl::opt<bool> DontVerify("disable-verify", cl::ReallyHidden);
36
Misha Brukman3da94ae2005-04-22 00:00:37 +000037static cl::opt<bool> DisableInline("disable-inlining",
Reid Spencerc457fbb2004-11-20 19:43:28 +000038 cl::desc("Do not run the inliner pass"));
39
40static cl::opt<bool>
41DisableOptimizations("disable-opt",
42 cl::desc("Do not run any optimization passes"));
43
44static cl::opt<bool> DisableInternalize("disable-internalize",
45 cl::desc("Do not mark all symbols as internal"));
46
Reid Spencerec9f0502006-08-20 20:48:44 +000047static cl::opt<bool> VerifyEach("verify-each",
48 cl::desc("Verify intermediate results of all passes"));
Reid Spencerc457fbb2004-11-20 19:43:28 +000049
Misha Brukman3da94ae2005-04-22 00:00:37 +000050static cl::alias ExportDynamic("export-dynamic",
Reid Spencerc457fbb2004-11-20 19:43:28 +000051 cl::aliasopt(DisableInternalize),
52 cl::desc("Alias for -disable-internalize"));
53
Reid Spencer7f04c082007-02-08 18:13:59 +000054static cl::opt<bool> Strip("strip-all",
55 cl::desc("Strip all symbol info from executable"));
56
57static cl::alias A0("s", cl::desc("Alias for --strip-all"),
58 cl::aliasopt(Strip));
59
60static cl::opt<bool> StripDebug("strip-debug",
61 cl::desc("Strip debugger symbol info from executable"));
62
63static cl::alias A1("S", cl::desc("Alias for --strip-debug"),
64 cl::aliasopt(StripDebug));
65
Reid Spencerc457fbb2004-11-20 19:43:28 +000066// A utility function that adds a pass to the pass manager but will also add
67// a verifier pass after if we're supposed to verify.
68static inline void addPass(PassManager &PM, Pass *P) {
69 // Add the pass to the pass manager...
70 PM.add(P);
Misha Brukman3da94ae2005-04-22 00:00:37 +000071
Reid Spencerc457fbb2004-11-20 19:43:28 +000072 // If we are verifying all of the intermediate steps, add the verifier...
Reid Spencerec9f0502006-08-20 20:48:44 +000073 if (VerifyEach)
Reid Spencerc457fbb2004-11-20 19:43:28 +000074 PM.add(createVerifierPass());
75}
76
77namespace llvm {
78
Misha Brukman3da94ae2005-04-22 00:00:37 +000079/// Optimize - Perform link time optimizations. This will run the scalar
80/// optimizations, any loaded plugin-optimization modules, and then the
Reid Spencerc457fbb2004-11-20 19:43:28 +000081/// inter-procedural optimizations if applicable.
82void Optimize(Module* M) {
83
84 // Instantiate the pass manager to organize the passes.
85 PassManager Passes;
86
87 // If we're verifying, start off with a verification pass.
Reid Spencerec9f0502006-08-20 20:48:44 +000088 if (VerifyEach)
Reid Spencerc457fbb2004-11-20 19:43:28 +000089 Passes.add(createVerifierPass());
90
91 // Add an appropriate TargetData instance for this module...
Chris Lattner831b1212006-06-16 18:23:49 +000092 addPass(Passes, new TargetData(M));
Reid Spencerc457fbb2004-11-20 19:43:28 +000093
Reid Spencerc457fbb2004-11-20 19:43:28 +000094 if (!DisableOptimizations) {
Chris Lattnerfbcd54f2005-10-18 06:29:43 +000095 // Now that composite has been compiled, scan through the module, looking
96 // for a main function. If main is defined, mark all other functions
97 // internal.
Chris Lattner037a7042007-06-06 20:51:14 +000098 if (!DisableInternalize)
99 addPass(Passes, createInternalizePass(true));
Reid Spencerc457fbb2004-11-20 19:43:28 +0000100
Reid Spencer7f04c082007-02-08 18:13:59 +0000101 // Propagate constants at call sites into the functions they call. This
102 // opens opportunities for globalopt (and inlining) by substituting function
103 // pointers passed as arguments to direct uses of functions.
104 addPass(Passes, createIPSCCPPass());
105
Reid Spencerc457fbb2004-11-20 19:43:28 +0000106 // Now that we internalized some globals, see if we can hack on them!
107 addPass(Passes, createGlobalOptimizerPass());
108
109 // Linking modules together can lead to duplicated global constants, only
110 // keep one copy of each constant...
111 addPass(Passes, createConstantMergePass());
112
Reid Spencerc457fbb2004-11-20 19:43:28 +0000113 // Remove unused arguments from functions...
114 addPass(Passes, createDeadArgEliminationPass());
115
Reid Spencer7f04c082007-02-08 18:13:59 +0000116 // Reduce the code after globalopt and ipsccp. Both can open up significant
117 // simplification opportunities, and both can propagate functions through
118 // function pointers. When this happens, we often have to resolve varargs
119 // calls, etc, so let instcombine do this.
120 addPass(Passes, createInstructionCombiningPass());
121
Reid Spencerc457fbb2004-11-20 19:43:28 +0000122 if (!DisableInline)
123 addPass(Passes, createFunctionInliningPass()); // Inline small functions
124
125 addPass(Passes, createPruneEHPass()); // Remove dead EH info
Reid Spencer7f04c082007-02-08 18:13:59 +0000126 addPass(Passes, createGlobalOptimizerPass()); // Optimize globals again.
Reid Spencerc457fbb2004-11-20 19:43:28 +0000127 addPass(Passes, createGlobalDCEPass()); // Remove dead functions
128
129 // If we didn't decide to inline a function, check to see if we can
130 // transform it to pass arguments by value instead of by reference.
131 addPass(Passes, createArgumentPromotionPass());
132
133 // The IPO passes may leave cruft around. Clean up after them.
134 addPass(Passes, createInstructionCombiningPass());
Chris Lattnere12d8e42008-04-21 04:28:40 +0000135 addPass(Passes, createJumpThreadingPass()); // Thread jumps.
Reid Spencerc457fbb2004-11-20 19:43:28 +0000136 addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
137
Reid Spencer7f04c082007-02-08 18:13:59 +0000138 // Run a few AA driven optimizations here and now, to cleanup the code.
Nick Lewycky677c2c22009-02-26 06:56:16 +0000139 addPass(Passes, createFunctionAttrsPass()); // Add nocapture
Reid Spencerc457fbb2004-11-20 19:43:28 +0000140 addPass(Passes, createGlobalsModRefPass()); // IP alias analysis
Reid Spencer7f04c082007-02-08 18:13:59 +0000141
Reid Spencerc457fbb2004-11-20 19:43:28 +0000142 addPass(Passes, createLICMPass()); // Hoist loop invariants
Nick Lewycky677c2c22009-02-26 06:56:16 +0000143 addPass(Passes, createGVNPass()); // Remove redundancies
Owen Andersond2368dc2008-04-22 07:12:26 +0000144 addPass(Passes, createMemCpyOptPass()); // Remove dead memcpy's
Owen Andersonf6a05f92007-08-01 06:36:51 +0000145 addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores
Reid Spencerc457fbb2004-11-20 19:43:28 +0000146
147 // Cleanup and simplify the code after the scalar optimizations.
148 addPass(Passes, createInstructionCombiningPass());
149
Chris Lattnere12d8e42008-04-21 04:28:40 +0000150 addPass(Passes, createJumpThreadingPass()); // Thread jumps.
Chris Lattner6c31e872008-06-25 16:51:55 +0000151 addPass(Passes, createPromoteMemoryToRegisterPass()); // Cleanup jumpthread.
152
Reid Spencer96690a82004-12-11 03:03:54 +0000153 // Delete basic blocks, which optimization passes may have killed...
154 addPass(Passes, createCFGSimplificationPass());
155
Reid Spencerc457fbb2004-11-20 19:43:28 +0000156 // Now that we have optimized the program, discard unreachable functions...
157 addPass(Passes, createGlobalDCEPass());
158 }
159
Reid Spencer7f04c082007-02-08 18:13:59 +0000160 // If the -s or -S command line options were specified, strip the symbols out
161 // of the resulting program to make it smaller. -s and -S are GNU ld options
162 // that we are supporting; they alias -strip-all and -strip-debug.
163 if (Strip || StripDebug)
164 addPass(Passes, createStripSymbolsPass(StripDebug && !Strip));
165
Reid Spencer8e33fae2006-08-20 19:18:36 +0000166 // Create a new optimization pass for each one specified on the command line
167 std::auto_ptr<TargetMachine> target;
168 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
169 const PassInfo *Opt = OptimizationList[i];
Reid Spencer8e33fae2006-08-20 19:18:36 +0000170 if (Opt->getNormalCtor())
Reid Spencerdcee9d02006-08-20 20:54:38 +0000171 addPass(Passes, Opt->getNormalCtor()());
Chris Lattnercd950a52006-12-01 21:59:37 +0000172 else
Reid Spencer8e33fae2006-08-20 19:18:36 +0000173 std::cerr << "llvm-ld: cannot create pass: " << Opt->getPassName()
174 << "\n";
175 }
176
177 // The user's passes may leave cruft around. Clean up after them them but
178 // only if we haven't got DisableOptimizations set
179 if (!DisableOptimizations) {
180 addPass(Passes, createInstructionCombiningPass());
181 addPass(Passes, createCFGSimplificationPass());
Owen Anderson735e6ea2008-07-02 18:42:07 +0000182 addPass(Passes, createAggressiveDCEPass());
Reid Spencer8e33fae2006-08-20 19:18:36 +0000183 addPass(Passes, createGlobalDCEPass());
Reid Spencerc457fbb2004-11-20 19:43:28 +0000184 }
185
186 // Make sure everything is still good.
Andrew Lenharth230293b2008-03-19 22:32:43 +0000187 if (!DontVerify)
Andrew Lenharth50b57422008-03-19 20:49:51 +0000188 Passes.add(createVerifierPass());
Reid Spencerc457fbb2004-11-20 19:43:28 +0000189
190 // Run our queue of passes all at once now, efficiently.
191 Passes.run(*M);
192}
193
194}