blob: 7f3f90014fc28b092e9f6eb0fba8e3d7c45328f4 [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"
Rafael Espindolac684e832011-08-02 21:50:27 +000015#include "llvm/PassManager.h"
16#include "llvm/Analysis/Verifier.h"
Reid Spencerc457fbb2004-11-20 19:43:28 +000017#include "llvm/Support/CommandLine.h"
Dan Gohman65f57c22009-07-15 16:35:29 +000018#include "llvm/Support/raw_ostream.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000019#include "llvm/Support/DynamicLibrary.h"
Reid Spencerc457fbb2004-11-20 19:43:28 +000020#include "llvm/Target/TargetData.h"
Reid Spencer8e33fae2006-08-20 19:18:36 +000021#include "llvm/Target/TargetMachine.h"
Reid Spencer8e33fae2006-08-20 19:18:36 +000022#include "llvm/Support/PassNameParser.h"
23#include "llvm/Support/PluginLoader.h"
Rafael Espindolac684e832011-08-02 21:50:27 +000024#include "llvm/Transforms/IPO.h"
Rafael Espindola3d453ac2011-08-02 21:50:24 +000025#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Rafael Espindolac684e832011-08-02 21:50:27 +000026#include "llvm/Transforms/Scalar.h"
Reid Spencerc457fbb2004-11-20 19:43:28 +000027using namespace llvm;
28
Reid Spencer8e33fae2006-08-20 19:18:36 +000029// Pass Name Options as generated by the PassNameParser
Chris Lattner30967a52006-08-27 22:07:43 +000030static cl::list<const PassInfo*, bool, PassNameParser>
Reid Spencer8e33fae2006-08-20 19:18:36 +000031 OptimizationList(cl::desc("Optimizations available:"));
Reid Spencerc457fbb2004-11-20 19:43:28 +000032
Andrew Lenharth230293b2008-03-19 22:32:43 +000033//Don't verify at the end
Andrew Lenharth50b57422008-03-19 20:49:51 +000034static cl::opt<bool> DontVerify("disable-verify", cl::ReallyHidden);
35
Misha Brukman3da94ae2005-04-22 00:00:37 +000036static cl::opt<bool> DisableInline("disable-inlining",
Reid Spencerc457fbb2004-11-20 19:43:28 +000037 cl::desc("Do not run the inliner pass"));
38
39static cl::opt<bool>
40DisableOptimizations("disable-opt",
41 cl::desc("Do not run any optimization passes"));
42
43static cl::opt<bool> DisableInternalize("disable-internalize",
44 cl::desc("Do not mark all symbols as internal"));
45
Reid Spencerec9f0502006-08-20 20:48:44 +000046static cl::opt<bool> VerifyEach("verify-each",
47 cl::desc("Verify intermediate results of all passes"));
Reid Spencerc457fbb2004-11-20 19:43:28 +000048
Misha Brukman3da94ae2005-04-22 00:00:37 +000049static cl::alias ExportDynamic("export-dynamic",
Reid Spencerc457fbb2004-11-20 19:43:28 +000050 cl::aliasopt(DisableInternalize),
51 cl::desc("Alias for -disable-internalize"));
52
Reid Spencer7f04c082007-02-08 18:13:59 +000053static cl::opt<bool> Strip("strip-all",
54 cl::desc("Strip all symbol info from executable"));
55
56static cl::alias A0("s", cl::desc("Alias for --strip-all"),
57 cl::aliasopt(Strip));
58
59static cl::opt<bool> StripDebug("strip-debug",
60 cl::desc("Strip debugger symbol info from executable"));
61
62static cl::alias A1("S", cl::desc("Alias for --strip-debug"),
63 cl::aliasopt(StripDebug));
64
Reid Spencerc457fbb2004-11-20 19:43:28 +000065// 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.
67static inline void addPass(PassManager &PM, Pass *P) {
68 // Add the pass to the pass manager...
69 PM.add(P);
Misha Brukman3da94ae2005-04-22 00:00:37 +000070
Reid Spencerc457fbb2004-11-20 19:43:28 +000071 // If we are verifying all of the intermediate steps, add the verifier...
Reid Spencerec9f0502006-08-20 20:48:44 +000072 if (VerifyEach)
Reid Spencerc457fbb2004-11-20 19:43:28 +000073 PM.add(createVerifierPass());
74}
75
76namespace llvm {
Misha Brukman3da94ae2005-04-22 00:00:37 +000077/// Optimize - Perform link time optimizations. This will run the scalar
78/// optimizations, any loaded plugin-optimization modules, and then the
Reid Spencerc457fbb2004-11-20 19:43:28 +000079/// inter-procedural optimizations if applicable.
Chris Lattner9d70b6b2011-05-22 00:21:15 +000080void Optimize(Module *M) {
Reid Spencerc457fbb2004-11-20 19:43:28 +000081
82 // Instantiate the pass manager to organize the passes.
83 PassManager Passes;
84
85 // If we're verifying, start off with a verification pass.
Reid Spencerec9f0502006-08-20 20:48:44 +000086 if (VerifyEach)
Reid Spencerc457fbb2004-11-20 19:43:28 +000087 Passes.add(createVerifierPass());
88
89 // Add an appropriate TargetData instance for this module...
Chris Lattner831b1212006-06-16 18:23:49 +000090 addPass(Passes, new TargetData(M));
Reid Spencerc457fbb2004-11-20 19:43:28 +000091
Daniel Dunbar006a0342009-06-03 21:06:14 +000092 if (!DisableOptimizations)
Chris Lattner9d70b6b2011-05-22 00:21:15 +000093 PassManagerBuilder().populateLTOPassManager(Passes, !DisableInternalize,
94 !DisableInline);
Reid Spencerc457fbb2004-11-20 19:43:28 +000095
Reid Spencer7f04c082007-02-08 18:13:59 +000096 // 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
Reid Spencer8e33fae2006-08-20 19:18:36 +0000102 // 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];
Reid Spencer8e33fae2006-08-20 19:18:36 +0000106 if (Opt->getNormalCtor())
Reid Spencerdcee9d02006-08-20 20:54:38 +0000107 addPass(Passes, Opt->getNormalCtor()());
Chris Lattnercd950a52006-12-01 21:59:37 +0000108 else
Dan Gohman65f57c22009-07-15 16:35:29 +0000109 errs() << "llvm-ld: cannot create pass: " << Opt->getPassName()
110 << "\n";
Reid Spencer8e33fae2006-08-20 19:18:36 +0000111 }
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());
Owen Anderson735e6ea2008-07-02 18:42:07 +0000118 addPass(Passes, createAggressiveDCEPass());
Reid Spencer8e33fae2006-08-20 19:18:36 +0000119 addPass(Passes, createGlobalDCEPass());
Reid Spencerc457fbb2004-11-20 19:43:28 +0000120 }
121
122 // Make sure everything is still good.
Andrew Lenharth230293b2008-03-19 22:32:43 +0000123 if (!DontVerify)
Andrew Lenharth50b57422008-03-19 20:49:51 +0000124 Passes.add(createVerifierPass());
Reid Spencerc457fbb2004-11-20 19:43:28 +0000125
126 // Run our queue of passes all at once now, efficiently.
127 Passes.run(*M);
128}
129
130}