blob: a05ab0abcf7f78d729ee572e0bb6fc6ae39552d4 [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"
Reid Spencerc457fbb2004-11-20 19:43:28 +000015#include "llvm/Support/CommandLine.h"
Dan Gohman65f57c22009-07-15 16:35:29 +000016#include "llvm/Support/raw_ostream.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000017#include "llvm/Support/DynamicLibrary.h"
Reid Spencerc457fbb2004-11-20 19:43:28 +000018#include "llvm/Target/TargetData.h"
Reid Spencer8e33fae2006-08-20 19:18:36 +000019#include "llvm/Target/TargetMachine.h"
Reid Spencer8e33fae2006-08-20 19:18:36 +000020#include "llvm/Support/PassNameParser.h"
21#include "llvm/Support/PluginLoader.h"
Rafael Espindola3d453ac2011-08-02 21:50:24 +000022#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Reid Spencerc457fbb2004-11-20 19:43:28 +000023using namespace llvm;
24
Reid Spencer8e33fae2006-08-20 19:18:36 +000025// Pass Name Options as generated by the PassNameParser
Chris Lattner30967a52006-08-27 22:07:43 +000026static cl::list<const PassInfo*, bool, PassNameParser>
Reid Spencer8e33fae2006-08-20 19:18:36 +000027 OptimizationList(cl::desc("Optimizations available:"));
Reid Spencerc457fbb2004-11-20 19:43:28 +000028
Andrew Lenharth230293b2008-03-19 22:32:43 +000029//Don't verify at the end
Andrew Lenharth50b57422008-03-19 20:49:51 +000030static cl::opt<bool> DontVerify("disable-verify", cl::ReallyHidden);
31
Misha Brukman3da94ae2005-04-22 00:00:37 +000032static cl::opt<bool> DisableInline("disable-inlining",
Reid Spencerc457fbb2004-11-20 19:43:28 +000033 cl::desc("Do not run the inliner pass"));
34
35static cl::opt<bool>
36DisableOptimizations("disable-opt",
37 cl::desc("Do not run any optimization passes"));
38
39static cl::opt<bool> DisableInternalize("disable-internalize",
40 cl::desc("Do not mark all symbols as internal"));
41
Reid Spencerec9f0502006-08-20 20:48:44 +000042static cl::opt<bool> VerifyEach("verify-each",
43 cl::desc("Verify intermediate results of all passes"));
Reid Spencerc457fbb2004-11-20 19:43:28 +000044
Misha Brukman3da94ae2005-04-22 00:00:37 +000045static cl::alias ExportDynamic("export-dynamic",
Reid Spencerc457fbb2004-11-20 19:43:28 +000046 cl::aliasopt(DisableInternalize),
47 cl::desc("Alias for -disable-internalize"));
48
Reid Spencer7f04c082007-02-08 18:13:59 +000049static cl::opt<bool> Strip("strip-all",
50 cl::desc("Strip all symbol info from executable"));
51
52static cl::alias A0("s", cl::desc("Alias for --strip-all"),
53 cl::aliasopt(Strip));
54
55static cl::opt<bool> StripDebug("strip-debug",
56 cl::desc("Strip debugger symbol info from executable"));
57
58static cl::alias A1("S", cl::desc("Alias for --strip-debug"),
59 cl::aliasopt(StripDebug));
60
Reid Spencerc457fbb2004-11-20 19:43:28 +000061// A utility function that adds a pass to the pass manager but will also add
62// a verifier pass after if we're supposed to verify.
63static inline void addPass(PassManager &PM, Pass *P) {
64 // Add the pass to the pass manager...
65 PM.add(P);
Misha Brukman3da94ae2005-04-22 00:00:37 +000066
Reid Spencerc457fbb2004-11-20 19:43:28 +000067 // If we are verifying all of the intermediate steps, add the verifier...
Reid Spencerec9f0502006-08-20 20:48:44 +000068 if (VerifyEach)
Reid Spencerc457fbb2004-11-20 19:43:28 +000069 PM.add(createVerifierPass());
70}
71
72namespace llvm {
Misha Brukman3da94ae2005-04-22 00:00:37 +000073/// Optimize - Perform link time optimizations. This will run the scalar
74/// optimizations, any loaded plugin-optimization modules, and then the
Reid Spencerc457fbb2004-11-20 19:43:28 +000075/// inter-procedural optimizations if applicable.
Chris Lattner9d70b6b2011-05-22 00:21:15 +000076void Optimize(Module *M) {
Reid Spencerc457fbb2004-11-20 19:43:28 +000077
78 // Instantiate the pass manager to organize the passes.
79 PassManager Passes;
80
81 // If we're verifying, start off with a verification pass.
Reid Spencerec9f0502006-08-20 20:48:44 +000082 if (VerifyEach)
Reid Spencerc457fbb2004-11-20 19:43:28 +000083 Passes.add(createVerifierPass());
84
85 // Add an appropriate TargetData instance for this module...
Chris Lattner831b1212006-06-16 18:23:49 +000086 addPass(Passes, new TargetData(M));
Reid Spencerc457fbb2004-11-20 19:43:28 +000087
Daniel Dunbar006a0342009-06-03 21:06:14 +000088 if (!DisableOptimizations)
Chris Lattner9d70b6b2011-05-22 00:21:15 +000089 PassManagerBuilder().populateLTOPassManager(Passes, !DisableInternalize,
90 !DisableInline);
Reid Spencerc457fbb2004-11-20 19:43:28 +000091
Reid Spencer7f04c082007-02-08 18:13:59 +000092 // If the -s or -S command line options were specified, strip the symbols out
93 // of the resulting program to make it smaller. -s and -S are GNU ld options
94 // that we are supporting; they alias -strip-all and -strip-debug.
95 if (Strip || StripDebug)
96 addPass(Passes, createStripSymbolsPass(StripDebug && !Strip));
97
Reid Spencer8e33fae2006-08-20 19:18:36 +000098 // Create a new optimization pass for each one specified on the command line
99 std::auto_ptr<TargetMachine> target;
100 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
101 const PassInfo *Opt = OptimizationList[i];
Reid Spencer8e33fae2006-08-20 19:18:36 +0000102 if (Opt->getNormalCtor())
Reid Spencerdcee9d02006-08-20 20:54:38 +0000103 addPass(Passes, Opt->getNormalCtor()());
Chris Lattnercd950a52006-12-01 21:59:37 +0000104 else
Dan Gohman65f57c22009-07-15 16:35:29 +0000105 errs() << "llvm-ld: cannot create pass: " << Opt->getPassName()
106 << "\n";
Reid Spencer8e33fae2006-08-20 19:18:36 +0000107 }
108
109 // The user's passes may leave cruft around. Clean up after them them but
110 // only if we haven't got DisableOptimizations set
111 if (!DisableOptimizations) {
112 addPass(Passes, createInstructionCombiningPass());
113 addPass(Passes, createCFGSimplificationPass());
Owen Anderson735e6ea2008-07-02 18:42:07 +0000114 addPass(Passes, createAggressiveDCEPass());
Reid Spencer8e33fae2006-08-20 19:18:36 +0000115 addPass(Passes, createGlobalDCEPass());
Reid Spencerc457fbb2004-11-20 19:43:28 +0000116 }
117
118 // Make sure everything is still good.
Andrew Lenharth230293b2008-03-19 22:32:43 +0000119 if (!DontVerify)
Andrew Lenharth50b57422008-03-19 20:49:51 +0000120 Passes.add(createVerifierPass());
Reid Spencerc457fbb2004-11-20 19:43:28 +0000121
122 // Run our queue of passes all at once now, efficiently.
123 Passes.run(*M);
124}
125
126}