blob: ff19942ead890931382747c6f4d5f0ed7975fd4a [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
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// Optimizations may be specified an arbitrary number of times on the command
11// line, They are run in the order specified.
12//
13//===----------------------------------------------------------------------===//
14
Owen Anderson25209b42009-07-01 16:58:40 +000015#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000016#include "llvm/Module.h"
17#include "llvm/PassManager.h"
18#include "llvm/CallGraphSCCPass.h"
19#include "llvm/Bitcode/ReaderWriter.h"
20#include "llvm/Assembly/PrintModulePass.h"
21#include "llvm/Analysis/Verifier.h"
22#include "llvm/Analysis/LoopPass.h"
23#include "llvm/Analysis/CallGraph.h"
24#include "llvm/Target/TargetData.h"
25#include "llvm/Target/TargetMachine.h"
26#include "llvm/Support/PassNameParser.h"
27#include "llvm/System/Signals.h"
David Greene57873752010-01-05 01:30:32 +000028#include "llvm/Support/Debug.h"
Dan Gohmanfb174702009-09-03 16:00:08 +000029#include "llvm/Support/IRReader.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Support/ManagedStatic.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/Support/PluginLoader.h"
Chris Lattnercb8c7752009-12-09 00:41:28 +000032#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbarcae51cb2009-06-03 18:22:15 +000033#include "llvm/Support/StandardPasses.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/Support/SystemUtils.h"
Daniel Dunbar3b475e92008-10-22 03:25:22 +000035#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/LinkAllPasses.h"
37#include "llvm/LinkAllVMCore.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include <memory>
39#include <algorithm>
40using namespace llvm;
41
42// The OptimizationList is automatically populated with registered Passes by the
43// PassNameParser.
44//
45static cl::list<const PassInfo*, bool, PassNameParser>
46PassList(cl::desc("Optimizations available:"));
47
48// Other command line options...
49//
50static cl::opt<std::string>
Eric Christopherdc019192009-08-21 23:29:40 +000051InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 cl::init("-"), cl::value_desc("filename"));
53
54static cl::opt<std::string>
55OutputFilename("o", cl::desc("Override output filename"),
56 cl::value_desc("filename"), cl::init("-"));
57
58static cl::opt<bool>
Dan Gohman176426d2009-08-25 15:34:52 +000059Force("f", cl::desc("Enable binary output on terminals"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060
61static cl::opt<bool>
62PrintEachXForm("p", cl::desc("Print module after each transformation"));
63
64static cl::opt<bool>
65NoOutput("disable-output",
66 cl::desc("Do not write result bitcode file"), cl::Hidden);
67
68static cl::opt<bool>
Duncan Sandse3d726a2009-10-14 20:01:39 +000069OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
Daniel Dunbar1a34ea62009-09-05 11:34:53 +000070
71static cl::opt<bool>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
73
74static cl::opt<bool>
75VerifyEach("verify-each", cl::desc("Verify after each transform"));
76
77static cl::opt<bool>
78StripDebug("strip-debug",
79 cl::desc("Strip debugger symbol info from translation unit"));
80
81static cl::opt<bool>
82DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
83
Eric Christopherdc019192009-08-21 23:29:40 +000084static cl::opt<bool>
85DisableOptimizations("disable-opt",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 cl::desc("Do not run any optimization passes"));
87
Eric Christopherdc019192009-08-21 23:29:40 +000088static cl::opt<bool>
Daniel Dunbar53459d72009-07-17 18:09:39 +000089DisableInternalize("disable-internalize",
90 cl::desc("Do not mark all symbols as internal"));
91
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092static cl::opt<bool>
Eric Christopherdc019192009-08-21 23:29:40 +000093StandardCompileOpts("std-compile-opts",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 cl::desc("Include the standard compile time optimizations"));
95
96static cl::opt<bool>
Eric Christopherdc019192009-08-21 23:29:40 +000097StandardLinkOpts("std-link-opts",
Daniel Dunbar53459d72009-07-17 18:09:39 +000098 cl::desc("Include the standard link time optimizations"));
99
100static cl::opt<bool>
Devang Patel14d07602008-09-16 22:25:14 +0000101OptLevelO1("O1",
102 cl::desc("Optimization level 1. Similar to llvm-gcc -O1"));
103
104static cl::opt<bool>
105OptLevelO2("O2",
Devang Pateld02ad902008-09-17 00:01:04 +0000106 cl::desc("Optimization level 2. Similar to llvm-gcc -O2"));
Devang Patel14d07602008-09-16 22:25:14 +0000107
108static cl::opt<bool>
109OptLevelO3("O3",
Devang Pateld02ad902008-09-17 00:01:04 +0000110 cl::desc("Optimization level 3. Similar to llvm-gcc -O3"));
Devang Patel14d07602008-09-16 22:25:14 +0000111
112static cl::opt<bool>
113UnitAtATime("funit-at-a-time",
Eric Christopher8ad4add2009-08-21 23:30:30 +0000114 cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
115 cl::init(true));
Devang Patel14d07602008-09-16 22:25:14 +0000116
117static cl::opt<bool>
118DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel8dba36c2008-09-17 16:01:39 +0000119 cl::desc("Disable simplify-libcalls"));
Devang Patel14d07602008-09-16 22:25:14 +0000120
121static cl::opt<bool>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
123
124static cl::alias
125QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
126
127static cl::opt<bool>
128AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
129
Chris Lattnercd0702f2009-10-22 00:44:10 +0000130static cl::opt<std::string>
131DefaultDataLayout("default-data-layout",
132 cl::desc("data layout string to use if not specified by module"),
133 cl::value_desc("layout-string"), cl::init(""));
134
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135// ---------- Define Printers for module and function passes ------------
136namespace {
137
138struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
139 static char ID;
140 const PassInfo *PassToPrint;
Eric Christopherdc019192009-08-21 23:29:40 +0000141 CallGraphSCCPassPrinter(const PassInfo *PI) :
Dan Gohmanc74a1972009-02-18 05:09:16 +0000142 CallGraphSCCPass(&ID), PassToPrint(PI) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143
Chris Lattnerc9976c72010-04-16 22:42:17 +0000144 virtual bool runOnSCC(CallGraphSCC &SCC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000146 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147
Chris Lattnerc9976c72010-04-16 22:42:17 +0000148 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
149 Function *F = (*I)->getFunction();
150 if (F)
Chris Lattner397f4562009-08-23 06:03:38 +0000151 getAnalysisID<Pass>(PassToPrint).print(outs(), F->getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 }
153 }
154 // Get and print pass...
155 return false;
156 }
Eric Christopherdc019192009-08-21 23:29:40 +0000157
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 virtual const char *getPassName() const { return "'Pass' Printer"; }
159
160 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
161 AU.addRequiredID(PassToPrint);
162 AU.setPreservesAll();
163 }
164};
165
166char CallGraphSCCPassPrinter::ID = 0;
167
168struct ModulePassPrinter : public ModulePass {
169 static char ID;
170 const PassInfo *PassToPrint;
Dan Gohmanc74a1972009-02-18 05:09:16 +0000171 ModulePassPrinter(const PassInfo *PI) : ModulePass(&ID),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 PassToPrint(PI) {}
173
174 virtual bool runOnModule(Module &M) {
175 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000176 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Chris Lattner397f4562009-08-23 06:03:38 +0000177 getAnalysisID<Pass>(PassToPrint).print(outs(), &M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 }
179
180 // Get and print pass...
181 return false;
182 }
183
184 virtual const char *getPassName() const { return "'Pass' Printer"; }
185
186 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
187 AU.addRequiredID(PassToPrint);
188 AU.setPreservesAll();
189 }
190};
191
192char ModulePassPrinter::ID = 0;
193struct FunctionPassPrinter : public FunctionPass {
194 const PassInfo *PassToPrint;
195 static char ID;
Dan Gohmanc74a1972009-02-18 05:09:16 +0000196 FunctionPassPrinter(const PassInfo *PI) : FunctionPass(&ID),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 PassToPrint(PI) {}
198
199 virtual bool runOnFunction(Function &F) {
Eric Christopherdc019192009-08-21 23:29:40 +0000200 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000201 outs() << "Printing analysis '" << PassToPrint->getPassName()
202 << "' for function '" << F.getName() << "':\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 }
204 // Get and print pass...
Chris Lattner397f4562009-08-23 06:03:38 +0000205 getAnalysisID<Pass>(PassToPrint).print(outs(), F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 return false;
207 }
208
209 virtual const char *getPassName() const { return "FunctionPass Printer"; }
210
211 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
212 AU.addRequiredID(PassToPrint);
213 AU.setPreservesAll();
214 }
215};
216
217char FunctionPassPrinter::ID = 0;
218
219struct LoopPassPrinter : public LoopPass {
220 static char ID;
221 const PassInfo *PassToPrint;
Eric Christopherdc019192009-08-21 23:29:40 +0000222 LoopPassPrinter(const PassInfo *PI) :
Dan Gohmanc74a1972009-02-18 05:09:16 +0000223 LoopPass(&ID), PassToPrint(PI) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224
225 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
226 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000227 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Chris Lattner397f4562009-08-23 06:03:38 +0000228 getAnalysisID<Pass>(PassToPrint).print(outs(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 L->getHeader()->getParent()->getParent());
230 }
231 // Get and print pass...
232 return false;
233 }
Eric Christopherdc019192009-08-21 23:29:40 +0000234
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 virtual const char *getPassName() const { return "'Pass' Printer"; }
236
237 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
238 AU.addRequiredID(PassToPrint);
239 AU.setPreservesAll();
240 }
241};
242
243char LoopPassPrinter::ID = 0;
244
245struct BasicBlockPassPrinter : public BasicBlockPass {
246 const PassInfo *PassToPrint;
247 static char ID;
Eric Christopherdc019192009-08-21 23:29:40 +0000248 BasicBlockPassPrinter(const PassInfo *PI)
Dan Gohmanc74a1972009-02-18 05:09:16 +0000249 : BasicBlockPass(&ID), PassToPrint(PI) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250
251 virtual bool runOnBasicBlock(BasicBlock &BB) {
252 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000253 outs() << "Printing Analysis info for BasicBlock '" << BB.getName()
254 << "': Pass " << PassToPrint->getPassName() << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 }
256
257 // Get and print pass...
Chris Lattner397f4562009-08-23 06:03:38 +0000258 getAnalysisID<Pass>(PassToPrint).print(outs(), BB.getParent()->getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 return false;
260 }
261
262 virtual const char *getPassName() const { return "BasicBlockPass Printer"; }
263
264 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
265 AU.addRequiredID(PassToPrint);
266 AU.setPreservesAll();
267 }
268};
269
270char BasicBlockPassPrinter::ID = 0;
271inline void addPass(PassManager &PM, Pass *P) {
272 // Add the pass to the pass manager...
273 PM.add(P);
274
275 // If we are verifying all of the intermediate steps, add the verifier...
276 if (VerifyEach) PM.add(createVerifierPass());
277}
278
Eric Christopherdc019192009-08-21 23:29:40 +0000279/// AddOptimizationPasses - This routine adds optimization passes
280/// based on selected optimization level, OptLevel. This routine
Devang Patel14d07602008-09-16 22:25:14 +0000281/// duplicates llvm-gcc behaviour.
282///
283/// OptLevel - Optimization Level
Zhongxing Xu42854c82008-11-26 02:57:24 +0000284void AddOptimizationPasses(PassManager &MPM, FunctionPassManager &FPM,
285 unsigned OptLevel) {
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000286 createStandardFunctionPasses(&FPM, OptLevel);
Devang Patel14d07602008-09-16 22:25:14 +0000287
Eli Friedmanabf44e32010-01-18 22:38:31 +0000288 llvm::Pass *InliningPass = 0;
289 if (DisableInline) {
290 // No inlining pass
291 } else if (OptLevel) {
292 unsigned Threshold = 200;
293 if (OptLevel > 2)
294 Threshold = 250;
295 InliningPass = createFunctionInliningPass(Threshold);
296 } else {
297 InliningPass = createAlwaysInlinerPass();
298 }
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000299 createStandardModulePasses(&MPM, OptLevel,
300 /*OptimizeSize=*/ false,
301 UnitAtATime,
302 /*UnrollLoops=*/ OptLevel > 1,
303 !DisableSimplifyLibCalls,
304 /*HaveExceptions=*/ true,
305 InliningPass);
Devang Patel14d07602008-09-16 22:25:14 +0000306}
307
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308void AddStandardCompilePasses(PassManager &PM) {
309 PM.add(createVerifierPass()); // Verify that input is correct
310
311 addPass(PM, createLowerSetJmpPass()); // Lower llvm.setjmp/.longjmp
312
313 // If the -strip-debug command line option was specified, do it.
314 if (StripDebug)
315 addPass(PM, createStripSymbolsPass(true));
316
317 if (DisableOptimizations) return;
318
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000319 llvm::Pass *InliningPass = !DisableInline ? createFunctionInliningPass() : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000321 // -std-compile-opts adds the same module passes as -O3.
Eric Christopherdc019192009-08-21 23:29:40 +0000322 createStandardModulePasses(&PM, 3,
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000323 /*OptimizeSize=*/ false,
324 /*UnitAtATime=*/ true,
325 /*UnrollLoops=*/ true,
326 /*SimplifyLibCalls=*/ true,
327 /*HaveExceptions=*/ true,
328 InliningPass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329}
330
Daniel Dunbar53459d72009-07-17 18:09:39 +0000331void AddStandardLinkPasses(PassManager &PM) {
332 PM.add(createVerifierPass()); // Verify that input is correct
333
334 // If the -strip-debug command line option was specified, do it.
335 if (StripDebug)
336 addPass(PM, createStripSymbolsPass(true));
337
338 if (DisableOptimizations) return;
339
340 createStandardLTOPasses(&PM, /*Internalize=*/ !DisableInternalize,
341 /*RunInliner=*/ !DisableInline,
342 /*VerifyEach=*/ VerifyEach);
343}
344
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345} // anonymous namespace
346
347
348//===----------------------------------------------------------------------===//
349// main for opt
350//
351int main(int argc, char **argv) {
Chris Lattnercb8c7752009-12-09 00:41:28 +0000352 sys::PrintStackTraceOnErrorSignal();
353 llvm::PrettyStackTraceProgram X(argc, argv);
354
David Greene57873752010-01-05 01:30:32 +0000355 // Enable debug stream buffering.
356 EnableDebugBuffering = true;
357
Chris Lattnercb8c7752009-12-09 00:41:28 +0000358 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Owen Andersone84b8b32009-07-15 22:16:10 +0000359 LLVMContext &Context = getGlobalContext();
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000360
361 cl::ParseCommandLineOptions(argc, argv,
362 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000364 // Allocate a full target machine description only if necessary.
365 // FIXME: The choice of target should be controllable on the command line.
366 std::auto_ptr<TargetMachine> target;
Devang Patelf6a112f2008-08-27 20:51:49 +0000367
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000368 SMDiagnostic Err;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000370 // Load the input module...
371 std::auto_ptr<Module> M;
372 M.reset(ParseIRFile(InputFilename, Err, Context));
Eric Christopherdc019192009-08-21 23:29:40 +0000373
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000374 if (M.get() == 0) {
375 Err.Print(argv[0], errs());
376 return 1;
377 }
378
379 // Figure out what stream we are supposed to write to...
380 // FIXME: outs() is not binary!
381 raw_ostream *Out = &outs(); // Default to printing to stdout...
382 if (OutputFilename != "-") {
Dan Gohman63bf7682010-01-17 17:47:24 +0000383 if (NoOutput || AnalyzeOnly) {
384 errs() << "WARNING: The -o (output filename) option is ignored when\n"
385 "the --disable-output or --analyze options are used.\n";
386 } else {
387 // Make sure that the Output file gets unlinked from the disk if we get a
388 // SIGINT
389 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000390
Dan Gohman63bf7682010-01-17 17:47:24 +0000391 std::string ErrorInfo;
392 Out = new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
393 raw_fd_ostream::F_Binary);
394 if (!ErrorInfo.empty()) {
395 errs() << ErrorInfo << '\n';
396 delete Out;
397 return 1;
398 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 }
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000400 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000402 // If the output is set to be emitted to standard out, and standard out is a
403 // console, print out a warning message and refuse to do it. We don't
404 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohman63bf7682010-01-17 17:47:24 +0000405 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000406 if (CheckBitcodeOutputToConsole(*Out, !Quiet))
407 NoOutput = true;
Dan Gohman31b20c72009-09-11 20:46:33 +0000408
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000409 // Create a PassManager to hold and optimize the collection of passes we are
410 // about to build...
411 //
412 PassManager Passes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000414 // Add an appropriate TargetData instance for this module...
415 TargetData *TD = 0;
416 const std::string &ModuleDataLayout = M.get()->getDataLayout();
417 if (!ModuleDataLayout.empty())
418 TD = new TargetData(ModuleDataLayout);
Kenneth Uildriksa092c122009-11-03 15:29:06 +0000419 else if (!DefaultDataLayout.empty())
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000420 TD = new TargetData(DefaultDataLayout);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000422 if (TD)
423 Passes.add(TD);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424
Jeffrey Yasskin12015bd2010-03-22 15:56:04 +0000425 OwningPtr<FunctionPassManager> FPasses;
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000426 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
Jeffrey Yasskin12015bd2010-03-22 15:56:04 +0000427 FPasses.reset(new FunctionPassManager(M.get()));
Chris Lattnercd0702f2009-10-22 00:44:10 +0000428 if (TD)
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000429 FPasses->add(new TargetData(*TD));
430 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000432 // If the -strip-debug command line option was specified, add it. If
433 // -std-compile-opts was also specified, it will handle StripDebug.
434 if (StripDebug && !StandardCompileOpts)
435 addPass(Passes, createStripSymbolsPass(true));
Eric Christopherdc019192009-08-21 23:29:40 +0000436
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000437 // Create a new optimization pass for each one specified on the command line
438 for (unsigned i = 0; i < PassList.size(); ++i) {
439 // Check to see if -std-compile-opts was specified before this option. If
440 // so, handle it.
441 if (StandardCompileOpts &&
442 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
Chris Lattnerfe3cc652008-07-13 19:35:21 +0000443 AddStandardCompilePasses(Passes);
444 StandardCompileOpts = false;
Eric Christopherdc019192009-08-21 23:29:40 +0000445 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000447 if (StandardLinkOpts &&
448 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar53459d72009-07-17 18:09:39 +0000449 AddStandardLinkPasses(Passes);
450 StandardLinkOpts = false;
Eric Christopherdc019192009-08-21 23:29:40 +0000451 }
Daniel Dunbar53459d72009-07-17 18:09:39 +0000452
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000453 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar53459d72009-07-17 18:09:39 +0000454 AddOptimizationPasses(Passes, *FPasses, 1);
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000455 OptLevelO1 = false;
Daniel Dunbar53459d72009-07-17 18:09:39 +0000456 }
Devang Patel14d07602008-09-16 22:25:14 +0000457
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000458 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar53459d72009-07-17 18:09:39 +0000459 AddOptimizationPasses(Passes, *FPasses, 2);
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000460 OptLevelO2 = false;
Daniel Dunbar53459d72009-07-17 18:09:39 +0000461 }
Devang Patel14d07602008-09-16 22:25:14 +0000462
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000463 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar53459d72009-07-17 18:09:39 +0000464 AddOptimizationPasses(Passes, *FPasses, 3);
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000465 OptLevelO3 = false;
Daniel Dunbar53459d72009-07-17 18:09:39 +0000466 }
Devang Patel14d07602008-09-16 22:25:14 +0000467
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000468 const PassInfo *PassInf = PassList[i];
469 Pass *P = 0;
470 if (PassInf->getNormalCtor())
471 P = PassInf->getNormalCtor()();
472 else
473 errs() << argv[0] << ": cannot create pass: "
474 << PassInf->getPassName() << "\n";
475 if (P) {
Benjamin Kramerd6722cc2010-02-18 12:57:05 +0000476 PassKind Kind = P->getPassKind();
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000477 addPass(Passes, P);
478
479 if (AnalyzeOnly) {
Benjamin Kramerd6722cc2010-02-18 12:57:05 +0000480 switch (Kind) {
Chris Lattnerf6241902010-01-22 06:03:06 +0000481 case PT_BasicBlock:
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000482 Passes.add(new BasicBlockPassPrinter(PassInf));
Chris Lattnerf6241902010-01-22 06:03:06 +0000483 break;
484 case PT_Loop:
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000485 Passes.add(new LoopPassPrinter(PassInf));
Chris Lattnerf6241902010-01-22 06:03:06 +0000486 break;
487 case PT_Function:
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000488 Passes.add(new FunctionPassPrinter(PassInf));
Chris Lattnerf6241902010-01-22 06:03:06 +0000489 break;
490 case PT_CallGraphSCC:
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000491 Passes.add(new CallGraphSCCPassPrinter(PassInf));
Chris Lattnerf6241902010-01-22 06:03:06 +0000492 break;
493 default:
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000494 Passes.add(new ModulePassPrinter(PassInf));
Chris Lattnerf6241902010-01-22 06:03:06 +0000495 break;
496 }
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000497 }
Devang Patel14d07602008-09-16 22:25:14 +0000498 }
499
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000500 if (PrintEachXForm)
501 Passes.add(createPrintModulePass(&errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502 }
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000503
504 // If -std-compile-opts was specified at the end of the pass list, add them.
505 if (StandardCompileOpts) {
506 AddStandardCompilePasses(Passes);
507 StandardCompileOpts = false;
508 }
509
510 if (StandardLinkOpts) {
511 AddStandardLinkPasses(Passes);
512 StandardLinkOpts = false;
513 }
514
515 if (OptLevelO1)
516 AddOptimizationPasses(Passes, *FPasses, 1);
517
518 if (OptLevelO2)
519 AddOptimizationPasses(Passes, *FPasses, 2);
520
521 if (OptLevelO3)
522 AddOptimizationPasses(Passes, *FPasses, 3);
523
524 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
525 FPasses->doInitialization();
526 for (Module::iterator I = M.get()->begin(), E = M.get()->end();
527 I != E; ++I)
528 FPasses->run(*I);
529 }
530
531 // Check that the module is well formed on completion of optimization
532 if (!NoVerify && !VerifyEach)
533 Passes.add(createVerifierPass());
534
Dan Gohman63bf7682010-01-17 17:47:24 +0000535 // Write bitcode or assembly out to disk or outs() as the last step...
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000536 if (!NoOutput && !AnalyzeOnly) {
537 if (OutputAssembly)
538 Passes.add(createPrintModulePass(Out));
539 else
540 Passes.add(createBitcodeWriterPass(*Out));
541 }
542
543 // Now that we have all of the passes ready, run them.
544 Passes.run(*M.get());
545
546 // Delete the raw_fd_ostream.
547 if (Out != &outs())
548 delete Out;
549 return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550}