blob: 7001064659cd8d454bbaa17e3a0db5b5e9419b9e [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"
Daniel Dunbar006a0342009-06-03 21:06:14 +000020#include "llvm/Support/StandardPasses.h"
Dan Gohman65f57c22009-07-15 16:35:29 +000021#include "llvm/Support/raw_ostream.h"
Reid Spencerc457fbb2004-11-20 19:43:28 +000022#include "llvm/System/DynamicLibrary.h"
23#include "llvm/Target/TargetData.h"
Reid Spencer8e33fae2006-08-20 19:18:36 +000024#include "llvm/Target/TargetMachine.h"
Reid Spencerc457fbb2004-11-20 19:43:28 +000025#include "llvm/Transforms/IPO.h"
26#include "llvm/Transforms/Scalar.h"
Reid Spencer8e33fae2006-08-20 19:18:36 +000027#include "llvm/Support/PassNameParser.h"
28#include "llvm/Support/PluginLoader.h"
Bill Wendlinga089d442006-11-17 10:09:22 +000029#include <iostream>
Reid Spencerc457fbb2004-11-20 19:43:28 +000030using namespace llvm;
31
Reid Spencer8e33fae2006-08-20 19:18:36 +000032// Pass Name Options as generated by the PassNameParser
Chris Lattner30967a52006-08-27 22:07:43 +000033static cl::list<const PassInfo*, bool, PassNameParser>
Reid Spencer8e33fae2006-08-20 19:18:36 +000034 OptimizationList(cl::desc("Optimizations available:"));
Reid Spencerc457fbb2004-11-20 19:43:28 +000035
Andrew Lenharth230293b2008-03-19 22:32:43 +000036//Don't verify at the end
Andrew Lenharth50b57422008-03-19 20:49:51 +000037static cl::opt<bool> DontVerify("disable-verify", cl::ReallyHidden);
38
Misha Brukman3da94ae2005-04-22 00:00:37 +000039static cl::opt<bool> DisableInline("disable-inlining",
Reid Spencerc457fbb2004-11-20 19:43:28 +000040 cl::desc("Do not run the inliner pass"));
41
42static cl::opt<bool>
43DisableOptimizations("disable-opt",
44 cl::desc("Do not run any optimization passes"));
45
46static cl::opt<bool> DisableInternalize("disable-internalize",
47 cl::desc("Do not mark all symbols as internal"));
48
Reid Spencerec9f0502006-08-20 20:48:44 +000049static cl::opt<bool> VerifyEach("verify-each",
50 cl::desc("Verify intermediate results of all passes"));
Reid Spencerc457fbb2004-11-20 19:43:28 +000051
Misha Brukman3da94ae2005-04-22 00:00:37 +000052static cl::alias ExportDynamic("export-dynamic",
Reid Spencerc457fbb2004-11-20 19:43:28 +000053 cl::aliasopt(DisableInternalize),
54 cl::desc("Alias for -disable-internalize"));
55
Reid Spencer7f04c082007-02-08 18:13:59 +000056static cl::opt<bool> Strip("strip-all",
57 cl::desc("Strip all symbol info from executable"));
58
59static cl::alias A0("s", cl::desc("Alias for --strip-all"),
60 cl::aliasopt(Strip));
61
62static cl::opt<bool> StripDebug("strip-debug",
63 cl::desc("Strip debugger symbol info from executable"));
64
65static cl::alias A1("S", cl::desc("Alias for --strip-debug"),
66 cl::aliasopt(StripDebug));
67
Reid Spencerc457fbb2004-11-20 19:43:28 +000068// A utility function that adds a pass to the pass manager but will also add
69// a verifier pass after if we're supposed to verify.
70static inline void addPass(PassManager &PM, Pass *P) {
71 // Add the pass to the pass manager...
72 PM.add(P);
Misha Brukman3da94ae2005-04-22 00:00:37 +000073
Reid Spencerc457fbb2004-11-20 19:43:28 +000074 // If we are verifying all of the intermediate steps, add the verifier...
Reid Spencerec9f0502006-08-20 20:48:44 +000075 if (VerifyEach)
Reid Spencerc457fbb2004-11-20 19:43:28 +000076 PM.add(createVerifierPass());
77}
78
79namespace llvm {
80
Misha Brukman3da94ae2005-04-22 00:00:37 +000081/// Optimize - Perform link time optimizations. This will run the scalar
82/// optimizations, any loaded plugin-optimization modules, and then the
Reid Spencerc457fbb2004-11-20 19:43:28 +000083/// inter-procedural optimizations if applicable.
84void Optimize(Module* M) {
85
86 // Instantiate the pass manager to organize the passes.
87 PassManager Passes;
88
89 // If we're verifying, start off with a verification pass.
Reid Spencerec9f0502006-08-20 20:48:44 +000090 if (VerifyEach)
Reid Spencerc457fbb2004-11-20 19:43:28 +000091 Passes.add(createVerifierPass());
92
93 // Add an appropriate TargetData instance for this module...
Chris Lattner831b1212006-06-16 18:23:49 +000094 addPass(Passes, new TargetData(M));
Reid Spencerc457fbb2004-11-20 19:43:28 +000095
Daniel Dunbar006a0342009-06-03 21:06:14 +000096 if (!DisableOptimizations)
97 createStandardLTOPasses(&Passes, !DisableInternalize, !DisableInline,
Daniel Dunbare0f0e0b2009-06-03 21:51:32 +000098 VerifyEach);
Reid Spencerc457fbb2004-11-20 19:43:28 +000099
Reid Spencer7f04c082007-02-08 18:13:59 +0000100 // If the -s or -S command line options were specified, strip the symbols out
101 // of the resulting program to make it smaller. -s and -S are GNU ld options
102 // that we are supporting; they alias -strip-all and -strip-debug.
103 if (Strip || StripDebug)
104 addPass(Passes, createStripSymbolsPass(StripDebug && !Strip));
105
Reid Spencer8e33fae2006-08-20 19:18:36 +0000106 // Create a new optimization pass for each one specified on the command line
107 std::auto_ptr<TargetMachine> target;
108 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
109 const PassInfo *Opt = OptimizationList[i];
Reid Spencer8e33fae2006-08-20 19:18:36 +0000110 if (Opt->getNormalCtor())
Reid Spencerdcee9d02006-08-20 20:54:38 +0000111 addPass(Passes, Opt->getNormalCtor()());
Chris Lattnercd950a52006-12-01 21:59:37 +0000112 else
Dan Gohman65f57c22009-07-15 16:35:29 +0000113 errs() << "llvm-ld: cannot create pass: " << Opt->getPassName()
114 << "\n";
Reid Spencer8e33fae2006-08-20 19:18:36 +0000115 }
116
117 // The user's passes may leave cruft around. Clean up after them them but
118 // only if we haven't got DisableOptimizations set
119 if (!DisableOptimizations) {
120 addPass(Passes, createInstructionCombiningPass());
121 addPass(Passes, createCFGSimplificationPass());
Owen Anderson735e6ea2008-07-02 18:42:07 +0000122 addPass(Passes, createAggressiveDCEPass());
Reid Spencer8e33fae2006-08-20 19:18:36 +0000123 addPass(Passes, createGlobalDCEPass());
Reid Spencerc457fbb2004-11-20 19:43:28 +0000124 }
125
126 // Make sure everything is still good.
Andrew Lenharth230293b2008-03-19 22:32:43 +0000127 if (!DontVerify)
Andrew Lenharth50b57422008-03-19 20:49:51 +0000128 Passes.add(createVerifierPass());
Reid Spencerc457fbb2004-11-20 19:43:28 +0000129
130 // Run our queue of passes all at once now, efficiently.
131 Passes.run(*M);
132}
133
134}